blob: 7b115ac32808f4e8cc468f0652babf4b1b2d2d10 [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"
Greg Clayton73b472d2010-10-27 03:32:59 +000065#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000066#include "lldb/Core/Log.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000067#include "lldb/Core/Module.h"
68#include "lldb/Core/PluginManager.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000069#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000070#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000071#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000072#include "lldb/Core/UniqueCStringMap.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000073#include "lldb/Expression/ASTDumper.h"
Jim Ingham151c0322015-09-15 21:13:50 +000074#include "lldb/Expression/ASTResultSynthesizer.h"
75#include "lldb/Expression/ClangExpressionDeclMap.h"
76#include "lldb/Expression/ClangUserExpression.h"
77#include "lldb/Expression/ClangFunctionCaller.h"
78#include "lldb/Expression/ClangUtilityFunction.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000080#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000081#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000082#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000083#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000084#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000085#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000086#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000088#include "lldb/Target/Process.h"
89#include "lldb/Target/Target.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000090
Greg Clayton261ac3f2015-08-28 01:01:03 +000091#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
92
Eli Friedman932197d2010-06-13 19:06:42 +000093#include <stdio.h>
94
Greg Clayton1341baf2013-07-11 23:36:31 +000095#include <mutex>
96
Greg Claytonc86103d2010-08-05 01:57:25 +000097using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098using namespace lldb_private;
99using namespace llvm;
100using namespace clang;
101
Greg Clayton56939cb2015-09-17 22:23:34 +0000102namespace
103{
104 static inline bool ClangASTContextSupportsLanguage (lldb::LanguageType language)
105 {
106 return language == eLanguageTypeUnknown || // Clang is the default type system
107 Language::LanguageIsC (language) ||
108 Language::LanguageIsCPlusPlus (language) ||
109 Language::LanguageIsObjC (language);
110 }
111}
112
Enrico Granata2267ad42014-09-16 17:28:40 +0000113typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000114
115static ClangASTMap &
116GetASTMap()
117{
Enrico Granata2267ad42014-09-16 17:28:40 +0000118 static ClangASTMap *g_map_ptr = nullptr;
119 static std::once_flag g_once_flag;
120 std::call_once(g_once_flag, []() {
121 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
122 });
123 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000124}
125
126
Greg Clayton57ee3062013-07-11 22:46:58 +0000127clang::AccessSpecifier
128ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000129{
130 switch (access)
131 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000132 default: break;
133 case eAccessNone: return AS_none;
134 case eAccessPublic: return AS_public;
135 case eAccessPrivate: return AS_private;
136 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000137 }
138 return AS_none;
139}
140
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000142ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143{
144 // FIXME: Cleanup per-file based stuff.
145
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000146 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 // to move these to the language standard, and have the driver resolve the
148 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000149 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000151 } else if (IK == IK_ObjC ||
152 IK == IK_ObjCXX ||
153 IK == IK_PreprocessedObjC ||
154 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155 Opts.ObjC1 = Opts.ObjC2 = 1;
156 }
157
158 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
159
160 if (LangStd == LangStandard::lang_unspecified) {
161 // Based on the base language, pick one.
162 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000163 case IK_None:
164 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000165 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000166 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000167 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 LangStd = LangStandard::lang_opencl;
169 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000170 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000171 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000172 LangStd = LangStandard::lang_cuda;
173 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000174 case IK_Asm:
175 case IK_C:
176 case IK_PreprocessedC:
177 case IK_ObjC:
178 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 LangStd = LangStandard::lang_gnu99;
180 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000181 case IK_CXX:
182 case IK_PreprocessedCXX:
183 case IK_ObjCXX:
184 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 LangStd = LangStandard::lang_gnucxx98;
186 break;
187 }
188 }
189
190 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000191 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 Opts.C99 = Std.isC99();
193 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000194 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 Opts.Digraphs = Std.hasDigraphs();
196 Opts.GNUMode = Std.isGNUMode();
197 Opts.GNUInline = !Std.isC99();
198 Opts.HexFloats = Std.hasHexFloats();
199 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000200
201 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202
203 // OpenCL has some additional defaults.
204 if (LangStd == LangStandard::lang_opencl) {
205 Opts.OpenCL = 1;
206 Opts.AltiVec = 1;
207 Opts.CXXOperatorNames = 1;
208 Opts.LaxVectorConversions = 1;
209 }
210
211 // OpenCL and C++ both have bool, true, false keywords.
212 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
213
214// if (Opts.CPlusPlus)
215// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
216//
217// if (Args.hasArg(OPT_fobjc_gc_only))
218// Opts.setGCMode(LangOptions::GCOnly);
219// else if (Args.hasArg(OPT_fobjc_gc))
220// Opts.setGCMode(LangOptions::HybridGC);
221//
222// if (Args.hasArg(OPT_print_ivar_layout))
223// Opts.ObjCGCBitmapPrint = 1;
224//
225// if (Args.hasArg(OPT_faltivec))
226// Opts.AltiVec = 1;
227//
228// if (Args.hasArg(OPT_pthread))
229// Opts.POSIXThreads = 1;
230//
231// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
232// "default");
233// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000234 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235// else if (Vis == "hidden")
236// Opts.setVisibilityMode(LangOptions::Hidden);
237// else if (Vis == "protected")
238// Opts.setVisibilityMode(LangOptions::Protected);
239// else
240// Diags.Report(diag::err_drv_invalid_value)
241// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
242
243// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
244
245 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
246 // is specified, or -std is set to a conforming mode.
247 Opts.Trigraphs = !Opts.GNUMode;
248// if (Args.hasArg(OPT_trigraphs))
249// Opts.Trigraphs = 1;
250//
251// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
252// OPT_fno_dollars_in_identifiers,
253// !Opts.AsmPreprocessor);
254// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
255// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
256// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
257// if (Args.hasArg(OPT_fno_lax_vector_conversions))
258// Opts.LaxVectorConversions = 0;
259// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
260// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
261// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000262 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
264// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
265// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
266// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
267// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
268// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
269// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
270// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
271// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
272// Diags);
273// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
274// Opts.ObjCConstantStringClass = getLastArgValue(Args,
275// OPT_fconstant_string_class);
276// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
277// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
278// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
279// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
280// Opts.Static = Args.hasArg(OPT_static_define);
281 Opts.OptimizeSize = 0;
282
283 // FIXME: Eliminate this dependency.
284// unsigned Opt =
285// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
286// Opts.Optimize = Opt != 0;
287 unsigned Opt = 0;
288
289 // This is the __NO_INLINE__ define, which just depends on things like the
290 // optimization level and -fno-inline, not actually whether the backend has
291 // inlining enabled.
292 //
293 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000294 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295
296// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
297// switch (SSP) {
298// default:
299// Diags.Report(diag::err_drv_invalid_value)
300// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
301// break;
302// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
303// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
304// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
305// }
306}
307
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Greg Claytonf73034f2015-09-08 18:15:05 +0000309ClangASTContext::ClangASTContext (const char *target_triple) :
310 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000311 m_target_triple (),
312 m_ast_ap (),
313 m_language_options_ap (),
314 m_source_manager_ap (),
315 m_diagnostics_engine_ap (),
316 m_target_options_rp (),
317 m_target_info_ap (),
318 m_identifier_table_ap (),
319 m_selector_table_ap (),
320 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000321 m_callback_tag_decl (nullptr),
322 m_callback_objc_decl (nullptr),
323 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000324 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000325 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
327 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000328 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
331//----------------------------------------------------------------------
332// Destructor
333//----------------------------------------------------------------------
334ClangASTContext::~ClangASTContext()
335{
Enrico Granata5d84a692014-08-19 21:46:37 +0000336 if (m_ast_ap.get())
337 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000338 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000339 if (!m_ast_owned)
340 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000341 }
342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 m_builtins_ap.reset();
344 m_selector_table_ap.reset();
345 m_identifier_table_ap.reset();
346 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000347 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000348 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 m_source_manager_ap.reset();
350 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000351 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
Greg Clayton56939cb2015-09-17 22:23:34 +0000354ConstString
355ClangASTContext::GetPluginNameStatic()
356{
357 return ConstString("clang");
358}
359
360ConstString
361ClangASTContext::GetPluginName()
362{
363 return ClangASTContext::GetPluginNameStatic();
364}
365
366uint32_t
367ClangASTContext::GetPluginVersion()
368{
369 return 1;
370}
371
372lldb::TypeSystemSP
373ClangASTContext::CreateInstance (lldb::LanguageType language, const lldb_private::ArchSpec &arch)
374{
375 if (ClangASTContextSupportsLanguage(language))
376 {
377 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
378 if (ast_sp)
379 {
380 if (arch.IsValid())
381 {
382 ArchSpec fixed_arch = arch;
383 // LLVM wants this to be set to iOS or MacOSX; if we're working on
384 // a bare-boards type image, change the triple for llvm's benefit.
385 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
386 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
387 {
388 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
389 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
390 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb)
391 {
392 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
393 }
394 else
395 {
396 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
397 }
398 }
399 ast_sp->SetArchitecture (fixed_arch);
400 }
401 }
402 return ast_sp;
403 }
404 return lldb::TypeSystemSP();
405}
406
407
408void
409ClangASTContext::Initialize()
410{
411 PluginManager::RegisterPlugin (GetPluginNameStatic(),
412 "clang base AST context plug-in",
413 CreateInstance);
414}
415
416void
417ClangASTContext::Terminate()
418{
419 PluginManager::UnregisterPlugin (CreateInstance);
420}
421
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422
423void
424ClangASTContext::Clear()
425{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000426 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 m_language_options_ap.reset();
428 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000429 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000430 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 m_target_info_ap.reset();
432 m_identifier_table_ap.reset();
433 m_selector_table_ap.reset();
434 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000435 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436}
437
438const char *
439ClangASTContext::GetTargetTriple ()
440{
441 return m_target_triple.c_str();
442}
443
444void
445ClangASTContext::SetTargetTriple (const char *target_triple)
446{
447 Clear();
448 m_target_triple.assign(target_triple);
449}
450
Greg Clayton514487e2011-02-15 21:59:32 +0000451void
452ClangASTContext::SetArchitecture (const ArchSpec &arch)
453{
Greg Clayton880cbb02011-07-30 01:26:02 +0000454 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000455}
456
Greg Clayton6beaaa62011-01-17 03:46:26 +0000457bool
458ClangASTContext::HasExternalSource ()
459{
460 ASTContext *ast = getASTContext();
461 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000462 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000463 return false;
464}
465
466void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000467ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000468{
469 ASTContext *ast = getASTContext();
470 if (ast)
471 {
472 ast->setExternalSource (ast_source_ap);
473 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
474 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
475 }
476}
477
478void
479ClangASTContext::RemoveExternalSource ()
480{
481 ASTContext *ast = getASTContext();
482
483 if (ast)
484 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000485 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000486 ast->setExternalSource (empty_ast_source_ap);
487 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
488 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
489 }
490}
491
Greg Claytond8d4a572015-08-11 21:38:15 +0000492void
493ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
494{
495 if (!m_ast_owned) {
496 m_ast_ap.release();
497 }
498 m_ast_owned = false;
499 m_ast_ap.reset(ast_ctx);
500 GetASTMap().Insert(ast_ctx, this);
501}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502
503ASTContext *
504ClangASTContext::getASTContext()
505{
Ed Masted4612ad2014-04-20 13:17:36 +0000506 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000508 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000509 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
510 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000511 *getIdentifierTable(),
512 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000513 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000514
515 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000516
517 // This can be NULL if we don't know anything about the architecture or if the
518 // target for an architecture isn't enabled in the llvm/clang that we built
519 TargetInfo *target_info = getTargetInfo();
520 if (target_info)
521 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000522
Greg Clayton6beaaa62011-01-17 03:46:26 +0000523 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
524 {
525 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
526 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
527 }
528
Enrico Granata2267ad42014-09-16 17:28:40 +0000529 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000530
531 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
532 ClangASTContext::CompleteObjCInterfaceDecl,
533 nullptr,
534 ClangASTContext::LayoutRecordType,
535 this));
536 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000537 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000538 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539}
540
Enrico Granata5d84a692014-08-19 21:46:37 +0000541ClangASTContext*
542ClangASTContext::GetASTContext (clang::ASTContext* ast)
543{
Enrico Granata2267ad42014-09-16 17:28:40 +0000544 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000545 return clang_ast;
546}
547
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548Builtin::Context *
549ClangASTContext::getBuiltinContext()
550{
Ed Masted4612ad2014-04-20 13:17:36 +0000551 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000552 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 return m_builtins_ap.get();
554}
555
556IdentifierTable *
557ClangASTContext::getIdentifierTable()
558{
Ed Masted4612ad2014-04-20 13:17:36 +0000559 if (m_identifier_table_ap.get() == nullptr)
560 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 return m_identifier_table_ap.get();
562}
563
564LangOptions *
565ClangASTContext::getLanguageOptions()
566{
Ed Masted4612ad2014-04-20 13:17:36 +0000567 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 {
569 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000570 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000571// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 }
573 return m_language_options_ap.get();
574}
575
576SelectorTable *
577ClangASTContext::getSelectorTable()
578{
Ed Masted4612ad2014-04-20 13:17:36 +0000579 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580 m_selector_table_ap.reset (new SelectorTable());
581 return m_selector_table_ap.get();
582}
583
Sean Callanan79439e82010-11-18 02:56:27 +0000584clang::FileManager *
585ClangASTContext::getFileManager()
586{
Ed Masted4612ad2014-04-20 13:17:36 +0000587 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000588 {
589 clang::FileSystemOptions file_system_options;
590 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
591 }
Sean Callanan79439e82010-11-18 02:56:27 +0000592 return m_file_manager_ap.get();
593}
594
Greg Claytone1a916a2010-07-21 22:12:05 +0000595clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596ClangASTContext::getSourceManager()
597{
Ed Masted4612ad2014-04-20 13:17:36 +0000598 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000599 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600 return m_source_manager_ap.get();
601}
602
Sean Callanan880e6802011-10-07 23:18:13 +0000603clang::DiagnosticsEngine *
604ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605{
Ed Masted4612ad2014-04-20 13:17:36 +0000606 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000607 {
608 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000609 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000610 }
Sean Callanan880e6802011-10-07 23:18:13 +0000611 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612}
613
Sean Callanan880e6802011-10-07 23:18:13 +0000614class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000615{
616public:
Sean Callanan880e6802011-10-07 23:18:13 +0000617 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000618 {
619 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
620 }
621
Sean Callanan880e6802011-10-07 23:18:13 +0000622 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000623 {
624 if (m_log)
625 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000626 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000627 info.FormatDiagnostic(diag_str);
628 diag_str.push_back('\0');
629 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
630 }
631 }
Sean Callanan880e6802011-10-07 23:18:13 +0000632
633 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
634 {
635 return new NullDiagnosticConsumer ();
636 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000637private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000638 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000639};
640
Sean Callanan880e6802011-10-07 23:18:13 +0000641DiagnosticConsumer *
642ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000643{
Ed Masted4612ad2014-04-20 13:17:36 +0000644 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000645 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000646
Sean Callanan880e6802011-10-07 23:18:13 +0000647 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000648}
649
Jason Molenda45938b92014-07-08 23:46:39 +0000650std::shared_ptr<TargetOptions> &
651ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000652 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 {
Alp Toker5f838642014-07-06 05:36:57 +0000654 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000655 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000656 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 }
Alp Toker5f838642014-07-06 05:36:57 +0000658 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
661
662TargetInfo *
663ClangASTContext::getTargetInfo()
664{
Greg Clayton70512312012-05-08 01:45:38 +0000665 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000666 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000667 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 return m_target_info_ap.get();
669}
670
671#pragma mark Basic Types
672
673static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000674QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000676 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 if (qual_type_bit_size == bit_size)
678 return true;
679 return false;
680}
Greg Clayton56939cb2015-09-17 22:23:34 +0000681
Greg Claytona1e5dc82015-08-11 22:53:00 +0000682CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +0000683ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, size_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684{
Greg Clayton57ee3062013-07-11 22:46:58 +0000685 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686}
687
Greg Claytona1e5dc82015-08-11 22:53:00 +0000688CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000689ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000691 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000692 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 switch (encoding)
694 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000695 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000696 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000697 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 break;
699
Greg Claytonc86103d2010-08-05 01:57:25 +0000700 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000701 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000702 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000703 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000704 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000705 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000706 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000707 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000708 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000709 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000710 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000711 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000712 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 break;
714
Greg Claytonc86103d2010-08-05 01:57:25 +0000715 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000716 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000717 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000718 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000719 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000720 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000721 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000722 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000723 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000724 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000725 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000726 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000727 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 break;
729
Greg Claytonc86103d2010-08-05 01:57:25 +0000730 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000731 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000732 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000733 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000734 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000735 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000736 return CompilerType (ast, ast->LongDoubleTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 break;
738
Greg Claytonc86103d2010-08-05 01:57:25 +0000739 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000740 // Sanity check that bit_size is a multiple of 8's.
741 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000742 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000743 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 }
745
Greg Claytona1e5dc82015-08-11 22:53:00 +0000746 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747}
748
Greg Clayton57ee3062013-07-11 22:46:58 +0000749
750
751lldb::BasicType
752ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
753{
754 if (name)
755 {
756 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
757 static TypeNameToBasicTypeMap g_type_map;
758 static std::once_flag g_once_flag;
759 std::call_once(g_once_flag, [](){
760 // "void"
761 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
762
763 // "char"
764 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
765 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
766 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
767 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
768 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
769 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
770 // "short"
771 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
772 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
773 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
774 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
775
776 // "int"
777 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
778 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
779 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
780 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
781
782 // "long"
783 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
784 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
785 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
786 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
787
788 // "long long"
789 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
790 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
791 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
792 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
793
794 // "int128"
795 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
796 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
797
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000798 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000799 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
800 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
801 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
802 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
803 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
804 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
805 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
806 g_type_map.Sort();
807 });
808
809 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
810 }
811 return eBasicTypeInvalid;
812}
813
Greg Claytona1e5dc82015-08-11 22:53:00 +0000814CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000815ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
816{
817 if (ast)
818 {
819 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
820 return ClangASTContext::GetBasicType (ast, basic_type);
821 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000822 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000823}
824
825uint32_t
826ClangASTContext::GetPointerByteSize ()
827{
828 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000829 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000830 return m_pointer_byte_size;
831}
832
Greg Claytona1e5dc82015-08-11 22:53:00 +0000833CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000834ClangASTContext::GetBasicType (lldb::BasicType basic_type)
835{
836 return GetBasicType (getASTContext(), basic_type);
837}
838
Greg Claytona1e5dc82015-08-11 22:53:00 +0000839CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000840ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
841{
842 if (ast)
843 {
Ed Masted4612ad2014-04-20 13:17:36 +0000844 clang_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000845
846 switch (basic_type)
847 {
848 case eBasicTypeInvalid:
849 case eBasicTypeOther:
850 break;
851 case eBasicTypeVoid:
852 clang_type = ast->VoidTy.getAsOpaquePtr();
853 break;
854 case eBasicTypeChar:
855 clang_type = ast->CharTy.getAsOpaquePtr();
856 break;
857 case eBasicTypeSignedChar:
858 clang_type = ast->SignedCharTy.getAsOpaquePtr();
859 break;
860 case eBasicTypeUnsignedChar:
861 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
862 break;
863 case eBasicTypeWChar:
864 clang_type = ast->getWCharType().getAsOpaquePtr();
865 break;
866 case eBasicTypeSignedWChar:
867 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
868 break;
869 case eBasicTypeUnsignedWChar:
870 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
871 break;
872 case eBasicTypeChar16:
873 clang_type = ast->Char16Ty.getAsOpaquePtr();
874 break;
875 case eBasicTypeChar32:
876 clang_type = ast->Char32Ty.getAsOpaquePtr();
877 break;
878 case eBasicTypeShort:
879 clang_type = ast->ShortTy.getAsOpaquePtr();
880 break;
881 case eBasicTypeUnsignedShort:
882 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
883 break;
884 case eBasicTypeInt:
885 clang_type = ast->IntTy.getAsOpaquePtr();
886 break;
887 case eBasicTypeUnsignedInt:
888 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
889 break;
890 case eBasicTypeLong:
891 clang_type = ast->LongTy.getAsOpaquePtr();
892 break;
893 case eBasicTypeUnsignedLong:
894 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
895 break;
896 case eBasicTypeLongLong:
897 clang_type = ast->LongLongTy.getAsOpaquePtr();
898 break;
899 case eBasicTypeUnsignedLongLong:
900 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
901 break;
902 case eBasicTypeInt128:
903 clang_type = ast->Int128Ty.getAsOpaquePtr();
904 break;
905 case eBasicTypeUnsignedInt128:
906 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
907 break;
908 case eBasicTypeBool:
909 clang_type = ast->BoolTy.getAsOpaquePtr();
910 break;
911 case eBasicTypeHalf:
912 clang_type = ast->HalfTy.getAsOpaquePtr();
913 break;
914 case eBasicTypeFloat:
915 clang_type = ast->FloatTy.getAsOpaquePtr();
916 break;
917 case eBasicTypeDouble:
918 clang_type = ast->DoubleTy.getAsOpaquePtr();
919 break;
920 case eBasicTypeLongDouble:
921 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
922 break;
923 case eBasicTypeFloatComplex:
924 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
925 break;
926 case eBasicTypeDoubleComplex:
927 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
928 break;
929 case eBasicTypeLongDoubleComplex:
930 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
931 break;
932 case eBasicTypeObjCID:
933 clang_type = ast->getObjCIdType().getAsOpaquePtr();
934 break;
935 case eBasicTypeObjCClass:
936 clang_type = ast->getObjCClassType().getAsOpaquePtr();
937 break;
938 case eBasicTypeObjCSel:
939 clang_type = ast->getObjCSelType().getAsOpaquePtr();
940 break;
941 case eBasicTypeNullPtr:
942 clang_type = ast->NullPtrTy.getAsOpaquePtr();
943 break;
944 }
945
946 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000947 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +0000948 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000949 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000950}
951
952
Greg Claytona1e5dc82015-08-11 22:53:00 +0000953CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
955{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000956 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000957
958#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000959 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000960 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 {
962 switch (dw_ate)
963 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000964 default:
965 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000966
Sean Callanan38d4df52012-04-03 01:10:10 +0000967 case DW_ATE_address:
968 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000969 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000970 break;
971
972 case DW_ATE_boolean:
973 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000974 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000975 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000976 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000977 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000978 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000979 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000980 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000981 break;
982
983 case DW_ATE_lo_user:
984 // This has been seen to mean DW_AT_complex_integer
985 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000986 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000987 if (::strstr(type_name, "complex"))
988 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000989 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
990 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000991 }
Greg Clayton605684e2011-10-28 23:06:08 +0000992 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000993 break;
994
995 case DW_ATE_complex_float:
996 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000997 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000998 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000999 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001000 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001001 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001002 else
Greg Clayton605684e2011-10-28 23:06:08 +00001003 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001004 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
1005 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +00001006 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001007 break;
1008
1009 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +00001010 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001011 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001012 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001014 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001015 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +00001016 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +00001017 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001018 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001019 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001020 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001021 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001022 return CompilerType (ast, ast->LongDoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001023 break;
1024
1025 case DW_ATE_signed:
1026 if (type_name)
1027 {
1028 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001029 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001030 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001031 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001032 if (streq(type_name, "void") &&
1033 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001034 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001035 if (strstr(type_name, "long long") &&
1036 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001037 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001038 if (strstr(type_name, "long") &&
1039 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001040 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001041 if (strstr(type_name, "short") &&
1042 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001043 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001044 if (strstr(type_name, "char"))
1045 {
1046 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001047 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001048 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001049 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001050 }
1051 if (strstr(type_name, "int"))
1052 {
1053 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001054 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001055 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001056 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001057 }
1058 }
1059 // We weren't able to match up a type name, just search by size
1060 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001061 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001062 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001063 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001064 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001065 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001066 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001067 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001068 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001069 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001070 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001071 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001072 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001073
Sean Callanan38d4df52012-04-03 01:10:10 +00001074 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001075 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +00001076 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001077 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001078 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001079 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001080 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001081 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001082 break;
1083
1084 case DW_ATE_unsigned:
1085 if (type_name)
1086 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001087 if (streq(type_name, "wchar_t"))
1088 {
1089 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1090 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001091 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001092 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001093 }
1094 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001095 if (strstr(type_name, "long long"))
1096 {
1097 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001098 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001099 }
1100 else if (strstr(type_name, "long"))
1101 {
1102 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001103 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001104 }
1105 else if (strstr(type_name, "short"))
1106 {
1107 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001108 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001109 }
1110 else if (strstr(type_name, "char"))
1111 {
1112 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001113 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001114 }
1115 else if (strstr(type_name, "int"))
1116 {
1117 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001118 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001119 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001120 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001121 }
1122 }
1123 // We weren't able to match up a type name, just search by size
1124 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001125 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001126 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001127 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001128 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001129 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001130 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001131 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001132 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001133 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001134 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001135 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001136 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001137
Sean Callanan38d4df52012-04-03 01:10:10 +00001138 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001139 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1140 {
1141 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001142 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001143 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001144 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001145 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001146 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001147 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001148 break;
1149
1150 case DW_ATE_imaginary_float:
1151 break;
1152
1153 case DW_ATE_UTF:
1154 if (type_name)
1155 {
1156 if (streq(type_name, "char16_t"))
1157 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001158 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001159 }
1160 else if (streq(type_name, "char32_t"))
1161 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001162 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001163 }
1164 }
1165 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 }
1167 }
1168 // This assert should fire for anything that we don't catch above so we know
1169 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001170 if (type_name)
1171 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001172 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 +00001173 }
1174 else
1175 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001176 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 +00001177 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001178 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179}
1180
Greg Claytona1e5dc82015-08-11 22:53:00 +00001181CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001182ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1183{
Greg Clayton57ee3062013-07-11 22:46:58 +00001184 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001185 return CompilerType (ast, ast->UnknownAnyTy);
1186 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001187}
1188
Greg Claytona1e5dc82015-08-11 22:53:00 +00001189CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190ClangASTContext::GetCStringType (bool is_const)
1191{
Greg Clayton57ee3062013-07-11 22:46:58 +00001192 ASTContext *ast = getASTContext();
1193 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194
1195 if (is_const)
1196 char_type.addConst();
1197
Greg Claytona1e5dc82015-08-11 22:53:00 +00001198 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199}
1200
Sean Callanan09ab4b72011-11-30 22:11:59 +00001201clang::DeclContext *
1202ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1203{
1204 return ast->getTranslationUnitDecl();
1205}
1206
Greg Clayton526e5af2010-11-13 03:52:47 +00001207clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001208ClangASTContext::CopyDecl (ASTContext *dst_ast,
1209 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001210 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001211{
Sean Callanan79439e82010-11-18 02:56:27 +00001212 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001213 FileManager file_manager (file_system_options);
1214 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001215 *src_ast, file_manager,
1216 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001217
1218 return importer.Import(source_decl);
1219}
1220
Sean Callanan23a30272010-07-16 00:00:27 +00001221bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001222ClangASTContext::AreTypesSame (CompilerType type1,
1223 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001224 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001225{
Greg Claytonf73034f2015-09-08 18:15:05 +00001226 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1227 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001228 return false;
1229
1230 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001231 return true;
1232
Greg Claytond8d4a572015-08-11 21:38:15 +00001233 QualType type1_qual = GetQualType(type1);
1234 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001235
1236 if (ignore_qualifiers)
1237 {
1238 type1_qual = type1_qual.getUnqualifiedType();
1239 type2_qual = type2_qual.getUnqualifiedType();
1240 }
1241
Greg Claytonf73034f2015-09-08 18:15:05 +00001242 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243}
1244
Greg Claytona1e5dc82015-08-11 22:53:00 +00001245CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001246ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1247{
1248 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1249 return GetTypeForDecl(interface_decl);
1250 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1251 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001252 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001253}
1254
Greg Clayton6beaaa62011-01-17 03:46:26 +00001255
Greg Claytona1e5dc82015-08-11 22:53:00 +00001256CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001257ClangASTContext::GetTypeForDecl (TagDecl *decl)
1258{
1259 // No need to call the getASTContext() accessor (which can create the AST
1260 // if it isn't created yet, because we can't have created a decl in this
1261 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001262 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001263 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001264 return CompilerType (ast, ast->getTagDeclType(decl));
1265 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001266}
1267
Greg Claytona1e5dc82015-08-11 22:53:00 +00001268CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001269ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1270{
1271 // No need to call the getASTContext() accessor (which can create the AST
1272 // if it isn't created yet, because we can't have created a decl in this
1273 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001274 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001275 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001276 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1277 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001278}
1279
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280#pragma mark Structure, Unions, Classes
1281
Greg Claytona1e5dc82015-08-11 22:53:00 +00001282CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001283ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1284 AccessType access_type,
1285 const char *name,
1286 int kind,
1287 LanguageType language,
1288 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001290 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001291 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001292
Ed Masted4612ad2014-04-20 13:17:36 +00001293 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001294 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295
Greg Clayton9e409562010-07-28 02:04:09 +00001296
Greg Claytone1be9962011-08-24 23:50:00 +00001297 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001298 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001299 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001300 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001301 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001302 }
1303
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001304 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1305 // we will need to update this code. I was told to currently always use
1306 // the CXXRecordDecl class since we often don't know from debug information
1307 // if something is struct or a class, so we default to always use the more
1308 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001309
1310 bool is_anonymous = (!name) || (!name[0]);
1311
Greg Claytonf0705c82011-10-22 03:33:13 +00001312 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1313 (TagDecl::TagKind)kind,
1314 decl_ctx,
1315 SourceLocation(),
1316 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001317 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001318
1319 if (is_anonymous)
1320 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001321
Greg Claytonc4ffd662013-03-08 01:37:30 +00001322 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001323 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001324 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001325 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001326
Greg Clayton55561e92011-10-26 03:31:36 +00001327 if (access_type != eAccessNone)
1328 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001329
1330 if (decl_ctx)
1331 decl_ctx->addDecl (decl);
1332
Greg Claytona1e5dc82015-08-11 22:53:00 +00001333 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001334 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001335 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001336}
1337
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001338static TemplateParameterList *
1339CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001340 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001341 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1342{
1343 const bool parameter_pack = false;
1344 const bool is_typename = false;
1345 const unsigned depth = 0;
1346 const size_t num_template_params = template_param_infos.GetSize();
1347 for (size_t i=0; i<num_template_params; ++i)
1348 {
1349 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001350
Ed Masted4612ad2014-04-20 13:17:36 +00001351 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001352 if (name && name[0])
1353 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001354 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001355 {
1356 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1357 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1358 SourceLocation(),
1359 SourceLocation(),
1360 depth,
1361 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001362 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001363 template_param_infos.args[i].getIntegralType(),
1364 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001365 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001366
1367 }
1368 else
1369 {
1370 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1371 ast->getTranslationUnitDecl(), // Is this the right decl context?
1372 SourceLocation(),
1373 SourceLocation(),
1374 depth,
1375 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001376 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001377 is_typename,
1378 parameter_pack));
1379 }
1380 }
1381
1382 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1383 SourceLocation(),
1384 SourceLocation(),
1385 &template_param_decls.front(),
1386 template_param_decls.size(),
1387 SourceLocation());
1388 return template_param_list;
1389}
1390
1391clang::FunctionTemplateDecl *
1392ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1393 clang::FunctionDecl *func_decl,
1394 const char *name,
1395 const TemplateParameterInfos &template_param_infos)
1396{
1397// /// \brief Create a function template node.
1398 ASTContext *ast = getASTContext();
1399
1400 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1401
1402 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1403 template_param_infos,
1404 template_param_decls);
1405 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1406 decl_ctx,
1407 func_decl->getLocation(),
1408 func_decl->getDeclName(),
1409 template_param_list,
1410 func_decl);
1411
1412 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1413 i < template_param_decl_count;
1414 ++i)
1415 {
1416 // TODO: verify which decl context we should put template_param_decls into..
1417 template_param_decls[i]->setDeclContext (func_decl);
1418 }
1419
1420 return func_tmpl_decl;
1421}
1422
1423void
1424ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1425 clang::FunctionTemplateDecl *func_tmpl_decl,
1426 const TemplateParameterInfos &infos)
1427{
1428 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1429 infos.args.data(),
1430 infos.args.size());
1431
1432 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1433 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001434 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001435}
1436
1437
Greg Claytonf0705c82011-10-22 03:33:13 +00001438ClassTemplateDecl *
1439ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001440 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001441 const char *class_name,
1442 int kind,
1443 const TemplateParameterInfos &template_param_infos)
1444{
1445 ASTContext *ast = getASTContext();
1446
Ed Masted4612ad2014-04-20 13:17:36 +00001447 ClassTemplateDecl *class_template_decl = nullptr;
1448 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001449 decl_ctx = ast->getTranslationUnitDecl();
1450
1451 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1452 DeclarationName decl_name (&identifier_info);
1453
1454 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001455
1456 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001457 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001458 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001459 if (class_template_decl)
1460 return class_template_decl;
1461 }
1462
1463 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001464
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001465 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1466 template_param_infos,
1467 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001468
1469 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1470 (TagDecl::TagKind)kind,
1471 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1472 SourceLocation(),
1473 SourceLocation(),
1474 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001475
1476 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1477 i < template_param_decl_count;
1478 ++i)
1479 {
1480 template_param_decls[i]->setDeclContext (template_cxx_decl);
1481 }
1482
Sean Callananb5c79622011-11-19 01:35:08 +00001483 // With templated classes, we say that a class is templated with
1484 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001485 //template_cxx_decl->startDefinition();
1486 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001487
Greg Claytonf0705c82011-10-22 03:33:13 +00001488 class_template_decl = ClassTemplateDecl::Create (*ast,
1489 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1490 SourceLocation(),
1491 decl_name,
1492 template_param_list,
1493 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001494 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001495
1496 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001497 {
Greg Clayton55561e92011-10-26 03:31:36 +00001498 if (access_type != eAccessNone)
1499 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001500
1501 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1502 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1503
Greg Claytonf0705c82011-10-22 03:33:13 +00001504 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001505
1506#ifdef LLDB_CONFIGURATION_DEBUG
1507 VerifyDecl(class_template_decl);
1508#endif
1509 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001510
1511 return class_template_decl;
1512}
1513
1514
1515ClassTemplateSpecializationDecl *
1516ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1517 ClassTemplateDecl *class_template_decl,
1518 int kind,
1519 const TemplateParameterInfos &template_param_infos)
1520{
1521 ASTContext *ast = getASTContext();
1522 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1523 (TagDecl::TagKind)kind,
1524 decl_ctx,
1525 SourceLocation(),
1526 SourceLocation(),
1527 class_template_decl,
1528 &template_param_infos.args.front(),
1529 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001530 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001531
Sean Callananfa4fab72013-02-01 06:55:48 +00001532 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1533
Greg Claytonf0705c82011-10-22 03:33:13 +00001534 return class_template_specialization_decl;
1535}
1536
Greg Claytona1e5dc82015-08-11 22:53:00 +00001537CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001538ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1539{
1540 if (class_template_specialization_decl)
1541 {
1542 ASTContext *ast = getASTContext();
1543 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001544 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001545 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001546 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001547}
1548
Greg Clayton090d0982011-06-19 03:43:27 +00001549static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001550check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001551{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001552 // Special-case call since it can take any number of operands
1553 if(op_kind == OO_Call)
1554 return true;
1555
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001556 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001557 if (num_params == 0)
1558 return unary;
1559 if (num_params == 1)
1560 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001561 else
Greg Clayton090d0982011-06-19 03:43:27 +00001562 return false;
1563}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001564
Greg Clayton090d0982011-06-19 03:43:27 +00001565bool
1566ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1567{
Sean Callanan5b26f272012-02-04 08:49:35 +00001568 switch (op_kind)
1569 {
1570 default:
1571 break;
1572 // C++ standard allows any number of arguments to new/delete
1573 case OO_New:
1574 case OO_Array_New:
1575 case OO_Delete:
1576 case OO_Array_Delete:
1577 return true;
1578 }
1579
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001580#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 +00001581 switch (op_kind)
1582 {
1583#include "clang/Basic/OperatorKinds.def"
1584 default: break;
1585 }
1586 return false;
1587}
1588
Greg Clayton57ee3062013-07-11 22:46:58 +00001589clang::AccessSpecifier
1590ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001591{
1592 clang::AccessSpecifier ret = lhs;
1593
1594 // Make the access equal to the stricter of the field and the nested field's access
1595 switch (ret)
1596 {
1597 case clang::AS_none:
1598 break;
1599 case clang::AS_private:
1600 break;
1601 case clang::AS_protected:
1602 if (rhs == AS_private)
1603 ret = AS_private;
1604 break;
1605 case clang::AS_public:
1606 ret = rhs;
1607 break;
1608 }
1609
1610 return ret;
1611}
1612
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001613bool
1614ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1615{
1616 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1617}
1618
1619bool
1620ClangASTContext::FieldIsBitfield
1621(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001622 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001623 FieldDecl* field,
1624 uint32_t& bitfield_bit_size
1625)
1626{
Ed Masted4612ad2014-04-20 13:17:36 +00001627 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628 return false;
1629
1630 if (field->isBitField())
1631 {
1632 Expr* bit_width_expr = field->getBitWidth();
1633 if (bit_width_expr)
1634 {
1635 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001636 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637 {
1638 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1639 return true;
1640 }
1641 }
1642 }
1643 return false;
1644}
1645
1646bool
1647ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1648{
Ed Masted4612ad2014-04-20 13:17:36 +00001649 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650 return false;
1651
1652 if (!record_decl->field_empty())
1653 return true;
1654
1655 // No fields, lets check this is a CXX record and check the base classes
1656 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1657 if (cxx_record_decl)
1658 {
1659 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1660 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1661 base_class != base_class_end;
1662 ++base_class)
1663 {
1664 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1665 if (RecordHasFields(base_class_decl))
1666 return true;
1667 }
1668 }
1669 return false;
1670}
1671
Greg Clayton8cf05932010-07-22 18:30:50 +00001672#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673
Greg Claytona1e5dc82015-08-11 22:53:00 +00001674CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001675ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001676(
1677 const char *name,
1678 DeclContext *decl_ctx,
1679 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001680 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001681 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001682)
1683{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001684 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001685 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001686 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001687 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001688 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001689
Greg Clayton6beaaa62011-01-17 03:46:26 +00001690 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001691 decl_ctx,
1692 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001693 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001694 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001695 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001696 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001697 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001698 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001699
Jim Ingham379397632012-10-27 02:54:13 +00001700 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001701 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001702
Greg Claytona1e5dc82015-08-11 22:53:00 +00001703 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704}
1705
1706static inline bool
1707BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1708{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001709 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710}
1711
Greg Clayton57ee3062013-07-11 22:46:58 +00001712uint32_t
1713ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714{
1715 uint32_t num_bases = 0;
1716 if (cxx_record_decl)
1717 {
1718 if (omit_empty_base_classes)
1719 {
1720 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1721 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1722 base_class != base_class_end;
1723 ++base_class)
1724 {
1725 // Skip empty base classes
1726 if (omit_empty_base_classes)
1727 {
1728 if (BaseSpecifierIsEmpty (base_class))
1729 continue;
1730 }
1731 ++num_bases;
1732 }
1733 }
1734 else
1735 num_bases = cxx_record_decl->getNumBases();
1736 }
1737 return num_bases;
1738}
1739
1740
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741#pragma mark Namespace Declarations
1742
1743NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001744ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745{
Ed Masted4612ad2014-04-20 13:17:36 +00001746 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001747 ASTContext *ast = getASTContext();
1748 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001749 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001750 decl_ctx = translation_unit_decl;
1751
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 if (name)
1753 {
Greg Clayton030a2042011-10-14 21:34:45 +00001754 IdentifierInfo &identifier_info = ast->Idents.get(name);
1755 DeclarationName decl_name (&identifier_info);
1756 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001757 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001758 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001759 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001760 if (namespace_decl)
1761 return namespace_decl;
1762 }
1763
Sean Callanan5b26f272012-02-04 08:49:35 +00001764 namespace_decl = NamespaceDecl::Create(*ast,
1765 decl_ctx,
1766 false,
1767 SourceLocation(),
1768 SourceLocation(),
1769 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001770 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001771
Greg Clayton9d3d6882011-10-31 23:51:19 +00001772 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001774 else
1775 {
1776 if (decl_ctx == translation_unit_decl)
1777 {
1778 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1779 if (namespace_decl)
1780 return namespace_decl;
1781
Sean Callanan5b26f272012-02-04 08:49:35 +00001782 namespace_decl = NamespaceDecl::Create(*ast,
1783 decl_ctx,
1784 false,
1785 SourceLocation(),
1786 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001787 nullptr,
1788 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001789 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1790 translation_unit_decl->addDecl (namespace_decl);
1791 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1792 }
1793 else
1794 {
1795 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1796 if (parent_namespace_decl)
1797 {
1798 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1799 if (namespace_decl)
1800 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001801 namespace_decl = NamespaceDecl::Create(*ast,
1802 decl_ctx,
1803 false,
1804 SourceLocation(),
1805 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001806 nullptr,
1807 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001808 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1809 parent_namespace_decl->addDecl (namespace_decl);
1810 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1811 }
1812 else
1813 {
1814 // BAD!!!
1815 }
1816 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001817 }
1818#ifdef LLDB_CONFIGURATION_DEBUG
1819 VerifyDecl(namespace_decl);
1820#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001821 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822}
1823
1824
Paul Hermand628cbb2015-09-15 23:44:17 +00001825clang::BlockDecl *
1826ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1827{
1828 if (ctx != nullptr)
1829 {
1830 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1831 ctx->addDecl(decl);
1832 return decl;
1833 }
1834 return nullptr;
1835}
1836
Paul Hermanea188fc2015-09-16 18:48:30 +00001837clang::DeclContext *
1838FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1839{
1840 if (root == nullptr)
1841 return nullptr;
1842
1843 std::set<clang::DeclContext *> path_left;
1844 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1845 path_left.insert(d);
1846
1847 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1848 if (path_left.find(d) != path_left.end())
1849 return d;
1850
1851 return nullptr;
1852}
1853
Paul Hermand628cbb2015-09-15 23:44:17 +00001854clang::UsingDirectiveDecl *
1855ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1856{
1857 if (decl_ctx != nullptr && ns_decl != nullptr)
1858 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001859 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001860 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1861 decl_ctx,
1862 clang::SourceLocation(),
1863 clang::SourceLocation(),
1864 clang::NestedNameSpecifierLoc(),
1865 clang::SourceLocation(),
1866 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001867 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001868 decl_ctx->addDecl(using_decl);
1869 return using_decl;
1870 }
1871 return nullptr;
1872}
1873
1874clang::UsingDecl *
1875ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1876{
1877 if (current_decl_ctx != nullptr && target != nullptr)
1878 {
1879 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1880 current_decl_ctx,
1881 clang::SourceLocation(),
1882 clang::NestedNameSpecifierLoc(),
1883 clang::DeclarationNameInfo(),
1884 false);
1885 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1886 current_decl_ctx,
1887 clang::SourceLocation(),
1888 using_decl,
1889 target);
1890 using_decl->addShadowDecl(shadow_decl);
1891 current_decl_ctx->addDecl(using_decl);
1892 return using_decl;
1893 }
1894 return nullptr;
1895}
1896
1897clang::VarDecl *
1898ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1899{
1900 if (decl_context != nullptr)
1901 {
1902 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1903 decl_context,
1904 clang::SourceLocation(),
1905 clang::SourceLocation(),
1906 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1907 type,
1908 nullptr,
1909 clang::SC_None);
1910 var_decl->setAccess(clang::AS_public);
1911 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001912 return var_decl;
1913 }
1914 return nullptr;
1915}
1916
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917#pragma mark Function Types
1918
1919FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001920ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1921 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001922 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001923 int storage,
1924 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925{
Ed Masted4612ad2014-04-20 13:17:36 +00001926 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001927 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001928 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001929 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930
Greg Clayton0d551042013-06-28 21:08:47 +00001931
1932 const bool hasWrittenPrototype = true;
1933 const bool isConstexprSpecified = false;
1934
Greg Clayton147e1fa2011-10-14 22:47:18 +00001935 if (name && name[0])
1936 {
1937 func_decl = FunctionDecl::Create (*ast,
1938 decl_ctx,
1939 SourceLocation(),
1940 SourceLocation(),
1941 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001942 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001943 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001944 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001945 is_inline,
1946 hasWrittenPrototype,
1947 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001949 else
1950 {
1951 func_decl = FunctionDecl::Create (*ast,
1952 decl_ctx,
1953 SourceLocation(),
1954 SourceLocation(),
1955 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00001956 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001957 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001958 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001959 is_inline,
1960 hasWrittenPrototype,
1961 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001962 }
1963 if (func_decl)
1964 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001965
1966#ifdef LLDB_CONFIGURATION_DEBUG
1967 VerifyDecl(func_decl);
1968#endif
1969
Greg Clayton147e1fa2011-10-14 22:47:18 +00001970 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001971}
1972
Greg Claytona1e5dc82015-08-11 22:53:00 +00001973CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001974ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001975 const CompilerType& result_type,
1976 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001977 unsigned num_args,
1978 bool is_variadic,
1979 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980{
Ed Masted4612ad2014-04-20 13:17:36 +00001981 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001982 std::vector<QualType> qual_type_args;
1983 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00001984 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985
1986 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001987 FunctionProtoType::ExtProtoInfo proto_info;
1988 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001989 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001990 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001991 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001992
Greg Claytona1e5dc82015-08-11 22:53:00 +00001993 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001994 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00001995 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996}
1997
1998ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00001999ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002000{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002001 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002002 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00002003 return ParmVarDecl::Create(*ast,
2004 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002006 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002007 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002008 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002009 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002010 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00002011 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012}
2013
2014void
2015ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2016{
2017 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002018 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019}
2020
2021
2022#pragma mark Array Types
2023
Greg Claytona1e5dc82015-08-11 22:53:00 +00002024CompilerType
2025ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002026 size_t element_count,
2027 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028{
Greg Clayton57ee3062013-07-11 22:46:58 +00002029 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002031 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002032 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002033
Greg Clayton1c8ef472013-04-05 23:27:21 +00002034 if (is_vector)
2035 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002036 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00002037 }
2038 else
2039 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002040
2041 llvm::APInt ap_element_count (64, element_count);
2042 if (element_count == 0)
2043 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002044 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002045 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002046 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002047 }
2048 else
2049 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002050 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002051 ap_element_count,
2052 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002053 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002054 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002055 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002057 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058}
2059
Greg Claytona1e5dc82015-08-11 22:53:00 +00002060CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00002061ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002062 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00002063 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00002064{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002065 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002066 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2067 return type;
2068 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00002069 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002070 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00002071 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00002072 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00002073 SetIsPacked(type);
2074 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002075 return type;
2076}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002077
2078#pragma mark Enumeration Types
2079
Greg Claytona1e5dc82015-08-11 22:53:00 +00002080CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002081ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002082(
Greg Claytond8d4a572015-08-11 21:38:15 +00002083 const char *name,
2084 DeclContext *decl_ctx,
2085 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002086 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002087 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002088{
2089 // TODO: Do something intelligent with the Declaration object passed in
2090 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002091 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002092
Greg Claytone02b8502010-10-12 04:29:14 +00002093 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002094 // const bool IsScoped = false;
2095 // const bool IsFixed = false;
2096
Greg Clayton6beaaa62011-01-17 03:46:26 +00002097 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002098 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002099 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002100 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002101 name && name[0] ? &ast->Idents.get(name) : nullptr,
2102 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002103 false, // IsScoped
2104 false, // IsScopedUsingClassTag
2105 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002106
2107
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002108 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002109 {
2110 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00002111 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00002112
2113 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2114
Greg Claytona1e5dc82015-08-11 22:53:00 +00002115 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002116 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002117 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118}
2119
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120// Disable this for now since I can't seem to get a nicely formatted float
2121// out of the APFloat class without just getting the float, double or quad
2122// and then using a formatted print on it which defeats the purpose. We ideally
2123// would like to get perfect string values for any kind of float semantics
2124// so we can support remote targets. The code below also requires a patch to
2125// llvm::APInt.
2126//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00002127//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 +00002128//{
2129// uint32_t count = 0;
2130// bool is_complex = false;
2131// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2132// {
2133// unsigned num_bytes_per_float = byte_size / count;
2134// unsigned num_bits_per_float = num_bytes_per_float * 8;
2135//
2136// float_str.clear();
2137// uint32_t i;
2138// for (i=0; i<count; i++)
2139// {
2140// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2141// bool is_ieee = false;
2142// APFloat ap_float(ap_int, is_ieee);
2143// char s[1024];
2144// unsigned int hex_digits = 0;
2145// bool upper_case = false;
2146//
2147// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2148// {
2149// if (i > 0)
2150// float_str.append(", ");
2151// float_str.append(s);
2152// if (i == 1 && is_complex)
2153// float_str.append(1, 'i');
2154// }
2155// }
2156// return !float_str.empty();
2157// }
2158// return false;
2159//}
2160
Greg Claytona1e5dc82015-08-11 22:53:00 +00002161CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002162ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2163 size_t bit_size, bool is_signed)
2164{
2165 if (ast)
2166 {
2167 if (is_signed)
2168 {
2169 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002170 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002171
2172 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002173 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002174
2175 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002176 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002177
2178 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002179 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002180
2181 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002182 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002183
2184 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002185 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002186 }
2187 else
2188 {
2189 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002190 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002191
2192 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002193 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002194
2195 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002196 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002197
2198 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002199 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002200
2201 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002202 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002203
2204 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002205 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002206 }
2207 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002208 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002209}
2210
Greg Claytona1e5dc82015-08-11 22:53:00 +00002211CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002212ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2213{
2214 if (ast)
2215 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002216 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002217}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218
Enrico Granata86027e92012-03-24 01:11:14 +00002219bool
Greg Claytona2721472011-06-25 00:44:06 +00002220ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2221 clang::Decl *decl)
2222{
2223 if (!decl)
2224 return false;
2225
2226 ExternalASTSource *ast_source = ast->getExternalSource();
2227
2228 if (!ast_source)
2229 return false;
2230
2231 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2232 {
Greg Clayton219cf312012-03-30 00:51:13 +00002233 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002234 return true;
2235
2236 if (!tag_decl->hasExternalLexicalStorage())
2237 return false;
2238
2239 ast_source->CompleteType(tag_decl);
2240
2241 return !tag_decl->getTypeForDecl()->isIncompleteType();
2242 }
2243 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2244 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002245 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002246 return true;
2247
2248 if (!objc_interface_decl->hasExternalLexicalStorage())
2249 return false;
2250
2251 ast_source->CompleteType(objc_interface_decl);
2252
Sean Callanan5b26f272012-02-04 08:49:35 +00002253 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002254 }
2255 else
2256 {
2257 return false;
2258 }
2259}
2260
Sean Callanan60217122012-04-13 00:10:03 +00002261void
Greg Claytond0029442013-03-27 01:48:02 +00002262ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002263 user_id_t user_id)
2264{
2265 ClangASTMetadata meta_data;
2266 meta_data.SetUserID (user_id);
2267 SetMetadata (object, meta_data);
2268}
2269
2270void
Sean Callanan60217122012-04-13 00:10:03 +00002271ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002272 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002273 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002274{
2275 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002276 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002277
2278 if (external_source)
2279 external_source->SetMetadata(object, metadata);
2280}
2281
Jim Ingham379397632012-10-27 02:54:13 +00002282ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002283ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002284 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002285{
2286 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002287 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002288
2289 if (external_source && external_source->HasMetadata(object))
2290 return external_source->GetMetadata(object);
2291 else
Ed Masted4612ad2014-04-20 13:17:36 +00002292 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002293}
2294
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002295clang::DeclContext *
2296ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2297{
Sean Callanana87bee82011-08-19 06:19:25 +00002298 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002299}
2300
2301clang::DeclContext *
2302ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2303{
Sean Callanana87bee82011-08-19 06:19:25 +00002304 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002305}
2306
Greg Claytond8d4a572015-08-11 21:38:15 +00002307bool
2308ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2309{
2310 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2311 if (clang_type)
2312 {
2313 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2314 if (tag_type)
2315 {
2316 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2317 if (tag_decl)
2318 {
2319 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2320 return true;
2321 }
2322 }
2323 }
2324 return false;
2325}
2326
2327
2328bool
2329ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2330 int default_accessibility,
2331 int *assigned_accessibilities,
2332 size_t num_assigned_accessibilities)
2333{
2334 if (record_decl)
2335 {
2336 uint32_t field_idx;
2337 clang::RecordDecl::field_iterator field, field_end;
2338 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2339 field != field_end;
2340 ++field, ++field_idx)
2341 {
2342 // If no accessibility was assigned, assign the correct one
2343 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2344 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2345 }
2346 return true;
2347 }
2348 return false;
2349}
2350
2351clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002352ClangASTContext::GetDeclContextForType (const CompilerType& type)
2353{
2354 return GetDeclContextForType(GetQualType(type));
2355}
2356
2357clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002358ClangASTContext::GetDeclContextForType (clang::QualType type)
2359{
2360 if (type.isNull())
2361 return nullptr;
2362
2363 clang::QualType qual_type = type.getCanonicalType();
2364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2365 switch (type_class)
2366 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002367 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2368 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2369 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2370 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2371 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2372 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2373 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002374 default:
2375 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002376 }
2377 // No DeclContext in this type...
2378 return nullptr;
2379}
2380
2381static bool
2382GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2383{
2384 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2385 switch (type_class)
2386 {
2387 case clang::Type::ConstantArray:
2388 case clang::Type::IncompleteArray:
2389 case clang::Type::VariableArray:
2390 {
2391 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2392
2393 if (array_type)
2394 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2395 }
2396 break;
2397
2398 case clang::Type::Record:
2399 case clang::Type::Enum:
2400 {
2401 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2402 if (tag_type)
2403 {
2404 clang::TagDecl *tag_decl = tag_type->getDecl();
2405 if (tag_decl)
2406 {
2407 if (tag_decl->isCompleteDefinition())
2408 return true;
2409
2410 if (!allow_completion)
2411 return false;
2412
2413 if (tag_decl->hasExternalLexicalStorage())
2414 {
2415 if (ast)
2416 {
2417 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2418 if (external_ast_source)
2419 {
2420 external_ast_source->CompleteType(tag_decl);
2421 return !tag_type->isIncompleteType();
2422 }
2423 }
2424 }
2425 return false;
2426 }
2427 }
2428
2429 }
2430 break;
2431
2432 case clang::Type::ObjCObject:
2433 case clang::Type::ObjCInterface:
2434 {
2435 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2436 if (objc_class_type)
2437 {
2438 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2439 // We currently can't complete objective C types through the newly added ASTContext
2440 // because it only supports TagDecl objects right now...
2441 if (class_interface_decl)
2442 {
2443 if (class_interface_decl->getDefinition())
2444 return true;
2445
2446 if (!allow_completion)
2447 return false;
2448
2449 if (class_interface_decl->hasExternalLexicalStorage())
2450 {
2451 if (ast)
2452 {
2453 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2454 if (external_ast_source)
2455 {
2456 external_ast_source->CompleteType (class_interface_decl);
2457 return !objc_class_type->isIncompleteType();
2458 }
2459 }
2460 }
2461 return false;
2462 }
2463 }
2464 }
2465 break;
2466
2467 case clang::Type::Typedef:
2468 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2469
2470 case clang::Type::Elaborated:
2471 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2472
2473 case clang::Type::Paren:
2474 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2475
2476 default:
2477 break;
2478 }
2479
2480 return true;
2481}
2482
2483static clang::ObjCIvarDecl::AccessControl
2484ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2485{
2486 switch (access)
2487 {
2488 case eAccessNone: return clang::ObjCIvarDecl::None;
2489 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2490 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2491 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2492 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2493 }
2494 return clang::ObjCIvarDecl::None;
2495}
2496
2497
2498//----------------------------------------------------------------------
2499// Tests
2500//----------------------------------------------------------------------
2501
2502bool
2503ClangASTContext::IsAggregateType (void* type)
2504{
2505 clang::QualType qual_type (GetCanonicalQualType(type));
2506
2507 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2508 switch (type_class)
2509 {
2510 case clang::Type::IncompleteArray:
2511 case clang::Type::VariableArray:
2512 case clang::Type::ConstantArray:
2513 case clang::Type::ExtVector:
2514 case clang::Type::Vector:
2515 case clang::Type::Record:
2516 case clang::Type::ObjCObject:
2517 case clang::Type::ObjCInterface:
2518 return true;
2519 case clang::Type::Elaborated:
2520 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2521 case clang::Type::Typedef:
2522 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2523 case clang::Type::Paren:
2524 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2525 default:
2526 break;
2527 }
2528 // The clang type does have a value
2529 return false;
2530}
2531
2532bool
2533ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002534 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002535 uint64_t *size,
2536 bool *is_incomplete)
2537{
2538 clang::QualType qual_type (GetCanonicalQualType(type));
2539
2540 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2541 switch (type_class)
2542 {
2543 default:
2544 break;
2545
2546 case clang::Type::ConstantArray:
2547 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002548 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002549 if (size)
2550 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2551 return true;
2552
2553 case clang::Type::IncompleteArray:
2554 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002555 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002556 if (size)
2557 *size = 0;
2558 if (is_incomplete)
2559 *is_incomplete = true;
2560 return true;
2561
2562 case clang::Type::VariableArray:
2563 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002564 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002565 if (size)
2566 *size = 0;
2567 return true;
2568
2569 case clang::Type::DependentSizedArray:
2570 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002571 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002572 if (size)
2573 *size = 0;
2574 return true;
2575
2576 case clang::Type::Typedef:
2577 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2578 element_type_ptr,
2579 size,
2580 is_incomplete);
2581 case clang::Type::Elaborated:
2582 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2583 element_type_ptr,
2584 size,
2585 is_incomplete);
2586 case clang::Type::Paren:
2587 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2588 element_type_ptr,
2589 size,
2590 is_incomplete);
2591 }
2592 if (element_type_ptr)
2593 element_type_ptr->Clear();
2594 if (size)
2595 *size = 0;
2596 if (is_incomplete)
2597 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002598 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002599}
2600
2601bool
2602ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002603 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002604 uint64_t *size)
2605{
2606 clang::QualType qual_type (GetCanonicalQualType(type));
2607
2608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2609 switch (type_class)
2610 {
2611 case clang::Type::Vector:
2612 {
2613 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2614 if (vector_type)
2615 {
2616 if (size)
2617 *size = vector_type->getNumElements();
2618 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002619 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002620 }
2621 return true;
2622 }
2623 break;
2624 case clang::Type::ExtVector:
2625 {
2626 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2627 if (ext_vector_type)
2628 {
2629 if (size)
2630 *size = ext_vector_type->getNumElements();
2631 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002632 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002633 }
2634 return true;
2635 }
2636 default:
2637 break;
2638 }
2639 return false;
2640}
2641
2642bool
2643ClangASTContext::IsRuntimeGeneratedType (void* type)
2644{
2645 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2646 if (!decl_ctx)
2647 return false;
2648
2649 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2650 return false;
2651
2652 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2653
2654 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2655 if (!ast_metadata)
2656 return false;
2657 return (ast_metadata->GetISAPtr() != 0);
2658}
2659
2660bool
2661ClangASTContext::IsCharType (void* type)
2662{
2663 return GetQualType(type).getUnqualifiedType()->isCharType();
2664}
2665
2666
2667bool
2668ClangASTContext::IsCompleteType (void* type)
2669{
2670 const bool allow_completion = false;
2671 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2672}
2673
2674bool
2675ClangASTContext::IsConst(void* type)
2676{
2677 return GetQualType(type).isConstQualified();
2678}
2679
2680bool
2681ClangASTContext::IsCStringType (void* type, uint32_t &length)
2682{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002683 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002684 length = 0;
2685 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2686
2687 if (!pointee_or_element_clang_type.IsValid())
2688 return false;
2689
2690 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2691 {
2692 if (pointee_or_element_clang_type.IsCharType())
2693 {
2694 if (type_flags.Test (eTypeIsArray))
2695 {
2696 // We know the size of the array and it could be a C string
2697 // since it is an array of characters
2698 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2699 }
2700 return true;
2701
2702 }
2703 }
2704 return false;
2705}
2706
2707bool
2708ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2709{
2710 if (type)
2711 {
2712 clang::QualType qual_type (GetCanonicalQualType(type));
2713
2714 if (qual_type->isFunctionType())
2715 {
2716 if (is_variadic_ptr)
2717 {
2718 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2719 if (function_proto_type)
2720 *is_variadic_ptr = function_proto_type->isVariadic();
2721 else
2722 *is_variadic_ptr = false;
2723 }
2724 return true;
2725 }
2726
2727 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2728 switch (type_class)
2729 {
2730 default:
2731 break;
2732 case clang::Type::Typedef:
2733 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2734 case clang::Type::Elaborated:
2735 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2736 case clang::Type::Paren:
2737 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2738 case clang::Type::LValueReference:
2739 case clang::Type::RValueReference:
2740 {
2741 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2742 if (reference_type)
2743 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2744 }
2745 break;
2746 }
2747 }
2748 return false;
2749}
2750
2751// Used to detect "Homogeneous Floating-point Aggregates"
2752uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002753ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002754{
2755 if (!type)
2756 return 0;
2757
2758 clang::QualType qual_type(GetCanonicalQualType(type));
2759 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2760 switch (type_class)
2761 {
2762 case clang::Type::Record:
2763 if (GetCompleteType (type))
2764 {
2765 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2766 if (cxx_record_decl)
2767 {
2768 if (cxx_record_decl->getNumBases() ||
2769 cxx_record_decl->isDynamicClass())
2770 return 0;
2771 }
2772 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2773 if (record_type)
2774 {
2775 const clang::RecordDecl *record_decl = record_type->getDecl();
2776 if (record_decl)
2777 {
2778 // We are looking for a structure that contains only floating point types
2779 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2780 uint32_t num_fields = 0;
2781 bool is_hva = false;
2782 bool is_hfa = false;
2783 clang::QualType base_qual_type;
2784 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2785 {
2786 clang::QualType field_qual_type = field_pos->getType();
2787 if (field_qual_type->isFloatingType())
2788 {
2789 if (field_qual_type->isComplexType())
2790 return 0;
2791 else
2792 {
2793 if (num_fields == 0)
2794 base_qual_type = field_qual_type;
2795 else
2796 {
2797 if (is_hva)
2798 return 0;
2799 is_hfa = true;
2800 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2801 return 0;
2802 }
2803 }
2804 }
2805 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2806 {
2807 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2808 if (array && array->getNumElements() <= 4)
2809 {
2810 if (num_fields == 0)
2811 base_qual_type = array->getElementType();
2812 else
2813 {
2814 if (is_hfa)
2815 return 0;
2816 is_hva = true;
2817 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2818 return 0;
2819 }
2820 }
2821 else
2822 return 0;
2823 }
2824 else
2825 return 0;
2826 ++num_fields;
2827 }
2828 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002829 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002830 return num_fields;
2831 }
2832 }
2833 }
2834 break;
2835
2836 case clang::Type::Typedef:
2837 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2838
2839 case clang::Type::Elaborated:
2840 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2841 default:
2842 break;
2843 }
2844 return 0;
2845}
2846
2847size_t
2848ClangASTContext::GetNumberOfFunctionArguments (void* type)
2849{
2850 if (type)
2851 {
2852 clang::QualType qual_type (GetCanonicalQualType(type));
2853 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2854 if (func)
2855 return func->getNumParams();
2856 }
2857 return 0;
2858}
2859
Greg Claytona1e5dc82015-08-11 22:53:00 +00002860CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002861ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2862{
2863 if (type)
2864 {
2865 clang::QualType qual_type (GetCanonicalQualType(type));
2866 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2867 if (func)
2868 {
2869 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002870 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002871 }
2872 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002873 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002874}
2875
2876bool
2877ClangASTContext::IsFunctionPointerType (void* type)
2878{
2879 if (type)
2880 {
2881 clang::QualType qual_type (GetCanonicalQualType(type));
2882
2883 if (qual_type->isFunctionPointerType())
2884 return true;
2885
2886 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2887 switch (type_class)
2888 {
2889 default:
2890 break;
2891 case clang::Type::Typedef:
2892 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2893 case clang::Type::Elaborated:
2894 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2895 case clang::Type::Paren:
2896 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2897
2898 case clang::Type::LValueReference:
2899 case clang::Type::RValueReference:
2900 {
2901 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2902 if (reference_type)
2903 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2904 }
2905 break;
2906 }
2907 }
2908 return false;
2909
2910}
2911
2912bool
2913ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2914{
2915 if (!type)
2916 return false;
2917
2918 clang::QualType qual_type (GetCanonicalQualType(type));
2919 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2920
2921 if (builtin_type)
2922 {
2923 if (builtin_type->isInteger())
2924 {
2925 is_signed = builtin_type->isSignedInteger();
2926 return true;
2927 }
2928 }
2929
2930 return false;
2931}
2932
2933bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002934ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002935{
2936 if (type)
2937 {
2938 clang::QualType qual_type (GetCanonicalQualType(type));
2939 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2940 switch (type_class)
2941 {
2942 case clang::Type::Builtin:
2943 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2944 {
2945 default:
2946 break;
2947 case clang::BuiltinType::ObjCId:
2948 case clang::BuiltinType::ObjCClass:
2949 return true;
2950 }
2951 return false;
2952 case clang::Type::ObjCObjectPointer:
2953 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002954 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002955 return true;
2956 case clang::Type::BlockPointer:
2957 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002958 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002959 return true;
2960 case clang::Type::Pointer:
2961 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002962 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002963 return true;
2964 case clang::Type::MemberPointer:
2965 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002966 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002967 return true;
2968 case clang::Type::Typedef:
2969 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2970 case clang::Type::Elaborated:
2971 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2972 case clang::Type::Paren:
2973 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2974 default:
2975 break;
2976 }
2977 }
2978 if (pointee_type)
2979 pointee_type->Clear();
2980 return false;
2981}
2982
2983
2984bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002985ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002986{
2987 if (type)
2988 {
2989 clang::QualType qual_type (GetCanonicalQualType(type));
2990 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2991 switch (type_class)
2992 {
2993 case clang::Type::Builtin:
2994 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2995 {
2996 default:
2997 break;
2998 case clang::BuiltinType::ObjCId:
2999 case clang::BuiltinType::ObjCClass:
3000 return true;
3001 }
3002 return false;
3003 case clang::Type::ObjCObjectPointer:
3004 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003005 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003006 return true;
3007 case clang::Type::BlockPointer:
3008 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003009 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003010 return true;
3011 case clang::Type::Pointer:
3012 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003013 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003014 return true;
3015 case clang::Type::MemberPointer:
3016 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003017 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003018 return true;
3019 case clang::Type::LValueReference:
3020 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003021 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003022 return true;
3023 case clang::Type::RValueReference:
3024 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003025 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003026 return true;
3027 case clang::Type::Typedef:
3028 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3029 case clang::Type::Elaborated:
3030 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3031 case clang::Type::Paren:
3032 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3033 default:
3034 break;
3035 }
3036 }
3037 if (pointee_type)
3038 pointee_type->Clear();
3039 return false;
3040}
3041
3042
3043bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003044ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003045{
3046 if (type)
3047 {
3048 clang::QualType qual_type (GetCanonicalQualType(type));
3049 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3050
3051 switch (type_class)
3052 {
3053 case clang::Type::LValueReference:
3054 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003055 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003056 if (is_rvalue)
3057 *is_rvalue = false;
3058 return true;
3059 case clang::Type::RValueReference:
3060 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003061 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003062 if (is_rvalue)
3063 *is_rvalue = true;
3064 return true;
3065 case clang::Type::Typedef:
3066 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3067 case clang::Type::Elaborated:
3068 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3069 case clang::Type::Paren:
3070 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3071
3072 default:
3073 break;
3074 }
3075 }
3076 if (pointee_type)
3077 pointee_type->Clear();
3078 return false;
3079}
3080
3081bool
3082ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
3083{
3084 if (type)
3085 {
3086 clang::QualType qual_type (GetCanonicalQualType(type));
3087
3088 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3089 {
3090 clang::BuiltinType::Kind kind = BT->getKind();
3091 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3092 {
3093 count = 1;
3094 is_complex = false;
3095 return true;
3096 }
3097 }
3098 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3099 {
3100 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3101 {
3102 count = 2;
3103 is_complex = true;
3104 return true;
3105 }
3106 }
3107 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3108 {
3109 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3110 {
3111 count = VT->getNumElements();
3112 is_complex = false;
3113 return true;
3114 }
3115 }
3116 }
3117 count = 0;
3118 is_complex = false;
3119 return false;
3120}
3121
3122
3123bool
3124ClangASTContext::IsDefined(void* type)
3125{
3126 if (!type)
3127 return false;
3128
3129 clang::QualType qual_type(GetQualType(type));
3130 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3131 if (tag_type)
3132 {
3133 clang::TagDecl *tag_decl = tag_type->getDecl();
3134 if (tag_decl)
3135 return tag_decl->isCompleteDefinition();
3136 return false;
3137 }
3138 else
3139 {
3140 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3141 if (objc_class_type)
3142 {
3143 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3144 if (class_interface_decl)
3145 return class_interface_decl->getDefinition() != nullptr;
3146 return false;
3147 }
3148 }
3149 return true;
3150}
3151
3152bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003153ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003154{
3155 if (type)
3156 {
3157 clang::QualType qual_type (GetCanonicalQualType(type));
3158
3159 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3160
3161 if (obj_pointer_type)
3162 return obj_pointer_type->isObjCClassType();
3163 }
3164 return false;
3165}
3166
3167bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003168ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003169{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003170 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003171 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3172 return false;
3173}
3174
3175bool
3176ClangASTContext::IsPolymorphicClass (void* type)
3177{
3178 if (type)
3179 {
3180 clang::QualType qual_type(GetCanonicalQualType(type));
3181 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3182 switch (type_class)
3183 {
3184 case clang::Type::Record:
3185 if (GetCompleteType(type))
3186 {
3187 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3188 const clang::RecordDecl *record_decl = record_type->getDecl();
3189 if (record_decl)
3190 {
3191 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3192 if (cxx_record_decl)
3193 return cxx_record_decl->isPolymorphic();
3194 }
3195 }
3196 break;
3197
3198 default:
3199 break;
3200 }
3201 }
3202 return false;
3203}
3204
3205bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003206ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003207 bool check_cplusplus,
3208 bool check_objc)
3209{
3210 clang::QualType pointee_qual_type;
3211 if (type)
3212 {
3213 clang::QualType qual_type (GetCanonicalQualType(type));
3214 bool success = false;
3215 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3216 switch (type_class)
3217 {
3218 case clang::Type::Builtin:
3219 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3220 {
3221 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003222 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003223 return true;
3224 }
3225 break;
3226
3227 case clang::Type::ObjCObjectPointer:
3228 if (check_objc)
3229 {
3230 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003231 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003232 return true;
3233 }
3234 break;
3235
3236 case clang::Type::Pointer:
3237 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3238 success = true;
3239 break;
3240
3241 case clang::Type::LValueReference:
3242 case clang::Type::RValueReference:
3243 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3244 success = true;
3245 break;
3246
3247 case clang::Type::Typedef:
3248 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3249 dynamic_pointee_type,
3250 check_cplusplus,
3251 check_objc);
3252
3253 case clang::Type::Elaborated:
3254 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3255 dynamic_pointee_type,
3256 check_cplusplus,
3257 check_objc);
3258
3259 case clang::Type::Paren:
3260 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3261 dynamic_pointee_type,
3262 check_cplusplus,
3263 check_objc);
3264 default:
3265 break;
3266 }
3267
3268 if (success)
3269 {
3270 // Check to make sure what we are pointing too is a possible dynamic C++ type
3271 // We currently accept any "void *" (in case we have a class that has been
3272 // watered down to an opaque pointer) and virtual C++ classes.
3273 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3274 switch (pointee_type_class)
3275 {
3276 case clang::Type::Builtin:
3277 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3278 {
3279 case clang::BuiltinType::UnknownAny:
3280 case clang::BuiltinType::Void:
3281 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003282 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003283 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003284 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003285 break;
3286 }
3287 break;
3288
3289 case clang::Type::Record:
3290 if (check_cplusplus)
3291 {
3292 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3293 if (cxx_record_decl)
3294 {
3295 bool is_complete = cxx_record_decl->isCompleteDefinition();
3296
3297 if (is_complete)
3298 success = cxx_record_decl->isDynamicClass();
3299 else
3300 {
3301 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3302 if (metadata)
3303 success = metadata->GetIsDynamicCXXType();
3304 else
3305 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003306 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003307 if (is_complete)
3308 success = cxx_record_decl->isDynamicClass();
3309 else
3310 success = false;
3311 }
3312 }
3313
3314 if (success)
3315 {
3316 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003317 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003318 return true;
3319 }
3320 }
3321 }
3322 break;
3323
3324 case clang::Type::ObjCObject:
3325 case clang::Type::ObjCInterface:
3326 if (check_objc)
3327 {
3328 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003329 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003330 return true;
3331 }
3332 break;
3333
3334 default:
3335 break;
3336 }
3337 }
3338 }
3339 if (dynamic_pointee_type)
3340 dynamic_pointee_type->Clear();
3341 return false;
3342}
3343
3344
3345bool
3346ClangASTContext::IsScalarType (void* type)
3347{
3348 if (!type)
3349 return false;
3350
3351 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3352}
3353
3354bool
3355ClangASTContext::IsTypedefType (void* type)
3356{
3357 if (!type)
3358 return false;
3359 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3360}
3361
3362bool
3363ClangASTContext::IsVoidType (void* type)
3364{
3365 if (!type)
3366 return false;
3367 return GetCanonicalQualType(type)->isVoidType();
3368}
3369
3370bool
Greg Clayton56939cb2015-09-17 22:23:34 +00003371ClangASTContext::SupportsLanguage (lldb::LanguageType language)
3372{
3373 return ClangASTContextSupportsLanguage(language);
3374}
3375
3376bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003377ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003378{
3379 if (type)
3380 {
3381 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003382 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003383 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003384 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3385 if (cxx_record_decl)
3386 {
3387 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3388 return true;
3389 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003390 }
3391 }
3392 class_name.clear();
3393 return false;
3394}
3395
3396
3397bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003398ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003399{
3400 if (!type)
3401 return false;
3402
3403 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003404 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003405 return true;
3406 return false;
3407}
3408
3409bool
3410ClangASTContext::IsBeingDefined (void* type)
3411{
3412 if (!type)
3413 return false;
3414 clang::QualType qual_type (GetCanonicalQualType(type));
3415 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3416 if (tag_type)
3417 return tag_type->isBeingDefined();
3418 return false;
3419}
3420
3421bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003422ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003423{
3424 if (!type)
3425 return false;
3426
3427 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003428
3429 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003430 {
3431 if (class_type_ptr)
3432 {
3433 if (!qual_type->isObjCClassType() &&
3434 !qual_type->isObjCIdType())
3435 {
3436 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3437 if (obj_pointer_type == nullptr)
3438 class_type_ptr->Clear();
3439 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003440 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003441 }
3442 }
3443 return true;
3444 }
3445 if (class_type_ptr)
3446 class_type_ptr->Clear();
3447 return false;
3448}
3449
3450bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003451ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003452{
3453 if (!type)
3454 return false;
3455
3456 clang::QualType qual_type (GetCanonicalQualType(type));
3457
3458 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3459 if (object_type)
3460 {
3461 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3462 if (interface)
3463 {
3464 class_name = interface->getNameAsString();
3465 return true;
3466 }
3467 }
3468 return false;
3469}
3470
3471
3472//----------------------------------------------------------------------
3473// Type Completion
3474//----------------------------------------------------------------------
3475
3476bool
3477ClangASTContext::GetCompleteType (void* type)
3478{
3479 if (!type)
3480 return false;
3481 const bool allow_completion = true;
3482 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3483}
3484
3485ConstString
3486ClangASTContext::GetTypeName (void* type)
3487{
3488 std::string type_name;
3489 if (type)
3490 {
3491 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3492 clang::QualType qual_type(GetQualType(type));
3493 printing_policy.SuppressTagKeyword = true;
3494 printing_policy.LangOpts.WChar = true;
3495 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3496 if (typedef_type)
3497 {
3498 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3499 type_name = typedef_decl->getQualifiedNameAsString();
3500 }
3501 else
3502 {
3503 type_name = qual_type.getAsString(printing_policy);
3504 }
3505 }
3506 return ConstString(type_name);
3507}
3508
3509uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003510ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003511{
3512 if (!type)
3513 return 0;
3514
3515 if (pointee_or_element_clang_type)
3516 pointee_or_element_clang_type->Clear();
3517
3518 clang::QualType qual_type (GetQualType(type));
3519
3520 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3521 switch (type_class)
3522 {
3523 case clang::Type::Builtin:
3524 {
3525 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3526
3527 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3528 switch (builtin_type->getKind())
3529 {
3530 case clang::BuiltinType::ObjCId:
3531 case clang::BuiltinType::ObjCClass:
3532 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003533 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003534 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3535 break;
3536
3537 case clang::BuiltinType::ObjCSel:
3538 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003539 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003540 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3541 break;
3542
3543 case clang::BuiltinType::Bool:
3544 case clang::BuiltinType::Char_U:
3545 case clang::BuiltinType::UChar:
3546 case clang::BuiltinType::WChar_U:
3547 case clang::BuiltinType::Char16:
3548 case clang::BuiltinType::Char32:
3549 case clang::BuiltinType::UShort:
3550 case clang::BuiltinType::UInt:
3551 case clang::BuiltinType::ULong:
3552 case clang::BuiltinType::ULongLong:
3553 case clang::BuiltinType::UInt128:
3554 case clang::BuiltinType::Char_S:
3555 case clang::BuiltinType::SChar:
3556 case clang::BuiltinType::WChar_S:
3557 case clang::BuiltinType::Short:
3558 case clang::BuiltinType::Int:
3559 case clang::BuiltinType::Long:
3560 case clang::BuiltinType::LongLong:
3561 case clang::BuiltinType::Int128:
3562 case clang::BuiltinType::Float:
3563 case clang::BuiltinType::Double:
3564 case clang::BuiltinType::LongDouble:
3565 builtin_type_flags |= eTypeIsScalar;
3566 if (builtin_type->isInteger())
3567 {
3568 builtin_type_flags |= eTypeIsInteger;
3569 if (builtin_type->isSignedInteger())
3570 builtin_type_flags |= eTypeIsSigned;
3571 }
3572 else if (builtin_type->isFloatingPoint())
3573 builtin_type_flags |= eTypeIsFloat;
3574 break;
3575 default:
3576 break;
3577 }
3578 return builtin_type_flags;
3579 }
3580
3581 case clang::Type::BlockPointer:
3582 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003583 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003584 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3585
3586 case clang::Type::Complex:
3587 {
3588 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3589 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3590 if (complex_type)
3591 {
3592 clang::QualType complex_element_type (complex_type->getElementType());
3593 if (complex_element_type->isIntegerType())
3594 complex_type_flags |= eTypeIsFloat;
3595 else if (complex_element_type->isFloatingType())
3596 complex_type_flags |= eTypeIsInteger;
3597 }
3598 return complex_type_flags;
3599 }
3600 break;
3601
3602 case clang::Type::ConstantArray:
3603 case clang::Type::DependentSizedArray:
3604 case clang::Type::IncompleteArray:
3605 case clang::Type::VariableArray:
3606 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003607 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003608 return eTypeHasChildren | eTypeIsArray;
3609
3610 case clang::Type::DependentName: return 0;
3611 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3612 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3613 case clang::Type::Decltype: return 0;
3614
3615 case clang::Type::Enum:
3616 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003617 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003618 return eTypeIsEnumeration | eTypeHasValue;
3619
3620 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003621 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003622 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003623 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003624
3625 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3626 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3627 case clang::Type::InjectedClassName: return 0;
3628
3629 case clang::Type::LValueReference:
3630 case clang::Type::RValueReference:
3631 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003632 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003633 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3634
3635 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3636
3637 case clang::Type::ObjCObjectPointer:
3638 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003639 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003640 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3641
3642 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3643 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3644
3645 case clang::Type::Pointer:
3646 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003647 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003648 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3649
3650 case clang::Type::Record:
3651 if (qual_type->getAsCXXRecordDecl())
3652 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3653 else
3654 return eTypeHasChildren | eTypeIsStructUnion;
3655 break;
3656 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3657 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3658 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3659
3660 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003661 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 +00003662 case clang::Type::TypeOfExpr: return 0;
3663 case clang::Type::TypeOf: return 0;
3664 case clang::Type::UnresolvedUsing: return 0;
3665
3666 case clang::Type::ExtVector:
3667 case clang::Type::Vector:
3668 {
3669 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3670 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3671 if (vector_type)
3672 {
3673 if (vector_type->isIntegerType())
3674 vector_type_flags |= eTypeIsFloat;
3675 else if (vector_type->isFloatingType())
3676 vector_type_flags |= eTypeIsInteger;
3677 }
3678 return vector_type_flags;
3679 }
3680 default: return 0;
3681 }
3682 return 0;
3683}
3684
3685
3686
3687lldb::LanguageType
3688ClangASTContext::GetMinimumLanguage (void* type)
3689{
3690 if (!type)
3691 return lldb::eLanguageTypeC;
3692
3693 // If the type is a reference, then resolve it to what it refers to first:
3694 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3695 if (qual_type->isAnyPointerType())
3696 {
3697 if (qual_type->isObjCObjectPointerType())
3698 return lldb::eLanguageTypeObjC;
3699
3700 clang::QualType pointee_type (qual_type->getPointeeType());
3701 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3702 return lldb::eLanguageTypeC_plus_plus;
3703 if (pointee_type->isObjCObjectOrInterfaceType())
3704 return lldb::eLanguageTypeObjC;
3705 if (pointee_type->isObjCClassType())
3706 return lldb::eLanguageTypeObjC;
3707 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3708 return lldb::eLanguageTypeObjC;
3709 }
3710 else
3711 {
3712 if (qual_type->isObjCObjectOrInterfaceType())
3713 return lldb::eLanguageTypeObjC;
3714 if (qual_type->getAsCXXRecordDecl())
3715 return lldb::eLanguageTypeC_plus_plus;
3716 switch (qual_type->getTypeClass())
3717 {
3718 default:
3719 break;
3720 case clang::Type::Builtin:
3721 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3722 {
3723 default:
3724 case clang::BuiltinType::Void:
3725 case clang::BuiltinType::Bool:
3726 case clang::BuiltinType::Char_U:
3727 case clang::BuiltinType::UChar:
3728 case clang::BuiltinType::WChar_U:
3729 case clang::BuiltinType::Char16:
3730 case clang::BuiltinType::Char32:
3731 case clang::BuiltinType::UShort:
3732 case clang::BuiltinType::UInt:
3733 case clang::BuiltinType::ULong:
3734 case clang::BuiltinType::ULongLong:
3735 case clang::BuiltinType::UInt128:
3736 case clang::BuiltinType::Char_S:
3737 case clang::BuiltinType::SChar:
3738 case clang::BuiltinType::WChar_S:
3739 case clang::BuiltinType::Short:
3740 case clang::BuiltinType::Int:
3741 case clang::BuiltinType::Long:
3742 case clang::BuiltinType::LongLong:
3743 case clang::BuiltinType::Int128:
3744 case clang::BuiltinType::Float:
3745 case clang::BuiltinType::Double:
3746 case clang::BuiltinType::LongDouble:
3747 break;
3748
3749 case clang::BuiltinType::NullPtr:
3750 return eLanguageTypeC_plus_plus;
3751
3752 case clang::BuiltinType::ObjCId:
3753 case clang::BuiltinType::ObjCClass:
3754 case clang::BuiltinType::ObjCSel:
3755 return eLanguageTypeObjC;
3756
3757 case clang::BuiltinType::Dependent:
3758 case clang::BuiltinType::Overload:
3759 case clang::BuiltinType::BoundMember:
3760 case clang::BuiltinType::UnknownAny:
3761 break;
3762 }
3763 break;
3764 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003765 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003766 }
3767 }
3768 return lldb::eLanguageTypeC;
3769}
3770
3771lldb::TypeClass
3772ClangASTContext::GetTypeClass (void* type)
3773{
3774 if (!type)
3775 return lldb::eTypeClassInvalid;
3776
3777 clang::QualType qual_type(GetQualType(type));
3778
3779 switch (qual_type->getTypeClass())
3780 {
3781 case clang::Type::UnaryTransform: break;
3782 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3783 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3784 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3785 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3786 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3787 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3788 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3789 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3790 case clang::Type::Vector: return lldb::eTypeClassVector;
3791 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3792 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3793 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3794 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3795 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3796 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3797 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3798 case clang::Type::Complex:
3799 if (qual_type->isComplexType())
3800 return lldb::eTypeClassComplexFloat;
3801 else
3802 return lldb::eTypeClassComplexInteger;
3803 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3804 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3805 case clang::Type::Record:
3806 {
3807 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3808 const clang::RecordDecl *record_decl = record_type->getDecl();
3809 if (record_decl->isUnion())
3810 return lldb::eTypeClassUnion;
3811 else if (record_decl->isStruct())
3812 return lldb::eTypeClassStruct;
3813 else
3814 return lldb::eTypeClassClass;
3815 }
3816 break;
3817 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3818 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3819 case clang::Type::UnresolvedUsing: break;
3820 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003821 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003822 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003823 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003824
3825 case clang::Type::Attributed: break;
3826 case clang::Type::TemplateTypeParm: break;
3827 case clang::Type::SubstTemplateTypeParm: break;
3828 case clang::Type::SubstTemplateTypeParmPack:break;
3829 case clang::Type::Auto: break;
3830 case clang::Type::InjectedClassName: break;
3831 case clang::Type::DependentName: break;
3832 case clang::Type::DependentTemplateSpecialization: break;
3833 case clang::Type::PackExpansion: break;
3834
3835 case clang::Type::TypeOfExpr: break;
3836 case clang::Type::TypeOf: break;
3837 case clang::Type::Decltype: break;
3838 case clang::Type::TemplateSpecialization: break;
3839 case clang::Type::Atomic: break;
3840
3841 // pointer type decayed from an array or function type.
3842 case clang::Type::Decayed: break;
3843 case clang::Type::Adjusted: break;
3844 }
3845 // We don't know hot to display this type...
3846 return lldb::eTypeClassOther;
3847
3848}
3849
3850unsigned
3851ClangASTContext::GetTypeQualifiers(void* type)
3852{
3853 if (type)
3854 return GetQualType(type).getQualifiers().getCVRQualifiers();
3855 return 0;
3856}
3857
3858//----------------------------------------------------------------------
3859// Creating related types
3860//----------------------------------------------------------------------
3861
Greg Claytona1e5dc82015-08-11 22:53:00 +00003862CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003863ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3864{
3865 if (type)
3866 {
3867 clang::QualType qual_type(GetCanonicalQualType(type));
3868
3869 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3870
3871 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003872 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003873
Greg Claytona1e5dc82015-08-11 22:53:00 +00003874 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003875
3876 // TODO: the real stride will be >= this value.. find the real one!
3877 if (stride)
3878 *stride = element_type.GetByteSize(nullptr);
3879
3880 return element_type;
3881
3882 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003883 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003884}
3885
Greg Claytona1e5dc82015-08-11 22:53:00 +00003886CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003887ClangASTContext::GetCanonicalType (void* type)
3888{
3889 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003890 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3891 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003892}
3893
3894static clang::QualType
3895GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3896{
3897 if (qual_type->isPointerType())
3898 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3899 else
3900 qual_type = qual_type.getUnqualifiedType();
3901 qual_type.removeLocalConst();
3902 qual_type.removeLocalRestrict();
3903 qual_type.removeLocalVolatile();
3904 return qual_type;
3905}
3906
Greg Claytona1e5dc82015-08-11 22:53:00 +00003907CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003908ClangASTContext::GetFullyUnqualifiedType (void* type)
3909{
3910 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003911 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3912 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003913}
3914
3915
3916int
3917ClangASTContext::GetFunctionArgumentCount (void* type)
3918{
3919 if (type)
3920 {
3921 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3922 if (func)
3923 return func->getNumParams();
3924 }
3925 return -1;
3926}
3927
Greg Claytona1e5dc82015-08-11 22:53:00 +00003928CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003929ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3930{
3931 if (type)
3932 {
3933 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3934 if (func)
3935 {
3936 const uint32_t num_args = func->getNumParams();
3937 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003938 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003939 }
3940 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003941 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003942}
3943
Greg Claytona1e5dc82015-08-11 22:53:00 +00003944CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003945ClangASTContext::GetFunctionReturnType (void* type)
3946{
3947 if (type)
3948 {
3949 clang::QualType qual_type(GetCanonicalQualType(type));
3950 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3951 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003952 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003953 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003954 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003955}
3956
3957size_t
3958ClangASTContext::GetNumMemberFunctions (void* type)
3959{
3960 size_t num_functions = 0;
3961 if (type)
3962 {
3963 clang::QualType qual_type(GetCanonicalQualType(type));
3964 switch (qual_type->getTypeClass()) {
3965 case clang::Type::Record:
3966 if (GetCompleteQualType (getASTContext(), qual_type))
3967 {
3968 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3969 const clang::RecordDecl *record_decl = record_type->getDecl();
3970 assert(record_decl);
3971 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3972 if (cxx_record_decl)
3973 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
3974 }
3975 break;
3976
3977 case clang::Type::ObjCObjectPointer:
3978 if (GetCompleteType(type))
3979 {
3980 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
3981 if (objc_class_type)
3982 {
3983 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
3984 if (class_interface_decl)
3985 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3986 }
3987 }
3988 break;
3989
3990 case clang::Type::ObjCObject:
3991 case clang::Type::ObjCInterface:
3992 if (GetCompleteType(type))
3993 {
3994 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
3995 if (objc_class_type)
3996 {
3997 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3998 if (class_interface_decl)
3999 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4000 }
4001 }
4002 break;
4003
4004
4005 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004006 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004007
4008 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004009 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004010
4011 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004012 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004013
4014 default:
4015 break;
4016 }
4017 }
4018 return num_functions;
4019}
4020
4021TypeMemberFunctionImpl
4022ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
4023{
4024 std::string name("");
4025 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004026 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00004027 clang::ObjCMethodDecl *method_decl(nullptr);
4028 if (type)
4029 {
4030 clang::QualType qual_type(GetCanonicalQualType(type));
4031 switch (qual_type->getTypeClass()) {
4032 case clang::Type::Record:
4033 if (GetCompleteQualType (getASTContext(), qual_type))
4034 {
4035 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4036 const clang::RecordDecl *record_decl = record_type->getDecl();
4037 assert(record_decl);
4038 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4039 if (cxx_record_decl)
4040 {
4041 auto method_iter = cxx_record_decl->method_begin();
4042 auto method_end = cxx_record_decl->method_end();
4043 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4044 {
4045 std::advance(method_iter, idx);
4046 auto method_decl = method_iter->getCanonicalDecl();
4047 if (method_decl)
4048 {
4049 if (!method_decl->getName().empty())
4050 name.assign(method_decl->getName().data());
4051 else
4052 name.clear();
4053 if (method_decl->isStatic())
4054 kind = lldb::eMemberFunctionKindStaticMethod;
4055 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4056 kind = lldb::eMemberFunctionKindConstructor;
4057 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4058 kind = lldb::eMemberFunctionKindDestructor;
4059 else
4060 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004061 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004062 }
4063 }
4064 }
4065 }
4066 break;
4067
4068 case clang::Type::ObjCObjectPointer:
4069 if (GetCompleteType(type))
4070 {
4071 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4072 if (objc_class_type)
4073 {
4074 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4075 if (class_interface_decl)
4076 {
4077 auto method_iter = class_interface_decl->meth_begin();
4078 auto method_end = class_interface_decl->meth_end();
4079 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4080 {
4081 std::advance(method_iter, idx);
4082 method_decl = method_iter->getCanonicalDecl();
4083 if (method_decl)
4084 {
4085 name = method_decl->getSelector().getAsString();
4086 if (method_decl->isClassMethod())
4087 kind = lldb::eMemberFunctionKindStaticMethod;
4088 else
4089 kind = lldb::eMemberFunctionKindInstanceMethod;
4090 }
4091 }
4092 }
4093 }
4094 }
4095 break;
4096
4097 case clang::Type::ObjCObject:
4098 case clang::Type::ObjCInterface:
4099 if (GetCompleteType(type))
4100 {
4101 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4102 if (objc_class_type)
4103 {
4104 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
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::Typedef:
4128 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4129
4130 case clang::Type::Elaborated:
4131 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4132
4133 case clang::Type::Paren:
4134 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4135
4136 default:
4137 break;
4138 }
4139 }
4140
4141 if (kind == eMemberFunctionKindUnknown)
4142 return TypeMemberFunctionImpl();
4143 if (method_decl)
4144 return TypeMemberFunctionImpl(method_decl, name, kind);
4145 if (type)
4146 return TypeMemberFunctionImpl(clang_type, name, kind);
4147
4148 return TypeMemberFunctionImpl();
4149}
4150
Greg Claytona1e5dc82015-08-11 22:53:00 +00004151CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004152ClangASTContext::GetNonReferenceType (void* type)
4153{
4154 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004155 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4156 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004157}
4158
Greg Claytona1e5dc82015-08-11 22:53:00 +00004159CompilerType
4160ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004161 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004162 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004163{
4164 if (type && typedef_name && typedef_name[0])
4165 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004166 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004167 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004168 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004169 clang::ASTContext* clang_ast = ast->getASTContext();
4170 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004171
4172 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004173 if (decl_ctx == nullptr)
4174 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004175
Greg Claytond8d4a572015-08-11 21:38:15 +00004176 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4177 decl_ctx,
4178 clang::SourceLocation(),
4179 clang::SourceLocation(),
4180 &clang_ast->Idents.get(typedef_name),
4181 clang_ast->getTrivialTypeSourceInfo(qual_type));
4182
4183 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4184
4185 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004186 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004187 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004188 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004189
4190}
4191
Greg Claytona1e5dc82015-08-11 22:53:00 +00004192CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004193ClangASTContext::GetPointeeType (void* type)
4194{
4195 if (type)
4196 {
4197 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004198 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004199 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004200 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004201}
4202
Greg Claytona1e5dc82015-08-11 22:53:00 +00004203CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004204ClangASTContext::GetPointerType (void* type)
4205{
4206 if (type)
4207 {
4208 clang::QualType qual_type (GetQualType(type));
4209
4210 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4211 switch (type_class)
4212 {
4213 case clang::Type::ObjCObject:
4214 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004215 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004216
4217 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004218 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004219 }
4220 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004221 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004222}
4223
Greg Clayton56939cb2015-09-17 22:23:34 +00004224
4225CompilerType
4226ClangASTContext::GetLValueReferenceType (void *type)
4227{
4228 if (type)
4229 return CompilerType(this, getASTContext()->getLValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4230 else
4231 return CompilerType();
4232}
4233
4234CompilerType
4235ClangASTContext::GetRValueReferenceType (void *type)
4236{
4237 if (type)
4238 return CompilerType(this, getASTContext()->getRValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4239 else
4240 return CompilerType();
4241}
4242
4243CompilerType
4244ClangASTContext::AddConstModifier (void *type)
4245{
4246 if (type)
4247 {
4248 clang::QualType result(GetQualType(type));
4249 result.addConst();
4250 return CompilerType (this, result.getAsOpaquePtr());
4251 }
4252 return CompilerType();
4253}
4254
4255CompilerType
4256ClangASTContext::AddVolatileModifier (void *type)
4257{
4258 if (type)
4259 {
4260 clang::QualType result(GetQualType(type));
4261 result.addVolatile();
4262 return CompilerType (this, result.getAsOpaquePtr());
4263 }
4264 return CompilerType();
4265
4266}
4267
4268CompilerType
4269ClangASTContext::AddRestrictModifier (void *type)
4270{
4271 if (type)
4272 {
4273 clang::QualType result(GetQualType(type));
4274 result.addRestrict();
4275 return CompilerType (this, result.getAsOpaquePtr());
4276 }
4277 return CompilerType();
4278
4279}
4280
4281CompilerType
4282ClangASTContext::CreateTypedef (void *type, const char *typedef_name, const CompilerDeclContext &compiler_decl_ctx)
4283{
4284 if (type)
4285 {
4286 clang::ASTContext* clang_ast = getASTContext();
4287 clang::QualType qual_type (GetQualType(type));
4288
4289 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4290 if (decl_ctx == nullptr)
4291 decl_ctx = getASTContext()->getTranslationUnitDecl();
4292
4293 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4294 decl_ctx,
4295 clang::SourceLocation(),
4296 clang::SourceLocation(),
4297 &clang_ast->Idents.get(typedef_name),
4298 clang_ast->getTrivialTypeSourceInfo(qual_type));
4299
4300 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4301
4302 // Get a uniqued clang::QualType for the typedef decl type
4303 return CompilerType (this, clang_ast->getTypedefType (decl).getAsOpaquePtr());
4304
4305 }
4306 return CompilerType();
4307
4308}
4309
Greg Claytona1e5dc82015-08-11 22:53:00 +00004310CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004311ClangASTContext::GetTypedefedType (void* type)
4312{
4313 if (type)
4314 {
4315 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4316 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004317 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004318 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004319 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004320}
4321
Greg Claytona1e5dc82015-08-11 22:53:00 +00004322CompilerType
4323ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004324{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004325 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004326 {
4327 clang::QualType qual_type(GetQualType(type));
4328 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004329 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004330 }
4331 return type;
4332}
4333
4334
4335//----------------------------------------------------------------------
4336// Create related types using the current type's AST
4337//----------------------------------------------------------------------
4338
Greg Claytona1e5dc82015-08-11 22:53:00 +00004339CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004340ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004341{
Greg Clayton99558cc42015-08-24 23:46:31 +00004342 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004343}
4344//----------------------------------------------------------------------
4345// Exploring the type
4346//----------------------------------------------------------------------
4347
4348uint64_t
4349ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4350{
4351 if (GetCompleteType (type))
4352 {
4353 clang::QualType qual_type(GetCanonicalQualType(type));
4354 switch (qual_type->getTypeClass())
4355 {
4356 case clang::Type::ObjCInterface:
4357 case clang::Type::ObjCObject:
4358 {
4359 ExecutionContext exe_ctx (exe_scope);
4360 Process *process = exe_ctx.GetProcessPtr();
4361 if (process)
4362 {
4363 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4364 if (objc_runtime)
4365 {
4366 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004367 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004368 return bit_size;
4369 }
4370 }
4371 else
4372 {
4373 static bool g_printed = false;
4374 if (!g_printed)
4375 {
4376 StreamString s;
4377 DumpTypeDescription(&s);
4378
4379 llvm::outs() << "warning: trying to determine the size of type ";
4380 llvm::outs() << s.GetString() << "\n";
4381 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4382 llvm::outs() << "backtrace:\n";
4383 llvm::sys::PrintStackTrace(llvm::outs());
4384 llvm::outs() << "\n";
4385 g_printed = true;
4386 }
4387 }
4388 }
4389 // fallthrough
4390 default:
4391 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4392 if (bit_size == 0)
4393 {
4394 if (qual_type->isIncompleteArrayType())
4395 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4396 }
4397 if (qual_type->isObjCObjectOrInterfaceType())
4398 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4399 return bit_size;
4400 }
4401 }
4402 return 0;
4403}
4404
4405size_t
4406ClangASTContext::GetTypeBitAlign (void* type)
4407{
4408 if (GetCompleteType(type))
4409 return getASTContext()->getTypeAlign(GetQualType(type));
4410 return 0;
4411}
4412
4413
4414lldb::Encoding
4415ClangASTContext::GetEncoding (void* type, uint64_t &count)
4416{
4417 if (!type)
4418 return lldb::eEncodingInvalid;
4419
4420 count = 1;
4421 clang::QualType qual_type(GetCanonicalQualType(type));
4422
4423 switch (qual_type->getTypeClass())
4424 {
4425 case clang::Type::UnaryTransform:
4426 break;
4427
4428 case clang::Type::FunctionNoProto:
4429 case clang::Type::FunctionProto:
4430 break;
4431
4432 case clang::Type::IncompleteArray:
4433 case clang::Type::VariableArray:
4434 break;
4435
4436 case clang::Type::ConstantArray:
4437 break;
4438
4439 case clang::Type::ExtVector:
4440 case clang::Type::Vector:
4441 // TODO: Set this to more than one???
4442 break;
4443
4444 case clang::Type::Builtin:
4445 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4446 {
4447 default: assert(0 && "Unknown builtin type!");
4448 case clang::BuiltinType::Void:
4449 break;
4450
4451 case clang::BuiltinType::Bool:
4452 case clang::BuiltinType::Char_S:
4453 case clang::BuiltinType::SChar:
4454 case clang::BuiltinType::WChar_S:
4455 case clang::BuiltinType::Char16:
4456 case clang::BuiltinType::Char32:
4457 case clang::BuiltinType::Short:
4458 case clang::BuiltinType::Int:
4459 case clang::BuiltinType::Long:
4460 case clang::BuiltinType::LongLong:
4461 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4462
4463 case clang::BuiltinType::Char_U:
4464 case clang::BuiltinType::UChar:
4465 case clang::BuiltinType::WChar_U:
4466 case clang::BuiltinType::UShort:
4467 case clang::BuiltinType::UInt:
4468 case clang::BuiltinType::ULong:
4469 case clang::BuiltinType::ULongLong:
4470 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4471
4472 case clang::BuiltinType::Float:
4473 case clang::BuiltinType::Double:
4474 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4475
4476 case clang::BuiltinType::ObjCClass:
4477 case clang::BuiltinType::ObjCId:
4478 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4479
4480 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4481
4482 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4483 case clang::BuiltinType::Kind::BoundMember:
4484 case clang::BuiltinType::Kind::BuiltinFn:
4485 case clang::BuiltinType::Kind::Dependent:
4486 case clang::BuiltinType::Kind::Half:
4487 case clang::BuiltinType::Kind::OCLEvent:
4488 case clang::BuiltinType::Kind::OCLImage1d:
4489 case clang::BuiltinType::Kind::OCLImage1dArray:
4490 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4491 case clang::BuiltinType::Kind::OCLImage2d:
4492 case clang::BuiltinType::Kind::OCLImage2dArray:
4493 case clang::BuiltinType::Kind::OCLImage3d:
4494 case clang::BuiltinType::Kind::OCLSampler:
4495 case clang::BuiltinType::Kind::Overload:
4496 case clang::BuiltinType::Kind::PseudoObject:
4497 case clang::BuiltinType::Kind::UnknownAny:
4498 break;
4499 }
4500 break;
4501 // All pointer types are represented as unsigned integer encodings.
4502 // We may nee to add a eEncodingPointer if we ever need to know the
4503 // difference
4504 case clang::Type::ObjCObjectPointer:
4505 case clang::Type::BlockPointer:
4506 case clang::Type::Pointer:
4507 case clang::Type::LValueReference:
4508 case clang::Type::RValueReference:
4509 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4510 case clang::Type::Complex:
4511 {
4512 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4513 if (qual_type->isComplexType())
4514 encoding = lldb::eEncodingIEEE754;
4515 else
4516 {
4517 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4518 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004519 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004520 else
4521 encoding = lldb::eEncodingSint;
4522 }
4523 count = 2;
4524 return encoding;
4525 }
4526
4527 case clang::Type::ObjCInterface: break;
4528 case clang::Type::Record: break;
4529 case clang::Type::Enum: return lldb::eEncodingSint;
4530 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004531 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004532
4533 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004534 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004535
4536 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004537 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004538
4539 case clang::Type::DependentSizedArray:
4540 case clang::Type::DependentSizedExtVector:
4541 case clang::Type::UnresolvedUsing:
4542 case clang::Type::Attributed:
4543 case clang::Type::TemplateTypeParm:
4544 case clang::Type::SubstTemplateTypeParm:
4545 case clang::Type::SubstTemplateTypeParmPack:
4546 case clang::Type::Auto:
4547 case clang::Type::InjectedClassName:
4548 case clang::Type::DependentName:
4549 case clang::Type::DependentTemplateSpecialization:
4550 case clang::Type::PackExpansion:
4551 case clang::Type::ObjCObject:
4552
4553 case clang::Type::TypeOfExpr:
4554 case clang::Type::TypeOf:
4555 case clang::Type::Decltype:
4556 case clang::Type::TemplateSpecialization:
4557 case clang::Type::Atomic:
4558 case clang::Type::Adjusted:
4559 break;
4560
4561 // pointer type decayed from an array or function type.
4562 case clang::Type::Decayed:
4563 break;
4564 }
4565 count = 0;
4566 return lldb::eEncodingInvalid;
4567}
4568
4569lldb::Format
4570ClangASTContext::GetFormat (void* type)
4571{
4572 if (!type)
4573 return lldb::eFormatDefault;
4574
4575 clang::QualType qual_type(GetCanonicalQualType(type));
4576
4577 switch (qual_type->getTypeClass())
4578 {
4579 case clang::Type::UnaryTransform:
4580 break;
4581
4582 case clang::Type::FunctionNoProto:
4583 case clang::Type::FunctionProto:
4584 break;
4585
4586 case clang::Type::IncompleteArray:
4587 case clang::Type::VariableArray:
4588 break;
4589
4590 case clang::Type::ConstantArray:
4591 return lldb::eFormatVoid; // no value
4592
4593 case clang::Type::ExtVector:
4594 case clang::Type::Vector:
4595 break;
4596
4597 case clang::Type::Builtin:
4598 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4599 {
4600 //default: assert(0 && "Unknown builtin type!");
4601 case clang::BuiltinType::UnknownAny:
4602 case clang::BuiltinType::Void:
4603 case clang::BuiltinType::BoundMember:
4604 break;
4605
4606 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4607 case clang::BuiltinType::Char_S:
4608 case clang::BuiltinType::SChar:
4609 case clang::BuiltinType::WChar_S:
4610 case clang::BuiltinType::Char_U:
4611 case clang::BuiltinType::UChar:
4612 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4613 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4614 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4615 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4616 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4617 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4618 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4619 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4620 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4621 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4622 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4623 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4624 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4625 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4626 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4627 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004628 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004629 return lldb::eFormatHex;
4630 }
4631 break;
4632 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4633 case clang::Type::BlockPointer: return lldb::eFormatHex;
4634 case clang::Type::Pointer: return lldb::eFormatHex;
4635 case clang::Type::LValueReference:
4636 case clang::Type::RValueReference: return lldb::eFormatHex;
4637 case clang::Type::MemberPointer: break;
4638 case clang::Type::Complex:
4639 {
4640 if (qual_type->isComplexType())
4641 return lldb::eFormatComplex;
4642 else
4643 return lldb::eFormatComplexInteger;
4644 }
4645 case clang::Type::ObjCInterface: break;
4646 case clang::Type::Record: break;
4647 case clang::Type::Enum: return lldb::eFormatEnum;
4648 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004649 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004650 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004651 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004652 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004653 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004654 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004655 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004656 case clang::Type::DependentSizedArray:
4657 case clang::Type::DependentSizedExtVector:
4658 case clang::Type::UnresolvedUsing:
4659 case clang::Type::Attributed:
4660 case clang::Type::TemplateTypeParm:
4661 case clang::Type::SubstTemplateTypeParm:
4662 case clang::Type::SubstTemplateTypeParmPack:
4663 case clang::Type::InjectedClassName:
4664 case clang::Type::DependentName:
4665 case clang::Type::DependentTemplateSpecialization:
4666 case clang::Type::PackExpansion:
4667 case clang::Type::ObjCObject:
4668
4669 case clang::Type::TypeOfExpr:
4670 case clang::Type::TypeOf:
4671 case clang::Type::Decltype:
4672 case clang::Type::TemplateSpecialization:
4673 case clang::Type::Atomic:
4674 case clang::Type::Adjusted:
4675 break;
4676
4677 // pointer type decayed from an array or function type.
4678 case clang::Type::Decayed:
4679 break;
4680 }
4681 // We don't know hot to display this type...
4682 return lldb::eFormatBytes;
4683}
4684
4685static bool
4686ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4687{
4688 while (class_interface_decl)
4689 {
4690 if (class_interface_decl->ivar_size() > 0)
4691 return true;
4692
4693 if (check_superclass)
4694 class_interface_decl = class_interface_decl->getSuperClass();
4695 else
4696 break;
4697 }
4698 return false;
4699}
4700
4701uint32_t
4702ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4703{
4704 if (!type)
4705 return 0;
4706
4707 uint32_t num_children = 0;
4708 clang::QualType qual_type(GetQualType(type));
4709 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4710 switch (type_class)
4711 {
4712 case clang::Type::Builtin:
4713 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4714 {
4715 case clang::BuiltinType::ObjCId: // child is Class
4716 case clang::BuiltinType::ObjCClass: // child is Class
4717 num_children = 1;
4718 break;
4719
4720 default:
4721 break;
4722 }
4723 break;
4724
4725 case clang::Type::Complex: return 0;
4726
4727 case clang::Type::Record:
4728 if (GetCompleteQualType (getASTContext(), qual_type))
4729 {
4730 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4731 const clang::RecordDecl *record_decl = record_type->getDecl();
4732 assert(record_decl);
4733 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4734 if (cxx_record_decl)
4735 {
4736 if (omit_empty_base_classes)
4737 {
4738 // Check each base classes to see if it or any of its
4739 // base classes contain any fields. This can help
4740 // limit the noise in variable views by not having to
4741 // show base classes that contain no members.
4742 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4743 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4744 base_class != base_class_end;
4745 ++base_class)
4746 {
4747 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4748
4749 // Skip empty base classes
4750 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4751 continue;
4752
4753 num_children++;
4754 }
4755 }
4756 else
4757 {
4758 // Include all base classes
4759 num_children += cxx_record_decl->getNumBases();
4760 }
4761
4762 }
4763 clang::RecordDecl::field_iterator field, field_end;
4764 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4765 ++num_children;
4766 }
4767 break;
4768
4769 case clang::Type::ObjCObject:
4770 case clang::Type::ObjCInterface:
4771 if (GetCompleteQualType (getASTContext(), qual_type))
4772 {
4773 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4774 assert (objc_class_type);
4775 if (objc_class_type)
4776 {
4777 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4778
4779 if (class_interface_decl)
4780 {
4781
4782 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4783 if (superclass_interface_decl)
4784 {
4785 if (omit_empty_base_classes)
4786 {
4787 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4788 ++num_children;
4789 }
4790 else
4791 ++num_children;
4792 }
4793
4794 num_children += class_interface_decl->ivar_size();
4795 }
4796 }
4797 }
4798 break;
4799
4800 case clang::Type::ObjCObjectPointer:
4801 {
4802 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4803 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004804 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004805 // If this type points to a simple type, then it has 1 child
4806 if (num_pointee_children == 0)
4807 num_children = 1;
4808 else
4809 num_children = num_pointee_children;
4810 }
4811 break;
4812
4813 case clang::Type::Vector:
4814 case clang::Type::ExtVector:
4815 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4816 break;
4817
4818 case clang::Type::ConstantArray:
4819 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4820 break;
4821
4822 case clang::Type::Pointer:
4823 {
4824 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4825 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004826 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004827 if (num_pointee_children == 0)
4828 {
4829 // We have a pointer to a pointee type that claims it has no children.
4830 // We will want to look at
4831 num_children = GetNumPointeeChildren (pointee_type);
4832 }
4833 else
4834 num_children = num_pointee_children;
4835 }
4836 break;
4837
4838 case clang::Type::LValueReference:
4839 case clang::Type::RValueReference:
4840 {
4841 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4842 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004843 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004844 // If this type points to a simple type, then it has 1 child
4845 if (num_pointee_children == 0)
4846 num_children = 1;
4847 else
4848 num_children = num_pointee_children;
4849 }
4850 break;
4851
4852
4853 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004854 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004855 break;
4856
4857 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004858 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004859 break;
4860
4861 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004862 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004863 break;
4864 default:
4865 break;
4866 }
4867 return num_children;
4868}
4869
Greg Clayton56939cb2015-09-17 22:23:34 +00004870CompilerType
4871ClangASTContext::GetBuiltinTypeByName (const ConstString &name)
4872{
4873 return GetBasicType (GetBasicTypeEnumeration (name));
4874}
4875
Greg Claytond8d4a572015-08-11 21:38:15 +00004876lldb::BasicType
4877ClangASTContext::GetBasicTypeEnumeration (void* type)
4878{
4879 if (type)
4880 {
4881 clang::QualType qual_type(GetQualType(type));
4882 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4883 if (type_class == clang::Type::Builtin)
4884 {
4885 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4886 {
4887 case clang::BuiltinType::Void: return eBasicTypeVoid;
4888 case clang::BuiltinType::Bool: return eBasicTypeBool;
4889 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4890 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4891 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4892 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4893 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4894 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4895 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4896 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4897 case clang::BuiltinType::Short: return eBasicTypeShort;
4898 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4899 case clang::BuiltinType::Int: return eBasicTypeInt;
4900 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4901 case clang::BuiltinType::Long: return eBasicTypeLong;
4902 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4903 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4904 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4905 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4906 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4907
4908 case clang::BuiltinType::Half: return eBasicTypeHalf;
4909 case clang::BuiltinType::Float: return eBasicTypeFloat;
4910 case clang::BuiltinType::Double: return eBasicTypeDouble;
4911 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4912
4913 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4914 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4915 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4916 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004917 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004918 return eBasicTypeOther;
4919 }
4920 }
4921 }
4922 return eBasicTypeInvalid;
4923}
4924
Greg Clayton99558cc42015-08-24 23:46:31 +00004925void
4926ClangASTContext::ForEachEnumerator (void* type, std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback)
4927{
4928 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
4929 if (enum_type)
4930 {
4931 const clang::EnumDecl *enum_decl = enum_type->getDecl();
4932 if (enum_decl)
4933 {
4934 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
4935
4936 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
4937 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
4938 {
4939 ConstString name(enum_pos->getNameAsString().c_str());
4940 if (!callback (integer_type, name, enum_pos->getInitVal()))
4941 break;
4942 }
4943 }
4944 }
4945}
4946
Greg Claytond8d4a572015-08-11 21:38:15 +00004947
4948#pragma mark Aggregate Types
4949
4950uint32_t
Greg Claytond8d4a572015-08-11 21:38:15 +00004951ClangASTContext::GetNumFields (void* type)
4952{
4953 if (!type)
4954 return 0;
4955
4956 uint32_t count = 0;
4957 clang::QualType qual_type(GetCanonicalQualType(type));
4958 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4959 switch (type_class)
4960 {
4961 case clang::Type::Record:
4962 if (GetCompleteType(type))
4963 {
4964 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
4965 if (record_type)
4966 {
4967 clang::RecordDecl *record_decl = record_type->getDecl();
4968 if (record_decl)
4969 {
4970 uint32_t field_idx = 0;
4971 clang::RecordDecl::field_iterator field, field_end;
4972 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4973 ++field_idx;
4974 count = field_idx;
4975 }
4976 }
4977 }
4978 break;
4979
4980 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004981 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004982 break;
4983
4984 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004985 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004986 break;
4987
4988 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004989 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004990 break;
4991
4992 case clang::Type::ObjCObjectPointer:
4993 if (GetCompleteType(type))
4994 {
4995 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4996 if (objc_class_type)
4997 {
4998 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4999
5000 if (class_interface_decl)
5001 count = class_interface_decl->ivar_size();
5002 }
5003 }
5004 break;
5005
5006 case clang::Type::ObjCObject:
5007 case clang::Type::ObjCInterface:
5008 if (GetCompleteType(type))
5009 {
5010 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5011 if (objc_class_type)
5012 {
5013 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5014
5015 if (class_interface_decl)
5016 count = class_interface_decl->ivar_size();
5017 }
5018 }
5019 break;
5020
5021 default:
5022 break;
5023 }
5024 return count;
5025}
5026
Greg Claytond8d4a572015-08-11 21:38:15 +00005027static clang_type_t
5028GetObjCFieldAtIndex (clang::ASTContext *ast,
5029 clang::ObjCInterfaceDecl *class_interface_decl,
5030 size_t idx,
5031 std::string& name,
5032 uint64_t *bit_offset_ptr,
5033 uint32_t *bitfield_bit_size_ptr,
5034 bool *is_bitfield_ptr)
5035{
5036 if (class_interface_decl)
5037 {
5038 if (idx < (class_interface_decl->ivar_size()))
5039 {
5040 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5041 uint32_t ivar_idx = 0;
5042
5043 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5044 {
5045 if (ivar_idx == idx)
5046 {
5047 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5048
5049 clang::QualType ivar_qual_type(ivar_decl->getType());
5050
5051 name.assign(ivar_decl->getNameAsString());
5052
5053 if (bit_offset_ptr)
5054 {
5055 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5056 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5057 }
5058
5059 const bool is_bitfield = ivar_pos->isBitField();
5060
5061 if (bitfield_bit_size_ptr)
5062 {
5063 *bitfield_bit_size_ptr = 0;
5064
5065 if (is_bitfield && ast)
5066 {
5067 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5068 llvm::APSInt bitfield_apsint;
5069 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5070 {
5071 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5072 }
5073 }
5074 }
5075 if (is_bitfield_ptr)
5076 *is_bitfield_ptr = is_bitfield;
5077
5078 return ivar_qual_type.getAsOpaquePtr();
5079 }
5080 }
5081 }
5082 }
5083 return nullptr;
5084}
5085
Greg Claytona1e5dc82015-08-11 22:53:00 +00005086CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005087ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
5088 std::string& name,
5089 uint64_t *bit_offset_ptr,
5090 uint32_t *bitfield_bit_size_ptr,
5091 bool *is_bitfield_ptr)
5092{
5093 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005094 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005095
5096 clang::QualType qual_type(GetCanonicalQualType(type));
5097 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5098 switch (type_class)
5099 {
5100 case clang::Type::Record:
5101 if (GetCompleteType(type))
5102 {
5103 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5104 const clang::RecordDecl *record_decl = record_type->getDecl();
5105 uint32_t field_idx = 0;
5106 clang::RecordDecl::field_iterator field, field_end;
5107 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5108 {
5109 if (idx == field_idx)
5110 {
5111 // Print the member type if requested
5112 // Print the member name and equal sign
5113 name.assign(field->getNameAsString());
5114
5115 // Figure out the type byte size (field_type_info.first) and
5116 // alignment (field_type_info.second) from the AST context.
5117 if (bit_offset_ptr)
5118 {
5119 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5120 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5121 }
5122
5123 const bool is_bitfield = field->isBitField();
5124
5125 if (bitfield_bit_size_ptr)
5126 {
5127 *bitfield_bit_size_ptr = 0;
5128
5129 if (is_bitfield)
5130 {
5131 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5132 llvm::APSInt bitfield_apsint;
5133 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5134 {
5135 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5136 }
5137 }
5138 }
5139 if (is_bitfield_ptr)
5140 *is_bitfield_ptr = is_bitfield;
5141
Greg Claytona1e5dc82015-08-11 22:53:00 +00005142 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005143 }
5144 }
5145 }
5146 break;
5147
5148 case clang::Type::ObjCObjectPointer:
5149 if (GetCompleteType(type))
5150 {
5151 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5152 if (objc_class_type)
5153 {
5154 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005155 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 +00005156 }
5157 }
5158 break;
5159
5160 case clang::Type::ObjCObject:
5161 case clang::Type::ObjCInterface:
5162 if (GetCompleteType(type))
5163 {
5164 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5165 assert (objc_class_type);
5166 if (objc_class_type)
5167 {
5168 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005169 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 +00005170 }
5171 }
5172 break;
5173
5174
5175 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005176 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005177 GetFieldAtIndex (idx,
5178 name,
5179 bit_offset_ptr,
5180 bitfield_bit_size_ptr,
5181 is_bitfield_ptr);
5182
5183 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005184 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005185 GetFieldAtIndex (idx,
5186 name,
5187 bit_offset_ptr,
5188 bitfield_bit_size_ptr,
5189 is_bitfield_ptr);
5190
5191 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005192 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005193 GetFieldAtIndex (idx,
5194 name,
5195 bit_offset_ptr,
5196 bitfield_bit_size_ptr,
5197 is_bitfield_ptr);
5198
5199 default:
5200 break;
5201 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005202 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005203}
5204
Greg Clayton99558cc42015-08-24 23:46:31 +00005205uint32_t
5206ClangASTContext::GetNumDirectBaseClasses (void *type)
5207{
5208 uint32_t count = 0;
5209 clang::QualType qual_type(GetCanonicalQualType(type));
5210 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5211 switch (type_class)
5212 {
5213 case clang::Type::Record:
5214 if (GetCompleteType(type))
5215 {
5216 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5217 if (cxx_record_decl)
5218 count = cxx_record_decl->getNumBases();
5219 }
5220 break;
5221
5222 case clang::Type::ObjCObjectPointer:
5223 count = GetPointeeType(type).GetNumDirectBaseClasses();
5224 break;
5225
5226 case clang::Type::ObjCObject:
5227 if (GetCompleteType(type))
5228 {
5229 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5230 if (objc_class_type)
5231 {
5232 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5233
5234 if (class_interface_decl && class_interface_decl->getSuperClass())
5235 count = 1;
5236 }
5237 }
5238 break;
5239 case clang::Type::ObjCInterface:
5240 if (GetCompleteType(type))
5241 {
5242 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5243 if (objc_interface_type)
5244 {
5245 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5246
5247 if (class_interface_decl && class_interface_decl->getSuperClass())
5248 count = 1;
5249 }
5250 }
5251 break;
5252
5253
5254 case clang::Type::Typedef:
5255 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5256 break;
5257
5258 case clang::Type::Elaborated:
5259 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5260 break;
5261
5262 case clang::Type::Paren:
5263 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5264
5265 default:
5266 break;
5267 }
5268 return count;
5269
5270}
5271
5272uint32_t
5273ClangASTContext::GetNumVirtualBaseClasses (void *type)
5274{
5275 uint32_t count = 0;
5276 clang::QualType qual_type(GetCanonicalQualType(type));
5277 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5278 switch (type_class)
5279 {
5280 case clang::Type::Record:
5281 if (GetCompleteType(type))
5282 {
5283 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5284 if (cxx_record_decl)
5285 count = cxx_record_decl->getNumVBases();
5286 }
5287 break;
5288
5289 case clang::Type::Typedef:
5290 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5291 break;
5292
5293 case clang::Type::Elaborated:
5294 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5295 break;
5296
5297 case clang::Type::Paren:
5298 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5299 break;
5300
5301 default:
5302 break;
5303 }
5304 return count;
5305
5306}
5307
5308CompilerType
5309ClangASTContext::GetDirectBaseClassAtIndex (void *type, size_t idx, uint32_t *bit_offset_ptr)
5310{
5311 clang::QualType qual_type(GetCanonicalQualType(type));
5312 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5313 switch (type_class)
5314 {
5315 case clang::Type::Record:
5316 if (GetCompleteType(type))
5317 {
5318 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5319 if (cxx_record_decl)
5320 {
5321 uint32_t curr_idx = 0;
5322 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5323 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5324 base_class != base_class_end;
5325 ++base_class, ++curr_idx)
5326 {
5327 if (curr_idx == idx)
5328 {
5329 if (bit_offset_ptr)
5330 {
5331 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5332 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5333 if (base_class->isVirtual())
5334 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5335 else
5336 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5337 }
5338 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5339 }
5340 }
5341 }
5342 }
5343 break;
5344
5345 case clang::Type::ObjCObjectPointer:
5346 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5347
5348 case clang::Type::ObjCObject:
5349 if (idx == 0 && GetCompleteType(type))
5350 {
5351 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5352 if (objc_class_type)
5353 {
5354 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5355
5356 if (class_interface_decl)
5357 {
5358 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5359 if (superclass_interface_decl)
5360 {
5361 if (bit_offset_ptr)
5362 *bit_offset_ptr = 0;
5363 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5364 }
5365 }
5366 }
5367 }
5368 break;
5369 case clang::Type::ObjCInterface:
5370 if (idx == 0 && GetCompleteType(type))
5371 {
5372 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5373 if (objc_interface_type)
5374 {
5375 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5376
5377 if (class_interface_decl)
5378 {
5379 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5380 if (superclass_interface_decl)
5381 {
5382 if (bit_offset_ptr)
5383 *bit_offset_ptr = 0;
5384 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5385 }
5386 }
5387 }
5388 }
5389 break;
5390
5391
5392 case clang::Type::Typedef:
5393 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5394
5395 case clang::Type::Elaborated:
5396 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5397
5398 case clang::Type::Paren:
5399 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5400
5401 default:
5402 break;
5403 }
5404 return CompilerType();
5405}
5406
5407CompilerType
5408ClangASTContext::GetVirtualBaseClassAtIndex (void *type,
5409 size_t idx,
5410 uint32_t *bit_offset_ptr)
5411{
5412 clang::QualType qual_type(GetCanonicalQualType(type));
5413 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5414 switch (type_class)
5415 {
5416 case clang::Type::Record:
5417 if (GetCompleteType(type))
5418 {
5419 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5420 if (cxx_record_decl)
5421 {
5422 uint32_t curr_idx = 0;
5423 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5424 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5425 base_class != base_class_end;
5426 ++base_class, ++curr_idx)
5427 {
5428 if (curr_idx == idx)
5429 {
5430 if (bit_offset_ptr)
5431 {
5432 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5433 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5434 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5435
5436 }
5437 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5438 }
5439 }
5440 }
5441 }
5442 break;
5443
5444 case clang::Type::Typedef:
5445 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5446
5447 case clang::Type::Elaborated:
5448 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5449
5450 case clang::Type::Paren:
5451 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5452
5453 default:
5454 break;
5455 }
5456 return CompilerType();
5457
5458}
5459
Greg Claytond8d4a572015-08-11 21:38:15 +00005460// If a pointer to a pointee type (the clang_type arg) says that it has no
5461// children, then we either need to trust it, or override it and return a
5462// different result. For example, an "int *" has one child that is an integer,
5463// but a function pointer doesn't have any children. Likewise if a Record type
5464// claims it has no children, then there really is nothing to show.
5465uint32_t
5466ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5467{
5468 if (type.isNull())
5469 return 0;
5470
5471 clang::QualType qual_type(type.getCanonicalType());
5472 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5473 switch (type_class)
5474 {
5475 case clang::Type::Builtin:
5476 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5477 {
5478 case clang::BuiltinType::UnknownAny:
5479 case clang::BuiltinType::Void:
5480 case clang::BuiltinType::NullPtr:
5481 case clang::BuiltinType::OCLEvent:
5482 case clang::BuiltinType::OCLImage1d:
5483 case clang::BuiltinType::OCLImage1dArray:
5484 case clang::BuiltinType::OCLImage1dBuffer:
5485 case clang::BuiltinType::OCLImage2d:
5486 case clang::BuiltinType::OCLImage2dArray:
5487 case clang::BuiltinType::OCLImage3d:
5488 case clang::BuiltinType::OCLSampler:
5489 return 0;
5490 case clang::BuiltinType::Bool:
5491 case clang::BuiltinType::Char_U:
5492 case clang::BuiltinType::UChar:
5493 case clang::BuiltinType::WChar_U:
5494 case clang::BuiltinType::Char16:
5495 case clang::BuiltinType::Char32:
5496 case clang::BuiltinType::UShort:
5497 case clang::BuiltinType::UInt:
5498 case clang::BuiltinType::ULong:
5499 case clang::BuiltinType::ULongLong:
5500 case clang::BuiltinType::UInt128:
5501 case clang::BuiltinType::Char_S:
5502 case clang::BuiltinType::SChar:
5503 case clang::BuiltinType::WChar_S:
5504 case clang::BuiltinType::Short:
5505 case clang::BuiltinType::Int:
5506 case clang::BuiltinType::Long:
5507 case clang::BuiltinType::LongLong:
5508 case clang::BuiltinType::Int128:
5509 case clang::BuiltinType::Float:
5510 case clang::BuiltinType::Double:
5511 case clang::BuiltinType::LongDouble:
5512 case clang::BuiltinType::Dependent:
5513 case clang::BuiltinType::Overload:
5514 case clang::BuiltinType::ObjCId:
5515 case clang::BuiltinType::ObjCClass:
5516 case clang::BuiltinType::ObjCSel:
5517 case clang::BuiltinType::BoundMember:
5518 case clang::BuiltinType::Half:
5519 case clang::BuiltinType::ARCUnbridgedCast:
5520 case clang::BuiltinType::PseudoObject:
5521 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005522 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005523 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005524 default:
5525 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005526 }
5527 break;
5528
5529 case clang::Type::Complex: return 1;
5530 case clang::Type::Pointer: return 1;
5531 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5532 case clang::Type::LValueReference: return 1;
5533 case clang::Type::RValueReference: return 1;
5534 case clang::Type::MemberPointer: return 0;
5535 case clang::Type::ConstantArray: return 0;
5536 case clang::Type::IncompleteArray: return 0;
5537 case clang::Type::VariableArray: return 0;
5538 case clang::Type::DependentSizedArray: return 0;
5539 case clang::Type::DependentSizedExtVector: return 0;
5540 case clang::Type::Vector: return 0;
5541 case clang::Type::ExtVector: return 0;
5542 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5543 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5544 case clang::Type::UnresolvedUsing: return 0;
5545 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5546 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5547 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5548 case clang::Type::TypeOfExpr: return 0;
5549 case clang::Type::TypeOf: return 0;
5550 case clang::Type::Decltype: return 0;
5551 case clang::Type::Record: return 0;
5552 case clang::Type::Enum: return 1;
5553 case clang::Type::TemplateTypeParm: return 1;
5554 case clang::Type::SubstTemplateTypeParm: return 1;
5555 case clang::Type::TemplateSpecialization: return 1;
5556 case clang::Type::InjectedClassName: return 0;
5557 case clang::Type::DependentName: return 1;
5558 case clang::Type::DependentTemplateSpecialization: return 1;
5559 case clang::Type::ObjCObject: return 0;
5560 case clang::Type::ObjCInterface: return 0;
5561 case clang::Type::ObjCObjectPointer: return 1;
5562 default:
5563 break;
5564 }
5565 return 0;
5566}
5567
5568
Greg Claytona1e5dc82015-08-11 22:53:00 +00005569CompilerType
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005570ClangASTContext::GetChildCompilerTypeAtIndex (void* type,
5571 ExecutionContext *exe_ctx,
5572 size_t idx,
5573 bool transparent_pointers,
5574 bool omit_empty_base_classes,
5575 bool ignore_array_bounds,
5576 std::string& child_name,
5577 uint32_t &child_byte_size,
5578 int32_t &child_byte_offset,
5579 uint32_t &child_bitfield_bit_size,
5580 uint32_t &child_bitfield_bit_offset,
5581 bool &child_is_base_class,
5582 bool &child_is_deref_of_parent,
5583 ValueObject *valobj)
Greg Claytond8d4a572015-08-11 21:38:15 +00005584{
5585 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005586 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005587
5588 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5589 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5590 child_bitfield_bit_size = 0;
5591 child_bitfield_bit_offset = 0;
5592 child_is_base_class = false;
5593
5594 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5595 uint32_t bit_offset;
5596 switch (parent_type_class)
5597 {
5598 case clang::Type::Builtin:
5599 if (idx_is_valid)
5600 {
5601 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5602 {
5603 case clang::BuiltinType::ObjCId:
5604 case clang::BuiltinType::ObjCClass:
5605 child_name = "isa";
5606 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005607 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005608
5609 default:
5610 break;
5611 }
5612 }
5613 break;
5614
5615 case clang::Type::Record:
5616 if (idx_is_valid && GetCompleteType(type))
5617 {
5618 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5619 const clang::RecordDecl *record_decl = record_type->getDecl();
5620 assert(record_decl);
5621 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5622 uint32_t child_idx = 0;
5623
5624 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5625 if (cxx_record_decl)
5626 {
5627 // We might have base classes to print out first
5628 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5629 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5630 base_class != base_class_end;
5631 ++base_class)
5632 {
5633 const clang::CXXRecordDecl *base_class_decl = nullptr;
5634
5635 // Skip empty base classes
5636 if (omit_empty_base_classes)
5637 {
5638 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5639 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5640 continue;
5641 }
5642
5643 if (idx == child_idx)
5644 {
5645 if (base_class_decl == nullptr)
5646 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5647
5648
5649 if (base_class->isVirtual())
5650 {
5651 bool handled = false;
5652 if (valobj)
5653 {
5654 Error err;
5655 AddressType addr_type = eAddressTypeInvalid;
5656 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5657
5658 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5659 {
5660
5661 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5662 Process *process = exe_ctx.GetProcessPtr();
5663 if (process)
5664 {
5665 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5666 if (vtable_ctx)
5667 {
5668 if (vtable_ctx->isMicrosoft())
5669 {
5670 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5671
5672 if (vtable_ptr_addr)
5673 {
5674 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5675
5676 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5677 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5678 {
5679 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5680 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5681 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5682 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5683 if (base_offset != UINT32_MAX)
5684 {
5685 handled = true;
5686 bit_offset = base_offset * 8;
5687 }
5688 }
5689 }
5690 }
5691 else
5692 {
5693 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5694 if (vtable_ptr_addr)
5695 {
5696 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5697 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5698 {
5699 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5700 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5701 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5702 if (base_offset != UINT32_MAX)
5703 {
5704 handled = true;
5705 bit_offset = base_offset * 8;
5706 }
5707 }
5708 }
5709 }
5710 }
5711 }
5712 }
5713
5714 }
5715 if (!handled)
5716 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5717 }
5718 else
5719 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5720
5721 // Base classes should be a multiple of 8 bits in size
5722 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005723 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005724 child_name = base_class_clang_type.GetTypeName().AsCString("");
5725 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5726
5727 // Base classes bit sizes should be a multiple of 8 bits in size
5728 assert (base_class_clang_type_bit_size % 8 == 0);
5729 child_byte_size = base_class_clang_type_bit_size / 8;
5730 child_is_base_class = true;
5731 return base_class_clang_type;
5732 }
5733 // We don't increment the child index in the for loop since we might
5734 // be skipping empty base classes
5735 ++child_idx;
5736 }
5737 }
5738 // Make sure index is in range...
5739 uint32_t field_idx = 0;
5740 clang::RecordDecl::field_iterator field, field_end;
5741 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5742 {
5743 if (idx == child_idx)
5744 {
5745 // Print the member type if requested
5746 // Print the member name and equal sign
5747 child_name.assign(field->getNameAsString().c_str());
5748
5749 // Figure out the type byte size (field_type_info.first) and
5750 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005751 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005752 assert(field_idx < record_layout.getFieldCount());
5753 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5754
5755 // Figure out the field offset within the current struct/union/class type
5756 bit_offset = record_layout.getFieldOffset (field_idx);
5757 child_byte_offset = bit_offset / 8;
5758 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5759 child_bitfield_bit_offset = bit_offset % 8;
5760
5761 return field_clang_type;
5762 }
5763 }
5764 }
5765 break;
5766
5767 case clang::Type::ObjCObject:
5768 case clang::Type::ObjCInterface:
5769 if (idx_is_valid && GetCompleteType(type))
5770 {
5771 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5772 assert (objc_class_type);
5773 if (objc_class_type)
5774 {
5775 uint32_t child_idx = 0;
5776 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5777
5778 if (class_interface_decl)
5779 {
5780
5781 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5782 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5783 if (superclass_interface_decl)
5784 {
5785 if (omit_empty_base_classes)
5786 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005787 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005788 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5789 {
5790 if (idx == 0)
5791 {
5792 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5793
5794
5795 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5796
5797 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5798
5799 child_byte_size = ivar_type_info.Width / 8;
5800 child_byte_offset = 0;
5801 child_is_base_class = true;
5802
Greg Claytona1e5dc82015-08-11 22:53:00 +00005803 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005804 }
5805
5806 ++child_idx;
5807 }
5808 }
5809 else
5810 ++child_idx;
5811 }
5812
5813 const uint32_t superclass_idx = child_idx;
5814
5815 if (idx < (child_idx + class_interface_decl->ivar_size()))
5816 {
5817 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5818
5819 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5820 {
5821 if (child_idx == idx)
5822 {
5823 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5824
5825 clang::QualType ivar_qual_type(ivar_decl->getType());
5826
5827 child_name.assign(ivar_decl->getNameAsString().c_str());
5828
5829 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5830
5831 child_byte_size = ivar_type_info.Width / 8;
5832
5833 // Figure out the field offset within the current struct/union/class type
5834 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5835 // that doesn't account for the space taken up by unbacked properties, or from
5836 // the changing size of base classes that are newer than this class.
5837 // So if we have a process around that we can ask about this object, do so.
5838 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5839 Process *process = nullptr;
5840 if (exe_ctx)
5841 process = exe_ctx->GetProcessPtr();
5842 if (process)
5843 {
5844 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5845 if (objc_runtime != nullptr)
5846 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005847 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005848 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5849 }
5850 }
5851
5852 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5853 bit_offset = UINT32_MAX;
5854
5855 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5856 {
5857 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5858 child_byte_offset = bit_offset / 8;
5859 }
5860
5861 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5862 // of a bitfield within its containing object. So regardless of where we get the byte
5863 // offset from, we still need to get the bit offset for bitfields from the layout.
5864
5865 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5866 {
5867 if (bit_offset == UINT32_MAX)
5868 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5869
5870 child_bitfield_bit_offset = bit_offset % 8;
5871 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005872 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005873 }
5874 ++child_idx;
5875 }
5876 }
5877 }
5878 }
5879 }
5880 break;
5881
5882 case clang::Type::ObjCObjectPointer:
5883 if (idx_is_valid)
5884 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005885 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005886
5887 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5888 {
5889 child_is_deref_of_parent = false;
5890 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005891 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
5892 idx,
5893 transparent_pointers,
5894 omit_empty_base_classes,
5895 ignore_array_bounds,
5896 child_name,
5897 child_byte_size,
5898 child_byte_offset,
5899 child_bitfield_bit_size,
5900 child_bitfield_bit_offset,
5901 child_is_base_class,
5902 tmp_child_is_deref_of_parent,
5903 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00005904 }
5905 else
5906 {
5907 child_is_deref_of_parent = true;
5908 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5909 if (parent_name)
5910 {
5911 child_name.assign(1, '*');
5912 child_name += parent_name;
5913 }
5914
5915 // We have a pointer to an simple type
5916 if (idx == 0 && pointee_clang_type.GetCompleteType())
5917 {
5918 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5919 child_byte_offset = 0;
5920 return pointee_clang_type;
5921 }
5922 }
5923 }
5924 break;
5925
5926 case clang::Type::Vector:
5927 case clang::Type::ExtVector:
5928 if (idx_is_valid)
5929 {
5930 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5931 if (array)
5932 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005933 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005934 if (element_type.GetCompleteType())
5935 {
5936 char element_name[64];
5937 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5938 child_name.assign(element_name);
5939 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5940 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5941 return element_type;
5942 }
5943 }
5944 }
5945 break;
5946
5947 case clang::Type::ConstantArray:
5948 case clang::Type::IncompleteArray:
5949 if (ignore_array_bounds || idx_is_valid)
5950 {
5951 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5952 if (array)
5953 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005954 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005955 if (element_type.GetCompleteType())
5956 {
5957 char element_name[64];
5958 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5959 child_name.assign(element_name);
5960 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5961 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5962 return element_type;
5963 }
5964 }
5965 }
5966 break;
5967
5968
5969 case clang::Type::Pointer:
5970 if (idx_is_valid)
5971 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005972 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005973
5974 // Don't dereference "void *" pointers
5975 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005976 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005977
5978 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5979 {
5980 child_is_deref_of_parent = false;
5981 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005982 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
5983 idx,
5984 transparent_pointers,
5985 omit_empty_base_classes,
5986 ignore_array_bounds,
5987 child_name,
5988 child_byte_size,
5989 child_byte_offset,
5990 child_bitfield_bit_size,
5991 child_bitfield_bit_offset,
5992 child_is_base_class,
5993 tmp_child_is_deref_of_parent,
5994 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00005995 }
5996 else
5997 {
5998 child_is_deref_of_parent = true;
5999
6000 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6001 if (parent_name)
6002 {
6003 child_name.assign(1, '*');
6004 child_name += parent_name;
6005 }
6006
6007 // We have a pointer to an simple type
6008 if (idx == 0)
6009 {
6010 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6011 child_byte_offset = 0;
6012 return pointee_clang_type;
6013 }
6014 }
6015 }
6016 break;
6017
6018 case clang::Type::LValueReference:
6019 case clang::Type::RValueReference:
6020 if (idx_is_valid)
6021 {
6022 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006023 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006024 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6025 {
6026 child_is_deref_of_parent = false;
6027 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006028 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6029 idx,
6030 transparent_pointers,
6031 omit_empty_base_classes,
6032 ignore_array_bounds,
6033 child_name,
6034 child_byte_size,
6035 child_byte_offset,
6036 child_bitfield_bit_size,
6037 child_bitfield_bit_offset,
6038 child_is_base_class,
6039 tmp_child_is_deref_of_parent,
6040 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00006041 }
6042 else
6043 {
6044 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6045 if (parent_name)
6046 {
6047 child_name.assign(1, '&');
6048 child_name += parent_name;
6049 }
6050
6051 // We have a pointer to an simple type
6052 if (idx == 0)
6053 {
6054 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6055 child_byte_offset = 0;
6056 return pointee_clang_type;
6057 }
6058 }
6059 }
6060 break;
6061
6062 case clang::Type::Typedef:
6063 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006064 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006065 return typedefed_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6066 idx,
6067 transparent_pointers,
6068 omit_empty_base_classes,
6069 ignore_array_bounds,
6070 child_name,
6071 child_byte_size,
6072 child_byte_offset,
6073 child_bitfield_bit_size,
6074 child_bitfield_bit_offset,
6075 child_is_base_class,
6076 child_is_deref_of_parent,
6077 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00006078 }
6079 break;
6080
6081 case clang::Type::Elaborated:
6082 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006083 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006084 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6085 idx,
6086 transparent_pointers,
6087 omit_empty_base_classes,
6088 ignore_array_bounds,
6089 child_name,
6090 child_byte_size,
6091 child_byte_offset,
6092 child_bitfield_bit_size,
6093 child_bitfield_bit_offset,
6094 child_is_base_class,
6095 child_is_deref_of_parent,
6096 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00006097 }
6098
6099 case clang::Type::Paren:
6100 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006101 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006102 return paren_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6103 idx,
6104 transparent_pointers,
6105 omit_empty_base_classes,
6106 ignore_array_bounds,
6107 child_name,
6108 child_byte_size,
6109 child_byte_offset,
6110 child_bitfield_bit_size,
6111 child_bitfield_bit_offset,
6112 child_is_base_class,
6113 child_is_deref_of_parent,
6114 valobj);
Greg Claytond8d4a572015-08-11 21:38:15 +00006115 }
6116
6117
6118 default:
6119 break;
6120 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006121 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006122}
6123
6124static uint32_t
6125GetIndexForRecordBase
6126(
6127 const clang::RecordDecl *record_decl,
6128 const clang::CXXBaseSpecifier *base_spec,
6129 bool omit_empty_base_classes
6130 )
6131{
6132 uint32_t child_idx = 0;
6133
6134 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6135
6136 // const char *super_name = record_decl->getNameAsCString();
6137 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6138 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6139 //
6140 if (cxx_record_decl)
6141 {
6142 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6143 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6144 base_class != base_class_end;
6145 ++base_class)
6146 {
6147 if (omit_empty_base_classes)
6148 {
6149 if (BaseSpecifierIsEmpty (base_class))
6150 continue;
6151 }
6152
6153 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6154 // child_idx,
6155 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6156 //
6157 //
6158 if (base_class == base_spec)
6159 return child_idx;
6160 ++child_idx;
6161 }
6162 }
6163
6164 return UINT32_MAX;
6165}
6166
6167
6168static uint32_t
6169GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6170 clang::NamedDecl *canonical_decl,
6171 bool omit_empty_base_classes)
6172{
6173 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6174 omit_empty_base_classes);
6175
6176 clang::RecordDecl::field_iterator field, field_end;
6177 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6178 field != field_end;
6179 ++field, ++child_idx)
6180 {
6181 if (field->getCanonicalDecl() == canonical_decl)
6182 return child_idx;
6183 }
6184
6185 return UINT32_MAX;
6186}
6187
6188// Look for a child member (doesn't include base classes, but it does include
6189// their members) in the type hierarchy. Returns an index path into "clang_type"
6190// on how to reach the appropriate member.
6191//
6192// class A
6193// {
6194// public:
6195// int m_a;
6196// int m_b;
6197// };
6198//
6199// class B
6200// {
6201// };
6202//
6203// class C :
6204// public B,
6205// public A
6206// {
6207// };
6208//
6209// If we have a clang type that describes "class C", and we wanted to looked
6210// "m_b" in it:
6211//
6212// With omit_empty_base_classes == false we would get an integer array back with:
6213// { 1, 1 }
6214// The first index 1 is the child index for "class A" within class C
6215// The second index 1 is the child index for "m_b" within class A
6216//
6217// With omit_empty_base_classes == true we would get an integer array back with:
6218// { 0, 1 }
6219// 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)
6220// The second index 1 is the child index for "m_b" within class A
6221
6222size_t
6223ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6224 bool omit_empty_base_classes,
6225 std::vector<uint32_t>& child_indexes)
6226{
6227 if (type && name && name[0])
6228 {
6229 clang::QualType qual_type(GetCanonicalQualType(type));
6230 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6231 switch (type_class)
6232 {
6233 case clang::Type::Record:
6234 if (GetCompleteType(type))
6235 {
6236 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6237 const clang::RecordDecl *record_decl = record_type->getDecl();
6238
6239 assert(record_decl);
6240 uint32_t child_idx = 0;
6241
6242 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6243
6244 // Try and find a field that matches NAME
6245 clang::RecordDecl::field_iterator field, field_end;
6246 llvm::StringRef name_sref(name);
6247 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6248 field != field_end;
6249 ++field, ++child_idx)
6250 {
6251 llvm::StringRef field_name = field->getName();
6252 if (field_name.empty())
6253 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006254 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006255 child_indexes.push_back(child_idx);
6256 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6257 return child_indexes.size();
6258 child_indexes.pop_back();
6259
6260 }
6261 else if (field_name.equals (name_sref))
6262 {
6263 // We have to add on the number of base classes to this index!
6264 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6265 return child_indexes.size();
6266 }
6267 }
6268
6269 if (cxx_record_decl)
6270 {
6271 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6272
6273 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6274
6275 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6276 // Didn't find things easily, lets let clang do its thang...
6277 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6278 clang::DeclarationName decl_name(&ident_ref);
6279
6280 clang::CXXBasePaths paths;
6281 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6282 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6283 },
6284 paths))
6285 {
6286 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6287 for (path = paths.begin(); path != path_end; ++path)
6288 {
6289 const size_t num_path_elements = path->size();
6290 for (size_t e=0; e<num_path_elements; ++e)
6291 {
6292 clang::CXXBasePathElement elem = (*path)[e];
6293
6294 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6295 if (child_idx == UINT32_MAX)
6296 {
6297 child_indexes.clear();
6298 return 0;
6299 }
6300 else
6301 {
6302 child_indexes.push_back (child_idx);
6303 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6304 }
6305 }
6306 for (clang::NamedDecl *path_decl : path->Decls)
6307 {
6308 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6309 if (child_idx == UINT32_MAX)
6310 {
6311 child_indexes.clear();
6312 return 0;
6313 }
6314 else
6315 {
6316 child_indexes.push_back (child_idx);
6317 }
6318 }
6319 }
6320 return child_indexes.size();
6321 }
6322 }
6323
6324 }
6325 break;
6326
6327 case clang::Type::ObjCObject:
6328 case clang::Type::ObjCInterface:
6329 if (GetCompleteType(type))
6330 {
6331 llvm::StringRef name_sref(name);
6332 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6333 assert (objc_class_type);
6334 if (objc_class_type)
6335 {
6336 uint32_t child_idx = 0;
6337 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6338
6339 if (class_interface_decl)
6340 {
6341 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6342 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6343
6344 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6345 {
6346 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6347
6348 if (ivar_decl->getName().equals (name_sref))
6349 {
6350 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6351 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6352 ++child_idx;
6353
6354 child_indexes.push_back (child_idx);
6355 return child_indexes.size();
6356 }
6357 }
6358
6359 if (superclass_interface_decl)
6360 {
6361 // The super class index is always zero for ObjC classes,
6362 // so we push it onto the child indexes in case we find
6363 // an ivar in our superclass...
6364 child_indexes.push_back (0);
6365
Greg Claytona1e5dc82015-08-11 22:53:00 +00006366 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006367 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6368 omit_empty_base_classes,
6369 child_indexes))
6370 {
6371 // We did find an ivar in a superclass so just
6372 // return the results!
6373 return child_indexes.size();
6374 }
6375
6376 // We didn't find an ivar matching "name" in our
6377 // superclass, pop the superclass zero index that
6378 // we pushed on above.
6379 child_indexes.pop_back();
6380 }
6381 }
6382 }
6383 }
6384 break;
6385
6386 case clang::Type::ObjCObjectPointer:
6387 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006388 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006389 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6390 omit_empty_base_classes,
6391 child_indexes);
6392 }
6393 break;
6394
6395
6396 case clang::Type::ConstantArray:
6397 {
6398 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6399 // const uint64_t element_count = array->getSize().getLimitedValue();
6400 //
6401 // if (idx < element_count)
6402 // {
6403 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6404 //
6405 // char element_name[32];
6406 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6407 //
6408 // child_name.assign(element_name);
6409 // assert(field_type_info.first % 8 == 0);
6410 // child_byte_size = field_type_info.first / 8;
6411 // child_byte_offset = idx * child_byte_size;
6412 // return array->getElementType().getAsOpaquePtr();
6413 // }
6414 }
6415 break;
6416
6417 // case clang::Type::MemberPointerType:
6418 // {
6419 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6420 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6421 //
6422 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6423 // {
6424 // return GetIndexOfChildWithName (ast,
6425 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6426 // name);
6427 // }
6428 // }
6429 // break;
6430 //
6431 case clang::Type::LValueReference:
6432 case clang::Type::RValueReference:
6433 {
6434 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6435 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006436 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006437
6438 if (pointee_clang_type.IsAggregateType ())
6439 {
6440 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6441 omit_empty_base_classes,
6442 child_indexes);
6443 }
6444 }
6445 break;
6446
6447 case clang::Type::Pointer:
6448 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006449 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006450
6451 if (pointee_clang_type.IsAggregateType ())
6452 {
6453 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6454 omit_empty_base_classes,
6455 child_indexes);
6456 }
6457 }
6458 break;
6459
6460 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006461 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006462 omit_empty_base_classes,
6463 child_indexes);
6464
6465 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006466 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006467 omit_empty_base_classes,
6468 child_indexes);
6469
6470 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006471 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006472 omit_empty_base_classes,
6473 child_indexes);
6474
6475 default:
6476 break;
6477 }
6478 }
6479 return 0;
6480}
6481
6482
6483// Get the index of the child of "clang_type" whose name matches. This function
6484// doesn't descend into the children, but only looks one level deep and name
6485// matches can include base class names.
6486
6487uint32_t
6488ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6489{
6490 if (type && name && name[0])
6491 {
6492 clang::QualType qual_type(GetCanonicalQualType(type));
6493
6494 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6495
6496 switch (type_class)
6497 {
6498 case clang::Type::Record:
6499 if (GetCompleteType(type))
6500 {
6501 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6502 const clang::RecordDecl *record_decl = record_type->getDecl();
6503
6504 assert(record_decl);
6505 uint32_t child_idx = 0;
6506
6507 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6508
6509 if (cxx_record_decl)
6510 {
6511 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6512 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6513 base_class != base_class_end;
6514 ++base_class)
6515 {
6516 // Skip empty base classes
6517 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6518 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6519 continue;
6520
Greg Claytona1e5dc82015-08-11 22:53:00 +00006521 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006522 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6523 if (base_class_type_name.compare (name) == 0)
6524 return child_idx;
6525 ++child_idx;
6526 }
6527 }
6528
6529 // Try and find a field that matches NAME
6530 clang::RecordDecl::field_iterator field, field_end;
6531 llvm::StringRef name_sref(name);
6532 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6533 field != field_end;
6534 ++field, ++child_idx)
6535 {
6536 if (field->getName().equals (name_sref))
6537 return child_idx;
6538 }
6539
6540 }
6541 break;
6542
6543 case clang::Type::ObjCObject:
6544 case clang::Type::ObjCInterface:
6545 if (GetCompleteType(type))
6546 {
6547 llvm::StringRef name_sref(name);
6548 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6549 assert (objc_class_type);
6550 if (objc_class_type)
6551 {
6552 uint32_t child_idx = 0;
6553 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6554
6555 if (class_interface_decl)
6556 {
6557 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6558 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6559
6560 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6561 {
6562 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6563
6564 if (ivar_decl->getName().equals (name_sref))
6565 {
6566 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6567 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6568 ++child_idx;
6569
6570 return child_idx;
6571 }
6572 }
6573
6574 if (superclass_interface_decl)
6575 {
6576 if (superclass_interface_decl->getName().equals (name_sref))
6577 return 0;
6578 }
6579 }
6580 }
6581 }
6582 break;
6583
6584 case clang::Type::ObjCObjectPointer:
6585 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006586 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006587 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6588 }
6589 break;
6590
6591 case clang::Type::ConstantArray:
6592 {
6593 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6594 // const uint64_t element_count = array->getSize().getLimitedValue();
6595 //
6596 // if (idx < element_count)
6597 // {
6598 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6599 //
6600 // char element_name[32];
6601 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6602 //
6603 // child_name.assign(element_name);
6604 // assert(field_type_info.first % 8 == 0);
6605 // child_byte_size = field_type_info.first / 8;
6606 // child_byte_offset = idx * child_byte_size;
6607 // return array->getElementType().getAsOpaquePtr();
6608 // }
6609 }
6610 break;
6611
6612 // case clang::Type::MemberPointerType:
6613 // {
6614 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6615 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6616 //
6617 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6618 // {
6619 // return GetIndexOfChildWithName (ast,
6620 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6621 // name);
6622 // }
6623 // }
6624 // break;
6625 //
6626 case clang::Type::LValueReference:
6627 case clang::Type::RValueReference:
6628 {
6629 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006630 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006631
6632 if (pointee_type.IsAggregateType ())
6633 {
6634 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6635 }
6636 }
6637 break;
6638
6639 case clang::Type::Pointer:
6640 {
6641 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006642 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006643
6644 if (pointee_type.IsAggregateType ())
6645 {
6646 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6647 }
6648 else
6649 {
6650 // if (parent_name)
6651 // {
6652 // child_name.assign(1, '*');
6653 // child_name += parent_name;
6654 // }
6655 //
6656 // // We have a pointer to an simple type
6657 // if (idx == 0)
6658 // {
6659 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6660 // assert(clang_type_info.first % 8 == 0);
6661 // child_byte_size = clang_type_info.first / 8;
6662 // child_byte_offset = 0;
6663 // return pointee_type.getAsOpaquePtr();
6664 // }
6665 }
6666 }
6667 break;
6668
6669 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006670 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006671
6672 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006673 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006674
6675 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006676 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006677
6678 default:
6679 break;
6680 }
6681 }
6682 return UINT32_MAX;
6683}
6684
6685
6686size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006687ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006688{
6689 if (!type)
6690 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006691
6692 clang::QualType qual_type (GetCanonicalQualType(type));
6693 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6694 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006695 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006696 case clang::Type::Record:
6697 if (GetCompleteType(type))
6698 {
6699 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6700 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006701 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006702 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6703 if (template_decl)
6704 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006705 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006706 }
6707 break;
6708
6709 case clang::Type::Typedef:
6710 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6711
6712 case clang::Type::Elaborated:
6713 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6714
6715 case clang::Type::Paren:
6716 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6717
6718 default:
6719 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006720 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006721
Greg Claytond8d4a572015-08-11 21:38:15 +00006722 return 0;
6723}
6724
Greg Claytona1e5dc82015-08-11 22:53:00 +00006725CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006726ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006727{
6728 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006729 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006730
6731 clang::QualType qual_type (GetCanonicalQualType(type));
6732 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6733 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006734 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006735 case clang::Type::Record:
6736 if (GetCompleteType(type))
6737 {
6738 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6739 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006740 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006741 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6742 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006743 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006744 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6745 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006746 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006747 case clang::TemplateArgument::Null:
6748 kind = eTemplateArgumentKindNull;
6749 return CompilerType();
6750
6751 case clang::TemplateArgument::Type:
6752 kind = eTemplateArgumentKindType;
6753 return CompilerType(getASTContext(), template_arg.getAsType());
6754
6755 case clang::TemplateArgument::Declaration:
6756 kind = eTemplateArgumentKindDeclaration;
6757 return CompilerType();
6758
6759 case clang::TemplateArgument::Integral:
6760 kind = eTemplateArgumentKindIntegral;
6761 return CompilerType(getASTContext(), template_arg.getIntegralType());
6762
6763 case clang::TemplateArgument::Template:
6764 kind = eTemplateArgumentKindTemplate;
6765 return CompilerType();
6766
6767 case clang::TemplateArgument::TemplateExpansion:
6768 kind = eTemplateArgumentKindTemplateExpansion;
6769 return CompilerType();
6770
6771 case clang::TemplateArgument::Expression:
6772 kind = eTemplateArgumentKindExpression;
6773 return CompilerType();
6774
6775 case clang::TemplateArgument::Pack:
6776 kind = eTemplateArgumentKindPack;
6777 return CompilerType();
6778
6779 default:
6780 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6781 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006782 }
6783 }
6784 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006785 }
6786 break;
6787
6788 case clang::Type::Typedef:
6789 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6790
6791 case clang::Type::Elaborated:
6792 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6793
6794 case clang::Type::Paren:
6795 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6796
6797 default:
6798 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006799 }
6800 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006801 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006802}
6803
6804static bool
6805IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6806{
6807 if (name == nullptr || name[0] == '\0')
6808 return false;
6809
6810#define OPERATOR_PREFIX "operator"
6811#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6812
6813 const char *post_op_name = nullptr;
6814
6815 bool no_space = true;
6816
6817 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6818 return false;
6819
6820 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6821
6822 if (post_op_name[0] == ' ')
6823 {
6824 post_op_name++;
6825 no_space = false;
6826 }
6827
6828#undef OPERATOR_PREFIX
6829#undef OPERATOR_PREFIX_LENGTH
6830
6831 // This is an operator, set the overloaded operator kind to invalid
6832 // in case this is a conversion operator...
6833 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6834
6835 switch (post_op_name[0])
6836 {
6837 default:
6838 if (no_space)
6839 return false;
6840 break;
6841 case 'n':
6842 if (no_space)
6843 return false;
6844 if (strcmp (post_op_name, "new") == 0)
6845 op_kind = clang::OO_New;
6846 else if (strcmp (post_op_name, "new[]") == 0)
6847 op_kind = clang::OO_Array_New;
6848 break;
6849
6850 case 'd':
6851 if (no_space)
6852 return false;
6853 if (strcmp (post_op_name, "delete") == 0)
6854 op_kind = clang::OO_Delete;
6855 else if (strcmp (post_op_name, "delete[]") == 0)
6856 op_kind = clang::OO_Array_Delete;
6857 break;
6858
6859 case '+':
6860 if (post_op_name[1] == '\0')
6861 op_kind = clang::OO_Plus;
6862 else if (post_op_name[2] == '\0')
6863 {
6864 if (post_op_name[1] == '=')
6865 op_kind = clang::OO_PlusEqual;
6866 else if (post_op_name[1] == '+')
6867 op_kind = clang::OO_PlusPlus;
6868 }
6869 break;
6870
6871 case '-':
6872 if (post_op_name[1] == '\0')
6873 op_kind = clang::OO_Minus;
6874 else if (post_op_name[2] == '\0')
6875 {
6876 switch (post_op_name[1])
6877 {
6878 case '=': op_kind = clang::OO_MinusEqual; break;
6879 case '-': op_kind = clang::OO_MinusMinus; break;
6880 case '>': op_kind = clang::OO_Arrow; break;
6881 }
6882 }
6883 else if (post_op_name[3] == '\0')
6884 {
6885 if (post_op_name[2] == '*')
6886 op_kind = clang::OO_ArrowStar; break;
6887 }
6888 break;
6889
6890 case '*':
6891 if (post_op_name[1] == '\0')
6892 op_kind = clang::OO_Star;
6893 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6894 op_kind = clang::OO_StarEqual;
6895 break;
6896
6897 case '/':
6898 if (post_op_name[1] == '\0')
6899 op_kind = clang::OO_Slash;
6900 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6901 op_kind = clang::OO_SlashEqual;
6902 break;
6903
6904 case '%':
6905 if (post_op_name[1] == '\0')
6906 op_kind = clang::OO_Percent;
6907 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6908 op_kind = clang::OO_PercentEqual;
6909 break;
6910
6911
6912 case '^':
6913 if (post_op_name[1] == '\0')
6914 op_kind = clang::OO_Caret;
6915 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6916 op_kind = clang::OO_CaretEqual;
6917 break;
6918
6919 case '&':
6920 if (post_op_name[1] == '\0')
6921 op_kind = clang::OO_Amp;
6922 else if (post_op_name[2] == '\0')
6923 {
6924 switch (post_op_name[1])
6925 {
6926 case '=': op_kind = clang::OO_AmpEqual; break;
6927 case '&': op_kind = clang::OO_AmpAmp; break;
6928 }
6929 }
6930 break;
6931
6932 case '|':
6933 if (post_op_name[1] == '\0')
6934 op_kind = clang::OO_Pipe;
6935 else if (post_op_name[2] == '\0')
6936 {
6937 switch (post_op_name[1])
6938 {
6939 case '=': op_kind = clang::OO_PipeEqual; break;
6940 case '|': op_kind = clang::OO_PipePipe; break;
6941 }
6942 }
6943 break;
6944
6945 case '~':
6946 if (post_op_name[1] == '\0')
6947 op_kind = clang::OO_Tilde;
6948 break;
6949
6950 case '!':
6951 if (post_op_name[1] == '\0')
6952 op_kind = clang::OO_Exclaim;
6953 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6954 op_kind = clang::OO_ExclaimEqual;
6955 break;
6956
6957 case '=':
6958 if (post_op_name[1] == '\0')
6959 op_kind = clang::OO_Equal;
6960 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6961 op_kind = clang::OO_EqualEqual;
6962 break;
6963
6964 case '<':
6965 if (post_op_name[1] == '\0')
6966 op_kind = clang::OO_Less;
6967 else if (post_op_name[2] == '\0')
6968 {
6969 switch (post_op_name[1])
6970 {
6971 case '<': op_kind = clang::OO_LessLess; break;
6972 case '=': op_kind = clang::OO_LessEqual; break;
6973 }
6974 }
6975 else if (post_op_name[3] == '\0')
6976 {
6977 if (post_op_name[2] == '=')
6978 op_kind = clang::OO_LessLessEqual;
6979 }
6980 break;
6981
6982 case '>':
6983 if (post_op_name[1] == '\0')
6984 op_kind = clang::OO_Greater;
6985 else if (post_op_name[2] == '\0')
6986 {
6987 switch (post_op_name[1])
6988 {
6989 case '>': op_kind = clang::OO_GreaterGreater; break;
6990 case '=': op_kind = clang::OO_GreaterEqual; break;
6991 }
6992 }
6993 else if (post_op_name[1] == '>' &&
6994 post_op_name[2] == '=' &&
6995 post_op_name[3] == '\0')
6996 {
6997 op_kind = clang::OO_GreaterGreaterEqual;
6998 }
6999 break;
7000
7001 case ',':
7002 if (post_op_name[1] == '\0')
7003 op_kind = clang::OO_Comma;
7004 break;
7005
7006 case '(':
7007 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7008 op_kind = clang::OO_Call;
7009 break;
7010
7011 case '[':
7012 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7013 op_kind = clang::OO_Subscript;
7014 break;
7015 }
7016
7017 return true;
7018}
7019
7020clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007021ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007022{
7023 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
7024 if (enutype)
7025 return enutype->getDecl();
7026 return NULL;
7027}
7028
7029clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007030ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007031{
7032 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7033 if (record_type)
7034 return record_type->getDecl();
7035 return nullptr;
7036}
7037
7038clang::CXXRecordDecl *
7039ClangASTContext::GetAsCXXRecordDecl (void* type)
7040{
7041 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7042}
7043
7044clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007045ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007046{
7047 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7048 if (objc_class_type)
7049 return objc_class_type->getInterface();
7050 return nullptr;
7051}
7052
7053clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007054ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7055 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007056 AccessType access,
7057 uint32_t bitfield_bit_size)
7058{
7059 if (!type.IsValid() || !field_clang_type.IsValid())
7060 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007061 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007062 if (!ast)
7063 return nullptr;
7064 clang::ASTContext* clang_ast = ast->getASTContext();
7065
7066 clang::FieldDecl *field = nullptr;
7067
7068 clang::Expr *bit_width = nullptr;
7069 if (bitfield_bit_size != 0)
7070 {
7071 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7072 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7073 }
7074
7075 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7076 if (record_decl)
7077 {
7078 field = clang::FieldDecl::Create (*clang_ast,
7079 record_decl,
7080 clang::SourceLocation(),
7081 clang::SourceLocation(),
7082 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7083 GetQualType(field_clang_type), // Field type
7084 nullptr, // TInfo *
7085 bit_width, // BitWidth
7086 false, // Mutable
7087 clang::ICIS_NoInit); // HasInit
7088
7089 if (!name)
7090 {
7091 // Determine whether this field corresponds to an anonymous
7092 // struct or union.
7093 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7094 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7095 if (!Rec->getDeclName()) {
7096 Rec->setAnonymousStructOrUnion(true);
7097 field->setImplicit();
7098
7099 }
7100 }
7101 }
7102
7103 if (field)
7104 {
7105 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7106
7107 record_decl->addDecl(field);
7108
7109#ifdef LLDB_CONFIGURATION_DEBUG
7110 VerifyDecl(field);
7111#endif
7112 }
7113 }
7114 else
7115 {
7116 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7117
7118 if (class_interface_decl)
7119 {
7120 const bool is_synthesized = false;
7121
7122 field_clang_type.GetCompleteType();
7123
7124 field = clang::ObjCIvarDecl::Create (*clang_ast,
7125 class_interface_decl,
7126 clang::SourceLocation(),
7127 clang::SourceLocation(),
7128 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7129 GetQualType(field_clang_type), // Field type
7130 nullptr, // TypeSourceInfo *
7131 ConvertAccessTypeToObjCIvarAccessControl (access),
7132 bit_width,
7133 is_synthesized);
7134
7135 if (field)
7136 {
7137 class_interface_decl->addDecl(field);
7138
7139#ifdef LLDB_CONFIGURATION_DEBUG
7140 VerifyDecl(field);
7141#endif
7142 }
7143 }
7144 }
7145 return field;
7146}
7147
7148void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007149ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007150{
Greg Claytonf73034f2015-09-08 18:15:05 +00007151 if (!type)
7152 return;
7153
7154 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007155 if (!ast)
7156 return;
7157
7158 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7159
7160 if (!record_decl)
7161 return;
7162
7163 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7164
7165 IndirectFieldVector indirect_fields;
7166 clang::RecordDecl::field_iterator field_pos;
7167 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7168 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7169 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7170 {
7171 if (field_pos->isAnonymousStructOrUnion())
7172 {
7173 clang::QualType field_qual_type = field_pos->getType();
7174
7175 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7176
7177 if (!field_record_type)
7178 continue;
7179
7180 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7181
7182 if (!field_record_decl)
7183 continue;
7184
7185 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7186 di != de;
7187 ++di)
7188 {
7189 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7190 {
7191 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7192 chain[0] = *field_pos;
7193 chain[1] = nested_field_decl;
7194 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7195 record_decl,
7196 clang::SourceLocation(),
7197 nested_field_decl->getIdentifier(),
7198 nested_field_decl->getType(),
7199 chain,
7200 2);
7201
7202 indirect_field->setImplicit();
7203
7204 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7205 nested_field_decl->getAccess()));
7206
7207 indirect_fields.push_back(indirect_field);
7208 }
7209 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7210 {
7211 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7212 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7213 chain[0] = *field_pos;
7214
7215 int chain_index = 1;
7216 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7217 nce = nested_indirect_field_decl->chain_end();
7218 nci < nce;
7219 ++nci)
7220 {
7221 chain[chain_index] = *nci;
7222 chain_index++;
7223 }
7224
7225 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7226 record_decl,
7227 clang::SourceLocation(),
7228 nested_indirect_field_decl->getIdentifier(),
7229 nested_indirect_field_decl->getType(),
7230 chain,
7231 nested_chain_size + 1);
7232
7233 indirect_field->setImplicit();
7234
7235 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7236 nested_indirect_field_decl->getAccess()));
7237
7238 indirect_fields.push_back(indirect_field);
7239 }
7240 }
7241 }
7242 }
7243
7244 // Check the last field to see if it has an incomplete array type as its
7245 // last member and if it does, the tell the record decl about it
7246 if (last_field_pos != field_end_pos)
7247 {
7248 if (last_field_pos->getType()->isIncompleteArrayType())
7249 record_decl->hasFlexibleArrayMember();
7250 }
7251
7252 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7253 ifi < ife;
7254 ++ifi)
7255 {
7256 record_decl->addDecl(*ifi);
7257 }
7258}
7259
7260void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007261ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007262{
Greg Claytonf73034f2015-09-08 18:15:05 +00007263 if (type)
7264 {
7265 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7266 if (ast)
7267 {
7268 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007269
Greg Claytonf73034f2015-09-08 18:15:05 +00007270 if (!record_decl)
7271 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007272
Greg Claytonf73034f2015-09-08 18:15:05 +00007273 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7274 }
7275 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007276}
7277
7278clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007279ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7280 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007281 AccessType access)
7282{
7283 clang::VarDecl *var_decl = nullptr;
7284
7285 if (!type.IsValid() || !var_type.IsValid())
7286 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007287 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007288 if (!ast)
7289 return nullptr;
7290
7291 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7292 if (record_decl)
7293 {
7294 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7295 record_decl, // DeclContext *
7296 clang::SourceLocation(), // clang::SourceLocation StartLoc
7297 clang::SourceLocation(), // clang::SourceLocation IdLoc
7298 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7299 GetQualType(var_type), // Variable clang::QualType
7300 nullptr, // TypeSourceInfo *
7301 clang::SC_Static); // StorageClass
7302 if (var_decl)
7303 {
7304 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7305 record_decl->addDecl(var_decl);
7306
7307#ifdef LLDB_CONFIGURATION_DEBUG
7308 VerifyDecl(var_decl);
7309#endif
7310 }
7311 }
7312 return var_decl;
7313}
7314
7315
7316clang::CXXMethodDecl *
7317ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007318 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007319 lldb::AccessType access,
7320 bool is_virtual,
7321 bool is_static,
7322 bool is_inline,
7323 bool is_explicit,
7324 bool is_attr_used,
7325 bool is_artificial)
7326{
7327 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7328 return nullptr;
7329
7330 clang::QualType record_qual_type(GetCanonicalQualType(type));
7331
7332 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7333
7334 if (cxx_record_decl == nullptr)
7335 return nullptr;
7336
7337 clang::QualType method_qual_type (GetQualType(method_clang_type));
7338
7339 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7340
7341 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7342
7343 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7344
7345 if (function_type == nullptr)
7346 return nullptr;
7347
7348 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7349
7350 if (!method_function_prototype)
7351 return nullptr;
7352
7353 unsigned int num_params = method_function_prototype->getNumParams();
7354
7355 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7356 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7357
7358 if (is_artificial)
7359 return nullptr; // skip everything artificial
7360
7361 if (name[0] == '~')
7362 {
7363 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7364 cxx_record_decl,
7365 clang::SourceLocation(),
7366 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7367 method_qual_type,
7368 nullptr,
7369 is_inline,
7370 is_artificial);
7371 cxx_method_decl = cxx_dtor_decl;
7372 }
7373 else if (decl_name == cxx_record_decl->getDeclName())
7374 {
7375 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7376 cxx_record_decl,
7377 clang::SourceLocation(),
7378 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7379 method_qual_type,
7380 nullptr, // TypeSourceInfo *
7381 is_explicit,
7382 is_inline,
7383 is_artificial,
7384 false /*is_constexpr*/);
7385 cxx_method_decl = cxx_ctor_decl;
7386 }
7387 else
7388 {
7389 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7390 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7391
7392 if (IsOperator (name, op_kind))
7393 {
7394 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7395 {
7396 // Check the number of operator parameters. Sometimes we have
7397 // seen bad DWARF that doesn't correctly describe operators and
7398 // if we try to create a method and add it to the class, clang
7399 // will assert and crash, so we need to make sure things are
7400 // acceptable.
7401 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7402 return nullptr;
7403 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7404 cxx_record_decl,
7405 clang::SourceLocation(),
7406 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7407 method_qual_type,
7408 nullptr, // TypeSourceInfo *
7409 SC,
7410 is_inline,
7411 false /*is_constexpr*/,
7412 clang::SourceLocation());
7413 }
7414 else if (num_params == 0)
7415 {
7416 // Conversion operators don't take params...
7417 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7418 cxx_record_decl,
7419 clang::SourceLocation(),
7420 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7421 method_qual_type,
7422 nullptr, // TypeSourceInfo *
7423 is_inline,
7424 is_explicit,
7425 false /*is_constexpr*/,
7426 clang::SourceLocation());
7427 }
7428 }
7429
7430 if (cxx_method_decl == nullptr)
7431 {
7432 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7433 cxx_record_decl,
7434 clang::SourceLocation(),
7435 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7436 method_qual_type,
7437 nullptr, // TypeSourceInfo *
7438 SC,
7439 is_inline,
7440 false /*is_constexpr*/,
7441 clang::SourceLocation());
7442 }
7443 }
7444
7445 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7446
7447 cxx_method_decl->setAccess (access_specifier);
7448 cxx_method_decl->setVirtualAsWritten (is_virtual);
7449
7450 if (is_attr_used)
7451 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7452
7453 // Populate the method decl with parameter decls
7454
7455 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7456
7457 for (unsigned param_index = 0;
7458 param_index < num_params;
7459 ++param_index)
7460 {
7461 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7462 cxx_method_decl,
7463 clang::SourceLocation(),
7464 clang::SourceLocation(),
7465 nullptr, // anonymous
7466 method_function_prototype->getParamType(param_index),
7467 nullptr,
7468 clang::SC_None,
7469 nullptr));
7470 }
7471
7472 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7473
7474 cxx_record_decl->addDecl (cxx_method_decl);
7475
7476 // Sometimes the debug info will mention a constructor (default/copy/move),
7477 // destructor, or assignment operator (copy/move) but there won't be any
7478 // version of this in the code. So we check if the function was artificially
7479 // generated and if it is trivial and this lets the compiler/backend know
7480 // that it can inline the IR for these when it needs to and we can avoid a
7481 // "missing function" error when running expressions.
7482
7483 if (is_artificial)
7484 {
7485 if (cxx_ctor_decl &&
7486 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7487 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7488 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7489 {
7490 cxx_ctor_decl->setDefaulted();
7491 cxx_ctor_decl->setTrivial(true);
7492 }
7493 else if (cxx_dtor_decl)
7494 {
7495 if (cxx_record_decl->hasTrivialDestructor())
7496 {
7497 cxx_dtor_decl->setDefaulted();
7498 cxx_dtor_decl->setTrivial(true);
7499 }
7500 }
7501 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7502 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7503 {
7504 cxx_method_decl->setDefaulted();
7505 cxx_method_decl->setTrivial(true);
7506 }
7507 }
7508
7509#ifdef LLDB_CONFIGURATION_DEBUG
7510 VerifyDecl(cxx_method_decl);
7511#endif
7512
7513 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7514 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7515 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7516 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7517 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7518 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7519 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7520 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7521 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7522 return cxx_method_decl;
7523}
7524
7525
7526#pragma mark C++ Base Classes
7527
7528clang::CXXBaseSpecifier *
7529ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7530{
7531 if (type)
7532 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7533 is_virtual,
7534 base_of_class,
7535 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7536 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7537 clang::SourceLocation());
7538 return nullptr;
7539}
7540
7541void
7542ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7543{
7544 for (unsigned i=0; i<num_base_classes; ++i)
7545 {
7546 delete base_classes[i];
7547 base_classes[i] = nullptr;
7548 }
7549}
7550
7551bool
7552ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7553 unsigned num_base_classes)
7554{
7555 if (type)
7556 {
7557 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7558 if (cxx_record_decl)
7559 {
7560 cxx_record_decl->setBases(base_classes, num_base_classes);
7561 return true;
7562 }
7563 }
7564 return false;
7565}
7566
7567bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007568ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007569{
Greg Claytonf73034f2015-09-08 18:15:05 +00007570 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007571 if (!ast)
7572 return false;
7573 clang::ASTContext* clang_ast = ast->getASTContext();
7574
7575 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7576 {
7577 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7578 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7579 if (class_interface_decl && super_interface_decl)
7580 {
7581 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7582 return true;
7583 }
7584 }
7585 return false;
7586}
7587
7588bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007589ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007590 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007591 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007592 clang::ObjCIvarDecl *ivar_decl,
7593 const char *property_setter_name,
7594 const char *property_getter_name,
7595 uint32_t property_attributes,
7596 ClangASTMetadata *metadata)
7597{
7598 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7599 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007600 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007601 if (!ast)
7602 return false;
7603 clang::ASTContext* clang_ast = ast->getASTContext();
7604
7605 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7606
7607 if (class_interface_decl)
7608 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007609 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007610
7611 if (property_clang_type.IsValid())
7612 property_clang_type_to_access = property_clang_type;
7613 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007614 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007615
7616 if (class_interface_decl && property_clang_type_to_access.IsValid())
7617 {
7618 clang::TypeSourceInfo *prop_type_source;
7619 if (ivar_decl)
7620 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7621 else
7622 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7623
7624 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7625 class_interface_decl,
7626 clang::SourceLocation(), // Source Location
7627 &clang_ast->Idents.get(property_name),
7628 clang::SourceLocation(), //Source Location for AT
7629 clang::SourceLocation(), //Source location for (
7630 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7631 prop_type_source);
7632
7633 if (property_decl)
7634 {
7635 if (metadata)
7636 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7637
7638 class_interface_decl->addDecl (property_decl);
7639
7640 clang::Selector setter_sel, getter_sel;
7641
7642 if (property_setter_name != nullptr)
7643 {
7644 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7645 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7646 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7647 }
7648 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7649 {
7650 std::string setter_sel_string("set");
7651 setter_sel_string.push_back(::toupper(property_name[0]));
7652 setter_sel_string.append(&property_name[1]);
7653 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7654 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7655 }
7656 property_decl->setSetterName(setter_sel);
7657 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7658
7659 if (property_getter_name != nullptr)
7660 {
7661 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7662 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7663 }
7664 else
7665 {
7666 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7667 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7668 }
7669 property_decl->setGetterName(getter_sel);
7670 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7671
7672 if (ivar_decl)
7673 property_decl->setPropertyIvarDecl (ivar_decl);
7674
7675 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7676 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7677 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7678 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7679 if (property_attributes & DW_APPLE_PROPERTY_assign)
7680 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7681 if (property_attributes & DW_APPLE_PROPERTY_retain)
7682 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7683 if (property_attributes & DW_APPLE_PROPERTY_copy)
7684 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7685 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7686 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7687
7688 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7689 {
7690 const bool isInstance = true;
7691 const bool isVariadic = false;
7692 const bool isSynthesized = false;
7693 const bool isImplicitlyDeclared = true;
7694 const bool isDefined = false;
7695 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7696 const bool HasRelatedResultType = false;
7697
7698 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7699 clang::SourceLocation(),
7700 clang::SourceLocation(),
7701 getter_sel,
7702 GetQualType(property_clang_type_to_access),
7703 nullptr,
7704 class_interface_decl,
7705 isInstance,
7706 isVariadic,
7707 isSynthesized,
7708 isImplicitlyDeclared,
7709 isDefined,
7710 impControl,
7711 HasRelatedResultType);
7712
7713 if (getter && metadata)
7714 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7715
7716 if (getter)
7717 {
7718 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7719
7720 class_interface_decl->addDecl(getter);
7721 }
7722 }
7723
7724 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7725 {
7726 clang::QualType result_type = clang_ast->VoidTy;
7727
7728 const bool isInstance = true;
7729 const bool isVariadic = false;
7730 const bool isSynthesized = false;
7731 const bool isImplicitlyDeclared = true;
7732 const bool isDefined = false;
7733 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7734 const bool HasRelatedResultType = false;
7735
7736 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7737 clang::SourceLocation(),
7738 clang::SourceLocation(),
7739 setter_sel,
7740 result_type,
7741 nullptr,
7742 class_interface_decl,
7743 isInstance,
7744 isVariadic,
7745 isSynthesized,
7746 isImplicitlyDeclared,
7747 isDefined,
7748 impControl,
7749 HasRelatedResultType);
7750
7751 if (setter && metadata)
7752 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7753
7754 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7755
7756 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7757 setter,
7758 clang::SourceLocation(),
7759 clang::SourceLocation(),
7760 nullptr, // anonymous
7761 GetQualType(property_clang_type_to_access),
7762 nullptr,
7763 clang::SC_Auto,
7764 nullptr));
7765
7766 if (setter)
7767 {
7768 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7769
7770 class_interface_decl->addDecl(setter);
7771 }
7772 }
7773
7774 return true;
7775 }
7776 }
7777 }
7778 return false;
7779}
7780
7781bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007782ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007783{
7784 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7785 if (class_interface_decl)
7786 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7787 return false;
7788}
7789
7790
7791clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007792ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007793 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007794 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007795 lldb::AccessType access,
7796 bool is_artificial)
7797{
7798 if (!type || !method_clang_type.IsValid())
7799 return nullptr;
7800
7801 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7802
7803 if (class_interface_decl == nullptr)
7804 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007805 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7806 if (lldb_ast == nullptr)
7807 return nullptr;
7808 clang::ASTContext *ast = lldb_ast->getASTContext();
7809
Greg Claytond8d4a572015-08-11 21:38:15 +00007810 const char *selector_start = ::strchr (name, ' ');
7811 if (selector_start == nullptr)
7812 return nullptr;
7813
7814 selector_start++;
7815 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7816
7817 size_t len = 0;
7818 const char *start;
7819 //printf ("name = '%s'\n", name);
7820
7821 unsigned num_selectors_with_args = 0;
7822 for (start = selector_start;
7823 start && *start != '\0' && *start != ']';
7824 start += len)
7825 {
7826 len = ::strcspn(start, ":]");
7827 bool has_arg = (start[len] == ':');
7828 if (has_arg)
7829 ++num_selectors_with_args;
7830 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7831 if (has_arg)
7832 len += 1;
7833 }
7834
7835
7836 if (selector_idents.size() == 0)
7837 return nullptr;
7838
7839 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7840 selector_idents.data());
7841
7842 clang::QualType method_qual_type (GetQualType(method_clang_type));
7843
7844 // Populate the method decl with parameter decls
7845 const clang::Type *method_type(method_qual_type.getTypePtr());
7846
7847 if (method_type == nullptr)
7848 return nullptr;
7849
7850 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7851
7852 if (!method_function_prototype)
7853 return nullptr;
7854
7855
7856 bool is_variadic = false;
7857 bool is_synthesized = false;
7858 bool is_defined = false;
7859 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7860
7861 const unsigned num_args = method_function_prototype->getNumParams();
7862
7863 if (num_args != num_selectors_with_args)
7864 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7865
7866 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7867 clang::SourceLocation(), // beginLoc,
7868 clang::SourceLocation(), // endLoc,
7869 method_selector,
7870 method_function_prototype->getReturnType(),
7871 nullptr, // TypeSourceInfo *ResultTInfo,
7872 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7873 name[0] == '-',
7874 is_variadic,
7875 is_synthesized,
7876 true, // is_implicitly_declared; we force this to true because we don't have source locations
7877 is_defined,
7878 imp_control,
7879 false /*has_related_result_type*/);
7880
7881
7882 if (objc_method_decl == nullptr)
7883 return nullptr;
7884
7885 if (num_args > 0)
7886 {
7887 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7888
7889 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7890 {
7891 params.push_back (clang::ParmVarDecl::Create (*ast,
7892 objc_method_decl,
7893 clang::SourceLocation(),
7894 clang::SourceLocation(),
7895 nullptr, // anonymous
7896 method_function_prototype->getParamType(param_index),
7897 nullptr,
7898 clang::SC_Auto,
7899 nullptr));
7900 }
7901
7902 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7903 }
7904
7905 class_interface_decl->addDecl (objc_method_decl);
7906
7907#ifdef LLDB_CONFIGURATION_DEBUG
7908 VerifyDecl(objc_method_decl);
7909#endif
7910
7911 return objc_method_decl;
7912}
7913
7914bool
7915ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7916{
7917 if (!type)
7918 return false;
7919
7920 clang::QualType qual_type (GetCanonicalQualType(type));
7921
7922 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7923 switch (type_class)
7924 {
7925 case clang::Type::Record:
7926 {
7927 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7928 if (cxx_record_decl)
7929 {
7930 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7931 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7932 return true;
7933 }
7934 }
7935 break;
7936
7937 case clang::Type::Enum:
7938 {
7939 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7940 if (enum_decl)
7941 {
7942 enum_decl->setHasExternalLexicalStorage (has_extern);
7943 enum_decl->setHasExternalVisibleStorage (has_extern);
7944 return true;
7945 }
7946 }
7947 break;
7948
7949 case clang::Type::ObjCObject:
7950 case clang::Type::ObjCInterface:
7951 {
7952 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7953 assert (objc_class_type);
7954 if (objc_class_type)
7955 {
7956 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7957
7958 if (class_interface_decl)
7959 {
7960 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7961 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7962 return true;
7963 }
7964 }
7965 }
7966 break;
7967
7968 case clang::Type::Typedef:
7969 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7970
7971 case clang::Type::Elaborated:
7972 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7973
7974 case clang::Type::Paren:
7975 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7976
7977 default:
7978 break;
7979 }
7980 return false;
7981}
7982
7983
7984#pragma mark TagDecl
7985
7986bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007987ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007988{
7989 if (type)
7990 {
7991
7992 clang::QualType qual_type (GetQualType(type));
7993 const clang::Type *t = qual_type.getTypePtr();
7994 if (t)
7995 {
7996 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7997 if (tag_type)
7998 {
7999 clang::TagDecl *tag_decl = tag_type->getDecl();
8000 if (tag_decl)
8001 {
8002 tag_decl->startDefinition();
8003 return true;
8004 }
8005 }
8006
8007 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
8008 if (object_type)
8009 {
8010 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8011 if (interface_decl)
8012 {
8013 interface_decl->startDefinition();
8014 return true;
8015 }
8016 }
8017 }
8018 }
8019 return false;
8020}
8021
8022bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008023ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008024{
8025 if (type)
8026 {
8027 clang::QualType qual_type (GetQualType(type));
8028 if (qual_type.isNull())
8029 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00008030 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8031 if (lldb_ast == nullptr)
8032 return false;
8033 clang::ASTContext *ast = lldb_ast->getASTContext();
8034
Greg Claytond8d4a572015-08-11 21:38:15 +00008035 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8036
8037 if (cxx_record_decl)
8038 {
8039 cxx_record_decl->completeDefinition();
8040
8041 return true;
8042 }
8043
8044 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
8045
8046 if (enutype)
8047 {
8048 clang::EnumDecl *enum_decl = enutype->getDecl();
8049
8050 if (enum_decl)
8051 {
8052 /// TODO This really needs to be fixed.
8053
8054 unsigned NumPositiveBits = 1;
8055 unsigned NumNegativeBits = 0;
8056
8057 clang::QualType promotion_qual_type;
8058 // If the enum integer type is less than an integer in bit width,
8059 // then we must promote it to an integer size.
8060 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8061 {
8062 if (enum_decl->getIntegerType()->isSignedIntegerType())
8063 promotion_qual_type = ast->IntTy;
8064 else
8065 promotion_qual_type = ast->UnsignedIntTy;
8066 }
8067 else
8068 promotion_qual_type = enum_decl->getIntegerType();
8069
8070 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
8071 return true;
8072 }
8073 }
8074 }
8075 return false;
8076}
8077
Greg Claytond8d4a572015-08-11 21:38:15 +00008078bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00008079ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
8080 const CompilerType &enumerator_clang_type,
8081 const Declaration &decl,
8082 const char *name,
8083 int64_t enum_value,
8084 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008085{
8086 if (type && enumerator_clang_type.IsValid() && name && name[0])
8087 {
8088 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8089
8090 bool is_signed = false;
8091 enumerator_clang_type.IsIntegerType (is_signed);
8092 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8093 if (clang_type)
8094 {
8095 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8096
8097 if (enutype)
8098 {
8099 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8100 enum_llvm_apsint = enum_value;
8101 clang::EnumConstantDecl *enumerator_decl =
8102 clang::EnumConstantDecl::Create (*getASTContext(),
8103 enutype->getDecl(),
8104 clang::SourceLocation(),
8105 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8106 GetQualType(enumerator_clang_type),
8107 nullptr,
8108 enum_llvm_apsint);
8109
8110 if (enumerator_decl)
8111 {
8112 enutype->getDecl()->addDecl(enumerator_decl);
8113
8114#ifdef LLDB_CONFIGURATION_DEBUG
8115 VerifyDecl(enumerator_decl);
8116#endif
8117
8118 return true;
8119 }
8120 }
8121 }
8122 }
8123 return false;
8124}
8125
Greg Claytona1e5dc82015-08-11 22:53:00 +00008126CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008127ClangASTContext::GetEnumerationIntegerType (void* type)
8128{
8129 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8130 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8131 if (clang_type)
8132 {
8133 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8134 if (enutype)
8135 {
8136 clang::EnumDecl *enum_decl = enutype->getDecl();
8137 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008138 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008139 }
8140 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008141 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008142}
8143
Greg Claytona1e5dc82015-08-11 22:53:00 +00008144CompilerType
8145ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008146{
8147 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8148 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008149 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008150 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008151 return CompilerType();
8152 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008153 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8154 GetQualType(type).getTypePtr()));
8155 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008156 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008157}
8158
8159
8160size_t
8161ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8162{
8163 if (type)
8164 {
8165 clang::QualType qual_type (GetCanonicalQualType(type));
8166 uint32_t count = 0;
8167 bool is_complex = false;
8168 if (IsFloatingPointType (type, count, is_complex))
8169 {
8170 // TODO: handle complex and vector types
8171 if (count != 1)
8172 return false;
8173
8174 llvm::StringRef s_sref(s);
8175 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8176
8177 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8178 const uint64_t byte_size = bit_size / 8;
8179 if (dst_size >= byte_size)
8180 {
8181 if (bit_size == sizeof(float)*8)
8182 {
8183 float float32 = ap_float.convertToFloat();
8184 ::memcpy (dst, &float32, byte_size);
8185 return byte_size;
8186 }
8187 else if (bit_size >= 64)
8188 {
8189 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8190 ::memcpy (dst, ap_int.getRawData(), byte_size);
8191 return byte_size;
8192 }
8193 }
8194 }
8195 }
8196 return 0;
8197}
8198
8199
8200
8201//----------------------------------------------------------------------
8202// Dumping types
8203//----------------------------------------------------------------------
8204#define DEPTH_INCREMENT 2
8205
8206void
8207ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8208 Stream *s,
8209 lldb::Format format,
8210 const lldb_private::DataExtractor &data,
8211 lldb::offset_t data_byte_offset,
8212 size_t data_byte_size,
8213 uint32_t bitfield_bit_size,
8214 uint32_t bitfield_bit_offset,
8215 bool show_types,
8216 bool show_summary,
8217 bool verbose,
8218 uint32_t depth)
8219{
8220 if (!type)
8221 return;
8222
8223 clang::QualType qual_type(GetQualType(type));
8224 switch (qual_type->getTypeClass())
8225 {
8226 case clang::Type::Record:
8227 if (GetCompleteType(type))
8228 {
8229 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8230 const clang::RecordDecl *record_decl = record_type->getDecl();
8231 assert(record_decl);
8232 uint32_t field_bit_offset = 0;
8233 uint32_t field_byte_offset = 0;
8234 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8235 uint32_t child_idx = 0;
8236
8237 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8238 if (cxx_record_decl)
8239 {
8240 // We might have base classes to print out first
8241 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8242 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8243 base_class != base_class_end;
8244 ++base_class)
8245 {
8246 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8247
8248 // Skip empty base classes
8249 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8250 continue;
8251
8252 if (base_class->isVirtual())
8253 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8254 else
8255 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8256 field_byte_offset = field_bit_offset / 8;
8257 assert (field_bit_offset % 8 == 0);
8258 if (child_idx == 0)
8259 s->PutChar('{');
8260 else
8261 s->PutChar(',');
8262
8263 clang::QualType base_class_qual_type = base_class->getType();
8264 std::string base_class_type_name(base_class_qual_type.getAsString());
8265
8266 // Indent and print the base class type name
8267 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8268
8269 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8270
8271 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008272 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008273 base_clang_type.DumpValue (exe_ctx,
8274 s, // Stream to dump to
8275 base_clang_type.GetFormat(), // The format with which to display the member
8276 data, // Data buffer containing all bytes for this type
8277 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8278 base_class_type_info.Width / 8, // Size of this type in bytes
8279 0, // Bitfield bit size
8280 0, // Bitfield bit offset
8281 show_types, // Boolean indicating if we should show the variable types
8282 show_summary, // Boolean indicating if we should show a summary for the current type
8283 verbose, // Verbose output?
8284 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8285
8286 ++child_idx;
8287 }
8288 }
8289 uint32_t field_idx = 0;
8290 clang::RecordDecl::field_iterator field, field_end;
8291 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8292 {
8293 // Print the starting squiggly bracket (if this is the
8294 // first member) or comma (for member 2 and beyond) for
8295 // the struct/union/class member.
8296 if (child_idx == 0)
8297 s->PutChar('{');
8298 else
8299 s->PutChar(',');
8300
8301 // Indent
8302 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8303
8304 clang::QualType field_type = field->getType();
8305 // Print the member type if requested
8306 // Figure out the type byte size (field_type_info.first) and
8307 // alignment (field_type_info.second) from the AST context.
8308 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8309 assert(field_idx < record_layout.getFieldCount());
8310 // Figure out the field offset within the current struct/union/class type
8311 field_bit_offset = record_layout.getFieldOffset (field_idx);
8312 field_byte_offset = field_bit_offset / 8;
8313 uint32_t field_bitfield_bit_size = 0;
8314 uint32_t field_bitfield_bit_offset = 0;
8315 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8316 field_bitfield_bit_offset = field_bit_offset % 8;
8317
8318 if (show_types)
8319 {
8320 std::string field_type_name(field_type.getAsString());
8321 if (field_bitfield_bit_size > 0)
8322 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8323 else
8324 s->Printf("(%s) ", field_type_name.c_str());
8325 }
8326 // Print the member name and equal sign
8327 s->Printf("%s = ", field->getNameAsString().c_str());
8328
8329
8330 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008331 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008332 field_clang_type.DumpValue (exe_ctx,
8333 s, // Stream to dump to
8334 field_clang_type.GetFormat(), // The format with which to display the member
8335 data, // Data buffer containing all bytes for this type
8336 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8337 field_type_info.Width / 8, // Size of this type in bytes
8338 field_bitfield_bit_size, // Bitfield bit size
8339 field_bitfield_bit_offset, // Bitfield bit offset
8340 show_types, // Boolean indicating if we should show the variable types
8341 show_summary, // Boolean indicating if we should show a summary for the current type
8342 verbose, // Verbose output?
8343 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8344 }
8345
8346 // Indent the trailing squiggly bracket
8347 if (child_idx > 0)
8348 s->Printf("\n%*s}", depth, "");
8349 }
8350 return;
8351
8352 case clang::Type::Enum:
8353 if (GetCompleteType(type))
8354 {
8355 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8356 const clang::EnumDecl *enum_decl = enutype->getDecl();
8357 assert(enum_decl);
8358 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8359 lldb::offset_t offset = data_byte_offset;
8360 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8361 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8362 {
8363 if (enum_pos->getInitVal() == enum_value)
8364 {
8365 s->Printf("%s", enum_pos->getNameAsString().c_str());
8366 return;
8367 }
8368 }
8369 // If we have gotten here we didn't get find the enumerator in the
8370 // enum decl, so just print the integer.
8371 s->Printf("%" PRIi64, enum_value);
8372 }
8373 return;
8374
8375 case clang::Type::ConstantArray:
8376 {
8377 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8378 bool is_array_of_characters = false;
8379 clang::QualType element_qual_type = array->getElementType();
8380
8381 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8382 if (canonical_type)
8383 is_array_of_characters = canonical_type->isCharType();
8384
8385 const uint64_t element_count = array->getSize().getLimitedValue();
8386
8387 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8388
8389 uint32_t element_idx = 0;
8390 uint32_t element_offset = 0;
8391 uint64_t element_byte_size = field_type_info.Width / 8;
8392 uint32_t element_stride = element_byte_size;
8393
8394 if (is_array_of_characters)
8395 {
8396 s->PutChar('"');
8397 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8398 s->PutChar('"');
8399 return;
8400 }
8401 else
8402 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008403 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008404 lldb::Format element_format = element_clang_type.GetFormat();
8405
8406 for (element_idx = 0; element_idx < element_count; ++element_idx)
8407 {
8408 // Print the starting squiggly bracket (if this is the
8409 // first member) or comman (for member 2 and beyong) for
8410 // the struct/union/class member.
8411 if (element_idx == 0)
8412 s->PutChar('{');
8413 else
8414 s->PutChar(',');
8415
8416 // Indent and print the index
8417 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8418
8419 // Figure out the field offset within the current struct/union/class type
8420 element_offset = element_idx * element_stride;
8421
8422 // Dump the value of the member
8423 element_clang_type.DumpValue (exe_ctx,
8424 s, // Stream to dump to
8425 element_format, // The format with which to display the element
8426 data, // Data buffer containing all bytes for this type
8427 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8428 element_byte_size, // Size of this type in bytes
8429 0, // Bitfield bit size
8430 0, // Bitfield bit offset
8431 show_types, // Boolean indicating if we should show the variable types
8432 show_summary, // Boolean indicating if we should show a summary for the current type
8433 verbose, // Verbose output?
8434 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8435 }
8436
8437 // Indent the trailing squiggly bracket
8438 if (element_idx > 0)
8439 s->Printf("\n%*s}", depth, "");
8440 }
8441 }
8442 return;
8443
8444 case clang::Type::Typedef:
8445 {
8446 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8447
Greg Claytona1e5dc82015-08-11 22:53:00 +00008448 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008449 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8450 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8451 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8452
8453 return typedef_clang_type.DumpValue (exe_ctx,
8454 s, // Stream to dump to
8455 typedef_format, // The format with which to display the element
8456 data, // Data buffer containing all bytes for this type
8457 data_byte_offset, // Offset into "data" where to grab value from
8458 typedef_byte_size, // Size of this type in bytes
8459 bitfield_bit_size, // Bitfield bit size
8460 bitfield_bit_offset,// Bitfield bit offset
8461 show_types, // Boolean indicating if we should show the variable types
8462 show_summary, // Boolean indicating if we should show a summary for the current type
8463 verbose, // Verbose output?
8464 depth); // Scope depth for any types that have children
8465 }
8466 break;
8467
8468 case clang::Type::Elaborated:
8469 {
8470 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008471 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008472 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8473 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8474 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8475
8476 return elaborated_clang_type.DumpValue (exe_ctx,
8477 s, // Stream to dump to
8478 elaborated_format, // The format with which to display the element
8479 data, // Data buffer containing all bytes for this type
8480 data_byte_offset, // Offset into "data" where to grab value from
8481 elaborated_byte_size, // Size of this type in bytes
8482 bitfield_bit_size, // Bitfield bit size
8483 bitfield_bit_offset,// Bitfield bit offset
8484 show_types, // Boolean indicating if we should show the variable types
8485 show_summary, // Boolean indicating if we should show a summary for the current type
8486 verbose, // Verbose output?
8487 depth); // Scope depth for any types that have children
8488 }
8489 break;
8490
8491 case clang::Type::Paren:
8492 {
8493 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008494 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008495
8496 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8497 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8498 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8499
8500 return desugar_clang_type.DumpValue (exe_ctx,
8501 s, // Stream to dump to
8502 desugar_format, // The format with which to display the element
8503 data, // Data buffer containing all bytes for this type
8504 data_byte_offset, // Offset into "data" where to grab value from
8505 desugar_byte_size, // Size of this type in bytes
8506 bitfield_bit_size, // Bitfield bit size
8507 bitfield_bit_offset,// Bitfield bit offset
8508 show_types, // Boolean indicating if we should show the variable types
8509 show_summary, // Boolean indicating if we should show a summary for the current type
8510 verbose, // Verbose output?
8511 depth); // Scope depth for any types that have children
8512 }
8513 break;
8514
8515 default:
8516 // We are down to a scalar type that we just need to display.
8517 data.Dump(s,
8518 data_byte_offset,
8519 format,
8520 data_byte_size,
8521 1,
8522 UINT32_MAX,
8523 LLDB_INVALID_ADDRESS,
8524 bitfield_bit_size,
8525 bitfield_bit_offset);
8526
8527 if (show_summary)
8528 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8529 break;
8530 }
8531}
8532
8533
8534
8535
8536bool
8537ClangASTContext::DumpTypeValue (void* type, Stream *s,
8538 lldb::Format format,
8539 const lldb_private::DataExtractor &data,
8540 lldb::offset_t byte_offset,
8541 size_t byte_size,
8542 uint32_t bitfield_bit_size,
8543 uint32_t bitfield_bit_offset,
8544 ExecutionContextScope *exe_scope)
8545{
8546 if (!type)
8547 return false;
8548 if (IsAggregateType(type))
8549 {
8550 return false;
8551 }
8552 else
8553 {
8554 clang::QualType qual_type(GetQualType(type));
8555
8556 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8557 switch (type_class)
8558 {
8559 case clang::Type::Typedef:
8560 {
8561 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008562 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008563 if (format == eFormatDefault)
8564 format = typedef_clang_type.GetFormat();
8565 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8566 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8567
8568 return typedef_clang_type.DumpTypeValue (s,
8569 format, // The format with which to display the element
8570 data, // Data buffer containing all bytes for this type
8571 byte_offset, // Offset into "data" where to grab value from
8572 typedef_byte_size, // Size of this type in bytes
8573 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8574 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8575 exe_scope);
8576 }
8577 break;
8578
8579 case clang::Type::Enum:
8580 // If our format is enum or default, show the enumeration value as
8581 // its enumeration string value, else just display it as requested.
8582 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8583 {
8584 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8585 const clang::EnumDecl *enum_decl = enutype->getDecl();
8586 assert(enum_decl);
8587 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8588 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8589 lldb::offset_t offset = byte_offset;
8590 if (is_signed)
8591 {
8592 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8593 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8594 {
8595 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8596 {
8597 s->PutCString (enum_pos->getNameAsString().c_str());
8598 return true;
8599 }
8600 }
8601 // If we have gotten here we didn't get find the enumerator in the
8602 // enum decl, so just print the integer.
8603 s->Printf("%" PRIi64, enum_svalue);
8604 }
8605 else
8606 {
8607 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8608 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8609 {
8610 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8611 {
8612 s->PutCString (enum_pos->getNameAsString().c_str());
8613 return true;
8614 }
8615 }
8616 // If we have gotten here we didn't get find the enumerator in the
8617 // enum decl, so just print the integer.
8618 s->Printf("%" PRIu64, enum_uvalue);
8619 }
8620 return true;
8621 }
8622 // format was not enum, just fall through and dump the value as requested....
8623
8624 default:
8625 // We are down to a scalar type that we just need to display.
8626 {
8627 uint32_t item_count = 1;
8628 // A few formats, we might need to modify our size and count for depending
8629 // on how we are trying to display the value...
8630 switch (format)
8631 {
8632 default:
8633 case eFormatBoolean:
8634 case eFormatBinary:
8635 case eFormatComplex:
8636 case eFormatCString: // NULL terminated C strings
8637 case eFormatDecimal:
8638 case eFormatEnum:
8639 case eFormatHex:
8640 case eFormatHexUppercase:
8641 case eFormatFloat:
8642 case eFormatOctal:
8643 case eFormatOSType:
8644 case eFormatUnsigned:
8645 case eFormatPointer:
8646 case eFormatVectorOfChar:
8647 case eFormatVectorOfSInt8:
8648 case eFormatVectorOfUInt8:
8649 case eFormatVectorOfSInt16:
8650 case eFormatVectorOfUInt16:
8651 case eFormatVectorOfSInt32:
8652 case eFormatVectorOfUInt32:
8653 case eFormatVectorOfSInt64:
8654 case eFormatVectorOfUInt64:
8655 case eFormatVectorOfFloat32:
8656 case eFormatVectorOfFloat64:
8657 case eFormatVectorOfUInt128:
8658 break;
8659
8660 case eFormatChar:
8661 case eFormatCharPrintable:
8662 case eFormatCharArray:
8663 case eFormatBytes:
8664 case eFormatBytesWithASCII:
8665 item_count = byte_size;
8666 byte_size = 1;
8667 break;
8668
8669 case eFormatUnicode16:
8670 item_count = byte_size / 2;
8671 byte_size = 2;
8672 break;
8673
8674 case eFormatUnicode32:
8675 item_count = byte_size / 4;
8676 byte_size = 4;
8677 break;
8678 }
8679 return data.Dump (s,
8680 byte_offset,
8681 format,
8682 byte_size,
8683 item_count,
8684 UINT32_MAX,
8685 LLDB_INVALID_ADDRESS,
8686 bitfield_bit_size,
8687 bitfield_bit_offset,
8688 exe_scope);
8689 }
8690 break;
8691 }
8692 }
8693 return 0;
8694}
8695
8696
8697
8698void
8699ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8700 Stream *s,
8701 const lldb_private::DataExtractor &data,
8702 lldb::offset_t data_byte_offset,
8703 size_t data_byte_size)
8704{
8705 uint32_t length = 0;
8706 if (IsCStringType (type, length))
8707 {
8708 if (exe_ctx)
8709 {
8710 Process *process = exe_ctx->GetProcessPtr();
8711 if (process)
8712 {
8713 lldb::offset_t offset = data_byte_offset;
8714 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8715 std::vector<uint8_t> buf;
8716 if (length > 0)
8717 buf.resize (length);
8718 else
8719 buf.resize (256);
8720
8721 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8722 buf.back() = '\0';
8723 size_t bytes_read;
8724 size_t total_cstr_len = 0;
8725 Error error;
8726 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8727 {
8728 const size_t len = strlen((const char *)&buf.front());
8729 if (len == 0)
8730 break;
8731 if (total_cstr_len == 0)
8732 s->PutCString (" \"");
8733 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8734 total_cstr_len += len;
8735 if (len < buf.size())
8736 break;
8737 pointer_address += total_cstr_len;
8738 }
8739 if (total_cstr_len > 0)
8740 s->PutChar ('"');
8741 }
8742 }
8743 }
8744}
8745
8746void
8747ClangASTContext::DumpTypeDescription (void* type)
8748{
8749 StreamFile s (stdout, false);
8750 DumpTypeDescription (&s);
8751 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8752 if (metadata)
8753 {
8754 metadata->Dump (&s);
8755 }
8756}
8757
8758void
8759ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8760{
8761 if (type)
8762 {
8763 clang::QualType qual_type(GetQualType(type));
8764
8765 llvm::SmallVector<char, 1024> buf;
8766 llvm::raw_svector_ostream llvm_ostrm (buf);
8767
8768 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8769 switch (type_class)
8770 {
8771 case clang::Type::ObjCObject:
8772 case clang::Type::ObjCInterface:
8773 {
8774 GetCompleteType(type);
8775
8776 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8777 assert (objc_class_type);
8778 if (objc_class_type)
8779 {
8780 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8781 if (class_interface_decl)
8782 {
8783 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8784 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8785 }
8786 }
8787 }
8788 break;
8789
8790 case clang::Type::Typedef:
8791 {
8792 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8793 if (typedef_type)
8794 {
8795 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8796 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8797 if (!clang_typedef_name.empty())
8798 {
8799 s->PutCString ("typedef ");
8800 s->PutCString (clang_typedef_name.c_str());
8801 }
8802 }
8803 }
8804 break;
8805
8806 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008807 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008808 return;
8809
8810 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008811 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008812 return;
8813
8814 case clang::Type::Record:
8815 {
8816 GetCompleteType(type);
8817
8818 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8819 const clang::RecordDecl *record_decl = record_type->getDecl();
8820 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8821
8822 if (cxx_record_decl)
8823 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8824 else
8825 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8826 }
8827 break;
8828
8829 default:
8830 {
8831 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8832 if (tag_type)
8833 {
8834 clang::TagDecl *tag_decl = tag_type->getDecl();
8835 if (tag_decl)
8836 tag_decl->print(llvm_ostrm, 0);
8837 }
8838 else
8839 {
8840 std::string clang_type_name(qual_type.getAsString());
8841 if (!clang_type_name.empty())
8842 s->PutCString (clang_type_name.c_str());
8843 }
8844 }
8845 }
8846
Greg Claytond8d4a572015-08-11 21:38:15 +00008847 if (buf.size() > 0)
8848 {
8849 s->Write (buf.data(), buf.size());
8850 }
8851 }
8852}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008853
Greg Clayton8b4edba2015-08-14 20:02:05 +00008854clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00008855ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008856 lldb::AccessType access_type,
8857 const char *parent_name,
8858 int tag_decl_kind,
8859 const ClangASTContext::TemplateParameterInfos &template_param_infos)
8860{
8861 if (template_param_infos.IsValid())
8862 {
8863 std::string template_basename(parent_name);
8864 template_basename.erase (template_basename.find('<'));
8865
8866 return CreateClassTemplateDecl (decl_ctx,
8867 access_type,
8868 template_basename.c_str(),
8869 tag_decl_kind,
8870 template_param_infos);
8871 }
8872 return NULL;
8873}
8874
Greg Clayton6dc8d582015-08-18 22:32:36 +00008875void
8876ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
8877{
8878 ClangASTContext *ast = (ClangASTContext *)baton;
8879 SymbolFile *sym_file = ast->GetSymbolFile();
8880 if (sym_file)
8881 {
8882 CompilerType clang_type = GetTypeForDecl (decl);
8883 if (clang_type)
8884 sym_file->CompleteType (clang_type);
8885 }
8886}
8887
8888void
8889ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
8890{
8891 ClangASTContext *ast = (ClangASTContext *)baton;
8892 SymbolFile *sym_file = ast->GetSymbolFile();
8893 if (sym_file)
8894 {
8895 CompilerType clang_type = GetTypeForDecl (decl);
8896 if (clang_type)
8897 sym_file->CompleteType (clang_type);
8898 }
8899}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008900
Greg Clayton261ac3f2015-08-28 01:01:03 +00008901
8902DWARFASTParser *
8903ClangASTContext::GetDWARFParser ()
8904{
8905 if (!m_dwarf_ast_parser_ap)
8906 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
8907 return m_dwarf_ast_parser_ap.get();
8908}
8909
8910
Greg Clayton8b4edba2015-08-14 20:02:05 +00008911bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00008912ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008913 const clang::RecordDecl *record_decl,
8914 uint64_t &bit_size,
8915 uint64_t &alignment,
8916 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
8917 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
8918 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
8919{
Greg Clayton6dc8d582015-08-18 22:32:36 +00008920 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00008921 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
8922 return dwarf_ast_parser->LayoutRecordType(record_decl, bit_size, alignment, field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00008923}
8924
Greg Clayton99558cc42015-08-24 23:46:31 +00008925//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00008926// CompilerDecl override functions
8927//----------------------------------------------------------------------
8928lldb::VariableSP
8929ClangASTContext::DeclGetVariable (void *opaque_decl)
8930{
8931 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
8932 {
8933 auto decl_search_it = m_decl_objects.find(opaque_decl);
8934 if (decl_search_it != m_decl_objects.end())
8935 return std::static_pointer_cast<Variable>(decl_search_it->second);
8936 }
8937 return VariableSP();
8938}
8939
8940void
8941ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
8942{
8943 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
8944 m_decl_objects.insert(std::make_pair(opaque_decl, object));
8945}
8946
8947ConstString
8948ClangASTContext::DeclGetName (void *opaque_decl)
8949{
8950 if (opaque_decl)
8951 {
8952 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
8953 if (nd != nullptr)
8954 return ConstString(nd->getName());
8955 }
8956 return ConstString();
8957}
8958
8959//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00008960// CompilerDeclContext functions
8961//----------------------------------------------------------------------
8962
Paul Hermand628cbb2015-09-15 23:44:17 +00008963std::vector<void *>
8964ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name)
8965{
8966 std::vector<void *> found_decls;
8967 if (opaque_decl_ctx)
8968 {
8969 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
8970 std::set<DeclContext *> searched;
8971 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00008972 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00008973
8974 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
8975 {
8976 search_queue.insert(std::make_pair(decl_context, decl_context));
8977
8978 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
8979 {
8980 searched.insert(it->second);
Paul Hermanea188fc2015-09-16 18:48:30 +00008981 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
8982
Paul Hermand628cbb2015-09-15 23:44:17 +00008983 for (clang::Decl *child : it->second->decls())
8984 {
Paul Hermanea188fc2015-09-16 18:48:30 +00008985 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00008986 {
8987 clang::DeclContext *from = ud->getCommonAncestor();
8988 if (searched.find(ud->getNominatedNamespace()) == searched.end())
8989 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
8990 }
8991 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
8992 {
8993 for (clang::UsingShadowDecl *usd : ud->shadows())
8994 {
8995 clang::Decl *target = usd->getTargetDecl();
8996 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
8997 {
8998 IdentifierInfo *ii = nd->getIdentifier();
8999 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9000 found_decls.push_back(nd);
9001 }
9002 }
9003 }
Paul Hermanea188fc2015-09-16 18:48:30 +00009004 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
9005 {
9006 IdentifierInfo *ii = nd->getIdentifier();
9007 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9008 found_decls.push_back(nd);
9009 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009010 }
9011 }
9012 }
9013 }
9014 return found_decls;
9015}
9016
Greg Clayton99558cc42015-08-24 23:46:31 +00009017bool
9018ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009019{
Greg Clayton99558cc42015-08-24 23:46:31 +00009020 if (opaque_decl_ctx)
9021 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9022 else
9023 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009024}
9025
Greg Clayton99558cc42015-08-24 23:46:31 +00009026ConstString
9027ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009028{
Greg Clayton99558cc42015-08-24 23:46:31 +00009029 if (opaque_decl_ctx)
9030 {
9031 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9032 if (named_decl)
9033 return ConstString(named_decl->getName());
9034 }
9035 return ConstString();
9036}
9037
9038bool
9039ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9040 lldb::LanguageType *language_ptr,
9041 bool *is_instance_method_ptr,
9042 ConstString *language_object_name_ptr)
9043{
9044 if (opaque_decl_ctx)
9045 {
9046 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9047 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9048 {
9049 if (is_instance_method_ptr)
9050 *is_instance_method_ptr = objc_method->isInstanceMethod();
9051 if (language_ptr)
9052 *language_ptr = eLanguageTypeObjC;
9053 if (language_object_name_ptr)
9054 language_object_name_ptr->SetCString("self");
9055 return true;
9056 }
9057 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9058 {
9059 if (is_instance_method_ptr)
9060 *is_instance_method_ptr = cxx_method->isInstance();
9061 if (language_ptr)
9062 *language_ptr = eLanguageTypeC_plus_plus;
9063 if (language_object_name_ptr)
9064 language_object_name_ptr->SetCString("this");
9065 return true;
9066 }
9067 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9068 {
9069 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9070 if (metadata && metadata->HasObjectPtr())
9071 {
9072 if (is_instance_method_ptr)
9073 *is_instance_method_ptr = true;
9074 if (language_ptr)
9075 *language_ptr = eLanguageTypeObjC;
9076 if (language_object_name_ptr)
9077 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9078 return true;
9079 }
9080 }
9081 }
9082 return false;
9083}
9084
9085clang::DeclContext *
9086ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9087{
9088 if (dc.IsClang())
9089 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9090 return nullptr;
9091}
9092
9093
9094ObjCMethodDecl *
9095ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
9096{
9097 if (dc.IsClang())
9098 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9099 return nullptr;
9100}
9101
9102CXXMethodDecl *
9103ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9104{
9105 if (dc.IsClang())
9106 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9107 return nullptr;
9108}
9109
9110clang::FunctionDecl *
9111ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9112{
9113 if (dc.IsClang())
9114 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9115 return nullptr;
9116}
9117
9118clang::NamespaceDecl *
9119ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9120{
9121 if (dc.IsClang())
9122 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9123 return nullptr;
9124}
9125
9126ClangASTMetadata *
9127ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9128{
9129 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9130 if (ast)
9131 return ClangASTContext::GetMetadata (ast, object);
9132 return nullptr;
9133}
9134
9135clang::ASTContext *
9136ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9137{
Greg Claytonf73034f2015-09-08 18:15:05 +00009138 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9139 if (ast)
9140 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009141 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009142}
9143
Jim Ingham151c0322015-09-15 21:13:50 +00009144ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9145 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
9146 m_target_wp(target.shared_from_this())
9147{
9148}
9149
9150UserExpression *
9151ClangASTContextForExpressions::GetUserExpression (const char *expr,
9152 const char *expr_prefix,
9153 lldb::LanguageType language,
9154 Expression::ResultType desired_type)
9155{
9156 TargetSP target_sp = m_target_wp.lock();
9157 if (!target_sp)
9158 return nullptr;
9159
9160 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type);
9161}
9162
9163FunctionCaller *
9164ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9165 const Address& function_address,
9166 const ValueList &arg_value_list,
9167 const char *name)
9168{
9169 TargetSP target_sp = m_target_wp.lock();
9170 if (!target_sp)
9171 return nullptr;
9172
9173 Process *process = target_sp->GetProcessSP().get();
9174 if (!process)
9175 return nullptr;
9176
9177 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9178}
9179
9180UtilityFunction *
9181ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9182 const char *name)
9183{
9184 TargetSP target_sp = m_target_wp.lock();
9185 if (!target_sp)
9186 return nullptr;
9187
9188 return new ClangUtilityFunction(*target_sp.get(), text, name);
9189}