blob: aab6fe7d245d790820d994cc2d62f19dd052eca2 [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.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000272llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000273 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000274 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275 switch (BT->getKind()) {
276 default:
277 case BuiltinType::Void:
278 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000279 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000280 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000281 TheCU, "objc_class",
282 getOrCreateMainFile(), 0, 0, 0,
Devang Patela2e57692010-10-28 17:27:32 +0000283 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,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000293 TheCU, "objc_class",
294 getOrCreateMainFile(), 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000295 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000296 llvm::DIType(), llvm::DIArray());
297 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
298
299 llvm::DIType ISATy =
300 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000301 TheCU, "", getOrCreateMainFile(),
Devang Patelc8972c62010-07-28 01:33:15 +0000302 0, Size, 0, 0, 0, OCTy);
303
304 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
305
306 llvm::DIType FieldTy =
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000307 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, TheCU,
308 "isa", getOrCreateMainFile(),
Devang Patelc8972c62010-07-28 01:33:15 +0000309 0,Size, 0, 0, 0, ISATy);
310 EltTys.push_back(FieldTy);
311 llvm::DIArray Elements =
312 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
313
314 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000315 TheCU, "objc_object",
316 getOrCreateMainFile(),
317 0, 0, 0, 0, 0,
Devang Patelc8972c62010-07-28 01:33:15 +0000318 llvm::DIType(), Elements);
319 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320 case BuiltinType::UChar:
321 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
322 case BuiltinType::Char_S:
323 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
324 case BuiltinType::UShort:
325 case BuiltinType::UInt:
326 case BuiltinType::ULong:
327 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
328 case BuiltinType::Short:
329 case BuiltinType::Int:
330 case BuiltinType::Long:
331 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
332 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
333 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000334 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000335 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000336 }
Devang Patel05127ca2010-07-28 23:23:29 +0000337
338 switch (BT->getKind()) {
339 case BuiltinType::Long: BTName = "long int"; break;
340 case BuiltinType::LongLong: BTName = "long long int"; break;
341 case BuiltinType::ULong: BTName = "long unsigned int"; break;
342 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
343 default:
344 BTName = BT->getName(CGM.getContext().getLangOptions());
345 break;
346 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000347 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000348 uint64_t Size = CGM.getContext().getTypeSize(BT);
349 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000350 uint64_t Offset = 0;
Devang Patelca80a5f2009-10-20 19:55:01 +0000351 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000352 DebugFactory.CreateBasicType(TheCU, BTName, getOrCreateMainFile(),
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000353 0, Size, Align, Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000354 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000355}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000356
Chris Lattnerb7003772009-04-23 06:13:01 +0000357llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000358 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000359 // Bit size, align and offset of the type.
360 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
361 if (Ty->isComplexIntegerType())
362 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Anders Carlsson20f12a22009-12-06 18:00:51 +0000364 uint64_t Size = CGM.getContext().getTypeSize(Ty);
365 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000366 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Devang Patelca80a5f2009-10-20 19:55:01 +0000368 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000369 DebugFactory.CreateBasicType(TheCU, "complex", getOrCreateMainFile(),
370 0, Size, Align, Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000371 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000372}
373
John McCalla1805292009-09-25 01:40:47 +0000374/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000375/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000376llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000377 QualifierCollector Qc;
378 const Type *T = Qc.strip(Ty);
379
380 // Ignore these qualifiers for now.
381 Qc.removeObjCGCAttr();
382 Qc.removeAddressSpace();
383
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 // We will create one Derived type for one qualifier and recurse to handle any
385 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000386 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000387 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000388 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000389 Qc.removeConst();
390 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000392 Qc.removeVolatile();
393 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000394 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000395 Qc.removeRestrict();
396 } else {
397 assert(Qc.empty() && "Unknown type qualifier for debug info");
398 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399 }
Mike Stump1eb44332009-09-09 15:08:12 +0000400
John McCalla1805292009-09-25 01:40:47 +0000401 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
402
Daniel Dunbar3845f862008-10-31 03:54:29 +0000403 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
404 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000406 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000407 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000408 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409}
410
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000411llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000412 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000413 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000414 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
415 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000416 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000417}
418
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000420 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000421 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
422 Ty->getPointeeType(), Unit);
423}
424
Devang Patelc69e1cf2010-09-30 19:05:55 +0000425/// CreatePointeeType - Create PointTee type. If Pointee is a record
426/// then emit record's fwd if debug info size reduction is enabled.
427llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
428 llvm::DIFile Unit) {
429 if (!CGM.getCodeGenOpts().LimitDebugInfo)
430 return getOrCreateType(PointeeTy, Unit);
431
432 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
433 RecordDecl *RD = RTy->getDecl();
434 unsigned RTag;
435 if (RD->isStruct())
436 RTag = llvm::dwarf::DW_TAG_structure_type;
437 else if (RD->isUnion())
438 RTag = llvm::dwarf::DW_TAG_union_type;
439 else {
440 assert(RD->isClass() && "Unknown RecordType!");
441 RTag = llvm::dwarf::DW_TAG_class_type;
442 }
443
444 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
445 unsigned Line = getLineNumber(RD->getLocation());
446 llvm::DIDescriptor FDContext =
447 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
448
449 return
450 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
451 DefUnit, Line, 0, 0, 0,
452 llvm::DIType::FlagFwdDecl,
453 llvm::DIType(), llvm::DIArray());
454 }
455 return getOrCreateType(PointeeTy, Unit);
456
457}
458
Anders Carlssona031b352009-11-06 19:19:55 +0000459llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
460 const Type *Ty,
461 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000462 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000464
465 // Size is always the size of a pointer. We can't use getTypeSize here
466 // because that does not return the correct value for references.
467 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000468 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
469 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Devang Patelc69e1cf2010-09-30 19:05:55 +0000471 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
472 0, Size, Align, 0, 0,
473 CreatePointeeType(PointeeTy, Unit));
474
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000475}
476
Mike Stump9bc093c2009-05-14 02:03:51 +0000477llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000478 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000479 if (BlockLiteralGenericSet)
480 return BlockLiteralGeneric;
481
Mike Stump9bc093c2009-05-14 02:03:51 +0000482 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
483
484 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
485
486 llvm::DIType FieldTy;
487
488 QualType FType;
489 uint64_t FieldSize, FieldOffset;
490 unsigned FieldAlign;
491
492 llvm::DIArray Elements;
493 llvm::DIType EltTy, DescTy;
494
495 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000496 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000497 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
498 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000499
Daniel Dunbarca308df2009-05-26 19:40:20 +0000500 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000501 EltTys.clear();
502
Devang Patele2472482010-09-29 21:05:52 +0000503 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000504 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000505
Mike Stump9bc093c2009-05-14 02:03:51 +0000506 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000507 Unit, LineNo, FieldOffset, 0, 0,
508 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Mike Stump9bc093c2009-05-14 02:03:51 +0000510 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 uint64_t Size = CGM.getContext().getTypeSize(Ty);
512 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Mike Stump9bc093c2009-05-14 02:03:51 +0000514 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000515 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000516 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000517
518 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000519 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000520 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000521 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000522 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
523 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000524 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000525 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000526
Anders Carlsson20f12a22009-12-06 18:00:51 +0000527 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000528 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000529 FieldSize = CGM.getContext().getTypeSize(Ty);
530 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000531 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000532 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000533 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000534 FieldOffset, 0, FieldTy);
535 EltTys.push_back(FieldTy);
536
537 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000538 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000539
540 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000541 Unit, LineNo, FieldOffset, 0, 0,
542 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Mike Stump9bc093c2009-05-14 02:03:51 +0000544 BlockLiteralGenericSet = true;
545 BlockLiteralGeneric
546 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000547 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000548 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000549 return BlockLiteralGeneric;
550}
551
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000553 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000554 // Typedefs are derived from some other type. If we have a typedef of a
555 // typedef, make sure to emit the whole chain.
556 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner9c85ba32008-11-10 06:08:34 +0000558 // We don't set size information, but do specify where the typedef was
559 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000560 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000561
Devang Pateleb6d79b2010-02-01 21:34:11 +0000562 llvm::DIDescriptor TyContext
563 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
564 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000565 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000566 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000567 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000568 Ty->getDecl()->getName(), Unit,
569 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000570 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000571}
572
Chris Lattner9c85ba32008-11-10 06:08:34 +0000573llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000574 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000575 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000576
Chris Lattner9c85ba32008-11-10 06:08:34 +0000577 // Add the result type at least.
578 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Chris Lattner9c85ba32008-11-10 06:08:34 +0000580 // Set up remainder of arguments if there is a prototype.
581 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000582 if (isa<FunctionNoProtoType>(Ty))
583 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
584 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000585 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
586 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000587 }
588
Chris Lattner9c85ba32008-11-10 06:08:34 +0000589 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000590 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Devang Patelca80a5f2009-10-20 19:55:01 +0000592 llvm::DIType DbgTy =
593 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000594 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000595 0, 0, 0, 0, 0,
596 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000597 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000598}
599
Devang Patel428deb52010-01-19 00:00:59 +0000600/// CollectRecordFields - A helper function to collect debug info for
601/// record fields. This is used while creating debug info entry for a Record.
602void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000603CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000604 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
605 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000606 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
607 for (RecordDecl::field_iterator I = RD->field_begin(),
608 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000609 I != E; ++I, ++FieldNo) {
610 FieldDecl *Field = *I;
611 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000612 llvm::StringRef FieldName = Field->getName();
613
Devang Patel4835fdd2010-02-12 01:31:06 +0000614 // Ignore unnamed fields. Do not ignore unnamed records.
615 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000616 continue;
617
618 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000619 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
620 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000621 QualType FType = Field->getType();
622 uint64_t FieldSize = 0;
623 unsigned FieldAlign = 0;
624 if (!FType->isIncompleteArrayType()) {
625
626 // Bit size, align and offset of the type.
627 FieldSize = CGM.getContext().getTypeSize(FType);
628 Expr *BitWidth = Field->getBitWidth();
629 if (BitWidth)
630 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000631 FieldAlign = CGM.getContext().getTypeAlign(FType);
632 }
633
634 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
635
Devang Patel71f4ff62010-04-21 23:12:37 +0000636 unsigned Flags = 0;
637 AccessSpecifier Access = I->getAccess();
638 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000639 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000640 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000641 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000642
Devang Patel428deb52010-01-19 00:00:59 +0000643 // Create a DW_TAG_member node to remember the offset of this field in the
644 // struct. FIXME: This is an absolutely insane way to capture this
645 // information. When we gut debug info, this should be fixed.
646 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
647 FieldName, FieldDefUnit,
648 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000649 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000650 EltTys.push_back(FieldTy);
651 }
652}
653
Devang Patela6da1922010-01-28 00:28:01 +0000654/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
655/// function type is not updated to include implicit "this" pointer. Use this
656/// routine to get a method type which includes "this" pointer.
657llvm::DIType
658CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000659 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000660 llvm::DIType FnTy
661 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
662 0),
663 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000664
Devang Patela6da1922010-01-28 00:28:01 +0000665 // Add "this" pointer.
666
Devang Patelab699792010-05-07 18:12:35 +0000667 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000668 assert (Args.getNumElements() && "Invalid number of arguments!");
669
670 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
671
672 // First element is always return type. For 'void' functions it is NULL.
673 Elts.push_back(Args.getElement(0));
674
Devang Patel2ed8f002010-08-27 17:47:47 +0000675 if (!Method->isStatic())
676 {
677 // "this" pointer is always first argument.
678 ASTContext &Context = CGM.getContext();
679 QualType ThisPtr =
680 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
681 llvm::DIType ThisPtrType =
682 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000683
Devang Patel2ed8f002010-08-27 17:47:47 +0000684 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
685 Elts.push_back(ThisPtrType);
686 }
Devang Patela6da1922010-01-28 00:28:01 +0000687
688 // Copy rest of the arguments.
689 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
690 Elts.push_back(Args.getElement(i));
691
692 llvm::DIArray EltTypeArray =
693 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
694
695 return
696 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000697 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000698 0, 0, 0, 0, 0,
699 llvm::DIType(), EltTypeArray);
700}
701
Devang Patel58faf202010-10-22 17:11:50 +0000702/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
703/// inside a function.
704static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
705 if (const CXXRecordDecl *NRD =
706 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
707 return isFunctionLocalClass(NRD);
708 else if (isa<FunctionDecl>(RD->getDeclContext()))
709 return true;
710 return false;
711
712}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000713/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
714/// a single member function GlobalDecl.
715llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000716CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000717 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000718 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000719 bool IsCtorOrDtor =
720 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
721
722 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000723 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000724
725 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
726 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000727 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000728 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000729 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000730
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000731 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000732 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
733 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000734
735 // Collect virtual method info.
736 llvm::DIType ContainingType;
737 unsigned Virtuality = 0;
738 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000739
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000740 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000741 if (Method->isPure())
742 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
743 else
744 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
745
746 // It doesn't make sense to give a virtual destructor a vtable index,
747 // since a single destructor has two entries in the vtable.
748 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000749 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000750 ContainingType = RecordTy;
751 }
752
Devang Patele2472482010-09-29 21:05:52 +0000753 unsigned Flags = 0;
754 if (Method->isImplicit())
755 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000756 AccessSpecifier Access = Method->getAccess();
757 if (Access == clang::AS_private)
758 Flags |= llvm::DIDescriptor::FlagPrivate;
759 else if (Access == clang::AS_protected)
760 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000761 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
762 if (CXXC->isExplicit())
763 Flags |= llvm::DIDescriptor::FlagExplicit;
764 } else if (const CXXConversionDecl *CXXC =
765 dyn_cast<CXXConversionDecl>(Method)) {
766 if (CXXC->isExplicit())
767 Flags |= llvm::DIDescriptor::FlagExplicit;
768 }
Devang Patel3951e712010-10-07 22:03:49 +0000769 if (Method->hasPrototype())
770 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000771
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000772 llvm::DISubprogram SP =
773 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
774 MethodLinkageName,
775 MethodDefUnit, MethodLine,
776 MethodTy, /*isLocalToUnit=*/false,
Devang Patel33133572010-10-22 18:31:12 +0000777 /* isDefinition=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000778 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000779 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000780 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000781
782 // Don't cache ctors or dtors since we have to emit multiple functions for
783 // a single ctor or dtor.
784 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000785 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000786
787 return SP;
788}
789
Devang Patel4125fd22010-01-19 01:54:44 +0000790/// CollectCXXMemberFunctions - A helper function to collect debug info for
791/// C++ member functions.This is used while creating debug info entry for
792/// a Record.
793void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000794CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000795 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000796 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000797 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
798 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000799 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000800
Devang Pateld5322da2010-02-09 19:09:28 +0000801 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000802 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000803
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000804 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000805 }
806}
807
Devang Patel2ed8f002010-08-27 17:47:47 +0000808/// CollectCXXFriends - A helper function to collect debug info for
809/// C++ base classes. This is used while creating debug info entry for
810/// a Record.
811void CGDebugInfo::
812CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
813 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
814 llvm::DIType RecordTy) {
815
816 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
817 BE = RD->friend_end(); BI != BE; ++BI) {
818
819 TypeSourceInfo *TInfo = (*BI)->getFriendType();
820 if(TInfo)
821 {
822 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
823
824 llvm::DIType DTy =
825 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
826 RecordTy, llvm::StringRef(),
827 Unit, 0, 0, 0,
828 0, 0, Ty);
829
830 EltTys.push_back(DTy);
831 }
832
833 }
834}
835
Devang Patela245c5b2010-01-25 23:32:18 +0000836/// CollectCXXBases - A helper function to collect debug info for
837/// C++ base classes. This is used while creating debug info entry for
838/// a Record.
839void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000840CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000841 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000842 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000843
Devang Patel239cec62010-02-01 21:39:52 +0000844 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
845 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
846 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000847 unsigned BFlags = 0;
848 uint64_t BaseOffset;
849
850 const CXXRecordDecl *Base =
851 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
852
853 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000854 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000855 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000856 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000857 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000858 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000859 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000860
861 AccessSpecifier Access = BI->getAccessSpecifier();
862 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000863 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000864 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000865 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000866
867 llvm::DIType DTy =
868 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
869 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000870 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000871 BaseOffset, BFlags,
872 getOrCreateType(BI->getType(),
873 Unit));
874 EltTys.push_back(DTy);
875 }
Devang Patela245c5b2010-01-25 23:32:18 +0000876}
877
Devang Patel4ce3f202010-01-28 18:11:52 +0000878/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000879llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000880 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000881 return VTablePtrType;
882
883 ASTContext &Context = CGM.getContext();
884
885 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000886 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
887 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000888 llvm::DIType SubTy =
889 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000890 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000891 0, 0, 0, 0, 0, llvm::DIType(), SElements);
892
893 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
894 llvm::DIType vtbl_ptr_type
895 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000896 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000897 0, Size, 0, 0, 0, SubTy);
898
Devang Pateld58562e2010-03-09 22:49:11 +0000899 VTablePtrType =
900 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
901 Unit, "", Unit,
902 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000903 return VTablePtrType;
904}
905
Anders Carlsson046c2942010-04-17 20:15:18 +0000906/// getVTableName - Get vtable name for the given Class.
907llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000908 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000909 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000910
911 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000912 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000913 memcpy(StrPtr, Name.data(), Name.length());
914 return llvm::StringRef(StrPtr, Name.length());
915}
916
917
Anders Carlsson046c2942010-04-17 20:15:18 +0000918/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000919/// debug info entry in EltTys vector.
920void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000921CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000922 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000923 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000924
925 // If there is a primary base then it will hold vtable info.
926 if (RL.getPrimaryBase())
927 return;
928
929 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000930 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000931 return;
932
933 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
934 llvm::DIType VPTR
935 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000936 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000937 0, Size, 0, 0, 0,
938 getOrCreateVTablePtrType(Unit));
939 EltTys.push_back(VPTR);
940}
941
Devang Patelc69e1cf2010-09-30 19:05:55 +0000942/// getOrCreateRecordType - Emit record type's standalone debug info.
943llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
944 SourceLocation Loc) {
945 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
946 DebugFactory.RecordType(T);
947 return T;
948}
949
Devang Patel65e99f22009-02-25 01:36:11 +0000950/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000951llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000952 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000953 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Chris Lattner9c85ba32008-11-10 06:08:34 +0000955 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000956 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000957 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000958 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000959 Tag = llvm::dwarf::DW_TAG_union_type;
960 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000961 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000962 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000963 }
964
Chris Lattner9c85ba32008-11-10 06:08:34 +0000965 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000966 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
967 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner9c85ba32008-11-10 06:08:34 +0000969 // Records and classes and unions can all be recursive. To handle them, we
970 // first generate a debug descriptor for the struct as a forward declaration.
971 // Then (if it is a definition) we go through and get debug info for all of
972 // its members. Finally, we create a descriptor for the complete type (which
973 // may refer to the forward decl if the struct is recursive) and replace all
974 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000975 llvm::DIDescriptor FDContext =
976 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
977
978 // If this is just a forward declaration, construct an appropriately
979 // marked node and just return it.
980 if (!RD->getDefinition()) {
981 llvm::DICompositeType FwdDecl =
982 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
983 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000984 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000985 llvm::DIType(), llvm::DIArray());
986
987 return FwdDecl;
988 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000989
Dan Gohman86968372010-08-20 22:39:57 +0000990 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Devang Patelab699792010-05-07 18:12:35 +0000992 llvm::MDNode *MN = FwdDecl;
993 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000994 // Otherwise, insert it into the TypeCache so that recursive uses will find
995 // it.
Devang Patelab699792010-05-07 18:12:35 +0000996 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000997 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000998 RegionStack.push_back(FwdDeclNode);
999 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001000
1001 // Convert all the elements.
1002 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1003
Devang Pateld6c5a262010-02-01 21:52:22 +00001004 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001005 if (CXXDecl) {
1006 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +00001007 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001008 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001009
1010 // Collect static variables with initializers.
1011 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1012 I != E; ++I)
1013 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1014 if (const Expr *Init = V->getInit()) {
1015 Expr::EvalResult Result;
1016 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1017 llvm::ConstantInt *CI
1018 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1019
1020 // Create the descriptor for static variable.
1021 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1022 llvm::StringRef VName = V->getName();
1023 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1024 // Do not use DIGlobalVariable for enums.
1025 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1026 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1027 getLineNumber(V->getLocation()),
1028 VTy, true, true, CI);
1029 }
1030 }
1031 }
1032 }
1033
Devang Pateld6c5a262010-02-01 21:52:22 +00001034 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001035 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001036 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001037 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001038 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001039
1040 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001041 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001042 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1043 // Seek non virtual primary base root.
1044 while (1) {
1045 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1046 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1047 if (PBT && !BRL.getPrimaryBaseWasVirtual())
1048 PBase = PBT;
1049 else
1050 break;
1051 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001052 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001053 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001054 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001055 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001056 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Chris Lattner9c85ba32008-11-10 06:08:34 +00001059 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001060 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001061
1062 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001063 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1064 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Devang Patele4c1ea02010-03-11 20:01:48 +00001066 RegionStack.pop_back();
1067 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1068 RegionMap.find(Ty->getDecl());
1069 if (RI != RegionMap.end())
1070 RegionMap.erase(RI);
1071
Devang Patel411894b2010-02-01 22:40:08 +00001072 llvm::DIDescriptor RDContext =
1073 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001074
1075 llvm::StringRef RDName = RD->getName();
1076 // If this is a class, include the template arguments also.
1077 if (Tag == llvm::dwarf::DW_TAG_class_type)
1078 RDName = getClassName(RD);
1079
Devang Patel0ce73f62009-07-22 18:57:00 +00001080 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001081 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001082 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001083 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001084 llvm::DIType(), Elements,
1085 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001086
1087 // Now that we have a real decl for the struct, replace anything using the
1088 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001089 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001090 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001091 return RealDecl;
1092}
1093
John McCallc12c5bb2010-05-15 11:32:37 +00001094/// CreateType - get objective-c object type.
1095llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1096 llvm::DIFile Unit) {
1097 // Ignore protocols.
1098 return getOrCreateType(Ty->getBaseType(), Unit);
1099}
1100
Devang Patel9ca36b62009-02-26 21:10:26 +00001101/// CreateType - get objective-c interface type.
1102llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001103 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001104 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001105 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001106
1107 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001108 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001109 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001110 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001111
Dan Gohman45f7c782010-08-23 21:15:56 +00001112 // If this is just a forward declaration, return a special forward-declaration
1113 // debug type.
1114 if (ID->isForwardDecl()) {
1115 llvm::DICompositeType FwdDecl =
1116 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1117 DefUnit, Line, 0, 0, 0, 0,
1118 llvm::DIType(), llvm::DIArray(),
1119 RuntimeLang);
1120 return FwdDecl;
1121 }
1122
Devang Patel9ca36b62009-02-26 21:10:26 +00001123 // To handle recursive interface, we
1124 // first generate a debug descriptor for the struct as a forward declaration.
1125 // Then (if it is a definition) we go through and get debug info for all of
1126 // its members. Finally, we create a descriptor for the complete type (which
1127 // may refer to the forward decl if the struct is recursive) and replace all
1128 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001129 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Devang Patelab699792010-05-07 18:12:35 +00001131 llvm::MDNode *MN = FwdDecl;
1132 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001133 // Otherwise, insert it into the TypeCache so that recursive uses will find
1134 // it.
Devang Patelab699792010-05-07 18:12:35 +00001135 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001136 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001137 RegionStack.push_back(FwdDeclNode);
1138 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001139
1140 // Convert all the elements.
1141 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1142
Devang Pateld6c5a262010-02-01 21:52:22 +00001143 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001144 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001145 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001146 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001147 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001148 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001149 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001150 0 /* offset */, 0, SClassTy);
1151 EltTys.push_back(InhTag);
1152 }
1153
Devang Pateld6c5a262010-02-01 21:52:22 +00001154 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001155
1156 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001157 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001158 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001159 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1160
Devang Patel73621622009-11-25 17:37:31 +00001161 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001162
Devang Patelde135022009-04-27 22:40:36 +00001163 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001164 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001165 continue;
1166
Devang Patel9ca36b62009-02-26 21:10:26 +00001167 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001168 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1169 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001170 QualType FType = Field->getType();
1171 uint64_t FieldSize = 0;
1172 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001173
Devang Patel99c20eb2009-03-20 18:24:39 +00001174 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Devang Patel99c20eb2009-03-20 18:24:39 +00001176 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001177 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001178 Expr *BitWidth = Field->getBitWidth();
1179 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001180 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001181
Anders Carlsson20f12a22009-12-06 18:00:51 +00001182 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001183 }
1184
Mike Stump1eb44332009-09-09 15:08:12 +00001185 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1186
Devang Patelc20482b2009-03-19 00:23:53 +00001187 unsigned Flags = 0;
1188 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001189 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001190 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001191 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Devang Patel9ca36b62009-02-26 21:10:26 +00001193 // Create a DW_TAG_member node to remember the offset of this field in the
1194 // struct. FIXME: This is an absolutely insane way to capture this
1195 // information. When we gut debug info, this should be fixed.
1196 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1197 FieldName, FieldDefUnit,
1198 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001199 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001200 EltTys.push_back(FieldTy);
1201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Devang Patel9ca36b62009-02-26 21:10:26 +00001203 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001204 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001205
Devang Patele4c1ea02010-03-11 20:01:48 +00001206 RegionStack.pop_back();
1207 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1208 RegionMap.find(Ty->getDecl());
1209 if (RI != RegionMap.end())
1210 RegionMap.erase(RI);
1211
Devang Patel9ca36b62009-02-26 21:10:26 +00001212 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001213 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1214 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001215
Devang Patel6c1fddf2009-07-27 18:42:03 +00001216 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001217 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001218 Line, Size, Align, 0, 0, llvm::DIType(),
1219 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001220
1221 // Now that we have a real decl for the struct, replace anything using the
1222 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001223 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001224 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001225
Devang Patel9ca36b62009-02-26 21:10:26 +00001226 return RealDecl;
1227}
1228
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001230 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001231 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001232
Chris Lattner9c85ba32008-11-10 06:08:34 +00001233}
1234
1235llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001236 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001237 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1238 return CreateType(RT, Unit);
1239 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1240 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Chris Lattner9c85ba32008-11-10 06:08:34 +00001242 return llvm::DIType();
1243}
1244
Devang Patel70c23cd2010-02-23 22:59:39 +00001245llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001246 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001247 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1248 uint64_t NumElems = Ty->getNumElements();
1249 if (NumElems > 0)
1250 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001251
Benjamin Kramerad468862010-03-30 11:36:44 +00001252 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1253 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001254
1255 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1256 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1257
1258 return
1259 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001260 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001261 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001262 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001263}
1264
Chris Lattner9c85ba32008-11-10 06:08:34 +00001265llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001266 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001267 uint64_t Size;
1268 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001269
1270
Nuno Lopes010d5142009-01-28 00:35:17 +00001271 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001272 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001273 Size = 0;
1274 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001275 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001276 } else if (Ty->isIncompleteArrayType()) {
1277 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001278 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001279 } else {
1280 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001281 Size = CGM.getContext().getTypeSize(Ty);
1282 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001283 }
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattner9c85ba32008-11-10 06:08:34 +00001285 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1286 // interior arrays, do we care? Why aren't nested arrays represented the
1287 // obvious/recursive way?
1288 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1289 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001290 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001291 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001292 else {
1293 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1294 uint64_t Upper = 0;
1295 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1296 if (CAT->getSize().getZExtValue())
1297 Upper = CAT->getSize().getZExtValue() - 1;
1298 // FIXME: Verify this is right for VLAs.
1299 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1300 EltTy = Ty->getElementType();
1301 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001302 }
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattner9c85ba32008-11-10 06:08:34 +00001304 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001305 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001306
Devang Patelca80a5f2009-10-20 19:55:01 +00001307 llvm::DIType DbgTy =
1308 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001309 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001310 0, Size, Align, 0, 0,
1311 getOrCreateType(EltTy, Unit),
1312 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001313 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001314}
1315
Anders Carlssona031b352009-11-06 19:19:55 +00001316llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001317 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001318 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1319 Ty, Ty->getPointeeType(), Unit);
1320}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001321
Anders Carlsson20f12a22009-12-06 18:00:51 +00001322llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001323 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001324 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1325 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1326
1327 if (!Ty->getPointeeType()->isFunctionType()) {
1328 // We have a data member pointer type.
1329 return PointerDiffDITy;
1330 }
1331
1332 // We have a member function pointer type. Treat it as a struct with two
1333 // ptrdiff_t members.
1334 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1335
1336 uint64_t FieldOffset = 0;
1337 llvm::DIDescriptor ElementTypes[2];
1338
1339 // FIXME: This should probably be a function type instead.
1340 ElementTypes[0] =
1341 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001342 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001343 Info.first, Info.second, FieldOffset, 0,
1344 PointerDiffDITy);
1345 FieldOffset += Info.first;
1346
1347 ElementTypes[1] =
1348 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001349 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001350 Info.first, Info.second, FieldOffset, 0,
1351 PointerDiffDITy);
1352
1353 llvm::DIArray Elements =
1354 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1355 llvm::array_lengthof(ElementTypes));
1356
1357 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1358 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001359 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001360 0, 0, 0, llvm::DIType(), Elements);
1361}
1362
Devang Patel6237cea2010-08-23 22:07:25 +00001363/// CreateEnumType - get enumeration type.
1364llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1365 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1366
1367 // Create DIEnumerator elements for each enumerator.
1368 for (EnumDecl::enumerator_iterator
1369 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1370 Enum != EnumEnd; ++Enum) {
1371 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1372 Enum->getInitVal().getZExtValue()));
1373 }
1374
1375 // Return a CompositeType for the enum itself.
1376 llvm::DIArray EltArray =
1377 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1378
1379 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1380 unsigned Line = getLineNumber(ED->getLocation());
1381 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001382 uint64_t Align = 0;
1383 if (!ED->getTypeForDecl()->isIncompleteType()) {
1384 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1385 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1386 }
Devang Patel4bc48872010-10-27 23:23:58 +00001387 llvm::DIDescriptor EnumContext =
1388 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001389 llvm::DIType DbgTy =
1390 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel4bc48872010-10-27 23:23:58 +00001391 EnumContext, ED->getName(),
1392 DefUnit, Line, Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001393 llvm::DIType(), EltArray);
1394 return DbgTy;
1395}
1396
Douglas Gregor840943d2009-12-21 20:18:30 +00001397static QualType UnwrapTypeForDebugInfo(QualType T) {
1398 do {
1399 QualType LastT = T;
1400 switch (T->getTypeClass()) {
1401 default:
1402 return T;
1403 case Type::TemplateSpecialization:
1404 T = cast<TemplateSpecializationType>(T)->desugar();
1405 break;
1406 case Type::TypeOfExpr: {
1407 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1408 T = Ty->getUnderlyingExpr()->getType();
1409 break;
1410 }
1411 case Type::TypeOf:
1412 T = cast<TypeOfType>(T)->getUnderlyingType();
1413 break;
1414 case Type::Decltype:
1415 T = cast<DecltypeType>(T)->getUnderlyingType();
1416 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001417 case Type::Elaborated:
1418 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001419 break;
1420 case Type::SubstTemplateTypeParm:
1421 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1422 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001423 }
1424
1425 assert(T != LastT && "Type unwrapping failed to unwrap!");
1426 if (T == LastT)
1427 return T;
1428 } while (true);
1429
1430 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001431}
1432
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001433/// getOrCreateType - Get the type from the cache or create a new
1434/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001435llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001436 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001437 if (Ty.isNull())
1438 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor840943d2009-12-21 20:18:30 +00001440 // Unwrap the type as needed for debug information.
1441 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001442
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001443 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001444 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001445 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001446 if (it != TypeCache.end()) {
1447 // Verify that the debug info still exists.
1448 if (&*it->second)
1449 return llvm::DIType(cast<llvm::MDNode>(it->second));
1450 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001451
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001452 // Otherwise create the type.
1453 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001454
1455 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001456 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001457 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001458}
1459
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001460/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001461llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001462 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001463 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001464 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001465 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001466
Douglas Gregor2101a822009-12-21 19:57:21 +00001467 const char *Diag = 0;
1468
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001469 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001470 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001471#define TYPE(Class, Base)
1472#define ABSTRACT_TYPE(Class, Base)
1473#define NON_CANONICAL_TYPE(Class, Base)
1474#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1475#include "clang/AST/TypeNodes.def"
1476 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001477
Anders Carlssonbfe69952009-11-06 18:24:04 +00001478 // FIXME: Handle these.
1479 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001480 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001481
1482 case Type::Vector:
1483 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001484 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001485 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001486 case Type::ObjCObject:
1487 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001488 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001489 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001490 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001491 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1492 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001493 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001494 return CreateType(cast<BlockPointerType>(Ty), Unit);
1495 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001496 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001497 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001498 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001499 case Type::FunctionProto:
1500 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001501 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001502 case Type::ConstantArray:
1503 case Type::VariableArray:
1504 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001505 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001506
1507 case Type::LValueReference:
1508 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1509
Anders Carlsson20f12a22009-12-06 18:00:51 +00001510 case Type::MemberPointer:
1511 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001512
1513 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001514 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001515 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001516 case Type::TypeOfExpr:
1517 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001518 case Type::Decltype:
1519 llvm_unreachable("type should have been unwrapped!");
1520 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001521
1522 case Type::RValueReference:
1523 // FIXME: Implement!
1524 Diag = "rvalue references";
1525 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001526 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001527
1528 assert(Diag && "Fall through without a diagnostic?");
1529 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1530 "debug information for %0 is not yet supported");
1531 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1532 << Diag;
1533 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001534}
1535
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001536/// CreateMemberType - Create new member and increase Offset by FType's size.
1537llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1538 llvm::StringRef Name,
1539 uint64_t *Offset) {
1540 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1541 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1542 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1543 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1544 Unit, Name, Unit, 0,
1545 FieldSize, FieldAlign,
1546 *Offset, 0, FieldTy);
1547 *Offset += FieldSize;
1548 return Ty;
1549}
1550
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001551/// EmitFunctionStart - Constructs the debug code for entering a function -
1552/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001553void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001554 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001555 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Devang Patel9c6c3a02010-01-14 00:36:21 +00001557 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001558 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001559
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001560 FnBeginRegionCount.push_back(RegionStack.size());
1561
Devang Patel9c6c3a02010-01-14 00:36:21 +00001562 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001563 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001564 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1565 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001566 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001567 // If there is a DISubprogram for this function available then use it.
1568 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1569 FI = SPCache.find(FD);
1570 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001571 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001572 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1573 llvm::MDNode *SPN = SP;
1574 RegionStack.push_back(SPN);
1575 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001576 return;
1577 }
1578 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001579 Name = getFunctionName(FD);
1580 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001581 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001582 if (LinkageName == Name)
1583 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001584 if (FD->hasPrototype())
1585 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001586 if (const NamespaceDecl *NSDecl =
1587 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1588 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001589 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001590 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001591 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001592 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001593 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001594 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001595 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001596 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001597 if (!Name.empty() && Name[0] == '\01')
1598 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Devang Patel970c6182010-04-24 00:49:16 +00001600 // It is expected that CurLoc is set before using EmitFunctionStart.
1601 // Usually, CurLoc points to the left bracket location of compound
1602 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001603 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001604 if (D->isImplicit())
1605 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001606 llvm::DISubprogram SP =
Devang Patela2e57692010-10-28 17:27:32 +00001607 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit,
1608 LineNo, getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001609 Fn->hasInternalLinkage(), true/*definition*/,
1610 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001611 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001612 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001614 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001615 llvm::MDNode *SPN = SP;
1616 RegionStack.push_back(SPN);
1617 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001618
1619 // Clear stack used to keep track of #line directives.
1620 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001621}
1622
1623
Devang Patel4d939e62010-07-20 22:20:10 +00001624void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001625 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001627 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001628 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001629 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001630 || (SM.getInstantiationLineNumber(CurLoc) ==
1631 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001632 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001633 // New Builder may not be in sync with CGDebugInfo.
1634 if (!Builder.getCurrentDebugLocation().isUnknown())
1635 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001636
1637 // Update last state.
1638 PrevLoc = CurLoc;
1639
Chris Lattnerc6034632010-04-01 06:31:43 +00001640 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001641 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1642 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001643 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001644}
1645
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001646/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1647/// has introduced scope change.
1648void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1649 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1650 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1651 return;
1652 SourceManager &SM = CGM.getContext().getSourceManager();
1653 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1654 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1655
1656 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1657 return;
1658
1659 // If #line directive stack is empty then we are entering a new scope.
1660 if (LineDirectiveFiles.empty()) {
1661 EmitRegionStart(Builder);
1662 LineDirectiveFiles.push_back(PCLoc.getFilename());
1663 return;
1664 }
1665
1666 assert (RegionStack.size() >= LineDirectiveFiles.size()
1667 && "error handling #line regions!");
1668
1669 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001670 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001671 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1672 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001673 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001674 SeenThisFile = true;
1675 break;
1676 }
1677
1678 // If #line for this file is seen earlier then pop out #line regions.
1679 if (SeenThisFile) {
1680 while (!LineDirectiveFiles.empty()) {
1681 const char *LastFile = LineDirectiveFiles.back();
1682 RegionStack.pop_back();
1683 LineDirectiveFiles.pop_back();
1684 if (!strcmp(PPLoc.getFilename(), LastFile))
1685 break;
1686 }
1687 return;
1688 }
1689
1690 // .. otherwise insert new #line region.
1691 EmitRegionStart(Builder);
1692 LineDirectiveFiles.push_back(PCLoc.getFilename());
1693
1694 return;
1695}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001696/// EmitRegionStart- Constructs the debug code for entering a declarative
1697/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001698void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001699 llvm::DIDescriptor D =
1700 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1701 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001702 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001703 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001704 getLineNumber(CurLoc),
1705 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001706 llvm::MDNode *DN = D;
1707 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001708}
1709
1710/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1711/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001712void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001713 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1714
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001715 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001716 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001718 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001719}
1720
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001721/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1722void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1723 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1724 unsigned RCount = FnBeginRegionCount.back();
1725 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1726
1727 // Pop all regions for this function.
1728 while (RegionStack.size() != RCount)
1729 EmitRegionEnd(Builder);
1730 FnBeginRegionCount.pop_back();
1731}
1732
Devang Patel809b9bb2010-02-10 18:49:08 +00001733// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1734// See BuildByRefType.
1735llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1736 uint64_t *XOffset) {
1737
1738 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1739
1740 QualType FType;
1741 uint64_t FieldSize, FieldOffset;
1742 unsigned FieldAlign;
1743
Devang Patel17800552010-03-09 00:44:50 +00001744 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001745 QualType Type = VD->getType();
1746
1747 FieldOffset = 0;
1748 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001749 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1750 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001751 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001752 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1753 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1754
Devang Patel809b9bb2010-02-10 18:49:08 +00001755 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1756 if (HasCopyAndDispose) {
1757 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001758 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1759 &FieldOffset));
1760 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1761 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001762 }
1763
1764 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1765 if (Align > CharUnits::fromQuantity(
1766 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1767 unsigned AlignedOffsetInBytes
1768 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1769 unsigned NumPaddingBytes
1770 = AlignedOffsetInBytes - FieldOffset/8;
1771
1772 if (NumPaddingBytes > 0) {
1773 llvm::APInt pad(32, NumPaddingBytes);
1774 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1775 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001776 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001777 }
1778 }
1779
1780 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001781 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001782 FieldSize = CGM.getContext().getTypeSize(FType);
1783 FieldAlign = Align.getQuantity()*8;
1784
1785 *XOffset = FieldOffset;
1786 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001787 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001788 0, FieldSize, FieldAlign,
1789 FieldOffset, 0, FieldTy);
1790 EltTys.push_back(FieldTy);
1791 FieldOffset += FieldSize;
1792
1793 llvm::DIArray Elements =
1794 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1795
Devang Patele2472482010-09-29 21:05:52 +00001796 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001797
1798 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001799 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001800 0, FieldOffset, 0, 0, Flags,
1801 llvm::DIType(), Elements);
1802
1803}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001804/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001805void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001806 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001807 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1808
Devang Patel17800552010-03-09 00:44:50 +00001809 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001810 llvm::DIType Ty;
1811 uint64_t XOffset = 0;
1812 if (VD->hasAttr<BlocksAttr>())
1813 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1814 else
1815 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001816
Devang Patelf4e54a22010-05-07 23:05:55 +00001817 // If there is not any debug info for type then do not emit debug info
1818 // for this variable.
1819 if (!Ty)
1820 return;
1821
Chris Lattner9c85ba32008-11-10 06:08:34 +00001822 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001823 unsigned Line = getLineNumber(VD->getLocation());
1824 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001825 unsigned Flags = 0;
1826 if (VD->isImplicit())
1827 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001828 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001829
1830 llvm::StringRef Name = VD->getName();
1831 if (!Name.empty()) {
1832 // Create the descriptor for the variable.
1833 llvm::DIVariable D =
1834 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1835 Name, Unit, Line, Ty,
1836 CGM.getLangOptions().Optimize, Flags);
1837
1838 // Insert an llvm.dbg.declare into the current block.
1839 llvm::Instruction *Call =
1840 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1841
1842 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001843 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001844 }
1845
1846 // If VD is an anonymous union then Storage represents value for
1847 // all union fields.
1848 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1849 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1850 if (RD->isUnion()) {
1851 for (RecordDecl::field_iterator I = RD->field_begin(),
1852 E = RD->field_end();
1853 I != E; ++I) {
1854 FieldDecl *Field = *I;
1855 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1856 llvm::StringRef FieldName = Field->getName();
1857
1858 // Ignore unnamed fields. Do not ignore unnamed records.
1859 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1860 continue;
1861
1862 // Use VarDecl's Tag, Scope and Line number.
1863 llvm::DIVariable D =
1864 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1865 FieldName, Unit, Line, FieldTy,
1866 CGM.getLangOptions().Optimize, Flags);
1867
1868 // Insert an llvm.dbg.declare into the current block.
1869 llvm::Instruction *Call =
1870 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1871
1872 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1873 }
1874 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001875}
1876
Mike Stumpb1a6e682009-09-30 02:43:10 +00001877/// EmitDeclare - Emit local variable declaration debug info.
1878void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1879 llvm::Value *Storage, CGBuilderTy &Builder,
1880 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001881 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001882 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1883
Devang Patel2b594b92010-04-26 23:28:46 +00001884 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001885 return;
1886
1887 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001888 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001889 llvm::DIType Ty;
1890 if (VD->hasAttr<BlocksAttr>())
1891 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1892 else
1893 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001894
1895 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001896 unsigned Line = getLineNumber(VD->getLocation());
1897 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001898
Devang Pateld6c5a262010-02-01 21:52:22 +00001899 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001900 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001901 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1902 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1903 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1904 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001905 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001906 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1907 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001908 // offset of __forwarding field
1909 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001910 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1911 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1912 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001913 // offset of x field
1914 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001915 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001916 }
1917
1918 // Create the descriptor for the variable.
1919 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001920 DebugFactory.CreateComplexVariable(Tag,
1921 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001922 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001923 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001924 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001925 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001926 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001927
Chris Lattnerc6034632010-04-01 06:31:43 +00001928 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001929 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001930}
1931
Devang Pateld6c5a262010-02-01 21:52:22 +00001932void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001933 llvm::Value *Storage,
1934 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001935 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001936}
1937
Mike Stumpb1a6e682009-09-30 02:43:10 +00001938void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1939 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1940 CodeGenFunction *CGF) {
1941 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1942}
1943
Chris Lattner9c85ba32008-11-10 06:08:34 +00001944/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1945/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001946void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001947 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001948 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001949}
1950
1951
1952
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001953/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001954void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001955 const VarDecl *D) {
1956
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001957 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001958 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001959 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001960
Devang Pateleb6d79b2010-02-01 21:34:11 +00001961 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001962 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001964 // CodeGen turns int[] into int[1] so we'll do the same here.
1965 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001967 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001968 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Anders Carlsson20f12a22009-12-06 18:00:51 +00001970 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001971 ArrayType::Normal, 0);
1972 }
Devang Patel5d822f02010-04-29 17:48:37 +00001973 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001974 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001975 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001976 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001977 if (LinkageName == DeclName)
1978 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001979 llvm::DIDescriptor DContext =
1980 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001981 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1982 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001983 Var->hasInternalLinkage(),
1984 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001985}
1986
Devang Patel9ca36b62009-02-26 21:10:26 +00001987/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001988void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001989 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001990 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001991 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001992 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001993
Devang Pateld6c5a262010-02-01 21:52:22 +00001994 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001995
Devang Pateld6c5a262010-02-01 21:52:22 +00001996 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001997 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Devang Patel9ca36b62009-02-26 21:10:26 +00001999 // CodeGen turns int[] into int[1] so we'll do the same here.
2000 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Devang Patel9ca36b62009-02-26 21:10:26 +00002002 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002003 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Anders Carlsson20f12a22009-12-06 18:00:51 +00002005 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002006 ArrayType::Normal, 0);
2007 }
2008
Devang Patelf6a39b72009-10-20 18:26:30 +00002009 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00002010 getOrCreateType(T, Unit),
2011 Var->hasInternalLinkage(),
2012 true/*definition*/, Var);
2013}
Devang Patelabb485f2010-02-01 19:16:32 +00002014
Devang Patel25c2c8f2010-08-10 17:53:33 +00002015/// EmitGlobalVariable - Emit global variable's debug info.
2016void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002017 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002018 // Create the descriptor for the variable.
2019 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2020 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002021 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002022 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2023 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2024 Ty = CreateEnumType(ED, Unit);
2025 }
Devang Patel0317ab02010-08-10 18:27:15 +00002026 // Do not use DIGlobalVariable for enums.
2027 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2028 return;
Devang Patel8d308382010-08-10 07:24:25 +00002029 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2030 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002031 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002032}
2033
Devang Patelabb485f2010-02-01 19:16:32 +00002034/// getOrCreateNamesSpace - Return namespace descriptor for the given
2035/// namespace decl.
2036llvm::DINameSpace
2037CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2038 llvm::DIDescriptor Unit) {
2039 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2040 NameSpaceCache.find(NSDecl);
2041 if (I != NameSpaceCache.end())
2042 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2043
Devang Patel8ab870d2010-05-12 23:46:38 +00002044 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002045 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002046 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002047 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002048 llvm::DINameSpace NS =
Devang Patel8c376682010-10-28 19:12:46 +00002049 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002050 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002051 return NS;
2052}