blob: 313c02dc6fdcc3addbdb115a8750a03d2769f2b5 [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() {
Eric Christopheraa2164c2011-09-29 00:00:45 +000049 assert(LexicalBlockStack.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())
Chandler Carruth40278532011-07-25 16:49:02 +000054 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(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.
Chris Lattner5f9e2722011-07-23 10:55:15 +000084StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Devang Patel9c6c3a02010-01-14 00:36:21 +000085 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());
Chris Lattner5f9e2722011-07-23 10:55:15 +000096 return StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000097}
98
Chris Lattner5f9e2722011-07-23 10:55:15 +000099StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
David Chisnall52044a22010-09-02 18:01:51 +0000100 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());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 return StringRef(StrPtr, OS.tell());
David Chisnall52044a22010-09-02 18:01:51 +0000120}
121
Devang Patel1f15c192011-04-18 17:30:25 +0000122/// getSelectorName - Return selector name. This is used for debugging
Devang Patel90c1eed2011-04-16 00:37:51 +0000123/// info.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000124StringRef CGDebugInfo::getSelectorName(Selector S) {
Devang Patel90c1eed2011-04-16 00:37:51 +0000125 llvm::SmallString<256> SName;
126 llvm::raw_svector_ostream OS(SName);
127 OS << S.getAsString();
128 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
129 memcpy(StrPtr, SName.begin(), OS.tell());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 return StringRef(StrPtr, OS.tell());
Devang Patel90c1eed2011-04-16 00:37:51 +0000131}
132
Devang Patel700a1cb2010-07-20 20:24:18 +0000133/// getClassName - Get class name including template argument list.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000134StringRef
Devang Patel700a1cb2010-07-20 20:24:18 +0000135CGDebugInfo::getClassName(RecordDecl *RD) {
136 ClassTemplateSpecializationDecl *Spec
137 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
138 if (!Spec)
139 return RD->getName();
140
141 const TemplateArgument *Args;
142 unsigned NumArgs;
143 std::string Buffer;
144 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
145 const TemplateSpecializationType *TST =
146 cast<TemplateSpecializationType>(TAW->getType());
147 Args = TST->getArgs();
148 NumArgs = TST->getNumArgs();
149 } else {
150 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000151 Args = TemplateArgs.data();
152 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000153 }
154 Buffer = RD->getIdentifier()->getNameStart();
155 PrintingPolicy Policy(CGM.getLangOptions());
156 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
157 NumArgs,
158 Policy);
159
160 // Copy this name on the side and use its reference.
161 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
162 memcpy(StrPtr, Buffer.data(), Buffer.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163 return StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000164}
165
Devang Patel17800552010-03-09 00:44:50 +0000166/// getOrCreateFile - Get the file debug info descriptor for the input location.
167llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000168 if (!Loc.isValid())
169 // If Location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000170 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel823d8e92010-12-08 22:42:58 +0000171
Anders Carlsson20f12a22009-12-06 18:00:51 +0000172 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000173 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000174
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregor8c457a82010-11-11 20:45:16 +0000176 // If the location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000177 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000178
Ted Kremenek9c250392010-03-30 00:27:51 +0000179 // Cache the results.
180 const char *fname = PLoc.getFilename();
181 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
182 DIFileCache.find(fname);
183
184 if (it != DIFileCache.end()) {
185 // Verify that the information still exists.
186 if (&*it->second)
187 return llvm::DIFile(cast<llvm::MDNode>(it->second));
188 }
189
Devang Patel16674e82011-02-22 18:56:36 +0000190 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000191
Devang Patelab699792010-05-07 18:12:35 +0000192 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000193 return F;
194
Devang Patel17800552010-03-09 00:44:50 +0000195}
Devang Patel8ab870d2010-05-12 23:46:38 +0000196
Devang Patel532105f2010-10-28 22:03:20 +0000197/// getOrCreateMainFile - Get the file info for main compile unit.
198llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel16674e82011-02-22 18:56:36 +0000199 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000200}
201
Devang Patel8ab870d2010-05-12 23:46:38 +0000202/// getLineNumber - Get line number for the location. If location is invalid
203/// then use current location.
204unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
205 assert (CurLoc.isValid() && "Invalid current location!");
206 SourceManager &SM = CGM.getContext().getSourceManager();
207 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000208 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000209}
210
211/// getColumnNumber - Get column number for the location. If location is
212/// invalid then use current location.
213unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
214 assert (CurLoc.isValid() && "Invalid current location!");
215 SourceManager &SM = CGM.getContext().getSourceManager();
216 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000217 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000218}
219
Chris Lattner5f9e2722011-07-23 10:55:15 +0000220StringRef CGDebugInfo::getCurrentDirname() {
Devang Patelac4d13c2010-07-27 15:17:16 +0000221 if (!CWDName.empty())
222 return CWDName;
223 char *CompDirnamePtr = NULL;
224 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
225 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
226 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000227 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patelac4d13c2010-07-27 15:17:16 +0000228}
229
Devang Patel17800552010-03-09 00:44:50 +0000230/// CreateCompileUnit - Create new compile unit.
231void CGDebugInfo::CreateCompileUnit() {
232
233 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000234 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000235 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
236 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000237 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000238
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000239 // The main file name provided via the "-main-file-name" option contains just
240 // the file name itself with no path information. This file name may have had
241 // a relative path, so we look into the actual file entry for the main
242 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000243 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000244 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000245 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000246 if (MainFileDir != ".")
247 MainFileName = MainFileDir + "/" + MainFileName;
248 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000249
Devang Patelac4d13c2010-07-27 15:17:16 +0000250 // Save filename string.
251 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
252 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000253 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patelac4d13c2010-07-27 15:17:16 +0000254
Chris Lattner515455a2009-03-25 03:28:08 +0000255 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000256 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000257 if (LO.CPlusPlus) {
258 if (LO.ObjC1)
259 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
260 else
261 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
262 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000263 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000264 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000265 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000266 } else {
267 LangTag = llvm::dwarf::DW_LANG_C89;
268 }
Devang Patel446c6192009-04-17 21:06:59 +0000269
Daniel Dunbar19f19832010-08-24 17:41:09 +0000270 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000271
272 // Figure out which version of the ObjC runtime we have.
273 unsigned RuntimeVers = 0;
274 if (LO.ObjC1)
275 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000277 // Create new compile unit.
Devang Patel16674e82011-02-22 18:56:36 +0000278 DBuilder.createCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000279 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000280 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000281 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000282 // FIXME - Eliminate TheCU.
283 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000284}
285
Devang Patel65e99f22009-02-25 01:36:11 +0000286/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000287/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000288llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000289 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000290 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000291 switch (BT->getKind()) {
Devang Patele7566cf2011-09-12 18:50:21 +0000292 case BuiltinType::Dependent:
David Blaikieb219cfc2011-09-23 05:06:16 +0000293 llvm_unreachable("Unexpected builtin type Dependent");
Devang Patele7566cf2011-09-12 18:50:21 +0000294 case BuiltinType::Overload:
David Blaikieb219cfc2011-09-23 05:06:16 +0000295 llvm_unreachable("Unexpected builtin type Overload");
Devang Patele7566cf2011-09-12 18:50:21 +0000296 case BuiltinType::BoundMember:
David Blaikieb219cfc2011-09-23 05:06:16 +0000297 llvm_unreachable("Unexpected builtin type BoundMember");
Devang Patele7566cf2011-09-12 18:50:21 +0000298 case BuiltinType::UnknownAny:
David Blaikieb219cfc2011-09-23 05:06:16 +0000299 llvm_unreachable("Unexpected builtin type UnknownAny");
Devang Patele7566cf2011-09-12 18:50:21 +0000300 case BuiltinType::NullPtr:
Devang Patelf60dca32011-09-14 23:14:14 +0000301 return DBuilder.
302 createNullPtrType(BT->getName(CGM.getContext().getLangOptions()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000303 case BuiltinType::Void:
304 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000305 case BuiltinType::ObjCClass:
Devang Patel16674e82011-02-22 18:56:36 +0000306 return DBuilder.createStructType(TheCU, "objc_class",
Devang Patel823d8e92010-12-08 22:42:58 +0000307 getOrCreateMainFile(), 0, 0, 0,
308 llvm::DIDescriptor::FlagFwdDecl,
309 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000310 case BuiltinType::ObjCId: {
311 // typedef struct objc_class *Class;
312 // typedef struct objc_object {
313 // Class isa;
314 // } *id;
315
316 llvm::DIType OCTy =
Devang Patel16674e82011-02-22 18:56:36 +0000317 DBuilder.createStructType(TheCU, "objc_class",
Devang Patel823d8e92010-12-08 22:42:58 +0000318 getOrCreateMainFile(), 0, 0, 0,
319 llvm::DIDescriptor::FlagFwdDecl,
320 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000321 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
322
Devang Patel16674e82011-02-22 18:56:36 +0000323 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000324
Chris Lattner5f9e2722011-07-23 10:55:15 +0000325 SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000326 llvm::DIType FieldTy =
Devang Patel1d323e02011-06-24 22:00:59 +0000327 DBuilder.createMemberType(getOrCreateMainFile(), "isa",
328 getOrCreateMainFile(), 0, Size,
329 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000330 EltTys.push_back(FieldTy);
Jay Foadc556ef22011-04-24 10:11:03 +0000331 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patelc8972c62010-07-28 01:33:15 +0000332
Devang Patel16674e82011-02-22 18:56:36 +0000333 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel823d8e92010-12-08 22:42:58 +0000334 getOrCreateMainFile(),
335 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000336 }
Devang Patel6e108ce2011-02-09 03:15:05 +0000337 case BuiltinType::ObjCSel: {
Devang Patel16674e82011-02-22 18:56:36 +0000338 return DBuilder.createStructType(TheCU, "objc_selector",
Devang Patel6e108ce2011-02-09 03:15:05 +0000339 getOrCreateMainFile(), 0, 0, 0,
340 llvm::DIDescriptor::FlagFwdDecl,
341 llvm::DIArray());
342 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000343 case BuiltinType::UChar:
344 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
345 case BuiltinType::Char_S:
346 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patele8ee3f22011-09-12 17:11:58 +0000347 case BuiltinType::Char16:
348 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000349 case BuiltinType::UShort:
350 case BuiltinType::UInt:
Devang Patel31c79b42011-05-05 17:06:30 +0000351 case BuiltinType::UInt128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000352 case BuiltinType::ULong:
Devang Patel68f76b12011-09-10 00:44:49 +0000353 case BuiltinType::WChar_U:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000354 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
355 case BuiltinType::Short:
356 case BuiltinType::Int:
Devang Patel31c79b42011-05-05 17:06:30 +0000357 case BuiltinType::Int128:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000358 case BuiltinType::Long:
Devang Patel68f76b12011-09-10 00:44:49 +0000359 case BuiltinType::WChar_S:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000360 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
361 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
362 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000363 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000364 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000365 }
Devang Patel05127ca2010-07-28 23:23:29 +0000366
367 switch (BT->getKind()) {
368 case BuiltinType::Long: BTName = "long int"; break;
369 case BuiltinType::LongLong: BTName = "long long int"; break;
370 case BuiltinType::ULong: BTName = "long unsigned int"; break;
371 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
372 default:
373 BTName = BT->getName(CGM.getContext().getLangOptions());
374 break;
375 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000377 uint64_t Size = CGM.getContext().getTypeSize(BT);
378 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000379 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000380 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000381 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000382}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000383
Devang Patel344ff5d2010-12-09 00:25:29 +0000384llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000385 // Bit size, align and offset of the type.
386 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
387 if (Ty->isComplexIntegerType())
388 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Anders Carlsson20f12a22009-12-06 18:00:51 +0000390 uint64_t Size = CGM.getContext().getTypeSize(Ty);
391 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000392 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000393 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel823d8e92010-12-08 22:42:58 +0000394
Devang Patelca80a5f2009-10-20 19:55:01 +0000395 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000396}
397
John McCalla1805292009-09-25 01:40:47 +0000398/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000399/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000400llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000401 QualifierCollector Qc;
402 const Type *T = Qc.strip(Ty);
403
404 // Ignore these qualifiers for now.
405 Qc.removeObjCGCAttr();
406 Qc.removeAddressSpace();
John McCallf85e1932011-06-15 23:02:42 +0000407 Qc.removeObjCLifetime();
John McCalla1805292009-09-25 01:40:47 +0000408
Chris Lattner9c85ba32008-11-10 06:08:34 +0000409 // We will create one Derived type for one qualifier and recurse to handle any
410 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000412 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000413 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000414 Qc.removeConst();
415 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000416 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000417 Qc.removeVolatile();
418 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000419 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000420 Qc.removeRestrict();
421 } else {
422 assert(Qc.empty() && "Unknown type qualifier for debug info");
423 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000424 }
Mike Stump1eb44332009-09-09 15:08:12 +0000425
John McCall49f4e1c2010-12-10 11:01:00 +0000426 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000427
Daniel Dunbar3845f862008-10-31 03:54:29 +0000428 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
429 // CVR derived types.
Devang Patel16674e82011-02-22 18:56:36 +0000430 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000431
Devang Patelca80a5f2009-10-20 19:55:01 +0000432 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000433}
434
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000435llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000436 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000437 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000438 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
439 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000440 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000441}
442
Chris Lattner9c85ba32008-11-10 06:08:34 +0000443llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000444 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000445 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
446 Ty->getPointeeType(), Unit);
447}
448
Eric Christopheredc95922011-09-13 23:45:09 +0000449/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patelc69e1cf2010-09-30 19:05:55 +0000450/// then emit record's fwd if debug info size reduction is enabled.
451llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
452 llvm::DIFile Unit) {
453 if (!CGM.getCodeGenOpts().LimitDebugInfo)
454 return getOrCreateType(PointeeTy, Unit);
455
456 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
457 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000458 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
459 unsigned Line = getLineNumber(RD->getLocation());
460 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +0000461 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000462
463 if (RD->isStruct())
Devang Patel16674e82011-02-22 18:56:36 +0000464 return DBuilder.createStructType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000465 Line, 0, 0, llvm::DIType::FlagFwdDecl,
466 llvm::DIArray());
467 else if (RD->isUnion())
Devang Patel16674e82011-02-22 18:56:36 +0000468 return DBuilder.createUnionType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000469 Line, 0, 0, llvm::DIType::FlagFwdDecl,
470 llvm::DIArray());
471 else {
472 assert(RD->isClass() && "Unknown RecordType!");
Devang Patel16674e82011-02-22 18:56:36 +0000473 return DBuilder.createClassType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000474 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
475 llvm::DIType(), llvm::DIArray());
476 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000477 }
478 return getOrCreateType(PointeeTy, Unit);
479
480}
481
Anders Carlssona031b352009-11-06 19:19:55 +0000482llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
483 const Type *Ty,
484 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000485 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000486
487 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Patel16674e82011-02-22 18:56:36 +0000488 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel823d8e92010-12-08 22:42:58 +0000489
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000490 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000491 // Size is always the size of a pointer. We can't use getTypeSize here
492 // because that does not return the correct value for references.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000493 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregorbcfd1f52011-09-02 00:18:52 +0000494 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000495 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Devang Patel823d8e92010-12-08 22:42:58 +0000497 return
Devang Patel16674e82011-02-22 18:56:36 +0000498 DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000499}
500
Mike Stump9bc093c2009-05-14 02:03:51 +0000501llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000502 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000503 if (BlockLiteralGenericSet)
504 return BlockLiteralGeneric;
505
Chris Lattner5f9e2722011-07-23 10:55:15 +0000506 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000507 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000508 QualType FType;
509 uint64_t FieldSize, FieldOffset;
510 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000511 llvm::DIArray Elements;
512 llvm::DIType EltTy, DescTy;
513
514 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000515 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000516 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
517 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000518
Jay Foadc556ef22011-04-24 10:11:03 +0000519 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000520 EltTys.clear();
521
Devang Patele2472482010-09-29 21:05:52 +0000522 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000523 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000524
Devang Patel16674e82011-02-22 18:56:36 +0000525 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel823d8e92010-12-08 22:42:58 +0000526 Unit, LineNo, FieldOffset, 0,
527 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Mike Stump9bc093c2009-05-14 02:03:51 +0000529 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000530 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Devang Patel16674e82011-02-22 18:56:36 +0000532 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000533
534 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000535 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000536 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000537 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000538 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
539 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000540 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000541 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000542
Anders Carlsson20f12a22009-12-06 18:00:51 +0000543 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000544 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000545 FieldSize = CGM.getContext().getTypeSize(Ty);
546 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel1d323e02011-06-24 22:00:59 +0000547 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000548 LineNo, FieldSize, FieldAlign,
549 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000550 EltTys.push_back(FieldTy);
551
552 FieldOffset += FieldSize;
Jay Foadc556ef22011-04-24 10:11:03 +0000553 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump9bc093c2009-05-14 02:03:51 +0000554
Devang Patel16674e82011-02-22 18:56:36 +0000555 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel823d8e92010-12-08 22:42:58 +0000556 Unit, LineNo, FieldOffset, 0,
557 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Mike Stump9bc093c2009-05-14 02:03:51 +0000559 BlockLiteralGenericSet = true;
Devang Patel16674e82011-02-22 18:56:36 +0000560 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000561 return BlockLiteralGeneric;
562}
563
Chris Lattner9c85ba32008-11-10 06:08:34 +0000564llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000565 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000566 // Typedefs are derived from some other type. If we have a typedef of a
567 // typedef, make sure to emit the whole chain.
568 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000569 if (!Src.Verify())
570 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000571 // We don't set size information, but do specify where the typedef was
572 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000573 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelc4903122011-06-03 17:23:47 +0000574 const TypedefNameDecl *TyDecl = Ty->getDecl();
575 llvm::DIDescriptor TydefContext =
576 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
577
578 return
579 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TydefContext);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000580}
581
Chris Lattner9c85ba32008-11-10 06:08:34 +0000582llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000583 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000584 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000585
Chris Lattner9c85ba32008-11-10 06:08:34 +0000586 // Add the result type at least.
587 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Chris Lattner9c85ba32008-11-10 06:08:34 +0000589 // Set up remainder of arguments if there is a prototype.
590 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000591 if (isa<FunctionNoProtoType>(Ty))
Devang Patel16674e82011-02-22 18:56:36 +0000592 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000593 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000594 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
595 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000596 }
597
Jay Foadc556ef22011-04-24 10:11:03 +0000598 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Devang Patel16674e82011-02-22 18:56:36 +0000600 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000601 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000602}
603
Chris Lattner5f9e2722011-07-23 10:55:15 +0000604llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall8178df32011-02-22 22:38:33 +0000605 QualType type,
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000606 uint64_t sizeInBitsOverride,
John McCall8178df32011-02-22 22:38:33 +0000607 SourceLocation loc,
608 AccessSpecifier AS,
609 uint64_t offsetInBits,
Devang Patel1d323e02011-06-24 22:00:59 +0000610 llvm::DIFile tunit,
611 llvm::DIDescriptor scope) {
John McCall8178df32011-02-22 22:38:33 +0000612 llvm::DIType debugType = getOrCreateType(type, tunit);
613
614 // Get the location for the field.
615 llvm::DIFile file = getOrCreateFile(loc);
616 unsigned line = getLineNumber(loc);
617
618 uint64_t sizeInBits = 0;
619 unsigned alignInBits = 0;
620 if (!type->isIncompleteArrayType()) {
621 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
622
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000623 if (sizeInBitsOverride)
624 sizeInBits = sizeInBitsOverride;
John McCall8178df32011-02-22 22:38:33 +0000625 }
626
627 unsigned flags = 0;
628 if (AS == clang::AS_private)
629 flags |= llvm::DIDescriptor::FlagPrivate;
630 else if (AS == clang::AS_protected)
631 flags |= llvm::DIDescriptor::FlagProtected;
632
Devang Patel1d323e02011-06-24 22:00:59 +0000633 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
634 alignInBits, offsetInBits, flags, debugType);
John McCall8178df32011-02-22 22:38:33 +0000635}
636
Devang Patel428deb52010-01-19 00:00:59 +0000637/// CollectRecordFields - A helper function to collect debug info for
638/// record fields. This is used while creating debug info entry for a Record.
639void CGDebugInfo::
John McCall8178df32011-02-22 22:38:33 +0000640CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000641 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel1d323e02011-06-24 22:00:59 +0000642 llvm::DIType RecordTy) {
John McCall8178df32011-02-22 22:38:33 +0000643 unsigned fieldNo = 0;
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000644 const FieldDecl *LastFD = 0;
645 bool IsMsStruct = record->hasAttr<MsStructAttr>();
646
John McCall8178df32011-02-22 22:38:33 +0000647 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
648 for (RecordDecl::field_iterator I = record->field_begin(),
649 E = record->field_end();
650 I != E; ++I, ++fieldNo) {
651 FieldDecl *field = *I;
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000652 if (IsMsStruct) {
653 // Zero-length bitfields following non-bitfield members are ignored
Fariborz Jahanian855a8e72011-05-03 20:21:04 +0000654 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000655 --fieldNo;
656 continue;
657 }
658 LastFD = field;
659 }
Devang Patel428deb52010-01-19 00:00:59 +0000660
Chris Lattner5f9e2722011-07-23 10:55:15 +0000661 StringRef name = field->getName();
John McCall8178df32011-02-22 22:38:33 +0000662 QualType type = field->getType();
663
664 // Ignore unnamed fields unless they're anonymous structs/unions.
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000665 if (name.empty() && !type->isRecordType()) {
666 LastFD = field;
Devang Patel428deb52010-01-19 00:00:59 +0000667 continue;
Fariborz Jahanianfbc3cc62011-04-28 23:43:23 +0000668 }
Devang Patel428deb52010-01-19 00:00:59 +0000669
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000670 uint64_t SizeInBitsOverride = 0;
671 if (field->isBitField()) {
672 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
673 assert(SizeInBitsOverride && "found named 0-width bitfield");
674 }
675
John McCall8178df32011-02-22 22:38:33 +0000676 llvm::DIType fieldType
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000677 = createFieldType(name, type, SizeInBitsOverride,
John McCall8178df32011-02-22 22:38:33 +0000678 field->getLocation(), field->getAccess(),
Devang Patel1d323e02011-06-24 22:00:59 +0000679 layout.getFieldOffset(fieldNo), tunit, RecordTy);
Devang Patel428deb52010-01-19 00:00:59 +0000680
John McCall8178df32011-02-22 22:38:33 +0000681 elements.push_back(fieldType);
Devang Patel428deb52010-01-19 00:00:59 +0000682 }
683}
684
Devang Patela6da1922010-01-28 00:28:01 +0000685/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
686/// function type is not updated to include implicit "this" pointer. Use this
687/// routine to get a method type which includes "this" pointer.
688llvm::DIType
689CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000690 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000691 llvm::DIType FnTy
692 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
693 0),
694 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000695
Devang Patela6da1922010-01-28 00:28:01 +0000696 // Add "this" pointer.
Devang Patelab699792010-05-07 18:12:35 +0000697 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000698 assert (Args.getNumElements() && "Invalid number of arguments!");
699
Chris Lattner5f9e2722011-07-23 10:55:15 +0000700 SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000701
702 // First element is always return type. For 'void' functions it is NULL.
703 Elts.push_back(Args.getElement(0));
704
Eric Christopher2121cda2011-09-14 01:10:50 +0000705 if (!Method->isStatic()) {
706 // "this" pointer is always first argument.
707 QualType ThisPtr = Method->getThisType(CGM.getContext());
708 llvm::DIType ThisPtrType =
709 DBuilder.createArtificialType(getOrCreateType(ThisPtr, Unit));
710
711 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
712 Elts.push_back(ThisPtrType);
713 }
Devang Patela6da1922010-01-28 00:28:01 +0000714
715 // Copy rest of the arguments.
716 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
717 Elts.push_back(Args.getElement(i));
718
Jay Foadc556ef22011-04-24 10:11:03 +0000719 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patela6da1922010-01-28 00:28:01 +0000720
Devang Patel16674e82011-02-22 18:56:36 +0000721 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000722}
723
Devang Patel58faf202010-10-22 17:11:50 +0000724/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
725/// inside a function.
726static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
727 if (const CXXRecordDecl *NRD =
728 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
729 return isFunctionLocalClass(NRD);
730 else if (isa<FunctionDecl>(RD->getDeclContext()))
731 return true;
732 return false;
733
734}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000735/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
736/// a single member function GlobalDecl.
737llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000738CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000739 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000740 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000741 bool IsCtorOrDtor =
742 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
743
Chris Lattner5f9e2722011-07-23 10:55:15 +0000744 StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000745 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000746
747 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
748 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000749 StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000750 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000751 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000752
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000753 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000754 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
755 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000756
757 // Collect virtual method info.
758 llvm::DIType ContainingType;
759 unsigned Virtuality = 0;
760 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000761
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000762 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000763 if (Method->isPure())
764 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
765 else
766 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
767
768 // It doesn't make sense to give a virtual destructor a vtable index,
769 // since a single destructor has two entries in the vtable.
770 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000771 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000772 ContainingType = RecordTy;
773 }
774
Devang Patele2472482010-09-29 21:05:52 +0000775 unsigned Flags = 0;
776 if (Method->isImplicit())
777 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000778 AccessSpecifier Access = Method->getAccess();
779 if (Access == clang::AS_private)
780 Flags |= llvm::DIDescriptor::FlagPrivate;
781 else if (Access == clang::AS_protected)
782 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000783 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
784 if (CXXC->isExplicit())
785 Flags |= llvm::DIDescriptor::FlagExplicit;
786 } else if (const CXXConversionDecl *CXXC =
787 dyn_cast<CXXConversionDecl>(Method)) {
788 if (CXXC->isExplicit())
789 Flags |= llvm::DIDescriptor::FlagExplicit;
790 }
Devang Patel3951e712010-10-07 22:03:49 +0000791 if (Method->hasPrototype())
792 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000793
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000794 llvm::DISubprogram SP =
Nick Lewycky7803ec82011-09-01 21:49:51 +0000795 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +0000796 MethodDefUnit, MethodLine,
797 MethodTy, /*isLocalToUnit=*/false,
798 /* isDefinition=*/ false,
799 Virtuality, VIndex, ContainingType,
800 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000801
Devang Patel22a5cdf2011-04-29 23:42:32 +0000802 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000803
804 return SP;
805}
806
Devang Patel4125fd22010-01-19 01:54:44 +0000807/// CollectCXXMemberFunctions - A helper function to collect debug info for
808/// C++ member functions.This is used while creating debug info entry for
809/// a Record.
810void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000811CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000812 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000813 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000814 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
815 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000816 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000817
Devang Pateld5322da2010-02-09 19:09:28 +0000818 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000819 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000820
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000821 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000822 }
823}
824
Devang Patel2ed8f002010-08-27 17:47:47 +0000825/// CollectCXXFriends - A helper function to collect debug info for
826/// C++ base classes. This is used while creating debug info entry for
827/// a Record.
828void CGDebugInfo::
829CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000830 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000831 llvm::DIType RecordTy) {
Devang Patel2ed8f002010-08-27 17:47:47 +0000832 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
833 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky7803ec82011-09-01 21:49:51 +0000834 if ((*BI)->isUnsupportedFriend())
835 continue;
Devang Patel823d8e92010-12-08 22:42:58 +0000836 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Patel16674e82011-02-22 18:56:36 +0000837 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +0000838 getOrCreateType(TInfo->getType(),
839 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000840 }
841}
842
Devang Patela245c5b2010-01-25 23:32:18 +0000843/// CollectCXXBases - A helper function to collect debug info for
844/// C++ base classes. This is used while creating debug info entry for
845/// a Record.
846void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000847CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000848 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000849 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000850
Devang Patel239cec62010-02-01 21:39:52 +0000851 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
852 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
853 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000854 unsigned BFlags = 0;
Devang Patel62c117d2011-04-04 20:36:06 +0000855 uint64_t BaseOffset;
Devang Patelca7daed2010-01-28 21:54:15 +0000856
857 const CXXRecordDecl *Base =
858 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
859
860 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000861 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000862 // expression where it expects +ve number.
Ken Dyck14c65ca2011-04-07 12:37:09 +0000863 BaseOffset =
Peter Collingbourne1d2b3172011-09-26 01:56:30 +0000864 0 - CGM.getVTableContext()
865 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Patele2472482010-09-29 21:05:52 +0000866 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000867 } else
Devang Patel62c117d2011-04-04 20:36:06 +0000868 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Ken Dyck14c65ca2011-04-07 12:37:09 +0000869 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
870 // BI->isVirtual() and bits when not.
Devang Patelca7daed2010-01-28 21:54:15 +0000871
872 AccessSpecifier Access = BI->getAccessSpecifier();
873 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000874 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000875 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000876 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000877
Devang Patel823d8e92010-12-08 22:42:58 +0000878 llvm::DIType DTy =
Devang Patel16674e82011-02-22 18:56:36 +0000879 DBuilder.createInheritance(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +0000880 getOrCreateType(BI->getType(), Unit),
Devang Patel62c117d2011-04-04 20:36:06 +0000881 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000882 EltTys.push_back(DTy);
883 }
Devang Patela245c5b2010-01-25 23:32:18 +0000884}
885
Devang Patel5ecb1df2011-04-05 22:54:11 +0000886/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel9c1714b2011-04-05 17:30:54 +0000887llvm::DIArray CGDebugInfo::
Devang Patel5ecb1df2011-04-05 22:54:11 +0000888CollectTemplateParams(const TemplateParameterList *TPList,
889 const TemplateArgumentList &TAList,
890 llvm::DIFile Unit) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000891 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patelc5ce2972011-04-05 20:15:06 +0000892 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
893 const TemplateArgument &TA = TAList[i];
Devang Patel5ecb1df2011-04-05 22:54:11 +0000894 const NamedDecl *ND = TPList->getParam(i);
Devang Patel9c1714b2011-04-05 17:30:54 +0000895 if (TA.getKind() == TemplateArgument::Type) {
896 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
897 llvm::DITemplateTypeParameter TTP =
Devang Patelc5ce2972011-04-05 20:15:06 +0000898 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel9c1714b2011-04-05 17:30:54 +0000899 TemplateParams.push_back(TTP);
900 } else if (TA.getKind() == TemplateArgument::Integral) {
901 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel9c1714b2011-04-05 17:30:54 +0000902 llvm::DITemplateValueParameter TVP =
Devang Patelc5ce2972011-04-05 20:15:06 +0000903 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
904 TA.getAsIntegral()->getZExtValue());
Devang Patel9c1714b2011-04-05 17:30:54 +0000905 TemplateParams.push_back(TVP);
906 }
907 }
Jay Foadc556ef22011-04-24 10:11:03 +0000908 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel9c1714b2011-04-05 17:30:54 +0000909}
910
Devang Patel5ecb1df2011-04-05 22:54:11 +0000911/// CollectFunctionTemplateParams - A helper function to collect debug
912/// info for function template parameters.
913llvm::DIArray CGDebugInfo::
914CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
915 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization){
916 const TemplateParameterList *TList =
917 FD->getTemplateSpecializationInfo()->getTemplate()->getTemplateParameters();
918 return
919 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
920 }
921 return llvm::DIArray();
922}
923
924/// CollectCXXTemplateParams - A helper function to collect debug info for
925/// template parameters.
926llvm::DIArray CGDebugInfo::
927CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
928 llvm::DIFile Unit) {
929 llvm::PointerUnion<ClassTemplateDecl *,
930 ClassTemplatePartialSpecializationDecl *>
931 PU = TSpecial->getSpecializedTemplateOrPartial();
932
933 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
934 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
935 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
936 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
937 return CollectTemplateParams(TPList, TAList, Unit);
938}
939
Devang Patel4ce3f202010-01-28 18:11:52 +0000940/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000941llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000942 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000943 return VTablePtrType;
944
945 ASTContext &Context = CGM.getContext();
946
947 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000948 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foadc556ef22011-04-24 10:11:03 +0000949 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Patel16674e82011-02-22 18:56:36 +0000950 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000951 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel16674e82011-02-22 18:56:36 +0000952 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel823d8e92010-12-08 22:42:58 +0000953 "__vtbl_ptr_type");
Devang Patel16674e82011-02-22 18:56:36 +0000954 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000955 return VTablePtrType;
956}
957
Anders Carlsson046c2942010-04-17 20:15:18 +0000958/// getVTableName - Get vtable name for the given Class.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000959StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000960 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000961 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000962
963 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000964 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000965 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner5f9e2722011-07-23 10:55:15 +0000966 return StringRef(StrPtr, Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000967}
968
969
Anders Carlsson046c2942010-04-17 20:15:18 +0000970/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000971/// debug info entry in EltTys vector.
972void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000973CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000974 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000975 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000976
977 // If there is a primary base then it will hold vtable info.
978 if (RL.getPrimaryBase())
979 return;
980
981 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000982 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000983 return;
984
985 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
986 llvm::DIType VPTR
Devang Patel1d323e02011-06-24 22:00:59 +0000987 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000988 0, Size, 0, 0, 0,
989 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000990 EltTys.push_back(VPTR);
991}
992
Devang Patelc69e1cf2010-09-30 19:05:55 +0000993/// getOrCreateRecordType - Emit record type's standalone debug info.
994llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
995 SourceLocation Loc) {
996 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel16674e82011-02-22 18:56:36 +0000997 DBuilder.retainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000998 return T;
999}
1000
Devang Patel65e99f22009-02-25 01:36:11 +00001001/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +00001002llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001003 RecordDecl *RD = Ty->getDecl();
Devang Patel31f7d022011-01-17 22:23:07 +00001004 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner9c85ba32008-11-10 06:08:34 +00001006 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +00001007 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1008 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner9c85ba32008-11-10 06:08:34 +00001010 // Records and classes and unions can all be recursive. To handle them, we
1011 // first generate a debug descriptor for the struct as a forward declaration.
1012 // Then (if it is a definition) we go through and get debug info for all of
1013 // its members. Finally, we create a descriptor for the complete type (which
1014 // may refer to the forward decl if the struct is recursive) and replace all
1015 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +00001016 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +00001017 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel0b897992010-07-08 19:56:29 +00001018
1019 // If this is just a forward declaration, construct an appropriately
1020 // marked node and just return it.
1021 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001022 llvm::DIType FwdDecl =
Devang Patel16674e82011-02-22 18:56:36 +00001023 DBuilder.createStructType(FDContext, RD->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001024 DefUnit, Line, 0, 0,
1025 llvm::DIDescriptor::FlagFwdDecl,
1026 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +00001027
1028 return FwdDecl;
1029 }
Devang Pateld0f251b2010-01-20 23:56:40 +00001030
Devang Patel16674e82011-02-22 18:56:36 +00001031 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Devang Patelab699792010-05-07 18:12:35 +00001033 llvm::MDNode *MN = FwdDecl;
1034 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001035 // Otherwise, insert it into the TypeCache so that recursive uses will find
1036 // it.
Devang Patelab699792010-05-07 18:12:35 +00001037 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001038 // Push the struct on region stack.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001039 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelab699792010-05-07 18:12:35 +00001040 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001041
1042 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001043 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001044
Devang Pateld6c5a262010-02-01 21:52:22 +00001045 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +00001046 if (CXXDecl) {
1047 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +00001048 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +00001049 }
Devang Pateldabc3e92010-08-12 00:02:44 +00001050
1051 // Collect static variables with initializers.
1052 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1053 I != E; ++I)
1054 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1055 if (const Expr *Init = V->getInit()) {
1056 Expr::EvalResult Result;
1057 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1058 llvm::ConstantInt *CI
1059 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1060
1061 // Create the descriptor for static variable.
1062 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001063 StringRef VName = V->getName();
Devang Pateldabc3e92010-08-12 00:02:44 +00001064 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1065 // Do not use DIGlobalVariable for enums.
1066 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel16674e82011-02-22 18:56:36 +00001067 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
Devang Patel823d8e92010-12-08 22:42:58 +00001068 getLineNumber(V->getLocation()),
1069 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +00001070 }
1071 }
1072 }
1073 }
1074
Devang Patel1d323e02011-06-24 22:00:59 +00001075 CollectRecordFields(RD, Unit, EltTys, FwdDecl);
Devang Patel9c1714b2011-04-05 17:30:54 +00001076 llvm::DIArray TParamsArray;
Devang Patel4ce3f202010-01-28 18:11:52 +00001077 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001078 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001079 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel9c1714b2011-04-05 17:30:54 +00001080 if (const ClassTemplateSpecializationDecl *TSpecial
1081 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
1082 TParamsArray = CollectCXXTemplateParams(TSpecial, Unit);
Devang Patel823d8e92010-12-08 22:42:58 +00001083 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001084
Eric Christopheraa2164c2011-09-29 00:00:45 +00001085 LexicalBlockStack.pop_back();
Devang Patel823d8e92010-12-08 22:42:58 +00001086 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1087 RegionMap.find(Ty->getDecl());
1088 if (RI != RegionMap.end())
1089 RegionMap.erase(RI);
1090
1091 llvm::DIDescriptor RDContext =
John McCall8178df32011-02-22 22:38:33 +00001092 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Chris Lattner5f9e2722011-07-23 10:55:15 +00001093 StringRef RDName = RD->getName();
Devang Patel823d8e92010-12-08 22:42:58 +00001094 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1095 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Jay Foadc556ef22011-04-24 10:11:03 +00001096 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel823d8e92010-12-08 22:42:58 +00001097 llvm::MDNode *RealDecl = NULL;
1098
Devang Patel5c5b5872011-02-28 22:32:45 +00001099 if (RD->isUnion())
Devang Patel16674e82011-02-22 18:56:36 +00001100 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Devang Patel5c5b5872011-02-28 22:32:45 +00001101 Size, Align, 0, Elements);
1102 else if (CXXDecl) {
Devang Patel823d8e92010-12-08 22:42:58 +00001103 RDName = getClassName(RD);
1104 // A class's primary base or the class itself contains the vtable.
1105 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +00001106 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001107 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1108 // Seek non virtual primary base root.
1109 while (1) {
1110 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1111 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001112 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001113 PBase = PBT;
1114 else
1115 break;
1116 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001117 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001118 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001119 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001120 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001121 ContainingType = FwdDecl;
Devang Patel9c1714b2011-04-05 17:30:54 +00001122
Devang Patel16674e82011-02-22 18:56:36 +00001123 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Devang Patel823d8e92010-12-08 22:42:58 +00001124 Size, Align, 0, 0, llvm::DIType(),
Devang Patelfa275df2011-02-02 21:38:49 +00001125 Elements, ContainingType,
1126 TParamsArray);
Devang Patel5c5b5872011-02-28 22:32:45 +00001127 } else
1128 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1129 Size, Align, 0, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Chris Lattner9c85ba32008-11-10 06:08:34 +00001131 // Now that we have a real decl for the struct, replace anything using the
1132 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001133 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001134 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001135 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001136}
1137
John McCallc12c5bb2010-05-15 11:32:37 +00001138/// CreateType - get objective-c object type.
1139llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1140 llvm::DIFile Unit) {
1141 // Ignore protocols.
1142 return getOrCreateType(Ty->getBaseType(), Unit);
1143}
1144
Devang Patel9ca36b62009-02-26 21:10:26 +00001145/// CreateType - get objective-c interface type.
1146llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001147 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001148 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001149 if (!ID)
1150 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001151
1152 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001153 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001154 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001155 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001156
Eric Christopherd1ab1a22011-10-06 00:31:18 +00001157 // If this is just a forward declaration return a special forward-declaration
1158 // debug type since we won't be able to lay out the entire type.
Dan Gohman45f7c782010-08-23 21:15:56 +00001159 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001160 llvm::DIType FwdDecl =
Devang Patel16674e82011-02-22 18:56:36 +00001161 DBuilder.createStructType(Unit, ID->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001162 DefUnit, Line, 0, 0, 0,
1163 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001164 return FwdDecl;
1165 }
1166
Eric Christopher1cd1d742011-10-06 00:30:52 +00001167 // To handle a recursive interface, we first generate a debug descriptor
1168 // for the struct as a forward declaration. Then (if it is a definition)
1169 // we go through and get debug info for all of its members. Finally, we
1170 // create a descriptor for the complete type (which may refer to the
1171 // forward decl if the struct is recursive) and replace all uses of the
1172 // forward declaration with the final definition.
Devang Patel16674e82011-02-22 18:56:36 +00001173 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Devang Patelab699792010-05-07 18:12:35 +00001175 llvm::MDNode *MN = FwdDecl;
1176 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001177 // Otherwise, insert it into the TypeCache so that recursive uses will find
1178 // it.
Devang Patelab699792010-05-07 18:12:35 +00001179 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001180 // Push the struct on region stack.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001181 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelab699792010-05-07 18:12:35 +00001182 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001183
1184 // Convert all the elements.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001185 SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001186
Devang Pateld6c5a262010-02-01 21:52:22 +00001187 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001188 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001189 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001190 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001191 if (!SClassTy.isValid())
1192 return llvm::DIType();
1193
Mike Stump1eb44332009-09-09 15:08:12 +00001194 llvm::DIType InhTag =
Devang Patel16674e82011-02-22 18:56:36 +00001195 DBuilder.createInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001196 EltTys.push_back(InhTag);
1197 }
1198
Devang Pateld6c5a262010-02-01 21:52:22 +00001199 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel8c6f9c42011-09-19 18:54:16 +00001200 ObjCImplementationDecl *ImpD = ID->getImplementation();
Devang Patel9ca36b62009-02-26 21:10:26 +00001201 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001202 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001203 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001204 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001205 if (!FieldTy.isValid())
1206 return llvm::DIType();
1207
Chris Lattner5f9e2722011-07-23 10:55:15 +00001208 StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001209
Devang Patelde135022009-04-27 22:40:36 +00001210 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001211 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001212 continue;
1213
Devang Patel9ca36b62009-02-26 21:10:26 +00001214 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001215 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1216 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001217 QualType FType = Field->getType();
1218 uint64_t FieldSize = 0;
1219 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001220
Devang Patel99c20eb2009-03-20 18:24:39 +00001221 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Devang Patel99c20eb2009-03-20 18:24:39 +00001223 // Bit size, align and offset of the type.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001224 FieldSize = Field->isBitField()
1225 ? Field->getBitWidthValue(CGM.getContext())
1226 : CGM.getContext().getTypeSize(FType);
1227 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001228 }
1229
Eric Christopherd1ab1a22011-10-06 00:31:18 +00001230 // We can't know the offset of our ivar in the structure if we're using
1231 // the non-fragile abi and the debugger should ignore the value anyways.
1232 // Call it the FieldNo+1 due to how debuggers use the information,
1233 // e.g. negating the value when it needs a lookup in the dynamic table.
1234 uint64_t FieldOffset = CGM.getLangOptions().ObjCNonFragileABI ? FieldNo+1
1235 : RL.getFieldOffset(FieldNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Devang Patelc20482b2009-03-19 00:23:53 +00001237 unsigned Flags = 0;
1238 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001239 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001240 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001241 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Chris Lattner5f9e2722011-07-23 10:55:15 +00001243 StringRef PropertyName;
1244 StringRef PropertyGetter;
1245 StringRef PropertySetter;
Eli Friedman10292cc2011-04-17 06:40:15 +00001246 unsigned PropertyAttributes = 0;
Devang Patel8c6f9c42011-09-19 18:54:16 +00001247 ObjCPropertyDecl *PD = NULL;
1248 if (ImpD)
1249 if (ObjCPropertyImplDecl *PImpD =
1250 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier()))
1251 PD = PImpD->getPropertyDecl();
1252 if (PD) {
Devang Patelfa936d82011-04-16 00:12:55 +00001253 PropertyName = PD->getName();
Devang Patel90c1eed2011-04-16 00:37:51 +00001254 PropertyGetter = getSelectorName(PD->getGetterName());
1255 PropertySetter = getSelectorName(PD->getSetterName());
Devang Patelfa936d82011-04-16 00:12:55 +00001256 PropertyAttributes = PD->getPropertyAttributes();
Devang Patel8c6f9c42011-09-19 18:54:16 +00001257 }
Devang Patelfa936d82011-04-16 00:12:55 +00001258 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1259 FieldLine, FieldSize, FieldAlign,
1260 FieldOffset, Flags, FieldTy,
1261 PropertyName, PropertyGetter,
1262 PropertySetter, PropertyAttributes);
Devang Patel9ca36b62009-02-26 21:10:26 +00001263 EltTys.push_back(FieldTy);
1264 }
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Jay Foadc556ef22011-04-24 10:11:03 +00001266 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel9ca36b62009-02-26 21:10:26 +00001267
Eric Christopheraa2164c2011-09-29 00:00:45 +00001268 LexicalBlockStack.pop_back();
Devang Patele4c1ea02010-03-11 20:01:48 +00001269 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1270 RegionMap.find(Ty->getDecl());
1271 if (RI != RegionMap.end())
1272 RegionMap.erase(RI);
1273
Devang Patel9ca36b62009-02-26 21:10:26 +00001274 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001275 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1276 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Devang Patel707b1e92011-05-12 19:07:41 +00001278 unsigned Flags = 0;
Devang Patelf568b642011-05-12 21:14:54 +00001279 if (ID->getImplementation())
Devang Patelaad16092011-05-12 21:29:57 +00001280 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
Devang Patel707b1e92011-05-12 19:07:41 +00001281
Devang Patel823d8e92010-12-08 22:42:58 +00001282 llvm::DIType RealDecl =
Devang Patel16674e82011-02-22 18:56:36 +00001283 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
Devang Patel707b1e92011-05-12 19:07:41 +00001284 Line, Size, Align, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00001285 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001286
1287 // Now that we have a real decl for the struct, replace anything using the
1288 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001289 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001290 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001291
Devang Patel9ca36b62009-02-26 21:10:26 +00001292 return RealDecl;
1293}
1294
Devang Patel31f7d022011-01-17 22:23:07 +00001295llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001296 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001297 return CreateType(RT);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001298 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001299 return CreateEnumType(ET->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Chris Lattner9c85ba32008-11-10 06:08:34 +00001301 return llvm::DIType();
1302}
1303
Devang Patel70c23cd2010-02-23 22:59:39 +00001304llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001305 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001306 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel6cf37dd2011-04-08 21:56:52 +00001307 int64_t NumElems = Ty->getNumElements();
1308 int64_t LowerBound = 0;
1309 if (NumElems == 0)
1310 // If number of elements are not known then this is an unbounded array.
1311 // Use Low = 1, Hi = 0 to express such arrays.
1312 LowerBound = 1;
1313 else
Devang Patel70c23cd2010-02-23 22:59:39 +00001314 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001315
Devang Patel6cf37dd2011-04-08 21:56:52 +00001316 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foadc556ef22011-04-24 10:11:03 +00001317 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patel70c23cd2010-02-23 22:59:39 +00001318
1319 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1320 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1321
1322 return
Devang Patel16674e82011-02-22 18:56:36 +00001323 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001324}
1325
Chris Lattner9c85ba32008-11-10 06:08:34 +00001326llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001327 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001328 uint64_t Size;
1329 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001330
1331
Nuno Lopes010d5142009-01-28 00:35:17 +00001332 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001333 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001334 Size = 0;
1335 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001336 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001337 } else if (Ty->isIncompleteArrayType()) {
1338 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001339 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patelba690a42011-04-04 23:18:38 +00001340 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patelae503df2011-04-01 19:02:33 +00001341 Size = 0;
1342 Align = 0;
Anders Carlsson835c9092009-01-05 01:23:29 +00001343 } else {
1344 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001345 Size = CGM.getContext().getTypeSize(Ty);
1346 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001347 }
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Chris Lattner9c85ba32008-11-10 06:08:34 +00001349 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1350 // interior arrays, do we care? Why aren't nested arrays represented the
1351 // obvious/recursive way?
Chris Lattner5f9e2722011-07-23 10:55:15 +00001352 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001353 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001354 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001355 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001356 else {
1357 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Devang Patel6cf37dd2011-04-08 21:56:52 +00001358 int64_t UpperBound = 0;
1359 int64_t LowerBound = 0;
Nick Lewycky3894c072011-04-09 00:25:15 +00001360 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Devang Patelcdf523c2010-10-06 18:30:00 +00001361 if (CAT->getSize().getZExtValue())
Devang Patel6cf37dd2011-04-08 21:56:52 +00001362 UpperBound = CAT->getSize().getZExtValue() - 1;
Nick Lewycky3894c072011-04-09 00:25:15 +00001363 } else
Devang Patel6cf37dd2011-04-08 21:56:52 +00001364 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1365 // arrays.
1366 LowerBound = 1;
1367
Devang Patelcdf523c2010-10-06 18:30:00 +00001368 // FIXME: Verify this is right for VLAs.
Devang Patel6cf37dd2011-04-08 21:56:52 +00001369 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound, UpperBound));
Devang Patelcdf523c2010-10-06 18:30:00 +00001370 EltTy = Ty->getElementType();
1371 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001372 }
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Jay Foadc556ef22011-04-24 10:11:03 +00001374 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001375
Devang Patelca80a5f2009-10-20 19:55:01 +00001376 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001377 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001378 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001379 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001380}
1381
Anders Carlssona031b352009-11-06 19:19:55 +00001382llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001383 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001384 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1385 Ty, Ty->getPointeeType(), Unit);
1386}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001387
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001388llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1389 llvm::DIFile Unit) {
1390 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1391 Ty, Ty->getPointeeType(), Unit);
1392}
1393
Anders Carlsson20f12a22009-12-06 18:00:51 +00001394llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001395 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001396 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1397 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1398
1399 if (!Ty->getPointeeType()->isFunctionType()) {
1400 // We have a data member pointer type.
1401 return PointerDiffDITy;
1402 }
1403
1404 // We have a member function pointer type. Treat it as a struct with two
1405 // ptrdiff_t members.
1406 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1407
1408 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001409 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001410
1411 // FIXME: This should probably be a function type instead.
1412 ElementTypes[0] =
Devang Patel1d323e02011-06-24 22:00:59 +00001413 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001414 Info.first, Info.second, FieldOffset, 0,
1415 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001416 FieldOffset += Info.first;
1417
1418 ElementTypes[1] =
Devang Patel1d323e02011-06-24 22:00:59 +00001419 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001420 Info.first, Info.second, FieldOffset, 0,
1421 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001422
Jay Foadc556ef22011-04-24 10:11:03 +00001423 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001424
Chris Lattner5f9e2722011-07-23 10:55:15 +00001425 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel823d8e92010-12-08 22:42:58 +00001426 U, 0, FieldOffset,
1427 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001428}
1429
Eli Friedmanb001de72011-10-06 23:00:33 +00001430llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1431 llvm::DIFile U) {
1432 // Ignore the atomic wrapping
1433 // FIXME: What is the correct representation?
1434 return getOrCreateType(Ty->getValueType(), U);
1435}
1436
Devang Patel6237cea2010-08-23 22:07:25 +00001437/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001438llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1439 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001440 SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001441
1442 // Create DIEnumerator elements for each enumerator.
1443 for (EnumDecl::enumerator_iterator
1444 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1445 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001446 Enumerators.push_back(
Devang Patel16674e82011-02-22 18:56:36 +00001447 DBuilder.createEnumerator(Enum->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001448 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001449 }
1450
1451 // Return a CompositeType for the enum itself.
Jay Foadc556ef22011-04-24 10:11:03 +00001452 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel6237cea2010-08-23 22:07:25 +00001453
1454 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1455 unsigned Line = getLineNumber(ED->getLocation());
1456 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001457 uint64_t Align = 0;
1458 if (!ED->getTypeForDecl()->isIncompleteType()) {
1459 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1460 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1461 }
Devang Patel4bc48872010-10-27 23:23:58 +00001462 llvm::DIDescriptor EnumContext =
John McCall8178df32011-02-22 22:38:33 +00001463 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel6237cea2010-08-23 22:07:25 +00001464 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001465 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel823d8e92010-12-08 22:42:58 +00001466 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001467 return DbgTy;
1468}
1469
Douglas Gregor840943d2009-12-21 20:18:30 +00001470static QualType UnwrapTypeForDebugInfo(QualType T) {
1471 do {
1472 QualType LastT = T;
1473 switch (T->getTypeClass()) {
1474 default:
1475 return T;
1476 case Type::TemplateSpecialization:
1477 T = cast<TemplateSpecializationType>(T)->desugar();
1478 break;
John McCallf4c73712011-01-19 06:33:43 +00001479 case Type::TypeOfExpr:
1480 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001481 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001482 case Type::TypeOf:
1483 T = cast<TypeOfType>(T)->getUnderlyingType();
1484 break;
1485 case Type::Decltype:
1486 T = cast<DecltypeType>(T)->getUnderlyingType();
1487 break;
Sean Huntca63c202011-05-24 22:41:36 +00001488 case Type::UnaryTransform:
1489 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1490 break;
John McCall9d156a72011-01-06 01:58:22 +00001491 case Type::Attributed:
1492 T = cast<AttributedType>(T)->getEquivalentType();
John McCall14aa2172011-03-04 04:00:19 +00001493 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001494 case Type::Elaborated:
1495 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001496 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001497 case Type::Paren:
1498 T = cast<ParenType>(T)->getInnerType();
1499 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001500 case Type::SubstTemplateTypeParm:
1501 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1502 break;
Anders Carlssonebc32792011-03-06 16:43:04 +00001503 case Type::Auto:
1504 T = cast<AutoType>(T)->getDeducedType();
1505 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001506 }
1507
1508 assert(T != LastT && "Type unwrapping failed to unwrap!");
1509 if (T == LastT)
1510 return T;
1511 } while (true);
1512
1513 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001514}
1515
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001516/// getOrCreateType - Get the type from the cache or create a new
1517/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001518llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001519 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001520 if (Ty.isNull())
1521 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001522
Douglas Gregor840943d2009-12-21 20:18:30 +00001523 // Unwrap the type as needed for debug information.
1524 Ty = UnwrapTypeForDebugInfo(Ty);
Devang Patele80d5672011-03-23 16:29:39 +00001525
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001526 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001527 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001528 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001529 if (it != TypeCache.end()) {
1530 // Verify that the debug info still exists.
1531 if (&*it->second)
1532 return llvm::DIType(cast<llvm::MDNode>(it->second));
1533 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001534
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001535 // Otherwise create the type.
1536 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001537
1538 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001539 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001540 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001541}
1542
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001543/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001544llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001545 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001546 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001547 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001548 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001549
Douglas Gregor2101a822009-12-21 19:57:21 +00001550 const char *Diag = 0;
1551
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001552 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001553 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001554#define TYPE(Class, Base)
1555#define ABSTRACT_TYPE(Class, Base)
1556#define NON_CANONICAL_TYPE(Class, Base)
1557#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1558#include "clang/AST/TypeNodes.def"
David Blaikieb219cfc2011-09-23 05:06:16 +00001559 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001560
Anders Carlssonbfe69952009-11-06 18:24:04 +00001561 case Type::ExtVector:
Devang Patel70c23cd2010-02-23 22:59:39 +00001562 case Type::Vector:
1563 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001564 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001565 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001566 case Type::ObjCObject:
1567 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001568 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001569 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001570 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001571 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001572 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001573 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001574 return CreateType(cast<BlockPointerType>(Ty), Unit);
1575 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001576 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001577 case Type::Enum:
Devang Patel31f7d022011-01-17 22:23:07 +00001578 return CreateType(cast<TagType>(Ty));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001579 case Type::FunctionProto:
1580 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001581 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001582 case Type::ConstantArray:
1583 case Type::VariableArray:
1584 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001585 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001586
1587 case Type::LValueReference:
1588 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001589 case Type::RValueReference:
1590 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001591
Anders Carlsson20f12a22009-12-06 18:00:51 +00001592 case Type::MemberPointer:
1593 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001594
Eli Friedmanb001de72011-10-06 23:00:33 +00001595 case Type::Atomic:
1596 return CreateType(cast<AtomicType>(Ty), Unit);
1597
John McCall9d156a72011-01-06 01:58:22 +00001598 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001599 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001600 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001601 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001602 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001603 case Type::TypeOfExpr:
1604 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001605 case Type::Decltype:
Sean Huntca63c202011-05-24 22:41:36 +00001606 case Type::UnaryTransform:
Richard Smith34b41d92011-02-20 03:19:35 +00001607 case Type::Auto:
Douglas Gregor840943d2009-12-21 20:18:30 +00001608 llvm_unreachable("type should have been unwrapped!");
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001609 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001610 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001611
1612 assert(Diag && "Fall through without a diagnostic?");
David Blaikied6471f72011-09-25 23:23:43 +00001613 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor2101a822009-12-21 19:57:21 +00001614 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001615 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001616 << Diag;
1617 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001618}
1619
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001620/// CreateMemberType - Create new member and increase Offset by FType's size.
1621llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001622 StringRef Name,
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001623 uint64_t *Offset) {
1624 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1625 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1626 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel1d323e02011-06-24 22:00:59 +00001627 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001628 FieldSize, FieldAlign,
1629 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001630 *Offset += FieldSize;
1631 return Ty;
1632}
1633
Devang Patel120bf322011-04-23 00:08:01 +00001634/// getFunctionDeclaration - Return debug info descriptor to describe method
1635/// declaration for the given method definition.
1636llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1637 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1638 if (!FD) return llvm::DISubprogram();
1639
1640 // Setup context.
1641 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1642
Devang Patel22a5cdf2011-04-29 23:42:32 +00001643 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1644 MI = SPCache.find(FD);
1645 if (MI != SPCache.end()) {
1646 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1647 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1648 return SP;
1649 }
1650
Devang Patel120bf322011-04-23 00:08:01 +00001651 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1652 E = FD->redecls_end(); I != E; ++I) {
1653 const FunctionDecl *NextFD = *I;
1654 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1655 MI = SPCache.find(NextFD);
1656 if (MI != SPCache.end()) {
1657 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1658 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1659 return SP;
1660 }
1661 }
1662 return llvm::DISubprogram();
1663}
1664
Devang Patel1c296522011-05-31 20:46:46 +00001665// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
1666// implicit parameter "this".
1667llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D, QualType FnType,
1668 llvm::DIFile F) {
1669 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1670 return getOrCreateMethodType(Method, F);
Devang Patelc478f212011-05-31 21:18:50 +00001671 else if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patelc478f212011-05-31 21:18:50 +00001672 // Add "self" and "_cmd"
Chris Lattner5f9e2722011-07-23 10:55:15 +00001673 SmallVector<llvm::Value *, 16> Elts;
Devang Patelc478f212011-05-31 21:18:50 +00001674
1675 // First element is always return type. For 'void' functions it is NULL.
Devang Pateld127bcb2011-05-31 22:21:11 +00001676 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patelc478f212011-05-31 21:18:50 +00001677 // "self" pointer is always first argument.
1678 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F));
1679 // "cmd" pointer is always second argument.
1680 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F));
Devang Pateld127bcb2011-05-31 22:21:11 +00001681 // Get rest of the arguments.
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001682 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Pateld127bcb2011-05-31 22:21:11 +00001683 PE = OMethod->param_end(); PI != PE; ++PI)
1684 Elts.push_back(getOrCreateType((*PI)->getType(), F));
1685
Devang Patelc478f212011-05-31 21:18:50 +00001686 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1687 return DBuilder.createSubroutineType(F, EltTypeArray);
1688 }
Devang Patel1c296522011-05-31 20:46:46 +00001689 return getOrCreateType(FnType, F);
1690}
1691
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001692/// EmitFunctionStart - Constructs the debug code for entering a function -
1693/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001694void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001695 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001696 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Chris Lattner5f9e2722011-07-23 10:55:15 +00001698 StringRef Name;
1699 StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001700
Eric Christopheraa2164c2011-09-29 00:00:45 +00001701 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001702
Devang Patel9c6c3a02010-01-14 00:36:21 +00001703 const Decl *D = GD.getDecl();
Devang Patel12e6d832011-04-05 20:28:21 +00001704
Devang Patel3951e712010-10-07 22:03:49 +00001705 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001706 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1707 llvm::DIDescriptor FDContext(Unit);
Devang Patel5ecb1df2011-04-05 22:54:11 +00001708 llvm::DIArray TParamsArray;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001709 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001710 // If there is a DISubprogram for this function available then use it.
1711 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1712 FI = SPCache.find(FD);
1713 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001714 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001715 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1716 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00001717 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00001718 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001719 return;
1720 }
1721 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001722 Name = getFunctionName(FD);
1723 // Use mangled name as linkage name for c/c++ functions.
Devang Patela87a2b22011-05-02 22:49:30 +00001724 if (!Fn->hasInternalLinkage())
Devang Patel2df74c02011-05-02 22:37:48 +00001725 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001726 if (LinkageName == Name)
Chris Lattner5f9e2722011-07-23 10:55:15 +00001727 LinkageName = StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001728 if (FD->hasPrototype())
1729 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001730 if (const NamespaceDecl *NSDecl =
1731 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel170cef32010-12-09 00:33:05 +00001732 FDContext = getOrCreateNameSpace(NSDecl);
Devang Patelbc6a1912011-05-17 00:20:09 +00001733 else if (const RecordDecl *RDecl =
1734 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
1735 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patel5ecb1df2011-04-05 22:54:11 +00001736
1737 // Collect template parameters.
1738 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001739 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001740 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001741 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001742 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001743 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001744 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001745 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001746 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001747 if (!Name.empty() && Name[0] == '\01')
1748 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Devang Patel970c6182010-04-24 00:49:16 +00001750 // It is expected that CurLoc is set before using EmitFunctionStart.
1751 // Usually, CurLoc points to the left bracket location of compound
1752 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001753 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001754 if (D->isImplicit())
1755 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel120bf322011-04-23 00:08:01 +00001756 llvm::DISubprogram SPDecl = getFunctionDeclaration(D);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001757 llvm::DISubprogram SP =
Devang Patel16674e82011-02-22 18:56:36 +00001758 DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
Devang Patel1c296522011-05-31 20:46:46 +00001759 LineNo, getOrCreateFunctionType(D, FnType, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001760 Fn->hasInternalLinkage(), true/*definition*/,
Devang Patel5ecb1df2011-04-05 22:54:11 +00001761 Flags, CGM.getLangOptions().Optimize, Fn,
Devang Patel120bf322011-04-23 00:08:01 +00001762 TParamsArray, SPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001764 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001765 llvm::MDNode *SPN = SP;
Eric Christopheraa2164c2011-09-29 00:00:45 +00001766 LexicalBlockStack.push_back(SPN);
Devang Patelab699792010-05-07 18:12:35 +00001767 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001768
1769 // Clear stack used to keep track of #line directives.
1770 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001771}
1772
Eric Christopher69a1b742011-09-29 00:00:37 +00001773// UpdateLineDirectiveRegion - Update region stack only if #line directive
1774// has introduced scope change.
1775void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1776 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1777 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1778 return;
1779 SourceManager &SM = CGM.getContext().getSourceManager();
1780 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1781 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1782
1783 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1784 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1785 return;
1786
1787 // If #line directive stack is empty then we are entering a new scope.
1788 if (LineDirectiveFiles.empty()) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00001789 EmitLexicalBlockStart(Builder);
Eric Christopher69a1b742011-09-29 00:00:37 +00001790 LineDirectiveFiles.push_back(PCLoc.getFilename());
1791 return;
1792 }
1793
Eric Christopheraa2164c2011-09-29 00:00:45 +00001794 assert (LexicalBlockStack.size() >= LineDirectiveFiles.size()
Eric Christopher69a1b742011-09-29 00:00:37 +00001795 && "error handling #line regions!");
1796
1797 bool SeenThisFile = false;
1798 // Chek if current file is already seen earlier.
1799 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1800 E = LineDirectiveFiles.end(); I != E; ++I)
1801 if (!strcmp(PCLoc.getFilename(), *I)) {
1802 SeenThisFile = true;
1803 break;
1804 }
1805
1806 // If #line for this file is seen earlier then pop out #line regions.
1807 if (SeenThisFile) {
1808 while (!LineDirectiveFiles.empty()) {
1809 const char *LastFile = LineDirectiveFiles.back();
Eric Christopheraa2164c2011-09-29 00:00:45 +00001810 LexicalBlockStack.pop_back();
Eric Christopher69a1b742011-09-29 00:00:37 +00001811 LineDirectiveFiles.pop_back();
1812 if (!strcmp(PPLoc.getFilename(), LastFile))
1813 break;
1814 }
1815 return;
1816 }
1817
1818 // .. otherwise insert new #line region.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001819 EmitLexicalBlockStart(Builder);
Eric Christopher69a1b742011-09-29 00:00:37 +00001820 LineDirectiveFiles.push_back(PCLoc.getFilename());
1821
1822 return;
1823}
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001824
Eric Christopher5321bc42011-09-29 00:00:41 +00001825/// EmitLocation - Emit metadata to indicate a change in line/column
1826/// information in the source file.
1827void CGDebugInfo::EmitLocation(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001828 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001830 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001831 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelcb2a07e2011-07-19 00:52:18 +00001832 if (CurLoc == PrevLoc ||
Chandler Carruth40278532011-07-25 16:49:02 +00001833 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patel4800ea62010-04-05 21:09:15 +00001834 // New Builder may not be in sync with CGDebugInfo.
1835 if (!Builder.getCurrentDebugLocation().isUnknown())
1836 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001837
Eric Christopher414ee4b2011-09-29 00:00:35 +00001838 // The file may have had a line directive change. Process any of
1839 // those before updating the state.
1840 UpdateLineDirectiveRegion(Builder);
1841
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001842 // Update last state.
1843 PrevLoc = CurLoc;
1844
Eric Christopheraa2164c2011-09-29 00:00:45 +00001845 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001846 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1847 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001848 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001849}
1850
Eric Christopheraa2164c2011-09-29 00:00:45 +00001851/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
Eric Christopher43202ae2011-09-26 15:03:22 +00001852/// region - beginning of a DW_TAG_lexical_block.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001853void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001854 llvm::DIDescriptor D =
Eric Christopheraa2164c2011-09-29 00:00:45 +00001855 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel823d8e92010-12-08 22:42:58 +00001856 llvm::DIDescriptor() :
Eric Christopheraa2164c2011-09-29 00:00:45 +00001857 llvm::DIDescriptor(LexicalBlockStack.back()),
Devang Patel823d8e92010-12-08 22:42:58 +00001858 getOrCreateFile(CurLoc),
1859 getLineNumber(CurLoc),
1860 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001861 llvm::MDNode *DN = D;
Eric Christopheraa2164c2011-09-29 00:00:45 +00001862 LexicalBlockStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001863}
1864
Eric Christopheraa2164c2011-09-29 00:00:45 +00001865/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher43202ae2011-09-26 15:03:22 +00001866/// region - end of a DW_TAG_lexical_block.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001867void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder) {
1868 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar5273f512008-10-17 01:07:56 +00001869
Eric Christopher43202ae2011-09-26 15:03:22 +00001870 // Provide a region stop point.
Eric Christopher5321bc42011-09-29 00:00:41 +00001871 EmitLocation(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001872
Eric Christopheraa2164c2011-09-29 00:00:45 +00001873 LexicalBlockStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001874}
1875
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001876/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1877void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00001878 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001879 unsigned RCount = FnBeginRegionCount.back();
Eric Christopheraa2164c2011-09-29 00:00:45 +00001880 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001881
1882 // Pop all regions for this function.
Eric Christopheraa2164c2011-09-29 00:00:45 +00001883 while (LexicalBlockStack.size() != RCount)
1884 EmitLexicalBlockEnd(Builder);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001885 FnBeginRegionCount.pop_back();
1886}
1887
Devang Patel809b9bb2010-02-10 18:49:08 +00001888// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1889// See BuildByRefType.
1890llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1891 uint64_t *XOffset) {
1892
Chris Lattner5f9e2722011-07-23 10:55:15 +00001893 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001894 QualType FType;
1895 uint64_t FieldSize, FieldOffset;
1896 unsigned FieldAlign;
1897
Devang Patel17800552010-03-09 00:44:50 +00001898 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001899 QualType Type = VD->getType();
1900
1901 FieldOffset = 0;
1902 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001903 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1904 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001905 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001906 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1907 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1908
John McCall6b5a61b2011-02-07 10:33:21 +00001909 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00001910 if (HasCopyAndDispose) {
1911 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001912 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1913 &FieldOffset));
1914 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1915 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001916 }
1917
1918 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck573be632011-04-22 17:34:18 +00001919 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001920 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck573be632011-04-22 17:34:18 +00001921 CharUnits FieldOffsetInBytes
1922 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
1923 CharUnits AlignedOffsetInBytes
1924 = FieldOffsetInBytes.RoundUpToAlignment(Align);
1925 CharUnits NumPaddingBytes
1926 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel809b9bb2010-02-10 18:49:08 +00001927
Ken Dyck573be632011-04-22 17:34:18 +00001928 if (NumPaddingBytes.isPositive()) {
1929 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel809b9bb2010-02-10 18:49:08 +00001930 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1931 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001932 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001933 }
1934 }
1935
1936 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001937 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001938 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck573be632011-04-22 17:34:18 +00001939 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel809b9bb2010-02-10 18:49:08 +00001940
1941 *XOffset = FieldOffset;
Devang Patel1d323e02011-06-24 22:00:59 +00001942 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00001943 0, FieldSize, FieldAlign,
1944 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001945 EltTys.push_back(FieldTy);
1946 FieldOffset += FieldSize;
1947
Jay Foadc556ef22011-04-24 10:11:03 +00001948 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel809b9bb2010-02-10 18:49:08 +00001949
Devang Patele2472482010-09-29 21:05:52 +00001950 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001951
Devang Patel16674e82011-02-22 18:56:36 +00001952 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00001953 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001954}
Devang Patel823d8e92010-12-08 22:42:58 +00001955
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001956/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001957void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel093ac462011-03-03 20:13:15 +00001958 llvm::Value *Storage,
1959 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00001960 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar5273f512008-10-17 01:07:56 +00001961
Devang Patel17800552010-03-09 00:44:50 +00001962 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001963 llvm::DIType Ty;
1964 uint64_t XOffset = 0;
1965 if (VD->hasAttr<BlocksAttr>())
1966 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1967 else
1968 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001969
Devang Patelf4e54a22010-05-07 23:05:55 +00001970 // If there is not any debug info for type then do not emit debug info
1971 // for this variable.
1972 if (!Ty)
1973 return;
1974
Devang Patel34753802011-02-16 01:11:51 +00001975 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
1976 // If Storage is an aggregate returned as 'sret' then let debugger know
1977 // about this.
Devang Patel0691f932011-02-10 00:40:52 +00001978 if (Arg->hasStructRetAttr())
Devang Patel16674e82011-02-22 18:56:36 +00001979 Ty = DBuilder.createReferenceType(Ty);
Devang Patel34753802011-02-16 01:11:51 +00001980 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
1981 // If an aggregate variable has non trivial destructor or non trivial copy
1982 // constructor than it is pass indirectly. Let debug info know about this
1983 // by using reference of the aggregate type as a argument type.
1984 if (!Record->hasTrivialCopyConstructor() || !Record->hasTrivialDestructor())
Devang Patel16674e82011-02-22 18:56:36 +00001985 Ty = DBuilder.createReferenceType(Ty);
Devang Patel34753802011-02-16 01:11:51 +00001986 }
1987 }
Devang Patel0691f932011-02-10 00:40:52 +00001988
Chris Lattner9c85ba32008-11-10 06:08:34 +00001989 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001990 unsigned Line = getLineNumber(VD->getLocation());
1991 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001992 unsigned Flags = 0;
1993 if (VD->isImplicit())
1994 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopheraa2164c2011-09-29 00:00:45 +00001995 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001996
Chris Lattner5f9e2722011-07-23 10:55:15 +00001997 StringRef Name = VD->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00001998 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001999 if (VD->hasAttr<BlocksAttr>()) {
2000 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002001 SmallVector<llvm::Value *, 9> addr;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002002 llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002003 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002004 // offset of __forwarding field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002005 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002006 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002007 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002008 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2009 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002010 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002011 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002012 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2013
2014 // Create the descriptor for the variable.
2015 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002016 DBuilder.createComplexVariable(Tag,
Eric Christopheraa2164c2011-09-29 00:00:45 +00002017 llvm::DIDescriptor(LexicalBlockStack.back()),
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002018 VD->getName(), Unit, Line, Ty,
Jay Foadc556ef22011-04-24 10:11:03 +00002019 addr, ArgNo);
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002020
2021 // Insert an llvm.dbg.declare into the current block.
2022 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002023 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelb1fd0eb2011-01-11 00:30:27 +00002024
2025 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2026 return;
2027 }
2028 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00002029 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00002030 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel823d8e92010-12-08 22:42:58 +00002031 Name, Unit, Line, Ty,
Devang Patel093ac462011-03-03 20:13:15 +00002032 CGM.getLangOptions().Optimize, Flags, ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002033
2034 // Insert an llvm.dbg.declare into the current block.
2035 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00002036 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00002037
2038 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00002039 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00002040 }
2041
2042 // If VD is an anonymous union then Storage represents value for
2043 // all union fields.
John McCall8178df32011-02-22 22:38:33 +00002044 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2045 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2046 if (RD->isUnion()) {
2047 for (RecordDecl::field_iterator I = RD->field_begin(),
2048 E = RD->field_end();
2049 I != E; ++I) {
2050 FieldDecl *Field = *I;
2051 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002052 StringRef FieldName = Field->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00002053
John McCall8178df32011-02-22 22:38:33 +00002054 // Ignore unnamed fields. Do not ignore unnamed records.
2055 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2056 continue;
Devang Patelcebbedd2010-10-12 23:24:54 +00002057
John McCall8178df32011-02-22 22:38:33 +00002058 // Use VarDecl's Tag, Scope and Line number.
2059 llvm::DIVariable D =
2060 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2061 FieldName, Unit, Line, FieldTy,
Devang Patel093ac462011-03-03 20:13:15 +00002062 CGM.getLangOptions().Optimize, Flags,
2063 ArgNo);
Devang Patelcebbedd2010-10-12 23:24:54 +00002064
John McCall8178df32011-02-22 22:38:33 +00002065 // Insert an llvm.dbg.declare into the current block.
2066 llvm::Instruction *Call =
2067 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00002068
John McCall8178df32011-02-22 22:38:33 +00002069 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelcebbedd2010-10-12 23:24:54 +00002070 }
John McCall8178df32011-02-22 22:38:33 +00002071 }
2072 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00002073}
2074
Devang Patele2d01912011-04-25 23:43:36 +00002075void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2076 llvm::Value *Storage,
2077 CGBuilderTy &Builder) {
2078 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2079}
Mike Stumpb1a6e682009-09-30 02:43:10 +00002080
Devang Patele2d01912011-04-25 23:43:36 +00002081void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2082 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2083 const CGBlockInfo &blockInfo) {
Eric Christopheraa2164c2011-09-29 00:00:45 +00002084 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patele2d01912011-04-25 23:43:36 +00002085
Devang Patel2b594b92010-04-26 23:28:46 +00002086 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00002087 return;
Devang Patele2d01912011-04-25 23:43:36 +00002088
John McCall6b5a61b2011-02-07 10:33:21 +00002089 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patele2d01912011-04-25 23:43:36 +00002090
Mike Stumpb1a6e682009-09-30 02:43:10 +00002091 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00002092 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00002093 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00002094 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00002095 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2096 else
2097 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002098
2099 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00002100 unsigned Line = getLineNumber(VD->getLocation());
2101 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00002102
John McCall6b5a61b2011-02-07 10:33:21 +00002103 const llvm::TargetData &target = CGM.getTargetData();
2104
2105 CharUnits offset = CharUnits::fromQuantity(
2106 target.getStructLayout(blockInfo.StructureType)
2107 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2108
Chris Lattner5f9e2722011-07-23 10:55:15 +00002109 SmallVector<llvm::Value *, 9> addr;
Chris Lattner2acc6e32011-07-18 04:24:23 +00002110 llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002111 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattner14b1a362010-01-25 03:29:35 +00002112 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00002113 if (isByRef) {
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002114 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2115 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002116 // offset of __forwarding field
Devang Patel37833b02011-04-26 21:16:49 +00002117 offset = CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits());
Chris Lattner14b1a362010-01-25 03:29:35 +00002118 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00002119 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2120 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00002121 // offset of x field
Ken Dyck0ebce0e2011-04-22 17:41:34 +00002122 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattner14b1a362010-01-25 03:29:35 +00002123 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002124 }
2125
2126 // Create the descriptor for the variable.
2127 llvm::DIVariable D =
Devang Patele2d01912011-04-25 23:43:36 +00002128 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopheraa2164c2011-09-29 00:00:45 +00002129 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foadc556ef22011-04-24 10:11:03 +00002130 VD->getName(), Unit, Line, Ty, addr);
Mike Stumpb1a6e682009-09-30 02:43:10 +00002131 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00002132 llvm::Instruction *Call =
Devang Patel50811d22011-04-25 23:52:27 +00002133 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Chris Lattnerd5b89022009-12-28 21:44:41 +00002134
Eric Christopheraa2164c2011-09-29 00:00:45 +00002135 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00002136 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00002137}
2138
Chris Lattner9c85ba32008-11-10 06:08:34 +00002139/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2140/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00002141void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel093ac462011-03-03 20:13:15 +00002142 unsigned ArgNo,
Devang Patel34753802011-02-16 01:11:51 +00002143 CGBuilderTy &Builder) {
Devang Patel093ac462011-03-03 20:13:15 +00002144 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00002145}
2146
John McCall8178df32011-02-22 22:38:33 +00002147namespace {
2148 struct BlockLayoutChunk {
2149 uint64_t OffsetInBits;
2150 const BlockDecl::Capture *Capture;
2151 };
2152 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2153 return l.OffsetInBits < r.OffsetInBits;
2154 }
2155}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002156
John McCall8178df32011-02-22 22:38:33 +00002157void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2158 llvm::Value *addr,
2159 CGBuilderTy &Builder) {
2160 ASTContext &C = CGM.getContext();
2161 const BlockDecl *blockDecl = block.getBlockDecl();
2162
2163 // Collect some general information about the block's location.
2164 SourceLocation loc = blockDecl->getCaretLocation();
2165 llvm::DIFile tunit = getOrCreateFile(loc);
2166 unsigned line = getLineNumber(loc);
2167 unsigned column = getColumnNumber(loc);
2168
2169 // Build the debug-info type for the block literal.
Nick Lewycky7d4b1592011-05-02 01:41:48 +00002170 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall8178df32011-02-22 22:38:33 +00002171
2172 const llvm::StructLayout *blockLayout =
2173 CGM.getTargetData().getStructLayout(block.StructureType);
2174
Chris Lattner5f9e2722011-07-23 10:55:15 +00002175 SmallVector<llvm::Value*, 16> fields;
John McCall8178df32011-02-22 22:38:33 +00002176 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2177 blockLayout->getElementOffsetInBits(0),
Devang Patel1d323e02011-06-24 22:00:59 +00002178 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002179 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2180 blockLayout->getElementOffsetInBits(1),
Devang Patel1d323e02011-06-24 22:00:59 +00002181 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002182 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2183 blockLayout->getElementOffsetInBits(2),
Devang Patel1d323e02011-06-24 22:00:59 +00002184 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002185 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2186 blockLayout->getElementOffsetInBits(3),
Devang Patel1d323e02011-06-24 22:00:59 +00002187 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002188 fields.push_back(createFieldType("__descriptor",
2189 C.getPointerType(block.NeedsCopyDispose ?
2190 C.getBlockDescriptorExtendedType() :
2191 C.getBlockDescriptorType()),
2192 0, loc, AS_public,
2193 blockLayout->getElementOffsetInBits(4),
Devang Patel1d323e02011-06-24 22:00:59 +00002194 tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002195
2196 // We want to sort the captures by offset, not because DWARF
2197 // requires this, but because we're paranoid about debuggers.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002198 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall8178df32011-02-22 22:38:33 +00002199
2200 // 'this' capture.
2201 if (blockDecl->capturesCXXThis()) {
2202 BlockLayoutChunk chunk;
2203 chunk.OffsetInBits =
2204 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2205 chunk.Capture = 0;
2206 chunks.push_back(chunk);
2207 }
2208
2209 // Variable captures.
2210 for (BlockDecl::capture_const_iterator
2211 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2212 i != e; ++i) {
2213 const BlockDecl::Capture &capture = *i;
2214 const VarDecl *variable = capture.getVariable();
2215 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2216
2217 // Ignore constant captures.
2218 if (captureInfo.isConstant())
2219 continue;
2220
2221 BlockLayoutChunk chunk;
2222 chunk.OffsetInBits =
2223 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2224 chunk.Capture = &capture;
2225 chunks.push_back(chunk);
2226 }
2227
2228 // Sort by offset.
2229 llvm::array_pod_sort(chunks.begin(), chunks.end());
2230
Chris Lattner5f9e2722011-07-23 10:55:15 +00002231 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall8178df32011-02-22 22:38:33 +00002232 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2233 uint64_t offsetInBits = i->OffsetInBits;
2234 const BlockDecl::Capture *capture = i->Capture;
2235
2236 // If we have a null capture, this must be the C++ 'this' capture.
2237 if (!capture) {
2238 const CXXMethodDecl *method =
2239 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2240 QualType type = method->getThisType(C);
2241
2242 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel1d323e02011-06-24 22:00:59 +00002243 offsetInBits, tunit, tunit));
John McCall8178df32011-02-22 22:38:33 +00002244 continue;
2245 }
2246
2247 const VarDecl *variable = capture->getVariable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002248 StringRef name = variable->getName();
John McCalld113a6f2011-03-02 06:57:14 +00002249
2250 llvm::DIType fieldType;
2251 if (capture->isByRef()) {
2252 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2253
2254 // FIXME: this creates a second copy of this type!
2255 uint64_t xoffset;
2256 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2257 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel1d323e02011-06-24 22:00:59 +00002258 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCalld113a6f2011-03-02 06:57:14 +00002259 ptrInfo.first, ptrInfo.second,
2260 offsetInBits, 0, fieldType);
2261 } else {
2262 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel1d323e02011-06-24 22:00:59 +00002263 loc, AS_public, offsetInBits, tunit, tunit);
John McCalld113a6f2011-03-02 06:57:14 +00002264 }
2265 fields.push_back(fieldType);
John McCall8178df32011-02-22 22:38:33 +00002266 }
2267
2268 llvm::SmallString<36> typeName;
2269 llvm::raw_svector_ostream(typeName)
2270 << "__block_literal_" << CGM.getUniqueBlockCount();
2271
Jay Foadc556ef22011-04-24 10:11:03 +00002272 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall8178df32011-02-22 22:38:33 +00002273
2274 llvm::DIType type =
2275 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2276 CGM.getContext().toBits(block.BlockSize),
2277 CGM.getContext().toBits(block.BlockAlign),
2278 0, fieldsArray);
2279 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2280
2281 // Get overall information about the block.
2282 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopheraa2164c2011-09-29 00:00:45 +00002283 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002284 StringRef name = ".block_descriptor";
John McCall8178df32011-02-22 22:38:33 +00002285
2286 // Create the descriptor for the parameter.
2287 llvm::DIVariable debugVar =
2288 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2289 llvm::DIDescriptor(scope),
2290 name, tunit, line, type,
Devang Patel093ac462011-03-03 20:13:15 +00002291 CGM.getLangOptions().Optimize, flags,
2292 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall8178df32011-02-22 22:38:33 +00002293
2294 // Insert an llvm.dbg.value into the current block.
2295 llvm::Instruction *declare =
2296 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2297 Builder.GetInsertBlock());
2298 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2299}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002300
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002301/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00002302void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00002303 const VarDecl *D) {
2304
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002305 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002306 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002307 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00002308
Devang Pateleb6d79b2010-02-01 21:34:11 +00002309 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002310 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002312 // CodeGen turns int[] into int[1] so we'll do the same here.
2313 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002315 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002316 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Anders Carlsson20f12a22009-12-06 18:00:51 +00002318 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002319 ArrayType::Normal, 0);
2320 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002321 StringRef DeclName = D->getName();
2322 StringRef LinkageName;
Devang Pateleb4c45b2011-02-09 19:16:38 +00002323 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2324 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00002325 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00002326 if (LinkageName == DeclName)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002327 LinkageName = StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00002328 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00002329 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel16674e82011-02-22 18:56:36 +00002330 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +00002331 Unit, LineNo, getOrCreateType(T, Unit),
2332 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002333}
2334
Devang Patel9ca36b62009-02-26 21:10:26 +00002335/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00002336void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00002337 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00002338 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002339 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002340 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002341
Chris Lattner5f9e2722011-07-23 10:55:15 +00002342 StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002343
Devang Pateld6c5a262010-02-01 21:52:22 +00002344 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002345 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002346
Devang Patel9ca36b62009-02-26 21:10:26 +00002347 // CodeGen turns int[] into int[1] so we'll do the same here.
2348 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Devang Patel9ca36b62009-02-26 21:10:26 +00002350 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002351 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002352
Anders Carlsson20f12a22009-12-06 18:00:51 +00002353 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002354 ArrayType::Normal, 0);
2355 }
2356
Devang Patel16674e82011-02-22 18:56:36 +00002357 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel823d8e92010-12-08 22:42:58 +00002358 getOrCreateType(T, Unit),
2359 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00002360}
Devang Patelabb485f2010-02-01 19:16:32 +00002361
Devang Patel25c2c8f2010-08-10 17:53:33 +00002362/// EmitGlobalVariable - Emit global variable's debug info.
2363void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002364 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002365 // Create the descriptor for the variable.
2366 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002367 StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002368 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002369 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2370 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel31f7d022011-01-17 22:23:07 +00002371 Ty = CreateEnumType(ED);
Devang Patel6237cea2010-08-23 22:07:25 +00002372 }
Devang Patel0317ab02010-08-10 18:27:15 +00002373 // Do not use DIGlobalVariable for enums.
2374 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2375 return;
Devang Patel16674e82011-02-22 18:56:36 +00002376 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002377 getLineNumber(VD->getLocation()),
2378 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002379}
2380
Devang Patelabb485f2010-02-01 19:16:32 +00002381/// getOrCreateNamesSpace - Return namespace descriptor for the given
2382/// namespace decl.
2383llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002384CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002385 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2386 NameSpaceCache.find(NSDecl);
2387 if (I != NameSpaceCache.end())
2388 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2389
Devang Patel8ab870d2010-05-12 23:46:38 +00002390 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002391 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002392 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002393 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002394 llvm::DINameSpace NS =
Devang Patel16674e82011-02-22 18:56:36 +00002395 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002396 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002397 return NS;
2398}
Devang Patele80d5672011-03-23 16:29:39 +00002399
2400/// UpdateCompletedType - Update type cache because the type is now
2401/// translated.
2402void CGDebugInfo::UpdateCompletedType(const TagDecl *TD) {
2403 QualType Ty = CGM.getContext().getTagDeclType(TD);
2404
2405 // If the type exist in type cache then remove it from the cache.
2406 // There is no need to prepare debug info for the completed type
2407 // right now. It will be generated on demand lazily.
2408 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2409 TypeCache.find(Ty.getAsOpaquePtr());
2410 if (it != TypeCache.end())
2411 TypeCache.erase(it);
2412}