blob: 46d1447c62c0634e965e75ca9279ccaa5fd62667 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000072 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel8b90a782010-05-13 23:52:37 +000073 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000103 if (const ObjCImplementationDecl *OID =
104 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000105 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000106 } else if (const ObjCInterfaceDecl *OID =
107 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000108 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000109 } else if (const ObjCCategoryImplDecl *OCD =
110 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000111 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
112 OCD->getIdentifier()->getNameStart() << ')';
113 }
114 OS << ' ' << OMD->getSelector().getAsString() << ']';
115
116 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
117 memcpy(StrPtr, MethodName.begin(), OS.tell());
118 return llvm::StringRef(StrPtr, OS.tell());
119}
120
Devang Patel700a1cb2010-07-20 20:24:18 +0000121/// getClassName - Get class name including template argument list.
122llvm::StringRef
123CGDebugInfo::getClassName(RecordDecl *RD) {
124 ClassTemplateSpecializationDecl *Spec
125 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
126 if (!Spec)
127 return RD->getName();
128
129 const TemplateArgument *Args;
130 unsigned NumArgs;
131 std::string Buffer;
132 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
133 const TemplateSpecializationType *TST =
134 cast<TemplateSpecializationType>(TAW->getType());
135 Args = TST->getArgs();
136 NumArgs = TST->getNumArgs();
137 } else {
138 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
139 Args = TemplateArgs.getFlatArgumentList();
140 NumArgs = TemplateArgs.flat_size();
141 }
142 Buffer = RD->getIdentifier()->getNameStart();
143 PrintingPolicy Policy(CGM.getLangOptions());
144 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
145 NumArgs,
146 Policy);
147
148 // Copy this name on the side and use its reference.
149 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
150 memcpy(StrPtr, Buffer.data(), Buffer.length());
151 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000152}
153
Devang Patel17800552010-03-09 00:44:50 +0000154/// getOrCreateFile - Get the file debug info descriptor for the input location.
155llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000156 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000157 // If Location is not valid then use main input file.
158 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
159 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000160 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000161 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000162
163 // Cache the results.
164 const char *fname = PLoc.getFilename();
165 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
166 DIFileCache.find(fname);
167
168 if (it != DIFileCache.end()) {
169 // Verify that the information still exists.
170 if (&*it->second)
171 return llvm::DIFile(cast<llvm::MDNode>(it->second));
172 }
173
Devang Patelac4d13c2010-07-27 15:17:16 +0000174 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
175 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000176
Devang Patelab699792010-05-07 18:12:35 +0000177 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000178 return F;
179
Devang Patel17800552010-03-09 00:44:50 +0000180}
Devang Patel8ab870d2010-05-12 23:46:38 +0000181
Devang Patel532105f2010-10-28 22:03:20 +0000182/// getOrCreateMainFile - Get the file info for main compile unit.
183llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
184 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
185 TheCU);
186}
187
Devang Patel8ab870d2010-05-12 23:46:38 +0000188/// getLineNumber - Get line number for the location. If location is invalid
189/// then use current location.
190unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
191 assert (CurLoc.isValid() && "Invalid current location!");
192 SourceManager &SM = CGM.getContext().getSourceManager();
193 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
194 return PLoc.getLine();
195}
196
197/// getColumnNumber - Get column number for the location. If location is
198/// invalid then use current location.
199unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
200 assert (CurLoc.isValid() && "Invalid current location!");
201 SourceManager &SM = CGM.getContext().getSourceManager();
202 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
203 return PLoc.getColumn();
204}
205
Devang Patelac4d13c2010-07-27 15:17:16 +0000206llvm::StringRef CGDebugInfo::getCurrentDirname() {
207 if (!CWDName.empty())
208 return CWDName;
209 char *CompDirnamePtr = NULL;
210 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
211 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
212 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
213 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
214}
215
Devang Patel17800552010-03-09 00:44:50 +0000216/// CreateCompileUnit - Create new compile unit.
217void CGDebugInfo::CreateCompileUnit() {
218
219 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000220 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000221 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
222 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000223 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000224
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000225 // The main file name provided via the "-main-file-name" option contains just
226 // the file name itself with no path information. This file name may have had
227 // a relative path, so we look into the actual file entry for the main
228 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000229 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000230 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000231 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000232 if (MainFileDir != ".")
233 MainFileName = MainFileDir + "/" + MainFileName;
234 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000235
Devang Patelac4d13c2010-07-27 15:17:16 +0000236 // Save filename string.
237 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
238 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
239 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
240
Chris Lattner515455a2009-03-25 03:28:08 +0000241 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000242 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000243 if (LO.CPlusPlus) {
244 if (LO.ObjC1)
245 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
246 else
247 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
248 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000249 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000250 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000251 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000252 } else {
253 LangTag = llvm::dwarf::DW_LANG_C89;
254 }
Devang Patel446c6192009-04-17 21:06:59 +0000255
Daniel Dunbar19f19832010-08-24 17:41:09 +0000256 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000257
258 // Figure out which version of the ObjC runtime we have.
259 unsigned RuntimeVers = 0;
260 if (LO.ObjC1)
261 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000263 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000264 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000265 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000266 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000267 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000268}
269
Devang Patel65e99f22009-02-25 01:36:11 +0000270/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000271/// one if necessary.
272llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000273 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000274 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000275 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000276 switch (BT->getKind()) {
277 default:
278 case BuiltinType::Void:
279 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000280 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000281 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela2e57692010-10-28 17:27:32 +0000282 Unit, "objc_class", Unit, 0, 0, 0,
283 0, llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000284 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000285 case BuiltinType::ObjCId: {
286 // typedef struct objc_class *Class;
287 // typedef struct objc_object {
288 // Class isa;
289 // } *id;
290
291 llvm::DIType OCTy =
292 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
293 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000294 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000295 llvm::DIType(), llvm::DIArray());
296 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
297
298 llvm::DIType ISATy =
299 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
300 Unit, "", Unit,
301 0, Size, 0, 0, 0, OCTy);
302
303 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
304
305 llvm::DIType FieldTy =
306 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
307 "isa", Unit,
308 0,Size, 0, 0, 0, ISATy);
309 EltTys.push_back(FieldTy);
310 llvm::DIArray Elements =
311 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
312
313 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela2e57692010-10-28 17:27:32 +0000314 Unit, "objc_object", Unit, 0, 0, 0,
315 0, 0,
Devang Patelc8972c62010-07-28 01:33:15 +0000316 llvm::DIType(), Elements);
317 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000318 case BuiltinType::UChar:
319 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
320 case BuiltinType::Char_S:
321 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
322 case BuiltinType::UShort:
323 case BuiltinType::UInt:
324 case BuiltinType::ULong:
325 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
326 case BuiltinType::Short:
327 case BuiltinType::Int:
328 case BuiltinType::Long:
329 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
330 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
331 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000332 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000333 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000334 }
Devang Patel05127ca2010-07-28 23:23:29 +0000335
336 switch (BT->getKind()) {
337 case BuiltinType::Long: BTName = "long int"; break;
338 case BuiltinType::LongLong: BTName = "long long int"; break;
339 case BuiltinType::ULong: BTName = "long unsigned int"; break;
340 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
341 default:
342 BTName = BT->getName(CGM.getContext().getLangOptions());
343 break;
344 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000345 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000346 uint64_t Size = CGM.getContext().getTypeSize(BT);
347 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348 uint64_t Offset = 0;
Devang Patelca80a5f2009-10-20 19:55:01 +0000349 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000350 DebugFactory.CreateBasicType(TheCU, BTName, getOrCreateMainFile(),
351 0, Size, Align,
Devang Patelca80a5f2009-10-20 19:55:01 +0000352 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000353 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000354}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000355
Chris Lattnerb7003772009-04-23 06:13:01 +0000356llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000357 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000358 // Bit size, align and offset of the type.
359 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
360 if (Ty->isComplexIntegerType())
361 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 uint64_t Size = CGM.getContext().getTypeSize(Ty);
364 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000365 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Devang Patelca80a5f2009-10-20 19:55:01 +0000367 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000368 DebugFactory.CreateBasicType(TheCU, "complex", getOrCreateMainFile(),
369 0, Size, Align, Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000370 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000371}
372
John McCalla1805292009-09-25 01:40:47 +0000373/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000374/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000375llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000376 QualifierCollector Qc;
377 const Type *T = Qc.strip(Ty);
378
379 // Ignore these qualifiers for now.
380 Qc.removeObjCGCAttr();
381 Qc.removeAddressSpace();
382
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 // We will create one Derived type for one qualifier and recurse to handle any
384 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000386 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000388 Qc.removeConst();
389 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000390 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000391 Qc.removeVolatile();
392 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000393 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000394 Qc.removeRestrict();
395 } else {
396 assert(Qc.empty() && "Unknown type qualifier for debug info");
397 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
John McCalla1805292009-09-25 01:40:47 +0000400 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
401
Daniel Dunbar3845f862008-10-31 03:54:29 +0000402 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
403 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000404 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000405 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000406 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000407 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000408}
409
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000410llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000411 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000412 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000413 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
414 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000415 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000416}
417
Chris Lattner9c85ba32008-11-10 06:08:34 +0000418llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000419 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000420 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
421 Ty->getPointeeType(), Unit);
422}
423
Devang Patelc69e1cf2010-09-30 19:05:55 +0000424/// CreatePointeeType - Create PointTee type. If Pointee is a record
425/// then emit record's fwd if debug info size reduction is enabled.
426llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
427 llvm::DIFile Unit) {
428 if (!CGM.getCodeGenOpts().LimitDebugInfo)
429 return getOrCreateType(PointeeTy, Unit);
430
431 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
432 RecordDecl *RD = RTy->getDecl();
433 unsigned RTag;
434 if (RD->isStruct())
435 RTag = llvm::dwarf::DW_TAG_structure_type;
436 else if (RD->isUnion())
437 RTag = llvm::dwarf::DW_TAG_union_type;
438 else {
439 assert(RD->isClass() && "Unknown RecordType!");
440 RTag = llvm::dwarf::DW_TAG_class_type;
441 }
442
443 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
444 unsigned Line = getLineNumber(RD->getLocation());
445 llvm::DIDescriptor FDContext =
446 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
447
448 return
449 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
450 DefUnit, Line, 0, 0, 0,
451 llvm::DIType::FlagFwdDecl,
452 llvm::DIType(), llvm::DIArray());
453 }
454 return getOrCreateType(PointeeTy, Unit);
455
456}
457
Anders Carlssona031b352009-11-06 19:19:55 +0000458llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
459 const Type *Ty,
460 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000461 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000462 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000463
464 // Size is always the size of a pointer. We can't use getTypeSize here
465 // because that does not return the correct value for references.
466 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000467 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
468 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Devang Patelc69e1cf2010-09-30 19:05:55 +0000470 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
471 0, Size, Align, 0, 0,
472 CreatePointeeType(PointeeTy, Unit));
473
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000474}
475
Mike Stump9bc093c2009-05-14 02:03:51 +0000476llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000477 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000478 if (BlockLiteralGenericSet)
479 return BlockLiteralGeneric;
480
Mike Stump9bc093c2009-05-14 02:03:51 +0000481 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
482
483 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
484
485 llvm::DIType FieldTy;
486
487 QualType FType;
488 uint64_t FieldSize, FieldOffset;
489 unsigned FieldAlign;
490
491 llvm::DIArray Elements;
492 llvm::DIType EltTy, DescTy;
493
494 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000495 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000496 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
497 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000498
Daniel Dunbarca308df2009-05-26 19:40:20 +0000499 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000500 EltTys.clear();
501
Devang Patele2472482010-09-29 21:05:52 +0000502 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000503 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000504
Mike Stump9bc093c2009-05-14 02:03:51 +0000505 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000506 Unit, LineNo, FieldOffset, 0, 0,
507 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Mike Stump9bc093c2009-05-14 02:03:51 +0000509 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000510 uint64_t Size = CGM.getContext().getTypeSize(Ty);
511 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Mike Stump9bc093c2009-05-14 02:03:51 +0000513 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000514 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000515 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000516
517 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000518 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000519 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000520 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000521 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
522 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000523 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000524 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000525
Anders Carlsson20f12a22009-12-06 18:00:51 +0000526 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000527 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000528 FieldSize = CGM.getContext().getTypeSize(Ty);
529 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000530 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000531 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000532 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000533 FieldOffset, 0, FieldTy);
534 EltTys.push_back(FieldTy);
535
536 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000537 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000538
539 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000540 Unit, LineNo, FieldOffset, 0, 0,
541 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Mike Stump9bc093c2009-05-14 02:03:51 +0000543 BlockLiteralGenericSet = true;
544 BlockLiteralGeneric
545 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000546 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000547 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000548 return BlockLiteralGeneric;
549}
550
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000552 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000553 // Typedefs are derived from some other type. If we have a typedef of a
554 // typedef, make sure to emit the whole chain.
555 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner9c85ba32008-11-10 06:08:34 +0000557 // We don't set size information, but do specify where the typedef was
558 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000559 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000560
Devang Pateleb6d79b2010-02-01 21:34:11 +0000561 llvm::DIDescriptor TyContext
562 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
563 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000564 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000565 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000566 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000567 Ty->getDecl()->getName(), Unit,
568 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000569 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000570}
571
Chris Lattner9c85ba32008-11-10 06:08:34 +0000572llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000573 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000574 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000575
Chris Lattner9c85ba32008-11-10 06:08:34 +0000576 // Add the result type at least.
577 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Chris Lattner9c85ba32008-11-10 06:08:34 +0000579 // Set up remainder of arguments if there is a prototype.
580 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000581 if (isa<FunctionNoProtoType>(Ty))
582 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
583 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000584 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
585 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000586 }
587
Chris Lattner9c85ba32008-11-10 06:08:34 +0000588 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000589 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Devang Patelca80a5f2009-10-20 19:55:01 +0000591 llvm::DIType DbgTy =
592 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000593 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000594 0, 0, 0, 0, 0,
595 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000596 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000597}
598
Devang Patel428deb52010-01-19 00:00:59 +0000599/// CollectRecordFields - A helper function to collect debug info for
600/// record fields. This is used while creating debug info entry for a Record.
601void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000602CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000603 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
604 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000605 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
606 for (RecordDecl::field_iterator I = RD->field_begin(),
607 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000608 I != E; ++I, ++FieldNo) {
609 FieldDecl *Field = *I;
610 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000611 llvm::StringRef FieldName = Field->getName();
612
Devang Patel4835fdd2010-02-12 01:31:06 +0000613 // Ignore unnamed fields. Do not ignore unnamed records.
614 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000615 continue;
616
617 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000618 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
619 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000620 QualType FType = Field->getType();
621 uint64_t FieldSize = 0;
622 unsigned FieldAlign = 0;
623 if (!FType->isIncompleteArrayType()) {
624
625 // Bit size, align and offset of the type.
626 FieldSize = CGM.getContext().getTypeSize(FType);
627 Expr *BitWidth = Field->getBitWidth();
628 if (BitWidth)
629 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000630 FieldAlign = CGM.getContext().getTypeAlign(FType);
631 }
632
633 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
634
Devang Patel71f4ff62010-04-21 23:12:37 +0000635 unsigned Flags = 0;
636 AccessSpecifier Access = I->getAccess();
637 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000638 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000639 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000640 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000641
Devang Patel428deb52010-01-19 00:00:59 +0000642 // Create a DW_TAG_member node to remember the offset of this field in the
643 // struct. FIXME: This is an absolutely insane way to capture this
644 // information. When we gut debug info, this should be fixed.
645 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
646 FieldName, FieldDefUnit,
647 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000648 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000649 EltTys.push_back(FieldTy);
650 }
651}
652
Devang Patela6da1922010-01-28 00:28:01 +0000653/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
654/// function type is not updated to include implicit "this" pointer. Use this
655/// routine to get a method type which includes "this" pointer.
656llvm::DIType
657CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000658 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000659 llvm::DIType FnTy
660 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
661 0),
662 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000663
Devang Patela6da1922010-01-28 00:28:01 +0000664 // Add "this" pointer.
665
Devang Patelab699792010-05-07 18:12:35 +0000666 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000667 assert (Args.getNumElements() && "Invalid number of arguments!");
668
669 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
670
671 // First element is always return type. For 'void' functions it is NULL.
672 Elts.push_back(Args.getElement(0));
673
Devang Patel2ed8f002010-08-27 17:47:47 +0000674 if (!Method->isStatic())
675 {
676 // "this" pointer is always first argument.
677 ASTContext &Context = CGM.getContext();
678 QualType ThisPtr =
679 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
680 llvm::DIType ThisPtrType =
681 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000682
Devang Patel2ed8f002010-08-27 17:47:47 +0000683 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
684 Elts.push_back(ThisPtrType);
685 }
Devang Patela6da1922010-01-28 00:28:01 +0000686
687 // Copy rest of the arguments.
688 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
689 Elts.push_back(Args.getElement(i));
690
691 llvm::DIArray EltTypeArray =
692 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
693
694 return
695 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000696 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000697 0, 0, 0, 0, 0,
698 llvm::DIType(), EltTypeArray);
699}
700
Devang Patel58faf202010-10-22 17:11:50 +0000701/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
702/// inside a function.
703static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
704 if (const CXXRecordDecl *NRD =
705 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
706 return isFunctionLocalClass(NRD);
707 else if (isa<FunctionDecl>(RD->getDeclContext()))
708 return true;
709 return false;
710
711}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000712/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
713/// a single member function GlobalDecl.
714llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000715CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000716 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000717 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000718 bool IsCtorOrDtor =
719 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
720
721 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000722 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000723
724 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
725 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000726 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000727 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000728 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000729
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000730 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000731 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
732 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000733
734 // Collect virtual method info.
735 llvm::DIType ContainingType;
736 unsigned Virtuality = 0;
737 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000738
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000739 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000740 if (Method->isPure())
741 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
742 else
743 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
744
745 // It doesn't make sense to give a virtual destructor a vtable index,
746 // since a single destructor has two entries in the vtable.
747 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000748 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000749 ContainingType = RecordTy;
750 }
751
Devang Patele2472482010-09-29 21:05:52 +0000752 unsigned Flags = 0;
753 if (Method->isImplicit())
754 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000755 AccessSpecifier Access = Method->getAccess();
756 if (Access == clang::AS_private)
757 Flags |= llvm::DIDescriptor::FlagPrivate;
758 else if (Access == clang::AS_protected)
759 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000760 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
761 if (CXXC->isExplicit())
762 Flags |= llvm::DIDescriptor::FlagExplicit;
763 } else if (const CXXConversionDecl *CXXC =
764 dyn_cast<CXXConversionDecl>(Method)) {
765 if (CXXC->isExplicit())
766 Flags |= llvm::DIDescriptor::FlagExplicit;
767 }
Devang Patel3951e712010-10-07 22:03:49 +0000768 if (Method->hasPrototype())
769 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000770
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000771 llvm::DISubprogram SP =
772 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
773 MethodLinkageName,
774 MethodDefUnit, MethodLine,
775 MethodTy, /*isLocalToUnit=*/false,
Devang Patel33133572010-10-22 18:31:12 +0000776 /* isDefinition=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000777 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000778 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000779 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000780
781 // Don't cache ctors or dtors since we have to emit multiple functions for
782 // a single ctor or dtor.
783 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000784 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000785
786 return SP;
787}
788
Devang Patel4125fd22010-01-19 01:54:44 +0000789/// CollectCXXMemberFunctions - A helper function to collect debug info for
790/// C++ member functions.This is used while creating debug info entry for
791/// a Record.
792void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000793CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000794 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000795 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000796 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
797 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000798 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000799
Devang Pateld5322da2010-02-09 19:09:28 +0000800 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000801 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000802
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000803 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000804 }
805}
806
Devang Patel2ed8f002010-08-27 17:47:47 +0000807/// CollectCXXFriends - A helper function to collect debug info for
808/// C++ base classes. This is used while creating debug info entry for
809/// a Record.
810void CGDebugInfo::
811CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
812 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
813 llvm::DIType RecordTy) {
814
815 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
816 BE = RD->friend_end(); BI != BE; ++BI) {
817
818 TypeSourceInfo *TInfo = (*BI)->getFriendType();
819 if(TInfo)
820 {
821 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
822
823 llvm::DIType DTy =
824 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
825 RecordTy, llvm::StringRef(),
826 Unit, 0, 0, 0,
827 0, 0, Ty);
828
829 EltTys.push_back(DTy);
830 }
831
832 }
833}
834
Devang Patela245c5b2010-01-25 23:32:18 +0000835/// CollectCXXBases - A helper function to collect debug info for
836/// C++ base classes. This is used while creating debug info entry for
837/// a Record.
838void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000839CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000840 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000841 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000842
Devang Patel239cec62010-02-01 21:39:52 +0000843 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
844 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
845 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000846 unsigned BFlags = 0;
847 uint64_t BaseOffset;
848
849 const CXXRecordDecl *Base =
850 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
851
852 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000853 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000854 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000855 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000856 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000857 } else
858 BaseOffset = RL.getBaseClassOffset(Base);
859
860 AccessSpecifier Access = BI->getAccessSpecifier();
861 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000862 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000863 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000864 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000865
866 llvm::DIType DTy =
867 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
868 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000869 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000870 BaseOffset, BFlags,
871 getOrCreateType(BI->getType(),
872 Unit));
873 EltTys.push_back(DTy);
874 }
Devang Patela245c5b2010-01-25 23:32:18 +0000875}
876
Devang Patel4ce3f202010-01-28 18:11:52 +0000877/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000878llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000879 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000880 return VTablePtrType;
881
882 ASTContext &Context = CGM.getContext();
883
884 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000885 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
886 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000887 llvm::DIType SubTy =
888 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000889 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000890 0, 0, 0, 0, 0, llvm::DIType(), SElements);
891
892 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
893 llvm::DIType vtbl_ptr_type
894 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000895 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000896 0, Size, 0, 0, 0, SubTy);
897
Devang Pateld58562e2010-03-09 22:49:11 +0000898 VTablePtrType =
899 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
900 Unit, "", Unit,
901 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000902 return VTablePtrType;
903}
904
Anders Carlsson046c2942010-04-17 20:15:18 +0000905/// getVTableName - Get vtable name for the given Class.
906llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000907 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000908 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000909
910 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000911 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000912 memcpy(StrPtr, Name.data(), Name.length());
913 return llvm::StringRef(StrPtr, Name.length());
914}
915
916
Anders Carlsson046c2942010-04-17 20:15:18 +0000917/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000918/// debug info entry in EltTys vector.
919void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000920CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000921 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000922 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000923
924 // If there is a primary base then it will hold vtable info.
925 if (RL.getPrimaryBase())
926 return;
927
928 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000929 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000930 return;
931
932 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
933 llvm::DIType VPTR
934 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000935 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000936 0, Size, 0, 0, 0,
937 getOrCreateVTablePtrType(Unit));
938 EltTys.push_back(VPTR);
939}
940
Devang Patelc69e1cf2010-09-30 19:05:55 +0000941/// getOrCreateRecordType - Emit record type's standalone debug info.
942llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
943 SourceLocation Loc) {
944 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
945 DebugFactory.RecordType(T);
946 return T;
947}
948
Devang Patel65e99f22009-02-25 01:36:11 +0000949/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000950llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000951 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000952 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000955 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000957 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 Tag = llvm::dwarf::DW_TAG_union_type;
959 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000960 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000961 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000962 }
963
Chris Lattner9c85ba32008-11-10 06:08:34 +0000964 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000965 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
966 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner9c85ba32008-11-10 06:08:34 +0000968 // Records and classes and unions can all be recursive. To handle them, we
969 // first generate a debug descriptor for the struct as a forward declaration.
970 // Then (if it is a definition) we go through and get debug info for all of
971 // its members. Finally, we create a descriptor for the complete type (which
972 // may refer to the forward decl if the struct is recursive) and replace all
973 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000974 llvm::DIDescriptor FDContext =
975 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
976
977 // If this is just a forward declaration, construct an appropriately
978 // marked node and just return it.
979 if (!RD->getDefinition()) {
980 llvm::DICompositeType FwdDecl =
981 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
982 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000983 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000984 llvm::DIType(), llvm::DIArray());
985
986 return FwdDecl;
987 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000988
Dan Gohman86968372010-08-20 22:39:57 +0000989 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Devang Patelab699792010-05-07 18:12:35 +0000991 llvm::MDNode *MN = FwdDecl;
992 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000993 // Otherwise, insert it into the TypeCache so that recursive uses will find
994 // it.
Devang Patelab699792010-05-07 18:12:35 +0000995 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000996 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000997 RegionStack.push_back(FwdDeclNode);
998 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000999
1000 // Convert all the elements.
1001 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1002
Devang Pateld6c5a262010-02-01 21:52:22 +00001003 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001004 if (CXXDecl) {
1005 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +00001006 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001007 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001008
1009 // Collect static variables with initializers.
1010 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1011 I != E; ++I)
1012 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1013 if (const Expr *Init = V->getInit()) {
1014 Expr::EvalResult Result;
1015 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1016 llvm::ConstantInt *CI
1017 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1018
1019 // Create the descriptor for static variable.
1020 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1021 llvm::StringRef VName = V->getName();
1022 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1023 // Do not use DIGlobalVariable for enums.
1024 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1025 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1026 getLineNumber(V->getLocation()),
1027 VTy, true, true, CI);
1028 }
1029 }
1030 }
1031 }
1032
Devang Pateld6c5a262010-02-01 21:52:22 +00001033 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001034 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001035 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001036 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001037 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001038
1039 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001040 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001041 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1042 // Seek non virtual primary base root.
1043 while (1) {
1044 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1045 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1046 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1047 PBase = PBT;
1048 else
1049 break;
1050 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001051 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001052 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001053 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001054 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001055 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Chris Lattner9c85ba32008-11-10 06:08:34 +00001058 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001059 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060
1061 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001062 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1063 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Devang Patele4c1ea02010-03-11 20:01:48 +00001065 RegionStack.pop_back();
1066 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1067 RegionMap.find(Ty->getDecl());
1068 if (RI != RegionMap.end())
1069 RegionMap.erase(RI);
1070
Devang Patel411894b2010-02-01 22:40:08 +00001071 llvm::DIDescriptor RDContext =
1072 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001073
1074 llvm::StringRef RDName = RD->getName();
1075 // If this is a class, include the template arguments also.
1076 if (Tag == llvm::dwarf::DW_TAG_class_type)
1077 RDName = getClassName(RD);
1078
Devang Patel0ce73f62009-07-22 18:57:00 +00001079 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001080 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001081 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001082 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001083 llvm::DIType(), Elements,
1084 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001085
1086 // Now that we have a real decl for the struct, replace anything using the
1087 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001088 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001089 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 return RealDecl;
1091}
1092
John McCallc12c5bb2010-05-15 11:32:37 +00001093/// CreateType - get objective-c object type.
1094llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1095 llvm::DIFile Unit) {
1096 // Ignore protocols.
1097 return getOrCreateType(Ty->getBaseType(), Unit);
1098}
1099
Devang Patel9ca36b62009-02-26 21:10:26 +00001100/// CreateType - get objective-c interface type.
1101llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001102 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001103 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001104 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001105
1106 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001107 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001108 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001109 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001110
Dan Gohman45f7c782010-08-23 21:15:56 +00001111 // If this is just a forward declaration, return a special forward-declaration
1112 // debug type.
1113 if (ID->isForwardDecl()) {
1114 llvm::DICompositeType FwdDecl =
1115 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1116 DefUnit, Line, 0, 0, 0, 0,
1117 llvm::DIType(), llvm::DIArray(),
1118 RuntimeLang);
1119 return FwdDecl;
1120 }
1121
Devang Patel9ca36b62009-02-26 21:10:26 +00001122 // To handle recursive interface, we
1123 // first generate a debug descriptor for the struct as a forward declaration.
1124 // Then (if it is a definition) we go through and get debug info for all of
1125 // its members. Finally, we create a descriptor for the complete type (which
1126 // may refer to the forward decl if the struct is recursive) and replace all
1127 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001128 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Devang Patelab699792010-05-07 18:12:35 +00001130 llvm::MDNode *MN = FwdDecl;
1131 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001132 // Otherwise, insert it into the TypeCache so that recursive uses will find
1133 // it.
Devang Patelab699792010-05-07 18:12:35 +00001134 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001135 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001136 RegionStack.push_back(FwdDeclNode);
1137 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001138
1139 // Convert all the elements.
1140 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1141
Devang Pateld6c5a262010-02-01 21:52:22 +00001142 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001143 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001144 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001145 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001146 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001147 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001148 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001149 0 /* offset */, 0, SClassTy);
1150 EltTys.push_back(InhTag);
1151 }
1152
Devang Pateld6c5a262010-02-01 21:52:22 +00001153 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001154
1155 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001156 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001157 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001158 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1159
Devang Patel73621622009-11-25 17:37:31 +00001160 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001161
Devang Patelde135022009-04-27 22:40:36 +00001162 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001163 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001164 continue;
1165
Devang Patel9ca36b62009-02-26 21:10:26 +00001166 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001167 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1168 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001169 QualType FType = Field->getType();
1170 uint64_t FieldSize = 0;
1171 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001172
Devang Patel99c20eb2009-03-20 18:24:39 +00001173 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Devang Patel99c20eb2009-03-20 18:24:39 +00001175 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001176 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001177 Expr *BitWidth = Field->getBitWidth();
1178 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001179 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001180
Anders Carlsson20f12a22009-12-06 18:00:51 +00001181 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001182 }
1183
Mike Stump1eb44332009-09-09 15:08:12 +00001184 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1185
Devang Patelc20482b2009-03-19 00:23:53 +00001186 unsigned Flags = 0;
1187 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001188 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001189 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001190 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Devang Patel9ca36b62009-02-26 21:10:26 +00001192 // Create a DW_TAG_member node to remember the offset of this field in the
1193 // struct. FIXME: This is an absolutely insane way to capture this
1194 // information. When we gut debug info, this should be fixed.
1195 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1196 FieldName, FieldDefUnit,
1197 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001198 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001199 EltTys.push_back(FieldTy);
1200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Devang Patel9ca36b62009-02-26 21:10:26 +00001202 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001203 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001204
Devang Patele4c1ea02010-03-11 20:01:48 +00001205 RegionStack.pop_back();
1206 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1207 RegionMap.find(Ty->getDecl());
1208 if (RI != RegionMap.end())
1209 RegionMap.erase(RI);
1210
Devang Patel9ca36b62009-02-26 21:10:26 +00001211 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001212 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1213 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Devang Patel6c1fddf2009-07-27 18:42:03 +00001215 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001216 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001217 Line, Size, Align, 0, 0, llvm::DIType(),
1218 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001219
1220 // Now that we have a real decl for the struct, replace anything using the
1221 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001222 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001223 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001224
Devang Patel9ca36b62009-02-26 21:10:26 +00001225 return RealDecl;
1226}
1227
Chris Lattner9c85ba32008-11-10 06:08:34 +00001228llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001229 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001230 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001231
Chris Lattner9c85ba32008-11-10 06:08:34 +00001232}
1233
1234llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001235 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1237 return CreateType(RT, Unit);
1238 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1239 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001240
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241 return llvm::DIType();
1242}
1243
Devang Patel70c23cd2010-02-23 22:59:39 +00001244llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001245 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001246 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1247 uint64_t NumElems = Ty->getNumElements();
1248 if (NumElems > 0)
1249 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001250
Benjamin Kramerad468862010-03-30 11:36:44 +00001251 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1252 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001253
1254 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1255 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1256
1257 return
1258 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001259 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001260 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001261 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001262}
1263
Chris Lattner9c85ba32008-11-10 06:08:34 +00001264llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001265 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001266 uint64_t Size;
1267 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001268
1269
Nuno Lopes010d5142009-01-28 00:35:17 +00001270 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001271 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001272 Size = 0;
1273 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001274 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001275 } else if (Ty->isIncompleteArrayType()) {
1276 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001277 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001278 } else {
1279 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001280 Size = CGM.getContext().getTypeSize(Ty);
1281 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001282 }
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Chris Lattner9c85ba32008-11-10 06:08:34 +00001284 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1285 // interior arrays, do we care? Why aren't nested arrays represented the
1286 // obvious/recursive way?
1287 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1288 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001289 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001290 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001291 else {
1292 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1293 uint64_t Upper = 0;
1294 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1295 if (CAT->getSize().getZExtValue())
1296 Upper = CAT->getSize().getZExtValue() - 1;
1297 // FIXME: Verify this is right for VLAs.
1298 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1299 EltTy = Ty->getElementType();
1300 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001301 }
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattner9c85ba32008-11-10 06:08:34 +00001303 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001304 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001305
Devang Patelca80a5f2009-10-20 19:55:01 +00001306 llvm::DIType DbgTy =
1307 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001308 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001309 0, Size, Align, 0, 0,
1310 getOrCreateType(EltTy, Unit),
1311 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001312 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001313}
1314
Anders Carlssona031b352009-11-06 19:19:55 +00001315llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001316 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001317 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1318 Ty, Ty->getPointeeType(), Unit);
1319}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001320
Anders Carlsson20f12a22009-12-06 18:00:51 +00001321llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001322 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001323 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1324 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1325
1326 if (!Ty->getPointeeType()->isFunctionType()) {
1327 // We have a data member pointer type.
1328 return PointerDiffDITy;
1329 }
1330
1331 // We have a member function pointer type. Treat it as a struct with two
1332 // ptrdiff_t members.
1333 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1334
1335 uint64_t FieldOffset = 0;
1336 llvm::DIDescriptor ElementTypes[2];
1337
1338 // FIXME: This should probably be a function type instead.
1339 ElementTypes[0] =
1340 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001341 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001342 Info.first, Info.second, FieldOffset, 0,
1343 PointerDiffDITy);
1344 FieldOffset += Info.first;
1345
1346 ElementTypes[1] =
1347 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001348 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001349 Info.first, Info.second, FieldOffset, 0,
1350 PointerDiffDITy);
1351
1352 llvm::DIArray Elements =
1353 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1354 llvm::array_lengthof(ElementTypes));
1355
1356 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1357 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001358 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001359 0, 0, 0, llvm::DIType(), Elements);
1360}
1361
Devang Patel6237cea2010-08-23 22:07:25 +00001362/// CreateEnumType - get enumeration type.
1363llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1364 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1365
1366 // Create DIEnumerator elements for each enumerator.
1367 for (EnumDecl::enumerator_iterator
1368 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1369 Enum != EnumEnd; ++Enum) {
1370 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1371 Enum->getInitVal().getZExtValue()));
1372 }
1373
1374 // Return a CompositeType for the enum itself.
1375 llvm::DIArray EltArray =
1376 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1377
1378 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1379 unsigned Line = getLineNumber(ED->getLocation());
1380 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001381 uint64_t Align = 0;
1382 if (!ED->getTypeForDecl()->isIncompleteType()) {
1383 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1384 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1385 }
Devang Patel4bc48872010-10-27 23:23:58 +00001386 llvm::DIDescriptor EnumContext =
1387 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001388 llvm::DIType DbgTy =
1389 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel4bc48872010-10-27 23:23:58 +00001390 EnumContext, ED->getName(),
1391 DefUnit, Line, Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001392 llvm::DIType(), EltArray);
1393 return DbgTy;
1394}
1395
Douglas Gregor840943d2009-12-21 20:18:30 +00001396static QualType UnwrapTypeForDebugInfo(QualType T) {
1397 do {
1398 QualType LastT = T;
1399 switch (T->getTypeClass()) {
1400 default:
1401 return T;
1402 case Type::TemplateSpecialization:
1403 T = cast<TemplateSpecializationType>(T)->desugar();
1404 break;
1405 case Type::TypeOfExpr: {
1406 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1407 T = Ty->getUnderlyingExpr()->getType();
1408 break;
1409 }
1410 case Type::TypeOf:
1411 T = cast<TypeOfType>(T)->getUnderlyingType();
1412 break;
1413 case Type::Decltype:
1414 T = cast<DecltypeType>(T)->getUnderlyingType();
1415 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001416 case Type::Elaborated:
1417 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001418 break;
1419 case Type::SubstTemplateTypeParm:
1420 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1421 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001422 }
1423
1424 assert(T != LastT && "Type unwrapping failed to unwrap!");
1425 if (T == LastT)
1426 return T;
1427 } while (true);
1428
1429 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001430}
1431
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001432/// getOrCreateType - Get the type from the cache or create a new
1433/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001434llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001435 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001436 if (Ty.isNull())
1437 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Douglas Gregor840943d2009-12-21 20:18:30 +00001439 // Unwrap the type as needed for debug information.
1440 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001441
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001442 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001443 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001444 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001445 if (it != TypeCache.end()) {
1446 // Verify that the debug info still exists.
1447 if (&*it->second)
1448 return llvm::DIType(cast<llvm::MDNode>(it->second));
1449 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001450
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001451 // Otherwise create the type.
1452 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001453
1454 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001455 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001456 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001457}
1458
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001459/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001460llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001461 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001462 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001463 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001464 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001465
Douglas Gregor2101a822009-12-21 19:57:21 +00001466 const char *Diag = 0;
1467
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001468 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001469 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001470#define TYPE(Class, Base)
1471#define ABSTRACT_TYPE(Class, Base)
1472#define NON_CANONICAL_TYPE(Class, Base)
1473#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1474#include "clang/AST/TypeNodes.def"
1475 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001476
Anders Carlssonbfe69952009-11-06 18:24:04 +00001477 // FIXME: Handle these.
1478 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001479 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001480
1481 case Type::Vector:
1482 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001483 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001484 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001485 case Type::ObjCObject:
1486 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001487 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001488 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1489 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1490 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1491 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001492 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001493 return CreateType(cast<BlockPointerType>(Ty), Unit);
1494 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001495 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001496 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001497 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001498 case Type::FunctionProto:
1499 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001500 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001501 case Type::ConstantArray:
1502 case Type::VariableArray:
1503 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001504 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001505
1506 case Type::LValueReference:
1507 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1508
Anders Carlsson20f12a22009-12-06 18:00:51 +00001509 case Type::MemberPointer:
1510 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001511
1512 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001513 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001514 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001515 case Type::TypeOfExpr:
1516 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001517 case Type::Decltype:
1518 llvm_unreachable("type should have been unwrapped!");
1519 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001520
1521 case Type::RValueReference:
1522 // FIXME: Implement!
1523 Diag = "rvalue references";
1524 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001525 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001526
1527 assert(Diag && "Fall through without a diagnostic?");
1528 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1529 "debug information for %0 is not yet supported");
1530 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1531 << Diag;
1532 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001533}
1534
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001535/// CreateMemberType - Create new member and increase Offset by FType's size.
1536llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1537 llvm::StringRef Name,
1538 uint64_t *Offset) {
1539 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1540 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1541 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1542 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1543 Unit, Name, Unit, 0,
1544 FieldSize, FieldAlign,
1545 *Offset, 0, FieldTy);
1546 *Offset += FieldSize;
1547 return Ty;
1548}
1549
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001550/// EmitFunctionStart - Constructs the debug code for entering a function -
1551/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001552void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001553 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001554 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Devang Patel9c6c3a02010-01-14 00:36:21 +00001556 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001557 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001558
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001559 FnBeginRegionCount.push_back(RegionStack.size());
1560
Devang Patel9c6c3a02010-01-14 00:36:21 +00001561 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001562 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001563 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1564 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001565 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001566 // If there is a DISubprogram for this function available then use it.
1567 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1568 FI = SPCache.find(FD);
1569 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001570 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001571 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1572 llvm::MDNode *SPN = SP;
1573 RegionStack.push_back(SPN);
1574 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001575 return;
1576 }
1577 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001578 Name = getFunctionName(FD);
1579 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001580 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001581 if (LinkageName == Name)
1582 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001583 if (FD->hasPrototype())
1584 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001585 if (const NamespaceDecl *NSDecl =
1586 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1587 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001588 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001589 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001590 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001591 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001592 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001593 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001594 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001595 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001596 if (!Name.empty() && Name[0] == '\01')
1597 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Devang Patel970c6182010-04-24 00:49:16 +00001599 // It is expected that CurLoc is set before using EmitFunctionStart.
1600 // Usually, CurLoc points to the left bracket location of compound
1601 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001602 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001603 if (D->isImplicit())
1604 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001605 llvm::DISubprogram SP =
Devang Patela2e57692010-10-28 17:27:32 +00001606 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit,
1607 LineNo, getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001608 Fn->hasInternalLinkage(), true/*definition*/,
1609 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001610 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001611 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001613 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001614 llvm::MDNode *SPN = SP;
1615 RegionStack.push_back(SPN);
1616 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001617
1618 // Clear stack used to keep track of #line directives.
1619 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001620}
1621
1622
Devang Patel4d939e62010-07-20 22:20:10 +00001623void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001624 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001626 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001627 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001628 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001629 || (SM.getInstantiationLineNumber(CurLoc) ==
1630 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001631 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001632 // New Builder may not be in sync with CGDebugInfo.
1633 if (!Builder.getCurrentDebugLocation().isUnknown())
1634 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001635
1636 // Update last state.
1637 PrevLoc = CurLoc;
1638
Chris Lattnerc6034632010-04-01 06:31:43 +00001639 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001640 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1641 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001642 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001643}
1644
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001645/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1646/// has introduced scope change.
1647void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1648 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1649 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1650 return;
1651 SourceManager &SM = CGM.getContext().getSourceManager();
1652 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1653 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1654
1655 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1656 return;
1657
1658 // If #line directive stack is empty then we are entering a new scope.
1659 if (LineDirectiveFiles.empty()) {
1660 EmitRegionStart(Builder);
1661 LineDirectiveFiles.push_back(PCLoc.getFilename());
1662 return;
1663 }
1664
1665 assert (RegionStack.size() >= LineDirectiveFiles.size()
1666 && "error handling #line regions!");
1667
1668 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001669 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001670 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1671 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001672 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001673 SeenThisFile = true;
1674 break;
1675 }
1676
1677 // If #line for this file is seen earlier then pop out #line regions.
1678 if (SeenThisFile) {
1679 while (!LineDirectiveFiles.empty()) {
1680 const char *LastFile = LineDirectiveFiles.back();
1681 RegionStack.pop_back();
1682 LineDirectiveFiles.pop_back();
1683 if (!strcmp(PPLoc.getFilename(), LastFile))
1684 break;
1685 }
1686 return;
1687 }
1688
1689 // .. otherwise insert new #line region.
1690 EmitRegionStart(Builder);
1691 LineDirectiveFiles.push_back(PCLoc.getFilename());
1692
1693 return;
1694}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001695/// EmitRegionStart- Constructs the debug code for entering a declarative
1696/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001697void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001698 llvm::DIDescriptor D =
1699 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1700 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001701 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001702 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001703 getLineNumber(CurLoc),
1704 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001705 llvm::MDNode *DN = D;
1706 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001707}
1708
1709/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1710/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001711void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001712 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1713
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001714 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001715 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001717 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001718}
1719
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001720/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1721void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1722 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1723 unsigned RCount = FnBeginRegionCount.back();
1724 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1725
1726 // Pop all regions for this function.
1727 while (RegionStack.size() != RCount)
1728 EmitRegionEnd(Builder);
1729 FnBeginRegionCount.pop_back();
1730}
1731
Devang Patel809b9bb2010-02-10 18:49:08 +00001732// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1733// See BuildByRefType.
1734llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1735 uint64_t *XOffset) {
1736
1737 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1738
1739 QualType FType;
1740 uint64_t FieldSize, FieldOffset;
1741 unsigned FieldAlign;
1742
Devang Patel17800552010-03-09 00:44:50 +00001743 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001744 QualType Type = VD->getType();
1745
1746 FieldOffset = 0;
1747 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001748 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1749 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001750 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001751 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1752 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1753
Devang Patel809b9bb2010-02-10 18:49:08 +00001754 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1755 if (HasCopyAndDispose) {
1756 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001757 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1758 &FieldOffset));
1759 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1760 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001761 }
1762
1763 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1764 if (Align > CharUnits::fromQuantity(
1765 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1766 unsigned AlignedOffsetInBytes
1767 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1768 unsigned NumPaddingBytes
1769 = AlignedOffsetInBytes - FieldOffset/8;
1770
1771 if (NumPaddingBytes > 0) {
1772 llvm::APInt pad(32, NumPaddingBytes);
1773 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1774 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001775 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001776 }
1777 }
1778
1779 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001780 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001781 FieldSize = CGM.getContext().getTypeSize(FType);
1782 FieldAlign = Align.getQuantity()*8;
1783
1784 *XOffset = FieldOffset;
1785 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001786 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001787 0, FieldSize, FieldAlign,
1788 FieldOffset, 0, FieldTy);
1789 EltTys.push_back(FieldTy);
1790 FieldOffset += FieldSize;
1791
1792 llvm::DIArray Elements =
1793 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1794
Devang Patele2472482010-09-29 21:05:52 +00001795 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001796
1797 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001798 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001799 0, FieldOffset, 0, 0, Flags,
1800 llvm::DIType(), Elements);
1801
1802}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001803/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001804void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001805 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001806 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1807
Devang Patel17800552010-03-09 00:44:50 +00001808 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001809 llvm::DIType Ty;
1810 uint64_t XOffset = 0;
1811 if (VD->hasAttr<BlocksAttr>())
1812 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1813 else
1814 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001815
Devang Patelf4e54a22010-05-07 23:05:55 +00001816 // If there is not any debug info for type then do not emit debug info
1817 // for this variable.
1818 if (!Ty)
1819 return;
1820
Chris Lattner9c85ba32008-11-10 06:08:34 +00001821 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001822 unsigned Line = getLineNumber(VD->getLocation());
1823 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001824 unsigned Flags = 0;
1825 if (VD->isImplicit())
1826 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001827 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001828
1829 llvm::StringRef Name = VD->getName();
1830 if (!Name.empty()) {
1831 // Create the descriptor for the variable.
1832 llvm::DIVariable D =
1833 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1834 Name, Unit, Line, Ty,
1835 CGM.getLangOptions().Optimize, Flags);
1836
1837 // Insert an llvm.dbg.declare into the current block.
1838 llvm::Instruction *Call =
1839 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1840
1841 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1842 }
1843
1844 // If VD is an anonymous union then Storage represents value for
1845 // all union fields.
1846 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1847 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1848 if (RD->isUnion()) {
1849 for (RecordDecl::field_iterator I = RD->field_begin(),
1850 E = RD->field_end();
1851 I != E; ++I) {
1852 FieldDecl *Field = *I;
1853 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1854 llvm::StringRef FieldName = Field->getName();
1855
1856 // Ignore unnamed fields. Do not ignore unnamed records.
1857 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1858 continue;
1859
1860 // Use VarDecl's Tag, Scope and Line number.
1861 llvm::DIVariable D =
1862 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1863 FieldName, Unit, Line, FieldTy,
1864 CGM.getLangOptions().Optimize, Flags);
1865
1866 // Insert an llvm.dbg.declare into the current block.
1867 llvm::Instruction *Call =
1868 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1869
1870 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1871 }
1872 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001873}
1874
Mike Stumpb1a6e682009-09-30 02:43:10 +00001875/// EmitDeclare - Emit local variable declaration debug info.
1876void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1877 llvm::Value *Storage, CGBuilderTy &Builder,
1878 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001879 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001880 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1881
Devang Patel2b594b92010-04-26 23:28:46 +00001882 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001883 return;
1884
1885 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001886 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001887 llvm::DIType Ty;
1888 if (VD->hasAttr<BlocksAttr>())
1889 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1890 else
1891 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001892
1893 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001894 unsigned Line = getLineNumber(VD->getLocation());
1895 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001896
Devang Pateld6c5a262010-02-01 21:52:22 +00001897 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001898 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001899 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1900 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1901 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1902 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001903 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001904 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1905 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001906 // offset of __forwarding field
1907 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001908 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1909 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1910 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001911 // offset of x field
1912 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001913 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001914 }
1915
1916 // Create the descriptor for the variable.
1917 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001918 DebugFactory.CreateComplexVariable(Tag,
1919 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001920 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001921 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001922 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001923 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001924 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001925
Chris Lattnerc6034632010-04-01 06:31:43 +00001926 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001927 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001928}
1929
Devang Pateld6c5a262010-02-01 21:52:22 +00001930void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001931 llvm::Value *Storage,
1932 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001933 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001934}
1935
Mike Stumpb1a6e682009-09-30 02:43:10 +00001936void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1937 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1938 CodeGenFunction *CGF) {
1939 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1940}
1941
Chris Lattner9c85ba32008-11-10 06:08:34 +00001942/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1943/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001944void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001945 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001946 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001947}
1948
1949
1950
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001951/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001952void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001953 const VarDecl *D) {
1954
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001955 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001956 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001957 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001958
Devang Pateleb6d79b2010-02-01 21:34:11 +00001959 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001960 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001962 // CodeGen turns int[] into int[1] so we'll do the same here.
1963 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001965 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001966 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Anders Carlsson20f12a22009-12-06 18:00:51 +00001968 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001969 ArrayType::Normal, 0);
1970 }
Devang Patel5d822f02010-04-29 17:48:37 +00001971 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001972 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001973 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001974 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001975 if (LinkageName == DeclName)
1976 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001977 llvm::DIDescriptor DContext =
1978 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001979 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1980 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001981 Var->hasInternalLinkage(),
1982 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001983}
1984
Devang Patel9ca36b62009-02-26 21:10:26 +00001985/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001986void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001987 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001988 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001989 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001990 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001991
Devang Pateld6c5a262010-02-01 21:52:22 +00001992 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001993
Devang Pateld6c5a262010-02-01 21:52:22 +00001994 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001995 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001996
Devang Patel9ca36b62009-02-26 21:10:26 +00001997 // CodeGen turns int[] into int[1] so we'll do the same here.
1998 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Devang Patel9ca36b62009-02-26 21:10:26 +00002000 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002001 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Anders Carlsson20f12a22009-12-06 18:00:51 +00002003 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002004 ArrayType::Normal, 0);
2005 }
2006
Devang Patelf6a39b72009-10-20 18:26:30 +00002007 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00002008 getOrCreateType(T, Unit),
2009 Var->hasInternalLinkage(),
2010 true/*definition*/, Var);
2011}
Devang Patelabb485f2010-02-01 19:16:32 +00002012
Devang Patel25c2c8f2010-08-10 17:53:33 +00002013/// EmitGlobalVariable - Emit global variable's debug info.
2014void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002015 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002016 // Create the descriptor for the variable.
2017 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2018 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002019 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002020 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2021 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2022 Ty = CreateEnumType(ED, Unit);
2023 }
Devang Patel0317ab02010-08-10 18:27:15 +00002024 // Do not use DIGlobalVariable for enums.
2025 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2026 return;
Devang Patel8d308382010-08-10 07:24:25 +00002027 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2028 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002029 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002030}
2031
Devang Patelabb485f2010-02-01 19:16:32 +00002032/// getOrCreateNamesSpace - Return namespace descriptor for the given
2033/// namespace decl.
2034llvm::DINameSpace
2035CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2036 llvm::DIDescriptor Unit) {
2037 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2038 NameSpaceCache.find(NSDecl);
2039 if (I != NameSpaceCache.end())
2040 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2041
Devang Patel8ab870d2010-05-12 23:46:38 +00002042 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002043 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002044 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002045 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002046 llvm::DINameSpace NS =
Devang Patel8c376682010-10-28 19:12:46 +00002047 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002048 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002049 return NS;
2050}