blob: 1771abf9b2858f5c480e249c1ab258bb05414983 [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();
186 PrintingPolicy Policy(CGM.getLangOptions());
187 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;
Devang Patel408dcf62010-03-09 00:44:50 +0000291 const LangOptions &LO = CGM.getLangOptions();
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.
335 createNullPtrType(BT->getName(CGM.getContext().getLangOptions()));
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,
340 "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 Christophere908a7a2012-02-20 18:05:04 +0000348 llvm::DIType OCTy =
349 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
350 "objc_class", getOrCreateMainFile(),
351 0);
Devang Patela652fab2010-07-28 01:33:15 +0000352 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
353
Devang Pateld7185b72011-02-22 18:56:36 +0000354 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patela652fab2010-07-28 01:33:15 +0000355
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000356 SmallVector<llvm::Value *, 16> EltTys;
Devang Patela652fab2010-07-28 01:33:15 +0000357 llvm::DIType FieldTy =
Devang Patel15013e72011-06-24 22:00:59 +0000358 DBuilder.createMemberType(getOrCreateMainFile(), "isa",
359 getOrCreateMainFile(), 0, Size,
360 0, 0, 0, ISATy);
Devang Patela652fab2010-07-28 01:33:15 +0000361 EltTys.push_back(FieldTy);
Jay Foaddbf81d82011-04-24 10:11:03 +0000362 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patela652fab2010-07-28 01:33:15 +0000363
Devang Pateld7185b72011-02-22 18:56:36 +0000364 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel00afcbe2010-12-08 22:42:58 +0000365 getOrCreateMainFile(),
366 0, 0, 0, 0, Elements);
Devang Patela652fab2010-07-28 01:33:15 +0000367 }
Devang Patel19ba2b42011-02-09 03:15:05 +0000368 case BuiltinType::ObjCSel: {
Eric Christophere908a7a2012-02-20 18:05:04 +0000369 return
370 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
371 "objc_selector", getOrCreateMainFile(),
372 0);
Devang Patel19ba2b42011-02-09 03:15:05 +0000373 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000374 case BuiltinType::UChar:
375 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
376 case BuiltinType::Char_S:
377 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
Devang Patel98ca8ae2011-09-12 17:11:58 +0000378 case BuiltinType::Char16:
379 case BuiltinType::Char32: Encoding = llvm::dwarf::DW_ATE_UTF; break;
Chris Lattneraffb3732008-11-10 06:08:34 +0000380 case BuiltinType::UShort:
381 case BuiltinType::UInt:
Devang Patel979aba52011-05-05 17:06:30 +0000382 case BuiltinType::UInt128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000383 case BuiltinType::ULong:
Devang Patel964d7582011-09-10 00:44:49 +0000384 case BuiltinType::WChar_U:
Chris Lattneraffb3732008-11-10 06:08:34 +0000385 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
386 case BuiltinType::Short:
387 case BuiltinType::Int:
Devang Patel979aba52011-05-05 17:06:30 +0000388 case BuiltinType::Int128:
Chris Lattneraffb3732008-11-10 06:08:34 +0000389 case BuiltinType::Long:
Devang Patel964d7582011-09-10 00:44:49 +0000390 case BuiltinType::WChar_S:
Chris Lattneraffb3732008-11-10 06:08:34 +0000391 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
392 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +0000393 case BuiltinType::Half:
Chris Lattneraffb3732008-11-10 06:08:34 +0000394 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000395 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000396 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000397 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000398
399 switch (BT->getKind()) {
400 case BuiltinType::Long: BTName = "long int"; break;
401 case BuiltinType::LongLong: BTName = "long long int"; break;
402 case BuiltinType::ULong: BTName = "long unsigned int"; break;
403 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
404 default:
405 BTName = BT->getName(CGM.getContext().getLangOptions());
406 break;
407 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000408 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000409 uint64_t Size = CGM.getContext().getTypeSize(BT);
410 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patele21912d2009-10-20 19:55:01 +0000411 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000412 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000413 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000414}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000415
Devang Patel4591f772010-12-09 00:25:29 +0000416llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000417 // Bit size, align and offset of the type.
418 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
419 if (Ty->isComplexIntegerType())
420 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000421
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000422 uint64_t Size = CGM.getContext().getTypeSize(Ty);
423 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patele21912d2009-10-20 19:55:01 +0000424 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +0000425 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel00afcbe2010-12-08 22:42:58 +0000426
Devang Patele21912d2009-10-20 19:55:01 +0000427 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000428}
429
John McCall0cf15512009-09-25 01:40:47 +0000430/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000431/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000432llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000433 QualifierCollector Qc;
434 const Type *T = Qc.strip(Ty);
435
436 // Ignore these qualifiers for now.
437 Qc.removeObjCGCAttr();
438 Qc.removeAddressSpace();
John McCall31168b02011-06-15 23:02:42 +0000439 Qc.removeObjCLifetime();
John McCall0cf15512009-09-25 01:40:47 +0000440
Chris Lattneraffb3732008-11-10 06:08:34 +0000441 // We will create one Derived type for one qualifier and recurse to handle any
442 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000443 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000444 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000445 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000446 Qc.removeConst();
447 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000448 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000449 Qc.removeVolatile();
450 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000451 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000452 Qc.removeRestrict();
453 } else {
454 assert(Qc.empty() && "Unknown type qualifier for debug info");
455 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000456 }
Mike Stump11289f42009-09-09 15:08:12 +0000457
John McCall717d9b02010-12-10 11:01:00 +0000458 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCall0cf15512009-09-25 01:40:47 +0000459
Daniel Dunbara290ded2008-10-31 03:54:29 +0000460 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
461 // CVR derived types.
Devang Pateld7185b72011-02-22 18:56:36 +0000462 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel00afcbe2010-12-08 22:42:58 +0000463
Devang Patele21912d2009-10-20 19:55:01 +0000464 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000465}
466
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000467llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000468 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000469 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000470 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
471 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000472 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000473}
474
Chris Lattneraffb3732008-11-10 06:08:34 +0000475llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000476 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000477 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
478 Ty->getPointeeType(), Unit);
479}
480
Eric Christopher000b14e2012-01-25 02:06:59 +0000481// Creates a forward declaration for a RecordDecl in the given context.
482llvm::DIType CGDebugInfo::createRecordFwdDecl(const RecordDecl *RD,
Devang Patel00fca3a2012-02-08 00:10:20 +0000483 llvm::DIDescriptor Ctx) {
Eric Christopher000b14e2012-01-25 02:06:59 +0000484 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
485 unsigned Line = getLineNumber(RD->getLocation());
Eric Christopherfe525232012-02-13 15:08:45 +0000486 StringRef RDName = RD->getName();
487
488 // Get the tag.
Eric Christopher000b14e2012-01-25 02:06:59 +0000489 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Eric Christopherfe525232012-02-13 15:08:45 +0000490 unsigned Tag = 0;
491 if (CXXDecl) {
492 RDName = getClassName(RD);
493 Tag = llvm::dwarf::DW_TAG_class_type;
494 }
Eric Christopher000b14e2012-01-25 02:06:59 +0000495 else if (RD->isStruct())
Eric Christopherfe525232012-02-13 15:08:45 +0000496 Tag = llvm::dwarf::DW_TAG_structure_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000497 else if (RD->isUnion())
Eric Christopherfe525232012-02-13 15:08:45 +0000498 Tag = llvm::dwarf::DW_TAG_union_type;
Eric Christopher000b14e2012-01-25 02:06:59 +0000499 else
500 llvm_unreachable("Unknown RecordDecl type!");
Eric Christopherfe525232012-02-13 15:08:45 +0000501
502 // Create the type.
Eric Christopher1a0c8822012-02-18 00:50:14 +0000503 return DBuilder.createForwardDecl(Tag, RDName, DefUnit, Line);
Eric Christopher000b14e2012-01-25 02:06:59 +0000504}
505
Eric Christopher45c4d472012-01-20 22:10:15 +0000506// Walk up the context chain and create forward decls for record decls,
507// and normal descriptors for namespaces.
508llvm::DIDescriptor CGDebugInfo::createContextChain(const Decl *Context) {
509 if (!Context)
510 return TheCU;
511
512 // See if we already have the parent.
513 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
514 I = RegionMap.find(Context);
515 if (I != RegionMap.end())
516 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
517
518 // Check namespace.
519 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
520 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
521
522 if (const RecordDecl *RD = dyn_cast<RecordDecl>(Context)) {
523 if (!RD->isDependentType()) {
Eric Christopher4c006e52012-02-16 22:54:45 +0000524 llvm::DIType Ty = getOrCreateLimitedType(CGM.getContext().getTypeDeclType(RD),
525 getOrCreateMainFile());
Eric Christopher45c4d472012-01-20 22:10:15 +0000526 return llvm::DIDescriptor(Ty);
527 }
528 }
529 return TheCU;
530}
531
Eric Christopher47300ad2011-09-13 23:45:09 +0000532/// CreatePointeeType - Create Pointee type. If Pointee is a record
Devang Patel91bbb552010-09-30 19:05:55 +0000533/// then emit record's fwd if debug info size reduction is enabled.
534llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
535 llvm::DIFile Unit) {
536 if (!CGM.getCodeGenOpts().LimitDebugInfo)
537 return getOrCreateType(PointeeTy, Unit);
Devang Patel242ce912011-10-24 23:15:17 +0000538
539 // Limit debug info for the pointee type.
540
Eric Christophercd888132011-12-16 23:40:18 +0000541 // If we have an existing type, use that, it's still smaller than creating
542 // a new type.
543 llvm::DIType Ty = getTypeOrNull(PointeeTy);
544 if (Ty.Verify()) return Ty;
545
Devang Patel242ce912011-10-24 23:15:17 +0000546 // Handle qualifiers.
547 if (PointeeTy.hasLocalQualifiers())
548 return CreateQualifiedType(PointeeTy, Unit);
549
Devang Patel91bbb552010-09-30 19:05:55 +0000550 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
551 RecordDecl *RD = RTy->getDecl();
Devang Patel91bbb552010-09-30 19:05:55 +0000552 llvm::DIDescriptor FDContext =
John McCall147d0212011-02-22 22:38:33 +0000553 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Eric Christopher66562a42012-02-20 18:05:24 +0000554 llvm::DIType RetTy = createRecordFwdDecl(RD, FDContext);
555 TypeCache[QualType(RTy, 0).getAsOpaquePtr()] = RetTy;
556 return RetTy;
Devang Patel91bbb552010-09-30 19:05:55 +0000557 }
558 return getOrCreateType(PointeeTy, Unit);
Eric Christopher8a41bd82012-02-13 14:56:11 +0000559
Devang Patel91bbb552010-09-30 19:05:55 +0000560}
561
Anders Carlsson443f6772009-11-06 19:19:55 +0000562llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
563 const Type *Ty,
564 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000565 llvm::DIFile Unit) {
Devang Patel00afcbe2010-12-08 22:42:58 +0000566 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Pateld7185b72011-02-22 18:56:36 +0000567 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel00afcbe2010-12-08 22:42:58 +0000568
Sanjiv Gupta98070572008-05-25 05:15:42 +0000569 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000570 // Size is always the size of a pointer. We can't use getTypeSize here
571 // because that does not return the correct value for references.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000572 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
Douglas Gregore8bbc122011-09-02 00:18:52 +0000573 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000574 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000575
Nick Lewycky16790352011-11-10 00:34:02 +0000576 return DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit),
577 Size, Align);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000578}
579
Mike Stump31f099c2009-05-14 02:03:51 +0000580llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000581 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000582 if (BlockLiteralGenericSet)
583 return BlockLiteralGeneric;
584
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000585 SmallVector<llvm::Value *, 8> EltTys;
Mike Stump31f099c2009-05-14 02:03:51 +0000586 llvm::DIType FieldTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000587 QualType FType;
588 uint64_t FieldSize, FieldOffset;
589 unsigned FieldAlign;
Mike Stump31f099c2009-05-14 02:03:51 +0000590 llvm::DIArray Elements;
591 llvm::DIType EltTy, DescTy;
592
593 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000594 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000595 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
596 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000597
Jay Foaddbf81d82011-04-24 10:11:03 +0000598 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000599 EltTys.clear();
600
Devang Pateldb2732a2010-09-29 21:05:52 +0000601 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000602 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000603
Devang Pateld7185b72011-02-22 18:56:36 +0000604 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel00afcbe2010-12-08 22:42:58 +0000605 Unit, LineNo, FieldOffset, 0,
606 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000607
Mike Stump31f099c2009-05-14 02:03:51 +0000608 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000609 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000610
Devang Pateld7185b72011-02-22 18:56:36 +0000611 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000612
613 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000614 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000615 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000616 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000617 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
618 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000619 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000620 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000621
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000622 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000623 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000624 FieldSize = CGM.getContext().getTypeSize(Ty);
625 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel15013e72011-06-24 22:00:59 +0000626 FieldTy = DBuilder.createMemberType(Unit, "__descriptor", Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +0000627 LineNo, FieldSize, FieldAlign,
628 FieldOffset, 0, FieldTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000629 EltTys.push_back(FieldTy);
630
631 FieldOffset += FieldSize;
Jay Foaddbf81d82011-04-24 10:11:03 +0000632 Elements = DBuilder.getOrCreateArray(EltTys);
Mike Stump31f099c2009-05-14 02:03:51 +0000633
Devang Pateld7185b72011-02-22 18:56:36 +0000634 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel00afcbe2010-12-08 22:42:58 +0000635 Unit, LineNo, FieldOffset, 0,
636 Flags, Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000637
Mike Stump31f099c2009-05-14 02:03:51 +0000638 BlockLiteralGenericSet = true;
Devang Pateld7185b72011-02-22 18:56:36 +0000639 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump31f099c2009-05-14 02:03:51 +0000640 return BlockLiteralGeneric;
641}
642
Nick Lewycky16790352011-11-10 00:34:02 +0000643llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000644 // Typedefs are derived from some other type. If we have a typedef of a
645 // typedef, make sure to emit the whole chain.
646 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel00afcbe2010-12-08 22:42:58 +0000647 if (!Src.Verify())
648 return llvm::DIType();
Chris Lattneraffb3732008-11-10 06:08:34 +0000649 // We don't set size information, but do specify where the typedef was
650 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000651 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000652 const TypedefNameDecl *TyDecl = Ty->getDecl();
Eric Christopher4c006e52012-02-16 22:54:45 +0000653
Nick Lewycky16790352011-11-10 00:34:02 +0000654 llvm::DIDescriptor TypedefContext =
Devang Patelf1e2a7f2011-06-03 17:23:47 +0000655 getContextDescriptor(cast<Decl>(Ty->getDecl()->getDeclContext()));
Eric Christopher4c006e52012-02-16 22:54:45 +0000656
657 return
Nick Lewycky16790352011-11-10 00:34:02 +0000658 DBuilder.createTypedef(Src, TyDecl->getName(), Unit, Line, TypedefContext);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000659}
660
Chris Lattneraffb3732008-11-10 06:08:34 +0000661llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000662 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000663 SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000664
Chris Lattneraffb3732008-11-10 06:08:34 +0000665 // Add the result type at least.
666 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattneraffb3732008-11-10 06:08:34 +0000668 // Set up remainder of arguments if there is a prototype.
669 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patel284fa412010-10-06 20:51:45 +0000670 if (isa<FunctionNoProtoType>(Ty))
Devang Pateld7185b72011-02-22 18:56:36 +0000671 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patel284fa412010-10-06 20:51:45 +0000672 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Eric Christopher8a41bd82012-02-13 14:56:11 +0000673 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
674 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta98070572008-05-25 05:15:42 +0000675 }
676
Jay Foaddbf81d82011-04-24 10:11:03 +0000677 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(EltTys);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Devang Pateld7185b72011-02-22 18:56:36 +0000679 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000680 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000681}
682
Eric Christopher8a41bd82012-02-13 14:56:11 +0000683
Eric Christophera15e6352012-01-26 01:57:13 +0000684void CGDebugInfo::
685CollectRecordStaticVars(const RecordDecl *RD, llvm::DIType FwdDecl) {
686
687 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
688 I != E; ++I)
689 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
690 if (V->getInit()) {
691 const APValue *Value = V->evaluateValue();
692 if (Value && Value->isInt()) {
693 llvm::ConstantInt *CI
694 = llvm::ConstantInt::get(CGM.getLLVMContext(), Value->getInt());
695
696 // Create the descriptor for static variable.
697 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
698 StringRef VName = V->getName();
699 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
700 // Do not use DIGlobalVariable for enums.
701 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
702 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
703 getLineNumber(V->getLocation()),
704 VTy, true, CI);
705 }
706 }
707 }
708 }
709}
710
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000711llvm::DIType CGDebugInfo::createFieldType(StringRef name,
John McCall147d0212011-02-22 22:38:33 +0000712 QualType type,
Richard Smithcaf33902011-10-10 18:28:20 +0000713 uint64_t sizeInBitsOverride,
John McCall147d0212011-02-22 22:38:33 +0000714 SourceLocation loc,
715 AccessSpecifier AS,
716 uint64_t offsetInBits,
Devang Patel15013e72011-06-24 22:00:59 +0000717 llvm::DIFile tunit,
718 llvm::DIDescriptor scope) {
John McCall147d0212011-02-22 22:38:33 +0000719 llvm::DIType debugType = getOrCreateType(type, tunit);
720
721 // Get the location for the field.
722 llvm::DIFile file = getOrCreateFile(loc);
723 unsigned line = getLineNumber(loc);
724
725 uint64_t sizeInBits = 0;
726 unsigned alignInBits = 0;
727 if (!type->isIncompleteArrayType()) {
728 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
729
Richard Smithcaf33902011-10-10 18:28:20 +0000730 if (sizeInBitsOverride)
731 sizeInBits = sizeInBitsOverride;
John McCall147d0212011-02-22 22:38:33 +0000732 }
733
734 unsigned flags = 0;
735 if (AS == clang::AS_private)
736 flags |= llvm::DIDescriptor::FlagPrivate;
737 else if (AS == clang::AS_protected)
738 flags |= llvm::DIDescriptor::FlagProtected;
739
Devang Patel15013e72011-06-24 22:00:59 +0000740 return DBuilder.createMemberType(scope, name, file, line, sizeInBits,
741 alignInBits, offsetInBits, flags, debugType);
John McCall147d0212011-02-22 22:38:33 +0000742}
743
Devang Patel889ce762010-01-19 00:00:59 +0000744/// CollectRecordFields - A helper function to collect debug info for
745/// record fields. This is used while creating debug info entry for a Record.
746void CGDebugInfo::
John McCall147d0212011-02-22 22:38:33 +0000747CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000748 SmallVectorImpl<llvm::Value *> &elements,
Devang Patel15013e72011-06-24 22:00:59 +0000749 llvm::DIType RecordTy) {
John McCall147d0212011-02-22 22:38:33 +0000750 unsigned fieldNo = 0;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000751 const FieldDecl *LastFD = 0;
752 bool IsMsStruct = record->hasAttr<MsStructAttr>();
753
John McCall147d0212011-02-22 22:38:33 +0000754 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
755 for (RecordDecl::field_iterator I = record->field_begin(),
756 E = record->field_end();
757 I != E; ++I, ++fieldNo) {
758 FieldDecl *field = *I;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000759 if (IsMsStruct) {
760 // Zero-length bitfields following non-bitfield members are ignored
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +0000761 if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((field), LastFD)) {
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000762 --fieldNo;
763 continue;
764 }
765 LastFD = field;
766 }
Devang Patel889ce762010-01-19 00:00:59 +0000767
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000768 StringRef name = field->getName();
John McCall147d0212011-02-22 22:38:33 +0000769 QualType type = field->getType();
770
771 // Ignore unnamed fields unless they're anonymous structs/unions.
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000772 if (name.empty() && !type->isRecordType()) {
773 LastFD = field;
Devang Patel889ce762010-01-19 00:00:59 +0000774 continue;
Fariborz Jahanian6d003c32011-04-28 23:43:23 +0000775 }
Devang Patel889ce762010-01-19 00:00:59 +0000776
Richard Smithcaf33902011-10-10 18:28:20 +0000777 uint64_t SizeInBitsOverride = 0;
778 if (field->isBitField()) {
779 SizeInBitsOverride = field->getBitWidthValue(CGM.getContext());
780 assert(SizeInBitsOverride && "found named 0-width bitfield");
781 }
782
John McCall147d0212011-02-22 22:38:33 +0000783 llvm::DIType fieldType
Richard Smithcaf33902011-10-10 18:28:20 +0000784 = createFieldType(name, type, SizeInBitsOverride,
John McCall147d0212011-02-22 22:38:33 +0000785 field->getLocation(), field->getAccess(),
Devang Patel15013e72011-06-24 22:00:59 +0000786 layout.getFieldOffset(fieldNo), tunit, RecordTy);
Devang Patel889ce762010-01-19 00:00:59 +0000787
John McCall147d0212011-02-22 22:38:33 +0000788 elements.push_back(fieldType);
Devang Patel889ce762010-01-19 00:00:59 +0000789 }
790}
791
Devang Patel3d4e6d92010-01-28 00:28:01 +0000792/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
793/// function type is not updated to include implicit "this" pointer. Use this
794/// routine to get a method type which includes "this" pointer.
795llvm::DIType
796CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000797 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000798 llvm::DIType FnTy
799 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
800 0),
801 Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000802
Devang Patel3d4e6d92010-01-28 00:28:01 +0000803 // Add "this" pointer.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000804 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000805 assert (Args.getNumElements() && "Invalid number of arguments!");
806
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000807 SmallVector<llvm::Value *, 16> Elts;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000808
809 // First element is always return type. For 'void' functions it is NULL.
810 Elts.push_back(Args.getElement(0));
811
Eric Christopher19329c42011-09-14 01:10:50 +0000812 if (!Method->isStatic()) {
813 // "this" pointer is always first argument.
814 QualType ThisPtr = Method->getThisType(CGM.getContext());
Devang Patelfa59ac32011-10-28 21:12:13 +0000815
816 const CXXRecordDecl *RD = Method->getParent();
817 if (isa<ClassTemplateSpecializationDecl>(RD)) {
818 // Create pointer type directly in this case.
819 const PointerType *ThisPtrTy = cast<PointerType>(ThisPtr);
820 QualType PointeeTy = ThisPtrTy->getPointeeType();
821 unsigned AS = CGM.getContext().getTargetAddressSpace(PointeeTy);
822 uint64_t Size = CGM.getContext().getTargetInfo().getPointerWidth(AS);
823 uint64_t Align = CGM.getContext().getTypeAlign(ThisPtrTy);
Nick Lewycky2219ef02011-11-09 04:25:21 +0000824 llvm::DIType PointeeType = getOrCreateType(PointeeTy, Unit);
Eric Christopher39e39c82012-02-09 07:26:21 +0000825 llvm::DIType ThisPtrType = DBuilder.createPointerType(PointeeType, Size, Align);
Devang Patelfa59ac32011-10-28 21:12:13 +0000826 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000827 // TODO: This and the artificial type below are misleading, the
828 // types aren't artificial the argument is, but the current
829 // metadata doesn't represent that.
830 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000831 Elts.push_back(ThisPtrType);
832 } else {
Eric Christopher39e39c82012-02-09 07:26:21 +0000833 llvm::DIType ThisPtrType = getOrCreateType(ThisPtr, Unit);
Devang Patelfa59ac32011-10-28 21:12:13 +0000834 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Eric Christopher39e39c82012-02-09 07:26:21 +0000835 ThisPtrType = DBuilder.createArtificialType(ThisPtrType);
Devang Patelfa59ac32011-10-28 21:12:13 +0000836 Elts.push_back(ThisPtrType);
837 }
Eric Christopher19329c42011-09-14 01:10:50 +0000838 }
Devang Patel3d4e6d92010-01-28 00:28:01 +0000839
840 // Copy rest of the arguments.
841 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
842 Elts.push_back(Args.getElement(i));
843
Jay Foaddbf81d82011-04-24 10:11:03 +0000844 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000845
Devang Pateld7185b72011-02-22 18:56:36 +0000846 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000847}
848
Devang Patelf79199d2010-10-22 17:11:50 +0000849/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
850/// inside a function.
851static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
Nick Lewycky16790352011-11-10 00:34:02 +0000852 if (const CXXRecordDecl *NRD = dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000853 return isFunctionLocalClass(NRD);
Nick Lewycky16790352011-11-10 00:34:02 +0000854 if (isa<FunctionDecl>(RD->getDeclContext()))
Devang Patelf79199d2010-10-22 17:11:50 +0000855 return true;
856 return false;
Devang Patelf79199d2010-10-22 17:11:50 +0000857}
Nick Lewycky2219ef02011-11-09 04:25:21 +0000858
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000859/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
860/// a single member function GlobalDecl.
861llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000862CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000863 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000864 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000865 bool IsCtorOrDtor =
866 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
867
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000868 StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000869 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000870
871 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
872 // make sense to give a single ctor/dtor a linkage name.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000873 StringRef MethodLinkageName;
Devang Patelf79199d2010-10-22 17:11:50 +0000874 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlssonea836bc2010-06-22 16:16:50 +0000875 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000876
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000877 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000878 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
879 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000880
881 // Collect virtual method info.
882 llvm::DIType ContainingType;
883 unsigned Virtuality = 0;
884 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000885
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000886 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000887 if (Method->isPure())
888 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
889 else
890 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
891
892 // It doesn't make sense to give a virtual destructor a vtable index,
893 // since a single destructor has two entries in the vtable.
894 if (!isa<CXXDestructorDecl>(Method))
Peter Collingbournea8341662011-09-26 01:56:30 +0000895 VIndex = CGM.getVTableContext().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000896 ContainingType = RecordTy;
897 }
898
Devang Pateldb2732a2010-09-29 21:05:52 +0000899 unsigned Flags = 0;
900 if (Method->isImplicit())
901 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel330b65e2010-09-29 21:46:16 +0000902 AccessSpecifier Access = Method->getAccess();
903 if (Access == clang::AS_private)
904 Flags |= llvm::DIDescriptor::FlagPrivate;
905 else if (Access == clang::AS_protected)
906 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000907 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
908 if (CXXC->isExplicit())
909 Flags |= llvm::DIDescriptor::FlagExplicit;
910 } else if (const CXXConversionDecl *CXXC =
911 dyn_cast<CXXConversionDecl>(Method)) {
912 if (CXXC->isExplicit())
913 Flags |= llvm::DIDescriptor::FlagExplicit;
914 }
Devang Patel251f8592010-10-07 22:03:49 +0000915 if (Method->hasPrototype())
916 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld18c5aa2010-10-01 23:32:17 +0000917
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000918 llvm::DISubprogram SP =
Nick Lewycky0112b112011-09-01 21:49:51 +0000919 DBuilder.createMethod(RecordTy, MethodName, MethodLinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +0000920 MethodDefUnit, MethodLine,
921 MethodTy, /*isLocalToUnit=*/false,
922 /* isDefinition=*/ false,
923 Virtuality, VIndex, ContainingType,
924 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000925
Eric Christopher459532e2011-11-17 23:45:00 +0000926 SPCache[Method->getCanonicalDecl()] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000927
928 return SP;
929}
930
Devang Patel7a12ad02010-01-19 01:54:44 +0000931/// CollectCXXMemberFunctions - A helper function to collect debug info for
Eric Christopherffbc4d02012-01-12 01:26:51 +0000932/// C++ member functions. This is used while creating debug info entry for
Devang Patel7a12ad02010-01-19 01:54:44 +0000933/// a Record.
934void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000935CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000936 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000937 llvm::DIType RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000938 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
939 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000940 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000941
Devang Patel0ae70d12010-02-09 19:09:28 +0000942 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000943 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000944
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000945 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000946 }
947}
948
Devang Patel96b7f552010-08-27 17:47:47 +0000949/// CollectCXXFriends - A helper function to collect debug info for
950/// C++ base classes. This is used while creating debug info entry for
951/// a Record.
952void CGDebugInfo::
953CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000954 SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel96b7f552010-08-27 17:47:47 +0000955 llvm::DIType RecordTy) {
Eric Christopher8c363622012-01-12 01:26:58 +0000956 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
Devang Patel96b7f552010-08-27 17:47:47 +0000957 BE = RD->friend_end(); BI != BE; ++BI) {
Nick Lewycky0112b112011-09-01 21:49:51 +0000958 if ((*BI)->isUnsupportedFriend())
959 continue;
Devang Patel00afcbe2010-12-08 22:42:58 +0000960 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Pateld7185b72011-02-22 18:56:36 +0000961 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +0000962 getOrCreateType(TInfo->getType(),
963 Unit)));
Devang Patel96b7f552010-08-27 17:47:47 +0000964 }
965}
966
Devang Patelc54353d2010-01-25 23:32:18 +0000967/// CollectCXXBases - A helper function to collect debug info for
968/// C++ base classes. This is used while creating debug info entry for
969/// a Record.
970void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000971CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000972 SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000973 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +0000974
Devang Patel1c0954c2010-02-01 21:39:52 +0000975 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
976 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
977 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000978 unsigned BFlags = 0;
Devang Patel84852bb2011-04-04 20:36:06 +0000979 uint64_t BaseOffset;
Devang Patel128aa9d2010-01-28 21:54:15 +0000980
981 const CXXRecordDecl *Base =
982 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
983
984 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000985 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000986 // expression where it expects +ve number.
Ken Dyckbb4e9772011-04-07 12:37:09 +0000987 BaseOffset =
Peter Collingbournea8341662011-09-26 01:56:30 +0000988 0 - CGM.getVTableContext()
989 .getVirtualBaseOffsetOffset(RD, Base).getQuantity();
Devang Pateldb2732a2010-09-29 21:05:52 +0000990 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patel128aa9d2010-01-28 21:54:15 +0000991 } else
Devang Patel84852bb2011-04-04 20:36:06 +0000992 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Ken Dyckbb4e9772011-04-07 12:37:09 +0000993 // FIXME: Inconsistent units for BaseOffset. It is in bytes when
994 // BI->isVirtual() and bits when not.
Devang Patel128aa9d2010-01-28 21:54:15 +0000995
996 AccessSpecifier Access = BI->getAccessSpecifier();
997 if (Access == clang::AS_private)
Devang Pateldb2732a2010-09-29 21:05:52 +0000998 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel128aa9d2010-01-28 21:54:15 +0000999 else if (Access == clang::AS_protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001000 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patel128aa9d2010-01-28 21:54:15 +00001001
Devang Patel00afcbe2010-12-08 22:42:58 +00001002 llvm::DIType DTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001003 DBuilder.createInheritance(RecordTy,
Devang Patel00afcbe2010-12-08 22:42:58 +00001004 getOrCreateType(BI->getType(), Unit),
Devang Patel84852bb2011-04-04 20:36:06 +00001005 BaseOffset, BFlags);
Devang Patel128aa9d2010-01-28 21:54:15 +00001006 EltTys.push_back(DTy);
1007 }
Devang Patelc54353d2010-01-25 23:32:18 +00001008}
1009
Devang Patelb87c4282011-04-05 22:54:11 +00001010/// CollectTemplateParams - A helper function to collect template parameters.
Devang Patel7522abd2011-04-05 17:30:54 +00001011llvm::DIArray CGDebugInfo::
Devang Patelb87c4282011-04-05 22:54:11 +00001012CollectTemplateParams(const TemplateParameterList *TPList,
1013 const TemplateArgumentList &TAList,
1014 llvm::DIFile Unit) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001015 SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel98d26c92011-04-05 20:15:06 +00001016 for (unsigned i = 0, e = TAList.size(); i != e; ++i) {
1017 const TemplateArgument &TA = TAList[i];
Devang Patelb87c4282011-04-05 22:54:11 +00001018 const NamedDecl *ND = TPList->getParam(i);
Devang Patel7522abd2011-04-05 17:30:54 +00001019 if (TA.getKind() == TemplateArgument::Type) {
1020 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
1021 llvm::DITemplateTypeParameter TTP =
Devang Patel98d26c92011-04-05 20:15:06 +00001022 DBuilder.createTemplateTypeParameter(TheCU, ND->getName(), TTy);
Devang Patel7522abd2011-04-05 17:30:54 +00001023 TemplateParams.push_back(TTP);
1024 } else if (TA.getKind() == TemplateArgument::Integral) {
1025 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
Devang Patel7522abd2011-04-05 17:30:54 +00001026 llvm::DITemplateValueParameter TVP =
Devang Patel98d26c92011-04-05 20:15:06 +00001027 DBuilder.createTemplateValueParameter(TheCU, ND->getName(), TTy,
1028 TA.getAsIntegral()->getZExtValue());
Devang Patel7522abd2011-04-05 17:30:54 +00001029 TemplateParams.push_back(TVP);
1030 }
1031 }
Jay Foaddbf81d82011-04-24 10:11:03 +00001032 return DBuilder.getOrCreateArray(TemplateParams);
Devang Patel7522abd2011-04-05 17:30:54 +00001033}
1034
Devang Patelb87c4282011-04-05 22:54:11 +00001035/// CollectFunctionTemplateParams - A helper function to collect debug
1036/// info for function template parameters.
1037llvm::DIArray CGDebugInfo::
1038CollectFunctionTemplateParams(const FunctionDecl *FD, llvm::DIFile Unit) {
Eric Christopherfefafac2011-10-11 23:00:51 +00001039 if (FD->getTemplatedKind() ==
1040 FunctionDecl::TK_FunctionTemplateSpecialization) {
Devang Patelb87c4282011-04-05 22:54:11 +00001041 const TemplateParameterList *TList =
Eric Christopherfefafac2011-10-11 23:00:51 +00001042 FD->getTemplateSpecializationInfo()->getTemplate()
1043 ->getTemplateParameters();
Devang Patelb87c4282011-04-05 22:54:11 +00001044 return
1045 CollectTemplateParams(TList, *FD->getTemplateSpecializationArgs(), Unit);
1046 }
1047 return llvm::DIArray();
1048}
1049
1050/// CollectCXXTemplateParams - A helper function to collect debug info for
1051/// template parameters.
1052llvm::DIArray CGDebugInfo::
1053CollectCXXTemplateParams(const ClassTemplateSpecializationDecl *TSpecial,
1054 llvm::DIFile Unit) {
1055 llvm::PointerUnion<ClassTemplateDecl *,
1056 ClassTemplatePartialSpecializationDecl *>
1057 PU = TSpecial->getSpecializedTemplateOrPartial();
1058
1059 TemplateParameterList *TPList = PU.is<ClassTemplateDecl *>() ?
1060 PU.get<ClassTemplateDecl *>()->getTemplateParameters() :
1061 PU.get<ClassTemplatePartialSpecializationDecl *>()->getTemplateParameters();
1062 const TemplateArgumentList &TAList = TSpecial->getTemplateInstantiationArgs();
1063 return CollectTemplateParams(TPList, TAList, Unit);
1064}
1065
Devang Patel84033fb2010-01-28 18:11:52 +00001066/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +00001067llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001068 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +00001069 return VTablePtrType;
1070
1071 ASTContext &Context = CGM.getContext();
1072
1073 /* Function type */
Devang Patel00afcbe2010-12-08 22:42:58 +00001074 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Jay Foaddbf81d82011-04-24 10:11:03 +00001075 llvm::DIArray SElements = DBuilder.getOrCreateArray(STy);
Devang Pateld7185b72011-02-22 18:56:36 +00001076 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel84033fb2010-01-28 18:11:52 +00001077 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Pateld7185b72011-02-22 18:56:36 +00001078 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001079 "__vtbl_ptr_type");
Devang Pateld7185b72011-02-22 18:56:36 +00001080 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel84033fb2010-01-28 18:11:52 +00001081 return VTablePtrType;
1082}
1083
Anders Carlsson11e51402010-04-17 20:15:18 +00001084/// getVTableName - Get vtable name for the given Class.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001085StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Eric Christopherbfecca32012-01-25 21:47:09 +00001086 // Construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +00001087 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +00001088
1089 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +00001090 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001091 memcpy(StrPtr, Name.data(), Name.length());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001092 return StringRef(StrPtr, Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +00001093}
1094
1095
Anders Carlsson11e51402010-04-17 20:15:18 +00001096/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +00001097/// debug info entry in EltTys vector.
1098void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +00001099CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001100 SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +00001101 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +00001102
1103 // If there is a primary base then it will hold vtable info.
1104 if (RL.getPrimaryBase())
1105 return;
1106
1107 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +00001108 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +00001109 return;
1110
1111 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
1112 llvm::DIType VPTR
Devang Patel15013e72011-06-24 22:00:59 +00001113 = DBuilder.createMemberType(Unit, getVTableName(RD), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00001114 0, Size, 0, 0, 0,
1115 getOrCreateVTablePtrType(Unit));
Devang Patel84033fb2010-01-28 18:11:52 +00001116 EltTys.push_back(VPTR);
1117}
1118
Devang Patel91bbb552010-09-30 19:05:55 +00001119/// getOrCreateRecordType - Emit record type's standalone debug info.
1120llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
1121 SourceLocation Loc) {
Nick Lewycky2219ef02011-11-09 04:25:21 +00001122 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel91bbb552010-09-30 19:05:55 +00001123 return T;
1124}
1125
Devang Patel410dc002009-02-25 01:36:11 +00001126/// CreateType - get structure or union type.
Devang Patel283e89d2011-01-17 22:23:07 +00001127llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Patel3efd1472010-02-01 21:52:22 +00001128 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattneraffb3732008-11-10 06:08:34 +00001130 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001131 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001132
Chris Lattneraffb3732008-11-10 06:08:34 +00001133 // Records and classes and unions can all be recursive. To handle them, we
1134 // first generate a debug descriptor for the struct as a forward declaration.
1135 // Then (if it is a definition) we go through and get debug info for all of
1136 // its members. Finally, we create a descriptor for the complete type (which
1137 // may refer to the forward decl if the struct is recursive) and replace all
1138 // uses of the forward declaration with the final definition.
Eric Christopher45c4d472012-01-20 22:10:15 +00001139
Eric Christopher4c006e52012-02-16 22:54:45 +00001140 llvm::DIType FwdDecl = getOrCreateLimitedType(QualType(Ty, 0), DefUnit);
Devang Patel8f3f76f2010-07-08 19:56:29 +00001141
Eric Christopher4c006e52012-02-16 22:54:45 +00001142 if (FwdDecl.isForwardDecl())
1143 return FwdDecl;
1144
Devang Patelba4ad7f2010-05-07 18:12:35 +00001145 llvm::MDNode *MN = FwdDecl;
1146 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Eric Christopher4c006e52012-02-16 22:54:45 +00001147
Devang Patel01bb5ce2010-03-11 20:01:48 +00001148 // Push the struct on region stack.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001149 LexicalBlockStack.push_back(FwdDeclNode);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001150 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001151
Eric Christopher4c006e52012-02-16 22:54:45 +00001152 // Add this to the completed types cache since we're completing it.
1153 CompletedTypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
1154
Chris Lattneraffb3732008-11-10 06:08:34 +00001155 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001156 SmallVector<llvm::Value *, 16> EltTys;
Chris Lattneraffb3732008-11-10 06:08:34 +00001157
Eric Christopher8913bd62012-01-26 07:01:04 +00001158 // Note: The split of CXXDecl information here is intentional, the
1159 // gdb tests will depend on a certain ordering at printout. The debug
1160 // information offsets are still correct if we merge them all together
1161 // though.
Devang Patel3efd1472010-02-01 21:52:22 +00001162 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +00001163 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001164 CollectCXXBases(CXXDecl, DefUnit, EltTys, FwdDecl);
1165 CollectVTableInfo(CXXDecl, DefUnit, EltTys);
Eric Christopher8913bd62012-01-26 07:01:04 +00001166 }
1167
1168 // Collect static variables with initializers and other fields.
1169 CollectRecordStaticVars(RD, FwdDecl);
1170 CollectRecordFields(RD, DefUnit, EltTys, FwdDecl);
1171 llvm::DIArray TParamsArray;
1172 if (CXXDecl) {
Eric Christopher57200332012-01-26 06:20:57 +00001173 CollectCXXMemberFunctions(CXXDecl, DefUnit, EltTys, FwdDecl);
1174 CollectCXXFriends(CXXDecl, DefUnit, EltTys, FwdDecl);
Devang Patel7522abd2011-04-05 17:30:54 +00001175 if (const ClassTemplateSpecializationDecl *TSpecial
1176 = dyn_cast<ClassTemplateSpecializationDecl>(RD))
Eric Christopher57200332012-01-26 06:20:57 +00001177 TParamsArray = CollectCXXTemplateParams(TSpecial, DefUnit);
Devang Patel00afcbe2010-12-08 22:42:58 +00001178 }
Devang Patelabb44132010-01-28 00:54:21 +00001179
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001180 LexicalBlockStack.pop_back();
Devang Patel00afcbe2010-12-08 22:42:58 +00001181 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1182 RegionMap.find(Ty->getDecl());
1183 if (RI != RegionMap.end())
1184 RegionMap.erase(RI);
1185
Jay Foaddbf81d82011-04-24 10:11:03 +00001186 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher4c006e52012-02-16 22:54:45 +00001187 // FIXME: Magic numbers ahoy! These should be changed when we
1188 // get some enums in llvm/Analysis/DebugInfo.h to refer to
1189 // them.
Eric Christopher9a897052012-02-15 23:51:20 +00001190 if (RD->isUnion())
Eric Christopher4c006e52012-02-16 22:54:45 +00001191 MN->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001192 else if (CXXDecl) {
Eric Christopher4c006e52012-02-16 22:54:45 +00001193 MN->replaceOperandWith(10, Elements);
1194 MN->replaceOperandWith(13, TParamsArray);
Eric Christopher9a897052012-02-15 23:51:20 +00001195 } else
Eric Christopher4c006e52012-02-16 22:54:45 +00001196 MN->replaceOperandWith(10, Elements);
Eric Christopher9a897052012-02-15 23:51:20 +00001197
Eric Christopher4c006e52012-02-16 22:54:45 +00001198 RegionMap[Ty->getDecl()] = llvm::WeakVH(MN);
1199 return llvm::DIType(MN);
Chris Lattneraffb3732008-11-10 06:08:34 +00001200}
1201
John McCall8b07ec22010-05-15 11:32:37 +00001202/// CreateType - get objective-c object type.
1203llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1204 llvm::DIFile Unit) {
1205 // Ignore protocols.
1206 return getOrCreateType(Ty->getBaseType(), Unit);
1207}
1208
Devang Patelf4c205b2009-02-26 21:10:26 +00001209/// CreateType - get objective-c interface type.
1210llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001211 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +00001212 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001213 if (!ID)
1214 return llvm::DIType();
Devang Patelf4c205b2009-02-26 21:10:26 +00001215
1216 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +00001217 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001218 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +00001219 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +00001220
Eric Christopherfab289a2011-10-06 00:31:18 +00001221 // If this is just a forward declaration return a special forward-declaration
1222 // debug type since we won't be able to lay out the entire type.
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001223 ObjCInterfaceDecl *Def = ID->getDefinition();
1224 if (!Def) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001225 llvm::DIType FwdDecl =
Eric Christophere908a7a2012-02-20 18:05:04 +00001226 DBuilder.createForwardDecl(llvm::dwarf::DW_TAG_structure_type,
1227 ID->getName(), DefUnit, Line,
1228 RuntimeLang);
Dan Gohman66427b12010-08-23 21:15:56 +00001229 return FwdDecl;
1230 }
Douglas Gregorc8b0c9d2011-12-15 23:32:29 +00001231 ID = Def;
Dan Gohman66427b12010-08-23 21:15:56 +00001232
Eric Christopher4c006e52012-02-16 22:54:45 +00001233 // Bit size, align and offset of the type.
1234 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1235 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001236
Eric Christopher4c006e52012-02-16 22:54:45 +00001237 unsigned Flags = 0;
1238 if (ID->getImplementation())
1239 Flags |= llvm::DIDescriptor::FlagObjcClassComplete;
1240
1241 llvm::DIType RealDecl =
1242 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
1243 Line, Size, Align, Flags,
1244 llvm::DIArray(), RuntimeLang);
1245
Devang Patelf4c205b2009-02-26 21:10:26 +00001246 // Otherwise, insert it into the TypeCache so that recursive uses will find
1247 // it.
Eric Christopher4c006e52012-02-16 22:54:45 +00001248 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = RealDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001249 // Push the struct on region stack.
Eric Christopher4c006e52012-02-16 22:54:45 +00001250 llvm::MDNode *MN = RealDecl;
1251 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
1252
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001253 LexicalBlockStack.push_back(FwdDeclNode);
Eric Christopher4c006e52012-02-16 22:54:45 +00001254 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001255
1256 // Convert all the elements.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001257 SmallVector<llvm::Value *, 16> EltTys;
Devang Patelf4c205b2009-02-26 21:10:26 +00001258
Devang Patel3efd1472010-02-01 21:52:22 +00001259 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001260 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001261 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001262 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001263 if (!SClassTy.isValid())
1264 return llvm::DIType();
1265
Mike Stump11289f42009-09-09 15:08:12 +00001266 llvm::DIType InhTag =
Eric Christopher4c006e52012-02-16 22:54:45 +00001267 DBuilder.createInheritance(RealDecl, SClassTy, 0, 0);
Devang Patelc0f58ea2009-03-10 21:30:26 +00001268 EltTys.push_back(InhTag);
1269 }
1270
Devang Patel37a5c952012-02-07 18:40:30 +00001271 for (ObjCContainerDecl::prop_iterator I = ID->prop_begin(),
1272 E = ID->prop_end(); I != E; ++I) {
1273 const ObjCPropertyDecl *PD = *I;
1274 llvm::MDNode *PropertyNode =
1275 DBuilder.createObjCProperty(PD->getName(),
Devang Patel56a321d2012-02-07 18:55:08 +00001276 getSelectorName(PD->getGetterName()),
1277 getSelectorName(PD->getSetterName()),
1278 PD->getPropertyAttributes());
Devang Patel37a5c952012-02-07 18:40:30 +00001279 EltTys.push_back(PropertyNode);
1280 }
1281
Devang Patel3efd1472010-02-01 21:52:22 +00001282 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001283 unsigned FieldNo = 0;
Fariborz Jahanian885e9df2010-10-01 00:01:53 +00001284 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanian77890872010-10-11 23:55:47 +00001285 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001286 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregor2f53a0b2010-11-30 06:38:09 +00001287 if (!FieldTy.isValid())
1288 return llvm::DIType();
1289
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001290 StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001291
Devang Pateldf348f12009-04-27 22:40:36 +00001292 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001293 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001294 continue;
1295
Devang Patelf4c205b2009-02-26 21:10:26 +00001296 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001297 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1298 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001299 QualType FType = Field->getType();
1300 uint64_t FieldSize = 0;
1301 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001302
Devang Patel9f804932009-03-20 18:24:39 +00001303 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001304
Devang Patel9f804932009-03-20 18:24:39 +00001305 // Bit size, align and offset of the type.
Richard Smithcaf33902011-10-10 18:28:20 +00001306 FieldSize = Field->isBitField()
1307 ? Field->getBitWidthValue(CGM.getContext())
1308 : CGM.getContext().getTypeSize(FType);
1309 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001310 }
1311
Eric Christopherfab289a2011-10-06 00:31:18 +00001312 // We can't know the offset of our ivar in the structure if we're using
1313 // the non-fragile abi and the debugger should ignore the value anyways.
1314 // Call it the FieldNo+1 due to how debuggers use the information,
1315 // e.g. negating the value when it needs a lookup in the dynamic table.
1316 uint64_t FieldOffset = CGM.getLangOptions().ObjCNonFragileABI ? FieldNo+1
1317 : RL.getFieldOffset(FieldNo);
Mike Stump11289f42009-09-09 15:08:12 +00001318
Devang Patelec4bad52009-03-19 00:23:53 +00001319 unsigned Flags = 0;
1320 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Pateldb2732a2010-09-29 21:05:52 +00001321 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelec4bad52009-03-19 00:23:53 +00001322 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Pateldb2732a2010-09-29 21:05:52 +00001323 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001324
Devang Patela21bbb22012-02-04 01:15:04 +00001325 llvm::MDNode *PropertyNode = NULL;
Devang Patel37a5c952012-02-07 18:40:30 +00001326 if (ObjCImplementationDecl *ImpD = ID->getImplementation()) {
Devang Patel5a540652011-09-19 18:54:16 +00001327 if (ObjCPropertyImplDecl *PImpD =
Devang Patel37a5c952012-02-07 18:40:30 +00001328 ImpD->FindPropertyImplIvarDecl(Field->getIdentifier())) {
1329 if (ObjCPropertyDecl *PD = PImpD->getPropertyDecl()) {
Devang Patel56a321d2012-02-07 18:55:08 +00001330 PropertyNode =
1331 DBuilder.createObjCProperty(PD->getName(),
Devang Patel37a5c952012-02-07 18:40:30 +00001332 getSelectorName(PD->getGetterName()),
1333 getSelectorName(PD->getSetterName()),
1334 PD->getPropertyAttributes());
Devang Patel00fca3a2012-02-08 00:10:20 +00001335 }
Devang Patel37a5c952012-02-07 18:40:30 +00001336 }
Devang Patela21bbb22012-02-04 01:15:04 +00001337 }
Devang Patel9d6c8572011-04-16 00:12:55 +00001338 FieldTy = DBuilder.createObjCIVar(FieldName, FieldDefUnit,
1339 FieldLine, FieldSize, FieldAlign,
1340 FieldOffset, Flags, FieldTy,
Devang Patel60fc2422012-02-06 18:20:02 +00001341 PropertyNode);
Devang Patelf4c205b2009-02-26 21:10:26 +00001342 EltTys.push_back(FieldTy);
1343 }
Mike Stump11289f42009-09-09 15:08:12 +00001344
Jay Foaddbf81d82011-04-24 10:11:03 +00001345 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Eric Christopher4c006e52012-02-16 22:54:45 +00001346 RealDecl->replaceOperandWith(10, Elements);
1347
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001348 LexicalBlockStack.pop_back();
Devang Patelf4c205b2009-02-26 21:10:26 +00001349 return RealDecl;
1350}
1351
Nick Lewycky2219ef02011-11-09 04:25:21 +00001352llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty, llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001353 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
Devang Patel0b37e792011-04-08 21:56:52 +00001354 int64_t NumElems = Ty->getNumElements();
1355 int64_t LowerBound = 0;
1356 if (NumElems == 0)
1357 // If number of elements are not known then this is an unbounded array.
1358 // Use Low = 1, Hi = 0 to express such arrays.
1359 LowerBound = 1;
1360 else
Devang Patelb4073382010-02-23 22:59:39 +00001361 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001362
Devang Patel0b37e792011-04-08 21:56:52 +00001363 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(LowerBound, NumElems);
Jay Foaddbf81d82011-04-24 10:11:03 +00001364 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscript);
Devang Patelb4073382010-02-23 22:59:39 +00001365
1366 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1367 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1368
1369 return
Devang Pateld7185b72011-02-22 18:56:36 +00001370 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001371}
1372
Chris Lattneraffb3732008-11-10 06:08:34 +00001373llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001374 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001375 uint64_t Size;
1376 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001377
1378
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001379 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001380 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001381 Size = 0;
1382 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001383 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001384 } else if (Ty->isIncompleteArrayType()) {
1385 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001386 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Devang Patela540f142011-04-04 23:18:38 +00001387 } else if (Ty->isDependentSizedArrayType() || Ty->isIncompleteType()) {
Devang Patel1ffe2342011-04-01 19:02:33 +00001388 Size = 0;
1389 Align = 0;
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001390 } else {
1391 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001392 Size = CGM.getContext().getTypeSize(Ty);
1393 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001394 }
Mike Stump11289f42009-09-09 15:08:12 +00001395
Chris Lattneraffb3732008-11-10 06:08:34 +00001396 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1397 // interior arrays, do we care? Why aren't nested arrays represented the
1398 // obvious/recursive way?
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001399 SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattneraffb3732008-11-10 06:08:34 +00001400 QualType EltTy(Ty, 0);
Devang Patelc0601d12010-10-06 18:30:00 +00001401 if (Ty->isIncompleteArrayType())
Chris Lattneraffb3732008-11-10 06:08:34 +00001402 EltTy = Ty->getElementType();
Devang Patelc0601d12010-10-06 18:30:00 +00001403 else {
1404 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Devang Patel0b37e792011-04-08 21:56:52 +00001405 int64_t UpperBound = 0;
1406 int64_t LowerBound = 0;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001407 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) {
Devang Patelc0601d12010-10-06 18:30:00 +00001408 if (CAT->getSize().getZExtValue())
Devang Patel0b37e792011-04-08 21:56:52 +00001409 UpperBound = CAT->getSize().getZExtValue() - 1;
Nick Lewyckyd85ae782011-04-09 00:25:15 +00001410 } else
Devang Patel0b37e792011-04-08 21:56:52 +00001411 // This is an unbounded array. Use Low = 1, Hi = 0 to express such
1412 // arrays.
1413 LowerBound = 1;
1414
Devang Patelc0601d12010-10-06 18:30:00 +00001415 // FIXME: Verify this is right for VLAs.
Eric Christopherfefafac2011-10-11 23:00:51 +00001416 Subscripts.push_back(DBuilder.getOrCreateSubrange(LowerBound,
1417 UpperBound));
Devang Patelc0601d12010-10-06 18:30:00 +00001418 EltTy = Ty->getElementType();
1419 }
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001420 }
Mike Stump11289f42009-09-09 15:08:12 +00001421
Jay Foaddbf81d82011-04-24 10:11:03 +00001422 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(Subscripts);
Chris Lattneraffb3732008-11-10 06:08:34 +00001423
Devang Patele21912d2009-10-20 19:55:01 +00001424 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001425 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001426 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001427 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001428}
1429
Anders Carlsson443f6772009-11-06 19:19:55 +00001430llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001431 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001432 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1433 Ty, Ty->getPointeeType(), Unit);
1434}
Chris Lattneraffb3732008-11-10 06:08:34 +00001435
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001436llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1437 llvm::DIFile Unit) {
1438 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1439 Ty, Ty->getPointeeType(), Unit);
1440}
1441
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001442llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001443 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001444 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1445 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1446
1447 if (!Ty->getPointeeType()->isFunctionType()) {
1448 // We have a data member pointer type.
1449 return PointerDiffDITy;
1450 }
1451
1452 // We have a member function pointer type. Treat it as a struct with two
1453 // ptrdiff_t members.
1454 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1455
1456 uint64_t FieldOffset = 0;
Devang Patel00afcbe2010-12-08 22:42:58 +00001457 llvm::Value *ElementTypes[2];
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001458
1459 // FIXME: This should probably be a function type instead.
1460 ElementTypes[0] =
Devang Patel15013e72011-06-24 22:00:59 +00001461 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001462 Info.first, Info.second, FieldOffset, 0,
1463 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001464 FieldOffset += Info.first;
1465
1466 ElementTypes[1] =
Devang Patel15013e72011-06-24 22:00:59 +00001467 DBuilder.createMemberType(U, "ptr", U, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001468 Info.first, Info.second, FieldOffset, 0,
1469 PointerDiffDITy);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001470
Jay Foaddbf81d82011-04-24 10:11:03 +00001471 llvm::DIArray Elements = DBuilder.getOrCreateArray(ElementTypes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001472
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001473 return DBuilder.createStructType(U, StringRef("test"),
Devang Patel00afcbe2010-12-08 22:42:58 +00001474 U, 0, FieldOffset,
1475 0, 0, Elements);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001476}
1477
Eli Friedman0dfb8892011-10-06 23:00:33 +00001478llvm::DIType CGDebugInfo::CreateType(const AtomicType *Ty,
1479 llvm::DIFile U) {
1480 // Ignore the atomic wrapping
1481 // FIXME: What is the correct representation?
1482 return getOrCreateType(Ty->getValueType(), U);
1483}
1484
Devang Patel41c20972010-08-23 22:07:25 +00001485/// CreateEnumType - get enumeration type.
Devang Patel283e89d2011-01-17 22:23:07 +00001486llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1487 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001488 SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel41c20972010-08-23 22:07:25 +00001489
1490 // Create DIEnumerator elements for each enumerator.
1491 for (EnumDecl::enumerator_iterator
1492 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1493 Enum != EnumEnd; ++Enum) {
Devang Patel00afcbe2010-12-08 22:42:58 +00001494 Enumerators.push_back(
Devang Pateld7185b72011-02-22 18:56:36 +00001495 DBuilder.createEnumerator(Enum->getName(),
Devang Patel00afcbe2010-12-08 22:42:58 +00001496 Enum->getInitVal().getZExtValue()));
Devang Patel41c20972010-08-23 22:07:25 +00001497 }
1498
1499 // Return a CompositeType for the enum itself.
Jay Foaddbf81d82011-04-24 10:11:03 +00001500 llvm::DIArray EltArray = DBuilder.getOrCreateArray(Enumerators);
Devang Patel41c20972010-08-23 22:07:25 +00001501
1502 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1503 unsigned Line = getLineNumber(ED->getLocation());
1504 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001505 uint64_t Align = 0;
1506 if (!ED->getTypeForDecl()->isIncompleteType()) {
1507 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1508 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1509 }
Devang Patel1bee63f2010-10-27 23:23:58 +00001510 llvm::DIDescriptor EnumContext =
John McCall147d0212011-02-22 22:38:33 +00001511 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel41c20972010-08-23 22:07:25 +00001512 llvm::DIType DbgTy =
Devang Pateld7185b72011-02-22 18:56:36 +00001513 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel00afcbe2010-12-08 22:42:58 +00001514 Size, Align, EltArray);
Devang Patel41c20972010-08-23 22:07:25 +00001515 return DbgTy;
1516}
1517
Douglas Gregor0f139a12009-12-21 20:18:30 +00001518static QualType UnwrapTypeForDebugInfo(QualType T) {
1519 do {
1520 QualType LastT = T;
1521 switch (T->getTypeClass()) {
1522 default:
1523 return T;
1524 case Type::TemplateSpecialization:
1525 T = cast<TemplateSpecializationType>(T)->desugar();
1526 break;
John McCall424cec92011-01-19 06:33:43 +00001527 case Type::TypeOfExpr:
1528 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001529 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001530 case Type::TypeOf:
1531 T = cast<TypeOfType>(T)->getUnderlyingType();
1532 break;
1533 case Type::Decltype:
1534 T = cast<DecltypeType>(T)->getUnderlyingType();
1535 break;
Alexis Hunte852b102011-05-24 22:41:36 +00001536 case Type::UnaryTransform:
1537 T = cast<UnaryTransformType>(T)->getUnderlyingType();
1538 break;
John McCall81904512011-01-06 01:58:22 +00001539 case Type::Attributed:
1540 T = cast<AttributedType>(T)->getEquivalentType();
John McCall4223a9e2011-03-04 04:00:19 +00001541 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001542 case Type::Elaborated:
1543 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001544 break;
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001545 case Type::Paren:
1546 T = cast<ParenType>(T)->getInnerType();
1547 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001548 case Type::SubstTemplateTypeParm:
1549 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1550 break;
Anders Carlsson829c4132011-03-06 16:43:04 +00001551 case Type::Auto:
1552 T = cast<AutoType>(T)->getDeducedType();
1553 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001554 }
1555
1556 assert(T != LastT && "Type unwrapping failed to unwrap!");
1557 if (T == LastT)
1558 return T;
1559 } while (true);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001560}
1561
Eric Christophercd888132011-12-16 23:40:18 +00001562/// getType - Get the type from the cache or return null type if it doesn't exist.
1563llvm::DIType CGDebugInfo::getTypeOrNull(QualType Ty) {
Mike Stump11289f42009-09-09 15:08:12 +00001564
Douglas Gregor0f139a12009-12-21 20:18:30 +00001565 // Unwrap the type as needed for debug information.
1566 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001567
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001568 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001569 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001570 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001571 if (it != TypeCache.end()) {
1572 // Verify that the debug info still exists.
1573 if (&*it->second)
1574 return llvm::DIType(cast<llvm::MDNode>(it->second));
1575 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001576
Eric Christophercd888132011-12-16 23:40:18 +00001577 return llvm::DIType();
1578}
1579
Eric Christopher4c006e52012-02-16 22:54:45 +00001580/// getCompletedTypeOrNull - Get the type from the cache or return null if it
1581/// doesn't exist.
1582llvm::DIType CGDebugInfo::getCompletedTypeOrNull(QualType Ty) {
1583
1584 // Unwrap the type as needed for debug information.
1585 Ty = UnwrapTypeForDebugInfo(Ty);
1586
1587 // Check for existing entry.
1588 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
1589 CompletedTypeCache.find(Ty.getAsOpaquePtr());
1590 if (it != CompletedTypeCache.end()) {
1591 // Verify that the debug info still exists.
1592 if (&*it->second)
1593 return llvm::DIType(cast<llvm::MDNode>(it->second));
1594 }
1595
1596 return llvm::DIType();
1597}
1598
1599
Eric Christophercd888132011-12-16 23:40:18 +00001600/// getOrCreateType - Get the type from the cache or create a new
1601/// one if necessary.
1602llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
1603 if (Ty.isNull())
1604 return llvm::DIType();
1605
1606 // Unwrap the type as needed for debug information.
1607 Ty = UnwrapTypeForDebugInfo(Ty);
Eric Christopher8a41bd82012-02-13 14:56:11 +00001608
Eric Christopher4c006e52012-02-16 22:54:45 +00001609 llvm::DIType T = getCompletedTypeOrNull(Ty);
1610
Eric Christopher8a41bd82012-02-13 14:56:11 +00001611 if (T.Verify()) return T;
Eric Christophercd888132011-12-16 23:40:18 +00001612
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001613 // Otherwise create the type.
1614 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Eric Christopher1c3785a2012-02-18 00:50:17 +00001615
1616 llvm::DIType TC = getTypeOrNull(Ty);
1617 if (TC.Verify() && TC.isForwardDecl())
1618 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), TC));
Eric Christopher4c006e52012-02-16 22:54:45 +00001619
Anders Carlsson6037e782009-11-14 20:52:05 +00001620 // And update the type cache.
Eric Christopher4c006e52012-02-16 22:54:45 +00001621 TypeCache[Ty.getAsOpaquePtr()] = Res;
1622
1623 if (!Res.isForwardDecl())
1624 CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001625 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001626}
1627
Anders Carlsson6037e782009-11-14 20:52:05 +00001628/// CreateTypeNode - Create a new debug type node.
Nick Lewycky6aad6df2011-11-09 04:27:23 +00001629llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001630 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001631 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001632 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001633
Douglas Gregor0915b432009-12-21 19:57:21 +00001634 const char *Diag = 0;
1635
Sanjiv Gupta98070572008-05-25 05:15:42 +00001636 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001637 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001638#define TYPE(Class, Base)
1639#define ABSTRACT_TYPE(Class, Base)
1640#define NON_CANONICAL_TYPE(Class, Base)
1641#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1642#include "clang/AST/TypeNodes.def"
David Blaikie83d382b2011-09-23 05:06:16 +00001643 llvm_unreachable("Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001644
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001645 case Type::ExtVector:
Devang Patelb4073382010-02-23 22:59:39 +00001646 case Type::Vector:
1647 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001648 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001649 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001650 case Type::ObjCObject:
1651 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001652 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001653 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001654 case Type::Builtin:
1655 return CreateType(cast<BuiltinType>(Ty));
1656 case Type::Complex:
1657 return CreateType(cast<ComplexType>(Ty));
1658 case Type::Pointer:
1659 return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001660 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001661 return CreateType(cast<BlockPointerType>(Ty), Unit);
Nick Lewycky2219ef02011-11-09 04:25:21 +00001662 case Type::Typedef:
1663 return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001664 case Type::Record:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001665 return CreateType(cast<RecordType>(Ty));
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001666 case Type::Enum:
Nick Lewycky2219ef02011-11-09 04:25:21 +00001667 return CreateEnumType(cast<EnumType>(Ty)->getDecl());
Chris Lattneraffb3732008-11-10 06:08:34 +00001668 case Type::FunctionProto:
1669 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001670 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001671 case Type::ConstantArray:
1672 case Type::VariableArray:
1673 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001674 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001675
1676 case Type::LValueReference:
1677 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregorb8c7fe92011-01-22 01:58:15 +00001678 case Type::RValueReference:
1679 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001680
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001681 case Type::MemberPointer:
1682 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001683
Eli Friedman0dfb8892011-10-06 23:00:33 +00001684 case Type::Atomic:
1685 return CreateType(cast<AtomicType>(Ty), Unit);
1686
John McCall81904512011-01-06 01:58:22 +00001687 case Type::Attributed:
Douglas Gregor0915b432009-12-21 19:57:21 +00001688 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001689 case Type::Elaborated:
Abramo Bagnara924a8f32010-12-10 16:29:40 +00001690 case Type::Paren:
Douglas Gregor0915b432009-12-21 19:57:21 +00001691 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001692 case Type::TypeOfExpr:
1693 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001694 case Type::Decltype:
Alexis Hunte852b102011-05-24 22:41:36 +00001695 case Type::UnaryTransform:
Richard Smith30482bc2011-02-20 03:19:35 +00001696 case Type::Auto:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001697 llvm_unreachable("type should have been unwrapped!");
Sanjiv Gupta98070572008-05-25 05:15:42 +00001698 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001699
1700 assert(Diag && "Fall through without a diagnostic?");
David Blaikie9c902b52011-09-25 23:23:43 +00001701 unsigned DiagID = CGM.getDiags().getCustomDiagID(DiagnosticsEngine::Error,
Douglas Gregor0915b432009-12-21 19:57:21 +00001702 "debug information for %0 is not yet supported");
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001703 CGM.getDiags().Report(DiagID)
Douglas Gregor0915b432009-12-21 19:57:21 +00001704 << Diag;
1705 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001706}
1707
Eric Christopher4c006e52012-02-16 22:54:45 +00001708/// getOrCreateLimitedType - Get the type from the cache or create a new
1709/// limited type if necessary.
1710llvm::DIType CGDebugInfo::getOrCreateLimitedType(QualType Ty,
1711 llvm::DIFile Unit) {
1712 if (Ty.isNull())
1713 return llvm::DIType();
1714
1715 // Unwrap the type as needed for debug information.
1716 Ty = UnwrapTypeForDebugInfo(Ty);
1717
1718 llvm::DIType T = getTypeOrNull(Ty);
1719
1720 // We may have cached a forward decl when we could have created
1721 // a non-forward decl. Go ahead and create a non-forward decl
1722 // now.
1723 if (T.Verify() && !T.isForwardDecl()) return T;
1724
1725 // Otherwise create the type.
1726 llvm::DIType Res = CreateLimitedTypeNode(Ty, Unit);
1727
Eric Christopher1c3785a2012-02-18 00:50:17 +00001728 if (T.Verify() && T.isForwardDecl())
1729 ReplaceMap.push_back(std::make_pair(Ty.getAsOpaquePtr(), T));
1730
Eric Christopher4c006e52012-02-16 22:54:45 +00001731 // And update the type cache.
1732 TypeCache[Ty.getAsOpaquePtr()] = Res;
1733 return Res;
1734}
1735
1736// TODO: Currently used for context chains when limiting debug info.
1737llvm::DIType CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
1738 RecordDecl *RD = Ty->getDecl();
1739
1740 // Get overall information about the record type for the debug info.
1741 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
1742 unsigned Line = getLineNumber(RD->getLocation());
1743 StringRef RDName = RD->getName();
1744
1745 llvm::DIDescriptor RDContext;
1746 if (CGM.getCodeGenOpts().LimitDebugInfo)
1747 RDContext = createContextChain(cast<Decl>(RD->getDeclContext()));
1748 else
1749 RDContext = getContextDescriptor(cast<Decl>(RD->getDeclContext()));
1750
1751 // If this is just a forward declaration, construct an appropriately
1752 // marked node and just return it.
Eric Christopher1c3785a2012-02-18 00:50:17 +00001753 if (!RD->getDefinition())
1754 return createRecordFwdDecl(RD, RDContext);
Eric Christopher4c006e52012-02-16 22:54:45 +00001755
1756 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1757 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1758 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
1759 llvm::MDNode *RealDecl = NULL;
1760
1761 if (RD->isUnion())
1762 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
1763 Size, Align, 0, llvm::DIArray());
1764 else if (CXXDecl) {
1765 RDName = getClassName(RD);
1766
1767 // FIXME: This could be a struct type giving a default visibility different
1768 // than C++ class type, but needs llvm metadata changes first.
1769 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
1770 Size, Align, 0, 0, llvm::DIType(),
Eric Christopher66562a42012-02-20 18:05:24 +00001771 llvm::DIArray(), llvm::DIType(),
Eric Christopher4c006e52012-02-16 22:54:45 +00001772 llvm::DIArray());
1773 } else
1774 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1775 Size, Align, 0, llvm::DIArray());
1776
1777 RegionMap[Ty->getDecl()] = llvm::WeakVH(RealDecl);
1778 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = llvm::DIType(RealDecl);
1779
1780 if (CXXDecl) {
1781 // A class's primary base or the class itself contains the vtable.
1782 llvm::MDNode *ContainingType = NULL;
1783 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
1784 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1785 // Seek non virtual primary base root.
1786 while (1) {
1787 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1788 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
1789 if (PBT && !BRL.isPrimaryBaseVirtual())
1790 PBase = PBT;
1791 else
1792 break;
1793 }
1794 ContainingType =
1795 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), DefUnit);
1796 }
1797 else if (CXXDecl->isDynamicClass())
1798 ContainingType = RealDecl;
1799
Eric Christopher2939e6e2012-02-17 07:09:48 +00001800 RealDecl->replaceOperandWith(12, ContainingType);
Eric Christopher4c006e52012-02-16 22:54:45 +00001801 }
1802 return llvm::DIType(RealDecl);
1803}
1804
1805/// CreateLimitedTypeNode - Create a new debug type node, but only forward
1806/// declare composite types that haven't been processed yet.
1807llvm::DIType CGDebugInfo::CreateLimitedTypeNode(QualType Ty,llvm::DIFile Unit) {
1808
1809 // Work out details of type.
1810 switch (Ty->getTypeClass()) {
1811#define TYPE(Class, Base)
1812#define ABSTRACT_TYPE(Class, Base)
1813#define NON_CANONICAL_TYPE(Class, Base)
1814#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1815 #include "clang/AST/TypeNodes.def"
1816 llvm_unreachable("Dependent types cannot show up in debug information");
1817
1818 case Type::Record:
1819 return CreateLimitedType(cast<RecordType>(Ty));
1820 default:
1821 return CreateTypeNode(Ty, Unit);
1822 }
1823}
1824
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001825/// CreateMemberType - Create new member and increase Offset by FType's size.
1826llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001827 StringRef Name,
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001828 uint64_t *Offset) {
1829 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1830 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1831 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel15013e72011-06-24 22:00:59 +00001832 llvm::DIType Ty = DBuilder.createMemberType(Unit, Name, Unit, 0,
Devang Patel00afcbe2010-12-08 22:42:58 +00001833 FieldSize, FieldAlign,
1834 *Offset, 0, FieldTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001835 *Offset += FieldSize;
1836 return Ty;
1837}
1838
Devang Patela6cb0642011-04-23 00:08:01 +00001839/// getFunctionDeclaration - Return debug info descriptor to describe method
1840/// declaration for the given method definition.
1841llvm::DISubprogram CGDebugInfo::getFunctionDeclaration(const Decl *D) {
1842 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
1843 if (!FD) return llvm::DISubprogram();
1844
1845 // Setup context.
1846 getContextDescriptor(cast<Decl>(D->getDeclContext()));
1847
Devang Patela3e3fde2011-04-29 23:42:32 +00001848 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001849 MI = SPCache.find(FD->getCanonicalDecl());
Devang Patela3e3fde2011-04-29 23:42:32 +00001850 if (MI != SPCache.end()) {
1851 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1852 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1853 return SP;
1854 }
1855
Devang Patela6cb0642011-04-23 00:08:01 +00001856 for (FunctionDecl::redecl_iterator I = FD->redecls_begin(),
1857 E = FD->redecls_end(); I != E; ++I) {
1858 const FunctionDecl *NextFD = *I;
1859 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001860 MI = SPCache.find(NextFD->getCanonicalDecl());
Devang Patela6cb0642011-04-23 00:08:01 +00001861 if (MI != SPCache.end()) {
1862 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(&*MI->second));
1863 if (SP.isSubprogram() && !llvm::DISubprogram(SP).isDefinition())
1864 return SP;
1865 }
1866 }
1867 return llvm::DISubprogram();
1868}
1869
Devang Patel2780e452011-05-31 20:46:46 +00001870// getOrCreateFunctionType - Construct DIType. If it is a c++ method, include
1871// implicit parameter "this".
Eric Christopherfefafac2011-10-11 23:00:51 +00001872llvm::DIType CGDebugInfo::getOrCreateFunctionType(const Decl * D,
1873 QualType FnType,
Devang Patel2780e452011-05-31 20:46:46 +00001874 llvm::DIFile F) {
1875 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D))
1876 return getOrCreateMethodType(Method, F);
Nick Lewycky16790352011-11-10 00:34:02 +00001877 if (const ObjCMethodDecl *OMethod = dyn_cast<ObjCMethodDecl>(D)) {
Devang Patel7ce99c32011-05-31 21:18:50 +00001878 // Add "self" and "_cmd"
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001879 SmallVector<llvm::Value *, 16> Elts;
Devang Patel7ce99c32011-05-31 21:18:50 +00001880
1881 // First element is always return type. For 'void' functions it is NULL.
Devang Patel5c71c212011-05-31 22:21:11 +00001882 Elts.push_back(getOrCreateType(OMethod->getResultType(), F));
Devang Patel7ce99c32011-05-31 21:18:50 +00001883 // "self" pointer is always first argument.
1884 Elts.push_back(getOrCreateType(OMethod->getSelfDecl()->getType(), F));
1885 // "cmd" pointer is always second argument.
1886 Elts.push_back(getOrCreateType(OMethod->getCmdDecl()->getType(), F));
Devang Patel5c71c212011-05-31 22:21:11 +00001887 // Get rest of the arguments.
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00001888 for (ObjCMethodDecl::param_const_iterator PI = OMethod->param_begin(),
Devang Patel5c71c212011-05-31 22:21:11 +00001889 PE = OMethod->param_end(); PI != PE; ++PI)
1890 Elts.push_back(getOrCreateType((*PI)->getType(), F));
1891
Devang Patel7ce99c32011-05-31 21:18:50 +00001892 llvm::DIArray EltTypeArray = DBuilder.getOrCreateArray(Elts);
1893 return DBuilder.createSubroutineType(F, EltTypeArray);
1894 }
Devang Patel2780e452011-05-31 20:46:46 +00001895 return getOrCreateType(FnType, F);
1896}
1897
Sanjiv Gupta98070572008-05-25 05:15:42 +00001898/// EmitFunctionStart - Constructs the debug code for entering a function -
1899/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001900void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001901 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001902 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001903
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001904 StringRef Name;
1905 StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001906
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001907 FnBeginRegionCount.push_back(LexicalBlockStack.size());
Devang Patel0884a602010-07-22 22:29:16 +00001908
Devang Patel934661e2010-01-14 00:36:21 +00001909 const Decl *D = GD.getDecl();
Eric Christopher7cdf9482011-10-13 21:45:18 +00001910
Devang Patel251f8592010-10-07 22:03:49 +00001911 unsigned Flags = 0;
Devang Patel33ddf692010-10-11 21:58:41 +00001912 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1913 llvm::DIDescriptor FDContext(Unit);
Devang Patelb87c4282011-04-05 22:54:11 +00001914 llvm::DIArray TParamsArray;
Devang Patel934661e2010-01-14 00:36:21 +00001915 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Eric Christopher04832b92011-11-14 18:55:02 +00001916 // If there is a DISubprogram for this function available then use it.
Devang Patel7a12ad02010-01-19 01:54:44 +00001917 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
Eric Christopher459532e2011-11-17 23:45:00 +00001918 FI = SPCache.find(FD->getCanonicalDecl());
Devang Patel7a12ad02010-01-19 01:54:44 +00001919 if (FI != SPCache.end()) {
Gabor Greifbf986082010-09-18 13:00:17 +00001920 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001921 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1922 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001923 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001924 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001925 return;
1926 }
1927 }
Devang Patel934661e2010-01-14 00:36:21 +00001928 Name = getFunctionName(FD);
1929 // Use mangled name as linkage name for c/c++ functions.
Devang Patel04ab75c2011-05-02 22:49:30 +00001930 if (!Fn->hasInternalLinkage())
Devang Patelb7ff0da2011-05-02 22:37:48 +00001931 LinkageName = CGM.getMangledName(GD);
Devang Patelf79199d2010-10-22 17:11:50 +00001932 if (LinkageName == Name)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001933 LinkageName = StringRef();
Devang Patel251f8592010-10-07 22:03:49 +00001934 if (FD->hasPrototype())
1935 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel33ddf692010-10-11 21:58:41 +00001936 if (const NamespaceDecl *NSDecl =
1937 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel8c445292010-12-09 00:33:05 +00001938 FDContext = getOrCreateNameSpace(NSDecl);
Devang Patelf9076f32011-05-17 00:20:09 +00001939 else if (const RecordDecl *RDecl =
1940 dyn_cast_or_null<RecordDecl>(FD->getDeclContext()))
1941 FDContext = getContextDescriptor(cast<Decl>(RDecl->getDeclContext()));
Devang Patelb87c4282011-04-05 22:54:11 +00001942
1943 // Collect template parameters.
1944 TParamsArray = CollectFunctionTemplateParams(FD, Unit);
David Chisnall6bf98ff2010-09-02 17:16:32 +00001945 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnallcf607442010-09-02 18:01:51 +00001946 Name = getObjCMethodName(OMD);
Devang Patel251f8592010-10-07 22:03:49 +00001947 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001948 } else {
Devang Patelf79199d2010-10-22 17:11:50 +00001949 // Use llvm function name.
Devang Patel934661e2010-01-14 00:36:21 +00001950 Name = Fn->getName();
Devang Patel251f8592010-10-07 22:03:49 +00001951 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel934661e2010-01-14 00:36:21 +00001952 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001953 if (!Name.empty() && Name[0] == '\01')
1954 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001955
Devang Patel84715932010-04-24 00:49:16 +00001956 // It is expected that CurLoc is set before using EmitFunctionStart.
1957 // Usually, CurLoc points to the left bracket location of compound
1958 // statement representing function body.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001959 unsigned LineNo = getLineNumber(CurLoc);
Devang Pateldb2732a2010-09-29 21:05:52 +00001960 if (D->isImplicit())
1961 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patela6cb0642011-04-23 00:08:01 +00001962 llvm::DISubprogram SPDecl = getFunctionDeclaration(D);
Chris Lattneraffb3732008-11-10 06:08:34 +00001963 llvm::DISubprogram SP =
Devang Pateld7185b72011-02-22 18:56:36 +00001964 DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
Devang Patel2780e452011-05-31 20:46:46 +00001965 LineNo, getOrCreateFunctionType(D, FnType, Unit),
Devang Patel00afcbe2010-12-08 22:42:58 +00001966 Fn->hasInternalLinkage(), true/*definition*/,
Devang Patelb87c4282011-04-05 22:54:11 +00001967 Flags, CGM.getLangOptions().Optimize, Fn,
Devang Patela6cb0642011-04-23 00:08:01 +00001968 TParamsArray, SPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001969
Sanjiv Gupta98070572008-05-25 05:15:42 +00001970 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001971 llvm::MDNode *SPN = SP;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001972 LexicalBlockStack.push_back(SPN);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001973 RegionMap[D] = llvm::WeakVH(SP);
Eric Christopher4fd315f2011-09-29 00:00:37 +00001974}
Sanjiv Gupta98070572008-05-25 05:15:42 +00001975
Eric Christopherbfa4dc52011-09-29 00:00:41 +00001976/// EmitLocation - Emit metadata to indicate a change in line/column
1977/// information in the source file.
Eric Christopher7cdf9482011-10-13 21:45:18 +00001978void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
1979
1980 // Update our current location
1981 setLocation(Loc);
1982
Sanjiv Gupta98070572008-05-25 05:15:42 +00001983 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001984
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001985 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001986 SourceManager &SM = CGM.getContext().getSourceManager();
Eric Christopher7cdf9482011-10-13 21:45:18 +00001987 if (CurLoc == PrevLoc ||
Chandler Carruth35f53202011-07-25 16:49:02 +00001988 SM.getExpansionLoc(CurLoc) == SM.getExpansionLoc(PrevLoc))
Devang Patela2c048e2010-04-05 21:09:15 +00001989 // New Builder may not be in sync with CGDebugInfo.
1990 if (!Builder.getCurrentDebugLocation().isUnknown())
1991 return;
Eric Christophere6556572011-09-29 00:00:35 +00001992
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001993 // Update last state.
1994 PrevLoc = CurLoc;
1995
Eric Christopher2f45aaa2011-09-29 00:00:45 +00001996 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00001997 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1998 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00001999 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002000}
2001
Eric Christopher7cdf9482011-10-13 21:45:18 +00002002/// CreateLexicalBlock - Creates a new lexical block node and pushes it on
2003/// the stack.
2004void CGDebugInfo::CreateLexicalBlock(SourceLocation Loc) {
Devang Patelb40f2952009-11-13 19:10:24 +00002005 llvm::DIDescriptor D =
Eric Christopher7cdf9482011-10-13 21:45:18 +00002006 DBuilder.createLexicalBlock(LexicalBlockStack.empty() ?
Devang Patel00fca3a2012-02-08 00:10:20 +00002007 llvm::DIDescriptor() :
2008 llvm::DIDescriptor(LexicalBlockStack.back()),
2009 getOrCreateFile(CurLoc),
2010 getLineNumber(CurLoc),
2011 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00002012 llvm::MDNode *DN = D;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002013 LexicalBlockStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002014}
2015
Eric Christopher7cdf9482011-10-13 21:45:18 +00002016/// EmitLexicalBlockStart - Constructs the debug code for entering a declarative
2017/// region - beginning of a DW_TAG_lexical_block.
2018void CGDebugInfo::EmitLexicalBlockStart(CGBuilderTy &Builder, SourceLocation Loc) {
2019 // Set our current location.
2020 setLocation(Loc);
2021
2022 // Create a new lexical block and push it on the stack.
2023 CreateLexicalBlock(Loc);
2024
2025 // Emit a line table change for the current location inside the new scope.
2026 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(Loc),
Devang Patel00fca3a2012-02-08 00:10:20 +00002027 getColumnNumber(Loc),
2028 LexicalBlockStack.back()));
Eric Christopher7cdf9482011-10-13 21:45:18 +00002029}
2030
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002031/// EmitLexicalBlockEnd - Constructs the debug code for exiting a declarative
Eric Christopher9c13eea2011-09-26 15:03:22 +00002032/// region - end of a DW_TAG_lexical_block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002033void CGDebugInfo::EmitLexicalBlockEnd(CGBuilderTy &Builder, SourceLocation Loc) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002034 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002035
Eric Christopher7cdf9482011-10-13 21:45:18 +00002036 // Provide an entry in the line table for the end of the block.
2037 EmitLocation(Builder, Loc);
Mike Stump11289f42009-09-09 15:08:12 +00002038
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002039 LexicalBlockStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00002040}
2041
Devang Patel0884a602010-07-22 22:29:16 +00002042/// EmitFunctionEnd - Constructs the debug code for exiting a function.
2043void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002044 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel0884a602010-07-22 22:29:16 +00002045 unsigned RCount = FnBeginRegionCount.back();
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002046 assert(RCount <= LexicalBlockStack.size() && "Region stack mismatch");
Devang Patel0884a602010-07-22 22:29:16 +00002047
2048 // Pop all regions for this function.
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002049 while (LexicalBlockStack.size() != RCount)
Eric Christopher7cdf9482011-10-13 21:45:18 +00002050 EmitLexicalBlockEnd(Builder, CurLoc);
Devang Patel0884a602010-07-22 22:29:16 +00002051 FnBeginRegionCount.pop_back();
2052}
2053
Devang Patel535fdaf2010-02-10 18:49:08 +00002054// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
2055// See BuildByRefType.
2056llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
2057 uint64_t *XOffset) {
2058
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002059 SmallVector<llvm::Value *, 5> EltTys;
Devang Patel535fdaf2010-02-10 18:49:08 +00002060 QualType FType;
2061 uint64_t FieldSize, FieldOffset;
2062 unsigned FieldAlign;
2063
Devang Patel408dcf62010-03-09 00:44:50 +00002064 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002065 QualType Type = VD->getType();
2066
2067 FieldOffset = 0;
2068 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002069 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
2070 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002071 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002072 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
2073 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
2074
John McCall351762c2011-02-07 10:33:21 +00002075 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel535fdaf2010-02-10 18:49:08 +00002076 if (HasCopyAndDispose) {
2077 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002078 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
2079 &FieldOffset));
2080 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
2081 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002082 }
2083
2084 CharUnits Align = CGM.getContext().getDeclAlign(VD);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002085 if (Align > CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002086 CGM.getContext().getTargetInfo().getPointerAlign(0))) {
Ken Dyck8159c1f2011-04-22 17:34:18 +00002087 CharUnits FieldOffsetInBytes
2088 = CGM.getContext().toCharUnitsFromBits(FieldOffset);
2089 CharUnits AlignedOffsetInBytes
2090 = FieldOffsetInBytes.RoundUpToAlignment(Align);
2091 CharUnits NumPaddingBytes
2092 = AlignedOffsetInBytes - FieldOffsetInBytes;
Devang Patel535fdaf2010-02-10 18:49:08 +00002093
Ken Dyck8159c1f2011-04-22 17:34:18 +00002094 if (NumPaddingBytes.isPositive()) {
2095 llvm::APInt pad(32, NumPaddingBytes.getQuantity());
Devang Patel535fdaf2010-02-10 18:49:08 +00002096 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
2097 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002098 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00002099 }
2100 }
2101
2102 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00002103 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00002104 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8159c1f2011-04-22 17:34:18 +00002105 FieldAlign = CGM.getContext().toBits(Align);
Devang Patel535fdaf2010-02-10 18:49:08 +00002106
2107 *XOffset = FieldOffset;
Devang Patel15013e72011-06-24 22:00:59 +00002108 FieldTy = DBuilder.createMemberType(Unit, VD->getName(), Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002109 0, FieldSize, FieldAlign,
2110 FieldOffset, 0, FieldTy);
Devang Patel535fdaf2010-02-10 18:49:08 +00002111 EltTys.push_back(FieldTy);
2112 FieldOffset += FieldSize;
2113
Jay Foaddbf81d82011-04-24 10:11:03 +00002114 llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
Devang Patel535fdaf2010-02-10 18:49:08 +00002115
Devang Pateldb2732a2010-09-29 21:05:52 +00002116 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel535fdaf2010-02-10 18:49:08 +00002117
Devang Pateld7185b72011-02-22 18:56:36 +00002118 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel00afcbe2010-12-08 22:42:58 +00002119 Elements);
Devang Patel535fdaf2010-02-10 18:49:08 +00002120}
Devang Patel00afcbe2010-12-08 22:42:58 +00002121
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002122/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00002123void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel68a15252011-03-03 20:13:15 +00002124 llvm::Value *Storage,
2125 unsigned ArgNo, CGBuilderTy &Builder) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002126 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Daniel Dunbar380827c2008-10-17 01:07:56 +00002127
Devang Patel408dcf62010-03-09 00:44:50 +00002128 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002129 llvm::DIType Ty;
2130 uint64_t XOffset = 0;
2131 if (VD->hasAttr<BlocksAttr>())
2132 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2133 else
2134 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00002135
Devang Patel67eba802010-05-07 23:05:55 +00002136 // If there is not any debug info for type then do not emit debug info
2137 // for this variable.
2138 if (!Ty)
2139 return;
2140
Devang Patel25468052011-02-16 01:11:51 +00002141 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
2142 // If Storage is an aggregate returned as 'sret' then let debugger know
2143 // about this.
Devang Patel425909d2011-02-10 00:40:52 +00002144 if (Arg->hasStructRetAttr())
Devang Pateld7185b72011-02-22 18:56:36 +00002145 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002146 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
2147 // If an aggregate variable has non trivial destructor or non trivial copy
2148 // constructor than it is pass indirectly. Let debug info know about this
2149 // by using reference of the aggregate type as a argument type.
Eric Christopherfefafac2011-10-11 23:00:51 +00002150 if (!Record->hasTrivialCopyConstructor() ||
2151 !Record->hasTrivialDestructor())
Devang Pateld7185b72011-02-22 18:56:36 +00002152 Ty = DBuilder.createReferenceType(Ty);
Devang Patel25468052011-02-16 01:11:51 +00002153 }
2154 }
Devang Patel425909d2011-02-10 00:40:52 +00002155
Chris Lattneraffb3732008-11-10 06:08:34 +00002156 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002157 unsigned Line = getLineNumber(VD->getLocation());
2158 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patel7c086222010-09-29 23:09:21 +00002159 unsigned Flags = 0;
2160 if (VD->isImplicit())
2161 Flags |= llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002162 llvm::MDNode *Scope = LexicalBlockStack.back();
Devang Patel67f70aa2010-10-12 23:24:54 +00002163
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002164 StringRef Name = VD->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002165 if (!Name.empty()) {
Devang Patelbc474982011-01-11 00:30:27 +00002166 if (VD->hasAttr<BlocksAttr>()) {
2167 CharUnits offset = CharUnits::fromQuantity(32);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002168 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002169 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002170 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002171 // offset of __forwarding field
Ken Dyckbbe38622011-04-22 17:41:34 +00002172 offset = CGM.getContext().toCharUnitsFromBits(
Douglas Gregore8bbc122011-09-02 00:18:52 +00002173 CGM.getContext().getTargetInfo().getPointerWidth(0));
Devang Patelbc474982011-01-11 00:30:27 +00002174 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002175 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2176 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelbc474982011-01-11 00:30:27 +00002177 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002178 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Devang Patelbc474982011-01-11 00:30:27 +00002179 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
2180
2181 // Create the descriptor for the variable.
2182 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002183 DBuilder.createComplexVariable(Tag,
Eric Christopherfefafac2011-10-11 23:00:51 +00002184 llvm::DIDescriptor(Scope),
Devang Patelbc474982011-01-11 00:30:27 +00002185 VD->getName(), Unit, Line, Ty,
Jay Foaddbf81d82011-04-24 10:11:03 +00002186 addr, ArgNo);
Devang Patelbc474982011-01-11 00:30:27 +00002187
2188 // Insert an llvm.dbg.declare into the current block.
2189 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002190 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelbc474982011-01-11 00:30:27 +00002191 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
2192 return;
2193 }
2194 // Create the descriptor for the variable.
Devang Patel67f70aa2010-10-12 23:24:54 +00002195 llvm::DIVariable D =
Devang Pateld7185b72011-02-22 18:56:36 +00002196 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel00afcbe2010-12-08 22:42:58 +00002197 Name, Unit, Line, Ty,
Devang Patel68a15252011-03-03 20:13:15 +00002198 CGM.getLangOptions().Optimize, Flags, ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002199
2200 // Insert an llvm.dbg.declare into the current block.
2201 llvm::Instruction *Call =
Devang Pateld7185b72011-02-22 18:56:36 +00002202 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel67f70aa2010-10-12 23:24:54 +00002203 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel31d1e212010-10-29 16:21:19 +00002204 return;
Devang Patel67f70aa2010-10-12 23:24:54 +00002205 }
2206
2207 // If VD is an anonymous union then Storage represents value for
2208 // all union fields.
John McCall147d0212011-02-22 22:38:33 +00002209 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
2210 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
2211 if (RD->isUnion()) {
2212 for (RecordDecl::field_iterator I = RD->field_begin(),
2213 E = RD->field_end();
2214 I != E; ++I) {
2215 FieldDecl *Field = *I;
2216 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002217 StringRef FieldName = Field->getName();
Devang Patel67f70aa2010-10-12 23:24:54 +00002218
John McCall147d0212011-02-22 22:38:33 +00002219 // Ignore unnamed fields. Do not ignore unnamed records.
2220 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
2221 continue;
Devang Patel67f70aa2010-10-12 23:24:54 +00002222
John McCall147d0212011-02-22 22:38:33 +00002223 // Use VarDecl's Tag, Scope and Line number.
2224 llvm::DIVariable D =
2225 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
2226 FieldName, Unit, Line, FieldTy,
Devang Patel68a15252011-03-03 20:13:15 +00002227 CGM.getLangOptions().Optimize, Flags,
2228 ArgNo);
Devang Patel67f70aa2010-10-12 23:24:54 +00002229
John McCall147d0212011-02-22 22:38:33 +00002230 // Insert an llvm.dbg.declare into the current block.
2231 llvm::Instruction *Call =
2232 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
John McCall147d0212011-02-22 22:38:33 +00002233 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patel67f70aa2010-10-12 23:24:54 +00002234 }
John McCall147d0212011-02-22 22:38:33 +00002235 }
2236 }
Sanjiv Gupta18de6242008-05-30 10:30:31 +00002237}
2238
Devang Patel4f325d12011-04-25 23:43:36 +00002239void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
2240 llvm::Value *Storage,
2241 CGBuilderTy &Builder) {
2242 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, 0, Builder);
2243}
Mike Stump2e722b92009-09-30 02:43:10 +00002244
Devang Patel4f325d12011-04-25 23:43:36 +00002245void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
2246 const VarDecl *VD, llvm::Value *Storage, CGBuilderTy &Builder,
2247 const CGBlockInfo &blockInfo) {
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002248 assert(!LexicalBlockStack.empty() && "Region stack mismatch, stack empty!");
Devang Patel4f325d12011-04-25 23:43:36 +00002249
Devang Patel42fb6f82010-04-26 23:28:46 +00002250 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00002251 return;
Devang Patel4f325d12011-04-25 23:43:36 +00002252
John McCall351762c2011-02-07 10:33:21 +00002253 bool isByRef = VD->hasAttr<BlocksAttr>();
Devang Patel4f325d12011-04-25 23:43:36 +00002254
Mike Stump2e722b92009-09-30 02:43:10 +00002255 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00002256 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00002257 llvm::DIType Ty;
John McCall351762c2011-02-07 10:33:21 +00002258 if (isByRef)
Devang Patel535fdaf2010-02-10 18:49:08 +00002259 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
2260 else
2261 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00002262
2263 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00002264 unsigned Line = getLineNumber(VD->getLocation());
2265 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00002266
John McCall351762c2011-02-07 10:33:21 +00002267 const llvm::TargetData &target = CGM.getTargetData();
2268
2269 CharUnits offset = CharUnits::fromQuantity(
2270 target.getStructLayout(blockInfo.StructureType)
2271 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
2272
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002273 SmallVector<llvm::Value *, 9> addr;
Chris Lattnerece04092012-02-07 00:39:47 +00002274 llvm::Type *Int64Ty = CGM.Int64Ty;
Devang Patel2d6390d2011-02-18 23:29:22 +00002275 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattnerbf784782010-01-25 03:29:35 +00002276 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall351762c2011-02-07 10:33:21 +00002277 if (isByRef) {
Devang Patel2d6390d2011-02-18 23:29:22 +00002278 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2279 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002280 // offset of __forwarding field
Eric Christopherfefafac2011-10-11 23:00:51 +00002281 offset = CGM.getContext()
2282 .toCharUnitsFromBits(target.getPointerSizeInBits());
Chris Lattnerbf784782010-01-25 03:29:35 +00002283 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel2d6390d2011-02-18 23:29:22 +00002284 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
2285 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00002286 // offset of x field
Ken Dyckbbe38622011-04-22 17:41:34 +00002287 offset = CGM.getContext().toCharUnitsFromBits(XOffset);
Chris Lattnerbf784782010-01-25 03:29:35 +00002288 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00002289 }
2290
2291 // Create the descriptor for the variable.
2292 llvm::DIVariable D =
Devang Patel4f325d12011-04-25 23:43:36 +00002293 DBuilder.createComplexVariable(llvm::dwarf::DW_TAG_auto_variable,
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002294 llvm::DIDescriptor(LexicalBlockStack.back()),
Jay Foaddbf81d82011-04-24 10:11:03 +00002295 VD->getName(), Unit, Line, Ty, addr);
Mike Stump2e722b92009-09-30 02:43:10 +00002296 // Insert an llvm.dbg.declare into the current block.
Eric Christopher7cdf9482011-10-13 21:45:18 +00002297 llvm::Instruction *Call =
Devang Patel420c8de2011-04-25 23:52:27 +00002298 DBuilder.insertDeclare(Storage, D, Builder.GetInsertPoint());
Eric Christopher7cdf9482011-10-13 21:45:18 +00002299 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column,
2300 LexicalBlockStack.back()));
Mike Stump2e722b92009-09-30 02:43:10 +00002301}
2302
Chris Lattneraffb3732008-11-10 06:08:34 +00002303/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
2304/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00002305void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel68a15252011-03-03 20:13:15 +00002306 unsigned ArgNo,
Devang Patel25468052011-02-16 01:11:51 +00002307 CGBuilderTy &Builder) {
Devang Patel68a15252011-03-03 20:13:15 +00002308 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, ArgNo, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00002309}
2310
John McCall147d0212011-02-22 22:38:33 +00002311namespace {
2312 struct BlockLayoutChunk {
2313 uint64_t OffsetInBits;
2314 const BlockDecl::Capture *Capture;
2315 };
2316 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
2317 return l.OffsetInBits < r.OffsetInBits;
2318 }
2319}
Chris Lattneraffb3732008-11-10 06:08:34 +00002320
John McCall147d0212011-02-22 22:38:33 +00002321void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
2322 llvm::Value *addr,
2323 CGBuilderTy &Builder) {
2324 ASTContext &C = CGM.getContext();
2325 const BlockDecl *blockDecl = block.getBlockDecl();
2326
2327 // Collect some general information about the block's location.
2328 SourceLocation loc = blockDecl->getCaretLocation();
2329 llvm::DIFile tunit = getOrCreateFile(loc);
2330 unsigned line = getLineNumber(loc);
2331 unsigned column = getColumnNumber(loc);
2332
2333 // Build the debug-info type for the block literal.
Nick Lewyckyfc49f722011-05-02 01:41:48 +00002334 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
John McCall147d0212011-02-22 22:38:33 +00002335
2336 const llvm::StructLayout *blockLayout =
2337 CGM.getTargetData().getStructLayout(block.StructureType);
2338
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002339 SmallVector<llvm::Value*, 16> fields;
John McCall147d0212011-02-22 22:38:33 +00002340 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
2341 blockLayout->getElementOffsetInBits(0),
Devang Patel15013e72011-06-24 22:00:59 +00002342 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002343 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
2344 blockLayout->getElementOffsetInBits(1),
Devang Patel15013e72011-06-24 22:00:59 +00002345 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002346 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
2347 blockLayout->getElementOffsetInBits(2),
Devang Patel15013e72011-06-24 22:00:59 +00002348 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002349 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
2350 blockLayout->getElementOffsetInBits(3),
Devang Patel15013e72011-06-24 22:00:59 +00002351 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002352 fields.push_back(createFieldType("__descriptor",
2353 C.getPointerType(block.NeedsCopyDispose ?
2354 C.getBlockDescriptorExtendedType() :
2355 C.getBlockDescriptorType()),
2356 0, loc, AS_public,
2357 blockLayout->getElementOffsetInBits(4),
Devang Patel15013e72011-06-24 22:00:59 +00002358 tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002359
2360 // We want to sort the captures by offset, not because DWARF
2361 // requires this, but because we're paranoid about debuggers.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002362 SmallVector<BlockLayoutChunk, 8> chunks;
John McCall147d0212011-02-22 22:38:33 +00002363
2364 // 'this' capture.
2365 if (blockDecl->capturesCXXThis()) {
2366 BlockLayoutChunk chunk;
2367 chunk.OffsetInBits =
2368 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2369 chunk.Capture = 0;
2370 chunks.push_back(chunk);
2371 }
2372
2373 // Variable captures.
2374 for (BlockDecl::capture_const_iterator
2375 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2376 i != e; ++i) {
2377 const BlockDecl::Capture &capture = *i;
2378 const VarDecl *variable = capture.getVariable();
2379 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2380
2381 // Ignore constant captures.
2382 if (captureInfo.isConstant())
2383 continue;
2384
2385 BlockLayoutChunk chunk;
2386 chunk.OffsetInBits =
2387 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2388 chunk.Capture = &capture;
2389 chunks.push_back(chunk);
2390 }
2391
2392 // Sort by offset.
2393 llvm::array_pod_sort(chunks.begin(), chunks.end());
2394
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002395 for (SmallVectorImpl<BlockLayoutChunk>::iterator
John McCall147d0212011-02-22 22:38:33 +00002396 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2397 uint64_t offsetInBits = i->OffsetInBits;
2398 const BlockDecl::Capture *capture = i->Capture;
2399
2400 // If we have a null capture, this must be the C++ 'this' capture.
2401 if (!capture) {
2402 const CXXMethodDecl *method =
2403 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2404 QualType type = method->getThisType(C);
2405
2406 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
Devang Patel15013e72011-06-24 22:00:59 +00002407 offsetInBits, tunit, tunit));
John McCall147d0212011-02-22 22:38:33 +00002408 continue;
2409 }
2410
2411 const VarDecl *variable = capture->getVariable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002412 StringRef name = variable->getName();
John McCall81a325e2011-03-02 06:57:14 +00002413
2414 llvm::DIType fieldType;
2415 if (capture->isByRef()) {
2416 std::pair<uint64_t,unsigned> ptrInfo = C.getTypeInfo(C.VoidPtrTy);
2417
2418 // FIXME: this creates a second copy of this type!
2419 uint64_t xoffset;
2420 fieldType = EmitTypeForVarWithBlocksAttr(variable, &xoffset);
2421 fieldType = DBuilder.createPointerType(fieldType, ptrInfo.first);
Devang Patel15013e72011-06-24 22:00:59 +00002422 fieldType = DBuilder.createMemberType(tunit, name, tunit, line,
John McCall81a325e2011-03-02 06:57:14 +00002423 ptrInfo.first, ptrInfo.second,
2424 offsetInBits, 0, fieldType);
2425 } else {
2426 fieldType = createFieldType(name, variable->getType(), 0,
Devang Patel15013e72011-06-24 22:00:59 +00002427 loc, AS_public, offsetInBits, tunit, tunit);
John McCall81a325e2011-03-02 06:57:14 +00002428 }
2429 fields.push_back(fieldType);
John McCall147d0212011-02-22 22:38:33 +00002430 }
2431
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002432 SmallString<36> typeName;
John McCall147d0212011-02-22 22:38:33 +00002433 llvm::raw_svector_ostream(typeName)
2434 << "__block_literal_" << CGM.getUniqueBlockCount();
2435
Jay Foaddbf81d82011-04-24 10:11:03 +00002436 llvm::DIArray fieldsArray = DBuilder.getOrCreateArray(fields);
John McCall147d0212011-02-22 22:38:33 +00002437
2438 llvm::DIType type =
2439 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2440 CGM.getContext().toBits(block.BlockSize),
2441 CGM.getContext().toBits(block.BlockAlign),
2442 0, fieldsArray);
2443 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2444
2445 // Get overall information about the block.
2446 unsigned flags = llvm::DIDescriptor::FlagArtificial;
Eric Christopher2f45aaa2011-09-29 00:00:45 +00002447 llvm::MDNode *scope = LexicalBlockStack.back();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002448 StringRef name = ".block_descriptor";
John McCall147d0212011-02-22 22:38:33 +00002449
2450 // Create the descriptor for the parameter.
2451 llvm::DIVariable debugVar =
2452 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2453 llvm::DIDescriptor(scope),
2454 name, tunit, line, type,
Devang Patel68a15252011-03-03 20:13:15 +00002455 CGM.getLangOptions().Optimize, flags,
2456 cast<llvm::Argument>(addr)->getArgNo() + 1);
John McCall147d0212011-02-22 22:38:33 +00002457
2458 // Insert an llvm.dbg.value into the current block.
2459 llvm::Instruction *declare =
2460 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2461 Builder.GetInsertBlock());
2462 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2463}
Chris Lattneraffb3732008-11-10 06:08:34 +00002464
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002465/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00002466void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00002467 const VarDecl *D) {
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002468 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002469 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002470 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00002471
Eric Christopher7cdf9482011-10-13 21:45:18 +00002472 setLocation(D->getLocation());
2473
Devang Patel7b7f46f2010-02-01 21:34:11 +00002474 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002475 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002476
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002477 // CodeGen turns int[] into int[1] so we'll do the same here.
2478 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002479
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002480 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002481 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002482
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002483 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Nick Lewycky2219ef02011-11-09 04:25:21 +00002484 ArrayType::Normal, 0);
Anders Carlssonf7a9a922008-11-26 17:40:42 +00002485 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002486 StringRef DeclName = D->getName();
2487 StringRef LinkageName;
Devang Patel84d40a42011-02-09 19:16:38 +00002488 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2489 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00002490 LinkageName = Var->getName();
Devang Patelf79199d2010-10-22 17:11:50 +00002491 if (LinkageName == DeclName)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002492 LinkageName = StringRef();
Devang Patel7b7f46f2010-02-01 21:34:11 +00002493 llvm::DIDescriptor DContext =
Devang Patel8c445292010-12-09 00:33:05 +00002494 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Pateld7185b72011-02-22 18:56:36 +00002495 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel00afcbe2010-12-08 22:42:58 +00002496 Unit, LineNo, getOrCreateType(T, Unit),
2497 Var->hasInternalLinkage(), Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00002498}
2499
Devang Patelf4c205b2009-02-26 21:10:26 +00002500/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00002501void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00002502 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00002503 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00002504 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00002505 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00002506
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002507 StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00002508
Devang Patel3efd1472010-02-01 21:52:22 +00002509 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00002510 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00002511
Devang Patelf4c205b2009-02-26 21:10:26 +00002512 // CodeGen turns int[] into int[1] so we'll do the same here.
2513 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00002514
Devang Patelf4c205b2009-02-26 21:10:26 +00002515 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002516 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00002517
Anders Carlsson3efc6e62009-12-06 18:00:51 +00002518 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00002519 ArrayType::Normal, 0);
2520 }
2521
Devang Pateld7185b72011-02-22 18:56:36 +00002522 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel00afcbe2010-12-08 22:42:58 +00002523 getOrCreateType(T, Unit),
2524 Var->hasInternalLinkage(), Var);
Devang Patelf4c205b2009-02-26 21:10:26 +00002525}
Devang Patel973f2eb2010-02-01 19:16:32 +00002526
Devang Pateldc866e12010-08-10 17:53:33 +00002527/// EmitGlobalVariable - Emit global variable's debug info.
2528void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCalla2fabff2010-10-09 01:34:31 +00002529 llvm::Constant *Init) {
Devang Patele03edfd2010-08-10 07:24:25 +00002530 // Create the descriptor for the variable.
2531 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002532 StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00002533 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00002534 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2535 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel283e89d2011-01-17 22:23:07 +00002536 Ty = CreateEnumType(ED);
Devang Patel41c20972010-08-23 22:07:25 +00002537 }
Devang Patel76e3b532010-08-10 18:27:15 +00002538 // Do not use DIGlobalVariable for enums.
2539 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2540 return;
Devang Pateld7185b72011-02-22 18:56:36 +00002541 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel00afcbe2010-12-08 22:42:58 +00002542 getLineNumber(VD->getLocation()),
2543 Ty, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00002544}
2545
Devang Patel973f2eb2010-02-01 19:16:32 +00002546/// getOrCreateNamesSpace - Return namespace descriptor for the given
2547/// namespace decl.
2548llvm::DINameSpace
Devang Patel8c445292010-12-09 00:33:05 +00002549CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patel973f2eb2010-02-01 19:16:32 +00002550 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2551 NameSpaceCache.find(NSDecl);
2552 if (I != NameSpaceCache.end())
2553 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2554
Devang Patelc5ffabc2010-05-12 23:46:38 +00002555 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelfaadd7b2010-10-28 19:12:46 +00002556 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00002557 llvm::DIDescriptor Context =
Devang Patel8c445292010-12-09 00:33:05 +00002558 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patel973f2eb2010-02-01 19:16:32 +00002559 llvm::DINameSpace NS =
Devang Pateld7185b72011-02-22 18:56:36 +00002560 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00002561 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00002562 return NS;
2563}
Eric Christopher1c3785a2012-02-18 00:50:17 +00002564
2565void CGDebugInfo::finalize(void) {
2566 for (std::vector<std::pair<void *, llvm::WeakVH> >::const_iterator VI
2567 = ReplaceMap.begin(), VE = ReplaceMap.end(); VI != VE; ++VI) {
2568 llvm::DIType Ty, RepTy;
2569 // Verify that the debug info still exists.
2570 if (&*VI->second)
2571 Ty = llvm::DIType(cast<llvm::MDNode>(VI->second));
2572
2573 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
2574 TypeCache.find(VI->first);
2575 if (it != TypeCache.end()) {
2576 // Verify that the debug info still exists.
2577 if (&*it->second)
2578 RepTy = llvm::DIType(cast<llvm::MDNode>(it->second));
2579 }
2580
Eric Christopher66562a42012-02-20 18:05:24 +00002581 if (Ty.Verify() && Ty.isForwardDecl() && RepTy.Verify()) {
Eric Christopher1c3785a2012-02-18 00:50:17 +00002582 Ty.replaceAllUsesWith(RepTy);
Eric Christopher66562a42012-02-20 18:05:24 +00002583 }
Eric Christopher1c3785a2012-02-18 00:50:17 +00002584 }
2585 DBuilder.finalize();
2586}