blob: 8f4266bab1c8a8658ce6d115baeb45f17d4db698 [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 Patel823d8e92010-12-08 22:42:58 +000041 : CGM(CGM), DBuilder(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) {
Devang Patel823d8e92010-12-08 22:42:58 +0000156 if (!Loc.isValid())
157 // If Location is not valid then use main input file.
158 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
159
Anders Carlsson20f12a22009-12-06 18:00:51 +0000160 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000161 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000162
Douglas Gregor8c457a82010-11-11 20:45:16 +0000163 if (PLoc.isInvalid())
164 // If the location is not valid then use main input file.
Devang Patel823d8e92010-12-08 22:42:58 +0000165 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000166
Ted Kremenek9c250392010-03-30 00:27:51 +0000167 // Cache the results.
168 const char *fname = PLoc.getFilename();
169 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
170 DIFileCache.find(fname);
171
172 if (it != DIFileCache.end()) {
173 // Verify that the information still exists.
174 if (&*it->second)
175 return llvm::DIFile(cast<llvm::MDNode>(it->second));
176 }
177
Devang Patel823d8e92010-12-08 22:42:58 +0000178 llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000179
Devang Patelab699792010-05-07 18:12:35 +0000180 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000181 return F;
182
Devang Patel17800552010-03-09 00:44:50 +0000183}
Devang Patel8ab870d2010-05-12 23:46:38 +0000184
Devang Patel532105f2010-10-28 22:03:20 +0000185/// getOrCreateMainFile - Get the file info for main compile unit.
186llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel823d8e92010-12-08 22:42:58 +0000187 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000188}
189
Devang Patel8ab870d2010-05-12 23:46:38 +0000190/// getLineNumber - Get line number for the location. If location is invalid
191/// then use current location.
192unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
193 assert (CurLoc.isValid() && "Invalid current location!");
194 SourceManager &SM = CGM.getContext().getSourceManager();
195 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000196 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000197}
198
199/// getColumnNumber - Get column number for the location. If location is
200/// invalid then use current location.
201unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
202 assert (CurLoc.isValid() && "Invalid current location!");
203 SourceManager &SM = CGM.getContext().getSourceManager();
204 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000205 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000206}
207
Devang Patelac4d13c2010-07-27 15:17:16 +0000208llvm::StringRef CGDebugInfo::getCurrentDirname() {
209 if (!CWDName.empty())
210 return CWDName;
211 char *CompDirnamePtr = NULL;
212 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
213 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
214 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
215 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
216}
217
Devang Patel17800552010-03-09 00:44:50 +0000218/// CreateCompileUnit - Create new compile unit.
219void CGDebugInfo::CreateCompileUnit() {
220
221 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000222 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000223 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
224 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000225 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000226
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000227 // The main file name provided via the "-main-file-name" option contains just
228 // the file name itself with no path information. This file name may have had
229 // a relative path, so we look into the actual file entry for the main
230 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000231 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000232 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000233 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000234 if (MainFileDir != ".")
235 MainFileName = MainFileDir + "/" + MainFileName;
236 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000237
Devang Patelac4d13c2010-07-27 15:17:16 +0000238 // Save filename string.
239 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
240 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
241 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
242
Chris Lattner515455a2009-03-25 03:28:08 +0000243 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000244 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000245 if (LO.CPlusPlus) {
246 if (LO.ObjC1)
247 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
248 else
249 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
250 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000251 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000252 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000253 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000254 } else {
255 LangTag = llvm::dwarf::DW_LANG_C89;
256 }
Devang Patel446c6192009-04-17 21:06:59 +0000257
Daniel Dunbar19f19832010-08-24 17:41:09 +0000258 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000259
260 // Figure out which version of the ObjC runtime we have.
261 unsigned RuntimeVers = 0;
262 if (LO.ObjC1)
263 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000265 // Create new compile unit.
Devang Patel823d8e92010-12-08 22:42:58 +0000266 DBuilder.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000267 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000268 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000269 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000270 // FIXME - Eliminate TheCU.
271 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000272}
273
Devang Patel65e99f22009-02-25 01:36:11 +0000274/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000276llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000277 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000278 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000279 switch (BT->getKind()) {
280 default:
281 case BuiltinType::Void:
282 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000283 case BuiltinType::ObjCClass:
Devang Patel823d8e92010-12-08 22:42:58 +0000284 return DBuilder.CreateStructType(TheCU, "objc_class",
285 getOrCreateMainFile(), 0, 0, 0,
286 llvm::DIDescriptor::FlagFwdDecl,
287 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000288 case BuiltinType::ObjCId: {
289 // typedef struct objc_class *Class;
290 // typedef struct objc_object {
291 // Class isa;
292 // } *id;
293
294 llvm::DIType OCTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000295 DBuilder.CreateStructType(TheCU, "objc_class",
296 getOrCreateMainFile(), 0, 0, 0,
297 llvm::DIDescriptor::FlagFwdDecl,
298 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000299 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
300
Devang Patel823d8e92010-12-08 22:42:58 +0000301 llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000302
Devang Patel823d8e92010-12-08 22:42:58 +0000303 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000304 llvm::DIType FieldTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000305 DBuilder.CreateMemberType("isa", getOrCreateMainFile(),
306 0,Size, 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000307 EltTys.push_back(FieldTy);
308 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +0000309 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelc8972c62010-07-28 01:33:15 +0000310
Devang Patel823d8e92010-12-08 22:42:58 +0000311 return DBuilder.CreateStructType(TheCU, "objc_object",
312 getOrCreateMainFile(),
313 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000314 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000315 case BuiltinType::UChar:
316 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
317 case BuiltinType::Char_S:
318 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
319 case BuiltinType::UShort:
320 case BuiltinType::UInt:
321 case BuiltinType::ULong:
322 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
323 case BuiltinType::Short:
324 case BuiltinType::Int:
325 case BuiltinType::Long:
326 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
327 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
328 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000329 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000330 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
Devang Patel05127ca2010-07-28 23:23:29 +0000332
333 switch (BT->getKind()) {
334 case BuiltinType::Long: BTName = "long int"; break;
335 case BuiltinType::LongLong: BTName = "long long int"; break;
336 case BuiltinType::ULong: BTName = "long unsigned int"; break;
337 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
338 default:
339 BTName = BT->getName(CGM.getContext().getLangOptions());
340 break;
341 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000342 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000343 uint64_t Size = CGM.getContext().getTypeSize(BT);
344 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000345 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000346 DBuilder.CreateBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000347 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000349
Devang Patel344ff5d2010-12-09 00:25:29 +0000350llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000351 // Bit size, align and offset of the type.
352 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
353 if (Ty->isComplexIntegerType())
354 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 uint64_t Size = CGM.getContext().getTypeSize(Ty);
357 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000358 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000359 DBuilder.CreateBasicType("complex", Size, Align, Encoding);
360
Devang Patelca80a5f2009-10-20 19:55:01 +0000361 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000362}
363
John McCalla1805292009-09-25 01:40:47 +0000364/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000365/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000366llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000367 QualifierCollector Qc;
368 const Type *T = Qc.strip(Ty);
369
370 // Ignore these qualifiers for now.
371 Qc.removeObjCGCAttr();
372 Qc.removeAddressSpace();
373
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374 // We will create one Derived type for one qualifier and recurse to handle any
375 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000377 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000379 Qc.removeConst();
380 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000382 Qc.removeVolatile();
383 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000385 Qc.removeRestrict();
386 } else {
387 assert(Qc.empty() && "Unknown type qualifier for debug info");
388 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
John McCalla1805292009-09-25 01:40:47 +0000391 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
392
Daniel Dunbar3845f862008-10-31 03:54:29 +0000393 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
394 // CVR derived types.
Devang Patel823d8e92010-12-08 22:42:58 +0000395 llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy);
396
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398}
399
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000400llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000401 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000402 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000403 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
404 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000406}
407
Chris Lattner9c85ba32008-11-10 06:08:34 +0000408llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000409 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000410 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
411 Ty->getPointeeType(), Unit);
412}
413
Devang Patelc69e1cf2010-09-30 19:05:55 +0000414/// CreatePointeeType - Create PointTee type. If Pointee is a record
415/// then emit record's fwd if debug info size reduction is enabled.
416llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
417 llvm::DIFile Unit) {
418 if (!CGM.getCodeGenOpts().LimitDebugInfo)
419 return getOrCreateType(PointeeTy, Unit);
420
421 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
422 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000423 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
424 unsigned Line = getLineNumber(RD->getLocation());
425 llvm::DIDescriptor FDContext =
426 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000427
428 if (RD->isStruct())
429 return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit,
430 Line, 0, 0, llvm::DIType::FlagFwdDecl,
431 llvm::DIArray());
432 else if (RD->isUnion())
433 return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit,
434 Line, 0, 0, llvm::DIType::FlagFwdDecl,
435 llvm::DIArray());
436 else {
437 assert(RD->isClass() && "Unknown RecordType!");
438 return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit,
439 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
440 llvm::DIType(), llvm::DIArray());
441 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000442 }
443 return getOrCreateType(PointeeTy, Unit);
444
445}
446
Anders Carlssona031b352009-11-06 19:19:55 +0000447llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
448 const Type *Ty,
449 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000450 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000451
452 if (Tag == llvm::dwarf::DW_TAG_reference_type)
453 return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit));
454
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000455 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000456 // Size is always the size of a pointer. We can't use getTypeSize here
457 // because that does not return the correct value for references.
458 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000459 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
460 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Devang Patel823d8e92010-12-08 22:42:58 +0000462 return
463 DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000464}
465
Mike Stump9bc093c2009-05-14 02:03:51 +0000466llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000467 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000468 if (BlockLiteralGenericSet)
469 return BlockLiteralGeneric;
470
Devang Patel823d8e92010-12-08 22:42:58 +0000471 llvm::SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000473 QualType FType;
474 uint64_t FieldSize, FieldOffset;
475 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000476 llvm::DIArray Elements;
477 llvm::DIType EltTy, DescTy;
478
479 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000480 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000481 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
482 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000483
Devang Patel823d8e92010-12-08 22:42:58 +0000484 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000485 EltTys.clear();
486
Devang Patele2472482010-09-29 21:05:52 +0000487 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000488 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000489
Devang Patel823d8e92010-12-08 22:42:58 +0000490 EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor",
491 Unit, LineNo, FieldOffset, 0,
492 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Mike Stump9bc093c2009-05-14 02:03:51 +0000494 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000495 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Devang Patel823d8e92010-12-08 22:42:58 +0000497 DescTy = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000498
499 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000500 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000501 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000502 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000503 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
504 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000505 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000506 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000507
Anders Carlsson20f12a22009-12-06 18:00:51 +0000508 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000509 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000510 FieldSize = CGM.getContext().getTypeSize(Ty);
511 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel823d8e92010-12-08 22:42:58 +0000512 FieldTy = DBuilder.CreateMemberType("__descriptor", Unit,
513 LineNo, FieldSize, FieldAlign,
514 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000515 EltTys.push_back(FieldTy);
516
517 FieldOffset += FieldSize;
Devang Patel823d8e92010-12-08 22:42:58 +0000518 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000519
Devang Patel823d8e92010-12-08 22:42:58 +0000520 EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic",
521 Unit, LineNo, FieldOffset, 0,
522 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Mike Stump9bc093c2009-05-14 02:03:51 +0000524 BlockLiteralGenericSet = true;
Devang Patel823d8e92010-12-08 22:42:58 +0000525 BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000526 return BlockLiteralGeneric;
527}
528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000530 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531 // Typedefs are derived from some other type. If we have a typedef of a
532 // typedef, make sure to emit the whole chain.
533 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000534 if (!Src.Verify())
535 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536 // We don't set size information, but do specify where the typedef was
537 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000538 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +0000539 llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(),
540 Unit, Line);
Devang Patelca80a5f2009-10-20 19:55:01 +0000541 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000542}
543
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000545 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000546 llvm::SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // Add the result type at least.
549 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551 // Set up remainder of arguments if there is a prototype.
552 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000553 if (isa<FunctionNoProtoType>(Ty))
Devang Patel823d8e92010-12-08 22:42:58 +0000554 EltTys.push_back(DBuilder.CreateUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000555 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
557 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000558 }
559
Chris Lattner9c85ba32008-11-10 06:08:34 +0000560 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000561 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Devang Patel823d8e92010-12-08 22:42:58 +0000563 llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000564 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000565}
566
Devang Patel428deb52010-01-19 00:00:59 +0000567/// CollectRecordFields - A helper function to collect debug info for
568/// record fields. This is used while creating debug info entry for a Record.
569void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000570CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000571 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel428deb52010-01-19 00:00:59 +0000572 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000573 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
574 for (RecordDecl::field_iterator I = RD->field_begin(),
575 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000576 I != E; ++I, ++FieldNo) {
577 FieldDecl *Field = *I;
578 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000579 llvm::StringRef FieldName = Field->getName();
580
Devang Patel4835fdd2010-02-12 01:31:06 +0000581 // Ignore unnamed fields. Do not ignore unnamed records.
582 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000583 continue;
584
585 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000586 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
587 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000588 QualType FType = Field->getType();
589 uint64_t FieldSize = 0;
590 unsigned FieldAlign = 0;
591 if (!FType->isIncompleteArrayType()) {
592
593 // Bit size, align and offset of the type.
594 FieldSize = CGM.getContext().getTypeSize(FType);
595 Expr *BitWidth = Field->getBitWidth();
596 if (BitWidth)
597 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000598 FieldAlign = CGM.getContext().getTypeAlign(FType);
599 }
600
601 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
602
Devang Patel71f4ff62010-04-21 23:12:37 +0000603 unsigned Flags = 0;
604 AccessSpecifier Access = I->getAccess();
605 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000606 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000607 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000608 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000609
Devang Patel823d8e92010-12-08 22:42:58 +0000610 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
611 FieldLine, FieldSize, FieldAlign,
612 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000613 EltTys.push_back(FieldTy);
614 }
615}
616
Devang Patela6da1922010-01-28 00:28:01 +0000617/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
618/// function type is not updated to include implicit "this" pointer. Use this
619/// routine to get a method type which includes "this" pointer.
620llvm::DIType
621CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000622 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000623 llvm::DIType FnTy
624 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
625 0),
626 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000627
Devang Patela6da1922010-01-28 00:28:01 +0000628 // Add "this" pointer.
629
Devang Patelab699792010-05-07 18:12:35 +0000630 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000631 assert (Args.getNumElements() && "Invalid number of arguments!");
632
Devang Patel823d8e92010-12-08 22:42:58 +0000633 llvm::SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000634
635 // First element is always return type. For 'void' functions it is NULL.
636 Elts.push_back(Args.getElement(0));
637
Devang Patel2ed8f002010-08-27 17:47:47 +0000638 if (!Method->isStatic())
639 {
640 // "this" pointer is always first argument.
641 ASTContext &Context = CGM.getContext();
642 QualType ThisPtr =
643 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
644 llvm::DIType ThisPtrType =
Devang Patel823d8e92010-12-08 22:42:58 +0000645 DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000646
Devang Patel2ed8f002010-08-27 17:47:47 +0000647 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
648 Elts.push_back(ThisPtrType);
649 }
Devang Patela6da1922010-01-28 00:28:01 +0000650
651 // Copy rest of the arguments.
652 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
653 Elts.push_back(Args.getElement(i));
654
655 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000656 DBuilder.GetOrCreateArray(Elts.data(), Elts.size());
Devang Patela6da1922010-01-28 00:28:01 +0000657
Devang Patel823d8e92010-12-08 22:42:58 +0000658 return DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000659}
660
Devang Patel58faf202010-10-22 17:11:50 +0000661/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
662/// inside a function.
663static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
664 if (const CXXRecordDecl *NRD =
665 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
666 return isFunctionLocalClass(NRD);
667 else if (isa<FunctionDecl>(RD->getDeclContext()))
668 return true;
669 return false;
670
671}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000672/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
673/// a single member function GlobalDecl.
674llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000675CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000676 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000677 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000678 bool IsCtorOrDtor =
679 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
680
681 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000682 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000683
684 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
685 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000686 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000687 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000688 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000689
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000690 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000691 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
692 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000693
694 // Collect virtual method info.
695 llvm::DIType ContainingType;
696 unsigned Virtuality = 0;
697 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000698
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000699 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000700 if (Method->isPure())
701 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
702 else
703 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
704
705 // It doesn't make sense to give a virtual destructor a vtable index,
706 // since a single destructor has two entries in the vtable.
707 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000708 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000709 ContainingType = RecordTy;
710 }
711
Devang Patele2472482010-09-29 21:05:52 +0000712 unsigned Flags = 0;
713 if (Method->isImplicit())
714 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000715 AccessSpecifier Access = Method->getAccess();
716 if (Access == clang::AS_private)
717 Flags |= llvm::DIDescriptor::FlagPrivate;
718 else if (Access == clang::AS_protected)
719 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000720 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
721 if (CXXC->isExplicit())
722 Flags |= llvm::DIDescriptor::FlagExplicit;
723 } else if (const CXXConversionDecl *CXXC =
724 dyn_cast<CXXConversionDecl>(Method)) {
725 if (CXXC->isExplicit())
726 Flags |= llvm::DIDescriptor::FlagExplicit;
727 }
Devang Patel3951e712010-10-07 22:03:49 +0000728 if (Method->hasPrototype())
729 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000730
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000731 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +0000732 DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName,
733 MethodDefUnit, MethodLine,
734 MethodTy, /*isLocalToUnit=*/false,
735 /* isDefinition=*/ false,
736 Virtuality, VIndex, ContainingType,
737 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000738
739 // Don't cache ctors or dtors since we have to emit multiple functions for
740 // a single ctor or dtor.
741 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000742 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000743
744 return SP;
745}
746
Devang Patel4125fd22010-01-19 01:54:44 +0000747/// CollectCXXMemberFunctions - A helper function to collect debug info for
748/// C++ member functions.This is used while creating debug info entry for
749/// a Record.
750void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000751CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000752 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000753 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000754 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
755 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000756 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000757
Devang Pateld5322da2010-02-09 19:09:28 +0000758 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000759 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000760
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000761 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000762 }
763}
764
Devang Patel2ed8f002010-08-27 17:47:47 +0000765/// CollectCXXFriends - A helper function to collect debug info for
766/// C++ base classes. This is used while creating debug info entry for
767/// a Record.
768void CGDebugInfo::
769CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000770 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000771 llvm::DIType RecordTy) {
772
773 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
774 BE = RD->friend_end(); BI != BE; ++BI) {
Devang Patel823d8e92010-12-08 22:42:58 +0000775 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
776 EltTys.push_back(DBuilder.CreateFriend(RecordTy,
777 getOrCreateType(TInfo->getType(),
778 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000779 }
780}
781
Devang Patela245c5b2010-01-25 23:32:18 +0000782/// CollectCXXBases - A helper function to collect debug info for
783/// C++ base classes. This is used while creating debug info entry for
784/// a Record.
785void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000786CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000787 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000788 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000789
Devang Patel239cec62010-02-01 21:39:52 +0000790 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
791 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
792 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000793 unsigned BFlags = 0;
794 uint64_t BaseOffset;
795
796 const CXXRecordDecl *Base =
797 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
798
799 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000800 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000801 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000802 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000803 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000804 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000805 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000806
807 AccessSpecifier Access = BI->getAccessSpecifier();
808 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000809 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000810 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000811 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000812
Devang Patel823d8e92010-12-08 22:42:58 +0000813 llvm::DIType DTy =
814 DBuilder.CreateInheritance(RecordTy,
815 getOrCreateType(BI->getType(), Unit),
816 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000817 EltTys.push_back(DTy);
818 }
Devang Patela245c5b2010-01-25 23:32:18 +0000819}
820
Devang Patel4ce3f202010-01-28 18:11:52 +0000821/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000822llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000823 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000824 return VTablePtrType;
825
826 ASTContext &Context = CGM.getContext();
827
828 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000829 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
830 llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1);
831 llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000832 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000833 llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0,
834 "__vtbl_ptr_type");
835 VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000836 return VTablePtrType;
837}
838
Anders Carlsson046c2942010-04-17 20:15:18 +0000839/// getVTableName - Get vtable name for the given Class.
840llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000841 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000842 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000843
844 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000845 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000846 memcpy(StrPtr, Name.data(), Name.length());
847 return llvm::StringRef(StrPtr, Name.length());
848}
849
850
Anders Carlsson046c2942010-04-17 20:15:18 +0000851/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000852/// debug info entry in EltTys vector.
853void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000854CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000855 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000856 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000857
858 // If there is a primary base then it will hold vtable info.
859 if (RL.getPrimaryBase())
860 return;
861
862 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000863 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000864 return;
865
866 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
867 llvm::DIType VPTR
Devang Patel823d8e92010-12-08 22:42:58 +0000868 = DBuilder.CreateMemberType(getVTableName(RD), Unit,
869 0, Size, 0, 0, 0,
870 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000871 EltTys.push_back(VPTR);
872}
873
Devang Patelc69e1cf2010-09-30 19:05:55 +0000874/// getOrCreateRecordType - Emit record type's standalone debug info.
875llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
876 SourceLocation Loc) {
877 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel823d8e92010-12-08 22:42:58 +0000878 DBuilder.RetainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000879 return T;
880}
881
Devang Patel65e99f22009-02-25 01:36:11 +0000882/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000883llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000884 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000885 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000888 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
889 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891 // Records and classes and unions can all be recursive. To handle them, we
892 // first generate a debug descriptor for the struct as a forward declaration.
893 // Then (if it is a definition) we go through and get debug info for all of
894 // its members. Finally, we create a descriptor for the complete type (which
895 // may refer to the forward decl if the struct is recursive) and replace all
896 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000897 llvm::DIDescriptor FDContext =
898 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
899
900 // If this is just a forward declaration, construct an appropriately
901 // marked node and just return it.
902 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +0000903 llvm::DIType FwdDecl =
904 DBuilder.CreateStructType(FDContext, RD->getName(),
905 DefUnit, Line, 0, 0,
906 llvm::DIDescriptor::FlagFwdDecl,
907 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +0000908
909 return FwdDecl;
910 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000911
Devang Patel823d8e92010-12-08 22:42:58 +0000912 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Devang Patelab699792010-05-07 18:12:35 +0000914 llvm::MDNode *MN = FwdDecl;
915 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000916 // Otherwise, insert it into the TypeCache so that recursive uses will find
917 // it.
Devang Patelab699792010-05-07 18:12:35 +0000918 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000919 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000920 RegionStack.push_back(FwdDeclNode);
921 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000922
923 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +0000924 llvm::SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000925
Devang Pateld6c5a262010-02-01 21:52:22 +0000926 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000927 if (CXXDecl) {
928 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000929 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000930 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000931
932 // Collect static variables with initializers.
933 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
934 I != E; ++I)
935 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
936 if (const Expr *Init = V->getInit()) {
937 Expr::EvalResult Result;
938 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
939 llvm::ConstantInt *CI
940 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
941
942 // Create the descriptor for static variable.
943 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
944 llvm::StringRef VName = V->getName();
945 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
946 // Do not use DIGlobalVariable for enums.
947 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel823d8e92010-12-08 22:42:58 +0000948 DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit,
949 getLineNumber(V->getLocation()),
950 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +0000951 }
952 }
953 }
954 }
955
Devang Pateld6c5a262010-02-01 21:52:22 +0000956 CollectRecordFields(RD, Unit, EltTys);
Devang Patel4ce3f202010-01-28 18:11:52 +0000957 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000958 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000959 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel823d8e92010-12-08 22:42:58 +0000960 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000961
Devang Patel823d8e92010-12-08 22:42:58 +0000962 RegionStack.pop_back();
963 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
964 RegionMap.find(Ty->getDecl());
965 if (RI != RegionMap.end())
966 RegionMap.erase(RI);
967
968 llvm::DIDescriptor RDContext =
969 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
970 llvm::StringRef RDName = RD->getName();
971 uint64_t Size = CGM.getContext().getTypeSize(Ty);
972 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
973 llvm::DIArray Elements =
974 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
975 llvm::MDNode *RealDecl = NULL;
976
977 if (RD->isStruct())
978 RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line,
979 Size, Align, 0, Elements);
980 else if (RD->isUnion())
981 RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line,
982 Size, Align, 0, Elements);
983 else {
984 assert(RD->isClass() && "Unknown RecordType!");
985 RDName = getClassName(RD);
986 // A class's primary base or the class itself contains the vtable.
987 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +0000988 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +0000989 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
990 // Seek non virtual primary base root.
991 while (1) {
992 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
993 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +0000994 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +0000995 PBase = PBT;
996 else
997 break;
998 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000999 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001000 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001001 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001002 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001003 ContainingType = FwdDecl;
Devang Patel823d8e92010-12-08 22:42:58 +00001004 RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line,
1005 Size, Align, 0, 0, llvm::DIType(),
1006 Elements, ContainingType);
Devang Patela245c5b2010-01-25 23:32:18 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner9c85ba32008-11-10 06:08:34 +00001009 // Now that we have a real decl for the struct, replace anything using the
1010 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001011 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001012 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001013 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001014}
1015
John McCallc12c5bb2010-05-15 11:32:37 +00001016/// CreateType - get objective-c object type.
1017llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1018 llvm::DIFile Unit) {
1019 // Ignore protocols.
1020 return getOrCreateType(Ty->getBaseType(), Unit);
1021}
1022
Devang Patel9ca36b62009-02-26 21:10:26 +00001023/// CreateType - get objective-c interface type.
1024llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001025 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001026 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001027 if (!ID)
1028 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001029
1030 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001031 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001032 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001033 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001034
Dan Gohman45f7c782010-08-23 21:15:56 +00001035 // If this is just a forward declaration, return a special forward-declaration
1036 // debug type.
1037 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001038 llvm::DIType FwdDecl =
1039 DBuilder.CreateStructType(Unit, ID->getName(),
1040 DefUnit, Line, 0, 0, 0,
1041 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001042 return FwdDecl;
1043 }
1044
Devang Patel9ca36b62009-02-26 21:10:26 +00001045 // To handle recursive interface, we
1046 // first generate a debug descriptor for the struct as a forward declaration.
1047 // Then (if it is a definition) we go through and get debug info for all of
1048 // its members. Finally, we create a descriptor for the complete type (which
1049 // may refer to the forward decl if the struct is recursive) and replace all
1050 // uses of the forward declaration with the final definition.
Devang Patel823d8e92010-12-08 22:42:58 +00001051 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Devang Patelab699792010-05-07 18:12:35 +00001053 llvm::MDNode *MN = FwdDecl;
1054 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001055 // Otherwise, insert it into the TypeCache so that recursive uses will find
1056 // it.
Devang Patelab699792010-05-07 18:12:35 +00001057 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001058 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001059 RegionStack.push_back(FwdDeclNode);
1060 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001061
1062 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +00001063 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001064
Devang Pateld6c5a262010-02-01 21:52:22 +00001065 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001066 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001067 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001068 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001069 if (!SClassTy.isValid())
1070 return llvm::DIType();
1071
Mike Stump1eb44332009-09-09 15:08:12 +00001072 llvm::DIType InhTag =
Devang Patel823d8e92010-12-08 22:42:58 +00001073 DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001074 EltTys.push_back(InhTag);
1075 }
1076
Devang Pateld6c5a262010-02-01 21:52:22 +00001077 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001078
1079 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001080 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001081 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001082 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001083 if (!FieldTy.isValid())
1084 return llvm::DIType();
1085
Devang Patel73621622009-11-25 17:37:31 +00001086 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001087
Devang Patelde135022009-04-27 22:40:36 +00001088 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001089 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001090 continue;
1091
Devang Patel9ca36b62009-02-26 21:10:26 +00001092 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001093 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1094 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001095 QualType FType = Field->getType();
1096 uint64_t FieldSize = 0;
1097 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001098
Devang Patel99c20eb2009-03-20 18:24:39 +00001099 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Devang Patel99c20eb2009-03-20 18:24:39 +00001101 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001102 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001103 Expr *BitWidth = Field->getBitWidth();
1104 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001105 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001106
Anders Carlsson20f12a22009-12-06 18:00:51 +00001107 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001108 }
1109
Mike Stump1eb44332009-09-09 15:08:12 +00001110 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1111
Devang Patelc20482b2009-03-19 00:23:53 +00001112 unsigned Flags = 0;
1113 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001114 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001115 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001116 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001117
Devang Patel823d8e92010-12-08 22:42:58 +00001118 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
1119 FieldLine, FieldSize, FieldAlign,
1120 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001121 EltTys.push_back(FieldTy);
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Devang Patel9ca36b62009-02-26 21:10:26 +00001124 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001125 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001126
Devang Patele4c1ea02010-03-11 20:01:48 +00001127 RegionStack.pop_back();
1128 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1129 RegionMap.find(Ty->getDecl());
1130 if (RI != RegionMap.end())
1131 RegionMap.erase(RI);
1132
Devang Patel9ca36b62009-02-26 21:10:26 +00001133 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001134 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1135 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Devang Patel823d8e92010-12-08 22:42:58 +00001137 llvm::DIType RealDecl =
1138 DBuilder.CreateStructType(Unit, ID->getName(), DefUnit,
1139 Line, Size, Align, 0,
1140 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001141
1142 // Now that we have a real decl for the struct, replace anything using the
1143 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001144 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001145 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001146
Devang Patel9ca36b62009-02-26 21:10:26 +00001147 return RealDecl;
1148}
1149
Chris Lattner9c85ba32008-11-10 06:08:34 +00001150llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001151 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001152 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001153
Chris Lattner9c85ba32008-11-10 06:08:34 +00001154}
1155
1156llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001157 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001158 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1159 return CreateType(RT, Unit);
1160 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1161 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Chris Lattner9c85ba32008-11-10 06:08:34 +00001163 return llvm::DIType();
1164}
1165
Devang Patel70c23cd2010-02-23 22:59:39 +00001166llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001167 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001168 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1169 uint64_t NumElems = Ty->getNumElements();
1170 if (NumElems > 0)
1171 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001172
Devang Patel823d8e92010-12-08 22:42:58 +00001173 llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems);
1174 llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001175
1176 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1177 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1178
1179 return
Devang Patel823d8e92010-12-08 22:42:58 +00001180 DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001181}
1182
Chris Lattner9c85ba32008-11-10 06:08:34 +00001183llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001184 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001185 uint64_t Size;
1186 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
1188
Nuno Lopes010d5142009-01-28 00:35:17 +00001189 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001190 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001191 Size = 0;
1192 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001193 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001194 } else if (Ty->isIncompleteArrayType()) {
1195 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001196 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001197 } else {
1198 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001199 Size = CGM.getContext().getTypeSize(Ty);
1200 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001201 }
Mike Stump1eb44332009-09-09 15:08:12 +00001202
Chris Lattner9c85ba32008-11-10 06:08:34 +00001203 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1204 // interior arrays, do we care? Why aren't nested arrays represented the
1205 // obvious/recursive way?
Devang Patel823d8e92010-12-08 22:42:58 +00001206 llvm::SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001207 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001208 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001209 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001210 else {
1211 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1212 uint64_t Upper = 0;
1213 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1214 if (CAT->getSize().getZExtValue())
1215 Upper = CAT->getSize().getZExtValue() - 1;
1216 // FIXME: Verify this is right for VLAs.
Devang Patel823d8e92010-12-08 22:42:58 +00001217 Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper));
Devang Patelcdf523c2010-10-06 18:30:00 +00001218 EltTy = Ty->getElementType();
1219 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001220 }
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Chris Lattner9c85ba32008-11-10 06:08:34 +00001222 llvm::DIArray SubscriptArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001223 DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001224
Devang Patelca80a5f2009-10-20 19:55:01 +00001225 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001226 DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1227 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001228 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229}
1230
Anders Carlssona031b352009-11-06 19:19:55 +00001231llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001232 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001233 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1234 Ty, Ty->getPointeeType(), Unit);
1235}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001236
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001238 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001239 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1240 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1241
1242 if (!Ty->getPointeeType()->isFunctionType()) {
1243 // We have a data member pointer type.
1244 return PointerDiffDITy;
1245 }
1246
1247 // We have a member function pointer type. Treat it as a struct with two
1248 // ptrdiff_t members.
1249 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1250
1251 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001252 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001253
1254 // FIXME: This should probably be a function type instead.
1255 ElementTypes[0] =
Devang Patel823d8e92010-12-08 22:42:58 +00001256 DBuilder.CreateMemberType("ptr", U, 0,
1257 Info.first, Info.second, FieldOffset, 0,
1258 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001259 FieldOffset += Info.first;
1260
1261 ElementTypes[1] =
Devang Patel823d8e92010-12-08 22:42:58 +00001262 DBuilder.CreateMemberType("ptr", U, 0,
1263 Info.first, Info.second, FieldOffset, 0,
1264 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001265
1266 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001267 DBuilder.GetOrCreateArray(&ElementTypes[0],
1268 llvm::array_lengthof(ElementTypes));
Anders Carlsson20f12a22009-12-06 18:00:51 +00001269
Devang Patel823d8e92010-12-08 22:42:58 +00001270 return DBuilder.CreateStructType(U, llvm::StringRef("test"),
1271 U, 0, FieldOffset,
1272 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001273}
1274
Devang Patel6237cea2010-08-23 22:07:25 +00001275/// CreateEnumType - get enumeration type.
1276llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
Devang Patel823d8e92010-12-08 22:42:58 +00001277 llvm::SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001278
1279 // Create DIEnumerator elements for each enumerator.
1280 for (EnumDecl::enumerator_iterator
1281 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1282 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001283 Enumerators.push_back(
1284 DBuilder.CreateEnumerator(Enum->getName(),
1285 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001286 }
1287
1288 // Return a CompositeType for the enum itself.
1289 llvm::DIArray EltArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001290 DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Devang Patel6237cea2010-08-23 22:07:25 +00001291
1292 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1293 unsigned Line = getLineNumber(ED->getLocation());
1294 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001295 uint64_t Align = 0;
1296 if (!ED->getTypeForDecl()->isIncompleteType()) {
1297 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1298 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1299 }
Devang Patel4bc48872010-10-27 23:23:58 +00001300 llvm::DIDescriptor EnumContext =
1301 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001302 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001303 DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1304 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001305 return DbgTy;
1306}
1307
Douglas Gregor840943d2009-12-21 20:18:30 +00001308static QualType UnwrapTypeForDebugInfo(QualType T) {
1309 do {
1310 QualType LastT = T;
1311 switch (T->getTypeClass()) {
1312 default:
1313 return T;
1314 case Type::TemplateSpecialization:
1315 T = cast<TemplateSpecializationType>(T)->desugar();
1316 break;
1317 case Type::TypeOfExpr: {
1318 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1319 T = Ty->getUnderlyingExpr()->getType();
1320 break;
1321 }
1322 case Type::TypeOf:
1323 T = cast<TypeOfType>(T)->getUnderlyingType();
1324 break;
1325 case Type::Decltype:
1326 T = cast<DecltypeType>(T)->getUnderlyingType();
1327 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001328 case Type::Elaborated:
1329 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001330 break;
1331 case Type::SubstTemplateTypeParm:
1332 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1333 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001334 }
1335
1336 assert(T != LastT && "Type unwrapping failed to unwrap!");
1337 if (T == LastT)
1338 return T;
1339 } while (true);
1340
1341 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001342}
1343
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001344/// getOrCreateType - Get the type from the cache or create a new
1345/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001346llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001347 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001348 if (Ty.isNull())
1349 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Douglas Gregor840943d2009-12-21 20:18:30 +00001351 // Unwrap the type as needed for debug information.
1352 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001353
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001354 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001355 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001356 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001357 if (it != TypeCache.end()) {
1358 // Verify that the debug info still exists.
1359 if (&*it->second)
1360 return llvm::DIType(cast<llvm::MDNode>(it->second));
1361 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001362
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001363 // Otherwise create the type.
1364 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001365
1366 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001367 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001368 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001369}
1370
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001371/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001372llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001373 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001374 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001375 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001376 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001377
Douglas Gregor2101a822009-12-21 19:57:21 +00001378 const char *Diag = 0;
1379
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001380 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001381 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001382#define TYPE(Class, Base)
1383#define ABSTRACT_TYPE(Class, Base)
1384#define NON_CANONICAL_TYPE(Class, Base)
1385#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1386#include "clang/AST/TypeNodes.def"
1387 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001388
Anders Carlssonbfe69952009-11-06 18:24:04 +00001389 // FIXME: Handle these.
1390 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001391 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001392
1393 case Type::Vector:
1394 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001395 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001396 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001397 case Type::ObjCObject:
1398 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001399 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001400 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001401 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001402 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001403 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001404 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001405 return CreateType(cast<BlockPointerType>(Ty), Unit);
1406 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001407 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001408 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001409 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001410 case Type::FunctionProto:
1411 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001412 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001413 case Type::ConstantArray:
1414 case Type::VariableArray:
1415 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001416 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001417
1418 case Type::LValueReference:
1419 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1420
Anders Carlsson20f12a22009-12-06 18:00:51 +00001421 case Type::MemberPointer:
1422 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001423
1424 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001425 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001426 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001427 case Type::TypeOfExpr:
1428 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001429 case Type::Decltype:
1430 llvm_unreachable("type should have been unwrapped!");
1431 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001432
1433 case Type::RValueReference:
1434 // FIXME: Implement!
1435 Diag = "rvalue references";
1436 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001437 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001438
1439 assert(Diag && "Fall through without a diagnostic?");
1440 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1441 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001442 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001443 << Diag;
1444 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001445}
1446
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001447/// CreateMemberType - Create new member and increase Offset by FType's size.
1448llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1449 llvm::StringRef Name,
1450 uint64_t *Offset) {
1451 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1452 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1453 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel823d8e92010-12-08 22:42:58 +00001454 llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0,
1455 FieldSize, FieldAlign,
1456 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001457 *Offset += FieldSize;
1458 return Ty;
1459}
1460
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001461/// EmitFunctionStart - Constructs the debug code for entering a function -
1462/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001463void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001464 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001465 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Devang Patel9c6c3a02010-01-14 00:36:21 +00001467 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001468 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001469
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001470 FnBeginRegionCount.push_back(RegionStack.size());
1471
Devang Patel9c6c3a02010-01-14 00:36:21 +00001472 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001473 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001474 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1475 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001476 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001477 // If there is a DISubprogram for this function available then use it.
1478 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1479 FI = SPCache.find(FD);
1480 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001481 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001482 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1483 llvm::MDNode *SPN = SP;
1484 RegionStack.push_back(SPN);
1485 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001486 return;
1487 }
1488 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001489 Name = getFunctionName(FD);
1490 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001491 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001492 if (LinkageName == Name)
1493 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001494 if (FD->hasPrototype())
1495 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001496 if (const NamespaceDecl *NSDecl =
1497 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1498 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001499 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001500 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001501 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001502 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001503 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001504 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001505 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001506 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001507 if (!Name.empty() && Name[0] == '\01')
1508 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Devang Patel970c6182010-04-24 00:49:16 +00001510 // It is expected that CurLoc is set before using EmitFunctionStart.
1511 // Usually, CurLoc points to the left bracket location of compound
1512 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001513 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001514 if (D->isImplicit())
1515 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001516 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +00001517 DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit,
1518 LineNo, getOrCreateType(FnType, Unit),
1519 Fn->hasInternalLinkage(), true/*definition*/,
1520 Flags, CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001522 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001523 llvm::MDNode *SPN = SP;
1524 RegionStack.push_back(SPN);
1525 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001526
1527 // Clear stack used to keep track of #line directives.
1528 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001529}
1530
1531
Devang Patel4d939e62010-07-20 22:20:10 +00001532void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001533 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001535 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001536 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001537 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001538 || (SM.getInstantiationLineNumber(CurLoc) ==
1539 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001540 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001541 // New Builder may not be in sync with CGDebugInfo.
1542 if (!Builder.getCurrentDebugLocation().isUnknown())
1543 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001544
1545 // Update last state.
1546 PrevLoc = CurLoc;
1547
Chris Lattnerc6034632010-04-01 06:31:43 +00001548 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001549 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1550 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001551 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001552}
1553
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001554/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1555/// has introduced scope change.
1556void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1557 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1558 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1559 return;
1560 SourceManager &SM = CGM.getContext().getSourceManager();
1561 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1562 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1563
Douglas Gregor8c457a82010-11-11 20:45:16 +00001564 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1565 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001566 return;
1567
1568 // If #line directive stack is empty then we are entering a new scope.
1569 if (LineDirectiveFiles.empty()) {
1570 EmitRegionStart(Builder);
1571 LineDirectiveFiles.push_back(PCLoc.getFilename());
1572 return;
1573 }
1574
1575 assert (RegionStack.size() >= LineDirectiveFiles.size()
1576 && "error handling #line regions!");
1577
1578 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001579 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001580 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1581 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001582 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001583 SeenThisFile = true;
1584 break;
1585 }
1586
1587 // If #line for this file is seen earlier then pop out #line regions.
1588 if (SeenThisFile) {
1589 while (!LineDirectiveFiles.empty()) {
1590 const char *LastFile = LineDirectiveFiles.back();
1591 RegionStack.pop_back();
1592 LineDirectiveFiles.pop_back();
1593 if (!strcmp(PPLoc.getFilename(), LastFile))
1594 break;
1595 }
1596 return;
1597 }
1598
1599 // .. otherwise insert new #line region.
1600 EmitRegionStart(Builder);
1601 LineDirectiveFiles.push_back(PCLoc.getFilename());
1602
1603 return;
1604}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001605/// EmitRegionStart- Constructs the debug code for entering a declarative
1606/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001607void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001608 llvm::DIDescriptor D =
Devang Patel823d8e92010-12-08 22:42:58 +00001609 DBuilder.CreateLexicalBlock(RegionStack.empty() ?
1610 llvm::DIDescriptor() :
1611 llvm::DIDescriptor(RegionStack.back()),
1612 getOrCreateFile(CurLoc),
1613 getLineNumber(CurLoc),
1614 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001615 llvm::MDNode *DN = D;
1616 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001617}
1618
1619/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1620/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001621void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001622 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1623
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001624 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001625 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001627 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001628}
1629
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001630/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1631void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1632 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1633 unsigned RCount = FnBeginRegionCount.back();
1634 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1635
1636 // Pop all regions for this function.
1637 while (RegionStack.size() != RCount)
1638 EmitRegionEnd(Builder);
1639 FnBeginRegionCount.pop_back();
1640}
1641
Devang Patel809b9bb2010-02-10 18:49:08 +00001642// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1643// See BuildByRefType.
1644llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1645 uint64_t *XOffset) {
1646
Devang Patel823d8e92010-12-08 22:42:58 +00001647 llvm::SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001648 QualType FType;
1649 uint64_t FieldSize, FieldOffset;
1650 unsigned FieldAlign;
1651
Devang Patel17800552010-03-09 00:44:50 +00001652 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001653 QualType Type = VD->getType();
1654
1655 FieldOffset = 0;
1656 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001657 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1658 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001659 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001660 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1661 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1662
Devang Patel809b9bb2010-02-10 18:49:08 +00001663 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1664 if (HasCopyAndDispose) {
1665 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001666 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1667 &FieldOffset));
1668 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1669 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001670 }
1671
1672 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1673 if (Align > CharUnits::fromQuantity(
1674 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1675 unsigned AlignedOffsetInBytes
1676 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1677 unsigned NumPaddingBytes
1678 = AlignedOffsetInBytes - FieldOffset/8;
1679
1680 if (NumPaddingBytes > 0) {
1681 llvm::APInt pad(32, NumPaddingBytes);
1682 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1683 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001684 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001685 }
1686 }
1687
1688 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001689 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001690 FieldSize = CGM.getContext().getTypeSize(FType);
1691 FieldAlign = Align.getQuantity()*8;
1692
1693 *XOffset = FieldOffset;
Devang Patel823d8e92010-12-08 22:42:58 +00001694 FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit,
1695 0, FieldSize, FieldAlign,
1696 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001697 EltTys.push_back(FieldTy);
1698 FieldOffset += FieldSize;
1699
1700 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001701 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel809b9bb2010-02-10 18:49:08 +00001702
Devang Patele2472482010-09-29 21:05:52 +00001703 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001704
Devang Patel823d8e92010-12-08 22:42:58 +00001705 return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
1706 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001707}
Devang Patel823d8e92010-12-08 22:42:58 +00001708
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001709/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001710void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001711 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001712 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1713
Devang Patel17800552010-03-09 00:44:50 +00001714 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001715 llvm::DIType Ty;
1716 uint64_t XOffset = 0;
1717 if (VD->hasAttr<BlocksAttr>())
1718 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1719 else
1720 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001721
Devang Patelf4e54a22010-05-07 23:05:55 +00001722 // If there is not any debug info for type then do not emit debug info
1723 // for this variable.
1724 if (!Ty)
1725 return;
1726
Chris Lattner9c85ba32008-11-10 06:08:34 +00001727 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001728 unsigned Line = getLineNumber(VD->getLocation());
1729 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001730 unsigned Flags = 0;
1731 if (VD->isImplicit())
1732 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001733 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001734
1735 llvm::StringRef Name = VD->getName();
1736 if (!Name.empty()) {
1737 // Create the descriptor for the variable.
1738 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001739 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1740 Name, Unit, Line, Ty,
1741 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001742
1743 // Insert an llvm.dbg.declare into the current block.
1744 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001745 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001746
1747 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001748 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001749 }
1750
1751 // If VD is an anonymous union then Storage represents value for
1752 // all union fields.
1753 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1754 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1755 if (RD->isUnion()) {
1756 for (RecordDecl::field_iterator I = RD->field_begin(),
1757 E = RD->field_end();
1758 I != E; ++I) {
1759 FieldDecl *Field = *I;
1760 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1761 llvm::StringRef FieldName = Field->getName();
1762
1763 // Ignore unnamed fields. Do not ignore unnamed records.
1764 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1765 continue;
1766
1767 // Use VarDecl's Tag, Scope and Line number.
1768 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001769 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1770 FieldName, Unit, Line, FieldTy,
1771 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001772
1773 // Insert an llvm.dbg.declare into the current block.
1774 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001775 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001776
1777 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1778 }
1779 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001780}
1781
Mike Stumpb1a6e682009-09-30 02:43:10 +00001782/// EmitDeclare - Emit local variable declaration debug info.
1783void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1784 llvm::Value *Storage, CGBuilderTy &Builder,
1785 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001786 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001787 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1788
Devang Patel2b594b92010-04-26 23:28:46 +00001789 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001790 return;
1791
1792 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001793 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001794 llvm::DIType Ty;
1795 if (VD->hasAttr<BlocksAttr>())
1796 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1797 else
1798 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001799
1800 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001801 unsigned Line = getLineNumber(VD->getLocation());
1802 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001803
Devang Pateld6c5a262010-02-01 21:52:22 +00001804 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001805 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001806 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1807 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1808 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1809 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001810 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001811 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1812 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001813 // offset of __forwarding field
1814 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001815 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1816 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1817 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001818 // offset of x field
1819 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001820 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001821 }
1822
1823 // Create the descriptor for the variable.
1824 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001825 DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
1826 VD->getName(), Unit, Line, Ty,
1827 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001828 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001829 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001830 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001831
Chris Lattnerc6034632010-04-01 06:31:43 +00001832 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001833 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001834}
1835
Devang Pateld6c5a262010-02-01 21:52:22 +00001836void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001837 llvm::Value *Storage,
1838 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001839 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001840}
1841
Mike Stumpb1a6e682009-09-30 02:43:10 +00001842void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1843 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1844 CodeGenFunction *CGF) {
1845 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1846}
1847
Chris Lattner9c85ba32008-11-10 06:08:34 +00001848/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1849/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001850void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001851 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001852 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001853}
1854
1855
1856
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001857/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001858void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001859 const VarDecl *D) {
1860
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001861 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001862 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001863 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001864
Devang Pateleb6d79b2010-02-01 21:34:11 +00001865 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001866 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001868 // CodeGen turns int[] into int[1] so we'll do the same here.
1869 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001870
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001871 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001872 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Anders Carlsson20f12a22009-12-06 18:00:51 +00001874 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001875 ArrayType::Normal, 0);
1876 }
Devang Patel5d822f02010-04-29 17:48:37 +00001877 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001878 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001879 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001880 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001881 if (LinkageName == DeclName)
1882 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001883 llvm::DIDescriptor DContext =
1884 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +00001885 DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName,
1886 Unit, LineNo, getOrCreateType(T, Unit),
1887 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001888}
1889
Devang Patel9ca36b62009-02-26 21:10:26 +00001890/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001891void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001892 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001893 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001894 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001895 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001896
Devang Pateld6c5a262010-02-01 21:52:22 +00001897 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001898
Devang Pateld6c5a262010-02-01 21:52:22 +00001899 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001900 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Devang Patel9ca36b62009-02-26 21:10:26 +00001902 // CodeGen turns int[] into int[1] so we'll do the same here.
1903 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Devang Patel9ca36b62009-02-26 21:10:26 +00001905 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001906 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Anders Carlsson20f12a22009-12-06 18:00:51 +00001908 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001909 ArrayType::Normal, 0);
1910 }
1911
Devang Patel823d8e92010-12-08 22:42:58 +00001912 DBuilder.CreateGlobalVariable(Name, Unit, LineNo,
1913 getOrCreateType(T, Unit),
1914 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00001915}
Devang Patelabb485f2010-02-01 19:16:32 +00001916
Devang Patel25c2c8f2010-08-10 17:53:33 +00001917/// EmitGlobalVariable - Emit global variable's debug info.
1918void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001919 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001920 // Create the descriptor for the variable.
1921 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1922 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001923 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001924 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1925 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1926 Ty = CreateEnumType(ED, Unit);
1927 }
Devang Patel0317ab02010-08-10 18:27:15 +00001928 // Do not use DIGlobalVariable for enums.
1929 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1930 return;
Devang Patel823d8e92010-12-08 22:42:58 +00001931 DBuilder.CreateStaticVariable(Unit, Name, Name, Unit,
1932 getLineNumber(VD->getLocation()),
1933 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001934}
1935
Devang Patelabb485f2010-02-01 19:16:32 +00001936/// getOrCreateNamesSpace - Return namespace descriptor for the given
1937/// namespace decl.
1938llvm::DINameSpace
1939CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1940 llvm::DIDescriptor Unit) {
1941 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1942 NameSpaceCache.find(NSDecl);
1943 if (I != NameSpaceCache.end())
1944 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1945
Devang Patel8ab870d2010-05-12 23:46:38 +00001946 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00001947 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001948 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001949 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001950 llvm::DINameSpace NS =
Devang Patel823d8e92010-12-08 22:42:58 +00001951 DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001952 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001953 return NS;
1954}