blob: d532214cc96b9d9a0f712fd014c0bfa3de9fbecb [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"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000035#include "llvm/Support/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();
Douglas Gregor910f8002010-11-07 23:05:16 +0000139 Args = TemplateArgs.data();
140 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000141 }
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) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000156 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000157 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000158
Douglas Gregor8c457a82010-11-11 20:45:16 +0000159 if (PLoc.isInvalid())
160 // If the location is not valid then use main input file.
161 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
162 TheCU);
163
Ted Kremenek9c250392010-03-30 00:27:51 +0000164 // Cache the results.
165 const char *fname = PLoc.getFilename();
166 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
167 DIFileCache.find(fname);
168
169 if (it != DIFileCache.end()) {
170 // Verify that the information still exists.
171 if (&*it->second)
172 return llvm::DIFile(cast<llvm::MDNode>(it->second));
173 }
174
Devang Patelac4d13c2010-07-27 15:17:16 +0000175 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
176 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000177
Devang Patelab699792010-05-07 18:12:35 +0000178 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000179 return F;
180
Devang Patel17800552010-03-09 00:44:50 +0000181}
Devang Patel8ab870d2010-05-12 23:46:38 +0000182
Devang Patel532105f2010-10-28 22:03:20 +0000183/// getOrCreateMainFile - Get the file info for main compile unit.
184llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
185 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
186 TheCU);
187}
188
Devang Patel8ab870d2010-05-12 23:46:38 +0000189/// getLineNumber - Get line number for the location. If location is invalid
190/// then use current location.
191unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
192 assert (CurLoc.isValid() && "Invalid current location!");
193 SourceManager &SM = CGM.getContext().getSourceManager();
194 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000195 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000196}
197
198/// getColumnNumber - Get column number for the location. If location is
199/// invalid then use current location.
200unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
201 assert (CurLoc.isValid() && "Invalid current location!");
202 SourceManager &SM = CGM.getContext().getSourceManager();
203 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000204 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000205}
206
Devang Patelac4d13c2010-07-27 15:17:16 +0000207llvm::StringRef CGDebugInfo::getCurrentDirname() {
208 if (!CWDName.empty())
209 return CWDName;
210 char *CompDirnamePtr = NULL;
211 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
212 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
213 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
214 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
215}
216
Devang Patel17800552010-03-09 00:44:50 +0000217/// CreateCompileUnit - Create new compile unit.
218void CGDebugInfo::CreateCompileUnit() {
219
220 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000221 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000222 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
223 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000224 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000225
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000226 // The main file name provided via the "-main-file-name" option contains just
227 // the file name itself with no path information. This file name may have had
228 // a relative path, so we look into the actual file entry for the main
229 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000230 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000231 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000232 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000233 if (MainFileDir != ".")
234 MainFileName = MainFileDir + "/" + MainFileName;
235 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000236
Devang Patelac4d13c2010-07-27 15:17:16 +0000237 // Save filename string.
238 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
239 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
240 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
241
Chris Lattner515455a2009-03-25 03:28:08 +0000242 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000243 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000244 if (LO.CPlusPlus) {
245 if (LO.ObjC1)
246 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
247 else
248 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
249 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000250 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000251 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000252 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000253 } else {
254 LangTag = llvm::dwarf::DW_LANG_C89;
255 }
Devang Patel446c6192009-04-17 21:06:59 +0000256
Daniel Dunbar19f19832010-08-24 17:41:09 +0000257 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000258
259 // Figure out which version of the ObjC runtime we have.
260 unsigned RuntimeVers = 0;
261 if (LO.ObjC1)
262 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000264 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000265 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000266 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000267 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000268 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000269}
270
Devang Patel65e99f22009-02-25 01:36:11 +0000271/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000272/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000273llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000274 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000275 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000276 switch (BT->getKind()) {
277 default:
278 case BuiltinType::Void:
279 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000280 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000281 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000282 TheCU, "objc_class",
283 getOrCreateMainFile(), 0, 0, 0,
Devang Patela2e57692010-10-28 17:27:32 +0000284 0, llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000285 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000286 case BuiltinType::ObjCId: {
287 // typedef struct objc_class *Class;
288 // typedef struct objc_object {
289 // Class isa;
290 // } *id;
291
292 llvm::DIType OCTy =
293 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000294 TheCU, "objc_class",
295 getOrCreateMainFile(), 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000296 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000297 llvm::DIType(), llvm::DIArray());
298 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
299
300 llvm::DIType ISATy =
301 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000302 TheCU, "", getOrCreateMainFile(),
Devang Patelc8972c62010-07-28 01:33:15 +0000303 0, Size, 0, 0, 0, OCTy);
304
305 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
306
307 llvm::DIType FieldTy =
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000308 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, TheCU,
309 "isa", getOrCreateMainFile(),
Devang Patelc8972c62010-07-28 01:33:15 +0000310 0,Size, 0, 0, 0, ISATy);
311 EltTys.push_back(FieldTy);
312 llvm::DIArray Elements =
313 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
314
315 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000316 TheCU, "objc_object",
317 getOrCreateMainFile(),
318 0, 0, 0, 0, 0,
Devang Patelc8972c62010-07-28 01:33:15 +0000319 llvm::DIType(), Elements);
320 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000321 case BuiltinType::UChar:
322 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
323 case BuiltinType::Char_S:
324 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
325 case BuiltinType::UShort:
326 case BuiltinType::UInt:
327 case BuiltinType::ULong:
328 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
329 case BuiltinType::Short:
330 case BuiltinType::Int:
331 case BuiltinType::Long:
332 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
333 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
334 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000335 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000336 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000337 }
Devang Patel05127ca2010-07-28 23:23:29 +0000338
339 switch (BT->getKind()) {
340 case BuiltinType::Long: BTName = "long int"; break;
341 case BuiltinType::LongLong: BTName = "long long int"; break;
342 case BuiltinType::ULong: BTName = "long unsigned int"; break;
343 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
344 default:
345 BTName = BT->getName(CGM.getContext().getLangOptions());
346 break;
347 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000349 uint64_t Size = CGM.getContext().getTypeSize(BT);
350 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000351 uint64_t Offset = 0;
Devang Patelca80a5f2009-10-20 19:55:01 +0000352 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000353 DebugFactory.CreateBasicType(TheCU, BTName, getOrCreateMainFile(),
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000354 0, Size, Align, Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000355 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000356}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000357
Chris Lattnerb7003772009-04-23 06:13:01 +0000358llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000359 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000360 // Bit size, align and offset of the type.
361 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
362 if (Ty->isComplexIntegerType())
363 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Anders Carlsson20f12a22009-12-06 18:00:51 +0000365 uint64_t Size = CGM.getContext().getTypeSize(Ty);
366 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000367 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Devang Patelca80a5f2009-10-20 19:55:01 +0000369 llvm::DIType DbgTy =
Devang Patel532105f2010-10-28 22:03:20 +0000370 DebugFactory.CreateBasicType(TheCU, "complex", getOrCreateMainFile(),
371 0, Size, Align, Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000372 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000373}
374
John McCalla1805292009-09-25 01:40:47 +0000375/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000376/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000377llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000378 QualifierCollector Qc;
379 const Type *T = Qc.strip(Ty);
380
381 // Ignore these qualifiers for now.
382 Qc.removeObjCGCAttr();
383 Qc.removeAddressSpace();
384
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 // We will create one Derived type for one qualifier and recurse to handle any
386 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000387 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000388 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000389 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000390 Qc.removeConst();
391 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000392 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000393 Qc.removeVolatile();
394 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000396 Qc.removeRestrict();
397 } else {
398 assert(Qc.empty() && "Unknown type qualifier for debug info");
399 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000400 }
Mike Stump1eb44332009-09-09 15:08:12 +0000401
John McCalla1805292009-09-25 01:40:47 +0000402 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
403
Daniel Dunbar3845f862008-10-31 03:54:29 +0000404 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
405 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000406 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000407 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000408 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000409 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000410}
411
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000412llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000413 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000414 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000415 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
416 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000417 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000418}
419
Chris Lattner9c85ba32008-11-10 06:08:34 +0000420llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000421 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000422 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
423 Ty->getPointeeType(), Unit);
424}
425
Devang Patelc69e1cf2010-09-30 19:05:55 +0000426/// CreatePointeeType - Create PointTee type. If Pointee is a record
427/// then emit record's fwd if debug info size reduction is enabled.
428llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
429 llvm::DIFile Unit) {
430 if (!CGM.getCodeGenOpts().LimitDebugInfo)
431 return getOrCreateType(PointeeTy, Unit);
432
433 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
434 RecordDecl *RD = RTy->getDecl();
435 unsigned RTag;
436 if (RD->isStruct())
437 RTag = llvm::dwarf::DW_TAG_structure_type;
438 else if (RD->isUnion())
439 RTag = llvm::dwarf::DW_TAG_union_type;
440 else {
441 assert(RD->isClass() && "Unknown RecordType!");
442 RTag = llvm::dwarf::DW_TAG_class_type;
443 }
444
445 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
446 unsigned Line = getLineNumber(RD->getLocation());
447 llvm::DIDescriptor FDContext =
448 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
449
450 return
451 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
452 DefUnit, Line, 0, 0, 0,
453 llvm::DIType::FlagFwdDecl,
454 llvm::DIType(), llvm::DIArray());
455 }
456 return getOrCreateType(PointeeTy, Unit);
457
458}
459
Anders Carlssona031b352009-11-06 19:19:55 +0000460llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
461 const Type *Ty,
462 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000463 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000464 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000465
466 // Size is always the size of a pointer. We can't use getTypeSize here
467 // because that does not return the correct value for references.
468 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000469 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
470 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Devang Patelc69e1cf2010-09-30 19:05:55 +0000472 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
473 0, Size, Align, 0, 0,
474 CreatePointeeType(PointeeTy, Unit));
475
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000476}
477
Mike Stump9bc093c2009-05-14 02:03:51 +0000478llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000479 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000480 if (BlockLiteralGenericSet)
481 return BlockLiteralGeneric;
482
Mike Stump9bc093c2009-05-14 02:03:51 +0000483 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
484
485 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
486
487 llvm::DIType FieldTy;
488
489 QualType FType;
490 uint64_t FieldSize, FieldOffset;
491 unsigned FieldAlign;
492
493 llvm::DIArray Elements;
494 llvm::DIType EltTy, DescTy;
495
496 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000497 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000498 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
499 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000500
Daniel Dunbarca308df2009-05-26 19:40:20 +0000501 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000502 EltTys.clear();
503
Devang Patele2472482010-09-29 21:05:52 +0000504 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000505 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000506
Mike Stump9bc093c2009-05-14 02:03:51 +0000507 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000508 Unit, LineNo, FieldOffset, 0, 0,
509 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Mike Stump9bc093c2009-05-14 02:03:51 +0000511 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000512 uint64_t Size = CGM.getContext().getTypeSize(Ty);
513 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Mike Stump9bc093c2009-05-14 02:03:51 +0000515 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000516 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000517 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000518
519 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000520 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000521 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000522 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000523 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
524 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000525 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000526 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000527
Anders Carlsson20f12a22009-12-06 18:00:51 +0000528 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000529 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000530 FieldSize = CGM.getContext().getTypeSize(Ty);
531 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000532 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000533 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000534 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000535 FieldOffset, 0, FieldTy);
536 EltTys.push_back(FieldTy);
537
538 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000539 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000540
541 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000542 Unit, LineNo, FieldOffset, 0, 0,
543 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Mike Stump9bc093c2009-05-14 02:03:51 +0000545 BlockLiteralGenericSet = true;
546 BlockLiteralGeneric
547 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000548 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000549 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000550 return BlockLiteralGeneric;
551}
552
Chris Lattner9c85ba32008-11-10 06:08:34 +0000553llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000554 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000555 // Typedefs are derived from some other type. If we have a typedef of a
556 // typedef, make sure to emit the whole chain.
557 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner9c85ba32008-11-10 06:08:34 +0000559 // We don't set size information, but do specify where the typedef was
560 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000561 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000562
Devang Pateleb6d79b2010-02-01 21:34:11 +0000563 llvm::DIDescriptor TyContext
564 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
565 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000566 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000567 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000568 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000569 Ty->getDecl()->getName(), Unit,
570 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000571 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000572}
573
Chris Lattner9c85ba32008-11-10 06:08:34 +0000574llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000575 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000576 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000577
Chris Lattner9c85ba32008-11-10 06:08:34 +0000578 // Add the result type at least.
579 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner9c85ba32008-11-10 06:08:34 +0000581 // Set up remainder of arguments if there is a prototype.
582 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000583 if (isa<FunctionNoProtoType>(Ty))
584 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
585 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
587 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000588 }
589
Chris Lattner9c85ba32008-11-10 06:08:34 +0000590 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000591 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Devang Patelca80a5f2009-10-20 19:55:01 +0000593 llvm::DIType DbgTy =
594 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000595 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000596 0, 0, 0, 0, 0,
597 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000598 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000599}
600
Devang Patel428deb52010-01-19 00:00:59 +0000601/// CollectRecordFields - A helper function to collect debug info for
602/// record fields. This is used while creating debug info entry for a Record.
603void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000604CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000605 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
606 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000607 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
608 for (RecordDecl::field_iterator I = RD->field_begin(),
609 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000610 I != E; ++I, ++FieldNo) {
611 FieldDecl *Field = *I;
612 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000613 llvm::StringRef FieldName = Field->getName();
614
Devang Patel4835fdd2010-02-12 01:31:06 +0000615 // Ignore unnamed fields. Do not ignore unnamed records.
616 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000617 continue;
618
619 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000620 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
621 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000622 QualType FType = Field->getType();
623 uint64_t FieldSize = 0;
624 unsigned FieldAlign = 0;
625 if (!FType->isIncompleteArrayType()) {
626
627 // Bit size, align and offset of the type.
628 FieldSize = CGM.getContext().getTypeSize(FType);
629 Expr *BitWidth = Field->getBitWidth();
630 if (BitWidth)
631 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000632 FieldAlign = CGM.getContext().getTypeAlign(FType);
633 }
634
635 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
636
Devang Patel71f4ff62010-04-21 23:12:37 +0000637 unsigned Flags = 0;
638 AccessSpecifier Access = I->getAccess();
639 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000640 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000641 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000642 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000643
Devang Patel428deb52010-01-19 00:00:59 +0000644 // Create a DW_TAG_member node to remember the offset of this field in the
645 // struct. FIXME: This is an absolutely insane way to capture this
646 // information. When we gut debug info, this should be fixed.
647 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
648 FieldName, FieldDefUnit,
649 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000650 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000651 EltTys.push_back(FieldTy);
652 }
653}
654
Devang Patela6da1922010-01-28 00:28:01 +0000655/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
656/// function type is not updated to include implicit "this" pointer. Use this
657/// routine to get a method type which includes "this" pointer.
658llvm::DIType
659CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000660 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000661 llvm::DIType FnTy
662 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
663 0),
664 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000665
Devang Patela6da1922010-01-28 00:28:01 +0000666 // Add "this" pointer.
667
Devang Patelab699792010-05-07 18:12:35 +0000668 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000669 assert (Args.getNumElements() && "Invalid number of arguments!");
670
671 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
672
673 // First element is always return type. For 'void' functions it is NULL.
674 Elts.push_back(Args.getElement(0));
675
Devang Patel2ed8f002010-08-27 17:47:47 +0000676 if (!Method->isStatic())
677 {
678 // "this" pointer is always first argument.
679 ASTContext &Context = CGM.getContext();
680 QualType ThisPtr =
681 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
682 llvm::DIType ThisPtrType =
683 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000684
Devang Patel2ed8f002010-08-27 17:47:47 +0000685 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
686 Elts.push_back(ThisPtrType);
687 }
Devang Patela6da1922010-01-28 00:28:01 +0000688
689 // Copy rest of the arguments.
690 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
691 Elts.push_back(Args.getElement(i));
692
693 llvm::DIArray EltTypeArray =
694 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
695
696 return
697 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000698 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000699 0, 0, 0, 0, 0,
700 llvm::DIType(), EltTypeArray);
701}
702
Devang Patel58faf202010-10-22 17:11:50 +0000703/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
704/// inside a function.
705static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
706 if (const CXXRecordDecl *NRD =
707 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
708 return isFunctionLocalClass(NRD);
709 else if (isa<FunctionDecl>(RD->getDeclContext()))
710 return true;
711 return false;
712
713}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000714/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
715/// a single member function GlobalDecl.
716llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000717CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000718 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000719 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000720 bool IsCtorOrDtor =
721 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
722
723 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000724 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000725
726 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
727 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000728 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000729 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000730 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000731
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000732 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000733 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
734 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000735
736 // Collect virtual method info.
737 llvm::DIType ContainingType;
738 unsigned Virtuality = 0;
739 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000740
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000741 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000742 if (Method->isPure())
743 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
744 else
745 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
746
747 // It doesn't make sense to give a virtual destructor a vtable index,
748 // since a single destructor has two entries in the vtable.
749 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000750 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000751 ContainingType = RecordTy;
752 }
753
Devang Patele2472482010-09-29 21:05:52 +0000754 unsigned Flags = 0;
755 if (Method->isImplicit())
756 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000757 AccessSpecifier Access = Method->getAccess();
758 if (Access == clang::AS_private)
759 Flags |= llvm::DIDescriptor::FlagPrivate;
760 else if (Access == clang::AS_protected)
761 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000762 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
763 if (CXXC->isExplicit())
764 Flags |= llvm::DIDescriptor::FlagExplicit;
765 } else if (const CXXConversionDecl *CXXC =
766 dyn_cast<CXXConversionDecl>(Method)) {
767 if (CXXC->isExplicit())
768 Flags |= llvm::DIDescriptor::FlagExplicit;
769 }
Devang Patel3951e712010-10-07 22:03:49 +0000770 if (Method->hasPrototype())
771 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000772
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000773 llvm::DISubprogram SP =
774 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
775 MethodLinkageName,
776 MethodDefUnit, MethodLine,
777 MethodTy, /*isLocalToUnit=*/false,
Devang Patel33133572010-10-22 18:31:12 +0000778 /* isDefinition=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000779 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000780 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000781 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000782
783 // Don't cache ctors or dtors since we have to emit multiple functions for
784 // a single ctor or dtor.
785 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000786 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000787
788 return SP;
789}
790
Devang Patel4125fd22010-01-19 01:54:44 +0000791/// CollectCXXMemberFunctions - A helper function to collect debug info for
792/// C++ member functions.This is used while creating debug info entry for
793/// a Record.
794void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000795CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000796 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000797 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000798 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
799 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000800 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000801
Devang Pateld5322da2010-02-09 19:09:28 +0000802 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000803 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000804
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000805 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000806 }
807}
808
Devang Patel2ed8f002010-08-27 17:47:47 +0000809/// CollectCXXFriends - A helper function to collect debug info for
810/// C++ base classes. This is used while creating debug info entry for
811/// a Record.
812void CGDebugInfo::
813CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
814 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
815 llvm::DIType RecordTy) {
816
817 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
818 BE = RD->friend_end(); BI != BE; ++BI) {
819
820 TypeSourceInfo *TInfo = (*BI)->getFriendType();
821 if(TInfo)
822 {
823 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
824
825 llvm::DIType DTy =
826 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
827 RecordTy, llvm::StringRef(),
828 Unit, 0, 0, 0,
829 0, 0, Ty);
830
831 EltTys.push_back(DTy);
832 }
833
834 }
835}
836
Devang Patela245c5b2010-01-25 23:32:18 +0000837/// CollectCXXBases - A helper function to collect debug info for
838/// C++ base classes. This is used while creating debug info entry for
839/// a Record.
840void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000841CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000842 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000843 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000844
Devang Patel239cec62010-02-01 21:39:52 +0000845 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
846 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
847 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000848 unsigned BFlags = 0;
849 uint64_t BaseOffset;
850
851 const CXXRecordDecl *Base =
852 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
853
854 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000855 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000856 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000857 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000858 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000859 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000860 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000861
862 AccessSpecifier Access = BI->getAccessSpecifier();
863 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000864 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000865 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000866 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000867
868 llvm::DIType DTy =
869 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
870 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000871 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000872 BaseOffset, BFlags,
873 getOrCreateType(BI->getType(),
874 Unit));
875 EltTys.push_back(DTy);
876 }
Devang Patela245c5b2010-01-25 23:32:18 +0000877}
878
Devang Patel4ce3f202010-01-28 18:11:52 +0000879/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000880llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000881 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000882 return VTablePtrType;
883
884 ASTContext &Context = CGM.getContext();
885
886 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000887 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
888 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000889 llvm::DIType SubTy =
890 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000891 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000892 0, 0, 0, 0, 0, llvm::DIType(), SElements);
893
894 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
895 llvm::DIType vtbl_ptr_type
896 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000897 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000898 0, Size, 0, 0, 0, SubTy);
899
Devang Pateld58562e2010-03-09 22:49:11 +0000900 VTablePtrType =
901 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
902 Unit, "", Unit,
903 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000904 return VTablePtrType;
905}
906
Anders Carlsson046c2942010-04-17 20:15:18 +0000907/// getVTableName - Get vtable name for the given Class.
908llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000909 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000910 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000911
912 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000913 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000914 memcpy(StrPtr, Name.data(), Name.length());
915 return llvm::StringRef(StrPtr, Name.length());
916}
917
918
Anders Carlsson046c2942010-04-17 20:15:18 +0000919/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000920/// debug info entry in EltTys vector.
921void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000922CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000923 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000924 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000925
926 // If there is a primary base then it will hold vtable info.
927 if (RL.getPrimaryBase())
928 return;
929
930 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000931 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000932 return;
933
934 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
935 llvm::DIType VPTR
936 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000937 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000938 0, Size, 0, 0, 0,
939 getOrCreateVTablePtrType(Unit));
940 EltTys.push_back(VPTR);
941}
942
Devang Patelc69e1cf2010-09-30 19:05:55 +0000943/// getOrCreateRecordType - Emit record type's standalone debug info.
944llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
945 SourceLocation Loc) {
946 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
947 DebugFactory.RecordType(T);
948 return T;
949}
950
Devang Patel65e99f22009-02-25 01:36:11 +0000951/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000952llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000953 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000954 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner9c85ba32008-11-10 06:08:34 +0000956 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000957 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000958 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000959 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000960 Tag = llvm::dwarf::DW_TAG_union_type;
961 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000962 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000963 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000964 }
965
Chris Lattner9c85ba32008-11-10 06:08:34 +0000966 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000967 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
968 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattner9c85ba32008-11-10 06:08:34 +0000970 // Records and classes and unions can all be recursive. To handle them, we
971 // first generate a debug descriptor for the struct as a forward declaration.
972 // Then (if it is a definition) we go through and get debug info for all of
973 // its members. Finally, we create a descriptor for the complete type (which
974 // may refer to the forward decl if the struct is recursive) and replace all
975 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000976 llvm::DIDescriptor FDContext =
977 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
978
979 // If this is just a forward declaration, construct an appropriately
980 // marked node and just return it.
981 if (!RD->getDefinition()) {
982 llvm::DICompositeType FwdDecl =
983 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
984 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000985 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000986 llvm::DIType(), llvm::DIArray());
987
988 return FwdDecl;
989 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000990
Dan Gohman86968372010-08-20 22:39:57 +0000991 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Devang Patelab699792010-05-07 18:12:35 +0000993 llvm::MDNode *MN = FwdDecl;
994 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000995 // Otherwise, insert it into the TypeCache so that recursive uses will find
996 // it.
Devang Patelab699792010-05-07 18:12:35 +0000997 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000998 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000999 RegionStack.push_back(FwdDeclNode);
1000 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001001
1002 // Convert all the elements.
1003 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1004
Devang Pateld6c5a262010-02-01 21:52:22 +00001005 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001006 if (CXXDecl) {
1007 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +00001008 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001009 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001010
1011 // Collect static variables with initializers.
1012 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1013 I != E; ++I)
1014 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1015 if (const Expr *Init = V->getInit()) {
1016 Expr::EvalResult Result;
1017 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1018 llvm::ConstantInt *CI
1019 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1020
1021 // Create the descriptor for static variable.
1022 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1023 llvm::StringRef VName = V->getName();
1024 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1025 // Do not use DIGlobalVariable for enums.
1026 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1027 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1028 getLineNumber(V->getLocation()),
1029 VTy, true, true, CI);
1030 }
1031 }
1032 }
1033 }
1034
Devang Pateld6c5a262010-02-01 21:52:22 +00001035 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001036 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001037 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001038 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001039 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001040
1041 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001042 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001043 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1044 // Seek non virtual primary base root.
1045 while (1) {
1046 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1047 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001048 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001049 PBase = PBT;
1050 else
1051 break;
1052 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001053 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001054 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001055 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001056 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001057 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001058 }
Mike Stump1eb44332009-09-09 15:08:12 +00001059
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001061 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001062
1063 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001064 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1065 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Devang Patele4c1ea02010-03-11 20:01:48 +00001067 RegionStack.pop_back();
1068 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1069 RegionMap.find(Ty->getDecl());
1070 if (RI != RegionMap.end())
1071 RegionMap.erase(RI);
1072
Devang Patel411894b2010-02-01 22:40:08 +00001073 llvm::DIDescriptor RDContext =
1074 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001075
1076 llvm::StringRef RDName = RD->getName();
1077 // If this is a class, include the template arguments also.
1078 if (Tag == llvm::dwarf::DW_TAG_class_type)
1079 RDName = getClassName(RD);
1080
Devang Patel0ce73f62009-07-22 18:57:00 +00001081 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001082 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001083 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001084 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001085 llvm::DIType(), Elements,
1086 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001087
1088 // Now that we have a real decl for the struct, replace anything using the
1089 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001090 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001091 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001092 return RealDecl;
1093}
1094
John McCallc12c5bb2010-05-15 11:32:37 +00001095/// CreateType - get objective-c object type.
1096llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1097 llvm::DIFile Unit) {
1098 // Ignore protocols.
1099 return getOrCreateType(Ty->getBaseType(), Unit);
1100}
1101
Devang Patel9ca36b62009-02-26 21:10:26 +00001102/// CreateType - get objective-c interface type.
1103llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001104 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001105 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001106 if (!ID)
1107 return llvm::DIType();
1108
Devang Patel9ca36b62009-02-26 21:10:26 +00001109 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001110
1111 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001112 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001113 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001114 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001115
Dan Gohman45f7c782010-08-23 21:15:56 +00001116 // If this is just a forward declaration, return a special forward-declaration
1117 // debug type.
1118 if (ID->isForwardDecl()) {
1119 llvm::DICompositeType FwdDecl =
1120 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1121 DefUnit, Line, 0, 0, 0, 0,
1122 llvm::DIType(), llvm::DIArray(),
1123 RuntimeLang);
1124 return FwdDecl;
1125 }
1126
Devang Patel9ca36b62009-02-26 21:10:26 +00001127 // To handle recursive interface, we
1128 // first generate a debug descriptor for the struct as a forward declaration.
1129 // Then (if it is a definition) we go through and get debug info for all of
1130 // its members. Finally, we create a descriptor for the complete type (which
1131 // may refer to the forward decl if the struct is recursive) and replace all
1132 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001133 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Devang Patelab699792010-05-07 18:12:35 +00001135 llvm::MDNode *MN = FwdDecl;
1136 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001137 // Otherwise, insert it into the TypeCache so that recursive uses will find
1138 // it.
Devang Patelab699792010-05-07 18:12:35 +00001139 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001140 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001141 RegionStack.push_back(FwdDeclNode);
1142 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001143
1144 // Convert all the elements.
1145 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1146
Devang Pateld6c5a262010-02-01 21:52:22 +00001147 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001148 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001149 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001150 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001151 if (!SClassTy.isValid())
1152 return llvm::DIType();
1153
Mike Stump1eb44332009-09-09 15:08:12 +00001154 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001155 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001156 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001157 0 /* offset */, 0, SClassTy);
1158 EltTys.push_back(InhTag);
1159 }
1160
Devang Pateld6c5a262010-02-01 21:52:22 +00001161 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001162
1163 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001164 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001165 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001166 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001167 if (!FieldTy.isValid())
1168 return llvm::DIType();
1169
Devang Patel73621622009-11-25 17:37:31 +00001170 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001171
Devang Patelde135022009-04-27 22:40:36 +00001172 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001173 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001174 continue;
1175
Devang Patel9ca36b62009-02-26 21:10:26 +00001176 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001177 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1178 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001179 QualType FType = Field->getType();
1180 uint64_t FieldSize = 0;
1181 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001182
Devang Patel99c20eb2009-03-20 18:24:39 +00001183 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Devang Patel99c20eb2009-03-20 18:24:39 +00001185 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001186 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001187 Expr *BitWidth = Field->getBitWidth();
1188 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001189 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001190
Anders Carlsson20f12a22009-12-06 18:00:51 +00001191 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001192 }
1193
Mike Stump1eb44332009-09-09 15:08:12 +00001194 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1195
Devang Patelc20482b2009-03-19 00:23:53 +00001196 unsigned Flags = 0;
1197 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001198 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001199 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001200 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Devang Patel9ca36b62009-02-26 21:10:26 +00001202 // Create a DW_TAG_member node to remember the offset of this field in the
1203 // struct. FIXME: This is an absolutely insane way to capture this
1204 // information. When we gut debug info, this should be fixed.
1205 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1206 FieldName, FieldDefUnit,
1207 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001208 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001209 EltTys.push_back(FieldTy);
1210 }
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Devang Patel9ca36b62009-02-26 21:10:26 +00001212 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001213 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001214
Devang Patele4c1ea02010-03-11 20:01:48 +00001215 RegionStack.pop_back();
1216 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1217 RegionMap.find(Ty->getDecl());
1218 if (RI != RegionMap.end())
1219 RegionMap.erase(RI);
1220
Devang Patel9ca36b62009-02-26 21:10:26 +00001221 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001222 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1223 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Devang Patel6c1fddf2009-07-27 18:42:03 +00001225 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001226 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001227 Line, Size, Align, 0, 0, llvm::DIType(),
1228 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001229
1230 // Now that we have a real decl for the struct, replace anything using the
1231 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001232 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001233 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001234
Devang Patel9ca36b62009-02-26 21:10:26 +00001235 return RealDecl;
1236}
1237
Chris Lattner9c85ba32008-11-10 06:08:34 +00001238llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001239 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001240 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241
Chris Lattner9c85ba32008-11-10 06:08:34 +00001242}
1243
1244llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001245 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001246 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1247 return CreateType(RT, Unit);
1248 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1249 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001250
Chris Lattner9c85ba32008-11-10 06:08:34 +00001251 return llvm::DIType();
1252}
1253
Devang Patel70c23cd2010-02-23 22:59:39 +00001254llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001255 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001256 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1257 uint64_t NumElems = Ty->getNumElements();
1258 if (NumElems > 0)
1259 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001260
Benjamin Kramerad468862010-03-30 11:36:44 +00001261 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1262 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001263
1264 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1265 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1266
1267 return
1268 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001269 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001270 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001271 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001272}
1273
Chris Lattner9c85ba32008-11-10 06:08:34 +00001274llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001275 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001276 uint64_t Size;
1277 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001278
1279
Nuno Lopes010d5142009-01-28 00:35:17 +00001280 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001281 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001282 Size = 0;
1283 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001284 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001285 } else if (Ty->isIncompleteArrayType()) {
1286 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001287 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001288 } else {
1289 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001290 Size = CGM.getContext().getTypeSize(Ty);
1291 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001292 }
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Chris Lattner9c85ba32008-11-10 06:08:34 +00001294 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1295 // interior arrays, do we care? Why aren't nested arrays represented the
1296 // obvious/recursive way?
1297 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1298 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001299 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001300 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001301 else {
1302 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1303 uint64_t Upper = 0;
1304 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1305 if (CAT->getSize().getZExtValue())
1306 Upper = CAT->getSize().getZExtValue() - 1;
1307 // FIXME: Verify this is right for VLAs.
1308 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1309 EltTy = Ty->getElementType();
1310 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
Chris Lattner9c85ba32008-11-10 06:08:34 +00001313 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001314 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001315
Devang Patelca80a5f2009-10-20 19:55:01 +00001316 llvm::DIType DbgTy =
1317 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001318 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001319 0, Size, Align, 0, 0,
1320 getOrCreateType(EltTy, Unit),
1321 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001322 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001323}
1324
Anders Carlssona031b352009-11-06 19:19:55 +00001325llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001326 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001327 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1328 Ty, Ty->getPointeeType(), Unit);
1329}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001330
Anders Carlsson20f12a22009-12-06 18:00:51 +00001331llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001332 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001333 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1334 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1335
1336 if (!Ty->getPointeeType()->isFunctionType()) {
1337 // We have a data member pointer type.
1338 return PointerDiffDITy;
1339 }
1340
1341 // We have a member function pointer type. Treat it as a struct with two
1342 // ptrdiff_t members.
1343 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1344
1345 uint64_t FieldOffset = 0;
1346 llvm::DIDescriptor ElementTypes[2];
1347
1348 // FIXME: This should probably be a function type instead.
1349 ElementTypes[0] =
1350 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001351 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001352 Info.first, Info.second, FieldOffset, 0,
1353 PointerDiffDITy);
1354 FieldOffset += Info.first;
1355
1356 ElementTypes[1] =
1357 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001358 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001359 Info.first, Info.second, FieldOffset, 0,
1360 PointerDiffDITy);
1361
1362 llvm::DIArray Elements =
1363 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1364 llvm::array_lengthof(ElementTypes));
1365
1366 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1367 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001368 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001369 0, 0, 0, llvm::DIType(), Elements);
1370}
1371
Devang Patel6237cea2010-08-23 22:07:25 +00001372/// CreateEnumType - get enumeration type.
1373llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1374 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1375
1376 // Create DIEnumerator elements for each enumerator.
1377 for (EnumDecl::enumerator_iterator
1378 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1379 Enum != EnumEnd; ++Enum) {
1380 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1381 Enum->getInitVal().getZExtValue()));
1382 }
1383
1384 // Return a CompositeType for the enum itself.
1385 llvm::DIArray EltArray =
1386 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1387
1388 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1389 unsigned Line = getLineNumber(ED->getLocation());
1390 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001391 uint64_t Align = 0;
1392 if (!ED->getTypeForDecl()->isIncompleteType()) {
1393 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1394 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1395 }
Devang Patel4bc48872010-10-27 23:23:58 +00001396 llvm::DIDescriptor EnumContext =
1397 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001398 llvm::DIType DbgTy =
1399 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel4bc48872010-10-27 23:23:58 +00001400 EnumContext, ED->getName(),
1401 DefUnit, Line, Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001402 llvm::DIType(), EltArray);
1403 return DbgTy;
1404}
1405
Douglas Gregor840943d2009-12-21 20:18:30 +00001406static QualType UnwrapTypeForDebugInfo(QualType T) {
1407 do {
1408 QualType LastT = T;
1409 switch (T->getTypeClass()) {
1410 default:
1411 return T;
1412 case Type::TemplateSpecialization:
1413 T = cast<TemplateSpecializationType>(T)->desugar();
1414 break;
1415 case Type::TypeOfExpr: {
1416 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1417 T = Ty->getUnderlyingExpr()->getType();
1418 break;
1419 }
1420 case Type::TypeOf:
1421 T = cast<TypeOfType>(T)->getUnderlyingType();
1422 break;
1423 case Type::Decltype:
1424 T = cast<DecltypeType>(T)->getUnderlyingType();
1425 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001426 case Type::Elaborated:
1427 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001428 break;
1429 case Type::SubstTemplateTypeParm:
1430 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1431 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001432 }
1433
1434 assert(T != LastT && "Type unwrapping failed to unwrap!");
1435 if (T == LastT)
1436 return T;
1437 } while (true);
1438
1439 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001440}
1441
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001442/// getOrCreateType - Get the type from the cache or create a new
1443/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001444llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001445 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001446 if (Ty.isNull())
1447 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Douglas Gregor840943d2009-12-21 20:18:30 +00001449 // Unwrap the type as needed for debug information.
1450 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001451
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001452 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001453 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001454 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001455 if (it != TypeCache.end()) {
1456 // Verify that the debug info still exists.
1457 if (&*it->second)
1458 return llvm::DIType(cast<llvm::MDNode>(it->second));
1459 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001460
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001461 // Otherwise create the type.
1462 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001463
1464 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001465 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001466 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001467}
1468
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001469/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001470llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001471 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001472 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001473 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001474 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001475
Douglas Gregor2101a822009-12-21 19:57:21 +00001476 const char *Diag = 0;
1477
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001478 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001479 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001480#define TYPE(Class, Base)
1481#define ABSTRACT_TYPE(Class, Base)
1482#define NON_CANONICAL_TYPE(Class, Base)
1483#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1484#include "clang/AST/TypeNodes.def"
1485 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001486
Anders Carlssonbfe69952009-11-06 18:24:04 +00001487 // FIXME: Handle these.
1488 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001489 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001490
1491 case Type::Vector:
1492 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001493 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001494 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001495 case Type::ObjCObject:
1496 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001497 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001498 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001499 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001500 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1501 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001502 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001503 return CreateType(cast<BlockPointerType>(Ty), Unit);
1504 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001505 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001506 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001507 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001508 case Type::FunctionProto:
1509 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001510 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001511 case Type::ConstantArray:
1512 case Type::VariableArray:
1513 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001514 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001515
1516 case Type::LValueReference:
1517 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1518
Anders Carlsson20f12a22009-12-06 18:00:51 +00001519 case Type::MemberPointer:
1520 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001521
1522 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001523 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001524 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001525 case Type::TypeOfExpr:
1526 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001527 case Type::Decltype:
1528 llvm_unreachable("type should have been unwrapped!");
1529 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001530
1531 case Type::RValueReference:
1532 // FIXME: Implement!
1533 Diag = "rvalue references";
1534 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001535 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001536
1537 assert(Diag && "Fall through without a diagnostic?");
1538 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1539 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001540 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001541 << Diag;
1542 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001543}
1544
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001545/// CreateMemberType - Create new member and increase Offset by FType's size.
1546llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1547 llvm::StringRef Name,
1548 uint64_t *Offset) {
1549 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1550 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1551 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1552 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1553 Unit, Name, Unit, 0,
1554 FieldSize, FieldAlign,
1555 *Offset, 0, FieldTy);
1556 *Offset += FieldSize;
1557 return Ty;
1558}
1559
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001560/// EmitFunctionStart - Constructs the debug code for entering a function -
1561/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001562void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001563 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001564 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Devang Patel9c6c3a02010-01-14 00:36:21 +00001566 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001567 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001568
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001569 FnBeginRegionCount.push_back(RegionStack.size());
1570
Devang Patel9c6c3a02010-01-14 00:36:21 +00001571 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001572 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001573 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1574 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001575 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001576 // If there is a DISubprogram for this function available then use it.
1577 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1578 FI = SPCache.find(FD);
1579 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001580 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001581 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1582 llvm::MDNode *SPN = SP;
1583 RegionStack.push_back(SPN);
1584 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001585 return;
1586 }
1587 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001588 Name = getFunctionName(FD);
1589 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001590 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001591 if (LinkageName == Name)
1592 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001593 if (FD->hasPrototype())
1594 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001595 if (const NamespaceDecl *NSDecl =
1596 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1597 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001598 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001599 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001600 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001601 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001602 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001603 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001604 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001605 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001606 if (!Name.empty() && Name[0] == '\01')
1607 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001608
Devang Patel970c6182010-04-24 00:49:16 +00001609 // It is expected that CurLoc is set before using EmitFunctionStart.
1610 // Usually, CurLoc points to the left bracket location of compound
1611 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001612 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001613 if (D->isImplicit())
1614 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001615 llvm::DISubprogram SP =
Devang Patela2e57692010-10-28 17:27:32 +00001616 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit,
1617 LineNo, getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001618 Fn->hasInternalLinkage(), true/*definition*/,
1619 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001620 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001621 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001623 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001624 llvm::MDNode *SPN = SP;
1625 RegionStack.push_back(SPN);
1626 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001627
1628 // Clear stack used to keep track of #line directives.
1629 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001630}
1631
1632
Devang Patel4d939e62010-07-20 22:20:10 +00001633void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001634 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001636 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001637 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001638 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001639 || (SM.getInstantiationLineNumber(CurLoc) ==
1640 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001641 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001642 // New Builder may not be in sync with CGDebugInfo.
1643 if (!Builder.getCurrentDebugLocation().isUnknown())
1644 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001645
1646 // Update last state.
1647 PrevLoc = CurLoc;
1648
Chris Lattnerc6034632010-04-01 06:31:43 +00001649 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001650 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1651 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001652 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001653}
1654
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001655/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1656/// has introduced scope change.
1657void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1658 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1659 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1660 return;
1661 SourceManager &SM = CGM.getContext().getSourceManager();
1662 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1663 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1664
Douglas Gregor8c457a82010-11-11 20:45:16 +00001665 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1666 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001667 return;
1668
1669 // If #line directive stack is empty then we are entering a new scope.
1670 if (LineDirectiveFiles.empty()) {
1671 EmitRegionStart(Builder);
1672 LineDirectiveFiles.push_back(PCLoc.getFilename());
1673 return;
1674 }
1675
1676 assert (RegionStack.size() >= LineDirectiveFiles.size()
1677 && "error handling #line regions!");
1678
1679 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001680 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001681 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1682 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001683 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001684 SeenThisFile = true;
1685 break;
1686 }
1687
1688 // If #line for this file is seen earlier then pop out #line regions.
1689 if (SeenThisFile) {
1690 while (!LineDirectiveFiles.empty()) {
1691 const char *LastFile = LineDirectiveFiles.back();
1692 RegionStack.pop_back();
1693 LineDirectiveFiles.pop_back();
1694 if (!strcmp(PPLoc.getFilename(), LastFile))
1695 break;
1696 }
1697 return;
1698 }
1699
1700 // .. otherwise insert new #line region.
1701 EmitRegionStart(Builder);
1702 LineDirectiveFiles.push_back(PCLoc.getFilename());
1703
1704 return;
1705}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001706/// EmitRegionStart- Constructs the debug code for entering a declarative
1707/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001708void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001709 llvm::DIDescriptor D =
1710 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1711 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001712 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001713 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001714 getLineNumber(CurLoc),
1715 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001716 llvm::MDNode *DN = D;
1717 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001718}
1719
1720/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1721/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001722void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001723 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1724
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001725 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001726 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001728 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001729}
1730
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001731/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1732void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1733 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1734 unsigned RCount = FnBeginRegionCount.back();
1735 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1736
1737 // Pop all regions for this function.
1738 while (RegionStack.size() != RCount)
1739 EmitRegionEnd(Builder);
1740 FnBeginRegionCount.pop_back();
1741}
1742
Devang Patel809b9bb2010-02-10 18:49:08 +00001743// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1744// See BuildByRefType.
1745llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1746 uint64_t *XOffset) {
1747
1748 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1749
1750 QualType FType;
1751 uint64_t FieldSize, FieldOffset;
1752 unsigned FieldAlign;
1753
Devang Patel17800552010-03-09 00:44:50 +00001754 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001755 QualType Type = VD->getType();
1756
1757 FieldOffset = 0;
1758 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001759 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1760 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001761 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001762 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1763 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1764
Devang Patel809b9bb2010-02-10 18:49:08 +00001765 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1766 if (HasCopyAndDispose) {
1767 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001768 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1769 &FieldOffset));
1770 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1771 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001772 }
1773
1774 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1775 if (Align > CharUnits::fromQuantity(
1776 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1777 unsigned AlignedOffsetInBytes
1778 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1779 unsigned NumPaddingBytes
1780 = AlignedOffsetInBytes - FieldOffset/8;
1781
1782 if (NumPaddingBytes > 0) {
1783 llvm::APInt pad(32, NumPaddingBytes);
1784 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1785 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001786 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001787 }
1788 }
1789
1790 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001791 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001792 FieldSize = CGM.getContext().getTypeSize(FType);
1793 FieldAlign = Align.getQuantity()*8;
1794
1795 *XOffset = FieldOffset;
1796 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001797 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001798 0, FieldSize, FieldAlign,
1799 FieldOffset, 0, FieldTy);
1800 EltTys.push_back(FieldTy);
1801 FieldOffset += FieldSize;
1802
1803 llvm::DIArray Elements =
1804 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1805
Devang Patele2472482010-09-29 21:05:52 +00001806 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001807
1808 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001809 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001810 0, FieldOffset, 0, 0, Flags,
1811 llvm::DIType(), Elements);
1812
1813}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001814/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001815void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001816 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001817 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1818
Devang Patel17800552010-03-09 00:44:50 +00001819 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001820 llvm::DIType Ty;
1821 uint64_t XOffset = 0;
1822 if (VD->hasAttr<BlocksAttr>())
1823 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1824 else
1825 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001826
Devang Patelf4e54a22010-05-07 23:05:55 +00001827 // If there is not any debug info for type then do not emit debug info
1828 // for this variable.
1829 if (!Ty)
1830 return;
1831
Chris Lattner9c85ba32008-11-10 06:08:34 +00001832 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001833 unsigned Line = getLineNumber(VD->getLocation());
1834 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001835 unsigned Flags = 0;
1836 if (VD->isImplicit())
1837 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001838 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001839
1840 llvm::StringRef Name = VD->getName();
1841 if (!Name.empty()) {
1842 // Create the descriptor for the variable.
1843 llvm::DIVariable D =
1844 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1845 Name, Unit, Line, Ty,
1846 CGM.getLangOptions().Optimize, Flags);
1847
1848 // Insert an llvm.dbg.declare into the current block.
1849 llvm::Instruction *Call =
1850 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1851
1852 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001853 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001854 }
1855
1856 // If VD is an anonymous union then Storage represents value for
1857 // all union fields.
1858 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1859 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1860 if (RD->isUnion()) {
1861 for (RecordDecl::field_iterator I = RD->field_begin(),
1862 E = RD->field_end();
1863 I != E; ++I) {
1864 FieldDecl *Field = *I;
1865 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1866 llvm::StringRef FieldName = Field->getName();
1867
1868 // Ignore unnamed fields. Do not ignore unnamed records.
1869 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1870 continue;
1871
1872 // Use VarDecl's Tag, Scope and Line number.
1873 llvm::DIVariable D =
1874 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1875 FieldName, Unit, Line, FieldTy,
1876 CGM.getLangOptions().Optimize, Flags);
1877
1878 // Insert an llvm.dbg.declare into the current block.
1879 llvm::Instruction *Call =
1880 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1881
1882 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1883 }
1884 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001885}
1886
Mike Stumpb1a6e682009-09-30 02:43:10 +00001887/// EmitDeclare - Emit local variable declaration debug info.
1888void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1889 llvm::Value *Storage, CGBuilderTy &Builder,
1890 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001891 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001892 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1893
Devang Patel2b594b92010-04-26 23:28:46 +00001894 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001895 return;
1896
1897 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001898 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001899 llvm::DIType Ty;
1900 if (VD->hasAttr<BlocksAttr>())
1901 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1902 else
1903 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001904
1905 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001906 unsigned Line = getLineNumber(VD->getLocation());
1907 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001908
Devang Pateld6c5a262010-02-01 21:52:22 +00001909 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001910 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001911 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1912 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1913 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1914 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001915 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001916 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1917 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001918 // offset of __forwarding field
1919 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001920 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1921 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1922 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001923 // offset of x field
1924 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001925 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001926 }
1927
1928 // Create the descriptor for the variable.
1929 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001930 DebugFactory.CreateComplexVariable(Tag,
1931 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001932 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001933 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001934 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001935 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001936 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001937
Chris Lattnerc6034632010-04-01 06:31:43 +00001938 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001939 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001940}
1941
Devang Pateld6c5a262010-02-01 21:52:22 +00001942void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001943 llvm::Value *Storage,
1944 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001945 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001946}
1947
Mike Stumpb1a6e682009-09-30 02:43:10 +00001948void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1949 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1950 CodeGenFunction *CGF) {
1951 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1952}
1953
Chris Lattner9c85ba32008-11-10 06:08:34 +00001954/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1955/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001956void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001957 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001958 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001959}
1960
1961
1962
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001963/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001964void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001965 const VarDecl *D) {
1966
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001967 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001968 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001969 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001970
Devang Pateleb6d79b2010-02-01 21:34:11 +00001971 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001972 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001974 // CodeGen turns int[] into int[1] so we'll do the same here.
1975 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001977 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001978 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Anders Carlsson20f12a22009-12-06 18:00:51 +00001980 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001981 ArrayType::Normal, 0);
1982 }
Devang Patel5d822f02010-04-29 17:48:37 +00001983 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001984 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001985 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001986 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001987 if (LinkageName == DeclName)
1988 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001989 llvm::DIDescriptor DContext =
1990 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001991 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1992 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001993 Var->hasInternalLinkage(),
1994 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001995}
1996
Devang Patel9ca36b62009-02-26 21:10:26 +00001997/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001998void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001999 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00002000 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002001 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002002 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002003
Devang Pateld6c5a262010-02-01 21:52:22 +00002004 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002005
Devang Pateld6c5a262010-02-01 21:52:22 +00002006 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002007 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Devang Patel9ca36b62009-02-26 21:10:26 +00002009 // CodeGen turns int[] into int[1] so we'll do the same here.
2010 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Devang Patel9ca36b62009-02-26 21:10:26 +00002012 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002013 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Anders Carlsson20f12a22009-12-06 18:00:51 +00002015 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002016 ArrayType::Normal, 0);
2017 }
2018
Devang Patelf6a39b72009-10-20 18:26:30 +00002019 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00002020 getOrCreateType(T, Unit),
2021 Var->hasInternalLinkage(),
2022 true/*definition*/, Var);
2023}
Devang Patelabb485f2010-02-01 19:16:32 +00002024
Devang Patel25c2c8f2010-08-10 17:53:33 +00002025/// EmitGlobalVariable - Emit global variable's debug info.
2026void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002027 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002028 // Create the descriptor for the variable.
2029 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2030 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002031 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002032 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2033 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
2034 Ty = CreateEnumType(ED, Unit);
2035 }
Devang Patel0317ab02010-08-10 18:27:15 +00002036 // Do not use DIGlobalVariable for enums.
2037 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2038 return;
Devang Patel8d308382010-08-10 07:24:25 +00002039 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
2040 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00002041 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002042}
2043
Devang Patelabb485f2010-02-01 19:16:32 +00002044/// getOrCreateNamesSpace - Return namespace descriptor for the given
2045/// namespace decl.
2046llvm::DINameSpace
2047CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2048 llvm::DIDescriptor Unit) {
2049 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2050 NameSpaceCache.find(NSDecl);
2051 if (I != NameSpaceCache.end())
2052 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2053
Devang Patel8ab870d2010-05-12 23:46:38 +00002054 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002055 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002056 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002057 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002058 llvm::DINameSpace NS =
Devang Patel8c376682010-10-28 19:12:46 +00002059 DebugFactory.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002060 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002061 return NS;
2062}