blob: ccf9c501259f7ab18ebd0065e845b510e2d09e24 [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"
John McCalld16c2cf2011-02-08 08:22:06 +000017#include "CGBlocks.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000018#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000019#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000020#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000021#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000022#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000024#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000026#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/Instructions.h"
31#include "llvm/Intrinsics.h"
32#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033#include "llvm/ADT/StringExtras.h"
34#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Support/Dwarf.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000036#include "llvm/Support/Path.h"
John McCall6b5a61b2011-02-07 10:33:21 +000037#include "llvm/Target/TargetData.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000038#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039using namespace clang;
40using namespace clang::CodeGen;
41
Anders Carlsson20f12a22009-12-06 18:00:51 +000042CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel823d8e92010-12-08 22:42:58 +000043 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000044 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000045 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000049 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000050}
51
Chris Lattner9c85ba32008-11-10 06:08:34 +000052void CGDebugInfo::setLocation(SourceLocation Loc) {
53 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000054 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000055}
56
Devang Patel33583052010-01-28 23:15:27 +000057/// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +000058llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000059 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000060 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000061
62 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
63 I = RegionMap.find(Context);
64 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000065 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000066
Devang Pateleb6d79b2010-02-01 21:34:11 +000067 // Check namespace.
68 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +000069 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +000070
71 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
72 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000073 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +000074 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +000075 return llvm::DIDescriptor(Ty);
76 }
77 }
Devang Patel170cef32010-12-09 00:33:05 +000078 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +000079}
80
Devang Patel9c6c3a02010-01-14 00:36:21 +000081/// getFunctionName - Get function name for the given FunctionDecl. If the
82/// name is constructred on demand (e.g. C++ destructor) then the name
83/// is stored on the side.
84llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
85 assert (FD && "Invalid FunctionDecl!");
86 IdentifierInfo *FII = FD->getIdentifier();
87 if (FII)
88 return FII->getName();
89
90 // Otherwise construct human readable name for debug info.
91 std::string NS = FD->getNameAsString();
92
93 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000094 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000095 memcpy(StrPtr, NS.data(), NS.length());
96 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000097}
98
David Chisnall52044a22010-09-02 18:01:51 +000099llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
100 llvm::SmallString<256> MethodName;
101 llvm::raw_svector_ostream OS(MethodName);
102 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
103 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000104 if (const ObjCImplementationDecl *OID =
105 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000106 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000107 } else if (const ObjCInterfaceDecl *OID =
108 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000109 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000110 } else if (const ObjCCategoryImplDecl *OCD =
111 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000112 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
113 OCD->getIdentifier()->getNameStart() << ')';
114 }
115 OS << ' ' << OMD->getSelector().getAsString() << ']';
116
117 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
118 memcpy(StrPtr, MethodName.begin(), OS.tell());
119 return llvm::StringRef(StrPtr, OS.tell());
120}
121
Devang Patel700a1cb2010-07-20 20:24:18 +0000122/// getClassName - Get class name including template argument list.
123llvm::StringRef
124CGDebugInfo::getClassName(RecordDecl *RD) {
125 ClassTemplateSpecializationDecl *Spec
126 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
127 if (!Spec)
128 return RD->getName();
129
130 const TemplateArgument *Args;
131 unsigned NumArgs;
132 std::string Buffer;
133 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
134 const TemplateSpecializationType *TST =
135 cast<TemplateSpecializationType>(TAW->getType());
136 Args = TST->getArgs();
137 NumArgs = TST->getNumArgs();
138 } else {
139 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000140 Args = TemplateArgs.data();
141 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000142 }
143 Buffer = RD->getIdentifier()->getNameStart();
144 PrintingPolicy Policy(CGM.getLangOptions());
145 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
146 NumArgs,
147 Policy);
148
149 // Copy this name on the side and use its reference.
150 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
151 memcpy(StrPtr, Buffer.data(), Buffer.length());
152 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000153}
154
Devang Patel17800552010-03-09 00:44:50 +0000155/// getOrCreateFile - Get the file debug info descriptor for the input location.
156llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000157 if (!Loc.isValid())
158 // If Location is not valid then use main input file.
159 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
160
Anders Carlsson20f12a22009-12-06 18:00:51 +0000161 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000162 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000163
Douglas Gregor8c457a82010-11-11 20:45:16 +0000164 if (PLoc.isInvalid())
165 // If the location is not valid then use main input file.
Devang Patel823d8e92010-12-08 22:42:58 +0000166 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000167
Ted Kremenek9c250392010-03-30 00:27:51 +0000168 // Cache the results.
169 const char *fname = PLoc.getFilename();
170 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
171 DIFileCache.find(fname);
172
173 if (it != DIFileCache.end()) {
174 // Verify that the information still exists.
175 if (&*it->second)
176 return llvm::DIFile(cast<llvm::MDNode>(it->second));
177 }
178
Devang Patel823d8e92010-12-08 22:42:58 +0000179 llvm::DIFile F = DBuilder.CreateFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000180
Devang Patelab699792010-05-07 18:12:35 +0000181 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000182 return F;
183
Devang Patel17800552010-03-09 00:44:50 +0000184}
Devang Patel8ab870d2010-05-12 23:46:38 +0000185
Devang Patel532105f2010-10-28 22:03:20 +0000186/// getOrCreateMainFile - Get the file info for main compile unit.
187llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel823d8e92010-12-08 22:42:58 +0000188 return DBuilder.CreateFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000189}
190
Devang Patel8ab870d2010-05-12 23:46:38 +0000191/// getLineNumber - Get line number for the location. If location is invalid
192/// then use current location.
193unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
194 assert (CurLoc.isValid() && "Invalid current location!");
195 SourceManager &SM = CGM.getContext().getSourceManager();
196 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000197 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000198}
199
200/// getColumnNumber - Get column number for the location. If location is
201/// invalid then use current location.
202unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
203 assert (CurLoc.isValid() && "Invalid current location!");
204 SourceManager &SM = CGM.getContext().getSourceManager();
205 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000206 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000207}
208
Devang Patelac4d13c2010-07-27 15:17:16 +0000209llvm::StringRef CGDebugInfo::getCurrentDirname() {
210 if (!CWDName.empty())
211 return CWDName;
212 char *CompDirnamePtr = NULL;
213 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
214 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
215 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
216 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
217}
218
Devang Patel17800552010-03-09 00:44:50 +0000219/// CreateCompileUnit - Create new compile unit.
220void CGDebugInfo::CreateCompileUnit() {
221
222 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000223 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000224 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
225 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000226 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000227
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000228 // The main file name provided via the "-main-file-name" option contains just
229 // the file name itself with no path information. This file name may have had
230 // a relative path, so we look into the actual file entry for the main
231 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000232 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000233 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000234 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000235 if (MainFileDir != ".")
236 MainFileName = MainFileDir + "/" + MainFileName;
237 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000238
Devang Patelac4d13c2010-07-27 15:17:16 +0000239 // Save filename string.
240 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
241 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
242 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
243
Chris Lattner515455a2009-03-25 03:28:08 +0000244 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000245 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000246 if (LO.CPlusPlus) {
247 if (LO.ObjC1)
248 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
249 else
250 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
251 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000252 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000253 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000254 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000255 } else {
256 LangTag = llvm::dwarf::DW_LANG_C89;
257 }
Devang Patel446c6192009-04-17 21:06:59 +0000258
Daniel Dunbar19f19832010-08-24 17:41:09 +0000259 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000260
261 // Figure out which version of the ObjC runtime we have.
262 unsigned RuntimeVers = 0;
263 if (LO.ObjC1)
264 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000266 // Create new compile unit.
Devang Patel823d8e92010-12-08 22:42:58 +0000267 DBuilder.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000268 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000269 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000270 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000271 // FIXME - Eliminate TheCU.
272 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000273}
274
Devang Patel65e99f22009-02-25 01:36:11 +0000275/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000276/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000277llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000279 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000280 switch (BT->getKind()) {
281 default:
282 case BuiltinType::Void:
283 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000284 case BuiltinType::ObjCClass:
Devang Patel823d8e92010-12-08 22:42:58 +0000285 return DBuilder.CreateStructType(TheCU, "objc_class",
286 getOrCreateMainFile(), 0, 0, 0,
287 llvm::DIDescriptor::FlagFwdDecl,
288 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000289 case BuiltinType::ObjCId: {
290 // typedef struct objc_class *Class;
291 // typedef struct objc_object {
292 // Class isa;
293 // } *id;
294
295 llvm::DIType OCTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000296 DBuilder.CreateStructType(TheCU, "objc_class",
297 getOrCreateMainFile(), 0, 0, 0,
298 llvm::DIDescriptor::FlagFwdDecl,
299 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000300 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
301
Devang Patel823d8e92010-12-08 22:42:58 +0000302 llvm::DIType ISATy = DBuilder.CreatePointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000303
Devang Patel823d8e92010-12-08 22:42:58 +0000304 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000305 llvm::DIType FieldTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000306 DBuilder.CreateMemberType("isa", getOrCreateMainFile(),
307 0,Size, 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000308 EltTys.push_back(FieldTy);
309 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +0000310 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelc8972c62010-07-28 01:33:15 +0000311
Devang Patel823d8e92010-12-08 22:42:58 +0000312 return DBuilder.CreateStructType(TheCU, "objc_object",
313 getOrCreateMainFile(),
314 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000315 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000316 case BuiltinType::UChar:
317 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
318 case BuiltinType::Char_S:
319 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
320 case BuiltinType::UShort:
321 case BuiltinType::UInt:
322 case BuiltinType::ULong:
323 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
324 case BuiltinType::Short:
325 case BuiltinType::Int:
326 case BuiltinType::Long:
327 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
328 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
329 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000330 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000331 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000332 }
Devang Patel05127ca2010-07-28 23:23:29 +0000333
334 switch (BT->getKind()) {
335 case BuiltinType::Long: BTName = "long int"; break;
336 case BuiltinType::LongLong: BTName = "long long int"; break;
337 case BuiltinType::ULong: BTName = "long unsigned int"; break;
338 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
339 default:
340 BTName = BT->getName(CGM.getContext().getLangOptions());
341 break;
342 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000343 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 uint64_t Size = CGM.getContext().getTypeSize(BT);
345 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000346 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000347 DBuilder.CreateBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000348 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000349}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000350
Devang Patel344ff5d2010-12-09 00:25:29 +0000351llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000352 // Bit size, align and offset of the type.
353 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
354 if (Ty->isComplexIntegerType())
355 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Anders Carlsson20f12a22009-12-06 18:00:51 +0000357 uint64_t Size = CGM.getContext().getTypeSize(Ty);
358 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000359 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +0000360 DBuilder.CreateBasicType("complex", Size, Align, Encoding);
361
Devang Patelca80a5f2009-10-20 19:55:01 +0000362 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000363}
364
John McCalla1805292009-09-25 01:40:47 +0000365/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000366/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000367llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000368 QualifierCollector Qc;
369 const Type *T = Qc.strip(Ty);
370
371 // Ignore these qualifiers for now.
372 Qc.removeObjCGCAttr();
373 Qc.removeAddressSpace();
374
Chris Lattner9c85ba32008-11-10 06:08:34 +0000375 // We will create one Derived type for one qualifier and recurse to handle any
376 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000377 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000378 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000379 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000380 Qc.removeConst();
381 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000382 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000383 Qc.removeVolatile();
384 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000386 Qc.removeRestrict();
387 } else {
388 assert(Qc.empty() && "Unknown type qualifier for debug info");
389 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
John McCall49f4e1c2010-12-10 11:01:00 +0000392 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000393
Daniel Dunbar3845f862008-10-31 03:54:29 +0000394 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
395 // CVR derived types.
Devang Patel823d8e92010-12-08 22:42:58 +0000396 llvm::DIType DbgTy = DBuilder.CreateQualifiedType(Tag, FromTy);
397
Devang Patelca80a5f2009-10-20 19:55:01 +0000398 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399}
400
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000401llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000402 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000403 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000404 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
405 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000406 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000407}
408
Chris Lattner9c85ba32008-11-10 06:08:34 +0000409llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000410 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000411 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
412 Ty->getPointeeType(), Unit);
413}
414
Devang Patelc69e1cf2010-09-30 19:05:55 +0000415/// CreatePointeeType - Create PointTee type. If Pointee is a record
416/// then emit record's fwd if debug info size reduction is enabled.
417llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
418 llvm::DIFile Unit) {
419 if (!CGM.getCodeGenOpts().LimitDebugInfo)
420 return getOrCreateType(PointeeTy, Unit);
421
422 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
423 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000424 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
425 unsigned Line = getLineNumber(RD->getLocation());
426 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000427 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000428
429 if (RD->isStruct())
430 return DBuilder.CreateStructType(FDContext, RD->getName(), DefUnit,
431 Line, 0, 0, llvm::DIType::FlagFwdDecl,
432 llvm::DIArray());
433 else if (RD->isUnion())
434 return DBuilder.CreateUnionType(FDContext, RD->getName(), DefUnit,
435 Line, 0, 0, llvm::DIType::FlagFwdDecl,
436 llvm::DIArray());
437 else {
438 assert(RD->isClass() && "Unknown RecordType!");
439 return DBuilder.CreateClassType(FDContext, RD->getName(), DefUnit,
440 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
441 llvm::DIType(), llvm::DIArray());
442 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000443 }
444 return getOrCreateType(PointeeTy, Unit);
445
446}
447
Anders Carlssona031b352009-11-06 19:19:55 +0000448llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
449 const Type *Ty,
450 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000451 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000452
453 if (Tag == llvm::dwarf::DW_TAG_reference_type)
454 return DBuilder.CreateReferenceType(CreatePointeeType(PointeeTy, Unit));
455
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000456 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000457 // Size is always the size of a pointer. We can't use getTypeSize here
458 // because that does not return the correct value for references.
459 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000460 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
461 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Devang Patel823d8e92010-12-08 22:42:58 +0000463 return
464 DBuilder.CreatePointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000465}
466
Mike Stump9bc093c2009-05-14 02:03:51 +0000467llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000468 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000469 if (BlockLiteralGenericSet)
470 return BlockLiteralGeneric;
471
Devang Patel823d8e92010-12-08 22:42:58 +0000472 llvm::SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000473 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000474 QualType FType;
475 uint64_t FieldSize, FieldOffset;
476 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000477 llvm::DIArray Elements;
478 llvm::DIType EltTy, DescTy;
479
480 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000481 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000482 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
483 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000484
Devang Patel823d8e92010-12-08 22:42:58 +0000485 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000486 EltTys.clear();
487
Devang Patele2472482010-09-29 21:05:52 +0000488 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000489 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000490
Devang Patel823d8e92010-12-08 22:42:58 +0000491 EltTy = DBuilder.CreateStructType(Unit, "__block_descriptor",
492 Unit, LineNo, FieldOffset, 0,
493 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Mike Stump9bc093c2009-05-14 02:03:51 +0000495 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000496 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Devang Patel823d8e92010-12-08 22:42:58 +0000498 DescTy = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000499
500 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000501 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000502 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000503 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000504 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
505 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000506 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000507 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000508
Anders Carlsson20f12a22009-12-06 18:00:51 +0000509 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000510 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 FieldSize = CGM.getContext().getTypeSize(Ty);
512 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel823d8e92010-12-08 22:42:58 +0000513 FieldTy = DBuilder.CreateMemberType("__descriptor", Unit,
514 LineNo, FieldSize, FieldAlign,
515 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000516 EltTys.push_back(FieldTy);
517
518 FieldOffset += FieldSize;
Devang Patel823d8e92010-12-08 22:42:58 +0000519 Elements = DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000520
Devang Patel823d8e92010-12-08 22:42:58 +0000521 EltTy = DBuilder.CreateStructType(Unit, "__block_literal_generic",
522 Unit, LineNo, FieldOffset, 0,
523 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Mike Stump9bc093c2009-05-14 02:03:51 +0000525 BlockLiteralGenericSet = true;
Devang Patel823d8e92010-12-08 22:42:58 +0000526 BlockLiteralGeneric = DBuilder.CreatePointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000527 return BlockLiteralGeneric;
528}
529
Chris Lattner9c85ba32008-11-10 06:08:34 +0000530llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000531 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000532 // Typedefs are derived from some other type. If we have a typedef of a
533 // typedef, make sure to emit the whole chain.
534 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000535 if (!Src.Verify())
536 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000537 // We don't set size information, but do specify where the typedef was
538 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000539 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +0000540 llvm::DIType DbgTy = DBuilder.CreateTypedef(Src, Ty->getDecl()->getName(),
541 Unit, Line);
Devang Patelca80a5f2009-10-20 19:55:01 +0000542 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000543}
544
Chris Lattner9c85ba32008-11-10 06:08:34 +0000545llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000546 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000547 llvm::SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000548
Chris Lattner9c85ba32008-11-10 06:08:34 +0000549 // Add the result type at least.
550 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner9c85ba32008-11-10 06:08:34 +0000552 // Set up remainder of arguments if there is a prototype.
553 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000554 if (isa<FunctionNoProtoType>(Ty))
Devang Patel823d8e92010-12-08 22:42:58 +0000555 EltTys.push_back(DBuilder.CreateUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000556 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000557 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
558 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000559 }
560
Chris Lattner9c85ba32008-11-10 06:08:34 +0000561 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000562 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000563
Devang Patel823d8e92010-12-08 22:42:58 +0000564 llvm::DIType DbgTy = DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000565 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566}
567
Devang Patel428deb52010-01-19 00:00:59 +0000568/// CollectRecordFields - A helper function to collect debug info for
569/// record fields. This is used while creating debug info entry for a Record.
570void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000571CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000572 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel428deb52010-01-19 00:00:59 +0000573 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000574 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
575 for (RecordDecl::field_iterator I = RD->field_begin(),
576 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000577 I != E; ++I, ++FieldNo) {
578 FieldDecl *Field = *I;
579 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000580 llvm::StringRef FieldName = Field->getName();
581
Devang Patel4835fdd2010-02-12 01:31:06 +0000582 // Ignore unnamed fields. Do not ignore unnamed records.
583 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000584 continue;
585
586 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000587 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
588 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000589 QualType FType = Field->getType();
590 uint64_t FieldSize = 0;
591 unsigned FieldAlign = 0;
592 if (!FType->isIncompleteArrayType()) {
593
594 // Bit size, align and offset of the type.
595 FieldSize = CGM.getContext().getTypeSize(FType);
596 Expr *BitWidth = Field->getBitWidth();
597 if (BitWidth)
598 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000599 FieldAlign = CGM.getContext().getTypeAlign(FType);
600 }
601
602 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
603
Devang Patel71f4ff62010-04-21 23:12:37 +0000604 unsigned Flags = 0;
605 AccessSpecifier Access = I->getAccess();
606 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000607 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000608 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000609 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000610
Devang Patel823d8e92010-12-08 22:42:58 +0000611 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
612 FieldLine, FieldSize, FieldAlign,
613 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000614 EltTys.push_back(FieldTy);
615 }
616}
617
Devang Patela6da1922010-01-28 00:28:01 +0000618/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
619/// function type is not updated to include implicit "this" pointer. Use this
620/// routine to get a method type which includes "this" pointer.
621llvm::DIType
622CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000623 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000624 llvm::DIType FnTy
625 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
626 0),
627 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000628
Devang Patela6da1922010-01-28 00:28:01 +0000629 // Add "this" pointer.
630
Devang Patelab699792010-05-07 18:12:35 +0000631 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000632 assert (Args.getNumElements() && "Invalid number of arguments!");
633
Devang Patel823d8e92010-12-08 22:42:58 +0000634 llvm::SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000635
636 // First element is always return type. For 'void' functions it is NULL.
637 Elts.push_back(Args.getElement(0));
638
Devang Patel2ed8f002010-08-27 17:47:47 +0000639 if (!Method->isStatic())
640 {
641 // "this" pointer is always first argument.
642 ASTContext &Context = CGM.getContext();
643 QualType ThisPtr =
644 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
645 llvm::DIType ThisPtrType =
Devang Patel823d8e92010-12-08 22:42:58 +0000646 DBuilder.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000647
Devang Patel2ed8f002010-08-27 17:47:47 +0000648 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
649 Elts.push_back(ThisPtrType);
650 }
Devang Patela6da1922010-01-28 00:28:01 +0000651
652 // Copy rest of the arguments.
653 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
654 Elts.push_back(Args.getElement(i));
655
656 llvm::DIArray EltTypeArray =
Devang Patel823d8e92010-12-08 22:42:58 +0000657 DBuilder.GetOrCreateArray(Elts.data(), Elts.size());
Devang Patela6da1922010-01-28 00:28:01 +0000658
Devang Patel823d8e92010-12-08 22:42:58 +0000659 return DBuilder.CreateSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000660}
661
Devang Patel58faf202010-10-22 17:11:50 +0000662/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
663/// inside a function.
664static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
665 if (const CXXRecordDecl *NRD =
666 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
667 return isFunctionLocalClass(NRD);
668 else if (isa<FunctionDecl>(RD->getDeclContext()))
669 return true;
670 return false;
671
672}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000673/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
674/// a single member function GlobalDecl.
675llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000676CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000677 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000678 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000679 bool IsCtorOrDtor =
680 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
681
682 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000683 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000684
685 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
686 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000687 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000688 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000689 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000690
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000691 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000692 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
693 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000694
695 // Collect virtual method info.
696 llvm::DIType ContainingType;
697 unsigned Virtuality = 0;
698 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000699
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000700 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000701 if (Method->isPure())
702 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
703 else
704 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
705
706 // It doesn't make sense to give a virtual destructor a vtable index,
707 // since a single destructor has two entries in the vtable.
708 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000709 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000710 ContainingType = RecordTy;
711 }
712
Devang Patele2472482010-09-29 21:05:52 +0000713 unsigned Flags = 0;
714 if (Method->isImplicit())
715 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000716 AccessSpecifier Access = Method->getAccess();
717 if (Access == clang::AS_private)
718 Flags |= llvm::DIDescriptor::FlagPrivate;
719 else if (Access == clang::AS_protected)
720 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000721 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
722 if (CXXC->isExplicit())
723 Flags |= llvm::DIDescriptor::FlagExplicit;
724 } else if (const CXXConversionDecl *CXXC =
725 dyn_cast<CXXConversionDecl>(Method)) {
726 if (CXXC->isExplicit())
727 Flags |= llvm::DIDescriptor::FlagExplicit;
728 }
Devang Patel3951e712010-10-07 22:03:49 +0000729 if (Method->hasPrototype())
730 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000731
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000732 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +0000733 DBuilder.CreateMethod(RecordTy , MethodName, MethodLinkageName,
734 MethodDefUnit, MethodLine,
735 MethodTy, /*isLocalToUnit=*/false,
736 /* isDefinition=*/ false,
737 Virtuality, VIndex, ContainingType,
738 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000739
740 // Don't cache ctors or dtors since we have to emit multiple functions for
741 // a single ctor or dtor.
742 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000743 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000744
745 return SP;
746}
747
Devang Patel4125fd22010-01-19 01:54:44 +0000748/// CollectCXXMemberFunctions - A helper function to collect debug info for
749/// C++ member functions.This is used while creating debug info entry for
750/// a Record.
751void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000752CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000753 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000754 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000755 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
756 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000757 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000758
Devang Pateld5322da2010-02-09 19:09:28 +0000759 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000760 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000761
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000762 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000763 }
764}
765
Devang Patel2ed8f002010-08-27 17:47:47 +0000766/// CollectCXXFriends - A helper function to collect debug info for
767/// C++ base classes. This is used while creating debug info entry for
768/// a Record.
769void CGDebugInfo::
770CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000771 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000772 llvm::DIType RecordTy) {
773
774 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
775 BE = RD->friend_end(); BI != BE; ++BI) {
Devang Patel823d8e92010-12-08 22:42:58 +0000776 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
777 EltTys.push_back(DBuilder.CreateFriend(RecordTy,
778 getOrCreateType(TInfo->getType(),
779 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000780 }
781}
782
Devang Patela245c5b2010-01-25 23:32:18 +0000783/// CollectCXXBases - A helper function to collect debug info for
784/// C++ base classes. This is used while creating debug info entry for
785/// a Record.
786void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000787CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000788 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000789 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000790
Devang Patel239cec62010-02-01 21:39:52 +0000791 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
792 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
793 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000794 unsigned BFlags = 0;
795 uint64_t BaseOffset;
796
797 const CXXRecordDecl *Base =
798 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
799
800 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000801 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000802 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000803 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000804 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000805 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000806 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000807
808 AccessSpecifier Access = BI->getAccessSpecifier();
809 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000810 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000811 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000812 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000813
Devang Patel823d8e92010-12-08 22:42:58 +0000814 llvm::DIType DTy =
815 DBuilder.CreateInheritance(RecordTy,
816 getOrCreateType(BI->getType(), Unit),
817 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000818 EltTys.push_back(DTy);
819 }
Devang Patela245c5b2010-01-25 23:32:18 +0000820}
821
Devang Patel4ce3f202010-01-28 18:11:52 +0000822/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000823llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000824 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000825 return VTablePtrType;
826
827 ASTContext &Context = CGM.getContext();
828
829 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000830 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
831 llvm::DIArray SElements = DBuilder.GetOrCreateArray(&STy, 1);
832 llvm::DIType SubTy = DBuilder.CreateSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000833 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000834 llvm::DIType vtbl_ptr_type = DBuilder.CreatePointerType(SubTy, Size, 0,
835 "__vtbl_ptr_type");
836 VTablePtrType = DBuilder.CreatePointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000837 return VTablePtrType;
838}
839
Anders Carlsson046c2942010-04-17 20:15:18 +0000840/// getVTableName - Get vtable name for the given Class.
841llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000842 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000843 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000844
845 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000846 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000847 memcpy(StrPtr, Name.data(), Name.length());
848 return llvm::StringRef(StrPtr, Name.length());
849}
850
851
Anders Carlsson046c2942010-04-17 20:15:18 +0000852/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000853/// debug info entry in EltTys vector.
854void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000855CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000856 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000857 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000858
859 // If there is a primary base then it will hold vtable info.
860 if (RL.getPrimaryBase())
861 return;
862
863 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000864 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000865 return;
866
867 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
868 llvm::DIType VPTR
Devang Patel823d8e92010-12-08 22:42:58 +0000869 = DBuilder.CreateMemberType(getVTableName(RD), Unit,
870 0, Size, 0, 0, 0,
871 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000872 EltTys.push_back(VPTR);
873}
874
Devang Patelc69e1cf2010-09-30 19:05:55 +0000875/// getOrCreateRecordType - Emit record type's standalone debug info.
876llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
877 SourceLocation Loc) {
878 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel823d8e92010-12-08 22:42:58 +0000879 DBuilder.RetainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000880 return T;
881}
882
Devang Patel65e99f22009-02-25 01:36:11 +0000883/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +0000884llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000885 RecordDecl *RD = Ty->getDecl();
Devang Patel31f7d022011-01-17 22:23:07 +0000886 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattner9c85ba32008-11-10 06:08:34 +0000888 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000889 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
890 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Chris Lattner9c85ba32008-11-10 06:08:34 +0000892 // Records and classes and unions can all be recursive. To handle them, we
893 // first generate a debug descriptor for the struct as a forward declaration.
894 // Then (if it is a definition) we go through and get debug info for all of
895 // its members. Finally, we create a descriptor for the complete type (which
896 // may refer to the forward decl if the struct is recursive) and replace all
897 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000898 llvm::DIDescriptor FDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000899 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel0b897992010-07-08 19:56:29 +0000900
901 // If this is just a forward declaration, construct an appropriately
902 // marked node and just return it.
903 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +0000904 llvm::DIType FwdDecl =
905 DBuilder.CreateStructType(FDContext, RD->getName(),
906 DefUnit, Line, 0, 0,
907 llvm::DIDescriptor::FlagFwdDecl,
908 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +0000909
910 return FwdDecl;
911 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000912
Devang Patel823d8e92010-12-08 22:42:58 +0000913 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Devang Patelab699792010-05-07 18:12:35 +0000915 llvm::MDNode *MN = FwdDecl;
916 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000917 // Otherwise, insert it into the TypeCache so that recursive uses will find
918 // it.
Devang Patelab699792010-05-07 18:12:35 +0000919 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000920 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000921 RegionStack.push_back(FwdDeclNode);
922 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000923
924 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +0000925 llvm::SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000926
Devang Pateld6c5a262010-02-01 21:52:22 +0000927 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000928 if (CXXDecl) {
929 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000930 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000931 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000932
933 // Collect static variables with initializers.
934 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
935 I != E; ++I)
936 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
937 if (const Expr *Init = V->getInit()) {
938 Expr::EvalResult Result;
939 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
940 llvm::ConstantInt *CI
941 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
942
943 // Create the descriptor for static variable.
944 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
945 llvm::StringRef VName = V->getName();
946 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
947 // Do not use DIGlobalVariable for enums.
948 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel823d8e92010-12-08 22:42:58 +0000949 DBuilder.CreateStaticVariable(FwdDecl, VName, VName, VUnit,
950 getLineNumber(V->getLocation()),
951 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +0000952 }
953 }
954 }
955 }
956
Devang Pateld6c5a262010-02-01 21:52:22 +0000957 CollectRecordFields(RD, Unit, EltTys);
Devang Patelfa275df2011-02-02 21:38:49 +0000958 llvm::SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel4ce3f202010-01-28 18:11:52 +0000959 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000960 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000961 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelfa275df2011-02-02 21:38:49 +0000962 if (ClassTemplateSpecializationDecl *TSpecial
963 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
964 const TemplateArgumentList &TAL = TSpecial->getTemplateArgs();
965 for (unsigned i = 0, e = TAL.size(); i != e; ++i) {
966 const TemplateArgument &TA = TAL[i];
967 if (TA.getKind() == TemplateArgument::Type) {
968 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
969 llvm::DITemplateTypeParameter TTP =
970 DBuilder.CreateTemplateTypeParameter(TheCU, TTy.getName(), TTy);
971 TemplateParams.push_back(TTP);
Devang Patel0ce34c62011-02-02 22:36:18 +0000972 } else if (TA.getKind() == TemplateArgument::Integral) {
973 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
974 // FIXME: Get parameter name, instead of parameter type name.
975 llvm::DITemplateValueParameter TVP =
976 DBuilder.CreateTemplateValueParameter(TheCU, TTy.getName(), TTy,
977 TA.getAsIntegral()->getZExtValue());
978 TemplateParams.push_back(TVP);
Devang Patelfa275df2011-02-02 21:38:49 +0000979 }
980 }
981 }
Devang Patel823d8e92010-12-08 22:42:58 +0000982 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000983
Devang Patel823d8e92010-12-08 22:42:58 +0000984 RegionStack.pop_back();
985 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
986 RegionMap.find(Ty->getDecl());
987 if (RI != RegionMap.end())
988 RegionMap.erase(RI);
989
990 llvm::DIDescriptor RDContext =
Devang Patel170cef32010-12-09 00:33:05 +0000991 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000992 llvm::StringRef RDName = RD->getName();
993 uint64_t Size = CGM.getContext().getTypeSize(Ty);
994 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
995 llvm::DIArray Elements =
996 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
997 llvm::MDNode *RealDecl = NULL;
998
999 if (RD->isStruct())
1000 RealDecl = DBuilder.CreateStructType(RDContext, RDName, DefUnit, Line,
1001 Size, Align, 0, Elements);
1002 else if (RD->isUnion())
1003 RealDecl = DBuilder.CreateUnionType(RDContext, RDName, DefUnit, Line,
1004 Size, Align, 0, Elements);
1005 else {
1006 assert(RD->isClass() && "Unknown RecordType!");
1007 RDName = getClassName(RD);
1008 // A class's primary base or the class itself contains the vtable.
1009 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +00001010 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001011 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1012 // Seek non virtual primary base root.
1013 while (1) {
1014 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1015 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001016 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001017 PBase = PBT;
1018 else
1019 break;
1020 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001021 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001022 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001023 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001024 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001025 ContainingType = FwdDecl;
Devang Patelfa275df2011-02-02 21:38:49 +00001026 llvm::DIArray TParamsArray =
1027 DBuilder.GetOrCreateArray(TemplateParams.data(), TemplateParams.size());
Devang Patel823d8e92010-12-08 22:42:58 +00001028 RealDecl = DBuilder.CreateClassType(RDContext, RDName, DefUnit, Line,
1029 Size, Align, 0, 0, llvm::DIType(),
Devang Patelfa275df2011-02-02 21:38:49 +00001030 Elements, ContainingType,
1031 TParamsArray);
Devang Patela245c5b2010-01-25 23:32:18 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner9c85ba32008-11-10 06:08:34 +00001034 // Now that we have a real decl for the struct, replace anything using the
1035 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001036 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001037 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001038 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001039}
1040
John McCallc12c5bb2010-05-15 11:32:37 +00001041/// CreateType - get objective-c object type.
1042llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1043 llvm::DIFile Unit) {
1044 // Ignore protocols.
1045 return getOrCreateType(Ty->getBaseType(), Unit);
1046}
1047
Devang Patel9ca36b62009-02-26 21:10:26 +00001048/// CreateType - get objective-c interface type.
1049llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001050 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001051 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001052 if (!ID)
1053 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001054
1055 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001056 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001057 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001058 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001059
Dan Gohman45f7c782010-08-23 21:15:56 +00001060 // If this is just a forward declaration, return a special forward-declaration
1061 // debug type.
1062 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001063 llvm::DIType FwdDecl =
1064 DBuilder.CreateStructType(Unit, ID->getName(),
1065 DefUnit, Line, 0, 0, 0,
1066 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001067 return FwdDecl;
1068 }
1069
Devang Patel9ca36b62009-02-26 21:10:26 +00001070 // To handle recursive interface, we
1071 // first generate a debug descriptor for the struct as a forward declaration.
1072 // Then (if it is a definition) we go through and get debug info for all of
1073 // its members. Finally, we create a descriptor for the complete type (which
1074 // may refer to the forward decl if the struct is recursive) and replace all
1075 // uses of the forward declaration with the final definition.
Devang Patel823d8e92010-12-08 22:42:58 +00001076 llvm::DIType FwdDecl = DBuilder.CreateTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001077
Devang Patelab699792010-05-07 18:12:35 +00001078 llvm::MDNode *MN = FwdDecl;
1079 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001080 // Otherwise, insert it into the TypeCache so that recursive uses will find
1081 // it.
Devang Patelab699792010-05-07 18:12:35 +00001082 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001083 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001084 RegionStack.push_back(FwdDeclNode);
1085 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001086
1087 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +00001088 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001089
Devang Pateld6c5a262010-02-01 21:52:22 +00001090 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001091 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001092 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001093 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001094 if (!SClassTy.isValid())
1095 return llvm::DIType();
1096
Mike Stump1eb44332009-09-09 15:08:12 +00001097 llvm::DIType InhTag =
Devang Patel823d8e92010-12-08 22:42:58 +00001098 DBuilder.CreateInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001099 EltTys.push_back(InhTag);
1100 }
1101
Devang Pateld6c5a262010-02-01 21:52:22 +00001102 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001103
1104 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001105 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001106 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001107 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001108 if (!FieldTy.isValid())
1109 return llvm::DIType();
1110
Devang Patel73621622009-11-25 17:37:31 +00001111 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001112
Devang Patelde135022009-04-27 22:40:36 +00001113 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001114 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001115 continue;
1116
Devang Patel9ca36b62009-02-26 21:10:26 +00001117 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001118 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1119 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001120 QualType FType = Field->getType();
1121 uint64_t FieldSize = 0;
1122 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001123
Devang Patel99c20eb2009-03-20 18:24:39 +00001124 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Devang Patel99c20eb2009-03-20 18:24:39 +00001126 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001127 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001128 Expr *BitWidth = Field->getBitWidth();
1129 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001130 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001131
Anders Carlsson20f12a22009-12-06 18:00:51 +00001132 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001133 }
1134
Mike Stump1eb44332009-09-09 15:08:12 +00001135 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1136
Devang Patelc20482b2009-03-19 00:23:53 +00001137 unsigned Flags = 0;
1138 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001139 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001140 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001141 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Devang Patel823d8e92010-12-08 22:42:58 +00001143 FieldTy = DBuilder.CreateMemberType(FieldName, FieldDefUnit,
1144 FieldLine, FieldSize, FieldAlign,
1145 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001146 EltTys.push_back(FieldTy);
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Devang Patel9ca36b62009-02-26 21:10:26 +00001149 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001150 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001151
Devang Patele4c1ea02010-03-11 20:01:48 +00001152 RegionStack.pop_back();
1153 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1154 RegionMap.find(Ty->getDecl());
1155 if (RI != RegionMap.end())
1156 RegionMap.erase(RI);
1157
Devang Patel9ca36b62009-02-26 21:10:26 +00001158 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1160 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Devang Patel823d8e92010-12-08 22:42:58 +00001162 llvm::DIType RealDecl =
1163 DBuilder.CreateStructType(Unit, ID->getName(), DefUnit,
1164 Line, Size, Align, 0,
1165 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001166
1167 // Now that we have a real decl for the struct, replace anything using the
1168 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001169 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001170 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001171
Devang Patel9ca36b62009-02-26 21:10:26 +00001172 return RealDecl;
1173}
1174
Devang Patel31f7d022011-01-17 22:23:07 +00001175llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001176 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001177 return CreateType(RT);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001178 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001179 return CreateEnumType(ET->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Chris Lattner9c85ba32008-11-10 06:08:34 +00001181 return llvm::DIType();
1182}
1183
Devang Patel70c23cd2010-02-23 22:59:39 +00001184llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001185 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001186 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1187 uint64_t NumElems = Ty->getNumElements();
1188 if (NumElems > 0)
1189 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001190
Devang Patel823d8e92010-12-08 22:42:58 +00001191 llvm::Value *Subscript = DBuilder.GetOrCreateSubrange(0, NumElems);
1192 llvm::DIArray SubscriptArray = DBuilder.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001193
1194 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1195 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1196
1197 return
Devang Patel823d8e92010-12-08 22:42:58 +00001198 DBuilder.CreateVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001199}
1200
Chris Lattner9c85ba32008-11-10 06:08:34 +00001201llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001202 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001203 uint64_t Size;
1204 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
1206
Nuno Lopes010d5142009-01-28 00:35:17 +00001207 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001208 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001209 Size = 0;
1210 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001211 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001212 } else if (Ty->isIncompleteArrayType()) {
1213 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001214 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001215 } else {
1216 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001217 Size = CGM.getContext().getTypeSize(Ty);
1218 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001219 }
Mike Stump1eb44332009-09-09 15:08:12 +00001220
Chris Lattner9c85ba32008-11-10 06:08:34 +00001221 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1222 // interior arrays, do we care? Why aren't nested arrays represented the
1223 // obvious/recursive way?
Devang Patel823d8e92010-12-08 22:42:58 +00001224 llvm::SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001225 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001226 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001227 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001228 else {
1229 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1230 uint64_t Upper = 0;
1231 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1232 if (CAT->getSize().getZExtValue())
1233 Upper = CAT->getSize().getZExtValue() - 1;
1234 // FIXME: Verify this is right for VLAs.
Devang Patel823d8e92010-12-08 22:42:58 +00001235 Subscripts.push_back(DBuilder.GetOrCreateSubrange(0, Upper));
Devang Patelcdf523c2010-10-06 18:30:00 +00001236 EltTy = Ty->getElementType();
1237 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001238 }
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Chris Lattner9c85ba32008-11-10 06:08:34 +00001240 llvm::DIArray SubscriptArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001241 DBuilder.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001242
Devang Patelca80a5f2009-10-20 19:55:01 +00001243 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001244 DBuilder.CreateArrayType(Size, Align, getOrCreateType(EltTy, Unit),
1245 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001246 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001247}
1248
Anders Carlssona031b352009-11-06 19:19:55 +00001249llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001250 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001251 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1252 Ty, Ty->getPointeeType(), Unit);
1253}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001254
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001255llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1256 llvm::DIFile Unit) {
1257 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1258 Ty, Ty->getPointeeType(), Unit);
1259}
1260
Anders Carlsson20f12a22009-12-06 18:00:51 +00001261llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001262 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001263 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1264 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1265
1266 if (!Ty->getPointeeType()->isFunctionType()) {
1267 // We have a data member pointer type.
1268 return PointerDiffDITy;
1269 }
1270
1271 // We have a member function pointer type. Treat it as a struct with two
1272 // ptrdiff_t members.
1273 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1274
1275 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001276 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001277
1278 // FIXME: This should probably be a function type instead.
1279 ElementTypes[0] =
Devang Patel823d8e92010-12-08 22:42:58 +00001280 DBuilder.CreateMemberType("ptr", U, 0,
1281 Info.first, Info.second, FieldOffset, 0,
1282 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001283 FieldOffset += Info.first;
1284
1285 ElementTypes[1] =
Devang Patel823d8e92010-12-08 22:42:58 +00001286 DBuilder.CreateMemberType("ptr", U, 0,
1287 Info.first, Info.second, FieldOffset, 0,
1288 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001289
1290 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001291 DBuilder.GetOrCreateArray(&ElementTypes[0],
1292 llvm::array_lengthof(ElementTypes));
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293
Devang Patel823d8e92010-12-08 22:42:58 +00001294 return DBuilder.CreateStructType(U, llvm::StringRef("test"),
1295 U, 0, FieldOffset,
1296 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001297}
1298
Devang Patel6237cea2010-08-23 22:07:25 +00001299/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001300llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1301 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +00001302 llvm::SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001303
1304 // Create DIEnumerator elements for each enumerator.
1305 for (EnumDecl::enumerator_iterator
1306 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1307 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001308 Enumerators.push_back(
1309 DBuilder.CreateEnumerator(Enum->getName(),
1310 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001311 }
1312
1313 // Return a CompositeType for the enum itself.
1314 llvm::DIArray EltArray =
Devang Patel823d8e92010-12-08 22:42:58 +00001315 DBuilder.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Devang Patel6237cea2010-08-23 22:07:25 +00001316
1317 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1318 unsigned Line = getLineNumber(ED->getLocation());
1319 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001320 uint64_t Align = 0;
1321 if (!ED->getTypeForDecl()->isIncompleteType()) {
1322 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1323 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1324 }
Devang Patel4bc48872010-10-27 23:23:58 +00001325 llvm::DIDescriptor EnumContext =
Devang Patel170cef32010-12-09 00:33:05 +00001326 getContextDescriptor(dyn_cast<Decl>(ED->getDeclContext()));
Devang Patel6237cea2010-08-23 22:07:25 +00001327 llvm::DIType DbgTy =
Devang Patel823d8e92010-12-08 22:42:58 +00001328 DBuilder.CreateEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
1329 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001330 return DbgTy;
1331}
1332
Douglas Gregor840943d2009-12-21 20:18:30 +00001333static QualType UnwrapTypeForDebugInfo(QualType T) {
1334 do {
1335 QualType LastT = T;
1336 switch (T->getTypeClass()) {
1337 default:
1338 return T;
1339 case Type::TemplateSpecialization:
1340 T = cast<TemplateSpecializationType>(T)->desugar();
1341 break;
John McCallf4c73712011-01-19 06:33:43 +00001342 case Type::TypeOfExpr:
1343 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001344 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001345 case Type::TypeOf:
1346 T = cast<TypeOfType>(T)->getUnderlyingType();
1347 break;
1348 case Type::Decltype:
1349 T = cast<DecltypeType>(T)->getUnderlyingType();
1350 break;
John McCall9d156a72011-01-06 01:58:22 +00001351 case Type::Attributed:
1352 T = cast<AttributedType>(T)->getEquivalentType();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001353 case Type::Elaborated:
1354 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001355 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001356 case Type::Paren:
1357 T = cast<ParenType>(T)->getInnerType();
1358 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001359 case Type::SubstTemplateTypeParm:
1360 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1361 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001362 }
1363
1364 assert(T != LastT && "Type unwrapping failed to unwrap!");
1365 if (T == LastT)
1366 return T;
1367 } while (true);
1368
1369 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001370}
1371
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001372/// getOrCreateType - Get the type from the cache or create a new
1373/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001374llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001375 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001376 if (Ty.isNull())
1377 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Douglas Gregor840943d2009-12-21 20:18:30 +00001379 // Unwrap the type as needed for debug information.
1380 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001381
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001382 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001383 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001384 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001385 if (it != TypeCache.end()) {
1386 // Verify that the debug info still exists.
1387 if (&*it->second)
1388 return llvm::DIType(cast<llvm::MDNode>(it->second));
1389 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001390
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001391 // Otherwise create the type.
1392 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001393
1394 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001395 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001396 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001397}
1398
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001399/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001400llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001401 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001402 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001403 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001404 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001405
Douglas Gregor2101a822009-12-21 19:57:21 +00001406 const char *Diag = 0;
1407
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001408 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001409 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001410#define TYPE(Class, Base)
1411#define ABSTRACT_TYPE(Class, Base)
1412#define NON_CANONICAL_TYPE(Class, Base)
1413#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1414#include "clang/AST/TypeNodes.def"
1415 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001416
Anders Carlssonbfe69952009-11-06 18:24:04 +00001417 // FIXME: Handle these.
1418 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001419 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001420
1421 case Type::Vector:
1422 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001423 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001424 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001425 case Type::ObjCObject:
1426 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001427 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001428 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001429 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001430 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001431 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001432 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001433 return CreateType(cast<BlockPointerType>(Ty), Unit);
1434 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001435 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001436 case Type::Enum:
Devang Patel31f7d022011-01-17 22:23:07 +00001437 return CreateType(cast<TagType>(Ty));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001438 case Type::FunctionProto:
1439 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001440 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001441 case Type::ConstantArray:
1442 case Type::VariableArray:
1443 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001444 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001445
1446 case Type::LValueReference:
1447 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001448 case Type::RValueReference:
1449 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001450
Anders Carlsson20f12a22009-12-06 18:00:51 +00001451 case Type::MemberPointer:
1452 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001453
John McCall9d156a72011-01-06 01:58:22 +00001454 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001455 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001456 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001457 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001458 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001459 case Type::TypeOfExpr:
1460 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001461 case Type::Decltype:
1462 llvm_unreachable("type should have been unwrapped!");
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001463 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001464 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001465
1466 assert(Diag && "Fall through without a diagnostic?");
1467 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1468 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001469 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001470 << Diag;
1471 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001472}
1473
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001474/// CreateMemberType - Create new member and increase Offset by FType's size.
1475llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1476 llvm::StringRef Name,
1477 uint64_t *Offset) {
1478 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1479 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1480 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel823d8e92010-12-08 22:42:58 +00001481 llvm::DIType Ty = DBuilder.CreateMemberType(Name, Unit, 0,
1482 FieldSize, FieldAlign,
1483 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001484 *Offset += FieldSize;
1485 return Ty;
1486}
1487
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001488/// EmitFunctionStart - Constructs the debug code for entering a function -
1489/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001490void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001491 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001492 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Devang Patel9c6c3a02010-01-14 00:36:21 +00001494 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001495 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001496
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001497 FnBeginRegionCount.push_back(RegionStack.size());
1498
Devang Patel9c6c3a02010-01-14 00:36:21 +00001499 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001500 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001501 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1502 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001503 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001504 // If there is a DISubprogram for this function available then use it.
1505 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1506 FI = SPCache.find(FD);
1507 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001508 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001509 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1510 llvm::MDNode *SPN = SP;
1511 RegionStack.push_back(SPN);
1512 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001513 return;
1514 }
1515 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001516 Name = getFunctionName(FD);
1517 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001518 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001519 if (LinkageName == Name)
1520 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001521 if (FD->hasPrototype())
1522 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001523 if (const NamespaceDecl *NSDecl =
1524 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel170cef32010-12-09 00:33:05 +00001525 FDContext = getOrCreateNameSpace(NSDecl);
David Chisnall70b9b442010-09-02 17:16:32 +00001526 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001527 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001528 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001529 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001530 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001531 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001532 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001533 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001534 if (!Name.empty() && Name[0] == '\01')
1535 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Devang Patel970c6182010-04-24 00:49:16 +00001537 // It is expected that CurLoc is set before using EmitFunctionStart.
1538 // Usually, CurLoc points to the left bracket location of compound
1539 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001540 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001541 if (D->isImplicit())
1542 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001543 llvm::DISubprogram SP =
Devang Patel823d8e92010-12-08 22:42:58 +00001544 DBuilder.CreateFunction(FDContext, Name, LinkageName, Unit,
1545 LineNo, getOrCreateType(FnType, Unit),
1546 Fn->hasInternalLinkage(), true/*definition*/,
1547 Flags, CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001549 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001550 llvm::MDNode *SPN = SP;
1551 RegionStack.push_back(SPN);
1552 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001553
1554 // Clear stack used to keep track of #line directives.
1555 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001556}
1557
1558
Devang Patel4d939e62010-07-20 22:20:10 +00001559void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001560 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001562 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001563 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001564 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001565 || (SM.getInstantiationLineNumber(CurLoc) ==
1566 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001567 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001568 // New Builder may not be in sync with CGDebugInfo.
1569 if (!Builder.getCurrentDebugLocation().isUnknown())
1570 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001571
1572 // Update last state.
1573 PrevLoc = CurLoc;
1574
Chris Lattnerc6034632010-04-01 06:31:43 +00001575 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001576 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1577 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001578 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001579}
1580
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001581/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1582/// has introduced scope change.
1583void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1584 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1585 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1586 return;
1587 SourceManager &SM = CGM.getContext().getSourceManager();
1588 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1589 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1590
Douglas Gregor8c457a82010-11-11 20:45:16 +00001591 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1592 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001593 return;
1594
1595 // If #line directive stack is empty then we are entering a new scope.
1596 if (LineDirectiveFiles.empty()) {
1597 EmitRegionStart(Builder);
1598 LineDirectiveFiles.push_back(PCLoc.getFilename());
1599 return;
1600 }
1601
1602 assert (RegionStack.size() >= LineDirectiveFiles.size()
1603 && "error handling #line regions!");
1604
1605 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001606 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001607 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1608 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001609 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001610 SeenThisFile = true;
1611 break;
1612 }
1613
1614 // If #line for this file is seen earlier then pop out #line regions.
1615 if (SeenThisFile) {
1616 while (!LineDirectiveFiles.empty()) {
1617 const char *LastFile = LineDirectiveFiles.back();
1618 RegionStack.pop_back();
1619 LineDirectiveFiles.pop_back();
1620 if (!strcmp(PPLoc.getFilename(), LastFile))
1621 break;
1622 }
1623 return;
1624 }
1625
1626 // .. otherwise insert new #line region.
1627 EmitRegionStart(Builder);
1628 LineDirectiveFiles.push_back(PCLoc.getFilename());
1629
1630 return;
1631}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001632/// EmitRegionStart- Constructs the debug code for entering a declarative
1633/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001634void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001635 llvm::DIDescriptor D =
Devang Patel823d8e92010-12-08 22:42:58 +00001636 DBuilder.CreateLexicalBlock(RegionStack.empty() ?
1637 llvm::DIDescriptor() :
1638 llvm::DIDescriptor(RegionStack.back()),
1639 getOrCreateFile(CurLoc),
1640 getLineNumber(CurLoc),
1641 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001642 llvm::MDNode *DN = D;
1643 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001644}
1645
1646/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1647/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001648void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001649 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1650
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001651 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001652 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001654 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001655}
1656
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001657/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1658void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1659 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1660 unsigned RCount = FnBeginRegionCount.back();
1661 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1662
1663 // Pop all regions for this function.
1664 while (RegionStack.size() != RCount)
1665 EmitRegionEnd(Builder);
1666 FnBeginRegionCount.pop_back();
1667}
1668
Devang Patel809b9bb2010-02-10 18:49:08 +00001669// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1670// See BuildByRefType.
1671llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1672 uint64_t *XOffset) {
1673
Devang Patel823d8e92010-12-08 22:42:58 +00001674 llvm::SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001675 QualType FType;
1676 uint64_t FieldSize, FieldOffset;
1677 unsigned FieldAlign;
1678
Devang Patel17800552010-03-09 00:44:50 +00001679 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001680 QualType Type = VD->getType();
1681
1682 FieldOffset = 0;
1683 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001684 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1685 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001686 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001687 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1688 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1689
John McCall6b5a61b2011-02-07 10:33:21 +00001690 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00001691 if (HasCopyAndDispose) {
1692 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001693 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1694 &FieldOffset));
1695 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1696 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001697 }
1698
1699 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1700 if (Align > CharUnits::fromQuantity(
1701 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1702 unsigned AlignedOffsetInBytes
1703 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1704 unsigned NumPaddingBytes
1705 = AlignedOffsetInBytes - FieldOffset/8;
1706
1707 if (NumPaddingBytes > 0) {
1708 llvm::APInt pad(32, NumPaddingBytes);
1709 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1710 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001711 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001712 }
1713 }
1714
1715 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001716 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001717 FieldSize = CGM.getContext().getTypeSize(FType);
1718 FieldAlign = Align.getQuantity()*8;
1719
1720 *XOffset = FieldOffset;
Devang Patel823d8e92010-12-08 22:42:58 +00001721 FieldTy = DBuilder.CreateMemberType(VD->getName(), Unit,
1722 0, FieldSize, FieldAlign,
1723 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001724 EltTys.push_back(FieldTy);
1725 FieldOffset += FieldSize;
1726
1727 llvm::DIArray Elements =
Devang Patel823d8e92010-12-08 22:42:58 +00001728 DBuilder.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel809b9bb2010-02-10 18:49:08 +00001729
Devang Patele2472482010-09-29 21:05:52 +00001730 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001731
Devang Patel823d8e92010-12-08 22:42:58 +00001732 return DBuilder.CreateStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
1733 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001734}
Devang Patel823d8e92010-12-08 22:42:58 +00001735
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001736/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001737void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001738 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001739 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1740
Devang Patel17800552010-03-09 00:44:50 +00001741 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001742 llvm::DIType Ty;
1743 uint64_t XOffset = 0;
1744 if (VD->hasAttr<BlocksAttr>())
1745 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1746 else
1747 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001748
Devang Patelf4e54a22010-05-07 23:05:55 +00001749 // If there is not any debug info for type then do not emit debug info
1750 // for this variable.
1751 if (!Ty)
1752 return;
1753
Chris Lattner9c85ba32008-11-10 06:08:34 +00001754 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001755 unsigned Line = getLineNumber(VD->getLocation());
1756 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001757 unsigned Flags = 0;
1758 if (VD->isImplicit())
1759 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001760 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001761
1762 llvm::StringRef Name = VD->getName();
1763 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001764 if (VD->hasAttr<BlocksAttr>()) {
1765 CharUnits offset = CharUnits::fromQuantity(32);
1766 llvm::SmallVector<llvm::Value *, 9> addr;
1767 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1768 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1769 // offset of __forwarding field
1770 offset =
1771 CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8);
1772 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1773 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1774 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1775 // offset of x field
1776 offset = CharUnits::fromQuantity(XOffset/8);
1777 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1778
1779 // Create the descriptor for the variable.
1780 llvm::DIVariable D =
1781 DBuilder.CreateComplexVariable(Tag,
1782 llvm::DIDescriptor(RegionStack.back()),
1783 VD->getName(), Unit, Line, Ty,
1784 addr.data(), addr.size());
1785
1786 // Insert an llvm.dbg.declare into the current block.
1787 llvm::Instruction *Call =
1788 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1789
1790 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1791 return;
1792 }
1793 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00001794 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001795 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1796 Name, Unit, Line, Ty,
1797 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001798
1799 // Insert an llvm.dbg.declare into the current block.
1800 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001801 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001802
1803 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001804 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001805 }
1806
1807 // If VD is an anonymous union then Storage represents value for
1808 // all union fields.
1809 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1810 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1811 if (RD->isUnion()) {
1812 for (RecordDecl::field_iterator I = RD->field_begin(),
1813 E = RD->field_end();
1814 I != E; ++I) {
1815 FieldDecl *Field = *I;
1816 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1817 llvm::StringRef FieldName = Field->getName();
1818
1819 // Ignore unnamed fields. Do not ignore unnamed records.
1820 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1821 continue;
1822
1823 // Use VarDecl's Tag, Scope and Line number.
1824 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001825 DBuilder.CreateLocalVariable(Tag, llvm::DIDescriptor(Scope),
1826 FieldName, Unit, Line, FieldTy,
1827 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001828
1829 // Insert an llvm.dbg.declare into the current block.
1830 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001831 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001832
1833 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1834 }
1835 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001836}
1837
Mike Stumpb1a6e682009-09-30 02:43:10 +00001838/// EmitDeclare - Emit local variable declaration debug info.
John McCall6b5a61b2011-02-07 10:33:21 +00001839void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001840 llvm::Value *Storage, CGBuilderTy &Builder,
John McCall6b5a61b2011-02-07 10:33:21 +00001841 const CGBlockInfo &blockInfo) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001842 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1843
Devang Patel2b594b92010-04-26 23:28:46 +00001844 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001845 return;
1846
John McCall6b5a61b2011-02-07 10:33:21 +00001847 bool isByRef = VD->hasAttr<BlocksAttr>();
1848
Mike Stumpb1a6e682009-09-30 02:43:10 +00001849 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001850 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001851 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00001852 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00001853 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1854 else
1855 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001856
1857 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001858 unsigned Line = getLineNumber(VD->getLocation());
1859 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001860
John McCall6b5a61b2011-02-07 10:33:21 +00001861 const llvm::TargetData &target = CGM.getTargetData();
1862
1863 CharUnits offset = CharUnits::fromQuantity(
1864 target.getStructLayout(blockInfo.StructureType)
1865 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
1866
Mike Stumpb1a6e682009-09-30 02:43:10 +00001867 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001868 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1869 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1870 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1871 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00001872 if (isByRef) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001873 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1874 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001875 // offset of __forwarding field
John McCall6b5a61b2011-02-07 10:33:21 +00001876 offset = CharUnits::fromQuantity(target.getPointerSize()/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001877 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1878 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1879 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001880 // offset of x field
1881 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001882 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001883 }
1884
1885 // Create the descriptor for the variable.
1886 llvm::DIVariable D =
Devang Patel823d8e92010-12-08 22:42:58 +00001887 DBuilder.CreateComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
1888 VD->getName(), Unit, Line, Ty,
1889 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001890 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001891 llvm::Instruction *Call =
Devang Patel823d8e92010-12-08 22:42:58 +00001892 DBuilder.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001893
Chris Lattnerc6034632010-04-01 06:31:43 +00001894 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001895 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001896}
1897
Devang Pateld6c5a262010-02-01 21:52:22 +00001898void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001899 llvm::Value *Storage,
1900 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001901 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001902}
1903
Mike Stumpb1a6e682009-09-30 02:43:10 +00001904void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
John McCall6b5a61b2011-02-07 10:33:21 +00001905 const VarDecl *variable, llvm::Value *Storage, CGBuilderTy &Builder,
1906 const CGBlockInfo &blockInfo) {
1907 EmitDeclare(variable, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder,
1908 blockInfo);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001909}
1910
Chris Lattner9c85ba32008-11-10 06:08:34 +00001911/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1912/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001913void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001914 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001915 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001916}
1917
1918
1919
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001920/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001921void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001922 const VarDecl *D) {
1923
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001924 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001925 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001926 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001927
Devang Pateleb6d79b2010-02-01 21:34:11 +00001928 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001929 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001931 // CodeGen turns int[] into int[1] so we'll do the same here.
1932 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001934 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001935 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001936
Anders Carlsson20f12a22009-12-06 18:00:51 +00001937 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001938 ArrayType::Normal, 0);
1939 }
Devang Patel5d822f02010-04-29 17:48:37 +00001940 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001941 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001942 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001943 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00001944 if (LinkageName == DeclName)
1945 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001946 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00001947 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +00001948 DBuilder.CreateStaticVariable(DContext, DeclName, LinkageName,
1949 Unit, LineNo, getOrCreateType(T, Unit),
1950 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001951}
1952
Devang Patel9ca36b62009-02-26 21:10:26 +00001953/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001954void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001955 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001956 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001957 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001958 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001959
Devang Pateld6c5a262010-02-01 21:52:22 +00001960 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001961
Devang Pateld6c5a262010-02-01 21:52:22 +00001962 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001963 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Devang Patel9ca36b62009-02-26 21:10:26 +00001965 // CodeGen turns int[] into int[1] so we'll do the same here.
1966 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Devang Patel9ca36b62009-02-26 21:10:26 +00001968 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001969 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Anders Carlsson20f12a22009-12-06 18:00:51 +00001971 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001972 ArrayType::Normal, 0);
1973 }
1974
Devang Patel823d8e92010-12-08 22:42:58 +00001975 DBuilder.CreateGlobalVariable(Name, Unit, LineNo,
1976 getOrCreateType(T, Unit),
1977 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00001978}
Devang Patelabb485f2010-02-01 19:16:32 +00001979
Devang Patel25c2c8f2010-08-10 17:53:33 +00001980/// EmitGlobalVariable - Emit global variable's debug info.
1981void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001982 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001983 // Create the descriptor for the variable.
1984 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1985 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001986 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001987 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1988 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel31f7d022011-01-17 22:23:07 +00001989 Ty = CreateEnumType(ED);
Devang Patel6237cea2010-08-23 22:07:25 +00001990 }
Devang Patel0317ab02010-08-10 18:27:15 +00001991 // Do not use DIGlobalVariable for enums.
1992 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1993 return;
Devang Patel823d8e92010-12-08 22:42:58 +00001994 DBuilder.CreateStaticVariable(Unit, Name, Name, Unit,
1995 getLineNumber(VD->getLocation()),
1996 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001997}
1998
Devang Patelabb485f2010-02-01 19:16:32 +00001999/// getOrCreateNamesSpace - Return namespace descriptor for the given
2000/// namespace decl.
2001llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002002CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002003 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2004 NameSpaceCache.find(NSDecl);
2005 if (I != NameSpaceCache.end())
2006 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2007
Devang Patel8ab870d2010-05-12 23:46:38 +00002008 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002009 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002010 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002011 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002012 llvm::DINameSpace NS =
Devang Patel823d8e92010-12-08 22:42:58 +00002013 DBuilder.CreateNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002014 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002015 return NS;
2016}