blob: 5e85a590df2a6258e70b1ae62c0e87293c73c828 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
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
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
12// C Includes
13// C++ Includes
Greg Claytonff48e4b2015-02-03 02:05:44 +000014#include <mutex> // std::once
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
16
17// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000018
19// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000020// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000021// or another. This is bad because it means that if clang was built in release
22// mode, it assumes that you are building in release mode which is not always
23// the case. You can end up with functions that are defined as empty in header
24// files when NDEBUG is not defined, and this can cause link errors with the
25// clang .a files that you have since you might be missing functions in the .a
26// file. So we have to define NDEBUG when including clang headers to avoid any
27// mismatches. This is covered by rdar://problem/8691220
28
Sean Callanan3b1d4f62011-10-26 17:46:51 +000029#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000030#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000031#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000032// Need to include assert.h so it is as clang would expect it to be (disabled)
33#include <assert.h>
34#endif
35
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "clang/AST/ASTContext.h"
37#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000038#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000040#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000041#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "clang/AST/RecordLayout.h"
43#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000044#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000046#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000048#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/SourceManager.h"
50#include "clang/Basic/TargetInfo.h"
51#include "clang/Basic/TargetOptions.h"
52#include "clang/Frontend/FrontendOptions.h"
53#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000054
55#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000056#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000057#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
58// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
59#include <assert.h>
60#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Greg Claytond8d4a572015-08-11 21:38:15 +000062#include "llvm/Support/Signals.h"
63
Greg Clayton514487e2011-02-15 21:59:32 +000064#include "lldb/Core/ArchSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065#include "lldb/Core/dwarf.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000066#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000067#include "lldb/Core/Log.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000068#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000069#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000070#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000071#include "lldb/Core/UniqueCStringMap.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000072#include "lldb/Expression/ASTDumper.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000073#include "lldb/Symbol/ClangASTContext.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000074#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000075#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000076#include "lldb/Target/ExecutionContext.h"
77#include "lldb/Target/Process.h"
78#include "lldb/Target/ObjCLanguageRuntime.h"
79
Eli Friedman932197d2010-06-13 19:06:42 +000080#include <stdio.h>
81
Greg Clayton1341baf2013-07-11 23:36:31 +000082#include <mutex>
83
Greg Clayton8b4edba2015-08-14 20:02:05 +000084
85//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
86
87#ifdef ENABLE_DEBUG_PRINTF
88#include <stdio.h>
89#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
90#else
91#define DEBUG_PRINTF(fmt, ...)
92#endif
93
Greg Claytonc86103d2010-08-05 01:57:25 +000094using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095using namespace lldb_private;
96using namespace llvm;
97using namespace clang;
98
Enrico Granata2267ad42014-09-16 17:28:40 +000099typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000100
101static ClangASTMap &
102GetASTMap()
103{
Enrico Granata2267ad42014-09-16 17:28:40 +0000104 static ClangASTMap *g_map_ptr = nullptr;
105 static std::once_flag g_once_flag;
106 std::call_once(g_once_flag, []() {
107 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
108 });
109 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000110}
111
112
Greg Clayton57ee3062013-07-11 22:46:58 +0000113clang::AccessSpecifier
114ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000115{
116 switch (access)
117 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000118 default: break;
119 case eAccessNone: return AS_none;
120 case eAccessPublic: return AS_public;
121 case eAccessPrivate: return AS_private;
122 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000123 }
124 return AS_none;
125}
126
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000128ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129{
130 // FIXME: Cleanup per-file based stuff.
131
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000132 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133 // to move these to the language standard, and have the driver resolve the
134 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000135 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000137 } else if (IK == IK_ObjC ||
138 IK == IK_ObjCXX ||
139 IK == IK_PreprocessedObjC ||
140 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 Opts.ObjC1 = Opts.ObjC2 = 1;
142 }
143
144 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
145
146 if (LangStd == LangStandard::lang_unspecified) {
147 // Based on the base language, pick one.
148 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000149 case IK_None:
150 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000151 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000152 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000153 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 LangStd = LangStandard::lang_opencl;
155 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000156 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000157 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000158 LangStd = LangStandard::lang_cuda;
159 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000160 case IK_Asm:
161 case IK_C:
162 case IK_PreprocessedC:
163 case IK_ObjC:
164 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 LangStd = LangStandard::lang_gnu99;
166 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000167 case IK_CXX:
168 case IK_PreprocessedCXX:
169 case IK_ObjCXX:
170 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 LangStd = LangStandard::lang_gnucxx98;
172 break;
173 }
174 }
175
176 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000177 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 Opts.C99 = Std.isC99();
179 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000180 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 Opts.Digraphs = Std.hasDigraphs();
182 Opts.GNUMode = Std.isGNUMode();
183 Opts.GNUInline = !Std.isC99();
184 Opts.HexFloats = Std.hasHexFloats();
185 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000186
187 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
189 // OpenCL has some additional defaults.
190 if (LangStd == LangStandard::lang_opencl) {
191 Opts.OpenCL = 1;
192 Opts.AltiVec = 1;
193 Opts.CXXOperatorNames = 1;
194 Opts.LaxVectorConversions = 1;
195 }
196
197 // OpenCL and C++ both have bool, true, false keywords.
198 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
199
200// if (Opts.CPlusPlus)
201// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
202//
203// if (Args.hasArg(OPT_fobjc_gc_only))
204// Opts.setGCMode(LangOptions::GCOnly);
205// else if (Args.hasArg(OPT_fobjc_gc))
206// Opts.setGCMode(LangOptions::HybridGC);
207//
208// if (Args.hasArg(OPT_print_ivar_layout))
209// Opts.ObjCGCBitmapPrint = 1;
210//
211// if (Args.hasArg(OPT_faltivec))
212// Opts.AltiVec = 1;
213//
214// if (Args.hasArg(OPT_pthread))
215// Opts.POSIXThreads = 1;
216//
217// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
218// "default");
219// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000220 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000221// else if (Vis == "hidden")
222// Opts.setVisibilityMode(LangOptions::Hidden);
223// else if (Vis == "protected")
224// Opts.setVisibilityMode(LangOptions::Protected);
225// else
226// Diags.Report(diag::err_drv_invalid_value)
227// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
228
229// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
230
231 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
232 // is specified, or -std is set to a conforming mode.
233 Opts.Trigraphs = !Opts.GNUMode;
234// if (Args.hasArg(OPT_trigraphs))
235// Opts.Trigraphs = 1;
236//
237// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
238// OPT_fno_dollars_in_identifiers,
239// !Opts.AsmPreprocessor);
240// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
241// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
242// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
243// if (Args.hasArg(OPT_fno_lax_vector_conversions))
244// Opts.LaxVectorConversions = 0;
245// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
246// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
247// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000248 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
250// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
251// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
252// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
253// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
254// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
255// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
256// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
257// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
258// Diags);
259// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
260// Opts.ObjCConstantStringClass = getLastArgValue(Args,
261// OPT_fconstant_string_class);
262// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
263// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
264// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
265// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
266// Opts.Static = Args.hasArg(OPT_static_define);
267 Opts.OptimizeSize = 0;
268
269 // FIXME: Eliminate this dependency.
270// unsigned Opt =
271// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
272// Opts.Optimize = Opt != 0;
273 unsigned Opt = 0;
274
275 // This is the __NO_INLINE__ define, which just depends on things like the
276 // optimization level and -fno-inline, not actually whether the backend has
277 // inlining enabled.
278 //
279 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000280 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
282// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
283// switch (SSP) {
284// default:
285// Diags.Report(diag::err_drv_invalid_value)
286// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
287// break;
288// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
289// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
290// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
291// }
292}
293
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
Greg Clayton6beaaa62011-01-17 03:46:26 +0000295ClangASTContext::ClangASTContext (const char *target_triple) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 m_target_triple(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000297 m_ast_ap(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 m_language_options_ap(),
299 m_source_manager_ap(),
Sean Callanan880e6802011-10-07 23:18:13 +0000300 m_diagnostics_engine_ap(),
Sean Callananc5069ad2012-10-17 22:11:14 +0000301 m_target_options_rp(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 m_target_info_ap(),
303 m_identifier_table_ap(),
304 m_selector_table_ap(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000305 m_builtins_ap(),
Ed Masted4612ad2014-04-20 13:17:36 +0000306 m_callback_tag_decl (nullptr),
307 m_callback_objc_decl (nullptr),
308 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000309 m_pointer_byte_size (0),
310 m_ast_owned(false)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000311
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312{
313 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000314 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
317//----------------------------------------------------------------------
318// Destructor
319//----------------------------------------------------------------------
320ClangASTContext::~ClangASTContext()
321{
Enrico Granata5d84a692014-08-19 21:46:37 +0000322 if (m_ast_ap.get())
323 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000324 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000325 if (!m_ast_owned)
326 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000327 }
328
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 m_builtins_ap.reset();
330 m_selector_table_ap.reset();
331 m_identifier_table_ap.reset();
332 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000333 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000334 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 m_source_manager_ap.reset();
336 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000337 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338}
339
340
341void
342ClangASTContext::Clear()
343{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 m_language_options_ap.reset();
346 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000347 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000348 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 m_target_info_ap.reset();
350 m_identifier_table_ap.reset();
351 m_selector_table_ap.reset();
352 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000353 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
356const char *
357ClangASTContext::GetTargetTriple ()
358{
359 return m_target_triple.c_str();
360}
361
362void
363ClangASTContext::SetTargetTriple (const char *target_triple)
364{
365 Clear();
366 m_target_triple.assign(target_triple);
367}
368
Greg Clayton514487e2011-02-15 21:59:32 +0000369void
370ClangASTContext::SetArchitecture (const ArchSpec &arch)
371{
Greg Clayton880cbb02011-07-30 01:26:02 +0000372 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000373}
374
Greg Clayton6beaaa62011-01-17 03:46:26 +0000375bool
376ClangASTContext::HasExternalSource ()
377{
378 ASTContext *ast = getASTContext();
379 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000380 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000381 return false;
382}
383
384void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000385ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000386{
387 ASTContext *ast = getASTContext();
388 if (ast)
389 {
390 ast->setExternalSource (ast_source_ap);
391 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
392 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
393 }
394}
395
396void
397ClangASTContext::RemoveExternalSource ()
398{
399 ASTContext *ast = getASTContext();
400
401 if (ast)
402 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000403 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000404 ast->setExternalSource (empty_ast_source_ap);
405 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
406 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
407 }
408}
409
Greg Claytond8d4a572015-08-11 21:38:15 +0000410void
411ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
412{
413 if (!m_ast_owned) {
414 m_ast_ap.release();
415 }
416 m_ast_owned = false;
417 m_ast_ap.reset(ast_ctx);
418 GetASTMap().Insert(ast_ctx, this);
419}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420
421ASTContext *
422ClangASTContext::getASTContext()
423{
Ed Masted4612ad2014-04-20 13:17:36 +0000424 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000426 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000427 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
428 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000429 *getIdentifierTable(),
430 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000431 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000432
433 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000434
435 // This can be NULL if we don't know anything about the architecture or if the
436 // target for an architecture isn't enabled in the llvm/clang that we built
437 TargetInfo *target_info = getTargetInfo();
438 if (target_info)
439 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000440
Greg Clayton6beaaa62011-01-17 03:46:26 +0000441 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
442 {
443 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
444 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
445 }
446
Enrico Granata2267ad42014-09-16 17:28:40 +0000447 GetASTMap().Insert(m_ast_ap.get(), this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000449 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450}
451
Enrico Granata5d84a692014-08-19 21:46:37 +0000452ClangASTContext*
453ClangASTContext::GetASTContext (clang::ASTContext* ast)
454{
Enrico Granata2267ad42014-09-16 17:28:40 +0000455 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000456 return clang_ast;
457}
458
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459Builtin::Context *
460ClangASTContext::getBuiltinContext()
461{
Ed Masted4612ad2014-04-20 13:17:36 +0000462 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000463 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 return m_builtins_ap.get();
465}
466
467IdentifierTable *
468ClangASTContext::getIdentifierTable()
469{
Ed Masted4612ad2014-04-20 13:17:36 +0000470 if (m_identifier_table_ap.get() == nullptr)
471 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 return m_identifier_table_ap.get();
473}
474
475LangOptions *
476ClangASTContext::getLanguageOptions()
477{
Ed Masted4612ad2014-04-20 13:17:36 +0000478 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 {
480 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000481 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000482// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 }
484 return m_language_options_ap.get();
485}
486
487SelectorTable *
488ClangASTContext::getSelectorTable()
489{
Ed Masted4612ad2014-04-20 13:17:36 +0000490 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491 m_selector_table_ap.reset (new SelectorTable());
492 return m_selector_table_ap.get();
493}
494
Sean Callanan79439e82010-11-18 02:56:27 +0000495clang::FileManager *
496ClangASTContext::getFileManager()
497{
Ed Masted4612ad2014-04-20 13:17:36 +0000498 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000499 {
500 clang::FileSystemOptions file_system_options;
501 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
502 }
Sean Callanan79439e82010-11-18 02:56:27 +0000503 return m_file_manager_ap.get();
504}
505
Greg Claytone1a916a2010-07-21 22:12:05 +0000506clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507ClangASTContext::getSourceManager()
508{
Ed Masted4612ad2014-04-20 13:17:36 +0000509 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000510 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511 return m_source_manager_ap.get();
512}
513
Sean Callanan880e6802011-10-07 23:18:13 +0000514clang::DiagnosticsEngine *
515ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516{
Ed Masted4612ad2014-04-20 13:17:36 +0000517 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000518 {
519 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000520 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000521 }
Sean Callanan880e6802011-10-07 23:18:13 +0000522 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523}
524
Sean Callanan880e6802011-10-07 23:18:13 +0000525class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000526{
527public:
Sean Callanan880e6802011-10-07 23:18:13 +0000528 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000529 {
530 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
531 }
532
Sean Callanan880e6802011-10-07 23:18:13 +0000533 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000534 {
535 if (m_log)
536 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000537 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000538 info.FormatDiagnostic(diag_str);
539 diag_str.push_back('\0');
540 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
541 }
542 }
Sean Callanan880e6802011-10-07 23:18:13 +0000543
544 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
545 {
546 return new NullDiagnosticConsumer ();
547 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000548private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000549 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000550};
551
Sean Callanan880e6802011-10-07 23:18:13 +0000552DiagnosticConsumer *
553ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000554{
Ed Masted4612ad2014-04-20 13:17:36 +0000555 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000556 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000557
Sean Callanan880e6802011-10-07 23:18:13 +0000558 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000559}
560
Jason Molenda45938b92014-07-08 23:46:39 +0000561std::shared_ptr<TargetOptions> &
562ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000563 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 {
Alp Toker5f838642014-07-06 05:36:57 +0000565 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000566 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000567 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 }
Alp Toker5f838642014-07-06 05:36:57 +0000569 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
572
573TargetInfo *
574ClangASTContext::getTargetInfo()
575{
Greg Clayton70512312012-05-08 01:45:38 +0000576 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000577 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000578 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 return m_target_info_ap.get();
580}
581
582#pragma mark Basic Types
583
584static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000585QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000587 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 if (qual_type_bit_size == bit_size)
589 return true;
590 return false;
591}
Greg Claytona1e5dc82015-08-11 22:53:00 +0000592CompilerType
Greg Claytonc86103d2010-08-05 01:57:25 +0000593ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594{
Greg Clayton57ee3062013-07-11 22:46:58 +0000595 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596}
597
Greg Claytona1e5dc82015-08-11 22:53:00 +0000598CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000599ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000601 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000602 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 switch (encoding)
604 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000605 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000606 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000607 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608 break;
609
Greg Claytonc86103d2010-08-05 01:57:25 +0000610 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000611 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000612 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000613 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000614 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000615 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000616 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000617 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000618 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000619 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000620 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000621 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000622 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 break;
624
Greg Claytonc86103d2010-08-05 01:57:25 +0000625 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000626 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000627 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000628 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000629 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000630 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000631 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000632 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000633 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000634 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000635 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000636 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000637 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 break;
639
Greg Claytonc86103d2010-08-05 01:57:25 +0000640 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000641 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000642 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000643 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000644 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000645 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000646 return CompilerType (ast, ast->LongDoubleTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647 break;
648
Greg Claytonc86103d2010-08-05 01:57:25 +0000649 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000650 // Sanity check that bit_size is a multiple of 8's.
651 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000652 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000653 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 }
655
Greg Claytona1e5dc82015-08-11 22:53:00 +0000656 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657}
658
Greg Clayton57ee3062013-07-11 22:46:58 +0000659
660
661lldb::BasicType
662ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
663{
664 if (name)
665 {
666 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
667 static TypeNameToBasicTypeMap g_type_map;
668 static std::once_flag g_once_flag;
669 std::call_once(g_once_flag, [](){
670 // "void"
671 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
672
673 // "char"
674 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
675 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
676 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
677 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
678 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
679 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
680 // "short"
681 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
682 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
683 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
684 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
685
686 // "int"
687 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
688 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
689 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
690 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
691
692 // "long"
693 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
694 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
695 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
696 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
697
698 // "long long"
699 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
700 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
701 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
702 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
703
704 // "int128"
705 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
706 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
707
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000708 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000709 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
710 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
711 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
712 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
713 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
714 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
715 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
716 g_type_map.Sort();
717 });
718
719 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
720 }
721 return eBasicTypeInvalid;
722}
723
Greg Claytona1e5dc82015-08-11 22:53:00 +0000724CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000725ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
726{
727 if (ast)
728 {
729 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
730 return ClangASTContext::GetBasicType (ast, basic_type);
731 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000732 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000733}
734
735uint32_t
736ClangASTContext::GetPointerByteSize ()
737{
738 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000739 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000740 return m_pointer_byte_size;
741}
742
Greg Claytona1e5dc82015-08-11 22:53:00 +0000743CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000744ClangASTContext::GetBasicType (lldb::BasicType basic_type)
745{
746 return GetBasicType (getASTContext(), basic_type);
747}
748
Greg Claytona1e5dc82015-08-11 22:53:00 +0000749CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000750ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
751{
752 if (ast)
753 {
Ed Masted4612ad2014-04-20 13:17:36 +0000754 clang_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000755
756 switch (basic_type)
757 {
758 case eBasicTypeInvalid:
759 case eBasicTypeOther:
760 break;
761 case eBasicTypeVoid:
762 clang_type = ast->VoidTy.getAsOpaquePtr();
763 break;
764 case eBasicTypeChar:
765 clang_type = ast->CharTy.getAsOpaquePtr();
766 break;
767 case eBasicTypeSignedChar:
768 clang_type = ast->SignedCharTy.getAsOpaquePtr();
769 break;
770 case eBasicTypeUnsignedChar:
771 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
772 break;
773 case eBasicTypeWChar:
774 clang_type = ast->getWCharType().getAsOpaquePtr();
775 break;
776 case eBasicTypeSignedWChar:
777 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
778 break;
779 case eBasicTypeUnsignedWChar:
780 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
781 break;
782 case eBasicTypeChar16:
783 clang_type = ast->Char16Ty.getAsOpaquePtr();
784 break;
785 case eBasicTypeChar32:
786 clang_type = ast->Char32Ty.getAsOpaquePtr();
787 break;
788 case eBasicTypeShort:
789 clang_type = ast->ShortTy.getAsOpaquePtr();
790 break;
791 case eBasicTypeUnsignedShort:
792 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
793 break;
794 case eBasicTypeInt:
795 clang_type = ast->IntTy.getAsOpaquePtr();
796 break;
797 case eBasicTypeUnsignedInt:
798 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
799 break;
800 case eBasicTypeLong:
801 clang_type = ast->LongTy.getAsOpaquePtr();
802 break;
803 case eBasicTypeUnsignedLong:
804 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
805 break;
806 case eBasicTypeLongLong:
807 clang_type = ast->LongLongTy.getAsOpaquePtr();
808 break;
809 case eBasicTypeUnsignedLongLong:
810 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
811 break;
812 case eBasicTypeInt128:
813 clang_type = ast->Int128Ty.getAsOpaquePtr();
814 break;
815 case eBasicTypeUnsignedInt128:
816 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
817 break;
818 case eBasicTypeBool:
819 clang_type = ast->BoolTy.getAsOpaquePtr();
820 break;
821 case eBasicTypeHalf:
822 clang_type = ast->HalfTy.getAsOpaquePtr();
823 break;
824 case eBasicTypeFloat:
825 clang_type = ast->FloatTy.getAsOpaquePtr();
826 break;
827 case eBasicTypeDouble:
828 clang_type = ast->DoubleTy.getAsOpaquePtr();
829 break;
830 case eBasicTypeLongDouble:
831 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
832 break;
833 case eBasicTypeFloatComplex:
834 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
835 break;
836 case eBasicTypeDoubleComplex:
837 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
838 break;
839 case eBasicTypeLongDoubleComplex:
840 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
841 break;
842 case eBasicTypeObjCID:
843 clang_type = ast->getObjCIdType().getAsOpaquePtr();
844 break;
845 case eBasicTypeObjCClass:
846 clang_type = ast->getObjCClassType().getAsOpaquePtr();
847 break;
848 case eBasicTypeObjCSel:
849 clang_type = ast->getObjCSelType().getAsOpaquePtr();
850 break;
851 case eBasicTypeNullPtr:
852 clang_type = ast->NullPtrTy.getAsOpaquePtr();
853 break;
854 }
855
856 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000857 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +0000858 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000859 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000860}
861
862
Greg Claytona1e5dc82015-08-11 22:53:00 +0000863CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
865{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000866 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000867
868#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000869 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000870 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 {
872 switch (dw_ate)
873 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000874 default:
875 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000876
Sean Callanan38d4df52012-04-03 01:10:10 +0000877 case DW_ATE_address:
878 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000879 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000880 break;
881
882 case DW_ATE_boolean:
883 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000884 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000885 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000886 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000887 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000888 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000889 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000890 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000891 break;
892
893 case DW_ATE_lo_user:
894 // This has been seen to mean DW_AT_complex_integer
895 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000896 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000897 if (::strstr(type_name, "complex"))
898 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000899 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
900 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000901 }
Greg Clayton605684e2011-10-28 23:06:08 +0000902 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000903 break;
904
905 case DW_ATE_complex_float:
906 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000907 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000908 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000909 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000910 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000911 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000912 else
Greg Clayton605684e2011-10-28 23:06:08 +0000913 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000914 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
915 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +0000916 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000917 break;
918
919 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +0000920 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000921 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000922 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000923 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000924 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000925 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000926 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +0000927 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000928 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000929 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000930 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000931 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000932 return CompilerType (ast, ast->LongDoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000933 break;
934
935 case DW_ATE_signed:
936 if (type_name)
937 {
938 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +0000939 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000940 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000941 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000942 if (streq(type_name, "void") &&
943 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000944 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000945 if (strstr(type_name, "long long") &&
946 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000947 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000948 if (strstr(type_name, "long") &&
949 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000950 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000951 if (strstr(type_name, "short") &&
952 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000953 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000954 if (strstr(type_name, "char"))
955 {
956 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000957 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000958 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000959 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000960 }
961 if (strstr(type_name, "int"))
962 {
963 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000964 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000965 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000966 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000967 }
968 }
969 // We weren't able to match up a type name, just search by size
970 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000971 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000972 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000973 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000974 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000975 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000976 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000977 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000979 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000980 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000981 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000982 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000983
Sean Callanan38d4df52012-04-03 01:10:10 +0000984 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000985 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +0000986 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000987 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000988 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000989 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000990 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000991 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000992 break;
993
994 case DW_ATE_unsigned:
995 if (type_name)
996 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +0000997 if (streq(type_name, "wchar_t"))
998 {
999 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1000 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001001 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001002 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001003 }
1004 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001005 if (strstr(type_name, "long long"))
1006 {
1007 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001008 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001009 }
1010 else if (strstr(type_name, "long"))
1011 {
1012 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001014 }
1015 else if (strstr(type_name, "short"))
1016 {
1017 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001018 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001019 }
1020 else if (strstr(type_name, "char"))
1021 {
1022 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001023 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001024 }
1025 else if (strstr(type_name, "int"))
1026 {
1027 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001028 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001029 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001030 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001031 }
1032 }
1033 // We weren't able to match up a type name, just search by size
1034 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001035 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001036 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001037 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001038 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001039 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001040 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001041 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001042 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001043 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001044 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001045 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001046 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001047
Sean Callanan38d4df52012-04-03 01:10:10 +00001048 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001049 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1050 {
1051 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001052 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001053 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001054 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001055 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001056 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001057 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001058 break;
1059
1060 case DW_ATE_imaginary_float:
1061 break;
1062
1063 case DW_ATE_UTF:
1064 if (type_name)
1065 {
1066 if (streq(type_name, "char16_t"))
1067 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001068 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001069 }
1070 else if (streq(type_name, "char32_t"))
1071 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001072 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001073 }
1074 }
1075 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 }
1077 }
1078 // This assert should fire for anything that we don't catch above so we know
1079 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001080 if (type_name)
1081 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001082 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type '%s' encoded with DW_ATE = 0x%x, bit_size = %u\n", type_name, dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +00001083 }
1084 else
1085 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001086 Host::SystemLog (Host::eSystemLogError, "error: need to add support for DW_TAG_base_type encoded with DW_ATE = 0x%x, bit_size = %u\n", dw_ate, bit_size);
Greg Claytondc968d12011-05-17 18:15:05 +00001087 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001088 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089}
1090
Greg Claytona1e5dc82015-08-11 22:53:00 +00001091CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001092ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1093{
Greg Clayton57ee3062013-07-11 22:46:58 +00001094 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001095 return CompilerType (ast, ast->UnknownAnyTy);
1096 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001097}
1098
Greg Claytona1e5dc82015-08-11 22:53:00 +00001099CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100ClangASTContext::GetCStringType (bool is_const)
1101{
Greg Clayton57ee3062013-07-11 22:46:58 +00001102 ASTContext *ast = getASTContext();
1103 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104
1105 if (is_const)
1106 char_type.addConst();
1107
Greg Claytona1e5dc82015-08-11 22:53:00 +00001108 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109}
1110
Sean Callanan09ab4b72011-11-30 22:11:59 +00001111clang::DeclContext *
1112ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1113{
1114 return ast->getTranslationUnitDecl();
1115}
1116
Greg Claytona1e5dc82015-08-11 22:53:00 +00001117CompilerType
Greg Clayton38a61402010-12-02 23:20:03 +00001118ClangASTContext::CopyType (ASTContext *dst_ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001119 CompilerType src)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120{
Sean Callanan79439e82010-11-18 02:56:27 +00001121 FileSystemOptions file_system_options;
Greg Claytond8d4a572015-08-11 21:38:15 +00001122 ClangASTContext *src_ast = src.GetTypeSystem()->AsClangASTContext();
1123 if (src_ast == nullptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001124 return CompilerType();
Greg Clayton38a61402010-12-02 23:20:03 +00001125 FileManager file_manager (file_system_options);
1126 ASTImporter importer(*dst_ast, file_manager,
Greg Claytond8d4a572015-08-11 21:38:15 +00001127 *src_ast->getASTContext(), file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001128 false);
Sean Callanan0617fcb2010-11-09 22:37:10 +00001129
Greg Claytond8d4a572015-08-11 21:38:15 +00001130 QualType dst (importer.Import(GetQualType(src)));
Sean Callanan0617fcb2010-11-09 22:37:10 +00001131
Greg Claytona1e5dc82015-08-11 22:53:00 +00001132 return CompilerType (dst_ast, dst);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133}
1134
Greg Clayton526e5af2010-11-13 03:52:47 +00001135
1136clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001137ClangASTContext::CopyDecl (ASTContext *dst_ast,
1138 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001139 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001140{
Sean Callanan79439e82010-11-18 02:56:27 +00001141 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001142 FileManager file_manager (file_system_options);
1143 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001144 *src_ast, file_manager,
1145 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001146
1147 return importer.Import(source_decl);
1148}
1149
Sean Callanan23a30272010-07-16 00:00:27 +00001150bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001151ClangASTContext::AreTypesSame (CompilerType type1,
1152 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001153 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001154{
Greg Claytond8d4a572015-08-11 21:38:15 +00001155 TypeSystem *ast = type1.GetTypeSystem();
1156 if (!ast->AsClangASTContext() || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001157 return false;
1158
1159 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001160 return true;
1161
Greg Claytond8d4a572015-08-11 21:38:15 +00001162 QualType type1_qual = GetQualType(type1);
1163 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001164
1165 if (ignore_qualifiers)
1166 {
1167 type1_qual = type1_qual.getUnqualifiedType();
1168 type2_qual = type2_qual.getUnqualifiedType();
1169 }
1170
Greg Claytond8d4a572015-08-11 21:38:15 +00001171 return ast->AsClangASTContext()->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172}
1173
Greg Claytona1e5dc82015-08-11 22:53:00 +00001174CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001175ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1176{
1177 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1178 return GetTypeForDecl(interface_decl);
1179 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1180 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001181 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001182}
1183
Greg Clayton6beaaa62011-01-17 03:46:26 +00001184
Greg Claytona1e5dc82015-08-11 22:53:00 +00001185CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001186ClangASTContext::GetTypeForDecl (TagDecl *decl)
1187{
1188 // No need to call the getASTContext() accessor (which can create the AST
1189 // if it isn't created yet, because we can't have created a decl in this
1190 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001191 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001192 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001193 return CompilerType (ast, ast->getTagDeclType(decl));
1194 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001195}
1196
Greg Claytona1e5dc82015-08-11 22:53:00 +00001197CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001198ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1199{
1200 // No need to call the getASTContext() accessor (which can create the AST
1201 // if it isn't created yet, because we can't have created a decl in this
1202 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001203 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001204 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001205 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1206 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001207}
1208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209#pragma mark Structure, Unions, Classes
1210
Greg Claytona1e5dc82015-08-11 22:53:00 +00001211CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001212ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1213 AccessType access_type,
1214 const char *name,
1215 int kind,
1216 LanguageType language,
1217 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001219 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001220 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001221
Ed Masted4612ad2014-04-20 13:17:36 +00001222 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001223 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224
Greg Clayton9e409562010-07-28 02:04:09 +00001225
Greg Claytone1be9962011-08-24 23:50:00 +00001226 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001227 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001228 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001229 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001230 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001231 }
1232
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1234 // we will need to update this code. I was told to currently always use
1235 // the CXXRecordDecl class since we often don't know from debug information
1236 // if something is struct or a class, so we default to always use the more
1237 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001238
1239 bool is_anonymous = (!name) || (!name[0]);
1240
Greg Claytonf0705c82011-10-22 03:33:13 +00001241 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1242 (TagDecl::TagKind)kind,
1243 decl_ctx,
1244 SourceLocation(),
1245 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001246 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001247
1248 if (is_anonymous)
1249 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001250
Greg Claytonc4ffd662013-03-08 01:37:30 +00001251 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001252 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001253 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001254 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001255
Greg Clayton55561e92011-10-26 03:31:36 +00001256 if (access_type != eAccessNone)
1257 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001258
1259 if (decl_ctx)
1260 decl_ctx->addDecl (decl);
1261
Greg Claytona1e5dc82015-08-11 22:53:00 +00001262 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001263 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001264 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001265}
1266
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001267static TemplateParameterList *
1268CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001269 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001270 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1271{
1272 const bool parameter_pack = false;
1273 const bool is_typename = false;
1274 const unsigned depth = 0;
1275 const size_t num_template_params = template_param_infos.GetSize();
1276 for (size_t i=0; i<num_template_params; ++i)
1277 {
1278 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001279
Ed Masted4612ad2014-04-20 13:17:36 +00001280 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001281 if (name && name[0])
1282 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001283 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001284 {
1285 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1286 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1287 SourceLocation(),
1288 SourceLocation(),
1289 depth,
1290 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001291 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001292 template_param_infos.args[i].getIntegralType(),
1293 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001294 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001295
1296 }
1297 else
1298 {
1299 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1300 ast->getTranslationUnitDecl(), // Is this the right decl context?
1301 SourceLocation(),
1302 SourceLocation(),
1303 depth,
1304 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001305 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001306 is_typename,
1307 parameter_pack));
1308 }
1309 }
1310
1311 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1312 SourceLocation(),
1313 SourceLocation(),
1314 &template_param_decls.front(),
1315 template_param_decls.size(),
1316 SourceLocation());
1317 return template_param_list;
1318}
1319
1320clang::FunctionTemplateDecl *
1321ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1322 clang::FunctionDecl *func_decl,
1323 const char *name,
1324 const TemplateParameterInfos &template_param_infos)
1325{
1326// /// \brief Create a function template node.
1327 ASTContext *ast = getASTContext();
1328
1329 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1330
1331 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1332 template_param_infos,
1333 template_param_decls);
1334 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1335 decl_ctx,
1336 func_decl->getLocation(),
1337 func_decl->getDeclName(),
1338 template_param_list,
1339 func_decl);
1340
1341 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1342 i < template_param_decl_count;
1343 ++i)
1344 {
1345 // TODO: verify which decl context we should put template_param_decls into..
1346 template_param_decls[i]->setDeclContext (func_decl);
1347 }
1348
1349 return func_tmpl_decl;
1350}
1351
1352void
1353ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1354 clang::FunctionTemplateDecl *func_tmpl_decl,
1355 const TemplateParameterInfos &infos)
1356{
1357 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1358 infos.args.data(),
1359 infos.args.size());
1360
1361 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1362 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001363 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001364}
1365
1366
Greg Claytonf0705c82011-10-22 03:33:13 +00001367ClassTemplateDecl *
1368ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001369 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001370 const char *class_name,
1371 int kind,
1372 const TemplateParameterInfos &template_param_infos)
1373{
1374 ASTContext *ast = getASTContext();
1375
Ed Masted4612ad2014-04-20 13:17:36 +00001376 ClassTemplateDecl *class_template_decl = nullptr;
1377 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001378 decl_ctx = ast->getTranslationUnitDecl();
1379
1380 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1381 DeclarationName decl_name (&identifier_info);
1382
1383 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001384
1385 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001386 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001387 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001388 if (class_template_decl)
1389 return class_template_decl;
1390 }
1391
1392 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001393
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001394 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1395 template_param_infos,
1396 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001397
1398 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1399 (TagDecl::TagKind)kind,
1400 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1401 SourceLocation(),
1402 SourceLocation(),
1403 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001404
1405 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1406 i < template_param_decl_count;
1407 ++i)
1408 {
1409 template_param_decls[i]->setDeclContext (template_cxx_decl);
1410 }
1411
Sean Callananb5c79622011-11-19 01:35:08 +00001412 // With templated classes, we say that a class is templated with
1413 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001414 //template_cxx_decl->startDefinition();
1415 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001416
Greg Claytonf0705c82011-10-22 03:33:13 +00001417 class_template_decl = ClassTemplateDecl::Create (*ast,
1418 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1419 SourceLocation(),
1420 decl_name,
1421 template_param_list,
1422 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001423 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001424
1425 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001426 {
Greg Clayton55561e92011-10-26 03:31:36 +00001427 if (access_type != eAccessNone)
1428 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001429
1430 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1431 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1432
Greg Claytonf0705c82011-10-22 03:33:13 +00001433 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001434
1435#ifdef LLDB_CONFIGURATION_DEBUG
1436 VerifyDecl(class_template_decl);
1437#endif
1438 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001439
1440 return class_template_decl;
1441}
1442
1443
1444ClassTemplateSpecializationDecl *
1445ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1446 ClassTemplateDecl *class_template_decl,
1447 int kind,
1448 const TemplateParameterInfos &template_param_infos)
1449{
1450 ASTContext *ast = getASTContext();
1451 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1452 (TagDecl::TagKind)kind,
1453 decl_ctx,
1454 SourceLocation(),
1455 SourceLocation(),
1456 class_template_decl,
1457 &template_param_infos.args.front(),
1458 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001459 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001460
Sean Callananfa4fab72013-02-01 06:55:48 +00001461 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1462
Greg Claytonf0705c82011-10-22 03:33:13 +00001463 return class_template_specialization_decl;
1464}
1465
Greg Claytona1e5dc82015-08-11 22:53:00 +00001466CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001467ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1468{
1469 if (class_template_specialization_decl)
1470 {
1471 ASTContext *ast = getASTContext();
1472 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001473 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001474 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001475 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476}
1477
Greg Clayton090d0982011-06-19 03:43:27 +00001478static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001479check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001480{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001481 // Special-case call since it can take any number of operands
1482 if(op_kind == OO_Call)
1483 return true;
1484
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001485 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001486 if (num_params == 0)
1487 return unary;
1488 if (num_params == 1)
1489 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001490 else
Greg Clayton090d0982011-06-19 03:43:27 +00001491 return false;
1492}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001493
Greg Clayton090d0982011-06-19 03:43:27 +00001494bool
1495ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1496{
Sean Callanan5b26f272012-02-04 08:49:35 +00001497 switch (op_kind)
1498 {
1499 default:
1500 break;
1501 // C++ standard allows any number of arguments to new/delete
1502 case OO_New:
1503 case OO_Array_New:
1504 case OO_Delete:
1505 case OO_Array_Delete:
1506 return true;
1507 }
1508
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001509#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly) case OO_##Name: return check_op_param (op_kind, Unary, Binary, num_params);
Greg Clayton090d0982011-06-19 03:43:27 +00001510 switch (op_kind)
1511 {
1512#include "clang/Basic/OperatorKinds.def"
1513 default: break;
1514 }
1515 return false;
1516}
1517
Greg Clayton57ee3062013-07-11 22:46:58 +00001518clang::AccessSpecifier
1519ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001520{
1521 clang::AccessSpecifier ret = lhs;
1522
1523 // Make the access equal to the stricter of the field and the nested field's access
1524 switch (ret)
1525 {
1526 case clang::AS_none:
1527 break;
1528 case clang::AS_private:
1529 break;
1530 case clang::AS_protected:
1531 if (rhs == AS_private)
1532 ret = AS_private;
1533 break;
1534 case clang::AS_public:
1535 ret = rhs;
1536 break;
1537 }
1538
1539 return ret;
1540}
1541
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001542bool
1543ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1544{
1545 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1546}
1547
1548bool
1549ClangASTContext::FieldIsBitfield
1550(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001551 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001552 FieldDecl* field,
1553 uint32_t& bitfield_bit_size
1554)
1555{
Ed Masted4612ad2014-04-20 13:17:36 +00001556 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557 return false;
1558
1559 if (field->isBitField())
1560 {
1561 Expr* bit_width_expr = field->getBitWidth();
1562 if (bit_width_expr)
1563 {
1564 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001565 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 {
1567 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1568 return true;
1569 }
1570 }
1571 }
1572 return false;
1573}
1574
1575bool
1576ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1577{
Ed Masted4612ad2014-04-20 13:17:36 +00001578 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 return false;
1580
1581 if (!record_decl->field_empty())
1582 return true;
1583
1584 // No fields, lets check this is a CXX record and check the base classes
1585 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1586 if (cxx_record_decl)
1587 {
1588 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1589 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1590 base_class != base_class_end;
1591 ++base_class)
1592 {
1593 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1594 if (RecordHasFields(base_class_decl))
1595 return true;
1596 }
1597 }
1598 return false;
1599}
1600
Greg Clayton8cf05932010-07-22 18:30:50 +00001601#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602
Greg Claytona1e5dc82015-08-11 22:53:00 +00001603CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001604ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001605(
1606 const char *name,
1607 DeclContext *decl_ctx,
1608 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001609 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001610 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001611)
1612{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001613 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001614 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001615 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001616 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001617 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001618
Greg Clayton6beaaa62011-01-17 03:46:26 +00001619 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001620 decl_ctx,
1621 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001622 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001623 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001624 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001625 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001626 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001627 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001628
Jim Ingham379397632012-10-27 02:54:13 +00001629 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001630 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001631
Greg Claytona1e5dc82015-08-11 22:53:00 +00001632 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633}
1634
1635static inline bool
1636BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1637{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001638 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639}
1640
Greg Clayton57ee3062013-07-11 22:46:58 +00001641uint32_t
1642ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643{
1644 uint32_t num_bases = 0;
1645 if (cxx_record_decl)
1646 {
1647 if (omit_empty_base_classes)
1648 {
1649 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1650 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1651 base_class != base_class_end;
1652 ++base_class)
1653 {
1654 // Skip empty base classes
1655 if (omit_empty_base_classes)
1656 {
1657 if (BaseSpecifierIsEmpty (base_class))
1658 continue;
1659 }
1660 ++num_bases;
1661 }
1662 }
1663 else
1664 num_bases = cxx_record_decl->getNumBases();
1665 }
1666 return num_bases;
1667}
1668
1669
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670#pragma mark Namespace Declarations
1671
1672NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001673ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674{
Ed Masted4612ad2014-04-20 13:17:36 +00001675 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001676 ASTContext *ast = getASTContext();
1677 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001678 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001679 decl_ctx = translation_unit_decl;
1680
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681 if (name)
1682 {
Greg Clayton030a2042011-10-14 21:34:45 +00001683 IdentifierInfo &identifier_info = ast->Idents.get(name);
1684 DeclarationName decl_name (&identifier_info);
1685 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001686 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001687 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001688 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001689 if (namespace_decl)
1690 return namespace_decl;
1691 }
1692
Sean Callanan5b26f272012-02-04 08:49:35 +00001693 namespace_decl = NamespaceDecl::Create(*ast,
1694 decl_ctx,
1695 false,
1696 SourceLocation(),
1697 SourceLocation(),
1698 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001699 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001700
Greg Clayton9d3d6882011-10-31 23:51:19 +00001701 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001703 else
1704 {
1705 if (decl_ctx == translation_unit_decl)
1706 {
1707 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1708 if (namespace_decl)
1709 return namespace_decl;
1710
Sean Callanan5b26f272012-02-04 08:49:35 +00001711 namespace_decl = NamespaceDecl::Create(*ast,
1712 decl_ctx,
1713 false,
1714 SourceLocation(),
1715 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001716 nullptr,
1717 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001718 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1719 translation_unit_decl->addDecl (namespace_decl);
1720 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1721 }
1722 else
1723 {
1724 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1725 if (parent_namespace_decl)
1726 {
1727 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1728 if (namespace_decl)
1729 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001730 namespace_decl = NamespaceDecl::Create(*ast,
1731 decl_ctx,
1732 false,
1733 SourceLocation(),
1734 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001735 nullptr,
1736 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001737 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1738 parent_namespace_decl->addDecl (namespace_decl);
1739 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1740 }
1741 else
1742 {
1743 // BAD!!!
1744 }
1745 }
1746
1747
1748 if (namespace_decl)
1749 {
1750 // If we make it here, we are creating the anonymous namespace decl
1751 // for the first time, so we need to do the using directive magic
1752 // like SEMA does
1753 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1754 decl_ctx,
1755 SourceLocation(),
1756 SourceLocation(),
1757 NestedNameSpecifierLoc(),
1758 SourceLocation(),
1759 namespace_decl,
1760 decl_ctx);
1761 using_directive_decl->setImplicit();
1762 decl_ctx->addDecl(using_directive_decl);
1763 }
1764 }
1765#ifdef LLDB_CONFIGURATION_DEBUG
1766 VerifyDecl(namespace_decl);
1767#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001768 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769}
1770
1771
1772#pragma mark Function Types
1773
1774FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001775ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1776 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001777 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001778 int storage,
1779 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780{
Ed Masted4612ad2014-04-20 13:17:36 +00001781 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001782 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001783 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001784 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785
Greg Clayton0d551042013-06-28 21:08:47 +00001786
1787 const bool hasWrittenPrototype = true;
1788 const bool isConstexprSpecified = false;
1789
Greg Clayton147e1fa2011-10-14 22:47:18 +00001790 if (name && name[0])
1791 {
1792 func_decl = FunctionDecl::Create (*ast,
1793 decl_ctx,
1794 SourceLocation(),
1795 SourceLocation(),
1796 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001797 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001798 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001799 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001800 is_inline,
1801 hasWrittenPrototype,
1802 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001804 else
1805 {
1806 func_decl = FunctionDecl::Create (*ast,
1807 decl_ctx,
1808 SourceLocation(),
1809 SourceLocation(),
1810 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00001811 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001812 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001813 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001814 is_inline,
1815 hasWrittenPrototype,
1816 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001817 }
1818 if (func_decl)
1819 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001820
1821#ifdef LLDB_CONFIGURATION_DEBUG
1822 VerifyDecl(func_decl);
1823#endif
1824
Greg Clayton147e1fa2011-10-14 22:47:18 +00001825 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826}
1827
Greg Claytona1e5dc82015-08-11 22:53:00 +00001828CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001829ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001830 const CompilerType& result_type,
1831 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001832 unsigned num_args,
1833 bool is_variadic,
1834 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835{
Ed Masted4612ad2014-04-20 13:17:36 +00001836 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837 std::vector<QualType> qual_type_args;
1838 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00001839 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840
1841 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001842 FunctionProtoType::ExtProtoInfo proto_info;
1843 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001844 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001845 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001846 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001847
Greg Claytona1e5dc82015-08-11 22:53:00 +00001848 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001849 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00001850 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851}
1852
1853ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00001854ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001856 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001857 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001858 return ParmVarDecl::Create(*ast,
1859 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001860 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001861 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001862 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00001863 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001864 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001865 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00001866 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867}
1868
1869void
1870ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
1871{
1872 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00001873 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001874}
1875
1876
1877#pragma mark Array Types
1878
Greg Claytona1e5dc82015-08-11 22:53:00 +00001879CompilerType
1880ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00001881 size_t element_count,
1882 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001883{
Greg Clayton57ee3062013-07-11 22:46:58 +00001884 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001886 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001887 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00001888
Greg Clayton1c8ef472013-04-05 23:27:21 +00001889 if (is_vector)
1890 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001891 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00001892 }
1893 else
1894 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00001895
1896 llvm::APInt ap_element_count (64, element_count);
1897 if (element_count == 0)
1898 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001899 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001900 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001901 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001902 }
1903 else
1904 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001905 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001906 ap_element_count,
1907 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001908 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001909 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00001910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001911 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001912 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913}
1914
Greg Claytona1e5dc82015-08-11 22:53:00 +00001915CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00001916ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001917 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00001918 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00001919{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001920 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00001921 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
1922 return type;
1923 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00001924 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001925 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00001926 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00001927 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00001928 SetIsPacked(type);
1929 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001930 return type;
1931}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001932
1933#pragma mark Enumeration Types
1934
Greg Claytona1e5dc82015-08-11 22:53:00 +00001935CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00001936ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00001937(
Greg Claytond8d4a572015-08-11 21:38:15 +00001938 const char *name,
1939 DeclContext *decl_ctx,
1940 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001941 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00001942 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943{
1944 // TODO: Do something intelligent with the Declaration object passed in
1945 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00001946 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00001947
Greg Claytone02b8502010-10-12 04:29:14 +00001948 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00001949 // const bool IsScoped = false;
1950 // const bool IsFixed = false;
1951
Greg Clayton6beaaa62011-01-17 03:46:26 +00001952 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00001953 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00001954 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00001955 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001956 name && name[0] ? &ast->Idents.get(name) : nullptr,
1957 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00001958 false, // IsScoped
1959 false, // IsScopedUsingClassTag
1960 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00001961
1962
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00001964 {
1965 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00001966 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00001967
1968 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
1969
Greg Claytona1e5dc82015-08-11 22:53:00 +00001970 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00001971 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001972 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001973}
1974
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975// Disable this for now since I can't seem to get a nicely formatted float
1976// out of the APFloat class without just getting the float, double or quad
1977// and then using a formatted print on it which defeats the purpose. We ideally
1978// would like to get perfect string values for any kind of float semantics
1979// so we can support remote targets. The code below also requires a patch to
1980// llvm::APInt.
1981//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00001982//ClangASTContext::ConvertFloatValueToString (ASTContext *ast, clang_type_t clang_type, const uint8_t* bytes, size_t byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983//{
1984// uint32_t count = 0;
1985// bool is_complex = false;
1986// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
1987// {
1988// unsigned num_bytes_per_float = byte_size / count;
1989// unsigned num_bits_per_float = num_bytes_per_float * 8;
1990//
1991// float_str.clear();
1992// uint32_t i;
1993// for (i=0; i<count; i++)
1994// {
1995// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
1996// bool is_ieee = false;
1997// APFloat ap_float(ap_int, is_ieee);
1998// char s[1024];
1999// unsigned int hex_digits = 0;
2000// bool upper_case = false;
2001//
2002// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2003// {
2004// if (i > 0)
2005// float_str.append(", ");
2006// float_str.append(s);
2007// if (i == 1 && is_complex)
2008// float_str.append(1, 'i');
2009// }
2010// }
2011// return !float_str.empty();
2012// }
2013// return false;
2014//}
2015
Greg Claytona1e5dc82015-08-11 22:53:00 +00002016CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002017ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2018 size_t bit_size, bool is_signed)
2019{
2020 if (ast)
2021 {
2022 if (is_signed)
2023 {
2024 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002025 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002026
2027 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002028 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002029
2030 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002031 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002032
2033 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002034 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002035
2036 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002037 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002038
2039 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002040 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002041 }
2042 else
2043 {
2044 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002045 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002046
2047 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002048 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002049
2050 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002051 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002052
2053 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002054 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002055
2056 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002057 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002058
2059 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002060 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002061 }
2062 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002063 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002064}
2065
Greg Claytona1e5dc82015-08-11 22:53:00 +00002066CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002067ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2068{
2069 if (ast)
2070 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002071 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002072}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073
Greg Claytona1e5dc82015-08-11 22:53:00 +00002074CompilerType
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002075ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2076 size_t bit_size)
2077{
2078 if (ast)
2079 {
2080 if (bit_size == ast->getTypeSize(ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002081 return CompilerType(ast, ast->FloatTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002082 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002083 return CompilerType(ast, ast->DoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002084 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002085 return CompilerType(ast, ast->LongDoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002086 else if (bit_size == ast->getTypeSize(ast->HalfTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002087 return CompilerType(ast, ast->HalfTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002088 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002089 return CompilerType();
Enrico Granata86027e92012-03-24 01:11:14 +00002090}
2091
2092bool
Greg Claytona2721472011-06-25 00:44:06 +00002093ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2094 clang::Decl *decl)
2095{
2096 if (!decl)
2097 return false;
2098
2099 ExternalASTSource *ast_source = ast->getExternalSource();
2100
2101 if (!ast_source)
2102 return false;
2103
2104 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2105 {
Greg Clayton219cf312012-03-30 00:51:13 +00002106 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002107 return true;
2108
2109 if (!tag_decl->hasExternalLexicalStorage())
2110 return false;
2111
2112 ast_source->CompleteType(tag_decl);
2113
2114 return !tag_decl->getTypeForDecl()->isIncompleteType();
2115 }
2116 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2117 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002118 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002119 return true;
2120
2121 if (!objc_interface_decl->hasExternalLexicalStorage())
2122 return false;
2123
2124 ast_source->CompleteType(objc_interface_decl);
2125
Sean Callanan5b26f272012-02-04 08:49:35 +00002126 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002127 }
2128 else
2129 {
2130 return false;
2131 }
2132}
2133
Sean Callanan60217122012-04-13 00:10:03 +00002134void
Greg Claytond0029442013-03-27 01:48:02 +00002135ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002136 user_id_t user_id)
2137{
2138 ClangASTMetadata meta_data;
2139 meta_data.SetUserID (user_id);
2140 SetMetadata (object, meta_data);
2141}
2142
2143void
Sean Callanan60217122012-04-13 00:10:03 +00002144ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002145 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002146 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002147{
2148 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002149 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002150
2151 if (external_source)
2152 external_source->SetMetadata(object, metadata);
2153}
2154
Jim Ingham379397632012-10-27 02:54:13 +00002155ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002156ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002157 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002158{
2159 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002160 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002161
2162 if (external_source && external_source->HasMetadata(object))
2163 return external_source->GetMetadata(object);
2164 else
Ed Masted4612ad2014-04-20 13:17:36 +00002165 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002166}
2167
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002168clang::DeclContext *
2169ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2170{
Sean Callanana87bee82011-08-19 06:19:25 +00002171 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002172}
2173
2174clang::DeclContext *
2175ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2176{
Sean Callanana87bee82011-08-19 06:19:25 +00002177 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002178}
2179
Greg Clayton685c88c2012-07-14 00:53:55 +00002180
2181bool
2182ClangASTContext::GetClassMethodInfoForDeclContext (clang::DeclContext *decl_ctx,
2183 lldb::LanguageType &language,
2184 bool &is_instance_method,
2185 ConstString &language_object_name)
2186{
2187 language_object_name.Clear();
2188 language = eLanguageTypeUnknown;
2189 is_instance_method = false;
2190
2191 if (decl_ctx)
2192 {
2193 if (clang::CXXMethodDecl *method_decl = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
2194 {
2195 if (method_decl->isStatic())
2196 {
2197 is_instance_method = false;
2198 }
2199 else
2200 {
2201 language_object_name.SetCString("this");
2202 is_instance_method = true;
2203 }
2204 language = eLanguageTypeC_plus_plus;
2205 return true;
2206 }
2207 else if (clang::ObjCMethodDecl *method_decl = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
2208 {
2209 // Both static and instance methods have a "self" object in objective C
2210 language_object_name.SetCString("self");
2211 if (method_decl->isInstanceMethod())
2212 {
2213 is_instance_method = true;
2214 }
2215 else
2216 {
2217 is_instance_method = false;
2218 }
2219 language = eLanguageTypeObjC;
2220 return true;
2221 }
Jim Ingham379397632012-10-27 02:54:13 +00002222 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
2223 {
Greg Claytond0029442013-03-27 01:48:02 +00002224 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00002225 if (metadata && metadata->HasObjectPtr())
2226 {
2227 language_object_name.SetCString (metadata->GetObjectPtrName());
2228 language = eLanguageTypeObjC;
2229 is_instance_method = true;
2230 }
2231 return true;
2232 }
Greg Clayton685c88c2012-07-14 00:53:55 +00002233 }
2234 return false;
2235}
2236
Greg Claytond8d4a572015-08-11 21:38:15 +00002237
2238bool
2239ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2240{
2241 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2242 if (clang_type)
2243 {
2244 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2245 if (tag_type)
2246 {
2247 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2248 if (tag_decl)
2249 {
2250 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2251 return true;
2252 }
2253 }
2254 }
2255 return false;
2256}
2257
2258
2259bool
2260ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2261 int default_accessibility,
2262 int *assigned_accessibilities,
2263 size_t num_assigned_accessibilities)
2264{
2265 if (record_decl)
2266 {
2267 uint32_t field_idx;
2268 clang::RecordDecl::field_iterator field, field_end;
2269 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2270 field != field_end;
2271 ++field, ++field_idx)
2272 {
2273 // If no accessibility was assigned, assign the correct one
2274 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2275 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2276 }
2277 return true;
2278 }
2279 return false;
2280}
2281
2282clang::DeclContext *
2283ClangASTContext::GetDeclContextForType (clang::QualType type)
2284{
2285 if (type.isNull())
2286 return nullptr;
2287
2288 clang::QualType qual_type = type.getCanonicalType();
2289 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2290 switch (type_class)
2291 {
2292 case clang::Type::UnaryTransform: break;
2293 case clang::Type::FunctionNoProto: break;
2294 case clang::Type::FunctionProto: break;
2295 case clang::Type::IncompleteArray: break;
2296 case clang::Type::VariableArray: break;
2297 case clang::Type::ConstantArray: break;
2298 case clang::Type::DependentSizedArray: break;
2299 case clang::Type::ExtVector: break;
2300 case clang::Type::DependentSizedExtVector: break;
2301 case clang::Type::Vector: break;
2302 case clang::Type::Builtin: break;
2303 case clang::Type::BlockPointer: break;
2304 case clang::Type::Pointer: break;
2305 case clang::Type::LValueReference: break;
2306 case clang::Type::RValueReference: break;
2307 case clang::Type::MemberPointer: break;
2308 case clang::Type::Complex: break;
2309 case clang::Type::ObjCObject: break;
2310 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2311 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2312 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2313 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2314 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2315 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2316 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
2317 case clang::Type::TypeOfExpr: break;
2318 case clang::Type::TypeOf: break;
2319 case clang::Type::Decltype: break;
2320 //case clang::Type::QualifiedName: break;
2321 case clang::Type::TemplateSpecialization: break;
2322 case clang::Type::DependentTemplateSpecialization: break;
2323 case clang::Type::TemplateTypeParm: break;
2324 case clang::Type::SubstTemplateTypeParm: break;
2325 case clang::Type::SubstTemplateTypeParmPack:break;
2326 case clang::Type::PackExpansion: break;
2327 case clang::Type::UnresolvedUsing: break;
2328 case clang::Type::Attributed: break;
2329 case clang::Type::Auto: break;
2330 case clang::Type::InjectedClassName: break;
2331 case clang::Type::DependentName: break;
2332 case clang::Type::Atomic: break;
2333 case clang::Type::Adjusted: break;
2334
2335 // pointer type decayed from an array or function type.
2336 case clang::Type::Decayed: break;
2337 }
2338 // No DeclContext in this type...
2339 return nullptr;
2340}
2341
2342static bool
2343GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2344{
2345 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2346 switch (type_class)
2347 {
2348 case clang::Type::ConstantArray:
2349 case clang::Type::IncompleteArray:
2350 case clang::Type::VariableArray:
2351 {
2352 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2353
2354 if (array_type)
2355 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2356 }
2357 break;
2358
2359 case clang::Type::Record:
2360 case clang::Type::Enum:
2361 {
2362 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2363 if (tag_type)
2364 {
2365 clang::TagDecl *tag_decl = tag_type->getDecl();
2366 if (tag_decl)
2367 {
2368 if (tag_decl->isCompleteDefinition())
2369 return true;
2370
2371 if (!allow_completion)
2372 return false;
2373
2374 if (tag_decl->hasExternalLexicalStorage())
2375 {
2376 if (ast)
2377 {
2378 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2379 if (external_ast_source)
2380 {
2381 external_ast_source->CompleteType(tag_decl);
2382 return !tag_type->isIncompleteType();
2383 }
2384 }
2385 }
2386 return false;
2387 }
2388 }
2389
2390 }
2391 break;
2392
2393 case clang::Type::ObjCObject:
2394 case clang::Type::ObjCInterface:
2395 {
2396 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2397 if (objc_class_type)
2398 {
2399 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2400 // We currently can't complete objective C types through the newly added ASTContext
2401 // because it only supports TagDecl objects right now...
2402 if (class_interface_decl)
2403 {
2404 if (class_interface_decl->getDefinition())
2405 return true;
2406
2407 if (!allow_completion)
2408 return false;
2409
2410 if (class_interface_decl->hasExternalLexicalStorage())
2411 {
2412 if (ast)
2413 {
2414 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2415 if (external_ast_source)
2416 {
2417 external_ast_source->CompleteType (class_interface_decl);
2418 return !objc_class_type->isIncompleteType();
2419 }
2420 }
2421 }
2422 return false;
2423 }
2424 }
2425 }
2426 break;
2427
2428 case clang::Type::Typedef:
2429 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2430
2431 case clang::Type::Elaborated:
2432 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2433
2434 case clang::Type::Paren:
2435 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2436
2437 default:
2438 break;
2439 }
2440
2441 return true;
2442}
2443
2444static clang::ObjCIvarDecl::AccessControl
2445ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2446{
2447 switch (access)
2448 {
2449 case eAccessNone: return clang::ObjCIvarDecl::None;
2450 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2451 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2452 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2453 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2454 }
2455 return clang::ObjCIvarDecl::None;
2456}
2457
2458
2459//----------------------------------------------------------------------
2460// Tests
2461//----------------------------------------------------------------------
2462
2463bool
2464ClangASTContext::IsAggregateType (void* type)
2465{
2466 clang::QualType qual_type (GetCanonicalQualType(type));
2467
2468 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2469 switch (type_class)
2470 {
2471 case clang::Type::IncompleteArray:
2472 case clang::Type::VariableArray:
2473 case clang::Type::ConstantArray:
2474 case clang::Type::ExtVector:
2475 case clang::Type::Vector:
2476 case clang::Type::Record:
2477 case clang::Type::ObjCObject:
2478 case clang::Type::ObjCInterface:
2479 return true;
2480 case clang::Type::Elaborated:
2481 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2482 case clang::Type::Typedef:
2483 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2484 case clang::Type::Paren:
2485 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2486 default:
2487 break;
2488 }
2489 // The clang type does have a value
2490 return false;
2491}
2492
2493bool
2494ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002495 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002496 uint64_t *size,
2497 bool *is_incomplete)
2498{
2499 clang::QualType qual_type (GetCanonicalQualType(type));
2500
2501 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2502 switch (type_class)
2503 {
2504 default:
2505 break;
2506
2507 case clang::Type::ConstantArray:
2508 if (element_type_ptr)
2509 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2510 if (size)
2511 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2512 return true;
2513
2514 case clang::Type::IncompleteArray:
2515 if (element_type_ptr)
2516 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2517 if (size)
2518 *size = 0;
2519 if (is_incomplete)
2520 *is_incomplete = true;
2521 return true;
2522
2523 case clang::Type::VariableArray:
2524 if (element_type_ptr)
2525 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2526 if (size)
2527 *size = 0;
2528 return true;
2529
2530 case clang::Type::DependentSizedArray:
2531 if (element_type_ptr)
2532 element_type_ptr->SetClangType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
2533 if (size)
2534 *size = 0;
2535 return true;
2536
2537 case clang::Type::Typedef:
2538 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2539 element_type_ptr,
2540 size,
2541 is_incomplete);
2542 case clang::Type::Elaborated:
2543 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2544 element_type_ptr,
2545 size,
2546 is_incomplete);
2547 case clang::Type::Paren:
2548 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2549 element_type_ptr,
2550 size,
2551 is_incomplete);
2552 }
2553 if (element_type_ptr)
2554 element_type_ptr->Clear();
2555 if (size)
2556 *size = 0;
2557 if (is_incomplete)
2558 *is_incomplete = false;
2559 return 0;
2560}
2561
2562bool
2563ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002564 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002565 uint64_t *size)
2566{
2567 clang::QualType qual_type (GetCanonicalQualType(type));
2568
2569 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2570 switch (type_class)
2571 {
2572 case clang::Type::Vector:
2573 {
2574 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2575 if (vector_type)
2576 {
2577 if (size)
2578 *size = vector_type->getNumElements();
2579 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002580 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002581 }
2582 return true;
2583 }
2584 break;
2585 case clang::Type::ExtVector:
2586 {
2587 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2588 if (ext_vector_type)
2589 {
2590 if (size)
2591 *size = ext_vector_type->getNumElements();
2592 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002593 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002594 }
2595 return true;
2596 }
2597 default:
2598 break;
2599 }
2600 return false;
2601}
2602
2603bool
2604ClangASTContext::IsRuntimeGeneratedType (void* type)
2605{
2606 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2607 if (!decl_ctx)
2608 return false;
2609
2610 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2611 return false;
2612
2613 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2614
2615 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2616 if (!ast_metadata)
2617 return false;
2618 return (ast_metadata->GetISAPtr() != 0);
2619}
2620
2621bool
2622ClangASTContext::IsCharType (void* type)
2623{
2624 return GetQualType(type).getUnqualifiedType()->isCharType();
2625}
2626
2627
2628bool
2629ClangASTContext::IsCompleteType (void* type)
2630{
2631 const bool allow_completion = false;
2632 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2633}
2634
2635bool
2636ClangASTContext::IsConst(void* type)
2637{
2638 return GetQualType(type).isConstQualified();
2639}
2640
2641bool
2642ClangASTContext::IsCStringType (void* type, uint32_t &length)
2643{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002644 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002645 length = 0;
2646 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2647
2648 if (!pointee_or_element_clang_type.IsValid())
2649 return false;
2650
2651 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2652 {
2653 if (pointee_or_element_clang_type.IsCharType())
2654 {
2655 if (type_flags.Test (eTypeIsArray))
2656 {
2657 // We know the size of the array and it could be a C string
2658 // since it is an array of characters
2659 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2660 }
2661 return true;
2662
2663 }
2664 }
2665 return false;
2666}
2667
2668bool
2669ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2670{
2671 if (type)
2672 {
2673 clang::QualType qual_type (GetCanonicalQualType(type));
2674
2675 if (qual_type->isFunctionType())
2676 {
2677 if (is_variadic_ptr)
2678 {
2679 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2680 if (function_proto_type)
2681 *is_variadic_ptr = function_proto_type->isVariadic();
2682 else
2683 *is_variadic_ptr = false;
2684 }
2685 return true;
2686 }
2687
2688 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2689 switch (type_class)
2690 {
2691 default:
2692 break;
2693 case clang::Type::Typedef:
2694 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2695 case clang::Type::Elaborated:
2696 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2697 case clang::Type::Paren:
2698 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2699 case clang::Type::LValueReference:
2700 case clang::Type::RValueReference:
2701 {
2702 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2703 if (reference_type)
2704 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2705 }
2706 break;
2707 }
2708 }
2709 return false;
2710}
2711
2712// Used to detect "Homogeneous Floating-point Aggregates"
2713uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002714ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002715{
2716 if (!type)
2717 return 0;
2718
2719 clang::QualType qual_type(GetCanonicalQualType(type));
2720 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2721 switch (type_class)
2722 {
2723 case clang::Type::Record:
2724 if (GetCompleteType (type))
2725 {
2726 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2727 if (cxx_record_decl)
2728 {
2729 if (cxx_record_decl->getNumBases() ||
2730 cxx_record_decl->isDynamicClass())
2731 return 0;
2732 }
2733 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2734 if (record_type)
2735 {
2736 const clang::RecordDecl *record_decl = record_type->getDecl();
2737 if (record_decl)
2738 {
2739 // We are looking for a structure that contains only floating point types
2740 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2741 uint32_t num_fields = 0;
2742 bool is_hva = false;
2743 bool is_hfa = false;
2744 clang::QualType base_qual_type;
2745 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2746 {
2747 clang::QualType field_qual_type = field_pos->getType();
2748 if (field_qual_type->isFloatingType())
2749 {
2750 if (field_qual_type->isComplexType())
2751 return 0;
2752 else
2753 {
2754 if (num_fields == 0)
2755 base_qual_type = field_qual_type;
2756 else
2757 {
2758 if (is_hva)
2759 return 0;
2760 is_hfa = true;
2761 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2762 return 0;
2763 }
2764 }
2765 }
2766 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2767 {
2768 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2769 if (array && array->getNumElements() <= 4)
2770 {
2771 if (num_fields == 0)
2772 base_qual_type = array->getElementType();
2773 else
2774 {
2775 if (is_hfa)
2776 return 0;
2777 is_hva = true;
2778 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2779 return 0;
2780 }
2781 }
2782 else
2783 return 0;
2784 }
2785 else
2786 return 0;
2787 ++num_fields;
2788 }
2789 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002790 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002791 return num_fields;
2792 }
2793 }
2794 }
2795 break;
2796
2797 case clang::Type::Typedef:
2798 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2799
2800 case clang::Type::Elaborated:
2801 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2802 default:
2803 break;
2804 }
2805 return 0;
2806}
2807
2808size_t
2809ClangASTContext::GetNumberOfFunctionArguments (void* type)
2810{
2811 if (type)
2812 {
2813 clang::QualType qual_type (GetCanonicalQualType(type));
2814 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2815 if (func)
2816 return func->getNumParams();
2817 }
2818 return 0;
2819}
2820
Greg Claytona1e5dc82015-08-11 22:53:00 +00002821CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002822ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2823{
2824 if (type)
2825 {
2826 clang::QualType qual_type (GetCanonicalQualType(type));
2827 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2828 if (func)
2829 {
2830 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002831 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002832 }
2833 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002834 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002835}
2836
2837bool
2838ClangASTContext::IsFunctionPointerType (void* type)
2839{
2840 if (type)
2841 {
2842 clang::QualType qual_type (GetCanonicalQualType(type));
2843
2844 if (qual_type->isFunctionPointerType())
2845 return true;
2846
2847 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2848 switch (type_class)
2849 {
2850 default:
2851 break;
2852 case clang::Type::Typedef:
2853 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2854 case clang::Type::Elaborated:
2855 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2856 case clang::Type::Paren:
2857 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2858
2859 case clang::Type::LValueReference:
2860 case clang::Type::RValueReference:
2861 {
2862 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2863 if (reference_type)
2864 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2865 }
2866 break;
2867 }
2868 }
2869 return false;
2870
2871}
2872
2873bool
2874ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2875{
2876 if (!type)
2877 return false;
2878
2879 clang::QualType qual_type (GetCanonicalQualType(type));
2880 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2881
2882 if (builtin_type)
2883 {
2884 if (builtin_type->isInteger())
2885 {
2886 is_signed = builtin_type->isSignedInteger();
2887 return true;
2888 }
2889 }
2890
2891 return false;
2892}
2893
2894bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002895ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002896{
2897 if (type)
2898 {
2899 clang::QualType qual_type (GetCanonicalQualType(type));
2900 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2901 switch (type_class)
2902 {
2903 case clang::Type::Builtin:
2904 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2905 {
2906 default:
2907 break;
2908 case clang::BuiltinType::ObjCId:
2909 case clang::BuiltinType::ObjCClass:
2910 return true;
2911 }
2912 return false;
2913 case clang::Type::ObjCObjectPointer:
2914 if (pointee_type)
2915 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
2916 return true;
2917 case clang::Type::BlockPointer:
2918 if (pointee_type)
2919 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
2920 return true;
2921 case clang::Type::Pointer:
2922 if (pointee_type)
2923 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
2924 return true;
2925 case clang::Type::MemberPointer:
2926 if (pointee_type)
2927 pointee_type->SetClangType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
2928 return true;
2929 case clang::Type::Typedef:
2930 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2931 case clang::Type::Elaborated:
2932 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2933 case clang::Type::Paren:
2934 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2935 default:
2936 break;
2937 }
2938 }
2939 if (pointee_type)
2940 pointee_type->Clear();
2941 return false;
2942}
2943
2944
2945bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002946ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002947{
2948 if (type)
2949 {
2950 clang::QualType qual_type (GetCanonicalQualType(type));
2951 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2952 switch (type_class)
2953 {
2954 case clang::Type::Builtin:
2955 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2956 {
2957 default:
2958 break;
2959 case clang::BuiltinType::ObjCId:
2960 case clang::BuiltinType::ObjCClass:
2961 return true;
2962 }
2963 return false;
2964 case clang::Type::ObjCObjectPointer:
2965 if (pointee_type)
2966 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
2967 return true;
2968 case clang::Type::BlockPointer:
2969 if (pointee_type)
2970 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
2971 return true;
2972 case clang::Type::Pointer:
2973 if (pointee_type)
2974 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
2975 return true;
2976 case clang::Type::MemberPointer:
2977 if (pointee_type)
2978 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
2979 return true;
2980 case clang::Type::LValueReference:
2981 if (pointee_type)
2982 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
2983 return true;
2984 case clang::Type::RValueReference:
2985 if (pointee_type)
2986 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
2987 return true;
2988 case clang::Type::Typedef:
2989 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2990 case clang::Type::Elaborated:
2991 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2992 case clang::Type::Paren:
2993 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2994 default:
2995 break;
2996 }
2997 }
2998 if (pointee_type)
2999 pointee_type->Clear();
3000 return false;
3001}
3002
3003
3004bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003005ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003006{
3007 if (type)
3008 {
3009 clang::QualType qual_type (GetCanonicalQualType(type));
3010 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3011
3012 switch (type_class)
3013 {
3014 case clang::Type::LValueReference:
3015 if (pointee_type)
3016 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3017 if (is_rvalue)
3018 *is_rvalue = false;
3019 return true;
3020 case clang::Type::RValueReference:
3021 if (pointee_type)
3022 pointee_type->SetClangType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3023 if (is_rvalue)
3024 *is_rvalue = true;
3025 return true;
3026 case clang::Type::Typedef:
3027 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3028 case clang::Type::Elaborated:
3029 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3030 case clang::Type::Paren:
3031 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3032
3033 default:
3034 break;
3035 }
3036 }
3037 if (pointee_type)
3038 pointee_type->Clear();
3039 return false;
3040}
3041
3042bool
3043ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
3044{
3045 if (type)
3046 {
3047 clang::QualType qual_type (GetCanonicalQualType(type));
3048
3049 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3050 {
3051 clang::BuiltinType::Kind kind = BT->getKind();
3052 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3053 {
3054 count = 1;
3055 is_complex = false;
3056 return true;
3057 }
3058 }
3059 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3060 {
3061 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3062 {
3063 count = 2;
3064 is_complex = true;
3065 return true;
3066 }
3067 }
3068 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3069 {
3070 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3071 {
3072 count = VT->getNumElements();
3073 is_complex = false;
3074 return true;
3075 }
3076 }
3077 }
3078 count = 0;
3079 is_complex = false;
3080 return false;
3081}
3082
3083
3084bool
3085ClangASTContext::IsDefined(void* type)
3086{
3087 if (!type)
3088 return false;
3089
3090 clang::QualType qual_type(GetQualType(type));
3091 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3092 if (tag_type)
3093 {
3094 clang::TagDecl *tag_decl = tag_type->getDecl();
3095 if (tag_decl)
3096 return tag_decl->isCompleteDefinition();
3097 return false;
3098 }
3099 else
3100 {
3101 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3102 if (objc_class_type)
3103 {
3104 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3105 if (class_interface_decl)
3106 return class_interface_decl->getDefinition() != nullptr;
3107 return false;
3108 }
3109 }
3110 return true;
3111}
3112
3113bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003114ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003115{
3116 if (type)
3117 {
3118 clang::QualType qual_type (GetCanonicalQualType(type));
3119
3120 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3121
3122 if (obj_pointer_type)
3123 return obj_pointer_type->isObjCClassType();
3124 }
3125 return false;
3126}
3127
3128bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003129ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003130{
3131 if (type)
3132 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3133 return false;
3134}
3135
3136bool
3137ClangASTContext::IsPolymorphicClass (void* type)
3138{
3139 if (type)
3140 {
3141 clang::QualType qual_type(GetCanonicalQualType(type));
3142 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3143 switch (type_class)
3144 {
3145 case clang::Type::Record:
3146 if (GetCompleteType(type))
3147 {
3148 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3149 const clang::RecordDecl *record_decl = record_type->getDecl();
3150 if (record_decl)
3151 {
3152 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3153 if (cxx_record_decl)
3154 return cxx_record_decl->isPolymorphic();
3155 }
3156 }
3157 break;
3158
3159 default:
3160 break;
3161 }
3162 }
3163 return false;
3164}
3165
3166bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003167ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003168 bool check_cplusplus,
3169 bool check_objc)
3170{
3171 clang::QualType pointee_qual_type;
3172 if (type)
3173 {
3174 clang::QualType qual_type (GetCanonicalQualType(type));
3175 bool success = false;
3176 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3177 switch (type_class)
3178 {
3179 case clang::Type::Builtin:
3180 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3181 {
3182 if (dynamic_pointee_type)
3183 dynamic_pointee_type->SetClangType(this, type);
3184 return true;
3185 }
3186 break;
3187
3188 case clang::Type::ObjCObjectPointer:
3189 if (check_objc)
3190 {
3191 if (dynamic_pointee_type)
3192 dynamic_pointee_type->SetClangType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
3193 return true;
3194 }
3195 break;
3196
3197 case clang::Type::Pointer:
3198 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3199 success = true;
3200 break;
3201
3202 case clang::Type::LValueReference:
3203 case clang::Type::RValueReference:
3204 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3205 success = true;
3206 break;
3207
3208 case clang::Type::Typedef:
3209 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3210 dynamic_pointee_type,
3211 check_cplusplus,
3212 check_objc);
3213
3214 case clang::Type::Elaborated:
3215 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3216 dynamic_pointee_type,
3217 check_cplusplus,
3218 check_objc);
3219
3220 case clang::Type::Paren:
3221 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3222 dynamic_pointee_type,
3223 check_cplusplus,
3224 check_objc);
3225 default:
3226 break;
3227 }
3228
3229 if (success)
3230 {
3231 // Check to make sure what we are pointing too is a possible dynamic C++ type
3232 // We currently accept any "void *" (in case we have a class that has been
3233 // watered down to an opaque pointer) and virtual C++ classes.
3234 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3235 switch (pointee_type_class)
3236 {
3237 case clang::Type::Builtin:
3238 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3239 {
3240 case clang::BuiltinType::UnknownAny:
3241 case clang::BuiltinType::Void:
3242 if (dynamic_pointee_type)
3243 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3244 return true;
3245
3246 case clang::BuiltinType::NullPtr:
3247 case clang::BuiltinType::Bool:
3248 case clang::BuiltinType::Char_U:
3249 case clang::BuiltinType::UChar:
3250 case clang::BuiltinType::WChar_U:
3251 case clang::BuiltinType::Char16:
3252 case clang::BuiltinType::Char32:
3253 case clang::BuiltinType::UShort:
3254 case clang::BuiltinType::UInt:
3255 case clang::BuiltinType::ULong:
3256 case clang::BuiltinType::ULongLong:
3257 case clang::BuiltinType::UInt128:
3258 case clang::BuiltinType::Char_S:
3259 case clang::BuiltinType::SChar:
3260 case clang::BuiltinType::WChar_S:
3261 case clang::BuiltinType::Short:
3262 case clang::BuiltinType::Int:
3263 case clang::BuiltinType::Long:
3264 case clang::BuiltinType::LongLong:
3265 case clang::BuiltinType::Int128:
3266 case clang::BuiltinType::Float:
3267 case clang::BuiltinType::Double:
3268 case clang::BuiltinType::LongDouble:
3269 case clang::BuiltinType::Dependent:
3270 case clang::BuiltinType::Overload:
3271 case clang::BuiltinType::ObjCId:
3272 case clang::BuiltinType::ObjCClass:
3273 case clang::BuiltinType::ObjCSel:
3274 case clang::BuiltinType::BoundMember:
3275 case clang::BuiltinType::Half:
3276 case clang::BuiltinType::ARCUnbridgedCast:
3277 case clang::BuiltinType::PseudoObject:
3278 case clang::BuiltinType::BuiltinFn:
3279 case clang::BuiltinType::OCLEvent:
3280 case clang::BuiltinType::OCLImage1d:
3281 case clang::BuiltinType::OCLImage1dArray:
3282 case clang::BuiltinType::OCLImage1dBuffer:
3283 case clang::BuiltinType::OCLImage2d:
3284 case clang::BuiltinType::OCLImage2dArray:
3285 case clang::BuiltinType::OCLImage3d:
3286 case clang::BuiltinType::OCLSampler:
3287 break;
3288 }
3289 break;
3290
3291 case clang::Type::Record:
3292 if (check_cplusplus)
3293 {
3294 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3295 if (cxx_record_decl)
3296 {
3297 bool is_complete = cxx_record_decl->isCompleteDefinition();
3298
3299 if (is_complete)
3300 success = cxx_record_decl->isDynamicClass();
3301 else
3302 {
3303 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3304 if (metadata)
3305 success = metadata->GetIsDynamicCXXType();
3306 else
3307 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003308 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003309 if (is_complete)
3310 success = cxx_record_decl->isDynamicClass();
3311 else
3312 success = false;
3313 }
3314 }
3315
3316 if (success)
3317 {
3318 if (dynamic_pointee_type)
3319 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3320 return true;
3321 }
3322 }
3323 }
3324 break;
3325
3326 case clang::Type::ObjCObject:
3327 case clang::Type::ObjCInterface:
3328 if (check_objc)
3329 {
3330 if (dynamic_pointee_type)
3331 dynamic_pointee_type->SetClangType(getASTContext(), pointee_qual_type);
3332 return true;
3333 }
3334 break;
3335
3336 default:
3337 break;
3338 }
3339 }
3340 }
3341 if (dynamic_pointee_type)
3342 dynamic_pointee_type->Clear();
3343 return false;
3344}
3345
3346
3347bool
3348ClangASTContext::IsScalarType (void* type)
3349{
3350 if (!type)
3351 return false;
3352
3353 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3354}
3355
3356bool
3357ClangASTContext::IsTypedefType (void* type)
3358{
3359 if (!type)
3360 return false;
3361 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3362}
3363
3364bool
3365ClangASTContext::IsVoidType (void* type)
3366{
3367 if (!type)
3368 return false;
3369 return GetCanonicalQualType(type)->isVoidType();
3370}
3371
3372bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003373ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003374{
3375 if (type)
3376 {
3377 clang::QualType qual_type (GetCanonicalQualType(type));
3378
3379 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3380 if (cxx_record_decl)
3381 {
3382 class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
3383 return true;
3384 }
3385 }
3386 class_name.clear();
3387 return false;
3388}
3389
3390
3391bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003392ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003393{
3394 if (!type)
3395 return false;
3396
3397 clang::QualType qual_type (GetCanonicalQualType(type));
3398 if (qual_type->getAsCXXRecordDecl() != nullptr)
3399 return true;
3400 return false;
3401}
3402
3403bool
3404ClangASTContext::IsBeingDefined (void* type)
3405{
3406 if (!type)
3407 return false;
3408 clang::QualType qual_type (GetCanonicalQualType(type));
3409 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3410 if (tag_type)
3411 return tag_type->isBeingDefined();
3412 return false;
3413}
3414
3415bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003416ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003417{
3418 if (!type)
3419 return false;
3420
3421 clang::QualType qual_type (GetCanonicalQualType(type));
3422
3423 if (qual_type->isObjCObjectPointerType())
3424 {
3425 if (class_type_ptr)
3426 {
3427 if (!qual_type->isObjCClassType() &&
3428 !qual_type->isObjCIdType())
3429 {
3430 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3431 if (obj_pointer_type == nullptr)
3432 class_type_ptr->Clear();
3433 else
3434 class_type_ptr->SetClangType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
3435 }
3436 }
3437 return true;
3438 }
3439 if (class_type_ptr)
3440 class_type_ptr->Clear();
3441 return false;
3442}
3443
3444bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003445ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003446{
3447 if (!type)
3448 return false;
3449
3450 clang::QualType qual_type (GetCanonicalQualType(type));
3451
3452 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3453 if (object_type)
3454 {
3455 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3456 if (interface)
3457 {
3458 class_name = interface->getNameAsString();
3459 return true;
3460 }
3461 }
3462 return false;
3463}
3464
3465
3466//----------------------------------------------------------------------
3467// Type Completion
3468//----------------------------------------------------------------------
3469
3470bool
3471ClangASTContext::GetCompleteType (void* type)
3472{
3473 if (!type)
3474 return false;
3475 const bool allow_completion = true;
3476 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3477}
3478
3479ConstString
3480ClangASTContext::GetTypeName (void* type)
3481{
3482 std::string type_name;
3483 if (type)
3484 {
3485 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3486 clang::QualType qual_type(GetQualType(type));
3487 printing_policy.SuppressTagKeyword = true;
3488 printing_policy.LangOpts.WChar = true;
3489 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3490 if (typedef_type)
3491 {
3492 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3493 type_name = typedef_decl->getQualifiedNameAsString();
3494 }
3495 else
3496 {
3497 type_name = qual_type.getAsString(printing_policy);
3498 }
3499 }
3500 return ConstString(type_name);
3501}
3502
3503uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003504ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003505{
3506 if (!type)
3507 return 0;
3508
3509 if (pointee_or_element_clang_type)
3510 pointee_or_element_clang_type->Clear();
3511
3512 clang::QualType qual_type (GetQualType(type));
3513
3514 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3515 switch (type_class)
3516 {
3517 case clang::Type::Builtin:
3518 {
3519 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3520
3521 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3522 switch (builtin_type->getKind())
3523 {
3524 case clang::BuiltinType::ObjCId:
3525 case clang::BuiltinType::ObjCClass:
3526 if (pointee_or_element_clang_type)
3527 pointee_or_element_clang_type->SetClangType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3528 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3529 break;
3530
3531 case clang::BuiltinType::ObjCSel:
3532 if (pointee_or_element_clang_type)
3533 pointee_or_element_clang_type->SetClangType(getASTContext(), getASTContext()->CharTy);
3534 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3535 break;
3536
3537 case clang::BuiltinType::Bool:
3538 case clang::BuiltinType::Char_U:
3539 case clang::BuiltinType::UChar:
3540 case clang::BuiltinType::WChar_U:
3541 case clang::BuiltinType::Char16:
3542 case clang::BuiltinType::Char32:
3543 case clang::BuiltinType::UShort:
3544 case clang::BuiltinType::UInt:
3545 case clang::BuiltinType::ULong:
3546 case clang::BuiltinType::ULongLong:
3547 case clang::BuiltinType::UInt128:
3548 case clang::BuiltinType::Char_S:
3549 case clang::BuiltinType::SChar:
3550 case clang::BuiltinType::WChar_S:
3551 case clang::BuiltinType::Short:
3552 case clang::BuiltinType::Int:
3553 case clang::BuiltinType::Long:
3554 case clang::BuiltinType::LongLong:
3555 case clang::BuiltinType::Int128:
3556 case clang::BuiltinType::Float:
3557 case clang::BuiltinType::Double:
3558 case clang::BuiltinType::LongDouble:
3559 builtin_type_flags |= eTypeIsScalar;
3560 if (builtin_type->isInteger())
3561 {
3562 builtin_type_flags |= eTypeIsInteger;
3563 if (builtin_type->isSignedInteger())
3564 builtin_type_flags |= eTypeIsSigned;
3565 }
3566 else if (builtin_type->isFloatingPoint())
3567 builtin_type_flags |= eTypeIsFloat;
3568 break;
3569 default:
3570 break;
3571 }
3572 return builtin_type_flags;
3573 }
3574
3575 case clang::Type::BlockPointer:
3576 if (pointee_or_element_clang_type)
3577 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3578 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3579
3580 case clang::Type::Complex:
3581 {
3582 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3583 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3584 if (complex_type)
3585 {
3586 clang::QualType complex_element_type (complex_type->getElementType());
3587 if (complex_element_type->isIntegerType())
3588 complex_type_flags |= eTypeIsFloat;
3589 else if (complex_element_type->isFloatingType())
3590 complex_type_flags |= eTypeIsInteger;
3591 }
3592 return complex_type_flags;
3593 }
3594 break;
3595
3596 case clang::Type::ConstantArray:
3597 case clang::Type::DependentSizedArray:
3598 case clang::Type::IncompleteArray:
3599 case clang::Type::VariableArray:
3600 if (pointee_or_element_clang_type)
3601 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
3602 return eTypeHasChildren | eTypeIsArray;
3603
3604 case clang::Type::DependentName: return 0;
3605 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3606 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3607 case clang::Type::Decltype: return 0;
3608
3609 case clang::Type::Enum:
3610 if (pointee_or_element_clang_type)
3611 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
3612 return eTypeIsEnumeration | eTypeHasValue;
3613
3614 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003615 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003616 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003617 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003618
3619 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3620 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3621 case clang::Type::InjectedClassName: return 0;
3622
3623 case clang::Type::LValueReference:
3624 case clang::Type::RValueReference:
3625 if (pointee_or_element_clang_type)
3626 pointee_or_element_clang_type->SetClangType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
3627 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3628
3629 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3630
3631 case clang::Type::ObjCObjectPointer:
3632 if (pointee_or_element_clang_type)
3633 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3634 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3635
3636 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3637 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3638
3639 case clang::Type::Pointer:
3640 if (pointee_or_element_clang_type)
3641 pointee_or_element_clang_type->SetClangType(getASTContext(), qual_type->getPointeeType());
3642 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3643
3644 case clang::Type::Record:
3645 if (qual_type->getAsCXXRecordDecl())
3646 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3647 else
3648 return eTypeHasChildren | eTypeIsStructUnion;
3649 break;
3650 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3651 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3652 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3653
3654 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003655 return eTypeIsTypedef | CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003656 case clang::Type::TypeOfExpr: return 0;
3657 case clang::Type::TypeOf: return 0;
3658 case clang::Type::UnresolvedUsing: return 0;
3659
3660 case clang::Type::ExtVector:
3661 case clang::Type::Vector:
3662 {
3663 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3664 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3665 if (vector_type)
3666 {
3667 if (vector_type->isIntegerType())
3668 vector_type_flags |= eTypeIsFloat;
3669 else if (vector_type->isFloatingType())
3670 vector_type_flags |= eTypeIsInteger;
3671 }
3672 return vector_type_flags;
3673 }
3674 default: return 0;
3675 }
3676 return 0;
3677}
3678
3679
3680
3681lldb::LanguageType
3682ClangASTContext::GetMinimumLanguage (void* type)
3683{
3684 if (!type)
3685 return lldb::eLanguageTypeC;
3686
3687 // If the type is a reference, then resolve it to what it refers to first:
3688 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3689 if (qual_type->isAnyPointerType())
3690 {
3691 if (qual_type->isObjCObjectPointerType())
3692 return lldb::eLanguageTypeObjC;
3693
3694 clang::QualType pointee_type (qual_type->getPointeeType());
3695 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3696 return lldb::eLanguageTypeC_plus_plus;
3697 if (pointee_type->isObjCObjectOrInterfaceType())
3698 return lldb::eLanguageTypeObjC;
3699 if (pointee_type->isObjCClassType())
3700 return lldb::eLanguageTypeObjC;
3701 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3702 return lldb::eLanguageTypeObjC;
3703 }
3704 else
3705 {
3706 if (qual_type->isObjCObjectOrInterfaceType())
3707 return lldb::eLanguageTypeObjC;
3708 if (qual_type->getAsCXXRecordDecl())
3709 return lldb::eLanguageTypeC_plus_plus;
3710 switch (qual_type->getTypeClass())
3711 {
3712 default:
3713 break;
3714 case clang::Type::Builtin:
3715 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3716 {
3717 default:
3718 case clang::BuiltinType::Void:
3719 case clang::BuiltinType::Bool:
3720 case clang::BuiltinType::Char_U:
3721 case clang::BuiltinType::UChar:
3722 case clang::BuiltinType::WChar_U:
3723 case clang::BuiltinType::Char16:
3724 case clang::BuiltinType::Char32:
3725 case clang::BuiltinType::UShort:
3726 case clang::BuiltinType::UInt:
3727 case clang::BuiltinType::ULong:
3728 case clang::BuiltinType::ULongLong:
3729 case clang::BuiltinType::UInt128:
3730 case clang::BuiltinType::Char_S:
3731 case clang::BuiltinType::SChar:
3732 case clang::BuiltinType::WChar_S:
3733 case clang::BuiltinType::Short:
3734 case clang::BuiltinType::Int:
3735 case clang::BuiltinType::Long:
3736 case clang::BuiltinType::LongLong:
3737 case clang::BuiltinType::Int128:
3738 case clang::BuiltinType::Float:
3739 case clang::BuiltinType::Double:
3740 case clang::BuiltinType::LongDouble:
3741 break;
3742
3743 case clang::BuiltinType::NullPtr:
3744 return eLanguageTypeC_plus_plus;
3745
3746 case clang::BuiltinType::ObjCId:
3747 case clang::BuiltinType::ObjCClass:
3748 case clang::BuiltinType::ObjCSel:
3749 return eLanguageTypeObjC;
3750
3751 case clang::BuiltinType::Dependent:
3752 case clang::BuiltinType::Overload:
3753 case clang::BuiltinType::BoundMember:
3754 case clang::BuiltinType::UnknownAny:
3755 break;
3756 }
3757 break;
3758 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003759 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003760 }
3761 }
3762 return lldb::eLanguageTypeC;
3763}
3764
3765lldb::TypeClass
3766ClangASTContext::GetTypeClass (void* type)
3767{
3768 if (!type)
3769 return lldb::eTypeClassInvalid;
3770
3771 clang::QualType qual_type(GetQualType(type));
3772
3773 switch (qual_type->getTypeClass())
3774 {
3775 case clang::Type::UnaryTransform: break;
3776 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3777 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3778 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3779 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3780 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3781 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3782 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3783 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3784 case clang::Type::Vector: return lldb::eTypeClassVector;
3785 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3786 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3787 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3788 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3789 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3790 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3791 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3792 case clang::Type::Complex:
3793 if (qual_type->isComplexType())
3794 return lldb::eTypeClassComplexFloat;
3795 else
3796 return lldb::eTypeClassComplexInteger;
3797 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3798 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3799 case clang::Type::Record:
3800 {
3801 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3802 const clang::RecordDecl *record_decl = record_type->getDecl();
3803 if (record_decl->isUnion())
3804 return lldb::eTypeClassUnion;
3805 else if (record_decl->isStruct())
3806 return lldb::eTypeClassStruct;
3807 else
3808 return lldb::eTypeClassClass;
3809 }
3810 break;
3811 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3812 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3813 case clang::Type::UnresolvedUsing: break;
3814 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003815 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003816 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003817 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003818
3819 case clang::Type::Attributed: break;
3820 case clang::Type::TemplateTypeParm: break;
3821 case clang::Type::SubstTemplateTypeParm: break;
3822 case clang::Type::SubstTemplateTypeParmPack:break;
3823 case clang::Type::Auto: break;
3824 case clang::Type::InjectedClassName: break;
3825 case clang::Type::DependentName: break;
3826 case clang::Type::DependentTemplateSpecialization: break;
3827 case clang::Type::PackExpansion: break;
3828
3829 case clang::Type::TypeOfExpr: break;
3830 case clang::Type::TypeOf: break;
3831 case clang::Type::Decltype: break;
3832 case clang::Type::TemplateSpecialization: break;
3833 case clang::Type::Atomic: break;
3834
3835 // pointer type decayed from an array or function type.
3836 case clang::Type::Decayed: break;
3837 case clang::Type::Adjusted: break;
3838 }
3839 // We don't know hot to display this type...
3840 return lldb::eTypeClassOther;
3841
3842}
3843
3844unsigned
3845ClangASTContext::GetTypeQualifiers(void* type)
3846{
3847 if (type)
3848 return GetQualType(type).getQualifiers().getCVRQualifiers();
3849 return 0;
3850}
3851
3852//----------------------------------------------------------------------
3853// Creating related types
3854//----------------------------------------------------------------------
3855
Greg Claytona1e5dc82015-08-11 22:53:00 +00003856CompilerType
3857ClangASTContext::AddConstModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003858{
3859 if (type && type.GetTypeSystem()->AsClangASTContext())
3860 {
3861 clang::QualType result(GetQualType(type));
3862 result.addConst();
Greg Claytona1e5dc82015-08-11 22:53:00 +00003863 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003864 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003865 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003866}
3867
Greg Claytona1e5dc82015-08-11 22:53:00 +00003868CompilerType
3869ClangASTContext::AddRestrictModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003870{
3871 if (type && type.GetTypeSystem()->AsClangASTContext())
3872 {
3873 clang::QualType result(GetQualType(type));
3874 result.getQualifiers().setRestrict (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003875 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003876 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003877 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003878}
3879
Greg Claytona1e5dc82015-08-11 22:53:00 +00003880CompilerType
3881ClangASTContext::AddVolatileModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003882{
3883 if (type && type.GetTypeSystem()->AsClangASTContext())
3884 {
3885 clang::QualType result(GetQualType(type));
3886 result.getQualifiers().setVolatile (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003887 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003888 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003889 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003890}
3891
Greg Claytona1e5dc82015-08-11 22:53:00 +00003892CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003893ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3894{
3895 if (type)
3896 {
3897 clang::QualType qual_type(GetCanonicalQualType(type));
3898
3899 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3900
3901 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003902 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003903
Greg Claytona1e5dc82015-08-11 22:53:00 +00003904 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003905
3906 // TODO: the real stride will be >= this value.. find the real one!
3907 if (stride)
3908 *stride = element_type.GetByteSize(nullptr);
3909
3910 return element_type;
3911
3912 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003913 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003914}
3915
Greg Claytona1e5dc82015-08-11 22:53:00 +00003916CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003917ClangASTContext::GetCanonicalType (void* type)
3918{
3919 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003920 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3921 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003922}
3923
3924static clang::QualType
3925GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3926{
3927 if (qual_type->isPointerType())
3928 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3929 else
3930 qual_type = qual_type.getUnqualifiedType();
3931 qual_type.removeLocalConst();
3932 qual_type.removeLocalRestrict();
3933 qual_type.removeLocalVolatile();
3934 return qual_type;
3935}
3936
Greg Claytona1e5dc82015-08-11 22:53:00 +00003937CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003938ClangASTContext::GetFullyUnqualifiedType (void* type)
3939{
3940 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003941 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3942 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003943}
3944
3945
3946int
3947ClangASTContext::GetFunctionArgumentCount (void* type)
3948{
3949 if (type)
3950 {
3951 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3952 if (func)
3953 return func->getNumParams();
3954 }
3955 return -1;
3956}
3957
Greg Claytona1e5dc82015-08-11 22:53:00 +00003958CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003959ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3960{
3961 if (type)
3962 {
3963 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3964 if (func)
3965 {
3966 const uint32_t num_args = func->getNumParams();
3967 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003968 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003969 }
3970 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003971 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003972}
3973
Greg Claytona1e5dc82015-08-11 22:53:00 +00003974CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003975ClangASTContext::GetFunctionReturnType (void* type)
3976{
3977 if (type)
3978 {
3979 clang::QualType qual_type(GetCanonicalQualType(type));
3980 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3981 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003982 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003983 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003984 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003985}
3986
3987size_t
3988ClangASTContext::GetNumMemberFunctions (void* type)
3989{
3990 size_t num_functions = 0;
3991 if (type)
3992 {
3993 clang::QualType qual_type(GetCanonicalQualType(type));
3994 switch (qual_type->getTypeClass()) {
3995 case clang::Type::Record:
3996 if (GetCompleteQualType (getASTContext(), qual_type))
3997 {
3998 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3999 const clang::RecordDecl *record_decl = record_type->getDecl();
4000 assert(record_decl);
4001 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4002 if (cxx_record_decl)
4003 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4004 }
4005 break;
4006
4007 case clang::Type::ObjCObjectPointer:
4008 if (GetCompleteType(type))
4009 {
4010 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4011 if (objc_class_type)
4012 {
4013 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4014 if (class_interface_decl)
4015 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4016 }
4017 }
4018 break;
4019
4020 case clang::Type::ObjCObject:
4021 case clang::Type::ObjCInterface:
4022 if (GetCompleteType(type))
4023 {
4024 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4025 if (objc_class_type)
4026 {
4027 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4028 if (class_interface_decl)
4029 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4030 }
4031 }
4032 break;
4033
4034
4035 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004036 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004037
4038 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004039 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004040
4041 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004042 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004043
4044 default:
4045 break;
4046 }
4047 }
4048 return num_functions;
4049}
4050
4051TypeMemberFunctionImpl
4052ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
4053{
4054 std::string name("");
4055 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004056 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00004057 clang::ObjCMethodDecl *method_decl(nullptr);
4058 if (type)
4059 {
4060 clang::QualType qual_type(GetCanonicalQualType(type));
4061 switch (qual_type->getTypeClass()) {
4062 case clang::Type::Record:
4063 if (GetCompleteQualType (getASTContext(), qual_type))
4064 {
4065 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4066 const clang::RecordDecl *record_decl = record_type->getDecl();
4067 assert(record_decl);
4068 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4069 if (cxx_record_decl)
4070 {
4071 auto method_iter = cxx_record_decl->method_begin();
4072 auto method_end = cxx_record_decl->method_end();
4073 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4074 {
4075 std::advance(method_iter, idx);
4076 auto method_decl = method_iter->getCanonicalDecl();
4077 if (method_decl)
4078 {
4079 if (!method_decl->getName().empty())
4080 name.assign(method_decl->getName().data());
4081 else
4082 name.clear();
4083 if (method_decl->isStatic())
4084 kind = lldb::eMemberFunctionKindStaticMethod;
4085 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4086 kind = lldb::eMemberFunctionKindConstructor;
4087 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4088 kind = lldb::eMemberFunctionKindDestructor;
4089 else
4090 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004091 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004092 }
4093 }
4094 }
4095 }
4096 break;
4097
4098 case clang::Type::ObjCObjectPointer:
4099 if (GetCompleteType(type))
4100 {
4101 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4102 if (objc_class_type)
4103 {
4104 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4105 if (class_interface_decl)
4106 {
4107 auto method_iter = class_interface_decl->meth_begin();
4108 auto method_end = class_interface_decl->meth_end();
4109 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4110 {
4111 std::advance(method_iter, idx);
4112 method_decl = method_iter->getCanonicalDecl();
4113 if (method_decl)
4114 {
4115 name = method_decl->getSelector().getAsString();
4116 if (method_decl->isClassMethod())
4117 kind = lldb::eMemberFunctionKindStaticMethod;
4118 else
4119 kind = lldb::eMemberFunctionKindInstanceMethod;
4120 }
4121 }
4122 }
4123 }
4124 }
4125 break;
4126
4127 case clang::Type::ObjCObject:
4128 case clang::Type::ObjCInterface:
4129 if (GetCompleteType(type))
4130 {
4131 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4132 if (objc_class_type)
4133 {
4134 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4135 if (class_interface_decl)
4136 {
4137 auto method_iter = class_interface_decl->meth_begin();
4138 auto method_end = class_interface_decl->meth_end();
4139 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4140 {
4141 std::advance(method_iter, idx);
4142 method_decl = method_iter->getCanonicalDecl();
4143 if (method_decl)
4144 {
4145 name = method_decl->getSelector().getAsString();
4146 if (method_decl->isClassMethod())
4147 kind = lldb::eMemberFunctionKindStaticMethod;
4148 else
4149 kind = lldb::eMemberFunctionKindInstanceMethod;
4150 }
4151 }
4152 }
4153 }
4154 }
4155 break;
4156
4157 case clang::Type::Typedef:
4158 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4159
4160 case clang::Type::Elaborated:
4161 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4162
4163 case clang::Type::Paren:
4164 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4165
4166 default:
4167 break;
4168 }
4169 }
4170
4171 if (kind == eMemberFunctionKindUnknown)
4172 return TypeMemberFunctionImpl();
4173 if (method_decl)
4174 return TypeMemberFunctionImpl(method_decl, name, kind);
4175 if (type)
4176 return TypeMemberFunctionImpl(clang_type, name, kind);
4177
4178 return TypeMemberFunctionImpl();
4179}
4180
Greg Claytona1e5dc82015-08-11 22:53:00 +00004181CompilerType
4182ClangASTContext::GetLValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004183{
4184 if (type)
4185 {
4186 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4187 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004188 return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004189 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004190 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004191}
4192
Greg Claytona1e5dc82015-08-11 22:53:00 +00004193CompilerType
4194ClangASTContext::GetRValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004195{
4196 if (type)
4197 {
4198 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4199 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004200 return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004201 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004202 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004203}
4204
Greg Claytona1e5dc82015-08-11 22:53:00 +00004205CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004206ClangASTContext::GetNonReferenceType (void* type)
4207{
4208 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004209 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4210 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004211}
4212
Greg Claytona1e5dc82015-08-11 22:53:00 +00004213CompilerType
4214ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004215 const char *typedef_name,
4216 clang::DeclContext *decl_ctx)
4217{
4218 if (type && typedef_name && typedef_name[0])
4219 {
4220 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
4221 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004222 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004223 clang::ASTContext* clang_ast = ast->getASTContext();
4224 clang::QualType qual_type (GetQualType(type));
4225 if (decl_ctx == nullptr)
4226 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4227 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4228 decl_ctx,
4229 clang::SourceLocation(),
4230 clang::SourceLocation(),
4231 &clang_ast->Idents.get(typedef_name),
4232 clang_ast->getTrivialTypeSourceInfo(qual_type));
4233
4234 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4235
4236 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004237 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004238 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004239 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004240
4241}
4242
Greg Claytona1e5dc82015-08-11 22:53:00 +00004243CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004244ClangASTContext::GetPointeeType (void* type)
4245{
4246 if (type)
4247 {
4248 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004249 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004250 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004251 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004252}
4253
Greg Claytona1e5dc82015-08-11 22:53:00 +00004254CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004255ClangASTContext::GetPointerType (void* type)
4256{
4257 if (type)
4258 {
4259 clang::QualType qual_type (GetQualType(type));
4260
4261 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4262 switch (type_class)
4263 {
4264 case clang::Type::ObjCObject:
4265 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004266 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004267
4268 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004269 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004270 }
4271 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004272 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004273}
4274
Greg Claytona1e5dc82015-08-11 22:53:00 +00004275CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004276ClangASTContext::GetTypedefedType (void* type)
4277{
4278 if (type)
4279 {
4280 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4281 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004282 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004283 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004284 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004285}
4286
Greg Claytona1e5dc82015-08-11 22:53:00 +00004287CompilerType
4288ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004289{
4290 if (type && type.GetTypeSystem()->AsClangASTContext())
4291 {
4292 clang::QualType qual_type(GetQualType(type));
4293 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004294 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004295 }
4296 return type;
4297}
4298
4299
4300//----------------------------------------------------------------------
4301// Create related types using the current type's AST
4302//----------------------------------------------------------------------
4303
Greg Claytona1e5dc82015-08-11 22:53:00 +00004304CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004305ClangASTContext::GetBasicTypeFromAST (void* type, lldb::BasicType basic_type)
4306{
4307 if (type)
4308 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004309 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004310}
4311//----------------------------------------------------------------------
4312// Exploring the type
4313//----------------------------------------------------------------------
4314
4315uint64_t
4316ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4317{
4318 if (GetCompleteType (type))
4319 {
4320 clang::QualType qual_type(GetCanonicalQualType(type));
4321 switch (qual_type->getTypeClass())
4322 {
4323 case clang::Type::ObjCInterface:
4324 case clang::Type::ObjCObject:
4325 {
4326 ExecutionContext exe_ctx (exe_scope);
4327 Process *process = exe_ctx.GetProcessPtr();
4328 if (process)
4329 {
4330 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4331 if (objc_runtime)
4332 {
4333 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004334 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004335 return bit_size;
4336 }
4337 }
4338 else
4339 {
4340 static bool g_printed = false;
4341 if (!g_printed)
4342 {
4343 StreamString s;
4344 DumpTypeDescription(&s);
4345
4346 llvm::outs() << "warning: trying to determine the size of type ";
4347 llvm::outs() << s.GetString() << "\n";
4348 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4349 llvm::outs() << "backtrace:\n";
4350 llvm::sys::PrintStackTrace(llvm::outs());
4351 llvm::outs() << "\n";
4352 g_printed = true;
4353 }
4354 }
4355 }
4356 // fallthrough
4357 default:
4358 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4359 if (bit_size == 0)
4360 {
4361 if (qual_type->isIncompleteArrayType())
4362 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4363 }
4364 if (qual_type->isObjCObjectOrInterfaceType())
4365 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4366 return bit_size;
4367 }
4368 }
4369 return 0;
4370}
4371
4372size_t
4373ClangASTContext::GetTypeBitAlign (void* type)
4374{
4375 if (GetCompleteType(type))
4376 return getASTContext()->getTypeAlign(GetQualType(type));
4377 return 0;
4378}
4379
4380
4381lldb::Encoding
4382ClangASTContext::GetEncoding (void* type, uint64_t &count)
4383{
4384 if (!type)
4385 return lldb::eEncodingInvalid;
4386
4387 count = 1;
4388 clang::QualType qual_type(GetCanonicalQualType(type));
4389
4390 switch (qual_type->getTypeClass())
4391 {
4392 case clang::Type::UnaryTransform:
4393 break;
4394
4395 case clang::Type::FunctionNoProto:
4396 case clang::Type::FunctionProto:
4397 break;
4398
4399 case clang::Type::IncompleteArray:
4400 case clang::Type::VariableArray:
4401 break;
4402
4403 case clang::Type::ConstantArray:
4404 break;
4405
4406 case clang::Type::ExtVector:
4407 case clang::Type::Vector:
4408 // TODO: Set this to more than one???
4409 break;
4410
4411 case clang::Type::Builtin:
4412 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4413 {
4414 default: assert(0 && "Unknown builtin type!");
4415 case clang::BuiltinType::Void:
4416 break;
4417
4418 case clang::BuiltinType::Bool:
4419 case clang::BuiltinType::Char_S:
4420 case clang::BuiltinType::SChar:
4421 case clang::BuiltinType::WChar_S:
4422 case clang::BuiltinType::Char16:
4423 case clang::BuiltinType::Char32:
4424 case clang::BuiltinType::Short:
4425 case clang::BuiltinType::Int:
4426 case clang::BuiltinType::Long:
4427 case clang::BuiltinType::LongLong:
4428 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4429
4430 case clang::BuiltinType::Char_U:
4431 case clang::BuiltinType::UChar:
4432 case clang::BuiltinType::WChar_U:
4433 case clang::BuiltinType::UShort:
4434 case clang::BuiltinType::UInt:
4435 case clang::BuiltinType::ULong:
4436 case clang::BuiltinType::ULongLong:
4437 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4438
4439 case clang::BuiltinType::Float:
4440 case clang::BuiltinType::Double:
4441 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4442
4443 case clang::BuiltinType::ObjCClass:
4444 case clang::BuiltinType::ObjCId:
4445 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4446
4447 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4448
4449 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4450 case clang::BuiltinType::Kind::BoundMember:
4451 case clang::BuiltinType::Kind::BuiltinFn:
4452 case clang::BuiltinType::Kind::Dependent:
4453 case clang::BuiltinType::Kind::Half:
4454 case clang::BuiltinType::Kind::OCLEvent:
4455 case clang::BuiltinType::Kind::OCLImage1d:
4456 case clang::BuiltinType::Kind::OCLImage1dArray:
4457 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4458 case clang::BuiltinType::Kind::OCLImage2d:
4459 case clang::BuiltinType::Kind::OCLImage2dArray:
4460 case clang::BuiltinType::Kind::OCLImage3d:
4461 case clang::BuiltinType::Kind::OCLSampler:
4462 case clang::BuiltinType::Kind::Overload:
4463 case clang::BuiltinType::Kind::PseudoObject:
4464 case clang::BuiltinType::Kind::UnknownAny:
4465 break;
4466 }
4467 break;
4468 // All pointer types are represented as unsigned integer encodings.
4469 // We may nee to add a eEncodingPointer if we ever need to know the
4470 // difference
4471 case clang::Type::ObjCObjectPointer:
4472 case clang::Type::BlockPointer:
4473 case clang::Type::Pointer:
4474 case clang::Type::LValueReference:
4475 case clang::Type::RValueReference:
4476 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4477 case clang::Type::Complex:
4478 {
4479 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4480 if (qual_type->isComplexType())
4481 encoding = lldb::eEncodingIEEE754;
4482 else
4483 {
4484 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4485 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004486 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004487 else
4488 encoding = lldb::eEncodingSint;
4489 }
4490 count = 2;
4491 return encoding;
4492 }
4493
4494 case clang::Type::ObjCInterface: break;
4495 case clang::Type::Record: break;
4496 case clang::Type::Enum: return lldb::eEncodingSint;
4497 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004498 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004499
4500 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004501 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004502
4503 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004504 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004505
4506 case clang::Type::DependentSizedArray:
4507 case clang::Type::DependentSizedExtVector:
4508 case clang::Type::UnresolvedUsing:
4509 case clang::Type::Attributed:
4510 case clang::Type::TemplateTypeParm:
4511 case clang::Type::SubstTemplateTypeParm:
4512 case clang::Type::SubstTemplateTypeParmPack:
4513 case clang::Type::Auto:
4514 case clang::Type::InjectedClassName:
4515 case clang::Type::DependentName:
4516 case clang::Type::DependentTemplateSpecialization:
4517 case clang::Type::PackExpansion:
4518 case clang::Type::ObjCObject:
4519
4520 case clang::Type::TypeOfExpr:
4521 case clang::Type::TypeOf:
4522 case clang::Type::Decltype:
4523 case clang::Type::TemplateSpecialization:
4524 case clang::Type::Atomic:
4525 case clang::Type::Adjusted:
4526 break;
4527
4528 // pointer type decayed from an array or function type.
4529 case clang::Type::Decayed:
4530 break;
4531 }
4532 count = 0;
4533 return lldb::eEncodingInvalid;
4534}
4535
4536lldb::Format
4537ClangASTContext::GetFormat (void* type)
4538{
4539 if (!type)
4540 return lldb::eFormatDefault;
4541
4542 clang::QualType qual_type(GetCanonicalQualType(type));
4543
4544 switch (qual_type->getTypeClass())
4545 {
4546 case clang::Type::UnaryTransform:
4547 break;
4548
4549 case clang::Type::FunctionNoProto:
4550 case clang::Type::FunctionProto:
4551 break;
4552
4553 case clang::Type::IncompleteArray:
4554 case clang::Type::VariableArray:
4555 break;
4556
4557 case clang::Type::ConstantArray:
4558 return lldb::eFormatVoid; // no value
4559
4560 case clang::Type::ExtVector:
4561 case clang::Type::Vector:
4562 break;
4563
4564 case clang::Type::Builtin:
4565 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4566 {
4567 //default: assert(0 && "Unknown builtin type!");
4568 case clang::BuiltinType::UnknownAny:
4569 case clang::BuiltinType::Void:
4570 case clang::BuiltinType::BoundMember:
4571 break;
4572
4573 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4574 case clang::BuiltinType::Char_S:
4575 case clang::BuiltinType::SChar:
4576 case clang::BuiltinType::WChar_S:
4577 case clang::BuiltinType::Char_U:
4578 case clang::BuiltinType::UChar:
4579 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4580 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4581 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4582 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4583 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4584 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4585 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4586 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4587 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4588 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4589 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4590 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4591 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4592 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4593 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4594 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
4595 case clang::BuiltinType::NullPtr:
4596 case clang::BuiltinType::Overload:
4597 case clang::BuiltinType::Dependent:
4598 case clang::BuiltinType::ObjCId:
4599 case clang::BuiltinType::ObjCClass:
4600 case clang::BuiltinType::ObjCSel:
4601 case clang::BuiltinType::Half:
4602 case clang::BuiltinType::ARCUnbridgedCast:
4603 case clang::BuiltinType::PseudoObject:
4604 case clang::BuiltinType::BuiltinFn:
4605 case clang::BuiltinType::OCLEvent:
4606 case clang::BuiltinType::OCLImage1d:
4607 case clang::BuiltinType::OCLImage1dArray:
4608 case clang::BuiltinType::OCLImage1dBuffer:
4609 case clang::BuiltinType::OCLImage2d:
4610 case clang::BuiltinType::OCLImage2dArray:
4611 case clang::BuiltinType::OCLImage3d:
4612 case clang::BuiltinType::OCLSampler:
4613 return lldb::eFormatHex;
4614 }
4615 break;
4616 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4617 case clang::Type::BlockPointer: return lldb::eFormatHex;
4618 case clang::Type::Pointer: return lldb::eFormatHex;
4619 case clang::Type::LValueReference:
4620 case clang::Type::RValueReference: return lldb::eFormatHex;
4621 case clang::Type::MemberPointer: break;
4622 case clang::Type::Complex:
4623 {
4624 if (qual_type->isComplexType())
4625 return lldb::eFormatComplex;
4626 else
4627 return lldb::eFormatComplexInteger;
4628 }
4629 case clang::Type::ObjCInterface: break;
4630 case clang::Type::Record: break;
4631 case clang::Type::Enum: return lldb::eFormatEnum;
4632 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004633 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004634 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004635 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004636 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004637 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004638 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004639 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004640 case clang::Type::DependentSizedArray:
4641 case clang::Type::DependentSizedExtVector:
4642 case clang::Type::UnresolvedUsing:
4643 case clang::Type::Attributed:
4644 case clang::Type::TemplateTypeParm:
4645 case clang::Type::SubstTemplateTypeParm:
4646 case clang::Type::SubstTemplateTypeParmPack:
4647 case clang::Type::InjectedClassName:
4648 case clang::Type::DependentName:
4649 case clang::Type::DependentTemplateSpecialization:
4650 case clang::Type::PackExpansion:
4651 case clang::Type::ObjCObject:
4652
4653 case clang::Type::TypeOfExpr:
4654 case clang::Type::TypeOf:
4655 case clang::Type::Decltype:
4656 case clang::Type::TemplateSpecialization:
4657 case clang::Type::Atomic:
4658 case clang::Type::Adjusted:
4659 break;
4660
4661 // pointer type decayed from an array or function type.
4662 case clang::Type::Decayed:
4663 break;
4664 }
4665 // We don't know hot to display this type...
4666 return lldb::eFormatBytes;
4667}
4668
4669static bool
4670ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4671{
4672 while (class_interface_decl)
4673 {
4674 if (class_interface_decl->ivar_size() > 0)
4675 return true;
4676
4677 if (check_superclass)
4678 class_interface_decl = class_interface_decl->getSuperClass();
4679 else
4680 break;
4681 }
4682 return false;
4683}
4684
4685uint32_t
4686ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4687{
4688 if (!type)
4689 return 0;
4690
4691 uint32_t num_children = 0;
4692 clang::QualType qual_type(GetQualType(type));
4693 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4694 switch (type_class)
4695 {
4696 case clang::Type::Builtin:
4697 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4698 {
4699 case clang::BuiltinType::ObjCId: // child is Class
4700 case clang::BuiltinType::ObjCClass: // child is Class
4701 num_children = 1;
4702 break;
4703
4704 default:
4705 break;
4706 }
4707 break;
4708
4709 case clang::Type::Complex: return 0;
4710
4711 case clang::Type::Record:
4712 if (GetCompleteQualType (getASTContext(), qual_type))
4713 {
4714 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4715 const clang::RecordDecl *record_decl = record_type->getDecl();
4716 assert(record_decl);
4717 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4718 if (cxx_record_decl)
4719 {
4720 if (omit_empty_base_classes)
4721 {
4722 // Check each base classes to see if it or any of its
4723 // base classes contain any fields. This can help
4724 // limit the noise in variable views by not having to
4725 // show base classes that contain no members.
4726 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4727 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4728 base_class != base_class_end;
4729 ++base_class)
4730 {
4731 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4732
4733 // Skip empty base classes
4734 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4735 continue;
4736
4737 num_children++;
4738 }
4739 }
4740 else
4741 {
4742 // Include all base classes
4743 num_children += cxx_record_decl->getNumBases();
4744 }
4745
4746 }
4747 clang::RecordDecl::field_iterator field, field_end;
4748 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4749 ++num_children;
4750 }
4751 break;
4752
4753 case clang::Type::ObjCObject:
4754 case clang::Type::ObjCInterface:
4755 if (GetCompleteQualType (getASTContext(), qual_type))
4756 {
4757 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4758 assert (objc_class_type);
4759 if (objc_class_type)
4760 {
4761 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4762
4763 if (class_interface_decl)
4764 {
4765
4766 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4767 if (superclass_interface_decl)
4768 {
4769 if (omit_empty_base_classes)
4770 {
4771 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4772 ++num_children;
4773 }
4774 else
4775 ++num_children;
4776 }
4777
4778 num_children += class_interface_decl->ivar_size();
4779 }
4780 }
4781 }
4782 break;
4783
4784 case clang::Type::ObjCObjectPointer:
4785 {
4786 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4787 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004788 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004789 // If this type points to a simple type, then it has 1 child
4790 if (num_pointee_children == 0)
4791 num_children = 1;
4792 else
4793 num_children = num_pointee_children;
4794 }
4795 break;
4796
4797 case clang::Type::Vector:
4798 case clang::Type::ExtVector:
4799 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4800 break;
4801
4802 case clang::Type::ConstantArray:
4803 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4804 break;
4805
4806 case clang::Type::Pointer:
4807 {
4808 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4809 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004810 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004811 if (num_pointee_children == 0)
4812 {
4813 // We have a pointer to a pointee type that claims it has no children.
4814 // We will want to look at
4815 num_children = GetNumPointeeChildren (pointee_type);
4816 }
4817 else
4818 num_children = num_pointee_children;
4819 }
4820 break;
4821
4822 case clang::Type::LValueReference:
4823 case clang::Type::RValueReference:
4824 {
4825 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4826 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004827 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004828 // If this type points to a simple type, then it has 1 child
4829 if (num_pointee_children == 0)
4830 num_children = 1;
4831 else
4832 num_children = num_pointee_children;
4833 }
4834 break;
4835
4836
4837 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004838 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004839 break;
4840
4841 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004842 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004843 break;
4844
4845 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004846 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004847 break;
4848 default:
4849 break;
4850 }
4851 return num_children;
4852}
4853
4854lldb::BasicType
4855ClangASTContext::GetBasicTypeEnumeration (void* type)
4856{
4857 if (type)
4858 {
4859 clang::QualType qual_type(GetQualType(type));
4860 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4861 if (type_class == clang::Type::Builtin)
4862 {
4863 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4864 {
4865 case clang::BuiltinType::Void: return eBasicTypeVoid;
4866 case clang::BuiltinType::Bool: return eBasicTypeBool;
4867 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4868 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4869 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4870 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4871 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4872 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4873 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4874 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4875 case clang::BuiltinType::Short: return eBasicTypeShort;
4876 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4877 case clang::BuiltinType::Int: return eBasicTypeInt;
4878 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4879 case clang::BuiltinType::Long: return eBasicTypeLong;
4880 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4881 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4882 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4883 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4884 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4885
4886 case clang::BuiltinType::Half: return eBasicTypeHalf;
4887 case clang::BuiltinType::Float: return eBasicTypeFloat;
4888 case clang::BuiltinType::Double: return eBasicTypeDouble;
4889 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4890
4891 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4892 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4893 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4894 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
4895 case clang::BuiltinType::Dependent:
4896 case clang::BuiltinType::Overload:
4897 case clang::BuiltinType::BoundMember:
4898 case clang::BuiltinType::PseudoObject:
4899 case clang::BuiltinType::UnknownAny:
4900 case clang::BuiltinType::BuiltinFn:
4901 case clang::BuiltinType::ARCUnbridgedCast:
4902 case clang::BuiltinType::OCLEvent:
4903 case clang::BuiltinType::OCLImage1d:
4904 case clang::BuiltinType::OCLImage1dArray:
4905 case clang::BuiltinType::OCLImage1dBuffer:
4906 case clang::BuiltinType::OCLImage2d:
4907 case clang::BuiltinType::OCLImage2dArray:
4908 case clang::BuiltinType::OCLImage3d:
4909 case clang::BuiltinType::OCLSampler:
4910 return eBasicTypeOther;
4911 }
4912 }
4913 }
4914 return eBasicTypeInvalid;
4915}
4916
4917
4918#pragma mark Aggregate Types
4919
4920uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00004921ClangASTContext::GetNumDirectBaseClasses (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004922{
4923 if (!type)
4924 return 0;
4925 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
4926 if (!ast)
4927 return 0;
4928
4929 uint32_t count = 0;
4930 clang::QualType qual_type(GetCanonicalQualType(type));
4931 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4932 switch (type_class)
4933 {
4934 case clang::Type::Record:
4935 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4936 {
4937 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
4938 if (cxx_record_decl)
4939 count = cxx_record_decl->getNumBases();
4940 }
4941 break;
4942
4943 case clang::Type::ObjCObjectPointer:
4944 count = GetNumDirectBaseClasses(ast->GetPointeeType(type.GetOpaqueQualType()));
4945 break;
4946
4947 case clang::Type::ObjCObject:
4948 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4949 {
4950 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
4951 if (objc_class_type)
4952 {
4953 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4954
4955 if (class_interface_decl && class_interface_decl->getSuperClass())
4956 count = 1;
4957 }
4958 }
4959 break;
4960 case clang::Type::ObjCInterface:
4961 if (ast->GetCompleteType(type.GetOpaqueQualType()))
4962 {
4963 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
4964 if (objc_interface_type)
4965 {
4966 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
4967
4968 if (class_interface_decl && class_interface_decl->getSuperClass())
4969 count = 1;
4970 }
4971 }
4972 break;
4973
4974
4975 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004976 count = GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004977 break;
4978
4979 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004980 count = GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004981 break;
4982
4983 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004984 return GetNumDirectBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()));
Greg Claytond8d4a572015-08-11 21:38:15 +00004985
4986 default:
4987 break;
4988 }
4989 return count;
4990}
4991
4992uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00004993ClangASTContext::GetNumVirtualBaseClasses (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004994{
4995 if (!type)
4996 return 0;
4997 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
4998 if (!ast)
4999 return 0;
5000
5001 uint32_t count = 0;
5002 clang::QualType qual_type(GetCanonicalQualType(type));
5003 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5004 switch (type_class)
5005 {
5006 case clang::Type::Record:
5007 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5008 {
5009 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5010 if (cxx_record_decl)
5011 count = cxx_record_decl->getNumVBases();
5012 }
5013 break;
5014
5015 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005016 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005017 break;
5018
5019 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005020 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005021 break;
5022
5023 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005024 count = GetNumVirtualBaseClasses(CompilerType (ast->getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()));
Greg Claytond8d4a572015-08-11 21:38:15 +00005025 break;
5026
5027 default:
5028 break;
5029 }
5030 return count;
5031}
5032
5033uint32_t
5034ClangASTContext::GetNumFields (void* type)
5035{
5036 if (!type)
5037 return 0;
5038
5039 uint32_t count = 0;
5040 clang::QualType qual_type(GetCanonicalQualType(type));
5041 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5042 switch (type_class)
5043 {
5044 case clang::Type::Record:
5045 if (GetCompleteType(type))
5046 {
5047 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5048 if (record_type)
5049 {
5050 clang::RecordDecl *record_decl = record_type->getDecl();
5051 if (record_decl)
5052 {
5053 uint32_t field_idx = 0;
5054 clang::RecordDecl::field_iterator field, field_end;
5055 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5056 ++field_idx;
5057 count = field_idx;
5058 }
5059 }
5060 }
5061 break;
5062
5063 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005064 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005065 break;
5066
5067 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005068 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005069 break;
5070
5071 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005072 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005073 break;
5074
5075 case clang::Type::ObjCObjectPointer:
5076 if (GetCompleteType(type))
5077 {
5078 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5079 if (objc_class_type)
5080 {
5081 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5082
5083 if (class_interface_decl)
5084 count = class_interface_decl->ivar_size();
5085 }
5086 }
5087 break;
5088
5089 case clang::Type::ObjCObject:
5090 case clang::Type::ObjCInterface:
5091 if (GetCompleteType(type))
5092 {
5093 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5094 if (objc_class_type)
5095 {
5096 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5097
5098 if (class_interface_decl)
5099 count = class_interface_decl->ivar_size();
5100 }
5101 }
5102 break;
5103
5104 default:
5105 break;
5106 }
5107 return count;
5108}
5109
Greg Claytona1e5dc82015-08-11 22:53:00 +00005110CompilerType
5111ClangASTContext::GetDirectBaseClassAtIndex (const CompilerType& type, size_t idx, uint32_t *bit_offset_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00005112{
5113 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005114 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005115 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
5116 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005117 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005118
5119 clang::QualType qual_type(GetCanonicalQualType(type));
5120 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5121 switch (type_class)
5122 {
5123 case clang::Type::Record:
5124 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5125 {
5126 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5127 if (cxx_record_decl)
5128 {
5129 uint32_t curr_idx = 0;
5130 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5131 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5132 base_class != base_class_end;
5133 ++base_class, ++curr_idx)
5134 {
5135 if (curr_idx == idx)
5136 {
5137 if (bit_offset_ptr)
5138 {
5139 const clang::ASTRecordLayout &record_layout = ast->getASTContext()->getASTRecordLayout(cxx_record_decl);
5140 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5141 if (base_class->isVirtual())
5142 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5143 else
5144 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5145 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005146 return CompilerType (ast, base_class->getType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00005147 }
5148 }
5149 }
5150 }
5151 break;
5152
5153 case clang::Type::ObjCObjectPointer:
5154 return GetDirectBaseClassAtIndex(ast->GetPointeeType(type.GetOpaqueQualType()), idx, bit_offset_ptr);
5155
5156 case clang::Type::ObjCObject:
5157 if (idx == 0 && ast->GetCompleteType(type.GetOpaqueQualType()))
5158 {
5159 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5160 if (objc_class_type)
5161 {
5162 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5163
5164 if (class_interface_decl)
5165 {
5166 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5167 if (superclass_interface_decl)
5168 {
5169 if (bit_offset_ptr)
5170 *bit_offset_ptr = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005171 return CompilerType (ast->getASTContext(), ast->getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005172 }
5173 }
5174 }
5175 }
5176 break;
5177 case clang::Type::ObjCInterface:
5178 if (idx == 0 && ast->GetCompleteType(type.GetOpaqueQualType()))
5179 {
5180 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5181 if (objc_interface_type)
5182 {
5183 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5184
5185 if (class_interface_decl)
5186 {
5187 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5188 if (superclass_interface_decl)
5189 {
5190 if (bit_offset_ptr)
5191 *bit_offset_ptr = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005192 return CompilerType (ast->getASTContext(), ast->getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005193 }
5194 }
5195 }
5196 }
5197 break;
5198
5199
5200 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005201 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005202
5203 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005204 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005205
5206 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005207 return GetDirectBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005208
5209 default:
5210 break;
5211 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005212 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005213}
5214
Greg Claytona1e5dc82015-08-11 22:53:00 +00005215CompilerType
5216ClangASTContext::GetVirtualBaseClassAtIndex (const CompilerType& type, size_t idx, uint32_t *bit_offset_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00005217{
5218 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005219 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005220 ClangASTContext *ast = type.GetTypeSystem()->AsClangASTContext();
5221 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005222 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005223
5224 clang::QualType qual_type(GetCanonicalQualType(type));
5225 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5226 switch (type_class)
5227 {
5228 case clang::Type::Record:
5229 if (ast->GetCompleteType(type.GetOpaqueQualType()))
5230 {
5231 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5232 if (cxx_record_decl)
5233 {
5234 uint32_t curr_idx = 0;
5235 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5236 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5237 base_class != base_class_end;
5238 ++base_class, ++curr_idx)
5239 {
5240 if (curr_idx == idx)
5241 {
5242 if (bit_offset_ptr)
5243 {
5244 const clang::ASTRecordLayout &record_layout = ast->getASTContext()->getASTRecordLayout(cxx_record_decl);
5245 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5246 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5247
5248 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005249 return CompilerType (ast, base_class->getType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00005250 }
5251 }
5252 }
5253 }
5254 break;
5255
5256 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005257 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005258
5259 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005260 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005261
5262 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005263 return GetVirtualBaseClassAtIndex (CompilerType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), idx, bit_offset_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00005264
5265 default:
5266 break;
5267 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005268 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005269}
5270
5271static clang_type_t
5272GetObjCFieldAtIndex (clang::ASTContext *ast,
5273 clang::ObjCInterfaceDecl *class_interface_decl,
5274 size_t idx,
5275 std::string& name,
5276 uint64_t *bit_offset_ptr,
5277 uint32_t *bitfield_bit_size_ptr,
5278 bool *is_bitfield_ptr)
5279{
5280 if (class_interface_decl)
5281 {
5282 if (idx < (class_interface_decl->ivar_size()))
5283 {
5284 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5285 uint32_t ivar_idx = 0;
5286
5287 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5288 {
5289 if (ivar_idx == idx)
5290 {
5291 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5292
5293 clang::QualType ivar_qual_type(ivar_decl->getType());
5294
5295 name.assign(ivar_decl->getNameAsString());
5296
5297 if (bit_offset_ptr)
5298 {
5299 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5300 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5301 }
5302
5303 const bool is_bitfield = ivar_pos->isBitField();
5304
5305 if (bitfield_bit_size_ptr)
5306 {
5307 *bitfield_bit_size_ptr = 0;
5308
5309 if (is_bitfield && ast)
5310 {
5311 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5312 llvm::APSInt bitfield_apsint;
5313 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5314 {
5315 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5316 }
5317 }
5318 }
5319 if (is_bitfield_ptr)
5320 *is_bitfield_ptr = is_bitfield;
5321
5322 return ivar_qual_type.getAsOpaquePtr();
5323 }
5324 }
5325 }
5326 }
5327 return nullptr;
5328}
5329
Greg Claytona1e5dc82015-08-11 22:53:00 +00005330CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005331ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
5332 std::string& name,
5333 uint64_t *bit_offset_ptr,
5334 uint32_t *bitfield_bit_size_ptr,
5335 bool *is_bitfield_ptr)
5336{
5337 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005338 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005339
5340 clang::QualType qual_type(GetCanonicalQualType(type));
5341 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5342 switch (type_class)
5343 {
5344 case clang::Type::Record:
5345 if (GetCompleteType(type))
5346 {
5347 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5348 const clang::RecordDecl *record_decl = record_type->getDecl();
5349 uint32_t field_idx = 0;
5350 clang::RecordDecl::field_iterator field, field_end;
5351 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5352 {
5353 if (idx == field_idx)
5354 {
5355 // Print the member type if requested
5356 // Print the member name and equal sign
5357 name.assign(field->getNameAsString());
5358
5359 // Figure out the type byte size (field_type_info.first) and
5360 // alignment (field_type_info.second) from the AST context.
5361 if (bit_offset_ptr)
5362 {
5363 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5364 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5365 }
5366
5367 const bool is_bitfield = field->isBitField();
5368
5369 if (bitfield_bit_size_ptr)
5370 {
5371 *bitfield_bit_size_ptr = 0;
5372
5373 if (is_bitfield)
5374 {
5375 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5376 llvm::APSInt bitfield_apsint;
5377 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5378 {
5379 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5380 }
5381 }
5382 }
5383 if (is_bitfield_ptr)
5384 *is_bitfield_ptr = is_bitfield;
5385
Greg Claytona1e5dc82015-08-11 22:53:00 +00005386 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005387 }
5388 }
5389 }
5390 break;
5391
5392 case clang::Type::ObjCObjectPointer:
5393 if (GetCompleteType(type))
5394 {
5395 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5396 if (objc_class_type)
5397 {
5398 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005399 return CompilerType (this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00005400 }
5401 }
5402 break;
5403
5404 case clang::Type::ObjCObject:
5405 case clang::Type::ObjCInterface:
5406 if (GetCompleteType(type))
5407 {
5408 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5409 assert (objc_class_type);
5410 if (objc_class_type)
5411 {
5412 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005413 return CompilerType (this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00005414 }
5415 }
5416 break;
5417
5418
5419 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005420 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005421 GetFieldAtIndex (idx,
5422 name,
5423 bit_offset_ptr,
5424 bitfield_bit_size_ptr,
5425 is_bitfield_ptr);
5426
5427 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005428 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005429 GetFieldAtIndex (idx,
5430 name,
5431 bit_offset_ptr,
5432 bitfield_bit_size_ptr,
5433 is_bitfield_ptr);
5434
5435 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005436 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005437 GetFieldAtIndex (idx,
5438 name,
5439 bit_offset_ptr,
5440 bitfield_bit_size_ptr,
5441 is_bitfield_ptr);
5442
5443 default:
5444 break;
5445 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005446 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005447}
5448
5449// If a pointer to a pointee type (the clang_type arg) says that it has no
5450// children, then we either need to trust it, or override it and return a
5451// different result. For example, an "int *" has one child that is an integer,
5452// but a function pointer doesn't have any children. Likewise if a Record type
5453// claims it has no children, then there really is nothing to show.
5454uint32_t
5455ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5456{
5457 if (type.isNull())
5458 return 0;
5459
5460 clang::QualType qual_type(type.getCanonicalType());
5461 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5462 switch (type_class)
5463 {
5464 case clang::Type::Builtin:
5465 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5466 {
5467 case clang::BuiltinType::UnknownAny:
5468 case clang::BuiltinType::Void:
5469 case clang::BuiltinType::NullPtr:
5470 case clang::BuiltinType::OCLEvent:
5471 case clang::BuiltinType::OCLImage1d:
5472 case clang::BuiltinType::OCLImage1dArray:
5473 case clang::BuiltinType::OCLImage1dBuffer:
5474 case clang::BuiltinType::OCLImage2d:
5475 case clang::BuiltinType::OCLImage2dArray:
5476 case clang::BuiltinType::OCLImage3d:
5477 case clang::BuiltinType::OCLSampler:
5478 return 0;
5479 case clang::BuiltinType::Bool:
5480 case clang::BuiltinType::Char_U:
5481 case clang::BuiltinType::UChar:
5482 case clang::BuiltinType::WChar_U:
5483 case clang::BuiltinType::Char16:
5484 case clang::BuiltinType::Char32:
5485 case clang::BuiltinType::UShort:
5486 case clang::BuiltinType::UInt:
5487 case clang::BuiltinType::ULong:
5488 case clang::BuiltinType::ULongLong:
5489 case clang::BuiltinType::UInt128:
5490 case clang::BuiltinType::Char_S:
5491 case clang::BuiltinType::SChar:
5492 case clang::BuiltinType::WChar_S:
5493 case clang::BuiltinType::Short:
5494 case clang::BuiltinType::Int:
5495 case clang::BuiltinType::Long:
5496 case clang::BuiltinType::LongLong:
5497 case clang::BuiltinType::Int128:
5498 case clang::BuiltinType::Float:
5499 case clang::BuiltinType::Double:
5500 case clang::BuiltinType::LongDouble:
5501 case clang::BuiltinType::Dependent:
5502 case clang::BuiltinType::Overload:
5503 case clang::BuiltinType::ObjCId:
5504 case clang::BuiltinType::ObjCClass:
5505 case clang::BuiltinType::ObjCSel:
5506 case clang::BuiltinType::BoundMember:
5507 case clang::BuiltinType::Half:
5508 case clang::BuiltinType::ARCUnbridgedCast:
5509 case clang::BuiltinType::PseudoObject:
5510 case clang::BuiltinType::BuiltinFn:
5511 return 1;
5512 }
5513 break;
5514
5515 case clang::Type::Complex: return 1;
5516 case clang::Type::Pointer: return 1;
5517 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5518 case clang::Type::LValueReference: return 1;
5519 case clang::Type::RValueReference: return 1;
5520 case clang::Type::MemberPointer: return 0;
5521 case clang::Type::ConstantArray: return 0;
5522 case clang::Type::IncompleteArray: return 0;
5523 case clang::Type::VariableArray: return 0;
5524 case clang::Type::DependentSizedArray: return 0;
5525 case clang::Type::DependentSizedExtVector: return 0;
5526 case clang::Type::Vector: return 0;
5527 case clang::Type::ExtVector: return 0;
5528 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5529 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5530 case clang::Type::UnresolvedUsing: return 0;
5531 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5532 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5533 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5534 case clang::Type::TypeOfExpr: return 0;
5535 case clang::Type::TypeOf: return 0;
5536 case clang::Type::Decltype: return 0;
5537 case clang::Type::Record: return 0;
5538 case clang::Type::Enum: return 1;
5539 case clang::Type::TemplateTypeParm: return 1;
5540 case clang::Type::SubstTemplateTypeParm: return 1;
5541 case clang::Type::TemplateSpecialization: return 1;
5542 case clang::Type::InjectedClassName: return 0;
5543 case clang::Type::DependentName: return 1;
5544 case clang::Type::DependentTemplateSpecialization: return 1;
5545 case clang::Type::ObjCObject: return 0;
5546 case clang::Type::ObjCInterface: return 0;
5547 case clang::Type::ObjCObjectPointer: return 1;
5548 default:
5549 break;
5550 }
5551 return 0;
5552}
5553
5554
Greg Claytona1e5dc82015-08-11 22:53:00 +00005555CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005556ClangASTContext::GetChildClangTypeAtIndex (void* type, ExecutionContext *exe_ctx,
5557 size_t idx,
5558 bool transparent_pointers,
5559 bool omit_empty_base_classes,
5560 bool ignore_array_bounds,
5561 std::string& child_name,
5562 uint32_t &child_byte_size,
5563 int32_t &child_byte_offset,
5564 uint32_t &child_bitfield_bit_size,
5565 uint32_t &child_bitfield_bit_offset,
5566 bool &child_is_base_class,
5567 bool &child_is_deref_of_parent,
5568 ValueObject *valobj)
5569{
5570 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005571 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005572
5573 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5574 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5575 child_bitfield_bit_size = 0;
5576 child_bitfield_bit_offset = 0;
5577 child_is_base_class = false;
5578
5579 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5580 uint32_t bit_offset;
5581 switch (parent_type_class)
5582 {
5583 case clang::Type::Builtin:
5584 if (idx_is_valid)
5585 {
5586 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5587 {
5588 case clang::BuiltinType::ObjCId:
5589 case clang::BuiltinType::ObjCClass:
5590 child_name = "isa";
5591 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005592 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005593
5594 default:
5595 break;
5596 }
5597 }
5598 break;
5599
5600 case clang::Type::Record:
5601 if (idx_is_valid && GetCompleteType(type))
5602 {
5603 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5604 const clang::RecordDecl *record_decl = record_type->getDecl();
5605 assert(record_decl);
5606 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5607 uint32_t child_idx = 0;
5608
5609 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5610 if (cxx_record_decl)
5611 {
5612 // We might have base classes to print out first
5613 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5614 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5615 base_class != base_class_end;
5616 ++base_class)
5617 {
5618 const clang::CXXRecordDecl *base_class_decl = nullptr;
5619
5620 // Skip empty base classes
5621 if (omit_empty_base_classes)
5622 {
5623 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5624 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5625 continue;
5626 }
5627
5628 if (idx == child_idx)
5629 {
5630 if (base_class_decl == nullptr)
5631 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5632
5633
5634 if (base_class->isVirtual())
5635 {
5636 bool handled = false;
5637 if (valobj)
5638 {
5639 Error err;
5640 AddressType addr_type = eAddressTypeInvalid;
5641 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5642
5643 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5644 {
5645
5646 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5647 Process *process = exe_ctx.GetProcessPtr();
5648 if (process)
5649 {
5650 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5651 if (vtable_ctx)
5652 {
5653 if (vtable_ctx->isMicrosoft())
5654 {
5655 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5656
5657 if (vtable_ptr_addr)
5658 {
5659 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5660
5661 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5662 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5663 {
5664 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5665 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5666 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5667 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5668 if (base_offset != UINT32_MAX)
5669 {
5670 handled = true;
5671 bit_offset = base_offset * 8;
5672 }
5673 }
5674 }
5675 }
5676 else
5677 {
5678 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5679 if (vtable_ptr_addr)
5680 {
5681 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5682 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5683 {
5684 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5685 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5686 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5687 if (base_offset != UINT32_MAX)
5688 {
5689 handled = true;
5690 bit_offset = base_offset * 8;
5691 }
5692 }
5693 }
5694 }
5695 }
5696 }
5697 }
5698
5699 }
5700 if (!handled)
5701 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5702 }
5703 else
5704 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5705
5706 // Base classes should be a multiple of 8 bits in size
5707 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005708 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005709 child_name = base_class_clang_type.GetTypeName().AsCString("");
5710 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5711
5712 // Base classes bit sizes should be a multiple of 8 bits in size
5713 assert (base_class_clang_type_bit_size % 8 == 0);
5714 child_byte_size = base_class_clang_type_bit_size / 8;
5715 child_is_base_class = true;
5716 return base_class_clang_type;
5717 }
5718 // We don't increment the child index in the for loop since we might
5719 // be skipping empty base classes
5720 ++child_idx;
5721 }
5722 }
5723 // Make sure index is in range...
5724 uint32_t field_idx = 0;
5725 clang::RecordDecl::field_iterator field, field_end;
5726 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5727 {
5728 if (idx == child_idx)
5729 {
5730 // Print the member type if requested
5731 // Print the member name and equal sign
5732 child_name.assign(field->getNameAsString().c_str());
5733
5734 // Figure out the type byte size (field_type_info.first) and
5735 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005736 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005737 assert(field_idx < record_layout.getFieldCount());
5738 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5739
5740 // Figure out the field offset within the current struct/union/class type
5741 bit_offset = record_layout.getFieldOffset (field_idx);
5742 child_byte_offset = bit_offset / 8;
5743 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5744 child_bitfield_bit_offset = bit_offset % 8;
5745
5746 return field_clang_type;
5747 }
5748 }
5749 }
5750 break;
5751
5752 case clang::Type::ObjCObject:
5753 case clang::Type::ObjCInterface:
5754 if (idx_is_valid && GetCompleteType(type))
5755 {
5756 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5757 assert (objc_class_type);
5758 if (objc_class_type)
5759 {
5760 uint32_t child_idx = 0;
5761 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5762
5763 if (class_interface_decl)
5764 {
5765
5766 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5767 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5768 if (superclass_interface_decl)
5769 {
5770 if (omit_empty_base_classes)
5771 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005772 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005773 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5774 {
5775 if (idx == 0)
5776 {
5777 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5778
5779
5780 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5781
5782 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5783
5784 child_byte_size = ivar_type_info.Width / 8;
5785 child_byte_offset = 0;
5786 child_is_base_class = true;
5787
Greg Claytona1e5dc82015-08-11 22:53:00 +00005788 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005789 }
5790
5791 ++child_idx;
5792 }
5793 }
5794 else
5795 ++child_idx;
5796 }
5797
5798 const uint32_t superclass_idx = child_idx;
5799
5800 if (idx < (child_idx + class_interface_decl->ivar_size()))
5801 {
5802 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5803
5804 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5805 {
5806 if (child_idx == idx)
5807 {
5808 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5809
5810 clang::QualType ivar_qual_type(ivar_decl->getType());
5811
5812 child_name.assign(ivar_decl->getNameAsString().c_str());
5813
5814 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5815
5816 child_byte_size = ivar_type_info.Width / 8;
5817
5818 // Figure out the field offset within the current struct/union/class type
5819 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5820 // that doesn't account for the space taken up by unbacked properties, or from
5821 // the changing size of base classes that are newer than this class.
5822 // So if we have a process around that we can ask about this object, do so.
5823 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5824 Process *process = nullptr;
5825 if (exe_ctx)
5826 process = exe_ctx->GetProcessPtr();
5827 if (process)
5828 {
5829 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5830 if (objc_runtime != nullptr)
5831 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005832 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005833 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5834 }
5835 }
5836
5837 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5838 bit_offset = UINT32_MAX;
5839
5840 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5841 {
5842 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5843 child_byte_offset = bit_offset / 8;
5844 }
5845
5846 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5847 // of a bitfield within its containing object. So regardless of where we get the byte
5848 // offset from, we still need to get the bit offset for bitfields from the layout.
5849
5850 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5851 {
5852 if (bit_offset == UINT32_MAX)
5853 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5854
5855 child_bitfield_bit_offset = bit_offset % 8;
5856 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005857 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005858 }
5859 ++child_idx;
5860 }
5861 }
5862 }
5863 }
5864 }
5865 break;
5866
5867 case clang::Type::ObjCObjectPointer:
5868 if (idx_is_valid)
5869 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005870 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005871
5872 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5873 {
5874 child_is_deref_of_parent = false;
5875 bool tmp_child_is_deref_of_parent = false;
5876 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5877 idx,
5878 transparent_pointers,
5879 omit_empty_base_classes,
5880 ignore_array_bounds,
5881 child_name,
5882 child_byte_size,
5883 child_byte_offset,
5884 child_bitfield_bit_size,
5885 child_bitfield_bit_offset,
5886 child_is_base_class,
5887 tmp_child_is_deref_of_parent,
5888 valobj);
5889 }
5890 else
5891 {
5892 child_is_deref_of_parent = true;
5893 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5894 if (parent_name)
5895 {
5896 child_name.assign(1, '*');
5897 child_name += parent_name;
5898 }
5899
5900 // We have a pointer to an simple type
5901 if (idx == 0 && pointee_clang_type.GetCompleteType())
5902 {
5903 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5904 child_byte_offset = 0;
5905 return pointee_clang_type;
5906 }
5907 }
5908 }
5909 break;
5910
5911 case clang::Type::Vector:
5912 case clang::Type::ExtVector:
5913 if (idx_is_valid)
5914 {
5915 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5916 if (array)
5917 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005918 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005919 if (element_type.GetCompleteType())
5920 {
5921 char element_name[64];
5922 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5923 child_name.assign(element_name);
5924 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5925 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5926 return element_type;
5927 }
5928 }
5929 }
5930 break;
5931
5932 case clang::Type::ConstantArray:
5933 case clang::Type::IncompleteArray:
5934 if (ignore_array_bounds || idx_is_valid)
5935 {
5936 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5937 if (array)
5938 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005939 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005940 if (element_type.GetCompleteType())
5941 {
5942 char element_name[64];
5943 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5944 child_name.assign(element_name);
5945 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5946 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5947 return element_type;
5948 }
5949 }
5950 }
5951 break;
5952
5953
5954 case clang::Type::Pointer:
5955 if (idx_is_valid)
5956 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005957 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005958
5959 // Don't dereference "void *" pointers
5960 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005961 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005962
5963 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5964 {
5965 child_is_deref_of_parent = false;
5966 bool tmp_child_is_deref_of_parent = false;
5967 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5968 idx,
5969 transparent_pointers,
5970 omit_empty_base_classes,
5971 ignore_array_bounds,
5972 child_name,
5973 child_byte_size,
5974 child_byte_offset,
5975 child_bitfield_bit_size,
5976 child_bitfield_bit_offset,
5977 child_is_base_class,
5978 tmp_child_is_deref_of_parent,
5979 valobj);
5980 }
5981 else
5982 {
5983 child_is_deref_of_parent = true;
5984
5985 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5986 if (parent_name)
5987 {
5988 child_name.assign(1, '*');
5989 child_name += parent_name;
5990 }
5991
5992 // We have a pointer to an simple type
5993 if (idx == 0)
5994 {
5995 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5996 child_byte_offset = 0;
5997 return pointee_clang_type;
5998 }
5999 }
6000 }
6001 break;
6002
6003 case clang::Type::LValueReference:
6004 case clang::Type::RValueReference:
6005 if (idx_is_valid)
6006 {
6007 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006008 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006009 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6010 {
6011 child_is_deref_of_parent = false;
6012 bool tmp_child_is_deref_of_parent = false;
6013 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6014 idx,
6015 transparent_pointers,
6016 omit_empty_base_classes,
6017 ignore_array_bounds,
6018 child_name,
6019 child_byte_size,
6020 child_byte_offset,
6021 child_bitfield_bit_size,
6022 child_bitfield_bit_offset,
6023 child_is_base_class,
6024 tmp_child_is_deref_of_parent,
6025 valobj);
6026 }
6027 else
6028 {
6029 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6030 if (parent_name)
6031 {
6032 child_name.assign(1, '&');
6033 child_name += parent_name;
6034 }
6035
6036 // We have a pointer to an simple type
6037 if (idx == 0)
6038 {
6039 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6040 child_byte_offset = 0;
6041 return pointee_clang_type;
6042 }
6043 }
6044 }
6045 break;
6046
6047 case clang::Type::Typedef:
6048 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006049 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006050 return typedefed_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6051 idx,
6052 transparent_pointers,
6053 omit_empty_base_classes,
6054 ignore_array_bounds,
6055 child_name,
6056 child_byte_size,
6057 child_byte_offset,
6058 child_bitfield_bit_size,
6059 child_bitfield_bit_offset,
6060 child_is_base_class,
6061 child_is_deref_of_parent,
6062 valobj);
6063 }
6064 break;
6065
6066 case clang::Type::Elaborated:
6067 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006068 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006069 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6070 idx,
6071 transparent_pointers,
6072 omit_empty_base_classes,
6073 ignore_array_bounds,
6074 child_name,
6075 child_byte_size,
6076 child_byte_offset,
6077 child_bitfield_bit_size,
6078 child_bitfield_bit_offset,
6079 child_is_base_class,
6080 child_is_deref_of_parent,
6081 valobj);
6082 }
6083
6084 case clang::Type::Paren:
6085 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006086 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00006087 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6088 idx,
6089 transparent_pointers,
6090 omit_empty_base_classes,
6091 ignore_array_bounds,
6092 child_name,
6093 child_byte_size,
6094 child_byte_offset,
6095 child_bitfield_bit_size,
6096 child_bitfield_bit_offset,
6097 child_is_base_class,
6098 child_is_deref_of_parent,
6099 valobj);
6100 }
6101
6102
6103 default:
6104 break;
6105 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006106 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006107}
6108
6109static uint32_t
6110GetIndexForRecordBase
6111(
6112 const clang::RecordDecl *record_decl,
6113 const clang::CXXBaseSpecifier *base_spec,
6114 bool omit_empty_base_classes
6115 )
6116{
6117 uint32_t child_idx = 0;
6118
6119 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6120
6121 // const char *super_name = record_decl->getNameAsCString();
6122 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6123 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6124 //
6125 if (cxx_record_decl)
6126 {
6127 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6128 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6129 base_class != base_class_end;
6130 ++base_class)
6131 {
6132 if (omit_empty_base_classes)
6133 {
6134 if (BaseSpecifierIsEmpty (base_class))
6135 continue;
6136 }
6137
6138 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6139 // child_idx,
6140 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6141 //
6142 //
6143 if (base_class == base_spec)
6144 return child_idx;
6145 ++child_idx;
6146 }
6147 }
6148
6149 return UINT32_MAX;
6150}
6151
6152
6153static uint32_t
6154GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6155 clang::NamedDecl *canonical_decl,
6156 bool omit_empty_base_classes)
6157{
6158 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6159 omit_empty_base_classes);
6160
6161 clang::RecordDecl::field_iterator field, field_end;
6162 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6163 field != field_end;
6164 ++field, ++child_idx)
6165 {
6166 if (field->getCanonicalDecl() == canonical_decl)
6167 return child_idx;
6168 }
6169
6170 return UINT32_MAX;
6171}
6172
6173// Look for a child member (doesn't include base classes, but it does include
6174// their members) in the type hierarchy. Returns an index path into "clang_type"
6175// on how to reach the appropriate member.
6176//
6177// class A
6178// {
6179// public:
6180// int m_a;
6181// int m_b;
6182// };
6183//
6184// class B
6185// {
6186// };
6187//
6188// class C :
6189// public B,
6190// public A
6191// {
6192// };
6193//
6194// If we have a clang type that describes "class C", and we wanted to looked
6195// "m_b" in it:
6196//
6197// With omit_empty_base_classes == false we would get an integer array back with:
6198// { 1, 1 }
6199// The first index 1 is the child index for "class A" within class C
6200// The second index 1 is the child index for "m_b" within class A
6201//
6202// With omit_empty_base_classes == true we would get an integer array back with:
6203// { 0, 1 }
6204// The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count)
6205// The second index 1 is the child index for "m_b" within class A
6206
6207size_t
6208ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6209 bool omit_empty_base_classes,
6210 std::vector<uint32_t>& child_indexes)
6211{
6212 if (type && name && name[0])
6213 {
6214 clang::QualType qual_type(GetCanonicalQualType(type));
6215 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6216 switch (type_class)
6217 {
6218 case clang::Type::Record:
6219 if (GetCompleteType(type))
6220 {
6221 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6222 const clang::RecordDecl *record_decl = record_type->getDecl();
6223
6224 assert(record_decl);
6225 uint32_t child_idx = 0;
6226
6227 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6228
6229 // Try and find a field that matches NAME
6230 clang::RecordDecl::field_iterator field, field_end;
6231 llvm::StringRef name_sref(name);
6232 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6233 field != field_end;
6234 ++field, ++child_idx)
6235 {
6236 llvm::StringRef field_name = field->getName();
6237 if (field_name.empty())
6238 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006239 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006240 child_indexes.push_back(child_idx);
6241 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6242 return child_indexes.size();
6243 child_indexes.pop_back();
6244
6245 }
6246 else if (field_name.equals (name_sref))
6247 {
6248 // We have to add on the number of base classes to this index!
6249 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6250 return child_indexes.size();
6251 }
6252 }
6253
6254 if (cxx_record_decl)
6255 {
6256 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6257
6258 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6259
6260 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6261 // Didn't find things easily, lets let clang do its thang...
6262 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6263 clang::DeclarationName decl_name(&ident_ref);
6264
6265 clang::CXXBasePaths paths;
6266 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6267 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6268 },
6269 paths))
6270 {
6271 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6272 for (path = paths.begin(); path != path_end; ++path)
6273 {
6274 const size_t num_path_elements = path->size();
6275 for (size_t e=0; e<num_path_elements; ++e)
6276 {
6277 clang::CXXBasePathElement elem = (*path)[e];
6278
6279 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6280 if (child_idx == UINT32_MAX)
6281 {
6282 child_indexes.clear();
6283 return 0;
6284 }
6285 else
6286 {
6287 child_indexes.push_back (child_idx);
6288 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6289 }
6290 }
6291 for (clang::NamedDecl *path_decl : path->Decls)
6292 {
6293 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6294 if (child_idx == UINT32_MAX)
6295 {
6296 child_indexes.clear();
6297 return 0;
6298 }
6299 else
6300 {
6301 child_indexes.push_back (child_idx);
6302 }
6303 }
6304 }
6305 return child_indexes.size();
6306 }
6307 }
6308
6309 }
6310 break;
6311
6312 case clang::Type::ObjCObject:
6313 case clang::Type::ObjCInterface:
6314 if (GetCompleteType(type))
6315 {
6316 llvm::StringRef name_sref(name);
6317 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6318 assert (objc_class_type);
6319 if (objc_class_type)
6320 {
6321 uint32_t child_idx = 0;
6322 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6323
6324 if (class_interface_decl)
6325 {
6326 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6327 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6328
6329 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6330 {
6331 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6332
6333 if (ivar_decl->getName().equals (name_sref))
6334 {
6335 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6336 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6337 ++child_idx;
6338
6339 child_indexes.push_back (child_idx);
6340 return child_indexes.size();
6341 }
6342 }
6343
6344 if (superclass_interface_decl)
6345 {
6346 // The super class index is always zero for ObjC classes,
6347 // so we push it onto the child indexes in case we find
6348 // an ivar in our superclass...
6349 child_indexes.push_back (0);
6350
Greg Claytona1e5dc82015-08-11 22:53:00 +00006351 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006352 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6353 omit_empty_base_classes,
6354 child_indexes))
6355 {
6356 // We did find an ivar in a superclass so just
6357 // return the results!
6358 return child_indexes.size();
6359 }
6360
6361 // We didn't find an ivar matching "name" in our
6362 // superclass, pop the superclass zero index that
6363 // we pushed on above.
6364 child_indexes.pop_back();
6365 }
6366 }
6367 }
6368 }
6369 break;
6370
6371 case clang::Type::ObjCObjectPointer:
6372 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006373 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006374 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6375 omit_empty_base_classes,
6376 child_indexes);
6377 }
6378 break;
6379
6380
6381 case clang::Type::ConstantArray:
6382 {
6383 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6384 // const uint64_t element_count = array->getSize().getLimitedValue();
6385 //
6386 // if (idx < element_count)
6387 // {
6388 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6389 //
6390 // char element_name[32];
6391 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6392 //
6393 // child_name.assign(element_name);
6394 // assert(field_type_info.first % 8 == 0);
6395 // child_byte_size = field_type_info.first / 8;
6396 // child_byte_offset = idx * child_byte_size;
6397 // return array->getElementType().getAsOpaquePtr();
6398 // }
6399 }
6400 break;
6401
6402 // case clang::Type::MemberPointerType:
6403 // {
6404 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6405 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6406 //
6407 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6408 // {
6409 // return GetIndexOfChildWithName (ast,
6410 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6411 // name);
6412 // }
6413 // }
6414 // break;
6415 //
6416 case clang::Type::LValueReference:
6417 case clang::Type::RValueReference:
6418 {
6419 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6420 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006421 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006422
6423 if (pointee_clang_type.IsAggregateType ())
6424 {
6425 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6426 omit_empty_base_classes,
6427 child_indexes);
6428 }
6429 }
6430 break;
6431
6432 case clang::Type::Pointer:
6433 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006434 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006435
6436 if (pointee_clang_type.IsAggregateType ())
6437 {
6438 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6439 omit_empty_base_classes,
6440 child_indexes);
6441 }
6442 }
6443 break;
6444
6445 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006446 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006447 omit_empty_base_classes,
6448 child_indexes);
6449
6450 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006451 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006452 omit_empty_base_classes,
6453 child_indexes);
6454
6455 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006456 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006457 omit_empty_base_classes,
6458 child_indexes);
6459
6460 default:
6461 break;
6462 }
6463 }
6464 return 0;
6465}
6466
6467
6468// Get the index of the child of "clang_type" whose name matches. This function
6469// doesn't descend into the children, but only looks one level deep and name
6470// matches can include base class names.
6471
6472uint32_t
6473ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6474{
6475 if (type && name && name[0])
6476 {
6477 clang::QualType qual_type(GetCanonicalQualType(type));
6478
6479 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6480
6481 switch (type_class)
6482 {
6483 case clang::Type::Record:
6484 if (GetCompleteType(type))
6485 {
6486 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6487 const clang::RecordDecl *record_decl = record_type->getDecl();
6488
6489 assert(record_decl);
6490 uint32_t child_idx = 0;
6491
6492 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6493
6494 if (cxx_record_decl)
6495 {
6496 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6497 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6498 base_class != base_class_end;
6499 ++base_class)
6500 {
6501 // Skip empty base classes
6502 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6503 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6504 continue;
6505
Greg Claytona1e5dc82015-08-11 22:53:00 +00006506 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006507 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6508 if (base_class_type_name.compare (name) == 0)
6509 return child_idx;
6510 ++child_idx;
6511 }
6512 }
6513
6514 // Try and find a field that matches NAME
6515 clang::RecordDecl::field_iterator field, field_end;
6516 llvm::StringRef name_sref(name);
6517 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6518 field != field_end;
6519 ++field, ++child_idx)
6520 {
6521 if (field->getName().equals (name_sref))
6522 return child_idx;
6523 }
6524
6525 }
6526 break;
6527
6528 case clang::Type::ObjCObject:
6529 case clang::Type::ObjCInterface:
6530 if (GetCompleteType(type))
6531 {
6532 llvm::StringRef name_sref(name);
6533 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6534 assert (objc_class_type);
6535 if (objc_class_type)
6536 {
6537 uint32_t child_idx = 0;
6538 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6539
6540 if (class_interface_decl)
6541 {
6542 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6543 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6544
6545 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6546 {
6547 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6548
6549 if (ivar_decl->getName().equals (name_sref))
6550 {
6551 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6552 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6553 ++child_idx;
6554
6555 return child_idx;
6556 }
6557 }
6558
6559 if (superclass_interface_decl)
6560 {
6561 if (superclass_interface_decl->getName().equals (name_sref))
6562 return 0;
6563 }
6564 }
6565 }
6566 }
6567 break;
6568
6569 case clang::Type::ObjCObjectPointer:
6570 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006571 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006572 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6573 }
6574 break;
6575
6576 case clang::Type::ConstantArray:
6577 {
6578 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6579 // const uint64_t element_count = array->getSize().getLimitedValue();
6580 //
6581 // if (idx < element_count)
6582 // {
6583 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6584 //
6585 // char element_name[32];
6586 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6587 //
6588 // child_name.assign(element_name);
6589 // assert(field_type_info.first % 8 == 0);
6590 // child_byte_size = field_type_info.first / 8;
6591 // child_byte_offset = idx * child_byte_size;
6592 // return array->getElementType().getAsOpaquePtr();
6593 // }
6594 }
6595 break;
6596
6597 // case clang::Type::MemberPointerType:
6598 // {
6599 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6600 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6601 //
6602 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6603 // {
6604 // return GetIndexOfChildWithName (ast,
6605 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6606 // name);
6607 // }
6608 // }
6609 // break;
6610 //
6611 case clang::Type::LValueReference:
6612 case clang::Type::RValueReference:
6613 {
6614 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006615 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006616
6617 if (pointee_type.IsAggregateType ())
6618 {
6619 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6620 }
6621 }
6622 break;
6623
6624 case clang::Type::Pointer:
6625 {
6626 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006627 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006628
6629 if (pointee_type.IsAggregateType ())
6630 {
6631 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6632 }
6633 else
6634 {
6635 // if (parent_name)
6636 // {
6637 // child_name.assign(1, '*');
6638 // child_name += parent_name;
6639 // }
6640 //
6641 // // We have a pointer to an simple type
6642 // if (idx == 0)
6643 // {
6644 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6645 // assert(clang_type_info.first % 8 == 0);
6646 // child_byte_size = clang_type_info.first / 8;
6647 // child_byte_offset = 0;
6648 // return pointee_type.getAsOpaquePtr();
6649 // }
6650 }
6651 }
6652 break;
6653
6654 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006655 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006656
6657 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006658 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006659
6660 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006661 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006662
6663 default:
6664 break;
6665 }
6666 }
6667 return UINT32_MAX;
6668}
6669
6670
6671size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006672ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006673{
6674 if (!type)
6675 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006676
6677 clang::QualType qual_type (GetCanonicalQualType(type));
6678 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6679 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006680 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006681 case clang::Type::Record:
6682 if (GetCompleteType(type))
6683 {
6684 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6685 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006686 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006687 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6688 if (template_decl)
6689 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006690 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006691 }
6692 break;
6693
6694 case clang::Type::Typedef:
6695 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6696
6697 case clang::Type::Elaborated:
6698 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6699
6700 case clang::Type::Paren:
6701 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6702
6703 default:
6704 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006705 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006706
Greg Claytond8d4a572015-08-11 21:38:15 +00006707 return 0;
6708}
6709
Greg Claytona1e5dc82015-08-11 22:53:00 +00006710CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006711ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006712{
6713 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006714 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006715
6716 clang::QualType qual_type (GetCanonicalQualType(type));
6717 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6718 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006719 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006720 case clang::Type::Record:
6721 if (GetCompleteType(type))
6722 {
6723 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6724 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006725 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006726 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6727 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006728 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006729 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6730 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006731 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006732 case clang::TemplateArgument::Null:
6733 kind = eTemplateArgumentKindNull;
6734 return CompilerType();
6735
6736 case clang::TemplateArgument::Type:
6737 kind = eTemplateArgumentKindType;
6738 return CompilerType(getASTContext(), template_arg.getAsType());
6739
6740 case clang::TemplateArgument::Declaration:
6741 kind = eTemplateArgumentKindDeclaration;
6742 return CompilerType();
6743
6744 case clang::TemplateArgument::Integral:
6745 kind = eTemplateArgumentKindIntegral;
6746 return CompilerType(getASTContext(), template_arg.getIntegralType());
6747
6748 case clang::TemplateArgument::Template:
6749 kind = eTemplateArgumentKindTemplate;
6750 return CompilerType();
6751
6752 case clang::TemplateArgument::TemplateExpansion:
6753 kind = eTemplateArgumentKindTemplateExpansion;
6754 return CompilerType();
6755
6756 case clang::TemplateArgument::Expression:
6757 kind = eTemplateArgumentKindExpression;
6758 return CompilerType();
6759
6760 case clang::TemplateArgument::Pack:
6761 kind = eTemplateArgumentKindPack;
6762 return CompilerType();
6763
6764 default:
6765 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6766 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006767 }
6768 }
6769 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006770 }
6771 break;
6772
6773 case clang::Type::Typedef:
6774 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6775
6776 case clang::Type::Elaborated:
6777 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6778
6779 case clang::Type::Paren:
6780 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6781
6782 default:
6783 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006784 }
6785 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006786 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006787}
6788
6789static bool
6790IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6791{
6792 if (name == nullptr || name[0] == '\0')
6793 return false;
6794
6795#define OPERATOR_PREFIX "operator"
6796#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6797
6798 const char *post_op_name = nullptr;
6799
6800 bool no_space = true;
6801
6802 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6803 return false;
6804
6805 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6806
6807 if (post_op_name[0] == ' ')
6808 {
6809 post_op_name++;
6810 no_space = false;
6811 }
6812
6813#undef OPERATOR_PREFIX
6814#undef OPERATOR_PREFIX_LENGTH
6815
6816 // This is an operator, set the overloaded operator kind to invalid
6817 // in case this is a conversion operator...
6818 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6819
6820 switch (post_op_name[0])
6821 {
6822 default:
6823 if (no_space)
6824 return false;
6825 break;
6826 case 'n':
6827 if (no_space)
6828 return false;
6829 if (strcmp (post_op_name, "new") == 0)
6830 op_kind = clang::OO_New;
6831 else if (strcmp (post_op_name, "new[]") == 0)
6832 op_kind = clang::OO_Array_New;
6833 break;
6834
6835 case 'd':
6836 if (no_space)
6837 return false;
6838 if (strcmp (post_op_name, "delete") == 0)
6839 op_kind = clang::OO_Delete;
6840 else if (strcmp (post_op_name, "delete[]") == 0)
6841 op_kind = clang::OO_Array_Delete;
6842 break;
6843
6844 case '+':
6845 if (post_op_name[1] == '\0')
6846 op_kind = clang::OO_Plus;
6847 else if (post_op_name[2] == '\0')
6848 {
6849 if (post_op_name[1] == '=')
6850 op_kind = clang::OO_PlusEqual;
6851 else if (post_op_name[1] == '+')
6852 op_kind = clang::OO_PlusPlus;
6853 }
6854 break;
6855
6856 case '-':
6857 if (post_op_name[1] == '\0')
6858 op_kind = clang::OO_Minus;
6859 else if (post_op_name[2] == '\0')
6860 {
6861 switch (post_op_name[1])
6862 {
6863 case '=': op_kind = clang::OO_MinusEqual; break;
6864 case '-': op_kind = clang::OO_MinusMinus; break;
6865 case '>': op_kind = clang::OO_Arrow; break;
6866 }
6867 }
6868 else if (post_op_name[3] == '\0')
6869 {
6870 if (post_op_name[2] == '*')
6871 op_kind = clang::OO_ArrowStar; break;
6872 }
6873 break;
6874
6875 case '*':
6876 if (post_op_name[1] == '\0')
6877 op_kind = clang::OO_Star;
6878 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6879 op_kind = clang::OO_StarEqual;
6880 break;
6881
6882 case '/':
6883 if (post_op_name[1] == '\0')
6884 op_kind = clang::OO_Slash;
6885 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6886 op_kind = clang::OO_SlashEqual;
6887 break;
6888
6889 case '%':
6890 if (post_op_name[1] == '\0')
6891 op_kind = clang::OO_Percent;
6892 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6893 op_kind = clang::OO_PercentEqual;
6894 break;
6895
6896
6897 case '^':
6898 if (post_op_name[1] == '\0')
6899 op_kind = clang::OO_Caret;
6900 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6901 op_kind = clang::OO_CaretEqual;
6902 break;
6903
6904 case '&':
6905 if (post_op_name[1] == '\0')
6906 op_kind = clang::OO_Amp;
6907 else if (post_op_name[2] == '\0')
6908 {
6909 switch (post_op_name[1])
6910 {
6911 case '=': op_kind = clang::OO_AmpEqual; break;
6912 case '&': op_kind = clang::OO_AmpAmp; break;
6913 }
6914 }
6915 break;
6916
6917 case '|':
6918 if (post_op_name[1] == '\0')
6919 op_kind = clang::OO_Pipe;
6920 else if (post_op_name[2] == '\0')
6921 {
6922 switch (post_op_name[1])
6923 {
6924 case '=': op_kind = clang::OO_PipeEqual; break;
6925 case '|': op_kind = clang::OO_PipePipe; break;
6926 }
6927 }
6928 break;
6929
6930 case '~':
6931 if (post_op_name[1] == '\0')
6932 op_kind = clang::OO_Tilde;
6933 break;
6934
6935 case '!':
6936 if (post_op_name[1] == '\0')
6937 op_kind = clang::OO_Exclaim;
6938 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6939 op_kind = clang::OO_ExclaimEqual;
6940 break;
6941
6942 case '=':
6943 if (post_op_name[1] == '\0')
6944 op_kind = clang::OO_Equal;
6945 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6946 op_kind = clang::OO_EqualEqual;
6947 break;
6948
6949 case '<':
6950 if (post_op_name[1] == '\0')
6951 op_kind = clang::OO_Less;
6952 else if (post_op_name[2] == '\0')
6953 {
6954 switch (post_op_name[1])
6955 {
6956 case '<': op_kind = clang::OO_LessLess; break;
6957 case '=': op_kind = clang::OO_LessEqual; break;
6958 }
6959 }
6960 else if (post_op_name[3] == '\0')
6961 {
6962 if (post_op_name[2] == '=')
6963 op_kind = clang::OO_LessLessEqual;
6964 }
6965 break;
6966
6967 case '>':
6968 if (post_op_name[1] == '\0')
6969 op_kind = clang::OO_Greater;
6970 else if (post_op_name[2] == '\0')
6971 {
6972 switch (post_op_name[1])
6973 {
6974 case '>': op_kind = clang::OO_GreaterGreater; break;
6975 case '=': op_kind = clang::OO_GreaterEqual; break;
6976 }
6977 }
6978 else if (post_op_name[1] == '>' &&
6979 post_op_name[2] == '=' &&
6980 post_op_name[3] == '\0')
6981 {
6982 op_kind = clang::OO_GreaterGreaterEqual;
6983 }
6984 break;
6985
6986 case ',':
6987 if (post_op_name[1] == '\0')
6988 op_kind = clang::OO_Comma;
6989 break;
6990
6991 case '(':
6992 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
6993 op_kind = clang::OO_Call;
6994 break;
6995
6996 case '[':
6997 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
6998 op_kind = clang::OO_Subscript;
6999 break;
7000 }
7001
7002 return true;
7003}
7004
7005clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007006ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007007{
7008 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
7009 if (enutype)
7010 return enutype->getDecl();
7011 return NULL;
7012}
7013
7014clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007015ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007016{
7017 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7018 if (record_type)
7019 return record_type->getDecl();
7020 return nullptr;
7021}
7022
7023clang::CXXRecordDecl *
7024ClangASTContext::GetAsCXXRecordDecl (void* type)
7025{
7026 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7027}
7028
7029clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007030ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007031{
7032 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7033 if (objc_class_type)
7034 return objc_class_type->getInterface();
7035 return nullptr;
7036}
7037
7038clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007039ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7040 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007041 AccessType access,
7042 uint32_t bitfield_bit_size)
7043{
7044 if (!type.IsValid() || !field_clang_type.IsValid())
7045 return nullptr;
7046 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7047 if (!ast)
7048 return nullptr;
7049 clang::ASTContext* clang_ast = ast->getASTContext();
7050
7051 clang::FieldDecl *field = nullptr;
7052
7053 clang::Expr *bit_width = nullptr;
7054 if (bitfield_bit_size != 0)
7055 {
7056 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7057 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7058 }
7059
7060 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7061 if (record_decl)
7062 {
7063 field = clang::FieldDecl::Create (*clang_ast,
7064 record_decl,
7065 clang::SourceLocation(),
7066 clang::SourceLocation(),
7067 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7068 GetQualType(field_clang_type), // Field type
7069 nullptr, // TInfo *
7070 bit_width, // BitWidth
7071 false, // Mutable
7072 clang::ICIS_NoInit); // HasInit
7073
7074 if (!name)
7075 {
7076 // Determine whether this field corresponds to an anonymous
7077 // struct or union.
7078 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7079 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7080 if (!Rec->getDeclName()) {
7081 Rec->setAnonymousStructOrUnion(true);
7082 field->setImplicit();
7083
7084 }
7085 }
7086 }
7087
7088 if (field)
7089 {
7090 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7091
7092 record_decl->addDecl(field);
7093
7094#ifdef LLDB_CONFIGURATION_DEBUG
7095 VerifyDecl(field);
7096#endif
7097 }
7098 }
7099 else
7100 {
7101 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7102
7103 if (class_interface_decl)
7104 {
7105 const bool is_synthesized = false;
7106
7107 field_clang_type.GetCompleteType();
7108
7109 field = clang::ObjCIvarDecl::Create (*clang_ast,
7110 class_interface_decl,
7111 clang::SourceLocation(),
7112 clang::SourceLocation(),
7113 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7114 GetQualType(field_clang_type), // Field type
7115 nullptr, // TypeSourceInfo *
7116 ConvertAccessTypeToObjCIvarAccessControl (access),
7117 bit_width,
7118 is_synthesized);
7119
7120 if (field)
7121 {
7122 class_interface_decl->addDecl(field);
7123
7124#ifdef LLDB_CONFIGURATION_DEBUG
7125 VerifyDecl(field);
7126#endif
7127 }
7128 }
7129 }
7130 return field;
7131}
7132
7133void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007134ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007135{
7136 ClangASTContext* ast = nullptr;
7137 if (type)
7138 ast = type.GetTypeSystem()->AsClangASTContext();
7139 if (!ast)
7140 return;
7141
7142 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7143
7144 if (!record_decl)
7145 return;
7146
7147 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7148
7149 IndirectFieldVector indirect_fields;
7150 clang::RecordDecl::field_iterator field_pos;
7151 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7152 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7153 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7154 {
7155 if (field_pos->isAnonymousStructOrUnion())
7156 {
7157 clang::QualType field_qual_type = field_pos->getType();
7158
7159 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7160
7161 if (!field_record_type)
7162 continue;
7163
7164 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7165
7166 if (!field_record_decl)
7167 continue;
7168
7169 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7170 di != de;
7171 ++di)
7172 {
7173 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7174 {
7175 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7176 chain[0] = *field_pos;
7177 chain[1] = nested_field_decl;
7178 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7179 record_decl,
7180 clang::SourceLocation(),
7181 nested_field_decl->getIdentifier(),
7182 nested_field_decl->getType(),
7183 chain,
7184 2);
7185
7186 indirect_field->setImplicit();
7187
7188 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7189 nested_field_decl->getAccess()));
7190
7191 indirect_fields.push_back(indirect_field);
7192 }
7193 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7194 {
7195 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7196 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7197 chain[0] = *field_pos;
7198
7199 int chain_index = 1;
7200 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7201 nce = nested_indirect_field_decl->chain_end();
7202 nci < nce;
7203 ++nci)
7204 {
7205 chain[chain_index] = *nci;
7206 chain_index++;
7207 }
7208
7209 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7210 record_decl,
7211 clang::SourceLocation(),
7212 nested_indirect_field_decl->getIdentifier(),
7213 nested_indirect_field_decl->getType(),
7214 chain,
7215 nested_chain_size + 1);
7216
7217 indirect_field->setImplicit();
7218
7219 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7220 nested_indirect_field_decl->getAccess()));
7221
7222 indirect_fields.push_back(indirect_field);
7223 }
7224 }
7225 }
7226 }
7227
7228 // Check the last field to see if it has an incomplete array type as its
7229 // last member and if it does, the tell the record decl about it
7230 if (last_field_pos != field_end_pos)
7231 {
7232 if (last_field_pos->getType()->isIncompleteArrayType())
7233 record_decl->hasFlexibleArrayMember();
7234 }
7235
7236 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7237 ifi < ife;
7238 ++ifi)
7239 {
7240 record_decl->addDecl(*ifi);
7241 }
7242}
7243
7244void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007245ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007246{
7247 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7248
7249 if (!record_decl)
7250 return;
7251
7252 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*type.GetTypeSystem()->AsClangASTContext()->getASTContext()));
7253}
7254
7255clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007256ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7257 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007258 AccessType access)
7259{
7260 clang::VarDecl *var_decl = nullptr;
7261
7262 if (!type.IsValid() || !var_type.IsValid())
7263 return nullptr;
7264 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7265 if (!ast)
7266 return nullptr;
7267
7268 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7269 if (record_decl)
7270 {
7271 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7272 record_decl, // DeclContext *
7273 clang::SourceLocation(), // clang::SourceLocation StartLoc
7274 clang::SourceLocation(), // clang::SourceLocation IdLoc
7275 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7276 GetQualType(var_type), // Variable clang::QualType
7277 nullptr, // TypeSourceInfo *
7278 clang::SC_Static); // StorageClass
7279 if (var_decl)
7280 {
7281 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7282 record_decl->addDecl(var_decl);
7283
7284#ifdef LLDB_CONFIGURATION_DEBUG
7285 VerifyDecl(var_decl);
7286#endif
7287 }
7288 }
7289 return var_decl;
7290}
7291
7292
7293clang::CXXMethodDecl *
7294ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007295 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007296 lldb::AccessType access,
7297 bool is_virtual,
7298 bool is_static,
7299 bool is_inline,
7300 bool is_explicit,
7301 bool is_attr_used,
7302 bool is_artificial)
7303{
7304 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7305 return nullptr;
7306
7307 clang::QualType record_qual_type(GetCanonicalQualType(type));
7308
7309 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7310
7311 if (cxx_record_decl == nullptr)
7312 return nullptr;
7313
7314 clang::QualType method_qual_type (GetQualType(method_clang_type));
7315
7316 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7317
7318 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7319
7320 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7321
7322 if (function_type == nullptr)
7323 return nullptr;
7324
7325 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7326
7327 if (!method_function_prototype)
7328 return nullptr;
7329
7330 unsigned int num_params = method_function_prototype->getNumParams();
7331
7332 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7333 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7334
7335 if (is_artificial)
7336 return nullptr; // skip everything artificial
7337
7338 if (name[0] == '~')
7339 {
7340 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7341 cxx_record_decl,
7342 clang::SourceLocation(),
7343 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7344 method_qual_type,
7345 nullptr,
7346 is_inline,
7347 is_artificial);
7348 cxx_method_decl = cxx_dtor_decl;
7349 }
7350 else if (decl_name == cxx_record_decl->getDeclName())
7351 {
7352 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7353 cxx_record_decl,
7354 clang::SourceLocation(),
7355 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7356 method_qual_type,
7357 nullptr, // TypeSourceInfo *
7358 is_explicit,
7359 is_inline,
7360 is_artificial,
7361 false /*is_constexpr*/);
7362 cxx_method_decl = cxx_ctor_decl;
7363 }
7364 else
7365 {
7366 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7367 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7368
7369 if (IsOperator (name, op_kind))
7370 {
7371 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7372 {
7373 // Check the number of operator parameters. Sometimes we have
7374 // seen bad DWARF that doesn't correctly describe operators and
7375 // if we try to create a method and add it to the class, clang
7376 // will assert and crash, so we need to make sure things are
7377 // acceptable.
7378 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7379 return nullptr;
7380 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7381 cxx_record_decl,
7382 clang::SourceLocation(),
7383 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7384 method_qual_type,
7385 nullptr, // TypeSourceInfo *
7386 SC,
7387 is_inline,
7388 false /*is_constexpr*/,
7389 clang::SourceLocation());
7390 }
7391 else if (num_params == 0)
7392 {
7393 // Conversion operators don't take params...
7394 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7395 cxx_record_decl,
7396 clang::SourceLocation(),
7397 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7398 method_qual_type,
7399 nullptr, // TypeSourceInfo *
7400 is_inline,
7401 is_explicit,
7402 false /*is_constexpr*/,
7403 clang::SourceLocation());
7404 }
7405 }
7406
7407 if (cxx_method_decl == nullptr)
7408 {
7409 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7410 cxx_record_decl,
7411 clang::SourceLocation(),
7412 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7413 method_qual_type,
7414 nullptr, // TypeSourceInfo *
7415 SC,
7416 is_inline,
7417 false /*is_constexpr*/,
7418 clang::SourceLocation());
7419 }
7420 }
7421
7422 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7423
7424 cxx_method_decl->setAccess (access_specifier);
7425 cxx_method_decl->setVirtualAsWritten (is_virtual);
7426
7427 if (is_attr_used)
7428 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7429
7430 // Populate the method decl with parameter decls
7431
7432 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7433
7434 for (unsigned param_index = 0;
7435 param_index < num_params;
7436 ++param_index)
7437 {
7438 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7439 cxx_method_decl,
7440 clang::SourceLocation(),
7441 clang::SourceLocation(),
7442 nullptr, // anonymous
7443 method_function_prototype->getParamType(param_index),
7444 nullptr,
7445 clang::SC_None,
7446 nullptr));
7447 }
7448
7449 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7450
7451 cxx_record_decl->addDecl (cxx_method_decl);
7452
7453 // Sometimes the debug info will mention a constructor (default/copy/move),
7454 // destructor, or assignment operator (copy/move) but there won't be any
7455 // version of this in the code. So we check if the function was artificially
7456 // generated and if it is trivial and this lets the compiler/backend know
7457 // that it can inline the IR for these when it needs to and we can avoid a
7458 // "missing function" error when running expressions.
7459
7460 if (is_artificial)
7461 {
7462 if (cxx_ctor_decl &&
7463 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7464 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7465 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7466 {
7467 cxx_ctor_decl->setDefaulted();
7468 cxx_ctor_decl->setTrivial(true);
7469 }
7470 else if (cxx_dtor_decl)
7471 {
7472 if (cxx_record_decl->hasTrivialDestructor())
7473 {
7474 cxx_dtor_decl->setDefaulted();
7475 cxx_dtor_decl->setTrivial(true);
7476 }
7477 }
7478 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7479 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7480 {
7481 cxx_method_decl->setDefaulted();
7482 cxx_method_decl->setTrivial(true);
7483 }
7484 }
7485
7486#ifdef LLDB_CONFIGURATION_DEBUG
7487 VerifyDecl(cxx_method_decl);
7488#endif
7489
7490 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7491 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7492 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7493 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7494 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7495 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7496 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7497 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7498 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7499 return cxx_method_decl;
7500}
7501
7502
7503#pragma mark C++ Base Classes
7504
7505clang::CXXBaseSpecifier *
7506ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7507{
7508 if (type)
7509 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7510 is_virtual,
7511 base_of_class,
7512 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7513 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7514 clang::SourceLocation());
7515 return nullptr;
7516}
7517
7518void
7519ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7520{
7521 for (unsigned i=0; i<num_base_classes; ++i)
7522 {
7523 delete base_classes[i];
7524 base_classes[i] = nullptr;
7525 }
7526}
7527
7528bool
7529ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7530 unsigned num_base_classes)
7531{
7532 if (type)
7533 {
7534 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7535 if (cxx_record_decl)
7536 {
7537 cxx_record_decl->setBases(base_classes, num_base_classes);
7538 return true;
7539 }
7540 }
7541 return false;
7542}
7543
7544bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007545ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007546{
7547 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7548 if (!ast)
7549 return false;
7550 clang::ASTContext* clang_ast = ast->getASTContext();
7551
7552 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7553 {
7554 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7555 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7556 if (class_interface_decl && super_interface_decl)
7557 {
7558 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7559 return true;
7560 }
7561 }
7562 return false;
7563}
7564
7565bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007566ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007567 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007568 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007569 clang::ObjCIvarDecl *ivar_decl,
7570 const char *property_setter_name,
7571 const char *property_getter_name,
7572 uint32_t property_attributes,
7573 ClangASTMetadata *metadata)
7574{
7575 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7576 return false;
7577 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
7578 if (!ast)
7579 return false;
7580 clang::ASTContext* clang_ast = ast->getASTContext();
7581
7582 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7583
7584 if (class_interface_decl)
7585 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007586 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007587
7588 if (property_clang_type.IsValid())
7589 property_clang_type_to_access = property_clang_type;
7590 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007591 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007592
7593 if (class_interface_decl && property_clang_type_to_access.IsValid())
7594 {
7595 clang::TypeSourceInfo *prop_type_source;
7596 if (ivar_decl)
7597 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7598 else
7599 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7600
7601 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7602 class_interface_decl,
7603 clang::SourceLocation(), // Source Location
7604 &clang_ast->Idents.get(property_name),
7605 clang::SourceLocation(), //Source Location for AT
7606 clang::SourceLocation(), //Source location for (
7607 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7608 prop_type_source);
7609
7610 if (property_decl)
7611 {
7612 if (metadata)
7613 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7614
7615 class_interface_decl->addDecl (property_decl);
7616
7617 clang::Selector setter_sel, getter_sel;
7618
7619 if (property_setter_name != nullptr)
7620 {
7621 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7622 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7623 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7624 }
7625 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7626 {
7627 std::string setter_sel_string("set");
7628 setter_sel_string.push_back(::toupper(property_name[0]));
7629 setter_sel_string.append(&property_name[1]);
7630 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7631 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7632 }
7633 property_decl->setSetterName(setter_sel);
7634 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7635
7636 if (property_getter_name != nullptr)
7637 {
7638 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7639 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7640 }
7641 else
7642 {
7643 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7644 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7645 }
7646 property_decl->setGetterName(getter_sel);
7647 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7648
7649 if (ivar_decl)
7650 property_decl->setPropertyIvarDecl (ivar_decl);
7651
7652 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7653 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7654 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7655 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7656 if (property_attributes & DW_APPLE_PROPERTY_assign)
7657 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7658 if (property_attributes & DW_APPLE_PROPERTY_retain)
7659 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7660 if (property_attributes & DW_APPLE_PROPERTY_copy)
7661 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7662 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7663 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7664
7665 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7666 {
7667 const bool isInstance = true;
7668 const bool isVariadic = false;
7669 const bool isSynthesized = false;
7670 const bool isImplicitlyDeclared = true;
7671 const bool isDefined = false;
7672 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7673 const bool HasRelatedResultType = false;
7674
7675 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7676 clang::SourceLocation(),
7677 clang::SourceLocation(),
7678 getter_sel,
7679 GetQualType(property_clang_type_to_access),
7680 nullptr,
7681 class_interface_decl,
7682 isInstance,
7683 isVariadic,
7684 isSynthesized,
7685 isImplicitlyDeclared,
7686 isDefined,
7687 impControl,
7688 HasRelatedResultType);
7689
7690 if (getter && metadata)
7691 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7692
7693 if (getter)
7694 {
7695 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7696
7697 class_interface_decl->addDecl(getter);
7698 }
7699 }
7700
7701 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7702 {
7703 clang::QualType result_type = clang_ast->VoidTy;
7704
7705 const bool isInstance = true;
7706 const bool isVariadic = false;
7707 const bool isSynthesized = false;
7708 const bool isImplicitlyDeclared = true;
7709 const bool isDefined = false;
7710 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7711 const bool HasRelatedResultType = false;
7712
7713 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7714 clang::SourceLocation(),
7715 clang::SourceLocation(),
7716 setter_sel,
7717 result_type,
7718 nullptr,
7719 class_interface_decl,
7720 isInstance,
7721 isVariadic,
7722 isSynthesized,
7723 isImplicitlyDeclared,
7724 isDefined,
7725 impControl,
7726 HasRelatedResultType);
7727
7728 if (setter && metadata)
7729 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7730
7731 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7732
7733 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7734 setter,
7735 clang::SourceLocation(),
7736 clang::SourceLocation(),
7737 nullptr, // anonymous
7738 GetQualType(property_clang_type_to_access),
7739 nullptr,
7740 clang::SC_Auto,
7741 nullptr));
7742
7743 if (setter)
7744 {
7745 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7746
7747 class_interface_decl->addDecl(setter);
7748 }
7749 }
7750
7751 return true;
7752 }
7753 }
7754 }
7755 return false;
7756}
7757
7758bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007759ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007760{
7761 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7762 if (class_interface_decl)
7763 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7764 return false;
7765}
7766
7767
7768clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007769ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007770 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007771 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007772 lldb::AccessType access,
7773 bool is_artificial)
7774{
7775 if (!type || !method_clang_type.IsValid())
7776 return nullptr;
7777
7778 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7779
7780 if (class_interface_decl == nullptr)
7781 return nullptr;
7782 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
7783
7784 const char *selector_start = ::strchr (name, ' ');
7785 if (selector_start == nullptr)
7786 return nullptr;
7787
7788 selector_start++;
7789 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7790
7791 size_t len = 0;
7792 const char *start;
7793 //printf ("name = '%s'\n", name);
7794
7795 unsigned num_selectors_with_args = 0;
7796 for (start = selector_start;
7797 start && *start != '\0' && *start != ']';
7798 start += len)
7799 {
7800 len = ::strcspn(start, ":]");
7801 bool has_arg = (start[len] == ':');
7802 if (has_arg)
7803 ++num_selectors_with_args;
7804 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7805 if (has_arg)
7806 len += 1;
7807 }
7808
7809
7810 if (selector_idents.size() == 0)
7811 return nullptr;
7812
7813 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7814 selector_idents.data());
7815
7816 clang::QualType method_qual_type (GetQualType(method_clang_type));
7817
7818 // Populate the method decl with parameter decls
7819 const clang::Type *method_type(method_qual_type.getTypePtr());
7820
7821 if (method_type == nullptr)
7822 return nullptr;
7823
7824 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7825
7826 if (!method_function_prototype)
7827 return nullptr;
7828
7829
7830 bool is_variadic = false;
7831 bool is_synthesized = false;
7832 bool is_defined = false;
7833 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7834
7835 const unsigned num_args = method_function_prototype->getNumParams();
7836
7837 if (num_args != num_selectors_with_args)
7838 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7839
7840 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7841 clang::SourceLocation(), // beginLoc,
7842 clang::SourceLocation(), // endLoc,
7843 method_selector,
7844 method_function_prototype->getReturnType(),
7845 nullptr, // TypeSourceInfo *ResultTInfo,
7846 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7847 name[0] == '-',
7848 is_variadic,
7849 is_synthesized,
7850 true, // is_implicitly_declared; we force this to true because we don't have source locations
7851 is_defined,
7852 imp_control,
7853 false /*has_related_result_type*/);
7854
7855
7856 if (objc_method_decl == nullptr)
7857 return nullptr;
7858
7859 if (num_args > 0)
7860 {
7861 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7862
7863 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7864 {
7865 params.push_back (clang::ParmVarDecl::Create (*ast,
7866 objc_method_decl,
7867 clang::SourceLocation(),
7868 clang::SourceLocation(),
7869 nullptr, // anonymous
7870 method_function_prototype->getParamType(param_index),
7871 nullptr,
7872 clang::SC_Auto,
7873 nullptr));
7874 }
7875
7876 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7877 }
7878
7879 class_interface_decl->addDecl (objc_method_decl);
7880
7881#ifdef LLDB_CONFIGURATION_DEBUG
7882 VerifyDecl(objc_method_decl);
7883#endif
7884
7885 return objc_method_decl;
7886}
7887
7888bool
7889ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7890{
7891 if (!type)
7892 return false;
7893
7894 clang::QualType qual_type (GetCanonicalQualType(type));
7895
7896 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7897 switch (type_class)
7898 {
7899 case clang::Type::Record:
7900 {
7901 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7902 if (cxx_record_decl)
7903 {
7904 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7905 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7906 return true;
7907 }
7908 }
7909 break;
7910
7911 case clang::Type::Enum:
7912 {
7913 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7914 if (enum_decl)
7915 {
7916 enum_decl->setHasExternalLexicalStorage (has_extern);
7917 enum_decl->setHasExternalVisibleStorage (has_extern);
7918 return true;
7919 }
7920 }
7921 break;
7922
7923 case clang::Type::ObjCObject:
7924 case clang::Type::ObjCInterface:
7925 {
7926 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7927 assert (objc_class_type);
7928 if (objc_class_type)
7929 {
7930 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7931
7932 if (class_interface_decl)
7933 {
7934 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7935 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7936 return true;
7937 }
7938 }
7939 }
7940 break;
7941
7942 case clang::Type::Typedef:
7943 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7944
7945 case clang::Type::Elaborated:
7946 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7947
7948 case clang::Type::Paren:
7949 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7950
7951 default:
7952 break;
7953 }
7954 return false;
7955}
7956
7957
7958#pragma mark TagDecl
7959
7960bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007961ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007962{
7963 if (type)
7964 {
7965
7966 clang::QualType qual_type (GetQualType(type));
7967 const clang::Type *t = qual_type.getTypePtr();
7968 if (t)
7969 {
7970 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7971 if (tag_type)
7972 {
7973 clang::TagDecl *tag_decl = tag_type->getDecl();
7974 if (tag_decl)
7975 {
7976 tag_decl->startDefinition();
7977 return true;
7978 }
7979 }
7980
7981 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
7982 if (object_type)
7983 {
7984 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
7985 if (interface_decl)
7986 {
7987 interface_decl->startDefinition();
7988 return true;
7989 }
7990 }
7991 }
7992 }
7993 return false;
7994}
7995
7996bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007997ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007998{
7999 if (type)
8000 {
8001 clang::QualType qual_type (GetQualType(type));
8002 if (qual_type.isNull())
8003 return false;
8004 clang::ASTContext* ast = type.GetTypeSystem()->AsClangASTContext()->getASTContext();
8005
8006 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8007
8008 if (cxx_record_decl)
8009 {
8010 cxx_record_decl->completeDefinition();
8011
8012 return true;
8013 }
8014
8015 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
8016
8017 if (enutype)
8018 {
8019 clang::EnumDecl *enum_decl = enutype->getDecl();
8020
8021 if (enum_decl)
8022 {
8023 /// TODO This really needs to be fixed.
8024
8025 unsigned NumPositiveBits = 1;
8026 unsigned NumNegativeBits = 0;
8027
8028 clang::QualType promotion_qual_type;
8029 // If the enum integer type is less than an integer in bit width,
8030 // then we must promote it to an integer size.
8031 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8032 {
8033 if (enum_decl->getIntegerType()->isSignedIntegerType())
8034 promotion_qual_type = ast->IntTy;
8035 else
8036 promotion_qual_type = ast->UnsignedIntTy;
8037 }
8038 else
8039 promotion_qual_type = enum_decl->getIntegerType();
8040
8041 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
8042 return true;
8043 }
8044 }
8045 }
8046 return false;
8047}
8048
Greg Claytond8d4a572015-08-11 21:38:15 +00008049bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00008050ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
8051 const CompilerType &enumerator_clang_type,
8052 const Declaration &decl,
8053 const char *name,
8054 int64_t enum_value,
8055 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008056{
8057 if (type && enumerator_clang_type.IsValid() && name && name[0])
8058 {
8059 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8060
8061 bool is_signed = false;
8062 enumerator_clang_type.IsIntegerType (is_signed);
8063 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8064 if (clang_type)
8065 {
8066 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8067
8068 if (enutype)
8069 {
8070 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8071 enum_llvm_apsint = enum_value;
8072 clang::EnumConstantDecl *enumerator_decl =
8073 clang::EnumConstantDecl::Create (*getASTContext(),
8074 enutype->getDecl(),
8075 clang::SourceLocation(),
8076 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8077 GetQualType(enumerator_clang_type),
8078 nullptr,
8079 enum_llvm_apsint);
8080
8081 if (enumerator_decl)
8082 {
8083 enutype->getDecl()->addDecl(enumerator_decl);
8084
8085#ifdef LLDB_CONFIGURATION_DEBUG
8086 VerifyDecl(enumerator_decl);
8087#endif
8088
8089 return true;
8090 }
8091 }
8092 }
8093 }
8094 return false;
8095}
8096
Greg Claytona1e5dc82015-08-11 22:53:00 +00008097CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008098ClangASTContext::GetEnumerationIntegerType (void* type)
8099{
8100 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8101 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8102 if (clang_type)
8103 {
8104 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8105 if (enutype)
8106 {
8107 clang::EnumDecl *enum_decl = enutype->getDecl();
8108 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008109 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008110 }
8111 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008112 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008113}
8114
Greg Claytona1e5dc82015-08-11 22:53:00 +00008115CompilerType
8116ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008117{
8118 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8119 {
8120 ClangASTContext* ast = type.GetTypeSystem()->AsClangASTContext();
8121 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008122 return CompilerType();
8123 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008124 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8125 GetQualType(type).getTypePtr()));
8126 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008127 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008128}
8129
8130
8131size_t
8132ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8133{
8134 if (type)
8135 {
8136 clang::QualType qual_type (GetCanonicalQualType(type));
8137 uint32_t count = 0;
8138 bool is_complex = false;
8139 if (IsFloatingPointType (type, count, is_complex))
8140 {
8141 // TODO: handle complex and vector types
8142 if (count != 1)
8143 return false;
8144
8145 llvm::StringRef s_sref(s);
8146 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8147
8148 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8149 const uint64_t byte_size = bit_size / 8;
8150 if (dst_size >= byte_size)
8151 {
8152 if (bit_size == sizeof(float)*8)
8153 {
8154 float float32 = ap_float.convertToFloat();
8155 ::memcpy (dst, &float32, byte_size);
8156 return byte_size;
8157 }
8158 else if (bit_size >= 64)
8159 {
8160 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8161 ::memcpy (dst, ap_int.getRawData(), byte_size);
8162 return byte_size;
8163 }
8164 }
8165 }
8166 }
8167 return 0;
8168}
8169
8170
8171
8172//----------------------------------------------------------------------
8173// Dumping types
8174//----------------------------------------------------------------------
8175#define DEPTH_INCREMENT 2
8176
8177void
8178ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8179 Stream *s,
8180 lldb::Format format,
8181 const lldb_private::DataExtractor &data,
8182 lldb::offset_t data_byte_offset,
8183 size_t data_byte_size,
8184 uint32_t bitfield_bit_size,
8185 uint32_t bitfield_bit_offset,
8186 bool show_types,
8187 bool show_summary,
8188 bool verbose,
8189 uint32_t depth)
8190{
8191 if (!type)
8192 return;
8193
8194 clang::QualType qual_type(GetQualType(type));
8195 switch (qual_type->getTypeClass())
8196 {
8197 case clang::Type::Record:
8198 if (GetCompleteType(type))
8199 {
8200 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8201 const clang::RecordDecl *record_decl = record_type->getDecl();
8202 assert(record_decl);
8203 uint32_t field_bit_offset = 0;
8204 uint32_t field_byte_offset = 0;
8205 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8206 uint32_t child_idx = 0;
8207
8208 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8209 if (cxx_record_decl)
8210 {
8211 // We might have base classes to print out first
8212 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8213 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8214 base_class != base_class_end;
8215 ++base_class)
8216 {
8217 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8218
8219 // Skip empty base classes
8220 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8221 continue;
8222
8223 if (base_class->isVirtual())
8224 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8225 else
8226 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8227 field_byte_offset = field_bit_offset / 8;
8228 assert (field_bit_offset % 8 == 0);
8229 if (child_idx == 0)
8230 s->PutChar('{');
8231 else
8232 s->PutChar(',');
8233
8234 clang::QualType base_class_qual_type = base_class->getType();
8235 std::string base_class_type_name(base_class_qual_type.getAsString());
8236
8237 // Indent and print the base class type name
8238 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8239
8240 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8241
8242 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008243 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008244 base_clang_type.DumpValue (exe_ctx,
8245 s, // Stream to dump to
8246 base_clang_type.GetFormat(), // The format with which to display the member
8247 data, // Data buffer containing all bytes for this type
8248 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8249 base_class_type_info.Width / 8, // Size of this type in bytes
8250 0, // Bitfield bit size
8251 0, // Bitfield bit offset
8252 show_types, // Boolean indicating if we should show the variable types
8253 show_summary, // Boolean indicating if we should show a summary for the current type
8254 verbose, // Verbose output?
8255 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8256
8257 ++child_idx;
8258 }
8259 }
8260 uint32_t field_idx = 0;
8261 clang::RecordDecl::field_iterator field, field_end;
8262 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8263 {
8264 // Print the starting squiggly bracket (if this is the
8265 // first member) or comma (for member 2 and beyond) for
8266 // the struct/union/class member.
8267 if (child_idx == 0)
8268 s->PutChar('{');
8269 else
8270 s->PutChar(',');
8271
8272 // Indent
8273 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8274
8275 clang::QualType field_type = field->getType();
8276 // Print the member type if requested
8277 // Figure out the type byte size (field_type_info.first) and
8278 // alignment (field_type_info.second) from the AST context.
8279 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8280 assert(field_idx < record_layout.getFieldCount());
8281 // Figure out the field offset within the current struct/union/class type
8282 field_bit_offset = record_layout.getFieldOffset (field_idx);
8283 field_byte_offset = field_bit_offset / 8;
8284 uint32_t field_bitfield_bit_size = 0;
8285 uint32_t field_bitfield_bit_offset = 0;
8286 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8287 field_bitfield_bit_offset = field_bit_offset % 8;
8288
8289 if (show_types)
8290 {
8291 std::string field_type_name(field_type.getAsString());
8292 if (field_bitfield_bit_size > 0)
8293 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8294 else
8295 s->Printf("(%s) ", field_type_name.c_str());
8296 }
8297 // Print the member name and equal sign
8298 s->Printf("%s = ", field->getNameAsString().c_str());
8299
8300
8301 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008302 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008303 field_clang_type.DumpValue (exe_ctx,
8304 s, // Stream to dump to
8305 field_clang_type.GetFormat(), // The format with which to display the member
8306 data, // Data buffer containing all bytes for this type
8307 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8308 field_type_info.Width / 8, // Size of this type in bytes
8309 field_bitfield_bit_size, // Bitfield bit size
8310 field_bitfield_bit_offset, // Bitfield bit offset
8311 show_types, // Boolean indicating if we should show the variable types
8312 show_summary, // Boolean indicating if we should show a summary for the current type
8313 verbose, // Verbose output?
8314 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8315 }
8316
8317 // Indent the trailing squiggly bracket
8318 if (child_idx > 0)
8319 s->Printf("\n%*s}", depth, "");
8320 }
8321 return;
8322
8323 case clang::Type::Enum:
8324 if (GetCompleteType(type))
8325 {
8326 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8327 const clang::EnumDecl *enum_decl = enutype->getDecl();
8328 assert(enum_decl);
8329 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8330 lldb::offset_t offset = data_byte_offset;
8331 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8332 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8333 {
8334 if (enum_pos->getInitVal() == enum_value)
8335 {
8336 s->Printf("%s", enum_pos->getNameAsString().c_str());
8337 return;
8338 }
8339 }
8340 // If we have gotten here we didn't get find the enumerator in the
8341 // enum decl, so just print the integer.
8342 s->Printf("%" PRIi64, enum_value);
8343 }
8344 return;
8345
8346 case clang::Type::ConstantArray:
8347 {
8348 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8349 bool is_array_of_characters = false;
8350 clang::QualType element_qual_type = array->getElementType();
8351
8352 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8353 if (canonical_type)
8354 is_array_of_characters = canonical_type->isCharType();
8355
8356 const uint64_t element_count = array->getSize().getLimitedValue();
8357
8358 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8359
8360 uint32_t element_idx = 0;
8361 uint32_t element_offset = 0;
8362 uint64_t element_byte_size = field_type_info.Width / 8;
8363 uint32_t element_stride = element_byte_size;
8364
8365 if (is_array_of_characters)
8366 {
8367 s->PutChar('"');
8368 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8369 s->PutChar('"');
8370 return;
8371 }
8372 else
8373 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008374 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008375 lldb::Format element_format = element_clang_type.GetFormat();
8376
8377 for (element_idx = 0; element_idx < element_count; ++element_idx)
8378 {
8379 // Print the starting squiggly bracket (if this is the
8380 // first member) or comman (for member 2 and beyong) for
8381 // the struct/union/class member.
8382 if (element_idx == 0)
8383 s->PutChar('{');
8384 else
8385 s->PutChar(',');
8386
8387 // Indent and print the index
8388 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8389
8390 // Figure out the field offset within the current struct/union/class type
8391 element_offset = element_idx * element_stride;
8392
8393 // Dump the value of the member
8394 element_clang_type.DumpValue (exe_ctx,
8395 s, // Stream to dump to
8396 element_format, // The format with which to display the element
8397 data, // Data buffer containing all bytes for this type
8398 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8399 element_byte_size, // Size of this type in bytes
8400 0, // Bitfield bit size
8401 0, // Bitfield bit offset
8402 show_types, // Boolean indicating if we should show the variable types
8403 show_summary, // Boolean indicating if we should show a summary for the current type
8404 verbose, // Verbose output?
8405 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8406 }
8407
8408 // Indent the trailing squiggly bracket
8409 if (element_idx > 0)
8410 s->Printf("\n%*s}", depth, "");
8411 }
8412 }
8413 return;
8414
8415 case clang::Type::Typedef:
8416 {
8417 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8418
Greg Claytona1e5dc82015-08-11 22:53:00 +00008419 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008420 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8421 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8422 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8423
8424 return typedef_clang_type.DumpValue (exe_ctx,
8425 s, // Stream to dump to
8426 typedef_format, // The format with which to display the element
8427 data, // Data buffer containing all bytes for this type
8428 data_byte_offset, // Offset into "data" where to grab value from
8429 typedef_byte_size, // Size of this type in bytes
8430 bitfield_bit_size, // Bitfield bit size
8431 bitfield_bit_offset,// Bitfield bit offset
8432 show_types, // Boolean indicating if we should show the variable types
8433 show_summary, // Boolean indicating if we should show a summary for the current type
8434 verbose, // Verbose output?
8435 depth); // Scope depth for any types that have children
8436 }
8437 break;
8438
8439 case clang::Type::Elaborated:
8440 {
8441 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008442 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008443 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8444 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8445 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8446
8447 return elaborated_clang_type.DumpValue (exe_ctx,
8448 s, // Stream to dump to
8449 elaborated_format, // The format with which to display the element
8450 data, // Data buffer containing all bytes for this type
8451 data_byte_offset, // Offset into "data" where to grab value from
8452 elaborated_byte_size, // Size of this type in bytes
8453 bitfield_bit_size, // Bitfield bit size
8454 bitfield_bit_offset,// Bitfield bit offset
8455 show_types, // Boolean indicating if we should show the variable types
8456 show_summary, // Boolean indicating if we should show a summary for the current type
8457 verbose, // Verbose output?
8458 depth); // Scope depth for any types that have children
8459 }
8460 break;
8461
8462 case clang::Type::Paren:
8463 {
8464 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008465 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008466
8467 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8468 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8469 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8470
8471 return desugar_clang_type.DumpValue (exe_ctx,
8472 s, // Stream to dump to
8473 desugar_format, // The format with which to display the element
8474 data, // Data buffer containing all bytes for this type
8475 data_byte_offset, // Offset into "data" where to grab value from
8476 desugar_byte_size, // Size of this type in bytes
8477 bitfield_bit_size, // Bitfield bit size
8478 bitfield_bit_offset,// Bitfield bit offset
8479 show_types, // Boolean indicating if we should show the variable types
8480 show_summary, // Boolean indicating if we should show a summary for the current type
8481 verbose, // Verbose output?
8482 depth); // Scope depth for any types that have children
8483 }
8484 break;
8485
8486 default:
8487 // We are down to a scalar type that we just need to display.
8488 data.Dump(s,
8489 data_byte_offset,
8490 format,
8491 data_byte_size,
8492 1,
8493 UINT32_MAX,
8494 LLDB_INVALID_ADDRESS,
8495 bitfield_bit_size,
8496 bitfield_bit_offset);
8497
8498 if (show_summary)
8499 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8500 break;
8501 }
8502}
8503
8504
8505
8506
8507bool
8508ClangASTContext::DumpTypeValue (void* type, Stream *s,
8509 lldb::Format format,
8510 const lldb_private::DataExtractor &data,
8511 lldb::offset_t byte_offset,
8512 size_t byte_size,
8513 uint32_t bitfield_bit_size,
8514 uint32_t bitfield_bit_offset,
8515 ExecutionContextScope *exe_scope)
8516{
8517 if (!type)
8518 return false;
8519 if (IsAggregateType(type))
8520 {
8521 return false;
8522 }
8523 else
8524 {
8525 clang::QualType qual_type(GetQualType(type));
8526
8527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8528 switch (type_class)
8529 {
8530 case clang::Type::Typedef:
8531 {
8532 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008533 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008534 if (format == eFormatDefault)
8535 format = typedef_clang_type.GetFormat();
8536 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8537 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8538
8539 return typedef_clang_type.DumpTypeValue (s,
8540 format, // The format with which to display the element
8541 data, // Data buffer containing all bytes for this type
8542 byte_offset, // Offset into "data" where to grab value from
8543 typedef_byte_size, // Size of this type in bytes
8544 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8545 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8546 exe_scope);
8547 }
8548 break;
8549
8550 case clang::Type::Enum:
8551 // If our format is enum or default, show the enumeration value as
8552 // its enumeration string value, else just display it as requested.
8553 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8554 {
8555 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8556 const clang::EnumDecl *enum_decl = enutype->getDecl();
8557 assert(enum_decl);
8558 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8559 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8560 lldb::offset_t offset = byte_offset;
8561 if (is_signed)
8562 {
8563 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8564 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8565 {
8566 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8567 {
8568 s->PutCString (enum_pos->getNameAsString().c_str());
8569 return true;
8570 }
8571 }
8572 // If we have gotten here we didn't get find the enumerator in the
8573 // enum decl, so just print the integer.
8574 s->Printf("%" PRIi64, enum_svalue);
8575 }
8576 else
8577 {
8578 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8579 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8580 {
8581 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8582 {
8583 s->PutCString (enum_pos->getNameAsString().c_str());
8584 return true;
8585 }
8586 }
8587 // If we have gotten here we didn't get find the enumerator in the
8588 // enum decl, so just print the integer.
8589 s->Printf("%" PRIu64, enum_uvalue);
8590 }
8591 return true;
8592 }
8593 // format was not enum, just fall through and dump the value as requested....
8594
8595 default:
8596 // We are down to a scalar type that we just need to display.
8597 {
8598 uint32_t item_count = 1;
8599 // A few formats, we might need to modify our size and count for depending
8600 // on how we are trying to display the value...
8601 switch (format)
8602 {
8603 default:
8604 case eFormatBoolean:
8605 case eFormatBinary:
8606 case eFormatComplex:
8607 case eFormatCString: // NULL terminated C strings
8608 case eFormatDecimal:
8609 case eFormatEnum:
8610 case eFormatHex:
8611 case eFormatHexUppercase:
8612 case eFormatFloat:
8613 case eFormatOctal:
8614 case eFormatOSType:
8615 case eFormatUnsigned:
8616 case eFormatPointer:
8617 case eFormatVectorOfChar:
8618 case eFormatVectorOfSInt8:
8619 case eFormatVectorOfUInt8:
8620 case eFormatVectorOfSInt16:
8621 case eFormatVectorOfUInt16:
8622 case eFormatVectorOfSInt32:
8623 case eFormatVectorOfUInt32:
8624 case eFormatVectorOfSInt64:
8625 case eFormatVectorOfUInt64:
8626 case eFormatVectorOfFloat32:
8627 case eFormatVectorOfFloat64:
8628 case eFormatVectorOfUInt128:
8629 break;
8630
8631 case eFormatChar:
8632 case eFormatCharPrintable:
8633 case eFormatCharArray:
8634 case eFormatBytes:
8635 case eFormatBytesWithASCII:
8636 item_count = byte_size;
8637 byte_size = 1;
8638 break;
8639
8640 case eFormatUnicode16:
8641 item_count = byte_size / 2;
8642 byte_size = 2;
8643 break;
8644
8645 case eFormatUnicode32:
8646 item_count = byte_size / 4;
8647 byte_size = 4;
8648 break;
8649 }
8650 return data.Dump (s,
8651 byte_offset,
8652 format,
8653 byte_size,
8654 item_count,
8655 UINT32_MAX,
8656 LLDB_INVALID_ADDRESS,
8657 bitfield_bit_size,
8658 bitfield_bit_offset,
8659 exe_scope);
8660 }
8661 break;
8662 }
8663 }
8664 return 0;
8665}
8666
8667
8668
8669void
8670ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8671 Stream *s,
8672 const lldb_private::DataExtractor &data,
8673 lldb::offset_t data_byte_offset,
8674 size_t data_byte_size)
8675{
8676 uint32_t length = 0;
8677 if (IsCStringType (type, length))
8678 {
8679 if (exe_ctx)
8680 {
8681 Process *process = exe_ctx->GetProcessPtr();
8682 if (process)
8683 {
8684 lldb::offset_t offset = data_byte_offset;
8685 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8686 std::vector<uint8_t> buf;
8687 if (length > 0)
8688 buf.resize (length);
8689 else
8690 buf.resize (256);
8691
8692 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8693 buf.back() = '\0';
8694 size_t bytes_read;
8695 size_t total_cstr_len = 0;
8696 Error error;
8697 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8698 {
8699 const size_t len = strlen((const char *)&buf.front());
8700 if (len == 0)
8701 break;
8702 if (total_cstr_len == 0)
8703 s->PutCString (" \"");
8704 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8705 total_cstr_len += len;
8706 if (len < buf.size())
8707 break;
8708 pointer_address += total_cstr_len;
8709 }
8710 if (total_cstr_len > 0)
8711 s->PutChar ('"');
8712 }
8713 }
8714 }
8715}
8716
8717void
8718ClangASTContext::DumpTypeDescription (void* type)
8719{
8720 StreamFile s (stdout, false);
8721 DumpTypeDescription (&s);
8722 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8723 if (metadata)
8724 {
8725 metadata->Dump (&s);
8726 }
8727}
8728
8729void
8730ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8731{
8732 if (type)
8733 {
8734 clang::QualType qual_type(GetQualType(type));
8735
8736 llvm::SmallVector<char, 1024> buf;
8737 llvm::raw_svector_ostream llvm_ostrm (buf);
8738
8739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8740 switch (type_class)
8741 {
8742 case clang::Type::ObjCObject:
8743 case clang::Type::ObjCInterface:
8744 {
8745 GetCompleteType(type);
8746
8747 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8748 assert (objc_class_type);
8749 if (objc_class_type)
8750 {
8751 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8752 if (class_interface_decl)
8753 {
8754 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8755 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8756 }
8757 }
8758 }
8759 break;
8760
8761 case clang::Type::Typedef:
8762 {
8763 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8764 if (typedef_type)
8765 {
8766 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8767 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8768 if (!clang_typedef_name.empty())
8769 {
8770 s->PutCString ("typedef ");
8771 s->PutCString (clang_typedef_name.c_str());
8772 }
8773 }
8774 }
8775 break;
8776
8777 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008778 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008779 return;
8780
8781 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008782 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008783 return;
8784
8785 case clang::Type::Record:
8786 {
8787 GetCompleteType(type);
8788
8789 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8790 const clang::RecordDecl *record_decl = record_type->getDecl();
8791 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8792
8793 if (cxx_record_decl)
8794 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8795 else
8796 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8797 }
8798 break;
8799
8800 default:
8801 {
8802 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8803 if (tag_type)
8804 {
8805 clang::TagDecl *tag_decl = tag_type->getDecl();
8806 if (tag_decl)
8807 tag_decl->print(llvm_ostrm, 0);
8808 }
8809 else
8810 {
8811 std::string clang_type_name(qual_type.getAsString());
8812 if (!clang_type_name.empty())
8813 s->PutCString (clang_type_name.c_str());
8814 }
8815 }
8816 }
8817
Greg Claytond8d4a572015-08-11 21:38:15 +00008818 if (buf.size() > 0)
8819 {
8820 s->Write (buf.data(), buf.size());
8821 }
8822 }
8823}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008824
8825// DWARF parsing functions
8826#pragma mark DWARF Parsing
8827
8828#include "lldb/Core/Module.h"
8829#include "lldb/Symbol/CompileUnit.h"
8830#include "lldb/Symbol/Function.h"
8831#include "lldb/Symbol/ObjectFile.h"
8832#include "lldb/Symbol/TypeList.h"
8833
Oleksiy Vyalov52ae0232015-08-14 21:16:00 +00008834#include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
8835#include "Plugins/SymbolFile/DWARF/DWARFDebugInfo.h"
8836#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
8837#include "Plugins/SymbolFile/DWARF/DWARFDeclContext.h"
8838#include "Plugins/SymbolFile/DWARF/DWARFDefines.h"
8839#include "Plugins/SymbolFile/DWARF/DWARFDIECollection.h"
8840#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
8841#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
8842#include "Plugins/SymbolFile/DWARF/UniqueDWARFASTType.h"
Greg Clayton8b4edba2015-08-14 20:02:05 +00008843
8844
8845class ClangASTContext::DelayedAddObjCClassProperty
8846{
8847public:
8848 DelayedAddObjCClassProperty(const CompilerType &class_opaque_type,
8849 const char *property_name,
8850 const CompilerType &property_opaque_type, // The property type is only required if you don't have an ivar decl
8851 clang::ObjCIvarDecl *ivar_decl,
8852 const char *property_setter_name,
8853 const char *property_getter_name,
8854 uint32_t property_attributes,
8855 const ClangASTMetadata *metadata) :
8856 m_class_opaque_type (class_opaque_type),
8857 m_property_name (property_name),
8858 m_property_opaque_type (property_opaque_type),
8859 m_ivar_decl (ivar_decl),
8860 m_property_setter_name (property_setter_name),
8861 m_property_getter_name (property_getter_name),
8862 m_property_attributes (property_attributes)
8863 {
8864 if (metadata != NULL)
8865 {
8866 m_metadata_ap.reset(new ClangASTMetadata());
8867 *m_metadata_ap = *metadata;
8868 }
8869 }
8870
8871 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
8872 {
8873 *this = rhs;
8874 }
8875
8876 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
8877 {
8878 m_class_opaque_type = rhs.m_class_opaque_type;
8879 m_property_name = rhs.m_property_name;
8880 m_property_opaque_type = rhs.m_property_opaque_type;
8881 m_ivar_decl = rhs.m_ivar_decl;
8882 m_property_setter_name = rhs.m_property_setter_name;
8883 m_property_getter_name = rhs.m_property_getter_name;
8884 m_property_attributes = rhs.m_property_attributes;
8885
8886 if (rhs.m_metadata_ap.get())
8887 {
8888 m_metadata_ap.reset (new ClangASTMetadata());
8889 *m_metadata_ap = *rhs.m_metadata_ap;
8890 }
8891 return *this;
8892 }
8893
8894 bool
8895 Finalize()
8896 {
8897 ClangASTContext* ast = m_class_opaque_type.GetTypeSystem()->AsClangASTContext();
8898 assert(ast);
8899 return ast->AddObjCClassProperty (m_class_opaque_type,
8900 m_property_name,
8901 m_property_opaque_type,
8902 m_ivar_decl,
8903 m_property_setter_name,
8904 m_property_getter_name,
8905 m_property_attributes,
8906 m_metadata_ap.get());
8907 }
8908
8909private:
8910 CompilerType m_class_opaque_type;
8911 const char *m_property_name;
8912 CompilerType m_property_opaque_type;
8913 clang::ObjCIvarDecl *m_ivar_decl;
8914 const char *m_property_setter_name;
8915 const char *m_property_getter_name;
8916 uint32_t m_property_attributes;
8917 std::unique_ptr<ClangASTMetadata> m_metadata_ap;
8918};
8919
8920bool
8921ClangASTContext::ParseTemplateDIE (SymbolFileDWARF *dwarf,
8922 DWARFCompileUnit* dwarf_cu,
8923 const DWARFDebugInfoEntry *die,
8924 ClangASTContext::TemplateParameterInfos &template_param_infos)
8925{
8926 const dw_tag_t tag = die->Tag();
8927
8928 switch (tag)
8929 {
8930 case DW_TAG_template_type_parameter:
8931 case DW_TAG_template_value_parameter:
8932 {
8933 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
8934
8935 DWARFDebugInfoEntry::Attributes attributes;
8936 const size_t num_attributes = die->GetAttributes (dwarf,
8937 dwarf_cu,
8938 fixed_form_sizes,
8939 attributes);
8940 const char *name = NULL;
8941 Type *lldb_type = NULL;
8942 CompilerType clang_type;
8943 uint64_t uval64 = 0;
8944 bool uval64_valid = false;
8945 if (num_attributes > 0)
8946 {
8947 DWARFFormValue form_value;
8948 for (size_t i=0; i<num_attributes; ++i)
8949 {
8950 const dw_attr_t attr = attributes.AttributeAtIndex(i);
8951
8952 switch (attr)
8953 {
8954 case DW_AT_name:
8955 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8956 name = form_value.AsCString(&dwarf->get_debug_str_data());
8957 break;
8958
8959 case DW_AT_type:
8960 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8961 {
8962 const dw_offset_t type_die_offset = form_value.Reference();
8963 lldb_type = dwarf->ResolveTypeUID(type_die_offset);
8964 if (lldb_type)
8965 clang_type = lldb_type->GetClangForwardType();
8966 }
8967 break;
8968
8969 case DW_AT_const_value:
8970 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
8971 {
8972 uval64_valid = true;
8973 uval64 = form_value.Unsigned();
8974 }
8975 break;
8976 default:
8977 break;
8978 }
8979 }
8980
8981 clang::ASTContext *ast = getASTContext();
8982 if (!clang_type)
8983 clang_type = GetBasicType(eBasicTypeVoid);
8984
8985 if (clang_type)
8986 {
8987 bool is_signed = false;
8988 if (name && name[0])
8989 template_param_infos.names.push_back(name);
8990 else
8991 template_param_infos.names.push_back(NULL);
8992
8993 if (tag == DW_TAG_template_value_parameter &&
8994 lldb_type != NULL &&
8995 clang_type.IsIntegerType (is_signed) &&
8996 uval64_valid)
8997 {
8998 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
8999 template_param_infos.args.push_back (clang::TemplateArgument (*ast,
9000 llvm::APSInt(apint),
9001 ClangASTContext::GetQualType(clang_type)));
9002 }
9003 else
9004 {
9005 template_param_infos.args.push_back (clang::TemplateArgument (ClangASTContext::GetQualType(clang_type)));
9006 }
9007 }
9008 else
9009 {
9010 return false;
9011 }
9012
9013 }
9014 }
9015 return true;
9016
9017 default:
9018 break;
9019 }
9020 return false;
9021}
9022
9023bool
9024ClangASTContext::ParseTemplateParameterInfos (SymbolFileDWARF *dwarf,
9025 DWARFCompileUnit* dwarf_cu,
9026 const DWARFDebugInfoEntry *parent_die,
9027 ClangASTContext::TemplateParameterInfos &template_param_infos)
9028{
9029
9030 if (parent_die == NULL)
9031 return false;
9032
9033 Args template_parameter_names;
9034 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
9035 die != NULL;
9036 die = die->GetSibling())
9037 {
9038 const dw_tag_t tag = die->Tag();
9039
9040 switch (tag)
9041 {
9042 case DW_TAG_template_type_parameter:
9043 case DW_TAG_template_value_parameter:
9044 ParseTemplateDIE (dwarf, dwarf_cu, die, template_param_infos);
9045 break;
9046
9047 default:
9048 break;
9049 }
9050 }
9051 if (template_param_infos.args.empty())
9052 return false;
9053 return template_param_infos.args.size() == template_param_infos.names.size();
9054}
9055
9056clang::ClassTemplateDecl *
9057ClangASTContext::ParseClassTemplateDecl (SymbolFileDWARF *dwarf,
9058 clang::DeclContext *decl_ctx,
9059 lldb::AccessType access_type,
9060 const char *parent_name,
9061 int tag_decl_kind,
9062 const ClangASTContext::TemplateParameterInfos &template_param_infos)
9063{
9064 if (template_param_infos.IsValid())
9065 {
9066 std::string template_basename(parent_name);
9067 template_basename.erase (template_basename.find('<'));
9068
9069 return CreateClassTemplateDecl (decl_ctx,
9070 access_type,
9071 template_basename.c_str(),
9072 tag_decl_kind,
9073 template_param_infos);
9074 }
9075 return NULL;
9076}
9077
9078bool
9079ClangASTContext::ResolveClangOpaqueTypeDefinition (SymbolFileDWARF *dwarf,
9080 DWARFCompileUnit *dwarf_cu,
9081 const DWARFDebugInfoEntry* die,
Yaron Kerenfe16dea2015-08-16 19:40:40 +00009082 lldb_private::Type *type,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009083 CompilerType &clang_type)
9084{
9085 // Disable external storage for this type so we don't get anymore
9086 // clang::ExternalASTSource queries for this type.
9087 SetHasExternalStorage (clang_type.GetOpaqueQualType(), false);
9088
9089 if (dwarf == nullptr || dwarf_cu == nullptr || die == nullptr)
9090 return false;
9091
9092 const dw_tag_t tag = die->Tag();
9093
9094 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
9095 if (log)
9096 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
9097 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
9098 dwarf->MakeUserID(die->GetOffset()),
9099 DW_TAG_value_to_name(tag),
9100 type->GetName().AsCString());
9101 assert (clang_type);
9102 DWARFDebugInfoEntry::Attributes attributes;
9103
9104 switch (tag)
9105 {
9106 case DW_TAG_structure_type:
9107 case DW_TAG_union_type:
9108 case DW_TAG_class_type:
9109 {
9110 LayoutInfo layout_info;
9111
9112 {
9113 if (die->HasChildren())
9114 {
9115 LanguageType class_language = eLanguageTypeUnknown;
9116 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type))
9117 {
9118 class_language = eLanguageTypeObjC;
9119 // For objective C we don't start the definition when
9120 // the class is created.
9121 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9122 }
9123
9124 int tag_decl_kind = -1;
9125 AccessType default_accessibility = eAccessNone;
9126 if (tag == DW_TAG_structure_type)
9127 {
9128 tag_decl_kind = clang::TTK_Struct;
9129 default_accessibility = eAccessPublic;
9130 }
9131 else if (tag == DW_TAG_union_type)
9132 {
9133 tag_decl_kind = clang::TTK_Union;
9134 default_accessibility = eAccessPublic;
9135 }
9136 else if (tag == DW_TAG_class_type)
9137 {
9138 tag_decl_kind = clang::TTK_Class;
9139 default_accessibility = eAccessPrivate;
9140 }
9141
9142 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9143 std::vector<clang::CXXBaseSpecifier *> base_classes;
9144 std::vector<int> member_accessibilities;
9145 bool is_a_class = false;
9146 // Parse members and base classes first
9147 DWARFDIECollection member_function_dies;
9148
9149 DelayedPropertyList delayed_properties;
9150 ParseChildMembers (sc,
9151 dwarf,
9152 dwarf_cu,
9153 die,
9154 clang_type,
9155 class_language,
9156 base_classes,
9157 member_accessibilities,
9158 member_function_dies,
9159 delayed_properties,
9160 default_accessibility,
9161 is_a_class,
9162 layout_info);
9163
9164 // Now parse any methods if there were any...
9165 size_t num_functions = member_function_dies.Size();
9166 if (num_functions > 0)
9167 {
9168 for (size_t i=0; i<num_functions; ++i)
9169 {
9170 dwarf->ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
9171 }
9172 }
9173
9174 if (class_language == eLanguageTypeObjC)
9175 {
9176 ConstString class_name (clang_type.GetTypeName());
9177 if (class_name)
9178 {
9179 DIEArray method_die_offsets;
9180 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
9181
9182 if (!method_die_offsets.empty())
9183 {
9184 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
9185
9186 DWARFCompileUnit* method_cu = NULL;
9187 const size_t num_matches = method_die_offsets.size();
9188 for (size_t i=0; i<num_matches; ++i)
9189 {
9190 const dw_offset_t die_offset = method_die_offsets[i];
9191 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
9192
9193 if (method_die)
9194 dwarf->ResolveType (method_cu, method_die);
9195 }
9196 }
9197
9198 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
9199 pi != pe;
9200 ++pi)
9201 pi->Finalize();
9202 }
9203 }
9204
9205 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
9206 // need to tell the clang type it is actually a class.
9207 if (class_language != eLanguageTypeObjC)
9208 {
9209 if (is_a_class && tag_decl_kind != clang::TTK_Class)
9210 SetTagTypeKind (ClangASTContext::GetQualType(clang_type), clang::TTK_Class);
9211 }
9212
9213 // Since DW_TAG_structure_type gets used for both classes
9214 // and structures, we may need to set any DW_TAG_member
9215 // fields to have a "private" access if none was specified.
9216 // When we parsed the child members we tracked that actual
9217 // accessibility value for each DW_TAG_member in the
9218 // "member_accessibilities" array. If the value for the
9219 // member is zero, then it was set to the "default_accessibility"
9220 // which for structs was "public". Below we correct this
9221 // by setting any fields to "private" that weren't correctly
9222 // set.
9223 if (is_a_class && !member_accessibilities.empty())
9224 {
9225 // This is a class and all members that didn't have
9226 // their access specified are private.
9227 SetDefaultAccessForRecordFields (GetAsRecordDecl(clang_type),
9228 eAccessPrivate,
9229 &member_accessibilities.front(),
9230 member_accessibilities.size());
9231 }
9232
9233 if (!base_classes.empty())
9234 {
9235 // Make sure all base classes refer to complete types and not
9236 // forward declarations. If we don't do this, clang will crash
9237 // with an assertion in the call to clang_type.SetBaseClassesForClassType()
9238 bool base_class_error = false;
9239 for (auto &base_class : base_classes)
9240 {
9241 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
9242 if (type_source_info)
9243 {
9244 CompilerType base_class_type (this, type_source_info->getType().getAsOpaquePtr());
9245 if (base_class_type.GetCompleteType() == false)
9246 {
9247 if (!base_class_error)
9248 {
9249 dwarf->GetObjectFile()->GetModule()->ReportError ("DWARF DIE at 0x%8.8x for class '%s' has a base class '%s' that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
9250 die->GetOffset(),
9251 die->GetName(dwarf, dwarf_cu),
9252 base_class_type.GetTypeName().GetCString(),
9253 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
9254 }
9255 // We have no choice other than to pretend that the base class
9256 // is complete. If we don't do this, clang will crash when we
9257 // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
9258 // below. Since we provide layout assistance, all ivars in this
9259 // class and other classes will be fine, this is the best we can do
9260 // short of crashing.
9261
9262 ClangASTContext::StartTagDeclarationDefinition (base_class_type);
9263 ClangASTContext::CompleteTagDeclarationDefinition (base_class_type);
9264 }
9265 }
9266 }
9267 SetBaseClassesForClassType (clang_type.GetOpaqueQualType(),
9268 &base_classes.front(),
9269 base_classes.size());
9270
9271 // Clang will copy each CXXBaseSpecifier in "base_classes"
9272 // so we have to free them all.
9273 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
9274 base_classes.size());
9275 }
9276 }
9277 }
9278
9279 ClangASTContext::BuildIndirectFields (clang_type);
9280 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9281
9282 if (!layout_info.field_offsets.empty() ||
9283 !layout_info.base_offsets.empty() ||
9284 !layout_info.vbase_offsets.empty() )
9285 {
9286 if (type)
9287 layout_info.bit_size = type->GetByteSize() * 8;
9288 if (layout_info.bit_size == 0)
9289 layout_info.bit_size = die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, 0) * 8;
9290
9291 clang::CXXRecordDecl *record_decl = GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
9292 if (record_decl)
9293 {
9294 if (log)
9295 {
9296 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9297
9298 if (module_sp)
9299 {
9300 module_sp->LogMessage (log,
9301 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
9302 static_cast<void*>(clang_type.GetOpaqueQualType()),
9303 static_cast<void*>(record_decl),
9304 layout_info.bit_size,
9305 layout_info.alignment,
9306 static_cast<uint32_t>(layout_info.field_offsets.size()),
9307 static_cast<uint32_t>(layout_info.base_offsets.size()),
9308 static_cast<uint32_t>(layout_info.vbase_offsets.size()));
9309
9310 uint32_t idx;
9311 {
9312 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
9313 end = layout_info.field_offsets.end();
9314 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
9315 {
9316 module_sp->LogMessage(log,
9317 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
9318 static_cast<void *>(clang_type.GetOpaqueQualType()),
9319 idx,
9320 static_cast<uint32_t>(pos->second),
9321 pos->first->getNameAsString().c_str());
9322 }
9323 }
9324
9325 {
9326 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
9327 base_end = layout_info.base_offsets.end();
9328 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
9329 {
9330 module_sp->LogMessage(log,
9331 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
9332 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
9333 base_pos->first->getNameAsString().c_str());
9334 }
9335 }
9336 {
9337 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
9338 vbase_end = layout_info.vbase_offsets.end();
9339 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
9340 {
9341 module_sp->LogMessage(log,
9342 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
9343 static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
9344 static_cast<uint32_t>(vbase_pos->second.getQuantity()),
9345 vbase_pos->first->getNameAsString().c_str());
9346 }
9347 }
9348
9349 }
9350 }
9351 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
9352 }
9353 }
9354 }
9355
9356 return (bool)clang_type;
9357
9358 case DW_TAG_enumeration_type:
9359 ClangASTContext::StartTagDeclarationDefinition (clang_type);
9360 if (die->HasChildren())
9361 {
9362 SymbolContext sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
9363 bool is_signed = false;
9364 clang_type.IsIntegerType(is_signed);
9365 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf, dwarf_cu, die);
9366 }
9367 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
9368 return (bool)clang_type;
9369
9370 default:
9371 assert(false && "not a forward clang type decl!");
9372 break;
9373 }
9374
9375 return false;
9376}
9377
9378
9379bool
9380ClangASTContext::LayoutRecordType(SymbolFileDWARF *dwarf,
9381 const clang::RecordDecl *record_decl,
9382 uint64_t &bit_size,
9383 uint64_t &alignment,
9384 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9385 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9386 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9387{
9388 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
9389 bool success = false;
9390 base_offsets.clear();
9391 vbase_offsets.clear();
9392 if (pos != m_record_decl_to_layout_map.end())
9393 {
9394 bit_size = pos->second.bit_size;
9395 alignment = pos->second.alignment;
9396 field_offsets.swap(pos->second.field_offsets);
9397 base_offsets.swap (pos->second.base_offsets);
9398 vbase_offsets.swap (pos->second.vbase_offsets);
9399 m_record_decl_to_layout_map.erase(pos);
9400 success = true;
9401 }
9402 else
9403 {
9404 bit_size = 0;
9405 alignment = 0;
9406 field_offsets.clear();
9407 }
9408 return success;
9409}
9410
9411
9412size_t
9413ClangASTContext::ParseChildEnumerators (const SymbolContext& sc,
9414 lldb_private::CompilerType &clang_type,
9415 bool is_signed,
9416 uint32_t enumerator_byte_size,
9417 SymbolFileDWARF *dwarf,
9418 DWARFCompileUnit* dwarf_cu,
9419 const DWARFDebugInfoEntry *parent_die)
9420{
9421 if (parent_die == NULL)
9422 return 0;
9423
9424 size_t enumerators_added = 0;
9425 const DWARFDebugInfoEntry *die;
9426 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9427
9428 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9429 {
9430 const dw_tag_t tag = die->Tag();
9431 if (tag == DW_TAG_enumerator)
9432 {
9433 DWARFDebugInfoEntry::Attributes attributes;
9434 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
9435 if (num_child_attributes > 0)
9436 {
9437 const char *name = NULL;
9438 bool got_value = false;
9439 int64_t enum_value = 0;
9440 Declaration decl;
9441
9442 uint32_t i;
9443 for (i=0; i<num_child_attributes; ++i)
9444 {
9445 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9446 DWARFFormValue form_value;
9447 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9448 {
9449 switch (attr)
9450 {
9451 case DW_AT_const_value:
9452 got_value = true;
9453 if (is_signed)
9454 enum_value = form_value.Signed();
9455 else
9456 enum_value = form_value.Unsigned();
9457 break;
9458
9459 case DW_AT_name:
9460 name = form_value.AsCString(&dwarf->get_debug_str_data());
9461 break;
9462
9463 case DW_AT_description:
9464 default:
9465 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9466 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9467 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9468 case DW_AT_sibling:
9469 break;
9470 }
9471 }
9472 }
9473
9474 if (name && name[0] && got_value)
9475 {
9476 AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(),
9477 GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
9478 decl,
9479 name,
9480 enum_value,
9481 enumerator_byte_size * 8);
9482 ++enumerators_added;
9483 }
9484 }
9485 }
9486 }
9487 return enumerators_added;
9488}
9489
9490#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
9491
9492class DIEStack
9493{
9494public:
9495
9496 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9497 {
9498 m_dies.push_back (DIEInfo(cu, die));
9499 }
9500
9501
9502 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
9503 {
9504 StreamString log_strm;
9505 const size_t n = m_dies.size();
9506 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
9507 for (size_t i=0; i<n; i++)
9508 {
9509 DWARFCompileUnit *cu = m_dies[i].cu;
9510 const DWARFDebugInfoEntry *die = m_dies[i].die;
9511 std::string qualified_name;
9512 die->GetQualifiedName(dwarf, cu, qualified_name);
9513 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
9514 (uint64_t)i,
9515 die->GetOffset(),
9516 DW_TAG_value_to_name(die->Tag()),
9517 qualified_name.c_str());
9518 }
9519 log->PutCString(log_strm.GetData());
9520 }
9521 void Pop ()
9522 {
9523 m_dies.pop_back();
9524 }
9525
9526 class ScopedPopper
9527 {
9528 public:
9529 ScopedPopper (DIEStack &die_stack) :
9530 m_die_stack (die_stack),
9531 m_valid (false)
9532 {
9533 }
9534
9535 void
9536 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
9537 {
9538 m_valid = true;
9539 m_die_stack.Push (cu, die);
9540 }
9541
9542 ~ScopedPopper ()
9543 {
9544 if (m_valid)
9545 m_die_stack.Pop();
9546 }
9547
9548
9549
9550 protected:
9551 DIEStack &m_die_stack;
9552 bool m_valid;
9553 };
9554
9555protected:
9556 struct DIEInfo {
9557 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
9558 cu(c),
9559 die(d)
9560 {
9561 }
9562 DWARFCompileUnit *cu;
9563 const DWARFDebugInfoEntry *die;
9564 };
9565 typedef std::vector<DIEInfo> Stack;
9566 Stack m_dies;
9567};
9568#endif
9569
9570
9571
9572static AccessType
9573DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
9574{
9575 switch (dwarf_accessibility)
9576 {
9577 case DW_ACCESS_public: return eAccessPublic;
9578 case DW_ACCESS_private: return eAccessPrivate;
9579 case DW_ACCESS_protected: return eAccessProtected;
9580 default: break;
9581 }
9582 return eAccessNone;
9583}
9584
9585static bool
9586DeclKindIsCXXClass (clang::Decl::Kind decl_kind)
9587{
9588 switch (decl_kind)
9589 {
9590 case clang::Decl::CXXRecord:
9591 case clang::Decl::ClassTemplateSpecialization:
9592 return true;
9593 default:
9594 break;
9595 }
9596 return false;
9597}
9598
9599struct BitfieldInfo
9600{
9601 uint64_t bit_size;
9602 uint64_t bit_offset;
9603
9604 BitfieldInfo () :
9605 bit_size (LLDB_INVALID_ADDRESS),
9606 bit_offset (LLDB_INVALID_ADDRESS)
9607 {
9608 }
9609
9610 void
9611 Clear()
9612 {
9613 bit_size = LLDB_INVALID_ADDRESS;
9614 bit_offset = LLDB_INVALID_ADDRESS;
9615 }
9616
9617 bool IsValid ()
9618 {
9619 return (bit_size != LLDB_INVALID_ADDRESS) &&
9620 (bit_offset != LLDB_INVALID_ADDRESS);
9621 }
9622};
9623
9624Function *
9625ClangASTContext::ParseFunctionFromDWARF (const SymbolContext& sc,
9626 SymbolFileDWARF *dwarf,
9627 DWARFCompileUnit* dwarf_cu,
9628 const DWARFDebugInfoEntry *die)
9629{
9630 DWARFDebugRanges::RangeList func_ranges;
9631 const char *name = NULL;
9632 const char *mangled = NULL;
9633 int decl_file = 0;
9634 int decl_line = 0;
9635 int decl_column = 0;
9636 int call_file = 0;
9637 int call_line = 0;
9638 int call_column = 0;
9639 DWARFExpression frame_base;
9640
9641 assert (die->Tag() == DW_TAG_subprogram);
9642
9643 if (die->Tag() != DW_TAG_subprogram)
9644 return NULL;
9645
9646 if (die->GetDIENamesAndRanges (dwarf,
9647 dwarf_cu,
9648 name,
9649 mangled,
9650 func_ranges,
9651 decl_file,
9652 decl_line,
9653 decl_column,
9654 call_file,
9655 call_line,
9656 call_column,
9657 &frame_base))
9658 {
9659 // Union of all ranges in the function DIE (if the function is discontiguous)
9660 AddressRange func_range;
9661 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
9662 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
9663 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
9664 {
9665 ModuleSP module_sp (dwarf->GetObjectFile()->GetModule());
9666 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
9667 if (func_range.GetBaseAddress().IsValid())
9668 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
9669 }
9670
9671 if (func_range.GetBaseAddress().IsValid())
9672 {
9673 Mangled func_name;
9674 if (mangled)
9675 func_name.SetValue(ConstString(mangled), true);
9676 else if (die->GetParent()->Tag() == DW_TAG_compile_unit &&
9677 LanguageRuntime::LanguageIsCPlusPlus(dwarf_cu->GetLanguageType()) &&
9678 name && strcmp(name, "main") != 0)
9679 {
9680 // If the mangled name is not present in the DWARF, generate the demangled name
9681 // using the decl context. We skip if the function is "main" as its name is
9682 // never mangled.
9683 bool is_static = false;
9684 bool is_variadic = false;
9685 unsigned type_quals = 0;
9686 std::vector<CompilerType> param_types;
9687 std::vector<clang::ParmVarDecl*> param_decls;
9688 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
9689 DWARFDeclContext decl_ctx;
9690 StreamString sstr;
9691
9692 die->GetDWARFDeclContext(dwarf, dwarf_cu, decl_ctx);
9693 sstr << decl_ctx.GetQualifiedName();
9694
9695 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(dwarf,
9696 dwarf_cu,
9697 die,
9698 &decl_ctx_die);
9699 ParseChildParameters(sc,
9700 containing_decl_ctx,
9701 dwarf,
9702 dwarf_cu,
9703 die,
9704 true,
9705 is_static,
9706 is_variadic,
9707 param_types,
9708 param_decls,
9709 type_quals);
9710 sstr << "(";
9711 for (size_t i = 0; i < param_types.size(); i++)
9712 {
9713 if (i > 0)
9714 sstr << ", ";
9715 sstr << param_types[i].GetTypeName();
9716 }
9717 if (is_variadic)
9718 sstr << ", ...";
9719 sstr << ")";
9720 if (type_quals & clang::Qualifiers::Const)
9721 sstr << " const";
9722
9723 func_name.SetValue(ConstString(sstr.GetData()), false);
9724 }
9725 else
9726 func_name.SetValue(ConstString(name), false);
9727
9728 FunctionSP func_sp;
9729 std::unique_ptr<Declaration> decl_ap;
9730 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
9731 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
9732 decl_line,
9733 decl_column));
9734
9735 // Supply the type _only_ if it has already been parsed
9736 Type *func_type = dwarf->m_die_to_type.lookup (die);
9737
9738 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
9739
9740 if (dwarf->FixupAddress (func_range.GetBaseAddress()))
9741 {
9742 const user_id_t func_user_id = dwarf->MakeUserID(die->GetOffset());
9743 func_sp.reset(new Function (sc.comp_unit,
9744 dwarf->MakeUserID(func_user_id), // UserID is the DIE offset
9745 dwarf->MakeUserID(func_user_id),
9746 func_name,
9747 func_type,
9748 func_range)); // first address range
9749
9750 if (func_sp.get() != NULL)
9751 {
9752 if (frame_base.IsValid())
9753 func_sp->GetFrameBaseExpression() = frame_base;
9754 sc.comp_unit->AddFunction(func_sp);
9755 return func_sp.get();
9756 }
9757 }
9758 }
9759 }
9760 return NULL;
9761}
9762
9763
9764size_t
9765ClangASTContext::ParseChildMembers (const SymbolContext& sc,
9766 SymbolFileDWARF *dwarf,
9767 DWARFCompileUnit* dwarf_cu,
9768 const DWARFDebugInfoEntry *parent_die,
9769 CompilerType &class_clang_type,
9770 const LanguageType class_language,
9771 std::vector<clang::CXXBaseSpecifier *>& base_classes,
9772 std::vector<int>& member_accessibilities,
9773 DWARFDIECollection& member_function_dies,
9774 DelayedPropertyList& delayed_properties,
9775 AccessType& default_accessibility,
9776 bool &is_a_class,
9777 LayoutInfo &layout_info)
9778{
9779 if (parent_die == NULL)
9780 return 0;
9781
9782 size_t count = 0;
9783 const DWARFDebugInfoEntry *die;
9784 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
9785 uint32_t member_idx = 0;
9786 BitfieldInfo last_field_info;
9787 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
9788 ClangASTContext* ast = class_clang_type.GetTypeSystem()->AsClangASTContext();
9789 if (ast == nullptr)
9790 return 0;
9791
9792 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
9793 {
9794 dw_tag_t tag = die->Tag();
9795
9796 switch (tag)
9797 {
9798 case DW_TAG_member:
9799 case DW_TAG_APPLE_property:
9800 {
9801 DWARFDebugInfoEntry::Attributes attributes;
9802 const size_t num_attributes = die->GetAttributes (dwarf,
9803 dwarf_cu,
9804 fixed_form_sizes,
9805 attributes);
9806 if (num_attributes > 0)
9807 {
9808 Declaration decl;
9809 //DWARFExpression location;
9810 const char *name = NULL;
9811 const char *prop_name = NULL;
9812 const char *prop_getter_name = NULL;
9813 const char *prop_setter_name = NULL;
9814 uint32_t prop_attributes = 0;
9815
9816
9817 bool is_artificial = false;
9818 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
9819 AccessType accessibility = eAccessNone;
9820 uint32_t member_byte_offset = UINT32_MAX;
9821 size_t byte_size = 0;
9822 size_t bit_offset = 0;
9823 size_t bit_size = 0;
9824 bool is_external = false; // On DW_TAG_members, this means the member is static
9825 uint32_t i;
9826 for (i=0; i<num_attributes && !is_artificial; ++i)
9827 {
9828 const dw_attr_t attr = attributes.AttributeAtIndex(i);
9829 DWARFFormValue form_value;
9830 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
9831 {
9832 switch (attr)
9833 {
9834 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
9835 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
9836 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
9837 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9838 case DW_AT_type: encoding_uid = form_value.Reference(); break;
9839 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
9840 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
9841 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
9842 case DW_AT_data_member_location:
9843 if (form_value.BlockData())
9844 {
9845 Value initialValue(0);
9846 Value memberOffset(0);
9847 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
9848 uint32_t block_length = form_value.Unsigned();
9849 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
9850 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
9851 NULL, // ClangExpressionVariableList *
9852 NULL, // ClangExpressionDeclMap *
9853 NULL, // RegisterContext *
9854 module_sp,
9855 debug_info_data,
9856 block_offset,
9857 block_length,
9858 eRegisterKindDWARF,
9859 &initialValue,
9860 memberOffset,
9861 NULL))
9862 {
9863 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
9864 }
9865 }
9866 else
9867 {
9868 // With DWARF 3 and later, if the value is an integer constant,
9869 // this form value is the offset in bytes from the beginning
9870 // of the containing entity.
9871 member_byte_offset = form_value.Unsigned();
9872 }
9873 break;
9874
9875 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
9876 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
9877 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9878 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9879 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
9880 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
9881 case DW_AT_external: is_external = form_value.Boolean(); break;
9882
9883 default:
9884 case DW_AT_declaration:
9885 case DW_AT_description:
9886 case DW_AT_mutable:
9887 case DW_AT_visibility:
9888 case DW_AT_sibling:
9889 break;
9890 }
9891 }
9892 }
9893
9894 if (prop_name)
9895 {
9896 ConstString fixed_getter;
9897 ConstString fixed_setter;
9898
9899 // Check if the property getter/setter were provided as full
9900 // names. We want basenames, so we extract them.
9901
9902 if (prop_getter_name && prop_getter_name[0] == '-')
9903 {
9904 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true);
9905 prop_getter_name = prop_getter_method.GetSelector().GetCString();
9906 }
9907
9908 if (prop_setter_name && prop_setter_name[0] == '-')
9909 {
9910 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true);
9911 prop_setter_name = prop_setter_method.GetSelector().GetCString();
9912 }
9913
9914 // If the names haven't been provided, they need to be
9915 // filled in.
9916
9917 if (!prop_getter_name)
9918 {
9919 prop_getter_name = prop_name;
9920 }
9921 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
9922 {
9923 StreamString ss;
9924
9925 ss.Printf("set%c%s:",
9926 toupper(prop_name[0]),
9927 &prop_name[1]);
9928
9929 fixed_setter.SetCString(ss.GetData());
9930 prop_setter_name = fixed_setter.GetCString();
9931 }
9932 }
9933
9934 // Clang has a DWARF generation bug where sometimes it
9935 // represents fields that are references with bad byte size
9936 // and bit size/offset information such as:
9937 //
9938 // DW_AT_byte_size( 0x00 )
9939 // DW_AT_bit_size( 0x40 )
9940 // DW_AT_bit_offset( 0xffffffffffffffc0 )
9941 //
9942 // So check the bit offset to make sure it is sane, and if
9943 // the values are not sane, remove them. If we don't do this
9944 // then we will end up with a crash if we try to use this
9945 // type in an expression when clang becomes unhappy with its
9946 // recycled debug info.
9947
9948 if (bit_offset > 128)
9949 {
9950 bit_size = 0;
9951 bit_offset = 0;
9952 }
9953
9954 // FIXME: Make Clang ignore Objective-C accessibility for expressions
9955 if (class_language == eLanguageTypeObjC ||
9956 class_language == eLanguageTypeObjC_plus_plus)
9957 accessibility = eAccessNone;
9958
9959 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
9960 {
9961 // Not all compilers will mark the vtable pointer
9962 // member as artificial (llvm-gcc). We can't have
9963 // the virtual members in our classes otherwise it
9964 // throws off all child offsets since we end up
9965 // having and extra pointer sized member in our
9966 // class layouts.
9967 is_artificial = true;
9968 }
9969
9970 // Handle static members
9971 if (is_external && member_byte_offset == UINT32_MAX)
9972 {
9973 Type *var_type = dwarf->ResolveTypeUID(encoding_uid);
9974
9975 if (var_type)
9976 {
9977 if (accessibility == eAccessNone)
9978 accessibility = eAccessPublic;
9979 ClangASTContext::AddVariableToRecordType (class_clang_type,
9980 name,
9981 var_type->GetClangLayoutType(),
9982 accessibility);
9983 }
9984 break;
9985 }
9986
9987 if (is_artificial == false)
9988 {
9989 Type *member_type = dwarf->ResolveTypeUID(encoding_uid);
9990
9991 clang::FieldDecl *field_decl = NULL;
9992 if (tag == DW_TAG_member)
9993 {
9994 if (member_type)
9995 {
9996 if (accessibility == eAccessNone)
9997 accessibility = default_accessibility;
9998 member_accessibilities.push_back(accessibility);
9999
10000 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
10001 if (bit_size > 0)
10002 {
10003
10004 BitfieldInfo this_field_info;
10005 this_field_info.bit_offset = field_bit_offset;
10006 this_field_info.bit_size = bit_size;
10007
10008 /////////////////////////////////////////////////////////////
10009 // How to locate a field given the DWARF debug information
10010 //
10011 // AT_byte_size indicates the size of the word in which the
10012 // bit offset must be interpreted.
10013 //
10014 // AT_data_member_location indicates the byte offset of the
10015 // word from the base address of the structure.
10016 //
10017 // AT_bit_offset indicates how many bits into the word
10018 // (according to the host endianness) the low-order bit of
10019 // the field starts. AT_bit_offset can be negative.
10020 //
10021 // AT_bit_size indicates the size of the field in bits.
10022 /////////////////////////////////////////////////////////////
10023
10024 if (byte_size == 0)
10025 byte_size = member_type->GetByteSize();
10026
10027 if (dwarf->GetObjectFile()->GetByteOrder() == eByteOrderLittle)
10028 {
10029 this_field_info.bit_offset += byte_size * 8;
10030 this_field_info.bit_offset -= (bit_offset + bit_size);
10031 }
10032 else
10033 {
10034 this_field_info.bit_offset += bit_offset;
10035 }
10036
10037 // Update the field bit offset we will report for layout
10038 field_bit_offset = this_field_info.bit_offset;
10039
10040 // If the member to be emitted did not start on a character boundary and there is
10041 // empty space between the last field and this one, then we need to emit an
10042 // anonymous member filling up the space up to its start. There are three cases
10043 // here:
10044 //
10045 // 1 If the previous member ended on a character boundary, then we can emit an
10046 // anonymous member starting at the most recent character boundary.
10047 //
10048 // 2 If the previous member did not end on a character boundary and the distance
10049 // from the end of the previous member to the current member is less than a
10050 // word width, then we can emit an anonymous member starting right after the
10051 // previous member and right before this member.
10052 //
10053 // 3 If the previous member did not end on a character boundary and the distance
10054 // from the end of the previous member to the current member is greater than
10055 // or equal a word width, then we act as in Case 1.
10056
10057 const uint64_t character_width = 8;
10058 const uint64_t word_width = 32;
10059
10060 // Objective-C has invalid DW_AT_bit_offset values in older versions
10061 // of clang, so we have to be careful and only insert unnamed bitfields
10062 // if we have a new enough clang.
10063 bool detect_unnamed_bitfields = true;
10064
10065 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
10066 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields ();
10067
10068 if (detect_unnamed_bitfields)
10069 {
10070 BitfieldInfo anon_field_info;
10071
10072 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
10073 {
10074 uint64_t last_field_end = 0;
10075
10076 if (last_field_info.IsValid())
10077 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
10078
10079 if (this_field_info.bit_offset != last_field_end)
10080 {
10081 if (((last_field_end % character_width) == 0) || // case 1
10082 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3
10083 {
10084 anon_field_info.bit_size = this_field_info.bit_offset % character_width;
10085 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
10086 }
10087 else // case 2
10088 {
10089 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
10090 anon_field_info.bit_offset = last_field_end;
10091 }
10092 }
10093 }
10094
10095 if (anon_field_info.IsValid())
10096 {
10097 clang::FieldDecl *unnamed_bitfield_decl =
10098 ClangASTContext::AddFieldToRecordType (class_clang_type,
10099 NULL,
10100 GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
10101 accessibility,
10102 anon_field_info.bit_size);
10103
10104 layout_info.field_offsets.insert(
10105 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
10106 }
10107 }
10108 last_field_info = this_field_info;
10109 }
10110 else
10111 {
10112 last_field_info.Clear();
10113 }
10114
10115 CompilerType member_clang_type = member_type->GetClangLayoutType();
10116
10117 {
10118 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
10119 // If the current field is at the end of the structure, then there is definitely no room for extra
10120 // elements and we override the type to array[0].
10121
10122 CompilerType member_array_element_type;
10123 uint64_t member_array_size;
10124 bool member_array_is_incomplete;
10125
10126 if (member_clang_type.IsArrayType(&member_array_element_type,
10127 &member_array_size,
10128 &member_array_is_incomplete) &&
10129 !member_array_is_incomplete)
10130 {
10131 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(dwarf, dwarf_cu, DW_AT_byte_size, UINT64_MAX);
10132
10133 if (member_byte_offset >= parent_byte_size)
10134 {
10135 if (member_array_size != 1)
10136 {
10137 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
10138 dwarf->MakeUserID(die->GetOffset()),
10139 name,
10140 encoding_uid,
10141 dwarf->MakeUserID(parent_die->GetOffset()));
10142 }
10143
10144 member_clang_type = CreateArrayType(member_array_element_type, 0, false);
10145 }
10146 }
10147 }
10148
10149 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type,
10150 name,
10151 member_clang_type,
10152 accessibility,
10153 bit_size);
10154
10155 SetMetadataAsUserID (field_decl, dwarf->MakeUserID(die->GetOffset()));
10156
10157 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
10158 }
10159 else
10160 {
10161 if (name)
10162 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10163 dwarf->MakeUserID(die->GetOffset()),
10164 name,
10165 encoding_uid);
10166 else
10167 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
10168 dwarf->MakeUserID(die->GetOffset()),
10169 encoding_uid);
10170 }
10171 }
10172
10173 if (prop_name != NULL && member_type)
10174 {
10175 clang::ObjCIvarDecl *ivar_decl = NULL;
10176
10177 if (field_decl)
10178 {
10179 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
10180 assert (ivar_decl != NULL);
10181 }
10182
10183 ClangASTMetadata metadata;
10184 metadata.SetUserID (dwarf->MakeUserID(die->GetOffset()));
10185 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
10186 prop_name,
10187 member_type->GetClangLayoutType(),
10188 ivar_decl,
10189 prop_setter_name,
10190 prop_getter_name,
10191 prop_attributes,
10192 &metadata));
10193
10194 if (ivar_decl)
10195 SetMetadataAsUserID (ivar_decl, dwarf->MakeUserID(die->GetOffset()));
10196 }
10197 }
10198 }
10199 ++member_idx;
10200 }
10201 break;
10202
10203 case DW_TAG_subprogram:
10204 // Let the type parsing code handle this one for us.
10205 member_function_dies.Append (die);
10206 break;
10207
10208 case DW_TAG_inheritance:
10209 {
10210 is_a_class = true;
10211 if (default_accessibility == eAccessNone)
10212 default_accessibility = eAccessPrivate;
10213 // TODO: implement DW_TAG_inheritance type parsing
10214 DWARFDebugInfoEntry::Attributes attributes;
10215 const size_t num_attributes = die->GetAttributes (dwarf,
10216 dwarf_cu,
10217 fixed_form_sizes,
10218 attributes);
10219 if (num_attributes > 0)
10220 {
10221 Declaration decl;
10222 DWARFExpression location;
10223 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10224 AccessType accessibility = default_accessibility;
10225 bool is_virtual = false;
10226 bool is_base_of_class = true;
10227 off_t member_byte_offset = 0;
10228 uint32_t i;
10229 for (i=0; i<num_attributes; ++i)
10230 {
10231 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10232 DWARFFormValue form_value;
10233 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10234 {
10235 switch (attr)
10236 {
10237 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10238 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10239 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10240 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10241 case DW_AT_data_member_location:
10242 if (form_value.BlockData())
10243 {
10244 Value initialValue(0);
10245 Value memberOffset(0);
10246 const DWARFDataExtractor& debug_info_data = dwarf->get_debug_info_data();
10247 uint32_t block_length = form_value.Unsigned();
10248 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
10249 if (DWARFExpression::Evaluate (NULL,
10250 NULL,
10251 NULL,
10252 NULL,
10253 module_sp,
10254 debug_info_data,
10255 block_offset,
10256 block_length,
10257 eRegisterKindDWARF,
10258 &initialValue,
10259 memberOffset,
10260 NULL))
10261 {
10262 member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
10263 }
10264 }
10265 else
10266 {
10267 // With DWARF 3 and later, if the value is an integer constant,
10268 // this form value is the offset in bytes from the beginning
10269 // of the containing entity.
10270 member_byte_offset = form_value.Unsigned();
10271 }
10272 break;
10273
10274 case DW_AT_accessibility:
10275 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
10276 break;
10277
10278 case DW_AT_virtuality:
10279 is_virtual = form_value.Boolean();
10280 break;
10281
10282 case DW_AT_sibling:
10283 break;
10284
10285 default:
10286 break;
10287 }
10288 }
10289 }
10290
10291 Type *base_class_type = dwarf->ResolveTypeUID(encoding_uid);
10292 if (base_class_type == NULL)
10293 {
10294 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to resolve the base class at 0x%8.8" PRIx64 " from enclosing type 0x%8.8x. \nPlease file a bug and attach the file at the start of this error message",
10295 die->GetOffset(),
10296 encoding_uid,
10297 parent_die->GetOffset());
10298 break;
10299 }
10300
10301 CompilerType base_class_clang_type = base_class_type->GetClangFullType();
10302 assert (base_class_clang_type);
10303 if (class_language == eLanguageTypeObjC)
10304 {
10305 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
10306 }
10307 else
10308 {
10309 base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(),
10310 accessibility,
10311 is_virtual,
10312 is_base_of_class));
10313
10314 if (is_virtual)
10315 {
10316 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
10317 // give us a constant offset, but gives us a DWARF expressions that requires an actual object
10318 // in memory. the DW_AT_data_member_location for a virtual base class looks like:
10319 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
10320 // Given this, there is really no valid response we can give to clang for virtual base
10321 // class offsets, and this should eventually be removed from LayoutRecordType() in the external
10322 // AST source in clang.
10323 }
10324 else
10325 {
10326 layout_info.base_offsets.insert(
10327 std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
10328 clang::CharUnits::fromQuantity(member_byte_offset)));
10329 }
10330 }
10331 }
10332 }
10333 break;
10334
10335 default:
10336 break;
10337 }
10338 }
10339
10340 return count;
10341}
10342
10343
10344size_t
10345ClangASTContext::ParseChildParameters (const SymbolContext& sc,
10346 clang::DeclContext *containing_decl_ctx,
10347 SymbolFileDWARF *dwarf,
10348 DWARFCompileUnit* dwarf_cu,
10349 const DWARFDebugInfoEntry *parent_die,
10350 bool skip_artificial,
10351 bool &is_static,
10352 bool &is_variadic,
10353 std::vector<CompilerType>& function_param_types,
10354 std::vector<clang::ParmVarDecl*>& function_param_decls,
10355 unsigned &type_quals)
10356{
10357 if (parent_die == NULL)
10358 return 0;
10359
10360 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10361
10362 size_t arg_idx = 0;
10363 const DWARFDebugInfoEntry *die;
10364 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10365 {
10366 dw_tag_t tag = die->Tag();
10367 switch (tag)
10368 {
10369 case DW_TAG_formal_parameter:
10370 {
10371 DWARFDebugInfoEntry::Attributes attributes;
10372 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10373 if (num_attributes > 0)
10374 {
10375 const char *name = NULL;
10376 Declaration decl;
10377 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
10378 bool is_artificial = false;
10379 // one of None, Auto, Register, Extern, Static, PrivateExtern
10380
10381 clang::StorageClass storage = clang::SC_None;
10382 uint32_t i;
10383 for (i=0; i<num_attributes; ++i)
10384 {
10385 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10386 DWARFFormValue form_value;
10387 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10388 {
10389 switch (attr)
10390 {
10391 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10392 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10393 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10394 case DW_AT_name: name = form_value.AsCString(&dwarf->get_debug_str_data()); break;
10395 case DW_AT_type: param_type_die_offset = form_value.Reference(); break;
10396 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
10397 case DW_AT_location:
10398 // if (form_value.BlockData())
10399 // {
10400 // const DWARFDataExtractor& debug_info_data = debug_info();
10401 // uint32_t block_length = form_value.Unsigned();
10402 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
10403 // }
10404 // else
10405 // {
10406 // }
10407 // break;
10408 case DW_AT_const_value:
10409 case DW_AT_default_value:
10410 case DW_AT_description:
10411 case DW_AT_endianity:
10412 case DW_AT_is_optional:
10413 case DW_AT_segment:
10414 case DW_AT_variable_parameter:
10415 default:
10416 case DW_AT_abstract_origin:
10417 case DW_AT_sibling:
10418 break;
10419 }
10420 }
10421 }
10422
10423 bool skip = false;
10424 if (skip_artificial)
10425 {
10426 if (is_artificial)
10427 {
10428 // In order to determine if a C++ member function is
10429 // "const" we have to look at the const-ness of "this"...
10430 // Ugly, but that
10431 if (arg_idx == 0)
10432 {
10433 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
10434 {
10435 // Often times compilers omit the "this" name for the
10436 // specification DIEs, so we can't rely upon the name
10437 // being in the formal parameter DIE...
10438 if (name == NULL || ::strcmp(name, "this")==0)
10439 {
10440 Type *this_type = dwarf->ResolveTypeUID (param_type_die_offset);
10441 if (this_type)
10442 {
10443 uint32_t encoding_mask = this_type->GetEncodingMask();
10444 if (encoding_mask & Type::eEncodingIsPointerUID)
10445 {
10446 is_static = false;
10447
10448 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
10449 type_quals |= clang::Qualifiers::Const;
10450 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
10451 type_quals |= clang::Qualifiers::Volatile;
10452 }
10453 }
10454 }
10455 }
10456 }
10457 skip = true;
10458 }
10459 else
10460 {
10461
10462 // HACK: Objective C formal parameters "self" and "_cmd"
10463 // are not marked as artificial in the DWARF...
10464 CompileUnit *comp_unit = dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
10465 if (comp_unit)
10466 {
10467 switch (comp_unit->GetLanguage())
10468 {
10469 case eLanguageTypeObjC:
10470 case eLanguageTypeObjC_plus_plus:
10471 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
10472 skip = true;
10473 break;
10474 default:
10475 break;
10476 }
10477 }
10478 }
10479 }
10480
10481 if (!skip)
10482 {
10483 Type *type = dwarf->ResolveTypeUID(param_type_die_offset);
10484 if (type)
10485 {
10486 function_param_types.push_back (type->GetClangForwardType());
10487
10488 clang::ParmVarDecl *param_var_decl = CreateParameterDeclaration (name,
10489 type->GetClangForwardType(),
10490 storage);
10491 assert(param_var_decl);
10492 function_param_decls.push_back(param_var_decl);
10493
10494 SetMetadataAsUserID (param_var_decl, dwarf->MakeUserID(die->GetOffset()));
10495 }
10496 }
10497 }
10498 arg_idx++;
10499 }
10500 break;
10501
10502 case DW_TAG_unspecified_parameters:
10503 is_variadic = true;
10504 break;
10505
10506 case DW_TAG_template_type_parameter:
10507 case DW_TAG_template_value_parameter:
10508 // The one caller of this was never using the template_param_infos,
10509 // and the local variable was taking up a large amount of stack space
10510 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
10511 // the template params back, we can add them back.
10512 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
10513 break;
10514
10515 default:
10516 break;
10517 }
10518 }
10519 return arg_idx;
10520}
10521
10522void
10523ClangASTContext::ParseChildArrayInfo (const SymbolContext& sc,
10524 SymbolFileDWARF *dwarf,
10525 DWARFCompileUnit* dwarf_cu,
10526 const DWARFDebugInfoEntry *parent_die,
10527 int64_t& first_index,
10528 std::vector<uint64_t>& element_orders,
10529 uint32_t& byte_stride,
10530 uint32_t& bit_stride)
10531{
10532 if (parent_die == NULL)
10533 return;
10534
10535 const DWARFDebugInfoEntry *die;
10536 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64());
10537 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
10538 {
10539 const dw_tag_t tag = die->Tag();
10540 switch (tag)
10541 {
10542 case DW_TAG_subrange_type:
10543 {
10544 DWARFDebugInfoEntry::Attributes attributes;
10545 const size_t num_child_attributes = die->GetAttributes(dwarf, dwarf_cu, fixed_form_sizes, attributes);
10546 if (num_child_attributes > 0)
10547 {
10548 uint64_t num_elements = 0;
10549 uint64_t lower_bound = 0;
10550 uint64_t upper_bound = 0;
10551 bool upper_bound_valid = false;
10552 uint32_t i;
10553 for (i=0; i<num_child_attributes; ++i)
10554 {
10555 const dw_attr_t attr = attributes.AttributeAtIndex(i);
10556 DWARFFormValue form_value;
10557 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10558 {
10559 switch (attr)
10560 {
10561 case DW_AT_name:
10562 break;
10563
10564 case DW_AT_count:
10565 num_elements = form_value.Unsigned();
10566 break;
10567
10568 case DW_AT_bit_stride:
10569 bit_stride = form_value.Unsigned();
10570 break;
10571
10572 case DW_AT_byte_stride:
10573 byte_stride = form_value.Unsigned();
10574 break;
10575
10576 case DW_AT_lower_bound:
10577 lower_bound = form_value.Unsigned();
10578 break;
10579
10580 case DW_AT_upper_bound:
10581 upper_bound_valid = true;
10582 upper_bound = form_value.Unsigned();
10583 break;
10584
10585 default:
10586 case DW_AT_abstract_origin:
10587 case DW_AT_accessibility:
10588 case DW_AT_allocated:
10589 case DW_AT_associated:
10590 case DW_AT_data_location:
10591 case DW_AT_declaration:
10592 case DW_AT_description:
10593 case DW_AT_sibling:
10594 case DW_AT_threads_scaled:
10595 case DW_AT_type:
10596 case DW_AT_visibility:
10597 break;
10598 }
10599 }
10600 }
10601
10602 if (num_elements == 0)
10603 {
10604 if (upper_bound_valid && upper_bound >= lower_bound)
10605 num_elements = upper_bound - lower_bound + 1;
10606 }
10607
10608 element_orders.push_back (num_elements);
10609 }
10610 }
10611 break;
10612 }
10613 }
10614}
10615
10616clang::DeclContext*
10617ClangASTContext::GetClangDeclContextContainingTypeUID (SymbolFileDWARF *dwarf, lldb::user_id_t type_uid)
10618{
10619 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
10620 if (debug_info && dwarf->UserIDMatches(type_uid))
10621 {
10622 DWARFCompileUnitSP cu_sp;
10623 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
10624 if (die)
10625 return GetClangDeclContextContainingDIE (dwarf, cu_sp.get(), die, NULL);
10626 }
10627 return NULL;
10628}
10629
10630clang::DeclContext*
10631ClangASTContext::GetClangDeclContextForTypeUID (SymbolFileDWARF *dwarf, const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
10632{
10633 if (dwarf->UserIDMatches(type_uid))
10634 return GetClangDeclContextForDIEOffset (dwarf, sc, type_uid);
10635 return NULL;
10636}
10637
10638
10639clang::DeclContext *
10640ClangASTContext::GetClangDeclContextForDIE (SymbolFileDWARF *dwarf,
10641 const SymbolContext &sc,
10642 DWARFCompileUnit *cu,
10643 const DWARFDebugInfoEntry *die)
10644{
10645 clang::DeclContext *clang_decl_ctx = dwarf->GetCachedClangDeclContextForDIE (die);
10646 if (clang_decl_ctx)
10647 return clang_decl_ctx;
10648 // If this DIE has a specification, or an abstract origin, then trace to those.
10649
10650 dw_offset_t die_offset = die->GetAttributeValueAsReference(dwarf, cu, DW_AT_specification, DW_INVALID_OFFSET);
10651 if (die_offset != DW_INVALID_OFFSET)
10652 return GetClangDeclContextForDIEOffset (dwarf, sc, die_offset);
10653
10654 die_offset = die->GetAttributeValueAsReference(dwarf, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
10655 if (die_offset != DW_INVALID_OFFSET)
10656 return GetClangDeclContextForDIEOffset (dwarf, sc, die_offset);
10657
10658 Log *log = nullptr; //(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10659 if (log)
10660 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(dwarf, cu));
10661 // This is the DIE we want. Parse it, then query our map.
10662 bool assert_not_being_parsed = true;
10663 dwarf->ResolveTypeUID (cu, die, assert_not_being_parsed);
10664
10665 clang_decl_ctx = dwarf->GetCachedClangDeclContextForDIE (die);
10666
10667 return clang_decl_ctx;
10668}
10669
10670
10671clang::DeclContext *
10672ClangASTContext::GetClangDeclContextContainingDIEOffset (SymbolFileDWARF *dwarf,
10673 dw_offset_t die_offset)
10674{
10675 if (die_offset != DW_INVALID_OFFSET)
10676 {
10677 DWARFCompileUnitSP cu_sp;
10678 const DWARFDebugInfoEntry* die = dwarf->DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
10679 return GetClangDeclContextContainingDIE (dwarf, cu_sp.get(), die, NULL);
10680 }
10681 return NULL;
10682}
10683
10684clang::DeclContext *
10685ClangASTContext::GetClangDeclContextForDIEOffset (SymbolFileDWARF *dwarf,
10686 const SymbolContext &sc,
10687 dw_offset_t die_offset)
10688{
10689 if (die_offset != DW_INVALID_OFFSET)
10690 {
10691 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
10692 if (debug_info)
10693 {
10694 DWARFCompileUnitSP cu_sp;
10695 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
10696 if (die)
10697 return GetClangDeclContextForDIE (dwarf, sc, cu_sp.get(), die);
10698 }
10699 }
10700 return NULL;
10701}
10702
10703clang::NamespaceDecl *
10704ClangASTContext::ResolveNamespaceDIE (SymbolFileDWARF *dwarf,
10705 DWARFCompileUnit *dwarf_cu,
10706 const DWARFDebugInfoEntry *die)
10707{
10708 if (die && die->Tag() == DW_TAG_namespace)
10709 {
10710 // See if we already parsed this namespace DIE and associated it with a
10711 // uniqued namespace declaration
10712 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(dwarf->m_die_to_decl_ctx[die]);
10713 if (namespace_decl)
10714 return namespace_decl;
10715 else
10716 {
10717 const char *namespace_name = die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL);
10718 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL);
10719 namespace_decl = GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
10720 Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10721 if (log)
10722 {
10723 if (namespace_name)
10724 {
10725 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10726 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
10727 static_cast<void*>(getASTContext()),
10728 dwarf->MakeUserID(die->GetOffset()),
10729 namespace_name,
10730 static_cast<void*>(namespace_decl),
10731 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10732 }
10733 else
10734 {
10735 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
10736 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
10737 static_cast<void*>(getASTContext()),
10738 dwarf->MakeUserID(die->GetOffset()),
10739 static_cast<void*>(namespace_decl),
10740 static_cast<void*>(namespace_decl->getOriginalNamespace()));
10741 }
10742 }
10743
10744 if (namespace_decl)
10745 dwarf->LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
10746 return namespace_decl;
10747 }
10748 }
10749 return NULL;
10750}
10751
10752clang::DeclContext *
10753ClangASTContext::GetClangDeclContextContainingDIE (SymbolFileDWARF *dwarf,
10754 DWARFCompileUnit *cu,
10755 const DWARFDebugInfoEntry *die,
10756 const DWARFDebugInfoEntry **decl_ctx_die_copy)
10757{
10758 if (dwarf->m_clang_tu_decl == NULL)
10759 dwarf->m_clang_tu_decl = getASTContext()->getTranslationUnitDecl();
10760
10761 const DWARFDebugInfoEntry *decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (cu, die);
10762
10763 if (decl_ctx_die_copy)
10764 *decl_ctx_die_copy = decl_ctx_die;
10765
10766 if (decl_ctx_die)
10767 {
10768
10769 SymbolFileDWARF::DIEToDeclContextMap::iterator pos = dwarf->m_die_to_decl_ctx.find (decl_ctx_die);
10770 if (pos != dwarf->m_die_to_decl_ctx.end())
10771 return pos->second;
10772
10773 switch (decl_ctx_die->Tag())
10774 {
10775 case DW_TAG_compile_unit:
10776 return dwarf->m_clang_tu_decl;
10777
10778 case DW_TAG_namespace:
10779 return ResolveNamespaceDIE (dwarf, cu, decl_ctx_die);
10780
10781 case DW_TAG_structure_type:
10782 case DW_TAG_union_type:
10783 case DW_TAG_class_type:
10784 {
10785 Type* type = dwarf->ResolveType (cu, decl_ctx_die);
10786 if (type)
10787 {
10788 clang::DeclContext *decl_ctx = GetDeclContextForType(type->GetClangForwardType());
10789 if (decl_ctx)
10790 {
10791 dwarf->LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
10792 if (decl_ctx)
10793 return decl_ctx;
10794 }
10795 }
10796 }
10797 break;
10798
10799 default:
10800 break;
10801 }
10802 }
10803 return dwarf->m_clang_tu_decl;
10804}
10805
10806
10807
10808TypeSP
10809ClangASTContext::ParseTypeFromDWARF (const SymbolContext& sc,
10810 SymbolFileDWARF *dwarf,
10811 DWARFCompileUnit* dwarf_cu,
10812 const DWARFDebugInfoEntry *die,
10813 Log *log,
10814 bool *type_is_new_ptr)
10815{
10816 TypeSP type_sp;
10817
10818 if (type_is_new_ptr)
10819 *type_is_new_ptr = false;
10820
10821#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10822 static DIEStack g_die_stack;
10823 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
10824#endif
10825
10826 AccessType accessibility = eAccessNone;
10827 if (die != NULL)
10828 {
10829 if (log)
10830 {
10831 const DWARFDebugInfoEntry *context_die;
10832 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &context_die);
10833
10834 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
10835 die->GetOffset(),
10836 static_cast<void*>(context),
10837 context_die->GetOffset(),
10838 DW_TAG_value_to_name(die->Tag()),
10839 die->GetName(dwarf, dwarf_cu));
10840
10841#if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
10842 scoped_die_logger.Push (dwarf_cu, die);
10843 g_die_stack.LogDIEs(log, dwarf);
10844#endif
10845 }
10846 //
10847 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
10848 // if (log && dwarf_cu)
10849 // {
10850 // StreamString s;
10851 // die->DumpLocation (this, dwarf_cu, s);
10852 // dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
10853 //
10854 // }
10855
10856 Type *type_ptr = dwarf->m_die_to_type.lookup (die);
10857 TypeList* type_list = dwarf->GetTypeList();
10858 if (type_ptr == NULL)
10859 {
10860 if (type_is_new_ptr)
10861 *type_is_new_ptr = true;
10862
10863 const dw_tag_t tag = die->Tag();
10864
10865 bool is_forward_declaration = false;
10866 DWARFDebugInfoEntry::Attributes attributes;
10867 const char *type_name_cstr = NULL;
10868 ConstString type_name_const_str;
10869 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
10870 uint64_t byte_size = 0;
10871 Declaration decl;
10872
10873 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
10874 CompilerType clang_type;
10875 DWARFFormValue form_value;
10876
10877 dw_attr_t attr;
10878
10879 switch (tag)
10880 {
10881 case DW_TAG_base_type:
10882 case DW_TAG_pointer_type:
10883 case DW_TAG_reference_type:
10884 case DW_TAG_rvalue_reference_type:
10885 case DW_TAG_typedef:
10886 case DW_TAG_const_type:
10887 case DW_TAG_restrict_type:
10888 case DW_TAG_volatile_type:
10889 case DW_TAG_unspecified_type:
10890 {
10891 // Set a bit that lets us know that we are currently parsing this
10892 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
10893
10894 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
10895 uint32_t encoding = 0;
10896 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
10897
10898 if (num_attributes > 0)
10899 {
10900 uint32_t i;
10901 for (i=0; i<num_attributes; ++i)
10902 {
10903 attr = attributes.AttributeAtIndex(i);
10904 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
10905 {
10906 switch (attr)
10907 {
10908 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
10909 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
10910 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
10911 case DW_AT_name:
10912
10913 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
10914 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
10915 // include the "&"...
10916 if (tag == DW_TAG_reference_type)
10917 {
10918 if (strchr (type_name_cstr, '&') == NULL)
10919 type_name_cstr = NULL;
10920 }
10921 if (type_name_cstr)
10922 type_name_const_str.SetCString(type_name_cstr);
10923 break;
10924 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
10925 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
10926 case DW_AT_type: encoding_uid = form_value.Reference(); break;
10927 default:
10928 case DW_AT_sibling:
10929 break;
10930 }
10931 }
10932 }
10933 }
10934
10935 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
10936
10937 switch (tag)
10938 {
10939 default:
10940 break;
10941
10942 case DW_TAG_unspecified_type:
10943 if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
10944 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
10945 {
10946 resolve_state = Type::eResolveStateFull;
10947 clang_type = GetBasicType(eBasicTypeNullPtr);
10948 break;
10949 }
10950 // Fall through to base type below in case we can handle the type there...
10951
10952 case DW_TAG_base_type:
10953 resolve_state = Type::eResolveStateFull;
10954 clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
10955 encoding,
10956 byte_size * 8);
10957 break;
10958
10959 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
10960 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
10961 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
10962 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
10963 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
10964 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
10965 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
10966 }
10967
10968 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
10969 {
10970 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
10971
10972 if (translation_unit_is_objc)
10973 {
10974 if (type_name_cstr != NULL)
10975 {
10976 static ConstString g_objc_type_name_id("id");
10977 static ConstString g_objc_type_name_Class("Class");
10978 static ConstString g_objc_type_name_selector("SEL");
10979
10980 if (type_name_const_str == g_objc_type_name_id)
10981 {
10982 if (log)
10983 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
10984 die->GetOffset(),
10985 DW_TAG_value_to_name(die->Tag()),
10986 die->GetName(dwarf, dwarf_cu));
10987 clang_type = GetBasicType(eBasicTypeObjCID);
10988 encoding_data_type = Type::eEncodingIsUID;
10989 encoding_uid = LLDB_INVALID_UID;
10990 resolve_state = Type::eResolveStateFull;
10991
10992 }
10993 else if (type_name_const_str == g_objc_type_name_Class)
10994 {
10995 if (log)
10996 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
10997 die->GetOffset(),
10998 DW_TAG_value_to_name(die->Tag()),
10999 die->GetName(dwarf, dwarf_cu));
11000 clang_type = GetBasicType(eBasicTypeObjCClass);
11001 encoding_data_type = Type::eEncodingIsUID;
11002 encoding_uid = LLDB_INVALID_UID;
11003 resolve_state = Type::eResolveStateFull;
11004 }
11005 else if (type_name_const_str == g_objc_type_name_selector)
11006 {
11007 if (log)
11008 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
11009 die->GetOffset(),
11010 DW_TAG_value_to_name(die->Tag()),
11011 die->GetName(dwarf, dwarf_cu));
11012 clang_type = GetBasicType(eBasicTypeObjCSel);
11013 encoding_data_type = Type::eEncodingIsUID;
11014 encoding_uid = LLDB_INVALID_UID;
11015 resolve_state = Type::eResolveStateFull;
11016 }
11017 }
11018 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID)
11019 {
11020 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id".
11021
11022 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid);
11023
11024 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type)
11025 {
11026 if (const char *struct_name = encoding_die->GetAttributeValueAsString(dwarf, dwarf_cu, DW_AT_name, NULL))
11027 {
11028 if (!strcmp(struct_name, "objc_object"))
11029 {
11030 if (log)
11031 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
11032 die->GetOffset(),
11033 DW_TAG_value_to_name(die->Tag()),
11034 die->GetName(dwarf, dwarf_cu));
11035 clang_type = GetBasicType(eBasicTypeObjCID);
11036 encoding_data_type = Type::eEncodingIsUID;
11037 encoding_uid = LLDB_INVALID_UID;
11038 resolve_state = Type::eResolveStateFull;
11039 }
11040 }
11041 }
11042 }
11043 }
11044 }
11045
11046 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11047 dwarf,
11048 type_name_const_str,
11049 byte_size,
11050 NULL,
11051 encoding_uid,
11052 encoding_data_type,
11053 &decl,
11054 clang_type,
11055 resolve_state));
11056
11057 dwarf->m_die_to_type[die] = type_sp.get();
11058
11059 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
11060 // if (encoding_type != NULL)
11061 // {
11062 // if (encoding_type != DIE_IS_BEING_PARSED)
11063 // type_sp->SetEncodingType(encoding_type);
11064 // else
11065 // m_indirect_fixups.push_back(type_sp.get());
11066 // }
11067 }
11068 break;
11069
11070 case DW_TAG_structure_type:
11071 case DW_TAG_union_type:
11072 case DW_TAG_class_type:
11073 {
11074 // Set a bit that lets us know that we are currently parsing this
11075 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11076 bool byte_size_valid = false;
11077
11078 LanguageType class_language = eLanguageTypeUnknown;
11079 bool is_complete_objc_class = false;
11080 //bool struct_is_class = false;
11081 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11082 if (num_attributes > 0)
11083 {
11084 uint32_t i;
11085 for (i=0; i<num_attributes; ++i)
11086 {
11087 attr = attributes.AttributeAtIndex(i);
11088 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11089 {
11090 switch (attr)
11091 {
11092 case DW_AT_decl_file:
11093 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
11094 {
11095 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
11096 // point to the compile unit file, so we clear this invalid value
11097 // so that we can still unique types efficiently.
11098 decl.SetFile(FileSpec ("<invalid>", false));
11099 }
11100 else
11101 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
11102 break;
11103
11104 case DW_AT_decl_line:
11105 decl.SetLine(form_value.Unsigned());
11106 break;
11107
11108 case DW_AT_decl_column:
11109 decl.SetColumn(form_value.Unsigned());
11110 break;
11111
11112 case DW_AT_name:
11113 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11114 type_name_const_str.SetCString(type_name_cstr);
11115 break;
11116
11117 case DW_AT_byte_size:
11118 byte_size = form_value.Unsigned();
11119 byte_size_valid = true;
11120 break;
11121
11122 case DW_AT_accessibility:
11123 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
11124 break;
11125
11126 case DW_AT_declaration:
11127 is_forward_declaration = form_value.Boolean();
11128 break;
11129
11130 case DW_AT_APPLE_runtime_class:
11131 class_language = (LanguageType)form_value.Signed();
11132 break;
11133
11134 case DW_AT_APPLE_objc_complete_type:
11135 is_complete_objc_class = form_value.Signed();
11136 break;
11137
11138 case DW_AT_allocated:
11139 case DW_AT_associated:
11140 case DW_AT_data_location:
11141 case DW_AT_description:
11142 case DW_AT_start_scope:
11143 case DW_AT_visibility:
11144 default:
11145 case DW_AT_sibling:
11146 break;
11147 }
11148 }
11149 }
11150 }
11151
11152 // UniqueDWARFASTType is large, so don't create a local variables on the
11153 // stack, put it on the heap. This function is often called recursively
11154 // and clang isn't good and sharing the stack space for variables in different blocks.
11155 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
11156
11157 // Only try and unique the type if it has a name.
11158 if (type_name_const_str &&
11159 dwarf->GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
11160 dwarf,
11161 dwarf_cu,
11162 die,
11163 decl,
11164 byte_size_valid ? byte_size : -1,
11165 *unique_ast_entry_ap))
11166 {
11167 // We have already parsed this type or from another
11168 // compile unit. GCC loves to use the "one definition
11169 // rule" which can result in multiple definitions
11170 // of the same class over and over in each compile
11171 // unit.
11172 type_sp = unique_ast_entry_ap->m_type_sp;
11173 if (type_sp)
11174 {
11175 dwarf->m_die_to_type[die] = type_sp.get();
11176 return type_sp;
11177 }
11178 }
11179
11180 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11181
11182 int tag_decl_kind = -1;
11183 AccessType default_accessibility = eAccessNone;
11184 if (tag == DW_TAG_structure_type)
11185 {
11186 tag_decl_kind = clang::TTK_Struct;
11187 default_accessibility = eAccessPublic;
11188 }
11189 else if (tag == DW_TAG_union_type)
11190 {
11191 tag_decl_kind = clang::TTK_Union;
11192 default_accessibility = eAccessPublic;
11193 }
11194 else if (tag == DW_TAG_class_type)
11195 {
11196 tag_decl_kind = clang::TTK_Class;
11197 default_accessibility = eAccessPrivate;
11198 }
11199
11200 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
11201 die->HasChildren() == false &&
11202 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
11203 {
11204 // Work around an issue with clang at the moment where
11205 // forward declarations for objective C classes are emitted
11206 // as:
11207 // DW_TAG_structure_type [2]
11208 // DW_AT_name( "ForwardObjcClass" )
11209 // DW_AT_byte_size( 0x00 )
11210 // DW_AT_decl_file( "..." )
11211 // DW_AT_decl_line( 1 )
11212 //
11213 // Note that there is no DW_AT_declaration and there are
11214 // no children, and the byte size is zero.
11215 is_forward_declaration = true;
11216 }
11217
11218 if (class_language == eLanguageTypeObjC ||
11219 class_language == eLanguageTypeObjC_plus_plus)
11220 {
11221 if (!is_complete_objc_class && dwarf->Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
11222 {
11223 // We have a valid eSymbolTypeObjCClass class symbol whose
11224 // name matches the current objective C class that we
11225 // are trying to find and this DIE isn't the complete
11226 // definition (we checked is_complete_objc_class above and
11227 // know it is false), so the real definition is in here somewhere
11228 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11229
11230 if (!type_sp)
11231 {
11232 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11233 if (debug_map_symfile)
11234 {
11235 // We weren't able to find a full declaration in
11236 // this DWARF, see if we have a declaration anywhere
11237 // else...
11238 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
11239 }
11240 }
11241
11242 if (type_sp)
11243 {
11244 if (log)
11245 {
11246 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11247 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
11248 static_cast<void*>(this),
11249 die->GetOffset(),
11250 DW_TAG_value_to_name(tag),
11251 type_name_cstr,
11252 type_sp->GetID());
11253 }
11254
11255 // We found a real definition for this type elsewhere
11256 // so lets use it and cache the fact that we found
11257 // a complete type for this die
11258 dwarf->m_die_to_type[die] = type_sp.get();
11259 return type_sp;
11260 }
11261 }
11262 }
11263
11264
11265 if (is_forward_declaration)
11266 {
11267 // We have a forward declaration to a type and we need
11268 // to try and find a full declaration. We look in the
11269 // current type index just in case we have a forward
11270 // declaration followed by an actual declarations in the
11271 // DWARF. If this fails, we need to look elsewhere...
11272 if (log)
11273 {
11274 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11275 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
11276 static_cast<void*>(this),
11277 die->GetOffset(),
11278 DW_TAG_value_to_name(tag),
11279 type_name_cstr);
11280 }
11281
11282 DWARFDeclContext die_decl_ctx;
11283 die->GetDWARFDeclContext(dwarf, dwarf_cu, die_decl_ctx);
11284
11285 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
11286 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11287
11288 if (!type_sp)
11289 {
11290 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11291 if (debug_map_symfile)
11292 {
11293 // We weren't able to find a full declaration in
11294 // this DWARF, see if we have a declaration anywhere
11295 // else...
11296 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
11297 }
11298 }
11299
11300 if (type_sp)
11301 {
11302 if (log)
11303 {
11304 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
11305 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
11306 static_cast<void*>(this),
11307 die->GetOffset(),
11308 DW_TAG_value_to_name(tag),
11309 type_name_cstr,
11310 type_sp->GetID());
11311 }
11312
11313 // We found a real definition for this type elsewhere
11314 // so lets use it and cache the fact that we found
11315 // a complete type for this die
11316 dwarf->m_die_to_type[die] = type_sp.get();
11317 return type_sp;
11318 }
11319 }
11320 assert (tag_decl_kind != -1);
11321 bool clang_type_was_created = false;
11322 clang_type.SetClangType(this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
11323 if (!clang_type)
11324 {
11325 const DWARFDebugInfoEntry *decl_ctx_die;
11326
11327 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11328 if (accessibility == eAccessNone && decl_ctx)
11329 {
11330 // Check the decl context that contains this class/struct/union.
11331 // If it is a class we must give it an accessibility.
11332 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
11333 if (DeclKindIsCXXClass (containing_decl_kind))
11334 accessibility = default_accessibility;
11335 }
11336
11337 ClangASTMetadata metadata;
11338 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
11339 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (dwarf_cu, die));
11340
11341 if (type_name_cstr && strchr (type_name_cstr, '<'))
11342 {
11343 ClangASTContext::TemplateParameterInfos template_param_infos;
11344 if (ParseTemplateParameterInfos (dwarf, dwarf_cu, die, template_param_infos))
11345 {
11346 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (dwarf,
11347 decl_ctx,
11348 accessibility,
11349 type_name_cstr,
11350 tag_decl_kind,
11351 template_param_infos);
11352
11353 clang::ClassTemplateSpecializationDecl *class_specialization_decl = CreateClassTemplateSpecializationDecl (decl_ctx,
11354 class_template_decl,
11355 tag_decl_kind,
11356 template_param_infos);
11357 clang_type = CreateClassTemplateSpecializationType (class_specialization_decl);
11358 clang_type_was_created = true;
11359
11360 SetMetadata (class_template_decl, metadata);
11361 SetMetadata (class_specialization_decl, metadata);
11362 }
11363 }
11364
11365 if (!clang_type_was_created)
11366 {
11367 clang_type_was_created = true;
11368 clang_type = CreateRecordType (decl_ctx,
11369 accessibility,
11370 type_name_cstr,
11371 tag_decl_kind,
11372 class_language,
11373 &metadata);
11374 }
11375 }
11376
11377 // Store a forward declaration to this class type in case any
11378 // parameters in any class methods need it for the clang
11379 // types for function prototypes.
11380 dwarf->LinkDeclContextToDIE(GetDeclContextForType(clang_type), die);
11381 type_sp.reset (new Type (dwarf->MakeUserID(die->GetOffset()),
11382 dwarf,
11383 type_name_const_str,
11384 byte_size,
11385 NULL,
11386 LLDB_INVALID_UID,
11387 Type::eEncodingIsUID,
11388 &decl,
11389 clang_type,
11390 Type::eResolveStateForward));
11391
11392 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
11393
11394
11395 // Add our type to the unique type map so we don't
11396 // end up creating many copies of the same type over
11397 // and over in the ASTContext for our module
11398 unique_ast_entry_ap->m_type_sp = type_sp;
11399 unique_ast_entry_ap->m_symfile = dwarf;
11400 unique_ast_entry_ap->m_cu = dwarf_cu;
11401 unique_ast_entry_ap->m_die = die;
11402 unique_ast_entry_ap->m_declaration = decl;
11403 unique_ast_entry_ap->m_byte_size = byte_size;
11404 dwarf->GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
11405 *unique_ast_entry_ap);
11406
11407 if (is_forward_declaration && die->HasChildren())
11408 {
11409 // Check to see if the DIE actually has a definition, some version of GCC will
11410 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
11411 // members, or inheritance, so we can't trust it
11412 const DWARFDebugInfoEntry *child_die = die->GetFirstChild();
11413 while (child_die)
11414 {
11415 switch (child_die->Tag())
11416 {
11417 case DW_TAG_inheritance:
11418 case DW_TAG_subprogram:
11419 case DW_TAG_member:
11420 case DW_TAG_APPLE_property:
11421 case DW_TAG_class_type:
11422 case DW_TAG_structure_type:
11423 case DW_TAG_enumeration_type:
11424 case DW_TAG_typedef:
11425 case DW_TAG_union_type:
11426 child_die = NULL;
11427 is_forward_declaration = false;
11428 break;
11429 default:
11430 child_die = child_die->GetSibling();
11431 break;
11432 }
11433 }
11434 }
11435
11436 if (!is_forward_declaration)
11437 {
11438 // Always start the definition for a class type so that
11439 // if the class has child classes or types that require
11440 // the class to be created for use as their decl contexts
11441 // the class will be ready to accept these child definitions.
11442 if (die->HasChildren() == false)
11443 {
11444 // No children for this struct/union/class, lets finish it
11445 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11446 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11447
11448 if (tag == DW_TAG_structure_type) // this only applies in C
11449 {
11450 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type);
11451
11452 if (record_decl)
11453 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo()));
11454 }
11455 }
11456 else if (clang_type_was_created)
11457 {
11458 // Start the definition if the class is not objective C since
11459 // the underlying decls respond to isCompleteDefinition(). Objective
11460 // C decls don't respond to isCompleteDefinition() so we can't
11461 // start the declaration definition right away. For C++ class/union/structs
11462 // we want to start the definition in case the class is needed as the
11463 // declaration context for a contained class or type without the need
11464 // to complete that type..
11465
11466 if (class_language != eLanguageTypeObjC &&
11467 class_language != eLanguageTypeObjC_plus_plus)
11468 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11469
11470 // Leave this as a forward declaration until we need
11471 // to know the details of the type. lldb_private::Type
11472 // will automatically call the SymbolFile virtual function
11473 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
11474 // When the definition needs to be defined.
11475 dwarf->m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType();
11476 dwarf->m_forward_decl_clang_type_to_die[ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] = die;
11477 SetHasExternalStorage (clang_type.GetOpaqueQualType(), true);
11478 }
11479 }
11480
11481 }
11482 break;
11483
11484 case DW_TAG_enumeration_type:
11485 {
11486 // Set a bit that lets us know that we are currently parsing this
11487 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11488
11489 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
11490
11491 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11492 if (num_attributes > 0)
11493 {
11494 uint32_t i;
11495
11496 for (i=0; i<num_attributes; ++i)
11497 {
11498 attr = attributes.AttributeAtIndex(i);
11499 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11500 {
11501 switch (attr)
11502 {
11503 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11504 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11505 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11506 case DW_AT_name:
11507 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11508 type_name_const_str.SetCString(type_name_cstr);
11509 break;
11510 case DW_AT_type: encoding_uid = form_value.Reference(); break;
11511 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
11512 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11513 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break;
11514 case DW_AT_allocated:
11515 case DW_AT_associated:
11516 case DW_AT_bit_stride:
11517 case DW_AT_byte_stride:
11518 case DW_AT_data_location:
11519 case DW_AT_description:
11520 case DW_AT_start_scope:
11521 case DW_AT_visibility:
11522 case DW_AT_specification:
11523 case DW_AT_abstract_origin:
11524 case DW_AT_sibling:
11525 break;
11526 }
11527 }
11528 }
11529
11530 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11531
11532 CompilerType enumerator_clang_type;
11533 clang_type.SetClangType (this, dwarf->m_forward_decl_die_to_clang_type.lookup (die));
11534 if (!clang_type)
11535 {
11536 if (encoding_uid != DW_INVALID_OFFSET)
11537 {
11538 Type *enumerator_type = dwarf->ResolveTypeUID(encoding_uid);
11539 if (enumerator_type)
11540 enumerator_clang_type = enumerator_type->GetClangFullType();
11541 }
11542
11543 if (!enumerator_clang_type)
11544 enumerator_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
11545 DW_ATE_signed,
11546 byte_size * 8);
11547
11548 clang_type = CreateEnumerationType (type_name_cstr,
11549 GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, NULL),
11550 decl,
11551 enumerator_clang_type);
11552 }
11553 else
11554 {
11555 enumerator_clang_type = GetEnumerationIntegerType (clang_type.GetOpaqueQualType());
11556 }
11557
11558 dwarf->LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
11559
11560 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
11561 dwarf,
11562 type_name_const_str,
11563 byte_size,
11564 NULL,
11565 encoding_uid,
11566 Type::eEncodingIsUID,
11567 &decl,
11568 clang_type,
11569 Type::eResolveStateForward));
11570
11571 ClangASTContext::StartTagDeclarationDefinition (clang_type);
11572 if (die->HasChildren())
11573 {
11574 SymbolContext cu_sc(dwarf->GetCompUnitForDWARFCompUnit(dwarf_cu));
11575 bool is_signed = false;
11576 enumerator_clang_type.IsIntegerType(is_signed);
11577 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf, dwarf_cu, die);
11578 }
11579 ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
11580 }
11581 }
11582 break;
11583
11584 case DW_TAG_inlined_subroutine:
11585 case DW_TAG_subprogram:
11586 case DW_TAG_subroutine_type:
11587 {
11588 // Set a bit that lets us know that we are currently parsing this
11589 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
11590
11591 //const char *mangled = NULL;
11592 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
11593 bool is_variadic = false;
11594 bool is_inline = false;
11595 bool is_static = false;
11596 bool is_virtual = false;
11597 bool is_explicit = false;
11598 bool is_artificial = false;
11599 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
11600 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
11601 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
11602
11603 unsigned type_quals = 0;
11604 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
11605
11606
11607 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
11608 if (num_attributes > 0)
11609 {
11610 uint32_t i;
11611 for (i=0; i<num_attributes; ++i)
11612 {
11613 attr = attributes.AttributeAtIndex(i);
11614 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
11615 {
11616 switch (attr)
11617 {
11618 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
11619 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
11620 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
11621 case DW_AT_name:
11622 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
11623 type_name_const_str.SetCString(type_name_cstr);
11624 break;
11625
11626 case DW_AT_linkage_name:
11627 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break;
11628 case DW_AT_type: type_die_offset = form_value.Reference(); break;
11629 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
11630 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
11631 case DW_AT_inline: is_inline = form_value.Boolean(); break;
11632 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break;
11633 case DW_AT_explicit: is_explicit = form_value.Boolean(); break;
11634 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
11635
11636
11637 case DW_AT_external:
11638 if (form_value.Unsigned())
11639 {
11640 if (storage == clang::SC_None)
11641 storage = clang::SC_Extern;
11642 else
11643 storage = clang::SC_PrivateExtern;
11644 }
11645 break;
11646
11647 case DW_AT_specification:
11648 specification_die_offset = form_value.Reference();
11649 break;
11650
11651 case DW_AT_abstract_origin:
11652 abstract_origin_die_offset = form_value.Reference();
11653 break;
11654
11655 case DW_AT_object_pointer:
11656 object_pointer_die_offset = form_value.Reference();
11657 break;
11658
11659 case DW_AT_allocated:
11660 case DW_AT_associated:
11661 case DW_AT_address_class:
11662 case DW_AT_calling_convention:
11663 case DW_AT_data_location:
11664 case DW_AT_elemental:
11665 case DW_AT_entry_pc:
11666 case DW_AT_frame_base:
11667 case DW_AT_high_pc:
11668 case DW_AT_low_pc:
11669 case DW_AT_prototyped:
11670 case DW_AT_pure:
11671 case DW_AT_ranges:
11672 case DW_AT_recursive:
11673 case DW_AT_return_addr:
11674 case DW_AT_segment:
11675 case DW_AT_start_scope:
11676 case DW_AT_static_link:
11677 case DW_AT_trampoline:
11678 case DW_AT_visibility:
11679 case DW_AT_vtable_elem_location:
11680 case DW_AT_description:
11681 case DW_AT_sibling:
11682 break;
11683 }
11684 }
11685 }
11686 }
11687
11688 std::string object_pointer_name;
11689 if (object_pointer_die_offset != DW_INVALID_OFFSET)
11690 {
11691 // Get the name from the object pointer die
11692 StreamString s;
11693 if (DWARFDebugInfoEntry::GetName (dwarf, dwarf_cu, object_pointer_die_offset, s))
11694 {
11695 object_pointer_name.assign(s.GetData());
11696 }
11697 }
11698
11699 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
11700
11701 CompilerType return_clang_type;
11702 Type *func_type = NULL;
11703
11704 if (type_die_offset != DW_INVALID_OFFSET)
11705 func_type = dwarf->ResolveTypeUID(type_die_offset);
11706
11707 if (func_type)
11708 return_clang_type = func_type->GetClangForwardType();
11709 else
11710 return_clang_type = GetBasicType(eBasicTypeVoid);
11711
11712
11713 std::vector<CompilerType> function_param_types;
11714 std::vector<clang::ParmVarDecl*> function_param_decls;
11715
11716 // Parse the function children for the parameters
11717
11718 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
11719 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf, dwarf_cu, die, &decl_ctx_die);
11720 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
11721
11722 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
11723 // Start off static. This will be set to false in ParseChildParameters(...)
11724 // if we find a "this" parameters as the first parameter
11725 if (is_cxx_method)
11726 is_static = true;
11727
11728 if (die->HasChildren())
11729 {
11730 bool skip_artificial = true;
11731 ParseChildParameters (sc,
11732 containing_decl_ctx,
11733 dwarf,
11734 dwarf_cu,
11735 die,
11736 skip_artificial,
11737 is_static,
11738 is_variadic,
11739 function_param_types,
11740 function_param_decls,
11741 type_quals);
11742 }
11743
11744 // clang_type will get the function prototype clang type after this call
11745 clang_type = CreateFunctionType (return_clang_type,
11746 function_param_types.data(),
11747 function_param_types.size(),
11748 is_variadic,
11749 type_quals);
11750
11751 bool ignore_containing_context = false;
11752
11753 if (type_name_cstr)
11754 {
11755 bool type_handled = false;
11756 if (tag == DW_TAG_subprogram)
11757 {
11758 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true);
11759 if (objc_method.IsValid(true))
11760 {
11761 CompilerType class_opaque_type;
11762 ConstString class_name(objc_method.GetClassName());
11763 if (class_name)
11764 {
11765 TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
11766
11767 if (complete_objc_class_type_sp)
11768 {
11769 CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
11770 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type))
11771 class_opaque_type = type_clang_forward_type;
11772 }
11773 }
11774
11775 if (class_opaque_type)
11776 {
11777 // If accessibility isn't set to anything valid, assume public for
11778 // now...
11779 if (accessibility == eAccessNone)
11780 accessibility = eAccessPublic;
11781
11782 clang::ObjCMethodDecl *objc_method_decl = AddMethodToObjCObjectType (class_opaque_type,
11783 type_name_cstr,
11784 clang_type,
11785 accessibility,
11786 is_artificial);
11787 type_handled = objc_method_decl != NULL;
11788 if (type_handled)
11789 {
11790 dwarf->LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
11791 SetMetadataAsUserID (objc_method_decl, dwarf->MakeUserID(die->GetOffset()));
11792 }
11793 else
11794 {
11795 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
11796 die->GetOffset(),
11797 tag,
11798 DW_TAG_value_to_name(tag));
11799 }
11800 }
11801 }
11802 else if (is_cxx_method)
11803 {
11804 // Look at the parent of this DIE and see if is is
11805 // a class or struct and see if this is actually a
11806 // C++ method
11807 Type *class_type = dwarf->ResolveType (dwarf_cu, decl_ctx_die);
11808 if (class_type)
11809 {
11810 if (class_type->GetID() != dwarf->MakeUserID(decl_ctx_die->GetOffset()))
11811 {
11812 // We uniqued the parent class of this function to another class
11813 // so we now need to associate all dies under "decl_ctx_die" to
11814 // DIEs in the DIE for "class_type"...
11815 SymbolFileDWARF *class_symfile = NULL;
11816 DWARFCompileUnitSP class_type_cu_sp;
11817 const DWARFDebugInfoEntry *class_type_die = NULL;
11818
11819 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
11820 if (debug_map_symfile)
11821 {
11822 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
11823 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11824 }
11825 else
11826 {
11827 class_symfile = dwarf;
11828 class_type_die = dwarf->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
11829 }
11830 if (class_type_die)
11831 {
11832 DWARFDIECollection failures;
11833
11834 CopyUniqueClassMethodTypes (dwarf,
11835 class_symfile,
11836 class_type,
11837 class_type_cu_sp.get(),
11838 class_type_die,
11839 dwarf_cu,
11840 decl_ctx_die,
11841 failures);
11842
11843 // FIXME do something with these failures that's smarter than
11844 // just dropping them on the ground. Unfortunately classes don't
11845 // like having stuff added to them after their definitions are
11846 // complete...
11847
11848 type_ptr = dwarf->m_die_to_type[die];
11849 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
11850 {
11851 type_sp = type_ptr->shared_from_this();
11852 break;
11853 }
11854 }
11855 }
11856
11857 if (specification_die_offset != DW_INVALID_OFFSET)
11858 {
11859 // We have a specification which we are going to base our function
11860 // prototype off of, so we need this type to be completed so that the
11861 // m_die_to_decl_ctx for the method in the specification has a valid
11862 // clang decl context.
11863 class_type->GetClangForwardType();
11864 // If we have a specification, then the function type should have been
11865 // made with the specification and not with this die.
11866 DWARFCompileUnitSP spec_cu_sp;
11867 const DWARFDebugInfoEntry* spec_die = dwarf->DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
11868 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, sc, dwarf_cu, spec_die);
11869 if (spec_clang_decl_ctx)
11870 {
11871 dwarf->LinkDeclContextToDIE(spec_clang_decl_ctx, die);
11872 }
11873 else
11874 {
11875 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n",
11876 dwarf->MakeUserID(die->GetOffset()),
11877 specification_die_offset);
11878 }
11879 type_handled = true;
11880 }
11881 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
11882 {
11883 // We have a specification which we are going to base our function
11884 // prototype off of, so we need this type to be completed so that the
11885 // m_die_to_decl_ctx for the method in the abstract origin has a valid
11886 // clang decl context.
11887 class_type->GetClangForwardType();
11888
11889 DWARFCompileUnitSP abs_cu_sp;
11890 const DWARFDebugInfoEntry* abs_die = dwarf->DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
11891 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (dwarf, sc, dwarf_cu, abs_die);
11892 if (abs_clang_decl_ctx)
11893 {
11894 dwarf->LinkDeclContextToDIE (abs_clang_decl_ctx, die);
11895 }
11896 else
11897 {
11898 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n",
11899 dwarf->MakeUserID(die->GetOffset()),
11900 abstract_origin_die_offset);
11901 }
11902 type_handled = true;
11903 }
11904 else
11905 {
11906 CompilerType class_opaque_type = class_type->GetClangForwardType();
11907 if (ClangASTContext::IsCXXClassType(class_opaque_type))
11908 {
11909 if (class_opaque_type.IsBeingDefined ())
11910 {
11911 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
11912 // in the DWARF for C++ methods... Default to public for now...
11913 if (accessibility == eAccessNone)
11914 accessibility = eAccessPublic;
11915
11916 if (!is_static && !die->HasChildren())
11917 {
11918 // We have a C++ member function with no children (this pointer!)
11919 // and clang will get mad if we try and make a function that isn't
11920 // well formed in the DWARF, so we will just skip it...
11921 type_handled = true;
11922 }
11923 else
11924 {
11925 clang::CXXMethodDecl *cxx_method_decl;
11926 // REMOVE THE CRASH DESCRIPTION BELOW
11927 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
11928 type_name_cstr,
11929 class_type->GetName().GetCString(),
11930 dwarf->MakeUserID(die->GetOffset()),
11931 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
11932
11933 const bool is_attr_used = false;
11934
11935 cxx_method_decl = AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(),
11936 type_name_cstr,
11937 clang_type,
11938 accessibility,
11939 is_virtual,
11940 is_static,
11941 is_inline,
11942 is_explicit,
11943 is_attr_used,
11944 is_artificial);
11945
11946 type_handled = cxx_method_decl != NULL;
11947
11948 if (type_handled)
11949 {
11950 dwarf->LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
11951
11952 Host::SetCrashDescription (NULL);
11953
11954
11955 ClangASTMetadata metadata;
11956 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
11957
11958 if (!object_pointer_name.empty())
11959 {
11960 metadata.SetObjectPtrName(object_pointer_name.c_str());
11961 if (log)
11962 log->Printf ("Setting object pointer name: %s on method object %p.\n",
11963 object_pointer_name.c_str(),
11964 static_cast<void*>(cxx_method_decl));
11965 }
11966 SetMetadata (cxx_method_decl, metadata);
11967 }
11968 else
11969 {
11970 ignore_containing_context = true;
11971 }
11972 }
11973 }
11974 else
11975 {
11976 // We were asked to parse the type for a method in a class, yet the
11977 // class hasn't been asked to complete itself through the
11978 // clang::ExternalASTSource protocol, so we need to just have the
11979 // class complete itself and do things the right way, then our
11980 // DIE should then have an entry in the dwarf->m_die_to_type map. First
11981 // we need to modify the dwarf->m_die_to_type so it doesn't think we are
11982 // trying to parse this DIE anymore...
11983 dwarf->m_die_to_type[die] = NULL;
11984
11985 // Now we get the full type to force our class type to complete itself
11986 // using the clang::ExternalASTSource protocol which will parse all
11987 // base classes and all methods (including the method for this DIE).
11988 class_type->GetClangFullType();
11989
11990 // The type for this DIE should have been filled in the function call above
11991 type_ptr = dwarf->m_die_to_type[die];
11992 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
11993 {
11994 type_sp = type_ptr->shared_from_this();
11995 break;
11996 }
11997
11998 // FIXME This is fixing some even uglier behavior but we really need to
11999 // uniq the methods of each class as well as the class itself.
12000 // <rdar://problem/11240464>
12001 type_handled = true;
12002 }
12003 }
12004 }
12005 }
12006 }
12007 }
12008
12009 if (!type_handled)
12010 {
12011 // We just have a function that isn't part of a class
12012 clang::FunctionDecl *function_decl = CreateFunctionDeclaration (ignore_containing_context ? GetTranslationUnitDecl() : containing_decl_ctx,
12013 type_name_cstr,
12014 clang_type,
12015 storage,
12016 is_inline);
12017
12018 // if (template_param_infos.GetSize() > 0)
12019 // {
12020 // clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx,
12021 // function_decl,
12022 // type_name_cstr,
12023 // template_param_infos);
12024 //
12025 // CreateFunctionTemplateSpecializationInfo (function_decl,
12026 // func_template_decl,
12027 // template_param_infos);
12028 // }
12029 // Add the decl to our DIE to decl context map
12030 assert (function_decl);
12031 dwarf->LinkDeclContextToDIE(function_decl, die);
12032 if (!function_param_decls.empty())
12033 SetFunctionParameters (function_decl,
12034 &function_param_decls.front(),
12035 function_param_decls.size());
12036
12037 ClangASTMetadata metadata;
12038 metadata.SetUserID(dwarf->MakeUserID(die->GetOffset()));
12039
12040 if (!object_pointer_name.empty())
12041 {
12042 metadata.SetObjectPtrName(object_pointer_name.c_str());
12043 if (log)
12044 log->Printf ("Setting object pointer name: %s on function object %p.",
12045 object_pointer_name.c_str(),
12046 static_cast<void*>(function_decl));
12047 }
12048 SetMetadata (function_decl, metadata);
12049 }
12050 }
12051 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12052 dwarf,
12053 type_name_const_str,
12054 0,
12055 NULL,
12056 LLDB_INVALID_UID,
12057 Type::eEncodingIsUID,
12058 &decl,
12059 clang_type,
12060 Type::eResolveStateFull));
12061 assert(type_sp.get());
12062 }
12063 break;
12064
12065 case DW_TAG_array_type:
12066 {
12067 // Set a bit that lets us know that we are currently parsing this
12068 dwarf->m_die_to_type[die] = DIE_IS_BEING_PARSED;
12069
12070 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
12071 int64_t first_index = 0;
12072 uint32_t byte_stride = 0;
12073 uint32_t bit_stride = 0;
12074 bool is_vector = false;
12075 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12076
12077 if (num_attributes > 0)
12078 {
12079 uint32_t i;
12080 for (i=0; i<num_attributes; ++i)
12081 {
12082 attr = attributes.AttributeAtIndex(i);
12083 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12084 {
12085 switch (attr)
12086 {
12087 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
12088 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
12089 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
12090 case DW_AT_name:
12091 type_name_cstr = form_value.AsCString(&dwarf->get_debug_str_data());
12092 type_name_const_str.SetCString(type_name_cstr);
12093 break;
12094
12095 case DW_AT_type: type_die_offset = form_value.Reference(); break;
12096 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
12097 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
12098 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
12099 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break;
12100 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
12101 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break;
12102 case DW_AT_allocated:
12103 case DW_AT_associated:
12104 case DW_AT_data_location:
12105 case DW_AT_description:
12106 case DW_AT_ordering:
12107 case DW_AT_start_scope:
12108 case DW_AT_visibility:
12109 case DW_AT_specification:
12110 case DW_AT_abstract_origin:
12111 case DW_AT_sibling:
12112 break;
12113 }
12114 }
12115 }
12116
12117 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", dwarf->MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
12118
12119 Type *element_type = dwarf->ResolveTypeUID(type_die_offset);
12120
12121 if (element_type)
12122 {
12123 std::vector<uint64_t> element_orders;
12124 ParseChildArrayInfo(sc, dwarf, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
12125 if (byte_stride == 0 && bit_stride == 0)
12126 byte_stride = element_type->GetByteSize();
12127 CompilerType array_element_type = element_type->GetClangForwardType();
12128 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
12129 if (element_orders.size() > 0)
12130 {
12131 uint64_t num_elements = 0;
12132 std::vector<uint64_t>::const_reverse_iterator pos;
12133 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
12134 for (pos = element_orders.rbegin(); pos != end; ++pos)
12135 {
12136 num_elements = *pos;
12137 clang_type = CreateArrayType (array_element_type,
12138 num_elements,
12139 is_vector);
12140 array_element_type = clang_type;
12141 array_element_bit_stride = num_elements ?
12142 array_element_bit_stride * num_elements :
12143 array_element_bit_stride;
12144 }
12145 }
12146 else
12147 {
12148 clang_type = CreateArrayType (array_element_type, 0, is_vector);
12149 }
12150 ConstString empty_name;
12151 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12152 dwarf,
12153 empty_name,
12154 array_element_bit_stride / 8,
12155 NULL,
12156 type_die_offset,
12157 Type::eEncodingIsUID,
12158 &decl,
12159 clang_type,
12160 Type::eResolveStateFull));
12161 type_sp->SetEncodingType (element_type);
12162 }
12163 }
12164 }
12165 break;
12166
12167 case DW_TAG_ptr_to_member_type:
12168 {
12169 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
12170 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
12171
12172 const size_t num_attributes = die->GetAttributes(dwarf, dwarf_cu, NULL, attributes);
12173
12174 if (num_attributes > 0) {
12175 uint32_t i;
12176 for (i=0; i<num_attributes; ++i)
12177 {
12178 attr = attributes.AttributeAtIndex(i);
12179 if (attributes.ExtractFormValueAtIndex(dwarf, i, form_value))
12180 {
12181 switch (attr)
12182 {
12183 case DW_AT_type:
12184 type_die_offset = form_value.Reference(); break;
12185 case DW_AT_containing_type:
12186 containing_type_die_offset = form_value.Reference(); break;
12187 }
12188 }
12189 }
12190
12191 Type *pointee_type = dwarf->ResolveTypeUID(type_die_offset);
12192 Type *class_type = dwarf->ResolveTypeUID(containing_type_die_offset);
12193
12194 CompilerType pointee_clang_type = pointee_type->GetClangForwardType();
12195 CompilerType class_clang_type = class_type->GetClangLayoutType();
12196
12197 clang_type = ClangASTContext::CreateMemberPointerType(pointee_clang_type, class_clang_type);
12198
12199 byte_size = clang_type.GetByteSize(nullptr);
12200
12201 type_sp.reset( new Type (dwarf->MakeUserID(die->GetOffset()),
12202 dwarf,
12203 type_name_const_str,
12204 byte_size,
12205 NULL,
12206 LLDB_INVALID_UID,
12207 Type::eEncodingIsUID,
12208 NULL,
12209 clang_type,
12210 Type::eResolveStateForward));
12211 }
12212
12213 break;
12214 }
12215 default:
12216 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
12217 die->GetOffset(),
12218 tag,
12219 DW_TAG_value_to_name(tag));
12220 break;
12221 }
12222
12223 if (type_sp.get())
12224 {
12225 const DWARFDebugInfoEntry *sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
12226 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
12227
12228 SymbolContextScope * symbol_context_scope = NULL;
12229 if (sc_parent_tag == DW_TAG_compile_unit)
12230 {
12231 symbol_context_scope = sc.comp_unit;
12232 }
12233 else if (sc.function != NULL && sc_parent_die)
12234 {
12235 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(dwarf->MakeUserID(sc_parent_die->GetOffset()));
12236 if (symbol_context_scope == NULL)
12237 symbol_context_scope = sc.function;
12238 }
12239
12240 if (symbol_context_scope != NULL)
12241 {
12242 type_sp->SetSymbolContextScope(symbol_context_scope);
12243 }
12244
12245 // We are ready to put this type into the uniqued list up at the module level
12246 type_list->Insert (type_sp);
12247
12248 dwarf->m_die_to_type[die] = type_sp.get();
12249 }
12250 }
12251 else if (type_ptr != DIE_IS_BEING_PARSED)
12252 {
12253 type_sp = type_ptr->shared_from_this();
12254 }
12255 }
12256 return type_sp;
12257}
12258
12259
12260bool
12261ClangASTContext::CopyUniqueClassMethodTypes (SymbolFileDWARF *dst_symfile,
12262 SymbolFileDWARF *src_symfile,
Yaron Kerenfe16dea2015-08-16 19:40:40 +000012263 lldb_private::Type *class_type,
Greg Clayton8b4edba2015-08-14 20:02:05 +000012264 DWARFCompileUnit* src_cu,
12265 const DWARFDebugInfoEntry *src_class_die,
12266 DWARFCompileUnit* dst_cu,
12267 const DWARFDebugInfoEntry *dst_class_die,
12268 DWARFDIECollection &failures)
12269{
12270 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
12271 return false;
12272 if (src_class_die->Tag() != dst_class_die->Tag())
12273 return false;
12274
12275 // We need to complete the class type so we can get all of the method types
12276 // parsed so we can then unique those types to their equivalent counterparts
12277 // in "dst_cu" and "dst_class_die"
12278 class_type->GetClangFullType();
12279
12280 const DWARFDebugInfoEntry *src_die;
12281 const DWARFDebugInfoEntry *dst_die;
12282 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
12283 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
12284 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial;
12285 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial;
12286 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
12287 {
12288 if (src_die->Tag() == DW_TAG_subprogram)
12289 {
12290 // Make sure this is a declaration and not a concrete instance by looking
12291 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12292 // are placed inside the class definitions and shouldn't be included in
12293 // the list of things are are tracking here.
12294 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1)
12295 {
12296 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12297 if (src_name)
12298 {
12299 ConstString src_const_name(src_name);
12300 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0))
12301 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
12302 else
12303 src_name_to_die.Append(src_const_name.GetCString(), src_die);
12304 }
12305 }
12306 }
12307 }
12308 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
12309 {
12310 if (dst_die->Tag() == DW_TAG_subprogram)
12311 {
12312 // Make sure this is a declaration and not a concrete instance by looking
12313 // for DW_AT_declaration set to 1. Sometimes concrete function instances
12314 // are placed inside the class definitions and shouldn't be included in
12315 // the list of things are are tracking here.
12316 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_declaration, 0) == 1)
12317 {
12318 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12319 if (dst_name)
12320 {
12321 ConstString dst_const_name(dst_name);
12322 if (dst_die->GetAttributeValueAsUnsigned(dst_symfile, dst_cu, DW_AT_artificial, 0))
12323 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
12324 else
12325 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
12326 }
12327 }
12328 }
12329 }
12330 const uint32_t src_size = src_name_to_die.GetSize ();
12331 const uint32_t dst_size = dst_name_to_die.GetSize ();
12332 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
12333
12334 // Is everything kosher so we can go through the members at top speed?
12335 bool fast_path = true;
12336
12337 if (src_size != dst_size)
12338 {
12339 if (src_size != 0 && dst_size != 0)
12340 {
12341 if (log)
12342 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
12343 src_class_die->GetOffset(),
12344 dst_class_die->GetOffset(),
12345 src_size,
12346 dst_size);
12347 }
12348
12349 fast_path = false;
12350 }
12351
12352 uint32_t idx;
12353
12354 if (fast_path)
12355 {
12356 for (idx = 0; idx < src_size; ++idx)
12357 {
12358 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12359 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12360
12361 if (src_die->Tag() != dst_die->Tag())
12362 {
12363 if (log)
12364 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
12365 src_class_die->GetOffset(),
12366 dst_class_die->GetOffset(),
12367 src_die->GetOffset(),
12368 DW_TAG_value_to_name(src_die->Tag()),
12369 dst_die->GetOffset(),
12370 DW_TAG_value_to_name(src_die->Tag()));
12371 fast_path = false;
12372 }
12373
12374 const char *src_name = src_die->GetMangledName (src_symfile, src_cu);
12375 const char *dst_name = dst_die->GetMangledName (dst_symfile, dst_cu);
12376
12377 // Make sure the names match
12378 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
12379 continue;
12380
12381 if (log)
12382 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
12383 src_class_die->GetOffset(),
12384 dst_class_die->GetOffset(),
12385 src_die->GetOffset(),
12386 src_name,
12387 dst_die->GetOffset(),
12388 dst_name);
12389
12390 fast_path = false;
12391 }
12392 }
12393
12394 // Now do the work of linking the DeclContexts and Types.
12395 if (fast_path)
12396 {
12397 // We can do this quickly. Just run across the tables index-for-index since
12398 // we know each node has matching names and tags.
12399 for (idx = 0; idx < src_size; ++idx)
12400 {
12401 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
12402 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
12403
12404 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
12405 if (src_decl_ctx)
12406 {
12407 if (log)
12408 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12409 static_cast<void*>(src_decl_ctx),
12410 src_die->GetOffset(), dst_die->GetOffset());
12411 dst_symfile->LinkDeclContextToDIE (src_decl_ctx, dst_die);
12412 }
12413 else
12414 {
12415 if (log)
12416 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
12417 src_die->GetOffset(), dst_die->GetOffset());
12418 }
12419
12420 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12421 if (src_child_type)
12422 {
12423 if (log)
12424 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12425 static_cast<void*>(src_child_type),
12426 src_child_type->GetID(),
12427 src_die->GetOffset(), dst_die->GetOffset());
12428 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12429 }
12430 else
12431 {
12432 if (log)
12433 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12434 }
12435 }
12436 }
12437 else
12438 {
12439 // We must do this slowly. For each member of the destination, look
12440 // up a member in the source with the same name, check its tag, and
12441 // unique them if everything matches up. Report failures.
12442
12443 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
12444 {
12445 src_name_to_die.Sort();
12446
12447 for (idx = 0; idx < dst_size; ++idx)
12448 {
12449 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
12450 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
12451 src_die = src_name_to_die.Find(dst_name, NULL);
12452
12453 if (src_die && (src_die->Tag() == dst_die->Tag()))
12454 {
12455 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die];
12456 if (src_decl_ctx)
12457 {
12458 if (log)
12459 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12460 static_cast<void*>(src_decl_ctx),
12461 src_die->GetOffset(),
12462 dst_die->GetOffset());
12463 dst_symfile->LinkDeclContextToDIE (src_decl_ctx, dst_die);
12464 }
12465 else
12466 {
12467 if (log)
12468 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12469 }
12470
12471 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12472 if (src_child_type)
12473 {
12474 if (log)
12475 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12476 static_cast<void*>(src_child_type),
12477 src_child_type->GetID(),
12478 src_die->GetOffset(),
12479 dst_die->GetOffset());
12480 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12481 }
12482 else
12483 {
12484 if (log)
12485 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12486 }
12487 }
12488 else
12489 {
12490 if (log)
12491 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset());
12492
12493 failures.Append(dst_die);
12494 }
12495 }
12496 }
12497 }
12498
12499 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
12500 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
12501
12502 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src;
12503
12504 if (src_size_artificial && dst_size_artificial)
12505 {
12506 dst_name_to_die_artificial.Sort();
12507
12508 for (idx = 0; idx < src_size_artificial; ++idx)
12509 {
12510 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
12511 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12512 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL);
12513
12514 if (dst_die)
12515 {
12516 // Both classes have the artificial types, link them
12517 clang::DeclContext *src_decl_ctx = dst_symfile->m_die_to_decl_ctx[src_die];
12518 if (src_decl_ctx)
12519 {
12520 if (log)
12521 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
12522 static_cast<void*>(src_decl_ctx),
12523 src_die->GetOffset(), dst_die->GetOffset());
12524 dst_symfile->LinkDeclContextToDIE (src_decl_ctx, dst_die);
12525 }
12526 else
12527 {
12528 if (log)
12529 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12530 }
12531
12532 Type *src_child_type = dst_symfile->m_die_to_type[src_die];
12533 if (src_child_type)
12534 {
12535 if (log)
12536 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
12537 static_cast<void*>(src_child_type),
12538 src_child_type->GetID(),
12539 src_die->GetOffset(), dst_die->GetOffset());
12540 dst_symfile->m_die_to_type[dst_die] = src_child_type;
12541 }
12542 else
12543 {
12544 if (log)
12545 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset());
12546 }
12547 }
12548 }
12549 }
12550
12551 if (dst_size_artificial)
12552 {
12553 for (idx = 0; idx < dst_size_artificial; ++idx)
12554 {
12555 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
12556 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
12557 if (log)
12558 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial);
12559
12560 failures.Append(dst_die);
12561 }
12562 }
12563
12564 return (failures.Size() != 0);
12565}
12566
12567
12568bool
12569ClangASTContext::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
12570 SymbolFileDWARF *dwarf,
12571 DWARFCompileUnit *cu,
12572 const DWARFDebugInfoEntry *die)
12573{
12574 // No namespace specified, so the answer is
12575 if (namespace_decl == NULL)
12576 return true;
12577
12578 Log *log = nullptr; //(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
12579
12580 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
12581 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (dwarf, cu, die, &decl_ctx_die);
12582 if (decl_ctx_die)
12583 {
12584 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
12585
12586 if (clang_namespace_decl)
12587 {
12588 if (decl_ctx_die->Tag() != DW_TAG_namespace)
12589 {
12590 if (log)
12591 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace");
12592 return false;
12593 }
12594
12595 if (clang_namespace_decl == die_clang_decl_ctx)
12596 return true;
12597 else
12598 return false;
12599 }
12600 else
12601 {
12602 // We have a namespace_decl that was not NULL but it contained
12603 // a NULL "clang::NamespaceDecl", so this means the global namespace
12604 // So as long the contained decl context DIE isn't a namespace
12605 // we should be ok.
12606 if (decl_ctx_die->Tag() != DW_TAG_namespace)
12607 return true;
12608 }
12609 }
12610
12611 if (log)
12612 dwarf->GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist");
12613
12614 return false;
12615}
12616