blob: 978542a213a457fce3e19b16849af6511fc7c97b [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
John McCalld16c2cf2011-02-08 08:22:06 +000017#include "CGBlocks.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000018#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000019#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000020#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000021#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000022#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000024#include "clang/Basic/SourceManager.h"
25#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000026#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000028#include "llvm/Constants.h"
29#include "llvm/DerivedTypes.h"
30#include "llvm/Instructions.h"
31#include "llvm/Intrinsics.h"
32#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000033#include "llvm/ADT/StringExtras.h"
34#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Support/Dwarf.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000036#include "llvm/Support/Path.h"
John McCall6b5a61b2011-02-07 10:33:21 +000037#include "llvm/Target/TargetData.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000038#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000039using namespace clang;
40using namespace clang::CodeGen;
41
Anders Carlsson20f12a22009-12-06 18:00:51 +000042CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel823d8e92010-12-08 22:42:58 +000043 : CGM(CGM), DBuilder(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000044 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000045 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000046}
47
Chris Lattner9c85ba32008-11-10 06:08:34 +000048CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000049 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000050}
51
Chris Lattner9c85ba32008-11-10 06:08:34 +000052void CGDebugInfo::setLocation(SourceLocation Loc) {
53 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000054 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000055}
56
Devang Patel33583052010-01-28 23:15:27 +000057/// getContextDescriptor - Get context info for the decl.
Devang Patel170cef32010-12-09 00:33:05 +000058llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000059 if (!Context)
Devang Patel170cef32010-12-09 00:33:05 +000060 return TheCU;
Devang Pateleb6d79b2010-02-01 21:34:11 +000061
62 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
63 I = RegionMap.find(Context);
64 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000065 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000066
Devang Pateleb6d79b2010-02-01 21:34:11 +000067 // Check namespace.
68 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
Devang Patel170cef32010-12-09 00:33:05 +000069 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl));
Devang Patel8b90a782010-05-13 23:52:37 +000070
71 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
72 if (!RDecl->isDependentType()) {
Devang Patela2e57692010-10-28 17:27:32 +000073 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
Devang Patel170cef32010-12-09 00:33:05 +000074 getOrCreateMainFile());
Devang Patel8b90a782010-05-13 23:52:37 +000075 return llvm::DIDescriptor(Ty);
76 }
77 }
Devang Patel170cef32010-12-09 00:33:05 +000078 return TheCU;
Devang Patel979ec2e2009-10-06 00:35:31 +000079}
80
Devang Patel9c6c3a02010-01-14 00:36:21 +000081/// getFunctionName - Get function name for the given FunctionDecl. If the
82/// name is constructred on demand (e.g. C++ destructor) then the name
83/// is stored on the side.
84llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
85 assert (FD && "Invalid FunctionDecl!");
86 IdentifierInfo *FII = FD->getIdentifier();
87 if (FII)
88 return FII->getName();
89
90 // Otherwise construct human readable name for debug info.
91 std::string NS = FD->getNameAsString();
92
93 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000094 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000095 memcpy(StrPtr, NS.data(), NS.length());
96 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000097}
98
David Chisnall52044a22010-09-02 18:01:51 +000099llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
100 llvm::SmallString<256> MethodName;
101 llvm::raw_svector_ostream OS(MethodName);
102 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
103 const DeclContext *DC = OMD->getDeclContext();
Devang Patela2e57692010-10-28 17:27:32 +0000104 if (const ObjCImplementationDecl *OID =
105 dyn_cast<const ObjCImplementationDecl>(DC)) {
David Chisnall52044a22010-09-02 18:01:51 +0000106 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000107 } else if (const ObjCInterfaceDecl *OID =
108 dyn_cast<const ObjCInterfaceDecl>(DC)) {
Fariborz Jahanian1a4c9372010-10-18 17:51:06 +0000109 OS << OID->getName();
Devang Patela2e57692010-10-28 17:27:32 +0000110 } else if (const ObjCCategoryImplDecl *OCD =
111 dyn_cast<const ObjCCategoryImplDecl>(DC)){
David Chisnall52044a22010-09-02 18:01:51 +0000112 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
113 OCD->getIdentifier()->getNameStart() << ')';
114 }
115 OS << ' ' << OMD->getSelector().getAsString() << ']';
116
117 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
118 memcpy(StrPtr, MethodName.begin(), OS.tell());
119 return llvm::StringRef(StrPtr, OS.tell());
120}
121
Devang Patel700a1cb2010-07-20 20:24:18 +0000122/// getClassName - Get class name including template argument list.
123llvm::StringRef
124CGDebugInfo::getClassName(RecordDecl *RD) {
125 ClassTemplateSpecializationDecl *Spec
126 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
127 if (!Spec)
128 return RD->getName();
129
130 const TemplateArgument *Args;
131 unsigned NumArgs;
132 std::string Buffer;
133 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
134 const TemplateSpecializationType *TST =
135 cast<TemplateSpecializationType>(TAW->getType());
136 Args = TST->getArgs();
137 NumArgs = TST->getNumArgs();
138 } else {
139 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor910f8002010-11-07 23:05:16 +0000140 Args = TemplateArgs.data();
141 NumArgs = TemplateArgs.size();
Devang Patel700a1cb2010-07-20 20:24:18 +0000142 }
143 Buffer = RD->getIdentifier()->getNameStart();
144 PrintingPolicy Policy(CGM.getLangOptions());
145 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
146 NumArgs,
147 Policy);
148
149 // Copy this name on the side and use its reference.
150 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
151 memcpy(StrPtr, Buffer.data(), Buffer.length());
152 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000153}
154
Devang Patel17800552010-03-09 00:44:50 +0000155/// getOrCreateFile - Get the file debug info descriptor for the input location.
156llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Devang Patel823d8e92010-12-08 22:42:58 +0000157 if (!Loc.isValid())
158 // If Location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000159 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel823d8e92010-12-08 22:42:58 +0000160
Anders Carlsson20f12a22009-12-06 18:00:51 +0000161 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000162 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000163
Devang Patelbb6f5482011-02-16 18:40:36 +0000164 if (PLoc.isInvalid() || llvm::StringRef(PLoc.getFilename()).empty())
Douglas Gregor8c457a82010-11-11 20:45:16 +0000165 // If the location is not valid then use main input file.
Devang Patel16674e82011-02-22 18:56:36 +0000166 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Douglas Gregor8c457a82010-11-11 20:45:16 +0000167
Ted Kremenek9c250392010-03-30 00:27:51 +0000168 // Cache the results.
169 const char *fname = PLoc.getFilename();
170 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
171 DIFileCache.find(fname);
172
173 if (it != DIFileCache.end()) {
174 // Verify that the information still exists.
175 if (&*it->second)
176 return llvm::DIFile(cast<llvm::MDNode>(it->second));
177 }
178
Devang Patel16674e82011-02-22 18:56:36 +0000179 llvm::DIFile F = DBuilder.createFile(PLoc.getFilename(), getCurrentDirname());
Ted Kremenek9c250392010-03-30 00:27:51 +0000180
Devang Patelab699792010-05-07 18:12:35 +0000181 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000182 return F;
183
Devang Patel17800552010-03-09 00:44:50 +0000184}
Devang Patel8ab870d2010-05-12 23:46:38 +0000185
Devang Patel532105f2010-10-28 22:03:20 +0000186/// getOrCreateMainFile - Get the file info for main compile unit.
187llvm::DIFile CGDebugInfo::getOrCreateMainFile() {
Devang Patel16674e82011-02-22 18:56:36 +0000188 return DBuilder.createFile(TheCU.getFilename(), TheCU.getDirectory());
Devang Patel532105f2010-10-28 22:03:20 +0000189}
190
Devang Patel8ab870d2010-05-12 23:46:38 +0000191/// getLineNumber - Get line number for the location. If location is invalid
192/// then use current location.
193unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
194 assert (CurLoc.isValid() && "Invalid current location!");
195 SourceManager &SM = CGM.getContext().getSourceManager();
196 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000197 return PLoc.isValid()? PLoc.getLine() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000198}
199
200/// getColumnNumber - Get column number for the location. If location is
201/// invalid then use current location.
202unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
203 assert (CurLoc.isValid() && "Invalid current location!");
204 SourceManager &SM = CGM.getContext().getSourceManager();
205 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
Douglas Gregor8c457a82010-11-11 20:45:16 +0000206 return PLoc.isValid()? PLoc.getColumn() : 0;
Devang Patel8ab870d2010-05-12 23:46:38 +0000207}
208
Devang Patelac4d13c2010-07-27 15:17:16 +0000209llvm::StringRef CGDebugInfo::getCurrentDirname() {
210 if (!CWDName.empty())
211 return CWDName;
212 char *CompDirnamePtr = NULL;
213 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
214 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
215 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
216 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
217}
218
Devang Patel17800552010-03-09 00:44:50 +0000219/// CreateCompileUnit - Create new compile unit.
220void CGDebugInfo::CreateCompileUnit() {
221
222 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000223 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000224 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
225 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000226 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000227
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000228 // The main file name provided via the "-main-file-name" option contains just
229 // the file name itself with no path information. This file name may have had
230 // a relative path, so we look into the actual file entry for the main
231 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000232 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000233 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000234 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000235 if (MainFileDir != ".")
236 MainFileName = MainFileDir + "/" + MainFileName;
237 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000238
Devang Patelac4d13c2010-07-27 15:17:16 +0000239 // Save filename string.
240 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
241 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
242 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
243
Chris Lattner515455a2009-03-25 03:28:08 +0000244 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000245 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000246 if (LO.CPlusPlus) {
247 if (LO.ObjC1)
248 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
249 else
250 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
251 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000252 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000253 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000254 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000255 } else {
256 LangTag = llvm::dwarf::DW_LANG_C89;
257 }
Devang Patel446c6192009-04-17 21:06:59 +0000258
Daniel Dunbar19f19832010-08-24 17:41:09 +0000259 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000260
261 // Figure out which version of the ObjC runtime we have.
262 unsigned RuntimeVers = 0;
263 if (LO.ObjC1)
264 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000266 // Create new compile unit.
Devang Patel16674e82011-02-22 18:56:36 +0000267 DBuilder.createCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000268 LangTag, Filename, getCurrentDirname(),
Devang Patel823d8e92010-12-08 22:42:58 +0000269 Producer,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000270 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Devang Patel823d8e92010-12-08 22:42:58 +0000271 // FIXME - Eliminate TheCU.
272 TheCU = llvm::DICompileUnit(DBuilder.getCU());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000273}
274
Devang Patel65e99f22009-02-25 01:36:11 +0000275/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000276/// one if necessary.
Devang Patelf1d1d9a2010-11-01 16:52:40 +0000277llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000278 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000279 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000280 switch (BT->getKind()) {
281 default:
282 case BuiltinType::Void:
283 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000284 case BuiltinType::ObjCClass:
Devang Patel16674e82011-02-22 18:56:36 +0000285 return DBuilder.createStructType(TheCU, "objc_class",
Devang Patel823d8e92010-12-08 22:42:58 +0000286 getOrCreateMainFile(), 0, 0, 0,
287 llvm::DIDescriptor::FlagFwdDecl,
288 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000289 case BuiltinType::ObjCId: {
290 // typedef struct objc_class *Class;
291 // typedef struct objc_object {
292 // Class isa;
293 // } *id;
294
295 llvm::DIType OCTy =
Devang Patel16674e82011-02-22 18:56:36 +0000296 DBuilder.createStructType(TheCU, "objc_class",
Devang Patel823d8e92010-12-08 22:42:58 +0000297 getOrCreateMainFile(), 0, 0, 0,
298 llvm::DIDescriptor::FlagFwdDecl,
299 llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000300 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
301
Devang Patel16674e82011-02-22 18:56:36 +0000302 llvm::DIType ISATy = DBuilder.createPointerType(OCTy, Size);
Devang Patelc8972c62010-07-28 01:33:15 +0000303
Devang Patel823d8e92010-12-08 22:42:58 +0000304 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patelc8972c62010-07-28 01:33:15 +0000305 llvm::DIType FieldTy =
Devang Patel16674e82011-02-22 18:56:36 +0000306 DBuilder.createMemberType("isa", getOrCreateMainFile(),
Devang Patel823d8e92010-12-08 22:42:58 +0000307 0,Size, 0, 0, 0, ISATy);
Devang Patelc8972c62010-07-28 01:33:15 +0000308 EltTys.push_back(FieldTy);
309 llvm::DIArray Elements =
Devang Patel16674e82011-02-22 18:56:36 +0000310 DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelc8972c62010-07-28 01:33:15 +0000311
Devang Patel16674e82011-02-22 18:56:36 +0000312 return DBuilder.createStructType(TheCU, "objc_object",
Devang Patel823d8e92010-12-08 22:42:58 +0000313 getOrCreateMainFile(),
314 0, 0, 0, 0, Elements);
Devang Patelc8972c62010-07-28 01:33:15 +0000315 }
Devang Patel6e108ce2011-02-09 03:15:05 +0000316 case BuiltinType::ObjCSel: {
Devang Patel16674e82011-02-22 18:56:36 +0000317 return DBuilder.createStructType(TheCU, "objc_selector",
Devang Patel6e108ce2011-02-09 03:15:05 +0000318 getOrCreateMainFile(), 0, 0, 0,
319 llvm::DIDescriptor::FlagFwdDecl,
320 llvm::DIArray());
321 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322 case BuiltinType::UChar:
323 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
324 case BuiltinType::Char_S:
325 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
326 case BuiltinType::UShort:
327 case BuiltinType::UInt:
328 case BuiltinType::ULong:
329 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
330 case BuiltinType::Short:
331 case BuiltinType::Int:
332 case BuiltinType::Long:
333 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
334 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
335 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000336 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000337 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000338 }
Devang Patel05127ca2010-07-28 23:23:29 +0000339
340 switch (BT->getKind()) {
341 case BuiltinType::Long: BTName = "long int"; break;
342 case BuiltinType::LongLong: BTName = "long long int"; break;
343 case BuiltinType::ULong: BTName = "long unsigned int"; break;
344 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
345 default:
346 BTName = BT->getName(CGM.getContext().getLangOptions());
347 break;
348 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000349 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000350 uint64_t Size = CGM.getContext().getTypeSize(BT);
351 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Devang Patelca80a5f2009-10-20 19:55:01 +0000352 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000353 DBuilder.createBasicType(BTName, Size, Align, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000354 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000355}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000356
Devang Patel344ff5d2010-12-09 00:25:29 +0000357llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000358 // Bit size, align and offset of the type.
359 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
360 if (Ty->isComplexIntegerType())
361 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 uint64_t Size = CGM.getContext().getTypeSize(Ty);
364 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Devang Patelca80a5f2009-10-20 19:55:01 +0000365 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +0000366 DBuilder.createBasicType("complex", Size, Align, Encoding);
Devang Patel823d8e92010-12-08 22:42:58 +0000367
Devang Patelca80a5f2009-10-20 19:55:01 +0000368 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000369}
370
John McCalla1805292009-09-25 01:40:47 +0000371/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000372/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000373llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000374 QualifierCollector Qc;
375 const Type *T = Qc.strip(Ty);
376
377 // Ignore these qualifiers for now.
378 Qc.removeObjCGCAttr();
379 Qc.removeAddressSpace();
380
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 // We will create one Derived type for one qualifier and recurse to handle any
382 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000383 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000384 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000386 Qc.removeConst();
387 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000388 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000389 Qc.removeVolatile();
390 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000391 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000392 Qc.removeRestrict();
393 } else {
394 assert(Qc.empty() && "Unknown type qualifier for debug info");
395 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000396 }
Mike Stump1eb44332009-09-09 15:08:12 +0000397
John McCall49f4e1c2010-12-10 11:01:00 +0000398 llvm::DIType FromTy = getOrCreateType(Qc.apply(CGM.getContext(), T), Unit);
John McCalla1805292009-09-25 01:40:47 +0000399
Daniel Dunbar3845f862008-10-31 03:54:29 +0000400 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
401 // CVR derived types.
Devang Patel16674e82011-02-22 18:56:36 +0000402 llvm::DIType DbgTy = DBuilder.createQualifiedType(Tag, FromTy);
Devang Patel823d8e92010-12-08 22:42:58 +0000403
Devang Patelca80a5f2009-10-20 19:55:01 +0000404 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000405}
406
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000407llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000408 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000409 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000410 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
411 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000412 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000413}
414
Chris Lattner9c85ba32008-11-10 06:08:34 +0000415llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000416 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000417 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
418 Ty->getPointeeType(), Unit);
419}
420
Devang Patelc69e1cf2010-09-30 19:05:55 +0000421/// CreatePointeeType - Create PointTee type. If Pointee is a record
422/// then emit record's fwd if debug info size reduction is enabled.
423llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
424 llvm::DIFile Unit) {
425 if (!CGM.getCodeGenOpts().LimitDebugInfo)
426 return getOrCreateType(PointeeTy, Unit);
427
428 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
429 RecordDecl *RD = RTy->getDecl();
Devang Patelc69e1cf2010-09-30 19:05:55 +0000430 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
431 unsigned Line = getLineNumber(RD->getLocation());
432 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +0000433 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +0000434
435 if (RD->isStruct())
Devang Patel16674e82011-02-22 18:56:36 +0000436 return DBuilder.createStructType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000437 Line, 0, 0, llvm::DIType::FlagFwdDecl,
438 llvm::DIArray());
439 else if (RD->isUnion())
Devang Patel16674e82011-02-22 18:56:36 +0000440 return DBuilder.createUnionType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000441 Line, 0, 0, llvm::DIType::FlagFwdDecl,
442 llvm::DIArray());
443 else {
444 assert(RD->isClass() && "Unknown RecordType!");
Devang Patel16674e82011-02-22 18:56:36 +0000445 return DBuilder.createClassType(FDContext, RD->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000446 Line, 0, 0, 0, llvm::DIType::FlagFwdDecl,
447 llvm::DIType(), llvm::DIArray());
448 }
Devang Patelc69e1cf2010-09-30 19:05:55 +0000449 }
450 return getOrCreateType(PointeeTy, Unit);
451
452}
453
Anders Carlssona031b352009-11-06 19:19:55 +0000454llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
455 const Type *Ty,
456 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000457 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000458
459 if (Tag == llvm::dwarf::DW_TAG_reference_type)
Devang Patel16674e82011-02-22 18:56:36 +0000460 return DBuilder.createReferenceType(CreatePointeeType(PointeeTy, Unit));
Devang Patel823d8e92010-12-08 22:42:58 +0000461
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000462 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000463 // Size is always the size of a pointer. We can't use getTypeSize here
464 // because that does not return the correct value for references.
465 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000466 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
467 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Devang Patel823d8e92010-12-08 22:42:58 +0000469 return
Devang Patel16674e82011-02-22 18:56:36 +0000470 DBuilder.createPointerType(CreatePointeeType(PointeeTy, Unit), Size, Align);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000471}
472
Mike Stump9bc093c2009-05-14 02:03:51 +0000473llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000474 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000475 if (BlockLiteralGenericSet)
476 return BlockLiteralGeneric;
477
Devang Patel823d8e92010-12-08 22:42:58 +0000478 llvm::SmallVector<llvm::Value *, 8> EltTys;
Mike Stump9bc093c2009-05-14 02:03:51 +0000479 llvm::DIType FieldTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000480 QualType FType;
481 uint64_t FieldSize, FieldOffset;
482 unsigned FieldAlign;
Mike Stump9bc093c2009-05-14 02:03:51 +0000483 llvm::DIArray Elements;
484 llvm::DIType EltTy, DescTy;
485
486 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000487 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000488 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
489 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000490
Devang Patel16674e82011-02-22 18:56:36 +0000491 Elements = DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000492 EltTys.clear();
493
Devang Patele2472482010-09-29 21:05:52 +0000494 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000495 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000496
Devang Patel16674e82011-02-22 18:56:36 +0000497 EltTy = DBuilder.createStructType(Unit, "__block_descriptor",
Devang Patel823d8e92010-12-08 22:42:58 +0000498 Unit, LineNo, FieldOffset, 0,
499 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Mike Stump9bc093c2009-05-14 02:03:51 +0000501 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000502 uint64_t Size = CGM.getContext().getTypeSize(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Devang Patel16674e82011-02-22 18:56:36 +0000504 DescTy = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000505
506 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000507 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000508 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000509 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000510 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
511 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000512 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000513 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000514
Anders Carlsson20f12a22009-12-06 18:00:51 +0000515 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000516 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000517 FieldSize = CGM.getContext().getTypeSize(Ty);
518 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Devang Patel16674e82011-02-22 18:56:36 +0000519 FieldTy = DBuilder.createMemberType("__descriptor", Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000520 LineNo, FieldSize, FieldAlign,
521 FieldOffset, 0, FieldTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000522 EltTys.push_back(FieldTy);
523
524 FieldOffset += FieldSize;
Devang Patel16674e82011-02-22 18:56:36 +0000525 Elements = DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000526
Devang Patel16674e82011-02-22 18:56:36 +0000527 EltTy = DBuilder.createStructType(Unit, "__block_literal_generic",
Devang Patel823d8e92010-12-08 22:42:58 +0000528 Unit, LineNo, FieldOffset, 0,
529 Flags, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Mike Stump9bc093c2009-05-14 02:03:51 +0000531 BlockLiteralGenericSet = true;
Devang Patel16674e82011-02-22 18:56:36 +0000532 BlockLiteralGeneric = DBuilder.createPointerType(EltTy, Size);
Mike Stump9bc093c2009-05-14 02:03:51 +0000533 return BlockLiteralGeneric;
534}
535
Chris Lattner9c85ba32008-11-10 06:08:34 +0000536llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000537 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000538 // Typedefs are derived from some other type. If we have a typedef of a
539 // typedef, make sure to emit the whole chain.
540 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Devang Patel823d8e92010-12-08 22:42:58 +0000541 if (!Src.Verify())
542 return llvm::DIType();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000543 // We don't set size information, but do specify where the typedef was
544 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000545 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Devang Patel16674e82011-02-22 18:56:36 +0000546 llvm::DIType DbgTy = DBuilder.createTypedef(Src, Ty->getDecl()->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +0000547 Unit, Line);
Devang Patelca80a5f2009-10-20 19:55:01 +0000548 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000549}
550
Chris Lattner9c85ba32008-11-10 06:08:34 +0000551llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000552 llvm::DIFile Unit) {
Devang Patel823d8e92010-12-08 22:42:58 +0000553 llvm::SmallVector<llvm::Value *, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000554
Chris Lattner9c85ba32008-11-10 06:08:34 +0000555 // Add the result type at least.
556 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner9c85ba32008-11-10 06:08:34 +0000558 // Set up remainder of arguments if there is a prototype.
559 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000560 if (isa<FunctionNoProtoType>(Ty))
Devang Patel16674e82011-02-22 18:56:36 +0000561 EltTys.push_back(DBuilder.createUnspecifiedParameter());
Devang Patelaf164bb2010-10-06 20:51:45 +0000562 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
564 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000565 }
566
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 llvm::DIArray EltTypeArray =
Devang Patel16674e82011-02-22 18:56:36 +0000568 DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Devang Patel16674e82011-02-22 18:56:36 +0000570 llvm::DIType DbgTy = DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000571 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000572}
573
John McCall8178df32011-02-22 22:38:33 +0000574llvm::DIType CGDebugInfo::createFieldType(llvm::StringRef name,
575 QualType type,
576 Expr *bitWidth,
577 SourceLocation loc,
578 AccessSpecifier AS,
579 uint64_t offsetInBits,
580 llvm::DIFile tunit) {
581 llvm::DIType debugType = getOrCreateType(type, tunit);
582
583 // Get the location for the field.
584 llvm::DIFile file = getOrCreateFile(loc);
585 unsigned line = getLineNumber(loc);
586
587 uint64_t sizeInBits = 0;
588 unsigned alignInBits = 0;
589 if (!type->isIncompleteArrayType()) {
590 llvm::tie(sizeInBits, alignInBits) = CGM.getContext().getTypeInfo(type);
591
592 if (bitWidth)
593 sizeInBits = bitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
594 }
595
596 unsigned flags = 0;
597 if (AS == clang::AS_private)
598 flags |= llvm::DIDescriptor::FlagPrivate;
599 else if (AS == clang::AS_protected)
600 flags |= llvm::DIDescriptor::FlagProtected;
601
602 return DBuilder.createMemberType(name, file, line, sizeInBits, alignInBits,
603 offsetInBits, flags, debugType);
604}
605
Devang Patel428deb52010-01-19 00:00:59 +0000606/// CollectRecordFields - A helper function to collect debug info for
607/// record fields. This is used while creating debug info entry for a Record.
608void CGDebugInfo::
John McCall8178df32011-02-22 22:38:33 +0000609CollectRecordFields(const RecordDecl *record, llvm::DIFile tunit,
610 llvm::SmallVectorImpl<llvm::Value *> &elements) {
611 unsigned fieldNo = 0;
612 const ASTRecordLayout &layout = CGM.getContext().getASTRecordLayout(record);
613 for (RecordDecl::field_iterator I = record->field_begin(),
614 E = record->field_end();
615 I != E; ++I, ++fieldNo) {
616 FieldDecl *field = *I;
Devang Patel428deb52010-01-19 00:00:59 +0000617
John McCall8178df32011-02-22 22:38:33 +0000618 llvm::StringRef name = field->getName();
619 QualType type = field->getType();
620
621 // Ignore unnamed fields unless they're anonymous structs/unions.
622 if (name.empty() && !type->isRecordType())
Devang Patel428deb52010-01-19 00:00:59 +0000623 continue;
624
John McCall8178df32011-02-22 22:38:33 +0000625 llvm::DIType fieldType
626 = createFieldType(name, type, field->getBitWidth(),
627 field->getLocation(), field->getAccess(),
628 layout.getFieldOffset(fieldNo), tunit);
Devang Patel428deb52010-01-19 00:00:59 +0000629
John McCall8178df32011-02-22 22:38:33 +0000630 elements.push_back(fieldType);
Devang Patel428deb52010-01-19 00:00:59 +0000631 }
632}
633
Devang Patela6da1922010-01-28 00:28:01 +0000634/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
635/// function type is not updated to include implicit "this" pointer. Use this
636/// routine to get a method type which includes "this" pointer.
637llvm::DIType
638CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000639 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000640 llvm::DIType FnTy
641 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
642 0),
643 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000644
Devang Patela6da1922010-01-28 00:28:01 +0000645 // Add "this" pointer.
646
Devang Patelab699792010-05-07 18:12:35 +0000647 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000648 assert (Args.getNumElements() && "Invalid number of arguments!");
649
Devang Patel823d8e92010-12-08 22:42:58 +0000650 llvm::SmallVector<llvm::Value *, 16> Elts;
Devang Patela6da1922010-01-28 00:28:01 +0000651
652 // First element is always return type. For 'void' functions it is NULL.
653 Elts.push_back(Args.getElement(0));
654
Devang Patel2ed8f002010-08-27 17:47:47 +0000655 if (!Method->isStatic())
656 {
657 // "this" pointer is always first argument.
658 ASTContext &Context = CGM.getContext();
659 QualType ThisPtr =
660 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
661 llvm::DIType ThisPtrType =
Devang Patel16674e82011-02-22 18:56:36 +0000662 DBuilder.createArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000663
Devang Patel2ed8f002010-08-27 17:47:47 +0000664 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
665 Elts.push_back(ThisPtrType);
666 }
Devang Patela6da1922010-01-28 00:28:01 +0000667
668 // Copy rest of the arguments.
669 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
670 Elts.push_back(Args.getElement(i));
671
672 llvm::DIArray EltTypeArray =
Devang Patel16674e82011-02-22 18:56:36 +0000673 DBuilder.getOrCreateArray(Elts.data(), Elts.size());
Devang Patela6da1922010-01-28 00:28:01 +0000674
Devang Patel16674e82011-02-22 18:56:36 +0000675 return DBuilder.createSubroutineType(Unit, EltTypeArray);
Devang Patela6da1922010-01-28 00:28:01 +0000676}
677
Devang Patel58faf202010-10-22 17:11:50 +0000678/// isFunctionLocalClass - Return true if CXXRecordDecl is defined
679/// inside a function.
680static bool isFunctionLocalClass(const CXXRecordDecl *RD) {
681 if (const CXXRecordDecl *NRD =
682 dyn_cast<CXXRecordDecl>(RD->getDeclContext()))
683 return isFunctionLocalClass(NRD);
684 else if (isa<FunctionDecl>(RD->getDeclContext()))
685 return true;
686 return false;
687
688}
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000689/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
690/// a single member function GlobalDecl.
691llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000692CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000693 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000694 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000695 bool IsCtorOrDtor =
696 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
697
698 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000699 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000700
701 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
702 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000703 llvm::StringRef MethodLinkageName;
Devang Patel58faf202010-10-22 17:11:50 +0000704 if (!IsCtorOrDtor && !isFunctionLocalClass(Method->getParent()))
Anders Carlsson9a20d552010-06-22 16:16:50 +0000705 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000706
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000707 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000708 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
709 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000710
711 // Collect virtual method info.
712 llvm::DIType ContainingType;
713 unsigned Virtuality = 0;
714 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000715
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000716 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000717 if (Method->isPure())
718 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
719 else
720 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
721
722 // It doesn't make sense to give a virtual destructor a vtable index,
723 // since a single destructor has two entries in the vtable.
724 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000725 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000726 ContainingType = RecordTy;
727 }
728
Devang Patele2472482010-09-29 21:05:52 +0000729 unsigned Flags = 0;
730 if (Method->isImplicit())
731 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000732 AccessSpecifier Access = Method->getAccess();
733 if (Access == clang::AS_private)
734 Flags |= llvm::DIDescriptor::FlagPrivate;
735 else if (Access == clang::AS_protected)
736 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000737 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
738 if (CXXC->isExplicit())
739 Flags |= llvm::DIDescriptor::FlagExplicit;
740 } else if (const CXXConversionDecl *CXXC =
741 dyn_cast<CXXConversionDecl>(Method)) {
742 if (CXXC->isExplicit())
743 Flags |= llvm::DIDescriptor::FlagExplicit;
744 }
Devang Patel3951e712010-10-07 22:03:49 +0000745 if (Method->hasPrototype())
746 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000747
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000748 llvm::DISubprogram SP =
Devang Patel16674e82011-02-22 18:56:36 +0000749 DBuilder.createMethod(RecordTy , MethodName, MethodLinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +0000750 MethodDefUnit, MethodLine,
751 MethodTy, /*isLocalToUnit=*/false,
752 /* isDefinition=*/ false,
753 Virtuality, VIndex, ContainingType,
754 Flags, CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000755
756 // Don't cache ctors or dtors since we have to emit multiple functions for
757 // a single ctor or dtor.
758 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000759 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000760
761 return SP;
762}
763
Devang Patel4125fd22010-01-19 01:54:44 +0000764/// CollectCXXMemberFunctions - A helper function to collect debug info for
765/// C++ member functions.This is used while creating debug info entry for
766/// a Record.
767void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000768CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000769 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000770 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000771 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
772 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000773 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000774
Devang Pateld5322da2010-02-09 19:09:28 +0000775 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000776 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000777
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000778 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000779 }
780}
781
Devang Patel2ed8f002010-08-27 17:47:47 +0000782/// CollectCXXFriends - A helper function to collect debug info for
783/// C++ base classes. This is used while creating debug info entry for
784/// a Record.
785void CGDebugInfo::
786CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000787 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Devang Patel2ed8f002010-08-27 17:47:47 +0000788 llvm::DIType RecordTy) {
789
790 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
791 BE = RD->friend_end(); BI != BE; ++BI) {
Devang Patel823d8e92010-12-08 22:42:58 +0000792 if (TypeSourceInfo *TInfo = (*BI)->getFriendType())
Devang Patel16674e82011-02-22 18:56:36 +0000793 EltTys.push_back(DBuilder.createFriend(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +0000794 getOrCreateType(TInfo->getType(),
795 Unit)));
Devang Patel2ed8f002010-08-27 17:47:47 +0000796 }
797}
798
Devang Patela245c5b2010-01-25 23:32:18 +0000799/// CollectCXXBases - A helper function to collect debug info for
800/// C++ base classes. This is used while creating debug info entry for
801/// a Record.
802void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000803CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000804 llvm::SmallVectorImpl<llvm::Value *> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000805 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000806
Devang Patel239cec62010-02-01 21:39:52 +0000807 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
808 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
809 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000810 unsigned BFlags = 0;
811 uint64_t BaseOffset;
812
813 const CXXRecordDecl *Base =
814 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
815
816 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000817 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000818 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000819 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000820 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000821 } else
Anders Carlssona14f5972010-10-31 23:22:37 +0000822 BaseOffset = RL.getBaseClassOffsetInBits(Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000823
824 AccessSpecifier Access = BI->getAccessSpecifier();
825 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000826 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000827 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000828 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000829
Devang Patel823d8e92010-12-08 22:42:58 +0000830 llvm::DIType DTy =
Devang Patel16674e82011-02-22 18:56:36 +0000831 DBuilder.createInheritance(RecordTy,
Devang Patel823d8e92010-12-08 22:42:58 +0000832 getOrCreateType(BI->getType(), Unit),
833 BaseOffset, BFlags);
Devang Patelca7daed2010-01-28 21:54:15 +0000834 EltTys.push_back(DTy);
835 }
Devang Patela245c5b2010-01-25 23:32:18 +0000836}
837
Devang Patel4ce3f202010-01-28 18:11:52 +0000838/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000839llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000840 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000841 return VTablePtrType;
842
843 ASTContext &Context = CGM.getContext();
844
845 /* Function type */
Devang Patel823d8e92010-12-08 22:42:58 +0000846 llvm::Value *STy = getOrCreateType(Context.IntTy, Unit);
Devang Patel16674e82011-02-22 18:56:36 +0000847 llvm::DIArray SElements = DBuilder.getOrCreateArray(&STy, 1);
848 llvm::DIType SubTy = DBuilder.createSubroutineType(Unit, SElements);
Devang Patel4ce3f202010-01-28 18:11:52 +0000849 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
Devang Patel16674e82011-02-22 18:56:36 +0000850 llvm::DIType vtbl_ptr_type = DBuilder.createPointerType(SubTy, Size, 0,
Devang Patel823d8e92010-12-08 22:42:58 +0000851 "__vtbl_ptr_type");
Devang Patel16674e82011-02-22 18:56:36 +0000852 VTablePtrType = DBuilder.createPointerType(vtbl_ptr_type, Size);
Devang Patel4ce3f202010-01-28 18:11:52 +0000853 return VTablePtrType;
854}
855
Anders Carlsson046c2942010-04-17 20:15:18 +0000856/// getVTableName - Get vtable name for the given Class.
857llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000858 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000859 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000860
861 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000862 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000863 memcpy(StrPtr, Name.data(), Name.length());
864 return llvm::StringRef(StrPtr, Name.length());
865}
866
867
Anders Carlsson046c2942010-04-17 20:15:18 +0000868/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000869/// debug info entry in EltTys vector.
870void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000871CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000872 llvm::SmallVectorImpl<llvm::Value *> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000873 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000874
875 // If there is a primary base then it will hold vtable info.
876 if (RL.getPrimaryBase())
877 return;
878
879 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000880 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000881 return;
882
883 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
884 llvm::DIType VPTR
Devang Patel16674e82011-02-22 18:56:36 +0000885 = DBuilder.createMemberType(getVTableName(RD), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +0000886 0, Size, 0, 0, 0,
887 getOrCreateVTablePtrType(Unit));
Devang Patel4ce3f202010-01-28 18:11:52 +0000888 EltTys.push_back(VPTR);
889}
890
Devang Patelc69e1cf2010-09-30 19:05:55 +0000891/// getOrCreateRecordType - Emit record type's standalone debug info.
892llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
893 SourceLocation Loc) {
894 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
Devang Patel16674e82011-02-22 18:56:36 +0000895 DBuilder.retainType(T);
Devang Patelc69e1cf2010-09-30 19:05:55 +0000896 return T;
897}
898
Devang Patel65e99f22009-02-25 01:36:11 +0000899/// CreateType - get structure or union type.
Devang Patel31f7d022011-01-17 22:23:07 +0000900llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000901 RecordDecl *RD = Ty->getDecl();
Devang Patel31f7d022011-01-17 22:23:07 +0000902 llvm::DIFile Unit = getOrCreateFile(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000905 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
906 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Chris Lattner9c85ba32008-11-10 06:08:34 +0000908 // Records and classes and unions can all be recursive. To handle them, we
909 // first generate a debug descriptor for the struct as a forward declaration.
910 // Then (if it is a definition) we go through and get debug info for all of
911 // its members. Finally, we create a descriptor for the complete type (which
912 // may refer to the forward decl if the struct is recursive) and replace all
913 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000914 llvm::DIDescriptor FDContext =
John McCall8178df32011-02-22 22:38:33 +0000915 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel0b897992010-07-08 19:56:29 +0000916
917 // If this is just a forward declaration, construct an appropriately
918 // marked node and just return it.
919 if (!RD->getDefinition()) {
Devang Patel823d8e92010-12-08 22:42:58 +0000920 llvm::DIType FwdDecl =
Devang Patel16674e82011-02-22 18:56:36 +0000921 DBuilder.createStructType(FDContext, RD->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +0000922 DefUnit, Line, 0, 0,
923 llvm::DIDescriptor::FlagFwdDecl,
924 llvm::DIArray());
Devang Patel0b897992010-07-08 19:56:29 +0000925
926 return FwdDecl;
927 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000928
Devang Patel16674e82011-02-22 18:56:36 +0000929 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Devang Patelab699792010-05-07 18:12:35 +0000931 llvm::MDNode *MN = FwdDecl;
932 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000933 // Otherwise, insert it into the TypeCache so that recursive uses will find
934 // it.
Devang Patelab699792010-05-07 18:12:35 +0000935 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000936 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000937 RegionStack.push_back(FwdDeclNode);
938 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000939
940 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +0000941 llvm::SmallVector<llvm::Value *, 16> EltTys;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000942
Devang Pateld6c5a262010-02-01 21:52:22 +0000943 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000944 if (CXXDecl) {
945 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000946 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000947 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000948
949 // Collect static variables with initializers.
950 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
951 I != E; ++I)
952 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
953 if (const Expr *Init = V->getInit()) {
954 Expr::EvalResult Result;
955 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
956 llvm::ConstantInt *CI
957 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
958
959 // Create the descriptor for static variable.
960 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
961 llvm::StringRef VName = V->getName();
962 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
963 // Do not use DIGlobalVariable for enums.
964 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
Devang Patel16674e82011-02-22 18:56:36 +0000965 DBuilder.createStaticVariable(FwdDecl, VName, VName, VUnit,
Devang Patel823d8e92010-12-08 22:42:58 +0000966 getLineNumber(V->getLocation()),
967 VTy, true, CI);
Devang Pateldabc3e92010-08-12 00:02:44 +0000968 }
969 }
970 }
971 }
972
Devang Pateld6c5a262010-02-01 21:52:22 +0000973 CollectRecordFields(RD, Unit, EltTys);
Devang Patelfa275df2011-02-02 21:38:49 +0000974 llvm::SmallVector<llvm::Value *, 16> TemplateParams;
Devang Patel4ce3f202010-01-28 18:11:52 +0000975 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000976 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000977 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelfa275df2011-02-02 21:38:49 +0000978 if (ClassTemplateSpecializationDecl *TSpecial
979 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
980 const TemplateArgumentList &TAL = TSpecial->getTemplateArgs();
981 for (unsigned i = 0, e = TAL.size(); i != e; ++i) {
982 const TemplateArgument &TA = TAL[i];
983 if (TA.getKind() == TemplateArgument::Type) {
984 llvm::DIType TTy = getOrCreateType(TA.getAsType(), Unit);
985 llvm::DITemplateTypeParameter TTP =
Devang Patel16674e82011-02-22 18:56:36 +0000986 DBuilder.createTemplateTypeParameter(TheCU, TTy.getName(), TTy);
Devang Patelfa275df2011-02-02 21:38:49 +0000987 TemplateParams.push_back(TTP);
Devang Patel0ce34c62011-02-02 22:36:18 +0000988 } else if (TA.getKind() == TemplateArgument::Integral) {
989 llvm::DIType TTy = getOrCreateType(TA.getIntegralType(), Unit);
990 // FIXME: Get parameter name, instead of parameter type name.
991 llvm::DITemplateValueParameter TVP =
Devang Patel16674e82011-02-22 18:56:36 +0000992 DBuilder.createTemplateValueParameter(TheCU, TTy.getName(), TTy,
Devang Patel0ce34c62011-02-02 22:36:18 +0000993 TA.getAsIntegral()->getZExtValue());
994 TemplateParams.push_back(TVP);
Devang Patelfa275df2011-02-02 21:38:49 +0000995 }
996 }
997 }
Devang Patel823d8e92010-12-08 22:42:58 +0000998 }
Devang Patel0ac8f312010-01-28 00:54:21 +0000999
Devang Patel823d8e92010-12-08 22:42:58 +00001000 RegionStack.pop_back();
1001 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1002 RegionMap.find(Ty->getDecl());
1003 if (RI != RegionMap.end())
1004 RegionMap.erase(RI);
1005
1006 llvm::DIDescriptor RDContext =
John McCall8178df32011-02-22 22:38:33 +00001007 getContextDescriptor(cast<Decl>(RD->getDeclContext()));
Devang Patel823d8e92010-12-08 22:42:58 +00001008 llvm::StringRef RDName = RD->getName();
1009 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1010 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1011 llvm::DIArray Elements =
Devang Patel16674e82011-02-22 18:56:36 +00001012 DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel823d8e92010-12-08 22:42:58 +00001013 llvm::MDNode *RealDecl = NULL;
1014
Devang Patel5c5b5872011-02-28 22:32:45 +00001015 if (RD->isUnion())
Devang Patel16674e82011-02-22 18:56:36 +00001016 RealDecl = DBuilder.createUnionType(RDContext, RDName, DefUnit, Line,
Devang Patel5c5b5872011-02-28 22:32:45 +00001017 Size, Align, 0, Elements);
1018 else if (CXXDecl) {
Devang Patel823d8e92010-12-08 22:42:58 +00001019 RDName = getClassName(RD);
1020 // A class's primary base or the class itself contains the vtable.
1021 llvm::MDNode *ContainingType = NULL;
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel5bc794f2010-10-14 22:59:23 +00001023 if (const CXXRecordDecl *PBase = RL.getPrimaryBase()) {
1024 // Seek non virtual primary base root.
1025 while (1) {
1026 const ASTRecordLayout &BRL = CGM.getContext().getASTRecordLayout(PBase);
1027 const CXXRecordDecl *PBT = BRL.getPrimaryBase();
Anders Carlssonc9e814b2010-11-24 23:12:57 +00001028 if (PBT && !BRL.isPrimaryBaseVirtual())
Devang Patel5bc794f2010-10-14 22:59:23 +00001029 PBase = PBT;
1030 else
1031 break;
1032 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001033 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001034 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel5bc794f2010-10-14 22:59:23 +00001035 }
Devang Patel0ac8f312010-01-28 00:54:21 +00001036 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001037 ContainingType = FwdDecl;
Devang Patelfa275df2011-02-02 21:38:49 +00001038 llvm::DIArray TParamsArray =
Devang Patel16674e82011-02-22 18:56:36 +00001039 DBuilder.getOrCreateArray(TemplateParams.data(), TemplateParams.size());
1040 RealDecl = DBuilder.createClassType(RDContext, RDName, DefUnit, Line,
Devang Patel823d8e92010-12-08 22:42:58 +00001041 Size, Align, 0, 0, llvm::DIType(),
Devang Patelfa275df2011-02-02 21:38:49 +00001042 Elements, ContainingType,
1043 TParamsArray);
Devang Patel5c5b5872011-02-28 22:32:45 +00001044 } else
1045 RealDecl = DBuilder.createStructType(RDContext, RDName, DefUnit, Line,
1046 Size, Align, 0, Elements);
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattner9c85ba32008-11-10 06:08:34 +00001048 // Now that we have a real decl for the struct, replace anything using the
1049 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001050 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001051 RegionMap[RD] = llvm::WeakVH(RealDecl);
Devang Patel823d8e92010-12-08 22:42:58 +00001052 return llvm::DIType(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001053}
1054
John McCallc12c5bb2010-05-15 11:32:37 +00001055/// CreateType - get objective-c object type.
1056llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1057 llvm::DIFile Unit) {
1058 // Ignore protocols.
1059 return getOrCreateType(Ty->getBaseType(), Unit);
1060}
1061
Devang Patel9ca36b62009-02-26 21:10:26 +00001062/// CreateType - get objective-c interface type.
1063llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001064 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001065 ObjCInterfaceDecl *ID = Ty->getDecl();
Douglas Gregora6a28972010-11-30 06:38:09 +00001066 if (!ID)
1067 return llvm::DIType();
Devang Patel9ca36b62009-02-26 21:10:26 +00001068
1069 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001070 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001071 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001072 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001073
Dan Gohman45f7c782010-08-23 21:15:56 +00001074 // If this is just a forward declaration, return a special forward-declaration
1075 // debug type.
1076 if (ID->isForwardDecl()) {
Devang Patel823d8e92010-12-08 22:42:58 +00001077 llvm::DIType FwdDecl =
Devang Patel16674e82011-02-22 18:56:36 +00001078 DBuilder.createStructType(Unit, ID->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001079 DefUnit, Line, 0, 0, 0,
1080 llvm::DIArray(), RuntimeLang);
Dan Gohman45f7c782010-08-23 21:15:56 +00001081 return FwdDecl;
1082 }
1083
Devang Patel9ca36b62009-02-26 21:10:26 +00001084 // To handle recursive interface, we
1085 // first generate a debug descriptor for the struct as a forward declaration.
1086 // Then (if it is a definition) we go through and get debug info for all of
1087 // its members. Finally, we create a descriptor for the complete type (which
1088 // may refer to the forward decl if the struct is recursive) and replace all
1089 // uses of the forward declaration with the final definition.
Devang Patel16674e82011-02-22 18:56:36 +00001090 llvm::DIType FwdDecl = DBuilder.createTemporaryType(DefUnit);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Devang Patelab699792010-05-07 18:12:35 +00001092 llvm::MDNode *MN = FwdDecl;
1093 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001094 // Otherwise, insert it into the TypeCache so that recursive uses will find
1095 // it.
Devang Patelab699792010-05-07 18:12:35 +00001096 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001097 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001098 RegionStack.push_back(FwdDeclNode);
1099 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001100
1101 // Convert all the elements.
Devang Patel823d8e92010-12-08 22:42:58 +00001102 llvm::SmallVector<llvm::Value *, 16> EltTys;
Devang Patel9ca36b62009-02-26 21:10:26 +00001103
Devang Pateld6c5a262010-02-01 21:52:22 +00001104 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001105 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001106 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001107 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001108 if (!SClassTy.isValid())
1109 return llvm::DIType();
1110
Mike Stump1eb44332009-09-09 15:08:12 +00001111 llvm::DIType InhTag =
Devang Patel16674e82011-02-22 18:56:36 +00001112 DBuilder.createInheritance(FwdDecl, SClassTy, 0, 0);
Devang Patelfbe899f2009-03-10 21:30:26 +00001113 EltTys.push_back(InhTag);
1114 }
1115
Devang Pateld6c5a262010-02-01 21:52:22 +00001116 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001117
1118 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001119 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001120 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001121 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Douglas Gregora6a28972010-11-30 06:38:09 +00001122 if (!FieldTy.isValid())
1123 return llvm::DIType();
1124
Devang Patel73621622009-11-25 17:37:31 +00001125 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001126
Devang Patelde135022009-04-27 22:40:36 +00001127 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001128 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001129 continue;
1130
Devang Patel9ca36b62009-02-26 21:10:26 +00001131 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001132 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1133 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001134 QualType FType = Field->getType();
1135 uint64_t FieldSize = 0;
1136 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001137
Devang Patel99c20eb2009-03-20 18:24:39 +00001138 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Devang Patel99c20eb2009-03-20 18:24:39 +00001140 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001141 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001142 Expr *BitWidth = Field->getBitWidth();
1143 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001144 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001145
Anders Carlsson20f12a22009-12-06 18:00:51 +00001146 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001147 }
1148
Mike Stump1eb44332009-09-09 15:08:12 +00001149 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1150
Devang Patelc20482b2009-03-19 00:23:53 +00001151 unsigned Flags = 0;
1152 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001153 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001154 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001155 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Devang Patel16674e82011-02-22 18:56:36 +00001157 FieldTy = DBuilder.createMemberType(FieldName, FieldDefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +00001158 FieldLine, FieldSize, FieldAlign,
1159 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001160 EltTys.push_back(FieldTy);
1161 }
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Devang Patel9ca36b62009-02-26 21:10:26 +00001163 llvm::DIArray Elements =
Devang Patel16674e82011-02-22 18:56:36 +00001164 DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001165
Devang Patele4c1ea02010-03-11 20:01:48 +00001166 RegionStack.pop_back();
1167 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1168 RegionMap.find(Ty->getDecl());
1169 if (RI != RegionMap.end())
1170 RegionMap.erase(RI);
1171
Devang Patel9ca36b62009-02-26 21:10:26 +00001172 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001173 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1174 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Devang Patel823d8e92010-12-08 22:42:58 +00001176 llvm::DIType RealDecl =
Devang Patel16674e82011-02-22 18:56:36 +00001177 DBuilder.createStructType(Unit, ID->getName(), DefUnit,
Devang Patel823d8e92010-12-08 22:42:58 +00001178 Line, Size, Align, 0,
1179 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001180
1181 // Now that we have a real decl for the struct, replace anything using the
1182 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001183 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001184 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001185
Devang Patel9ca36b62009-02-26 21:10:26 +00001186 return RealDecl;
1187}
1188
Devang Patel31f7d022011-01-17 22:23:07 +00001189llvm::DIType CGDebugInfo::CreateType(const TagType *Ty) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001190 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001191 return CreateType(RT);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001192 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
Devang Patel31f7d022011-01-17 22:23:07 +00001193 return CreateEnumType(ET->getDecl());
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Chris Lattner9c85ba32008-11-10 06:08:34 +00001195 return llvm::DIType();
1196}
1197
Devang Patel70c23cd2010-02-23 22:59:39 +00001198llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001199 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001200 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1201 uint64_t NumElems = Ty->getNumElements();
1202 if (NumElems > 0)
1203 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001204
Devang Patel16674e82011-02-22 18:56:36 +00001205 llvm::Value *Subscript = DBuilder.getOrCreateSubrange(0, NumElems);
1206 llvm::DIArray SubscriptArray = DBuilder.getOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001207
1208 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1209 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1210
1211 return
Devang Patel16674e82011-02-22 18:56:36 +00001212 DBuilder.createVectorType(Size, Align, ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001213}
1214
Chris Lattner9c85ba32008-11-10 06:08:34 +00001215llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001216 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001217 uint64_t Size;
1218 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001219
1220
Nuno Lopes010d5142009-01-28 00:35:17 +00001221 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001222 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001223 Size = 0;
1224 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001225 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001226 } else if (Ty->isIncompleteArrayType()) {
1227 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001228 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001229 } else {
1230 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001231 Size = CGM.getContext().getTypeSize(Ty);
1232 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001233 }
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Chris Lattner9c85ba32008-11-10 06:08:34 +00001235 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1236 // interior arrays, do we care? Why aren't nested arrays represented the
1237 // obvious/recursive way?
Devang Patel823d8e92010-12-08 22:42:58 +00001238 llvm::SmallVector<llvm::Value *, 8> Subscripts;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001239 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001240 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001241 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001242 else {
1243 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1244 uint64_t Upper = 0;
1245 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1246 if (CAT->getSize().getZExtValue())
1247 Upper = CAT->getSize().getZExtValue() - 1;
1248 // FIXME: Verify this is right for VLAs.
Devang Patel16674e82011-02-22 18:56:36 +00001249 Subscripts.push_back(DBuilder.getOrCreateSubrange(0, Upper));
Devang Patelcdf523c2010-10-06 18:30:00 +00001250 EltTy = Ty->getElementType();
1251 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001252 }
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattner9c85ba32008-11-10 06:08:34 +00001254 llvm::DIArray SubscriptArray =
Devang Patel16674e82011-02-22 18:56:36 +00001255 DBuilder.getOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001256
Devang Patelca80a5f2009-10-20 19:55:01 +00001257 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001258 DBuilder.createArrayType(Size, Align, getOrCreateType(EltTy, Unit),
Devang Patel823d8e92010-12-08 22:42:58 +00001259 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001260 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001261}
1262
Anders Carlssona031b352009-11-06 19:19:55 +00001263llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001264 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001265 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1266 Ty, Ty->getPointeeType(), Unit);
1267}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001268
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001269llvm::DIType CGDebugInfo::CreateType(const RValueReferenceType *Ty,
1270 llvm::DIFile Unit) {
1271 return CreatePointerLikeType(llvm::dwarf::DW_TAG_rvalue_reference_type,
1272 Ty, Ty->getPointeeType(), Unit);
1273}
1274
Anders Carlsson20f12a22009-12-06 18:00:51 +00001275llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001276 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001277 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1278 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1279
1280 if (!Ty->getPointeeType()->isFunctionType()) {
1281 // We have a data member pointer type.
1282 return PointerDiffDITy;
1283 }
1284
1285 // We have a member function pointer type. Treat it as a struct with two
1286 // ptrdiff_t members.
1287 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1288
1289 uint64_t FieldOffset = 0;
Devang Patel823d8e92010-12-08 22:42:58 +00001290 llvm::Value *ElementTypes[2];
Anders Carlsson20f12a22009-12-06 18:00:51 +00001291
1292 // FIXME: This should probably be a function type instead.
1293 ElementTypes[0] =
Devang Patel16674e82011-02-22 18:56:36 +00001294 DBuilder.createMemberType("ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001295 Info.first, Info.second, FieldOffset, 0,
1296 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001297 FieldOffset += Info.first;
1298
1299 ElementTypes[1] =
Devang Patel16674e82011-02-22 18:56:36 +00001300 DBuilder.createMemberType("ptr", U, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001301 Info.first, Info.second, FieldOffset, 0,
1302 PointerDiffDITy);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303
1304 llvm::DIArray Elements =
Devang Patel16674e82011-02-22 18:56:36 +00001305 DBuilder.getOrCreateArray(&ElementTypes[0],
Devang Patel823d8e92010-12-08 22:42:58 +00001306 llvm::array_lengthof(ElementTypes));
Anders Carlsson20f12a22009-12-06 18:00:51 +00001307
Devang Patel16674e82011-02-22 18:56:36 +00001308 return DBuilder.createStructType(U, llvm::StringRef("test"),
Devang Patel823d8e92010-12-08 22:42:58 +00001309 U, 0, FieldOffset,
1310 0, 0, Elements);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001311}
1312
Devang Patel6237cea2010-08-23 22:07:25 +00001313/// CreateEnumType - get enumeration type.
Devang Patel31f7d022011-01-17 22:23:07 +00001314llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED) {
1315 llvm::DIFile Unit = getOrCreateFile(ED->getLocation());
Devang Patel823d8e92010-12-08 22:42:58 +00001316 llvm::SmallVector<llvm::Value *, 16> Enumerators;
Devang Patel6237cea2010-08-23 22:07:25 +00001317
1318 // Create DIEnumerator elements for each enumerator.
1319 for (EnumDecl::enumerator_iterator
1320 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1321 Enum != EnumEnd; ++Enum) {
Devang Patel823d8e92010-12-08 22:42:58 +00001322 Enumerators.push_back(
Devang Patel16674e82011-02-22 18:56:36 +00001323 DBuilder.createEnumerator(Enum->getName(),
Devang Patel823d8e92010-12-08 22:42:58 +00001324 Enum->getInitVal().getZExtValue()));
Devang Patel6237cea2010-08-23 22:07:25 +00001325 }
1326
1327 // Return a CompositeType for the enum itself.
1328 llvm::DIArray EltArray =
Devang Patel16674e82011-02-22 18:56:36 +00001329 DBuilder.getOrCreateArray(Enumerators.data(), Enumerators.size());
Devang Patel6237cea2010-08-23 22:07:25 +00001330
1331 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1332 unsigned Line = getLineNumber(ED->getLocation());
1333 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001334 uint64_t Align = 0;
1335 if (!ED->getTypeForDecl()->isIncompleteType()) {
1336 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1337 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1338 }
Devang Patel4bc48872010-10-27 23:23:58 +00001339 llvm::DIDescriptor EnumContext =
John McCall8178df32011-02-22 22:38:33 +00001340 getContextDescriptor(cast<Decl>(ED->getDeclContext()));
Devang Patel6237cea2010-08-23 22:07:25 +00001341 llvm::DIType DbgTy =
Devang Patel16674e82011-02-22 18:56:36 +00001342 DBuilder.createEnumerationType(EnumContext, ED->getName(), DefUnit, Line,
Devang Patel823d8e92010-12-08 22:42:58 +00001343 Size, Align, EltArray);
Devang Patel6237cea2010-08-23 22:07:25 +00001344 return DbgTy;
1345}
1346
Douglas Gregor840943d2009-12-21 20:18:30 +00001347static QualType UnwrapTypeForDebugInfo(QualType T) {
1348 do {
1349 QualType LastT = T;
1350 switch (T->getTypeClass()) {
1351 default:
1352 return T;
1353 case Type::TemplateSpecialization:
1354 T = cast<TemplateSpecializationType>(T)->desugar();
1355 break;
John McCallf4c73712011-01-19 06:33:43 +00001356 case Type::TypeOfExpr:
1357 T = cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001358 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001359 case Type::TypeOf:
1360 T = cast<TypeOfType>(T)->getUnderlyingType();
1361 break;
1362 case Type::Decltype:
1363 T = cast<DecltypeType>(T)->getUnderlyingType();
1364 break;
John McCall9d156a72011-01-06 01:58:22 +00001365 case Type::Attributed:
1366 T = cast<AttributedType>(T)->getEquivalentType();
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001367 case Type::Elaborated:
1368 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001369 break;
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001370 case Type::Paren:
1371 T = cast<ParenType>(T)->getInnerType();
1372 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001373 case Type::SubstTemplateTypeParm:
1374 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1375 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001376 }
1377
1378 assert(T != LastT && "Type unwrapping failed to unwrap!");
1379 if (T == LastT)
1380 return T;
1381 } while (true);
1382
1383 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001384}
1385
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001386/// getOrCreateType - Get the type from the cache or create a new
1387/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001388llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001389 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001390 if (Ty.isNull())
1391 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Douglas Gregor840943d2009-12-21 20:18:30 +00001393 // Unwrap the type as needed for debug information.
1394 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001395
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001396 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001397 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001398 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001399 if (it != TypeCache.end()) {
1400 // Verify that the debug info still exists.
1401 if (&*it->second)
1402 return llvm::DIType(cast<llvm::MDNode>(it->second));
1403 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001404
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001405 // Otherwise create the type.
1406 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001407
1408 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001409 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001410 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001411}
1412
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001413/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001414llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001415 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001416 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001417 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001418 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001419
Douglas Gregor2101a822009-12-21 19:57:21 +00001420 const char *Diag = 0;
1421
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001422 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001423 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001424#define TYPE(Class, Base)
1425#define ABSTRACT_TYPE(Class, Base)
1426#define NON_CANONICAL_TYPE(Class, Base)
1427#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1428#include "clang/AST/TypeNodes.def"
1429 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001430
Anders Carlssonbfe69952009-11-06 18:24:04 +00001431 // FIXME: Handle these.
1432 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001433 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001434
1435 case Type::Vector:
1436 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001437 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001438 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001439 case Type::ObjCObject:
1440 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001441 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001442 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
Devang Patelf1d1d9a2010-11-01 16:52:40 +00001443 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty));
Devang Patel344ff5d2010-12-09 00:25:29 +00001444 case Type::Complex: return CreateType(cast<ComplexType>(Ty));
Daniel Dunbar03faac32009-09-19 19:27:14 +00001445 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001446 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001447 return CreateType(cast<BlockPointerType>(Ty), Unit);
1448 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001449 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001450 case Type::Enum:
Devang Patel31f7d022011-01-17 22:23:07 +00001451 return CreateType(cast<TagType>(Ty));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001452 case Type::FunctionProto:
1453 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001454 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001455 case Type::ConstantArray:
1456 case Type::VariableArray:
1457 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001458 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001459
1460 case Type::LValueReference:
1461 return CreateType(cast<LValueReferenceType>(Ty), Unit);
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001462 case Type::RValueReference:
1463 return CreateType(cast<RValueReferenceType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001464
Anders Carlsson20f12a22009-12-06 18:00:51 +00001465 case Type::MemberPointer:
1466 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001467
John McCall9d156a72011-01-06 01:58:22 +00001468 case Type::Attributed:
Douglas Gregor2101a822009-12-21 19:57:21 +00001469 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001470 case Type::Elaborated:
Abramo Bagnara075f8f12010-12-10 16:29:40 +00001471 case Type::Paren:
Douglas Gregor2101a822009-12-21 19:57:21 +00001472 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001473 case Type::TypeOfExpr:
1474 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001475 case Type::Decltype:
Richard Smith34b41d92011-02-20 03:19:35 +00001476 case Type::Auto:
Douglas Gregor840943d2009-12-21 20:18:30 +00001477 llvm_unreachable("type should have been unwrapped!");
Douglas Gregor36b8ee62011-01-22 01:58:15 +00001478 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001479 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001480
1481 assert(Diag && "Fall through without a diagnostic?");
1482 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1483 "debug information for %0 is not yet supported");
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00001484 CGM.getDiags().Report(DiagID)
Douglas Gregor2101a822009-12-21 19:57:21 +00001485 << Diag;
1486 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001487}
1488
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001489/// CreateMemberType - Create new member and increase Offset by FType's size.
1490llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1491 llvm::StringRef Name,
1492 uint64_t *Offset) {
1493 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1494 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1495 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel16674e82011-02-22 18:56:36 +00001496 llvm::DIType Ty = DBuilder.createMemberType(Name, Unit, 0,
Devang Patel823d8e92010-12-08 22:42:58 +00001497 FieldSize, FieldAlign,
1498 *Offset, 0, FieldTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001499 *Offset += FieldSize;
1500 return Ty;
1501}
1502
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001503/// EmitFunctionStart - Constructs the debug code for entering a function -
1504/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001505void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001506 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001507 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Devang Patel9c6c3a02010-01-14 00:36:21 +00001509 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001510 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001511
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001512 FnBeginRegionCount.push_back(RegionStack.size());
1513
Devang Patel9c6c3a02010-01-14 00:36:21 +00001514 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001515 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001516 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1517 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001518 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001519 // If there is a DISubprogram for this function available then use it.
1520 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1521 FI = SPCache.find(FD);
1522 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001523 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001524 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1525 llvm::MDNode *SPN = SP;
1526 RegionStack.push_back(SPN);
1527 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001528 return;
1529 }
1530 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001531 Name = getFunctionName(FD);
1532 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001533 LinkageName = CGM.getMangledName(GD);
Devang Patel58faf202010-10-22 17:11:50 +00001534 if (LinkageName == Name)
1535 LinkageName = llvm::StringRef();
Devang Patel3951e712010-10-07 22:03:49 +00001536 if (FD->hasPrototype())
1537 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001538 if (const NamespaceDecl *NSDecl =
1539 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
Devang Patel170cef32010-12-09 00:33:05 +00001540 FDContext = getOrCreateNameSpace(NSDecl);
David Chisnall70b9b442010-09-02 17:16:32 +00001541 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001542 Name = getObjCMethodName(OMD);
Devang Patel3951e712010-10-07 22:03:49 +00001543 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001544 } else {
Devang Patel58faf202010-10-22 17:11:50 +00001545 // Use llvm function name.
Devang Patel9c6c3a02010-01-14 00:36:21 +00001546 Name = Fn->getName();
Devang Patel3951e712010-10-07 22:03:49 +00001547 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001548 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001549 if (!Name.empty() && Name[0] == '\01')
1550 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Devang Patel970c6182010-04-24 00:49:16 +00001552 // It is expected that CurLoc is set before using EmitFunctionStart.
1553 // Usually, CurLoc points to the left bracket location of compound
1554 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001555 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001556 if (D->isImplicit())
1557 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001558 llvm::DISubprogram SP =
Devang Patel16674e82011-02-22 18:56:36 +00001559 DBuilder.createFunction(FDContext, Name, LinkageName, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00001560 LineNo, getOrCreateType(FnType, Unit),
1561 Fn->hasInternalLinkage(), true/*definition*/,
1562 Flags, CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001564 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001565 llvm::MDNode *SPN = SP;
1566 RegionStack.push_back(SPN);
1567 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001568
1569 // Clear stack used to keep track of #line directives.
1570 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001571}
1572
1573
Devang Patel4d939e62010-07-20 22:20:10 +00001574void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001575 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001577 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001578 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001579 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001580 || (SM.getInstantiationLineNumber(CurLoc) ==
1581 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001582 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001583 // New Builder may not be in sync with CGDebugInfo.
1584 if (!Builder.getCurrentDebugLocation().isUnknown())
1585 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001586
1587 // Update last state.
1588 PrevLoc = CurLoc;
1589
Chris Lattnerc6034632010-04-01 06:31:43 +00001590 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001591 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1592 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001593 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001594}
1595
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001596/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1597/// has introduced scope change.
1598void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1599 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1600 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1601 return;
1602 SourceManager &SM = CGM.getContext().getSourceManager();
1603 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1604 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1605
Douglas Gregor8c457a82010-11-11 20:45:16 +00001606 if (PCLoc.isInvalid() || PPLoc.isInvalid() ||
1607 !strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001608 return;
1609
1610 // If #line directive stack is empty then we are entering a new scope.
1611 if (LineDirectiveFiles.empty()) {
1612 EmitRegionStart(Builder);
1613 LineDirectiveFiles.push_back(PCLoc.getFilename());
1614 return;
1615 }
1616
1617 assert (RegionStack.size() >= LineDirectiveFiles.size()
1618 && "error handling #line regions!");
1619
1620 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001621 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001622 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1623 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001624 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001625 SeenThisFile = true;
1626 break;
1627 }
1628
1629 // If #line for this file is seen earlier then pop out #line regions.
1630 if (SeenThisFile) {
1631 while (!LineDirectiveFiles.empty()) {
1632 const char *LastFile = LineDirectiveFiles.back();
1633 RegionStack.pop_back();
1634 LineDirectiveFiles.pop_back();
1635 if (!strcmp(PPLoc.getFilename(), LastFile))
1636 break;
1637 }
1638 return;
1639 }
1640
1641 // .. otherwise insert new #line region.
1642 EmitRegionStart(Builder);
1643 LineDirectiveFiles.push_back(PCLoc.getFilename());
1644
1645 return;
1646}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001647/// EmitRegionStart- Constructs the debug code for entering a declarative
1648/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001649void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001650 llvm::DIDescriptor D =
Devang Patel16674e82011-02-22 18:56:36 +00001651 DBuilder.createLexicalBlock(RegionStack.empty() ?
Devang Patel823d8e92010-12-08 22:42:58 +00001652 llvm::DIDescriptor() :
1653 llvm::DIDescriptor(RegionStack.back()),
1654 getOrCreateFile(CurLoc),
1655 getLineNumber(CurLoc),
1656 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001657 llvm::MDNode *DN = D;
1658 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001659}
1660
1661/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1662/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001663void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001664 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1665
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001666 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001667 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001669 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001670}
1671
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001672/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1673void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1674 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1675 unsigned RCount = FnBeginRegionCount.back();
1676 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1677
1678 // Pop all regions for this function.
1679 while (RegionStack.size() != RCount)
1680 EmitRegionEnd(Builder);
1681 FnBeginRegionCount.pop_back();
1682}
1683
Devang Patel809b9bb2010-02-10 18:49:08 +00001684// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1685// See BuildByRefType.
1686llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1687 uint64_t *XOffset) {
1688
Devang Patel823d8e92010-12-08 22:42:58 +00001689 llvm::SmallVector<llvm::Value *, 5> EltTys;
Devang Patel809b9bb2010-02-10 18:49:08 +00001690 QualType FType;
1691 uint64_t FieldSize, FieldOffset;
1692 unsigned FieldAlign;
1693
Devang Patel17800552010-03-09 00:44:50 +00001694 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001695 QualType Type = VD->getType();
1696
1697 FieldOffset = 0;
1698 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001699 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1700 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001701 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001702 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1703 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1704
John McCall6b5a61b2011-02-07 10:33:21 +00001705 bool HasCopyAndDispose = CGM.getContext().BlockRequiresCopying(Type);
Devang Patel809b9bb2010-02-10 18:49:08 +00001706 if (HasCopyAndDispose) {
1707 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001708 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1709 &FieldOffset));
1710 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1711 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001712 }
1713
1714 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1715 if (Align > CharUnits::fromQuantity(
1716 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1717 unsigned AlignedOffsetInBytes
1718 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1719 unsigned NumPaddingBytes
1720 = AlignedOffsetInBytes - FieldOffset/8;
1721
1722 if (NumPaddingBytes > 0) {
1723 llvm::APInt pad(32, NumPaddingBytes);
1724 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1725 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001726 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001727 }
1728 }
1729
1730 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001731 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001732 FieldSize = CGM.getContext().getTypeSize(FType);
1733 FieldAlign = Align.getQuantity()*8;
1734
1735 *XOffset = FieldOffset;
Devang Patel16674e82011-02-22 18:56:36 +00001736 FieldTy = DBuilder.createMemberType(VD->getName(), Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00001737 0, FieldSize, FieldAlign,
1738 FieldOffset, 0, FieldTy);
Devang Patel809b9bb2010-02-10 18:49:08 +00001739 EltTys.push_back(FieldTy);
1740 FieldOffset += FieldSize;
1741
1742 llvm::DIArray Elements =
Devang Patel16674e82011-02-22 18:56:36 +00001743 DBuilder.getOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel809b9bb2010-02-10 18:49:08 +00001744
Devang Patele2472482010-09-29 21:05:52 +00001745 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001746
Devang Patel16674e82011-02-22 18:56:36 +00001747 return DBuilder.createStructType(Unit, "", Unit, 0, FieldOffset, 0, Flags,
Devang Patel823d8e92010-12-08 22:42:58 +00001748 Elements);
Devang Patel809b9bb2010-02-10 18:49:08 +00001749}
Devang Patel823d8e92010-12-08 22:42:58 +00001750
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001751/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001752void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Devang Patel34753802011-02-16 01:11:51 +00001753 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001754 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1755
Devang Patel17800552010-03-09 00:44:50 +00001756 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001757 llvm::DIType Ty;
1758 uint64_t XOffset = 0;
1759 if (VD->hasAttr<BlocksAttr>())
1760 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1761 else
1762 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001763
Devang Patelf4e54a22010-05-07 23:05:55 +00001764 // If there is not any debug info for type then do not emit debug info
1765 // for this variable.
1766 if (!Ty)
1767 return;
1768
Devang Patel34753802011-02-16 01:11:51 +00001769 if (llvm::Argument *Arg = dyn_cast<llvm::Argument>(Storage)) {
1770 // If Storage is an aggregate returned as 'sret' then let debugger know
1771 // about this.
Devang Patel0691f932011-02-10 00:40:52 +00001772 if (Arg->hasStructRetAttr())
Devang Patel16674e82011-02-22 18:56:36 +00001773 Ty = DBuilder.createReferenceType(Ty);
Devang Patel34753802011-02-16 01:11:51 +00001774 else if (CXXRecordDecl *Record = VD->getType()->getAsCXXRecordDecl()) {
1775 // If an aggregate variable has non trivial destructor or non trivial copy
1776 // constructor than it is pass indirectly. Let debug info know about this
1777 // by using reference of the aggregate type as a argument type.
1778 if (!Record->hasTrivialCopyConstructor() || !Record->hasTrivialDestructor())
Devang Patel16674e82011-02-22 18:56:36 +00001779 Ty = DBuilder.createReferenceType(Ty);
Devang Patel34753802011-02-16 01:11:51 +00001780 }
1781 }
Devang Patel0691f932011-02-10 00:40:52 +00001782
Chris Lattner9c85ba32008-11-10 06:08:34 +00001783 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001784 unsigned Line = getLineNumber(VD->getLocation());
1785 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001786 unsigned Flags = 0;
1787 if (VD->isImplicit())
1788 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001789 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001790
1791 llvm::StringRef Name = VD->getName();
1792 if (!Name.empty()) {
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001793 if (VD->hasAttr<BlocksAttr>()) {
1794 CharUnits offset = CharUnits::fromQuantity(32);
1795 llvm::SmallVector<llvm::Value *, 9> addr;
1796 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel4a4e2ef2011-02-18 23:29:22 +00001797 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001798 // offset of __forwarding field
1799 offset =
1800 CharUnits::fromQuantity(CGM.getContext().Target.getPointerWidth(0)/8);
1801 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00001802 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
1803 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001804 // offset of x field
1805 offset = CharUnits::fromQuantity(XOffset/8);
1806 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1807
1808 // Create the descriptor for the variable.
1809 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00001810 DBuilder.createComplexVariable(Tag,
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001811 llvm::DIDescriptor(RegionStack.back()),
1812 VD->getName(), Unit, Line, Ty,
1813 addr.data(), addr.size());
1814
1815 // Insert an llvm.dbg.declare into the current block.
1816 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00001817 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelb1fd0eb2011-01-11 00:30:27 +00001818
1819 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1820 return;
1821 }
1822 // Create the descriptor for the variable.
Devang Patelcebbedd2010-10-12 23:24:54 +00001823 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00001824 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
Devang Patel823d8e92010-12-08 22:42:58 +00001825 Name, Unit, Line, Ty,
1826 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001827
1828 // Insert an llvm.dbg.declare into the current block.
1829 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00001830 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001831
1832 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelf4dd9622010-10-29 16:21:19 +00001833 return;
Devang Patelcebbedd2010-10-12 23:24:54 +00001834 }
1835
1836 // If VD is an anonymous union then Storage represents value for
1837 // all union fields.
John McCall8178df32011-02-22 22:38:33 +00001838 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType())) {
1839 const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1840 if (RD->isUnion()) {
1841 for (RecordDecl::field_iterator I = RD->field_begin(),
1842 E = RD->field_end();
1843 I != E; ++I) {
1844 FieldDecl *Field = *I;
1845 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1846 llvm::StringRef FieldName = Field->getName();
Devang Patelcebbedd2010-10-12 23:24:54 +00001847
John McCall8178df32011-02-22 22:38:33 +00001848 // Ignore unnamed fields. Do not ignore unnamed records.
1849 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1850 continue;
Devang Patelcebbedd2010-10-12 23:24:54 +00001851
John McCall8178df32011-02-22 22:38:33 +00001852 // Use VarDecl's Tag, Scope and Line number.
1853 llvm::DIVariable D =
1854 DBuilder.createLocalVariable(Tag, llvm::DIDescriptor(Scope),
1855 FieldName, Unit, Line, FieldTy,
1856 CGM.getLangOptions().Optimize, Flags);
Devang Patelcebbedd2010-10-12 23:24:54 +00001857
John McCall8178df32011-02-22 22:38:33 +00001858 // Insert an llvm.dbg.declare into the current block.
1859 llvm::Instruction *Call =
1860 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patelcebbedd2010-10-12 23:24:54 +00001861
John McCall8178df32011-02-22 22:38:33 +00001862 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Devang Patelcebbedd2010-10-12 23:24:54 +00001863 }
John McCall8178df32011-02-22 22:38:33 +00001864 }
1865 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001866}
1867
Mike Stumpb1a6e682009-09-30 02:43:10 +00001868/// EmitDeclare - Emit local variable declaration debug info.
John McCall6b5a61b2011-02-07 10:33:21 +00001869void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001870 llvm::Value *Storage, CGBuilderTy &Builder,
John McCall6b5a61b2011-02-07 10:33:21 +00001871 const CGBlockInfo &blockInfo) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001872 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1873
Devang Patel2b594b92010-04-26 23:28:46 +00001874 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001875 return;
1876
John McCall6b5a61b2011-02-07 10:33:21 +00001877 bool isByRef = VD->hasAttr<BlocksAttr>();
1878
Mike Stumpb1a6e682009-09-30 02:43:10 +00001879 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001880 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001881 llvm::DIType Ty;
John McCall6b5a61b2011-02-07 10:33:21 +00001882 if (isByRef)
Devang Patel809b9bb2010-02-10 18:49:08 +00001883 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1884 else
1885 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001886
1887 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001888 unsigned Line = getLineNumber(VD->getLocation());
1889 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001890
John McCall6b5a61b2011-02-07 10:33:21 +00001891 const llvm::TargetData &target = CGM.getTargetData();
1892
1893 CharUnits offset = CharUnits::fromQuantity(
1894 target.getStructLayout(blockInfo.StructureType)
1895 ->getElementOffset(blockInfo.getCapture(VD).getIndex()));
1896
Mike Stumpb1a6e682009-09-30 02:43:10 +00001897 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001898 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
Devang Patel4a4e2ef2011-02-18 23:29:22 +00001899 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Chris Lattner14b1a362010-01-25 03:29:35 +00001900 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
John McCall6b5a61b2011-02-07 10:33:21 +00001901 if (isByRef) {
Devang Patel4a4e2ef2011-02-18 23:29:22 +00001902 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
1903 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001904 // offset of __forwarding field
John McCall6b5a61b2011-02-07 10:33:21 +00001905 offset = CharUnits::fromQuantity(target.getPointerSize()/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001906 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Devang Patel4a4e2ef2011-02-18 23:29:22 +00001907 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpDeref));
1908 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIBuilder::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001909 // offset of x field
1910 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001911 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001912 }
1913
1914 // Create the descriptor for the variable.
1915 llvm::DIVariable D =
Devang Patel16674e82011-02-22 18:56:36 +00001916 DBuilder.createComplexVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel823d8e92010-12-08 22:42:58 +00001917 VD->getName(), Unit, Line, Ty,
1918 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001919 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001920 llvm::Instruction *Call =
Devang Patel16674e82011-02-22 18:56:36 +00001921 DBuilder.insertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001922
Chris Lattnerc6034632010-04-01 06:31:43 +00001923 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001924 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001925}
1926
Devang Pateld6c5a262010-02-01 21:52:22 +00001927void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001928 llvm::Value *Storage,
1929 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001930 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001931}
1932
Mike Stumpb1a6e682009-09-30 02:43:10 +00001933void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
John McCall6b5a61b2011-02-07 10:33:21 +00001934 const VarDecl *variable, llvm::Value *Storage, CGBuilderTy &Builder,
1935 const CGBlockInfo &blockInfo) {
1936 EmitDeclare(variable, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder,
1937 blockInfo);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001938}
1939
Chris Lattner9c85ba32008-11-10 06:08:34 +00001940/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1941/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001942void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Devang Patel34753802011-02-16 01:11:51 +00001943 CGBuilderTy &Builder) {
1944 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001945}
1946
John McCall8178df32011-02-22 22:38:33 +00001947namespace {
1948 struct BlockLayoutChunk {
1949 uint64_t OffsetInBits;
1950 const BlockDecl::Capture *Capture;
1951 };
1952 bool operator<(const BlockLayoutChunk &l, const BlockLayoutChunk &r) {
1953 return l.OffsetInBits < r.OffsetInBits;
1954 }
1955}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001956
John McCall8178df32011-02-22 22:38:33 +00001957void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block,
1958 llvm::Value *addr,
1959 CGBuilderTy &Builder) {
1960 ASTContext &C = CGM.getContext();
1961 const BlockDecl *blockDecl = block.getBlockDecl();
1962
1963 // Collect some general information about the block's location.
1964 SourceLocation loc = blockDecl->getCaretLocation();
1965 llvm::DIFile tunit = getOrCreateFile(loc);
1966 unsigned line = getLineNumber(loc);
1967 unsigned column = getColumnNumber(loc);
1968
1969 // Build the debug-info type for the block literal.
1970 llvm::DIDescriptor enclosingContext =
1971 getContextDescriptor(cast<Decl>(blockDecl->getDeclContext()));
1972
1973 const llvm::StructLayout *blockLayout =
1974 CGM.getTargetData().getStructLayout(block.StructureType);
1975
1976 llvm::SmallVector<llvm::Value*, 16> fields;
1977 fields.push_back(createFieldType("__isa", C.VoidPtrTy, 0, loc, AS_public,
1978 blockLayout->getElementOffsetInBits(0),
1979 tunit));
1980 fields.push_back(createFieldType("__flags", C.IntTy, 0, loc, AS_public,
1981 blockLayout->getElementOffsetInBits(1),
1982 tunit));
1983 fields.push_back(createFieldType("__reserved", C.IntTy, 0, loc, AS_public,
1984 blockLayout->getElementOffsetInBits(2),
1985 tunit));
1986 fields.push_back(createFieldType("__FuncPtr", C.VoidPtrTy, 0, loc, AS_public,
1987 blockLayout->getElementOffsetInBits(3),
1988 tunit));
1989 fields.push_back(createFieldType("__descriptor",
1990 C.getPointerType(block.NeedsCopyDispose ?
1991 C.getBlockDescriptorExtendedType() :
1992 C.getBlockDescriptorType()),
1993 0, loc, AS_public,
1994 blockLayout->getElementOffsetInBits(4),
1995 tunit));
1996
1997 // We want to sort the captures by offset, not because DWARF
1998 // requires this, but because we're paranoid about debuggers.
1999 llvm::SmallVector<BlockLayoutChunk, 8> chunks;
2000
2001 // 'this' capture.
2002 if (blockDecl->capturesCXXThis()) {
2003 BlockLayoutChunk chunk;
2004 chunk.OffsetInBits =
2005 blockLayout->getElementOffsetInBits(block.CXXThisIndex);
2006 chunk.Capture = 0;
2007 chunks.push_back(chunk);
2008 }
2009
2010 // Variable captures.
2011 for (BlockDecl::capture_const_iterator
2012 i = blockDecl->capture_begin(), e = blockDecl->capture_end();
2013 i != e; ++i) {
2014 const BlockDecl::Capture &capture = *i;
2015 const VarDecl *variable = capture.getVariable();
2016 const CGBlockInfo::Capture &captureInfo = block.getCapture(variable);
2017
2018 // Ignore constant captures.
2019 if (captureInfo.isConstant())
2020 continue;
2021
2022 BlockLayoutChunk chunk;
2023 chunk.OffsetInBits =
2024 blockLayout->getElementOffsetInBits(captureInfo.getIndex());
2025 chunk.Capture = &capture;
2026 chunks.push_back(chunk);
2027 }
2028
2029 // Sort by offset.
2030 llvm::array_pod_sort(chunks.begin(), chunks.end());
2031
2032 for (llvm::SmallVectorImpl<BlockLayoutChunk>::iterator
2033 i = chunks.begin(), e = chunks.end(); i != e; ++i) {
2034 uint64_t offsetInBits = i->OffsetInBits;
2035 const BlockDecl::Capture *capture = i->Capture;
2036
2037 // If we have a null capture, this must be the C++ 'this' capture.
2038 if (!capture) {
2039 const CXXMethodDecl *method =
2040 cast<CXXMethodDecl>(blockDecl->getNonClosureContext());
2041 QualType type = method->getThisType(C);
2042
2043 fields.push_back(createFieldType("this", type, 0, loc, AS_public,
2044 offsetInBits, tunit));
2045 continue;
2046 }
2047
2048 const VarDecl *variable = capture->getVariable();
2049 QualType type = (capture->isByRef() ? C.VoidPtrTy : variable->getType());
2050 llvm::StringRef name = variable->getName();
2051 fields.push_back(createFieldType(name, type, 0, loc, AS_public,
2052 offsetInBits, tunit));
2053 }
2054
2055 llvm::SmallString<36> typeName;
2056 llvm::raw_svector_ostream(typeName)
2057 << "__block_literal_" << CGM.getUniqueBlockCount();
2058
2059 llvm::DIArray fieldsArray =
2060 DBuilder.getOrCreateArray(fields.data(), fields.size());
2061
2062 llvm::DIType type =
2063 DBuilder.createStructType(tunit, typeName.str(), tunit, line,
2064 CGM.getContext().toBits(block.BlockSize),
2065 CGM.getContext().toBits(block.BlockAlign),
2066 0, fieldsArray);
2067 type = DBuilder.createPointerType(type, CGM.PointerWidthInBits);
2068
2069 // Get overall information about the block.
2070 unsigned flags = llvm::DIDescriptor::FlagArtificial;
2071 llvm::MDNode *scope = RegionStack.back();
2072 llvm::StringRef name = ".block_descriptor";
2073
2074 // Create the descriptor for the parameter.
2075 llvm::DIVariable debugVar =
2076 DBuilder.createLocalVariable(llvm::dwarf::DW_TAG_arg_variable,
2077 llvm::DIDescriptor(scope),
2078 name, tunit, line, type,
2079 CGM.getLangOptions().Optimize, flags);
2080
2081 // Insert an llvm.dbg.value into the current block.
2082 llvm::Instruction *declare =
2083 DBuilder.insertDbgValueIntrinsic(addr, 0, debugVar,
2084 Builder.GetInsertBlock());
2085 declare->setDebugLoc(llvm::DebugLoc::get(line, column, scope));
2086}
Chris Lattner9c85ba32008-11-10 06:08:34 +00002087
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002088/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00002089void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00002090 const VarDecl *D) {
2091
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002092 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002093 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002094 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00002095
Devang Pateleb6d79b2010-02-01 21:34:11 +00002096 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002097 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002099 // CodeGen turns int[] into int[1] so we'll do the same here.
2100 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002101
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002102 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002103 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Anders Carlsson20f12a22009-12-06 18:00:51 +00002105 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00002106 ArrayType::Normal, 0);
2107 }
Devang Patel5d822f02010-04-29 17:48:37 +00002108 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00002109 llvm::StringRef LinkageName;
Devang Pateleb4c45b2011-02-09 19:16:38 +00002110 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext())
2111 && !isa<ObjCMethodDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00002112 LinkageName = Var->getName();
Devang Patel58faf202010-10-22 17:11:50 +00002113 if (LinkageName == DeclName)
2114 LinkageName = llvm::StringRef();
Devang Pateleb6d79b2010-02-01 21:34:11 +00002115 llvm::DIDescriptor DContext =
Devang Patel170cef32010-12-09 00:33:05 +00002116 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()));
Devang Patel16674e82011-02-22 18:56:36 +00002117 DBuilder.createStaticVariable(DContext, DeclName, LinkageName,
Devang Patel823d8e92010-12-08 22:42:58 +00002118 Unit, LineNo, getOrCreateType(T, Unit),
2119 Var->hasInternalLinkage(), Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00002120}
2121
Devang Patel9ca36b62009-02-26 21:10:26 +00002122/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00002123void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00002124 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00002125 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00002126 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00002127 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00002128
Devang Pateld6c5a262010-02-01 21:52:22 +00002129 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00002130
Devang Pateld6c5a262010-02-01 21:52:22 +00002131 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00002132 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Devang Patel9ca36b62009-02-26 21:10:26 +00002134 // CodeGen turns int[] into int[1] so we'll do the same here.
2135 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00002136
Devang Patel9ca36b62009-02-26 21:10:26 +00002137 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00002138 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00002139
Anders Carlsson20f12a22009-12-06 18:00:51 +00002140 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00002141 ArrayType::Normal, 0);
2142 }
2143
Devang Patel16674e82011-02-22 18:56:36 +00002144 DBuilder.createGlobalVariable(Name, Unit, LineNo,
Devang Patel823d8e92010-12-08 22:42:58 +00002145 getOrCreateType(T, Unit),
2146 Var->hasInternalLinkage(), Var);
Devang Patel9ca36b62009-02-26 21:10:26 +00002147}
Devang Patelabb485f2010-02-01 19:16:32 +00002148
Devang Patel25c2c8f2010-08-10 17:53:33 +00002149/// EmitGlobalVariable - Emit global variable's debug info.
2150void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00002151 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00002152 // Create the descriptor for the variable.
2153 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
2154 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00002155 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00002156 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
2157 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
Devang Patel31f7d022011-01-17 22:23:07 +00002158 Ty = CreateEnumType(ED);
Devang Patel6237cea2010-08-23 22:07:25 +00002159 }
Devang Patel0317ab02010-08-10 18:27:15 +00002160 // Do not use DIGlobalVariable for enums.
2161 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
2162 return;
Devang Patel16674e82011-02-22 18:56:36 +00002163 DBuilder.createStaticVariable(Unit, Name, Name, Unit,
Devang Patel823d8e92010-12-08 22:42:58 +00002164 getLineNumber(VD->getLocation()),
2165 Ty, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00002166}
2167
Devang Patelabb485f2010-02-01 19:16:32 +00002168/// getOrCreateNamesSpace - Return namespace descriptor for the given
2169/// namespace decl.
2170llvm::DINameSpace
Devang Patel170cef32010-12-09 00:33:05 +00002171CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl) {
Devang Patelabb485f2010-02-01 19:16:32 +00002172 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2173 NameSpaceCache.find(NSDecl);
2174 if (I != NameSpaceCache.end())
2175 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2176
Devang Patel8ab870d2010-05-12 23:46:38 +00002177 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel8c376682010-10-28 19:12:46 +00002178 llvm::DIFile FileD = getOrCreateFile(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002179 llvm::DIDescriptor Context =
Devang Patel170cef32010-12-09 00:33:05 +00002180 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()));
Devang Patelabb485f2010-02-01 19:16:32 +00002181 llvm::DINameSpace NS =
Devang Patel16674e82011-02-22 18:56:36 +00002182 DBuilder.createNameSpace(Context, NSDecl->getName(), FileD, LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002183 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002184 return NS;
2185}