blob: 7a67192379aa580684ed6faedeff40d165acf802 [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 Patel170cef32010-12-09 00:33:05 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000057 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000058 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000059
60 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
61 I = RegionMap.find(Context);
62 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000063 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000064
Devang Pateleb6d79b2010-02-01 21:34:11 +000065 // Check namespace.
66 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +000067 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +000068
69 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
70 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000071 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +000072 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +000073 return llvm::DIDescriptor(Ty);
74 }
75 }
Devang Patel170cef32010-12-09 00:33:05 +000076 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +000077}
78
Devang Patel9c6c3a02010-01-14 00:36:21 +000079/// getFunctionName - Get function name for the given FunctionDecl. If the
80/// name is constructred on demand (e.g. C++ destructor) then the name
81/// is stored on the side.
82llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
83 assert (FD && "Invalid FunctionDecl!");
84 IdentifierInfo *FII = FD->getIdentifier();
85 if (FII)
86 return FII->getName();
87
88 // Otherwise construct human readable name for debug info.
89 std::string NS = FD->getNameAsString();
90
91 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000095}
96
David Chisnall52044a22010-09-02 18:01:51 +000097llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
98 llvm::SmallString<256> MethodName;
99 llvm::raw_svector_ostream OS(MethodName);
100 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
101 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000102 if (const ObjCImplementationDecl *OID =
103 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000104 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000105 } else if (const ObjCInterfaceDecl *OID =
106 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000107 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000108 } else if (const ObjCCategoryImplDecl *OCD =
109 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000110 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
111 OCD->getIdentifier()->getNameStart() << ')';
112 }
113 OS << ' ' << OMD->getSelector().getAsString() << ']';
114
115 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
116 memcpy(StrPtr, MethodName.begin(), OS.tell());
117 return llvm::StringRef(StrPtr, OS.tell());
118}
119
Devang Patel700a1cb2010-07-20 20:24:18 +0000120/// getClassName - Get class name including template argument list.
121llvm::StringRef
122CGDebugInfo::getClassName(RecordDecl *RD) {
123 ClassTemplateSpecializationDecl *Spec
124 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
125 if (!Spec)
126 return RD->getName();
127
128 const TemplateArgument *Args;
129 unsigned NumArgs;
130 std::string Buffer;
131 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
132 const TemplateSpecializationType *TST =
133 cast<TemplateSpecializationType>(TAW->getType());
134 Args = TST->getArgs();
135 NumArgs = TST->getNumArgs();
136 } else {
137 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000138 Args = TemplateArgs.data();
139 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000140 }
141 Buffer = RD->getIdentifier()->getNameStart();
142 PrintingPolicy Policy(CGM.getLangOptions());
143 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
144 NumArgs,
145 Policy);
146
147 // Copy this name on the side and use its reference.
148 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
149 memcpy(StrPtr, Buffer.data(), Buffer.length());
150 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000151}
152
Devang Patel17800552010-03-09 00:44:50 +0000153/// getOrCreateFile - Get the file debug info descriptor for the input location.
154llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000155 if (!Loc.isValid())
156 // If Location is not valid then use main input file.
157 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
158
Anders Carlsson20f12a22009-12-06 18:00:51 +0000159 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000160 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000161
Douglas Gregor8c457a82010-11-11 20:45:16 +0000162 if (PLoc.isInvalid())
163 // If the location is not valid then use main input file.
Devang Patel823d8e92010-12-08 22:42:58 +0000164 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000165
Ted Kremenek9c250392010-03-30 00:27:51 +0000166 // Cache the results.
167 const char *fname = PLoc.getFilename();
168 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
169 DIFileCache.find(fname);
170
171 if (it != DIFileCache.end()) {
172 // Verify that the information still exists.
173 if (&*it->second)
174 return llvm::DIFile(cast<llvm::MDNode>(it->second));
175 }
176
Devang Patel823d8e92010-12-08 22:42:58 +0000177 llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000178
Devang Patelab699792010-05-07 18:12:35 +0000179 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000180 return F;
181
Devang Patel17800552010-03-09 00:44:50 +0000182}
Devang Patel8ab870d2010-05-12 23:46:38 +0000183
Devang Patel532105f2010-10-28 22:03:20 +0000184/// getOrCreateMainFile - Get the file info for main compile unit.
185llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel823d8e92010-12-08 22:42:58 +0000186 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000187}
188
Devang Patel8ab870d2010-05-12 23:46:38 +0000189/// getLineNumber - Get line number for the location. If location is invalid
190/// then use current location.
191unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
192 assert (CurLoc.isValid() && "Invalid current location!");
193 SourceManager &SM = CGM.getContext().getSourceManager();
194 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000195 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000196}
197
198/// getColumnNumber - Get column number for the location. If location is
199/// invalid then use current location.
200unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
201 assert (CurLoc.isValid() && "Invalid current location!");
202 SourceManager &SM = CGM.getContext().getSourceManager();
203 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000204 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000205}
206
Devang Patelac4d13c2010-07-27 15:17:16 +0000207llvm::StringRef CGDebugInfo::getCurrentDirname() {
208 if (!CWDName.empty())
209 return CWDName;
210 char *CompDirnamePtr = NULL;
211 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
212 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
213 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
214 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
215}
216
Devang Patel17800552010-03-09 00:44:50 +0000217/// CreateCompileUnit - Create new compile unit.
218void CGDebugInfo::CreateCompileUnit() {
219
220 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000221 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000222 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
223 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000224 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000225
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000226 // The main file name provided via the "-main-file-name" option contains just
227 // the file name itself with no path information. This file name may have had
228 // a relative path, so we look into the actual file entry for the main
229 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000230 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000231 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000232 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000233 if (MainFileDir != ".")
234 MainFileName = MainFileDir + "/" + MainFileName;
235 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000236
Devang Patelac4d13c2010-07-27 15:17:16 +0000237 // Save filename string.
238 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
239 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
240 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
241
Chris Lattner515455a2009-03-25 03:28:08 +0000242 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000243 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000244 if (LO.CPlusPlus) {
245 if (LO.ObjC1)
246 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
247 else
248 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
249 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000250 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000251 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000252 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000253 } else {
254 LangTag = llvm::dwarf::DW_LANG_C89;
255 }
Devang Patel446c6192009-04-17 21:06:59 +0000256
Daniel Dunbar19f19832010-08-24 17:41:09 +0000257 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000258
259 // Figure out which version of the ObjC runtime we have.
260 unsigned RuntimeVers = 0;
261 if (LO.ObjC1)
262 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000264 // Create new compile unit.
Devang Patel823d8e92010-12-08 22:42:58 +0000265 DBuilder.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000266 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000267 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000268 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000269 // FIXME - Eliminate TheCU.
270 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000271}
272
Devang Patel65e99f22009-02-25 01:36:11 +0000273/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000274/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000275llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000276 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000277 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 switch (BT->getKind()) {
279 default:
280 case BuiltinType::Void:
281 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000282 case BuiltinType::ObjCClass:
Devang Patel823d8e92010-12-08 22:42:58 +0000283 return DBuilder.CreateStructType(TheCU, "objc_class",
284 getOrCreateMainFile(), 0, 0, 0,
285 llvm::DIDescriptor::FlagFwdDecl,
286 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000287 case BuiltinType::ObjCId: {
288 // typedef struct objc_class *Class;
289 // typedef struct objc_object {
290 // Class isa;
291 // } *id;
292
293 llvm::DIType OCTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000294 DBuilder.CreateStructType(TheCU, "objc_class",
295 getOrCreateMainFile(), 0, 0, 0,
296 llvm::DIDescriptor::FlagFwdDecl,
297 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000298 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
299
Devang Patel823d8e92010-12-08 22:42:58 +0000300 llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000301
Devang Patel823d8e92010-12-08 22:42:58 +0000302 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000303 llvm::DIType FieldTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000304 DBuilder.CreateMemberType("isa", getOrCreateMainFile(),
305 0,Size, 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000306 EltTys.push_back(FieldTy);
307 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +0000308 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelc8972c62010-07-28 01:33:15 +0000309
Devang Patel823d8e92010-12-08 22:42:58 +0000310 return DBuilder.CreateStructType(TheCU, "objc_object",
311 getOrCreateMainFile(),
312 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000313 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000314 case BuiltinType::UChar:
315 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
316 case BuiltinType::Char_S:
317 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
318 case BuiltinType::UShort:
319 case BuiltinType::UInt:
320 case BuiltinType::ULong:
321 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
322 case BuiltinType::Short:
323 case BuiltinType::Int:
324 case BuiltinType::Long:
325 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
326 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
327 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000328 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000329 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000330 }
Devang Patel05127ca2010-07-28 23:23:29 +0000331
332 switch (BT->getKind()) {
333 case BuiltinType::Long: BTName = "long int"; break;
334 case BuiltinType::LongLong: BTName = "long long int"; break;
335 case BuiltinType::ULong: BTName = "long unsigned int"; break;
336 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
337 default:
338 BTName = BT->getName(CGM.getContext().getLangOptions());
339 break;
340 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000341 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000342 uint64_t Size = CGM.getContext().getTypeSize(BT);
343 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000344 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000345 DBuilder.CreateBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000346 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000347}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000348
Devang Patel344ff5d2010-12-09 00:25:29 +0000349llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000350 // Bit size, align and offset of the type.
351 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
352 if (Ty->isComplexIntegerType())
353 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 uint64_t Size = CGM.getContext().getTypeSize(Ty);
356 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000357 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000358 DBuilder.CreateBasicType("complex", Size, Align, Encoding);
359
Devang Patelca80a5f2009-10-20 19:55:01 +0000360 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000361}
362
John McCalla1805292009-09-25 01:40:47 +0000363/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000364/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000365llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000366 QualifierCollector Qc;
367 const Type *T = Qc.strip(Ty);
368
369 // Ignore these qualifiers for now.
370 Qc.removeObjCGCAttr();
371 Qc.removeAddressSpace();
372
Chris Lattner9c85ba32008-11-10 06:08:34 +0000373 // We will create one Derived type for one qualifier and recurse to handle any
374 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000375 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000376 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000378 Qc.removeConst();
379 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000380 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000381 Qc.removeVolatile();
382 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000384 Qc.removeRestrict();
385 } else {
386 assert(Qc.empty() && "Unknown type qualifier for debug info");
387 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
John McCall49f4e1c2010-12-10 11:01:00 +0000390 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000391
Daniel Dunbar3845f862008-10-31 03:54:29 +0000392 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
393 // CVR derived types.
Devang Patel823d8e92010-12-08 22:42:58 +0000394 llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy);
395
Devang Patelca80a5f2009-10-20 19:55:01 +0000396 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000397}
398
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000399llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000400 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000401 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000402 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
403 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000404 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000405}
406
Chris Lattner9c85ba32008-11-10 06:08:34 +0000407llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000408 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000409 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
410 Ty->getPointeeType(), Unit);
411}
412
Devang Patelc69e1cf2010-09-30 19:05:55 +0000413/// CreatePointeeType - Create PointTee type. If Pointee is a record
414/// then emit record's fwd if debug info size reduction is enabled.
415llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
416 llvm::DIFile Unit) {
417 if (!CGM.getCodeGenOpts().LimitDebugInfo)
418 return getOrCreateType(PointeeTy, Unit);
419
420 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
421 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000422 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
423 unsigned Line = getLineNumber(RD->getLocation());
424 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000425 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000426
427 if (RD->isStruct())
428 return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit,
429 Line, 0, 0, llvm::DIType::FlagFwdDecl,
430 llvm::DIArray());
431 else if (RD->isUnion())
432 return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit,
433 Line, 0, 0, llvm::DIType::FlagFwdDecl,
434 llvm::DIArray());
435 else {
436 assert(RD->isClass() && "Unknown RecordType!");
437 return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit,
438 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
439 llvm::DIType(), llvm::DIArray());
440 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000441 }
442 return getOrCreateType(PointeeTy, Unit);
443
444}
445
Anders Carlssona031b352009-11-06 19:19:55 +0000446llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
447 const Type *Ty,
448 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000449 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000450
451 if (Tag == llvm::dwarf::DW_TAG_reference_type)
452 return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit));
453
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000454 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000455 // Size is always the size of a pointer. We can't use getTypeSize here
456 // because that does not return the correct value for references.
457 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000458 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
459 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Devang Patel823d8e92010-12-08 22:42:58 +0000461 return
462 DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000463}
464
Mike Stump9bc093c2009-05-14 02:03:51 +0000465llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000466 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000467 if (BlockLiteralGenericSet)
468 return BlockLiteralGeneric;
469
Devang Patel823d8e92010-12-08 22:42:58 +0000470 llvm::SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000471 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 QualType FType;
473 uint64_t FieldSize, FieldOffset;
474 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000475 llvm::DIArray Elements;
476 llvm::DIType EltTy, DescTy;
477
478 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000479 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000480 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
481 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000482
Devang Patel823d8e92010-12-08 22:42:58 +0000483 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000484 EltTys.clear();
485
Devang Patele2472482010-09-29 21:05:52 +0000486 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000487 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000488
Devang Patel823d8e92010-12-08 22:42:58 +0000489 EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor",
490 Unit, LineNo, FieldOffset, 0,
491 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Mike Stump9bc093c2009-05-14 02:03:51 +0000493 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000494 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Devang Patel823d8e92010-12-08 22:42:58 +0000496 DescTy = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000497
498 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000499 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000500 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000501 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000502 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
503 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000504 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000505 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000506
Anders Carlsson20f12a22009-12-06 18:00:51 +0000507 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000508 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000509 FieldSize = CGM.getContext().getTypeSize(Ty);
510 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel823d8e92010-12-08 22:42:58 +0000511 FieldTy = DBuilder.CreateMemberType("__descriptor", Unit,
512 LineNo, FieldSize, FieldAlign,
513 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000514 EltTys.push_back(FieldTy);
515
516 FieldOffset += FieldSize;
Devang Patel823d8e92010-12-08 22:42:58 +0000517 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000518
Devang Patel823d8e92010-12-08 22:42:58 +0000519 EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic",
520 Unit, LineNo, FieldOffset, 0,
521 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Mike Stump9bc093c2009-05-14 02:03:51 +0000523 BlockLiteralGenericSet = true;
Devang Patel823d8e92010-12-08 22:42:58 +0000524 BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000525 return BlockLiteralGeneric;
526}
527
Chris Lattner9c85ba32008-11-10 06:08:34 +0000528llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000529 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000530 // Typedefs are derived from some other type. If we have a typedef of a
531 // typedef, make sure to emit the whole chain.
532 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000533 if (!Src.Verify())
534 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000535 // We don't set size information, but do specify where the typedef was
536 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000537 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +0000538 llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(),
539 Unit, Line);
Devang Patelca80a5f2009-10-20 19:55:01 +0000540 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000541}
542
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000544 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000545 llvm::SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000546
Chris Lattner9c85ba32008-11-10 06:08:34 +0000547 // Add the result type at least.
548 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Chris Lattner9c85ba32008-11-10 06:08:34 +0000550 // Set up remainder of arguments if there is a prototype.
551 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000552 if (isa<FunctionNoProtoType>(Ty))
Devang Patel823d8e92010-12-08 22:42:58 +0000553 EltTys.push_back(DBuilder.CreateUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000554 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000555 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
556 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000557 }
558
Chris Lattner9c85ba32008-11-10 06:08:34 +0000559 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000560 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Devang Patel823d8e92010-12-08 22:42:58 +0000562 llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000563 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000564}
565
Devang Patel428deb52010-01-19 00:00:59 +0000566/// CollectRecordFields - A helper function to collect debug info for
567/// record fields. This is used while creating debug info entry for a Record.
568void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000569CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000570 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel428deb52010-01-19 00:00:59 +0000571 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000572 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
573 for (RecordDecl::field_iterator I = RD->field_begin(),
574 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000575 I != E; ++I, ++FieldNo) {
576 FieldDecl *Field = *I;
577 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000578 llvm::StringRef FieldName = Field->getName();
579
Devang Patel4835fdd2010-02-12 01:31:06 +0000580 // Ignore unnamed fields. Do not ignore unnamed records.
581 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000582 continue;
583
584 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000585 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
586 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000587 QualType FType = Field->getType();
588 uint64_t FieldSize = 0;
589 unsigned FieldAlign = 0;
590 if (!FType->isIncompleteArrayType()) {
591
592 // Bit size, align and offset of the type.
593 FieldSize = CGM.getContext().getTypeSize(FType);
594 Expr *BitWidth = Field->getBitWidth();
595 if (BitWidth)
596 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000597 FieldAlign = CGM.getContext().getTypeAlign(FType);
598 }
599
600 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
601
Devang Patel71f4ff62010-04-21 23:12:37 +0000602 unsigned Flags = 0;
603 AccessSpecifier Access = I->getAccess();
604 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000605 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000606 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000607 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000608
Devang Patel823d8e92010-12-08 22:42:58 +0000609 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
610 FieldLine, FieldSize, FieldAlign,
611 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000612 EltTys.push_back(FieldTy);
613 }
614}
615
Devang Patela6da1922010-01-28 00:28:01 +0000616/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
617/// function type is not updated to include implicit "this" pointer. Use this
618/// routine to get a method type which includes "this" pointer.
619llvm::DIType
620CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000621 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000622 llvm::DIType FnTy
623 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
624 0),
625 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000626
Devang Patela6da1922010-01-28 00:28:01 +0000627 // Add "this" pointer.
628
Devang Patelab699792010-05-07 18:12:35 +0000629 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000630 assert (Args.getNumElements() && "Invalid number of arguments!");
631
Devang Patel823d8e92010-12-08 22:42:58 +0000632 llvm::SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000633
634 // First element is always return type. For 'void' functions it is NULL.
635 Elts.push_back(Args.getElement(0));
636
Devang Patel2ed8f002010-08-27 17:47:47 +0000637 if (!Method->isStatic())
638 {
639 // "this" pointer is always first argument.
640 ASTContext &Context = CGM.getContext();
641 QualType ThisPtr =
642 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
643 llvm::DIType ThisPtrType =
Devang Patel823d8e92010-12-08 22:42:58 +0000644 DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000645
Devang Patel2ed8f002010-08-27 17:47:47 +0000646 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
647 Elts.push_back(ThisPtrType);
648 }
Devang Patela6da1922010-01-28 00:28:01 +0000649
650 // Copy rest of the arguments.
651 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
652 Elts.push_back(Args.getElement(i));
653
654 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000655 DBuilder.GetOrCreateArray(Elts.data(), Elts.size());
Devang Patela6da1922010-01-28 00:28:01 +0000656
Devang Patel823d8e92010-12-08 22:42:58 +0000657 return DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000658}
659
Devang Patel58faf202010-10-22 17:11:50 +0000660/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
661/// inside a function.
662static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
663 if (const CXXRecordDecl *NRD =
664 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
665 return isFunctionLocalClass(NRD);
666 else if (isa<FunctionDecl>(RD->getDeclContext()))
667 return true;
668 return false;
669
670}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000671/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
672/// a single member function GlobalDecl.
673llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000674CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000675 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000676 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000677 bool IsCtorOrDtor =
678 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
679
680 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000681 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000682
683 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
684 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000685 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000686 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000687 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000688
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000689 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000690 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
691 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000692
693 // Collect virtual method info.
694 llvm::DIType ContainingType;
695 unsigned Virtuality = 0;
696 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000697
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000698 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000699 if (Method->isPure())
700 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
701 else
702 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
703
704 // It doesn't make sense to give a virtual destructor a vtable index,
705 // since a single destructor has two entries in the vtable.
706 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000707 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000708 ContainingType = RecordTy;
709 }
710
Devang Patele2472482010-09-29 21:05:52 +0000711 unsigned Flags = 0;
712 if (Method->isImplicit())
713 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000714 AccessSpecifier Access = Method->getAccess();
715 if (Access == clang::AS_private)
716 Flags |= llvm::DIDescriptor::FlagPrivate;
717 else if (Access == clang::AS_protected)
718 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000719 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
720 if (CXXC->isExplicit())
721 Flags |= llvm::DIDescriptor::FlagExplicit;
722 } else if (const CXXConversionDecl *CXXC =
723 dyn_cast<CXXConversionDecl>(Method)) {
724 if (CXXC->isExplicit())
725 Flags |= llvm::DIDescriptor::FlagExplicit;
726 }
Devang Patel3951e712010-10-07 22:03:49 +0000727 if (Method->hasPrototype())
728 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000729
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000730 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +0000731 DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName,
732 MethodDefUnit, MethodLine,
733 MethodTy, /*isLocalToUnit=*/false,
734 /* isDefinition=*/ false,
735 Virtuality, VIndex, ContainingType,
736 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000737
738 // Don't cache ctors or dtors since we have to emit multiple functions for
739 // a single ctor or dtor.
740 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000741 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000742
743 return SP;
744}
745
Devang Patel4125fd22010-01-19 01:54:44 +0000746/// CollectCXXMemberFunctions - A helper function to collect debug info for
747/// C++ member functions.This is used while creating debug info entry for
748/// a Record.
749void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000750CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000751 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000752 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000753 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
754 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000755 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000756
Devang Pateld5322da2010-02-09 19:09:28 +0000757 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000758 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000759
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000760 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000761 }
762}
763
Devang Patel2ed8f002010-08-27 17:47:47 +0000764/// CollectCXXFriends - A helper function to collect debug info for
765/// C++ base classes. This is used while creating debug info entry for
766/// a Record.
767void CGDebugInfo::
768CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000769 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000770 llvm::DIType RecordTy) {
771
772 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
773 BE = RD->friend_end(); BI != BE; ++BI) {
Devang Patel823d8e92010-12-08 22:42:58 +0000774 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
775 EltTys.push_back(DBuilder.CreateFriend(RecordTy,
776 getOrCreateType(TInfo->getType(),
777 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000778 }
779}
780
Devang Patela245c5b2010-01-25 23:32:18 +0000781/// CollectCXXBases - A helper function to collect debug info for
782/// C++ base classes. This is used while creating debug info entry for
783/// a Record.
784void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000785CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000786 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000787 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000788
Devang Patel239cec62010-02-01 21:39:52 +0000789 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
790 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
791 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000792 unsigned BFlags = 0;
793 uint64_t BaseOffset;
794
795 const CXXRecordDecl *Base =
796 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
797
798 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000799 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000800 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000801 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000802 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000803 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000804 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000805
806 AccessSpecifier Access = BI->getAccessSpecifier();
807 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000808 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000809 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000810 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000811
Devang Patel823d8e92010-12-08 22:42:58 +0000812 llvm::DIType DTy =
813 DBuilder.CreateInheritance(RecordTy,
814 getOrCreateType(BI->getType(), Unit),
815 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000816 EltTys.push_back(DTy);
817 }
Devang Patela245c5b2010-01-25 23:32:18 +0000818}
819
Devang Patel4ce3f202010-01-28 18:11:52 +0000820/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000821llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000822 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000823 return VTablePtrType;
824
825 ASTContext &Context = CGM.getContext();
826
827 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000828 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
829 llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1);
830 llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000831 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000832 llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0,
833 "__vtbl_ptr_type");
834 VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000835 return VTablePtrType;
836}
837
Anders Carlsson046c2942010-04-17 20:15:18 +0000838/// getVTableName - Get vtable name for the given Class.
839llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000840 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000841 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000842
843 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000844 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000845 memcpy(StrPtr, Name.data(), Name.length());
846 return llvm::StringRef(StrPtr, Name.length());
847}
848
849
Anders Carlsson046c2942010-04-17 20:15:18 +0000850/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000851/// debug info entry in EltTys vector.
852void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000853CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000854 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000855 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000856
857 // If there is a primary base then it will hold vtable info.
858 if (RL.getPrimaryBase())
859 return;
860
861 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000862 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000863 return;
864
865 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
866 llvm::DIType VPTR
Devang Patel823d8e92010-12-08 22:42:58 +0000867 = DBuilder.CreateMemberType(getVTableName(RD), Unit,
868 0, Size, 0, 0, 0,
869 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000870 EltTys.push_back(VPTR);
871}
872
Devang Patelc69e1cf2010-09-30 19:05:55 +0000873/// getOrCreateRecordType - Emit record type's standalone debug info.
874llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
875 SourceLocation Loc) {
876 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel823d8e92010-12-08 22:42:58 +0000877 DBuilder.RetainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000878 return T;
879}
880
Devang Patel65e99f22009-02-25 01:36:11 +0000881/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +0000882llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 RecordDecl *RD = Ty->getDecl();
Devang Patel31f7d022011-01-17 22:23:07 +0000884 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Chris Lattner9c85ba32008-11-10 06:08:34 +0000886 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000887 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
888 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Chris Lattner9c85ba32008-11-10 06:08:34 +0000890 // Records and classes and unions can all be recursive. To handle them, we
891 // first generate a debug descriptor for the struct as a forward declaration.
892 // Then (if it is a definition) we go through and get debug info for all of
893 // its members. Finally, we create a descriptor for the complete type (which
894 // may refer to the forward decl if the struct is recursive) and replace all
895 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000896 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000897 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel0b897992010-07-08 19:56:29 +0000898
899 // If this is just a forward declaration, construct an appropriately
900 // marked node and just return it.
901 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +0000902 llvm::DIType FwdDecl =
903 DBuilder.CreateStructType(FDContext, RD->getName(),
904 DefUnit, Line, 0, 0,
905 llvm::DIDescriptor::FlagFwdDecl,
906 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +0000907
908 return FwdDecl;
909 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000910
Devang Patel823d8e92010-12-08 22:42:58 +0000911 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Devang Patelab699792010-05-07 18:12:35 +0000913 llvm::MDNode *MN = FwdDecl;
914 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000915 // Otherwise, insert it into the TypeCache so that recursive uses will find
916 // it.
Devang Patelab699792010-05-07 18:12:35 +0000917 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000918 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000919 RegionStack.push_back(FwdDeclNode);
920 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000921
922 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +0000923 llvm::SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000924
Devang Pateld6c5a262010-02-01 21:52:22 +0000925 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000926 if (CXXDecl) {
927 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000928 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000929 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000930
931 // Collect static variables with initializers.
932 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
933 I != E; ++I)
934 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
935 if (const Expr *Init = V->getInit()) {
936 Expr::EvalResult Result;
937 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
938 llvm::ConstantInt *CI
939 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
940
941 // Create the descriptor for static variable.
942 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
943 llvm::StringRef VName = V->getName();
944 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
945 // Do not use DIGlobalVariable for enums.
946 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel823d8e92010-12-08 22:42:58 +0000947 DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit,
948 getLineNumber(V->getLocation()),
949 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +0000950 }
951 }
952 }
953 }
954
Devang Pateld6c5a262010-02-01 21:52:22 +0000955 CollectRecordFields(RD, Unit, EltTys);
Devang Patelfa275df2011-02-02 21:38:49 +0000956 llvm::SmallVector<llvm::Value *, 16> TemplateParams;
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 Patelfa275df2011-02-02 21:38:49 +0000960 if (ClassTemplateSpecializationDecl *TSpecial
961 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
962 const TemplateArgumentList &TAL = TSpecial->getTemplateArgs();
963 for (unsigned i = 0, e = TAL.size(); i != e; ++i) {
964 const TemplateArgument &TA = TAL[i];
965 if (TA.getKind() == TemplateArgument::Type) {
966 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
967 llvm::DITemplateTypeParameter TTP =
968 DBuilder.CreateTemplateTypeParameter(TheCU, TTy.getName(), TTy);
969 TemplateParams.push_back(TTP);
Devang Patel0ce34c62011-02-02 22:36:18 +0000970 } else if (TA.getKind() == TemplateArgument::Integral) {
971 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
972 // FIXME: Get parameter name, instead of parameter type name.
973 llvm::DITemplateValueParameter TVP =
974 DBuilder.CreateTemplateValueParameter(TheCU, TTy.getName(), TTy,
975 TA.getAsIntegral()->getZExtValue());
976 TemplateParams.push_back(TVP);
Devang Patelfa275df2011-02-02 21:38:49 +0000977 }
978 }
979 }
Devang Patel823d8e92010-12-08 22:42:58 +0000980 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000981
Devang Patel823d8e92010-12-08 22:42:58 +0000982 RegionStack.pop_back();
983 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
984 RegionMap.find(Ty->getDecl());
985 if (RI != RegionMap.end())
986 RegionMap.erase(RI);
987
988 llvm::DIDescriptor RDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000989 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000990 llvm::StringRef RDName = RD->getName();
991 uint64_t Size = CGM.getContext().getTypeSize(Ty);
992 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
993 llvm::DIArray Elements =
994 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
995 llvm::MDNode *RealDecl = NULL;
996
997 if (RD->isStruct())
998 RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line,
999 Size, Align, 0, Elements);
1000 else if (RD->isUnion())
1001 RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line,
1002 Size, Align, 0, Elements);
1003 else {
1004 assert(RD->isClass() && "Unknown RecordType!");
1005 RDName = getClassName(RD);
1006 // A class's primary base or the class itself contains the vtable.
1007 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +00001008 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001009 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1010 // Seek non virtual primary base root.
1011 while (1) {
1012 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1013 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001014 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001015 PBase = PBT;
1016 else
1017 break;
1018 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001019 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001020 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001021 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001022 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001023 ContainingType = FwdDecl;
Devang Patelfa275df2011-02-02 21:38:49 +00001024 llvm::DIArray TParamsArray =
1025 DBuilder.GetOrCreateArray(TemplateParams.data(), TemplateParams.size());
Devang Patel823d8e92010-12-08 22:42:58 +00001026 RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line,
1027 Size, Align, 0, 0, llvm::DIType(),
Devang Patelfa275df2011-02-02 21:38:49 +00001028 Elements, ContainingType,
1029 TParamsArray);
Devang Patela245c5b2010-01-25 23:32:18 +00001030 }
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattner9c85ba32008-11-10 06:08:34 +00001032 // Now that we have a real decl for the struct, replace anything using the
1033 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001034 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001035 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001036 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037}
1038
John McCallc12c5bb2010-05-15 11:32:37 +00001039/// CreateType - get objective-c object type.
1040llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1041 llvm::DIFile Unit) {
1042 // Ignore protocols.
1043 return getOrCreateType(Ty->getBaseType(), Unit);
1044}
1045
Devang Patel9ca36b62009-02-26 21:10:26 +00001046/// CreateType - get objective-c interface type.
1047llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001048 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001049 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001050 if (!ID)
1051 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001052
1053 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001054 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001055 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001056 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001057
Dan Gohman45f7c782010-08-23 21:15:56 +00001058 // If this is just a forward declaration, return a special forward-declaration
1059 // debug type.
1060 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001061 llvm::DIType FwdDecl =
1062 DBuilder.CreateStructType(Unit, ID->getName(),
1063 DefUnit, Line, 0, 0, 0,
1064 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001065 return FwdDecl;
1066 }
1067
Devang Patel9ca36b62009-02-26 21:10:26 +00001068 // To handle recursive interface, we
1069 // first generate a debug descriptor for the struct as a forward declaration.
1070 // Then (if it is a definition) we go through and get debug info for all of
1071 // its members. Finally, we create a descriptor for the complete type (which
1072 // may refer to the forward decl if the struct is recursive) and replace all
1073 // uses of the forward declaration with the final definition.
Devang Patel823d8e92010-12-08 22:42:58 +00001074 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Devang Patelab699792010-05-07 18:12:35 +00001076 llvm::MDNode *MN = FwdDecl;
1077 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001078 // Otherwise, insert it into the TypeCache so that recursive uses will find
1079 // it.
Devang Patelab699792010-05-07 18:12:35 +00001080 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001081 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001082 RegionStack.push_back(FwdDeclNode);
1083 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001084
1085 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +00001086 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001087
Devang Pateld6c5a262010-02-01 21:52:22 +00001088 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001089 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001090 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001091 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001092 if (!SClassTy.isValid())
1093 return llvm::DIType();
1094
Mike Stump1eb44332009-09-09 15:08:12 +00001095 llvm::DIType InhTag =
Devang Patel823d8e92010-12-08 22:42:58 +00001096 DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001097 EltTys.push_back(InhTag);
1098 }
1099
Devang Pateld6c5a262010-02-01 21:52:22 +00001100 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001101
1102 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001103 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001104 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001105 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001106 if (!FieldTy.isValid())
1107 return llvm::DIType();
1108
Devang Patel73621622009-11-25 17:37:31 +00001109 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001110
Devang Patelde135022009-04-27 22:40:36 +00001111 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001112 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001113 continue;
1114
Devang Patel9ca36b62009-02-26 21:10:26 +00001115 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001116 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1117 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001118 QualType FType = Field->getType();
1119 uint64_t FieldSize = 0;
1120 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001121
Devang Patel99c20eb2009-03-20 18:24:39 +00001122 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Devang Patel99c20eb2009-03-20 18:24:39 +00001124 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001125 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001126 Expr *BitWidth = Field->getBitWidth();
1127 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001128 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001129
Anders Carlsson20f12a22009-12-06 18:00:51 +00001130 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001131 }
1132
Mike Stump1eb44332009-09-09 15:08:12 +00001133 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1134
Devang Patelc20482b2009-03-19 00:23:53 +00001135 unsigned Flags = 0;
1136 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001137 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001138 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001139 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Devang Patel823d8e92010-12-08 22:42:58 +00001141 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
1142 FieldLine, FieldSize, FieldAlign,
1143 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001144 EltTys.push_back(FieldTy);
1145 }
Mike Stump1eb44332009-09-09 15:08:12 +00001146
Devang Patel9ca36b62009-02-26 21:10:26 +00001147 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001148 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001149
Devang Patele4c1ea02010-03-11 20:01:48 +00001150 RegionStack.pop_back();
1151 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1152 RegionMap.find(Ty->getDecl());
1153 if (RI != RegionMap.end())
1154 RegionMap.erase(RI);
1155
Devang Patel9ca36b62009-02-26 21:10:26 +00001156 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001157 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1158 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Devang Patel823d8e92010-12-08 22:42:58 +00001160 llvm::DIType RealDecl =
1161 DBuilder.CreateStructType(Unit, ID->getName(), DefUnit,
1162 Line, Size, Align, 0,
1163 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001164
1165 // Now that we have a real decl for the struct, replace anything using the
1166 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001167 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001168 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001169
Devang Patel9ca36b62009-02-26 21:10:26 +00001170 return RealDecl;
1171}
1172
Devang Patel31f7d022011-01-17 22:23:07 +00001173llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001174 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001175 return CreateType(RT);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001176 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001177 return CreateEnumType(ET->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001178
Chris Lattner9c85ba32008-11-10 06:08:34 +00001179 return llvm::DIType();
1180}
1181
Devang Patel70c23cd2010-02-23 22:59:39 +00001182llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001183 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001184 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1185 uint64_t NumElems = Ty->getNumElements();
1186 if (NumElems > 0)
1187 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001188
Devang Patel823d8e92010-12-08 22:42:58 +00001189 llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems);
1190 llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001191
1192 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1193 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1194
1195 return
Devang Patel823d8e92010-12-08 22:42:58 +00001196 DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001197}
1198
Chris Lattner9c85ba32008-11-10 06:08:34 +00001199llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001200 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001201 uint64_t Size;
1202 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001203
1204
Nuno Lopes010d5142009-01-28 00:35:17 +00001205 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001206 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001207 Size = 0;
1208 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001209 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001210 } else if (Ty->isIncompleteArrayType()) {
1211 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001212 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001213 } else {
1214 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001215 Size = CGM.getContext().getTypeSize(Ty);
1216 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001217 }
Mike Stump1eb44332009-09-09 15:08:12 +00001218
Chris Lattner9c85ba32008-11-10 06:08:34 +00001219 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1220 // interior arrays, do we care? Why aren't nested arrays represented the
1221 // obvious/recursive way?
Devang Patel823d8e92010-12-08 22:42:58 +00001222 llvm::SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001223 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001224 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001226 else {
1227 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1228 uint64_t Upper = 0;
1229 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1230 if (CAT->getSize().getZExtValue())
1231 Upper = CAT->getSize().getZExtValue() - 1;
1232 // FIXME: Verify this is right for VLAs.
Devang Patel823d8e92010-12-08 22:42:58 +00001233 Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper));
Devang Patelcdf523c2010-10-06 18:30:00 +00001234 EltTy = Ty->getElementType();
1235 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Chris Lattner9c85ba32008-11-10 06:08:34 +00001238 llvm::DIArray SubscriptArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001239 DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001240
Devang Patelca80a5f2009-10-20 19:55:01 +00001241 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001242 DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1243 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001244 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001245}
1246
Anders Carlssona031b352009-11-06 19:19:55 +00001247llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001248 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001249 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1250 Ty, Ty->getPointeeType(), Unit);
1251}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001252
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001253llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1254 llvm::DIFile Unit) {
1255 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1256 Ty, Ty->getPointeeType(), Unit);
1257}
1258
Anders Carlsson20f12a22009-12-06 18:00:51 +00001259llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001260 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001261 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1262 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1263
1264 if (!Ty->getPointeeType()->isFunctionType()) {
1265 // We have a data member pointer type.
1266 return PointerDiffDITy;
1267 }
1268
1269 // We have a member function pointer type. Treat it as a struct with two
1270 // ptrdiff_t members.
1271 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1272
1273 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001274 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001275
1276 // FIXME: This should probably be a function type instead.
1277 ElementTypes[0] =
Devang Patel823d8e92010-12-08 22:42:58 +00001278 DBuilder.CreateMemberType("ptr", U, 0,
1279 Info.first, Info.second, FieldOffset, 0,
1280 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001281 FieldOffset += Info.first;
1282
1283 ElementTypes[1] =
Devang Patel823d8e92010-12-08 22:42:58 +00001284 DBuilder.CreateMemberType("ptr", U, 0,
1285 Info.first, Info.second, FieldOffset, 0,
1286 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001287
1288 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001289 DBuilder.GetOrCreateArray(&ElementTypes[0],
1290 llvm::array_lengthof(ElementTypes));
Anders Carlsson20f12a22009-12-06 18:00:51 +00001291
Devang Patel823d8e92010-12-08 22:42:58 +00001292 return DBuilder.CreateStructType(U, llvm::StringRef("test"),
1293 U, 0, FieldOffset,
1294 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001295}
1296
Devang Patel6237cea2010-08-23 22:07:25 +00001297/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001298llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1299 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +00001300 llvm::SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001301
1302 // Create DIEnumerator elements for each enumerator.
1303 for (EnumDecl::enumerator_iterator
1304 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1305 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001306 Enumerators.push_back(
1307 DBuilder.CreateEnumerator(Enum->getName(),
1308 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001309 }
1310
1311 // Return a CompositeType for the enum itself.
1312 llvm::DIArray EltArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001313 DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Devang Patel6237cea2010-08-23 22:07:25 +00001314
1315 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1316 unsigned Line = getLineNumber(ED->getLocation());
1317 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001318 uint64_t Align = 0;
1319 if (!ED->getTypeForDecl()->isIncompleteType()) {
1320 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1321 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1322 }
Devang Patel4bc48872010-10-27 23:23:58 +00001323 llvm::DIDescriptor EnumContext =
Devang Patel170cef32010-12-09 00:33:05 +00001324 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()));
Devang Patel6237cea2010-08-23 22:07:25 +00001325 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001326 DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1327 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001328 return DbgTy;
1329}
1330
Douglas Gregor840943d2009-12-21 20:18:30 +00001331static QualType UnwrapTypeForDebugInfo(QualType T) {
1332 do {
1333 QualType LastT = T;
1334 switch (T->getTypeClass()) {
1335 default:
1336 return T;
1337 case Type::TemplateSpecialization:
1338 T = cast<TemplateSpecializationType>(T)->desugar();
1339 break;
John McCallf4c73712011-01-19 06:33:43 +00001340 case Type::TypeOfExpr:
1341 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001342 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001343 case Type::TypeOf:
1344 T = cast<TypeOfType>(T)->getUnderlyingType();
1345 break;
1346 case Type::Decltype:
1347 T = cast<DecltypeType>(T)->getUnderlyingType();
1348 break;
John McCall9d156a72011-01-06 01:58:22 +00001349 case Type::Attributed:
1350 T = cast<AttributedType>(T)->getEquivalentType();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001351 case Type::Elaborated:
1352 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001353 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001354 case Type::Paren:
1355 T = cast<ParenType>(T)->getInnerType();
1356 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001357 case Type::SubstTemplateTypeParm:
1358 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1359 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001360 }
1361
1362 assert(T != LastT && "Type unwrapping failed to unwrap!");
1363 if (T == LastT)
1364 return T;
1365 } while (true);
1366
1367 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001368}
1369
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001370/// getOrCreateType - Get the type from the cache or create a new
1371/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001372llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001373 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001374 if (Ty.isNull())
1375 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Douglas Gregor840943d2009-12-21 20:18:30 +00001377 // Unwrap the type as needed for debug information.
1378 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001379
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001380 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001381 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001382 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001383 if (it != TypeCache.end()) {
1384 // Verify that the debug info still exists.
1385 if (&*it->second)
1386 return llvm::DIType(cast<llvm::MDNode>(it->second));
1387 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001388
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001389 // Otherwise create the type.
1390 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001391
1392 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001393 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001394 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001395}
1396
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001397/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001398llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001399 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001400 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001401 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001402 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001403
Douglas Gregor2101a822009-12-21 19:57:21 +00001404 const char *Diag = 0;
1405
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001406 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001407 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001408#define TYPE(Class, Base)
1409#define ABSTRACT_TYPE(Class, Base)
1410#define NON_CANONICAL_TYPE(Class, Base)
1411#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1412#include "clang/AST/TypeNodes.def"
1413 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001414
Anders Carlssonbfe69952009-11-06 18:24:04 +00001415 // FIXME: Handle these.
1416 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001417 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001418
1419 case Type::Vector:
1420 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001421 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001422 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001423 case Type::ObjCObject:
1424 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001425 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001426 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001427 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001428 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001429 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001430 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001431 return CreateType(cast<BlockPointerType>(Ty), Unit);
1432 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001433 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001434 case Type::Enum:
Devang Patel31f7d022011-01-17 22:23:07 +00001435 return CreateType(cast<TagType>(Ty));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001436 case Type::FunctionProto:
1437 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001438 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001439 case Type::ConstantArray:
1440 case Type::VariableArray:
1441 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001442 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001443
1444 case Type::LValueReference:
1445 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001446 case Type::RValueReference:
1447 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001448
Anders Carlsson20f12a22009-12-06 18:00:51 +00001449 case Type::MemberPointer:
1450 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001451
John McCall9d156a72011-01-06 01:58:22 +00001452 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001453 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001454 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001455 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001456 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001457 case Type::TypeOfExpr:
1458 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001459 case Type::Decltype:
1460 llvm_unreachable("type should have been unwrapped!");
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001461 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001462 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001463
1464 assert(Diag && "Fall through without a diagnostic?");
1465 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1466 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001467 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001468 << Diag;
1469 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001470}
1471
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001472/// CreateMemberType - Create new member and increase Offset by FType's size.
1473llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1474 llvm::StringRef Name,
1475 uint64_t *Offset) {
1476 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1477 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1478 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel823d8e92010-12-08 22:42:58 +00001479 llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0,
1480 FieldSize, FieldAlign,
1481 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001482 *Offset += FieldSize;
1483 return Ty;
1484}
1485
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001486/// EmitFunctionStart - Constructs the debug code for entering a function -
1487/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001488void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001489 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001490 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Devang Patel9c6c3a02010-01-14 00:36:21 +00001492 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001493 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001494
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001495 FnBeginRegionCount.push_back(RegionStack.size());
1496
Devang Patel9c6c3a02010-01-14 00:36:21 +00001497 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001498 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001499 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1500 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001501 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001502 // If there is a DISubprogram for this function available then use it.
1503 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1504 FI = SPCache.find(FD);
1505 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001506 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001507 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1508 llvm::MDNode *SPN = SP;
1509 RegionStack.push_back(SPN);
1510 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001511 return;
1512 }
1513 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001514 Name = getFunctionName(FD);
1515 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001516 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001517 if (LinkageName == Name)
1518 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001519 if (FD->hasPrototype())
1520 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001521 if (const NamespaceDecl *NSDecl =
1522 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel170cef32010-12-09 00:33:05 +00001523 FDContext = getOrCreateNameSpace(NSDecl);
David Chisnall70b9b442010-09-02 17:16:32 +00001524 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001525 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001526 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001527 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001528 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001529 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001530 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001531 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001532 if (!Name.empty() && Name[0] == '\01')
1533 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Devang Patel970c6182010-04-24 00:49:16 +00001535 // It is expected that CurLoc is set before using EmitFunctionStart.
1536 // Usually, CurLoc points to the left bracket location of compound
1537 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001538 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001539 if (D->isImplicit())
1540 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001541 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +00001542 DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit,
1543 LineNo, getOrCreateType(FnType, Unit),
1544 Fn->hasInternalLinkage(), true/*definition*/,
1545 Flags, CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001547 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001548 llvm::MDNode *SPN = SP;
1549 RegionStack.push_back(SPN);
1550 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001551
1552 // Clear stack used to keep track of #line directives.
1553 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001554}
1555
1556
Devang Patel4d939e62010-07-20 22:20:10 +00001557void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001558 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001560 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001561 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001562 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001563 || (SM.getInstantiationLineNumber(CurLoc) ==
1564 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001565 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001566 // New Builder may not be in sync with CGDebugInfo.
1567 if (!Builder.getCurrentDebugLocation().isUnknown())
1568 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001569
1570 // Update last state.
1571 PrevLoc = CurLoc;
1572
Chris Lattnerc6034632010-04-01 06:31:43 +00001573 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001574 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1575 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001576 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001577}
1578
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001579/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1580/// has introduced scope change.
1581void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1582 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1583 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1584 return;
1585 SourceManager &SM = CGM.getContext().getSourceManager();
1586 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1587 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1588
Douglas Gregor8c457a82010-11-11 20:45:16 +00001589 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1590 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001591 return;
1592
1593 // If #line directive stack is empty then we are entering a new scope.
1594 if (LineDirectiveFiles.empty()) {
1595 EmitRegionStart(Builder);
1596 LineDirectiveFiles.push_back(PCLoc.getFilename());
1597 return;
1598 }
1599
1600 assert (RegionStack.size() >= LineDirectiveFiles.size()
1601 && "error handling #line regions!");
1602
1603 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001604 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001605 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1606 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001607 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001608 SeenThisFile = true;
1609 break;
1610 }
1611
1612 // If #line for this file is seen earlier then pop out #line regions.
1613 if (SeenThisFile) {
1614 while (!LineDirectiveFiles.empty()) {
1615 const char *LastFile = LineDirectiveFiles.back();
1616 RegionStack.pop_back();
1617 LineDirectiveFiles.pop_back();
1618 if (!strcmp(PPLoc.getFilename(), LastFile))
1619 break;
1620 }
1621 return;
1622 }
1623
1624 // .. otherwise insert new #line region.
1625 EmitRegionStart(Builder);
1626 LineDirectiveFiles.push_back(PCLoc.getFilename());
1627
1628 return;
1629}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001630/// EmitRegionStart- Constructs the debug code for entering a declarative
1631/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001632void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001633 llvm::DIDescriptor D =
Devang Patel823d8e92010-12-08 22:42:58 +00001634 DBuilder.CreateLexicalBlock(RegionStack.empty() ?
1635 llvm::DIDescriptor() :
1636 llvm::DIDescriptor(RegionStack.back()),
1637 getOrCreateFile(CurLoc),
1638 getLineNumber(CurLoc),
1639 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001640 llvm::MDNode *DN = D;
1641 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001642}
1643
1644/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1645/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001646void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001647 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1648
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001649 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001650 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001652 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001653}
1654
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001655/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1656void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1657 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1658 unsigned RCount = FnBeginRegionCount.back();
1659 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1660
1661 // Pop all regions for this function.
1662 while (RegionStack.size() != RCount)
1663 EmitRegionEnd(Builder);
1664 FnBeginRegionCount.pop_back();
1665}
1666
Devang Patel809b9bb2010-02-10 18:49:08 +00001667// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1668// See BuildByRefType.
1669llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1670 uint64_t *XOffset) {
1671
Devang Patel823d8e92010-12-08 22:42:58 +00001672 llvm::SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001673 QualType FType;
1674 uint64_t FieldSize, FieldOffset;
1675 unsigned FieldAlign;
1676
Devang Patel17800552010-03-09 00:44:50 +00001677 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001678 QualType Type = VD->getType();
1679
1680 FieldOffset = 0;
1681 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001682 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1683 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001684 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001685 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1686 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1687
Devang Patel809b9bb2010-02-10 18:49:08 +00001688 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1689 if (HasCopyAndDispose) {
1690 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001691 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1692 &FieldOffset));
1693 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1694 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001695 }
1696
1697 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1698 if (Align > CharUnits::fromQuantity(
1699 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1700 unsigned AlignedOffsetInBytes
1701 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1702 unsigned NumPaddingBytes
1703 = AlignedOffsetInBytes - FieldOffset/8;
1704
1705 if (NumPaddingBytes > 0) {
1706 llvm::APInt pad(32, NumPaddingBytes);
1707 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1708 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001709 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001710 }
1711 }
1712
1713 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001714 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001715 FieldSize = CGM.getContext().getTypeSize(FType);
1716 FieldAlign = Align.getQuantity()*8;
1717
1718 *XOffset = FieldOffset;
Devang Patel823d8e92010-12-08 22:42:58 +00001719 FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit,
1720 0, FieldSize, FieldAlign,
1721 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001722 EltTys.push_back(FieldTy);
1723 FieldOffset += FieldSize;
1724
1725 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001726 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel809b9bb2010-02-10 18:49:08 +00001727
Devang Patele2472482010-09-29 21:05:52 +00001728 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001729
Devang Patel823d8e92010-12-08 22:42:58 +00001730 return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
1731 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001732}
Devang Patel823d8e92010-12-08 22:42:58 +00001733
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001734/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001735void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001736 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001737 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1738
Devang Patel17800552010-03-09 00:44:50 +00001739 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001740 llvm::DIType Ty;
1741 uint64_t XOffset = 0;
1742 if (VD->hasAttr<BlocksAttr>())
1743 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1744 else
1745 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001746
Devang Patelf4e54a22010-05-07 23:05:55 +00001747 // If there is not any debug info for type then do not emit debug info
1748 // for this variable.
1749 if (!Ty)
1750 return;
1751
Chris Lattner9c85ba32008-11-10 06:08:34 +00001752 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001753 unsigned Line = getLineNumber(VD->getLocation());
1754 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001755 unsigned Flags = 0;
1756 if (VD->isImplicit())
1757 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001758 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001759
1760 llvm::StringRef Name = VD->getName();
1761 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001762 if (VD->hasAttr<BlocksAttr>()) {
1763 CharUnits offset = CharUnits::fromQuantity(32);
1764 llvm::SmallVector<llvm::Value *, 9> addr;
1765 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1766 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1767 // offset of __forwarding field
1768 offset =
1769 CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8);
1770 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1771 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1772 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1773 // offset of x field
1774 offset = CharUnits::fromQuantity(XOffset/8);
1775 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1776
1777 // Create the descriptor for the variable.
1778 llvm::DIVariable D =
1779 DBuilder.CreateComplexVariable(Tag,
1780 llvm::DIDescriptor(RegionStack.back()),
1781 VD->getName(), Unit, Line, Ty,
1782 addr.data(), addr.size());
1783
1784 // Insert an llvm.dbg.declare into the current block.
1785 llvm::Instruction *Call =
1786 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1787
1788 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1789 return;
1790 }
1791 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00001792 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001793 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1794 Name, Unit, Line, Ty,
1795 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001796
1797 // Insert an llvm.dbg.declare into the current block.
1798 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001799 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001800
1801 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001802 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001803 }
1804
1805 // If VD is an anonymous union then Storage represents value for
1806 // all union fields.
1807 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1808 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1809 if (RD->isUnion()) {
1810 for (RecordDecl::field_iterator I = RD->field_begin(),
1811 E = RD->field_end();
1812 I != E; ++I) {
1813 FieldDecl *Field = *I;
1814 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1815 llvm::StringRef FieldName = Field->getName();
1816
1817 // Ignore unnamed fields. Do not ignore unnamed records.
1818 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1819 continue;
1820
1821 // Use VarDecl's Tag, Scope and Line number.
1822 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001823 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1824 FieldName, Unit, Line, FieldTy,
1825 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001826
1827 // Insert an llvm.dbg.declare into the current block.
1828 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001829 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001830
1831 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1832 }
1833 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001834}
1835
Mike Stumpb1a6e682009-09-30 02:43:10 +00001836/// EmitDeclare - Emit local variable declaration debug info.
1837void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1838 llvm::Value *Storage, CGBuilderTy &Builder,
1839 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001840 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001841 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1842
Devang Patel2b594b92010-04-26 23:28:46 +00001843 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001844 return;
1845
1846 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001847 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001848 llvm::DIType Ty;
1849 if (VD->hasAttr<BlocksAttr>())
1850 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1851 else
1852 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001853
1854 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001855 unsigned Line = getLineNumber(VD->getLocation());
1856 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001857
Devang Pateld6c5a262010-02-01 21:52:22 +00001858 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001859 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001860 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1861 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1862 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1863 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001864 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001865 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1866 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001867 // offset of __forwarding field
1868 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001869 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1870 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1871 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001872 // offset of x field
1873 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001874 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001875 }
1876
1877 // Create the descriptor for the variable.
1878 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001879 DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
1880 VD->getName(), Unit, Line, Ty,
1881 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001882 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001883 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001884 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001885
Chris Lattnerc6034632010-04-01 06:31:43 +00001886 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001887 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001888}
1889
Devang Pateld6c5a262010-02-01 21:52:22 +00001890void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001891 llvm::Value *Storage,
1892 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001893 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001894}
1895
Mike Stumpb1a6e682009-09-30 02:43:10 +00001896void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1897 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1898 CodeGenFunction *CGF) {
1899 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1900}
1901
Chris Lattner9c85ba32008-11-10 06:08:34 +00001902/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1903/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001904void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001905 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001906 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001907}
1908
1909
1910
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001911/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001912void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001913 const VarDecl *D) {
1914
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001915 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001916 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001917 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001918
Devang Pateleb6d79b2010-02-01 21:34:11 +00001919 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001920 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001922 // CodeGen turns int[] into int[1] so we'll do the same here.
1923 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001925 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001926 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Anders Carlsson20f12a22009-12-06 18:00:51 +00001928 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001929 ArrayType::Normal, 0);
1930 }
Devang Patel5d822f02010-04-29 17:48:37 +00001931 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001932 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001933 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001934 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001935 if (LinkageName == DeclName)
1936 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001937 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00001938 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +00001939 DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName,
1940 Unit, LineNo, getOrCreateType(T, Unit),
1941 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001942}
1943
Devang Patel9ca36b62009-02-26 21:10:26 +00001944/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001945void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001946 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001947 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001948 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001949 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001950
Devang Pateld6c5a262010-02-01 21:52:22 +00001951 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001952
Devang Pateld6c5a262010-02-01 21:52:22 +00001953 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001954 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Devang Patel9ca36b62009-02-26 21:10:26 +00001956 // CodeGen turns int[] into int[1] so we'll do the same here.
1957 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Devang Patel9ca36b62009-02-26 21:10:26 +00001959 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001960 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Anders Carlsson20f12a22009-12-06 18:00:51 +00001962 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001963 ArrayType::Normal, 0);
1964 }
1965
Devang Patel823d8e92010-12-08 22:42:58 +00001966 DBuilder.CreateGlobalVariable(Name, Unit, LineNo,
1967 getOrCreateType(T, Unit),
1968 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00001969}
Devang Patelabb485f2010-02-01 19:16:32 +00001970
Devang Patel25c2c8f2010-08-10 17:53:33 +00001971/// EmitGlobalVariable - Emit global variable's debug info.
1972void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001973 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001974 // Create the descriptor for the variable.
1975 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1976 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001977 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001978 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1979 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel31f7d022011-01-17 22:23:07 +00001980 Ty = CreateEnumType(ED);
Devang Patel6237cea2010-08-23 22:07:25 +00001981 }
Devang Patel0317ab02010-08-10 18:27:15 +00001982 // Do not use DIGlobalVariable for enums.
1983 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1984 return;
Devang Patel823d8e92010-12-08 22:42:58 +00001985 DBuilder.CreateStaticVariable(Unit, Name, Name, Unit,
1986 getLineNumber(VD->getLocation()),
1987 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001988}
1989
Devang Patelabb485f2010-02-01 19:16:32 +00001990/// getOrCreateNamesSpace - Return namespace descriptor for the given
1991/// namespace decl.
1992llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00001993CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00001994 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1995 NameSpaceCache.find(NSDecl);
1996 if (I != NameSpaceCache.end())
1997 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1998
Devang Patel8ab870d2010-05-12 23:46:38 +00001999 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002000 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002001 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002002 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002003 llvm::DINameSpace NS =
Devang Patel823d8e92010-12-08 22:42:58 +00002004 DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002005 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002006 return NS;
2007}