blob: 019779bd05e236fd854b77b8cd20b091b80b99aa [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"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000039 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000040 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000041 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000042}
43
Chris Lattner9c85ba32008-11-10 06:08:34 +000044CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000045 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048void CGDebugInfo::setLocation(SourceLocation Loc) {
49 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000050 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000051}
52
Devang Patel33583052010-01-28 23:15:27 +000053/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000054llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000055 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000056 if (!Context)
57 return CompileUnit;
58
59 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
60 I = RegionMap.find(Context);
61 if (I != RegionMap.end())
62 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000063
Devang Pateleb6d79b2010-02-01 21:34:11 +000064 // Check namespace.
65 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
66 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000067
68 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
69 if (!RDecl->isDependentType()) {
70 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
71 llvm::DIFile(CompileUnit));
72 return llvm::DIDescriptor(Ty);
73 }
74 }
Devang Patel979ec2e2009-10-06 00:35:31 +000075 return CompileUnit;
76}
77
Devang Patel9c6c3a02010-01-14 00:36:21 +000078/// getFunctionName - Get function name for the given FunctionDecl. If the
79/// name is constructred on demand (e.g. C++ destructor) then the name
80/// is stored on the side.
81llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
82 assert (FD && "Invalid FunctionDecl!");
83 IdentifierInfo *FII = FD->getIdentifier();
84 if (FII)
85 return FII->getName();
86
87 // Otherwise construct human readable name for debug info.
88 std::string NS = FD->getNameAsString();
89
90 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000091 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000092 memcpy(StrPtr, NS.data(), NS.length());
93 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000094}
95
Devang Patel17800552010-03-09 00:44:50 +000096/// getOrCreateFile - Get the file debug info descriptor for the input location.
97llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +000098 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +000099 // If Location is not valid then use main input file.
100 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
101 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000102 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000103 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000104
105 // Cache the results.
106 const char *fname = PLoc.getFilename();
107 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
108 DIFileCache.find(fname);
109
110 if (it != DIFileCache.end()) {
111 // Verify that the information still exists.
112 if (&*it->second)
113 return llvm::DIFile(cast<llvm::MDNode>(it->second));
114 }
115
116 // FIXME: We shouldn't even need to call 'makeAbsolute()' in the cases
117 // where we can consult the FileEntry.
Devang Patel17800552010-03-09 00:44:50 +0000118 llvm::sys::Path AbsFileName(PLoc.getFilename());
Benjamin Kramer47daf682009-12-08 11:02:29 +0000119 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000120
Ted Kremenek9c250392010-03-30 00:27:51 +0000121 llvm::DIFile F = DebugFactory.CreateFile(AbsFileName.getLast(),
122 AbsFileName.getDirname(), TheCU);
123
Devang Patelab699792010-05-07 18:12:35 +0000124 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000125 return F;
126
Devang Patel17800552010-03-09 00:44:50 +0000127}
Devang Patel8ab870d2010-05-12 23:46:38 +0000128
129/// getLineNumber - Get line number for the location. If location is invalid
130/// then use current location.
131unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
132 assert (CurLoc.isValid() && "Invalid current location!");
133 SourceManager &SM = CGM.getContext().getSourceManager();
134 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
135 return PLoc.getLine();
136}
137
138/// getColumnNumber - Get column number for the location. If location is
139/// invalid then use current location.
140unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
141 assert (CurLoc.isValid() && "Invalid current location!");
142 SourceManager &SM = CGM.getContext().getSourceManager();
143 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
144 return PLoc.getColumn();
145}
146
Devang Patel17800552010-03-09 00:44:50 +0000147/// CreateCompileUnit - Create new compile unit.
148void CGDebugInfo::CreateCompileUnit() {
149
150 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000151 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000152 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
153 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000154 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000155
Devang Patel22fe5852010-03-12 21:04:27 +0000156 llvm::sys::Path AbsFileName(MainFileName);
Devang Patel17800552010-03-09 00:44:50 +0000157 AbsFileName.makeAbsolute();
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000158
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000159 // The main file name provided via the "-main-file-name" option contains just
160 // the file name itself with no path information. This file name may have had
161 // a relative path, so we look into the actual file entry for the main
162 // file to determine the real absolute path for the file.
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000163 std::string MainFileDir;
164 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID()))
165 MainFileDir = MainFile->getDir()->getName();
166 else
167 MainFileDir = AbsFileName.getDirname();
168
Chris Lattner515455a2009-03-25 03:28:08 +0000169 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000170 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000171 if (LO.CPlusPlus) {
172 if (LO.ObjC1)
173 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
174 else
175 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
176 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000177 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000178 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000179 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000180 } else {
181 LangTag = llvm::dwarf::DW_LANG_C89;
182 }
Devang Patel446c6192009-04-17 21:06:59 +0000183
Benjamin Kramer47daf682009-12-08 11:02:29 +0000184 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000185#ifdef CLANG_VENDOR
186 CLANG_VENDOR
187#endif
188 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000189
190 // Figure out which version of the ObjC runtime we have.
191 unsigned RuntimeVers = 0;
192 if (LO.ObjC1)
193 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000195 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000196 TheCU = DebugFactory.CreateCompileUnit(
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000197 LangTag, AbsFileName.getLast(), MainFileDir, Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000198 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000199}
200
Devang Patel65e99f22009-02-25 01:36:11 +0000201/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000202/// one if necessary.
203llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000204 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000205 unsigned Encoding = 0;
206 switch (BT->getKind()) {
207 default:
208 case BuiltinType::Void:
209 return llvm::DIType();
210 case BuiltinType::UChar:
211 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
212 case BuiltinType::Char_S:
213 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
214 case BuiltinType::UShort:
215 case BuiltinType::UInt:
216 case BuiltinType::ULong:
217 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
218 case BuiltinType::Short:
219 case BuiltinType::Int:
220 case BuiltinType::Long:
221 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
222 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
223 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000224 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000226 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000227 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000228 uint64_t Size = CGM.getContext().getTypeSize(BT);
229 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000230 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Devang Patelca80a5f2009-10-20 19:55:01 +0000232 llvm::DIType DbgTy =
233 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000234 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000235 Unit, 0, Size, Align,
236 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000237 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000238}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000239
Chris Lattnerb7003772009-04-23 06:13:01 +0000240llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000241 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000242 // Bit size, align and offset of the type.
243 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
244 if (Ty->isComplexIntegerType())
245 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Anders Carlsson20f12a22009-12-06 18:00:51 +0000247 uint64_t Size = CGM.getContext().getTypeSize(Ty);
248 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000249 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Devang Patelca80a5f2009-10-20 19:55:01 +0000251 llvm::DIType DbgTy =
252 DebugFactory.CreateBasicType(Unit, "complex",
253 Unit, 0, Size, Align,
254 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000255 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000256}
257
John McCalla1805292009-09-25 01:40:47 +0000258/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000259/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000260llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000261 QualifierCollector Qc;
262 const Type *T = Qc.strip(Ty);
263
264 // Ignore these qualifiers for now.
265 Qc.removeObjCGCAttr();
266 Qc.removeAddressSpace();
267
Chris Lattner9c85ba32008-11-10 06:08:34 +0000268 // We will create one Derived type for one qualifier and recurse to handle any
269 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000270 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000271 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000272 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000273 Qc.removeConst();
274 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000276 Qc.removeVolatile();
277 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000279 Qc.removeRestrict();
280 } else {
281 assert(Qc.empty() && "Unknown type qualifier for debug info");
282 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
John McCalla1805292009-09-25 01:40:47 +0000285 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
286
Daniel Dunbar3845f862008-10-31 03:54:29 +0000287 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
288 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000289 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000290 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000291 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000292 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000293}
294
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000295llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000296 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000297 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000298 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
299 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000300 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000301}
302
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000304 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000305 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
306 Ty->getPointeeType(), Unit);
307}
308
309llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
310 const Type *Ty,
311 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000312 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000313 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000315 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000316
317 // Size is always the size of a pointer. We can't use getTypeSize here
318 // because that does not return the correct value for references.
319 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000320 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
321 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Devang Patelca80a5f2009-10-20 19:55:01 +0000323 return
Devang Pateld58562e2010-03-09 22:49:11 +0000324 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000325 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000326
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000327}
328
Mike Stump9bc093c2009-05-14 02:03:51 +0000329llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000330 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 if (BlockLiteralGenericSet)
332 return BlockLiteralGeneric;
333
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
335
336 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
337
338 llvm::DIType FieldTy;
339
340 QualType FType;
341 uint64_t FieldSize, FieldOffset;
342 unsigned FieldAlign;
343
344 llvm::DIArray Elements;
345 llvm::DIType EltTy, DescTy;
346
347 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000348 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000349 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
350 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000351
Daniel Dunbarca308df2009-05-26 19:40:20 +0000352 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 EltTys.clear();
354
Mike Stump3d363c52009-10-02 02:30:50 +0000355 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000356 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000357
Mike Stump9bc093c2009-05-14 02:03:51 +0000358 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000359 Unit, LineNo, FieldOffset, 0, 0,
360 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Mike Stump9bc093c2009-05-14 02:03:51 +0000362 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 uint64_t Size = CGM.getContext().getTypeSize(Ty);
364 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Mike Stump9bc093c2009-05-14 02:03:51 +0000366 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000367 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000368 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000369
370 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000371 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000372 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000373 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000374 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
375 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000376 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000377 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000378
Anders Carlsson20f12a22009-12-06 18:00:51 +0000379 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000380 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000381 FieldSize = CGM.getContext().getTypeSize(Ty);
382 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000383 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000384 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000385 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000386 FieldOffset, 0, FieldTy);
387 EltTys.push_back(FieldTy);
388
389 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000390 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000391
392 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000393 Unit, LineNo, FieldOffset, 0, 0,
394 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Mike Stump9bc093c2009-05-14 02:03:51 +0000396 BlockLiteralGenericSet = true;
397 BlockLiteralGeneric
398 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000399 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000400 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 return BlockLiteralGeneric;
402}
403
Chris Lattner9c85ba32008-11-10 06:08:34 +0000404llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000405 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000406 // Typedefs are derived from some other type. If we have a typedef of a
407 // typedef, make sure to emit the whole chain.
408 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner9c85ba32008-11-10 06:08:34 +0000410 // We don't set size information, but do specify where the typedef was
411 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000412 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000413
Devang Pateleb6d79b2010-02-01 21:34:11 +0000414 llvm::DIDescriptor TyContext
415 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
416 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000417 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000418 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000419 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000420 Ty->getDecl()->getName(), Unit,
421 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000422 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000423}
424
Chris Lattner9c85ba32008-11-10 06:08:34 +0000425llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000426 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000427 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Add the result type at least.
430 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432 // Set up remainder of arguments if there is a prototype.
433 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000434 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
436 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
437 } else {
438 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000439 }
440
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000442 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Devang Patelca80a5f2009-10-20 19:55:01 +0000444 llvm::DIType DbgTy =
445 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000446 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000447 0, 0, 0, 0, 0,
448 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000449 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000450}
451
Devang Patel428deb52010-01-19 00:00:59 +0000452/// CollectRecordFields - A helper function to collect debug info for
453/// record fields. This is used while creating debug info entry for a Record.
454void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000455CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000456 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
457 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000458 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
459 for (RecordDecl::field_iterator I = RD->field_begin(),
460 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000461 I != E; ++I, ++FieldNo) {
462 FieldDecl *Field = *I;
463 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
464
465 llvm::StringRef FieldName = Field->getName();
466
Devang Patel4835fdd2010-02-12 01:31:06 +0000467 // Ignore unnamed fields. Do not ignore unnamed records.
468 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000469 continue;
470
471 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000472 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
473 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000474 QualType FType = Field->getType();
475 uint64_t FieldSize = 0;
476 unsigned FieldAlign = 0;
477 if (!FType->isIncompleteArrayType()) {
478
479 // Bit size, align and offset of the type.
480 FieldSize = CGM.getContext().getTypeSize(FType);
481 Expr *BitWidth = Field->getBitWidth();
482 if (BitWidth)
483 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
484
485 FieldAlign = CGM.getContext().getTypeAlign(FType);
486 }
487
488 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
489
Devang Patel71f4ff62010-04-21 23:12:37 +0000490 unsigned Flags = 0;
491 AccessSpecifier Access = I->getAccess();
492 if (Access == clang::AS_private)
493 Flags |= llvm::DIType::FlagPrivate;
494 else if (Access == clang::AS_protected)
495 Flags |= llvm::DIType::FlagProtected;
496
Devang Patel428deb52010-01-19 00:00:59 +0000497 // Create a DW_TAG_member node to remember the offset of this field in the
498 // struct. FIXME: This is an absolutely insane way to capture this
499 // information. When we gut debug info, this should be fixed.
500 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
501 FieldName, FieldDefUnit,
502 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000503 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000504 EltTys.push_back(FieldTy);
505 }
506}
507
Devang Patela6da1922010-01-28 00:28:01 +0000508/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
509/// function type is not updated to include implicit "this" pointer. Use this
510/// routine to get a method type which includes "this" pointer.
511llvm::DIType
512CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000513 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000514 llvm::DIType FnTy
515 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
516 0),
517 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000518
519 // Static methods do not need "this" pointer argument.
520 if (Method->isStatic())
521 return FnTy;
522
Devang Patela6da1922010-01-28 00:28:01 +0000523 // Add "this" pointer.
524
Devang Patelab699792010-05-07 18:12:35 +0000525 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000526 assert (Args.getNumElements() && "Invalid number of arguments!");
527
528 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
529
530 // First element is always return type. For 'void' functions it is NULL.
531 Elts.push_back(Args.getElement(0));
532
533 // "this" pointer is always first argument.
534 ASTContext &Context = CGM.getContext();
535 QualType ThisPtr =
536 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000537 llvm::DIType ThisPtrType =
538 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patelab699792010-05-07 18:12:35 +0000539 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000540 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000541
542 // Copy rest of the arguments.
543 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
544 Elts.push_back(Args.getElement(i));
545
546 llvm::DIArray EltTypeArray =
547 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
548
549 return
550 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000551 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000552 0, 0, 0, 0, 0,
553 llvm::DIType(), EltTypeArray);
554}
555
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000556/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
557/// a single member function GlobalDecl.
558llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000559CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000560 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000561 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000562 bool IsCtorOrDtor =
563 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
564
565 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000566 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000567
568 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
569 // make sense to give a single ctor/dtor a linkage name.
John McCallf746aa62010-03-19 23:29:14 +0000570 MangleBuffer MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571 if (!IsCtorOrDtor)
John McCallf746aa62010-03-19 23:29:14 +0000572 CGM.getMangledName(MethodLinkageName, Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000573
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000574 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000575 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
576 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000577
578 // Collect virtual method info.
579 llvm::DIType ContainingType;
580 unsigned Virtuality = 0;
581 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000582
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000583 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000584 if (Method->isPure())
585 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
586 else
587 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
588
589 // It doesn't make sense to give a virtual destructor a vtable index,
590 // since a single destructor has two entries in the vtable.
591 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000592 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000593 ContainingType = RecordTy;
594 }
595
596 llvm::DISubprogram SP =
597 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
598 MethodLinkageName,
599 MethodDefUnit, MethodLine,
600 MethodTy, /*isLocalToUnit=*/false,
601 Method->isThisDeclarationADefinition(),
602 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000603
604 // Don't cache ctors or dtors since we have to emit multiple functions for
605 // a single ctor or dtor.
606 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000607 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000608
609 return SP;
610}
611
Devang Patel4125fd22010-01-19 01:54:44 +0000612/// CollectCXXMemberFunctions - A helper function to collect debug info for
613/// C++ member functions.This is used while creating debug info entry for
614/// a Record.
615void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000616CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000617 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
618 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000619 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
620 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000621 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000622
Devang Pateld5322da2010-02-09 19:09:28 +0000623 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000624 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000625
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000627 }
628}
629
Devang Patela245c5b2010-01-25 23:32:18 +0000630/// CollectCXXBases - A helper function to collect debug info for
631/// C++ base classes. This is used while creating debug info entry for
632/// a Record.
633void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000634CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000635 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
636 llvm::DICompositeType &RecordTy) {
637
Devang Patel239cec62010-02-01 21:39:52 +0000638 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
639 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
640 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000641 unsigned BFlags = 0;
642 uint64_t BaseOffset;
643
644 const CXXRecordDecl *Base =
645 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
646
647 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000648 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000649 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000650 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000651 BFlags = llvm::DIType::FlagVirtual;
652 } else
653 BaseOffset = RL.getBaseClassOffset(Base);
654
655 AccessSpecifier Access = BI->getAccessSpecifier();
656 if (Access == clang::AS_private)
657 BFlags |= llvm::DIType::FlagPrivate;
658 else if (Access == clang::AS_protected)
659 BFlags |= llvm::DIType::FlagProtected;
660
661 llvm::DIType DTy =
662 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
663 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000664 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000665 BaseOffset, BFlags,
666 getOrCreateType(BI->getType(),
667 Unit));
668 EltTys.push_back(DTy);
669 }
Devang Patela245c5b2010-01-25 23:32:18 +0000670}
671
Devang Patel4ce3f202010-01-28 18:11:52 +0000672/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000673llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000674 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000675 return VTablePtrType;
676
677 ASTContext &Context = CGM.getContext();
678
679 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000680 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
681 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000682 llvm::DIType SubTy =
683 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000684 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000685 0, 0, 0, 0, 0, llvm::DIType(), SElements);
686
687 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
688 llvm::DIType vtbl_ptr_type
689 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000690 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000691 0, Size, 0, 0, 0, SubTy);
692
Devang Pateld58562e2010-03-09 22:49:11 +0000693 VTablePtrType =
694 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
695 Unit, "", Unit,
696 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000697 return VTablePtrType;
698}
699
Anders Carlsson046c2942010-04-17 20:15:18 +0000700/// getVTableName - Get vtable name for the given Class.
701llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000702 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000703 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000704
705 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000706 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000707 memcpy(StrPtr, Name.data(), Name.length());
708 return llvm::StringRef(StrPtr, Name.length());
709}
710
711
Anders Carlsson046c2942010-04-17 20:15:18 +0000712/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000713/// debug info entry in EltTys vector.
714void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000715CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000716 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000717 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000718
719 // If there is a primary base then it will hold vtable info.
720 if (RL.getPrimaryBase())
721 return;
722
723 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000724 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000725 return;
726
727 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
728 llvm::DIType VPTR
729 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000730 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000731 0, Size, 0, 0, 0,
732 getOrCreateVTablePtrType(Unit));
733 EltTys.push_back(VPTR);
734}
735
Devang Patel65e99f22009-02-25 01:36:11 +0000736/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000737llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000738 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000739 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattner9c85ba32008-11-10 06:08:34 +0000741 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000742 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000743 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000744 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000745 Tag = llvm::dwarf::DW_TAG_union_type;
746 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000747 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000748 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000749 }
750
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000752 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
753 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755 // Records and classes and unions can all be recursive. To handle them, we
756 // first generate a debug descriptor for the struct as a forward declaration.
757 // Then (if it is a definition) we go through and get debug info for all of
758 // its members. Finally, we create a descriptor for the complete type (which
759 // may refer to the forward decl if the struct is recursive) and replace all
760 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000761
Devang Pateld6c5a262010-02-01 21:52:22 +0000762 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000763 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000764 llvm::SmallString<128> FwdDeclName;
765 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel411894b2010-02-01 22:40:08 +0000766 llvm::DIDescriptor FDContext =
767 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000768 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000769 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000770 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000771 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Chris Lattner9c85ba32008-11-10 06:08:34 +0000773 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000774 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000775 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000776
Devang Patelab699792010-05-07 18:12:35 +0000777 llvm::MDNode *MN = FwdDecl;
778 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000779 // Otherwise, insert it into the TypeCache so that recursive uses will find
780 // it.
Devang Patelab699792010-05-07 18:12:35 +0000781 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000782 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000783 RegionStack.push_back(FwdDeclNode);
784 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000785
786 // Convert all the elements.
787 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
788
Devang Pateld6c5a262010-02-01 21:52:22 +0000789 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000790 if (CXXDecl) {
791 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000792 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000793 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000794 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000795 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000796 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000797 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000798
799 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000800 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000801 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
802 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000803 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000804 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000805 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Chris Lattner9c85ba32008-11-10 06:08:34 +0000808 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000809 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000810
811 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000812 uint64_t Size = CGM.getContext().getTypeSize(Ty);
813 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Devang Patele4c1ea02010-03-11 20:01:48 +0000815 RegionStack.pop_back();
816 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
817 RegionMap.find(Ty->getDecl());
818 if (RI != RegionMap.end())
819 RegionMap.erase(RI);
820
Devang Patel411894b2010-02-01 22:40:08 +0000821 llvm::DIDescriptor RDContext =
822 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000823 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000824 DebugFactory.CreateCompositeType(Tag, RDContext,
825 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000826 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000827 llvm::DIType(), Elements,
828 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000829
830 // Now that we have a real decl for the struct, replace anything using the
831 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000832 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000833 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 return RealDecl;
835}
836
Devang Patel9ca36b62009-02-26 21:10:26 +0000837/// CreateType - get objective-c interface type.
838llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000839 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000840 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000841 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000842
843 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000844 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000845 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000846 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000847
Devang Patel9ca36b62009-02-26 21:10:26 +0000848 // To handle recursive interface, we
849 // first generate a debug descriptor for the struct as a forward declaration.
850 // Then (if it is a definition) we go through and get debug info for all of
851 // its members. Finally, we create a descriptor for the complete type (which
852 // may refer to the forward decl if the struct is recursive) and replace all
853 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000854 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000855 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000856 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000857 llvm::DIType(), llvm::DIArray(),
858 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Devang Patel9ca36b62009-02-26 21:10:26 +0000860 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000861 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000862 return FwdDecl;
863
Devang Patelab699792010-05-07 18:12:35 +0000864 llvm::MDNode *MN = FwdDecl;
865 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000866 // Otherwise, insert it into the TypeCache so that recursive uses will find
867 // it.
Devang Patelab699792010-05-07 18:12:35 +0000868 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000869 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000870 RegionStack.push_back(FwdDeclNode);
871 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000872
873 // Convert all the elements.
874 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
875
Devang Pateld6c5a262010-02-01 21:52:22 +0000876 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000877 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000878 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000879 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000880 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000881 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000882 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000883 0 /* offset */, 0, SClassTy);
884 EltTys.push_back(InhTag);
885 }
886
Devang Pateld6c5a262010-02-01 21:52:22 +0000887 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000888
889 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000890 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
891 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000892 ObjCIvarDecl *Field = *I;
893 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
894
Devang Patel73621622009-11-25 17:37:31 +0000895 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000896
Devang Patelde135022009-04-27 22:40:36 +0000897 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000898 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000899 continue;
900
Devang Patel9ca36b62009-02-26 21:10:26 +0000901 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000902 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
903 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +0000904 QualType FType = Field->getType();
905 uint64_t FieldSize = 0;
906 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000907
Devang Patel99c20eb2009-03-20 18:24:39 +0000908 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Devang Patel99c20eb2009-03-20 18:24:39 +0000910 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000911 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000912 Expr *BitWidth = Field->getBitWidth();
913 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000914 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000915
Anders Carlsson20f12a22009-12-06 18:00:51 +0000916 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000917 }
918
Mike Stump1eb44332009-09-09 15:08:12 +0000919 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
920
Devang Patelc20482b2009-03-19 00:23:53 +0000921 unsigned Flags = 0;
922 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
923 Flags = llvm::DIType::FlagProtected;
924 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
925 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Devang Patel9ca36b62009-02-26 21:10:26 +0000927 // Create a DW_TAG_member node to remember the offset of this field in the
928 // struct. FIXME: This is an absolutely insane way to capture this
929 // information. When we gut debug info, this should be fixed.
930 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
931 FieldName, FieldDefUnit,
932 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000933 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000934 EltTys.push_back(FieldTy);
935 }
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Devang Patel9ca36b62009-02-26 21:10:26 +0000937 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000938 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000939
Devang Patele4c1ea02010-03-11 20:01:48 +0000940 RegionStack.pop_back();
941 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
942 RegionMap.find(Ty->getDecl());
943 if (RI != RegionMap.end())
944 RegionMap.erase(RI);
945
Devang Patel9ca36b62009-02-26 21:10:26 +0000946 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000947 uint64_t Size = CGM.getContext().getTypeSize(Ty);
948 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Devang Patel6c1fddf2009-07-27 18:42:03 +0000950 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000951 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000952 Line, Size, Align, 0, 0, llvm::DIType(),
953 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000954
955 // Now that we have a real decl for the struct, replace anything using the
956 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000957 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000958 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000959
Devang Patel9ca36b62009-02-26 21:10:26 +0000960 return RealDecl;
961}
962
Chris Lattner9c85ba32008-11-10 06:08:34 +0000963llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000964 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000965 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000966
967 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
968
969 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000970 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000971 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000972 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000973 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000974 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattner9c85ba32008-11-10 06:08:34 +0000977 // Return a CompositeType for the enum itself.
978 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000979 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000980
Devang Patel8ab870d2010-05-12 23:46:38 +0000981 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
982 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Chris Lattner9c85ba32008-11-10 06:08:34 +0000984 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000985 uint64_t Size = 0;
986 unsigned Align = 0;
987 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000988 Size = CGM.getContext().getTypeSize(Ty);
989 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Devang Patelca80a5f2009-10-20 19:55:01 +0000992 llvm::DIType DbgTy =
993 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +0000994 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +0000995 Size, Align, 0, 0,
996 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000997 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998}
999
1000llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001001 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001002 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1003 return CreateType(RT, Unit);
1004 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1005 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner9c85ba32008-11-10 06:08:34 +00001007 return llvm::DIType();
1008}
1009
Devang Patel70c23cd2010-02-23 22:59:39 +00001010llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001011 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001012 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1013 uint64_t NumElems = Ty->getNumElements();
1014 if (NumElems > 0)
1015 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001016
Benjamin Kramerad468862010-03-30 11:36:44 +00001017 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1018 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001019
1020 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1021 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1022
1023 return
1024 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001025 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001026 0, Size, Align, 0, 0,
1027 ElementTy, SubscriptArray);
1028}
1029
Chris Lattner9c85ba32008-11-10 06:08:34 +00001030llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001031 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001032 uint64_t Size;
1033 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001034
1035
Nuno Lopes010d5142009-01-28 00:35:17 +00001036 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001038 Size = 0;
1039 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001040 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001041 } else if (Ty->isIncompleteArrayType()) {
1042 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001043 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001044 } else {
1045 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 Size = CGM.getContext().getTypeSize(Ty);
1047 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Chris Lattner9c85ba32008-11-10 06:08:34 +00001050 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1051 // interior arrays, do we care? Why aren't nested arrays represented the
1052 // obvious/recursive way?
1053 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1054 QualType EltTy(Ty, 0);
1055 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001056 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001057 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001058 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001059 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060 // FIXME: Verify this is right for VLAs.
1061 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1062 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner9c85ba32008-11-10 06:08:34 +00001065 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001066 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067
Devang Patelca80a5f2009-10-20 19:55:01 +00001068 llvm::DIType DbgTy =
1069 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001070 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001071 0, Size, Align, 0, 0,
1072 getOrCreateType(EltTy, Unit),
1073 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001074 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075}
1076
Anders Carlssona031b352009-11-06 19:19:55 +00001077llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001078 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001079 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1080 Ty, Ty->getPointeeType(), Unit);
1081}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082
Anders Carlsson20f12a22009-12-06 18:00:51 +00001083llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001084 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001085 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1086 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1087
1088 if (!Ty->getPointeeType()->isFunctionType()) {
1089 // We have a data member pointer type.
1090 return PointerDiffDITy;
1091 }
1092
1093 // We have a member function pointer type. Treat it as a struct with two
1094 // ptrdiff_t members.
1095 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1096
1097 uint64_t FieldOffset = 0;
1098 llvm::DIDescriptor ElementTypes[2];
1099
1100 // FIXME: This should probably be a function type instead.
1101 ElementTypes[0] =
1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001103 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001104 Info.first, Info.second, FieldOffset, 0,
1105 PointerDiffDITy);
1106 FieldOffset += Info.first;
1107
1108 ElementTypes[1] =
1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001110 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001111 Info.first, Info.second, FieldOffset, 0,
1112 PointerDiffDITy);
1113
1114 llvm::DIArray Elements =
1115 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1116 llvm::array_lengthof(ElementTypes));
1117
1118 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1119 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001120 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001121 0, 0, 0, llvm::DIType(), Elements);
1122}
1123
Douglas Gregor840943d2009-12-21 20:18:30 +00001124static QualType UnwrapTypeForDebugInfo(QualType T) {
1125 do {
1126 QualType LastT = T;
1127 switch (T->getTypeClass()) {
1128 default:
1129 return T;
1130 case Type::TemplateSpecialization:
1131 T = cast<TemplateSpecializationType>(T)->desugar();
1132 break;
1133 case Type::TypeOfExpr: {
1134 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1135 T = Ty->getUnderlyingExpr()->getType();
1136 break;
1137 }
1138 case Type::TypeOf:
1139 T = cast<TypeOfType>(T)->getUnderlyingType();
1140 break;
1141 case Type::Decltype:
1142 T = cast<DecltypeType>(T)->getUnderlyingType();
1143 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001144 case Type::Elaborated:
1145 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001146 break;
1147 case Type::SubstTemplateTypeParm:
1148 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1149 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001150 }
1151
1152 assert(T != LastT && "Type unwrapping failed to unwrap!");
1153 if (T == LastT)
1154 return T;
1155 } while (true);
1156
1157 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001158}
1159
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001160/// getOrCreateType - Get the type from the cache or create a new
1161/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001162llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001163 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001164 if (Ty.isNull())
1165 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Douglas Gregor840943d2009-12-21 20:18:30 +00001167 // Unwrap the type as needed for debug information.
1168 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001169
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001170 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001171 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001172 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001173 if (it != TypeCache.end()) {
1174 // Verify that the debug info still exists.
1175 if (&*it->second)
1176 return llvm::DIType(cast<llvm::MDNode>(it->second));
1177 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001178
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001179 // Otherwise create the type.
1180 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001181
1182 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001183 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001184 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001185}
1186
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001187/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001188llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001189 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001190 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001191 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001192 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001193
Douglas Gregor2101a822009-12-21 19:57:21 +00001194 const char *Diag = 0;
1195
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001196 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001197 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001198#define TYPE(Class, Base)
1199#define ABSTRACT_TYPE(Class, Base)
1200#define NON_CANONICAL_TYPE(Class, Base)
1201#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1202#include "clang/AST/TypeNodes.def"
1203 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001204
Anders Carlssonbfe69952009-11-06 18:24:04 +00001205 // FIXME: Handle these.
1206 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001207 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001208
1209 case Type::Vector:
1210 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001211 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001212 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001213 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001214 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1215 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1216 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1217 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001218 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001219 return CreateType(cast<BlockPointerType>(Ty), Unit);
1220 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001221 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001222 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001223 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001224 case Type::FunctionProto:
1225 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001226 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001227 case Type::ConstantArray:
1228 case Type::VariableArray:
1229 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001230 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001231
1232 case Type::LValueReference:
1233 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1234
Anders Carlsson20f12a22009-12-06 18:00:51 +00001235 case Type::MemberPointer:
1236 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001237
1238 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001239 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001240 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001241 case Type::TypeOfExpr:
1242 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001243 case Type::Decltype:
1244 llvm_unreachable("type should have been unwrapped!");
1245 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001246
1247 case Type::RValueReference:
1248 // FIXME: Implement!
1249 Diag = "rvalue references";
1250 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001251 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001252
1253 assert(Diag && "Fall through without a diagnostic?");
1254 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1255 "debug information for %0 is not yet supported");
1256 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1257 << Diag;
1258 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001259}
1260
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001261/// CreateMemberType - Create new member and increase Offset by FType's size.
1262llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1263 llvm::StringRef Name,
1264 uint64_t *Offset) {
1265 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1266 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1267 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1268 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1269 Unit, Name, Unit, 0,
1270 FieldSize, FieldAlign,
1271 *Offset, 0, FieldTy);
1272 *Offset += FieldSize;
1273 return Ty;
1274}
1275
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001276/// EmitFunctionStart - Constructs the debug code for entering a function -
1277/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001278void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001279 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001280 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Devang Patel9c6c3a02010-01-14 00:36:21 +00001282 llvm::StringRef Name;
John McCallf746aa62010-03-19 23:29:14 +00001283 MangleBuffer LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001284
1285 const Decl *D = GD.getDecl();
1286 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001287 // If there is a DISubprogram for this function available then use it.
1288 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1289 FI = SPCache.find(FD);
1290 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001291 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001292 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1293 llvm::MDNode *SPN = SP;
1294 RegionStack.push_back(SPN);
1295 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001296 return;
1297 }
1298 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001299 Name = getFunctionName(FD);
1300 // Use mangled name as linkage name for c/c++ functions.
John McCallf746aa62010-03-19 23:29:14 +00001301 CGM.getMangledName(LinkageName, GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001302 } else {
1303 // Use llvm function name as linkage name.
1304 Name = Fn->getName();
John McCallf746aa62010-03-19 23:29:14 +00001305 LinkageName.setString(Name);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001306 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001307 if (!Name.empty() && Name[0] == '\01')
1308 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Devang Patel970c6182010-04-24 00:49:16 +00001310 // It is expected that CurLoc is set before using EmitFunctionStart.
1311 // Usually, CurLoc points to the left bracket location of compound
1312 // statement representing function body.
1313 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001314 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattner9c85ba32008-11-10 06:08:34 +00001316 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001317 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001318 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001319 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001321 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001322 llvm::MDNode *SPN = SP;
1323 RegionStack.push_back(SPN);
1324 RegionMap[D] = llvm::WeakVH(SP);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001325}
1326
1327
Chris Lattner9c85ba32008-11-10 06:08:34 +00001328void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001329 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001331 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001332 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001333 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001334 || (SM.getInstantiationLineNumber(CurLoc) ==
1335 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001336 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001337 // New Builder may not be in sync with CGDebugInfo.
1338 if (!Builder.getCurrentDebugLocation().isUnknown())
1339 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001340
1341 // Update last state.
1342 PrevLoc = CurLoc;
1343
Chris Lattnerc6034632010-04-01 06:31:43 +00001344 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001345 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1346 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001347 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001348}
1349
1350/// EmitRegionStart- Constructs the debug code for entering a declarative
1351/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001352void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001353 llvm::DIDescriptor D =
1354 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1355 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001356 llvm::DIDescriptor(RegionStack.back()),
Devang Patel8ab870d2010-05-12 23:46:38 +00001357 getLineNumber(CurLoc),
1358 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001359 llvm::MDNode *DN = D;
1360 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001361}
1362
1363/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1364/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001365void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001366 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1367
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001368 // Provide an region stop point.
1369 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001370
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001371 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001372}
1373
Devang Patel809b9bb2010-02-10 18:49:08 +00001374// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1375// See BuildByRefType.
1376llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1377 uint64_t *XOffset) {
1378
1379 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1380
1381 QualType FType;
1382 uint64_t FieldSize, FieldOffset;
1383 unsigned FieldAlign;
1384
Devang Patel17800552010-03-09 00:44:50 +00001385 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001386 QualType Type = VD->getType();
1387
1388 FieldOffset = 0;
1389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001390 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1391 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001392 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001393 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1394 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1395
Devang Patel809b9bb2010-02-10 18:49:08 +00001396 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1397 if (HasCopyAndDispose) {
1398 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001399 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1400 &FieldOffset));
1401 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1402 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001403 }
1404
1405 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1406 if (Align > CharUnits::fromQuantity(
1407 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1408 unsigned AlignedOffsetInBytes
1409 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1410 unsigned NumPaddingBytes
1411 = AlignedOffsetInBytes - FieldOffset/8;
1412
1413 if (NumPaddingBytes > 0) {
1414 llvm::APInt pad(32, NumPaddingBytes);
1415 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1416 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001417 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001418 }
1419 }
1420
1421 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001422 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001423 FieldSize = CGM.getContext().getTypeSize(FType);
1424 FieldAlign = Align.getQuantity()*8;
1425
1426 *XOffset = FieldOffset;
1427 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001428 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001429 0, FieldSize, FieldAlign,
1430 FieldOffset, 0, FieldTy);
1431 EltTys.push_back(FieldTy);
1432 FieldOffset += FieldSize;
1433
1434 llvm::DIArray Elements =
1435 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1436
1437 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1438
1439 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001440 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001441 0, FieldOffset, 0, 0, Flags,
1442 llvm::DIType(), Elements);
1443
1444}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001445/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001446void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001447 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001448 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1449
Devang Patel17800552010-03-09 00:44:50 +00001450 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001451 llvm::DIType Ty;
1452 uint64_t XOffset = 0;
1453 if (VD->hasAttr<BlocksAttr>())
1454 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1455 else
1456 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001457
Devang Patelf4e54a22010-05-07 23:05:55 +00001458 // If there is not any debug info for type then do not emit debug info
1459 // for this variable.
1460 if (!Ty)
1461 return;
1462
Chris Lattner9c85ba32008-11-10 06:08:34 +00001463 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001464 unsigned Line = getLineNumber(VD->getLocation());
1465 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Chris Lattner9c85ba32008-11-10 06:08:34 +00001467 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001468 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001469 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001470 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001471 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001472 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001473 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001474 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001475
Chris Lattnerc6034632010-04-01 06:31:43 +00001476 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001477 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001478}
1479
Mike Stumpb1a6e682009-09-30 02:43:10 +00001480/// EmitDeclare - Emit local variable declaration debug info.
1481void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1482 llvm::Value *Storage, CGBuilderTy &Builder,
1483 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001484 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001485 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1486
Devang Patel2b594b92010-04-26 23:28:46 +00001487 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001488 return;
1489
1490 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001491 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001492 llvm::DIType Ty;
1493 if (VD->hasAttr<BlocksAttr>())
1494 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1495 else
1496 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001497
1498 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001499 unsigned Line = getLineNumber(VD->getLocation());
1500 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001501
Devang Pateld6c5a262010-02-01 21:52:22 +00001502 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001503 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001504 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1505 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1506 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1507 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001508 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001509 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1510 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001511 // offset of __forwarding field
1512 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001513 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1514 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1515 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001516 // offset of x field
1517 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001518 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001519 }
1520
1521 // Create the descriptor for the variable.
1522 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001523 DebugFactory.CreateComplexVariable(Tag,
1524 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001525 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001526 addr);
1527 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001528 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001529 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001530
Chris Lattnerc6034632010-04-01 06:31:43 +00001531 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001532 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001533}
1534
Devang Pateld6c5a262010-02-01 21:52:22 +00001535void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001536 llvm::Value *Storage,
1537 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001538 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001539}
1540
Mike Stumpb1a6e682009-09-30 02:43:10 +00001541void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1542 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1543 CodeGenFunction *CGF) {
1544 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1545}
1546
Chris Lattner9c85ba32008-11-10 06:08:34 +00001547/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1548/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001549void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001550 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001551 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001552}
1553
1554
1555
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001556/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001557void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001558 const VarDecl *D) {
1559
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001560 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001561 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001562 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001563
Devang Pateleb6d79b2010-02-01 21:34:11 +00001564 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001565 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001567 // CodeGen turns int[] into int[1] so we'll do the same here.
1568 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001570 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001571 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Anders Carlsson20f12a22009-12-06 18:00:51 +00001573 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001574 ArrayType::Normal, 0);
1575 }
Devang Patel5d822f02010-04-29 17:48:37 +00001576 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001577 llvm::StringRef LinkageName;
1578 if (D->getDeclContext() && isa<FunctionDecl>(D->getDeclContext()))
1579 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001580 llvm::DIDescriptor DContext =
1581 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001582 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1583 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001584 Var->hasInternalLinkage(),
1585 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001586}
1587
Devang Patel9ca36b62009-02-26 21:10:26 +00001588/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001589void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001590 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001591 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001592 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001593 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001594
Devang Pateld6c5a262010-02-01 21:52:22 +00001595 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001596
Devang Pateld6c5a262010-02-01 21:52:22 +00001597 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001598 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Devang Patel9ca36b62009-02-26 21:10:26 +00001600 // CodeGen turns int[] into int[1] so we'll do the same here.
1601 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Devang Patel9ca36b62009-02-26 21:10:26 +00001603 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001604 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Anders Carlsson20f12a22009-12-06 18:00:51 +00001606 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001607 ArrayType::Normal, 0);
1608 }
1609
Devang Patelf6a39b72009-10-20 18:26:30 +00001610 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001611 getOrCreateType(T, Unit),
1612 Var->hasInternalLinkage(),
1613 true/*definition*/, Var);
1614}
Devang Patelabb485f2010-02-01 19:16:32 +00001615
1616/// getOrCreateNamesSpace - Return namespace descriptor for the given
1617/// namespace decl.
1618llvm::DINameSpace
1619CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1620 llvm::DIDescriptor Unit) {
1621 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1622 NameSpaceCache.find(NSDecl);
1623 if (I != NameSpaceCache.end())
1624 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1625
Devang Patel8ab870d2010-05-12 23:46:38 +00001626 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001627
1628 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001629 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001630 llvm::DINameSpace NS =
1631 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001632 llvm::DIFile(Unit), LineNo);
1633 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001634 return NS;
1635}