blob: 57d8108925d344a26b54680057ab7422b802c121 [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"
Benjamin Kramer3307c5082012-02-04 12:31:12 +000024#include "clang/Basic/SourceManager.h"
Benjamin Kramer7ec12c92012-02-07 22:29:24 +000025#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"
Benjamin Kramerfd0b05f2011-10-14 18:45:11 +000036#include "llvm/Support/FileSystem.h"
John McCall351762c2011-02-07 10:33:21 +000037#include "llvm/Target/TargetData.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000038using namespace clang;
39using namespace clang::CodeGen;
40
Anders Carlsson3efc6e62009-12-06 18:00:51 +000041CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel00afcbe2010-12-08 22:42:58 +000042 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman196f7102010-08-20 22:02:57 +000043 BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000044 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000045}
46
Chris Lattneraffb3732008-11-10 06:08:34 +000047CGDebugInfo::~CGDebugInfo() {
Eric Christopherfefafac2011-10-11 23:00:51 +000048 assert(LexicalBlockStack.empty() &&
49 "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) {
Eric Christopherfb4cd402011-10-11 23:00:45 +000053 // If the new location isn't valid return.
54 if (!Loc.isValid()) return;
55
56 CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
Eric Christopher7cdf9482011-10-13 21:45:18 +000057
58 // If we've changed files in the middle of a lexical scope go ahead
59 // and create a new lexical scope with file node if it's different
60 // from the one in the scope.
61 if (LexicalBlockStack.empty()) return;
62
63 SourceManager &SM = CGM.getContext().getSourceManager();
64 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
65 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
66
67 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
68 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
69 return;
70
71 llvm::MDNode *LB = LexicalBlockStack.back();
72 llvm::DIScope Scope = llvm::DIScope(LB);
73 if (Scope.isLexicalBlockFile()) {
74 llvm::DILexicalBlockFile LBF = llvm::DILexicalBlockFile(LB);
75 llvm::DIDescriptor D
76 = DBuilder.createLexicalBlockFile(LBF.getScope(),
Devang Patel00fca3a2012-02-08 00:10:20 +000077 getOrCreateFile(CurLoc));
Eric Christopher7cdf9482011-10-13 21:45:18 +000078 llvm::MDNode *N = D;
79 LexicalBlockStack.pop_back();
80 LexicalBlockStack.push_back(N);
81 } else if (Scope.isLexicalBlock()) {
82 llvm::DIDescriptor D
83 = DBuilder.createLexicalBlockFile(Scope, getOrCreateFile(CurLoc));
84 llvm::MDNode *N = D;
85 LexicalBlockStack.pop_back();
86 LexicalBlockStack.push_back(N);
87 }
Sanjiv Gupta98070572008-05-25 05:15:42 +000088}
89
Devang Patel7bfc5962010-01-28 23:15:27 +000090/// getContextDescriptor - Get context info for the decl.
Devang Patel8c445292010-12-09 00:33:05 +000091llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Patel7b7f46f2010-02-01 21:34:11 +000092 if (!Context)
Devang Patel8c445292010-12-09 00:33:05 +000093 return TheCU;
Devang Patel7b7f46f2010-02-01 21:34:11 +000094
95 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
96 I = RegionMap.find(Context);
97 if (I != RegionMap.end())
Gabor Greifbf986082010-09-18 13:00:17 +000098 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000099
Devang Patel7b7f46f2010-02-01 21:34:11 +0000100 // Check namespace.
101 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel8c445292010-12-09 00:33:05 +0000102 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel98f21712010-05-13 23:52:37 +0000103
104 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
105 if (!RDecl->isDependentType()) {
Devang Patel8e007302010-10-28 17:27:32 +0000106 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel8c445292010-12-09 00:33:05 +0000107 getOrCreateMainFile());
Devang Patel98f21712010-05-13 23:52:37 +0000108 return llvm::DIDescriptor(Ty);
109 }
110 }
Devang Patel8c445292010-12-09 00:33:05 +0000111 return TheCU;
Devang Patelfaf7e9a2009-10-06 00:35:31 +0000112}
113
Devang Patel934661e2010-01-14 00:36:21 +0000114/// getFunctionName - Get function name for the given FunctionDecl. If the
115/// name is constructred on demand (e.g. C++ destructor) then the name
116/// is stored on the side.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000117StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
Devang Patel934661e2010-01-14 00:36:21 +0000118 assert (FD && "Invalid FunctionDecl!");
119 IdentifierInfo *FII = FD->getIdentifier();
Eric Christopherd36ad662012-03-14 00:25:46 +0000120 FunctionTemplateSpecializationInfo *Info
121 = FD->getTemplateSpecializationInfo();
122 if (!Info && FII)
Devang Patel934661e2010-01-14 00:36:21 +0000123 return FII->getName();
124
125 // Otherwise construct human readable name for debug info.
126 std::string NS = FD->getNameAsString();
127
Eric Christopherd36ad662012-03-14 00:25:46 +0000128 // Add any template specialization args.
129 if (Info) {
130 const TemplateArgumentList *TArgs = Info->TemplateArguments;
131 const TemplateArgument *Args = TArgs->data();
132 unsigned NumArgs = TArgs->size();
133 PrintingPolicy Policy(CGM.getLangOpts());
134 NS += TemplateSpecializationType::PrintTemplateArgumentList(Args,
135 NumArgs,
136 Policy);
137 }
138
Devang Patel934661e2010-01-14 00:36:21 +0000139 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000140 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +0000141 memcpy(StrPtr, NS.data(), NS.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000142 return StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +0000143}
144
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000145StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000146 SmallString<256> MethodName;
David Chisnallcf607442010-09-02 18:01:51 +0000147 llvm::raw_svector_ostream OS(MethodName);
148 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
149 const DeclContext *DC = OMD->getDeclContext();
Devang Patel8e007302010-10-28 17:27:32 +0000150 if (const ObjCImplementationDecl *OID =
151 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnallcf607442010-09-02 18:01:51 +0000152 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000153 } else if (const ObjCInterfaceDecl *OID =
154 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanianf34011e2010-10-18 17:51:06 +0000155 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000156 } else if (const ObjCCategoryImplDecl *OCD =
157 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnallcf607442010-09-02 18:01:51 +0000158 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
159 OCD->getIdentifier()->getNameStart() << ')';
160 }
161 OS << ' ' << OMD->getSelector().getAsString() << ']';
162
163 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
164 memcpy(StrPtr, MethodName.begin(), OS.tell());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000165 return StringRef(StrPtr, OS.tell());
David Chisnallcf607442010-09-02 18:01:51 +0000166}
167
Eric Christopherbd9c9102012-03-29 17:31:33 +0000168/// getSelectorName - Return selector name. This is used for debugging
169/// info.
170StringRef CGDebugInfo::getSelectorName(Selector S) {
171 const std::string &SName = S.getAsString();
172 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
173 memcpy(StrPtr, SName.data(), SName.size());
174 return StringRef(StrPtr, SName.size());
175}
176
Devang Patel6c018202010-07-20 20:24:18 +0000177/// getClassName - Get class name including template argument list.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000178StringRef
Eric Christopherb2db8e42012-02-08 01:53:14 +0000179CGDebugInfo::getClassName(const RecordDecl *RD) {
180 const ClassTemplateSpecializationDecl *Spec
Devang Patel6c018202010-07-20 20:24:18 +0000181 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
182 if (!Spec)
183 return RD->getName();
184
185 const TemplateArgument *Args;
186 unsigned NumArgs;
Devang Patel6c018202010-07-20 20:24:18 +0000187 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
188 const TemplateSpecializationType *TST =
189 cast<TemplateSpecializationType>(TAW->getType());
190 Args = TST->getArgs();
191 NumArgs = TST->getNumArgs();
192 } else {
193 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000194 Args = TemplateArgs.data();
195 NumArgs = TemplateArgs.size();
Devang Patel6c018202010-07-20 20:24:18 +0000196 }
Benjamin Kramer6d3f1112012-04-13 18:00:37 +0000197 StringRef Name = RD->getIdentifier()->getName();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000198 PrintingPolicy Policy(CGM.getLangOpts());
Benjamin Kramer6d3f1112012-04-13 18:00:37 +0000199 std::string TemplateArgList =
200 TemplateSpecializationType::PrintTemplateArgumentList(Args, NumArgs, Policy);
Devang Patel6c018202010-07-20 20:24:18 +0000201
202 // Copy this name on the side and use its reference.
Benjamin Kramer6d3f1112012-04-13 18:00:37 +0000203 size_t Length = Name.size() + TemplateArgList.size();
204 char *StrPtr = DebugInfoNames.Allocate<char>(Length);
205 memcpy(StrPtr, Name.data(), Name.size());
206 memcpy(StrPtr + Name.size(), TemplateArgList.data(), TemplateArgList.size());
207 return StringRef(StrPtr, Length);
Devang Patel6c018202010-07-20 20:24:18 +0000208}
209
Devang Patel408dcf62010-03-09 00:44:50 +0000210/// getOrCreateFile - Get the file debug info descriptor for the input location.
211llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000212 if (!Loc.isValid())
213 // If Location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000214 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel00afcbe2010-12-08 22:42:58 +0000215
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000216 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000217 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000218
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000219 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregorad3832e2010-11-11 20:45:16 +0000220 // If the location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000221 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregorad3832e2010-11-11 20:45:16 +0000222
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000223 // Cache the results.
224 const char *fname = PLoc.getFilename();
225 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
226 DIFileCache.find(fname);
227
228 if (it != DIFileCache.end()) {
229 // Verify that the information still exists.
230 if (&*it->second)
231 return llvm::DIFile(cast<llvm::MDNode>(it->second));
232 }
233
Devang Pateld7185b72011-02-22 18:56:36 +0000234 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000235
Devang Patelba4ad7f2010-05-07 18:12:35 +0000236 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000237 return F;
Devang Patel408dcf62010-03-09 00:44:50 +0000238}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000239
Devang Pateled23f182010-10-28 22:03:20 +0000240/// getOrCreateMainFile - Get the file info for main compile unit.
241llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Pateld7185b72011-02-22 18:56:36 +0000242 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Pateled23f182010-10-28 22:03:20 +0000243}
244
Devang Patelc5ffabc2010-05-12 23:46:38 +0000245/// getLineNumber - Get line number for the location. If location is invalid
246/// then use current location.
247unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
Devang Patelf93d0b82012-02-06 23:24:13 +0000248 if (Loc.isInvalid() && CurLoc.isInvalid())
249 return 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000250 SourceManager &SM = CGM.getContext().getSourceManager();
251 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregorad3832e2010-11-11 20:45:16 +0000252 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000253}
254
255/// getColumnNumber - Get column number for the location. If location is
256/// invalid then use current location.
257unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
Devang Patelf93d0b82012-02-06 23:24:13 +0000258 if (Loc.isInvalid() && CurLoc.isInvalid())
259 return 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000260 SourceManager &SM = CGM.getContext().getSourceManager();
261 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregorad3832e2010-11-11 20:45:16 +0000262 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000263}
264
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000265StringRef CGDebugInfo::getCurrentDirname() {
Nick Lewyckyba743b72011-10-21 02:32:14 +0000266 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
267 return CGM.getCodeGenOpts().DebugCompilationDir;
268
Devang Patel6014edd2010-07-27 15:17:16 +0000269 if (!CWDName.empty())
270 return CWDName;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000271 SmallString<256> CWD;
Benjamin Kramerfd0b05f2011-10-14 18:45:11 +0000272 llvm::sys::fs::current_path(CWD);
273 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
274 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000275 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patel6014edd2010-07-27 15:17:16 +0000276}
277
Devang Patel408dcf62010-03-09 00:44:50 +0000278/// CreateCompileUnit - Create new compile unit.
279void CGDebugInfo::CreateCompileUnit() {
280
281 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000282 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000283 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
284 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000285 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000286
Douglas Gregor65f7a3f2010-03-22 21:28:29 +0000287 // The main file name provided via the "-main-file-name" option contains just
288 // the file name itself with no path information. This file name may have had
289 // a relative path, so we look into the actual file entry for the main
290 // file to determine the real absolute path for the file.
Devang Patelcb9fe9e2010-07-23 23:04:28 +0000291 std::string MainFileDir;
Devang Patel6014edd2010-07-27 15:17:16 +0000292 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000293 MainFileDir = MainFile->getDir()->getName();
Devang Patel6014edd2010-07-27 15:17:16 +0000294 if (MainFileDir != ".")
295 MainFileName = MainFileDir + "/" + MainFileName;
296 }
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000297
Devang Patel6014edd2010-07-27 15:17:16 +0000298 // Save filename string.
299 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
300 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000301 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patel6014edd2010-07-27 15:17:16 +0000302
Chris Lattner8c37df42009-03-25 03:28:08 +0000303 unsigned LangTag;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000304 const LangOptions &LO = CGM.getLangOpts();
Chris Lattner8c37df42009-03-25 03:28:08 +0000305 if (LO.CPlusPlus) {
306 if (LO.ObjC1)
307 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
308 else
309 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
310 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000311 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000312 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000313 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000314 } else {
315 LangTag = llvm::dwarf::DW_LANG_C89;
316 }
Devang Patel75009452009-04-17 21:06:59 +0000317
Daniel Dunbar64c222a2010-08-24 17:41:09 +0000318 std::string Producer = getClangFullVersion();
Chris Lattner5912de12009-05-02 01:00:04 +0000319
320 // Figure out which version of the ObjC runtime we have.
321 unsigned RuntimeVers = 0;
322 if (LO.ObjC1)
323 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000324
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000325 // Create new compile unit.
Devang Pateld7185b72011-02-22 18:56:36 +0000326 DBuilder.createCompileUnit(
Devang Patel4f6e73b2010-07-27 20:49:59 +0000327 LangTag, Filename, getCurrentDirname(),
Devang Patel00afcbe2010-12-08 22:42:58 +0000328 Producer,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000329 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel00afcbe2010-12-08 22:42:58 +0000330 // FIXME - Eliminate TheCU.
331 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000332}
333
Devang Patel410dc002009-02-25 01:36:11 +0000334/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000335/// one if necessary.
Devang Patel86f30f52010-11-01 16:52:40 +0000336llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000337 unsigned Encoding = 0;
Argyrios Kyrtzidisbbff3da2012-05-05 04:20:28 +0000338 StringRef BTName;
Chris Lattneraffb3732008-11-10 06:08:34 +0000339 switch (BT->getKind()) {
John McCalle314e272011-10-18 21:02:43 +0000340#define BUILTIN_TYPE(Id, SingletonId)
341#define PLACEHOLDER_TYPE(Id, SingletonId) \
342 case BuiltinType::Id:
343#include "clang/AST/BuiltinTypes.def"
Devang Patelb0fa5b52011-09-12 18:50:21 +0000344 case BuiltinType::Dependent:
John McCalle314e272011-10-18 21:02:43 +0000345 llvm_unreachable("Unexpected builtin type");
Devang Patelb0fa5b52011-09-12 18:50:21 +0000346 case BuiltinType::NullPtr:
Devang Patelecaf9ac2011-09-14 23:14:14 +0000347 return DBuilder.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000348 createNullPtrType(BT->getName(CGM.getContext().getLangOpts()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000349 case BuiltinType::Void:
350 return llvm::DIType();
Devang Patela652fab2010-07-28 01:33:15 +0000351 case BuiltinType::ObjCClass:
Eric Christophere908a7a2012-02-20 18:05:04 +0000352 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher3cc207b2012-04-23 19:00:24 +0000353 "objc_class", TheCU,
354 getOrCreateMainFile(), 0);
Devang Patela652fab2010-07-28 01:33:15 +0000355 case BuiltinType::ObjCId: {
356 // typedef struct objc_class *Class;
357 // typedef struct objc_object {
358 // Class isa;
359 // } *id;
360
Eric Christopher55b2bfa2012-02-23 00:43:12 +0000361 // TODO: Cache these two types to avoid duplicates.
Eric Christophere908a7a2012-02-20 18:05:04 +0000362 llvm::DIType OCTy =
363 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher3cc207b2012-04-23 19:00:24 +0000364 "objc_class", TheCU, getOrCreateMainFile(), 0);
Devang Patela652fab2010-07-28 01:33:15 +0000365 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
366
Devang Pateld7185b72011-02-22 18:56:36 +0000367 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patela652fab2010-07-28 01:33:15 +0000368
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000369 SmallVector<llvm::Value *, 16> EltTys;
Devang Patela652fab2010-07-28 01:33:15 +0000370 llvm::DIType FieldTy =
Devang Patel15013e72011-06-24 22:00:59 +0000371 DBuilder.createMemberType(getOrCreateMainFile(), "isa",
372 getOrCreateMainFile(), 0, Size,
373 0, 0, 0, ISATy);
Devang Patela652fab2010-07-28 01:33:15 +0000374 EltTys.push_back(FieldTy);
Jay Foaddbf81d82011-04-24 10:11:03 +0000375 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patela652fab2010-07-28 01:33:15 +0000376
Devang Pateld7185b72011-02-22 18:56:36 +0000377 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel00afcbe2010-12-08 22:42:58 +0000378 getOrCreateMainFile(),
379 0, 0, 0, 0, Elements);
Devang Patela652fab2010-07-28 01:33:15 +0000380 }
Devang Patel19ba2b42011-02-09 03:15:05 +0000381 case BuiltinType::ObjCSel: {
Eric Christophere908a7a2012-02-20 18:05:04 +0000382 return
383 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher3cc207b2012-04-23 19:00:24 +0000384 "objc_selector", TheCU, getOrCreateMainFile(),
Eric Christopher8678a4a2012-02-20 23:02:36 +0000385 0);
Devang Patel19ba2b42011-02-09 03:15:05 +0000386 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000387 case BuiltinType::UChar:
388 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
389 case BuiltinType::Char_S:
390 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patel98ca8ae2011-09-12 17:11:58 +0000391 case BuiltinType::Char16:
392 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattneraffb3732008-11-10 06:08:34 +0000393 case BuiltinType::UShort:
394 case BuiltinType::UInt:
Devang Patel979aba52011-05-05 17:06:30 +0000395 case BuiltinType::UInt128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000396 case BuiltinType::ULong:
Devang Patel964d7582011-09-10 00:44:49 +0000397 case BuiltinType::WChar_U:
Chris Lattneraffb3732008-11-10 06:08:34 +0000398 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
399 case BuiltinType::Short:
400 case BuiltinType::Int:
Devang Patel979aba52011-05-05 17:06:30 +0000401 case BuiltinType::Int128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000402 case BuiltinType::Long:
Devang Patel964d7582011-09-10 00:44:49 +0000403 case BuiltinType::WChar_S:
Chris Lattneraffb3732008-11-10 06:08:34 +0000404 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
405 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000406 case BuiltinType::Half:
Chris Lattneraffb3732008-11-10 06:08:34 +0000407 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000408 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000409 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000410 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000411
412 switch (BT->getKind()) {
413 case BuiltinType::Long: BTName = "long int"; break;
414 case BuiltinType::LongLong: BTName = "long long int"; break;
415 case BuiltinType::ULong: BTName = "long unsigned int"; break;
416 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
417 default:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000418 BTName = BT->getName(CGM.getContext().getLangOpts());
Devang Patelc7f16ab2010-07-28 23:23:29 +0000419 break;
420 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000421 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000422 uint64_t Size = CGM.getContext().getTypeSize(BT);
423 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patele21912d2009-10-20 19:55:01 +0000424 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000425 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000426 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000427}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000428
Devang Patel4591f772010-12-09 00:25:29 +0000429llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000430 // Bit size, align and offset of the type.
431 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
432 if (Ty->isComplexIntegerType())
433 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000434
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000435 uint64_t Size = CGM.getContext().getTypeSize(Ty);
436 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patele21912d2009-10-20 19:55:01 +0000437 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000438 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel00afcbe2010-12-08 22:42:58 +0000439
Devang Patele21912d2009-10-20 19:55:01 +0000440 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000441}
442
John McCall0cf15512009-09-25 01:40:47 +0000443/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000444/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000445llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000446 QualifierCollector Qc;
447 const Type *T = Qc.strip(Ty);
448
449 // Ignore these qualifiers for now.
450 Qc.removeObjCGCAttr();
451 Qc.removeAddressSpace();
John McCall31168b02011-06-15 23:02:42 +0000452 Qc.removeObjCLifetime();
John McCall0cf15512009-09-25 01:40:47 +0000453
Chris Lattneraffb3732008-11-10 06:08:34 +0000454 // We will create one Derived type for one qualifier and recurse to handle any
455 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000456 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000457 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000458 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000459 Qc.removeConst();
460 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000461 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000462 Qc.removeVolatile();
463 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000464 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000465 Qc.removeRestrict();
466 } else {
467 assert(Qc.empty() && "Unknown type qualifier for debug info");
468 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
John McCall717d9b02010-12-10 11:01:00 +0000471 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCall0cf15512009-09-25 01:40:47 +0000472
Daniel Dunbara290ded2008-10-31 03:54:29 +0000473 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
474 // CVR derived types.
Devang Pateld7185b72011-02-22 18:56:36 +0000475 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel00afcbe2010-12-08 22:42:58 +0000476
Devang Patele21912d2009-10-20 19:55:01 +0000477 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000478}
479
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000480llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000481 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000482 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000483 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
484 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000485 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000486}
487
Chris Lattneraffb3732008-11-10 06:08:34 +0000488llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000489 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000490 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
491 Ty->getPointeeType(), Unit);
492}
493
Eric Christopher000b14e2012-01-25 02:06:59 +0000494// Creates a forward declaration for a RecordDecl in the given context.
495llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
Devang Patel00fca3a2012-02-08 00:10:20 +0000496 llvm::DIDescriptor Ctx) {
Eric Christopher000b14e2012-01-25 02:06:59 +0000497 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
498 unsigned Line = getLineNumber(RD->getLocation());
Eric Christopherfe525232012-02-13 15:08:45 +0000499 StringRef RDName = RD->getName();
500
501 // Get the tag.
Eric Christopher000b14e2012-01-25 02:06:59 +0000502 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Eric Christopherfe525232012-02-13 15:08:45 +0000503 unsigned Tag = 0;
504 if (CXXDecl) {
505 RDName = getClassName(RD);
506 Tag = llvm::dwarf::DW_TAG_class_type;
507 }
Eric Christopher000b14e2012-01-25 02:06:59 +0000508 else if (RD->isStruct())
Eric Christopherfe525232012-02-13 15:08:45 +0000509 Tag = llvm::dwarf::DW_TAG_structure_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000510 else if (RD->isUnion())
Eric Christopherfe525232012-02-13 15:08:45 +0000511 Tag = llvm::dwarf::DW_TAG_union_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000512 else
513 llvm_unreachable("Unknown RecordDecl type!");
Eric Christopherfe525232012-02-13 15:08:45 +0000514
515 // Create the type.
Eric Christopher3cc207b2012-04-23 19:00:24 +0000516 return DBuilder.createForwardDecl(Tag, RDName, Ctx, DefUnit, Line);
Eric Christopher000b14e2012-01-25 02:06:59 +0000517}
518
Eric Christopher45c4d472012-01-20 22:10:15 +0000519// Walk up the context chain and create forward decls for record decls,
520// and normal descriptors for namespaces.
521llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
522 if (!Context)
523 return TheCU;
524
525 // See if we already have the parent.
526 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
527 I = RegionMap.find(Context);
528 if (I != RegionMap.end())
529 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
530
531 // Check namespace.
532 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
533 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
534
535 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
536 if (!RD->isDependentType()) {
Eric Christopher4c006e52012-02-16 22:54:45 +0000537 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
538 getOrCreateMainFile());
Eric Christopher45c4d472012-01-20 22:10:15 +0000539 return llvm::DIDescriptor(Ty);
540 }
541 }
542 return TheCU;
543}
544
Eric Christopher47300ad2011-09-13 23:45:09 +0000545/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patel91bbb552010-09-30 19:05:55 +0000546/// then emit record's fwd if debug info size reduction is enabled.
547llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
548 llvm::DIFile Unit) {
Alexey Samsonov486e1fe2012-04-27 07:24:20 +0000549 if (CGM.getCodeGenOpts().DebugInfo != CodeGenOptions::LimitedDebugInfo)
Devang Patel91bbb552010-09-30 19:05:55 +0000550 return getOrCreateType(PointeeTy, Unit);
Devang Patel242ce912011-10-24 23:15:17 +0000551
552 // Limit debug info for the pointee type.
553
Eric Christophercd888132011-12-16 23:40:18 +0000554 // If we have an existing type, use that, it's still smaller than creating
555 // a new type.
556 llvm::DIType Ty = getTypeOrNull(PointeeTy);
557 if (Ty.Verify()) return Ty;
558
Devang Patel242ce912011-10-24 23:15:17 +0000559 // Handle qualifiers.
560 if (PointeeTy.hasLocalQualifiers())
561 return CreateQualifiedType(PointeeTy, Unit);
562
Devang Patel91bbb552010-09-30 19:05:55 +0000563 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
564 RecordDecl *RD = RTy->getDecl();
Devang Patel91bbb552010-09-30 19:05:55 +0000565 llvm::DIDescriptor FDContext =
John McCall147d0212011-02-22 22:38:33 +0000566 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Eric Christopher66562a42012-02-20 18:05:24 +0000567 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
568 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy;
569 return RetTy;
Devang Patel91bbb552010-09-30 19:05:55 +0000570 }
571 return getOrCreateType(PointeeTy, Unit);
Eric Christopher8a41bd82012-02-13 14:56:11 +0000572
Devang Patel91bbb552010-09-30 19:05:55 +0000573}
574
Anders Carlsson443f6772009-11-06 19:19:55 +0000575llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
576 const Type *Ty,
577 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000578 llvm::DIFile Unit) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000579 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Pateld7185b72011-02-22 18:56:36 +0000580 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel00afcbe2010-12-08 22:42:58 +0000581
Sanjiv Gupta98070572008-05-25 05:15:42 +0000582 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000583 // Size is always the size of a pointer. We can't use getTypeSize here
584 // because that does not return the correct value for references.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000585 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregore8bbc122011-09-02 00:18:52 +0000586 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000587 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000588
Nick Lewycky16790352011-11-10 00:34:02 +0000589 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit),
590 Size, Align);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000591}
592
Mike Stump31f099c2009-05-14 02:03:51 +0000593llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000594 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000595 if (BlockLiteralGenericSet)
596 return BlockLiteralGeneric;
597
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000598 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump31f099c2009-05-14 02:03:51 +0000599 llvm::DIType FieldTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000600 QualType FType;
601 uint64_t FieldSize, FieldOffset;
602 unsigned FieldAlign;
Mike Stump31f099c2009-05-14 02:03:51 +0000603 llvm::DIArray Elements;
604 llvm::DIType EltTy, DescTy;
605
606 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000607 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000608 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
609 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000610
Jay Foaddbf81d82011-04-24 10:11:03 +0000611 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000612 EltTys.clear();
613
Devang Pateldb2732a2010-09-29 21:05:52 +0000614 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000615 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000616
Devang Pateld7185b72011-02-22 18:56:36 +0000617 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel00afcbe2010-12-08 22:42:58 +0000618 Unit, LineNo, FieldOffset, 0,
619 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000620
Mike Stump31f099c2009-05-14 02:03:51 +0000621 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000622 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000623
Devang Pateld7185b72011-02-22 18:56:36 +0000624 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000625
626 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000627 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000628 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000629 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000630 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
631 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000632 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000633 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000634
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000635 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000636 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000637 FieldSize = CGM.getContext().getTypeSize(Ty);
638 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel15013e72011-06-24 22:00:59 +0000639 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000640 LineNo, FieldSize, FieldAlign,
641 FieldOffset, 0, FieldTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000642 EltTys.push_back(FieldTy);
643
644 FieldOffset += FieldSize;
Jay Foaddbf81d82011-04-24 10:11:03 +0000645 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000646
Devang Pateld7185b72011-02-22 18:56:36 +0000647 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel00afcbe2010-12-08 22:42:58 +0000648 Unit, LineNo, FieldOffset, 0,
649 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000650
Mike Stump31f099c2009-05-14 02:03:51 +0000651 BlockLiteralGenericSet = true;
Devang Pateld7185b72011-02-22 18:56:36 +0000652 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000653 return BlockLiteralGeneric;
654}
655
Nick Lewycky16790352011-11-10 00:34:02 +0000656llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000657 // Typedefs are derived from some other type. If we have a typedef of a
658 // typedef, make sure to emit the whole chain.
659 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel00afcbe2010-12-08 22:42:58 +0000660 if (!Src.Verify())
661 return llvm::DIType();
Chris Lattneraffb3732008-11-10 06:08:34 +0000662 // We don't set size information, but do specify where the typedef was
663 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000664 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000665 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher4c006e52012-02-16 22:54:45 +0000666
Nick Lewycky16790352011-11-10 00:34:02 +0000667 llvm::DIDescriptor TypedefContext =
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000668 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher4c006e52012-02-16 22:54:45 +0000669
670 return
Nick Lewycky16790352011-11-10 00:34:02 +0000671 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000672}
673
Chris Lattneraffb3732008-11-10 06:08:34 +0000674llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000675 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000676 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000677
Chris Lattneraffb3732008-11-10 06:08:34 +0000678 // Add the result type at least.
679 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattneraffb3732008-11-10 06:08:34 +0000681 // Set up remainder of arguments if there is a prototype.
682 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patel284fa412010-10-06 20:51:45 +0000683 if (isa<FunctionNoProtoType>(Ty))
Devang Pateld7185b72011-02-22 18:56:36 +0000684 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patel284fa412010-10-06 20:51:45 +0000685 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Eric Christopher8a41bd82012-02-13 14:56:11 +0000686 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
687 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta98070572008-05-25 05:15:42 +0000688 }
689
Jay Foaddbf81d82011-04-24 10:11:03 +0000690 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Eric Christopherde098cd2012-05-16 22:02:36 +0000691 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000692}
693
Eric Christopher8a41bd82012-02-13 14:56:11 +0000694
Eric Christophera15e6352012-01-26 01:57:13 +0000695void CGDebugInfo::
696CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) {
697
698 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
699 I != E; ++I)
700 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
701 if (V->getInit()) {
702 const APValue *Value = V->evaluateValue();
703 if (Value && Value->isInt()) {
704 llvm::ConstantInt *CI
705 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
706
707 // Create the descriptor for static variable.
708 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
709 StringRef VName = V->getName();
710 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
711 // Do not use DIGlobalVariable for enums.
712 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
713 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
714 getLineNumber(V->getLocation()),
715 VTy, true, CI);
716 }
717 }
718 }
719 }
720}
721
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000722llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall147d0212011-02-22 22:38:33 +0000723 QualType type,
Richard Smithcaf33902011-10-10 18:28:20 +0000724 uint64_t sizeInBitsOverride,
John McCall147d0212011-02-22 22:38:33 +0000725 SourceLocation loc,
726 AccessSpecifier AS,
727 uint64_t offsetInBits,
Devang Patel15013e72011-06-24 22:00:59 +0000728 llvm::DIFile tunit,
729 llvm::DIDescriptor scope) {
John McCall147d0212011-02-22 22:38:33 +0000730 llvm::DIType debugType = getOrCreateType(type, tunit);
731
732 // Get the location for the field.
733 llvm::DIFile file = getOrCreateFile(loc);
734 unsigned line = getLineNumber(loc);
735
736 uint64_t sizeInBits = 0;
737 unsigned alignInBits = 0;
738 if (!type->isIncompleteArrayType()) {
739 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
740
Richard Smithcaf33902011-10-10 18:28:20 +0000741 if (sizeInBitsOverride)
742 sizeInBits = sizeInBitsOverride;
John McCall147d0212011-02-22 22:38:33 +0000743 }
744
745 unsigned flags = 0;
746 if (AS == clang::AS_private)
747 flags |= llvm::DIDescriptor::FlagPrivate;
748 else if (AS == clang::AS_protected)
749 flags |= llvm::DIDescriptor::FlagProtected;
750
Devang Patel15013e72011-06-24 22:00:59 +0000751 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
752 alignInBits, offsetInBits, flags, debugType);
John McCall147d0212011-02-22 22:38:33 +0000753}
754
Devang Patel889ce762010-01-19 00:00:59 +0000755/// CollectRecordFields - A helper function to collect debug info for
756/// record fields. This is used while creating debug info entry for a Record.
757void CGDebugInfo::
John McCall147d0212011-02-22 22:38:33 +0000758CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000759 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel15013e72011-06-24 22:00:59 +0000760 llvm::DIType RecordTy) {
John McCall147d0212011-02-22 22:38:33 +0000761 unsigned fieldNo = 0;
762 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Eric Christopher4b6753c2012-03-01 21:36:52 +0000763 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
764
765 // For C++11 Lambdas a Fields will be the same as a Capture, but the Capture
766 // has the name and the location of the variable so we should iterate over
767 // both concurrently.
768 if (CXXDecl && CXXDecl->isLambda()) {
769 RecordDecl::field_iterator Field = CXXDecl->field_begin();
770 unsigned fieldno = 0;
771 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
772 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
773 const LambdaExpr::Capture C = *I;
774 // TODO: Need to handle 'this' in some way by probably renaming the
775 // this of the lambda class and having a field member of 'this'.
776 if (C.capturesVariable()) {
777 VarDecl *V = C.getCapturedVar();
778 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
779 StringRef VName = V->getName();
780 uint64_t SizeInBitsOverride = 0;
781 if (Field->isBitField()) {
782 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
783 assert(SizeInBitsOverride && "found named 0-width bitfield");
784 }
785 llvm::DIType fieldType
786 = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
787 Field->getAccess(), layout.getFieldOffset(fieldno),
788 VUnit, RecordTy);
789 elements.push_back(fieldType);
790 }
791 }
792 } else {
793 bool IsMsStruct = record->hasAttr<MsStructAttr>();
794 const FieldDecl *LastFD = 0;
795 for (RecordDecl::field_iterator I = record->field_begin(),
796 E = record->field_end();
797 I != E; ++I, ++fieldNo) {
David Blaikie2d7c57e2012-04-30 02:36:29 +0000798 FieldDecl *field = &*I;
Eric Christopher4b6753c2012-03-01 21:36:52 +0000799
800 if (IsMsStruct) {
801 // Zero-length bitfields following non-bitfield members are ignored
802 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
803 --fieldNo;
804 continue;
805 }
806 LastFD = field;
807 }
808
809 StringRef name = field->getName();
810 QualType type = field->getType();
811
812 // Ignore unnamed fields unless they're anonymous structs/unions.
813 if (name.empty() && !type->isRecordType()) {
814 LastFD = field;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000815 continue;
816 }
Eric Christopher4b6753c2012-03-01 21:36:52 +0000817
818 uint64_t SizeInBitsOverride = 0;
819 if (field->isBitField()) {
820 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
821 assert(SizeInBitsOverride && "found named 0-width bitfield");
822 }
823
824 llvm::DIType fieldType
825 = createFieldType(name, type, SizeInBitsOverride,
826 field->getLocation(), field->getAccess(),
827 layout.getFieldOffset(fieldNo), tunit, RecordTy);
828
829 elements.push_back(fieldType);
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000830 }
Devang Patel889ce762010-01-19 00:00:59 +0000831 }
832}
833
Devang Patel3d4e6d92010-01-28 00:28:01 +0000834/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
835/// function type is not updated to include implicit "this" pointer. Use this
836/// routine to get a method type which includes "this" pointer.
837llvm::DIType
838CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000839 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000840 llvm::DIType FnTy
841 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
842 0),
843 Unit);
Eric Christopher947bb5a2012-03-13 23:40:48 +0000844
Devang Patel3d4e6d92010-01-28 00:28:01 +0000845 // Add "this" pointer.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000846 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000847 assert (Args.getNumElements() && "Invalid number of arguments!");
848
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000849 SmallVector<llvm::Value *, 16> Elts;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000850
851 // First element is always return type. For 'void' functions it is NULL.
852 Elts.push_back(Args.getElement(0));
853
Eric Christopher19329c42011-09-14 01:10:50 +0000854 if (!Method->isStatic()) {
855 // "this" pointer is always first argument.
856 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patelfa59ac32011-10-28 21:12:13 +0000857
858 const CXXRecordDecl *RD = Method->getParent();
859 if (isa<ClassTemplateSpecializationDecl>(RD)) {
860 // Create pointer type directly in this case.
861 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
862 QualType PointeeTy = ThisPtrTy->getPointeeType();
863 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
864 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
865 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Nick Lewycky2219ef02011-11-09 04:25:21 +0000866 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher39e39c82012-02-09 07:26:21 +0000867 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align);
Devang Patelfa59ac32011-10-28 21:12:13 +0000868 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000869 // TODO: This and the artificial type below are misleading, the
870 // types aren't artificial the argument is, but the current
871 // metadata doesn't represent that.
872 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000873 Elts.push_back(ThisPtrType);
874 } else {
Eric Christopher39e39c82012-02-09 07:26:21 +0000875 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Devang Patelfa59ac32011-10-28 21:12:13 +0000876 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000877 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000878 Elts.push_back(ThisPtrType);
879 }
Eric Christopher19329c42011-09-14 01:10:50 +0000880 }
Devang Patel3d4e6d92010-01-28 00:28:01 +0000881
882 // Copy rest of the arguments.
883 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
884 Elts.push_back(Args.getElement(i));
885
Jay Foaddbf81d82011-04-24 10:11:03 +0000886 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000887
Devang Pateld7185b72011-02-22 18:56:36 +0000888 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000889}
890
Devang Patelf79199d2010-10-22 17:11:50 +0000891/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
892/// inside a function.
893static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
Nick Lewycky16790352011-11-10 00:34:02 +0000894 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000895 return isFunctionLocalClass(NRD);
Nick Lewycky16790352011-11-10 00:34:02 +0000896 if (isa<FunctionDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000897 return true;
898 return false;
Devang Patelf79199d2010-10-22 17:11:50 +0000899}
Nick Lewycky2219ef02011-11-09 04:25:21 +0000900
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000901/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
902/// a single member function GlobalDecl.
903llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000904CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000905 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000906 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000907 bool IsCtorOrDtor =
908 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
909
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000910 StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000911 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000912
913 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
914 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000915 StringRef MethodLinkageName;
Devang Patelf79199d2010-10-22 17:11:50 +0000916 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlssonea836bc2010-06-22 16:16:50 +0000917 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000918
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000919 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000920 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
921 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000922
923 // Collect virtual method info.
924 llvm::DIType ContainingType;
925 unsigned Virtuality = 0;
926 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000927
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000928 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000929 if (Method->isPure())
930 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
931 else
932 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
933
934 // It doesn't make sense to give a virtual destructor a vtable index,
935 // since a single destructor has two entries in the vtable.
936 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbournea8341662011-09-26 01:56:30 +0000937 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000938 ContainingType = RecordTy;
939 }
940
Devang Pateldb2732a2010-09-29 21:05:52 +0000941 unsigned Flags = 0;
942 if (Method->isImplicit())
943 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel330b65e2010-09-29 21:46:16 +0000944 AccessSpecifier Access = Method->getAccess();
945 if (Access == clang::AS_private)
946 Flags |= llvm::DIDescriptor::FlagPrivate;
947 else if (Access == clang::AS_protected)
948 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000949 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
950 if (CXXC->isExplicit())
951 Flags |= llvm::DIDescriptor::FlagExplicit;
952 } else if (const CXXConversionDecl *CXXC =
953 dyn_cast<CXXConversionDecl>(Method)) {
954 if (CXXC->isExplicit())
955 Flags |= llvm::DIDescriptor::FlagExplicit;
956 }
Devang Patel251f8592010-10-07 22:03:49 +0000957 if (Method->hasPrototype())
958 Flags |= llvm::DIDescriptor::FlagPrototyped;
Eric Christopher947bb5a2012-03-13 23:40:48 +0000959
960 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000961 llvm::DISubprogram SP =
Nick Lewycky0112b112011-09-01 21:49:51 +0000962 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +0000963 MethodDefUnit, MethodLine,
964 MethodTy, /*isLocalToUnit=*/false,
965 /* isDefinition=*/ false,
966 Virtuality, VIndex, ContainingType,
Eric Christopher947bb5a2012-03-13 23:40:48 +0000967 Flags, CGM.getLangOpts().Optimize, NULL,
968 TParamsArray);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000969
Eric Christopher459532e2011-11-17 23:45:00 +0000970 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000971
972 return SP;
973}
974
Devang Patel7a12ad02010-01-19 01:54:44 +0000975/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherffbc4d02012-01-12 01:26:51 +0000976/// C++ member functions. This is used while creating debug info entry for
Devang Patel7a12ad02010-01-19 01:54:44 +0000977/// a Record.
978void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000979CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000980 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000981 llvm::DIType RecordTy) {
Eric Christopher947bb5a2012-03-13 23:40:48 +0000982
983 // Since we want more than just the individual member decls if we
984 // have templated functions iterate over every declaration to gather
985 // the functions.
986 for(DeclContext::decl_iterator I = RD->decls_begin(),
987 E = RD->decls_end(); I != E; ++I) {
988 Decl *D = *I;
989 if (D->isImplicit() && !D->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000990 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000991
Eric Christopher947bb5a2012-03-13 23:40:48 +0000992 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
993 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
994 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
995 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(),
996 SE = FTD->spec_end(); SI != SE; ++SI) {
997 FunctionDecl *FD = *SI;
998 if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(FD))
999 EltTys.push_back(CreateCXXMemberFunction(M, Unit, RecordTy));
1000 }
Devang Patel7a12ad02010-01-19 01:54:44 +00001001 }
1002}
1003
Devang Patel96b7f552010-08-27 17:47:47 +00001004/// CollectCXXFriends - A helper function to collect debug info for
1005/// C++ base classes. This is used while creating debug info entry for
1006/// a Record.
1007void CGDebugInfo::
1008CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001009 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel96b7f552010-08-27 17:47:47 +00001010 llvm::DIType RecordTy) {
Eric Christopher8c363622012-01-12 01:26:58 +00001011 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
Devang Patel96b7f552010-08-27 17:47:47 +00001012 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky0112b112011-09-01 21:49:51 +00001013 if ((*BI)->isUnsupportedFriend())
1014 continue;
Devang Patel00afcbe2010-12-08 22:42:58 +00001015 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Pateld7185b72011-02-22 18:56:36 +00001016 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +00001017 getOrCreateType(TInfo->getType(),
1018 Unit)));
Devang Patel96b7f552010-08-27 17:47:47 +00001019 }
1020}
1021
Devang Patelc54353d2010-01-25 23:32:18 +00001022/// CollectCXXBases - A helper function to collect debug info for
1023/// C++ base classes. This is used while creating debug info entry for
1024/// a Record.
1025void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +00001026CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001027 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +00001028 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +00001029
Devang Patel1c0954c2010-02-01 21:39:52 +00001030 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1031 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1032 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +00001033 unsigned BFlags = 0;
Devang Patel84852bb2011-04-04 20:36:06 +00001034 uint64_t BaseOffset;
Devang Patel128aa9d2010-01-28 21:54:15 +00001035
1036 const CXXRecordDecl *Base =
1037 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
1038
1039 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +00001040 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +00001041 // expression where it expects +ve number.
Ken Dyckbb4e9772011-04-07 12:37:09 +00001042 BaseOffset =
Peter Collingbournea8341662011-09-26 01:56:30 +00001043 0 - CGM.getVTableContext()
1044 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Pateldb2732a2010-09-29 21:05:52 +00001045 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patel128aa9d2010-01-28 21:54:15 +00001046 } else
Devang Patel84852bb2011-04-04 20:36:06 +00001047 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Ken Dyckbb4e9772011-04-07 12:37:09 +00001048 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1049 // BI->isVirtual() and bits when not.
Devang Patel128aa9d2010-01-28 21:54:15 +00001050
1051 AccessSpecifier Access = BI->getAccessSpecifier();
1052 if (Access == clang::AS_private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001053 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel128aa9d2010-01-28 21:54:15 +00001054 else if (Access == clang::AS_protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001055 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patel128aa9d2010-01-28 21:54:15 +00001056
Devang Patel00afcbe2010-12-08 22:42:58 +00001057 llvm::DIType DTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001058 DBuilder.createInheritance(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +00001059 getOrCreateType(BI->getType(), Unit),
Devang Patel84852bb2011-04-04 20:36:06 +00001060 BaseOffset, BFlags);
Devang Patel128aa9d2010-01-28 21:54:15 +00001061 EltTys.push_back(DTy);
1062 }
Devang Patelc54353d2010-01-25 23:32:18 +00001063}
1064
Devang Patelb87c4282011-04-05 22:54:11 +00001065/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel7522abd2011-04-05 17:30:54 +00001066llvm::DIArray CGDebugInfo::
Devang Patelb87c4282011-04-05 22:54:11 +00001067CollectTemplateParams(const TemplateParameterList *TPList,
1068 const TemplateArgumentList &TAList,
1069 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001070 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel98d26c92011-04-05 20:15:06 +00001071 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1072 const TemplateArgument &TA = TAList[i];
Devang Patelb87c4282011-04-05 22:54:11 +00001073 const NamedDecl *ND = TPList->getParam(i);
Devang Patel7522abd2011-04-05 17:30:54 +00001074 if (TA.getKind() == TemplateArgument::Type) {
1075 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1076 llvm::DITemplateTypeParameter TTP =
Devang Patel98d26c92011-04-05 20:15:06 +00001077 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel7522abd2011-04-05 17:30:54 +00001078 TemplateParams.push_back(TTP);
1079 } else if (TA.getKind() == TemplateArgument::Integral) {
1080 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel7522abd2011-04-05 17:30:54 +00001081 llvm::DITemplateValueParameter TVP =
Devang Patel98d26c92011-04-05 20:15:06 +00001082 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
1083 TA.getAsIntegral()->getZExtValue());
Devang Patel7522abd2011-04-05 17:30:54 +00001084 TemplateParams.push_back(TVP);
1085 }
1086 }
Jay Foaddbf81d82011-04-24 10:11:03 +00001087 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel7522abd2011-04-05 17:30:54 +00001088}
1089
Devang Patelb87c4282011-04-05 22:54:11 +00001090/// CollectFunctionTemplateParams - A helper function to collect debug
1091/// info for function template parameters.
1092llvm::DIArray CGDebugInfo::
1093CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
Eric Christopherfefafac2011-10-11 23:00:51 +00001094 if (FD->getTemplatedKind() ==
1095 FunctionDecl::TK_FunctionTemplateSpecialization) {
Devang Patelb87c4282011-04-05 22:54:11 +00001096 const TemplateParameterList *TList =
Eric Christopherfefafac2011-10-11 23:00:51 +00001097 FD->getTemplateSpecializationInfo()->getTemplate()
1098 ->getTemplateParameters();
Devang Patelb87c4282011-04-05 22:54:11 +00001099 return
1100 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
1101 }
1102 return llvm::DIArray();
1103}
1104
1105/// CollectCXXTemplateParams - A helper function to collect debug info for
1106/// template parameters.
1107llvm::DIArray CGDebugInfo::
1108CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1109 llvm::DIFile Unit) {
1110 llvm::PointerUnion<ClassTemplateDecl *,
1111 ClassTemplatePartialSpecializationDecl *>
1112 PU = TSpecial->getSpecializedTemplateOrPartial();
1113
1114 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1115 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1116 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1117 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
1118 return CollectTemplateParams(TPList, TAList, Unit);
1119}
1120
Devang Patel84033fb2010-01-28 18:11:52 +00001121/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +00001122llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001123 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +00001124 return VTablePtrType;
1125
1126 ASTContext &Context = CGM.getContext();
1127
1128 /* Function type */
Devang Patel00afcbe2010-12-08 22:42:58 +00001129 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foaddbf81d82011-04-24 10:11:03 +00001130 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Pateld7185b72011-02-22 18:56:36 +00001131 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel84033fb2010-01-28 18:11:52 +00001132 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Pateld7185b72011-02-22 18:56:36 +00001133 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001134 "__vtbl_ptr_type");
Devang Pateld7185b72011-02-22 18:56:36 +00001135 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel84033fb2010-01-28 18:11:52 +00001136 return VTablePtrType;
1137}
1138
Anders Carlsson11e51402010-04-17 20:15:18 +00001139/// getVTableName - Get vtable name for the given Class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001140StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Eric Christopherbfecca32012-01-25 21:47:09 +00001141 // Construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +00001142 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +00001143
1144 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +00001145 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001146 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001147 return StringRef(StrPtr, Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001148}
1149
1150
Anders Carlsson11e51402010-04-17 20:15:18 +00001151/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +00001152/// debug info entry in EltTys vector.
1153void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +00001154CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001155 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +00001156 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +00001157
1158 // If there is a primary base then it will hold vtable info.
1159 if (RL.getPrimaryBase())
1160 return;
1161
1162 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +00001163 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +00001164 return;
1165
1166 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1167 llvm::DIType VPTR
Devang Patel15013e72011-06-24 22:00:59 +00001168 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00001169 0, Size, 0, 0, 0,
1170 getOrCreateVTablePtrType(Unit));
Devang Patel84033fb2010-01-28 18:11:52 +00001171 EltTys.push_back(VPTR);
1172}
1173
Devang Patel91bbb552010-09-30 19:05:55 +00001174/// getOrCreateRecordType - Emit record type's standalone debug info.
1175llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
1176 SourceLocation Loc) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00001177 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001178 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel91bbb552010-09-30 19:05:55 +00001179 return T;
1180}
1181
Eric Christopher3d19de92012-04-11 05:56:05 +00001182/// getOrCreateInterfaceType - Emit an objective c interface type standalone
1183/// debug info.
1184llvm::DIType CGDebugInfo::getOrCreateInterfaceType(QualType D,
1185 SourceLocation Loc) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00001186 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopher3d19de92012-04-11 05:56:05 +00001187 llvm::DIType T = getOrCreateType(D, getOrCreateFile(Loc));
1188 DBuilder.retainType(T);
1189 return T;
1190}
1191
Devang Patel410dc002009-02-25 01:36:11 +00001192/// CreateType - get structure or union type.
Devang Patel283e89d2011-01-17 22:23:07 +00001193llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Patel3efd1472010-02-01 21:52:22 +00001194 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattneraffb3732008-11-10 06:08:34 +00001196 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001197 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattneraffb3732008-11-10 06:08:34 +00001199 // Records and classes and unions can all be recursive. To handle them, we
1200 // first generate a debug descriptor for the struct as a forward declaration.
1201 // Then (if it is a definition) we go through and get debug info for all of
1202 // its members. Finally, we create a descriptor for the complete type (which
1203 // may refer to the forward decl if the struct is recursive) and replace all
1204 // uses of the forward declaration with the final definition.
Eric Christopher45c4d472012-01-20 22:10:15 +00001205
Eric Christopher4c006e52012-02-16 22:54:45 +00001206 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit);
Devang Patel8f3f76f2010-07-08 19:56:29 +00001207
Eric Christopher4c006e52012-02-16 22:54:45 +00001208 if (FwdDecl.isForwardDecl())
1209 return FwdDecl;
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001210
1211 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(FwdDecl);
1212
Devang Patel01bb5ce2010-03-11 20:01:48 +00001213 // Push the struct on region stack.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001214 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001215 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001216
Eric Christopher4c006e52012-02-16 22:54:45 +00001217 // Add this to the completed types cache since we're completing it.
1218 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1219
Chris Lattneraffb3732008-11-10 06:08:34 +00001220 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001221 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattneraffb3732008-11-10 06:08:34 +00001222
Eric Christopher8913bd62012-01-26 07:01:04 +00001223 // Note: The split of CXXDecl information here is intentional, the
1224 // gdb tests will depend on a certain ordering at printout. The debug
1225 // information offsets are still correct if we merge them all together
1226 // though.
Devang Patel3efd1472010-02-01 21:52:22 +00001227 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +00001228 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001229 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1230 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
Eric Christopher8913bd62012-01-26 07:01:04 +00001231 }
1232
1233 // Collect static variables with initializers and other fields.
1234 CollectRecordStaticVars(RD, FwdDecl);
1235 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1236 llvm::DIArray TParamsArray;
1237 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001238 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1239 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Devang Patel7522abd2011-04-05 17:30:54 +00001240 if (const ClassTemplateSpecializationDecl *TSpecial
1241 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Eric Christopher57200332012-01-26 06:20:57 +00001242 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit);
Devang Patel00afcbe2010-12-08 22:42:58 +00001243 }
Devang Patelabb44132010-01-28 00:54:21 +00001244
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001245 LexicalBlockStack.pop_back();
Benjamin Kramere894e092012-03-24 18:22:12 +00001246 RegionMap.erase(Ty->getDecl());
Devang Patel00afcbe2010-12-08 22:42:58 +00001247
Jay Foaddbf81d82011-04-24 10:11:03 +00001248 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher4c006e52012-02-16 22:54:45 +00001249 // FIXME: Magic numbers ahoy! These should be changed when we
1250 // get some enums in llvm/Analysis/DebugInfo.h to refer to
1251 // them.
Eric Christopher9a897052012-02-15 23:51:20 +00001252 if (RD->isUnion())
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001253 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001254 else if (CXXDecl) {
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001255 FwdDeclNode->replaceOperandWith(10, Elements);
1256 FwdDeclNode->replaceOperandWith(13, TParamsArray);
Eric Christopher9a897052012-02-15 23:51:20 +00001257 } else
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001258 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001259
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001260 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDeclNode);
1261 return llvm::DIType(FwdDeclNode);
Chris Lattneraffb3732008-11-10 06:08:34 +00001262}
1263
John McCall8b07ec22010-05-15 11:32:37 +00001264/// CreateType - get objective-c object type.
1265llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1266 llvm::DIFile Unit) {
1267 // Ignore protocols.
1268 return getOrCreateType(Ty->getBaseType(), Unit);
1269}
1270
Devang Patelf4c205b2009-02-26 21:10:26 +00001271/// CreateType - get objective-c interface type.
1272llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001273 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +00001274 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001275 if (!ID)
1276 return llvm::DIType();
Devang Patelf4c205b2009-02-26 21:10:26 +00001277
1278 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +00001279 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001280 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +00001281 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +00001282
Eric Christopherfab289a2011-10-06 00:31:18 +00001283 // If this is just a forward declaration return a special forward-declaration
1284 // debug type since we won't be able to lay out the entire type.
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001285 ObjCInterfaceDecl *Def = ID->getDefinition();
1286 if (!Def) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001287 llvm::DIType FwdDecl =
Eric Christophere908a7a2012-02-20 18:05:04 +00001288 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher3cc207b2012-04-23 19:00:24 +00001289 ID->getName(), TheCU, DefUnit, Line,
Eric Christophere908a7a2012-02-20 18:05:04 +00001290 RuntimeLang);
Dan Gohman66427b12010-08-23 21:15:56 +00001291 return FwdDecl;
1292 }
Eric Christopher3d19de92012-04-11 05:56:05 +00001293
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001294 ID = Def;
Dan Gohman66427b12010-08-23 21:15:56 +00001295
Eric Christopher4c006e52012-02-16 22:54:45 +00001296 // Bit size, align and offset of the type.
1297 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1298 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001299
Eric Christopher4c006e52012-02-16 22:54:45 +00001300 unsigned Flags = 0;
1301 if (ID->getImplementation())
1302 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1303
1304 llvm::DIType RealDecl =
1305 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1306 Line, Size, Align, Flags,
1307 llvm::DIArray(), RuntimeLang);
Eric Christopher3d19de92012-04-11 05:56:05 +00001308
Eric Christopher7a5fdd82012-02-27 08:23:23 +00001309 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1310 // will find it and we're emitting the complete type.
1311 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001312 // Push the struct on region stack.
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001313 llvm::TrackingVH<llvm::MDNode> FwdDeclNode(RealDecl);
Eric Christopher4c006e52012-02-16 22:54:45 +00001314
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001315 LexicalBlockStack.push_back(FwdDeclNode);
Eric Christopher4c006e52012-02-16 22:54:45 +00001316 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001317
1318 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001319 SmallVector<llvm::Value *, 16> EltTys;
Devang Patelf4c205b2009-02-26 21:10:26 +00001320
Devang Patel3efd1472010-02-01 21:52:22 +00001321 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001322 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001323 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001324 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001325 if (!SClassTy.isValid())
1326 return llvm::DIType();
1327
Mike Stump11289f42009-09-09 15:08:12 +00001328 llvm::DIType InhTag =
Eric Christopher4c006e52012-02-16 22:54:45 +00001329 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Devang Patelc0f58ea2009-03-10 21:30:26 +00001330 EltTys.push_back(InhTag);
1331 }
1332
Devang Patel37a5c952012-02-07 18:40:30 +00001333 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1334 E = ID->prop_end(); I != E; ++I) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00001335 const ObjCPropertyDecl *PD = &*I;
Eric Christophereb031692012-03-29 08:43:37 +00001336 SourceLocation Loc = PD->getLocation();
1337 llvm::DIFile PUnit = getOrCreateFile(Loc);
1338 unsigned PLine = getLineNumber(Loc);
Eric Christopherf3dd7132012-04-05 22:03:32 +00001339 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1340 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
Devang Patel37a5c952012-02-07 18:40:30 +00001341 llvm::MDNode *PropertyNode =
1342 DBuilder.createObjCProperty(PD->getName(),
Eric Christophereb031692012-03-29 08:43:37 +00001343 PUnit, PLine,
Eric Christopherf3dd7132012-04-05 22:03:32 +00001344 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherbd9c9102012-03-29 17:31:33 +00001345 getSelectorName(PD->getGetterName()),
Eric Christopherf3dd7132012-04-05 22:03:32 +00001346 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherbd9c9102012-03-29 17:31:33 +00001347 getSelectorName(PD->getSetterName()),
Eric Christophereb031692012-03-29 08:43:37 +00001348 PD->getPropertyAttributes(),
1349 getOrCreateType(PD->getType(), PUnit));
Devang Patel37a5c952012-02-07 18:40:30 +00001350 EltTys.push_back(PropertyNode);
1351 }
1352
Devang Patel3efd1472010-02-01 21:52:22 +00001353 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001354 unsigned FieldNo = 0;
Fariborz Jahanian885e9df2010-10-01 00:01:53 +00001355 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanian77890872010-10-11 23:55:47 +00001356 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001357 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001358 if (!FieldTy.isValid())
1359 return llvm::DIType();
1360
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001361 StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001362
Devang Pateldf348f12009-04-27 22:40:36 +00001363 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001364 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001365 continue;
1366
Devang Patelf4c205b2009-02-26 21:10:26 +00001367 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001368 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1369 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001370 QualType FType = Field->getType();
1371 uint64_t FieldSize = 0;
1372 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001373
Devang Patel9f804932009-03-20 18:24:39 +00001374 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001375
Devang Patel9f804932009-03-20 18:24:39 +00001376 // Bit size, align and offset of the type.
Richard Smithcaf33902011-10-10 18:28:20 +00001377 FieldSize = Field->isBitField()
1378 ? Field->getBitWidthValue(CGM.getContext())
1379 : CGM.getContext().getTypeSize(FType);
1380 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001381 }
1382
Eric Christopherfab289a2011-10-06 00:31:18 +00001383 // We can't know the offset of our ivar in the structure if we're using
1384 // the non-fragile abi and the debugger should ignore the value anyways.
1385 // Call it the FieldNo+1 due to how debuggers use the information,
1386 // e.g. negating the value when it needs a lookup in the dynamic table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001387 uint64_t FieldOffset = CGM.getLangOpts().ObjCNonFragileABI ? FieldNo+1
Eric Christopherfab289a2011-10-06 00:31:18 +00001388 : RL.getFieldOffset(FieldNo);
Mike Stump11289f42009-09-09 15:08:12 +00001389
Devang Patelec4bad52009-03-19 00:23:53 +00001390 unsigned Flags = 0;
1391 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001392 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelec4bad52009-03-19 00:23:53 +00001393 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001394 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001395
Devang Patela21bbb22012-02-04 01:15:04 +00001396 llvm::MDNode *PropertyNode = NULL;
Devang Patel37a5c952012-02-07 18:40:30 +00001397 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Devang Patel5a540652011-09-19 18:54:16 +00001398 if (ObjCPropertyImplDecl *PImpD =
Devang Patel37a5c952012-02-07 18:40:30 +00001399 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1400 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Eric Christophereb031692012-03-29 08:43:37 +00001401 SourceLocation Loc = PD->getLocation();
1402 llvm::DIFile PUnit = getOrCreateFile(Loc);
1403 unsigned PLine = getLineNumber(Loc);
Eric Christopherf3dd7132012-04-05 22:03:32 +00001404 ObjCMethodDecl *Getter = PD->getGetterMethodDecl();
1405 ObjCMethodDecl *Setter = PD->getSetterMethodDecl();
1406 PropertyNode =
1407 DBuilder.createObjCProperty(PD->getName(),
1408 PUnit, PLine,
1409 (Getter && Getter->isImplicit()) ? "" :
Eric Christopherbd9c9102012-03-29 17:31:33 +00001410 getSelectorName(PD->getGetterName()),
Eric Christopherf3dd7132012-04-05 22:03:32 +00001411 (Setter && Setter->isImplicit()) ? "" :
Eric Christopherbd9c9102012-03-29 17:31:33 +00001412 getSelectorName(PD->getSetterName()),
Eric Christopherf3dd7132012-04-05 22:03:32 +00001413 PD->getPropertyAttributes(),
1414 getOrCreateType(PD->getType(), PUnit));
Devang Patel00fca3a2012-02-08 00:10:20 +00001415 }
Devang Patel37a5c952012-02-07 18:40:30 +00001416 }
Devang Patela21bbb22012-02-04 01:15:04 +00001417 }
Devang Patel9d6c8572011-04-16 00:12:55 +00001418 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1419 FieldLine, FieldSize, FieldAlign,
1420 FieldOffset, Flags, FieldTy,
Devang Patel60fc2422012-02-06 18:20:02 +00001421 PropertyNode);
Devang Patelf4c205b2009-02-26 21:10:26 +00001422 EltTys.push_back(FieldTy);
1423 }
Mike Stump11289f42009-09-09 15:08:12 +00001424
Jay Foaddbf81d82011-04-24 10:11:03 +00001425 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001426 FwdDeclNode->replaceOperandWith(10, Elements);
Eric Christopher4c006e52012-02-16 22:54:45 +00001427
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001428 LexicalBlockStack.pop_back();
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001429 return llvm::DIType(FwdDeclNode);
Devang Patelf4c205b2009-02-26 21:10:26 +00001430}
1431
Nick Lewycky2219ef02011-11-09 04:25:21 +00001432llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001433 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel0b37e792011-04-08 21:56:52 +00001434 int64_t NumElems = Ty->getNumElements();
1435 int64_t LowerBound = 0;
1436 if (NumElems == 0)
1437 // If number of elements are not known then this is an unbounded array.
1438 // Use Low = 1, Hi = 0 to express such arrays.
1439 LowerBound = 1;
1440 else
Devang Patelb4073382010-02-23 22:59:39 +00001441 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001442
Devang Patel0b37e792011-04-08 21:56:52 +00001443 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foaddbf81d82011-04-24 10:11:03 +00001444 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patelb4073382010-02-23 22:59:39 +00001445
1446 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1447 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1448
1449 return
Devang Pateld7185b72011-02-22 18:56:36 +00001450 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001451}
1452
Chris Lattneraffb3732008-11-10 06:08:34 +00001453llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001454 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001455 uint64_t Size;
1456 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001457
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001458 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001459 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001460 Size = 0;
1461 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001462 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001463 } else if (Ty->isIncompleteArrayType()) {
1464 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001465 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patela540f142011-04-04 23:18:38 +00001466 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patel1ffe2342011-04-01 19:02:33 +00001467 Size = 0;
1468 Align = 0;
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001469 } else {
1470 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001471 Size = CGM.getContext().getTypeSize(Ty);
1472 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001473 }
Mike Stump11289f42009-09-09 15:08:12 +00001474
Chris Lattneraffb3732008-11-10 06:08:34 +00001475 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1476 // interior arrays, do we care? Why aren't nested arrays represented the
1477 // obvious/recursive way?
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001478 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattneraffb3732008-11-10 06:08:34 +00001479 QualType EltTy(Ty, 0);
Devang Patelc0601d12010-10-06 18:30:00 +00001480 if (Ty->isIncompleteArrayType())
Chris Lattneraffb3732008-11-10 06:08:34 +00001481 EltTy = Ty->getElementType();
Devang Patelc0601d12010-10-06 18:30:00 +00001482 else {
1483 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Devang Patel0b37e792011-04-08 21:56:52 +00001484 int64_t UpperBound = 0;
1485 int64_t LowerBound = 0;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001486 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Devang Patelc0601d12010-10-06 18:30:00 +00001487 if (CAT->getSize().getZExtValue())
Devang Patel0b37e792011-04-08 21:56:52 +00001488 UpperBound = CAT->getSize().getZExtValue() - 1;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001489 } else
Devang Patel0b37e792011-04-08 21:56:52 +00001490 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1491 // arrays.
1492 LowerBound = 1;
1493
Devang Patelc0601d12010-10-06 18:30:00 +00001494 // FIXME: Verify this is right for VLAs.
Eric Christopherfefafac2011-10-11 23:00:51 +00001495 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
1496 UpperBound));
Devang Patelc0601d12010-10-06 18:30:00 +00001497 EltTy = Ty->getElementType();
1498 }
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001499 }
Mike Stump11289f42009-09-09 15:08:12 +00001500
Jay Foaddbf81d82011-04-24 10:11:03 +00001501 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattneraffb3732008-11-10 06:08:34 +00001502
Devang Patele21912d2009-10-20 19:55:01 +00001503 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001504 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001505 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001506 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001507}
1508
Anders Carlsson443f6772009-11-06 19:19:55 +00001509llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001510 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001511 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1512 Ty, Ty->getPointeeType(), Unit);
1513}
Chris Lattneraffb3732008-11-10 06:08:34 +00001514
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001515llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1516 llvm::DIFile Unit) {
1517 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1518 Ty, Ty->getPointeeType(), Unit);
1519}
1520
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001521llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001522 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001523 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1524 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1525
1526 if (!Ty->getPointeeType()->isFunctionType()) {
1527 // We have a data member pointer type.
1528 return PointerDiffDITy;
1529 }
1530
1531 // We have a member function pointer type. Treat it as a struct with two
1532 // ptrdiff_t members.
1533 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1534
1535 uint64_t FieldOffset = 0;
Devang Patel00afcbe2010-12-08 22:42:58 +00001536 llvm::Value *ElementTypes[2];
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001537
1538 // FIXME: This should probably be a function type instead.
1539 ElementTypes[0] =
Devang Patel15013e72011-06-24 22:00:59 +00001540 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001541 Info.first, Info.second, FieldOffset, 0,
1542 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001543 FieldOffset += Info.first;
1544
1545 ElementTypes[1] =
Devang Patel15013e72011-06-24 22:00:59 +00001546 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001547 Info.first, Info.second, FieldOffset, 0,
1548 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001549
Jay Foaddbf81d82011-04-24 10:11:03 +00001550 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001551
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001552 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel00afcbe2010-12-08 22:42:58 +00001553 U, 0, FieldOffset,
1554 0, 0, Elements);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001555}
1556
Eli Friedman0dfb8892011-10-06 23:00:33 +00001557llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1558 llvm::DIFile U) {
1559 // Ignore the atomic wrapping
1560 // FIXME: What is the correct representation?
1561 return getOrCreateType(Ty->getValueType(), U);
1562}
1563
Devang Patel41c20972010-08-23 22:07:25 +00001564/// CreateEnumType - get enumeration type.
Devang Patel283e89d2011-01-17 22:23:07 +00001565llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1566 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001567 SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel41c20972010-08-23 22:07:25 +00001568
1569 // Create DIEnumerator elements for each enumerator.
1570 for (EnumDecl::enumerator_iterator
1571 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1572 Enum != EnumEnd; ++Enum) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001573 Enumerators.push_back(
Devang Pateld7185b72011-02-22 18:56:36 +00001574 DBuilder.createEnumerator(Enum->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001575 Enum->getInitVal().getZExtValue()));
Devang Patel41c20972010-08-23 22:07:25 +00001576 }
1577
1578 // Return a CompositeType for the enum itself.
Jay Foaddbf81d82011-04-24 10:11:03 +00001579 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel41c20972010-08-23 22:07:25 +00001580
1581 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1582 unsigned Line = getLineNumber(ED->getLocation());
1583 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001584 uint64_t Align = 0;
1585 if (!ED->getTypeForDecl()->isIncompleteType()) {
1586 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1587 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1588 }
Devang Patel1bee63f2010-10-27 23:23:58 +00001589 llvm::DIDescriptor EnumContext =
John McCall147d0212011-02-22 22:38:33 +00001590 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel41c20972010-08-23 22:07:25 +00001591 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001592 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel00afcbe2010-12-08 22:42:58 +00001593 Size, Align, EltArray);
Devang Patel41c20972010-08-23 22:07:25 +00001594 return DbgTy;
1595}
1596
Douglas Gregor0f139a12009-12-21 20:18:30 +00001597static QualType UnwrapTypeForDebugInfo(QualType T) {
1598 do {
1599 QualType LastT = T;
1600 switch (T->getTypeClass()) {
1601 default:
1602 return T;
1603 case Type::TemplateSpecialization:
1604 T = cast<TemplateSpecializationType>(T)->desugar();
1605 break;
John McCall424cec92011-01-19 06:33:43 +00001606 case Type::TypeOfExpr:
1607 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001608 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001609 case Type::TypeOf:
1610 T = cast<TypeOfType>(T)->getUnderlyingType();
1611 break;
1612 case Type::Decltype:
1613 T = cast<DecltypeType>(T)->getUnderlyingType();
1614 break;
Alexis Hunte852b102011-05-24 22:41:36 +00001615 case Type::UnaryTransform:
1616 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1617 break;
John McCall81904512011-01-06 01:58:22 +00001618 case Type::Attributed:
1619 T = cast<AttributedType>(T)->getEquivalentType();
John McCall4223a9e2011-03-04 04:00:19 +00001620 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001621 case Type::Elaborated:
1622 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001623 break;
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001624 case Type::Paren:
1625 T = cast<ParenType>(T)->getInnerType();
1626 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001627 case Type::SubstTemplateTypeParm:
1628 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1629 break;
Anders Carlsson829c4132011-03-06 16:43:04 +00001630 case Type::Auto:
1631 T = cast<AutoType>(T)->getDeducedType();
1632 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001633 }
1634
1635 assert(T != LastT && "Type unwrapping failed to unwrap!");
1636 if (T == LastT)
1637 return T;
1638 } while (true);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001639}
1640
Eric Christophercd888132011-12-16 23:40:18 +00001641/// getType - Get the type from the cache or return null type if it doesn't exist.
1642llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump11289f42009-09-09 15:08:12 +00001643
Douglas Gregor0f139a12009-12-21 20:18:30 +00001644 // Unwrap the type as needed for debug information.
1645 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001646
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001647 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001648 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001649 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001650 if (it != TypeCache.end()) {
1651 // Verify that the debug info still exists.
1652 if (&*it->second)
1653 return llvm::DIType(cast<llvm::MDNode>(it->second));
1654 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001655
Eric Christophercd888132011-12-16 23:40:18 +00001656 return llvm::DIType();
1657}
1658
Eric Christopher4c006e52012-02-16 22:54:45 +00001659/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1660/// doesn't exist.
1661llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1662
1663 // Unwrap the type as needed for debug information.
1664 Ty = UnwrapTypeForDebugInfo(Ty);
1665
1666 // Check for existing entry.
1667 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1668 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1669 if (it != CompletedTypeCache.end()) {
1670 // Verify that the debug info still exists.
1671 if (&*it->second)
1672 return llvm::DIType(cast<llvm::MDNode>(it->second));
1673 }
1674
1675 return llvm::DIType();
1676}
1677
1678
Eric Christophercd888132011-12-16 23:40:18 +00001679/// getOrCreateType - Get the type from the cache or create a new
1680/// one if necessary.
1681llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1682 if (Ty.isNull())
1683 return llvm::DIType();
1684
1685 // Unwrap the type as needed for debug information.
1686 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001687
Eric Christopher4c006e52012-02-16 22:54:45 +00001688 llvm::DIType T = getCompletedTypeOrNull(Ty);
1689
Eric Christopher8a41bd82012-02-13 14:56:11 +00001690 if (T.Verify()) return T;
Eric Christophercd888132011-12-16 23:40:18 +00001691
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001692 // Otherwise create the type.
1693 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher1c3785a2012-02-18 00:50:17 +00001694
1695 llvm::DIType TC = getTypeOrNull(Ty);
1696 if (TC.Verify() && TC.isForwardDecl())
1697 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), TC));
Eric Christopher4c006e52012-02-16 22:54:45 +00001698
Anders Carlsson6037e782009-11-14 20:52:05 +00001699 // And update the type cache.
Eric Christopher4c006e52012-02-16 22:54:45 +00001700 TypeCache[Ty.getAsOpaquePtr()] = Res;
1701
1702 if (!Res.isForwardDecl())
1703 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001704 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001705}
1706
Anders Carlsson6037e782009-11-14 20:52:05 +00001707/// CreateTypeNode - Create a new debug type node.
Nick Lewycky6aad6df2011-11-09 04:27:23 +00001708llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001709 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001710 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001711 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001712
Douglas Gregor0915b432009-12-21 19:57:21 +00001713 const char *Diag = 0;
1714
Sanjiv Gupta98070572008-05-25 05:15:42 +00001715 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001716 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001717#define TYPE(Class, Base)
1718#define ABSTRACT_TYPE(Class, Base)
1719#define NON_CANONICAL_TYPE(Class, Base)
1720#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1721#include "clang/AST/TypeNodes.def"
David Blaikie83d382b2011-09-23 05:06:16 +00001722 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001723
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001724 case Type::ExtVector:
Devang Patelb4073382010-02-23 22:59:39 +00001725 case Type::Vector:
1726 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001727 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001728 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001729 case Type::ObjCObject:
1730 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001731 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001732 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001733 case Type::Builtin:
1734 return CreateType(cast<BuiltinType>(Ty));
1735 case Type::Complex:
1736 return CreateType(cast<ComplexType>(Ty));
1737 case Type::Pointer:
1738 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001739 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001740 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001741 case Type::Typedef:
1742 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001743 case Type::Record:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001744 return CreateType(cast<RecordType>(Ty));
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001745 case Type::Enum:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001746 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattneraffb3732008-11-10 06:08:34 +00001747 case Type::FunctionProto:
1748 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001749 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001750 case Type::ConstantArray:
1751 case Type::VariableArray:
1752 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001753 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001754
1755 case Type::LValueReference:
1756 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001757 case Type::RValueReference:
1758 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001759
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001760 case Type::MemberPointer:
1761 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001762
Eli Friedman0dfb8892011-10-06 23:00:33 +00001763 case Type::Atomic:
1764 return CreateType(cast<AtomicType>(Ty), Unit);
1765
John McCall81904512011-01-06 01:58:22 +00001766 case Type::Attributed:
Douglas Gregor0915b432009-12-21 19:57:21 +00001767 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001768 case Type::Elaborated:
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001769 case Type::Paren:
Douglas Gregor0915b432009-12-21 19:57:21 +00001770 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001771 case Type::TypeOfExpr:
1772 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001773 case Type::Decltype:
Alexis Hunte852b102011-05-24 22:41:36 +00001774 case Type::UnaryTransform:
Richard Smith30482bc2011-02-20 03:19:35 +00001775 case Type::Auto:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001776 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta98070572008-05-25 05:15:42 +00001777 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001778
1779 assert(Diag && "Fall through without a diagnostic?");
David Blaikie9c902b52011-09-25 23:23:43 +00001780 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor0915b432009-12-21 19:57:21 +00001781 "debug information for %0 is not yet supported");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001782 CGM.getDiags().Report(DiagID)
Douglas Gregor0915b432009-12-21 19:57:21 +00001783 << Diag;
1784 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001785}
1786
Eric Christopher4c006e52012-02-16 22:54:45 +00001787/// getOrCreateLimitedType - Get the type from the cache or create a new
1788/// limited type if necessary.
1789llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1790 llvm::DIFile Unit) {
1791 if (Ty.isNull())
1792 return llvm::DIType();
1793
1794 // Unwrap the type as needed for debug information.
1795 Ty = UnwrapTypeForDebugInfo(Ty);
1796
1797 llvm::DIType T = getTypeOrNull(Ty);
1798
1799 // We may have cached a forward decl when we could have created
1800 // a non-forward decl. Go ahead and create a non-forward decl
1801 // now.
1802 if (T.Verify() && !T.isForwardDecl()) return T;
1803
1804 // Otherwise create the type.
1805 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1806
Eric Christopher1c3785a2012-02-18 00:50:17 +00001807 if (T.Verify() && T.isForwardDecl())
1808 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), T));
1809
Eric Christopher4c006e52012-02-16 22:54:45 +00001810 // And update the type cache.
1811 TypeCache[Ty.getAsOpaquePtr()] = Res;
1812 return Res;
1813}
1814
1815// TODO: Currently used for context chains when limiting debug info.
1816llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1817 RecordDecl *RD = Ty->getDecl();
1818
1819 // Get overall information about the record type for the debug info.
1820 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1821 unsigned Line = getLineNumber(RD->getLocation());
1822 StringRef RDName = RD->getName();
1823
1824 llvm::DIDescriptor RDContext;
Alexey Samsonov486e1fe2012-04-27 07:24:20 +00001825 if (CGM.getCodeGenOpts().DebugInfo == CodeGenOptions::LimitedDebugInfo)
Eric Christopher4c006e52012-02-16 22:54:45 +00001826 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1827 else
1828 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1829
1830 // If this is just a forward declaration, construct an appropriately
1831 // marked node and just return it.
Eric Christopher1c3785a2012-02-18 00:50:17 +00001832 if (!RD->getDefinition())
1833 return createRecordFwdDecl(RD, RDContext);
Eric Christopher4c006e52012-02-16 22:54:45 +00001834
1835 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1836 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1837 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Benjamin Kramer8cc491a2012-03-20 19:49:14 +00001838 llvm::TrackingVH<llvm::MDNode> RealDecl;
Eric Christopher4c006e52012-02-16 22:54:45 +00001839
1840 if (RD->isUnion())
1841 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1842 Size, Align, 0, llvm::DIArray());
1843 else if (CXXDecl) {
1844 RDName = getClassName(RD);
1845
1846 // FIXME: This could be a struct type giving a default visibility different
1847 // than C++ class type, but needs llvm metadata changes first.
1848 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1849 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher66562a42012-02-20 18:05:24 +00001850 llvm::DIArray(), llvm::DIType(),
Eric Christopher4c006e52012-02-16 22:54:45 +00001851 llvm::DIArray());
1852 } else
1853 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1854 Size, Align, 0, llvm::DIArray());
1855
1856 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1857 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1858
1859 if (CXXDecl) {
1860 // A class's primary base or the class itself contains the vtable.
1861 llvm::MDNode *ContainingType = NULL;
1862 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1863 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1864 // Seek non virtual primary base root.
1865 while (1) {
1866 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1867 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1868 if (PBT && !BRL.isPrimaryBaseVirtual())
1869 PBase = PBT;
1870 else
1871 break;
1872 }
1873 ContainingType =
1874 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1875 }
1876 else if (CXXDecl->isDynamicClass())
1877 ContainingType = RealDecl;
1878
Eric Christopher2939e6e2012-02-17 07:09:48 +00001879 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher4c006e52012-02-16 22:54:45 +00001880 }
1881 return llvm::DIType(RealDecl);
1882}
1883
1884/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1885/// declare composite types that haven't been processed yet.
1886llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1887
1888 // Work out details of type.
1889 switch (Ty->getTypeClass()) {
1890#define TYPE(Class, Base)
1891#define ABSTRACT_TYPE(Class, Base)
1892#define NON_CANONICAL_TYPE(Class, Base)
1893#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1894 #include "clang/AST/TypeNodes.def"
1895 llvm_unreachable("Dependent types cannot show up in debug information");
1896
1897 case Type::Record:
1898 return CreateLimitedType(cast<RecordType>(Ty));
1899 default:
1900 return CreateTypeNode(Ty, Unit);
1901 }
1902}
1903
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001904/// CreateMemberType - Create new member and increase Offset by FType's size.
1905llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001906 StringRef Name,
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001907 uint64_t *Offset) {
1908 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1909 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1910 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel15013e72011-06-24 22:00:59 +00001911 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001912 FieldSize, FieldAlign,
1913 *Offset, 0, FieldTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001914 *Offset += FieldSize;
1915 return Ty;
1916}
1917
Devang Patela6cb0642011-04-23 00:08:01 +00001918/// getFunctionDeclaration - Return debug info descriptor to describe method
1919/// declaration for the given method definition.
1920llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1921 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1922 if (!FD) return llvm::DISubprogram();
1923
1924 // Setup context.
1925 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1926
Devang Patela3e3fde2011-04-29 23:42:32 +00001927 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001928 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patela3e3fde2011-04-29 23:42:32 +00001929 if (MI != SPCache.end()) {
1930 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1931 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1932 return SP;
1933 }
1934
Devang Patela6cb0642011-04-23 00:08:01 +00001935 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1936 E = FD->redecls_end(); I != E; ++I) {
1937 const FunctionDecl *NextFD = *I;
1938 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001939 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patela6cb0642011-04-23 00:08:01 +00001940 if (MI != SPCache.end()) {
1941 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1942 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1943 return SP;
1944 }
1945 }
1946 return llvm::DISubprogram();
1947}
1948
Devang Patel2780e452011-05-31 20:46:46 +00001949// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
1950// implicit parameter "this".
Eric Christopherfefafac2011-10-11 23:00:51 +00001951llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D,
1952 QualType FnType,
Devang Patel2780e452011-05-31 20:46:46 +00001953 llvm::DIFile F) {
1954 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1955 return getOrCreateMethodType(Method, F);
Nick Lewycky16790352011-11-10 00:34:02 +00001956 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patel7ce99c32011-05-31 21:18:50 +00001957 // Add "self" and "_cmd"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001958 SmallVector<llvm::Value *, 16> Elts;
Devang Patel7ce99c32011-05-31 21:18:50 +00001959
1960 // First element is always return type. For 'void' functions it is NULL.
Devang Patel5c71c212011-05-31 22:21:11 +00001961 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patel7ce99c32011-05-31 21:18:50 +00001962 // "self" pointer is always first argument.
1963 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F));
1964 // "cmd" pointer is always second argument.
1965 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F));
Devang Patel5c71c212011-05-31 22:21:11 +00001966 // Get rest of the arguments.
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001967 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Patel5c71c212011-05-31 22:21:11 +00001968 PE = OMethod->param_end(); PI != PE; ++PI)
1969 Elts.push_back(getOrCreateType((*PI)->getType(), F));
1970
Devang Patel7ce99c32011-05-31 21:18:50 +00001971 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1972 return DBuilder.createSubroutineType(F, EltTypeArray);
1973 }
Devang Patel2780e452011-05-31 20:46:46 +00001974 return getOrCreateType(FnType, F);
1975}
1976
Eric Christopher6dde3e12012-03-20 23:28:32 +00001977/// EmitFunctionStart - Constructs the debug code for entering a function.
Devang Patel934661e2010-01-14 00:36:21 +00001978void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001979 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001980 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001981
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001982 StringRef Name;
1983 StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001984
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001985 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel0884a602010-07-22 22:29:16 +00001986
Devang Patel934661e2010-01-14 00:36:21 +00001987 const Decl *D = GD.getDecl();
Eric Christopherb7e821a2012-04-03 00:44:15 +00001988 // Use the location of the declaration.
1989 SourceLocation Loc = D->getLocation();
Eric Christopher7cdf9482011-10-13 21:45:18 +00001990
Devang Patel251f8592010-10-07 22:03:49 +00001991 unsigned Flags = 0;
Eric Christopherb7e821a2012-04-03 00:44:15 +00001992 llvm::DIFile Unit = getOrCreateFile(Loc);
Devang Patel33ddf692010-10-11 21:58:41 +00001993 llvm::DIDescriptor FDContext(Unit);
Devang Patelb87c4282011-04-05 22:54:11 +00001994 llvm::DIArray TParamsArray;
Devang Patel934661e2010-01-14 00:36:21 +00001995 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopher04832b92011-11-14 18:55:02 +00001996 // If there is a DISubprogram for this function available then use it.
Devang Patel7a12ad02010-01-19 01:54:44 +00001997 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001998 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel7a12ad02010-01-19 01:54:44 +00001999 if (FI != SPCache.end()) {
Gabor Greifbf986082010-09-18 13:00:17 +00002000 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00002001 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
2002 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002003 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002004 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00002005 return;
2006 }
2007 }
Devang Patel934661e2010-01-14 00:36:21 +00002008 Name = getFunctionName(FD);
2009 // Use mangled name as linkage name for c/c++ functions.
Eric Christopherdb1554c2012-04-12 00:35:06 +00002010 if (FD->hasPrototype()) {
Devang Patelb7ff0da2011-05-02 22:37:48 +00002011 LinkageName = CGM.getMangledName(GD);
Eric Christopherdb1554c2012-04-12 00:35:06 +00002012 Flags |= llvm::DIDescriptor::FlagPrototyped;
2013 }
Alexey Samsonov74a38682012-05-04 07:39:27 +00002014 if (LinkageName == Name ||
2015 CGM.getCodeGenOpts().DebugInfo <= CodeGenOptions::DebugLineTablesOnly)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002016 LinkageName = StringRef();
Eric Christopherdb1554c2012-04-12 00:35:06 +00002017
Alexey Samsonov74a38682012-05-04 07:39:27 +00002018 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) {
2019 if (const NamespaceDecl *NSDecl =
2020 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
2021 FDContext = getOrCreateNameSpace(NSDecl);
2022 else if (const RecordDecl *RDecl =
2023 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
2024 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patelb87c4282011-04-05 22:54:11 +00002025
Alexey Samsonov74a38682012-05-04 07:39:27 +00002026 // Collect template parameters.
2027 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
2028 }
David Chisnall6bf98ff2010-09-02 17:16:32 +00002029 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnallcf607442010-09-02 18:01:51 +00002030 Name = getObjCMethodName(OMD);
Devang Patel251f8592010-10-07 22:03:49 +00002031 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00002032 } else {
Devang Patelf79199d2010-10-22 17:11:50 +00002033 // Use llvm function name.
Devang Patel934661e2010-01-14 00:36:21 +00002034 Name = Fn->getName();
Devang Patel251f8592010-10-07 22:03:49 +00002035 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00002036 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002037 if (!Name.empty() && Name[0] == '\01')
2038 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002039
Eric Christopherb7e821a2012-04-03 00:44:15 +00002040 unsigned LineNo = getLineNumber(Loc);
Devang Pateldb2732a2010-09-29 21:05:52 +00002041 if (D->isImplicit())
2042 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopherb7e821a2012-04-03 00:44:15 +00002043
Alexey Samsonov74a38682012-05-04 07:39:27 +00002044 llvm::DIType DIFnType;
2045 llvm::DISubprogram SPDecl;
2046 if (CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo) {
2047 DIFnType = getOrCreateFunctionType(D, FnType, Unit);
2048 SPDecl = getFunctionDeclaration(D);
2049 } else {
2050 // Create fake but valid subroutine type. Otherwise
2051 // llvm::DISubprogram::Verify() would return false, and
2052 // subprogram DIE will miss DW_AT_decl_file and
2053 // DW_AT_decl_line fields.
2054 SmallVector<llvm::Value*, 16> Elts;
2055 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
2056 DIFnType = DBuilder.createSubroutineType(Unit, EltTypeArray);
2057 }
2058 llvm::DISubprogram SP;
2059 SP = DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
2060 LineNo, DIFnType,
2061 Fn->hasInternalLinkage(), true/*definition*/,
2062 getLineNumber(CurLoc), Flags,
2063 CGM.getLangOpts().Optimize,
2064 Fn, TParamsArray, SPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002065
Sanjiv Gupta98070572008-05-25 05:15:42 +00002066 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00002067 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002068 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002069 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher4fd315f2011-09-29 00:00:37 +00002070}
Sanjiv Gupta98070572008-05-25 05:15:42 +00002071
Eric Christopherbfa4dc52011-09-29 00:00:41 +00002072/// EmitLocation - Emit metadata to indicate a change in line/column
2073/// information in the source file.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002074void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2075
2076 // Update our current location
2077 setLocation(Loc);
2078
Sanjiv Gupta98070572008-05-25 05:15:42 +00002079 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00002080
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002081 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002082 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher7cdf9482011-10-13 21:45:18 +00002083 if (CurLoc == PrevLoc ||
Chandler Carruth35f53202011-07-25 16:49:02 +00002084 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patela2c048e2010-04-05 21:09:15 +00002085 // New Builder may not be in sync with CGDebugInfo.
2086 if (!Builder.getCurrentDebugLocation().isUnknown())
2087 return;
Eric Christophere6556572011-09-29 00:00:35 +00002088
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002089 // Update last state.
2090 PrevLoc = CurLoc;
2091
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002092 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00002093 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
2094 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00002095 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002096}
2097
Eric Christopher7cdf9482011-10-13 21:45:18 +00002098/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2099/// the stack.
2100void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patelb40f2952009-11-13 19:10:24 +00002101 llvm::DIDescriptor D =
Eric Christopher7cdf9482011-10-13 21:45:18 +00002102 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel00fca3a2012-02-08 00:10:20 +00002103 llvm::DIDescriptor() :
2104 llvm::DIDescriptor(LexicalBlockStack.back()),
2105 getOrCreateFile(CurLoc),
2106 getLineNumber(CurLoc),
2107 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00002108 llvm::MDNode *DN = D;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002109 LexicalBlockStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002110}
2111
Eric Christopher7cdf9482011-10-13 21:45:18 +00002112/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2113/// region - beginning of a DW_TAG_lexical_block.
2114void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2115 // Set our current location.
2116 setLocation(Loc);
2117
2118 // Create a new lexical block and push it on the stack.
2119 CreateLexicalBlock(Loc);
2120
2121 // Emit a line table change for the current location inside the new scope.
2122 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel00fca3a2012-02-08 00:10:20 +00002123 getColumnNumber(Loc),
2124 LexicalBlockStack.back()));
Eric Christopher7cdf9482011-10-13 21:45:18 +00002125}
2126
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002127/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher9c13eea2011-09-26 15:03:22 +00002128/// region - end of a DW_TAG_lexical_block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002129void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002130 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002131
Eric Christopher7cdf9482011-10-13 21:45:18 +00002132 // Provide an entry in the line table for the end of the block.
2133 EmitLocation(Builder, Loc);
Mike Stump11289f42009-09-09 15:08:12 +00002134
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002135 LexicalBlockStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002136}
2137
Devang Patel0884a602010-07-22 22:29:16 +00002138/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2139void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002140 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel0884a602010-07-22 22:29:16 +00002141 unsigned RCount = FnBeginRegionCount.back();
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002142 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel0884a602010-07-22 22:29:16 +00002143
2144 // Pop all regions for this function.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002145 while (LexicalBlockStack.size() != RCount)
Eric Christopher7cdf9482011-10-13 21:45:18 +00002146 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel0884a602010-07-22 22:29:16 +00002147 FnBeginRegionCount.pop_back();
2148}
2149
Devang Patel535fdaf2010-02-10 18:49:08 +00002150// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2151// See BuildByRefType.
2152llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2153 uint64_t *XOffset) {
2154
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002155 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel535fdaf2010-02-10 18:49:08 +00002156 QualType FType;
2157 uint64_t FieldSize, FieldOffset;
2158 unsigned FieldAlign;
2159
Devang Patel408dcf62010-03-09 00:44:50 +00002160 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002161 QualType Type = VD->getType();
2162
2163 FieldOffset = 0;
2164 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002165 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2166 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002167 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002168 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2169 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2170
John McCall351762c2011-02-07 10:33:21 +00002171 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel535fdaf2010-02-10 18:49:08 +00002172 if (HasCopyAndDispose) {
2173 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002174 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2175 &FieldOffset));
2176 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2177 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002178 }
2179
2180 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002181 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002182 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck8159c1f2011-04-22 17:34:18 +00002183 CharUnits FieldOffsetInBytes
2184 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2185 CharUnits AlignedOffsetInBytes
2186 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2187 CharUnits NumPaddingBytes
2188 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel535fdaf2010-02-10 18:49:08 +00002189
Ken Dyck8159c1f2011-04-22 17:34:18 +00002190 if (NumPaddingBytes.isPositive()) {
2191 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel535fdaf2010-02-10 18:49:08 +00002192 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2193 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002194 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002195 }
2196 }
2197
2198 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002199 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00002200 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002201 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel535fdaf2010-02-10 18:49:08 +00002202
2203 *XOffset = FieldOffset;
Devang Patel15013e72011-06-24 22:00:59 +00002204 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002205 0, FieldSize, FieldAlign,
2206 FieldOffset, 0, FieldTy);
Devang Patel535fdaf2010-02-10 18:49:08 +00002207 EltTys.push_back(FieldTy);
2208 FieldOffset += FieldSize;
2209
Jay Foaddbf81d82011-04-24 10:11:03 +00002210 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel535fdaf2010-02-10 18:49:08 +00002211
Devang Pateldb2732a2010-09-29 21:05:52 +00002212 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel535fdaf2010-02-10 18:49:08 +00002213
Devang Pateld7185b72011-02-22 18:56:36 +00002214 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel00afcbe2010-12-08 22:42:58 +00002215 Elements);
Devang Patel535fdaf2010-02-10 18:49:08 +00002216}
Devang Patel00afcbe2010-12-08 22:42:58 +00002217
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002218/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00002219void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel68a15252011-03-03 20:13:15 +00002220 llvm::Value *Storage,
2221 unsigned ArgNo, CGBuilderTy &Builder) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002222 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002223 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002224
Devang Patel408dcf62010-03-09 00:44:50 +00002225 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002226 llvm::DIType Ty;
2227 uint64_t XOffset = 0;
2228 if (VD->hasAttr<BlocksAttr>())
2229 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2230 else
2231 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00002232
Devang Patel67eba802010-05-07 23:05:55 +00002233 // If there is not any debug info for type then do not emit debug info
2234 // for this variable.
2235 if (!Ty)
2236 return;
2237
Devang Patel25468052011-02-16 01:11:51 +00002238 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2239 // If Storage is an aggregate returned as 'sret' then let debugger know
2240 // about this.
Devang Patel425909d2011-02-10 00:40:52 +00002241 if (Arg->hasStructRetAttr())
Devang Pateld7185b72011-02-22 18:56:36 +00002242 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002243 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2244 // If an aggregate variable has non trivial destructor or non trivial copy
2245 // constructor than it is pass indirectly. Let debug info know about this
2246 // by using reference of the aggregate type as a argument type.
Eric Christopherfefafac2011-10-11 23:00:51 +00002247 if (!Record->hasTrivialCopyConstructor() ||
2248 !Record->hasTrivialDestructor())
Devang Pateld7185b72011-02-22 18:56:36 +00002249 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002250 }
2251 }
Devang Patel425909d2011-02-10 00:40:52 +00002252
Chris Lattneraffb3732008-11-10 06:08:34 +00002253 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002254 unsigned Line = getLineNumber(VD->getLocation());
2255 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patel7c086222010-09-29 23:09:21 +00002256 unsigned Flags = 0;
2257 if (VD->isImplicit())
2258 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002259 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel67f70aa2010-10-12 23:24:54 +00002260
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002261 StringRef Name = VD->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002262 if (!Name.empty()) {
Devang Patelbc474982011-01-11 00:30:27 +00002263 if (VD->hasAttr<BlocksAttr>()) {
2264 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002265 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002266 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002267 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002268 // offset of __forwarding field
Ken Dyckbbe38622011-04-22 17:41:34 +00002269 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002270 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelbc474982011-01-11 00:30:27 +00002271 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002272 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2273 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002274 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002275 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelbc474982011-01-11 00:30:27 +00002276 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2277
2278 // Create the descriptor for the variable.
2279 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002280 DBuilder.createComplexVariable(Tag,
Eric Christopherfefafac2011-10-11 23:00:51 +00002281 llvm::DIDescriptor(Scope),
Devang Patelbc474982011-01-11 00:30:27 +00002282 VD->getName(), Unit, Line, Ty,
Jay Foaddbf81d82011-04-24 10:11:03 +00002283 addr, ArgNo);
Devang Patelbc474982011-01-11 00:30:27 +00002284
2285 // Insert an llvm.dbg.declare into the current block.
2286 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002287 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelbc474982011-01-11 00:30:27 +00002288 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2289 return;
Eric Christopherc0e7a3d2012-05-08 18:56:47 +00002290 } else if (isa<VariableArrayType>(VD->getType())) {
2291 // These are "complex" variables in that they need an op_deref.
Devang Patelbc474982011-01-11 00:30:27 +00002292 // Create the descriptor for the variable.
Eric Christopherc0e7a3d2012-05-08 18:56:47 +00002293 llvm::Value *Addr = llvm::ConstantInt::get(CGM.Int64Ty,
2294 llvm::DIBuilder::OpDeref);
2295 llvm::DIVariable D =
2296 DBuilder.createComplexVariable(Tag,
2297 llvm::DIDescriptor(Scope),
2298 Name, Unit, Line, Ty,
2299 Addr, ArgNo);
2300
2301 // Insert an llvm.dbg.declare into the current block.
2302 llvm::Instruction *Call =
2303 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
2304 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2305 return;
2306 }
2307
2308 // Create the descriptor for the variable.
Devang Patel67f70aa2010-10-12 23:24:54 +00002309 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002310 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel00afcbe2010-12-08 22:42:58 +00002311 Name, Unit, Line, Ty,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002312 CGM.getLangOpts().Optimize, Flags, ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002313
2314 // Insert an llvm.dbg.declare into the current block.
2315 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002316 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel67f70aa2010-10-12 23:24:54 +00002317 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel31d1e212010-10-29 16:21:19 +00002318 return;
Devang Patel67f70aa2010-10-12 23:24:54 +00002319 }
2320
2321 // If VD is an anonymous union then Storage represents value for
2322 // all union fields.
John McCall147d0212011-02-22 22:38:33 +00002323 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2324 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2325 if (RD->isUnion()) {
2326 for (RecordDecl::field_iterator I = RD->field_begin(),
2327 E = RD->field_end();
2328 I != E; ++I) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002329 FieldDecl *Field = &*I;
John McCall147d0212011-02-22 22:38:33 +00002330 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002331 StringRef FieldName = Field->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002332
John McCall147d0212011-02-22 22:38:33 +00002333 // Ignore unnamed fields. Do not ignore unnamed records.
2334 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2335 continue;
Devang Patel67f70aa2010-10-12 23:24:54 +00002336
John McCall147d0212011-02-22 22:38:33 +00002337 // Use VarDecl's Tag, Scope and Line number.
2338 llvm::DIVariable D =
2339 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2340 FieldName, Unit, Line, FieldTy,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002341 CGM.getLangOpts().Optimize, Flags,
Devang Patel68a15252011-03-03 20:13:15 +00002342 ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002343
John McCall147d0212011-02-22 22:38:33 +00002344 // Insert an llvm.dbg.declare into the current block.
2345 llvm::Instruction *Call =
2346 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall147d0212011-02-22 22:38:33 +00002347 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel67f70aa2010-10-12 23:24:54 +00002348 }
John McCall147d0212011-02-22 22:38:33 +00002349 }
2350 }
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002351}
2352
Devang Patel4f325d12011-04-25 23:43:36 +00002353void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2354 llvm::Value *Storage,
2355 CGBuilderTy &Builder) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002356 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patel4f325d12011-04-25 23:43:36 +00002357 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2358}
Mike Stump2e722b92009-09-30 02:43:10 +00002359
Devang Patel4f325d12011-04-25 23:43:36 +00002360void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2361 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2362 const CGBlockInfo &blockInfo) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002363 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002364 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel4f325d12011-04-25 23:43:36 +00002365
Devang Patel42fb6f82010-04-26 23:28:46 +00002366 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00002367 return;
Devang Patel4f325d12011-04-25 23:43:36 +00002368
John McCall351762c2011-02-07 10:33:21 +00002369 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patel4f325d12011-04-25 23:43:36 +00002370
Mike Stump2e722b92009-09-30 02:43:10 +00002371 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00002372 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002373 llvm::DIType Ty;
John McCall351762c2011-02-07 10:33:21 +00002374 if (isByRef)
Devang Patel535fdaf2010-02-10 18:49:08 +00002375 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2376 else
2377 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00002378
2379 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002380 unsigned Line = getLineNumber(VD->getLocation());
2381 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00002382
John McCall351762c2011-02-07 10:33:21 +00002383 const llvm::TargetData &target = CGM.getTargetData();
2384
2385 CharUnits offset = CharUnits::fromQuantity(
2386 target.getStructLayout(blockInfo.StructureType)
2387 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2388
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002389 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002390 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002391 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattnerbf784782010-01-25 03:29:35 +00002392 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall351762c2011-02-07 10:33:21 +00002393 if (isByRef) {
Devang Patel2d6390d2011-02-18 23:29:22 +00002394 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2395 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002396 // offset of __forwarding field
Eric Christopherfefafac2011-10-11 23:00:51 +00002397 offset = CGM.getContext()
2398 .toCharUnitsFromBits(target.getPointerSizeInBits());
Chris Lattnerbf784782010-01-25 03:29:35 +00002399 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002400 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2401 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002402 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002403 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattnerbf784782010-01-25 03:29:35 +00002404 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00002405 }
2406
2407 // Create the descriptor for the variable.
2408 llvm::DIVariable D =
Devang Patel4f325d12011-04-25 23:43:36 +00002409 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002410 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foaddbf81d82011-04-24 10:11:03 +00002411 VD->getName(), Unit, Line, Ty, addr);
Mike Stump2e722b92009-09-30 02:43:10 +00002412 // Insert an llvm.dbg.declare into the current block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002413 llvm::Instruction *Call =
Devang Patel420c8de2011-04-25 23:52:27 +00002414 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher7cdf9482011-10-13 21:45:18 +00002415 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2416 LexicalBlockStack.back()));
Mike Stump2e722b92009-09-30 02:43:10 +00002417}
2418
Chris Lattneraffb3732008-11-10 06:08:34 +00002419/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2420/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00002421void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel68a15252011-03-03 20:13:15 +00002422 unsigned ArgNo,
Devang Patel25468052011-02-16 01:11:51 +00002423 CGBuilderTy &Builder) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002424 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patel68a15252011-03-03 20:13:15 +00002425 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00002426}
2427
John McCall147d0212011-02-22 22:38:33 +00002428namespace {
2429 struct BlockLayoutChunk {
2430 uint64_t OffsetInBits;
2431 const BlockDecl::Capture *Capture;
2432 };
2433 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2434 return l.OffsetInBits < r.OffsetInBits;
2435 }
2436}
Chris Lattneraffb3732008-11-10 06:08:34 +00002437
John McCall147d0212011-02-22 22:38:33 +00002438void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2439 llvm::Value *addr,
2440 CGBuilderTy &Builder) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002441 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
John McCall147d0212011-02-22 22:38:33 +00002442 ASTContext &C = CGM.getContext();
2443 const BlockDecl *blockDecl = block.getBlockDecl();
2444
2445 // Collect some general information about the block's location.
2446 SourceLocation loc = blockDecl->getCaretLocation();
2447 llvm::DIFile tunit = getOrCreateFile(loc);
2448 unsigned line = getLineNumber(loc);
2449 unsigned column = getColumnNumber(loc);
2450
2451 // Build the debug-info type for the block literal.
Nick Lewyckyfc49f722011-05-02 01:41:48 +00002452 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall147d0212011-02-22 22:38:33 +00002453
2454 const llvm::StructLayout *blockLayout =
2455 CGM.getTargetData().getStructLayout(block.StructureType);
2456
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002457 SmallVector<llvm::Value*, 16> fields;
John McCall147d0212011-02-22 22:38:33 +00002458 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2459 blockLayout->getElementOffsetInBits(0),
Devang Patel15013e72011-06-24 22:00:59 +00002460 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002461 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2462 blockLayout->getElementOffsetInBits(1),
Devang Patel15013e72011-06-24 22:00:59 +00002463 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002464 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2465 blockLayout->getElementOffsetInBits(2),
Devang Patel15013e72011-06-24 22:00:59 +00002466 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002467 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2468 blockLayout->getElementOffsetInBits(3),
Devang Patel15013e72011-06-24 22:00:59 +00002469 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002470 fields.push_back(createFieldType("__descriptor",
2471 C.getPointerType(block.NeedsCopyDispose ?
2472 C.getBlockDescriptorExtendedType() :
2473 C.getBlockDescriptorType()),
2474 0, loc, AS_public,
2475 blockLayout->getElementOffsetInBits(4),
Devang Patel15013e72011-06-24 22:00:59 +00002476 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002477
2478 // We want to sort the captures by offset, not because DWARF
2479 // requires this, but because we're paranoid about debuggers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002480 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall147d0212011-02-22 22:38:33 +00002481
2482 // 'this' capture.
2483 if (blockDecl->capturesCXXThis()) {
2484 BlockLayoutChunk chunk;
2485 chunk.OffsetInBits =
2486 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2487 chunk.Capture = 0;
2488 chunks.push_back(chunk);
2489 }
2490
2491 // Variable captures.
2492 for (BlockDecl::capture_const_iterator
2493 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2494 i != e; ++i) {
2495 const BlockDecl::Capture &capture = *i;
2496 const VarDecl *variable = capture.getVariable();
2497 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2498
2499 // Ignore constant captures.
2500 if (captureInfo.isConstant())
2501 continue;
2502
2503 BlockLayoutChunk chunk;
2504 chunk.OffsetInBits =
2505 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2506 chunk.Capture = &capture;
2507 chunks.push_back(chunk);
2508 }
2509
2510 // Sort by offset.
2511 llvm::array_pod_sort(chunks.begin(), chunks.end());
2512
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002513 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall147d0212011-02-22 22:38:33 +00002514 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2515 uint64_t offsetInBits = i->OffsetInBits;
2516 const BlockDecl::Capture *capture = i->Capture;
2517
2518 // If we have a null capture, this must be the C++ 'this' capture.
2519 if (!capture) {
2520 const CXXMethodDecl *method =
2521 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2522 QualType type = method->getThisType(C);
2523
2524 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel15013e72011-06-24 22:00:59 +00002525 offsetInBits, tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002526 continue;
2527 }
2528
2529 const VarDecl *variable = capture->getVariable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002530 StringRef name = variable->getName();
John McCall81a325e2011-03-02 06:57:14 +00002531
2532 llvm::DIType fieldType;
2533 if (capture->isByRef()) {
2534 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2535
2536 // FIXME: this creates a second copy of this type!
2537 uint64_t xoffset;
2538 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2539 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel15013e72011-06-24 22:00:59 +00002540 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCall81a325e2011-03-02 06:57:14 +00002541 ptrInfo.first, ptrInfo.second,
2542 offsetInBits, 0, fieldType);
2543 } else {
2544 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel15013e72011-06-24 22:00:59 +00002545 loc, AS_public, offsetInBits, tunit, tunit);
John McCall81a325e2011-03-02 06:57:14 +00002546 }
2547 fields.push_back(fieldType);
John McCall147d0212011-02-22 22:38:33 +00002548 }
2549
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002550 SmallString<36> typeName;
John McCall147d0212011-02-22 22:38:33 +00002551 llvm::raw_svector_ostream(typeName)
2552 << "__block_literal_" << CGM.getUniqueBlockCount();
2553
Jay Foaddbf81d82011-04-24 10:11:03 +00002554 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall147d0212011-02-22 22:38:33 +00002555
2556 llvm::DIType type =
2557 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2558 CGM.getContext().toBits(block.BlockSize),
2559 CGM.getContext().toBits(block.BlockAlign),
2560 0, fieldsArray);
2561 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2562
2563 // Get overall information about the block.
2564 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002565 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002566 StringRef name = ".block_descriptor";
John McCall147d0212011-02-22 22:38:33 +00002567
2568 // Create the descriptor for the parameter.
2569 llvm::DIVariable debugVar =
2570 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2571 llvm::DIDescriptor(scope),
2572 name, tunit, line, type,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002573 CGM.getLangOpts().Optimize, flags,
Devang Patel68a15252011-03-03 20:13:15 +00002574 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall147d0212011-02-22 22:38:33 +00002575
2576 // Insert an llvm.dbg.value into the current block.
2577 llvm::Instruction *declare =
2578 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2579 Builder.GetInsertBlock());
2580 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2581}
Chris Lattneraffb3732008-11-10 06:08:34 +00002582
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002583/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00002584void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00002585 const VarDecl *D) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002586 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002587 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002588 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002589 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00002590
Eric Christopher7cdf9482011-10-13 21:45:18 +00002591 setLocation(D->getLocation());
2592
Devang Patel7b7f46f2010-02-01 21:34:11 +00002593 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002594 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002595
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002596 // CodeGen turns int[] into int[1] so we'll do the same here.
2597 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002598
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002599 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002600 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002601
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002602 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewycky2219ef02011-11-09 04:25:21 +00002603 ArrayType::Normal, 0);
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002604 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002605 StringRef DeclName = D->getName();
2606 StringRef LinkageName;
Devang Patel84d40a42011-02-09 19:16:38 +00002607 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2608 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00002609 LinkageName = Var->getName();
Devang Patelf79199d2010-10-22 17:11:50 +00002610 if (LinkageName == DeclName)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002611 LinkageName = StringRef();
Devang Patel7b7f46f2010-02-01 21:34:11 +00002612 llvm::DIDescriptor DContext =
Devang Patel8c445292010-12-09 00:33:05 +00002613 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Pateld7185b72011-02-22 18:56:36 +00002614 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +00002615 Unit, LineNo, getOrCreateType(T, Unit),
2616 Var->hasInternalLinkage(), Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002617}
2618
Devang Patelf4c205b2009-02-26 21:10:26 +00002619/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00002620void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00002621 ObjCInterfaceDecl *ID) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002622 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patelf4c205b2009-02-26 21:10:26 +00002623 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002624 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002625 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00002626
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002627 StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00002628
Devang Patel3efd1472010-02-01 21:52:22 +00002629 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00002630 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002631
Devang Patelf4c205b2009-02-26 21:10:26 +00002632 // CodeGen turns int[] into int[1] so we'll do the same here.
2633 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002634
Devang Patelf4c205b2009-02-26 21:10:26 +00002635 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002636 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002637
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002638 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00002639 ArrayType::Normal, 0);
2640 }
2641
Devang Pateld7185b72011-02-22 18:56:36 +00002642 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel00afcbe2010-12-08 22:42:58 +00002643 getOrCreateType(T, Unit),
2644 Var->hasInternalLinkage(), Var);
Devang Patelf4c205b2009-02-26 21:10:26 +00002645}
Devang Patel973f2eb2010-02-01 19:16:32 +00002646
Devang Pateldc866e12010-08-10 17:53:33 +00002647/// EmitGlobalVariable - Emit global variable's debug info.
2648void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCalla2fabff2010-10-09 01:34:31 +00002649 llvm::Constant *Init) {
Alexey Samsonov74a38682012-05-04 07:39:27 +00002650 assert(CGM.getCodeGenOpts().DebugInfo >= CodeGenOptions::LimitedDebugInfo);
Devang Patele03edfd2010-08-10 07:24:25 +00002651 // Create the descriptor for the variable.
2652 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002653 StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00002654 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00002655 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2656 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel283e89d2011-01-17 22:23:07 +00002657 Ty = CreateEnumType(ED);
Devang Patel41c20972010-08-23 22:07:25 +00002658 }
Devang Patel76e3b532010-08-10 18:27:15 +00002659 // Do not use DIGlobalVariable for enums.
2660 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2661 return;
Devang Pateld7185b72011-02-22 18:56:36 +00002662 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002663 getLineNumber(VD->getLocation()),
2664 Ty, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00002665}
2666
Devang Patel973f2eb2010-02-01 19:16:32 +00002667/// getOrCreateNamesSpace - Return namespace descriptor for the given
2668/// namespace decl.
2669llvm::DINameSpace
Devang Patel8c445292010-12-09 00:33:05 +00002670CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patel973f2eb2010-02-01 19:16:32 +00002671 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2672 NameSpaceCache.find(NSDecl);
2673 if (I != NameSpaceCache.end())
2674 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2675
Devang Patelc5ffabc2010-05-12 23:46:38 +00002676 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelfaadd7b2010-10-28 19:12:46 +00002677 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00002678 llvm::DIDescriptor Context =
Devang Patel8c445292010-12-09 00:33:05 +00002679 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patel973f2eb2010-02-01 19:16:32 +00002680 llvm::DINameSpace NS =
Devang Pateld7185b72011-02-22 18:56:36 +00002681 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002682 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00002683 return NS;
2684}
Eric Christopher1c3785a2012-02-18 00:50:17 +00002685
2686void CGDebugInfo::finalize(void) {
2687 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2688 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2689 llvm::DIType Ty, RepTy;
2690 // Verify that the debug info still exists.
2691 if (&*VI->second)
2692 Ty = llvm::DIType(cast<llvm::MDNode>(VI->second));
2693
2694 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2695 TypeCache.find(VI->first);
2696 if (it != TypeCache.end()) {
2697 // Verify that the debug info still exists.
2698 if (&*it->second)
2699 RepTy = llvm::DIType(cast<llvm::MDNode>(it->second));
2700 }
2701
Eric Christopher66562a42012-02-20 18:05:24 +00002702 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher1c3785a2012-02-18 00:50:17 +00002703 Ty.replaceAllUsesWith(RepTy);
Eric Christopher66562a42012-02-20 18:05:24 +00002704 }
Eric Christopher1c3785a2012-02-18 00:50:17 +00002705 }
2706 DBuilder.finalize();
2707}