blob: 92b44bd14d796be1c8ce4953d7a046e7b1ab64e2 [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 Patel22fe5852010-03-12 21:04:27 +0000191 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000192 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000193
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.
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000198 std::string MainFileDir;
199 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
200 MainFileDir = MainFile->getDir()->getName();
201 else
202 MainFileDir = AbsFileName.getDirname();
203
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(
Douglas Gregorf7ad5002010-03-19 14:49:09 +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();
245 case BuiltinType::UChar:
246 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
247 case BuiltinType::Char_S:
248 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
249 case BuiltinType::UShort:
250 case BuiltinType::UInt:
251 case BuiltinType::ULong:
252 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
253 case BuiltinType::Short:
254 case BuiltinType::Int:
255 case BuiltinType::Long:
256 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
257 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
258 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000259 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000261 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000262 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000263 uint64_t Size = CGM.getContext().getTypeSize(BT);
264 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Devang Patelca80a5f2009-10-20 19:55:01 +0000267 llvm::DIType DbgTy =
268 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000269 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000270 Unit, 0, Size, Align,
271 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000272 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000273}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000274
Chris Lattnerb7003772009-04-23 06:13:01 +0000275llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000276 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000277 // Bit size, align and offset of the type.
278 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
279 if (Ty->isComplexIntegerType())
280 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Anders Carlsson20f12a22009-12-06 18:00:51 +0000282 uint64_t Size = CGM.getContext().getTypeSize(Ty);
283 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000284 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Devang Patelca80a5f2009-10-20 19:55:01 +0000286 llvm::DIType DbgTy =
287 DebugFactory.CreateBasicType(Unit, "complex",
288 Unit, 0, Size, Align,
289 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000290 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000291}
292
John McCalla1805292009-09-25 01:40:47 +0000293/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000294/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000295llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000296 QualifierCollector Qc;
297 const Type *T = Qc.strip(Ty);
298
299 // Ignore these qualifiers for now.
300 Qc.removeObjCGCAttr();
301 Qc.removeAddressSpace();
302
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303 // We will create one Derived type for one qualifier and recurse to handle any
304 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000305 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000306 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000307 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000308 Qc.removeConst();
309 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000310 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000311 Qc.removeVolatile();
312 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000313 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000314 Qc.removeRestrict();
315 } else {
316 assert(Qc.empty() && "Unknown type qualifier for debug info");
317 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000318 }
Mike Stump1eb44332009-09-09 15:08:12 +0000319
John McCalla1805292009-09-25 01:40:47 +0000320 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
321
Daniel Dunbar3845f862008-10-31 03:54:29 +0000322 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
323 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000324 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000325 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000326 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000327 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000328}
329
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000330llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000331 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000332 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000333 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
334 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000335 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000336}
337
Chris Lattner9c85ba32008-11-10 06:08:34 +0000338llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000339 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000340 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
341 Ty->getPointeeType(), Unit);
342}
343
344llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
345 const Type *Ty,
346 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000347 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000348 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000350 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000351
352 // Size is always the size of a pointer. We can't use getTypeSize here
353 // because that does not return the correct value for references.
354 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
356 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Devang Patelca80a5f2009-10-20 19:55:01 +0000358 return
Devang Pateld58562e2010-03-09 22:49:11 +0000359 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000360 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000361
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000362}
363
Mike Stump9bc093c2009-05-14 02:03:51 +0000364llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000365 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 if (BlockLiteralGenericSet)
367 return BlockLiteralGeneric;
368
Mike Stump9bc093c2009-05-14 02:03:51 +0000369 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
370
371 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
372
373 llvm::DIType FieldTy;
374
375 QualType FType;
376 uint64_t FieldSize, FieldOffset;
377 unsigned FieldAlign;
378
379 llvm::DIArray Elements;
380 llvm::DIType EltTy, DescTy;
381
382 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000383 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000384 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
385 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000386
Daniel Dunbarca308df2009-05-26 19:40:20 +0000387 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000388 EltTys.clear();
389
Mike Stump3d363c52009-10-02 02:30:50 +0000390 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000391 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000392
Mike Stump9bc093c2009-05-14 02:03:51 +0000393 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000394 Unit, LineNo, FieldOffset, 0, 0,
395 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Mike Stump9bc093c2009-05-14 02:03:51 +0000397 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000398 uint64_t Size = CGM.getContext().getTypeSize(Ty);
399 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000402 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000403 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000404
405 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000406 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000407 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000408 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000409 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
410 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000411 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000412 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000413
Anders Carlsson20f12a22009-12-06 18:00:51 +0000414 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000415 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000416 FieldSize = CGM.getContext().getTypeSize(Ty);
417 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000418 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000419 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000420 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000421 FieldOffset, 0, FieldTy);
422 EltTys.push_back(FieldTy);
423
424 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000425 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000426
427 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000428 Unit, LineNo, FieldOffset, 0, 0,
429 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Mike Stump9bc093c2009-05-14 02:03:51 +0000431 BlockLiteralGenericSet = true;
432 BlockLiteralGeneric
433 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000434 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000435 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000436 return BlockLiteralGeneric;
437}
438
Chris Lattner9c85ba32008-11-10 06:08:34 +0000439llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000440 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 // Typedefs are derived from some other type. If we have a typedef of a
442 // typedef, make sure to emit the whole chain.
443 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner9c85ba32008-11-10 06:08:34 +0000445 // We don't set size information, but do specify where the typedef was
446 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000447 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000448
Devang Pateleb6d79b2010-02-01 21:34:11 +0000449 llvm::DIDescriptor TyContext
450 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
451 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000452 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000453 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000454 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000455 Ty->getDecl()->getName(), Unit,
456 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000457 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000458}
459
Chris Lattner9c85ba32008-11-10 06:08:34 +0000460llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000461 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000462 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463
Chris Lattner9c85ba32008-11-10 06:08:34 +0000464 // Add the result type at least.
465 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner9c85ba32008-11-10 06:08:34 +0000467 // Set up remainder of arguments if there is a prototype.
468 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000469 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000470 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
471 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
472 } else {
473 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000474 }
475
Chris Lattner9c85ba32008-11-10 06:08:34 +0000476 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000477 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Devang Patelca80a5f2009-10-20 19:55:01 +0000479 llvm::DIType DbgTy =
480 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000481 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000482 0, 0, 0, 0, 0,
483 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000484 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000485}
486
Devang Patel428deb52010-01-19 00:00:59 +0000487/// CollectRecordFields - A helper function to collect debug info for
488/// record fields. This is used while creating debug info entry for a Record.
489void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000490CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000491 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
492 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000493 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
494 for (RecordDecl::field_iterator I = RD->field_begin(),
495 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000496 I != E; ++I, ++FieldNo) {
497 FieldDecl *Field = *I;
498 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
499
500 llvm::StringRef FieldName = Field->getName();
501
Devang Patel4835fdd2010-02-12 01:31:06 +0000502 // Ignore unnamed fields. Do not ignore unnamed records.
503 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000504 continue;
505
506 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000507 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
508 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000509 QualType FType = Field->getType();
510 uint64_t FieldSize = 0;
511 unsigned FieldAlign = 0;
512 if (!FType->isIncompleteArrayType()) {
513
514 // Bit size, align and offset of the type.
515 FieldSize = CGM.getContext().getTypeSize(FType);
516 Expr *BitWidth = Field->getBitWidth();
517 if (BitWidth)
518 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
519
520 FieldAlign = CGM.getContext().getTypeAlign(FType);
521 }
522
523 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
524
Devang Patel71f4ff62010-04-21 23:12:37 +0000525 unsigned Flags = 0;
526 AccessSpecifier Access = I->getAccess();
527 if (Access == clang::AS_private)
528 Flags |= llvm::DIType::FlagPrivate;
529 else if (Access == clang::AS_protected)
530 Flags |= llvm::DIType::FlagProtected;
531
Devang Patel428deb52010-01-19 00:00:59 +0000532 // Create a DW_TAG_member node to remember the offset of this field in the
533 // struct. FIXME: This is an absolutely insane way to capture this
534 // information. When we gut debug info, this should be fixed.
535 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
536 FieldName, FieldDefUnit,
537 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000538 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000539 EltTys.push_back(FieldTy);
540 }
541}
542
Devang Patela6da1922010-01-28 00:28:01 +0000543/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
544/// function type is not updated to include implicit "this" pointer. Use this
545/// routine to get a method type which includes "this" pointer.
546llvm::DIType
547CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000548 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000549 llvm::DIType FnTy
550 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
551 0),
552 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000553
554 // Static methods do not need "this" pointer argument.
555 if (Method->isStatic())
556 return FnTy;
557
Devang Patela6da1922010-01-28 00:28:01 +0000558 // Add "this" pointer.
559
Devang Patelab699792010-05-07 18:12:35 +0000560 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000561 assert (Args.getNumElements() && "Invalid number of arguments!");
562
563 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
564
565 // First element is always return type. For 'void' functions it is NULL.
566 Elts.push_back(Args.getElement(0));
567
568 // "this" pointer is always first argument.
569 ASTContext &Context = CGM.getContext();
570 QualType ThisPtr =
571 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000572 llvm::DIType ThisPtrType =
573 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000574
Devang Patel700a1cb2010-07-20 20:24:18 +0000575 unsigned Quals = Method->getTypeQualifiers();
576 if (Quals & Qualifiers::Const)
577 ThisPtrType =
578 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_const_type,
579 Unit, "", Unit,
580 0, 0, 0, 0, 0, ThisPtrType);
581 if (Quals & Qualifiers::Volatile)
582 ThisPtrType =
583 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_volatile_type,
584 Unit, "", Unit,
585 0, 0, 0, 0, 0, ThisPtrType);
586
Devang Patelab699792010-05-07 18:12:35 +0000587 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000588 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000589
590 // Copy rest of the arguments.
591 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
592 Elts.push_back(Args.getElement(i));
593
594 llvm::DIArray EltTypeArray =
595 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
596
597 return
598 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000599 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000600 0, 0, 0, 0, 0,
601 llvm::DIType(), EltTypeArray);
602}
603
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000604/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
605/// a single member function GlobalDecl.
606llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000607CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000608 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000609 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000610 bool IsCtorOrDtor =
611 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
612
613 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000614 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000615
616 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
617 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000618 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000619 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000620 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000621
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000622 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000623 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
624 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000625
626 // Collect virtual method info.
627 llvm::DIType ContainingType;
628 unsigned Virtuality = 0;
629 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000630
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000631 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000632 if (Method->isPure())
633 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
634 else
635 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
636
637 // It doesn't make sense to give a virtual destructor a vtable index,
638 // since a single destructor has two entries in the vtable.
639 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000640 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000641 ContainingType = RecordTy;
642 }
643
644 llvm::DISubprogram SP =
645 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
646 MethodLinkageName,
647 MethodDefUnit, MethodLine,
648 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000649 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000650 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000651 Method->isImplicit(),
652 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000653
654 // Don't cache ctors or dtors since we have to emit multiple functions for
655 // a single ctor or dtor.
656 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000657 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000658
659 return SP;
660}
661
Devang Patel4125fd22010-01-19 01:54:44 +0000662/// CollectCXXMemberFunctions - A helper function to collect debug info for
663/// C++ member functions.This is used while creating debug info entry for
664/// a Record.
665void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000666CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000667 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
668 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000669 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
670 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000671 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000672
Devang Pateld5322da2010-02-09 19:09:28 +0000673 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000674 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000675
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000676 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000677 }
678}
679
Devang Patela245c5b2010-01-25 23:32:18 +0000680/// CollectCXXBases - A helper function to collect debug info for
681/// C++ base classes. This is used while creating debug info entry for
682/// a Record.
683void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000684CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000685 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
686 llvm::DICompositeType &RecordTy) {
687
Devang Patel239cec62010-02-01 21:39:52 +0000688 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
689 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
690 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000691 unsigned BFlags = 0;
692 uint64_t BaseOffset;
693
694 const CXXRecordDecl *Base =
695 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
696
697 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000698 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000699 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000700 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000701 BFlags = llvm::DIType::FlagVirtual;
702 } else
703 BaseOffset = RL.getBaseClassOffset(Base);
704
705 AccessSpecifier Access = BI->getAccessSpecifier();
706 if (Access == clang::AS_private)
707 BFlags |= llvm::DIType::FlagPrivate;
708 else if (Access == clang::AS_protected)
709 BFlags |= llvm::DIType::FlagProtected;
710
711 llvm::DIType DTy =
712 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
713 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000714 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000715 BaseOffset, BFlags,
716 getOrCreateType(BI->getType(),
717 Unit));
718 EltTys.push_back(DTy);
719 }
Devang Patela245c5b2010-01-25 23:32:18 +0000720}
721
Devang Patel4ce3f202010-01-28 18:11:52 +0000722/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000723llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000724 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000725 return VTablePtrType;
726
727 ASTContext &Context = CGM.getContext();
728
729 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000730 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
731 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000732 llvm::DIType SubTy =
733 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000734 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000735 0, 0, 0, 0, 0, llvm::DIType(), SElements);
736
737 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
738 llvm::DIType vtbl_ptr_type
739 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000740 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000741 0, Size, 0, 0, 0, SubTy);
742
Devang Pateld58562e2010-03-09 22:49:11 +0000743 VTablePtrType =
744 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
745 Unit, "", Unit,
746 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000747 return VTablePtrType;
748}
749
Anders Carlsson046c2942010-04-17 20:15:18 +0000750/// getVTableName - Get vtable name for the given Class.
751llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000752 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000753 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000754
755 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000756 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000757 memcpy(StrPtr, Name.data(), Name.length());
758 return llvm::StringRef(StrPtr, Name.length());
759}
760
761
Anders Carlsson046c2942010-04-17 20:15:18 +0000762/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000763/// debug info entry in EltTys vector.
764void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000765CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000766 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000767 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000768
769 // If there is a primary base then it will hold vtable info.
770 if (RL.getPrimaryBase())
771 return;
772
773 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000774 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000775 return;
776
777 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
778 llvm::DIType VPTR
779 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000780 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000781 0, Size, 0, 0, 0,
782 getOrCreateVTablePtrType(Unit));
783 EltTys.push_back(VPTR);
784}
785
Devang Patel65e99f22009-02-25 01:36:11 +0000786/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000787llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000788 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000789 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000792 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000793 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000794 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000795 Tag = llvm::dwarf::DW_TAG_union_type;
796 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000797 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000798 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000799 }
800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000802 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
803 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattner9c85ba32008-11-10 06:08:34 +0000805 // Records and classes and unions can all be recursive. To handle them, we
806 // first generate a debug descriptor for the struct as a forward declaration.
807 // Then (if it is a definition) we go through and get debug info for all of
808 // its members. Finally, we create a descriptor for the complete type (which
809 // may refer to the forward decl if the struct is recursive) and replace all
810 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000811 llvm::DIDescriptor FDContext =
812 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
813
814 // If this is just a forward declaration, construct an appropriately
815 // marked node and just return it.
816 if (!RD->getDefinition()) {
817 llvm::DICompositeType FwdDecl =
818 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
819 DefUnit, Line, 0, 0, 0,
820 llvm::DIType::FlagFwdDecl,
821 llvm::DIType(), llvm::DIArray());
822
823 return FwdDecl;
824 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000825
Devang Pateld6c5a262010-02-01 21:52:22 +0000826 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000827 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000828 llvm::SmallString<128> FwdDeclName;
829 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000830 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000831 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000832 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Devang Patelab699792010-05-07 18:12:35 +0000835 llvm::MDNode *MN = FwdDecl;
836 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837 // Otherwise, insert it into the TypeCache so that recursive uses will find
838 // it.
Devang Patelab699792010-05-07 18:12:35 +0000839 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000840 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000841 RegionStack.push_back(FwdDeclNode);
842 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000843
844 // Convert all the elements.
845 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
846
Devang Pateld6c5a262010-02-01 21:52:22 +0000847 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000848 if (CXXDecl) {
849 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000850 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000851 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000852 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000853 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000854 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000855 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000856
857 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000858 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000859 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
860 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000861 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000862 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000863 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Chris Lattner9c85ba32008-11-10 06:08:34 +0000866 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000867 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000868
869 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000870 uint64_t Size = CGM.getContext().getTypeSize(Ty);
871 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Devang Patele4c1ea02010-03-11 20:01:48 +0000873 RegionStack.pop_back();
874 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
875 RegionMap.find(Ty->getDecl());
876 if (RI != RegionMap.end())
877 RegionMap.erase(RI);
878
Devang Patel411894b2010-02-01 22:40:08 +0000879 llvm::DIDescriptor RDContext =
880 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000881
882 llvm::StringRef RDName = RD->getName();
883 // If this is a class, include the template arguments also.
884 if (Tag == llvm::dwarf::DW_TAG_class_type)
885 RDName = getClassName(RD);
886
Devang Patel0ce73f62009-07-22 18:57:00 +0000887 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000888 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000889 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000890 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000891 llvm::DIType(), Elements,
892 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000893
894 // Now that we have a real decl for the struct, replace anything using the
895 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000896 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000897 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000898 return RealDecl;
899}
900
John McCallc12c5bb2010-05-15 11:32:37 +0000901/// CreateType - get objective-c object type.
902llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
903 llvm::DIFile Unit) {
904 // Ignore protocols.
905 return getOrCreateType(Ty->getBaseType(), Unit);
906}
907
Devang Patel9ca36b62009-02-26 21:10:26 +0000908/// CreateType - get objective-c interface type.
909llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000910 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000911 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000912 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000913
914 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000915 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000916 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000917 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000918
Devang Patel9ca36b62009-02-26 21:10:26 +0000919 // To handle recursive interface, we
920 // first generate a debug descriptor for the struct as a forward declaration.
921 // Then (if it is a definition) we go through and get debug info for all of
922 // its members. Finally, we create a descriptor for the complete type (which
923 // may refer to the forward decl if the struct is recursive) and replace all
924 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000925 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000926 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000927 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000928 llvm::DIType(), llvm::DIArray(),
929 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Devang Patel9ca36b62009-02-26 21:10:26 +0000931 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000932 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000933 return FwdDecl;
934
Devang Patelab699792010-05-07 18:12:35 +0000935 llvm::MDNode *MN = FwdDecl;
936 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000937 // Otherwise, insert it into the TypeCache so that recursive uses will find
938 // it.
Devang Patelab699792010-05-07 18:12:35 +0000939 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000940 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000941 RegionStack.push_back(FwdDeclNode);
942 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000943
944 // Convert all the elements.
945 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
946
Devang Pateld6c5a262010-02-01 21:52:22 +0000947 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000948 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000949 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000950 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000951 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000952 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000953 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000954 0 /* offset */, 0, SClassTy);
955 EltTys.push_back(InhTag);
956 }
957
Devang Pateld6c5a262010-02-01 21:52:22 +0000958 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000959
960 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000961 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
962 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000963 ObjCIvarDecl *Field = *I;
964 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
965
Devang Patel73621622009-11-25 17:37:31 +0000966 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000967
Devang Patelde135022009-04-27 22:40:36 +0000968 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000969 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000970 continue;
971
Devang Patel9ca36b62009-02-26 21:10:26 +0000972 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000973 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
974 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000975 QualType FType = Field->getType();
976 uint64_t FieldSize = 0;
977 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000978
Devang Patel99c20eb2009-03-20 18:24:39 +0000979 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000980
Devang Patel99c20eb2009-03-20 18:24:39 +0000981 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000982 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000983 Expr *BitWidth = Field->getBitWidth();
984 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000985 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000986
Anders Carlsson20f12a22009-12-06 18:00:51 +0000987 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000988 }
989
Mike Stump1eb44332009-09-09 15:08:12 +0000990 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
991
Devang Patelc20482b2009-03-19 00:23:53 +0000992 unsigned Flags = 0;
993 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
994 Flags = llvm::DIType::FlagProtected;
995 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
996 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Devang Patel9ca36b62009-02-26 21:10:26 +0000998 // Create a DW_TAG_member node to remember the offset of this field in the
999 // struct. FIXME: This is an absolutely insane way to capture this
1000 // information. When we gut debug info, this should be fixed.
1001 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1002 FieldName, FieldDefUnit,
1003 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001004 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001005 EltTys.push_back(FieldTy);
1006 }
Mike Stump1eb44332009-09-09 15:08:12 +00001007
Devang Patel9ca36b62009-02-26 21:10:26 +00001008 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001009 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001010
Devang Patele4c1ea02010-03-11 20:01:48 +00001011 RegionStack.pop_back();
1012 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1013 RegionMap.find(Ty->getDecl());
1014 if (RI != RegionMap.end())
1015 RegionMap.erase(RI);
1016
Devang Patel9ca36b62009-02-26 21:10:26 +00001017 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001018 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1019 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Devang Patel6c1fddf2009-07-27 18:42:03 +00001021 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001023 Line, Size, Align, 0, 0, llvm::DIType(),
1024 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001025
1026 // Now that we have a real decl for the struct, replace anything using the
1027 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001028 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001029 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001030
Devang Patel9ca36b62009-02-26 21:10:26 +00001031 return RealDecl;
1032}
1033
Chris Lattner9c85ba32008-11-10 06:08:34 +00001034llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001035 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001036 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037
1038 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1039
1040 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001041 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001042 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001043 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001044 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001045 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattner9c85ba32008-11-10 06:08:34 +00001048 // Return a CompositeType for the enum itself.
1049 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001050 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001051
Devang Patel8ab870d2010-05-12 23:46:38 +00001052 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1053 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner9c85ba32008-11-10 06:08:34 +00001055 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001056 uint64_t Size = 0;
1057 unsigned Align = 0;
1058 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001059 Size = CGM.getContext().getTypeSize(Ty);
1060 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Devang Patelca80a5f2009-10-20 19:55:01 +00001063 llvm::DIType DbgTy =
1064 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001065 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001066 Size, Align, 0, 0,
1067 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001068 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001069}
1070
1071llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001072 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001073 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1074 return CreateType(RT, Unit);
1075 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1076 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Chris Lattner9c85ba32008-11-10 06:08:34 +00001078 return llvm::DIType();
1079}
1080
Devang Patel70c23cd2010-02-23 22:59:39 +00001081llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001082 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001083 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1084 uint64_t NumElems = Ty->getNumElements();
1085 if (NumElems > 0)
1086 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001087
Benjamin Kramerad468862010-03-30 11:36:44 +00001088 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1089 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001090
1091 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1092 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1093
1094 return
1095 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001096 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001097 0, Size, Align, 0, 0,
1098 ElementTy, SubscriptArray);
1099}
1100
Chris Lattner9c85ba32008-11-10 06:08:34 +00001101llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001102 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001103 uint64_t Size;
1104 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
1106
Nuno Lopes010d5142009-01-28 00:35:17 +00001107 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001108 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001109 Size = 0;
1110 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001111 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001112 } else if (Ty->isIncompleteArrayType()) {
1113 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001114 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001115 } else {
1116 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001117 Size = CGM.getContext().getTypeSize(Ty);
1118 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001119 }
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Chris Lattner9c85ba32008-11-10 06:08:34 +00001121 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1122 // interior arrays, do we care? Why aren't nested arrays represented the
1123 // obvious/recursive way?
1124 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1125 QualType EltTy(Ty, 0);
1126 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001127 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001128 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001129 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001130 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001131 // FIXME: Verify this is right for VLAs.
1132 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1133 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner9c85ba32008-11-10 06:08:34 +00001136 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001137 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001138
Devang Patelca80a5f2009-10-20 19:55:01 +00001139 llvm::DIType DbgTy =
1140 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001141 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001142 0, Size, Align, 0, 0,
1143 getOrCreateType(EltTy, Unit),
1144 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001145 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001146}
1147
Anders Carlssona031b352009-11-06 19:19:55 +00001148llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001149 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001150 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1151 Ty, Ty->getPointeeType(), Unit);
1152}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001153
Anders Carlsson20f12a22009-12-06 18:00:51 +00001154llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001155 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001156 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1157 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1158
1159 if (!Ty->getPointeeType()->isFunctionType()) {
1160 // We have a data member pointer type.
1161 return PointerDiffDITy;
1162 }
1163
1164 // We have a member function pointer type. Treat it as a struct with two
1165 // ptrdiff_t members.
1166 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1167
1168 uint64_t FieldOffset = 0;
1169 llvm::DIDescriptor ElementTypes[2];
1170
1171 // FIXME: This should probably be a function type instead.
1172 ElementTypes[0] =
1173 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001174 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001175 Info.first, Info.second, FieldOffset, 0,
1176 PointerDiffDITy);
1177 FieldOffset += Info.first;
1178
1179 ElementTypes[1] =
1180 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001181 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001182 Info.first, Info.second, FieldOffset, 0,
1183 PointerDiffDITy);
1184
1185 llvm::DIArray Elements =
1186 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1187 llvm::array_lengthof(ElementTypes));
1188
1189 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1190 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001191 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001192 0, 0, 0, llvm::DIType(), Elements);
1193}
1194
Douglas Gregor840943d2009-12-21 20:18:30 +00001195static QualType UnwrapTypeForDebugInfo(QualType T) {
1196 do {
1197 QualType LastT = T;
1198 switch (T->getTypeClass()) {
1199 default:
1200 return T;
1201 case Type::TemplateSpecialization:
1202 T = cast<TemplateSpecializationType>(T)->desugar();
1203 break;
1204 case Type::TypeOfExpr: {
1205 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1206 T = Ty->getUnderlyingExpr()->getType();
1207 break;
1208 }
1209 case Type::TypeOf:
1210 T = cast<TypeOfType>(T)->getUnderlyingType();
1211 break;
1212 case Type::Decltype:
1213 T = cast<DecltypeType>(T)->getUnderlyingType();
1214 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001215 case Type::Elaborated:
1216 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001217 break;
1218 case Type::SubstTemplateTypeParm:
1219 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1220 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001221 }
1222
1223 assert(T != LastT && "Type unwrapping failed to unwrap!");
1224 if (T == LastT)
1225 return T;
1226 } while (true);
1227
1228 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001229}
1230
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001231/// getOrCreateType - Get the type from the cache or create a new
1232/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001233llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001234 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001235 if (Ty.isNull())
1236 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Douglas Gregor840943d2009-12-21 20:18:30 +00001238 // Unwrap the type as needed for debug information.
1239 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001240
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001241 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001242 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001243 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001244 if (it != TypeCache.end()) {
1245 // Verify that the debug info still exists.
1246 if (&*it->second)
1247 return llvm::DIType(cast<llvm::MDNode>(it->second));
1248 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001249
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001250 // Otherwise create the type.
1251 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001252
1253 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001254 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001255 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001256}
1257
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001258/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001259llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001260 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001261 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001262 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001263 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001264
Douglas Gregor2101a822009-12-21 19:57:21 +00001265 const char *Diag = 0;
1266
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001267 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001268 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001269#define TYPE(Class, Base)
1270#define ABSTRACT_TYPE(Class, Base)
1271#define NON_CANONICAL_TYPE(Class, Base)
1272#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1273#include "clang/AST/TypeNodes.def"
1274 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001275
Anders Carlssonbfe69952009-11-06 18:24:04 +00001276 // FIXME: Handle these.
1277 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001278 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001279
1280 case Type::Vector:
1281 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001282 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001283 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001284 case Type::ObjCObject:
1285 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001286 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001287 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1288 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1289 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1290 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001291 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001292 return CreateType(cast<BlockPointerType>(Ty), Unit);
1293 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001294 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001295 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001296 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001297 case Type::FunctionProto:
1298 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001299 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300 case Type::ConstantArray:
1301 case Type::VariableArray:
1302 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001303 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001304
1305 case Type::LValueReference:
1306 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1307
Anders Carlsson20f12a22009-12-06 18:00:51 +00001308 case Type::MemberPointer:
1309 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001310
1311 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001312 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001313 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001314 case Type::TypeOfExpr:
1315 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001316 case Type::Decltype:
1317 llvm_unreachable("type should have been unwrapped!");
1318 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001319
1320 case Type::RValueReference:
1321 // FIXME: Implement!
1322 Diag = "rvalue references";
1323 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001324 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001325
1326 assert(Diag && "Fall through without a diagnostic?");
1327 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1328 "debug information for %0 is not yet supported");
1329 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1330 << Diag;
1331 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001332}
1333
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001334/// CreateMemberType - Create new member and increase Offset by FType's size.
1335llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1336 llvm::StringRef Name,
1337 uint64_t *Offset) {
1338 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1339 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1340 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1341 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1342 Unit, Name, Unit, 0,
1343 FieldSize, FieldAlign,
1344 *Offset, 0, FieldTy);
1345 *Offset += FieldSize;
1346 return Ty;
1347}
1348
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001349/// EmitFunctionStart - Constructs the debug code for entering a function -
1350/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001351void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001352 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001353 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Devang Patel9c6c3a02010-01-14 00:36:21 +00001355 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001356 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001357
1358 const Decl *D = GD.getDecl();
1359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001360 // If there is a DISubprogram for this function available then use it.
1361 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1362 FI = SPCache.find(FD);
1363 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001364 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001365 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1366 llvm::MDNode *SPN = SP;
1367 RegionStack.push_back(SPN);
1368 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001369 return;
1370 }
1371 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001372 Name = getFunctionName(FD);
1373 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001374 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001375 } else {
1376 // Use llvm function name as linkage name.
1377 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001378 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001379 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001380 if (!Name.empty() && Name[0] == '\01')
1381 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Devang Patel970c6182010-04-24 00:49:16 +00001383 // It is expected that CurLoc is set before using EmitFunctionStart.
1384 // Usually, CurLoc points to the left bracket location of compound
1385 // statement representing function body.
1386 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001387 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Chris Lattner9c85ba32008-11-10 06:08:34 +00001389 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001390 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001391 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001392 Fn->hasInternalLinkage(), true/*definition*/,
1393 0, 0, llvm::DIType(),
1394 D->isImplicit(),
1395 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001397 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001398 llvm::MDNode *SPN = SP;
1399 RegionStack.push_back(SPN);
1400 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001401}
1402
1403
Chris Lattner9c85ba32008-11-10 06:08:34 +00001404void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001405 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001407 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001408 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001409 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001410 || (SM.getInstantiationLineNumber(CurLoc) ==
1411 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001412 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001413 // New Builder may not be in sync with CGDebugInfo.
1414 if (!Builder.getCurrentDebugLocation().isUnknown())
1415 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001416
1417 // Update last state.
1418 PrevLoc = CurLoc;
1419
Chris Lattnerc6034632010-04-01 06:31:43 +00001420 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001421 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1422 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001423 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001424}
1425
1426/// EmitRegionStart- Constructs the debug code for entering a declarative
1427/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001428void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001429 llvm::DIDescriptor D =
1430 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1431 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001432 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001433 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001434 getLineNumber(CurLoc),
1435 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001436 llvm::MDNode *DN = D;
1437 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001438}
1439
1440/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1441/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001442void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001443 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1444
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001445 // Provide an region stop point.
1446 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001448 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001449}
1450
Devang Patel809b9bb2010-02-10 18:49:08 +00001451// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1452// See BuildByRefType.
1453llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1454 uint64_t *XOffset) {
1455
1456 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1457
1458 QualType FType;
1459 uint64_t FieldSize, FieldOffset;
1460 unsigned FieldAlign;
1461
Devang Patel17800552010-03-09 00:44:50 +00001462 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001463 QualType Type = VD->getType();
1464
1465 FieldOffset = 0;
1466 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001467 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1468 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001469 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001470 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1471 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1472
Devang Patel809b9bb2010-02-10 18:49:08 +00001473 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1474 if (HasCopyAndDispose) {
1475 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001476 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1477 &FieldOffset));
1478 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1479 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001480 }
1481
1482 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1483 if (Align > CharUnits::fromQuantity(
1484 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1485 unsigned AlignedOffsetInBytes
1486 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1487 unsigned NumPaddingBytes
1488 = AlignedOffsetInBytes - FieldOffset/8;
1489
1490 if (NumPaddingBytes > 0) {
1491 llvm::APInt pad(32, NumPaddingBytes);
1492 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1493 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001494 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001495 }
1496 }
1497
1498 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001499 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001500 FieldSize = CGM.getContext().getTypeSize(FType);
1501 FieldAlign = Align.getQuantity()*8;
1502
1503 *XOffset = FieldOffset;
1504 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001505 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001506 0, FieldSize, FieldAlign,
1507 FieldOffset, 0, FieldTy);
1508 EltTys.push_back(FieldTy);
1509 FieldOffset += FieldSize;
1510
1511 llvm::DIArray Elements =
1512 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1513
1514 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1515
1516 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001517 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001518 0, FieldOffset, 0, 0, Flags,
1519 llvm::DIType(), Elements);
1520
1521}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001522/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001523void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001524 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001525 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1526
Devang Patel17800552010-03-09 00:44:50 +00001527 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001528 llvm::DIType Ty;
1529 uint64_t XOffset = 0;
1530 if (VD->hasAttr<BlocksAttr>())
1531 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1532 else
1533 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001534
Devang Patelf4e54a22010-05-07 23:05:55 +00001535 // If there is not any debug info for type then do not emit debug info
1536 // for this variable.
1537 if (!Ty)
1538 return;
1539
Chris Lattner9c85ba32008-11-10 06:08:34 +00001540 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001541 unsigned Line = getLineNumber(VD->getLocation());
1542 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Chris Lattner9c85ba32008-11-10 06:08:34 +00001544 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001545 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001546 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001547 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001548 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001549 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001550 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001551 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001552
Chris Lattnerc6034632010-04-01 06:31:43 +00001553 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001554 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001555}
1556
Mike Stumpb1a6e682009-09-30 02:43:10 +00001557/// EmitDeclare - Emit local variable declaration debug info.
1558void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1559 llvm::Value *Storage, CGBuilderTy &Builder,
1560 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001561 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001562 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1563
Devang Patel2b594b92010-04-26 23:28:46 +00001564 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001565 return;
1566
1567 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001568 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001569 llvm::DIType Ty;
1570 if (VD->hasAttr<BlocksAttr>())
1571 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1572 else
1573 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001574
1575 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001576 unsigned Line = getLineNumber(VD->getLocation());
1577 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001578
Devang Pateld6c5a262010-02-01 21:52:22 +00001579 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001580 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001581 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1582 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1583 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1584 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001585 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001586 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1587 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001588 // offset of __forwarding field
1589 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001590 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1591 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1592 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001593 // offset of x field
1594 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001595 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001596 }
1597
1598 // Create the descriptor for the variable.
1599 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001600 DebugFactory.CreateComplexVariable(Tag,
1601 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001602 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001603 addr);
1604 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001605 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001606 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001607
Chris Lattnerc6034632010-04-01 06:31:43 +00001608 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001609 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001610}
1611
Devang Pateld6c5a262010-02-01 21:52:22 +00001612void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001613 llvm::Value *Storage,
1614 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001615 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001616}
1617
Mike Stumpb1a6e682009-09-30 02:43:10 +00001618void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1619 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1620 CodeGenFunction *CGF) {
1621 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1622}
1623
Chris Lattner9c85ba32008-11-10 06:08:34 +00001624/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1625/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001626void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001627 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001628 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001629}
1630
1631
1632
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001633/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001634void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001635 const VarDecl *D) {
1636
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001637 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001638 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001639 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001640
Devang Pateleb6d79b2010-02-01 21:34:11 +00001641 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001642 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001644 // CodeGen turns int[] into int[1] so we'll do the same here.
1645 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001647 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001648 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Anders Carlsson20f12a22009-12-06 18:00:51 +00001650 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001651 ArrayType::Normal, 0);
1652 }
Devang Patel5d822f02010-04-29 17:48:37 +00001653 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001654 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001655 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001656 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001657 llvm::DIDescriptor DContext =
1658 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001659 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1660 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001661 Var->hasInternalLinkage(),
1662 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001663}
1664
Devang Patel9ca36b62009-02-26 21:10:26 +00001665/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001666void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001667 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001668 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001669 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001670 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001671
Devang Pateld6c5a262010-02-01 21:52:22 +00001672 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001673
Devang Pateld6c5a262010-02-01 21:52:22 +00001674 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001675 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Devang Patel9ca36b62009-02-26 21:10:26 +00001677 // CodeGen turns int[] into int[1] so we'll do the same here.
1678 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Devang Patel9ca36b62009-02-26 21:10:26 +00001680 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001681 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001682
Anders Carlsson20f12a22009-12-06 18:00:51 +00001683 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001684 ArrayType::Normal, 0);
1685 }
1686
Devang Patelf6a39b72009-10-20 18:26:30 +00001687 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001688 getOrCreateType(T, Unit),
1689 Var->hasInternalLinkage(),
1690 true/*definition*/, Var);
1691}
Devang Patelabb485f2010-02-01 19:16:32 +00001692
1693/// getOrCreateNamesSpace - Return namespace descriptor for the given
1694/// namespace decl.
1695llvm::DINameSpace
1696CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1697 llvm::DIDescriptor Unit) {
1698 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1699 NameSpaceCache.find(NSDecl);
1700 if (I != NameSpaceCache.end())
1701 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1702
Devang Patel8ab870d2010-05-12 23:46:38 +00001703 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001704
1705 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001706 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001707 llvm::DINameSpace NS =
1708 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001709 llvm::DIFile(Unit), LineNo);
1710 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001711 return NS;
1712}