blob: 5770764dfa0379a7b70b44f81b2505450cad387a [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
John McCallad7c5c12011-02-08 08:22:06 +000017#include "CGBlocks.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000018#include "clang/AST/ASTContext.h"
Devang Patel96b7f552010-08-27 17:47:47 +000019#include "clang/AST/DeclFriend.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000020#include "clang/AST/DeclObjC.h"
Devang Patel6c018202010-07-20 20:24:18 +000021#include "clang/AST/DeclTemplate.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000022#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000024#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000026#include "clang/Basic/Version.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000033#include "llvm/ADT/StringExtras.h"
34#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000035#include "llvm/Support/Dwarf.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000036#include "llvm/Support/Path.h"
John McCall351762c2011-02-07 10:33:21 +000037#include "llvm/Target/TargetData.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000038#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000039using namespace clang;
40using namespace clang::CodeGen;
41
Anders Carlsson3efc6e62009-12-06 18:00:51 +000042CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel00afcbe2010-12-08 22:42:58 +000043 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman196f7102010-08-20 22:02:57 +000044 BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000045 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000046}
47
Chris Lattneraffb3732008-11-10 06:08:34 +000048CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000049 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000050}
51
Chris Lattneraffb3732008-11-10 06:08:34 +000052void CGDebugInfo::setLocation(SourceLocation Loc) {
53 if (Loc.isValid())
Chandler Carruth35f53202011-07-25 16:49:02 +000054 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000055}
56
Devang Patel7bfc5962010-01-28 23:15:27 +000057/// getContextDescriptor - Get context info for the decl.
Devang Patel8c445292010-12-09 00:33:05 +000058llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Patel7b7f46f2010-02-01 21:34:11 +000059 if (!Context)
Devang Patel8c445292010-12-09 00:33:05 +000060 return TheCU;
Devang Patel7b7f46f2010-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 Greifbf986082010-09-18 13:00:17 +000065 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000066
Devang Patel7b7f46f2010-02-01 21:34:11 +000067 // Check namespace.
68 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel8c445292010-12-09 00:33:05 +000069 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel98f21712010-05-13 23:52:37 +000070
71 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
72 if (!RDecl->isDependentType()) {
Devang Patel8e007302010-10-28 17:27:32 +000073 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel8c445292010-12-09 00:33:05 +000074 getOrCreateMainFile());
Devang Patel98f21712010-05-13 23:52:37 +000075 return llvm::DIDescriptor(Ty);
76 }
77 }
Devang Patel8c445292010-12-09 00:33:05 +000078 return TheCU;
Devang Patelfaf7e9a2009-10-06 00:35:31 +000079}
80
Devang Patel934661e2010-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 Lattner0e62c1c2011-07-23 10:55:15 +000084StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Devang Patel934661e2010-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 Patel0d61eeb2010-01-28 18:21:00 +000094 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000095 memcpy(StrPtr, NS.data(), NS.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +000096 return StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000097}
98
Chris Lattner0e62c1c2011-07-23 10:55:15 +000099StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
David Chisnallcf607442010-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 Patel8e007302010-10-28 17:27:32 +0000104 if (const ObjCImplementationDecl *OID =
105 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnallcf607442010-09-02 18:01:51 +0000106 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000107 } else if (const ObjCInterfaceDecl *OID =
108 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanianf34011e2010-10-18 17:51:06 +0000109 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000110 } else if (const ObjCCategoryImplDecl *OCD =
111 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnallcf607442010-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 Lattner0e62c1c2011-07-23 10:55:15 +0000119 return StringRef(StrPtr, OS.tell());
David Chisnallcf607442010-09-02 18:01:51 +0000120}
121
Devang Patel43cfa5d2011-04-18 17:30:25 +0000122/// getSelectorName - Return selector name. This is used for debugging
Devang Patel7294d742011-04-16 00:37:51 +0000123/// info.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000124StringRef CGDebugInfo::getSelectorName(Selector S) {
Devang Patel7294d742011-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 Lattner0e62c1c2011-07-23 10:55:15 +0000130 return StringRef(StrPtr, OS.tell());
Devang Patel7294d742011-04-16 00:37:51 +0000131}
132
Devang Patel6c018202010-07-20 20:24:18 +0000133/// getClassName - Get class name including template argument list.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000134StringRef
Devang Patel6c018202010-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 Gregor1ccc8412010-11-07 23:05:16 +0000151 Args = TemplateArgs.data();
152 NumArgs = TemplateArgs.size();
Devang Patel6c018202010-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 Lattner0e62c1c2011-07-23 10:55:15 +0000163 return StringRef(StrPtr, Buffer.length());
Devang Patel6c018202010-07-20 20:24:18 +0000164}
165
Devang Patel408dcf62010-03-09 00:44:50 +0000166/// getOrCreateFile - Get the file debug info descriptor for the input location.
167llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000168 if (!Loc.isValid())
169 // If Location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000170 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel00afcbe2010-12-08 22:42:58 +0000171
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000172 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000173 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000174
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000175 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregorad3832e2010-11-11 20:45:16 +0000176 // If the location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000177 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregorad3832e2010-11-11 20:45:16 +0000178
Ted Kremenekdbb8cd12010-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 Pateld7185b72011-02-22 18:56:36 +0000190 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000191
Devang Patelba4ad7f2010-05-07 18:12:35 +0000192 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000193 return F;
194
Devang Patel408dcf62010-03-09 00:44:50 +0000195}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000196
Devang Pateled23f182010-10-28 22:03:20 +0000197/// getOrCreateMainFile - Get the file info for main compile unit.
198llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Pateld7185b72011-02-22 18:56:36 +0000199 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Pateled23f182010-10-28 22:03:20 +0000200}
201
Devang Patelc5ffabc2010-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 Gregorad3832e2010-11-11 20:45:16 +0000208 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patelc5ffabc2010-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 Gregorad3832e2010-11-11 20:45:16 +0000217 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000218}
219
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000220StringRef CGDebugInfo::getCurrentDirname() {
Devang Patel6014edd2010-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 Lattner0e62c1c2011-07-23 10:55:15 +0000227 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patel6014edd2010-07-27 15:17:16 +0000228}
229
Devang Patel408dcf62010-03-09 00:44:50 +0000230/// CreateCompileUnit - Create new compile unit.
231void CGDebugInfo::CreateCompileUnit() {
232
233 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000234 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000235 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
236 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000237 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000238
Douglas Gregor65f7a3f2010-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 Patelcb9fe9e2010-07-23 23:04:28 +0000243 std::string MainFileDir;
Devang Patel6014edd2010-07-27 15:17:16 +0000244 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000245 MainFileDir = MainFile->getDir()->getName();
Devang Patel6014edd2010-07-27 15:17:16 +0000246 if (MainFileDir != ".")
247 MainFileName = MainFileDir + "/" + MainFileName;
248 }
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000249
Devang Patel6014edd2010-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 Lattner0e62c1c2011-07-23 10:55:15 +0000253 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patel6014edd2010-07-27 15:17:16 +0000254
Chris Lattner8c37df42009-03-25 03:28:08 +0000255 unsigned LangTag;
Devang Patel408dcf62010-03-09 00:44:50 +0000256 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner8c37df42009-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 Patel94406c92009-03-24 20:35:51 +0000263 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000264 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000265 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000266 } else {
267 LangTag = llvm::dwarf::DW_LANG_C89;
268 }
Devang Patel75009452009-04-17 21:06:59 +0000269
Daniel Dunbar64c222a2010-08-24 17:41:09 +0000270 std::string Producer = getClangFullVersion();
Chris Lattner5912de12009-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 Stump11289f42009-09-09 15:08:12 +0000276
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000277 // Create new compile unit.
Devang Pateld7185b72011-02-22 18:56:36 +0000278 DBuilder.createCompileUnit(
Devang Patel4f6e73b2010-07-27 20:49:59 +0000279 LangTag, Filename, getCurrentDirname(),
Devang Patel00afcbe2010-12-08 22:42:58 +0000280 Producer,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000281 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel00afcbe2010-12-08 22:42:58 +0000282 // FIXME - Eliminate TheCU.
283 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000284}
285
Devang Patel410dc002009-02-25 01:36:11 +0000286/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000287/// one if necessary.
Devang Patel86f30f52010-11-01 16:52:40 +0000288llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000289 unsigned Encoding = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000290 const char *BTName = NULL;
Chris Lattneraffb3732008-11-10 06:08:34 +0000291 switch (BT->getKind()) {
292 default:
Devang Patel33e097b2011-09-12 18:24:46 +0000293 assert(0 && "Unexpected builtin");
294 return llvm::DIType();
Chris Lattneraffb3732008-11-10 06:08:34 +0000295 case BuiltinType::Void:
296 return llvm::DIType();
Devang Patela652fab2010-07-28 01:33:15 +0000297 case BuiltinType::ObjCClass:
Devang Pateld7185b72011-02-22 18:56:36 +0000298 return DBuilder.createStructType(TheCU, "objc_class",
Devang Patel00afcbe2010-12-08 22:42:58 +0000299 getOrCreateMainFile(), 0, 0, 0,
300 llvm::DIDescriptor::FlagFwdDecl,
301 llvm::DIArray());
Devang Patela652fab2010-07-28 01:33:15 +0000302 case BuiltinType::ObjCId: {
303 // typedef struct objc_class *Class;
304 // typedef struct objc_object {
305 // Class isa;
306 // } *id;
307
308 llvm::DIType OCTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000309 DBuilder.createStructType(TheCU, "objc_class",
Devang Patel00afcbe2010-12-08 22:42:58 +0000310 getOrCreateMainFile(), 0, 0, 0,
311 llvm::DIDescriptor::FlagFwdDecl,
312 llvm::DIArray());
Devang Patela652fab2010-07-28 01:33:15 +0000313 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
314
Devang Pateld7185b72011-02-22 18:56:36 +0000315 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patela652fab2010-07-28 01:33:15 +0000316
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000317 SmallVector<llvm::Value *, 16> EltTys;
Devang Patela652fab2010-07-28 01:33:15 +0000318 llvm::DIType FieldTy =
Devang Patel15013e72011-06-24 22:00:59 +0000319 DBuilder.createMemberType(getOrCreateMainFile(), "isa",
320 getOrCreateMainFile(), 0, Size,
321 0, 0, 0, ISATy);
Devang Patela652fab2010-07-28 01:33:15 +0000322 EltTys.push_back(FieldTy);
Jay Foaddbf81d82011-04-24 10:11:03 +0000323 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patela652fab2010-07-28 01:33:15 +0000324
Devang Pateld7185b72011-02-22 18:56:36 +0000325 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel00afcbe2010-12-08 22:42:58 +0000326 getOrCreateMainFile(),
327 0, 0, 0, 0, Elements);
Devang Patela652fab2010-07-28 01:33:15 +0000328 }
Devang Patel19ba2b42011-02-09 03:15:05 +0000329 case BuiltinType::ObjCSel: {
Devang Pateld7185b72011-02-22 18:56:36 +0000330 return DBuilder.createStructType(TheCU, "objc_selector",
Devang Patel19ba2b42011-02-09 03:15:05 +0000331 getOrCreateMainFile(), 0, 0, 0,
332 llvm::DIDescriptor::FlagFwdDecl,
333 llvm::DIArray());
334 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000335 case BuiltinType::UChar:
336 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
337 case BuiltinType::Char_S:
338 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patel98ca8ae2011-09-12 17:11:58 +0000339 case BuiltinType::Char16:
340 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattneraffb3732008-11-10 06:08:34 +0000341 case BuiltinType::UShort:
342 case BuiltinType::UInt:
Devang Patel979aba52011-05-05 17:06:30 +0000343 case BuiltinType::UInt128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000344 case BuiltinType::ULong:
Devang Patel964d7582011-09-10 00:44:49 +0000345 case BuiltinType::WChar_U:
Chris Lattneraffb3732008-11-10 06:08:34 +0000346 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
347 case BuiltinType::Short:
348 case BuiltinType::Int:
Devang Patel979aba52011-05-05 17:06:30 +0000349 case BuiltinType::Int128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000350 case BuiltinType::Long:
Devang Patel964d7582011-09-10 00:44:49 +0000351 case BuiltinType::WChar_S:
Chris Lattneraffb3732008-11-10 06:08:34 +0000352 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
353 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
354 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000355 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000356 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000357 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000358
359 switch (BT->getKind()) {
360 case BuiltinType::Long: BTName = "long int"; break;
361 case BuiltinType::LongLong: BTName = "long long int"; break;
362 case BuiltinType::ULong: BTName = "long unsigned int"; break;
363 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
364 default:
365 BTName = BT->getName(CGM.getContext().getLangOptions());
366 break;
367 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000368 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000369 uint64_t Size = CGM.getContext().getTypeSize(BT);
370 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patele21912d2009-10-20 19:55:01 +0000371 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000372 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000373 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000374}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000375
Devang Patel4591f772010-12-09 00:25:29 +0000376llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000377 // Bit size, align and offset of the type.
378 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
379 if (Ty->isComplexIntegerType())
380 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000381
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000382 uint64_t Size = CGM.getContext().getTypeSize(Ty);
383 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patele21912d2009-10-20 19:55:01 +0000384 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000385 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel00afcbe2010-12-08 22:42:58 +0000386
Devang Patele21912d2009-10-20 19:55:01 +0000387 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000388}
389
John McCall0cf15512009-09-25 01:40:47 +0000390/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000391/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000392llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000393 QualifierCollector Qc;
394 const Type *T = Qc.strip(Ty);
395
396 // Ignore these qualifiers for now.
397 Qc.removeObjCGCAttr();
398 Qc.removeAddressSpace();
John McCall31168b02011-06-15 23:02:42 +0000399 Qc.removeObjCLifetime();
John McCall0cf15512009-09-25 01:40:47 +0000400
Chris Lattneraffb3732008-11-10 06:08:34 +0000401 // We will create one Derived type for one qualifier and recurse to handle any
402 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000403 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000404 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000405 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000406 Qc.removeConst();
407 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000408 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000409 Qc.removeVolatile();
410 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000411 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000412 Qc.removeRestrict();
413 } else {
414 assert(Qc.empty() && "Unknown type qualifier for debug info");
415 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000416 }
Mike Stump11289f42009-09-09 15:08:12 +0000417
John McCall717d9b02010-12-10 11:01:00 +0000418 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCall0cf15512009-09-25 01:40:47 +0000419
Daniel Dunbara290ded2008-10-31 03:54:29 +0000420 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
421 // CVR derived types.
Devang Pateld7185b72011-02-22 18:56:36 +0000422 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel00afcbe2010-12-08 22:42:58 +0000423
Devang Patele21912d2009-10-20 19:55:01 +0000424 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000425}
426
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000427llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000428 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000429 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000430 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
431 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000432 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000433}
434
Chris Lattneraffb3732008-11-10 06:08:34 +0000435llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000436 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000437 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
438 Ty->getPointeeType(), Unit);
439}
440
Devang Patel91bbb552010-09-30 19:05:55 +0000441/// CreatePointeeType - Create PointTee type. If Pointee is a record
442/// then emit record's fwd if debug info size reduction is enabled.
443llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
444 llvm::DIFile Unit) {
445 if (!CGM.getCodeGenOpts().LimitDebugInfo)
446 return getOrCreateType(PointeeTy, Unit);
447
448 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
449 RecordDecl *RD = RTy->getDecl();
Devang Patel91bbb552010-09-30 19:05:55 +0000450 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
451 unsigned Line = getLineNumber(RD->getLocation());
452 llvm::DIDescriptor FDContext =
John McCall147d0212011-02-22 22:38:33 +0000453 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel00afcbe2010-12-08 22:42:58 +0000454
455 if (RD->isStruct())
Devang Pateld7185b72011-02-22 18:56:36 +0000456 return DBuilder.createStructType(FDContext, RD->getName(), DefUnit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000457 Line, 0, 0, llvm::DIType::FlagFwdDecl,
458 llvm::DIArray());
459 else if (RD->isUnion())
Devang Pateld7185b72011-02-22 18:56:36 +0000460 return DBuilder.createUnionType(FDContext, RD->getName(), DefUnit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000461 Line, 0, 0, llvm::DIType::FlagFwdDecl,
462 llvm::DIArray());
463 else {
464 assert(RD->isClass() && "Unknown RecordType!");
Devang Pateld7185b72011-02-22 18:56:36 +0000465 return DBuilder.createClassType(FDContext, RD->getName(), DefUnit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000466 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
467 llvm::DIType(), llvm::DIArray());
468 }
Devang Patel91bbb552010-09-30 19:05:55 +0000469 }
470 return getOrCreateType(PointeeTy, Unit);
471
472}
473
Anders Carlsson443f6772009-11-06 19:19:55 +0000474llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
475 const Type *Ty,
476 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000477 llvm::DIFile Unit) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000478
479 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Pateld7185b72011-02-22 18:56:36 +0000480 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel00afcbe2010-12-08 22:42:58 +0000481
Sanjiv Gupta98070572008-05-25 05:15:42 +0000482 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000483 // Size is always the size of a pointer. We can't use getTypeSize here
484 // because that does not return the correct value for references.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000485 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregore8bbc122011-09-02 00:18:52 +0000486 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000487 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000488
Devang Patel00afcbe2010-12-08 22:42:58 +0000489 return
Devang Pateld7185b72011-02-22 18:56:36 +0000490 DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000491}
492
Mike Stump31f099c2009-05-14 02:03:51 +0000493llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000494 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000495 if (BlockLiteralGenericSet)
496 return BlockLiteralGeneric;
497
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000498 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump31f099c2009-05-14 02:03:51 +0000499 llvm::DIType FieldTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000500 QualType FType;
501 uint64_t FieldSize, FieldOffset;
502 unsigned FieldAlign;
Mike Stump31f099c2009-05-14 02:03:51 +0000503 llvm::DIArray Elements;
504 llvm::DIType EltTy, DescTy;
505
506 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000507 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000508 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
509 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000510
Jay Foaddbf81d82011-04-24 10:11:03 +0000511 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000512 EltTys.clear();
513
Devang Pateldb2732a2010-09-29 21:05:52 +0000514 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000515 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000516
Devang Pateld7185b72011-02-22 18:56:36 +0000517 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel00afcbe2010-12-08 22:42:58 +0000518 Unit, LineNo, FieldOffset, 0,
519 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000520
Mike Stump31f099c2009-05-14 02:03:51 +0000521 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000522 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000523
Devang Pateld7185b72011-02-22 18:56:36 +0000524 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000525
526 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000527 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000528 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000529 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000530 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
531 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000532 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000533 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000534
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000535 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000536 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000537 FieldSize = CGM.getContext().getTypeSize(Ty);
538 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel15013e72011-06-24 22:00:59 +0000539 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000540 LineNo, FieldSize, FieldAlign,
541 FieldOffset, 0, FieldTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000542 EltTys.push_back(FieldTy);
543
544 FieldOffset += FieldSize;
Jay Foaddbf81d82011-04-24 10:11:03 +0000545 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000546
Devang Pateld7185b72011-02-22 18:56:36 +0000547 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel00afcbe2010-12-08 22:42:58 +0000548 Unit, LineNo, FieldOffset, 0,
549 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000550
Mike Stump31f099c2009-05-14 02:03:51 +0000551 BlockLiteralGenericSet = true;
Devang Pateld7185b72011-02-22 18:56:36 +0000552 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000553 return BlockLiteralGeneric;
554}
555
Chris Lattneraffb3732008-11-10 06:08:34 +0000556llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000557 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000558 // Typedefs are derived from some other type. If we have a typedef of a
559 // typedef, make sure to emit the whole chain.
560 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel00afcbe2010-12-08 22:42:58 +0000561 if (!Src.Verify())
562 return llvm::DIType();
Chris Lattneraffb3732008-11-10 06:08:34 +0000563 // We don't set size information, but do specify where the typedef was
564 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000565 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000566 const TypedefNameDecl *TyDecl = Ty->getDecl();
567 llvm::DIDescriptor TydefContext =
568 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
569
570 return
571 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TydefContext);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000572}
573
Chris Lattneraffb3732008-11-10 06:08:34 +0000574llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000575 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000576 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000577
Chris Lattneraffb3732008-11-10 06:08:34 +0000578 // Add the result type at least.
579 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000580
Chris Lattneraffb3732008-11-10 06:08:34 +0000581 // Set up remainder of arguments if there is a prototype.
582 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patel284fa412010-10-06 20:51:45 +0000583 if (isa<FunctionNoProtoType>(Ty))
Devang Pateld7185b72011-02-22 18:56:36 +0000584 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patel284fa412010-10-06 20:51:45 +0000585 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000586 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
587 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta98070572008-05-25 05:15:42 +0000588 }
589
Jay Foaddbf81d82011-04-24 10:11:03 +0000590 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Mike Stump11289f42009-09-09 15:08:12 +0000591
Devang Pateld7185b72011-02-22 18:56:36 +0000592 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000593 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000594}
595
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000596llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall147d0212011-02-22 22:38:33 +0000597 QualType type,
598 Expr *bitWidth,
599 SourceLocation loc,
600 AccessSpecifier AS,
601 uint64_t offsetInBits,
Devang Patel15013e72011-06-24 22:00:59 +0000602 llvm::DIFile tunit,
603 llvm::DIDescriptor scope) {
John McCall147d0212011-02-22 22:38:33 +0000604 llvm::DIType debugType = getOrCreateType(type, tunit);
605
606 // Get the location for the field.
607 llvm::DIFile file = getOrCreateFile(loc);
608 unsigned line = getLineNumber(loc);
609
610 uint64_t sizeInBits = 0;
611 unsigned alignInBits = 0;
612 if (!type->isIncompleteArrayType()) {
613 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
614
615 if (bitWidth)
616 sizeInBits = bitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
617 }
618
619 unsigned flags = 0;
620 if (AS == clang::AS_private)
621 flags |= llvm::DIDescriptor::FlagPrivate;
622 else if (AS == clang::AS_protected)
623 flags |= llvm::DIDescriptor::FlagProtected;
624
Devang Patel15013e72011-06-24 22:00:59 +0000625 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
626 alignInBits, offsetInBits, flags, debugType);
John McCall147d0212011-02-22 22:38:33 +0000627}
628
Devang Patel889ce762010-01-19 00:00:59 +0000629/// CollectRecordFields - A helper function to collect debug info for
630/// record fields. This is used while creating debug info entry for a Record.
631void CGDebugInfo::
John McCall147d0212011-02-22 22:38:33 +0000632CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000633 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel15013e72011-06-24 22:00:59 +0000634 llvm::DIType RecordTy) {
John McCall147d0212011-02-22 22:38:33 +0000635 unsigned fieldNo = 0;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000636 const FieldDecl *LastFD = 0;
637 bool IsMsStruct = record->hasAttr<MsStructAttr>();
638
John McCall147d0212011-02-22 22:38:33 +0000639 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
640 for (RecordDecl::field_iterator I = record->field_begin(),
641 E = record->field_end();
642 I != E; ++I, ++fieldNo) {
643 FieldDecl *field = *I;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000644 if (IsMsStruct) {
645 // Zero-length bitfields following non-bitfield members are ignored
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000646 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000647 --fieldNo;
648 continue;
649 }
650 LastFD = field;
651 }
Devang Patel889ce762010-01-19 00:00:59 +0000652
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000653 StringRef name = field->getName();
John McCall147d0212011-02-22 22:38:33 +0000654 QualType type = field->getType();
655
656 // Ignore unnamed fields unless they're anonymous structs/unions.
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000657 if (name.empty() && !type->isRecordType()) {
658 LastFD = field;
Devang Patel889ce762010-01-19 00:00:59 +0000659 continue;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000660 }
Devang Patel889ce762010-01-19 00:00:59 +0000661
John McCall147d0212011-02-22 22:38:33 +0000662 llvm::DIType fieldType
663 = createFieldType(name, type, field->getBitWidth(),
664 field->getLocation(), field->getAccess(),
Devang Patel15013e72011-06-24 22:00:59 +0000665 layout.getFieldOffset(fieldNo), tunit, RecordTy);
Devang Patel889ce762010-01-19 00:00:59 +0000666
John McCall147d0212011-02-22 22:38:33 +0000667 elements.push_back(fieldType);
Devang Patel889ce762010-01-19 00:00:59 +0000668 }
669}
670
Devang Patel3d4e6d92010-01-28 00:28:01 +0000671/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
672/// function type is not updated to include implicit "this" pointer. Use this
673/// routine to get a method type which includes "this" pointer.
674llvm::DIType
675CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000676 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000677 llvm::DIType FnTy
678 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
679 0),
680 Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000681
Devang Patel3d4e6d92010-01-28 00:28:01 +0000682 // Add "this" pointer.
683
Devang Patelba4ad7f2010-05-07 18:12:35 +0000684 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000685 assert (Args.getNumElements() && "Invalid number of arguments!");
686
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000687 SmallVector<llvm::Value *, 16> Elts;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000688
689 // First element is always return type. For 'void' functions it is NULL.
690 Elts.push_back(Args.getElement(0));
691
Devang Patel96b7f552010-08-27 17:47:47 +0000692 if (!Method->isStatic())
693 {
694 // "this" pointer is always first argument.
Devang Patel78019ec2011-04-05 23:26:36 +0000695 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patel96b7f552010-08-27 17:47:47 +0000696 llvm::DIType ThisPtrType =
Devang Pateld7185b72011-02-22 18:56:36 +0000697 DBuilder.createArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel0a34e312010-07-13 00:24:30 +0000698
Devang Patel96b7f552010-08-27 17:47:47 +0000699 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
700 Elts.push_back(ThisPtrType);
701 }
Devang Patel3d4e6d92010-01-28 00:28:01 +0000702
703 // Copy rest of the arguments.
704 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
705 Elts.push_back(Args.getElement(i));
706
Jay Foaddbf81d82011-04-24 10:11:03 +0000707 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000708
Devang Pateld7185b72011-02-22 18:56:36 +0000709 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000710}
711
Devang Patelf79199d2010-10-22 17:11:50 +0000712/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
713/// inside a function.
714static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
715 if (const CXXRecordDecl *NRD =
716 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
717 return isFunctionLocalClass(NRD);
718 else if (isa<FunctionDecl>(RD->getDeclContext()))
719 return true;
720 return false;
721
722}
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000723/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
724/// a single member function GlobalDecl.
725llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000726CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000727 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000728 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000729 bool IsCtorOrDtor =
730 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
731
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000732 StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000733 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000734
735 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
736 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000737 StringRef MethodLinkageName;
Devang Patelf79199d2010-10-22 17:11:50 +0000738 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlssonea836bc2010-06-22 16:16:50 +0000739 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000740
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000741 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000742 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
743 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000744
745 // Collect virtual method info.
746 llvm::DIType ContainingType;
747 unsigned Virtuality = 0;
748 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000749
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000750 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000751 if (Method->isPure())
752 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
753 else
754 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
755
756 // It doesn't make sense to give a virtual destructor a vtable index,
757 // since a single destructor has two entries in the vtable.
758 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson11e51402010-04-17 20:15:18 +0000759 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000760 ContainingType = RecordTy;
761 }
762
Devang Pateldb2732a2010-09-29 21:05:52 +0000763 unsigned Flags = 0;
764 if (Method->isImplicit())
765 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel330b65e2010-09-29 21:46:16 +0000766 AccessSpecifier Access = Method->getAccess();
767 if (Access == clang::AS_private)
768 Flags |= llvm::DIDescriptor::FlagPrivate;
769 else if (Access == clang::AS_protected)
770 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000771 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
772 if (CXXC->isExplicit())
773 Flags |= llvm::DIDescriptor::FlagExplicit;
774 } else if (const CXXConversionDecl *CXXC =
775 dyn_cast<CXXConversionDecl>(Method)) {
776 if (CXXC->isExplicit())
777 Flags |= llvm::DIDescriptor::FlagExplicit;
778 }
Devang Patel251f8592010-10-07 22:03:49 +0000779 if (Method->hasPrototype())
780 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000781
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000782 llvm::DISubprogram SP =
Nick Lewycky0112b112011-09-01 21:49:51 +0000783 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +0000784 MethodDefUnit, MethodLine,
785 MethodTy, /*isLocalToUnit=*/false,
786 /* isDefinition=*/ false,
787 Virtuality, VIndex, ContainingType,
788 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000789
Devang Patela3e3fde2011-04-29 23:42:32 +0000790 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000791
792 return SP;
793}
794
Devang Patel7a12ad02010-01-19 01:54:44 +0000795/// CollectCXXMemberFunctions - A helper function to collect debug info for
796/// C++ member functions.This is used while creating debug info entry for
797/// a Record.
798void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000799CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000800 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000801 llvm::DIType RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000802 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
803 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000804 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000805
Devang Patel0ae70d12010-02-09 19:09:28 +0000806 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000807 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000808
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000809 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000810 }
811}
812
Devang Patel96b7f552010-08-27 17:47:47 +0000813/// CollectCXXFriends - A helper function to collect debug info for
814/// C++ base classes. This is used while creating debug info entry for
815/// a Record.
816void CGDebugInfo::
817CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000818 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel96b7f552010-08-27 17:47:47 +0000819 llvm::DIType RecordTy) {
Devang Patel96b7f552010-08-27 17:47:47 +0000820 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
821 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky0112b112011-09-01 21:49:51 +0000822 if ((*BI)->isUnsupportedFriend())
823 continue;
Devang Patel00afcbe2010-12-08 22:42:58 +0000824 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Pateld7185b72011-02-22 18:56:36 +0000825 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +0000826 getOrCreateType(TInfo->getType(),
827 Unit)));
Devang Patel96b7f552010-08-27 17:47:47 +0000828 }
829}
830
Devang Patelc54353d2010-01-25 23:32:18 +0000831/// CollectCXXBases - A helper function to collect debug info for
832/// C++ base classes. This is used while creating debug info entry for
833/// a Record.
834void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000835CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000836 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000837 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +0000838
Devang Patel1c0954c2010-02-01 21:39:52 +0000839 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
840 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
841 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000842 unsigned BFlags = 0;
Devang Patel84852bb2011-04-04 20:36:06 +0000843 uint64_t BaseOffset;
Devang Patel128aa9d2010-01-28 21:54:15 +0000844
845 const CXXRecordDecl *Base =
846 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
847
848 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000849 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000850 // expression where it expects +ve number.
Ken Dyckbb4e9772011-04-07 12:37:09 +0000851 BaseOffset =
852 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Pateldb2732a2010-09-29 21:05:52 +0000853 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patel128aa9d2010-01-28 21:54:15 +0000854 } else
Devang Patel84852bb2011-04-04 20:36:06 +0000855 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Ken Dyckbb4e9772011-04-07 12:37:09 +0000856 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
857 // BI->isVirtual() and bits when not.
Devang Patel128aa9d2010-01-28 21:54:15 +0000858
859 AccessSpecifier Access = BI->getAccessSpecifier();
860 if (Access == clang::AS_private)
Devang Pateldb2732a2010-09-29 21:05:52 +0000861 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel128aa9d2010-01-28 21:54:15 +0000862 else if (Access == clang::AS_protected)
Devang Pateldb2732a2010-09-29 21:05:52 +0000863 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patel128aa9d2010-01-28 21:54:15 +0000864
Devang Patel00afcbe2010-12-08 22:42:58 +0000865 llvm::DIType DTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000866 DBuilder.createInheritance(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +0000867 getOrCreateType(BI->getType(), Unit),
Devang Patel84852bb2011-04-04 20:36:06 +0000868 BaseOffset, BFlags);
Devang Patel128aa9d2010-01-28 21:54:15 +0000869 EltTys.push_back(DTy);
870 }
Devang Patelc54353d2010-01-25 23:32:18 +0000871}
872
Devang Patelb87c4282011-04-05 22:54:11 +0000873/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel7522abd2011-04-05 17:30:54 +0000874llvm::DIArray CGDebugInfo::
Devang Patelb87c4282011-04-05 22:54:11 +0000875CollectTemplateParams(const TemplateParameterList *TPList,
876 const TemplateArgumentList &TAList,
877 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000878 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel98d26c92011-04-05 20:15:06 +0000879 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
880 const TemplateArgument &TA = TAList[i];
Devang Patelb87c4282011-04-05 22:54:11 +0000881 const NamedDecl *ND = TPList->getParam(i);
Devang Patel7522abd2011-04-05 17:30:54 +0000882 if (TA.getKind() == TemplateArgument::Type) {
883 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
884 llvm::DITemplateTypeParameter TTP =
Devang Patel98d26c92011-04-05 20:15:06 +0000885 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel7522abd2011-04-05 17:30:54 +0000886 TemplateParams.push_back(TTP);
887 } else if (TA.getKind() == TemplateArgument::Integral) {
888 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel7522abd2011-04-05 17:30:54 +0000889 llvm::DITemplateValueParameter TVP =
Devang Patel98d26c92011-04-05 20:15:06 +0000890 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
891 TA.getAsIntegral()->getZExtValue());
Devang Patel7522abd2011-04-05 17:30:54 +0000892 TemplateParams.push_back(TVP);
893 }
894 }
Jay Foaddbf81d82011-04-24 10:11:03 +0000895 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel7522abd2011-04-05 17:30:54 +0000896}
897
Devang Patelb87c4282011-04-05 22:54:11 +0000898/// CollectFunctionTemplateParams - A helper function to collect debug
899/// info for function template parameters.
900llvm::DIArray CGDebugInfo::
901CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
902 if (FD->getTemplatedKind() == FunctionDecl::TK_FunctionTemplateSpecialization){
903 const TemplateParameterList *TList =
904 FD->getTemplateSpecializationInfo()->getTemplate()->getTemplateParameters();
905 return
906 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
907 }
908 return llvm::DIArray();
909}
910
911/// CollectCXXTemplateParams - A helper function to collect debug info for
912/// template parameters.
913llvm::DIArray CGDebugInfo::
914CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
915 llvm::DIFile Unit) {
916 llvm::PointerUnion<ClassTemplateDecl *,
917 ClassTemplatePartialSpecializationDecl *>
918 PU = TSpecial->getSpecializedTemplateOrPartial();
919
920 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
921 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
922 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
923 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
924 return CollectTemplateParams(TPList, TAList, Unit);
925}
926
Devang Patel84033fb2010-01-28 18:11:52 +0000927/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +0000928llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +0000929 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +0000930 return VTablePtrType;
931
932 ASTContext &Context = CGM.getContext();
933
934 /* Function type */
Devang Patel00afcbe2010-12-08 22:42:58 +0000935 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foaddbf81d82011-04-24 10:11:03 +0000936 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Pateld7185b72011-02-22 18:56:36 +0000937 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel84033fb2010-01-28 18:11:52 +0000938 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Pateld7185b72011-02-22 18:56:36 +0000939 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +0000940 "__vtbl_ptr_type");
Devang Pateld7185b72011-02-22 18:56:36 +0000941 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel84033fb2010-01-28 18:11:52 +0000942 return VTablePtrType;
943}
944
Anders Carlsson11e51402010-04-17 20:15:18 +0000945/// getVTableName - Get vtable name for the given Class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000946StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000947 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000948 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000949
950 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000951 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000952 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000953 return StringRef(StrPtr, Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000954}
955
956
Anders Carlsson11e51402010-04-17 20:15:18 +0000957/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +0000958/// debug info entry in EltTys vector.
959void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +0000960CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000961 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000962 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000963
964 // If there is a primary base then it will hold vtable info.
965 if (RL.getPrimaryBase())
966 return;
967
968 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000969 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000970 return;
971
972 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
973 llvm::DIType VPTR
Devang Patel15013e72011-06-24 22:00:59 +0000974 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000975 0, Size, 0, 0, 0,
976 getOrCreateVTablePtrType(Unit));
Devang Patel84033fb2010-01-28 18:11:52 +0000977 EltTys.push_back(VPTR);
978}
979
Devang Patel91bbb552010-09-30 19:05:55 +0000980/// getOrCreateRecordType - Emit record type's standalone debug info.
981llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
982 SourceLocation Loc) {
983 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Pateld7185b72011-02-22 18:56:36 +0000984 DBuilder.retainType(T);
Devang Patel91bbb552010-09-30 19:05:55 +0000985 return T;
986}
987
Devang Patel410dc002009-02-25 01:36:11 +0000988/// CreateType - get structure or union type.
Devang Patel283e89d2011-01-17 22:23:07 +0000989llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Patel3efd1472010-02-01 21:52:22 +0000990 RecordDecl *RD = Ty->getDecl();
Devang Patel283e89d2011-01-17 22:23:07 +0000991 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattneraffb3732008-11-10 06:08:34 +0000993 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000994 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
995 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattneraffb3732008-11-10 06:08:34 +0000997 // Records and classes and unions can all be recursive. To handle them, we
998 // first generate a debug descriptor for the struct as a forward declaration.
999 // Then (if it is a definition) we go through and get debug info for all of
1000 // its members. Finally, we create a descriptor for the complete type (which
1001 // may refer to the forward decl if the struct is recursive) and replace all
1002 // uses of the forward declaration with the final definition.
Devang Patel8f3f76f2010-07-08 19:56:29 +00001003 llvm::DIDescriptor FDContext =
John McCall147d0212011-02-22 22:38:33 +00001004 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel8f3f76f2010-07-08 19:56:29 +00001005
1006 // If this is just a forward declaration, construct an appropriately
1007 // marked node and just return it.
1008 if (!RD->getDefinition()) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001009 llvm::DIType FwdDecl =
Devang Pateld7185b72011-02-22 18:56:36 +00001010 DBuilder.createStructType(FDContext, RD->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001011 DefUnit, Line, 0, 0,
1012 llvm::DIDescriptor::FlagFwdDecl,
1013 llvm::DIArray());
Devang Patel8f3f76f2010-07-08 19:56:29 +00001014
1015 return FwdDecl;
1016 }
Devang Patel3f4a77e2010-01-20 23:56:40 +00001017
Devang Pateld7185b72011-02-22 18:56:36 +00001018 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump11289f42009-09-09 15:08:12 +00001019
Devang Patelba4ad7f2010-05-07 18:12:35 +00001020 llvm::MDNode *MN = FwdDecl;
1021 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattneraffb3732008-11-10 06:08:34 +00001022 // Otherwise, insert it into the TypeCache so that recursive uses will find
1023 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001024 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001025 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001026 RegionStack.push_back(FwdDeclNode);
1027 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001028
1029 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001030 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattneraffb3732008-11-10 06:08:34 +00001031
Devang Patel3efd1472010-02-01 21:52:22 +00001032 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +00001033 if (CXXDecl) {
1034 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson11e51402010-04-17 20:15:18 +00001035 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +00001036 }
Devang Patelcaa23f02010-08-12 00:02:44 +00001037
1038 // Collect static variables with initializers.
1039 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
1040 I != E; ++I)
1041 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
1042 if (const Expr *Init = V->getInit()) {
1043 Expr::EvalResult Result;
1044 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
1045 llvm::ConstantInt *CI
1046 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
1047
1048 // Create the descriptor for static variable.
1049 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001050 StringRef VName = V->getName();
Devang Patelcaa23f02010-08-12 00:02:44 +00001051 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1052 // Do not use DIGlobalVariable for enums.
1053 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Pateld7185b72011-02-22 18:56:36 +00001054 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
Devang Patel00afcbe2010-12-08 22:42:58 +00001055 getLineNumber(V->getLocation()),
1056 VTy, true, CI);
Devang Patelcaa23f02010-08-12 00:02:44 +00001057 }
1058 }
1059 }
1060 }
1061
Devang Patel15013e72011-06-24 22:00:59 +00001062 CollectRecordFields(RD, Unit, EltTys, FwdDecl);
Devang Patel7522abd2011-04-05 17:30:54 +00001063 llvm::DIArray TParamsArray;
Devang Patel84033fb2010-01-28 18:11:52 +00001064 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001065 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel96b7f552010-08-27 17:47:47 +00001066 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel7522abd2011-04-05 17:30:54 +00001067 if (const ClassTemplateSpecializationDecl *TSpecial
1068 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
1069 TParamsArray = CollectCXXTemplateParams(TSpecial, Unit);
Devang Patel00afcbe2010-12-08 22:42:58 +00001070 }
Devang Patelabb44132010-01-28 00:54:21 +00001071
Devang Patel00afcbe2010-12-08 22:42:58 +00001072 RegionStack.pop_back();
1073 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1074 RegionMap.find(Ty->getDecl());
1075 if (RI != RegionMap.end())
1076 RegionMap.erase(RI);
1077
1078 llvm::DIDescriptor RDContext =
John McCall147d0212011-02-22 22:38:33 +00001079 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001080 StringRef RDName = RD->getName();
Devang Patel00afcbe2010-12-08 22:42:58 +00001081 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1082 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Jay Foaddbf81d82011-04-24 10:11:03 +00001083 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel00afcbe2010-12-08 22:42:58 +00001084 llvm::MDNode *RealDecl = NULL;
1085
Devang Patelbbe4ff92011-02-28 22:32:45 +00001086 if (RD->isUnion())
Devang Pateld7185b72011-02-22 18:56:36 +00001087 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Devang Patelbbe4ff92011-02-28 22:32:45 +00001088 Size, Align, 0, Elements);
1089 else if (CXXDecl) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001090 RDName = getClassName(RD);
1091 // A class's primary base or the class itself contains the vtable.
1092 llvm::MDNode *ContainingType = NULL;
Devang Patel3efd1472010-02-01 21:52:22 +00001093 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0f585612010-10-14 22:59:23 +00001094 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1095 // Seek non virtual primary base root.
1096 while (1) {
1097 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1098 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlsson7f95cd12010-11-24 23:12:57 +00001099 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel0f585612010-10-14 22:59:23 +00001100 PBase = PBT;
1101 else
1102 break;
1103 }
Devang Patelabb44132010-01-28 00:54:21 +00001104 ContainingType =
Devang Patelba4ad7f2010-05-07 18:12:35 +00001105 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0f585612010-10-14 22:59:23 +00001106 }
Devang Patelabb44132010-01-28 00:54:21 +00001107 else if (CXXDecl->isDynamicClass())
Devang Patelba4ad7f2010-05-07 18:12:35 +00001108 ContainingType = FwdDecl;
Devang Patel7522abd2011-04-05 17:30:54 +00001109
Devang Pateld7185b72011-02-22 18:56:36 +00001110 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Devang Patel00afcbe2010-12-08 22:42:58 +00001111 Size, Align, 0, 0, llvm::DIType(),
Devang Patele3773c22011-02-02 21:38:49 +00001112 Elements, ContainingType,
1113 TParamsArray);
Devang Patelbbe4ff92011-02-28 22:32:45 +00001114 } else
1115 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1116 Size, Align, 0, Elements);
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattneraffb3732008-11-10 06:08:34 +00001118 // Now that we have a real decl for the struct, replace anything using the
1119 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +00001120 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001121 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel00afcbe2010-12-08 22:42:58 +00001122 return llvm::DIType(RealDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001123}
1124
John McCall8b07ec22010-05-15 11:32:37 +00001125/// CreateType - get objective-c object type.
1126llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1127 llvm::DIFile Unit) {
1128 // Ignore protocols.
1129 return getOrCreateType(Ty->getBaseType(), Unit);
1130}
1131
Devang Patelf4c205b2009-02-26 21:10:26 +00001132/// CreateType - get objective-c interface type.
1133llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001134 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +00001135 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001136 if (!ID)
1137 return llvm::DIType();
Devang Patelf4c205b2009-02-26 21:10:26 +00001138
1139 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +00001140 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001141 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +00001142 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +00001143
Dan Gohman66427b12010-08-23 21:15:56 +00001144 // If this is just a forward declaration, return a special forward-declaration
1145 // debug type.
1146 if (ID->isForwardDecl()) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001147 llvm::DIType FwdDecl =
Devang Pateld7185b72011-02-22 18:56:36 +00001148 DBuilder.createStructType(Unit, ID->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001149 DefUnit, Line, 0, 0, 0,
1150 llvm::DIArray(), RuntimeLang);
Dan Gohman66427b12010-08-23 21:15:56 +00001151 return FwdDecl;
1152 }
1153
Devang Patelf4c205b2009-02-26 21:10:26 +00001154 // To handle recursive interface, we
1155 // first generate a debug descriptor for the struct as a forward declaration.
1156 // Then (if it is a definition) we go through and get debug info for all of
1157 // its members. Finally, we create a descriptor for the complete type (which
1158 // may refer to the forward decl if the struct is recursive) and replace all
1159 // uses of the forward declaration with the final definition.
Devang Pateld7185b72011-02-22 18:56:36 +00001160 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump11289f42009-09-09 15:08:12 +00001161
Devang Patelba4ad7f2010-05-07 18:12:35 +00001162 llvm::MDNode *MN = FwdDecl;
1163 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patelf4c205b2009-02-26 21:10:26 +00001164 // Otherwise, insert it into the TypeCache so that recursive uses will find
1165 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001166 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001167 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001168 RegionStack.push_back(FwdDeclNode);
1169 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001170
1171 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 SmallVector<llvm::Value *, 16> EltTys;
Devang Patelf4c205b2009-02-26 21:10:26 +00001173
Devang Patel3efd1472010-02-01 21:52:22 +00001174 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001175 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001176 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001177 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001178 if (!SClassTy.isValid())
1179 return llvm::DIType();
1180
Mike Stump11289f42009-09-09 15:08:12 +00001181 llvm::DIType InhTag =
Devang Pateld7185b72011-02-22 18:56:36 +00001182 DBuilder.createInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelc0f58ea2009-03-10 21:30:26 +00001183 EltTys.push_back(InhTag);
1184 }
1185
Devang Patel3efd1472010-02-01 21:52:22 +00001186 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001187
1188 unsigned FieldNo = 0;
Fariborz Jahanian885e9df2010-10-01 00:01:53 +00001189 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanian77890872010-10-11 23:55:47 +00001190 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001191 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001192 if (!FieldTy.isValid())
1193 return llvm::DIType();
1194
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001195 StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001196
Devang Pateldf348f12009-04-27 22:40:36 +00001197 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001198 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001199 continue;
1200
Devang Patelf4c205b2009-02-26 21:10:26 +00001201 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001202 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1203 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001204 QualType FType = Field->getType();
1205 uint64_t FieldSize = 0;
1206 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001207
Devang Patel9f804932009-03-20 18:24:39 +00001208 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001209
Devang Patel9f804932009-03-20 18:24:39 +00001210 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001211 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001212 Expr *BitWidth = Field->getBitWidth();
1213 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001214 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +00001215
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001216 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001217 }
1218
Mike Stump11289f42009-09-09 15:08:12 +00001219 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1220
Devang Patelec4bad52009-03-19 00:23:53 +00001221 unsigned Flags = 0;
1222 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001223 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelec4bad52009-03-19 00:23:53 +00001224 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001225 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001226
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001227 StringRef PropertyName;
1228 StringRef PropertyGetter;
1229 StringRef PropertySetter;
Eli Friedman2e2ff882011-04-17 06:40:15 +00001230 unsigned PropertyAttributes = 0;
Devang Patel9d6c8572011-04-16 00:12:55 +00001231 if (ObjCPropertyDecl *PD =
1232 ID->FindPropertyVisibleInPrimaryClass(Field->getIdentifier())) {
1233 PropertyName = PD->getName();
Devang Patel7294d742011-04-16 00:37:51 +00001234 PropertyGetter = getSelectorName(PD->getGetterName());
1235 PropertySetter = getSelectorName(PD->getSetterName());
Devang Patel9d6c8572011-04-16 00:12:55 +00001236 PropertyAttributes = PD->getPropertyAttributes();
1237 }
1238 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1239 FieldLine, FieldSize, FieldAlign,
1240 FieldOffset, Flags, FieldTy,
1241 PropertyName, PropertyGetter,
1242 PropertySetter, PropertyAttributes);
Devang Patelf4c205b2009-02-26 21:10:26 +00001243 EltTys.push_back(FieldTy);
1244 }
Mike Stump11289f42009-09-09 15:08:12 +00001245
Jay Foaddbf81d82011-04-24 10:11:03 +00001246 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patelf4c205b2009-02-26 21:10:26 +00001247
Devang Patel01bb5ce2010-03-11 20:01:48 +00001248 RegionStack.pop_back();
1249 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1250 RegionMap.find(Ty->getDecl());
1251 if (RI != RegionMap.end())
1252 RegionMap.erase(RI);
1253
Devang Patelf4c205b2009-02-26 21:10:26 +00001254 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001255 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1256 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001257
Devang Patel36882c82011-05-12 19:07:41 +00001258 unsigned Flags = 0;
Devang Patel70d77d12011-05-12 21:14:54 +00001259 if (ID->getImplementation())
Devang Patele91b54c2011-05-12 21:29:57 +00001260 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
Devang Patel36882c82011-05-12 19:07:41 +00001261
Devang Patel00afcbe2010-12-08 22:42:58 +00001262 llvm::DIType RealDecl =
Devang Pateld7185b72011-02-22 18:56:36 +00001263 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
Devang Patel36882c82011-05-12 19:07:41 +00001264 Line, Size, Align, Flags,
Devang Patel00afcbe2010-12-08 22:42:58 +00001265 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +00001266
1267 // Now that we have a real decl for the struct, replace anything using the
1268 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +00001269 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001270 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +00001271
Devang Patelf4c205b2009-02-26 21:10:26 +00001272 return RealDecl;
1273}
1274
Devang Patel283e89d2011-01-17 22:23:07 +00001275llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001276 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel283e89d2011-01-17 22:23:07 +00001277 return CreateType(RT);
Chris Lattneraffb3732008-11-10 06:08:34 +00001278 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel283e89d2011-01-17 22:23:07 +00001279 return CreateEnumType(ET->getDecl());
Mike Stump11289f42009-09-09 15:08:12 +00001280
Chris Lattneraffb3732008-11-10 06:08:34 +00001281 return llvm::DIType();
1282}
1283
Devang Patelb4073382010-02-23 22:59:39 +00001284llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedman04831922010-08-22 01:00:03 +00001285 llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001286 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel0b37e792011-04-08 21:56:52 +00001287 int64_t NumElems = Ty->getNumElements();
1288 int64_t LowerBound = 0;
1289 if (NumElems == 0)
1290 // If number of elements are not known then this is an unbounded array.
1291 // Use Low = 1, Hi = 0 to express such arrays.
1292 LowerBound = 1;
1293 else
Devang Patelb4073382010-02-23 22:59:39 +00001294 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001295
Devang Patel0b37e792011-04-08 21:56:52 +00001296 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foaddbf81d82011-04-24 10:11:03 +00001297 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patelb4073382010-02-23 22:59:39 +00001298
1299 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1300 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1301
1302 return
Devang Pateld7185b72011-02-22 18:56:36 +00001303 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001304}
1305
Chris Lattneraffb3732008-11-10 06:08:34 +00001306llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001307 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001308 uint64_t Size;
1309 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001310
1311
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001312 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001313 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001314 Size = 0;
1315 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001316 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001317 } else if (Ty->isIncompleteArrayType()) {
1318 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001319 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patela540f142011-04-04 23:18:38 +00001320 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patel1ffe2342011-04-01 19:02:33 +00001321 Size = 0;
1322 Align = 0;
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001323 } else {
1324 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001325 Size = CGM.getContext().getTypeSize(Ty);
1326 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattneraffb3732008-11-10 06:08:34 +00001329 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1330 // interior arrays, do we care? Why aren't nested arrays represented the
1331 // obvious/recursive way?
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001332 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattneraffb3732008-11-10 06:08:34 +00001333 QualType EltTy(Ty, 0);
Devang Patelc0601d12010-10-06 18:30:00 +00001334 if (Ty->isIncompleteArrayType())
Chris Lattneraffb3732008-11-10 06:08:34 +00001335 EltTy = Ty->getElementType();
Devang Patelc0601d12010-10-06 18:30:00 +00001336 else {
1337 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Devang Patel0b37e792011-04-08 21:56:52 +00001338 int64_t UpperBound = 0;
1339 int64_t LowerBound = 0;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001340 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Devang Patelc0601d12010-10-06 18:30:00 +00001341 if (CAT->getSize().getZExtValue())
Devang Patel0b37e792011-04-08 21:56:52 +00001342 UpperBound = CAT->getSize().getZExtValue() - 1;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001343 } else
Devang Patel0b37e792011-04-08 21:56:52 +00001344 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1345 // arrays.
1346 LowerBound = 1;
1347
Devang Patelc0601d12010-10-06 18:30:00 +00001348 // FIXME: Verify this is right for VLAs.
Devang Patel0b37e792011-04-08 21:56:52 +00001349 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound, UpperBound));
Devang Patelc0601d12010-10-06 18:30:00 +00001350 EltTy = Ty->getElementType();
1351 }
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001352 }
Mike Stump11289f42009-09-09 15:08:12 +00001353
Jay Foaddbf81d82011-04-24 10:11:03 +00001354 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattneraffb3732008-11-10 06:08:34 +00001355
Devang Patele21912d2009-10-20 19:55:01 +00001356 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001357 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001358 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001359 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001360}
1361
Anders Carlsson443f6772009-11-06 19:19:55 +00001362llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001363 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001364 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1365 Ty, Ty->getPointeeType(), Unit);
1366}
Chris Lattneraffb3732008-11-10 06:08:34 +00001367
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001368llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1369 llvm::DIFile Unit) {
1370 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1371 Ty, Ty->getPointeeType(), Unit);
1372}
1373
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001374llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001375 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001376 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1377 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1378
1379 if (!Ty->getPointeeType()->isFunctionType()) {
1380 // We have a data member pointer type.
1381 return PointerDiffDITy;
1382 }
1383
1384 // We have a member function pointer type. Treat it as a struct with two
1385 // ptrdiff_t members.
1386 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1387
1388 uint64_t FieldOffset = 0;
Devang Patel00afcbe2010-12-08 22:42:58 +00001389 llvm::Value *ElementTypes[2];
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001390
1391 // FIXME: This should probably be a function type instead.
1392 ElementTypes[0] =
Devang Patel15013e72011-06-24 22:00:59 +00001393 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001394 Info.first, Info.second, FieldOffset, 0,
1395 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001396 FieldOffset += Info.first;
1397
1398 ElementTypes[1] =
Devang Patel15013e72011-06-24 22:00:59 +00001399 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001400 Info.first, Info.second, FieldOffset, 0,
1401 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001402
Jay Foaddbf81d82011-04-24 10:11:03 +00001403 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001404
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001405 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel00afcbe2010-12-08 22:42:58 +00001406 U, 0, FieldOffset,
1407 0, 0, Elements);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001408}
1409
Devang Patel41c20972010-08-23 22:07:25 +00001410/// CreateEnumType - get enumeration type.
Devang Patel283e89d2011-01-17 22:23:07 +00001411llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1412 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001413 SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel41c20972010-08-23 22:07:25 +00001414
1415 // Create DIEnumerator elements for each enumerator.
1416 for (EnumDecl::enumerator_iterator
1417 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1418 Enum != EnumEnd; ++Enum) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001419 Enumerators.push_back(
Devang Pateld7185b72011-02-22 18:56:36 +00001420 DBuilder.createEnumerator(Enum->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001421 Enum->getInitVal().getZExtValue()));
Devang Patel41c20972010-08-23 22:07:25 +00001422 }
1423
1424 // Return a CompositeType for the enum itself.
Jay Foaddbf81d82011-04-24 10:11:03 +00001425 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel41c20972010-08-23 22:07:25 +00001426
1427 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1428 unsigned Line = getLineNumber(ED->getLocation());
1429 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001430 uint64_t Align = 0;
1431 if (!ED->getTypeForDecl()->isIncompleteType()) {
1432 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1433 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1434 }
Devang Patel1bee63f2010-10-27 23:23:58 +00001435 llvm::DIDescriptor EnumContext =
John McCall147d0212011-02-22 22:38:33 +00001436 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel41c20972010-08-23 22:07:25 +00001437 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001438 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel00afcbe2010-12-08 22:42:58 +00001439 Size, Align, EltArray);
Devang Patel41c20972010-08-23 22:07:25 +00001440 return DbgTy;
1441}
1442
Douglas Gregor0f139a12009-12-21 20:18:30 +00001443static QualType UnwrapTypeForDebugInfo(QualType T) {
1444 do {
1445 QualType LastT = T;
1446 switch (T->getTypeClass()) {
1447 default:
1448 return T;
1449 case Type::TemplateSpecialization:
1450 T = cast<TemplateSpecializationType>(T)->desugar();
1451 break;
John McCall424cec92011-01-19 06:33:43 +00001452 case Type::TypeOfExpr:
1453 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001454 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001455 case Type::TypeOf:
1456 T = cast<TypeOfType>(T)->getUnderlyingType();
1457 break;
1458 case Type::Decltype:
1459 T = cast<DecltypeType>(T)->getUnderlyingType();
1460 break;
Alexis Hunte852b102011-05-24 22:41:36 +00001461 case Type::UnaryTransform:
1462 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1463 break;
John McCall81904512011-01-06 01:58:22 +00001464 case Type::Attributed:
1465 T = cast<AttributedType>(T)->getEquivalentType();
John McCall4223a9e2011-03-04 04:00:19 +00001466 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001467 case Type::Elaborated:
1468 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001469 break;
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001470 case Type::Paren:
1471 T = cast<ParenType>(T)->getInnerType();
1472 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001473 case Type::SubstTemplateTypeParm:
1474 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1475 break;
Anders Carlsson829c4132011-03-06 16:43:04 +00001476 case Type::Auto:
1477 T = cast<AutoType>(T)->getDeducedType();
1478 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001479 }
1480
1481 assert(T != LastT && "Type unwrapping failed to unwrap!");
1482 if (T == LastT)
1483 return T;
1484 } while (true);
1485
1486 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001487}
1488
Sanjiv Gupta98070572008-05-25 05:15:42 +00001489/// getOrCreateType - Get the type from the cache or create a new
1490/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001491llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001492 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001493 if (Ty.isNull())
1494 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregor0f139a12009-12-21 20:18:30 +00001496 // Unwrap the type as needed for debug information.
1497 Ty = UnwrapTypeForDebugInfo(Ty);
Devang Patel945b8ae2011-03-23 16:29:39 +00001498
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001499 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001500 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001501 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001502 if (it != TypeCache.end()) {
1503 // Verify that the debug info still exists.
1504 if (&*it->second)
1505 return llvm::DIType(cast<llvm::MDNode>(it->second));
1506 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001507
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001508 // Otherwise create the type.
1509 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001510
1511 // And update the type cache.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001512 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001513 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001514}
1515
Anders Carlsson6037e782009-11-14 20:52:05 +00001516/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001517llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001518 llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001519 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001520 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001521 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001522
Douglas Gregor0915b432009-12-21 19:57:21 +00001523 const char *Diag = 0;
1524
Sanjiv Gupta98070572008-05-25 05:15:42 +00001525 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001526 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001527#define TYPE(Class, Base)
1528#define ABSTRACT_TYPE(Class, Base)
1529#define NON_CANONICAL_TYPE(Class, Base)
1530#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1531#include "clang/AST/TypeNodes.def"
1532 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001533
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001534 case Type::ExtVector:
Devang Patelb4073382010-02-23 22:59:39 +00001535 case Type::Vector:
1536 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001537 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001538 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001539 case Type::ObjCObject:
1540 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001541 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001542 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patel86f30f52010-11-01 16:52:40 +00001543 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel4591f772010-12-09 00:25:29 +00001544 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001545 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001546 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001547 return CreateType(cast<BlockPointerType>(Ty), Unit);
1548 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001549 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001550 case Type::Enum:
Devang Patel283e89d2011-01-17 22:23:07 +00001551 return CreateType(cast<TagType>(Ty));
Chris Lattneraffb3732008-11-10 06:08:34 +00001552 case Type::FunctionProto:
1553 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001554 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001555 case Type::ConstantArray:
1556 case Type::VariableArray:
1557 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001558 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001559
1560 case Type::LValueReference:
1561 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001562 case Type::RValueReference:
1563 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001564
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001565 case Type::MemberPointer:
1566 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001567
John McCall81904512011-01-06 01:58:22 +00001568 case Type::Attributed:
Douglas Gregor0915b432009-12-21 19:57:21 +00001569 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001570 case Type::Elaborated:
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001571 case Type::Paren:
Douglas Gregor0915b432009-12-21 19:57:21 +00001572 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001573 case Type::TypeOfExpr:
1574 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001575 case Type::Decltype:
Alexis Hunte852b102011-05-24 22:41:36 +00001576 case Type::UnaryTransform:
Richard Smith30482bc2011-02-20 03:19:35 +00001577 case Type::Auto:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001578 llvm_unreachable("type should have been unwrapped!");
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001579 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001580 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001581
1582 assert(Diag && "Fall through without a diagnostic?");
1583 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1584 "debug information for %0 is not yet supported");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001585 CGM.getDiags().Report(DiagID)
Douglas Gregor0915b432009-12-21 19:57:21 +00001586 << Diag;
1587 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001588}
1589
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001590/// CreateMemberType - Create new member and increase Offset by FType's size.
1591llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001592 StringRef Name,
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001593 uint64_t *Offset) {
1594 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1595 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1596 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel15013e72011-06-24 22:00:59 +00001597 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001598 FieldSize, FieldAlign,
1599 *Offset, 0, FieldTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001600 *Offset += FieldSize;
1601 return Ty;
1602}
1603
Devang Patela6cb0642011-04-23 00:08:01 +00001604/// getFunctionDeclaration - Return debug info descriptor to describe method
1605/// declaration for the given method definition.
1606llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1607 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1608 if (!FD) return llvm::DISubprogram();
1609
1610 // Setup context.
1611 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1612
Devang Patela3e3fde2011-04-29 23:42:32 +00001613 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1614 MI = SPCache.find(FD);
1615 if (MI != SPCache.end()) {
1616 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1617 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1618 return SP;
1619 }
1620
Devang Patela6cb0642011-04-23 00:08:01 +00001621 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1622 E = FD->redecls_end(); I != E; ++I) {
1623 const FunctionDecl *NextFD = *I;
1624 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1625 MI = SPCache.find(NextFD);
1626 if (MI != SPCache.end()) {
1627 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1628 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1629 return SP;
1630 }
1631 }
1632 return llvm::DISubprogram();
1633}
1634
Devang Patel2780e452011-05-31 20:46:46 +00001635// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
1636// implicit parameter "this".
1637llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D, QualType FnType,
1638 llvm::DIFile F) {
1639 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1640 return getOrCreateMethodType(Method, F);
Devang Patel7ce99c32011-05-31 21:18:50 +00001641 else if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patel7ce99c32011-05-31 21:18:50 +00001642 // Add "self" and "_cmd"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001643 SmallVector<llvm::Value *, 16> Elts;
Devang Patel7ce99c32011-05-31 21:18:50 +00001644
1645 // First element is always return type. For 'void' functions it is NULL.
Devang Patel5c71c212011-05-31 22:21:11 +00001646 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patel7ce99c32011-05-31 21:18:50 +00001647 // "self" pointer is always first argument.
1648 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F));
1649 // "cmd" pointer is always second argument.
1650 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F));
Devang Patel5c71c212011-05-31 22:21:11 +00001651 // Get rest of the arguments.
1652 for (ObjCMethodDecl::param_iterator PI = OMethod->param_begin(),
1653 PE = OMethod->param_end(); PI != PE; ++PI)
1654 Elts.push_back(getOrCreateType((*PI)->getType(), F));
1655
Devang Patel7ce99c32011-05-31 21:18:50 +00001656 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1657 return DBuilder.createSubroutineType(F, EltTypeArray);
1658 }
Devang Patel2780e452011-05-31 20:46:46 +00001659 return getOrCreateType(FnType, F);
1660}
1661
Sanjiv Gupta98070572008-05-25 05:15:42 +00001662/// EmitFunctionStart - Constructs the debug code for entering a function -
1663/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001664void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001665 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001666 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001667
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001668 StringRef Name;
1669 StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001670
Devang Patel0884a602010-07-22 22:29:16 +00001671 FnBeginRegionCount.push_back(RegionStack.size());
1672
Devang Patel934661e2010-01-14 00:36:21 +00001673 const Decl *D = GD.getDecl();
Devang Patel095421b2011-04-05 20:28:21 +00001674
Devang Patel251f8592010-10-07 22:03:49 +00001675 unsigned Flags = 0;
Devang Patel33ddf692010-10-11 21:58:41 +00001676 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1677 llvm::DIDescriptor FDContext(Unit);
Devang Patelb87c4282011-04-05 22:54:11 +00001678 llvm::DIArray TParamsArray;
Devang Patel934661e2010-01-14 00:36:21 +00001679 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001680 // If there is a DISubprogram for this function available then use it.
1681 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1682 FI = SPCache.find(FD);
1683 if (FI != SPCache.end()) {
Gabor Greifbf986082010-09-18 13:00:17 +00001684 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001685 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1686 llvm::MDNode *SPN = SP;
1687 RegionStack.push_back(SPN);
1688 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001689 return;
1690 }
1691 }
Devang Patel934661e2010-01-14 00:36:21 +00001692 Name = getFunctionName(FD);
1693 // Use mangled name as linkage name for c/c++ functions.
Devang Patel04ab75c2011-05-02 22:49:30 +00001694 if (!Fn->hasInternalLinkage())
Devang Patelb7ff0da2011-05-02 22:37:48 +00001695 LinkageName = CGM.getMangledName(GD);
Devang Patelf79199d2010-10-22 17:11:50 +00001696 if (LinkageName == Name)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001697 LinkageName = StringRef();
Devang Patel251f8592010-10-07 22:03:49 +00001698 if (FD->hasPrototype())
1699 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel33ddf692010-10-11 21:58:41 +00001700 if (const NamespaceDecl *NSDecl =
1701 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel8c445292010-12-09 00:33:05 +00001702 FDContext = getOrCreateNameSpace(NSDecl);
Devang Patelf9076f32011-05-17 00:20:09 +00001703 else if (const RecordDecl *RDecl =
1704 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
1705 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patelb87c4282011-04-05 22:54:11 +00001706
1707 // Collect template parameters.
1708 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
David Chisnall6bf98ff2010-09-02 17:16:32 +00001709 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnallcf607442010-09-02 18:01:51 +00001710 Name = getObjCMethodName(OMD);
Devang Patel251f8592010-10-07 22:03:49 +00001711 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001712 } else {
Devang Patelf79199d2010-10-22 17:11:50 +00001713 // Use llvm function name.
Devang Patel934661e2010-01-14 00:36:21 +00001714 Name = Fn->getName();
Devang Patel251f8592010-10-07 22:03:49 +00001715 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001716 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001717 if (!Name.empty() && Name[0] == '\01')
1718 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001719
Devang Patel84715932010-04-24 00:49:16 +00001720 // It is expected that CurLoc is set before using EmitFunctionStart.
1721 // Usually, CurLoc points to the left bracket location of compound
1722 // statement representing function body.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001723 unsigned LineNo = getLineNumber(CurLoc);
Devang Pateldb2732a2010-09-29 21:05:52 +00001724 if (D->isImplicit())
1725 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patela6cb0642011-04-23 00:08:01 +00001726 llvm::DISubprogram SPDecl = getFunctionDeclaration(D);
Chris Lattneraffb3732008-11-10 06:08:34 +00001727 llvm::DISubprogram SP =
Devang Pateld7185b72011-02-22 18:56:36 +00001728 DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
Devang Patel2780e452011-05-31 20:46:46 +00001729 LineNo, getOrCreateFunctionType(D, FnType, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001730 Fn->hasInternalLinkage(), true/*definition*/,
Devang Patelb87c4282011-04-05 22:54:11 +00001731 Flags, CGM.getLangOptions().Optimize, Fn,
Devang Patela6cb0642011-04-23 00:08:01 +00001732 TParamsArray, SPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001733
Sanjiv Gupta98070572008-05-25 05:15:42 +00001734 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001735 llvm::MDNode *SPN = SP;
1736 RegionStack.push_back(SPN);
1737 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel0884a602010-07-22 22:29:16 +00001738
1739 // Clear stack used to keep track of #line directives.
1740 LineDirectiveFiles.clear();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001741}
1742
1743
Devang Patel11a42a42010-07-20 22:20:10 +00001744void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001745 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001746
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001747 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001748 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel206f5092011-07-19 00:52:18 +00001749 if (CurLoc == PrevLoc ||
Chandler Carruth35f53202011-07-25 16:49:02 +00001750 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patela2c048e2010-04-05 21:09:15 +00001751 // New Builder may not be in sync with CGDebugInfo.
1752 if (!Builder.getCurrentDebugLocation().isUnknown())
1753 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001754
1755 // Update last state.
1756 PrevLoc = CurLoc;
1757
Chris Lattnere675d0f2010-04-01 06:31:43 +00001758 llvm::MDNode *Scope = RegionStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00001759 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1760 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00001761 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001762}
1763
Devang Patel0884a602010-07-22 22:29:16 +00001764/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1765/// has introduced scope change.
1766void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1767 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1768 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1769 return;
1770 SourceManager &SM = CGM.getContext().getSourceManager();
1771 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1772 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1773
Douglas Gregorad3832e2010-11-11 20:45:16 +00001774 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1775 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel0884a602010-07-22 22:29:16 +00001776 return;
1777
1778 // If #line directive stack is empty then we are entering a new scope.
1779 if (LineDirectiveFiles.empty()) {
1780 EmitRegionStart(Builder);
1781 LineDirectiveFiles.push_back(PCLoc.getFilename());
1782 return;
1783 }
1784
1785 assert (RegionStack.size() >= LineDirectiveFiles.size()
1786 && "error handling #line regions!");
1787
1788 bool SeenThisFile = false;
Devang Patel28b52862010-09-15 20:50:40 +00001789 // Chek if current file is already seen earlier.
Devang Patel0884a602010-07-22 22:29:16 +00001790 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1791 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel28b52862010-09-15 20:50:40 +00001792 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel0884a602010-07-22 22:29:16 +00001793 SeenThisFile = true;
1794 break;
1795 }
1796
1797 // If #line for this file is seen earlier then pop out #line regions.
1798 if (SeenThisFile) {
1799 while (!LineDirectiveFiles.empty()) {
1800 const char *LastFile = LineDirectiveFiles.back();
1801 RegionStack.pop_back();
1802 LineDirectiveFiles.pop_back();
1803 if (!strcmp(PPLoc.getFilename(), LastFile))
1804 break;
1805 }
1806 return;
1807 }
1808
1809 // .. otherwise insert new #line region.
1810 EmitRegionStart(Builder);
1811 LineDirectiveFiles.push_back(PCLoc.getFilename());
1812
1813 return;
1814}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001815/// EmitRegionStart- Constructs the debug code for entering a declarative
1816/// region - "llvm.dbg.region.start.".
Devang Patel11a42a42010-07-20 22:20:10 +00001817void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001818 llvm::DIDescriptor D =
Devang Pateld7185b72011-02-22 18:56:36 +00001819 DBuilder.createLexicalBlock(RegionStack.empty() ?
Devang Patel00afcbe2010-12-08 22:42:58 +00001820 llvm::DIDescriptor() :
1821 llvm::DIDescriptor(RegionStack.back()),
1822 getOrCreateFile(CurLoc),
1823 getLineNumber(CurLoc),
1824 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001825 llvm::MDNode *DN = D;
1826 RegionStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001827}
1828
1829/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1830/// region - "llvm.dbg.region.end."
Devang Patel11a42a42010-07-20 22:20:10 +00001831void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001832 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1833
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001834 // Provide an region stop point.
Devang Patel11a42a42010-07-20 22:20:10 +00001835 EmitStopPoint(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001836
Sanjiv Gupta98070572008-05-25 05:15:42 +00001837 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001838}
1839
Devang Patel0884a602010-07-22 22:29:16 +00001840/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1841void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1842 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1843 unsigned RCount = FnBeginRegionCount.back();
1844 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1845
1846 // Pop all regions for this function.
1847 while (RegionStack.size() != RCount)
1848 EmitRegionEnd(Builder);
1849 FnBeginRegionCount.pop_back();
1850}
1851
Devang Patel535fdaf2010-02-10 18:49:08 +00001852// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1853// See BuildByRefType.
1854llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1855 uint64_t *XOffset) {
1856
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001857 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel535fdaf2010-02-10 18:49:08 +00001858 QualType FType;
1859 uint64_t FieldSize, FieldOffset;
1860 unsigned FieldAlign;
1861
Devang Patel408dcf62010-03-09 00:44:50 +00001862 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001863 QualType Type = VD->getType();
1864
1865 FieldOffset = 0;
1866 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001867 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1868 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001869 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001870 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1871 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1872
John McCall351762c2011-02-07 10:33:21 +00001873 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel535fdaf2010-02-10 18:49:08 +00001874 if (HasCopyAndDispose) {
1875 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001876 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1877 &FieldOffset));
1878 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1879 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001880 }
1881
1882 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8159c1f2011-04-22 17:34:18 +00001883 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00001884 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck8159c1f2011-04-22 17:34:18 +00001885 CharUnits FieldOffsetInBytes
1886 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
1887 CharUnits AlignedOffsetInBytes
1888 = FieldOffsetInBytes.RoundUpToAlignment(Align);
1889 CharUnits NumPaddingBytes
1890 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel535fdaf2010-02-10 18:49:08 +00001891
Ken Dyck8159c1f2011-04-22 17:34:18 +00001892 if (NumPaddingBytes.isPositive()) {
1893 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel535fdaf2010-02-10 18:49:08 +00001894 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1895 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001896 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001897 }
1898 }
1899
1900 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001901 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00001902 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8159c1f2011-04-22 17:34:18 +00001903 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel535fdaf2010-02-10 18:49:08 +00001904
1905 *XOffset = FieldOffset;
Devang Patel15013e72011-06-24 22:00:59 +00001906 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00001907 0, FieldSize, FieldAlign,
1908 FieldOffset, 0, FieldTy);
Devang Patel535fdaf2010-02-10 18:49:08 +00001909 EltTys.push_back(FieldTy);
1910 FieldOffset += FieldSize;
1911
Jay Foaddbf81d82011-04-24 10:11:03 +00001912 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel535fdaf2010-02-10 18:49:08 +00001913
Devang Pateldb2732a2010-09-29 21:05:52 +00001914 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel535fdaf2010-02-10 18:49:08 +00001915
Devang Pateld7185b72011-02-22 18:56:36 +00001916 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel00afcbe2010-12-08 22:42:58 +00001917 Elements);
Devang Patel535fdaf2010-02-10 18:49:08 +00001918}
Devang Patel00afcbe2010-12-08 22:42:58 +00001919
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001920/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001921void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel68a15252011-03-03 20:13:15 +00001922 llvm::Value *Storage,
1923 unsigned ArgNo, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001924 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1925
Devang Patel408dcf62010-03-09 00:44:50 +00001926 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001927 llvm::DIType Ty;
1928 uint64_t XOffset = 0;
1929 if (VD->hasAttr<BlocksAttr>())
1930 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1931 else
1932 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00001933
Devang Patel67eba802010-05-07 23:05:55 +00001934 // If there is not any debug info for type then do not emit debug info
1935 // for this variable.
1936 if (!Ty)
1937 return;
1938
Devang Patel25468052011-02-16 01:11:51 +00001939 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
1940 // If Storage is an aggregate returned as 'sret' then let debugger know
1941 // about this.
Devang Patel425909d2011-02-10 00:40:52 +00001942 if (Arg->hasStructRetAttr())
Devang Pateld7185b72011-02-22 18:56:36 +00001943 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00001944 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
1945 // If an aggregate variable has non trivial destructor or non trivial copy
1946 // constructor than it is pass indirectly. Let debug info know about this
1947 // by using reference of the aggregate type as a argument type.
1948 if (!Record->hasTrivialCopyConstructor() || !Record->hasTrivialDestructor())
Devang Pateld7185b72011-02-22 18:56:36 +00001949 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00001950 }
1951 }
Devang Patel425909d2011-02-10 00:40:52 +00001952
Chris Lattneraffb3732008-11-10 06:08:34 +00001953 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001954 unsigned Line = getLineNumber(VD->getLocation());
1955 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patel7c086222010-09-29 23:09:21 +00001956 unsigned Flags = 0;
1957 if (VD->isImplicit())
1958 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnere675d0f2010-04-01 06:31:43 +00001959 llvm::MDNode *Scope = RegionStack.back();
Devang Patel67f70aa2010-10-12 23:24:54 +00001960
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001961 StringRef Name = VD->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00001962 if (!Name.empty()) {
Devang Patelbc474982011-01-11 00:30:27 +00001963 if (VD->hasAttr<BlocksAttr>()) {
1964 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001965 SmallVector<llvm::Value *, 9> addr;
Chris Lattner2192fe52011-07-18 04:24:23 +00001966 llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel2d6390d2011-02-18 23:29:22 +00001967 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00001968 // offset of __forwarding field
Ken Dyckbbe38622011-04-22 17:41:34 +00001969 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00001970 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelbc474982011-01-11 00:30:27 +00001971 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00001972 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
1973 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00001974 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00001975 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelbc474982011-01-11 00:30:27 +00001976 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1977
1978 // Create the descriptor for the variable.
1979 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00001980 DBuilder.createComplexVariable(Tag,
Devang Patelbc474982011-01-11 00:30:27 +00001981 llvm::DIDescriptor(RegionStack.back()),
1982 VD->getName(), Unit, Line, Ty,
Jay Foaddbf81d82011-04-24 10:11:03 +00001983 addr, ArgNo);
Devang Patelbc474982011-01-11 00:30:27 +00001984
1985 // Insert an llvm.dbg.declare into the current block.
1986 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00001987 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelbc474982011-01-11 00:30:27 +00001988
1989 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1990 return;
1991 }
1992 // Create the descriptor for the variable.
Devang Patel67f70aa2010-10-12 23:24:54 +00001993 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00001994 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel00afcbe2010-12-08 22:42:58 +00001995 Name, Unit, Line, Ty,
Devang Patel68a15252011-03-03 20:13:15 +00001996 CGM.getLangOptions().Optimize, Flags, ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00001997
1998 // Insert an llvm.dbg.declare into the current block.
1999 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002000 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel67f70aa2010-10-12 23:24:54 +00002001
2002 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel31d1e212010-10-29 16:21:19 +00002003 return;
Devang Patel67f70aa2010-10-12 23:24:54 +00002004 }
2005
2006 // If VD is an anonymous union then Storage represents value for
2007 // all union fields.
John McCall147d0212011-02-22 22:38:33 +00002008 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2009 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2010 if (RD->isUnion()) {
2011 for (RecordDecl::field_iterator I = RD->field_begin(),
2012 E = RD->field_end();
2013 I != E; ++I) {
2014 FieldDecl *Field = *I;
2015 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002016 StringRef FieldName = Field->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002017
John McCall147d0212011-02-22 22:38:33 +00002018 // Ignore unnamed fields. Do not ignore unnamed records.
2019 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2020 continue;
Devang Patel67f70aa2010-10-12 23:24:54 +00002021
John McCall147d0212011-02-22 22:38:33 +00002022 // Use VarDecl's Tag, Scope and Line number.
2023 llvm::DIVariable D =
2024 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2025 FieldName, Unit, Line, FieldTy,
Devang Patel68a15252011-03-03 20:13:15 +00002026 CGM.getLangOptions().Optimize, Flags,
2027 ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002028
John McCall147d0212011-02-22 22:38:33 +00002029 // Insert an llvm.dbg.declare into the current block.
2030 llvm::Instruction *Call =
2031 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel67f70aa2010-10-12 23:24:54 +00002032
John McCall147d0212011-02-22 22:38:33 +00002033 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel67f70aa2010-10-12 23:24:54 +00002034 }
John McCall147d0212011-02-22 22:38:33 +00002035 }
2036 }
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002037}
2038
Devang Patel4f325d12011-04-25 23:43:36 +00002039void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2040 llvm::Value *Storage,
2041 CGBuilderTy &Builder) {
2042 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2043}
Mike Stump2e722b92009-09-30 02:43:10 +00002044
Devang Patel4f325d12011-04-25 23:43:36 +00002045void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2046 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2047 const CGBlockInfo &blockInfo) {
2048 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
2049
Devang Patel42fb6f82010-04-26 23:28:46 +00002050 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00002051 return;
Devang Patel4f325d12011-04-25 23:43:36 +00002052
John McCall351762c2011-02-07 10:33:21 +00002053 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patel4f325d12011-04-25 23:43:36 +00002054
Mike Stump2e722b92009-09-30 02:43:10 +00002055 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00002056 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002057 llvm::DIType Ty;
John McCall351762c2011-02-07 10:33:21 +00002058 if (isByRef)
Devang Patel535fdaf2010-02-10 18:49:08 +00002059 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2060 else
2061 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00002062
2063 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002064 unsigned Line = getLineNumber(VD->getLocation());
2065 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00002066
John McCall351762c2011-02-07 10:33:21 +00002067 const llvm::TargetData &target = CGM.getTargetData();
2068
2069 CharUnits offset = CharUnits::fromQuantity(
2070 target.getStructLayout(blockInfo.StructureType)
2071 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2072
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002073 SmallVector<llvm::Value *, 9> addr;
Chris Lattner2192fe52011-07-18 04:24:23 +00002074 llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel2d6390d2011-02-18 23:29:22 +00002075 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattnerbf784782010-01-25 03:29:35 +00002076 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall351762c2011-02-07 10:33:21 +00002077 if (isByRef) {
Devang Patel2d6390d2011-02-18 23:29:22 +00002078 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2079 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002080 // offset of __forwarding field
Devang Patele67eca42011-04-26 21:16:49 +00002081 offset = CGM.getContext().toCharUnitsFromBits(target.getPointerSizeInBits());
Chris Lattnerbf784782010-01-25 03:29:35 +00002082 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002083 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2084 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002085 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002086 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattnerbf784782010-01-25 03:29:35 +00002087 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00002088 }
2089
2090 // Create the descriptor for the variable.
2091 llvm::DIVariable D =
Devang Patel4f325d12011-04-25 23:43:36 +00002092 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
2093 llvm::DIDescriptor(RegionStack.back()),
Jay Foaddbf81d82011-04-24 10:11:03 +00002094 VD->getName(), Unit, Line, Ty, addr);
Mike Stump2e722b92009-09-30 02:43:10 +00002095 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00002096 llvm::Instruction *Call =
Devang Patel420c8de2011-04-25 23:52:27 +00002097 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Chris Lattner5e124bf2009-12-28 21:44:41 +00002098
Chris Lattnere675d0f2010-04-01 06:31:43 +00002099 llvm::MDNode *Scope = RegionStack.back();
Devang Patel82bbfb52010-05-10 23:48:38 +00002100 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stump2e722b92009-09-30 02:43:10 +00002101}
2102
Chris Lattneraffb3732008-11-10 06:08:34 +00002103/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2104/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00002105void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel68a15252011-03-03 20:13:15 +00002106 unsigned ArgNo,
Devang Patel25468052011-02-16 01:11:51 +00002107 CGBuilderTy &Builder) {
Devang Patel68a15252011-03-03 20:13:15 +00002108 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00002109}
2110
John McCall147d0212011-02-22 22:38:33 +00002111namespace {
2112 struct BlockLayoutChunk {
2113 uint64_t OffsetInBits;
2114 const BlockDecl::Capture *Capture;
2115 };
2116 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2117 return l.OffsetInBits < r.OffsetInBits;
2118 }
2119}
Chris Lattneraffb3732008-11-10 06:08:34 +00002120
John McCall147d0212011-02-22 22:38:33 +00002121void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2122 llvm::Value *addr,
2123 CGBuilderTy &Builder) {
2124 ASTContext &C = CGM.getContext();
2125 const BlockDecl *blockDecl = block.getBlockDecl();
2126
2127 // Collect some general information about the block's location.
2128 SourceLocation loc = blockDecl->getCaretLocation();
2129 llvm::DIFile tunit = getOrCreateFile(loc);
2130 unsigned line = getLineNumber(loc);
2131 unsigned column = getColumnNumber(loc);
2132
2133 // Build the debug-info type for the block literal.
Nick Lewyckyfc49f722011-05-02 01:41:48 +00002134 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall147d0212011-02-22 22:38:33 +00002135
2136 const llvm::StructLayout *blockLayout =
2137 CGM.getTargetData().getStructLayout(block.StructureType);
2138
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002139 SmallVector<llvm::Value*, 16> fields;
John McCall147d0212011-02-22 22:38:33 +00002140 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2141 blockLayout->getElementOffsetInBits(0),
Devang Patel15013e72011-06-24 22:00:59 +00002142 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002143 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2144 blockLayout->getElementOffsetInBits(1),
Devang Patel15013e72011-06-24 22:00:59 +00002145 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002146 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2147 blockLayout->getElementOffsetInBits(2),
Devang Patel15013e72011-06-24 22:00:59 +00002148 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002149 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2150 blockLayout->getElementOffsetInBits(3),
Devang Patel15013e72011-06-24 22:00:59 +00002151 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002152 fields.push_back(createFieldType("__descriptor",
2153 C.getPointerType(block.NeedsCopyDispose ?
2154 C.getBlockDescriptorExtendedType() :
2155 C.getBlockDescriptorType()),
2156 0, loc, AS_public,
2157 blockLayout->getElementOffsetInBits(4),
Devang Patel15013e72011-06-24 22:00:59 +00002158 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002159
2160 // We want to sort the captures by offset, not because DWARF
2161 // requires this, but because we're paranoid about debuggers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002162 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall147d0212011-02-22 22:38:33 +00002163
2164 // 'this' capture.
2165 if (blockDecl->capturesCXXThis()) {
2166 BlockLayoutChunk chunk;
2167 chunk.OffsetInBits =
2168 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2169 chunk.Capture = 0;
2170 chunks.push_back(chunk);
2171 }
2172
2173 // Variable captures.
2174 for (BlockDecl::capture_const_iterator
2175 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2176 i != e; ++i) {
2177 const BlockDecl::Capture &capture = *i;
2178 const VarDecl *variable = capture.getVariable();
2179 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2180
2181 // Ignore constant captures.
2182 if (captureInfo.isConstant())
2183 continue;
2184
2185 BlockLayoutChunk chunk;
2186 chunk.OffsetInBits =
2187 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2188 chunk.Capture = &capture;
2189 chunks.push_back(chunk);
2190 }
2191
2192 // Sort by offset.
2193 llvm::array_pod_sort(chunks.begin(), chunks.end());
2194
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002195 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall147d0212011-02-22 22:38:33 +00002196 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2197 uint64_t offsetInBits = i->OffsetInBits;
2198 const BlockDecl::Capture *capture = i->Capture;
2199
2200 // If we have a null capture, this must be the C++ 'this' capture.
2201 if (!capture) {
2202 const CXXMethodDecl *method =
2203 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2204 QualType type = method->getThisType(C);
2205
2206 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel15013e72011-06-24 22:00:59 +00002207 offsetInBits, tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002208 continue;
2209 }
2210
2211 const VarDecl *variable = capture->getVariable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002212 StringRef name = variable->getName();
John McCall81a325e2011-03-02 06:57:14 +00002213
2214 llvm::DIType fieldType;
2215 if (capture->isByRef()) {
2216 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2217
2218 // FIXME: this creates a second copy of this type!
2219 uint64_t xoffset;
2220 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2221 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel15013e72011-06-24 22:00:59 +00002222 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCall81a325e2011-03-02 06:57:14 +00002223 ptrInfo.first, ptrInfo.second,
2224 offsetInBits, 0, fieldType);
2225 } else {
2226 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel15013e72011-06-24 22:00:59 +00002227 loc, AS_public, offsetInBits, tunit, tunit);
John McCall81a325e2011-03-02 06:57:14 +00002228 }
2229 fields.push_back(fieldType);
John McCall147d0212011-02-22 22:38:33 +00002230 }
2231
2232 llvm::SmallString<36> typeName;
2233 llvm::raw_svector_ostream(typeName)
2234 << "__block_literal_" << CGM.getUniqueBlockCount();
2235
Jay Foaddbf81d82011-04-24 10:11:03 +00002236 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall147d0212011-02-22 22:38:33 +00002237
2238 llvm::DIType type =
2239 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2240 CGM.getContext().toBits(block.BlockSize),
2241 CGM.getContext().toBits(block.BlockAlign),
2242 0, fieldsArray);
2243 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2244
2245 // Get overall information about the block.
2246 unsigned flags = llvm::DIDescriptor::FlagArtificial;
2247 llvm::MDNode *scope = RegionStack.back();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002248 StringRef name = ".block_descriptor";
John McCall147d0212011-02-22 22:38:33 +00002249
2250 // Create the descriptor for the parameter.
2251 llvm::DIVariable debugVar =
2252 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2253 llvm::DIDescriptor(scope),
2254 name, tunit, line, type,
Devang Patel68a15252011-03-03 20:13:15 +00002255 CGM.getLangOptions().Optimize, flags,
2256 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall147d0212011-02-22 22:38:33 +00002257
2258 // Insert an llvm.dbg.value into the current block.
2259 llvm::Instruction *declare =
2260 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2261 Builder.GetInsertBlock());
2262 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2263}
Chris Lattneraffb3732008-11-10 06:08:34 +00002264
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002265/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00002266void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00002267 const VarDecl *D) {
2268
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002269 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002270 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002271 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00002272
Devang Patel7b7f46f2010-02-01 21:34:11 +00002273 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002274 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002275
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002276 // CodeGen turns int[] into int[1] so we'll do the same here.
2277 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002278
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002279 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002280 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002281
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002282 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002283 ArrayType::Normal, 0);
2284 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002285 StringRef DeclName = D->getName();
2286 StringRef LinkageName;
Devang Patel84d40a42011-02-09 19:16:38 +00002287 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2288 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00002289 LinkageName = Var->getName();
Devang Patelf79199d2010-10-22 17:11:50 +00002290 if (LinkageName == DeclName)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002291 LinkageName = StringRef();
Devang Patel7b7f46f2010-02-01 21:34:11 +00002292 llvm::DIDescriptor DContext =
Devang Patel8c445292010-12-09 00:33:05 +00002293 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Pateld7185b72011-02-22 18:56:36 +00002294 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +00002295 Unit, LineNo, getOrCreateType(T, Unit),
2296 Var->hasInternalLinkage(), Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002297}
2298
Devang Patelf4c205b2009-02-26 21:10:26 +00002299/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00002300void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00002301 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00002302 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002303 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002304 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00002305
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002306 StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00002307
Devang Patel3efd1472010-02-01 21:52:22 +00002308 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00002309 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002310
Devang Patelf4c205b2009-02-26 21:10:26 +00002311 // CodeGen turns int[] into int[1] so we'll do the same here.
2312 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002313
Devang Patelf4c205b2009-02-26 21:10:26 +00002314 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002315 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002316
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002317 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00002318 ArrayType::Normal, 0);
2319 }
2320
Devang Pateld7185b72011-02-22 18:56:36 +00002321 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel00afcbe2010-12-08 22:42:58 +00002322 getOrCreateType(T, Unit),
2323 Var->hasInternalLinkage(), Var);
Devang Patelf4c205b2009-02-26 21:10:26 +00002324}
Devang Patel973f2eb2010-02-01 19:16:32 +00002325
Devang Pateldc866e12010-08-10 17:53:33 +00002326/// EmitGlobalVariable - Emit global variable's debug info.
2327void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCalla2fabff2010-10-09 01:34:31 +00002328 llvm::Constant *Init) {
Devang Patele03edfd2010-08-10 07:24:25 +00002329 // Create the descriptor for the variable.
2330 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002331 StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00002332 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00002333 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2334 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel283e89d2011-01-17 22:23:07 +00002335 Ty = CreateEnumType(ED);
Devang Patel41c20972010-08-23 22:07:25 +00002336 }
Devang Patel76e3b532010-08-10 18:27:15 +00002337 // Do not use DIGlobalVariable for enums.
2338 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2339 return;
Devang Pateld7185b72011-02-22 18:56:36 +00002340 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002341 getLineNumber(VD->getLocation()),
2342 Ty, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00002343}
2344
Devang Patel973f2eb2010-02-01 19:16:32 +00002345/// getOrCreateNamesSpace - Return namespace descriptor for the given
2346/// namespace decl.
2347llvm::DINameSpace
Devang Patel8c445292010-12-09 00:33:05 +00002348CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patel973f2eb2010-02-01 19:16:32 +00002349 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2350 NameSpaceCache.find(NSDecl);
2351 if (I != NameSpaceCache.end())
2352 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2353
Devang Patelc5ffabc2010-05-12 23:46:38 +00002354 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelfaadd7b2010-10-28 19:12:46 +00002355 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00002356 llvm::DIDescriptor Context =
Devang Patel8c445292010-12-09 00:33:05 +00002357 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patel973f2eb2010-02-01 19:16:32 +00002358 llvm::DINameSpace NS =
Devang Pateld7185b72011-02-22 18:56:36 +00002359 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002360 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00002361 return NS;
2362}
Devang Patel945b8ae2011-03-23 16:29:39 +00002363
2364/// UpdateCompletedType - Update type cache because the type is now
2365/// translated.
2366void CGDebugInfo::UpdateCompletedType(const TagDecl *TD) {
2367 QualType Ty = CGM.getContext().getTagDeclType(TD);
2368
2369 // If the type exist in type cache then remove it from the cache.
2370 // There is no need to prepare debug info for the completed type
2371 // right now. It will be generated on demand lazily.
2372 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2373 TypeCache.find(Ty.getAsOpaquePtr());
2374 if (it != TypeCache.end())
2375 TypeCache.erase(it);
2376}