blob: d88f24339a5571d135b5526c6101d9351fe83981 [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();
120 if (FII)
121 return FII->getName();
122
123 // Otherwise construct human readable name for debug info.
124 std::string NS = FD->getNameAsString();
125
126 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000127 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +0000128 memcpy(StrPtr, NS.data(), NS.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000129 return StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +0000130}
131
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000132StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000133 SmallString<256> MethodName;
David Chisnallcf607442010-09-02 18:01:51 +0000134 llvm::raw_svector_ostream OS(MethodName);
135 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
136 const DeclContext *DC = OMD->getDeclContext();
Devang Patel8e007302010-10-28 17:27:32 +0000137 if (const ObjCImplementationDecl *OID =
138 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnallcf607442010-09-02 18:01:51 +0000139 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000140 } else if (const ObjCInterfaceDecl *OID =
141 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanianf34011e2010-10-18 17:51:06 +0000142 OS << OID->getName();
Devang Patel8e007302010-10-28 17:27:32 +0000143 } else if (const ObjCCategoryImplDecl *OCD =
144 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnallcf607442010-09-02 18:01:51 +0000145 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
146 OCD->getIdentifier()->getNameStart() << ')';
147 }
148 OS << ' ' << OMD->getSelector().getAsString() << ']';
149
150 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
151 memcpy(StrPtr, MethodName.begin(), OS.tell());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000152 return StringRef(StrPtr, OS.tell());
David Chisnallcf607442010-09-02 18:01:51 +0000153}
154
Devang Patel43cfa5d2011-04-18 17:30:25 +0000155/// getSelectorName - Return selector name. This is used for debugging
Devang Patel7294d742011-04-16 00:37:51 +0000156/// info.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000157StringRef CGDebugInfo::getSelectorName(Selector S) {
Benjamin Kramer47b5b312011-10-14 18:45:16 +0000158 const std::string &SName = S.getAsString();
159 char *StrPtr = DebugInfoNames.Allocate<char>(SName.size());
160 memcpy(StrPtr, SName.data(), SName.size());
161 return StringRef(StrPtr, SName.size());
Devang Patel7294d742011-04-16 00:37:51 +0000162}
163
Devang Patel6c018202010-07-20 20:24:18 +0000164/// getClassName - Get class name including template argument list.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000165StringRef
Eric Christopherb2db8e42012-02-08 01:53:14 +0000166CGDebugInfo::getClassName(const RecordDecl *RD) {
167 const ClassTemplateSpecializationDecl *Spec
Devang Patel6c018202010-07-20 20:24:18 +0000168 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
169 if (!Spec)
170 return RD->getName();
171
172 const TemplateArgument *Args;
173 unsigned NumArgs;
174 std::string Buffer;
175 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
176 const TemplateSpecializationType *TST =
177 cast<TemplateSpecializationType>(TAW->getType());
178 Args = TST->getArgs();
179 NumArgs = TST->getNumArgs();
180 } else {
181 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000182 Args = TemplateArgs.data();
183 NumArgs = TemplateArgs.size();
Devang Patel6c018202010-07-20 20:24:18 +0000184 }
185 Buffer = RD->getIdentifier()->getNameStart();
David Blaikiebbafb8a2012-03-11 07:00:24 +0000186 PrintingPolicy Policy(CGM.getLangOpts());
Devang Patel6c018202010-07-20 20:24:18 +0000187 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
188 NumArgs,
189 Policy);
190
191 // Copy this name on the side and use its reference.
192 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
193 memcpy(StrPtr, Buffer.data(), Buffer.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000194 return StringRef(StrPtr, Buffer.length());
Devang Patel6c018202010-07-20 20:24:18 +0000195}
196
Devang Patel408dcf62010-03-09 00:44:50 +0000197/// getOrCreateFile - Get the file debug info descriptor for the input location.
198llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000199 if (!Loc.isValid())
200 // If Location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000201 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel00afcbe2010-12-08 22:42:58 +0000202
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000203 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000204 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000205
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000206 if (PLoc.isInvalid() || StringRef(PLoc.getFilename()).empty())
Douglas Gregorad3832e2010-11-11 20:45:16 +0000207 // If the location is not valid then use main input file.
Devang Pateld7185b72011-02-22 18:56:36 +0000208 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregorad3832e2010-11-11 20:45:16 +0000209
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000210 // Cache the results.
211 const char *fname = PLoc.getFilename();
212 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
213 DIFileCache.find(fname);
214
215 if (it != DIFileCache.end()) {
216 // Verify that the information still exists.
217 if (&*it->second)
218 return llvm::DIFile(cast<llvm::MDNode>(it->second));
219 }
220
Devang Pateld7185b72011-02-22 18:56:36 +0000221 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000222
Devang Patelba4ad7f2010-05-07 18:12:35 +0000223 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000224 return F;
Devang Patel408dcf62010-03-09 00:44:50 +0000225}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000226
Devang Pateled23f182010-10-28 22:03:20 +0000227/// getOrCreateMainFile - Get the file info for main compile unit.
228llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Pateld7185b72011-02-22 18:56:36 +0000229 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Pateled23f182010-10-28 22:03:20 +0000230}
231
Devang Patelc5ffabc2010-05-12 23:46:38 +0000232/// getLineNumber - Get line number for the location. If location is invalid
233/// then use current location.
234unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
Devang Patelf93d0b82012-02-06 23:24:13 +0000235 if (Loc.isInvalid() && CurLoc.isInvalid())
236 return 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000237 SourceManager &SM = CGM.getContext().getSourceManager();
238 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregorad3832e2010-11-11 20:45:16 +0000239 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000240}
241
242/// getColumnNumber - Get column number for the location. If location is
243/// invalid then use current location.
244unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
Devang Patelf93d0b82012-02-06 23:24:13 +0000245 if (Loc.isInvalid() && CurLoc.isInvalid())
246 return 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000247 SourceManager &SM = CGM.getContext().getSourceManager();
248 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregorad3832e2010-11-11 20:45:16 +0000249 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000250}
251
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000252StringRef CGDebugInfo::getCurrentDirname() {
Nick Lewyckyba743b72011-10-21 02:32:14 +0000253 if (!CGM.getCodeGenOpts().DebugCompilationDir.empty())
254 return CGM.getCodeGenOpts().DebugCompilationDir;
255
Devang Patel6014edd2010-07-27 15:17:16 +0000256 if (!CWDName.empty())
257 return CWDName;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000258 SmallString<256> CWD;
Benjamin Kramerfd0b05f2011-10-14 18:45:11 +0000259 llvm::sys::fs::current_path(CWD);
260 char *CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
261 memcpy(CompDirnamePtr, CWD.data(), CWD.size());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000262 return CWDName = StringRef(CompDirnamePtr, CWD.size());
Devang Patel6014edd2010-07-27 15:17:16 +0000263}
264
Devang Patel408dcf62010-03-09 00:44:50 +0000265/// CreateCompileUnit - Create new compile unit.
266void CGDebugInfo::CreateCompileUnit() {
267
268 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000269 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000270 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
271 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000272 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000273
Douglas Gregor65f7a3f2010-03-22 21:28:29 +0000274 // The main file name provided via the "-main-file-name" option contains just
275 // the file name itself with no path information. This file name may have had
276 // a relative path, so we look into the actual file entry for the main
277 // file to determine the real absolute path for the file.
Devang Patelcb9fe9e2010-07-23 23:04:28 +0000278 std::string MainFileDir;
Devang Patel6014edd2010-07-27 15:17:16 +0000279 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000280 MainFileDir = MainFile->getDir()->getName();
Devang Patel6014edd2010-07-27 15:17:16 +0000281 if (MainFileDir != ".")
282 MainFileName = MainFileDir + "/" + MainFileName;
283 }
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000284
Devang Patel6014edd2010-07-27 15:17:16 +0000285 // Save filename string.
286 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
287 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000288 StringRef Filename(FilenamePtr, MainFileName.length());
Devang Patel6014edd2010-07-27 15:17:16 +0000289
Chris Lattner8c37df42009-03-25 03:28:08 +0000290 unsigned LangTag;
David Blaikiebbafb8a2012-03-11 07:00:24 +0000291 const LangOptions &LO = CGM.getLangOpts();
Chris Lattner8c37df42009-03-25 03:28:08 +0000292 if (LO.CPlusPlus) {
293 if (LO.ObjC1)
294 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
295 else
296 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
297 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000298 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000299 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000300 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000301 } else {
302 LangTag = llvm::dwarf::DW_LANG_C89;
303 }
Devang Patel75009452009-04-17 21:06:59 +0000304
Daniel Dunbar64c222a2010-08-24 17:41:09 +0000305 std::string Producer = getClangFullVersion();
Chris Lattner5912de12009-05-02 01:00:04 +0000306
307 // Figure out which version of the ObjC runtime we have.
308 unsigned RuntimeVers = 0;
309 if (LO.ObjC1)
310 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000311
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000312 // Create new compile unit.
Devang Pateld7185b72011-02-22 18:56:36 +0000313 DBuilder.createCompileUnit(
Devang Patel4f6e73b2010-07-27 20:49:59 +0000314 LangTag, Filename, getCurrentDirname(),
Devang Patel00afcbe2010-12-08 22:42:58 +0000315 Producer,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000316 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel00afcbe2010-12-08 22:42:58 +0000317 // FIXME - Eliminate TheCU.
318 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000319}
320
Devang Patel410dc002009-02-25 01:36:11 +0000321/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000322/// one if necessary.
Devang Patel86f30f52010-11-01 16:52:40 +0000323llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000324 unsigned Encoding = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000325 const char *BTName = NULL;
Chris Lattneraffb3732008-11-10 06:08:34 +0000326 switch (BT->getKind()) {
John McCalle314e272011-10-18 21:02:43 +0000327#define BUILTIN_TYPE(Id, SingletonId)
328#define PLACEHOLDER_TYPE(Id, SingletonId) \
329 case BuiltinType::Id:
330#include "clang/AST/BuiltinTypes.def"
Devang Patelb0fa5b52011-09-12 18:50:21 +0000331 case BuiltinType::Dependent:
John McCalle314e272011-10-18 21:02:43 +0000332 llvm_unreachable("Unexpected builtin type");
Devang Patelb0fa5b52011-09-12 18:50:21 +0000333 case BuiltinType::NullPtr:
Devang Patelecaf9ac2011-09-14 23:14:14 +0000334 return DBuilder.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000335 createNullPtrType(BT->getName(CGM.getContext().getLangOpts()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000336 case BuiltinType::Void:
337 return llvm::DIType();
Devang Patela652fab2010-07-28 01:33:15 +0000338 case BuiltinType::ObjCClass:
Eric Christophere908a7a2012-02-20 18:05:04 +0000339 return DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher8678a4a2012-02-20 23:02:36 +0000340 "objc_class", getOrCreateMainFile(),
341 0);
Devang Patela652fab2010-07-28 01:33:15 +0000342 case BuiltinType::ObjCId: {
343 // typedef struct objc_class *Class;
344 // typedef struct objc_object {
345 // Class isa;
346 // } *id;
347
Eric Christopher55b2bfa2012-02-23 00:43:12 +0000348 // TODO: Cache these two types to avoid duplicates.
Eric Christophere908a7a2012-02-20 18:05:04 +0000349 llvm::DIType OCTy =
350 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher8678a4a2012-02-20 23:02:36 +0000351 "objc_class", getOrCreateMainFile(),
352 0);
Devang Patela652fab2010-07-28 01:33:15 +0000353 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
354
Devang Pateld7185b72011-02-22 18:56:36 +0000355 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patela652fab2010-07-28 01:33:15 +0000356
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000357 SmallVector<llvm::Value *, 16> EltTys;
Devang Patela652fab2010-07-28 01:33:15 +0000358 llvm::DIType FieldTy =
Devang Patel15013e72011-06-24 22:00:59 +0000359 DBuilder.createMemberType(getOrCreateMainFile(), "isa",
360 getOrCreateMainFile(), 0, Size,
361 0, 0, 0, ISATy);
Devang Patela652fab2010-07-28 01:33:15 +0000362 EltTys.push_back(FieldTy);
Jay Foaddbf81d82011-04-24 10:11:03 +0000363 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patela652fab2010-07-28 01:33:15 +0000364
Devang Pateld7185b72011-02-22 18:56:36 +0000365 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel00afcbe2010-12-08 22:42:58 +0000366 getOrCreateMainFile(),
367 0, 0, 0, 0, Elements);
Devang Patela652fab2010-07-28 01:33:15 +0000368 }
Devang Patel19ba2b42011-02-09 03:15:05 +0000369 case BuiltinType::ObjCSel: {
Eric Christophere908a7a2012-02-20 18:05:04 +0000370 return
371 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
Eric Christopher8678a4a2012-02-20 23:02:36 +0000372 "objc_selector", getOrCreateMainFile(),
373 0);
Devang Patel19ba2b42011-02-09 03:15:05 +0000374 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000375 case BuiltinType::UChar:
376 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
377 case BuiltinType::Char_S:
378 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patel98ca8ae2011-09-12 17:11:58 +0000379 case BuiltinType::Char16:
380 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattneraffb3732008-11-10 06:08:34 +0000381 case BuiltinType::UShort:
382 case BuiltinType::UInt:
Devang Patel979aba52011-05-05 17:06:30 +0000383 case BuiltinType::UInt128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000384 case BuiltinType::ULong:
Devang Patel964d7582011-09-10 00:44:49 +0000385 case BuiltinType::WChar_U:
Chris Lattneraffb3732008-11-10 06:08:34 +0000386 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
387 case BuiltinType::Short:
388 case BuiltinType::Int:
Devang Patel979aba52011-05-05 17:06:30 +0000389 case BuiltinType::Int128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000390 case BuiltinType::Long:
Devang Patel964d7582011-09-10 00:44:49 +0000391 case BuiltinType::WChar_S:
Chris Lattneraffb3732008-11-10 06:08:34 +0000392 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
393 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000394 case BuiltinType::Half:
Chris Lattneraffb3732008-11-10 06:08:34 +0000395 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000396 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000397 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000398 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000399
400 switch (BT->getKind()) {
401 case BuiltinType::Long: BTName = "long int"; break;
402 case BuiltinType::LongLong: BTName = "long long int"; break;
403 case BuiltinType::ULong: BTName = "long unsigned int"; break;
404 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
405 default:
David Blaikiebbafb8a2012-03-11 07:00:24 +0000406 BTName = BT->getName(CGM.getContext().getLangOpts());
Devang Patelc7f16ab2010-07-28 23:23:29 +0000407 break;
408 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000409 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000410 uint64_t Size = CGM.getContext().getTypeSize(BT);
411 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patele21912d2009-10-20 19:55:01 +0000412 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000413 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000414 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000415}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000416
Devang Patel4591f772010-12-09 00:25:29 +0000417llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000418 // Bit size, align and offset of the type.
419 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
420 if (Ty->isComplexIntegerType())
421 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000422
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000423 uint64_t Size = CGM.getContext().getTypeSize(Ty);
424 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patele21912d2009-10-20 19:55:01 +0000425 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000426 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel00afcbe2010-12-08 22:42:58 +0000427
Devang Patele21912d2009-10-20 19:55:01 +0000428 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000429}
430
John McCall0cf15512009-09-25 01:40:47 +0000431/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000432/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000433llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000434 QualifierCollector Qc;
435 const Type *T = Qc.strip(Ty);
436
437 // Ignore these qualifiers for now.
438 Qc.removeObjCGCAttr();
439 Qc.removeAddressSpace();
John McCall31168b02011-06-15 23:02:42 +0000440 Qc.removeObjCLifetime();
John McCall0cf15512009-09-25 01:40:47 +0000441
Chris Lattneraffb3732008-11-10 06:08:34 +0000442 // We will create one Derived type for one qualifier and recurse to handle any
443 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000444 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000445 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000446 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000447 Qc.removeConst();
448 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000449 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000450 Qc.removeVolatile();
451 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000452 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000453 Qc.removeRestrict();
454 } else {
455 assert(Qc.empty() && "Unknown type qualifier for debug info");
456 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
John McCall717d9b02010-12-10 11:01:00 +0000459 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCall0cf15512009-09-25 01:40:47 +0000460
Daniel Dunbara290ded2008-10-31 03:54:29 +0000461 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
462 // CVR derived types.
Devang Pateld7185b72011-02-22 18:56:36 +0000463 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel00afcbe2010-12-08 22:42:58 +0000464
Devang Patele21912d2009-10-20 19:55:01 +0000465 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000466}
467
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000468llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000469 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000470 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000471 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
472 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000473 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000474}
475
Chris Lattneraffb3732008-11-10 06:08:34 +0000476llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000477 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000478 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
479 Ty->getPointeeType(), Unit);
480}
481
Eric Christopher000b14e2012-01-25 02:06:59 +0000482// Creates a forward declaration for a RecordDecl in the given context.
483llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
Devang Patel00fca3a2012-02-08 00:10:20 +0000484 llvm::DIDescriptor Ctx) {
Eric Christopher000b14e2012-01-25 02:06:59 +0000485 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
486 unsigned Line = getLineNumber(RD->getLocation());
Eric Christopherfe525232012-02-13 15:08:45 +0000487 StringRef RDName = RD->getName();
488
489 // Get the tag.
Eric Christopher000b14e2012-01-25 02:06:59 +0000490 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Eric Christopherfe525232012-02-13 15:08:45 +0000491 unsigned Tag = 0;
492 if (CXXDecl) {
493 RDName = getClassName(RD);
494 Tag = llvm::dwarf::DW_TAG_class_type;
495 }
Eric Christopher000b14e2012-01-25 02:06:59 +0000496 else if (RD->isStruct())
Eric Christopherfe525232012-02-13 15:08:45 +0000497 Tag = llvm::dwarf::DW_TAG_structure_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000498 else if (RD->isUnion())
Eric Christopherfe525232012-02-13 15:08:45 +0000499 Tag = llvm::dwarf::DW_TAG_union_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000500 else
501 llvm_unreachable("Unknown RecordDecl type!");
Eric Christopherfe525232012-02-13 15:08:45 +0000502
503 // Create the type.
Eric Christopher1a0c8822012-02-18 00:50:14 +0000504 return DBuilder.createForwardDecl(Tag, RDName, DefUnit, Line);
Eric Christopher000b14e2012-01-25 02:06:59 +0000505}
506
Eric Christopher45c4d472012-01-20 22:10:15 +0000507// Walk up the context chain and create forward decls for record decls,
508// and normal descriptors for namespaces.
509llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
510 if (!Context)
511 return TheCU;
512
513 // See if we already have the parent.
514 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
515 I = RegionMap.find(Context);
516 if (I != RegionMap.end())
517 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
518
519 // Check namespace.
520 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
521 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
522
523 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
524 if (!RD->isDependentType()) {
Eric Christopher4c006e52012-02-16 22:54:45 +0000525 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
526 getOrCreateMainFile());
Eric Christopher45c4d472012-01-20 22:10:15 +0000527 return llvm::DIDescriptor(Ty);
528 }
529 }
530 return TheCU;
531}
532
Eric Christopher47300ad2011-09-13 23:45:09 +0000533/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patel91bbb552010-09-30 19:05:55 +0000534/// then emit record's fwd if debug info size reduction is enabled.
535llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
536 llvm::DIFile Unit) {
537 if (!CGM.getCodeGenOpts().LimitDebugInfo)
538 return getOrCreateType(PointeeTy, Unit);
Devang Patel242ce912011-10-24 23:15:17 +0000539
540 // Limit debug info for the pointee type.
541
Eric Christophercd888132011-12-16 23:40:18 +0000542 // If we have an existing type, use that, it's still smaller than creating
543 // a new type.
544 llvm::DIType Ty = getTypeOrNull(PointeeTy);
545 if (Ty.Verify()) return Ty;
546
Devang Patel242ce912011-10-24 23:15:17 +0000547 // Handle qualifiers.
548 if (PointeeTy.hasLocalQualifiers())
549 return CreateQualifiedType(PointeeTy, Unit);
550
Devang Patel91bbb552010-09-30 19:05:55 +0000551 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
552 RecordDecl *RD = RTy->getDecl();
Devang Patel91bbb552010-09-30 19:05:55 +0000553 llvm::DIDescriptor FDContext =
John McCall147d0212011-02-22 22:38:33 +0000554 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Eric Christopher66562a42012-02-20 18:05:24 +0000555 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
556 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy;
557 return RetTy;
Devang Patel91bbb552010-09-30 19:05:55 +0000558 }
559 return getOrCreateType(PointeeTy, Unit);
Eric Christopher8a41bd82012-02-13 14:56:11 +0000560
Devang Patel91bbb552010-09-30 19:05:55 +0000561}
562
Anders Carlsson443f6772009-11-06 19:19:55 +0000563llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
564 const Type *Ty,
565 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000566 llvm::DIFile Unit) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000567 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Pateld7185b72011-02-22 18:56:36 +0000568 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel00afcbe2010-12-08 22:42:58 +0000569
Sanjiv Gupta98070572008-05-25 05:15:42 +0000570 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000571 // Size is always the size of a pointer. We can't use getTypeSize here
572 // because that does not return the correct value for references.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000573 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregore8bbc122011-09-02 00:18:52 +0000574 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000575 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000576
Nick Lewycky16790352011-11-10 00:34:02 +0000577 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit),
578 Size, Align);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000579}
580
Mike Stump31f099c2009-05-14 02:03:51 +0000581llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000582 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000583 if (BlockLiteralGenericSet)
584 return BlockLiteralGeneric;
585
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000586 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump31f099c2009-05-14 02:03:51 +0000587 llvm::DIType FieldTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000588 QualType FType;
589 uint64_t FieldSize, FieldOffset;
590 unsigned FieldAlign;
Mike Stump31f099c2009-05-14 02:03:51 +0000591 llvm::DIArray Elements;
592 llvm::DIType EltTy, DescTy;
593
594 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000595 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000596 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
597 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000598
Jay Foaddbf81d82011-04-24 10:11:03 +0000599 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000600 EltTys.clear();
601
Devang Pateldb2732a2010-09-29 21:05:52 +0000602 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000603 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000604
Devang Pateld7185b72011-02-22 18:56:36 +0000605 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel00afcbe2010-12-08 22:42:58 +0000606 Unit, LineNo, FieldOffset, 0,
607 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000608
Mike Stump31f099c2009-05-14 02:03:51 +0000609 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000610 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000611
Devang Pateld7185b72011-02-22 18:56:36 +0000612 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000613
614 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000615 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000616 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000617 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000618 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
619 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000620 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000621 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000622
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000623 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000624 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000625 FieldSize = CGM.getContext().getTypeSize(Ty);
626 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel15013e72011-06-24 22:00:59 +0000627 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000628 LineNo, FieldSize, FieldAlign,
629 FieldOffset, 0, FieldTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000630 EltTys.push_back(FieldTy);
631
632 FieldOffset += FieldSize;
Jay Foaddbf81d82011-04-24 10:11:03 +0000633 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000634
Devang Pateld7185b72011-02-22 18:56:36 +0000635 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel00afcbe2010-12-08 22:42:58 +0000636 Unit, LineNo, FieldOffset, 0,
637 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000638
Mike Stump31f099c2009-05-14 02:03:51 +0000639 BlockLiteralGenericSet = true;
Devang Pateld7185b72011-02-22 18:56:36 +0000640 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000641 return BlockLiteralGeneric;
642}
643
Nick Lewycky16790352011-11-10 00:34:02 +0000644llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000645 // Typedefs are derived from some other type. If we have a typedef of a
646 // typedef, make sure to emit the whole chain.
647 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel00afcbe2010-12-08 22:42:58 +0000648 if (!Src.Verify())
649 return llvm::DIType();
Chris Lattneraffb3732008-11-10 06:08:34 +0000650 // We don't set size information, but do specify where the typedef was
651 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000652 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000653 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher4c006e52012-02-16 22:54:45 +0000654
Nick Lewycky16790352011-11-10 00:34:02 +0000655 llvm::DIDescriptor TypedefContext =
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000656 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher4c006e52012-02-16 22:54:45 +0000657
658 return
Nick Lewycky16790352011-11-10 00:34:02 +0000659 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000660}
661
Chris Lattneraffb3732008-11-10 06:08:34 +0000662llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000663 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000664 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000665
Chris Lattneraffb3732008-11-10 06:08:34 +0000666 // Add the result type at least.
667 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattneraffb3732008-11-10 06:08:34 +0000669 // Set up remainder of arguments if there is a prototype.
670 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patel284fa412010-10-06 20:51:45 +0000671 if (isa<FunctionNoProtoType>(Ty))
Devang Pateld7185b72011-02-22 18:56:36 +0000672 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patel284fa412010-10-06 20:51:45 +0000673 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Eric Christopher8a41bd82012-02-13 14:56:11 +0000674 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
675 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta98070572008-05-25 05:15:42 +0000676 }
677
Jay Foaddbf81d82011-04-24 10:11:03 +0000678 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Mike Stump11289f42009-09-09 15:08:12 +0000679
Devang Pateld7185b72011-02-22 18:56:36 +0000680 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000681 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000682}
683
Eric Christopher8a41bd82012-02-13 14:56:11 +0000684
Eric Christophera15e6352012-01-26 01:57:13 +0000685void CGDebugInfo::
686CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) {
687
688 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
689 I != E; ++I)
690 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
691 if (V->getInit()) {
692 const APValue *Value = V->evaluateValue();
693 if (Value && Value->isInt()) {
694 llvm::ConstantInt *CI
695 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
696
697 // Create the descriptor for static variable.
698 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
699 StringRef VName = V->getName();
700 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
701 // Do not use DIGlobalVariable for enums.
702 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
703 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
704 getLineNumber(V->getLocation()),
705 VTy, true, CI);
706 }
707 }
708 }
709 }
710}
711
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000712llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall147d0212011-02-22 22:38:33 +0000713 QualType type,
Richard Smithcaf33902011-10-10 18:28:20 +0000714 uint64_t sizeInBitsOverride,
John McCall147d0212011-02-22 22:38:33 +0000715 SourceLocation loc,
716 AccessSpecifier AS,
717 uint64_t offsetInBits,
Devang Patel15013e72011-06-24 22:00:59 +0000718 llvm::DIFile tunit,
719 llvm::DIDescriptor scope) {
John McCall147d0212011-02-22 22:38:33 +0000720 llvm::DIType debugType = getOrCreateType(type, tunit);
721
722 // Get the location for the field.
723 llvm::DIFile file = getOrCreateFile(loc);
724 unsigned line = getLineNumber(loc);
725
726 uint64_t sizeInBits = 0;
727 unsigned alignInBits = 0;
728 if (!type->isIncompleteArrayType()) {
729 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
730
Richard Smithcaf33902011-10-10 18:28:20 +0000731 if (sizeInBitsOverride)
732 sizeInBits = sizeInBitsOverride;
John McCall147d0212011-02-22 22:38:33 +0000733 }
734
735 unsigned flags = 0;
736 if (AS == clang::AS_private)
737 flags |= llvm::DIDescriptor::FlagPrivate;
738 else if (AS == clang::AS_protected)
739 flags |= llvm::DIDescriptor::FlagProtected;
740
Devang Patel15013e72011-06-24 22:00:59 +0000741 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
742 alignInBits, offsetInBits, flags, debugType);
John McCall147d0212011-02-22 22:38:33 +0000743}
744
Devang Patel889ce762010-01-19 00:00:59 +0000745/// CollectRecordFields - A helper function to collect debug info for
746/// record fields. This is used while creating debug info entry for a Record.
747void CGDebugInfo::
John McCall147d0212011-02-22 22:38:33 +0000748CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000749 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel15013e72011-06-24 22:00:59 +0000750 llvm::DIType RecordTy) {
John McCall147d0212011-02-22 22:38:33 +0000751 unsigned fieldNo = 0;
752 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
Eric Christopher4b6753c2012-03-01 21:36:52 +0000753 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(record);
754
755 // For C++11 Lambdas a Fields will be the same as a Capture, but the Capture
756 // has the name and the location of the variable so we should iterate over
757 // both concurrently.
758 if (CXXDecl && CXXDecl->isLambda()) {
759 RecordDecl::field_iterator Field = CXXDecl->field_begin();
760 unsigned fieldno = 0;
761 for (CXXRecordDecl::capture_const_iterator I = CXXDecl->captures_begin(),
762 E = CXXDecl->captures_end(); I != E; ++I, ++Field, ++fieldno) {
763 const LambdaExpr::Capture C = *I;
764 // TODO: Need to handle 'this' in some way by probably renaming the
765 // this of the lambda class and having a field member of 'this'.
766 if (C.capturesVariable()) {
767 VarDecl *V = C.getCapturedVar();
768 llvm::DIFile VUnit = getOrCreateFile(C.getLocation());
769 StringRef VName = V->getName();
770 uint64_t SizeInBitsOverride = 0;
771 if (Field->isBitField()) {
772 SizeInBitsOverride = Field->getBitWidthValue(CGM.getContext());
773 assert(SizeInBitsOverride && "found named 0-width bitfield");
774 }
775 llvm::DIType fieldType
776 = createFieldType(VName, Field->getType(), SizeInBitsOverride, C.getLocation(),
777 Field->getAccess(), layout.getFieldOffset(fieldno),
778 VUnit, RecordTy);
779 elements.push_back(fieldType);
780 }
781 }
782 } else {
783 bool IsMsStruct = record->hasAttr<MsStructAttr>();
784 const FieldDecl *LastFD = 0;
785 for (RecordDecl::field_iterator I = record->field_begin(),
786 E = record->field_end();
787 I != E; ++I, ++fieldNo) {
788 FieldDecl *field = *I;
789
790 if (IsMsStruct) {
791 // Zero-length bitfields following non-bitfield members are ignored
792 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
793 --fieldNo;
794 continue;
795 }
796 LastFD = field;
797 }
798
799 StringRef name = field->getName();
800 QualType type = field->getType();
801
802 // Ignore unnamed fields unless they're anonymous structs/unions.
803 if (name.empty() && !type->isRecordType()) {
804 LastFD = field;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000805 continue;
806 }
Eric Christopher4b6753c2012-03-01 21:36:52 +0000807
808 uint64_t SizeInBitsOverride = 0;
809 if (field->isBitField()) {
810 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
811 assert(SizeInBitsOverride && "found named 0-width bitfield");
812 }
813
814 llvm::DIType fieldType
815 = createFieldType(name, type, SizeInBitsOverride,
816 field->getLocation(), field->getAccess(),
817 layout.getFieldOffset(fieldNo), tunit, RecordTy);
818
819 elements.push_back(fieldType);
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000820 }
Devang Patel889ce762010-01-19 00:00:59 +0000821 }
822}
823
Devang Patel3d4e6d92010-01-28 00:28:01 +0000824/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
825/// function type is not updated to include implicit "this" pointer. Use this
826/// routine to get a method type which includes "this" pointer.
827llvm::DIType
828CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000829 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000830 llvm::DIType FnTy
831 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
832 0),
833 Unit);
Eric Christopher947bb5a2012-03-13 23:40:48 +0000834
Devang Patel3d4e6d92010-01-28 00:28:01 +0000835 // Add "this" pointer.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000836 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000837 assert (Args.getNumElements() && "Invalid number of arguments!");
838
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000839 SmallVector<llvm::Value *, 16> Elts;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000840
841 // First element is always return type. For 'void' functions it is NULL.
842 Elts.push_back(Args.getElement(0));
843
Eric Christopher19329c42011-09-14 01:10:50 +0000844 if (!Method->isStatic()) {
845 // "this" pointer is always first argument.
846 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patelfa59ac32011-10-28 21:12:13 +0000847
848 const CXXRecordDecl *RD = Method->getParent();
849 if (isa<ClassTemplateSpecializationDecl>(RD)) {
850 // Create pointer type directly in this case.
851 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
852 QualType PointeeTy = ThisPtrTy->getPointeeType();
853 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
854 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
855 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Nick Lewycky2219ef02011-11-09 04:25:21 +0000856 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher39e39c82012-02-09 07:26:21 +0000857 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align);
Devang Patelfa59ac32011-10-28 21:12:13 +0000858 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000859 // TODO: This and the artificial type below are misleading, the
860 // types aren't artificial the argument is, but the current
861 // metadata doesn't represent that.
862 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000863 Elts.push_back(ThisPtrType);
864 } else {
Eric Christopher39e39c82012-02-09 07:26:21 +0000865 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Devang Patelfa59ac32011-10-28 21:12:13 +0000866 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000867 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000868 Elts.push_back(ThisPtrType);
869 }
Eric Christopher19329c42011-09-14 01:10:50 +0000870 }
Devang Patel3d4e6d92010-01-28 00:28:01 +0000871
872 // Copy rest of the arguments.
873 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
874 Elts.push_back(Args.getElement(i));
875
Jay Foaddbf81d82011-04-24 10:11:03 +0000876 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000877
Devang Pateld7185b72011-02-22 18:56:36 +0000878 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000879}
880
Devang Patelf79199d2010-10-22 17:11:50 +0000881/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
882/// inside a function.
883static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
Nick Lewycky16790352011-11-10 00:34:02 +0000884 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000885 return isFunctionLocalClass(NRD);
Nick Lewycky16790352011-11-10 00:34:02 +0000886 if (isa<FunctionDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000887 return true;
888 return false;
Devang Patelf79199d2010-10-22 17:11:50 +0000889}
Nick Lewycky2219ef02011-11-09 04:25:21 +0000890
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000891/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
892/// a single member function GlobalDecl.
893llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000894CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000895 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000896 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000897 bool IsCtorOrDtor =
898 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
899
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000900 StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000901 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000902
903 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
904 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000905 StringRef MethodLinkageName;
Devang Patelf79199d2010-10-22 17:11:50 +0000906 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlssonea836bc2010-06-22 16:16:50 +0000907 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000908
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000909 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000910 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
911 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000912
913 // Collect virtual method info.
914 llvm::DIType ContainingType;
915 unsigned Virtuality = 0;
916 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000917
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000918 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000919 if (Method->isPure())
920 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
921 else
922 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
923
924 // It doesn't make sense to give a virtual destructor a vtable index,
925 // since a single destructor has two entries in the vtable.
926 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbournea8341662011-09-26 01:56:30 +0000927 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000928 ContainingType = RecordTy;
929 }
930
Devang Pateldb2732a2010-09-29 21:05:52 +0000931 unsigned Flags = 0;
932 if (Method->isImplicit())
933 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel330b65e2010-09-29 21:46:16 +0000934 AccessSpecifier Access = Method->getAccess();
935 if (Access == clang::AS_private)
936 Flags |= llvm::DIDescriptor::FlagPrivate;
937 else if (Access == clang::AS_protected)
938 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000939 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
940 if (CXXC->isExplicit())
941 Flags |= llvm::DIDescriptor::FlagExplicit;
942 } else if (const CXXConversionDecl *CXXC =
943 dyn_cast<CXXConversionDecl>(Method)) {
944 if (CXXC->isExplicit())
945 Flags |= llvm::DIDescriptor::FlagExplicit;
946 }
Devang Patel251f8592010-10-07 22:03:49 +0000947 if (Method->hasPrototype())
948 Flags |= llvm::DIDescriptor::FlagPrototyped;
Eric Christopher947bb5a2012-03-13 23:40:48 +0000949
950 llvm::DIArray TParamsArray = CollectFunctionTemplateParams(Method, Unit);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000951 llvm::DISubprogram SP =
Nick Lewycky0112b112011-09-01 21:49:51 +0000952 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +0000953 MethodDefUnit, MethodLine,
954 MethodTy, /*isLocalToUnit=*/false,
955 /* isDefinition=*/ false,
956 Virtuality, VIndex, ContainingType,
Eric Christopher947bb5a2012-03-13 23:40:48 +0000957 Flags, CGM.getLangOpts().Optimize, NULL,
958 TParamsArray);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000959
Eric Christopher459532e2011-11-17 23:45:00 +0000960 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000961
962 return SP;
963}
964
Devang Patel7a12ad02010-01-19 01:54:44 +0000965/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherffbc4d02012-01-12 01:26:51 +0000966/// C++ member functions. This is used while creating debug info entry for
Devang Patel7a12ad02010-01-19 01:54:44 +0000967/// a Record.
968void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000969CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000970 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000971 llvm::DIType RecordTy) {
Eric Christopher947bb5a2012-03-13 23:40:48 +0000972
973 // Since we want more than just the individual member decls if we
974 // have templated functions iterate over every declaration to gather
975 // the functions.
976 for(DeclContext::decl_iterator I = RD->decls_begin(),
977 E = RD->decls_end(); I != E; ++I) {
978 Decl *D = *I;
979 if (D->isImplicit() && !D->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000980 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000981
Eric Christopher947bb5a2012-03-13 23:40:48 +0000982 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
983 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
984 else if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
985 for (FunctionTemplateDecl::spec_iterator SI = FTD->spec_begin(),
986 SE = FTD->spec_end(); SI != SE; ++SI) {
987 FunctionDecl *FD = *SI;
988 if (CXXMethodDecl *M = dyn_cast<CXXMethodDecl>(FD))
989 EltTys.push_back(CreateCXXMemberFunction(M, Unit, RecordTy));
990 }
Devang Patel7a12ad02010-01-19 01:54:44 +0000991 }
992}
993
Devang Patel96b7f552010-08-27 17:47:47 +0000994/// CollectCXXFriends - A helper function to collect debug info for
995/// C++ base classes. This is used while creating debug info entry for
996/// a Record.
997void CGDebugInfo::
998CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000999 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel96b7f552010-08-27 17:47:47 +00001000 llvm::DIType RecordTy) {
Eric Christopher8c363622012-01-12 01:26:58 +00001001 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
Devang Patel96b7f552010-08-27 17:47:47 +00001002 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky0112b112011-09-01 21:49:51 +00001003 if ((*BI)->isUnsupportedFriend())
1004 continue;
Devang Patel00afcbe2010-12-08 22:42:58 +00001005 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Pateld7185b72011-02-22 18:56:36 +00001006 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +00001007 getOrCreateType(TInfo->getType(),
1008 Unit)));
Devang Patel96b7f552010-08-27 17:47:47 +00001009 }
1010}
1011
Devang Patelc54353d2010-01-25 23:32:18 +00001012/// CollectCXXBases - A helper function to collect debug info for
1013/// C++ base classes. This is used while creating debug info entry for
1014/// a Record.
1015void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +00001016CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001017 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +00001018 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +00001019
Devang Patel1c0954c2010-02-01 21:39:52 +00001020 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1021 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
1022 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +00001023 unsigned BFlags = 0;
Devang Patel84852bb2011-04-04 20:36:06 +00001024 uint64_t BaseOffset;
Devang Patel128aa9d2010-01-28 21:54:15 +00001025
1026 const CXXRecordDecl *Base =
1027 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
1028
1029 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +00001030 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +00001031 // expression where it expects +ve number.
Ken Dyckbb4e9772011-04-07 12:37:09 +00001032 BaseOffset =
Peter Collingbournea8341662011-09-26 01:56:30 +00001033 0 - CGM.getVTableContext()
1034 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Pateldb2732a2010-09-29 21:05:52 +00001035 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patel128aa9d2010-01-28 21:54:15 +00001036 } else
Devang Patel84852bb2011-04-04 20:36:06 +00001037 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Ken Dyckbb4e9772011-04-07 12:37:09 +00001038 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
1039 // BI->isVirtual() and bits when not.
Devang Patel128aa9d2010-01-28 21:54:15 +00001040
1041 AccessSpecifier Access = BI->getAccessSpecifier();
1042 if (Access == clang::AS_private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001043 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel128aa9d2010-01-28 21:54:15 +00001044 else if (Access == clang::AS_protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001045 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patel128aa9d2010-01-28 21:54:15 +00001046
Devang Patel00afcbe2010-12-08 22:42:58 +00001047 llvm::DIType DTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001048 DBuilder.createInheritance(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +00001049 getOrCreateType(BI->getType(), Unit),
Devang Patel84852bb2011-04-04 20:36:06 +00001050 BaseOffset, BFlags);
Devang Patel128aa9d2010-01-28 21:54:15 +00001051 EltTys.push_back(DTy);
1052 }
Devang Patelc54353d2010-01-25 23:32:18 +00001053}
1054
Devang Patelb87c4282011-04-05 22:54:11 +00001055/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel7522abd2011-04-05 17:30:54 +00001056llvm::DIArray CGDebugInfo::
Devang Patelb87c4282011-04-05 22:54:11 +00001057CollectTemplateParams(const TemplateParameterList *TPList,
1058 const TemplateArgumentList &TAList,
1059 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001060 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel98d26c92011-04-05 20:15:06 +00001061 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1062 const TemplateArgument &TA = TAList[i];
Devang Patelb87c4282011-04-05 22:54:11 +00001063 const NamedDecl *ND = TPList->getParam(i);
Devang Patel7522abd2011-04-05 17:30:54 +00001064 if (TA.getKind() == TemplateArgument::Type) {
1065 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1066 llvm::DITemplateTypeParameter TTP =
Devang Patel98d26c92011-04-05 20:15:06 +00001067 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel7522abd2011-04-05 17:30:54 +00001068 TemplateParams.push_back(TTP);
1069 } else if (TA.getKind() == TemplateArgument::Integral) {
1070 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel7522abd2011-04-05 17:30:54 +00001071 llvm::DITemplateValueParameter TVP =
Devang Patel98d26c92011-04-05 20:15:06 +00001072 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
1073 TA.getAsIntegral()->getZExtValue());
Devang Patel7522abd2011-04-05 17:30:54 +00001074 TemplateParams.push_back(TVP);
1075 }
1076 }
Jay Foaddbf81d82011-04-24 10:11:03 +00001077 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel7522abd2011-04-05 17:30:54 +00001078}
1079
Devang Patelb87c4282011-04-05 22:54:11 +00001080/// CollectFunctionTemplateParams - A helper function to collect debug
1081/// info for function template parameters.
1082llvm::DIArray CGDebugInfo::
1083CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
Eric Christopherfefafac2011-10-11 23:00:51 +00001084 if (FD->getTemplatedKind() ==
1085 FunctionDecl::TK_FunctionTemplateSpecialization) {
Devang Patelb87c4282011-04-05 22:54:11 +00001086 const TemplateParameterList *TList =
Eric Christopherfefafac2011-10-11 23:00:51 +00001087 FD->getTemplateSpecializationInfo()->getTemplate()
1088 ->getTemplateParameters();
Devang Patelb87c4282011-04-05 22:54:11 +00001089 return
1090 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
1091 }
1092 return llvm::DIArray();
1093}
1094
1095/// CollectCXXTemplateParams - A helper function to collect debug info for
1096/// template parameters.
1097llvm::DIArray CGDebugInfo::
1098CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1099 llvm::DIFile Unit) {
1100 llvm::PointerUnion<ClassTemplateDecl *,
1101 ClassTemplatePartialSpecializationDecl *>
1102 PU = TSpecial->getSpecializedTemplateOrPartial();
1103
1104 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1105 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1106 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1107 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
1108 return CollectTemplateParams(TPList, TAList, Unit);
1109}
1110
Devang Patel84033fb2010-01-28 18:11:52 +00001111/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +00001112llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001113 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +00001114 return VTablePtrType;
1115
1116 ASTContext &Context = CGM.getContext();
1117
1118 /* Function type */
Devang Patel00afcbe2010-12-08 22:42:58 +00001119 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foaddbf81d82011-04-24 10:11:03 +00001120 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Pateld7185b72011-02-22 18:56:36 +00001121 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel84033fb2010-01-28 18:11:52 +00001122 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Pateld7185b72011-02-22 18:56:36 +00001123 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001124 "__vtbl_ptr_type");
Devang Pateld7185b72011-02-22 18:56:36 +00001125 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel84033fb2010-01-28 18:11:52 +00001126 return VTablePtrType;
1127}
1128
Anders Carlsson11e51402010-04-17 20:15:18 +00001129/// getVTableName - Get vtable name for the given Class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001130StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Eric Christopherbfecca32012-01-25 21:47:09 +00001131 // Construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +00001132 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +00001133
1134 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +00001135 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001136 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001137 return StringRef(StrPtr, Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001138}
1139
1140
Anders Carlsson11e51402010-04-17 20:15:18 +00001141/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +00001142/// debug info entry in EltTys vector.
1143void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +00001144CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001145 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +00001146 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +00001147
1148 // If there is a primary base then it will hold vtable info.
1149 if (RL.getPrimaryBase())
1150 return;
1151
1152 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +00001153 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +00001154 return;
1155
1156 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1157 llvm::DIType VPTR
Devang Patel15013e72011-06-24 22:00:59 +00001158 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00001159 0, Size, 0, 0, 0,
1160 getOrCreateVTablePtrType(Unit));
Devang Patel84033fb2010-01-28 18:11:52 +00001161 EltTys.push_back(VPTR);
1162}
1163
Devang Patel91bbb552010-09-30 19:05:55 +00001164/// getOrCreateRecordType - Emit record type's standalone debug info.
1165llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
1166 SourceLocation Loc) {
Nick Lewycky2219ef02011-11-09 04:25:21 +00001167 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel91bbb552010-09-30 19:05:55 +00001168 return T;
1169}
1170
Devang Patel410dc002009-02-25 01:36:11 +00001171/// CreateType - get structure or union type.
Devang Patel283e89d2011-01-17 22:23:07 +00001172llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Patel3efd1472010-02-01 21:52:22 +00001173 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001174
Chris Lattneraffb3732008-11-10 06:08:34 +00001175 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001176 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattneraffb3732008-11-10 06:08:34 +00001178 // Records and classes and unions can all be recursive. To handle them, we
1179 // first generate a debug descriptor for the struct as a forward declaration.
1180 // Then (if it is a definition) we go through and get debug info for all of
1181 // its members. Finally, we create a descriptor for the complete type (which
1182 // may refer to the forward decl if the struct is recursive) and replace all
1183 // uses of the forward declaration with the final definition.
Eric Christopher45c4d472012-01-20 22:10:15 +00001184
Eric Christopher4c006e52012-02-16 22:54:45 +00001185 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit);
Devang Patel8f3f76f2010-07-08 19:56:29 +00001186
Eric Christopher4c006e52012-02-16 22:54:45 +00001187 if (FwdDecl.isForwardDecl())
1188 return FwdDecl;
1189
Devang Patelba4ad7f2010-05-07 18:12:35 +00001190 llvm::MDNode *MN = FwdDecl;
1191 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Eric Christopher4c006e52012-02-16 22:54:45 +00001192
Devang Patel01bb5ce2010-03-11 20:01:48 +00001193 // Push the struct on region stack.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001194 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001195 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001196
Eric Christopher4c006e52012-02-16 22:54:45 +00001197 // Add this to the completed types cache since we're completing it.
1198 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1199
Chris Lattneraffb3732008-11-10 06:08:34 +00001200 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001201 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattneraffb3732008-11-10 06:08:34 +00001202
Eric Christopher8913bd62012-01-26 07:01:04 +00001203 // Note: The split of CXXDecl information here is intentional, the
1204 // gdb tests will depend on a certain ordering at printout. The debug
1205 // information offsets are still correct if we merge them all together
1206 // though.
Devang Patel3efd1472010-02-01 21:52:22 +00001207 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +00001208 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001209 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1210 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
Eric Christopher8913bd62012-01-26 07:01:04 +00001211 }
1212
1213 // Collect static variables with initializers and other fields.
1214 CollectRecordStaticVars(RD, FwdDecl);
1215 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1216 llvm::DIArray TParamsArray;
1217 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001218 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1219 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Devang Patel7522abd2011-04-05 17:30:54 +00001220 if (const ClassTemplateSpecializationDecl *TSpecial
1221 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Eric Christopher57200332012-01-26 06:20:57 +00001222 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit);
Devang Patel00afcbe2010-12-08 22:42:58 +00001223 }
Devang Patelabb44132010-01-28 00:54:21 +00001224
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001225 LexicalBlockStack.pop_back();
Devang Patel00afcbe2010-12-08 22:42:58 +00001226 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1227 RegionMap.find(Ty->getDecl());
1228 if (RI != RegionMap.end())
1229 RegionMap.erase(RI);
1230
Jay Foaddbf81d82011-04-24 10:11:03 +00001231 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher4c006e52012-02-16 22:54:45 +00001232 // FIXME: Magic numbers ahoy! These should be changed when we
1233 // get some enums in llvm/Analysis/DebugInfo.h to refer to
1234 // them.
Eric Christopher9a897052012-02-15 23:51:20 +00001235 if (RD->isUnion())
Eric Christopher4c006e52012-02-16 22:54:45 +00001236 MN->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001237 else if (CXXDecl) {
Eric Christopher4c006e52012-02-16 22:54:45 +00001238 MN->replaceOperandWith(10, Elements);
1239 MN->replaceOperandWith(13, TParamsArray);
Eric Christopher9a897052012-02-15 23:51:20 +00001240 } else
Eric Christopher4c006e52012-02-16 22:54:45 +00001241 MN->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001242
Eric Christopher4c006e52012-02-16 22:54:45 +00001243 RegionMap[Ty->getDecl()] = llvm::WeakVH(MN);
1244 return llvm::DIType(MN);
Chris Lattneraffb3732008-11-10 06:08:34 +00001245}
1246
John McCall8b07ec22010-05-15 11:32:37 +00001247/// CreateType - get objective-c object type.
1248llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1249 llvm::DIFile Unit) {
1250 // Ignore protocols.
1251 return getOrCreateType(Ty->getBaseType(), Unit);
1252}
1253
Devang Patelf4c205b2009-02-26 21:10:26 +00001254/// CreateType - get objective-c interface type.
1255llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001256 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +00001257 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001258 if (!ID)
1259 return llvm::DIType();
Devang Patelf4c205b2009-02-26 21:10:26 +00001260
1261 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +00001262 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001263 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +00001264 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +00001265
Eric Christopherfab289a2011-10-06 00:31:18 +00001266 // If this is just a forward declaration return a special forward-declaration
1267 // debug type since we won't be able to lay out the entire type.
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001268 ObjCInterfaceDecl *Def = ID->getDefinition();
1269 if (!Def) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001270 llvm::DIType FwdDecl =
Eric Christophere908a7a2012-02-20 18:05:04 +00001271 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1272 ID->getName(), DefUnit, Line,
1273 RuntimeLang);
Dan Gohman66427b12010-08-23 21:15:56 +00001274 return FwdDecl;
1275 }
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001276 ID = Def;
Dan Gohman66427b12010-08-23 21:15:56 +00001277
Eric Christopher4c006e52012-02-16 22:54:45 +00001278 // Bit size, align and offset of the type.
1279 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1280 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001281
Eric Christopher4c006e52012-02-16 22:54:45 +00001282 unsigned Flags = 0;
1283 if (ID->getImplementation())
1284 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1285
1286 llvm::DIType RealDecl =
1287 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1288 Line, Size, Align, Flags,
1289 llvm::DIArray(), RuntimeLang);
1290
Eric Christopher7a5fdd82012-02-27 08:23:23 +00001291 // Otherwise, insert it into the CompletedTypeCache so that recursive uses
1292 // will find it and we're emitting the complete type.
1293 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001294 // Push the struct on region stack.
Eric Christopher4c006e52012-02-16 22:54:45 +00001295 llvm::MDNode *MN = RealDecl;
1296 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
1297
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001298 LexicalBlockStack.push_back(FwdDeclNode);
Eric Christopher4c006e52012-02-16 22:54:45 +00001299 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001300
1301 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001302 SmallVector<llvm::Value *, 16> EltTys;
Devang Patelf4c205b2009-02-26 21:10:26 +00001303
Devang Patel3efd1472010-02-01 21:52:22 +00001304 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001305 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001306 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001307 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001308 if (!SClassTy.isValid())
1309 return llvm::DIType();
1310
Mike Stump11289f42009-09-09 15:08:12 +00001311 llvm::DIType InhTag =
Eric Christopher4c006e52012-02-16 22:54:45 +00001312 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Devang Patelc0f58ea2009-03-10 21:30:26 +00001313 EltTys.push_back(InhTag);
1314 }
1315
Devang Patel37a5c952012-02-07 18:40:30 +00001316 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1317 E = ID->prop_end(); I != E; ++I) {
1318 const ObjCPropertyDecl *PD = *I;
1319 llvm::MDNode *PropertyNode =
1320 DBuilder.createObjCProperty(PD->getName(),
Devang Patel56a321d2012-02-07 18:55:08 +00001321 getSelectorName(PD->getGetterName()),
1322 getSelectorName(PD->getSetterName()),
1323 PD->getPropertyAttributes());
Devang Patel37a5c952012-02-07 18:40:30 +00001324 EltTys.push_back(PropertyNode);
1325 }
1326
Devang Patel3efd1472010-02-01 21:52:22 +00001327 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001328 unsigned FieldNo = 0;
Fariborz Jahanian885e9df2010-10-01 00:01:53 +00001329 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanian77890872010-10-11 23:55:47 +00001330 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001331 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001332 if (!FieldTy.isValid())
1333 return llvm::DIType();
1334
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001335 StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001336
Devang Pateldf348f12009-04-27 22:40:36 +00001337 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001338 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001339 continue;
1340
Devang Patelf4c205b2009-02-26 21:10:26 +00001341 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001342 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1343 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001344 QualType FType = Field->getType();
1345 uint64_t FieldSize = 0;
1346 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001347
Devang Patel9f804932009-03-20 18:24:39 +00001348 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001349
Devang Patel9f804932009-03-20 18:24:39 +00001350 // Bit size, align and offset of the type.
Richard Smithcaf33902011-10-10 18:28:20 +00001351 FieldSize = Field->isBitField()
1352 ? Field->getBitWidthValue(CGM.getContext())
1353 : CGM.getContext().getTypeSize(FType);
1354 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001355 }
1356
Eric Christopherfab289a2011-10-06 00:31:18 +00001357 // We can't know the offset of our ivar in the structure if we're using
1358 // the non-fragile abi and the debugger should ignore the value anyways.
1359 // Call it the FieldNo+1 due to how debuggers use the information,
1360 // e.g. negating the value when it needs a lookup in the dynamic table.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001361 uint64_t FieldOffset = CGM.getLangOpts().ObjCNonFragileABI ? FieldNo+1
Eric Christopherfab289a2011-10-06 00:31:18 +00001362 : RL.getFieldOffset(FieldNo);
Mike Stump11289f42009-09-09 15:08:12 +00001363
Devang Patelec4bad52009-03-19 00:23:53 +00001364 unsigned Flags = 0;
1365 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001366 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelec4bad52009-03-19 00:23:53 +00001367 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001368 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Devang Patela21bbb22012-02-04 01:15:04 +00001370 llvm::MDNode *PropertyNode = NULL;
Devang Patel37a5c952012-02-07 18:40:30 +00001371 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Devang Patel5a540652011-09-19 18:54:16 +00001372 if (ObjCPropertyImplDecl *PImpD =
Devang Patel37a5c952012-02-07 18:40:30 +00001373 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1374 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Devang Patel56a321d2012-02-07 18:55:08 +00001375 PropertyNode =
1376 DBuilder.createObjCProperty(PD->getName(),
Devang Patel37a5c952012-02-07 18:40:30 +00001377 getSelectorName(PD->getGetterName()),
1378 getSelectorName(PD->getSetterName()),
1379 PD->getPropertyAttributes());
Devang Patel00fca3a2012-02-08 00:10:20 +00001380 }
Devang Patel37a5c952012-02-07 18:40:30 +00001381 }
Devang Patela21bbb22012-02-04 01:15:04 +00001382 }
Devang Patel9d6c8572011-04-16 00:12:55 +00001383 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1384 FieldLine, FieldSize, FieldAlign,
1385 FieldOffset, Flags, FieldTy,
Devang Patel60fc2422012-02-06 18:20:02 +00001386 PropertyNode);
Devang Patelf4c205b2009-02-26 21:10:26 +00001387 EltTys.push_back(FieldTy);
1388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389
Jay Foaddbf81d82011-04-24 10:11:03 +00001390 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher4c006e52012-02-16 22:54:45 +00001391 RealDecl->replaceOperandWith(10, Elements);
1392
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001393 LexicalBlockStack.pop_back();
Devang Patelf4c205b2009-02-26 21:10:26 +00001394 return RealDecl;
1395}
1396
Nick Lewycky2219ef02011-11-09 04:25:21 +00001397llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001398 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel0b37e792011-04-08 21:56:52 +00001399 int64_t NumElems = Ty->getNumElements();
1400 int64_t LowerBound = 0;
1401 if (NumElems == 0)
1402 // If number of elements are not known then this is an unbounded array.
1403 // Use Low = 1, Hi = 0 to express such arrays.
1404 LowerBound = 1;
1405 else
Devang Patelb4073382010-02-23 22:59:39 +00001406 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001407
Devang Patel0b37e792011-04-08 21:56:52 +00001408 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foaddbf81d82011-04-24 10:11:03 +00001409 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patelb4073382010-02-23 22:59:39 +00001410
1411 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1412 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1413
1414 return
Devang Pateld7185b72011-02-22 18:56:36 +00001415 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001416}
1417
Chris Lattneraffb3732008-11-10 06:08:34 +00001418llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001419 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001420 uint64_t Size;
1421 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001422
1423
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001424 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001425 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001426 Size = 0;
1427 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001428 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001429 } else if (Ty->isIncompleteArrayType()) {
1430 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001431 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patela540f142011-04-04 23:18:38 +00001432 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patel1ffe2342011-04-01 19:02:33 +00001433 Size = 0;
1434 Align = 0;
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001435 } else {
1436 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001437 Size = CGM.getContext().getTypeSize(Ty);
1438 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001439 }
Mike Stump11289f42009-09-09 15:08:12 +00001440
Chris Lattneraffb3732008-11-10 06:08:34 +00001441 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1442 // interior arrays, do we care? Why aren't nested arrays represented the
1443 // obvious/recursive way?
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001444 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattneraffb3732008-11-10 06:08:34 +00001445 QualType EltTy(Ty, 0);
Devang Patelc0601d12010-10-06 18:30:00 +00001446 if (Ty->isIncompleteArrayType())
Chris Lattneraffb3732008-11-10 06:08:34 +00001447 EltTy = Ty->getElementType();
Devang Patelc0601d12010-10-06 18:30:00 +00001448 else {
1449 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Devang Patel0b37e792011-04-08 21:56:52 +00001450 int64_t UpperBound = 0;
1451 int64_t LowerBound = 0;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001452 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Devang Patelc0601d12010-10-06 18:30:00 +00001453 if (CAT->getSize().getZExtValue())
Devang Patel0b37e792011-04-08 21:56:52 +00001454 UpperBound = CAT->getSize().getZExtValue() - 1;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001455 } else
Devang Patel0b37e792011-04-08 21:56:52 +00001456 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1457 // arrays.
1458 LowerBound = 1;
1459
Devang Patelc0601d12010-10-06 18:30:00 +00001460 // FIXME: Verify this is right for VLAs.
Eric Christopherfefafac2011-10-11 23:00:51 +00001461 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
1462 UpperBound));
Devang Patelc0601d12010-10-06 18:30:00 +00001463 EltTy = Ty->getElementType();
1464 }
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001465 }
Mike Stump11289f42009-09-09 15:08:12 +00001466
Jay Foaddbf81d82011-04-24 10:11:03 +00001467 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattneraffb3732008-11-10 06:08:34 +00001468
Devang Patele21912d2009-10-20 19:55:01 +00001469 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001470 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001471 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001472 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001473}
1474
Anders Carlsson443f6772009-11-06 19:19:55 +00001475llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001476 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001477 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1478 Ty, Ty->getPointeeType(), Unit);
1479}
Chris Lattneraffb3732008-11-10 06:08:34 +00001480
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001481llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1482 llvm::DIFile Unit) {
1483 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1484 Ty, Ty->getPointeeType(), Unit);
1485}
1486
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001487llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001488 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001489 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1490 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1491
1492 if (!Ty->getPointeeType()->isFunctionType()) {
1493 // We have a data member pointer type.
1494 return PointerDiffDITy;
1495 }
1496
1497 // We have a member function pointer type. Treat it as a struct with two
1498 // ptrdiff_t members.
1499 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1500
1501 uint64_t FieldOffset = 0;
Devang Patel00afcbe2010-12-08 22:42:58 +00001502 llvm::Value *ElementTypes[2];
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001503
1504 // FIXME: This should probably be a function type instead.
1505 ElementTypes[0] =
Devang Patel15013e72011-06-24 22:00:59 +00001506 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001507 Info.first, Info.second, FieldOffset, 0,
1508 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001509 FieldOffset += Info.first;
1510
1511 ElementTypes[1] =
Devang Patel15013e72011-06-24 22:00:59 +00001512 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001513 Info.first, Info.second, FieldOffset, 0,
1514 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001515
Jay Foaddbf81d82011-04-24 10:11:03 +00001516 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001517
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001518 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel00afcbe2010-12-08 22:42:58 +00001519 U, 0, FieldOffset,
1520 0, 0, Elements);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001521}
1522
Eli Friedman0dfb8892011-10-06 23:00:33 +00001523llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1524 llvm::DIFile U) {
1525 // Ignore the atomic wrapping
1526 // FIXME: What is the correct representation?
1527 return getOrCreateType(Ty->getValueType(), U);
1528}
1529
Devang Patel41c20972010-08-23 22:07:25 +00001530/// CreateEnumType - get enumeration type.
Devang Patel283e89d2011-01-17 22:23:07 +00001531llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1532 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001533 SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel41c20972010-08-23 22:07:25 +00001534
1535 // Create DIEnumerator elements for each enumerator.
1536 for (EnumDecl::enumerator_iterator
1537 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1538 Enum != EnumEnd; ++Enum) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001539 Enumerators.push_back(
Devang Pateld7185b72011-02-22 18:56:36 +00001540 DBuilder.createEnumerator(Enum->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001541 Enum->getInitVal().getZExtValue()));
Devang Patel41c20972010-08-23 22:07:25 +00001542 }
1543
1544 // Return a CompositeType for the enum itself.
Jay Foaddbf81d82011-04-24 10:11:03 +00001545 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel41c20972010-08-23 22:07:25 +00001546
1547 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1548 unsigned Line = getLineNumber(ED->getLocation());
1549 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001550 uint64_t Align = 0;
1551 if (!ED->getTypeForDecl()->isIncompleteType()) {
1552 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1553 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1554 }
Devang Patel1bee63f2010-10-27 23:23:58 +00001555 llvm::DIDescriptor EnumContext =
John McCall147d0212011-02-22 22:38:33 +00001556 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel41c20972010-08-23 22:07:25 +00001557 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001558 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel00afcbe2010-12-08 22:42:58 +00001559 Size, Align, EltArray);
Devang Patel41c20972010-08-23 22:07:25 +00001560 return DbgTy;
1561}
1562
Douglas Gregor0f139a12009-12-21 20:18:30 +00001563static QualType UnwrapTypeForDebugInfo(QualType T) {
1564 do {
1565 QualType LastT = T;
1566 switch (T->getTypeClass()) {
1567 default:
1568 return T;
1569 case Type::TemplateSpecialization:
1570 T = cast<TemplateSpecializationType>(T)->desugar();
1571 break;
John McCall424cec92011-01-19 06:33:43 +00001572 case Type::TypeOfExpr:
1573 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001574 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001575 case Type::TypeOf:
1576 T = cast<TypeOfType>(T)->getUnderlyingType();
1577 break;
1578 case Type::Decltype:
1579 T = cast<DecltypeType>(T)->getUnderlyingType();
1580 break;
Alexis Hunte852b102011-05-24 22:41:36 +00001581 case Type::UnaryTransform:
1582 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1583 break;
John McCall81904512011-01-06 01:58:22 +00001584 case Type::Attributed:
1585 T = cast<AttributedType>(T)->getEquivalentType();
John McCall4223a9e2011-03-04 04:00:19 +00001586 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001587 case Type::Elaborated:
1588 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001589 break;
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001590 case Type::Paren:
1591 T = cast<ParenType>(T)->getInnerType();
1592 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001593 case Type::SubstTemplateTypeParm:
1594 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1595 break;
Anders Carlsson829c4132011-03-06 16:43:04 +00001596 case Type::Auto:
1597 T = cast<AutoType>(T)->getDeducedType();
1598 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001599 }
1600
1601 assert(T != LastT && "Type unwrapping failed to unwrap!");
1602 if (T == LastT)
1603 return T;
1604 } while (true);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001605}
1606
Eric Christophercd888132011-12-16 23:40:18 +00001607/// getType - Get the type from the cache or return null type if it doesn't exist.
1608llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump11289f42009-09-09 15:08:12 +00001609
Douglas Gregor0f139a12009-12-21 20:18:30 +00001610 // Unwrap the type as needed for debug information.
1611 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001612
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001613 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001614 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001615 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001616 if (it != TypeCache.end()) {
1617 // Verify that the debug info still exists.
1618 if (&*it->second)
1619 return llvm::DIType(cast<llvm::MDNode>(it->second));
1620 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001621
Eric Christophercd888132011-12-16 23:40:18 +00001622 return llvm::DIType();
1623}
1624
Eric Christopher4c006e52012-02-16 22:54:45 +00001625/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1626/// doesn't exist.
1627llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1628
1629 // Unwrap the type as needed for debug information.
1630 Ty = UnwrapTypeForDebugInfo(Ty);
1631
1632 // Check for existing entry.
1633 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1634 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1635 if (it != CompletedTypeCache.end()) {
1636 // Verify that the debug info still exists.
1637 if (&*it->second)
1638 return llvm::DIType(cast<llvm::MDNode>(it->second));
1639 }
1640
1641 return llvm::DIType();
1642}
1643
1644
Eric Christophercd888132011-12-16 23:40:18 +00001645/// getOrCreateType - Get the type from the cache or create a new
1646/// one if necessary.
1647llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1648 if (Ty.isNull())
1649 return llvm::DIType();
1650
1651 // Unwrap the type as needed for debug information.
1652 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001653
Eric Christopher4c006e52012-02-16 22:54:45 +00001654 llvm::DIType T = getCompletedTypeOrNull(Ty);
1655
Eric Christopher8a41bd82012-02-13 14:56:11 +00001656 if (T.Verify()) return T;
Eric Christophercd888132011-12-16 23:40:18 +00001657
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001658 // Otherwise create the type.
1659 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher1c3785a2012-02-18 00:50:17 +00001660
1661 llvm::DIType TC = getTypeOrNull(Ty);
1662 if (TC.Verify() && TC.isForwardDecl())
1663 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), TC));
Eric Christopher4c006e52012-02-16 22:54:45 +00001664
Anders Carlsson6037e782009-11-14 20:52:05 +00001665 // And update the type cache.
Eric Christopher4c006e52012-02-16 22:54:45 +00001666 TypeCache[Ty.getAsOpaquePtr()] = Res;
1667
1668 if (!Res.isForwardDecl())
1669 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001670 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001671}
1672
Anders Carlsson6037e782009-11-14 20:52:05 +00001673/// CreateTypeNode - Create a new debug type node.
Nick Lewycky6aad6df2011-11-09 04:27:23 +00001674llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001675 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001676 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001677 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001678
Douglas Gregor0915b432009-12-21 19:57:21 +00001679 const char *Diag = 0;
1680
Sanjiv Gupta98070572008-05-25 05:15:42 +00001681 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001682 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001683#define TYPE(Class, Base)
1684#define ABSTRACT_TYPE(Class, Base)
1685#define NON_CANONICAL_TYPE(Class, Base)
1686#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1687#include "clang/AST/TypeNodes.def"
David Blaikie83d382b2011-09-23 05:06:16 +00001688 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001689
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001690 case Type::ExtVector:
Devang Patelb4073382010-02-23 22:59:39 +00001691 case Type::Vector:
1692 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001693 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001694 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001695 case Type::ObjCObject:
1696 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001697 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001698 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001699 case Type::Builtin:
1700 return CreateType(cast<BuiltinType>(Ty));
1701 case Type::Complex:
1702 return CreateType(cast<ComplexType>(Ty));
1703 case Type::Pointer:
1704 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001705 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001706 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001707 case Type::Typedef:
1708 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001709 case Type::Record:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001710 return CreateType(cast<RecordType>(Ty));
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001711 case Type::Enum:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001712 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattneraffb3732008-11-10 06:08:34 +00001713 case Type::FunctionProto:
1714 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001715 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001716 case Type::ConstantArray:
1717 case Type::VariableArray:
1718 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001719 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001720
1721 case Type::LValueReference:
1722 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001723 case Type::RValueReference:
1724 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001725
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001726 case Type::MemberPointer:
1727 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001728
Eli Friedman0dfb8892011-10-06 23:00:33 +00001729 case Type::Atomic:
1730 return CreateType(cast<AtomicType>(Ty), Unit);
1731
John McCall81904512011-01-06 01:58:22 +00001732 case Type::Attributed:
Douglas Gregor0915b432009-12-21 19:57:21 +00001733 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001734 case Type::Elaborated:
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001735 case Type::Paren:
Douglas Gregor0915b432009-12-21 19:57:21 +00001736 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001737 case Type::TypeOfExpr:
1738 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001739 case Type::Decltype:
Alexis Hunte852b102011-05-24 22:41:36 +00001740 case Type::UnaryTransform:
Richard Smith30482bc2011-02-20 03:19:35 +00001741 case Type::Auto:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001742 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta98070572008-05-25 05:15:42 +00001743 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001744
1745 assert(Diag && "Fall through without a diagnostic?");
David Blaikie9c902b52011-09-25 23:23:43 +00001746 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor0915b432009-12-21 19:57:21 +00001747 "debug information for %0 is not yet supported");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001748 CGM.getDiags().Report(DiagID)
Douglas Gregor0915b432009-12-21 19:57:21 +00001749 << Diag;
1750 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001751}
1752
Eric Christopher4c006e52012-02-16 22:54:45 +00001753/// getOrCreateLimitedType - Get the type from the cache or create a new
1754/// limited type if necessary.
1755llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1756 llvm::DIFile Unit) {
1757 if (Ty.isNull())
1758 return llvm::DIType();
1759
1760 // Unwrap the type as needed for debug information.
1761 Ty = UnwrapTypeForDebugInfo(Ty);
1762
1763 llvm::DIType T = getTypeOrNull(Ty);
1764
1765 // We may have cached a forward decl when we could have created
1766 // a non-forward decl. Go ahead and create a non-forward decl
1767 // now.
1768 if (T.Verify() && !T.isForwardDecl()) return T;
1769
1770 // Otherwise create the type.
1771 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1772
Eric Christopher1c3785a2012-02-18 00:50:17 +00001773 if (T.Verify() && T.isForwardDecl())
1774 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), T));
1775
Eric Christopher4c006e52012-02-16 22:54:45 +00001776 // And update the type cache.
1777 TypeCache[Ty.getAsOpaquePtr()] = Res;
1778 return Res;
1779}
1780
1781// TODO: Currently used for context chains when limiting debug info.
1782llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1783 RecordDecl *RD = Ty->getDecl();
1784
1785 // Get overall information about the record type for the debug info.
1786 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1787 unsigned Line = getLineNumber(RD->getLocation());
1788 StringRef RDName = RD->getName();
1789
1790 llvm::DIDescriptor RDContext;
1791 if (CGM.getCodeGenOpts().LimitDebugInfo)
1792 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1793 else
1794 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1795
1796 // If this is just a forward declaration, construct an appropriately
1797 // marked node and just return it.
Eric Christopher1c3785a2012-02-18 00:50:17 +00001798 if (!RD->getDefinition())
1799 return createRecordFwdDecl(RD, RDContext);
Eric Christopher4c006e52012-02-16 22:54:45 +00001800
1801 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1802 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1803 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1804 llvm::MDNode *RealDecl = NULL;
1805
1806 if (RD->isUnion())
1807 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1808 Size, Align, 0, llvm::DIArray());
1809 else if (CXXDecl) {
1810 RDName = getClassName(RD);
1811
1812 // FIXME: This could be a struct type giving a default visibility different
1813 // than C++ class type, but needs llvm metadata changes first.
1814 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1815 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher66562a42012-02-20 18:05:24 +00001816 llvm::DIArray(), llvm::DIType(),
Eric Christopher4c006e52012-02-16 22:54:45 +00001817 llvm::DIArray());
1818 } else
1819 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1820 Size, Align, 0, llvm::DIArray());
1821
1822 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1823 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1824
1825 if (CXXDecl) {
1826 // A class's primary base or the class itself contains the vtable.
1827 llvm::MDNode *ContainingType = NULL;
1828 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1829 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1830 // Seek non virtual primary base root.
1831 while (1) {
1832 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1833 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1834 if (PBT && !BRL.isPrimaryBaseVirtual())
1835 PBase = PBT;
1836 else
1837 break;
1838 }
1839 ContainingType =
1840 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1841 }
1842 else if (CXXDecl->isDynamicClass())
1843 ContainingType = RealDecl;
1844
Eric Christopher2939e6e2012-02-17 07:09:48 +00001845 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher4c006e52012-02-16 22:54:45 +00001846 }
1847 return llvm::DIType(RealDecl);
1848}
1849
1850/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1851/// declare composite types that haven't been processed yet.
1852llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1853
1854 // Work out details of type.
1855 switch (Ty->getTypeClass()) {
1856#define TYPE(Class, Base)
1857#define ABSTRACT_TYPE(Class, Base)
1858#define NON_CANONICAL_TYPE(Class, Base)
1859#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1860 #include "clang/AST/TypeNodes.def"
1861 llvm_unreachable("Dependent types cannot show up in debug information");
1862
1863 case Type::Record:
1864 return CreateLimitedType(cast<RecordType>(Ty));
1865 default:
1866 return CreateTypeNode(Ty, Unit);
1867 }
1868}
1869
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001870/// CreateMemberType - Create new member and increase Offset by FType's size.
1871llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001872 StringRef Name,
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001873 uint64_t *Offset) {
1874 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1875 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1876 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel15013e72011-06-24 22:00:59 +00001877 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001878 FieldSize, FieldAlign,
1879 *Offset, 0, FieldTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001880 *Offset += FieldSize;
1881 return Ty;
1882}
1883
Devang Patela6cb0642011-04-23 00:08:01 +00001884/// getFunctionDeclaration - Return debug info descriptor to describe method
1885/// declaration for the given method definition.
1886llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1887 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1888 if (!FD) return llvm::DISubprogram();
1889
1890 // Setup context.
1891 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1892
Devang Patela3e3fde2011-04-29 23:42:32 +00001893 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001894 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patela3e3fde2011-04-29 23:42:32 +00001895 if (MI != SPCache.end()) {
1896 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1897 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1898 return SP;
1899 }
1900
Devang Patela6cb0642011-04-23 00:08:01 +00001901 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1902 E = FD->redecls_end(); I != E; ++I) {
1903 const FunctionDecl *NextFD = *I;
1904 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001905 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patela6cb0642011-04-23 00:08:01 +00001906 if (MI != SPCache.end()) {
1907 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1908 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1909 return SP;
1910 }
1911 }
1912 return llvm::DISubprogram();
1913}
1914
Devang Patel2780e452011-05-31 20:46:46 +00001915// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
1916// implicit parameter "this".
Eric Christopherfefafac2011-10-11 23:00:51 +00001917llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D,
1918 QualType FnType,
Devang Patel2780e452011-05-31 20:46:46 +00001919 llvm::DIFile F) {
1920 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1921 return getOrCreateMethodType(Method, F);
Nick Lewycky16790352011-11-10 00:34:02 +00001922 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patel7ce99c32011-05-31 21:18:50 +00001923 // Add "self" and "_cmd"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001924 SmallVector<llvm::Value *, 16> Elts;
Devang Patel7ce99c32011-05-31 21:18:50 +00001925
1926 // First element is always return type. For 'void' functions it is NULL.
Devang Patel5c71c212011-05-31 22:21:11 +00001927 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patel7ce99c32011-05-31 21:18:50 +00001928 // "self" pointer is always first argument.
1929 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F));
1930 // "cmd" pointer is always second argument.
1931 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F));
Devang Patel5c71c212011-05-31 22:21:11 +00001932 // Get rest of the arguments.
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001933 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Patel5c71c212011-05-31 22:21:11 +00001934 PE = OMethod->param_end(); PI != PE; ++PI)
1935 Elts.push_back(getOrCreateType((*PI)->getType(), F));
1936
Devang Patel7ce99c32011-05-31 21:18:50 +00001937 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1938 return DBuilder.createSubroutineType(F, EltTypeArray);
1939 }
Devang Patel2780e452011-05-31 20:46:46 +00001940 return getOrCreateType(FnType, F);
1941}
1942
Sanjiv Gupta98070572008-05-25 05:15:42 +00001943/// EmitFunctionStart - Constructs the debug code for entering a function -
1944/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001945void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001946 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001947 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001948
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001949 StringRef Name;
1950 StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001951
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001952 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel0884a602010-07-22 22:29:16 +00001953
Devang Patel934661e2010-01-14 00:36:21 +00001954 const Decl *D = GD.getDecl();
Eric Christopher7cdf9482011-10-13 21:45:18 +00001955
Devang Patel251f8592010-10-07 22:03:49 +00001956 unsigned Flags = 0;
Devang Patel33ddf692010-10-11 21:58:41 +00001957 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1958 llvm::DIDescriptor FDContext(Unit);
Devang Patelb87c4282011-04-05 22:54:11 +00001959 llvm::DIArray TParamsArray;
Devang Patel934661e2010-01-14 00:36:21 +00001960 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopher04832b92011-11-14 18:55:02 +00001961 // If there is a DISubprogram for this function available then use it.
Devang Patel7a12ad02010-01-19 01:54:44 +00001962 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001963 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel7a12ad02010-01-19 01:54:44 +00001964 if (FI != SPCache.end()) {
Gabor Greifbf986082010-09-18 13:00:17 +00001965 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001966 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1967 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001968 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001969 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001970 return;
1971 }
1972 }
Devang Patel934661e2010-01-14 00:36:21 +00001973 Name = getFunctionName(FD);
1974 // Use mangled name as linkage name for c/c++ functions.
Devang Patel04ab75c2011-05-02 22:49:30 +00001975 if (!Fn->hasInternalLinkage())
Devang Patelb7ff0da2011-05-02 22:37:48 +00001976 LinkageName = CGM.getMangledName(GD);
Devang Patelf79199d2010-10-22 17:11:50 +00001977 if (LinkageName == Name)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001978 LinkageName = StringRef();
Devang Patel251f8592010-10-07 22:03:49 +00001979 if (FD->hasPrototype())
1980 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel33ddf692010-10-11 21:58:41 +00001981 if (const NamespaceDecl *NSDecl =
1982 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel8c445292010-12-09 00:33:05 +00001983 FDContext = getOrCreateNameSpace(NSDecl);
Devang Patelf9076f32011-05-17 00:20:09 +00001984 else if (const RecordDecl *RDecl =
1985 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
1986 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patelb87c4282011-04-05 22:54:11 +00001987
1988 // Collect template parameters.
1989 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
David Chisnall6bf98ff2010-09-02 17:16:32 +00001990 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnallcf607442010-09-02 18:01:51 +00001991 Name = getObjCMethodName(OMD);
Devang Patel251f8592010-10-07 22:03:49 +00001992 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001993 } else {
Devang Patelf79199d2010-10-22 17:11:50 +00001994 // Use llvm function name.
Devang Patel934661e2010-01-14 00:36:21 +00001995 Name = Fn->getName();
Devang Patel251f8592010-10-07 22:03:49 +00001996 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001997 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001998 if (!Name.empty() && Name[0] == '\01')
1999 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002000
Devang Patel84715932010-04-24 00:49:16 +00002001 // It is expected that CurLoc is set before using EmitFunctionStart.
2002 // Usually, CurLoc points to the left bracket location of compound
2003 // statement representing function body.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002004 unsigned LineNo = getLineNumber(CurLoc);
Devang Pateldb2732a2010-09-29 21:05:52 +00002005 if (D->isImplicit())
2006 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patela6cb0642011-04-23 00:08:01 +00002007 llvm::DISubprogram SPDecl = getFunctionDeclaration(D);
Chris Lattneraffb3732008-11-10 06:08:34 +00002008 llvm::DISubprogram SP =
Devang Pateld7185b72011-02-22 18:56:36 +00002009 DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
Devang Patel2780e452011-05-31 20:46:46 +00002010 LineNo, getOrCreateFunctionType(D, FnType, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00002011 Fn->hasInternalLinkage(), true/*definition*/,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002012 Flags, CGM.getLangOpts().Optimize, Fn,
Devang Patela6cb0642011-04-23 00:08:01 +00002013 TParamsArray, SPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002014
Sanjiv Gupta98070572008-05-25 05:15:42 +00002015 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00002016 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002017 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002018 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher4fd315f2011-09-29 00:00:37 +00002019}
Sanjiv Gupta98070572008-05-25 05:15:42 +00002020
Eric Christopherbfa4dc52011-09-29 00:00:41 +00002021/// EmitLocation - Emit metadata to indicate a change in line/column
2022/// information in the source file.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002023void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
2024
2025 // Update our current location
2026 setLocation(Loc);
2027
Sanjiv Gupta98070572008-05-25 05:15:42 +00002028 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00002029
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002030 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002031 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher7cdf9482011-10-13 21:45:18 +00002032 if (CurLoc == PrevLoc ||
Chandler Carruth35f53202011-07-25 16:49:02 +00002033 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patela2c048e2010-04-05 21:09:15 +00002034 // New Builder may not be in sync with CGDebugInfo.
2035 if (!Builder.getCurrentDebugLocation().isUnknown())
2036 return;
Eric Christophere6556572011-09-29 00:00:35 +00002037
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002038 // Update last state.
2039 PrevLoc = CurLoc;
2040
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002041 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00002042 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
2043 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00002044 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002045}
2046
Eric Christopher7cdf9482011-10-13 21:45:18 +00002047/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2048/// the stack.
2049void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patelb40f2952009-11-13 19:10:24 +00002050 llvm::DIDescriptor D =
Eric Christopher7cdf9482011-10-13 21:45:18 +00002051 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel00fca3a2012-02-08 00:10:20 +00002052 llvm::DIDescriptor() :
2053 llvm::DIDescriptor(LexicalBlockStack.back()),
2054 getOrCreateFile(CurLoc),
2055 getLineNumber(CurLoc),
2056 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00002057 llvm::MDNode *DN = D;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002058 LexicalBlockStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002059}
2060
Eric Christopher7cdf9482011-10-13 21:45:18 +00002061/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2062/// region - beginning of a DW_TAG_lexical_block.
2063void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2064 // Set our current location.
2065 setLocation(Loc);
2066
2067 // Create a new lexical block and push it on the stack.
2068 CreateLexicalBlock(Loc);
2069
2070 // Emit a line table change for the current location inside the new scope.
2071 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel00fca3a2012-02-08 00:10:20 +00002072 getColumnNumber(Loc),
2073 LexicalBlockStack.back()));
Eric Christopher7cdf9482011-10-13 21:45:18 +00002074}
2075
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002076/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher9c13eea2011-09-26 15:03:22 +00002077/// region - end of a DW_TAG_lexical_block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002078void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002079 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002080
Eric Christopher7cdf9482011-10-13 21:45:18 +00002081 // Provide an entry in the line table for the end of the block.
2082 EmitLocation(Builder, Loc);
Mike Stump11289f42009-09-09 15:08:12 +00002083
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002084 LexicalBlockStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002085}
2086
Devang Patel0884a602010-07-22 22:29:16 +00002087/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2088void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002089 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel0884a602010-07-22 22:29:16 +00002090 unsigned RCount = FnBeginRegionCount.back();
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002091 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel0884a602010-07-22 22:29:16 +00002092
2093 // Pop all regions for this function.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002094 while (LexicalBlockStack.size() != RCount)
Eric Christopher7cdf9482011-10-13 21:45:18 +00002095 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel0884a602010-07-22 22:29:16 +00002096 FnBeginRegionCount.pop_back();
2097}
2098
Devang Patel535fdaf2010-02-10 18:49:08 +00002099// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2100// See BuildByRefType.
2101llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2102 uint64_t *XOffset) {
2103
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002104 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel535fdaf2010-02-10 18:49:08 +00002105 QualType FType;
2106 uint64_t FieldSize, FieldOffset;
2107 unsigned FieldAlign;
2108
Devang Patel408dcf62010-03-09 00:44:50 +00002109 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002110 QualType Type = VD->getType();
2111
2112 FieldOffset = 0;
2113 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002114 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2115 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002116 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002117 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2118 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2119
John McCall351762c2011-02-07 10:33:21 +00002120 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel535fdaf2010-02-10 18:49:08 +00002121 if (HasCopyAndDispose) {
2122 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002123 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2124 &FieldOffset));
2125 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2126 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002127 }
2128
2129 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002130 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002131 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck8159c1f2011-04-22 17:34:18 +00002132 CharUnits FieldOffsetInBytes
2133 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2134 CharUnits AlignedOffsetInBytes
2135 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2136 CharUnits NumPaddingBytes
2137 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel535fdaf2010-02-10 18:49:08 +00002138
Ken Dyck8159c1f2011-04-22 17:34:18 +00002139 if (NumPaddingBytes.isPositive()) {
2140 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel535fdaf2010-02-10 18:49:08 +00002141 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2142 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002143 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002144 }
2145 }
2146
2147 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002148 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00002149 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002150 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel535fdaf2010-02-10 18:49:08 +00002151
2152 *XOffset = FieldOffset;
Devang Patel15013e72011-06-24 22:00:59 +00002153 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002154 0, FieldSize, FieldAlign,
2155 FieldOffset, 0, FieldTy);
Devang Patel535fdaf2010-02-10 18:49:08 +00002156 EltTys.push_back(FieldTy);
2157 FieldOffset += FieldSize;
2158
Jay Foaddbf81d82011-04-24 10:11:03 +00002159 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel535fdaf2010-02-10 18:49:08 +00002160
Devang Pateldb2732a2010-09-29 21:05:52 +00002161 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel535fdaf2010-02-10 18:49:08 +00002162
Devang Pateld7185b72011-02-22 18:56:36 +00002163 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel00afcbe2010-12-08 22:42:58 +00002164 Elements);
Devang Patel535fdaf2010-02-10 18:49:08 +00002165}
Devang Patel00afcbe2010-12-08 22:42:58 +00002166
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002167/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00002168void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel68a15252011-03-03 20:13:15 +00002169 llvm::Value *Storage,
2170 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002171 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002172
Devang Patel408dcf62010-03-09 00:44:50 +00002173 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002174 llvm::DIType Ty;
2175 uint64_t XOffset = 0;
2176 if (VD->hasAttr<BlocksAttr>())
2177 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2178 else
2179 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00002180
Devang Patel67eba802010-05-07 23:05:55 +00002181 // If there is not any debug info for type then do not emit debug info
2182 // for this variable.
2183 if (!Ty)
2184 return;
2185
Devang Patel25468052011-02-16 01:11:51 +00002186 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2187 // If Storage is an aggregate returned as 'sret' then let debugger know
2188 // about this.
Devang Patel425909d2011-02-10 00:40:52 +00002189 if (Arg->hasStructRetAttr())
Devang Pateld7185b72011-02-22 18:56:36 +00002190 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002191 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2192 // If an aggregate variable has non trivial destructor or non trivial copy
2193 // constructor than it is pass indirectly. Let debug info know about this
2194 // by using reference of the aggregate type as a argument type.
Eric Christopherfefafac2011-10-11 23:00:51 +00002195 if (!Record->hasTrivialCopyConstructor() ||
2196 !Record->hasTrivialDestructor())
Devang Pateld7185b72011-02-22 18:56:36 +00002197 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002198 }
2199 }
Devang Patel425909d2011-02-10 00:40:52 +00002200
Chris Lattneraffb3732008-11-10 06:08:34 +00002201 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002202 unsigned Line = getLineNumber(VD->getLocation());
2203 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patel7c086222010-09-29 23:09:21 +00002204 unsigned Flags = 0;
2205 if (VD->isImplicit())
2206 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002207 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel67f70aa2010-10-12 23:24:54 +00002208
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002209 StringRef Name = VD->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002210 if (!Name.empty()) {
Devang Patelbc474982011-01-11 00:30:27 +00002211 if (VD->hasAttr<BlocksAttr>()) {
2212 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002213 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002214 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002215 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002216 // offset of __forwarding field
Ken Dyckbbe38622011-04-22 17:41:34 +00002217 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002218 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelbc474982011-01-11 00:30:27 +00002219 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002220 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2221 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002222 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002223 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelbc474982011-01-11 00:30:27 +00002224 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2225
2226 // Create the descriptor for the variable.
2227 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002228 DBuilder.createComplexVariable(Tag,
Eric Christopherfefafac2011-10-11 23:00:51 +00002229 llvm::DIDescriptor(Scope),
Devang Patelbc474982011-01-11 00:30:27 +00002230 VD->getName(), Unit, Line, Ty,
Jay Foaddbf81d82011-04-24 10:11:03 +00002231 addr, ArgNo);
Devang Patelbc474982011-01-11 00:30:27 +00002232
2233 // Insert an llvm.dbg.declare into the current block.
2234 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002235 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelbc474982011-01-11 00:30:27 +00002236 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2237 return;
2238 }
2239 // Create the descriptor for the variable.
Devang Patel67f70aa2010-10-12 23:24:54 +00002240 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002241 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel00afcbe2010-12-08 22:42:58 +00002242 Name, Unit, Line, Ty,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002243 CGM.getLangOpts().Optimize, Flags, ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002244
2245 // Insert an llvm.dbg.declare into the current block.
2246 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002247 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel67f70aa2010-10-12 23:24:54 +00002248 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel31d1e212010-10-29 16:21:19 +00002249 return;
Devang Patel67f70aa2010-10-12 23:24:54 +00002250 }
2251
2252 // If VD is an anonymous union then Storage represents value for
2253 // all union fields.
John McCall147d0212011-02-22 22:38:33 +00002254 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2255 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2256 if (RD->isUnion()) {
2257 for (RecordDecl::field_iterator I = RD->field_begin(),
2258 E = RD->field_end();
2259 I != E; ++I) {
2260 FieldDecl *Field = *I;
2261 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002262 StringRef FieldName = Field->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002263
John McCall147d0212011-02-22 22:38:33 +00002264 // Ignore unnamed fields. Do not ignore unnamed records.
2265 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2266 continue;
Devang Patel67f70aa2010-10-12 23:24:54 +00002267
John McCall147d0212011-02-22 22:38:33 +00002268 // Use VarDecl's Tag, Scope and Line number.
2269 llvm::DIVariable D =
2270 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2271 FieldName, Unit, Line, FieldTy,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002272 CGM.getLangOpts().Optimize, Flags,
Devang Patel68a15252011-03-03 20:13:15 +00002273 ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002274
John McCall147d0212011-02-22 22:38:33 +00002275 // Insert an llvm.dbg.declare into the current block.
2276 llvm::Instruction *Call =
2277 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall147d0212011-02-22 22:38:33 +00002278 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel67f70aa2010-10-12 23:24:54 +00002279 }
John McCall147d0212011-02-22 22:38:33 +00002280 }
2281 }
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002282}
2283
Devang Patel4f325d12011-04-25 23:43:36 +00002284void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2285 llvm::Value *Storage,
2286 CGBuilderTy &Builder) {
2287 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2288}
Mike Stump2e722b92009-09-30 02:43:10 +00002289
Devang Patel4f325d12011-04-25 23:43:36 +00002290void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2291 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2292 const CGBlockInfo &blockInfo) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002293 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel4f325d12011-04-25 23:43:36 +00002294
Devang Patel42fb6f82010-04-26 23:28:46 +00002295 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00002296 return;
Devang Patel4f325d12011-04-25 23:43:36 +00002297
John McCall351762c2011-02-07 10:33:21 +00002298 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patel4f325d12011-04-25 23:43:36 +00002299
Mike Stump2e722b92009-09-30 02:43:10 +00002300 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00002301 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002302 llvm::DIType Ty;
John McCall351762c2011-02-07 10:33:21 +00002303 if (isByRef)
Devang Patel535fdaf2010-02-10 18:49:08 +00002304 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2305 else
2306 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00002307
2308 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002309 unsigned Line = getLineNumber(VD->getLocation());
2310 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00002311
John McCall351762c2011-02-07 10:33:21 +00002312 const llvm::TargetData &target = CGM.getTargetData();
2313
2314 CharUnits offset = CharUnits::fromQuantity(
2315 target.getStructLayout(blockInfo.StructureType)
2316 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2317
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002318 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002319 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002320 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattnerbf784782010-01-25 03:29:35 +00002321 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall351762c2011-02-07 10:33:21 +00002322 if (isByRef) {
Devang Patel2d6390d2011-02-18 23:29:22 +00002323 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2324 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002325 // offset of __forwarding field
Eric Christopherfefafac2011-10-11 23:00:51 +00002326 offset = CGM.getContext()
2327 .toCharUnitsFromBits(target.getPointerSizeInBits());
Chris Lattnerbf784782010-01-25 03:29:35 +00002328 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002329 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2330 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002331 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002332 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattnerbf784782010-01-25 03:29:35 +00002333 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00002334 }
2335
2336 // Create the descriptor for the variable.
2337 llvm::DIVariable D =
Devang Patel4f325d12011-04-25 23:43:36 +00002338 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002339 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foaddbf81d82011-04-24 10:11:03 +00002340 VD->getName(), Unit, Line, Ty, addr);
Mike Stump2e722b92009-09-30 02:43:10 +00002341 // Insert an llvm.dbg.declare into the current block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002342 llvm::Instruction *Call =
Devang Patel420c8de2011-04-25 23:52:27 +00002343 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher7cdf9482011-10-13 21:45:18 +00002344 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2345 LexicalBlockStack.back()));
Mike Stump2e722b92009-09-30 02:43:10 +00002346}
2347
Chris Lattneraffb3732008-11-10 06:08:34 +00002348/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2349/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00002350void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel68a15252011-03-03 20:13:15 +00002351 unsigned ArgNo,
Devang Patel25468052011-02-16 01:11:51 +00002352 CGBuilderTy &Builder) {
Devang Patel68a15252011-03-03 20:13:15 +00002353 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00002354}
2355
John McCall147d0212011-02-22 22:38:33 +00002356namespace {
2357 struct BlockLayoutChunk {
2358 uint64_t OffsetInBits;
2359 const BlockDecl::Capture *Capture;
2360 };
2361 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2362 return l.OffsetInBits < r.OffsetInBits;
2363 }
2364}
Chris Lattneraffb3732008-11-10 06:08:34 +00002365
John McCall147d0212011-02-22 22:38:33 +00002366void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2367 llvm::Value *addr,
2368 CGBuilderTy &Builder) {
2369 ASTContext &C = CGM.getContext();
2370 const BlockDecl *blockDecl = block.getBlockDecl();
2371
2372 // Collect some general information about the block's location.
2373 SourceLocation loc = blockDecl->getCaretLocation();
2374 llvm::DIFile tunit = getOrCreateFile(loc);
2375 unsigned line = getLineNumber(loc);
2376 unsigned column = getColumnNumber(loc);
2377
2378 // Build the debug-info type for the block literal.
Nick Lewyckyfc49f722011-05-02 01:41:48 +00002379 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall147d0212011-02-22 22:38:33 +00002380
2381 const llvm::StructLayout *blockLayout =
2382 CGM.getTargetData().getStructLayout(block.StructureType);
2383
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002384 SmallVector<llvm::Value*, 16> fields;
John McCall147d0212011-02-22 22:38:33 +00002385 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2386 blockLayout->getElementOffsetInBits(0),
Devang Patel15013e72011-06-24 22:00:59 +00002387 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002388 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2389 blockLayout->getElementOffsetInBits(1),
Devang Patel15013e72011-06-24 22:00:59 +00002390 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002391 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2392 blockLayout->getElementOffsetInBits(2),
Devang Patel15013e72011-06-24 22:00:59 +00002393 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002394 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2395 blockLayout->getElementOffsetInBits(3),
Devang Patel15013e72011-06-24 22:00:59 +00002396 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002397 fields.push_back(createFieldType("__descriptor",
2398 C.getPointerType(block.NeedsCopyDispose ?
2399 C.getBlockDescriptorExtendedType() :
2400 C.getBlockDescriptorType()),
2401 0, loc, AS_public,
2402 blockLayout->getElementOffsetInBits(4),
Devang Patel15013e72011-06-24 22:00:59 +00002403 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002404
2405 // We want to sort the captures by offset, not because DWARF
2406 // requires this, but because we're paranoid about debuggers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002407 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall147d0212011-02-22 22:38:33 +00002408
2409 // 'this' capture.
2410 if (blockDecl->capturesCXXThis()) {
2411 BlockLayoutChunk chunk;
2412 chunk.OffsetInBits =
2413 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2414 chunk.Capture = 0;
2415 chunks.push_back(chunk);
2416 }
2417
2418 // Variable captures.
2419 for (BlockDecl::capture_const_iterator
2420 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2421 i != e; ++i) {
2422 const BlockDecl::Capture &capture = *i;
2423 const VarDecl *variable = capture.getVariable();
2424 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2425
2426 // Ignore constant captures.
2427 if (captureInfo.isConstant())
2428 continue;
2429
2430 BlockLayoutChunk chunk;
2431 chunk.OffsetInBits =
2432 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2433 chunk.Capture = &capture;
2434 chunks.push_back(chunk);
2435 }
2436
2437 // Sort by offset.
2438 llvm::array_pod_sort(chunks.begin(), chunks.end());
2439
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002440 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall147d0212011-02-22 22:38:33 +00002441 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2442 uint64_t offsetInBits = i->OffsetInBits;
2443 const BlockDecl::Capture *capture = i->Capture;
2444
2445 // If we have a null capture, this must be the C++ 'this' capture.
2446 if (!capture) {
2447 const CXXMethodDecl *method =
2448 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2449 QualType type = method->getThisType(C);
2450
2451 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel15013e72011-06-24 22:00:59 +00002452 offsetInBits, tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002453 continue;
2454 }
2455
2456 const VarDecl *variable = capture->getVariable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002457 StringRef name = variable->getName();
John McCall81a325e2011-03-02 06:57:14 +00002458
2459 llvm::DIType fieldType;
2460 if (capture->isByRef()) {
2461 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2462
2463 // FIXME: this creates a second copy of this type!
2464 uint64_t xoffset;
2465 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2466 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel15013e72011-06-24 22:00:59 +00002467 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCall81a325e2011-03-02 06:57:14 +00002468 ptrInfo.first, ptrInfo.second,
2469 offsetInBits, 0, fieldType);
2470 } else {
2471 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel15013e72011-06-24 22:00:59 +00002472 loc, AS_public, offsetInBits, tunit, tunit);
John McCall81a325e2011-03-02 06:57:14 +00002473 }
2474 fields.push_back(fieldType);
John McCall147d0212011-02-22 22:38:33 +00002475 }
2476
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002477 SmallString<36> typeName;
John McCall147d0212011-02-22 22:38:33 +00002478 llvm::raw_svector_ostream(typeName)
2479 << "__block_literal_" << CGM.getUniqueBlockCount();
2480
Jay Foaddbf81d82011-04-24 10:11:03 +00002481 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall147d0212011-02-22 22:38:33 +00002482
2483 llvm::DIType type =
2484 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2485 CGM.getContext().toBits(block.BlockSize),
2486 CGM.getContext().toBits(block.BlockAlign),
2487 0, fieldsArray);
2488 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2489
2490 // Get overall information about the block.
2491 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002492 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002493 StringRef name = ".block_descriptor";
John McCall147d0212011-02-22 22:38:33 +00002494
2495 // Create the descriptor for the parameter.
2496 llvm::DIVariable debugVar =
2497 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2498 llvm::DIDescriptor(scope),
2499 name, tunit, line, type,
David Blaikiebbafb8a2012-03-11 07:00:24 +00002500 CGM.getLangOpts().Optimize, flags,
Devang Patel68a15252011-03-03 20:13:15 +00002501 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall147d0212011-02-22 22:38:33 +00002502
2503 // Insert an llvm.dbg.value into the current block.
2504 llvm::Instruction *declare =
2505 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2506 Builder.GetInsertBlock());
2507 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2508}
Chris Lattneraffb3732008-11-10 06:08:34 +00002509
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002510/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00002511void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00002512 const VarDecl *D) {
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002513 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002514 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002515 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00002516
Eric Christopher7cdf9482011-10-13 21:45:18 +00002517 setLocation(D->getLocation());
2518
Devang Patel7b7f46f2010-02-01 21:34:11 +00002519 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002520 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002521
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002522 // CodeGen turns int[] into int[1] so we'll do the same here.
2523 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002524
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002525 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002526 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002527
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002528 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewycky2219ef02011-11-09 04:25:21 +00002529 ArrayType::Normal, 0);
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002530 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002531 StringRef DeclName = D->getName();
2532 StringRef LinkageName;
Devang Patel84d40a42011-02-09 19:16:38 +00002533 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2534 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00002535 LinkageName = Var->getName();
Devang Patelf79199d2010-10-22 17:11:50 +00002536 if (LinkageName == DeclName)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002537 LinkageName = StringRef();
Devang Patel7b7f46f2010-02-01 21:34:11 +00002538 llvm::DIDescriptor DContext =
Devang Patel8c445292010-12-09 00:33:05 +00002539 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Pateld7185b72011-02-22 18:56:36 +00002540 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +00002541 Unit, LineNo, getOrCreateType(T, Unit),
2542 Var->hasInternalLinkage(), Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002543}
2544
Devang Patelf4c205b2009-02-26 21:10:26 +00002545/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00002546void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00002547 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00002548 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002549 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002550 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00002551
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002552 StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00002553
Devang Patel3efd1472010-02-01 21:52:22 +00002554 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00002555 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002556
Devang Patelf4c205b2009-02-26 21:10:26 +00002557 // CodeGen turns int[] into int[1] so we'll do the same here.
2558 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002559
Devang Patelf4c205b2009-02-26 21:10:26 +00002560 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002561 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002562
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002563 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00002564 ArrayType::Normal, 0);
2565 }
2566
Devang Pateld7185b72011-02-22 18:56:36 +00002567 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel00afcbe2010-12-08 22:42:58 +00002568 getOrCreateType(T, Unit),
2569 Var->hasInternalLinkage(), Var);
Devang Patelf4c205b2009-02-26 21:10:26 +00002570}
Devang Patel973f2eb2010-02-01 19:16:32 +00002571
Devang Pateldc866e12010-08-10 17:53:33 +00002572/// EmitGlobalVariable - Emit global variable's debug info.
2573void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCalla2fabff2010-10-09 01:34:31 +00002574 llvm::Constant *Init) {
Devang Patele03edfd2010-08-10 07:24:25 +00002575 // Create the descriptor for the variable.
2576 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002577 StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00002578 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00002579 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2580 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel283e89d2011-01-17 22:23:07 +00002581 Ty = CreateEnumType(ED);
Devang Patel41c20972010-08-23 22:07:25 +00002582 }
Devang Patel76e3b532010-08-10 18:27:15 +00002583 // Do not use DIGlobalVariable for enums.
2584 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2585 return;
Devang Pateld7185b72011-02-22 18:56:36 +00002586 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002587 getLineNumber(VD->getLocation()),
2588 Ty, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00002589}
2590
Devang Patel973f2eb2010-02-01 19:16:32 +00002591/// getOrCreateNamesSpace - Return namespace descriptor for the given
2592/// namespace decl.
2593llvm::DINameSpace
Devang Patel8c445292010-12-09 00:33:05 +00002594CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patel973f2eb2010-02-01 19:16:32 +00002595 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2596 NameSpaceCache.find(NSDecl);
2597 if (I != NameSpaceCache.end())
2598 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2599
Devang Patelc5ffabc2010-05-12 23:46:38 +00002600 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelfaadd7b2010-10-28 19:12:46 +00002601 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00002602 llvm::DIDescriptor Context =
Devang Patel8c445292010-12-09 00:33:05 +00002603 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patel973f2eb2010-02-01 19:16:32 +00002604 llvm::DINameSpace NS =
Devang Pateld7185b72011-02-22 18:56:36 +00002605 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002606 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00002607 return NS;
2608}
Eric Christopher1c3785a2012-02-18 00:50:17 +00002609
2610void CGDebugInfo::finalize(void) {
2611 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2612 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2613 llvm::DIType Ty, RepTy;
2614 // Verify that the debug info still exists.
2615 if (&*VI->second)
2616 Ty = llvm::DIType(cast<llvm::MDNode>(VI->second));
2617
2618 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2619 TypeCache.find(VI->first);
2620 if (it != TypeCache.end()) {
2621 // Verify that the debug info still exists.
2622 if (&*it->second)
2623 RepTy = llvm::DIType(cast<llvm::MDNode>(it->second));
2624 }
2625
Eric Christopher66562a42012-02-20 18:05:24 +00002626 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher1c3785a2012-02-18 00:50:17 +00002627 Ty.replaceAllUsesWith(RepTy);
Eric Christopher66562a42012-02-20 18:05:24 +00002628 }
Eric Christopher1c3785a2012-02-18 00:50:17 +00002629 }
2630 DBuilder.finalize();
2631}