blob: 753cdcddd28c3f78e17b893c3928f062ec6174bc [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
Devang Patel6e6bc392010-07-23 23:04:28 +0000191 llvm::sys::Path AbsFileName(MainFileName);
192 AbsFileName.makeAbsolute();
193
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000194 // The main file name provided via the "-main-file-name" option contains just
195 // the file name itself with no path information. This file name may have had
196 // a relative path, so we look into the actual file entry for the main
197 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000198 std::string MainFileDir;
199 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000200 MainFileDir = MainFile->getDir()->getName();
Devang Patel6e6bc392010-07-23 23:04:28 +0000201 else
202 MainFileDir = AbsFileName.getDirname();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000203
Chris Lattner515455a2009-03-25 03:28:08 +0000204 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000205 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000206 if (LO.CPlusPlus) {
207 if (LO.ObjC1)
208 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
209 else
210 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
211 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000212 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000213 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000214 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000215 } else {
216 LangTag = llvm::dwarf::DW_LANG_C89;
217 }
Devang Patel446c6192009-04-17 21:06:59 +0000218
Benjamin Kramer47daf682009-12-08 11:02:29 +0000219 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000220#ifdef CLANG_VENDOR
221 CLANG_VENDOR
222#endif
223 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000224
225 // Figure out which version of the ObjC runtime we have.
226 unsigned RuntimeVers = 0;
227 if (LO.ObjC1)
228 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000230 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000231 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel6e6bc392010-07-23 23:04:28 +0000232 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000233 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000234}
235
Devang Patel65e99f22009-02-25 01:36:11 +0000236/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000237/// one if necessary.
238llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000239 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000240 unsigned Encoding = 0;
241 switch (BT->getKind()) {
242 default:
243 case BuiltinType::Void:
244 return llvm::DIType();
Devang Pateleaf5ee92010-07-21 22:41:25 +0000245 case BuiltinType::ObjCId:
246 // id is struct objc_object *.
247 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
248 Unit, "objc_object", Unit, 0, 0, 0, 0,
249 llvm::DIType::FlagFwdDecl,
250 llvm::DIType(), llvm::DIArray());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000251 case BuiltinType::UChar:
252 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
253 case BuiltinType::Char_S:
254 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
255 case BuiltinType::UShort:
256 case BuiltinType::UInt:
257 case BuiltinType::ULong:
258 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
259 case BuiltinType::Short:
260 case BuiltinType::Int:
261 case BuiltinType::Long:
262 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
263 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
264 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000265 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000266 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000267 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000269 uint64_t Size = CGM.getContext().getTypeSize(BT);
270 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Devang Patelca80a5f2009-10-20 19:55:01 +0000273 llvm::DIType DbgTy =
274 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000275 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000276 Unit, 0, Size, Align,
277 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000278 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000279}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000280
Chris Lattnerb7003772009-04-23 06:13:01 +0000281llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000282 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000283 // Bit size, align and offset of the type.
284 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
285 if (Ty->isComplexIntegerType())
286 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Anders Carlsson20f12a22009-12-06 18:00:51 +0000288 uint64_t Size = CGM.getContext().getTypeSize(Ty);
289 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000290 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000291
Devang Patelca80a5f2009-10-20 19:55:01 +0000292 llvm::DIType DbgTy =
293 DebugFactory.CreateBasicType(Unit, "complex",
294 Unit, 0, Size, Align,
295 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000296 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000297}
298
John McCalla1805292009-09-25 01:40:47 +0000299/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000300/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000301llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000302 QualifierCollector Qc;
303 const Type *T = Qc.strip(Ty);
304
305 // Ignore these qualifiers for now.
306 Qc.removeObjCGCAttr();
307 Qc.removeAddressSpace();
308
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 // We will create one Derived type for one qualifier and recurse to handle any
310 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000311 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000312 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000314 Qc.removeConst();
315 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000316 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000317 Qc.removeVolatile();
318 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000319 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000320 Qc.removeRestrict();
321 } else {
322 assert(Qc.empty() && "Unknown type qualifier for debug info");
323 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
John McCalla1805292009-09-25 01:40:47 +0000326 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
327
Daniel Dunbar3845f862008-10-31 03:54:29 +0000328 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
329 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000330 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000331 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000332 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000333 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000334}
335
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000336llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000337 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000338 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000339 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
340 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000342}
343
Chris Lattner9c85ba32008-11-10 06:08:34 +0000344llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000345 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000346 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
347 Ty->getPointeeType(), Unit);
348}
349
350llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
351 const Type *Ty,
352 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000353 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000354 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000356 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000357
358 // Size is always the size of a pointer. We can't use getTypeSize here
359 // because that does not return the correct value for references.
360 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000361 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
362 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Devang Patelca80a5f2009-10-20 19:55:01 +0000364 return
Devang Pateld58562e2010-03-09 22:49:11 +0000365 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000366 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000367
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000368}
369
Mike Stump9bc093c2009-05-14 02:03:51 +0000370llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000371 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000372 if (BlockLiteralGenericSet)
373 return BlockLiteralGeneric;
374
Mike Stump9bc093c2009-05-14 02:03:51 +0000375 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
376
377 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
378
379 llvm::DIType FieldTy;
380
381 QualType FType;
382 uint64_t FieldSize, FieldOffset;
383 unsigned FieldAlign;
384
385 llvm::DIArray Elements;
386 llvm::DIType EltTy, DescTy;
387
388 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000389 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000390 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
391 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000392
Daniel Dunbarca308df2009-05-26 19:40:20 +0000393 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000394 EltTys.clear();
395
Mike Stump3d363c52009-10-02 02:30:50 +0000396 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000397 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000398
Mike Stump9bc093c2009-05-14 02:03:51 +0000399 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000400 Unit, LineNo, FieldOffset, 0, 0,
401 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Mike Stump9bc093c2009-05-14 02:03:51 +0000403 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000404 uint64_t Size = CGM.getContext().getTypeSize(Ty);
405 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Mike Stump9bc093c2009-05-14 02:03:51 +0000407 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000408 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000409 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000410
411 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000412 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000413 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000414 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000415 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
416 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000417 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000418 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000419
Anders Carlsson20f12a22009-12-06 18:00:51 +0000420 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000421 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000422 FieldSize = CGM.getContext().getTypeSize(Ty);
423 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000424 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000425 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000426 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000427 FieldOffset, 0, FieldTy);
428 EltTys.push_back(FieldTy);
429
430 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000431 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000432
433 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000434 Unit, LineNo, FieldOffset, 0, 0,
435 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Mike Stump9bc093c2009-05-14 02:03:51 +0000437 BlockLiteralGenericSet = true;
438 BlockLiteralGeneric
439 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000440 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000441 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000442 return BlockLiteralGeneric;
443}
444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000446 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 // Typedefs are derived from some other type. If we have a typedef of a
448 // typedef, make sure to emit the whole chain.
449 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner9c85ba32008-11-10 06:08:34 +0000451 // We don't set size information, but do specify where the typedef was
452 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000453 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000454
Devang Pateleb6d79b2010-02-01 21:34:11 +0000455 llvm::DIDescriptor TyContext
456 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
457 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000458 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000459 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000460 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000461 Ty->getDecl()->getName(), Unit,
462 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000463 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000464}
465
Chris Lattner9c85ba32008-11-10 06:08:34 +0000466llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000467 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000468 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000469
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 // Add the result type at least.
471 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner9c85ba32008-11-10 06:08:34 +0000473 // Set up remainder of arguments if there is a prototype.
474 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000475 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
477 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
478 } else {
479 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000480 }
481
Chris Lattner9c85ba32008-11-10 06:08:34 +0000482 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000483 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Devang Patelca80a5f2009-10-20 19:55:01 +0000485 llvm::DIType DbgTy =
486 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000487 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000488 0, 0, 0, 0, 0,
489 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000490 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000491}
492
Devang Patel428deb52010-01-19 00:00:59 +0000493/// CollectRecordFields - A helper function to collect debug info for
494/// record fields. This is used while creating debug info entry for a Record.
495void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000496CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000497 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
498 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000499 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
500 for (RecordDecl::field_iterator I = RD->field_begin(),
501 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000502 I != E; ++I, ++FieldNo) {
503 FieldDecl *Field = *I;
504 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
505
506 llvm::StringRef FieldName = Field->getName();
507
Devang Patel4835fdd2010-02-12 01:31:06 +0000508 // Ignore unnamed fields. Do not ignore unnamed records.
509 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000510 continue;
511
512 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000513 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
514 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000515 QualType FType = Field->getType();
516 uint64_t FieldSize = 0;
517 unsigned FieldAlign = 0;
518 if (!FType->isIncompleteArrayType()) {
519
520 // Bit size, align and offset of the type.
521 FieldSize = CGM.getContext().getTypeSize(FType);
522 Expr *BitWidth = Field->getBitWidth();
523 if (BitWidth)
524 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
525
526 FieldAlign = CGM.getContext().getTypeAlign(FType);
527 }
528
529 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
530
Devang Patel71f4ff62010-04-21 23:12:37 +0000531 unsigned Flags = 0;
532 AccessSpecifier Access = I->getAccess();
533 if (Access == clang::AS_private)
534 Flags |= llvm::DIType::FlagPrivate;
535 else if (Access == clang::AS_protected)
536 Flags |= llvm::DIType::FlagProtected;
537
Devang Patel428deb52010-01-19 00:00:59 +0000538 // Create a DW_TAG_member node to remember the offset of this field in the
539 // struct. FIXME: This is an absolutely insane way to capture this
540 // information. When we gut debug info, this should be fixed.
541 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
542 FieldName, FieldDefUnit,
543 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000544 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000545 EltTys.push_back(FieldTy);
546 }
547}
548
Devang Patela6da1922010-01-28 00:28:01 +0000549/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
550/// function type is not updated to include implicit "this" pointer. Use this
551/// routine to get a method type which includes "this" pointer.
552llvm::DIType
553CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000554 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000555 llvm::DIType FnTy
556 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
557 0),
558 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000559
560 // Static methods do not need "this" pointer argument.
561 if (Method->isStatic())
562 return FnTy;
563
Devang Patela6da1922010-01-28 00:28:01 +0000564 // Add "this" pointer.
565
Devang Patelab699792010-05-07 18:12:35 +0000566 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000567 assert (Args.getNumElements() && "Invalid number of arguments!");
568
569 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
570
571 // First element is always return type. For 'void' functions it is NULL.
572 Elts.push_back(Args.getElement(0));
573
574 // "this" pointer is always first argument.
575 ASTContext &Context = CGM.getContext();
576 QualType ThisPtr =
577 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000578 llvm::DIType ThisPtrType =
579 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000580
Devang Patelab699792010-05-07 18:12:35 +0000581 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000582 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000583
584 // Copy rest of the arguments.
585 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
586 Elts.push_back(Args.getElement(i));
587
588 llvm::DIArray EltTypeArray =
589 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
590
591 return
592 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000593 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000594 0, 0, 0, 0, 0,
595 llvm::DIType(), EltTypeArray);
596}
597
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000598/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
599/// a single member function GlobalDecl.
600llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000601CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000602 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000603 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000604 bool IsCtorOrDtor =
605 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
606
607 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000608 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000609
610 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
611 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000612 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000613 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000614 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000615
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000616 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000617 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
618 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000619
620 // Collect virtual method info.
621 llvm::DIType ContainingType;
622 unsigned Virtuality = 0;
623 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000624
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000625 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000626 if (Method->isPure())
627 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
628 else
629 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
630
631 // It doesn't make sense to give a virtual destructor a vtable index,
632 // since a single destructor has two entries in the vtable.
633 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000634 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000635 ContainingType = RecordTy;
636 }
637
638 llvm::DISubprogram SP =
639 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
640 MethodLinkageName,
641 MethodDefUnit, MethodLine,
642 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000643 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000644 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000645 Method->isImplicit(),
646 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000647
648 // Don't cache ctors or dtors since we have to emit multiple functions for
649 // a single ctor or dtor.
650 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000651 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000652
653 return SP;
654}
655
Devang Patel4125fd22010-01-19 01:54:44 +0000656/// CollectCXXMemberFunctions - A helper function to collect debug info for
657/// C++ member functions.This is used while creating debug info entry for
658/// a Record.
659void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000660CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000661 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
662 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000663 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
664 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000665 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000666
Devang Pateld5322da2010-02-09 19:09:28 +0000667 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000668 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000669
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000670 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000671 }
672}
673
Devang Patela245c5b2010-01-25 23:32:18 +0000674/// CollectCXXBases - A helper function to collect debug info for
675/// C++ base classes. This is used while creating debug info entry for
676/// a Record.
677void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000678CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000679 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
680 llvm::DICompositeType &RecordTy) {
681
Devang Patel239cec62010-02-01 21:39:52 +0000682 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
683 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
684 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000685 unsigned BFlags = 0;
686 uint64_t BaseOffset;
687
688 const CXXRecordDecl *Base =
689 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
690
691 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000692 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000693 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000694 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000695 BFlags = llvm::DIType::FlagVirtual;
696 } else
697 BaseOffset = RL.getBaseClassOffset(Base);
698
699 AccessSpecifier Access = BI->getAccessSpecifier();
700 if (Access == clang::AS_private)
701 BFlags |= llvm::DIType::FlagPrivate;
702 else if (Access == clang::AS_protected)
703 BFlags |= llvm::DIType::FlagProtected;
704
705 llvm::DIType DTy =
706 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
707 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000708 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000709 BaseOffset, BFlags,
710 getOrCreateType(BI->getType(),
711 Unit));
712 EltTys.push_back(DTy);
713 }
Devang Patela245c5b2010-01-25 23:32:18 +0000714}
715
Devang Patel4ce3f202010-01-28 18:11:52 +0000716/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000717llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000718 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000719 return VTablePtrType;
720
721 ASTContext &Context = CGM.getContext();
722
723 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000724 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
725 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000726 llvm::DIType SubTy =
727 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000728 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000729 0, 0, 0, 0, 0, llvm::DIType(), SElements);
730
731 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
732 llvm::DIType vtbl_ptr_type
733 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000734 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000735 0, Size, 0, 0, 0, SubTy);
736
Devang Pateld58562e2010-03-09 22:49:11 +0000737 VTablePtrType =
738 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
739 Unit, "", Unit,
740 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000741 return VTablePtrType;
742}
743
Anders Carlsson046c2942010-04-17 20:15:18 +0000744/// getVTableName - Get vtable name for the given Class.
745llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000746 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000747 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000748
749 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000750 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000751 memcpy(StrPtr, Name.data(), Name.length());
752 return llvm::StringRef(StrPtr, Name.length());
753}
754
755
Anders Carlsson046c2942010-04-17 20:15:18 +0000756/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000757/// debug info entry in EltTys vector.
758void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000759CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000760 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000761 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000762
763 // If there is a primary base then it will hold vtable info.
764 if (RL.getPrimaryBase())
765 return;
766
767 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000768 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000769 return;
770
771 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
772 llvm::DIType VPTR
773 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000774 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000775 0, Size, 0, 0, 0,
776 getOrCreateVTablePtrType(Unit));
777 EltTys.push_back(VPTR);
778}
779
Devang Patel65e99f22009-02-25 01:36:11 +0000780/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000781llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000782 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000783 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000786 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000787 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000788 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000789 Tag = llvm::dwarf::DW_TAG_union_type;
790 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000791 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000793 }
794
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000796 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
797 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 // Records and classes and unions can all be recursive. To handle them, we
800 // first generate a debug descriptor for the struct as a forward declaration.
801 // Then (if it is a definition) we go through and get debug info for all of
802 // its members. Finally, we create a descriptor for the complete type (which
803 // may refer to the forward decl if the struct is recursive) and replace all
804 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000805 llvm::DIDescriptor FDContext =
806 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
807
808 // If this is just a forward declaration, construct an appropriately
809 // marked node and just return it.
810 if (!RD->getDefinition()) {
811 llvm::DICompositeType FwdDecl =
812 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
813 DefUnit, Line, 0, 0, 0,
814 llvm::DIType::FlagFwdDecl,
815 llvm::DIType(), llvm::DIArray());
816
817 return FwdDecl;
818 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000819
Devang Pateld6c5a262010-02-01 21:52:22 +0000820 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000821 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000822 llvm::SmallString<128> FwdDeclName;
823 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000824 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000825 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000826 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Devang Patelab699792010-05-07 18:12:35 +0000829 llvm::MDNode *MN = FwdDecl;
830 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000831 // Otherwise, insert it into the TypeCache so that recursive uses will find
832 // it.
Devang Patelab699792010-05-07 18:12:35 +0000833 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000834 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000835 RegionStack.push_back(FwdDeclNode);
836 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837
838 // Convert all the elements.
839 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
840
Devang Pateld6c5a262010-02-01 21:52:22 +0000841 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000842 if (CXXDecl) {
843 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000844 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000845 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000846 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000847 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000848 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000849 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000850
851 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000852 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000853 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
854 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000855 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000856 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000857 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattner9c85ba32008-11-10 06:08:34 +0000860 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000861 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000862
863 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000864 uint64_t Size = CGM.getContext().getTypeSize(Ty);
865 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Devang Patele4c1ea02010-03-11 20:01:48 +0000867 RegionStack.pop_back();
868 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
869 RegionMap.find(Ty->getDecl());
870 if (RI != RegionMap.end())
871 RegionMap.erase(RI);
872
Devang Patel411894b2010-02-01 22:40:08 +0000873 llvm::DIDescriptor RDContext =
874 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000875
876 llvm::StringRef RDName = RD->getName();
877 // If this is a class, include the template arguments also.
878 if (Tag == llvm::dwarf::DW_TAG_class_type)
879 RDName = getClassName(RD);
880
Devang Patel0ce73f62009-07-22 18:57:00 +0000881 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000882 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000883 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000884 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000885 llvm::DIType(), Elements,
886 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887
888 // Now that we have a real decl for the struct, replace anything using the
889 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000890 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000891 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000892 return RealDecl;
893}
894
John McCallc12c5bb2010-05-15 11:32:37 +0000895/// CreateType - get objective-c object type.
896llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
897 llvm::DIFile Unit) {
898 // Ignore protocols.
899 return getOrCreateType(Ty->getBaseType(), Unit);
900}
901
Devang Patel9ca36b62009-02-26 21:10:26 +0000902/// CreateType - get objective-c interface type.
903llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000904 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000905 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000906 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000907
908 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000909 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000910 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000911 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000912
Devang Patel9ca36b62009-02-26 21:10:26 +0000913 // To handle recursive interface, we
914 // first generate a debug descriptor for the struct as a forward declaration.
915 // Then (if it is a definition) we go through and get debug info for all of
916 // its members. Finally, we create a descriptor for the complete type (which
917 // may refer to the forward decl if the struct is recursive) and replace all
918 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000919 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000920 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000921 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000922 llvm::DIType(), llvm::DIArray(),
923 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Devang Patel9ca36b62009-02-26 21:10:26 +0000925 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000926 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000927 return FwdDecl;
928
Devang Patelab699792010-05-07 18:12:35 +0000929 llvm::MDNode *MN = FwdDecl;
930 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000931 // Otherwise, insert it into the TypeCache so that recursive uses will find
932 // it.
Devang Patelab699792010-05-07 18:12:35 +0000933 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000934 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000935 RegionStack.push_back(FwdDeclNode);
936 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000937
938 // Convert all the elements.
939 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
940
Devang Pateld6c5a262010-02-01 21:52:22 +0000941 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000942 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000943 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000944 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000945 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000946 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000947 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000948 0 /* offset */, 0, SClassTy);
949 EltTys.push_back(InhTag);
950 }
951
Devang Pateld6c5a262010-02-01 21:52:22 +0000952 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000953
954 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000955 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
956 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000957 ObjCIvarDecl *Field = *I;
958 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
959
Devang Patel73621622009-11-25 17:37:31 +0000960 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000961
Devang Patelde135022009-04-27 22:40:36 +0000962 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000963 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000964 continue;
965
Devang Patel9ca36b62009-02-26 21:10:26 +0000966 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000967 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
968 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000969 QualType FType = Field->getType();
970 uint64_t FieldSize = 0;
971 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000972
Devang Patel99c20eb2009-03-20 18:24:39 +0000973 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Devang Patel99c20eb2009-03-20 18:24:39 +0000975 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000976 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000977 Expr *BitWidth = Field->getBitWidth();
978 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000979 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000980
Anders Carlsson20f12a22009-12-06 18:00:51 +0000981 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000982 }
983
Mike Stump1eb44332009-09-09 15:08:12 +0000984 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
985
Devang Patelc20482b2009-03-19 00:23:53 +0000986 unsigned Flags = 0;
987 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
988 Flags = llvm::DIType::FlagProtected;
989 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
990 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Devang Patel9ca36b62009-02-26 21:10:26 +0000992 // Create a DW_TAG_member node to remember the offset of this field in the
993 // struct. FIXME: This is an absolutely insane way to capture this
994 // information. When we gut debug info, this should be fixed.
995 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
996 FieldName, FieldDefUnit,
997 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000998 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000999 EltTys.push_back(FieldTy);
1000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Devang Patel9ca36b62009-02-26 21:10:26 +00001002 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001003 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001004
Devang Patele4c1ea02010-03-11 20:01:48 +00001005 RegionStack.pop_back();
1006 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1007 RegionMap.find(Ty->getDecl());
1008 if (RI != RegionMap.end())
1009 RegionMap.erase(RI);
1010
Devang Patel9ca36b62009-02-26 21:10:26 +00001011 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001012 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1013 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Devang Patel6c1fddf2009-07-27 18:42:03 +00001015 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001016 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001017 Line, Size, Align, 0, 0, llvm::DIType(),
1018 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001019
1020 // Now that we have a real decl for the struct, replace anything using the
1021 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001022 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001023 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001024
Devang Patel9ca36b62009-02-26 21:10:26 +00001025 return RealDecl;
1026}
1027
Chris Lattner9c85ba32008-11-10 06:08:34 +00001028llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001029 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001030 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001031
1032 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1033
1034 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001035 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001036 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001037 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001038 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001039 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040 }
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Chris Lattner9c85ba32008-11-10 06:08:34 +00001042 // Return a CompositeType for the enum itself.
1043 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001044 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001045
Devang Patel8ab870d2010-05-12 23:46:38 +00001046 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1047 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Chris Lattner9c85ba32008-11-10 06:08:34 +00001049 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001050 uint64_t Size = 0;
1051 unsigned Align = 0;
1052 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001053 Size = CGM.getContext().getTypeSize(Ty);
1054 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Devang Patelca80a5f2009-10-20 19:55:01 +00001057 llvm::DIType DbgTy =
1058 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001059 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001060 Size, Align, 0, 0,
1061 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001062 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001063}
1064
1065llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001066 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1068 return CreateType(RT, Unit);
1069 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1070 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 return llvm::DIType();
1073}
1074
Devang Patel70c23cd2010-02-23 22:59:39 +00001075llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001076 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001077 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1078 uint64_t NumElems = Ty->getNumElements();
1079 if (NumElems > 0)
1080 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001081
Benjamin Kramerad468862010-03-30 11:36:44 +00001082 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1083 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001084
1085 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1086 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1087
1088 return
1089 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001090 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001091 0, Size, Align, 0, 0,
1092 ElementTy, SubscriptArray);
1093}
1094
Chris Lattner9c85ba32008-11-10 06:08:34 +00001095llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001096 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001097 uint64_t Size;
1098 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001099
1100
Nuno Lopes010d5142009-01-28 00:35:17 +00001101 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001102 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001103 Size = 0;
1104 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001105 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001106 } else if (Ty->isIncompleteArrayType()) {
1107 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001108 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001109 } else {
1110 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001111 Size = CGM.getContext().getTypeSize(Ty);
1112 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001113 }
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattner9c85ba32008-11-10 06:08:34 +00001115 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1116 // interior arrays, do we care? Why aren't nested arrays represented the
1117 // obvious/recursive way?
1118 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1119 QualType EltTy(Ty, 0);
1120 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001121 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001122 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001123 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001124 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001125 // FIXME: Verify this is right for VLAs.
1126 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1127 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001128 }
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Chris Lattner9c85ba32008-11-10 06:08:34 +00001130 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001131 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001132
Devang Patelca80a5f2009-10-20 19:55:01 +00001133 llvm::DIType DbgTy =
1134 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001135 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001136 0, Size, Align, 0, 0,
1137 getOrCreateType(EltTy, Unit),
1138 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001139 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001140}
1141
Anders Carlssona031b352009-11-06 19:19:55 +00001142llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001143 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001144 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1145 Ty, Ty->getPointeeType(), Unit);
1146}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001147
Anders Carlsson20f12a22009-12-06 18:00:51 +00001148llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001149 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1151 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1152
1153 if (!Ty->getPointeeType()->isFunctionType()) {
1154 // We have a data member pointer type.
1155 return PointerDiffDITy;
1156 }
1157
1158 // We have a member function pointer type. Treat it as a struct with two
1159 // ptrdiff_t members.
1160 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1161
1162 uint64_t FieldOffset = 0;
1163 llvm::DIDescriptor ElementTypes[2];
1164
1165 // FIXME: This should probably be a function type instead.
1166 ElementTypes[0] =
1167 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001168 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001169 Info.first, Info.second, FieldOffset, 0,
1170 PointerDiffDITy);
1171 FieldOffset += Info.first;
1172
1173 ElementTypes[1] =
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
1179 llvm::DIArray Elements =
1180 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1181 llvm::array_lengthof(ElementTypes));
1182
1183 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1184 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001185 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001186 0, 0, 0, llvm::DIType(), Elements);
1187}
1188
Douglas Gregor840943d2009-12-21 20:18:30 +00001189static QualType UnwrapTypeForDebugInfo(QualType T) {
1190 do {
1191 QualType LastT = T;
1192 switch (T->getTypeClass()) {
1193 default:
1194 return T;
1195 case Type::TemplateSpecialization:
1196 T = cast<TemplateSpecializationType>(T)->desugar();
1197 break;
1198 case Type::TypeOfExpr: {
1199 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1200 T = Ty->getUnderlyingExpr()->getType();
1201 break;
1202 }
1203 case Type::TypeOf:
1204 T = cast<TypeOfType>(T)->getUnderlyingType();
1205 break;
1206 case Type::Decltype:
1207 T = cast<DecltypeType>(T)->getUnderlyingType();
1208 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001209 case Type::Elaborated:
1210 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001211 break;
1212 case Type::SubstTemplateTypeParm:
1213 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1214 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001215 }
1216
1217 assert(T != LastT && "Type unwrapping failed to unwrap!");
1218 if (T == LastT)
1219 return T;
1220 } while (true);
1221
1222 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001223}
1224
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001225/// getOrCreateType - Get the type from the cache or create a new
1226/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001227llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001228 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229 if (Ty.isNull())
1230 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001231
Douglas Gregor840943d2009-12-21 20:18:30 +00001232 // Unwrap the type as needed for debug information.
1233 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001234
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001235 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001236 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001237 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001238 if (it != TypeCache.end()) {
1239 // Verify that the debug info still exists.
1240 if (&*it->second)
1241 return llvm::DIType(cast<llvm::MDNode>(it->second));
1242 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001243
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001244 // Otherwise create the type.
1245 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001246
1247 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001248 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001249 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001250}
1251
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001252/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001253llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001254 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001255 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001256 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001257 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001258
Douglas Gregor2101a822009-12-21 19:57:21 +00001259 const char *Diag = 0;
1260
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001261 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001262 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001263#define TYPE(Class, Base)
1264#define ABSTRACT_TYPE(Class, Base)
1265#define NON_CANONICAL_TYPE(Class, Base)
1266#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1267#include "clang/AST/TypeNodes.def"
1268 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001269
Anders Carlssonbfe69952009-11-06 18:24:04 +00001270 // FIXME: Handle these.
1271 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001272 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001273
1274 case Type::Vector:
1275 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001276 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001277 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001278 case Type::ObjCObject:
1279 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001280 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001281 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1282 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1283 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1284 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001285 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001286 return CreateType(cast<BlockPointerType>(Ty), Unit);
1287 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001288 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001289 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001290 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001291 case Type::FunctionProto:
1292 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001293 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001294 case Type::ConstantArray:
1295 case Type::VariableArray:
1296 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001297 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001298
1299 case Type::LValueReference:
1300 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1301
Anders Carlsson20f12a22009-12-06 18:00:51 +00001302 case Type::MemberPointer:
1303 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001304
1305 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001306 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001307 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001308 case Type::TypeOfExpr:
1309 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001310 case Type::Decltype:
1311 llvm_unreachable("type should have been unwrapped!");
1312 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001313
1314 case Type::RValueReference:
1315 // FIXME: Implement!
1316 Diag = "rvalue references";
1317 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001318 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001319
1320 assert(Diag && "Fall through without a diagnostic?");
1321 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1322 "debug information for %0 is not yet supported");
1323 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1324 << Diag;
1325 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001326}
1327
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001328/// CreateMemberType - Create new member and increase Offset by FType's size.
1329llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1330 llvm::StringRef Name,
1331 uint64_t *Offset) {
1332 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1333 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1334 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1335 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1336 Unit, Name, Unit, 0,
1337 FieldSize, FieldAlign,
1338 *Offset, 0, FieldTy);
1339 *Offset += FieldSize;
1340 return Ty;
1341}
1342
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001343/// EmitFunctionStart - Constructs the debug code for entering a function -
1344/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001345void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001346 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001347 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Devang Patel9c6c3a02010-01-14 00:36:21 +00001349 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001350 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001351
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001352 FnBeginRegionCount.push_back(RegionStack.size());
1353
Devang Patel9c6c3a02010-01-14 00:36:21 +00001354 const Decl *D = GD.getDecl();
1355 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001356 // If there is a DISubprogram for this function available then use it.
1357 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1358 FI = SPCache.find(FD);
1359 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001360 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001361 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1362 llvm::MDNode *SPN = SP;
1363 RegionStack.push_back(SPN);
1364 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001365 return;
1366 }
1367 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001368 Name = getFunctionName(FD);
1369 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001370 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001371 } else {
1372 // Use llvm function name as linkage name.
1373 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001374 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001375 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001376 if (!Name.empty() && Name[0] == '\01')
1377 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Devang Patel970c6182010-04-24 00:49:16 +00001379 // It is expected that CurLoc is set before using EmitFunctionStart.
1380 // Usually, CurLoc points to the left bracket location of compound
1381 // statement representing function body.
1382 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001383 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Chris Lattner9c85ba32008-11-10 06:08:34 +00001385 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001386 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001387 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001388 Fn->hasInternalLinkage(), true/*definition*/,
1389 0, 0, llvm::DIType(),
1390 D->isImplicit(),
1391 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001393 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001394 llvm::MDNode *SPN = SP;
1395 RegionStack.push_back(SPN);
1396 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001397
1398 // Clear stack used to keep track of #line directives.
1399 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001400}
1401
1402
Devang Patel4d939e62010-07-20 22:20:10 +00001403void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001404 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001406 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001407 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001408 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001409 || (SM.getInstantiationLineNumber(CurLoc) ==
1410 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001411 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001412 // New Builder may not be in sync with CGDebugInfo.
1413 if (!Builder.getCurrentDebugLocation().isUnknown())
1414 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001415
1416 // Update last state.
1417 PrevLoc = CurLoc;
1418
Chris Lattnerc6034632010-04-01 06:31:43 +00001419 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001420 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1421 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001422 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001423}
1424
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001425/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1426/// has introduced scope change.
1427void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1428 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1429 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1430 return;
1431 SourceManager &SM = CGM.getContext().getSourceManager();
1432 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1433 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1434
1435 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1436 return;
1437
1438 // If #line directive stack is empty then we are entering a new scope.
1439 if (LineDirectiveFiles.empty()) {
1440 EmitRegionStart(Builder);
1441 LineDirectiveFiles.push_back(PCLoc.getFilename());
1442 return;
1443 }
1444
1445 assert (RegionStack.size() >= LineDirectiveFiles.size()
1446 && "error handling #line regions!");
1447
1448 bool SeenThisFile = false;
1449 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1450 E = LineDirectiveFiles.end(); I != E; ++I)
1451 if (!strcmp(PPLoc.getFilename(), *I)) {
1452 SeenThisFile = true;
1453 break;
1454 }
1455
1456 // If #line for this file is seen earlier then pop out #line regions.
1457 if (SeenThisFile) {
1458 while (!LineDirectiveFiles.empty()) {
1459 const char *LastFile = LineDirectiveFiles.back();
1460 RegionStack.pop_back();
1461 LineDirectiveFiles.pop_back();
1462 if (!strcmp(PPLoc.getFilename(), LastFile))
1463 break;
1464 }
1465 return;
1466 }
1467
1468 // .. otherwise insert new #line region.
1469 EmitRegionStart(Builder);
1470 LineDirectiveFiles.push_back(PCLoc.getFilename());
1471
1472 return;
1473}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001474/// EmitRegionStart- Constructs the debug code for entering a declarative
1475/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001476void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001477 llvm::DIDescriptor D =
1478 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1479 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001480 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001481 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001482 getLineNumber(CurLoc),
1483 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001484 llvm::MDNode *DN = D;
1485 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001486}
1487
1488/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1489/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001490void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001491 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1492
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001493 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001494 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001496 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001497}
1498
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001499/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1500void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1501 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1502 unsigned RCount = FnBeginRegionCount.back();
1503 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1504
1505 // Pop all regions for this function.
1506 while (RegionStack.size() != RCount)
1507 EmitRegionEnd(Builder);
1508 FnBeginRegionCount.pop_back();
1509}
1510
Devang Patel809b9bb2010-02-10 18:49:08 +00001511// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1512// See BuildByRefType.
1513llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1514 uint64_t *XOffset) {
1515
1516 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1517
1518 QualType FType;
1519 uint64_t FieldSize, FieldOffset;
1520 unsigned FieldAlign;
1521
Devang Patel17800552010-03-09 00:44:50 +00001522 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001523 QualType Type = VD->getType();
1524
1525 FieldOffset = 0;
1526 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001527 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1528 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001529 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001530 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1531 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1532
Devang Patel809b9bb2010-02-10 18:49:08 +00001533 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1534 if (HasCopyAndDispose) {
1535 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001536 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1537 &FieldOffset));
1538 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1539 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001540 }
1541
1542 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1543 if (Align > CharUnits::fromQuantity(
1544 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1545 unsigned AlignedOffsetInBytes
1546 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1547 unsigned NumPaddingBytes
1548 = AlignedOffsetInBytes - FieldOffset/8;
1549
1550 if (NumPaddingBytes > 0) {
1551 llvm::APInt pad(32, NumPaddingBytes);
1552 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1553 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001554 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001555 }
1556 }
1557
1558 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001559 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001560 FieldSize = CGM.getContext().getTypeSize(FType);
1561 FieldAlign = Align.getQuantity()*8;
1562
1563 *XOffset = FieldOffset;
1564 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001565 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001566 0, FieldSize, FieldAlign,
1567 FieldOffset, 0, FieldTy);
1568 EltTys.push_back(FieldTy);
1569 FieldOffset += FieldSize;
1570
1571 llvm::DIArray Elements =
1572 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1573
1574 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1575
1576 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001577 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001578 0, FieldOffset, 0, 0, Flags,
1579 llvm::DIType(), Elements);
1580
1581}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001582/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001583void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001584 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001585 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1586
Devang Patel17800552010-03-09 00:44:50 +00001587 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001588 llvm::DIType Ty;
1589 uint64_t XOffset = 0;
1590 if (VD->hasAttr<BlocksAttr>())
1591 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1592 else
1593 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001594
Devang Patelf4e54a22010-05-07 23:05:55 +00001595 // If there is not any debug info for type then do not emit debug info
1596 // for this variable.
1597 if (!Ty)
1598 return;
1599
Chris Lattner9c85ba32008-11-10 06:08:34 +00001600 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001601 unsigned Line = getLineNumber(VD->getLocation());
1602 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Chris Lattner9c85ba32008-11-10 06:08:34 +00001604 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001605 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001606 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001607 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001608 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001609 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001610 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001611 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001612
Chris Lattnerc6034632010-04-01 06:31:43 +00001613 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001614 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001615}
1616
Mike Stumpb1a6e682009-09-30 02:43:10 +00001617/// EmitDeclare - Emit local variable declaration debug info.
1618void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1619 llvm::Value *Storage, CGBuilderTy &Builder,
1620 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001621 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001622 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1623
Devang Patel2b594b92010-04-26 23:28:46 +00001624 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001625 return;
1626
1627 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001628 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001629 llvm::DIType Ty;
1630 if (VD->hasAttr<BlocksAttr>())
1631 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1632 else
1633 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001634
1635 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001636 unsigned Line = getLineNumber(VD->getLocation());
1637 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001638
Devang Pateld6c5a262010-02-01 21:52:22 +00001639 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001640 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001641 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1642 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1643 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1644 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001645 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001646 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1647 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001648 // offset of __forwarding field
1649 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001650 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1651 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1652 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001653 // offset of x field
1654 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001655 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001656 }
1657
1658 // Create the descriptor for the variable.
1659 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001660 DebugFactory.CreateComplexVariable(Tag,
1661 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001662 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001663 addr);
1664 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001665 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001666 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001667
Chris Lattnerc6034632010-04-01 06:31:43 +00001668 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001669 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001670}
1671
Devang Pateld6c5a262010-02-01 21:52:22 +00001672void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001673 llvm::Value *Storage,
1674 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001675 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001676}
1677
Mike Stumpb1a6e682009-09-30 02:43:10 +00001678void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1679 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1680 CodeGenFunction *CGF) {
1681 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1682}
1683
Chris Lattner9c85ba32008-11-10 06:08:34 +00001684/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1685/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001686void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001687 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001688 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001689}
1690
1691
1692
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001693/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001694void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001695 const VarDecl *D) {
1696
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001697 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001698 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001699 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001700
Devang Pateleb6d79b2010-02-01 21:34:11 +00001701 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001702 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001704 // CodeGen turns int[] into int[1] so we'll do the same here.
1705 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001707 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001708 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Anders Carlsson20f12a22009-12-06 18:00:51 +00001710 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001711 ArrayType::Normal, 0);
1712 }
Devang Patel5d822f02010-04-29 17:48:37 +00001713 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001714 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001715 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001716 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001717 llvm::DIDescriptor DContext =
1718 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001719 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1720 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001721 Var->hasInternalLinkage(),
1722 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001723}
1724
Devang Patel9ca36b62009-02-26 21:10:26 +00001725/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001726void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001727 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001728 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001729 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001730 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001731
Devang Pateld6c5a262010-02-01 21:52:22 +00001732 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001733
Devang Pateld6c5a262010-02-01 21:52:22 +00001734 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001735 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Devang Patel9ca36b62009-02-26 21:10:26 +00001737 // CodeGen turns int[] into int[1] so we'll do the same here.
1738 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Devang Patel9ca36b62009-02-26 21:10:26 +00001740 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001741 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Anders Carlsson20f12a22009-12-06 18:00:51 +00001743 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001744 ArrayType::Normal, 0);
1745 }
1746
Devang Patelf6a39b72009-10-20 18:26:30 +00001747 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001748 getOrCreateType(T, Unit),
1749 Var->hasInternalLinkage(),
1750 true/*definition*/, Var);
1751}
Devang Patelabb485f2010-02-01 19:16:32 +00001752
1753/// getOrCreateNamesSpace - Return namespace descriptor for the given
1754/// namespace decl.
1755llvm::DINameSpace
1756CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1757 llvm::DIDescriptor Unit) {
1758 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1759 NameSpaceCache.find(NSDecl);
1760 if (I != NameSpaceCache.end())
1761 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1762
Devang Patel8ab870d2010-05-12 23:46:38 +00001763 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001764
1765 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001766 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001767 llvm::DINameSpace NS =
1768 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001769 llvm::DIFile(Unit), LineNo);
1770 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001771 return NS;
1772}