blob: 6526ee0f63288c7db3e5b1ad9c3724ebe0b24a74 [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"
John McCall6b5a61b2011-02-07 10:33:21 +000036#include "llvm/Target/TargetData.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000037#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000038using namespace clang;
39using namespace clang::CodeGen;
40
Anders Carlsson20f12a22009-12-06 18:00:51 +000041CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel823d8e92010-12-08 22:42:58 +000042 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000043 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000044 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000048 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000049}
50
Chris Lattner9c85ba32008-11-10 06:08:34 +000051void CGDebugInfo::setLocation(SourceLocation Loc) {
52 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000053 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000054}
55
Devang Patel33583052010-01-28 23:15:27 +000056/// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +000057llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000059 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000060
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +000068 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000072 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +000073 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +000074 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel170cef32010-12-09 00:33:05 +000077 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +000078}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000103 if (const ObjCImplementationDecl *OID =
104 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000105 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000106 } else if (const ObjCInterfaceDecl *OID =
107 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000108 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000109 } else if (const ObjCCategoryImplDecl *OCD =
110 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000111 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
112 OCD->getIdentifier()->getNameStart() << ')';
113 }
114 OS << ' ' << OMD->getSelector().getAsString() << ']';
115
116 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
117 memcpy(StrPtr, MethodName.begin(), OS.tell());
118 return llvm::StringRef(StrPtr, OS.tell());
119}
120
Devang Patel700a1cb2010-07-20 20:24:18 +0000121/// getClassName - Get class name including template argument list.
122llvm::StringRef
123CGDebugInfo::getClassName(RecordDecl *RD) {
124 ClassTemplateSpecializationDecl *Spec
125 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
126 if (!Spec)
127 return RD->getName();
128
129 const TemplateArgument *Args;
130 unsigned NumArgs;
131 std::string Buffer;
132 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
133 const TemplateSpecializationType *TST =
134 cast<TemplateSpecializationType>(TAW->getType());
135 Args = TST->getArgs();
136 NumArgs = TST->getNumArgs();
137 } else {
138 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000139 Args = TemplateArgs.data();
140 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000141 }
142 Buffer = RD->getIdentifier()->getNameStart();
143 PrintingPolicy Policy(CGM.getLangOptions());
144 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
145 NumArgs,
146 Policy);
147
148 // Copy this name on the side and use its reference.
149 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
150 memcpy(StrPtr, Buffer.data(), Buffer.length());
151 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000152}
153
Devang Patel17800552010-03-09 00:44:50 +0000154/// getOrCreateFile - Get the file debug info descriptor for the input location.
155llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000156 if (!Loc.isValid())
157 // If Location is not valid then use main input file.
158 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
159
Anders Carlsson20f12a22009-12-06 18:00:51 +0000160 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000161 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000162
Douglas Gregor8c457a82010-11-11 20:45:16 +0000163 if (PLoc.isInvalid())
164 // If the location is not valid then use main input file.
Devang Patel823d8e92010-12-08 22:42:58 +0000165 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000166
Ted Kremenek9c250392010-03-30 00:27:51 +0000167 // Cache the results.
168 const char *fname = PLoc.getFilename();
169 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
170 DIFileCache.find(fname);
171
172 if (it != DIFileCache.end()) {
173 // Verify that the information still exists.
174 if (&*it->second)
175 return llvm::DIFile(cast<llvm::MDNode>(it->second));
176 }
177
Devang Patel823d8e92010-12-08 22:42:58 +0000178 llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000179
Devang Patelab699792010-05-07 18:12:35 +0000180 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000181 return F;
182
Devang Patel17800552010-03-09 00:44:50 +0000183}
Devang Patel8ab870d2010-05-12 23:46:38 +0000184
Devang Patel532105f2010-10-28 22:03:20 +0000185/// getOrCreateMainFile - Get the file info for main compile unit.
186llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel823d8e92010-12-08 22:42:58 +0000187 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000188}
189
Devang Patel8ab870d2010-05-12 23:46:38 +0000190/// getLineNumber - Get line number for the location. If location is invalid
191/// then use current location.
192unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
193 assert (CurLoc.isValid() && "Invalid current location!");
194 SourceManager &SM = CGM.getContext().getSourceManager();
195 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000196 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000197}
198
199/// getColumnNumber - Get column number for the location. If location is
200/// invalid then use current location.
201unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
202 assert (CurLoc.isValid() && "Invalid current location!");
203 SourceManager &SM = CGM.getContext().getSourceManager();
204 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000205 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000206}
207
Devang Patelac4d13c2010-07-27 15:17:16 +0000208llvm::StringRef CGDebugInfo::getCurrentDirname() {
209 if (!CWDName.empty())
210 return CWDName;
211 char *CompDirnamePtr = NULL;
212 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
213 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
214 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
215 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
216}
217
Devang Patel17800552010-03-09 00:44:50 +0000218/// CreateCompileUnit - Create new compile unit.
219void CGDebugInfo::CreateCompileUnit() {
220
221 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000222 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000223 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
224 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000225 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000226
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000227 // The main file name provided via the "-main-file-name" option contains just
228 // the file name itself with no path information. This file name may have had
229 // a relative path, so we look into the actual file entry for the main
230 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000231 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000232 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000233 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000234 if (MainFileDir != ".")
235 MainFileName = MainFileDir + "/" + MainFileName;
236 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000237
Devang Patelac4d13c2010-07-27 15:17:16 +0000238 // Save filename string.
239 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
240 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
241 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
242
Chris Lattner515455a2009-03-25 03:28:08 +0000243 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000244 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000245 if (LO.CPlusPlus) {
246 if (LO.ObjC1)
247 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
248 else
249 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
250 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000251 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000252 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000253 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000254 } else {
255 LangTag = llvm::dwarf::DW_LANG_C89;
256 }
Devang Patel446c6192009-04-17 21:06:59 +0000257
Daniel Dunbar19f19832010-08-24 17:41:09 +0000258 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000259
260 // Figure out which version of the ObjC runtime we have.
261 unsigned RuntimeVers = 0;
262 if (LO.ObjC1)
263 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000265 // Create new compile unit.
Devang Patel823d8e92010-12-08 22:42:58 +0000266 DBuilder.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000267 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000268 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000269 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000270 // FIXME - Eliminate TheCU.
271 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000272}
273
Devang Patel65e99f22009-02-25 01:36:11 +0000274/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000275/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000276llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000277 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000278 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000279 switch (BT->getKind()) {
280 default:
281 case BuiltinType::Void:
282 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000283 case BuiltinType::ObjCClass:
Devang Patel823d8e92010-12-08 22:42:58 +0000284 return DBuilder.CreateStructType(TheCU, "objc_class",
285 getOrCreateMainFile(), 0, 0, 0,
286 llvm::DIDescriptor::FlagFwdDecl,
287 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000288 case BuiltinType::ObjCId: {
289 // typedef struct objc_class *Class;
290 // typedef struct objc_object {
291 // Class isa;
292 // } *id;
293
294 llvm::DIType OCTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000295 DBuilder.CreateStructType(TheCU, "objc_class",
296 getOrCreateMainFile(), 0, 0, 0,
297 llvm::DIDescriptor::FlagFwdDecl,
298 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000299 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
300
Devang Patel823d8e92010-12-08 22:42:58 +0000301 llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000302
Devang Patel823d8e92010-12-08 22:42:58 +0000303 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000304 llvm::DIType FieldTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000305 DBuilder.CreateMemberType("isa", getOrCreateMainFile(),
306 0,Size, 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000307 EltTys.push_back(FieldTy);
308 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +0000309 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelc8972c62010-07-28 01:33:15 +0000310
Devang Patel823d8e92010-12-08 22:42:58 +0000311 return DBuilder.CreateStructType(TheCU, "objc_object",
312 getOrCreateMainFile(),
313 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000314 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000315 case BuiltinType::UChar:
316 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
317 case BuiltinType::Char_S:
318 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
319 case BuiltinType::UShort:
320 case BuiltinType::UInt:
321 case BuiltinType::ULong:
322 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
323 case BuiltinType::Short:
324 case BuiltinType::Int:
325 case BuiltinType::Long:
326 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
327 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
328 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000329 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000330 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000331 }
Devang Patel05127ca2010-07-28 23:23:29 +0000332
333 switch (BT->getKind()) {
334 case BuiltinType::Long: BTName = "long int"; break;
335 case BuiltinType::LongLong: BTName = "long long int"; break;
336 case BuiltinType::ULong: BTName = "long unsigned int"; break;
337 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
338 default:
339 BTName = BT->getName(CGM.getContext().getLangOptions());
340 break;
341 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000342 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000343 uint64_t Size = CGM.getContext().getTypeSize(BT);
344 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000345 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000346 DBuilder.CreateBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000347 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000348}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000349
Devang Patel344ff5d2010-12-09 00:25:29 +0000350llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000351 // Bit size, align and offset of the type.
352 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
353 if (Ty->isComplexIntegerType())
354 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 uint64_t Size = CGM.getContext().getTypeSize(Ty);
357 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000358 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000359 DBuilder.CreateBasicType("complex", Size, Align, Encoding);
360
Devang Patelca80a5f2009-10-20 19:55:01 +0000361 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000362}
363
John McCalla1805292009-09-25 01:40:47 +0000364/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000365/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000366llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000367 QualifierCollector Qc;
368 const Type *T = Qc.strip(Ty);
369
370 // Ignore these qualifiers for now.
371 Qc.removeObjCGCAttr();
372 Qc.removeAddressSpace();
373
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374 // We will create one Derived type for one qualifier and recurse to handle any
375 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000377 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000379 Qc.removeConst();
380 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000382 Qc.removeVolatile();
383 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000385 Qc.removeRestrict();
386 } else {
387 assert(Qc.empty() && "Unknown type qualifier for debug info");
388 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
John McCall49f4e1c2010-12-10 11:01:00 +0000391 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000392
Daniel Dunbar3845f862008-10-31 03:54:29 +0000393 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
394 // CVR derived types.
Devang Patel823d8e92010-12-08 22:42:58 +0000395 llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy);
396
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000398}
399
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000400llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000401 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000402 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000403 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
404 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000406}
407
Chris Lattner9c85ba32008-11-10 06:08:34 +0000408llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000409 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000410 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
411 Ty->getPointeeType(), Unit);
412}
413
Devang Patelc69e1cf2010-09-30 19:05:55 +0000414/// CreatePointeeType - Create PointTee type. If Pointee is a record
415/// then emit record's fwd if debug info size reduction is enabled.
416llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
417 llvm::DIFile Unit) {
418 if (!CGM.getCodeGenOpts().LimitDebugInfo)
419 return getOrCreateType(PointeeTy, Unit);
420
421 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
422 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000423 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
424 unsigned Line = getLineNumber(RD->getLocation());
425 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000426 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000427
428 if (RD->isStruct())
429 return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit,
430 Line, 0, 0, llvm::DIType::FlagFwdDecl,
431 llvm::DIArray());
432 else if (RD->isUnion())
433 return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit,
434 Line, 0, 0, llvm::DIType::FlagFwdDecl,
435 llvm::DIArray());
436 else {
437 assert(RD->isClass() && "Unknown RecordType!");
438 return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit,
439 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
440 llvm::DIType(), llvm::DIArray());
441 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000442 }
443 return getOrCreateType(PointeeTy, Unit);
444
445}
446
Anders Carlssona031b352009-11-06 19:19:55 +0000447llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
448 const Type *Ty,
449 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000450 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000451
452 if (Tag == llvm::dwarf::DW_TAG_reference_type)
453 return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit));
454
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000455 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000456 // Size is always the size of a pointer. We can't use getTypeSize here
457 // because that does not return the correct value for references.
458 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000459 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
460 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Devang Patel823d8e92010-12-08 22:42:58 +0000462 return
463 DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000464}
465
Mike Stump9bc093c2009-05-14 02:03:51 +0000466llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000467 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000468 if (BlockLiteralGenericSet)
469 return BlockLiteralGeneric;
470
Devang Patel823d8e92010-12-08 22:42:58 +0000471 llvm::SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000473 QualType FType;
474 uint64_t FieldSize, FieldOffset;
475 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000476 llvm::DIArray Elements;
477 llvm::DIType EltTy, DescTy;
478
479 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000480 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000481 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
482 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000483
Devang Patel823d8e92010-12-08 22:42:58 +0000484 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000485 EltTys.clear();
486
Devang Patele2472482010-09-29 21:05:52 +0000487 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000488 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000489
Devang Patel823d8e92010-12-08 22:42:58 +0000490 EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor",
491 Unit, LineNo, FieldOffset, 0,
492 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Mike Stump9bc093c2009-05-14 02:03:51 +0000494 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000495 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Devang Patel823d8e92010-12-08 22:42:58 +0000497 DescTy = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000498
499 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000500 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000501 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000502 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000503 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
504 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000505 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000506 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000507
Anders Carlsson20f12a22009-12-06 18:00:51 +0000508 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000509 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000510 FieldSize = CGM.getContext().getTypeSize(Ty);
511 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel823d8e92010-12-08 22:42:58 +0000512 FieldTy = DBuilder.CreateMemberType("__descriptor", Unit,
513 LineNo, FieldSize, FieldAlign,
514 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000515 EltTys.push_back(FieldTy);
516
517 FieldOffset += FieldSize;
Devang Patel823d8e92010-12-08 22:42:58 +0000518 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000519
Devang Patel823d8e92010-12-08 22:42:58 +0000520 EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic",
521 Unit, LineNo, FieldOffset, 0,
522 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Mike Stump9bc093c2009-05-14 02:03:51 +0000524 BlockLiteralGenericSet = true;
Devang Patel823d8e92010-12-08 22:42:58 +0000525 BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000526 return BlockLiteralGeneric;
527}
528
Chris Lattner9c85ba32008-11-10 06:08:34 +0000529llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000530 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000531 // Typedefs are derived from some other type. If we have a typedef of a
532 // typedef, make sure to emit the whole chain.
533 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000534 if (!Src.Verify())
535 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536 // We don't set size information, but do specify where the typedef was
537 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000538 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +0000539 llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(),
540 Unit, Line);
Devang Patelca80a5f2009-10-20 19:55:01 +0000541 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000542}
543
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000545 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000546 llvm::SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // Add the result type at least.
549 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551 // Set up remainder of arguments if there is a prototype.
552 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000553 if (isa<FunctionNoProtoType>(Ty))
Devang Patel823d8e92010-12-08 22:42:58 +0000554 EltTys.push_back(DBuilder.CreateUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000555 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000556 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
557 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000558 }
559
Chris Lattner9c85ba32008-11-10 06:08:34 +0000560 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000561 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Devang Patel823d8e92010-12-08 22:42:58 +0000563 llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000564 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000565}
566
Devang Patel428deb52010-01-19 00:00:59 +0000567/// CollectRecordFields - A helper function to collect debug info for
568/// record fields. This is used while creating debug info entry for a Record.
569void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000570CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000571 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel428deb52010-01-19 00:00:59 +0000572 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000573 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
574 for (RecordDecl::field_iterator I = RD->field_begin(),
575 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000576 I != E; ++I, ++FieldNo) {
577 FieldDecl *Field = *I;
578 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000579 llvm::StringRef FieldName = Field->getName();
580
Devang Patel4835fdd2010-02-12 01:31:06 +0000581 // Ignore unnamed fields. Do not ignore unnamed records.
582 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000583 continue;
584
585 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000586 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
587 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000588 QualType FType = Field->getType();
589 uint64_t FieldSize = 0;
590 unsigned FieldAlign = 0;
591 if (!FType->isIncompleteArrayType()) {
592
593 // Bit size, align and offset of the type.
594 FieldSize = CGM.getContext().getTypeSize(FType);
595 Expr *BitWidth = Field->getBitWidth();
596 if (BitWidth)
597 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000598 FieldAlign = CGM.getContext().getTypeAlign(FType);
599 }
600
601 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
602
Devang Patel71f4ff62010-04-21 23:12:37 +0000603 unsigned Flags = 0;
604 AccessSpecifier Access = I->getAccess();
605 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000606 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000607 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000608 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000609
Devang Patel823d8e92010-12-08 22:42:58 +0000610 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
611 FieldLine, FieldSize, FieldAlign,
612 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000613 EltTys.push_back(FieldTy);
614 }
615}
616
Devang Patela6da1922010-01-28 00:28:01 +0000617/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
618/// function type is not updated to include implicit "this" pointer. Use this
619/// routine to get a method type which includes "this" pointer.
620llvm::DIType
621CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000622 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000623 llvm::DIType FnTy
624 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
625 0),
626 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000627
Devang Patela6da1922010-01-28 00:28:01 +0000628 // Add "this" pointer.
629
Devang Patelab699792010-05-07 18:12:35 +0000630 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000631 assert (Args.getNumElements() && "Invalid number of arguments!");
632
Devang Patel823d8e92010-12-08 22:42:58 +0000633 llvm::SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000634
635 // First element is always return type. For 'void' functions it is NULL.
636 Elts.push_back(Args.getElement(0));
637
Devang Patel2ed8f002010-08-27 17:47:47 +0000638 if (!Method->isStatic())
639 {
640 // "this" pointer is always first argument.
641 ASTContext &Context = CGM.getContext();
642 QualType ThisPtr =
643 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
644 llvm::DIType ThisPtrType =
Devang Patel823d8e92010-12-08 22:42:58 +0000645 DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000646
Devang Patel2ed8f002010-08-27 17:47:47 +0000647 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
648 Elts.push_back(ThisPtrType);
649 }
Devang Patela6da1922010-01-28 00:28:01 +0000650
651 // Copy rest of the arguments.
652 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
653 Elts.push_back(Args.getElement(i));
654
655 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000656 DBuilder.GetOrCreateArray(Elts.data(), Elts.size());
Devang Patela6da1922010-01-28 00:28:01 +0000657
Devang Patel823d8e92010-12-08 22:42:58 +0000658 return DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000659}
660
Devang Patel58faf202010-10-22 17:11:50 +0000661/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
662/// inside a function.
663static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
664 if (const CXXRecordDecl *NRD =
665 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
666 return isFunctionLocalClass(NRD);
667 else if (isa<FunctionDecl>(RD->getDeclContext()))
668 return true;
669 return false;
670
671}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000672/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
673/// a single member function GlobalDecl.
674llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000675CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000676 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000677 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000678 bool IsCtorOrDtor =
679 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
680
681 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000682 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000683
684 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
685 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000686 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000687 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000688 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000689
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000690 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000691 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
692 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000693
694 // Collect virtual method info.
695 llvm::DIType ContainingType;
696 unsigned Virtuality = 0;
697 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000698
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000699 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000700 if (Method->isPure())
701 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
702 else
703 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
704
705 // It doesn't make sense to give a virtual destructor a vtable index,
706 // since a single destructor has two entries in the vtable.
707 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000708 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000709 ContainingType = RecordTy;
710 }
711
Devang Patele2472482010-09-29 21:05:52 +0000712 unsigned Flags = 0;
713 if (Method->isImplicit())
714 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000715 AccessSpecifier Access = Method->getAccess();
716 if (Access == clang::AS_private)
717 Flags |= llvm::DIDescriptor::FlagPrivate;
718 else if (Access == clang::AS_protected)
719 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000720 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
721 if (CXXC->isExplicit())
722 Flags |= llvm::DIDescriptor::FlagExplicit;
723 } else if (const CXXConversionDecl *CXXC =
724 dyn_cast<CXXConversionDecl>(Method)) {
725 if (CXXC->isExplicit())
726 Flags |= llvm::DIDescriptor::FlagExplicit;
727 }
Devang Patel3951e712010-10-07 22:03:49 +0000728 if (Method->hasPrototype())
729 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000730
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000731 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +0000732 DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName,
733 MethodDefUnit, MethodLine,
734 MethodTy, /*isLocalToUnit=*/false,
735 /* isDefinition=*/ false,
736 Virtuality, VIndex, ContainingType,
737 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000738
739 // Don't cache ctors or dtors since we have to emit multiple functions for
740 // a single ctor or dtor.
741 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000742 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000743
744 return SP;
745}
746
Devang Patel4125fd22010-01-19 01:54:44 +0000747/// CollectCXXMemberFunctions - A helper function to collect debug info for
748/// C++ member functions.This is used while creating debug info entry for
749/// a Record.
750void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000751CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000752 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000753 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000754 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
755 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000756 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000757
Devang Pateld5322da2010-02-09 19:09:28 +0000758 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000759 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000760
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000761 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000762 }
763}
764
Devang Patel2ed8f002010-08-27 17:47:47 +0000765/// CollectCXXFriends - A helper function to collect debug info for
766/// C++ base classes. This is used while creating debug info entry for
767/// a Record.
768void CGDebugInfo::
769CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000770 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000771 llvm::DIType RecordTy) {
772
773 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
774 BE = RD->friend_end(); BI != BE; ++BI) {
Devang Patel823d8e92010-12-08 22:42:58 +0000775 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
776 EltTys.push_back(DBuilder.CreateFriend(RecordTy,
777 getOrCreateType(TInfo->getType(),
778 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000779 }
780}
781
Devang Patela245c5b2010-01-25 23:32:18 +0000782/// CollectCXXBases - A helper function to collect debug info for
783/// C++ base classes. This is used while creating debug info entry for
784/// a Record.
785void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000786CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000787 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000788 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000789
Devang Patel239cec62010-02-01 21:39:52 +0000790 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
791 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
792 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000793 unsigned BFlags = 0;
794 uint64_t BaseOffset;
795
796 const CXXRecordDecl *Base =
797 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
798
799 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000800 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000801 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000802 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000803 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000804 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000805 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000806
807 AccessSpecifier Access = BI->getAccessSpecifier();
808 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000809 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000810 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000811 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000812
Devang Patel823d8e92010-12-08 22:42:58 +0000813 llvm::DIType DTy =
814 DBuilder.CreateInheritance(RecordTy,
815 getOrCreateType(BI->getType(), Unit),
816 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000817 EltTys.push_back(DTy);
818 }
Devang Patela245c5b2010-01-25 23:32:18 +0000819}
820
Devang Patel4ce3f202010-01-28 18:11:52 +0000821/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000822llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000823 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000824 return VTablePtrType;
825
826 ASTContext &Context = CGM.getContext();
827
828 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000829 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
830 llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1);
831 llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000832 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000833 llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0,
834 "__vtbl_ptr_type");
835 VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000836 return VTablePtrType;
837}
838
Anders Carlsson046c2942010-04-17 20:15:18 +0000839/// getVTableName - Get vtable name for the given Class.
840llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000841 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000842 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000843
844 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000845 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000846 memcpy(StrPtr, Name.data(), Name.length());
847 return llvm::StringRef(StrPtr, Name.length());
848}
849
850
Anders Carlsson046c2942010-04-17 20:15:18 +0000851/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000852/// debug info entry in EltTys vector.
853void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000854CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000855 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000856 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000857
858 // If there is a primary base then it will hold vtable info.
859 if (RL.getPrimaryBase())
860 return;
861
862 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000863 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000864 return;
865
866 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
867 llvm::DIType VPTR
Devang Patel823d8e92010-12-08 22:42:58 +0000868 = DBuilder.CreateMemberType(getVTableName(RD), Unit,
869 0, Size, 0, 0, 0,
870 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000871 EltTys.push_back(VPTR);
872}
873
Devang Patelc69e1cf2010-09-30 19:05:55 +0000874/// getOrCreateRecordType - Emit record type's standalone debug info.
875llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
876 SourceLocation Loc) {
877 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel823d8e92010-12-08 22:42:58 +0000878 DBuilder.RetainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000879 return T;
880}
881
Devang Patel65e99f22009-02-25 01:36:11 +0000882/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +0000883llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000884 RecordDecl *RD = Ty->getDecl();
Devang Patel31f7d022011-01-17 22:23:07 +0000885 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner9c85ba32008-11-10 06:08:34 +0000887 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000888 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
889 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Chris Lattner9c85ba32008-11-10 06:08:34 +0000891 // Records and classes and unions can all be recursive. To handle them, we
892 // first generate a debug descriptor for the struct as a forward declaration.
893 // Then (if it is a definition) we go through and get debug info for all of
894 // its members. Finally, we create a descriptor for the complete type (which
895 // may refer to the forward decl if the struct is recursive) and replace all
896 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000897 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000898 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel0b897992010-07-08 19:56:29 +0000899
900 // If this is just a forward declaration, construct an appropriately
901 // marked node and just return it.
902 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +0000903 llvm::DIType FwdDecl =
904 DBuilder.CreateStructType(FDContext, RD->getName(),
905 DefUnit, Line, 0, 0,
906 llvm::DIDescriptor::FlagFwdDecl,
907 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +0000908
909 return FwdDecl;
910 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000911
Devang Patel823d8e92010-12-08 22:42:58 +0000912 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Devang Patelab699792010-05-07 18:12:35 +0000914 llvm::MDNode *MN = FwdDecl;
915 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000916 // Otherwise, insert it into the TypeCache so that recursive uses will find
917 // it.
Devang Patelab699792010-05-07 18:12:35 +0000918 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000919 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000920 RegionStack.push_back(FwdDeclNode);
921 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000922
923 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +0000924 llvm::SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000925
Devang Pateld6c5a262010-02-01 21:52:22 +0000926 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000927 if (CXXDecl) {
928 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000929 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000930 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000931
932 // Collect static variables with initializers.
933 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
934 I != E; ++I)
935 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
936 if (const Expr *Init = V->getInit()) {
937 Expr::EvalResult Result;
938 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
939 llvm::ConstantInt *CI
940 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
941
942 // Create the descriptor for static variable.
943 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
944 llvm::StringRef VName = V->getName();
945 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
946 // Do not use DIGlobalVariable for enums.
947 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel823d8e92010-12-08 22:42:58 +0000948 DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit,
949 getLineNumber(V->getLocation()),
950 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +0000951 }
952 }
953 }
954 }
955
Devang Pateld6c5a262010-02-01 21:52:22 +0000956 CollectRecordFields(RD, Unit, EltTys);
Devang Patelfa275df2011-02-02 21:38:49 +0000957 llvm::SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel4ce3f202010-01-28 18:11:52 +0000958 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000959 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000960 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelfa275df2011-02-02 21:38:49 +0000961 if (ClassTemplateSpecializationDecl *TSpecial
962 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
963 const TemplateArgumentList &TAL = TSpecial->getTemplateArgs();
964 for (unsigned i = 0, e = TAL.size(); i != e; ++i) {
965 const TemplateArgument &TA = TAL[i];
966 if (TA.getKind() == TemplateArgument::Type) {
967 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
968 llvm::DITemplateTypeParameter TTP =
969 DBuilder.CreateTemplateTypeParameter(TheCU, TTy.getName(), TTy);
970 TemplateParams.push_back(TTP);
Devang Patel0ce34c62011-02-02 22:36:18 +0000971 } else if (TA.getKind() == TemplateArgument::Integral) {
972 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
973 // FIXME: Get parameter name, instead of parameter type name.
974 llvm::DITemplateValueParameter TVP =
975 DBuilder.CreateTemplateValueParameter(TheCU, TTy.getName(), TTy,
976 TA.getAsIntegral()->getZExtValue());
977 TemplateParams.push_back(TVP);
Devang Patelfa275df2011-02-02 21:38:49 +0000978 }
979 }
980 }
Devang Patel823d8e92010-12-08 22:42:58 +0000981 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000982
Devang Patel823d8e92010-12-08 22:42:58 +0000983 RegionStack.pop_back();
984 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
985 RegionMap.find(Ty->getDecl());
986 if (RI != RegionMap.end())
987 RegionMap.erase(RI);
988
989 llvm::DIDescriptor RDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000990 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000991 llvm::StringRef RDName = RD->getName();
992 uint64_t Size = CGM.getContext().getTypeSize(Ty);
993 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
994 llvm::DIArray Elements =
995 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
996 llvm::MDNode *RealDecl = NULL;
997
998 if (RD->isStruct())
999 RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line,
1000 Size, Align, 0, Elements);
1001 else if (RD->isUnion())
1002 RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line,
1003 Size, Align, 0, Elements);
1004 else {
1005 assert(RD->isClass() && "Unknown RecordType!");
1006 RDName = getClassName(RD);
1007 // A class's primary base or the class itself contains the vtable.
1008 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +00001009 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001010 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1011 // Seek non virtual primary base root.
1012 while (1) {
1013 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1014 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001015 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001016 PBase = PBT;
1017 else
1018 break;
1019 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001020 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001021 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001022 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001023 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001024 ContainingType = FwdDecl;
Devang Patelfa275df2011-02-02 21:38:49 +00001025 llvm::DIArray TParamsArray =
1026 DBuilder.GetOrCreateArray(TemplateParams.data(), TemplateParams.size());
Devang Patel823d8e92010-12-08 22:42:58 +00001027 RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line,
1028 Size, Align, 0, 0, llvm::DIType(),
Devang Patelfa275df2011-02-02 21:38:49 +00001029 Elements, ContainingType,
1030 TParamsArray);
Devang Patela245c5b2010-01-25 23:32:18 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattner9c85ba32008-11-10 06:08:34 +00001033 // Now that we have a real decl for the struct, replace anything using the
1034 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001035 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001036 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001037 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001038}
1039
John McCallc12c5bb2010-05-15 11:32:37 +00001040/// CreateType - get objective-c object type.
1041llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1042 llvm::DIFile Unit) {
1043 // Ignore protocols.
1044 return getOrCreateType(Ty->getBaseType(), Unit);
1045}
1046
Devang Patel9ca36b62009-02-26 21:10:26 +00001047/// CreateType - get objective-c interface type.
1048llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001049 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001050 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001051 if (!ID)
1052 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001053
1054 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001055 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001056 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001057 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001058
Dan Gohman45f7c782010-08-23 21:15:56 +00001059 // If this is just a forward declaration, return a special forward-declaration
1060 // debug type.
1061 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001062 llvm::DIType FwdDecl =
1063 DBuilder.CreateStructType(Unit, ID->getName(),
1064 DefUnit, Line, 0, 0, 0,
1065 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001066 return FwdDecl;
1067 }
1068
Devang Patel9ca36b62009-02-26 21:10:26 +00001069 // To handle recursive interface, we
1070 // first generate a debug descriptor for the struct as a forward declaration.
1071 // Then (if it is a definition) we go through and get debug info for all of
1072 // its members. Finally, we create a descriptor for the complete type (which
1073 // may refer to the forward decl if the struct is recursive) and replace all
1074 // uses of the forward declaration with the final definition.
Devang Patel823d8e92010-12-08 22:42:58 +00001075 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Devang Patelab699792010-05-07 18:12:35 +00001077 llvm::MDNode *MN = FwdDecl;
1078 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001079 // Otherwise, insert it into the TypeCache so that recursive uses will find
1080 // it.
Devang Patelab699792010-05-07 18:12:35 +00001081 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001082 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001083 RegionStack.push_back(FwdDeclNode);
1084 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001085
1086 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +00001087 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001088
Devang Pateld6c5a262010-02-01 21:52:22 +00001089 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001090 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001091 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001093 if (!SClassTy.isValid())
1094 return llvm::DIType();
1095
Mike Stump1eb44332009-09-09 15:08:12 +00001096 llvm::DIType InhTag =
Devang Patel823d8e92010-12-08 22:42:58 +00001097 DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001098 EltTys.push_back(InhTag);
1099 }
1100
Devang Pateld6c5a262010-02-01 21:52:22 +00001101 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001102
1103 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001104 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001105 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001106 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001107 if (!FieldTy.isValid())
1108 return llvm::DIType();
1109
Devang Patel73621622009-11-25 17:37:31 +00001110 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001111
Devang Patelde135022009-04-27 22:40:36 +00001112 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001113 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001114 continue;
1115
Devang Patel9ca36b62009-02-26 21:10:26 +00001116 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001117 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1118 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001119 QualType FType = Field->getType();
1120 uint64_t FieldSize = 0;
1121 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001122
Devang Patel99c20eb2009-03-20 18:24:39 +00001123 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Devang Patel99c20eb2009-03-20 18:24:39 +00001125 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001126 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001127 Expr *BitWidth = Field->getBitWidth();
1128 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001129 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001130
Anders Carlsson20f12a22009-12-06 18:00:51 +00001131 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001132 }
1133
Mike Stump1eb44332009-09-09 15:08:12 +00001134 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1135
Devang Patelc20482b2009-03-19 00:23:53 +00001136 unsigned Flags = 0;
1137 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001138 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001139 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001140 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Devang Patel823d8e92010-12-08 22:42:58 +00001142 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
1143 FieldLine, FieldSize, FieldAlign,
1144 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001145 EltTys.push_back(FieldTy);
1146 }
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Devang Patel9ca36b62009-02-26 21:10:26 +00001148 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001149 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001150
Devang Patele4c1ea02010-03-11 20:01:48 +00001151 RegionStack.pop_back();
1152 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1153 RegionMap.find(Ty->getDecl());
1154 if (RI != RegionMap.end())
1155 RegionMap.erase(RI);
1156
Devang Patel9ca36b62009-02-26 21:10:26 +00001157 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001158 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1159 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Devang Patel823d8e92010-12-08 22:42:58 +00001161 llvm::DIType RealDecl =
1162 DBuilder.CreateStructType(Unit, ID->getName(), DefUnit,
1163 Line, Size, Align, 0,
1164 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001165
1166 // Now that we have a real decl for the struct, replace anything using the
1167 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001168 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001169 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001170
Devang Patel9ca36b62009-02-26 21:10:26 +00001171 return RealDecl;
1172}
1173
Devang Patel31f7d022011-01-17 22:23:07 +00001174llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001175 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001176 return CreateType(RT);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001177 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001178 return CreateEnumType(ET->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Chris Lattner9c85ba32008-11-10 06:08:34 +00001180 return llvm::DIType();
1181}
1182
Devang Patel70c23cd2010-02-23 22:59:39 +00001183llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001184 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001185 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1186 uint64_t NumElems = Ty->getNumElements();
1187 if (NumElems > 0)
1188 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001189
Devang Patel823d8e92010-12-08 22:42:58 +00001190 llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems);
1191 llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001192
1193 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1194 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1195
1196 return
Devang Patel823d8e92010-12-08 22:42:58 +00001197 DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001198}
1199
Chris Lattner9c85ba32008-11-10 06:08:34 +00001200llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001201 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001202 uint64_t Size;
1203 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
1205
Nuno Lopes010d5142009-01-28 00:35:17 +00001206 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001207 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001208 Size = 0;
1209 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001210 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001211 } else if (Ty->isIncompleteArrayType()) {
1212 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001213 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001214 } else {
1215 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001216 Size = CGM.getContext().getTypeSize(Ty);
1217 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001218 }
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Chris Lattner9c85ba32008-11-10 06:08:34 +00001220 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1221 // interior arrays, do we care? Why aren't nested arrays represented the
1222 // obvious/recursive way?
Devang Patel823d8e92010-12-08 22:42:58 +00001223 llvm::SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001224 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001225 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001226 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001227 else {
1228 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1229 uint64_t Upper = 0;
1230 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1231 if (CAT->getSize().getZExtValue())
1232 Upper = CAT->getSize().getZExtValue() - 1;
1233 // FIXME: Verify this is right for VLAs.
Devang Patel823d8e92010-12-08 22:42:58 +00001234 Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper));
Devang Patelcdf523c2010-10-06 18:30:00 +00001235 EltTy = Ty->getElementType();
1236 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001237 }
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Chris Lattner9c85ba32008-11-10 06:08:34 +00001239 llvm::DIArray SubscriptArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001240 DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241
Devang Patelca80a5f2009-10-20 19:55:01 +00001242 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001243 DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1244 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001245 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001246}
1247
Anders Carlssona031b352009-11-06 19:19:55 +00001248llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001249 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001250 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1251 Ty, Ty->getPointeeType(), Unit);
1252}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001253
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001254llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1255 llvm::DIFile Unit) {
1256 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1257 Ty, Ty->getPointeeType(), Unit);
1258}
1259
Anders Carlsson20f12a22009-12-06 18:00:51 +00001260llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001261 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001262 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1263 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1264
1265 if (!Ty->getPointeeType()->isFunctionType()) {
1266 // We have a data member pointer type.
1267 return PointerDiffDITy;
1268 }
1269
1270 // We have a member function pointer type. Treat it as a struct with two
1271 // ptrdiff_t members.
1272 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1273
1274 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001275 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001276
1277 // FIXME: This should probably be a function type instead.
1278 ElementTypes[0] =
Devang Patel823d8e92010-12-08 22:42:58 +00001279 DBuilder.CreateMemberType("ptr", U, 0,
1280 Info.first, Info.second, FieldOffset, 0,
1281 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001282 FieldOffset += Info.first;
1283
1284 ElementTypes[1] =
Devang Patel823d8e92010-12-08 22:42:58 +00001285 DBuilder.CreateMemberType("ptr", U, 0,
1286 Info.first, Info.second, FieldOffset, 0,
1287 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001288
1289 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001290 DBuilder.GetOrCreateArray(&ElementTypes[0],
1291 llvm::array_lengthof(ElementTypes));
Anders Carlsson20f12a22009-12-06 18:00:51 +00001292
Devang Patel823d8e92010-12-08 22:42:58 +00001293 return DBuilder.CreateStructType(U, llvm::StringRef("test"),
1294 U, 0, FieldOffset,
1295 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001296}
1297
Devang Patel6237cea2010-08-23 22:07:25 +00001298/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001299llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1300 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +00001301 llvm::SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001302
1303 // Create DIEnumerator elements for each enumerator.
1304 for (EnumDecl::enumerator_iterator
1305 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1306 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001307 Enumerators.push_back(
1308 DBuilder.CreateEnumerator(Enum->getName(),
1309 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001310 }
1311
1312 // Return a CompositeType for the enum itself.
1313 llvm::DIArray EltArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001314 DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Devang Patel6237cea2010-08-23 22:07:25 +00001315
1316 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1317 unsigned Line = getLineNumber(ED->getLocation());
1318 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001319 uint64_t Align = 0;
1320 if (!ED->getTypeForDecl()->isIncompleteType()) {
1321 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1322 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1323 }
Devang Patel4bc48872010-10-27 23:23:58 +00001324 llvm::DIDescriptor EnumContext =
Devang Patel170cef32010-12-09 00:33:05 +00001325 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()));
Devang Patel6237cea2010-08-23 22:07:25 +00001326 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001327 DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1328 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001329 return DbgTy;
1330}
1331
Douglas Gregor840943d2009-12-21 20:18:30 +00001332static QualType UnwrapTypeForDebugInfo(QualType T) {
1333 do {
1334 QualType LastT = T;
1335 switch (T->getTypeClass()) {
1336 default:
1337 return T;
1338 case Type::TemplateSpecialization:
1339 T = cast<TemplateSpecializationType>(T)->desugar();
1340 break;
John McCallf4c73712011-01-19 06:33:43 +00001341 case Type::TypeOfExpr:
1342 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001343 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001344 case Type::TypeOf:
1345 T = cast<TypeOfType>(T)->getUnderlyingType();
1346 break;
1347 case Type::Decltype:
1348 T = cast<DecltypeType>(T)->getUnderlyingType();
1349 break;
John McCall9d156a72011-01-06 01:58:22 +00001350 case Type::Attributed:
1351 T = cast<AttributedType>(T)->getEquivalentType();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001352 case Type::Elaborated:
1353 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001354 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001355 case Type::Paren:
1356 T = cast<ParenType>(T)->getInnerType();
1357 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001358 case Type::SubstTemplateTypeParm:
1359 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1360 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001361 }
1362
1363 assert(T != LastT && "Type unwrapping failed to unwrap!");
1364 if (T == LastT)
1365 return T;
1366 } while (true);
1367
1368 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001369}
1370
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001371/// getOrCreateType - Get the type from the cache or create a new
1372/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001373llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001374 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001375 if (Ty.isNull())
1376 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001377
Douglas Gregor840943d2009-12-21 20:18:30 +00001378 // Unwrap the type as needed for debug information.
1379 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001380
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001381 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001382 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001383 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001384 if (it != TypeCache.end()) {
1385 // Verify that the debug info still exists.
1386 if (&*it->second)
1387 return llvm::DIType(cast<llvm::MDNode>(it->second));
1388 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001389
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001390 // Otherwise create the type.
1391 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001392
1393 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001394 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001395 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001396}
1397
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001398/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001399llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001400 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001401 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001402 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001403 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001404
Douglas Gregor2101a822009-12-21 19:57:21 +00001405 const char *Diag = 0;
1406
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001407 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001408 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001409#define TYPE(Class, Base)
1410#define ABSTRACT_TYPE(Class, Base)
1411#define NON_CANONICAL_TYPE(Class, Base)
1412#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1413#include "clang/AST/TypeNodes.def"
1414 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001415
Anders Carlssonbfe69952009-11-06 18:24:04 +00001416 // FIXME: Handle these.
1417 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001418 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001419
1420 case Type::Vector:
1421 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001422 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001423 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001424 case Type::ObjCObject:
1425 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001426 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001427 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001428 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001429 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001430 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001431 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001432 return CreateType(cast<BlockPointerType>(Ty), Unit);
1433 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001434 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001435 case Type::Enum:
Devang Patel31f7d022011-01-17 22:23:07 +00001436 return CreateType(cast<TagType>(Ty));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001437 case Type::FunctionProto:
1438 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001439 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001440 case Type::ConstantArray:
1441 case Type::VariableArray:
1442 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001443 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001444
1445 case Type::LValueReference:
1446 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001447 case Type::RValueReference:
1448 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001449
Anders Carlsson20f12a22009-12-06 18:00:51 +00001450 case Type::MemberPointer:
1451 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001452
John McCall9d156a72011-01-06 01:58:22 +00001453 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001454 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001455 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001456 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001457 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001458 case Type::TypeOfExpr:
1459 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001460 case Type::Decltype:
1461 llvm_unreachable("type should have been unwrapped!");
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001462 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001463 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001464
1465 assert(Diag && "Fall through without a diagnostic?");
1466 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1467 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001468 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001469 << Diag;
1470 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001471}
1472
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001473/// CreateMemberType - Create new member and increase Offset by FType's size.
1474llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1475 llvm::StringRef Name,
1476 uint64_t *Offset) {
1477 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1478 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1479 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel823d8e92010-12-08 22:42:58 +00001480 llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0,
1481 FieldSize, FieldAlign,
1482 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001483 *Offset += FieldSize;
1484 return Ty;
1485}
1486
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001487/// EmitFunctionStart - Constructs the debug code for entering a function -
1488/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001489void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001490 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001491 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Devang Patel9c6c3a02010-01-14 00:36:21 +00001493 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001494 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001495
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001496 FnBeginRegionCount.push_back(RegionStack.size());
1497
Devang Patel9c6c3a02010-01-14 00:36:21 +00001498 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001499 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001500 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1501 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001502 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001503 // If there is a DISubprogram for this function available then use it.
1504 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1505 FI = SPCache.find(FD);
1506 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001507 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001508 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1509 llvm::MDNode *SPN = SP;
1510 RegionStack.push_back(SPN);
1511 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001512 return;
1513 }
1514 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001515 Name = getFunctionName(FD);
1516 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001517 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001518 if (LinkageName == Name)
1519 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001520 if (FD->hasPrototype())
1521 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001522 if (const NamespaceDecl *NSDecl =
1523 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel170cef32010-12-09 00:33:05 +00001524 FDContext = getOrCreateNameSpace(NSDecl);
David Chisnall70b9b442010-09-02 17:16:32 +00001525 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001526 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001527 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001528 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001529 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001530 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001531 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001532 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001533 if (!Name.empty() && Name[0] == '\01')
1534 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001535
Devang Patel970c6182010-04-24 00:49:16 +00001536 // It is expected that CurLoc is set before using EmitFunctionStart.
1537 // Usually, CurLoc points to the left bracket location of compound
1538 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001539 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001540 if (D->isImplicit())
1541 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001542 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +00001543 DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit,
1544 LineNo, getOrCreateType(FnType, Unit),
1545 Fn->hasInternalLinkage(), true/*definition*/,
1546 Flags, CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001548 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001549 llvm::MDNode *SPN = SP;
1550 RegionStack.push_back(SPN);
1551 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001552
1553 // Clear stack used to keep track of #line directives.
1554 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001555}
1556
1557
Devang Patel4d939e62010-07-20 22:20:10 +00001558void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001559 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001561 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001562 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001563 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001564 || (SM.getInstantiationLineNumber(CurLoc) ==
1565 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001566 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001567 // New Builder may not be in sync with CGDebugInfo.
1568 if (!Builder.getCurrentDebugLocation().isUnknown())
1569 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001570
1571 // Update last state.
1572 PrevLoc = CurLoc;
1573
Chris Lattnerc6034632010-04-01 06:31:43 +00001574 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001575 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1576 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001577 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001578}
1579
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001580/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1581/// has introduced scope change.
1582void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1583 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1584 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1585 return;
1586 SourceManager &SM = CGM.getContext().getSourceManager();
1587 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1588 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1589
Douglas Gregor8c457a82010-11-11 20:45:16 +00001590 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1591 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001592 return;
1593
1594 // If #line directive stack is empty then we are entering a new scope.
1595 if (LineDirectiveFiles.empty()) {
1596 EmitRegionStart(Builder);
1597 LineDirectiveFiles.push_back(PCLoc.getFilename());
1598 return;
1599 }
1600
1601 assert (RegionStack.size() >= LineDirectiveFiles.size()
1602 && "error handling #line regions!");
1603
1604 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001605 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001606 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1607 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001608 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001609 SeenThisFile = true;
1610 break;
1611 }
1612
1613 // If #line for this file is seen earlier then pop out #line regions.
1614 if (SeenThisFile) {
1615 while (!LineDirectiveFiles.empty()) {
1616 const char *LastFile = LineDirectiveFiles.back();
1617 RegionStack.pop_back();
1618 LineDirectiveFiles.pop_back();
1619 if (!strcmp(PPLoc.getFilename(), LastFile))
1620 break;
1621 }
1622 return;
1623 }
1624
1625 // .. otherwise insert new #line region.
1626 EmitRegionStart(Builder);
1627 LineDirectiveFiles.push_back(PCLoc.getFilename());
1628
1629 return;
1630}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001631/// EmitRegionStart- Constructs the debug code for entering a declarative
1632/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001633void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001634 llvm::DIDescriptor D =
Devang Patel823d8e92010-12-08 22:42:58 +00001635 DBuilder.CreateLexicalBlock(RegionStack.empty() ?
1636 llvm::DIDescriptor() :
1637 llvm::DIDescriptor(RegionStack.back()),
1638 getOrCreateFile(CurLoc),
1639 getLineNumber(CurLoc),
1640 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001641 llvm::MDNode *DN = D;
1642 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001643}
1644
1645/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1646/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001647void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001648 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1649
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001650 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001651 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001653 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001654}
1655
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001656/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1657void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1658 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1659 unsigned RCount = FnBeginRegionCount.back();
1660 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1661
1662 // Pop all regions for this function.
1663 while (RegionStack.size() != RCount)
1664 EmitRegionEnd(Builder);
1665 FnBeginRegionCount.pop_back();
1666}
1667
Devang Patel809b9bb2010-02-10 18:49:08 +00001668// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1669// See BuildByRefType.
1670llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1671 uint64_t *XOffset) {
1672
Devang Patel823d8e92010-12-08 22:42:58 +00001673 llvm::SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001674 QualType FType;
1675 uint64_t FieldSize, FieldOffset;
1676 unsigned FieldAlign;
1677
Devang Patel17800552010-03-09 00:44:50 +00001678 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001679 QualType Type = VD->getType();
1680
1681 FieldOffset = 0;
1682 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001683 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1684 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001685 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001686 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1687 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1688
John McCall6b5a61b2011-02-07 10:33:21 +00001689 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00001690 if (HasCopyAndDispose) {
1691 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001692 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1693 &FieldOffset));
1694 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1695 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001696 }
1697
1698 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1699 if (Align > CharUnits::fromQuantity(
1700 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1701 unsigned AlignedOffsetInBytes
1702 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1703 unsigned NumPaddingBytes
1704 = AlignedOffsetInBytes - FieldOffset/8;
1705
1706 if (NumPaddingBytes > 0) {
1707 llvm::APInt pad(32, NumPaddingBytes);
1708 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1709 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001710 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001711 }
1712 }
1713
1714 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001715 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001716 FieldSize = CGM.getContext().getTypeSize(FType);
1717 FieldAlign = Align.getQuantity()*8;
1718
1719 *XOffset = FieldOffset;
Devang Patel823d8e92010-12-08 22:42:58 +00001720 FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit,
1721 0, FieldSize, FieldAlign,
1722 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001723 EltTys.push_back(FieldTy);
1724 FieldOffset += FieldSize;
1725
1726 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001727 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel809b9bb2010-02-10 18:49:08 +00001728
Devang Patele2472482010-09-29 21:05:52 +00001729 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001730
Devang Patel823d8e92010-12-08 22:42:58 +00001731 return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
1732 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001733}
Devang Patel823d8e92010-12-08 22:42:58 +00001734
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001735/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001736void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001737 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001738 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1739
Devang Patel17800552010-03-09 00:44:50 +00001740 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001741 llvm::DIType Ty;
1742 uint64_t XOffset = 0;
1743 if (VD->hasAttr<BlocksAttr>())
1744 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1745 else
1746 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001747
Devang Patelf4e54a22010-05-07 23:05:55 +00001748 // If there is not any debug info for type then do not emit debug info
1749 // for this variable.
1750 if (!Ty)
1751 return;
1752
Chris Lattner9c85ba32008-11-10 06:08:34 +00001753 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001754 unsigned Line = getLineNumber(VD->getLocation());
1755 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001756 unsigned Flags = 0;
1757 if (VD->isImplicit())
1758 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001759 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001760
1761 llvm::StringRef Name = VD->getName();
1762 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001763 if (VD->hasAttr<BlocksAttr>()) {
1764 CharUnits offset = CharUnits::fromQuantity(32);
1765 llvm::SmallVector<llvm::Value *, 9> addr;
1766 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1767 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1768 // offset of __forwarding field
1769 offset =
1770 CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8);
1771 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1772 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1773 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1774 // offset of x field
1775 offset = CharUnits::fromQuantity(XOffset/8);
1776 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1777
1778 // Create the descriptor for the variable.
1779 llvm::DIVariable D =
1780 DBuilder.CreateComplexVariable(Tag,
1781 llvm::DIDescriptor(RegionStack.back()),
1782 VD->getName(), Unit, Line, Ty,
1783 addr.data(), addr.size());
1784
1785 // Insert an llvm.dbg.declare into the current block.
1786 llvm::Instruction *Call =
1787 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1788
1789 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1790 return;
1791 }
1792 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00001793 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001794 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1795 Name, Unit, Line, Ty,
1796 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001797
1798 // Insert an llvm.dbg.declare into the current block.
1799 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001800 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001801
1802 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001803 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001804 }
1805
1806 // If VD is an anonymous union then Storage represents value for
1807 // all union fields.
1808 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1809 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1810 if (RD->isUnion()) {
1811 for (RecordDecl::field_iterator I = RD->field_begin(),
1812 E = RD->field_end();
1813 I != E; ++I) {
1814 FieldDecl *Field = *I;
1815 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1816 llvm::StringRef FieldName = Field->getName();
1817
1818 // Ignore unnamed fields. Do not ignore unnamed records.
1819 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1820 continue;
1821
1822 // Use VarDecl's Tag, Scope and Line number.
1823 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001824 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1825 FieldName, Unit, Line, FieldTy,
1826 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001827
1828 // Insert an llvm.dbg.declare into the current block.
1829 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001830 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001831
1832 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1833 }
1834 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001835}
1836
Mike Stumpb1a6e682009-09-30 02:43:10 +00001837/// EmitDeclare - Emit local variable declaration debug info.
John McCall6b5a61b2011-02-07 10:33:21 +00001838void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001839 llvm::Value *Storage, CGBuilderTy &Builder,
John McCall6b5a61b2011-02-07 10:33:21 +00001840 const CGBlockInfo &blockInfo) {
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
John McCall6b5a61b2011-02-07 10:33:21 +00001846 bool isByRef = VD->hasAttr<BlocksAttr>();
1847
Mike Stumpb1a6e682009-09-30 02:43:10 +00001848 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001849 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001850 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00001851 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00001852 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1853 else
1854 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001855
1856 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001857 unsigned Line = getLineNumber(VD->getLocation());
1858 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001859
John McCall6b5a61b2011-02-07 10:33:21 +00001860 const llvm::TargetData &target = CGM.getTargetData();
1861
1862 CharUnits offset = CharUnits::fromQuantity(
1863 target.getStructLayout(blockInfo.StructureType)
1864 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
1865
Mike Stumpb1a6e682009-09-30 02:43:10 +00001866 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001867 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1868 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1869 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1870 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00001871 if (isByRef) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001872 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1873 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001874 // offset of __forwarding field
John McCall6b5a61b2011-02-07 10:33:21 +00001875 offset = CharUnits::fromQuantity(target.getPointerSize()/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001876 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1877 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1878 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001879 // offset of x field
1880 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001881 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001882 }
1883
1884 // Create the descriptor for the variable.
1885 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001886 DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
1887 VD->getName(), Unit, Line, Ty,
1888 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001889 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001890 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001891 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001892
Chris Lattnerc6034632010-04-01 06:31:43 +00001893 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001894 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001895}
1896
Devang Pateld6c5a262010-02-01 21:52:22 +00001897void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001898 llvm::Value *Storage,
1899 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001900 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001901}
1902
Mike Stumpb1a6e682009-09-30 02:43:10 +00001903void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
John McCall6b5a61b2011-02-07 10:33:21 +00001904 const VarDecl *variable, llvm::Value *Storage, CGBuilderTy &Builder,
1905 const CGBlockInfo &blockInfo) {
1906 EmitDeclare(variable, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder,
1907 blockInfo);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001908}
1909
Chris Lattner9c85ba32008-11-10 06:08:34 +00001910/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1911/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001912void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001913 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001914 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001915}
1916
1917
1918
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001919/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001920void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001921 const VarDecl *D) {
1922
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001923 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001924 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001925 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001926
Devang Pateleb6d79b2010-02-01 21:34:11 +00001927 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001928 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001930 // CodeGen turns int[] into int[1] so we'll do the same here.
1931 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001933 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001934 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Anders Carlsson20f12a22009-12-06 18:00:51 +00001936 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001937 ArrayType::Normal, 0);
1938 }
Devang Patel5d822f02010-04-29 17:48:37 +00001939 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001940 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001941 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001942 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001943 if (LinkageName == DeclName)
1944 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001945 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00001946 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +00001947 DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName,
1948 Unit, LineNo, getOrCreateType(T, Unit),
1949 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001950}
1951
Devang Patel9ca36b62009-02-26 21:10:26 +00001952/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001953void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001954 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001955 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001956 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001957 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001958
Devang Pateld6c5a262010-02-01 21:52:22 +00001959 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001960
Devang Pateld6c5a262010-02-01 21:52:22 +00001961 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001962 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Devang Patel9ca36b62009-02-26 21:10:26 +00001964 // CodeGen turns int[] into int[1] so we'll do the same here.
1965 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Devang Patel9ca36b62009-02-26 21:10:26 +00001967 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001968 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Anders Carlsson20f12a22009-12-06 18:00:51 +00001970 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001971 ArrayType::Normal, 0);
1972 }
1973
Devang Patel823d8e92010-12-08 22:42:58 +00001974 DBuilder.CreateGlobalVariable(Name, Unit, LineNo,
1975 getOrCreateType(T, Unit),
1976 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00001977}
Devang Patelabb485f2010-02-01 19:16:32 +00001978
Devang Patel25c2c8f2010-08-10 17:53:33 +00001979/// EmitGlobalVariable - Emit global variable's debug info.
1980void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001981 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001982 // Create the descriptor for the variable.
1983 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1984 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001985 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001986 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1987 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel31f7d022011-01-17 22:23:07 +00001988 Ty = CreateEnumType(ED);
Devang Patel6237cea2010-08-23 22:07:25 +00001989 }
Devang Patel0317ab02010-08-10 18:27:15 +00001990 // Do not use DIGlobalVariable for enums.
1991 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1992 return;
Devang Patel823d8e92010-12-08 22:42:58 +00001993 DBuilder.CreateStaticVariable(Unit, Name, Name, Unit,
1994 getLineNumber(VD->getLocation()),
1995 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001996}
1997
Devang Patelabb485f2010-02-01 19:16:32 +00001998/// getOrCreateNamesSpace - Return namespace descriptor for the given
1999/// namespace decl.
2000llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002001CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002002 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2003 NameSpaceCache.find(NSDecl);
2004 if (I != NameSpaceCache.end())
2005 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2006
Devang Patel8ab870d2010-05-12 23:46:38 +00002007 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002008 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002009 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002010 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002011 llvm::DINameSpace NS =
Devang Patel823d8e92010-12-08 22:42:58 +00002012 DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002013 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002014 return NS;
2015}