blob: 712c5dd3aacb4afbaad3bff8a0a7015e0fa190d7 [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 Claytonf0705c82011-10-22 03:33:13 +000067#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000068#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000069#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000070#include "lldb/Core/UniqueCStringMap.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000071#include "lldb/Expression/ASTDumper.h"
Jim Ingham151c0322015-09-15 21:13:50 +000072#include "lldb/Expression/ASTResultSynthesizer.h"
73#include "lldb/Expression/ClangExpressionDeclMap.h"
74#include "lldb/Expression/ClangUserExpression.h"
75#include "lldb/Expression/ClangFunctionCaller.h"
76#include "lldb/Expression/ClangUtilityFunction.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000077#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000078#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000079#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000080#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000081#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000082#include "lldb/Target/ExecutionContext.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000083#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000084#include "lldb/Target/Process.h"
85#include "lldb/Target/Target.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000086
Greg Clayton261ac3f2015-08-28 01:01:03 +000087#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
88
Eli Friedman932197d2010-06-13 19:06:42 +000089#include <stdio.h>
90
Greg Clayton1341baf2013-07-11 23:36:31 +000091#include <mutex>
92
Greg Claytonc86103d2010-08-05 01:57:25 +000093using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094using namespace lldb_private;
95using namespace llvm;
96using namespace clang;
97
Enrico Granata2267ad42014-09-16 17:28:40 +000098typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +000099
100static ClangASTMap &
101GetASTMap()
102{
Enrico Granata2267ad42014-09-16 17:28:40 +0000103 static ClangASTMap *g_map_ptr = nullptr;
104 static std::once_flag g_once_flag;
105 std::call_once(g_once_flag, []() {
106 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
107 });
108 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000109}
110
111
Greg Clayton57ee3062013-07-11 22:46:58 +0000112clang::AccessSpecifier
113ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000114{
115 switch (access)
116 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000117 default: break;
118 case eAccessNone: return AS_none;
119 case eAccessPublic: return AS_public;
120 case eAccessPrivate: return AS_private;
121 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000122 }
123 return AS_none;
124}
125
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000127ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128{
129 // FIXME: Cleanup per-file based stuff.
130
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000131 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132 // to move these to the language standard, and have the driver resolve the
133 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000134 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000136 } else if (IK == IK_ObjC ||
137 IK == IK_ObjCXX ||
138 IK == IK_PreprocessedObjC ||
139 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 Opts.ObjC1 = Opts.ObjC2 = 1;
141 }
142
143 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
144
145 if (LangStd == LangStandard::lang_unspecified) {
146 // Based on the base language, pick one.
147 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000148 case IK_None:
149 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000150 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000151 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000152 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 LangStd = LangStandard::lang_opencl;
154 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000155 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000156 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000157 LangStd = LangStandard::lang_cuda;
158 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000159 case IK_Asm:
160 case IK_C:
161 case IK_PreprocessedC:
162 case IK_ObjC:
163 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 LangStd = LangStandard::lang_gnu99;
165 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000166 case IK_CXX:
167 case IK_PreprocessedCXX:
168 case IK_ObjCXX:
169 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 LangStd = LangStandard::lang_gnucxx98;
171 break;
172 }
173 }
174
175 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000176 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 Opts.C99 = Std.isC99();
178 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000179 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 Opts.Digraphs = Std.hasDigraphs();
181 Opts.GNUMode = Std.isGNUMode();
182 Opts.GNUInline = !Std.isC99();
183 Opts.HexFloats = Std.hasHexFloats();
184 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000185
186 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187
188 // OpenCL has some additional defaults.
189 if (LangStd == LangStandard::lang_opencl) {
190 Opts.OpenCL = 1;
191 Opts.AltiVec = 1;
192 Opts.CXXOperatorNames = 1;
193 Opts.LaxVectorConversions = 1;
194 }
195
196 // OpenCL and C++ both have bool, true, false keywords.
197 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
198
199// if (Opts.CPlusPlus)
200// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
201//
202// if (Args.hasArg(OPT_fobjc_gc_only))
203// Opts.setGCMode(LangOptions::GCOnly);
204// else if (Args.hasArg(OPT_fobjc_gc))
205// Opts.setGCMode(LangOptions::HybridGC);
206//
207// if (Args.hasArg(OPT_print_ivar_layout))
208// Opts.ObjCGCBitmapPrint = 1;
209//
210// if (Args.hasArg(OPT_faltivec))
211// Opts.AltiVec = 1;
212//
213// if (Args.hasArg(OPT_pthread))
214// Opts.POSIXThreads = 1;
215//
216// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
217// "default");
218// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000219 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220// else if (Vis == "hidden")
221// Opts.setVisibilityMode(LangOptions::Hidden);
222// else if (Vis == "protected")
223// Opts.setVisibilityMode(LangOptions::Protected);
224// else
225// Diags.Report(diag::err_drv_invalid_value)
226// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
227
228// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
229
230 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
231 // is specified, or -std is set to a conforming mode.
232 Opts.Trigraphs = !Opts.GNUMode;
233// if (Args.hasArg(OPT_trigraphs))
234// Opts.Trigraphs = 1;
235//
236// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
237// OPT_fno_dollars_in_identifiers,
238// !Opts.AsmPreprocessor);
239// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
240// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
241// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
242// if (Args.hasArg(OPT_fno_lax_vector_conversions))
243// Opts.LaxVectorConversions = 0;
244// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
245// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
246// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000247 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
249// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
250// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
251// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
252// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
253// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
254// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
255// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
256// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
257// Diags);
258// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
259// Opts.ObjCConstantStringClass = getLastArgValue(Args,
260// OPT_fconstant_string_class);
261// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
262// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
263// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
264// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
265// Opts.Static = Args.hasArg(OPT_static_define);
266 Opts.OptimizeSize = 0;
267
268 // FIXME: Eliminate this dependency.
269// unsigned Opt =
270// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
271// Opts.Optimize = Opt != 0;
272 unsigned Opt = 0;
273
274 // This is the __NO_INLINE__ define, which just depends on things like the
275 // optimization level and -fno-inline, not actually whether the backend has
276 // inlining enabled.
277 //
278 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000279 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280
281// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
282// switch (SSP) {
283// default:
284// Diags.Report(diag::err_drv_invalid_value)
285// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
286// break;
287// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
288// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
289// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
290// }
291}
292
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293
Greg Claytonf73034f2015-09-08 18:15:05 +0000294ClangASTContext::ClangASTContext (const char *target_triple) :
295 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000296 m_target_triple (),
297 m_ast_ap (),
298 m_language_options_ap (),
299 m_source_manager_ap (),
300 m_diagnostics_engine_ap (),
301 m_target_options_rp (),
302 m_target_info_ap (),
303 m_identifier_table_ap (),
304 m_selector_table_ap (),
305 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000306 m_callback_tag_decl (nullptr),
307 m_callback_objc_decl (nullptr),
308 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000309 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000310 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311{
312 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000313 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314}
315
316//----------------------------------------------------------------------
317// Destructor
318//----------------------------------------------------------------------
319ClangASTContext::~ClangASTContext()
320{
Enrico Granata5d84a692014-08-19 21:46:37 +0000321 if (m_ast_ap.get())
322 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000323 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000324 if (!m_ast_owned)
325 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000326 }
327
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 m_builtins_ap.reset();
329 m_selector_table_ap.reset();
330 m_identifier_table_ap.reset();
331 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000332 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000333 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 m_source_manager_ap.reset();
335 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000336 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
339
340void
341ClangASTContext::Clear()
342{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000343 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 m_language_options_ap.reset();
345 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000346 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000347 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 m_target_info_ap.reset();
349 m_identifier_table_ap.reset();
350 m_selector_table_ap.reset();
351 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000352 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353}
354
355const char *
356ClangASTContext::GetTargetTriple ()
357{
358 return m_target_triple.c_str();
359}
360
361void
362ClangASTContext::SetTargetTriple (const char *target_triple)
363{
364 Clear();
365 m_target_triple.assign(target_triple);
366}
367
Greg Clayton514487e2011-02-15 21:59:32 +0000368void
369ClangASTContext::SetArchitecture (const ArchSpec &arch)
370{
Greg Clayton880cbb02011-07-30 01:26:02 +0000371 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000372}
373
Greg Clayton6beaaa62011-01-17 03:46:26 +0000374bool
375ClangASTContext::HasExternalSource ()
376{
377 ASTContext *ast = getASTContext();
378 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000379 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000380 return false;
381}
382
383void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000384ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000385{
386 ASTContext *ast = getASTContext();
387 if (ast)
388 {
389 ast->setExternalSource (ast_source_ap);
390 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
391 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
392 }
393}
394
395void
396ClangASTContext::RemoveExternalSource ()
397{
398 ASTContext *ast = getASTContext();
399
400 if (ast)
401 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000402 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000403 ast->setExternalSource (empty_ast_source_ap);
404 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
405 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
406 }
407}
408
Greg Claytond8d4a572015-08-11 21:38:15 +0000409void
410ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
411{
412 if (!m_ast_owned) {
413 m_ast_ap.release();
414 }
415 m_ast_owned = false;
416 m_ast_ap.reset(ast_ctx);
417 GetASTMap().Insert(ast_ctx, this);
418}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419
420ASTContext *
421ClangASTContext::getASTContext()
422{
Ed Masted4612ad2014-04-20 13:17:36 +0000423 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000425 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000426 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
427 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000428 *getIdentifierTable(),
429 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000430 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000431
432 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000433
434 // This can be NULL if we don't know anything about the architecture or if the
435 // target for an architecture isn't enabled in the llvm/clang that we built
436 TargetInfo *target_info = getTargetInfo();
437 if (target_info)
438 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000439
Greg Clayton6beaaa62011-01-17 03:46:26 +0000440 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
441 {
442 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
443 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
444 }
445
Enrico Granata2267ad42014-09-16 17:28:40 +0000446 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000447
448 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
449 ClangASTContext::CompleteObjCInterfaceDecl,
450 nullptr,
451 ClangASTContext::LayoutRecordType,
452 this));
453 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000455 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
Enrico Granata5d84a692014-08-19 21:46:37 +0000458ClangASTContext*
459ClangASTContext::GetASTContext (clang::ASTContext* ast)
460{
Enrico Granata2267ad42014-09-16 17:28:40 +0000461 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000462 return clang_ast;
463}
464
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465Builtin::Context *
466ClangASTContext::getBuiltinContext()
467{
Ed Masted4612ad2014-04-20 13:17:36 +0000468 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000469 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 return m_builtins_ap.get();
471}
472
473IdentifierTable *
474ClangASTContext::getIdentifierTable()
475{
Ed Masted4612ad2014-04-20 13:17:36 +0000476 if (m_identifier_table_ap.get() == nullptr)
477 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 return m_identifier_table_ap.get();
479}
480
481LangOptions *
482ClangASTContext::getLanguageOptions()
483{
Ed Masted4612ad2014-04-20 13:17:36 +0000484 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 {
486 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000487 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000488// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 }
490 return m_language_options_ap.get();
491}
492
493SelectorTable *
494ClangASTContext::getSelectorTable()
495{
Ed Masted4612ad2014-04-20 13:17:36 +0000496 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 m_selector_table_ap.reset (new SelectorTable());
498 return m_selector_table_ap.get();
499}
500
Sean Callanan79439e82010-11-18 02:56:27 +0000501clang::FileManager *
502ClangASTContext::getFileManager()
503{
Ed Masted4612ad2014-04-20 13:17:36 +0000504 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000505 {
506 clang::FileSystemOptions file_system_options;
507 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
508 }
Sean Callanan79439e82010-11-18 02:56:27 +0000509 return m_file_manager_ap.get();
510}
511
Greg Claytone1a916a2010-07-21 22:12:05 +0000512clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513ClangASTContext::getSourceManager()
514{
Ed Masted4612ad2014-04-20 13:17:36 +0000515 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000516 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 return m_source_manager_ap.get();
518}
519
Sean Callanan880e6802011-10-07 23:18:13 +0000520clang::DiagnosticsEngine *
521ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522{
Ed Masted4612ad2014-04-20 13:17:36 +0000523 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000524 {
525 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000526 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000527 }
Sean Callanan880e6802011-10-07 23:18:13 +0000528 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529}
530
Sean Callanan880e6802011-10-07 23:18:13 +0000531class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000532{
533public:
Sean Callanan880e6802011-10-07 23:18:13 +0000534 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000535 {
536 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
537 }
538
Sean Callanan880e6802011-10-07 23:18:13 +0000539 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000540 {
541 if (m_log)
542 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000543 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000544 info.FormatDiagnostic(diag_str);
545 diag_str.push_back('\0');
546 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
547 }
548 }
Sean Callanan880e6802011-10-07 23:18:13 +0000549
550 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
551 {
552 return new NullDiagnosticConsumer ();
553 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000554private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000555 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000556};
557
Sean Callanan880e6802011-10-07 23:18:13 +0000558DiagnosticConsumer *
559ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000560{
Ed Masted4612ad2014-04-20 13:17:36 +0000561 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000562 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000563
Sean Callanan880e6802011-10-07 23:18:13 +0000564 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000565}
566
Jason Molenda45938b92014-07-08 23:46:39 +0000567std::shared_ptr<TargetOptions> &
568ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000569 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 {
Alp Toker5f838642014-07-06 05:36:57 +0000571 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000572 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000573 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 }
Alp Toker5f838642014-07-06 05:36:57 +0000575 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576}
577
578
579TargetInfo *
580ClangASTContext::getTargetInfo()
581{
Greg Clayton70512312012-05-08 01:45:38 +0000582 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000583 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000584 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 return m_target_info_ap.get();
586}
587
588#pragma mark Basic Types
589
590static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000591QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000593 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594 if (qual_type_bit_size == bit_size)
595 return true;
596 return false;
597}
Greg Claytona1e5dc82015-08-11 22:53:00 +0000598CompilerType
Greg Claytonc86103d2010-08-05 01:57:25 +0000599ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600{
Greg Clayton57ee3062013-07-11 22:46:58 +0000601 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Claytona1e5dc82015-08-11 22:53:00 +0000604CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000605ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000607 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000608 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 switch (encoding)
610 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000611 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000612 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000613 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 break;
615
Greg Claytonc86103d2010-08-05 01:57:25 +0000616 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000617 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000618 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000619 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000620 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000621 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000622 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000623 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000624 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000625 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000626 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000627 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000628 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 break;
630
Greg Claytonc86103d2010-08-05 01:57:25 +0000631 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000632 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000633 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000634 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000635 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000636 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000637 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000638 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000639 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000640 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000641 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000642 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000643 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 break;
645
Greg Claytonc86103d2010-08-05 01:57:25 +0000646 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000647 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000648 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000649 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000650 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000651 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000652 return CompilerType (ast, ast->LongDoubleTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000653 break;
654
Greg Claytonc86103d2010-08-05 01:57:25 +0000655 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000656 // Sanity check that bit_size is a multiple of 8's.
657 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000658 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000659 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 }
661
Greg Claytona1e5dc82015-08-11 22:53:00 +0000662 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663}
664
Greg Clayton57ee3062013-07-11 22:46:58 +0000665
666
667lldb::BasicType
668ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
669{
670 if (name)
671 {
672 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
673 static TypeNameToBasicTypeMap g_type_map;
674 static std::once_flag g_once_flag;
675 std::call_once(g_once_flag, [](){
676 // "void"
677 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
678
679 // "char"
680 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
681 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
682 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
683 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
684 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
685 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
686 // "short"
687 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
688 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
689 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
690 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
691
692 // "int"
693 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
694 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
695 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
696 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
697
698 // "long"
699 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
700 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
701 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
702 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
703
704 // "long long"
705 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
706 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
707 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
708 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
709
710 // "int128"
711 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
712 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
713
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000714 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000715 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
716 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
717 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
718 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
719 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
720 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
721 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
722 g_type_map.Sort();
723 });
724
725 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
726 }
727 return eBasicTypeInvalid;
728}
729
Greg Claytona1e5dc82015-08-11 22:53:00 +0000730CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000731ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
732{
733 if (ast)
734 {
735 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
736 return ClangASTContext::GetBasicType (ast, basic_type);
737 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000738 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000739}
740
741uint32_t
742ClangASTContext::GetPointerByteSize ()
743{
744 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000745 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000746 return m_pointer_byte_size;
747}
748
Greg Claytona1e5dc82015-08-11 22:53:00 +0000749CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000750ClangASTContext::GetBasicType (lldb::BasicType basic_type)
751{
752 return GetBasicType (getASTContext(), basic_type);
753}
754
Greg Claytona1e5dc82015-08-11 22:53:00 +0000755CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000756ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
757{
758 if (ast)
759 {
Ed Masted4612ad2014-04-20 13:17:36 +0000760 clang_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000761
762 switch (basic_type)
763 {
764 case eBasicTypeInvalid:
765 case eBasicTypeOther:
766 break;
767 case eBasicTypeVoid:
768 clang_type = ast->VoidTy.getAsOpaquePtr();
769 break;
770 case eBasicTypeChar:
771 clang_type = ast->CharTy.getAsOpaquePtr();
772 break;
773 case eBasicTypeSignedChar:
774 clang_type = ast->SignedCharTy.getAsOpaquePtr();
775 break;
776 case eBasicTypeUnsignedChar:
777 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
778 break;
779 case eBasicTypeWChar:
780 clang_type = ast->getWCharType().getAsOpaquePtr();
781 break;
782 case eBasicTypeSignedWChar:
783 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
784 break;
785 case eBasicTypeUnsignedWChar:
786 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
787 break;
788 case eBasicTypeChar16:
789 clang_type = ast->Char16Ty.getAsOpaquePtr();
790 break;
791 case eBasicTypeChar32:
792 clang_type = ast->Char32Ty.getAsOpaquePtr();
793 break;
794 case eBasicTypeShort:
795 clang_type = ast->ShortTy.getAsOpaquePtr();
796 break;
797 case eBasicTypeUnsignedShort:
798 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
799 break;
800 case eBasicTypeInt:
801 clang_type = ast->IntTy.getAsOpaquePtr();
802 break;
803 case eBasicTypeUnsignedInt:
804 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
805 break;
806 case eBasicTypeLong:
807 clang_type = ast->LongTy.getAsOpaquePtr();
808 break;
809 case eBasicTypeUnsignedLong:
810 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
811 break;
812 case eBasicTypeLongLong:
813 clang_type = ast->LongLongTy.getAsOpaquePtr();
814 break;
815 case eBasicTypeUnsignedLongLong:
816 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
817 break;
818 case eBasicTypeInt128:
819 clang_type = ast->Int128Ty.getAsOpaquePtr();
820 break;
821 case eBasicTypeUnsignedInt128:
822 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
823 break;
824 case eBasicTypeBool:
825 clang_type = ast->BoolTy.getAsOpaquePtr();
826 break;
827 case eBasicTypeHalf:
828 clang_type = ast->HalfTy.getAsOpaquePtr();
829 break;
830 case eBasicTypeFloat:
831 clang_type = ast->FloatTy.getAsOpaquePtr();
832 break;
833 case eBasicTypeDouble:
834 clang_type = ast->DoubleTy.getAsOpaquePtr();
835 break;
836 case eBasicTypeLongDouble:
837 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
838 break;
839 case eBasicTypeFloatComplex:
840 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
841 break;
842 case eBasicTypeDoubleComplex:
843 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
844 break;
845 case eBasicTypeLongDoubleComplex:
846 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
847 break;
848 case eBasicTypeObjCID:
849 clang_type = ast->getObjCIdType().getAsOpaquePtr();
850 break;
851 case eBasicTypeObjCClass:
852 clang_type = ast->getObjCClassType().getAsOpaquePtr();
853 break;
854 case eBasicTypeObjCSel:
855 clang_type = ast->getObjCSelType().getAsOpaquePtr();
856 break;
857 case eBasicTypeNullPtr:
858 clang_type = ast->NullPtrTy.getAsOpaquePtr();
859 break;
860 }
861
862 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000863 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +0000864 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000865 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000866}
867
868
Greg Claytona1e5dc82015-08-11 22:53:00 +0000869CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
871{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000872 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000873
874#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000875 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000876 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000877 {
878 switch (dw_ate)
879 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000880 default:
881 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000882
Sean Callanan38d4df52012-04-03 01:10:10 +0000883 case DW_ATE_address:
884 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000885 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000886 break;
887
888 case DW_ATE_boolean:
889 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000890 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000891 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000892 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000893 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000894 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000895 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000896 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000897 break;
898
899 case DW_ATE_lo_user:
900 // This has been seen to mean DW_AT_complex_integer
901 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000902 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000903 if (::strstr(type_name, "complex"))
904 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000905 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
906 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000907 }
Greg Clayton605684e2011-10-28 23:06:08 +0000908 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000909 break;
910
911 case DW_ATE_complex_float:
912 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000913 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000914 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000915 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000916 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000917 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000918 else
Greg Clayton605684e2011-10-28 23:06:08 +0000919 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000920 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
921 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +0000922 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000923 break;
924
925 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +0000926 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000927 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000928 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000929 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000930 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000931 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000932 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +0000933 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000934 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000935 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000936 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000937 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000938 return CompilerType (ast, ast->LongDoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000939 break;
940
941 case DW_ATE_signed:
942 if (type_name)
943 {
944 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +0000945 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000946 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000947 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000948 if (streq(type_name, "void") &&
949 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000950 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000951 if (strstr(type_name, "long long") &&
952 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000953 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000954 if (strstr(type_name, "long") &&
955 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000956 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000957 if (strstr(type_name, "short") &&
958 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000959 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000960 if (strstr(type_name, "char"))
961 {
962 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000963 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000964 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000965 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000966 }
967 if (strstr(type_name, "int"))
968 {
969 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000970 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000971 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000972 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000973 }
974 }
975 // We weren't able to match up a type name, just search by size
976 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000977 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000979 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000980 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000981 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000982 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000983 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000984 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000985 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000986 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000987 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +0000988 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000989
Sean Callanan38d4df52012-04-03 01:10:10 +0000990 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000991 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +0000992 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000993 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000994 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000995 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000996 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000997 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000998 break;
999
1000 case DW_ATE_unsigned:
1001 if (type_name)
1002 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001003 if (streq(type_name, "wchar_t"))
1004 {
1005 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1006 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001007 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001008 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001009 }
1010 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001011 if (strstr(type_name, "long long"))
1012 {
1013 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001014 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001015 }
1016 else if (strstr(type_name, "long"))
1017 {
1018 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001019 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001020 }
1021 else if (strstr(type_name, "short"))
1022 {
1023 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001024 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001025 }
1026 else if (strstr(type_name, "char"))
1027 {
1028 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001029 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001030 }
1031 else if (strstr(type_name, "int"))
1032 {
1033 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001034 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001035 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001036 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001037 }
1038 }
1039 // We weren't able to match up a type name, just search by size
1040 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001041 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001042 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001043 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001044 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001045 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001046 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001047 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001048 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001049 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001050 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001051 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001052 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001053
Sean Callanan38d4df52012-04-03 01:10:10 +00001054 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001055 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1056 {
1057 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001058 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001059 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001060 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001061 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001062 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001063 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001064 break;
1065
1066 case DW_ATE_imaginary_float:
1067 break;
1068
1069 case DW_ATE_UTF:
1070 if (type_name)
1071 {
1072 if (streq(type_name, "char16_t"))
1073 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001074 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001075 }
1076 else if (streq(type_name, "char32_t"))
1077 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001078 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001079 }
1080 }
1081 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082 }
1083 }
1084 // This assert should fire for anything that we don't catch above so we know
1085 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001086 if (type_name)
1087 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001088 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 +00001089 }
1090 else
1091 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001092 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 +00001093 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001094 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095}
1096
Greg Claytona1e5dc82015-08-11 22:53:00 +00001097CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001098ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1099{
Greg Clayton57ee3062013-07-11 22:46:58 +00001100 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001101 return CompilerType (ast, ast->UnknownAnyTy);
1102 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001103}
1104
Greg Claytona1e5dc82015-08-11 22:53:00 +00001105CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106ClangASTContext::GetCStringType (bool is_const)
1107{
Greg Clayton57ee3062013-07-11 22:46:58 +00001108 ASTContext *ast = getASTContext();
1109 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110
1111 if (is_const)
1112 char_type.addConst();
1113
Greg Claytona1e5dc82015-08-11 22:53:00 +00001114 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115}
1116
Sean Callanan09ab4b72011-11-30 22:11:59 +00001117clang::DeclContext *
1118ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1119{
1120 return ast->getTranslationUnitDecl();
1121}
1122
Greg Clayton526e5af2010-11-13 03:52:47 +00001123clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001124ClangASTContext::CopyDecl (ASTContext *dst_ast,
1125 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001126 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001127{
Sean Callanan79439e82010-11-18 02:56:27 +00001128 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001129 FileManager file_manager (file_system_options);
1130 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001131 *src_ast, file_manager,
1132 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001133
1134 return importer.Import(source_decl);
1135}
1136
Sean Callanan23a30272010-07-16 00:00:27 +00001137bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001138ClangASTContext::AreTypesSame (CompilerType type1,
1139 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001140 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001141{
Greg Claytonf73034f2015-09-08 18:15:05 +00001142 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1143 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001144 return false;
1145
1146 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001147 return true;
1148
Greg Claytond8d4a572015-08-11 21:38:15 +00001149 QualType type1_qual = GetQualType(type1);
1150 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001151
1152 if (ignore_qualifiers)
1153 {
1154 type1_qual = type1_qual.getUnqualifiedType();
1155 type2_qual = type2_qual.getUnqualifiedType();
1156 }
1157
Greg Claytonf73034f2015-09-08 18:15:05 +00001158 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159}
1160
Greg Claytona1e5dc82015-08-11 22:53:00 +00001161CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001162ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1163{
1164 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1165 return GetTypeForDecl(interface_decl);
1166 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1167 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001168 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001169}
1170
Greg Clayton6beaaa62011-01-17 03:46:26 +00001171
Greg Claytona1e5dc82015-08-11 22:53:00 +00001172CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001173ClangASTContext::GetTypeForDecl (TagDecl *decl)
1174{
1175 // No need to call the getASTContext() accessor (which can create the AST
1176 // if it isn't created yet, because we can't have created a decl in this
1177 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001178 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001179 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001180 return CompilerType (ast, ast->getTagDeclType(decl));
1181 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001182}
1183
Greg Claytona1e5dc82015-08-11 22:53:00 +00001184CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001185ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1186{
1187 // No need to call the getASTContext() accessor (which can create the AST
1188 // if it isn't created yet, because we can't have created a decl in this
1189 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001190 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001191 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001192 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1193 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001194}
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196#pragma mark Structure, Unions, Classes
1197
Greg Claytona1e5dc82015-08-11 22:53:00 +00001198CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001199ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1200 AccessType access_type,
1201 const char *name,
1202 int kind,
1203 LanguageType language,
1204 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001206 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001207 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001208
Ed Masted4612ad2014-04-20 13:17:36 +00001209 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001210 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211
Greg Clayton9e409562010-07-28 02:04:09 +00001212
Greg Claytone1be9962011-08-24 23:50:00 +00001213 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001214 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001215 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001216 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001217 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001218 }
1219
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1221 // we will need to update this code. I was told to currently always use
1222 // the CXXRecordDecl class since we often don't know from debug information
1223 // if something is struct or a class, so we default to always use the more
1224 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001225
1226 bool is_anonymous = (!name) || (!name[0]);
1227
Greg Claytonf0705c82011-10-22 03:33:13 +00001228 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1229 (TagDecl::TagKind)kind,
1230 decl_ctx,
1231 SourceLocation(),
1232 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001233 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001234
1235 if (is_anonymous)
1236 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001237
Greg Claytonc4ffd662013-03-08 01:37:30 +00001238 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001239 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001240 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001241 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001242
Greg Clayton55561e92011-10-26 03:31:36 +00001243 if (access_type != eAccessNone)
1244 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001245
1246 if (decl_ctx)
1247 decl_ctx->addDecl (decl);
1248
Greg Claytona1e5dc82015-08-11 22:53:00 +00001249 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001250 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001251 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001252}
1253
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001254static TemplateParameterList *
1255CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001256 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001257 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1258{
1259 const bool parameter_pack = false;
1260 const bool is_typename = false;
1261 const unsigned depth = 0;
1262 const size_t num_template_params = template_param_infos.GetSize();
1263 for (size_t i=0; i<num_template_params; ++i)
1264 {
1265 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001266
Ed Masted4612ad2014-04-20 13:17:36 +00001267 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001268 if (name && name[0])
1269 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001270 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001271 {
1272 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1273 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1274 SourceLocation(),
1275 SourceLocation(),
1276 depth,
1277 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001278 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001279 template_param_infos.args[i].getIntegralType(),
1280 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001281 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001282
1283 }
1284 else
1285 {
1286 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1287 ast->getTranslationUnitDecl(), // Is this the right decl context?
1288 SourceLocation(),
1289 SourceLocation(),
1290 depth,
1291 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001292 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001293 is_typename,
1294 parameter_pack));
1295 }
1296 }
1297
1298 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1299 SourceLocation(),
1300 SourceLocation(),
1301 &template_param_decls.front(),
1302 template_param_decls.size(),
1303 SourceLocation());
1304 return template_param_list;
1305}
1306
1307clang::FunctionTemplateDecl *
1308ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1309 clang::FunctionDecl *func_decl,
1310 const char *name,
1311 const TemplateParameterInfos &template_param_infos)
1312{
1313// /// \brief Create a function template node.
1314 ASTContext *ast = getASTContext();
1315
1316 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1317
1318 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1319 template_param_infos,
1320 template_param_decls);
1321 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1322 decl_ctx,
1323 func_decl->getLocation(),
1324 func_decl->getDeclName(),
1325 template_param_list,
1326 func_decl);
1327
1328 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1329 i < template_param_decl_count;
1330 ++i)
1331 {
1332 // TODO: verify which decl context we should put template_param_decls into..
1333 template_param_decls[i]->setDeclContext (func_decl);
1334 }
1335
1336 return func_tmpl_decl;
1337}
1338
1339void
1340ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1341 clang::FunctionTemplateDecl *func_tmpl_decl,
1342 const TemplateParameterInfos &infos)
1343{
1344 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1345 infos.args.data(),
1346 infos.args.size());
1347
1348 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1349 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001350 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001351}
1352
1353
Greg Claytonf0705c82011-10-22 03:33:13 +00001354ClassTemplateDecl *
1355ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001356 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001357 const char *class_name,
1358 int kind,
1359 const TemplateParameterInfos &template_param_infos)
1360{
1361 ASTContext *ast = getASTContext();
1362
Ed Masted4612ad2014-04-20 13:17:36 +00001363 ClassTemplateDecl *class_template_decl = nullptr;
1364 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001365 decl_ctx = ast->getTranslationUnitDecl();
1366
1367 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1368 DeclarationName decl_name (&identifier_info);
1369
1370 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001371
1372 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001373 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001374 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001375 if (class_template_decl)
1376 return class_template_decl;
1377 }
1378
1379 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001380
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001381 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1382 template_param_infos,
1383 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001384
1385 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1386 (TagDecl::TagKind)kind,
1387 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1388 SourceLocation(),
1389 SourceLocation(),
1390 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001391
1392 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1393 i < template_param_decl_count;
1394 ++i)
1395 {
1396 template_param_decls[i]->setDeclContext (template_cxx_decl);
1397 }
1398
Sean Callananb5c79622011-11-19 01:35:08 +00001399 // With templated classes, we say that a class is templated with
1400 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001401 //template_cxx_decl->startDefinition();
1402 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001403
Greg Claytonf0705c82011-10-22 03:33:13 +00001404 class_template_decl = ClassTemplateDecl::Create (*ast,
1405 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1406 SourceLocation(),
1407 decl_name,
1408 template_param_list,
1409 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001410 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001411
1412 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001413 {
Greg Clayton55561e92011-10-26 03:31:36 +00001414 if (access_type != eAccessNone)
1415 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001416
1417 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1418 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1419
Greg Claytonf0705c82011-10-22 03:33:13 +00001420 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001421
1422#ifdef LLDB_CONFIGURATION_DEBUG
1423 VerifyDecl(class_template_decl);
1424#endif
1425 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001426
1427 return class_template_decl;
1428}
1429
1430
1431ClassTemplateSpecializationDecl *
1432ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1433 ClassTemplateDecl *class_template_decl,
1434 int kind,
1435 const TemplateParameterInfos &template_param_infos)
1436{
1437 ASTContext *ast = getASTContext();
1438 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1439 (TagDecl::TagKind)kind,
1440 decl_ctx,
1441 SourceLocation(),
1442 SourceLocation(),
1443 class_template_decl,
1444 &template_param_infos.args.front(),
1445 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001446 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001447
Sean Callananfa4fab72013-02-01 06:55:48 +00001448 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1449
Greg Claytonf0705c82011-10-22 03:33:13 +00001450 return class_template_specialization_decl;
1451}
1452
Greg Claytona1e5dc82015-08-11 22:53:00 +00001453CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001454ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1455{
1456 if (class_template_specialization_decl)
1457 {
1458 ASTContext *ast = getASTContext();
1459 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001460 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001461 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001462 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463}
1464
Greg Clayton090d0982011-06-19 03:43:27 +00001465static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001466check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001467{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001468 // Special-case call since it can take any number of operands
1469 if(op_kind == OO_Call)
1470 return true;
1471
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001472 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001473 if (num_params == 0)
1474 return unary;
1475 if (num_params == 1)
1476 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001477 else
Greg Clayton090d0982011-06-19 03:43:27 +00001478 return false;
1479}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001480
Greg Clayton090d0982011-06-19 03:43:27 +00001481bool
1482ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1483{
Sean Callanan5b26f272012-02-04 08:49:35 +00001484 switch (op_kind)
1485 {
1486 default:
1487 break;
1488 // C++ standard allows any number of arguments to new/delete
1489 case OO_New:
1490 case OO_Array_New:
1491 case OO_Delete:
1492 case OO_Array_Delete:
1493 return true;
1494 }
1495
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001496#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 +00001497 switch (op_kind)
1498 {
1499#include "clang/Basic/OperatorKinds.def"
1500 default: break;
1501 }
1502 return false;
1503}
1504
Greg Clayton57ee3062013-07-11 22:46:58 +00001505clang::AccessSpecifier
1506ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001507{
1508 clang::AccessSpecifier ret = lhs;
1509
1510 // Make the access equal to the stricter of the field and the nested field's access
1511 switch (ret)
1512 {
1513 case clang::AS_none:
1514 break;
1515 case clang::AS_private:
1516 break;
1517 case clang::AS_protected:
1518 if (rhs == AS_private)
1519 ret = AS_private;
1520 break;
1521 case clang::AS_public:
1522 ret = rhs;
1523 break;
1524 }
1525
1526 return ret;
1527}
1528
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529bool
1530ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1531{
1532 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1533}
1534
1535bool
1536ClangASTContext::FieldIsBitfield
1537(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001538 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001539 FieldDecl* field,
1540 uint32_t& bitfield_bit_size
1541)
1542{
Ed Masted4612ad2014-04-20 13:17:36 +00001543 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544 return false;
1545
1546 if (field->isBitField())
1547 {
1548 Expr* bit_width_expr = field->getBitWidth();
1549 if (bit_width_expr)
1550 {
1551 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001552 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553 {
1554 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1555 return true;
1556 }
1557 }
1558 }
1559 return false;
1560}
1561
1562bool
1563ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1564{
Ed Masted4612ad2014-04-20 13:17:36 +00001565 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 return false;
1567
1568 if (!record_decl->field_empty())
1569 return true;
1570
1571 // No fields, lets check this is a CXX record and check the base classes
1572 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1573 if (cxx_record_decl)
1574 {
1575 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1576 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1577 base_class != base_class_end;
1578 ++base_class)
1579 {
1580 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1581 if (RecordHasFields(base_class_decl))
1582 return true;
1583 }
1584 }
1585 return false;
1586}
1587
Greg Clayton8cf05932010-07-22 18:30:50 +00001588#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589
Greg Claytona1e5dc82015-08-11 22:53:00 +00001590CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001591ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001592(
1593 const char *name,
1594 DeclContext *decl_ctx,
1595 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001596 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001597 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001598)
1599{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001600 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001601 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001602 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001603 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001604 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001605
Greg Clayton6beaaa62011-01-17 03:46:26 +00001606 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001607 decl_ctx,
1608 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001609 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001610 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001611 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001612 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001613 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001614 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001615
Jim Ingham379397632012-10-27 02:54:13 +00001616 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001617 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001618
Greg Claytona1e5dc82015-08-11 22:53:00 +00001619 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001620}
1621
1622static inline bool
1623BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1624{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001625 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001626}
1627
Greg Clayton57ee3062013-07-11 22:46:58 +00001628uint32_t
1629ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630{
1631 uint32_t num_bases = 0;
1632 if (cxx_record_decl)
1633 {
1634 if (omit_empty_base_classes)
1635 {
1636 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1637 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1638 base_class != base_class_end;
1639 ++base_class)
1640 {
1641 // Skip empty base classes
1642 if (omit_empty_base_classes)
1643 {
1644 if (BaseSpecifierIsEmpty (base_class))
1645 continue;
1646 }
1647 ++num_bases;
1648 }
1649 }
1650 else
1651 num_bases = cxx_record_decl->getNumBases();
1652 }
1653 return num_bases;
1654}
1655
1656
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657#pragma mark Namespace Declarations
1658
1659NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001660ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001661{
Ed Masted4612ad2014-04-20 13:17:36 +00001662 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001663 ASTContext *ast = getASTContext();
1664 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001665 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001666 decl_ctx = translation_unit_decl;
1667
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668 if (name)
1669 {
Greg Clayton030a2042011-10-14 21:34:45 +00001670 IdentifierInfo &identifier_info = ast->Idents.get(name);
1671 DeclarationName decl_name (&identifier_info);
1672 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001673 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001674 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001675 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001676 if (namespace_decl)
1677 return namespace_decl;
1678 }
1679
Sean Callanan5b26f272012-02-04 08:49:35 +00001680 namespace_decl = NamespaceDecl::Create(*ast,
1681 decl_ctx,
1682 false,
1683 SourceLocation(),
1684 SourceLocation(),
1685 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001686 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001687
Greg Clayton9d3d6882011-10-31 23:51:19 +00001688 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001690 else
1691 {
1692 if (decl_ctx == translation_unit_decl)
1693 {
1694 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1695 if (namespace_decl)
1696 return namespace_decl;
1697
Sean Callanan5b26f272012-02-04 08:49:35 +00001698 namespace_decl = NamespaceDecl::Create(*ast,
1699 decl_ctx,
1700 false,
1701 SourceLocation(),
1702 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001703 nullptr,
1704 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001705 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1706 translation_unit_decl->addDecl (namespace_decl);
1707 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1708 }
1709 else
1710 {
1711 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1712 if (parent_namespace_decl)
1713 {
1714 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1715 if (namespace_decl)
1716 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001717 namespace_decl = NamespaceDecl::Create(*ast,
1718 decl_ctx,
1719 false,
1720 SourceLocation(),
1721 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001722 nullptr,
1723 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001724 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1725 parent_namespace_decl->addDecl (namespace_decl);
1726 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1727 }
1728 else
1729 {
1730 // BAD!!!
1731 }
1732 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001733 }
1734#ifdef LLDB_CONFIGURATION_DEBUG
1735 VerifyDecl(namespace_decl);
1736#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001737 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738}
1739
1740
Paul Hermand628cbb2015-09-15 23:44:17 +00001741clang::BlockDecl *
1742ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1743{
1744 if (ctx != nullptr)
1745 {
1746 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1747 ctx->addDecl(decl);
1748 return decl;
1749 }
1750 return nullptr;
1751}
1752
Paul Hermanea188fc2015-09-16 18:48:30 +00001753clang::DeclContext *
1754FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1755{
1756 if (root == nullptr)
1757 return nullptr;
1758
1759 std::set<clang::DeclContext *> path_left;
1760 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1761 path_left.insert(d);
1762
1763 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1764 if (path_left.find(d) != path_left.end())
1765 return d;
1766
1767 return nullptr;
1768}
1769
Paul Hermand628cbb2015-09-15 23:44:17 +00001770clang::UsingDirectiveDecl *
1771ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1772{
1773 if (decl_ctx != nullptr && ns_decl != nullptr)
1774 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001775 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001776 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1777 decl_ctx,
1778 clang::SourceLocation(),
1779 clang::SourceLocation(),
1780 clang::NestedNameSpecifierLoc(),
1781 clang::SourceLocation(),
1782 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001783 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001784 decl_ctx->addDecl(using_decl);
1785 return using_decl;
1786 }
1787 return nullptr;
1788}
1789
1790clang::UsingDecl *
1791ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1792{
1793 if (current_decl_ctx != nullptr && target != nullptr)
1794 {
1795 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1796 current_decl_ctx,
1797 clang::SourceLocation(),
1798 clang::NestedNameSpecifierLoc(),
1799 clang::DeclarationNameInfo(),
1800 false);
1801 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1802 current_decl_ctx,
1803 clang::SourceLocation(),
1804 using_decl,
1805 target);
1806 using_decl->addShadowDecl(shadow_decl);
1807 current_decl_ctx->addDecl(using_decl);
1808 return using_decl;
1809 }
1810 return nullptr;
1811}
1812
1813clang::VarDecl *
1814ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1815{
1816 if (decl_context != nullptr)
1817 {
1818 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1819 decl_context,
1820 clang::SourceLocation(),
1821 clang::SourceLocation(),
1822 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1823 type,
1824 nullptr,
1825 clang::SC_None);
1826 var_decl->setAccess(clang::AS_public);
1827 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001828 return var_decl;
1829 }
1830 return nullptr;
1831}
1832
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833#pragma mark Function Types
1834
1835FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001836ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1837 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001838 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001839 int storage,
1840 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841{
Ed Masted4612ad2014-04-20 13:17:36 +00001842 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001843 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001844 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001845 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001846
Greg Clayton0d551042013-06-28 21:08:47 +00001847
1848 const bool hasWrittenPrototype = true;
1849 const bool isConstexprSpecified = false;
1850
Greg Clayton147e1fa2011-10-14 22:47:18 +00001851 if (name && name[0])
1852 {
1853 func_decl = FunctionDecl::Create (*ast,
1854 decl_ctx,
1855 SourceLocation(),
1856 SourceLocation(),
1857 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001858 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001859 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001860 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001861 is_inline,
1862 hasWrittenPrototype,
1863 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001865 else
1866 {
1867 func_decl = FunctionDecl::Create (*ast,
1868 decl_ctx,
1869 SourceLocation(),
1870 SourceLocation(),
1871 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00001872 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001873 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001874 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001875 is_inline,
1876 hasWrittenPrototype,
1877 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001878 }
1879 if (func_decl)
1880 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001881
1882#ifdef LLDB_CONFIGURATION_DEBUG
1883 VerifyDecl(func_decl);
1884#endif
1885
Greg Clayton147e1fa2011-10-14 22:47:18 +00001886 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887}
1888
Greg Claytona1e5dc82015-08-11 22:53:00 +00001889CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001890ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001891 const CompilerType& result_type,
1892 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001893 unsigned num_args,
1894 bool is_variadic,
1895 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896{
Ed Masted4612ad2014-04-20 13:17:36 +00001897 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898 std::vector<QualType> qual_type_args;
1899 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00001900 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901
1902 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001903 FunctionProtoType::ExtProtoInfo proto_info;
1904 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001905 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001906 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001907 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001908
Greg Claytona1e5dc82015-08-11 22:53:00 +00001909 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001910 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00001911 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912}
1913
1914ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00001915ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001917 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001918 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001919 return ParmVarDecl::Create(*ast,
1920 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001921 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001922 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001923 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00001924 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001925 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001926 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00001927 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001928}
1929
1930void
1931ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
1932{
1933 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00001934 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935}
1936
1937
1938#pragma mark Array Types
1939
Greg Claytona1e5dc82015-08-11 22:53:00 +00001940CompilerType
1941ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00001942 size_t element_count,
1943 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944{
Greg Clayton57ee3062013-07-11 22:46:58 +00001945 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001947 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001948 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00001949
Greg Clayton1c8ef472013-04-05 23:27:21 +00001950 if (is_vector)
1951 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001952 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00001953 }
1954 else
1955 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00001956
1957 llvm::APInt ap_element_count (64, element_count);
1958 if (element_count == 0)
1959 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001960 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001961 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001962 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001963 }
1964 else
1965 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001966 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001967 ap_element_count,
1968 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001969 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001970 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00001971 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001973 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974}
1975
Greg Claytona1e5dc82015-08-11 22:53:00 +00001976CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00001977ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001978 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00001979 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00001980{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001981 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00001982 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
1983 return type;
1984 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00001985 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001986 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00001987 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00001988 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00001989 SetIsPacked(type);
1990 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001991 return type;
1992}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993
1994#pragma mark Enumeration Types
1995
Greg Claytona1e5dc82015-08-11 22:53:00 +00001996CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00001997ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00001998(
Greg Claytond8d4a572015-08-11 21:38:15 +00001999 const char *name,
2000 DeclContext *decl_ctx,
2001 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002002 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002003 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004{
2005 // TODO: Do something intelligent with the Declaration object passed in
2006 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002007 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002008
Greg Claytone02b8502010-10-12 04:29:14 +00002009 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002010 // const bool IsScoped = false;
2011 // const bool IsFixed = false;
2012
Greg Clayton6beaaa62011-01-17 03:46:26 +00002013 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002014 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002015 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002016 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002017 name && name[0] ? &ast->Idents.get(name) : nullptr,
2018 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002019 false, // IsScoped
2020 false, // IsScopedUsingClassTag
2021 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002022
2023
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002025 {
2026 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00002027 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00002028
2029 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2030
Greg Claytona1e5dc82015-08-11 22:53:00 +00002031 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002032 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002033 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002034}
2035
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036// Disable this for now since I can't seem to get a nicely formatted float
2037// out of the APFloat class without just getting the float, double or quad
2038// and then using a formatted print on it which defeats the purpose. We ideally
2039// would like to get perfect string values for any kind of float semantics
2040// so we can support remote targets. The code below also requires a patch to
2041// llvm::APInt.
2042//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00002043//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 +00002044//{
2045// uint32_t count = 0;
2046// bool is_complex = false;
2047// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2048// {
2049// unsigned num_bytes_per_float = byte_size / count;
2050// unsigned num_bits_per_float = num_bytes_per_float * 8;
2051//
2052// float_str.clear();
2053// uint32_t i;
2054// for (i=0; i<count; i++)
2055// {
2056// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2057// bool is_ieee = false;
2058// APFloat ap_float(ap_int, is_ieee);
2059// char s[1024];
2060// unsigned int hex_digits = 0;
2061// bool upper_case = false;
2062//
2063// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2064// {
2065// if (i > 0)
2066// float_str.append(", ");
2067// float_str.append(s);
2068// if (i == 1 && is_complex)
2069// float_str.append(1, 'i');
2070// }
2071// }
2072// return !float_str.empty();
2073// }
2074// return false;
2075//}
2076
Greg Claytona1e5dc82015-08-11 22:53:00 +00002077CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002078ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2079 size_t bit_size, bool is_signed)
2080{
2081 if (ast)
2082 {
2083 if (is_signed)
2084 {
2085 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002086 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002087
2088 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002089 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002090
2091 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002092 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002093
2094 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002095 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002096
2097 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002098 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002099
2100 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002101 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002102 }
2103 else
2104 {
2105 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002106 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002107
2108 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002109 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002110
2111 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002112 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002113
2114 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002115 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002116
2117 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002118 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002119
2120 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002121 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002122 }
2123 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002124 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002125}
2126
Greg Claytona1e5dc82015-08-11 22:53:00 +00002127CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002128ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2129{
2130 if (ast)
2131 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002132 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002133}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134
Greg Claytona1e5dc82015-08-11 22:53:00 +00002135CompilerType
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002136ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2137 size_t bit_size)
2138{
2139 if (ast)
2140 {
2141 if (bit_size == ast->getTypeSize(ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002142 return CompilerType(ast, ast->FloatTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002143 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002144 return CompilerType(ast, ast->DoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002145 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002146 return CompilerType(ast, ast->LongDoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002147 else if (bit_size == ast->getTypeSize(ast->HalfTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002148 return CompilerType(ast, ast->HalfTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002149 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002150 return CompilerType();
Enrico Granata86027e92012-03-24 01:11:14 +00002151}
2152
2153bool
Greg Claytona2721472011-06-25 00:44:06 +00002154ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2155 clang::Decl *decl)
2156{
2157 if (!decl)
2158 return false;
2159
2160 ExternalASTSource *ast_source = ast->getExternalSource();
2161
2162 if (!ast_source)
2163 return false;
2164
2165 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2166 {
Greg Clayton219cf312012-03-30 00:51:13 +00002167 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002168 return true;
2169
2170 if (!tag_decl->hasExternalLexicalStorage())
2171 return false;
2172
2173 ast_source->CompleteType(tag_decl);
2174
2175 return !tag_decl->getTypeForDecl()->isIncompleteType();
2176 }
2177 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2178 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002179 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002180 return true;
2181
2182 if (!objc_interface_decl->hasExternalLexicalStorage())
2183 return false;
2184
2185 ast_source->CompleteType(objc_interface_decl);
2186
Sean Callanan5b26f272012-02-04 08:49:35 +00002187 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002188 }
2189 else
2190 {
2191 return false;
2192 }
2193}
2194
Sean Callanan60217122012-04-13 00:10:03 +00002195void
Greg Claytond0029442013-03-27 01:48:02 +00002196ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002197 user_id_t user_id)
2198{
2199 ClangASTMetadata meta_data;
2200 meta_data.SetUserID (user_id);
2201 SetMetadata (object, meta_data);
2202}
2203
2204void
Sean Callanan60217122012-04-13 00:10:03 +00002205ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002206 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002207 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002208{
2209 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002210 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002211
2212 if (external_source)
2213 external_source->SetMetadata(object, metadata);
2214}
2215
Jim Ingham379397632012-10-27 02:54:13 +00002216ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002217ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002218 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002219{
2220 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002221 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002222
2223 if (external_source && external_source->HasMetadata(object))
2224 return external_source->GetMetadata(object);
2225 else
Ed Masted4612ad2014-04-20 13:17:36 +00002226 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002227}
2228
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002229clang::DeclContext *
2230ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2231{
Sean Callanana87bee82011-08-19 06:19:25 +00002232 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002233}
2234
2235clang::DeclContext *
2236ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2237{
Sean Callanana87bee82011-08-19 06:19:25 +00002238 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002239}
2240
Greg Claytond8d4a572015-08-11 21:38:15 +00002241bool
2242ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2243{
2244 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2245 if (clang_type)
2246 {
2247 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2248 if (tag_type)
2249 {
2250 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2251 if (tag_decl)
2252 {
2253 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2254 return true;
2255 }
2256 }
2257 }
2258 return false;
2259}
2260
2261
2262bool
2263ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2264 int default_accessibility,
2265 int *assigned_accessibilities,
2266 size_t num_assigned_accessibilities)
2267{
2268 if (record_decl)
2269 {
2270 uint32_t field_idx;
2271 clang::RecordDecl::field_iterator field, field_end;
2272 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2273 field != field_end;
2274 ++field, ++field_idx)
2275 {
2276 // If no accessibility was assigned, assign the correct one
2277 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2278 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2279 }
2280 return true;
2281 }
2282 return false;
2283}
2284
2285clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002286ClangASTContext::GetDeclContextForType (const CompilerType& type)
2287{
2288 return GetDeclContextForType(GetQualType(type));
2289}
2290
2291clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002292ClangASTContext::GetDeclContextForType (clang::QualType type)
2293{
2294 if (type.isNull())
2295 return nullptr;
2296
2297 clang::QualType qual_type = type.getCanonicalType();
2298 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2299 switch (type_class)
2300 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002301 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2302 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2303 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2304 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2305 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2306 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2307 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002308 default:
2309 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002310 }
2311 // No DeclContext in this type...
2312 return nullptr;
2313}
2314
2315static bool
2316GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2317{
2318 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2319 switch (type_class)
2320 {
2321 case clang::Type::ConstantArray:
2322 case clang::Type::IncompleteArray:
2323 case clang::Type::VariableArray:
2324 {
2325 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2326
2327 if (array_type)
2328 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2329 }
2330 break;
2331
2332 case clang::Type::Record:
2333 case clang::Type::Enum:
2334 {
2335 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2336 if (tag_type)
2337 {
2338 clang::TagDecl *tag_decl = tag_type->getDecl();
2339 if (tag_decl)
2340 {
2341 if (tag_decl->isCompleteDefinition())
2342 return true;
2343
2344 if (!allow_completion)
2345 return false;
2346
2347 if (tag_decl->hasExternalLexicalStorage())
2348 {
2349 if (ast)
2350 {
2351 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2352 if (external_ast_source)
2353 {
2354 external_ast_source->CompleteType(tag_decl);
2355 return !tag_type->isIncompleteType();
2356 }
2357 }
2358 }
2359 return false;
2360 }
2361 }
2362
2363 }
2364 break;
2365
2366 case clang::Type::ObjCObject:
2367 case clang::Type::ObjCInterface:
2368 {
2369 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2370 if (objc_class_type)
2371 {
2372 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2373 // We currently can't complete objective C types through the newly added ASTContext
2374 // because it only supports TagDecl objects right now...
2375 if (class_interface_decl)
2376 {
2377 if (class_interface_decl->getDefinition())
2378 return true;
2379
2380 if (!allow_completion)
2381 return false;
2382
2383 if (class_interface_decl->hasExternalLexicalStorage())
2384 {
2385 if (ast)
2386 {
2387 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2388 if (external_ast_source)
2389 {
2390 external_ast_source->CompleteType (class_interface_decl);
2391 return !objc_class_type->isIncompleteType();
2392 }
2393 }
2394 }
2395 return false;
2396 }
2397 }
2398 }
2399 break;
2400
2401 case clang::Type::Typedef:
2402 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2403
2404 case clang::Type::Elaborated:
2405 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2406
2407 case clang::Type::Paren:
2408 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2409
2410 default:
2411 break;
2412 }
2413
2414 return true;
2415}
2416
2417static clang::ObjCIvarDecl::AccessControl
2418ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2419{
2420 switch (access)
2421 {
2422 case eAccessNone: return clang::ObjCIvarDecl::None;
2423 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2424 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2425 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2426 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2427 }
2428 return clang::ObjCIvarDecl::None;
2429}
2430
2431
2432//----------------------------------------------------------------------
2433// Tests
2434//----------------------------------------------------------------------
2435
2436bool
2437ClangASTContext::IsAggregateType (void* type)
2438{
2439 clang::QualType qual_type (GetCanonicalQualType(type));
2440
2441 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2442 switch (type_class)
2443 {
2444 case clang::Type::IncompleteArray:
2445 case clang::Type::VariableArray:
2446 case clang::Type::ConstantArray:
2447 case clang::Type::ExtVector:
2448 case clang::Type::Vector:
2449 case clang::Type::Record:
2450 case clang::Type::ObjCObject:
2451 case clang::Type::ObjCInterface:
2452 return true;
2453 case clang::Type::Elaborated:
2454 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2455 case clang::Type::Typedef:
2456 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2457 case clang::Type::Paren:
2458 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2459 default:
2460 break;
2461 }
2462 // The clang type does have a value
2463 return false;
2464}
2465
2466bool
2467ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002468 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002469 uint64_t *size,
2470 bool *is_incomplete)
2471{
2472 clang::QualType qual_type (GetCanonicalQualType(type));
2473
2474 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2475 switch (type_class)
2476 {
2477 default:
2478 break;
2479
2480 case clang::Type::ConstantArray:
2481 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002482 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002483 if (size)
2484 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2485 return true;
2486
2487 case clang::Type::IncompleteArray:
2488 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002489 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002490 if (size)
2491 *size = 0;
2492 if (is_incomplete)
2493 *is_incomplete = true;
2494 return true;
2495
2496 case clang::Type::VariableArray:
2497 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002498 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002499 if (size)
2500 *size = 0;
2501 return true;
2502
2503 case clang::Type::DependentSizedArray:
2504 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002505 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002506 if (size)
2507 *size = 0;
2508 return true;
2509
2510 case clang::Type::Typedef:
2511 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2512 element_type_ptr,
2513 size,
2514 is_incomplete);
2515 case clang::Type::Elaborated:
2516 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2517 element_type_ptr,
2518 size,
2519 is_incomplete);
2520 case clang::Type::Paren:
2521 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2522 element_type_ptr,
2523 size,
2524 is_incomplete);
2525 }
2526 if (element_type_ptr)
2527 element_type_ptr->Clear();
2528 if (size)
2529 *size = 0;
2530 if (is_incomplete)
2531 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002532 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002533}
2534
2535bool
2536ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002537 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002538 uint64_t *size)
2539{
2540 clang::QualType qual_type (GetCanonicalQualType(type));
2541
2542 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2543 switch (type_class)
2544 {
2545 case clang::Type::Vector:
2546 {
2547 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2548 if (vector_type)
2549 {
2550 if (size)
2551 *size = vector_type->getNumElements();
2552 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002553 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002554 }
2555 return true;
2556 }
2557 break;
2558 case clang::Type::ExtVector:
2559 {
2560 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2561 if (ext_vector_type)
2562 {
2563 if (size)
2564 *size = ext_vector_type->getNumElements();
2565 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002566 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002567 }
2568 return true;
2569 }
2570 default:
2571 break;
2572 }
2573 return false;
2574}
2575
2576bool
2577ClangASTContext::IsRuntimeGeneratedType (void* type)
2578{
2579 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2580 if (!decl_ctx)
2581 return false;
2582
2583 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2584 return false;
2585
2586 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2587
2588 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2589 if (!ast_metadata)
2590 return false;
2591 return (ast_metadata->GetISAPtr() != 0);
2592}
2593
2594bool
2595ClangASTContext::IsCharType (void* type)
2596{
2597 return GetQualType(type).getUnqualifiedType()->isCharType();
2598}
2599
2600
2601bool
2602ClangASTContext::IsCompleteType (void* type)
2603{
2604 const bool allow_completion = false;
2605 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2606}
2607
2608bool
2609ClangASTContext::IsConst(void* type)
2610{
2611 return GetQualType(type).isConstQualified();
2612}
2613
2614bool
2615ClangASTContext::IsCStringType (void* type, uint32_t &length)
2616{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002617 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002618 length = 0;
2619 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2620
2621 if (!pointee_or_element_clang_type.IsValid())
2622 return false;
2623
2624 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2625 {
2626 if (pointee_or_element_clang_type.IsCharType())
2627 {
2628 if (type_flags.Test (eTypeIsArray))
2629 {
2630 // We know the size of the array and it could be a C string
2631 // since it is an array of characters
2632 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2633 }
2634 return true;
2635
2636 }
2637 }
2638 return false;
2639}
2640
2641bool
2642ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2643{
2644 if (type)
2645 {
2646 clang::QualType qual_type (GetCanonicalQualType(type));
2647
2648 if (qual_type->isFunctionType())
2649 {
2650 if (is_variadic_ptr)
2651 {
2652 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2653 if (function_proto_type)
2654 *is_variadic_ptr = function_proto_type->isVariadic();
2655 else
2656 *is_variadic_ptr = false;
2657 }
2658 return true;
2659 }
2660
2661 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2662 switch (type_class)
2663 {
2664 default:
2665 break;
2666 case clang::Type::Typedef:
2667 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2668 case clang::Type::Elaborated:
2669 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2670 case clang::Type::Paren:
2671 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2672 case clang::Type::LValueReference:
2673 case clang::Type::RValueReference:
2674 {
2675 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2676 if (reference_type)
2677 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2678 }
2679 break;
2680 }
2681 }
2682 return false;
2683}
2684
2685// Used to detect "Homogeneous Floating-point Aggregates"
2686uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002687ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002688{
2689 if (!type)
2690 return 0;
2691
2692 clang::QualType qual_type(GetCanonicalQualType(type));
2693 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2694 switch (type_class)
2695 {
2696 case clang::Type::Record:
2697 if (GetCompleteType (type))
2698 {
2699 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2700 if (cxx_record_decl)
2701 {
2702 if (cxx_record_decl->getNumBases() ||
2703 cxx_record_decl->isDynamicClass())
2704 return 0;
2705 }
2706 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2707 if (record_type)
2708 {
2709 const clang::RecordDecl *record_decl = record_type->getDecl();
2710 if (record_decl)
2711 {
2712 // We are looking for a structure that contains only floating point types
2713 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2714 uint32_t num_fields = 0;
2715 bool is_hva = false;
2716 bool is_hfa = false;
2717 clang::QualType base_qual_type;
2718 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2719 {
2720 clang::QualType field_qual_type = field_pos->getType();
2721 if (field_qual_type->isFloatingType())
2722 {
2723 if (field_qual_type->isComplexType())
2724 return 0;
2725 else
2726 {
2727 if (num_fields == 0)
2728 base_qual_type = field_qual_type;
2729 else
2730 {
2731 if (is_hva)
2732 return 0;
2733 is_hfa = true;
2734 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2735 return 0;
2736 }
2737 }
2738 }
2739 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2740 {
2741 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2742 if (array && array->getNumElements() <= 4)
2743 {
2744 if (num_fields == 0)
2745 base_qual_type = array->getElementType();
2746 else
2747 {
2748 if (is_hfa)
2749 return 0;
2750 is_hva = true;
2751 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2752 return 0;
2753 }
2754 }
2755 else
2756 return 0;
2757 }
2758 else
2759 return 0;
2760 ++num_fields;
2761 }
2762 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002763 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002764 return num_fields;
2765 }
2766 }
2767 }
2768 break;
2769
2770 case clang::Type::Typedef:
2771 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2772
2773 case clang::Type::Elaborated:
2774 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2775 default:
2776 break;
2777 }
2778 return 0;
2779}
2780
2781size_t
2782ClangASTContext::GetNumberOfFunctionArguments (void* type)
2783{
2784 if (type)
2785 {
2786 clang::QualType qual_type (GetCanonicalQualType(type));
2787 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2788 if (func)
2789 return func->getNumParams();
2790 }
2791 return 0;
2792}
2793
Greg Claytona1e5dc82015-08-11 22:53:00 +00002794CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002795ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2796{
2797 if (type)
2798 {
2799 clang::QualType qual_type (GetCanonicalQualType(type));
2800 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2801 if (func)
2802 {
2803 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002804 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002805 }
2806 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002807 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002808}
2809
2810bool
2811ClangASTContext::IsFunctionPointerType (void* type)
2812{
2813 if (type)
2814 {
2815 clang::QualType qual_type (GetCanonicalQualType(type));
2816
2817 if (qual_type->isFunctionPointerType())
2818 return true;
2819
2820 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2821 switch (type_class)
2822 {
2823 default:
2824 break;
2825 case clang::Type::Typedef:
2826 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2827 case clang::Type::Elaborated:
2828 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2829 case clang::Type::Paren:
2830 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2831
2832 case clang::Type::LValueReference:
2833 case clang::Type::RValueReference:
2834 {
2835 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2836 if (reference_type)
2837 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2838 }
2839 break;
2840 }
2841 }
2842 return false;
2843
2844}
2845
2846bool
2847ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2848{
2849 if (!type)
2850 return false;
2851
2852 clang::QualType qual_type (GetCanonicalQualType(type));
2853 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2854
2855 if (builtin_type)
2856 {
2857 if (builtin_type->isInteger())
2858 {
2859 is_signed = builtin_type->isSignedInteger();
2860 return true;
2861 }
2862 }
2863
2864 return false;
2865}
2866
2867bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002868ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002869{
2870 if (type)
2871 {
2872 clang::QualType qual_type (GetCanonicalQualType(type));
2873 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2874 switch (type_class)
2875 {
2876 case clang::Type::Builtin:
2877 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2878 {
2879 default:
2880 break;
2881 case clang::BuiltinType::ObjCId:
2882 case clang::BuiltinType::ObjCClass:
2883 return true;
2884 }
2885 return false;
2886 case clang::Type::ObjCObjectPointer:
2887 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002888 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002889 return true;
2890 case clang::Type::BlockPointer:
2891 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002892 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002893 return true;
2894 case clang::Type::Pointer:
2895 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002896 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002897 return true;
2898 case clang::Type::MemberPointer:
2899 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002900 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002901 return true;
2902 case clang::Type::Typedef:
2903 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2904 case clang::Type::Elaborated:
2905 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2906 case clang::Type::Paren:
2907 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2908 default:
2909 break;
2910 }
2911 }
2912 if (pointee_type)
2913 pointee_type->Clear();
2914 return false;
2915}
2916
2917
2918bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002919ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002920{
2921 if (type)
2922 {
2923 clang::QualType qual_type (GetCanonicalQualType(type));
2924 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2925 switch (type_class)
2926 {
2927 case clang::Type::Builtin:
2928 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2929 {
2930 default:
2931 break;
2932 case clang::BuiltinType::ObjCId:
2933 case clang::BuiltinType::ObjCClass:
2934 return true;
2935 }
2936 return false;
2937 case clang::Type::ObjCObjectPointer:
2938 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002939 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002940 return true;
2941 case clang::Type::BlockPointer:
2942 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002943 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002944 return true;
2945 case clang::Type::Pointer:
2946 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002947 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002948 return true;
2949 case clang::Type::MemberPointer:
2950 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002951 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002952 return true;
2953 case clang::Type::LValueReference:
2954 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002955 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002956 return true;
2957 case clang::Type::RValueReference:
2958 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002959 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002960 return true;
2961 case clang::Type::Typedef:
2962 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2963 case clang::Type::Elaborated:
2964 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2965 case clang::Type::Paren:
2966 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2967 default:
2968 break;
2969 }
2970 }
2971 if (pointee_type)
2972 pointee_type->Clear();
2973 return false;
2974}
2975
2976
2977bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002978ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00002979{
2980 if (type)
2981 {
2982 clang::QualType qual_type (GetCanonicalQualType(type));
2983 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2984
2985 switch (type_class)
2986 {
2987 case clang::Type::LValueReference:
2988 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002989 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002990 if (is_rvalue)
2991 *is_rvalue = false;
2992 return true;
2993 case clang::Type::RValueReference:
2994 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002995 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002996 if (is_rvalue)
2997 *is_rvalue = true;
2998 return true;
2999 case clang::Type::Typedef:
3000 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3001 case clang::Type::Elaborated:
3002 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3003 case clang::Type::Paren:
3004 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3005
3006 default:
3007 break;
3008 }
3009 }
3010 if (pointee_type)
3011 pointee_type->Clear();
3012 return false;
3013}
3014
3015bool
3016ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
3017{
3018 if (type)
3019 {
3020 clang::QualType qual_type (GetCanonicalQualType(type));
3021
3022 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3023 {
3024 clang::BuiltinType::Kind kind = BT->getKind();
3025 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3026 {
3027 count = 1;
3028 is_complex = false;
3029 return true;
3030 }
3031 }
3032 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3033 {
3034 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3035 {
3036 count = 2;
3037 is_complex = true;
3038 return true;
3039 }
3040 }
3041 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3042 {
3043 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3044 {
3045 count = VT->getNumElements();
3046 is_complex = false;
3047 return true;
3048 }
3049 }
3050 }
3051 count = 0;
3052 is_complex = false;
3053 return false;
3054}
3055
3056
3057bool
3058ClangASTContext::IsDefined(void* type)
3059{
3060 if (!type)
3061 return false;
3062
3063 clang::QualType qual_type(GetQualType(type));
3064 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3065 if (tag_type)
3066 {
3067 clang::TagDecl *tag_decl = tag_type->getDecl();
3068 if (tag_decl)
3069 return tag_decl->isCompleteDefinition();
3070 return false;
3071 }
3072 else
3073 {
3074 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3075 if (objc_class_type)
3076 {
3077 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3078 if (class_interface_decl)
3079 return class_interface_decl->getDefinition() != nullptr;
3080 return false;
3081 }
3082 }
3083 return true;
3084}
3085
3086bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003087ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003088{
3089 if (type)
3090 {
3091 clang::QualType qual_type (GetCanonicalQualType(type));
3092
3093 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3094
3095 if (obj_pointer_type)
3096 return obj_pointer_type->isObjCClassType();
3097 }
3098 return false;
3099}
3100
3101bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003102ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003103{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003104 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003105 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3106 return false;
3107}
3108
3109bool
3110ClangASTContext::IsPolymorphicClass (void* type)
3111{
3112 if (type)
3113 {
3114 clang::QualType qual_type(GetCanonicalQualType(type));
3115 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3116 switch (type_class)
3117 {
3118 case clang::Type::Record:
3119 if (GetCompleteType(type))
3120 {
3121 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3122 const clang::RecordDecl *record_decl = record_type->getDecl();
3123 if (record_decl)
3124 {
3125 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3126 if (cxx_record_decl)
3127 return cxx_record_decl->isPolymorphic();
3128 }
3129 }
3130 break;
3131
3132 default:
3133 break;
3134 }
3135 }
3136 return false;
3137}
3138
3139bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003140ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003141 bool check_cplusplus,
3142 bool check_objc)
3143{
3144 clang::QualType pointee_qual_type;
3145 if (type)
3146 {
3147 clang::QualType qual_type (GetCanonicalQualType(type));
3148 bool success = false;
3149 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3150 switch (type_class)
3151 {
3152 case clang::Type::Builtin:
3153 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3154 {
3155 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003156 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003157 return true;
3158 }
3159 break;
3160
3161 case clang::Type::ObjCObjectPointer:
3162 if (check_objc)
3163 {
3164 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003165 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003166 return true;
3167 }
3168 break;
3169
3170 case clang::Type::Pointer:
3171 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3172 success = true;
3173 break;
3174
3175 case clang::Type::LValueReference:
3176 case clang::Type::RValueReference:
3177 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3178 success = true;
3179 break;
3180
3181 case clang::Type::Typedef:
3182 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3183 dynamic_pointee_type,
3184 check_cplusplus,
3185 check_objc);
3186
3187 case clang::Type::Elaborated:
3188 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3189 dynamic_pointee_type,
3190 check_cplusplus,
3191 check_objc);
3192
3193 case clang::Type::Paren:
3194 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3195 dynamic_pointee_type,
3196 check_cplusplus,
3197 check_objc);
3198 default:
3199 break;
3200 }
3201
3202 if (success)
3203 {
3204 // Check to make sure what we are pointing too is a possible dynamic C++ type
3205 // We currently accept any "void *" (in case we have a class that has been
3206 // watered down to an opaque pointer) and virtual C++ classes.
3207 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3208 switch (pointee_type_class)
3209 {
3210 case clang::Type::Builtin:
3211 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3212 {
3213 case clang::BuiltinType::UnknownAny:
3214 case clang::BuiltinType::Void:
3215 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003216 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003217 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003218 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003219 break;
3220 }
3221 break;
3222
3223 case clang::Type::Record:
3224 if (check_cplusplus)
3225 {
3226 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3227 if (cxx_record_decl)
3228 {
3229 bool is_complete = cxx_record_decl->isCompleteDefinition();
3230
3231 if (is_complete)
3232 success = cxx_record_decl->isDynamicClass();
3233 else
3234 {
3235 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3236 if (metadata)
3237 success = metadata->GetIsDynamicCXXType();
3238 else
3239 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003240 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003241 if (is_complete)
3242 success = cxx_record_decl->isDynamicClass();
3243 else
3244 success = false;
3245 }
3246 }
3247
3248 if (success)
3249 {
3250 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003251 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003252 return true;
3253 }
3254 }
3255 }
3256 break;
3257
3258 case clang::Type::ObjCObject:
3259 case clang::Type::ObjCInterface:
3260 if (check_objc)
3261 {
3262 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003263 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003264 return true;
3265 }
3266 break;
3267
3268 default:
3269 break;
3270 }
3271 }
3272 }
3273 if (dynamic_pointee_type)
3274 dynamic_pointee_type->Clear();
3275 return false;
3276}
3277
3278
3279bool
3280ClangASTContext::IsScalarType (void* type)
3281{
3282 if (!type)
3283 return false;
3284
3285 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3286}
3287
3288bool
3289ClangASTContext::IsTypedefType (void* type)
3290{
3291 if (!type)
3292 return false;
3293 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3294}
3295
3296bool
3297ClangASTContext::IsVoidType (void* type)
3298{
3299 if (!type)
3300 return false;
3301 return GetCanonicalQualType(type)->isVoidType();
3302}
3303
3304bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003305ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003306{
3307 if (type)
3308 {
3309 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003310 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003311 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003312 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3313 if (cxx_record_decl)
3314 {
3315 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3316 return true;
3317 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003318 }
3319 }
3320 class_name.clear();
3321 return false;
3322}
3323
3324
3325bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003326ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003327{
3328 if (!type)
3329 return false;
3330
3331 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003332 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003333 return true;
3334 return false;
3335}
3336
3337bool
3338ClangASTContext::IsBeingDefined (void* type)
3339{
3340 if (!type)
3341 return false;
3342 clang::QualType qual_type (GetCanonicalQualType(type));
3343 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3344 if (tag_type)
3345 return tag_type->isBeingDefined();
3346 return false;
3347}
3348
3349bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003350ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003351{
3352 if (!type)
3353 return false;
3354
3355 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003356
3357 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003358 {
3359 if (class_type_ptr)
3360 {
3361 if (!qual_type->isObjCClassType() &&
3362 !qual_type->isObjCIdType())
3363 {
3364 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3365 if (obj_pointer_type == nullptr)
3366 class_type_ptr->Clear();
3367 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003368 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003369 }
3370 }
3371 return true;
3372 }
3373 if (class_type_ptr)
3374 class_type_ptr->Clear();
3375 return false;
3376}
3377
3378bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003379ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003380{
3381 if (!type)
3382 return false;
3383
3384 clang::QualType qual_type (GetCanonicalQualType(type));
3385
3386 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3387 if (object_type)
3388 {
3389 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3390 if (interface)
3391 {
3392 class_name = interface->getNameAsString();
3393 return true;
3394 }
3395 }
3396 return false;
3397}
3398
3399
3400//----------------------------------------------------------------------
3401// Type Completion
3402//----------------------------------------------------------------------
3403
3404bool
3405ClangASTContext::GetCompleteType (void* type)
3406{
3407 if (!type)
3408 return false;
3409 const bool allow_completion = true;
3410 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3411}
3412
3413ConstString
3414ClangASTContext::GetTypeName (void* type)
3415{
3416 std::string type_name;
3417 if (type)
3418 {
3419 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3420 clang::QualType qual_type(GetQualType(type));
3421 printing_policy.SuppressTagKeyword = true;
3422 printing_policy.LangOpts.WChar = true;
3423 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3424 if (typedef_type)
3425 {
3426 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3427 type_name = typedef_decl->getQualifiedNameAsString();
3428 }
3429 else
3430 {
3431 type_name = qual_type.getAsString(printing_policy);
3432 }
3433 }
3434 return ConstString(type_name);
3435}
3436
3437uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003438ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003439{
3440 if (!type)
3441 return 0;
3442
3443 if (pointee_or_element_clang_type)
3444 pointee_or_element_clang_type->Clear();
3445
3446 clang::QualType qual_type (GetQualType(type));
3447
3448 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3449 switch (type_class)
3450 {
3451 case clang::Type::Builtin:
3452 {
3453 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3454
3455 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3456 switch (builtin_type->getKind())
3457 {
3458 case clang::BuiltinType::ObjCId:
3459 case clang::BuiltinType::ObjCClass:
3460 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003461 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003462 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3463 break;
3464
3465 case clang::BuiltinType::ObjCSel:
3466 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003467 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003468 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3469 break;
3470
3471 case clang::BuiltinType::Bool:
3472 case clang::BuiltinType::Char_U:
3473 case clang::BuiltinType::UChar:
3474 case clang::BuiltinType::WChar_U:
3475 case clang::BuiltinType::Char16:
3476 case clang::BuiltinType::Char32:
3477 case clang::BuiltinType::UShort:
3478 case clang::BuiltinType::UInt:
3479 case clang::BuiltinType::ULong:
3480 case clang::BuiltinType::ULongLong:
3481 case clang::BuiltinType::UInt128:
3482 case clang::BuiltinType::Char_S:
3483 case clang::BuiltinType::SChar:
3484 case clang::BuiltinType::WChar_S:
3485 case clang::BuiltinType::Short:
3486 case clang::BuiltinType::Int:
3487 case clang::BuiltinType::Long:
3488 case clang::BuiltinType::LongLong:
3489 case clang::BuiltinType::Int128:
3490 case clang::BuiltinType::Float:
3491 case clang::BuiltinType::Double:
3492 case clang::BuiltinType::LongDouble:
3493 builtin_type_flags |= eTypeIsScalar;
3494 if (builtin_type->isInteger())
3495 {
3496 builtin_type_flags |= eTypeIsInteger;
3497 if (builtin_type->isSignedInteger())
3498 builtin_type_flags |= eTypeIsSigned;
3499 }
3500 else if (builtin_type->isFloatingPoint())
3501 builtin_type_flags |= eTypeIsFloat;
3502 break;
3503 default:
3504 break;
3505 }
3506 return builtin_type_flags;
3507 }
3508
3509 case clang::Type::BlockPointer:
3510 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003511 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003512 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3513
3514 case clang::Type::Complex:
3515 {
3516 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3517 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3518 if (complex_type)
3519 {
3520 clang::QualType complex_element_type (complex_type->getElementType());
3521 if (complex_element_type->isIntegerType())
3522 complex_type_flags |= eTypeIsFloat;
3523 else if (complex_element_type->isFloatingType())
3524 complex_type_flags |= eTypeIsInteger;
3525 }
3526 return complex_type_flags;
3527 }
3528 break;
3529
3530 case clang::Type::ConstantArray:
3531 case clang::Type::DependentSizedArray:
3532 case clang::Type::IncompleteArray:
3533 case clang::Type::VariableArray:
3534 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003535 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003536 return eTypeHasChildren | eTypeIsArray;
3537
3538 case clang::Type::DependentName: return 0;
3539 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3540 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3541 case clang::Type::Decltype: return 0;
3542
3543 case clang::Type::Enum:
3544 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003545 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003546 return eTypeIsEnumeration | eTypeHasValue;
3547
3548 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003549 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003550 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003551 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003552
3553 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3554 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3555 case clang::Type::InjectedClassName: return 0;
3556
3557 case clang::Type::LValueReference:
3558 case clang::Type::RValueReference:
3559 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003560 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003561 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3562
3563 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3564
3565 case clang::Type::ObjCObjectPointer:
3566 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003567 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003568 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3569
3570 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3571 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3572
3573 case clang::Type::Pointer:
3574 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003575 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003576 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3577
3578 case clang::Type::Record:
3579 if (qual_type->getAsCXXRecordDecl())
3580 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3581 else
3582 return eTypeHasChildren | eTypeIsStructUnion;
3583 break;
3584 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3585 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3586 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3587
3588 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003589 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 +00003590 case clang::Type::TypeOfExpr: return 0;
3591 case clang::Type::TypeOf: return 0;
3592 case clang::Type::UnresolvedUsing: return 0;
3593
3594 case clang::Type::ExtVector:
3595 case clang::Type::Vector:
3596 {
3597 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3598 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3599 if (vector_type)
3600 {
3601 if (vector_type->isIntegerType())
3602 vector_type_flags |= eTypeIsFloat;
3603 else if (vector_type->isFloatingType())
3604 vector_type_flags |= eTypeIsInteger;
3605 }
3606 return vector_type_flags;
3607 }
3608 default: return 0;
3609 }
3610 return 0;
3611}
3612
3613
3614
3615lldb::LanguageType
3616ClangASTContext::GetMinimumLanguage (void* type)
3617{
3618 if (!type)
3619 return lldb::eLanguageTypeC;
3620
3621 // If the type is a reference, then resolve it to what it refers to first:
3622 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3623 if (qual_type->isAnyPointerType())
3624 {
3625 if (qual_type->isObjCObjectPointerType())
3626 return lldb::eLanguageTypeObjC;
3627
3628 clang::QualType pointee_type (qual_type->getPointeeType());
3629 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3630 return lldb::eLanguageTypeC_plus_plus;
3631 if (pointee_type->isObjCObjectOrInterfaceType())
3632 return lldb::eLanguageTypeObjC;
3633 if (pointee_type->isObjCClassType())
3634 return lldb::eLanguageTypeObjC;
3635 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3636 return lldb::eLanguageTypeObjC;
3637 }
3638 else
3639 {
3640 if (qual_type->isObjCObjectOrInterfaceType())
3641 return lldb::eLanguageTypeObjC;
3642 if (qual_type->getAsCXXRecordDecl())
3643 return lldb::eLanguageTypeC_plus_plus;
3644 switch (qual_type->getTypeClass())
3645 {
3646 default:
3647 break;
3648 case clang::Type::Builtin:
3649 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3650 {
3651 default:
3652 case clang::BuiltinType::Void:
3653 case clang::BuiltinType::Bool:
3654 case clang::BuiltinType::Char_U:
3655 case clang::BuiltinType::UChar:
3656 case clang::BuiltinType::WChar_U:
3657 case clang::BuiltinType::Char16:
3658 case clang::BuiltinType::Char32:
3659 case clang::BuiltinType::UShort:
3660 case clang::BuiltinType::UInt:
3661 case clang::BuiltinType::ULong:
3662 case clang::BuiltinType::ULongLong:
3663 case clang::BuiltinType::UInt128:
3664 case clang::BuiltinType::Char_S:
3665 case clang::BuiltinType::SChar:
3666 case clang::BuiltinType::WChar_S:
3667 case clang::BuiltinType::Short:
3668 case clang::BuiltinType::Int:
3669 case clang::BuiltinType::Long:
3670 case clang::BuiltinType::LongLong:
3671 case clang::BuiltinType::Int128:
3672 case clang::BuiltinType::Float:
3673 case clang::BuiltinType::Double:
3674 case clang::BuiltinType::LongDouble:
3675 break;
3676
3677 case clang::BuiltinType::NullPtr:
3678 return eLanguageTypeC_plus_plus;
3679
3680 case clang::BuiltinType::ObjCId:
3681 case clang::BuiltinType::ObjCClass:
3682 case clang::BuiltinType::ObjCSel:
3683 return eLanguageTypeObjC;
3684
3685 case clang::BuiltinType::Dependent:
3686 case clang::BuiltinType::Overload:
3687 case clang::BuiltinType::BoundMember:
3688 case clang::BuiltinType::UnknownAny:
3689 break;
3690 }
3691 break;
3692 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003693 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003694 }
3695 }
3696 return lldb::eLanguageTypeC;
3697}
3698
3699lldb::TypeClass
3700ClangASTContext::GetTypeClass (void* type)
3701{
3702 if (!type)
3703 return lldb::eTypeClassInvalid;
3704
3705 clang::QualType qual_type(GetQualType(type));
3706
3707 switch (qual_type->getTypeClass())
3708 {
3709 case clang::Type::UnaryTransform: break;
3710 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3711 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3712 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3713 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3714 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3715 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3716 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3717 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3718 case clang::Type::Vector: return lldb::eTypeClassVector;
3719 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3720 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3721 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3722 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3723 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3724 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3725 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3726 case clang::Type::Complex:
3727 if (qual_type->isComplexType())
3728 return lldb::eTypeClassComplexFloat;
3729 else
3730 return lldb::eTypeClassComplexInteger;
3731 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3732 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3733 case clang::Type::Record:
3734 {
3735 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3736 const clang::RecordDecl *record_decl = record_type->getDecl();
3737 if (record_decl->isUnion())
3738 return lldb::eTypeClassUnion;
3739 else if (record_decl->isStruct())
3740 return lldb::eTypeClassStruct;
3741 else
3742 return lldb::eTypeClassClass;
3743 }
3744 break;
3745 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3746 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3747 case clang::Type::UnresolvedUsing: break;
3748 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003749 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003750 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003751 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003752
3753 case clang::Type::Attributed: break;
3754 case clang::Type::TemplateTypeParm: break;
3755 case clang::Type::SubstTemplateTypeParm: break;
3756 case clang::Type::SubstTemplateTypeParmPack:break;
3757 case clang::Type::Auto: break;
3758 case clang::Type::InjectedClassName: break;
3759 case clang::Type::DependentName: break;
3760 case clang::Type::DependentTemplateSpecialization: break;
3761 case clang::Type::PackExpansion: break;
3762
3763 case clang::Type::TypeOfExpr: break;
3764 case clang::Type::TypeOf: break;
3765 case clang::Type::Decltype: break;
3766 case clang::Type::TemplateSpecialization: break;
3767 case clang::Type::Atomic: break;
3768
3769 // pointer type decayed from an array or function type.
3770 case clang::Type::Decayed: break;
3771 case clang::Type::Adjusted: break;
3772 }
3773 // We don't know hot to display this type...
3774 return lldb::eTypeClassOther;
3775
3776}
3777
3778unsigned
3779ClangASTContext::GetTypeQualifiers(void* type)
3780{
3781 if (type)
3782 return GetQualType(type).getQualifiers().getCVRQualifiers();
3783 return 0;
3784}
3785
3786//----------------------------------------------------------------------
3787// Creating related types
3788//----------------------------------------------------------------------
3789
Greg Claytona1e5dc82015-08-11 22:53:00 +00003790CompilerType
3791ClangASTContext::AddConstModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003792{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003793 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003794 {
Greg Claytonf73034f2015-09-08 18:15:05 +00003795 // Make sure this type is a clang AST type
Greg Claytond8d4a572015-08-11 21:38:15 +00003796 clang::QualType result(GetQualType(type));
3797 result.addConst();
Greg Claytona1e5dc82015-08-11 22:53:00 +00003798 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003799 }
Greg Claytonf73034f2015-09-08 18:15:05 +00003800
Greg Claytona1e5dc82015-08-11 22:53:00 +00003801 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003802}
3803
Greg Claytona1e5dc82015-08-11 22:53:00 +00003804CompilerType
3805ClangASTContext::AddRestrictModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003806{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003807 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003808 {
3809 clang::QualType result(GetQualType(type));
3810 result.getQualifiers().setRestrict (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003811 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003812 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003813 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003814}
3815
Greg Claytona1e5dc82015-08-11 22:53:00 +00003816CompilerType
3817ClangASTContext::AddVolatileModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003818{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003819 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003820 {
3821 clang::QualType result(GetQualType(type));
3822 result.getQualifiers().setVolatile (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003823 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003824 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003825 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003826}
3827
Greg Claytona1e5dc82015-08-11 22:53:00 +00003828CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003829ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3830{
3831 if (type)
3832 {
3833 clang::QualType qual_type(GetCanonicalQualType(type));
3834
3835 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3836
3837 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003838 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003839
Greg Claytona1e5dc82015-08-11 22:53:00 +00003840 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003841
3842 // TODO: the real stride will be >= this value.. find the real one!
3843 if (stride)
3844 *stride = element_type.GetByteSize(nullptr);
3845
3846 return element_type;
3847
3848 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003849 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003850}
3851
Greg Claytona1e5dc82015-08-11 22:53:00 +00003852CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003853ClangASTContext::GetCanonicalType (void* type)
3854{
3855 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003856 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3857 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003858}
3859
3860static clang::QualType
3861GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3862{
3863 if (qual_type->isPointerType())
3864 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3865 else
3866 qual_type = qual_type.getUnqualifiedType();
3867 qual_type.removeLocalConst();
3868 qual_type.removeLocalRestrict();
3869 qual_type.removeLocalVolatile();
3870 return qual_type;
3871}
3872
Greg Claytona1e5dc82015-08-11 22:53:00 +00003873CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003874ClangASTContext::GetFullyUnqualifiedType (void* type)
3875{
3876 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003877 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3878 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003879}
3880
3881
3882int
3883ClangASTContext::GetFunctionArgumentCount (void* type)
3884{
3885 if (type)
3886 {
3887 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3888 if (func)
3889 return func->getNumParams();
3890 }
3891 return -1;
3892}
3893
Greg Claytona1e5dc82015-08-11 22:53:00 +00003894CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003895ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3896{
3897 if (type)
3898 {
3899 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3900 if (func)
3901 {
3902 const uint32_t num_args = func->getNumParams();
3903 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003904 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003905 }
3906 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003907 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003908}
3909
Greg Claytona1e5dc82015-08-11 22:53:00 +00003910CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003911ClangASTContext::GetFunctionReturnType (void* type)
3912{
3913 if (type)
3914 {
3915 clang::QualType qual_type(GetCanonicalQualType(type));
3916 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3917 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003918 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003919 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003920 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003921}
3922
3923size_t
3924ClangASTContext::GetNumMemberFunctions (void* type)
3925{
3926 size_t num_functions = 0;
3927 if (type)
3928 {
3929 clang::QualType qual_type(GetCanonicalQualType(type));
3930 switch (qual_type->getTypeClass()) {
3931 case clang::Type::Record:
3932 if (GetCompleteQualType (getASTContext(), qual_type))
3933 {
3934 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3935 const clang::RecordDecl *record_decl = record_type->getDecl();
3936 assert(record_decl);
3937 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3938 if (cxx_record_decl)
3939 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
3940 }
3941 break;
3942
3943 case clang::Type::ObjCObjectPointer:
3944 if (GetCompleteType(type))
3945 {
3946 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
3947 if (objc_class_type)
3948 {
3949 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
3950 if (class_interface_decl)
3951 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3952 }
3953 }
3954 break;
3955
3956 case clang::Type::ObjCObject:
3957 case clang::Type::ObjCInterface:
3958 if (GetCompleteType(type))
3959 {
3960 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
3961 if (objc_class_type)
3962 {
3963 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3964 if (class_interface_decl)
3965 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3966 }
3967 }
3968 break;
3969
3970
3971 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003972 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003973
3974 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003975 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003976
3977 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003978 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00003979
3980 default:
3981 break;
3982 }
3983 }
3984 return num_functions;
3985}
3986
3987TypeMemberFunctionImpl
3988ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
3989{
3990 std::string name("");
3991 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003992 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00003993 clang::ObjCMethodDecl *method_decl(nullptr);
3994 if (type)
3995 {
3996 clang::QualType qual_type(GetCanonicalQualType(type));
3997 switch (qual_type->getTypeClass()) {
3998 case clang::Type::Record:
3999 if (GetCompleteQualType (getASTContext(), qual_type))
4000 {
4001 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4002 const clang::RecordDecl *record_decl = record_type->getDecl();
4003 assert(record_decl);
4004 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4005 if (cxx_record_decl)
4006 {
4007 auto method_iter = cxx_record_decl->method_begin();
4008 auto method_end = cxx_record_decl->method_end();
4009 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4010 {
4011 std::advance(method_iter, idx);
4012 auto method_decl = method_iter->getCanonicalDecl();
4013 if (method_decl)
4014 {
4015 if (!method_decl->getName().empty())
4016 name.assign(method_decl->getName().data());
4017 else
4018 name.clear();
4019 if (method_decl->isStatic())
4020 kind = lldb::eMemberFunctionKindStaticMethod;
4021 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4022 kind = lldb::eMemberFunctionKindConstructor;
4023 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4024 kind = lldb::eMemberFunctionKindDestructor;
4025 else
4026 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004027 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004028 }
4029 }
4030 }
4031 }
4032 break;
4033
4034 case clang::Type::ObjCObjectPointer:
4035 if (GetCompleteType(type))
4036 {
4037 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4038 if (objc_class_type)
4039 {
4040 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4041 if (class_interface_decl)
4042 {
4043 auto method_iter = class_interface_decl->meth_begin();
4044 auto method_end = class_interface_decl->meth_end();
4045 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4046 {
4047 std::advance(method_iter, idx);
4048 method_decl = method_iter->getCanonicalDecl();
4049 if (method_decl)
4050 {
4051 name = method_decl->getSelector().getAsString();
4052 if (method_decl->isClassMethod())
4053 kind = lldb::eMemberFunctionKindStaticMethod;
4054 else
4055 kind = lldb::eMemberFunctionKindInstanceMethod;
4056 }
4057 }
4058 }
4059 }
4060 }
4061 break;
4062
4063 case clang::Type::ObjCObject:
4064 case clang::Type::ObjCInterface:
4065 if (GetCompleteType(type))
4066 {
4067 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4068 if (objc_class_type)
4069 {
4070 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4071 if (class_interface_decl)
4072 {
4073 auto method_iter = class_interface_decl->meth_begin();
4074 auto method_end = class_interface_decl->meth_end();
4075 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4076 {
4077 std::advance(method_iter, idx);
4078 method_decl = method_iter->getCanonicalDecl();
4079 if (method_decl)
4080 {
4081 name = method_decl->getSelector().getAsString();
4082 if (method_decl->isClassMethod())
4083 kind = lldb::eMemberFunctionKindStaticMethod;
4084 else
4085 kind = lldb::eMemberFunctionKindInstanceMethod;
4086 }
4087 }
4088 }
4089 }
4090 }
4091 break;
4092
4093 case clang::Type::Typedef:
4094 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4095
4096 case clang::Type::Elaborated:
4097 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4098
4099 case clang::Type::Paren:
4100 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4101
4102 default:
4103 break;
4104 }
4105 }
4106
4107 if (kind == eMemberFunctionKindUnknown)
4108 return TypeMemberFunctionImpl();
4109 if (method_decl)
4110 return TypeMemberFunctionImpl(method_decl, name, kind);
4111 if (type)
4112 return TypeMemberFunctionImpl(clang_type, name, kind);
4113
4114 return TypeMemberFunctionImpl();
4115}
4116
Greg Claytona1e5dc82015-08-11 22:53:00 +00004117CompilerType
4118ClangASTContext::GetLValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004119{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004120 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004121 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004122 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Ryan Brown57bee1e2015-09-14 22:45:11 +00004123 return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004124 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004125 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004126}
4127
Greg Claytona1e5dc82015-08-11 22:53:00 +00004128CompilerType
4129ClangASTContext::GetRValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004130{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004131 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004132 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004133 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Ryan Brown57bee1e2015-09-14 22:45:11 +00004134 return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004135 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004136 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004137}
4138
Greg Claytona1e5dc82015-08-11 22:53:00 +00004139CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004140ClangASTContext::GetNonReferenceType (void* type)
4141{
4142 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004143 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4144 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004145}
4146
Greg Claytona1e5dc82015-08-11 22:53:00 +00004147CompilerType
4148ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004149 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004150 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004151{
4152 if (type && typedef_name && typedef_name[0])
4153 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004154 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004155 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004156 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004157 clang::ASTContext* clang_ast = ast->getASTContext();
4158 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004159
4160 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004161 if (decl_ctx == nullptr)
4162 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004163
Greg Claytond8d4a572015-08-11 21:38:15 +00004164 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4165 decl_ctx,
4166 clang::SourceLocation(),
4167 clang::SourceLocation(),
4168 &clang_ast->Idents.get(typedef_name),
4169 clang_ast->getTrivialTypeSourceInfo(qual_type));
4170
4171 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4172
4173 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004174 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004175 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004176 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004177
4178}
4179
Greg Claytona1e5dc82015-08-11 22:53:00 +00004180CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004181ClangASTContext::GetPointeeType (void* type)
4182{
4183 if (type)
4184 {
4185 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004186 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
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
Greg Claytona1e5dc82015-08-11 22:53:00 +00004191CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004192ClangASTContext::GetPointerType (void* type)
4193{
4194 if (type)
4195 {
4196 clang::QualType qual_type (GetQualType(type));
4197
4198 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4199 switch (type_class)
4200 {
4201 case clang::Type::ObjCObject:
4202 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004203 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004204
4205 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004206 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004207 }
4208 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004209 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004210}
4211
Greg Claytona1e5dc82015-08-11 22:53:00 +00004212CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004213ClangASTContext::GetTypedefedType (void* type)
4214{
4215 if (type)
4216 {
4217 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4218 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004219 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004220 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004221 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004222}
4223
Greg Claytona1e5dc82015-08-11 22:53:00 +00004224CompilerType
4225ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004226{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004227 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004228 {
4229 clang::QualType qual_type(GetQualType(type));
4230 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004231 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004232 }
4233 return type;
4234}
4235
4236
4237//----------------------------------------------------------------------
4238// Create related types using the current type's AST
4239//----------------------------------------------------------------------
4240
Greg Claytona1e5dc82015-08-11 22:53:00 +00004241CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004242ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004243{
Greg Clayton99558cc42015-08-24 23:46:31 +00004244 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004245}
4246//----------------------------------------------------------------------
4247// Exploring the type
4248//----------------------------------------------------------------------
4249
4250uint64_t
4251ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4252{
4253 if (GetCompleteType (type))
4254 {
4255 clang::QualType qual_type(GetCanonicalQualType(type));
4256 switch (qual_type->getTypeClass())
4257 {
4258 case clang::Type::ObjCInterface:
4259 case clang::Type::ObjCObject:
4260 {
4261 ExecutionContext exe_ctx (exe_scope);
4262 Process *process = exe_ctx.GetProcessPtr();
4263 if (process)
4264 {
4265 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4266 if (objc_runtime)
4267 {
4268 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004269 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004270 return bit_size;
4271 }
4272 }
4273 else
4274 {
4275 static bool g_printed = false;
4276 if (!g_printed)
4277 {
4278 StreamString s;
4279 DumpTypeDescription(&s);
4280
4281 llvm::outs() << "warning: trying to determine the size of type ";
4282 llvm::outs() << s.GetString() << "\n";
4283 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4284 llvm::outs() << "backtrace:\n";
4285 llvm::sys::PrintStackTrace(llvm::outs());
4286 llvm::outs() << "\n";
4287 g_printed = true;
4288 }
4289 }
4290 }
4291 // fallthrough
4292 default:
4293 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4294 if (bit_size == 0)
4295 {
4296 if (qual_type->isIncompleteArrayType())
4297 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4298 }
4299 if (qual_type->isObjCObjectOrInterfaceType())
4300 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4301 return bit_size;
4302 }
4303 }
4304 return 0;
4305}
4306
4307size_t
4308ClangASTContext::GetTypeBitAlign (void* type)
4309{
4310 if (GetCompleteType(type))
4311 return getASTContext()->getTypeAlign(GetQualType(type));
4312 return 0;
4313}
4314
4315
4316lldb::Encoding
4317ClangASTContext::GetEncoding (void* type, uint64_t &count)
4318{
4319 if (!type)
4320 return lldb::eEncodingInvalid;
4321
4322 count = 1;
4323 clang::QualType qual_type(GetCanonicalQualType(type));
4324
4325 switch (qual_type->getTypeClass())
4326 {
4327 case clang::Type::UnaryTransform:
4328 break;
4329
4330 case clang::Type::FunctionNoProto:
4331 case clang::Type::FunctionProto:
4332 break;
4333
4334 case clang::Type::IncompleteArray:
4335 case clang::Type::VariableArray:
4336 break;
4337
4338 case clang::Type::ConstantArray:
4339 break;
4340
4341 case clang::Type::ExtVector:
4342 case clang::Type::Vector:
4343 // TODO: Set this to more than one???
4344 break;
4345
4346 case clang::Type::Builtin:
4347 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4348 {
4349 default: assert(0 && "Unknown builtin type!");
4350 case clang::BuiltinType::Void:
4351 break;
4352
4353 case clang::BuiltinType::Bool:
4354 case clang::BuiltinType::Char_S:
4355 case clang::BuiltinType::SChar:
4356 case clang::BuiltinType::WChar_S:
4357 case clang::BuiltinType::Char16:
4358 case clang::BuiltinType::Char32:
4359 case clang::BuiltinType::Short:
4360 case clang::BuiltinType::Int:
4361 case clang::BuiltinType::Long:
4362 case clang::BuiltinType::LongLong:
4363 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4364
4365 case clang::BuiltinType::Char_U:
4366 case clang::BuiltinType::UChar:
4367 case clang::BuiltinType::WChar_U:
4368 case clang::BuiltinType::UShort:
4369 case clang::BuiltinType::UInt:
4370 case clang::BuiltinType::ULong:
4371 case clang::BuiltinType::ULongLong:
4372 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4373
4374 case clang::BuiltinType::Float:
4375 case clang::BuiltinType::Double:
4376 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4377
4378 case clang::BuiltinType::ObjCClass:
4379 case clang::BuiltinType::ObjCId:
4380 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4381
4382 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4383
4384 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4385 case clang::BuiltinType::Kind::BoundMember:
4386 case clang::BuiltinType::Kind::BuiltinFn:
4387 case clang::BuiltinType::Kind::Dependent:
4388 case clang::BuiltinType::Kind::Half:
4389 case clang::BuiltinType::Kind::OCLEvent:
4390 case clang::BuiltinType::Kind::OCLImage1d:
4391 case clang::BuiltinType::Kind::OCLImage1dArray:
4392 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4393 case clang::BuiltinType::Kind::OCLImage2d:
4394 case clang::BuiltinType::Kind::OCLImage2dArray:
4395 case clang::BuiltinType::Kind::OCLImage3d:
4396 case clang::BuiltinType::Kind::OCLSampler:
4397 case clang::BuiltinType::Kind::Overload:
4398 case clang::BuiltinType::Kind::PseudoObject:
4399 case clang::BuiltinType::Kind::UnknownAny:
4400 break;
4401 }
4402 break;
4403 // All pointer types are represented as unsigned integer encodings.
4404 // We may nee to add a eEncodingPointer if we ever need to know the
4405 // difference
4406 case clang::Type::ObjCObjectPointer:
4407 case clang::Type::BlockPointer:
4408 case clang::Type::Pointer:
4409 case clang::Type::LValueReference:
4410 case clang::Type::RValueReference:
4411 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4412 case clang::Type::Complex:
4413 {
4414 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4415 if (qual_type->isComplexType())
4416 encoding = lldb::eEncodingIEEE754;
4417 else
4418 {
4419 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4420 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004421 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004422 else
4423 encoding = lldb::eEncodingSint;
4424 }
4425 count = 2;
4426 return encoding;
4427 }
4428
4429 case clang::Type::ObjCInterface: break;
4430 case clang::Type::Record: break;
4431 case clang::Type::Enum: return lldb::eEncodingSint;
4432 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004433 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004434
4435 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004436 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004437
4438 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004439 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004440
4441 case clang::Type::DependentSizedArray:
4442 case clang::Type::DependentSizedExtVector:
4443 case clang::Type::UnresolvedUsing:
4444 case clang::Type::Attributed:
4445 case clang::Type::TemplateTypeParm:
4446 case clang::Type::SubstTemplateTypeParm:
4447 case clang::Type::SubstTemplateTypeParmPack:
4448 case clang::Type::Auto:
4449 case clang::Type::InjectedClassName:
4450 case clang::Type::DependentName:
4451 case clang::Type::DependentTemplateSpecialization:
4452 case clang::Type::PackExpansion:
4453 case clang::Type::ObjCObject:
4454
4455 case clang::Type::TypeOfExpr:
4456 case clang::Type::TypeOf:
4457 case clang::Type::Decltype:
4458 case clang::Type::TemplateSpecialization:
4459 case clang::Type::Atomic:
4460 case clang::Type::Adjusted:
4461 break;
4462
4463 // pointer type decayed from an array or function type.
4464 case clang::Type::Decayed:
4465 break;
4466 }
4467 count = 0;
4468 return lldb::eEncodingInvalid;
4469}
4470
4471lldb::Format
4472ClangASTContext::GetFormat (void* type)
4473{
4474 if (!type)
4475 return lldb::eFormatDefault;
4476
4477 clang::QualType qual_type(GetCanonicalQualType(type));
4478
4479 switch (qual_type->getTypeClass())
4480 {
4481 case clang::Type::UnaryTransform:
4482 break;
4483
4484 case clang::Type::FunctionNoProto:
4485 case clang::Type::FunctionProto:
4486 break;
4487
4488 case clang::Type::IncompleteArray:
4489 case clang::Type::VariableArray:
4490 break;
4491
4492 case clang::Type::ConstantArray:
4493 return lldb::eFormatVoid; // no value
4494
4495 case clang::Type::ExtVector:
4496 case clang::Type::Vector:
4497 break;
4498
4499 case clang::Type::Builtin:
4500 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4501 {
4502 //default: assert(0 && "Unknown builtin type!");
4503 case clang::BuiltinType::UnknownAny:
4504 case clang::BuiltinType::Void:
4505 case clang::BuiltinType::BoundMember:
4506 break;
4507
4508 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4509 case clang::BuiltinType::Char_S:
4510 case clang::BuiltinType::SChar:
4511 case clang::BuiltinType::WChar_S:
4512 case clang::BuiltinType::Char_U:
4513 case clang::BuiltinType::UChar:
4514 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4515 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4516 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4517 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4518 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4519 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4520 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4521 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4522 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4523 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4524 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4525 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4526 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4527 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4528 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4529 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004530 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004531 return lldb::eFormatHex;
4532 }
4533 break;
4534 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4535 case clang::Type::BlockPointer: return lldb::eFormatHex;
4536 case clang::Type::Pointer: return lldb::eFormatHex;
4537 case clang::Type::LValueReference:
4538 case clang::Type::RValueReference: return lldb::eFormatHex;
4539 case clang::Type::MemberPointer: break;
4540 case clang::Type::Complex:
4541 {
4542 if (qual_type->isComplexType())
4543 return lldb::eFormatComplex;
4544 else
4545 return lldb::eFormatComplexInteger;
4546 }
4547 case clang::Type::ObjCInterface: break;
4548 case clang::Type::Record: break;
4549 case clang::Type::Enum: return lldb::eFormatEnum;
4550 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004551 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004552 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004553 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004554 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004555 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004556 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004557 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004558 case clang::Type::DependentSizedArray:
4559 case clang::Type::DependentSizedExtVector:
4560 case clang::Type::UnresolvedUsing:
4561 case clang::Type::Attributed:
4562 case clang::Type::TemplateTypeParm:
4563 case clang::Type::SubstTemplateTypeParm:
4564 case clang::Type::SubstTemplateTypeParmPack:
4565 case clang::Type::InjectedClassName:
4566 case clang::Type::DependentName:
4567 case clang::Type::DependentTemplateSpecialization:
4568 case clang::Type::PackExpansion:
4569 case clang::Type::ObjCObject:
4570
4571 case clang::Type::TypeOfExpr:
4572 case clang::Type::TypeOf:
4573 case clang::Type::Decltype:
4574 case clang::Type::TemplateSpecialization:
4575 case clang::Type::Atomic:
4576 case clang::Type::Adjusted:
4577 break;
4578
4579 // pointer type decayed from an array or function type.
4580 case clang::Type::Decayed:
4581 break;
4582 }
4583 // We don't know hot to display this type...
4584 return lldb::eFormatBytes;
4585}
4586
4587static bool
4588ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4589{
4590 while (class_interface_decl)
4591 {
4592 if (class_interface_decl->ivar_size() > 0)
4593 return true;
4594
4595 if (check_superclass)
4596 class_interface_decl = class_interface_decl->getSuperClass();
4597 else
4598 break;
4599 }
4600 return false;
4601}
4602
4603uint32_t
4604ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4605{
4606 if (!type)
4607 return 0;
4608
4609 uint32_t num_children = 0;
4610 clang::QualType qual_type(GetQualType(type));
4611 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4612 switch (type_class)
4613 {
4614 case clang::Type::Builtin:
4615 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4616 {
4617 case clang::BuiltinType::ObjCId: // child is Class
4618 case clang::BuiltinType::ObjCClass: // child is Class
4619 num_children = 1;
4620 break;
4621
4622 default:
4623 break;
4624 }
4625 break;
4626
4627 case clang::Type::Complex: return 0;
4628
4629 case clang::Type::Record:
4630 if (GetCompleteQualType (getASTContext(), qual_type))
4631 {
4632 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4633 const clang::RecordDecl *record_decl = record_type->getDecl();
4634 assert(record_decl);
4635 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4636 if (cxx_record_decl)
4637 {
4638 if (omit_empty_base_classes)
4639 {
4640 // Check each base classes to see if it or any of its
4641 // base classes contain any fields. This can help
4642 // limit the noise in variable views by not having to
4643 // show base classes that contain no members.
4644 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4645 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4646 base_class != base_class_end;
4647 ++base_class)
4648 {
4649 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4650
4651 // Skip empty base classes
4652 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4653 continue;
4654
4655 num_children++;
4656 }
4657 }
4658 else
4659 {
4660 // Include all base classes
4661 num_children += cxx_record_decl->getNumBases();
4662 }
4663
4664 }
4665 clang::RecordDecl::field_iterator field, field_end;
4666 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4667 ++num_children;
4668 }
4669 break;
4670
4671 case clang::Type::ObjCObject:
4672 case clang::Type::ObjCInterface:
4673 if (GetCompleteQualType (getASTContext(), qual_type))
4674 {
4675 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4676 assert (objc_class_type);
4677 if (objc_class_type)
4678 {
4679 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4680
4681 if (class_interface_decl)
4682 {
4683
4684 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4685 if (superclass_interface_decl)
4686 {
4687 if (omit_empty_base_classes)
4688 {
4689 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4690 ++num_children;
4691 }
4692 else
4693 ++num_children;
4694 }
4695
4696 num_children += class_interface_decl->ivar_size();
4697 }
4698 }
4699 }
4700 break;
4701
4702 case clang::Type::ObjCObjectPointer:
4703 {
4704 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4705 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004706 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004707 // If this type points to a simple type, then it has 1 child
4708 if (num_pointee_children == 0)
4709 num_children = 1;
4710 else
4711 num_children = num_pointee_children;
4712 }
4713 break;
4714
4715 case clang::Type::Vector:
4716 case clang::Type::ExtVector:
4717 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4718 break;
4719
4720 case clang::Type::ConstantArray:
4721 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4722 break;
4723
4724 case clang::Type::Pointer:
4725 {
4726 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4727 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004728 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004729 if (num_pointee_children == 0)
4730 {
4731 // We have a pointer to a pointee type that claims it has no children.
4732 // We will want to look at
4733 num_children = GetNumPointeeChildren (pointee_type);
4734 }
4735 else
4736 num_children = num_pointee_children;
4737 }
4738 break;
4739
4740 case clang::Type::LValueReference:
4741 case clang::Type::RValueReference:
4742 {
4743 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4744 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004745 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004746 // If this type points to a simple type, then it has 1 child
4747 if (num_pointee_children == 0)
4748 num_children = 1;
4749 else
4750 num_children = num_pointee_children;
4751 }
4752 break;
4753
4754
4755 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004756 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004757 break;
4758
4759 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004760 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004761 break;
4762
4763 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004764 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004765 break;
4766 default:
4767 break;
4768 }
4769 return num_children;
4770}
4771
4772lldb::BasicType
4773ClangASTContext::GetBasicTypeEnumeration (void* type)
4774{
4775 if (type)
4776 {
4777 clang::QualType qual_type(GetQualType(type));
4778 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4779 if (type_class == clang::Type::Builtin)
4780 {
4781 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4782 {
4783 case clang::BuiltinType::Void: return eBasicTypeVoid;
4784 case clang::BuiltinType::Bool: return eBasicTypeBool;
4785 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4786 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4787 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4788 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4789 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4790 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4791 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4792 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4793 case clang::BuiltinType::Short: return eBasicTypeShort;
4794 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4795 case clang::BuiltinType::Int: return eBasicTypeInt;
4796 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4797 case clang::BuiltinType::Long: return eBasicTypeLong;
4798 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4799 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4800 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4801 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4802 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4803
4804 case clang::BuiltinType::Half: return eBasicTypeHalf;
4805 case clang::BuiltinType::Float: return eBasicTypeFloat;
4806 case clang::BuiltinType::Double: return eBasicTypeDouble;
4807 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4808
4809 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4810 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4811 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4812 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004813 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004814 return eBasicTypeOther;
4815 }
4816 }
4817 }
4818 return eBasicTypeInvalid;
4819}
4820
Greg Clayton99558cc42015-08-24 23:46:31 +00004821void
4822ClangASTContext::ForEachEnumerator (void* type, std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback)
4823{
4824 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
4825 if (enum_type)
4826 {
4827 const clang::EnumDecl *enum_decl = enum_type->getDecl();
4828 if (enum_decl)
4829 {
4830 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
4831
4832 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
4833 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
4834 {
4835 ConstString name(enum_pos->getNameAsString().c_str());
4836 if (!callback (integer_type, name, enum_pos->getInitVal()))
4837 break;
4838 }
4839 }
4840 }
4841}
4842
Greg Claytond8d4a572015-08-11 21:38:15 +00004843
4844#pragma mark Aggregate Types
4845
4846uint32_t
Greg Claytond8d4a572015-08-11 21:38:15 +00004847ClangASTContext::GetNumFields (void* type)
4848{
4849 if (!type)
4850 return 0;
4851
4852 uint32_t count = 0;
4853 clang::QualType qual_type(GetCanonicalQualType(type));
4854 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4855 switch (type_class)
4856 {
4857 case clang::Type::Record:
4858 if (GetCompleteType(type))
4859 {
4860 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
4861 if (record_type)
4862 {
4863 clang::RecordDecl *record_decl = record_type->getDecl();
4864 if (record_decl)
4865 {
4866 uint32_t field_idx = 0;
4867 clang::RecordDecl::field_iterator field, field_end;
4868 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4869 ++field_idx;
4870 count = field_idx;
4871 }
4872 }
4873 }
4874 break;
4875
4876 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004877 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004878 break;
4879
4880 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004881 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004882 break;
4883
4884 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004885 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004886 break;
4887
4888 case clang::Type::ObjCObjectPointer:
4889 if (GetCompleteType(type))
4890 {
4891 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4892 if (objc_class_type)
4893 {
4894 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4895
4896 if (class_interface_decl)
4897 count = class_interface_decl->ivar_size();
4898 }
4899 }
4900 break;
4901
4902 case clang::Type::ObjCObject:
4903 case clang::Type::ObjCInterface:
4904 if (GetCompleteType(type))
4905 {
4906 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4907 if (objc_class_type)
4908 {
4909 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4910
4911 if (class_interface_decl)
4912 count = class_interface_decl->ivar_size();
4913 }
4914 }
4915 break;
4916
4917 default:
4918 break;
4919 }
4920 return count;
4921}
4922
Greg Claytond8d4a572015-08-11 21:38:15 +00004923static clang_type_t
4924GetObjCFieldAtIndex (clang::ASTContext *ast,
4925 clang::ObjCInterfaceDecl *class_interface_decl,
4926 size_t idx,
4927 std::string& name,
4928 uint64_t *bit_offset_ptr,
4929 uint32_t *bitfield_bit_size_ptr,
4930 bool *is_bitfield_ptr)
4931{
4932 if (class_interface_decl)
4933 {
4934 if (idx < (class_interface_decl->ivar_size()))
4935 {
4936 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
4937 uint32_t ivar_idx = 0;
4938
4939 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
4940 {
4941 if (ivar_idx == idx)
4942 {
4943 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
4944
4945 clang::QualType ivar_qual_type(ivar_decl->getType());
4946
4947 name.assign(ivar_decl->getNameAsString());
4948
4949 if (bit_offset_ptr)
4950 {
4951 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
4952 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
4953 }
4954
4955 const bool is_bitfield = ivar_pos->isBitField();
4956
4957 if (bitfield_bit_size_ptr)
4958 {
4959 *bitfield_bit_size_ptr = 0;
4960
4961 if (is_bitfield && ast)
4962 {
4963 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
4964 llvm::APSInt bitfield_apsint;
4965 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
4966 {
4967 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
4968 }
4969 }
4970 }
4971 if (is_bitfield_ptr)
4972 *is_bitfield_ptr = is_bitfield;
4973
4974 return ivar_qual_type.getAsOpaquePtr();
4975 }
4976 }
4977 }
4978 }
4979 return nullptr;
4980}
4981
Greg Claytona1e5dc82015-08-11 22:53:00 +00004982CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004983ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
4984 std::string& name,
4985 uint64_t *bit_offset_ptr,
4986 uint32_t *bitfield_bit_size_ptr,
4987 bool *is_bitfield_ptr)
4988{
4989 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004990 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004991
4992 clang::QualType qual_type(GetCanonicalQualType(type));
4993 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4994 switch (type_class)
4995 {
4996 case clang::Type::Record:
4997 if (GetCompleteType(type))
4998 {
4999 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5000 const clang::RecordDecl *record_decl = record_type->getDecl();
5001 uint32_t field_idx = 0;
5002 clang::RecordDecl::field_iterator field, field_end;
5003 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5004 {
5005 if (idx == field_idx)
5006 {
5007 // Print the member type if requested
5008 // Print the member name and equal sign
5009 name.assign(field->getNameAsString());
5010
5011 // Figure out the type byte size (field_type_info.first) and
5012 // alignment (field_type_info.second) from the AST context.
5013 if (bit_offset_ptr)
5014 {
5015 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5016 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5017 }
5018
5019 const bool is_bitfield = field->isBitField();
5020
5021 if (bitfield_bit_size_ptr)
5022 {
5023 *bitfield_bit_size_ptr = 0;
5024
5025 if (is_bitfield)
5026 {
5027 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5028 llvm::APSInt bitfield_apsint;
5029 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5030 {
5031 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5032 }
5033 }
5034 }
5035 if (is_bitfield_ptr)
5036 *is_bitfield_ptr = is_bitfield;
5037
Greg Claytona1e5dc82015-08-11 22:53:00 +00005038 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005039 }
5040 }
5041 }
5042 break;
5043
5044 case clang::Type::ObjCObjectPointer:
5045 if (GetCompleteType(type))
5046 {
5047 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5048 if (objc_class_type)
5049 {
5050 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005051 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 +00005052 }
5053 }
5054 break;
5055
5056 case clang::Type::ObjCObject:
5057 case clang::Type::ObjCInterface:
5058 if (GetCompleteType(type))
5059 {
5060 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5061 assert (objc_class_type);
5062 if (objc_class_type)
5063 {
5064 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005065 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 +00005066 }
5067 }
5068 break;
5069
5070
5071 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005072 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005073 GetFieldAtIndex (idx,
5074 name,
5075 bit_offset_ptr,
5076 bitfield_bit_size_ptr,
5077 is_bitfield_ptr);
5078
5079 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005080 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005081 GetFieldAtIndex (idx,
5082 name,
5083 bit_offset_ptr,
5084 bitfield_bit_size_ptr,
5085 is_bitfield_ptr);
5086
5087 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005088 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005089 GetFieldAtIndex (idx,
5090 name,
5091 bit_offset_ptr,
5092 bitfield_bit_size_ptr,
5093 is_bitfield_ptr);
5094
5095 default:
5096 break;
5097 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005098 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005099}
5100
Greg Clayton99558cc42015-08-24 23:46:31 +00005101uint32_t
5102ClangASTContext::GetNumDirectBaseClasses (void *type)
5103{
5104 uint32_t count = 0;
5105 clang::QualType qual_type(GetCanonicalQualType(type));
5106 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5107 switch (type_class)
5108 {
5109 case clang::Type::Record:
5110 if (GetCompleteType(type))
5111 {
5112 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5113 if (cxx_record_decl)
5114 count = cxx_record_decl->getNumBases();
5115 }
5116 break;
5117
5118 case clang::Type::ObjCObjectPointer:
5119 count = GetPointeeType(type).GetNumDirectBaseClasses();
5120 break;
5121
5122 case clang::Type::ObjCObject:
5123 if (GetCompleteType(type))
5124 {
5125 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5126 if (objc_class_type)
5127 {
5128 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5129
5130 if (class_interface_decl && class_interface_decl->getSuperClass())
5131 count = 1;
5132 }
5133 }
5134 break;
5135 case clang::Type::ObjCInterface:
5136 if (GetCompleteType(type))
5137 {
5138 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5139 if (objc_interface_type)
5140 {
5141 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5142
5143 if (class_interface_decl && class_interface_decl->getSuperClass())
5144 count = 1;
5145 }
5146 }
5147 break;
5148
5149
5150 case clang::Type::Typedef:
5151 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5152 break;
5153
5154 case clang::Type::Elaborated:
5155 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5156 break;
5157
5158 case clang::Type::Paren:
5159 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5160
5161 default:
5162 break;
5163 }
5164 return count;
5165
5166}
5167
5168uint32_t
5169ClangASTContext::GetNumVirtualBaseClasses (void *type)
5170{
5171 uint32_t count = 0;
5172 clang::QualType qual_type(GetCanonicalQualType(type));
5173 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5174 switch (type_class)
5175 {
5176 case clang::Type::Record:
5177 if (GetCompleteType(type))
5178 {
5179 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5180 if (cxx_record_decl)
5181 count = cxx_record_decl->getNumVBases();
5182 }
5183 break;
5184
5185 case clang::Type::Typedef:
5186 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5187 break;
5188
5189 case clang::Type::Elaborated:
5190 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5191 break;
5192
5193 case clang::Type::Paren:
5194 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5195 break;
5196
5197 default:
5198 break;
5199 }
5200 return count;
5201
5202}
5203
5204CompilerType
5205ClangASTContext::GetDirectBaseClassAtIndex (void *type, size_t idx, uint32_t *bit_offset_ptr)
5206{
5207 clang::QualType qual_type(GetCanonicalQualType(type));
5208 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5209 switch (type_class)
5210 {
5211 case clang::Type::Record:
5212 if (GetCompleteType(type))
5213 {
5214 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5215 if (cxx_record_decl)
5216 {
5217 uint32_t curr_idx = 0;
5218 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5219 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5220 base_class != base_class_end;
5221 ++base_class, ++curr_idx)
5222 {
5223 if (curr_idx == idx)
5224 {
5225 if (bit_offset_ptr)
5226 {
5227 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5228 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5229 if (base_class->isVirtual())
5230 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5231 else
5232 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5233 }
5234 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5235 }
5236 }
5237 }
5238 }
5239 break;
5240
5241 case clang::Type::ObjCObjectPointer:
5242 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5243
5244 case clang::Type::ObjCObject:
5245 if (idx == 0 && GetCompleteType(type))
5246 {
5247 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5248 if (objc_class_type)
5249 {
5250 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5251
5252 if (class_interface_decl)
5253 {
5254 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5255 if (superclass_interface_decl)
5256 {
5257 if (bit_offset_ptr)
5258 *bit_offset_ptr = 0;
5259 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5260 }
5261 }
5262 }
5263 }
5264 break;
5265 case clang::Type::ObjCInterface:
5266 if (idx == 0 && GetCompleteType(type))
5267 {
5268 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5269 if (objc_interface_type)
5270 {
5271 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5272
5273 if (class_interface_decl)
5274 {
5275 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5276 if (superclass_interface_decl)
5277 {
5278 if (bit_offset_ptr)
5279 *bit_offset_ptr = 0;
5280 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5281 }
5282 }
5283 }
5284 }
5285 break;
5286
5287
5288 case clang::Type::Typedef:
5289 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5290
5291 case clang::Type::Elaborated:
5292 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5293
5294 case clang::Type::Paren:
5295 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5296
5297 default:
5298 break;
5299 }
5300 return CompilerType();
5301}
5302
5303CompilerType
5304ClangASTContext::GetVirtualBaseClassAtIndex (void *type,
5305 size_t idx,
5306 uint32_t *bit_offset_ptr)
5307{
5308 clang::QualType qual_type(GetCanonicalQualType(type));
5309 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5310 switch (type_class)
5311 {
5312 case clang::Type::Record:
5313 if (GetCompleteType(type))
5314 {
5315 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5316 if (cxx_record_decl)
5317 {
5318 uint32_t curr_idx = 0;
5319 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5320 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5321 base_class != base_class_end;
5322 ++base_class, ++curr_idx)
5323 {
5324 if (curr_idx == idx)
5325 {
5326 if (bit_offset_ptr)
5327 {
5328 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5329 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5330 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5331
5332 }
5333 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5334 }
5335 }
5336 }
5337 }
5338 break;
5339
5340 case clang::Type::Typedef:
5341 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5342
5343 case clang::Type::Elaborated:
5344 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5345
5346 case clang::Type::Paren:
5347 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5348
5349 default:
5350 break;
5351 }
5352 return CompilerType();
5353
5354}
5355
Greg Claytond8d4a572015-08-11 21:38:15 +00005356// If a pointer to a pointee type (the clang_type arg) says that it has no
5357// children, then we either need to trust it, or override it and return a
5358// different result. For example, an "int *" has one child that is an integer,
5359// but a function pointer doesn't have any children. Likewise if a Record type
5360// claims it has no children, then there really is nothing to show.
5361uint32_t
5362ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5363{
5364 if (type.isNull())
5365 return 0;
5366
5367 clang::QualType qual_type(type.getCanonicalType());
5368 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5369 switch (type_class)
5370 {
5371 case clang::Type::Builtin:
5372 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5373 {
5374 case clang::BuiltinType::UnknownAny:
5375 case clang::BuiltinType::Void:
5376 case clang::BuiltinType::NullPtr:
5377 case clang::BuiltinType::OCLEvent:
5378 case clang::BuiltinType::OCLImage1d:
5379 case clang::BuiltinType::OCLImage1dArray:
5380 case clang::BuiltinType::OCLImage1dBuffer:
5381 case clang::BuiltinType::OCLImage2d:
5382 case clang::BuiltinType::OCLImage2dArray:
5383 case clang::BuiltinType::OCLImage3d:
5384 case clang::BuiltinType::OCLSampler:
5385 return 0;
5386 case clang::BuiltinType::Bool:
5387 case clang::BuiltinType::Char_U:
5388 case clang::BuiltinType::UChar:
5389 case clang::BuiltinType::WChar_U:
5390 case clang::BuiltinType::Char16:
5391 case clang::BuiltinType::Char32:
5392 case clang::BuiltinType::UShort:
5393 case clang::BuiltinType::UInt:
5394 case clang::BuiltinType::ULong:
5395 case clang::BuiltinType::ULongLong:
5396 case clang::BuiltinType::UInt128:
5397 case clang::BuiltinType::Char_S:
5398 case clang::BuiltinType::SChar:
5399 case clang::BuiltinType::WChar_S:
5400 case clang::BuiltinType::Short:
5401 case clang::BuiltinType::Int:
5402 case clang::BuiltinType::Long:
5403 case clang::BuiltinType::LongLong:
5404 case clang::BuiltinType::Int128:
5405 case clang::BuiltinType::Float:
5406 case clang::BuiltinType::Double:
5407 case clang::BuiltinType::LongDouble:
5408 case clang::BuiltinType::Dependent:
5409 case clang::BuiltinType::Overload:
5410 case clang::BuiltinType::ObjCId:
5411 case clang::BuiltinType::ObjCClass:
5412 case clang::BuiltinType::ObjCSel:
5413 case clang::BuiltinType::BoundMember:
5414 case clang::BuiltinType::Half:
5415 case clang::BuiltinType::ARCUnbridgedCast:
5416 case clang::BuiltinType::PseudoObject:
5417 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005418 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005419 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005420 default:
5421 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005422 }
5423 break;
5424
5425 case clang::Type::Complex: return 1;
5426 case clang::Type::Pointer: return 1;
5427 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5428 case clang::Type::LValueReference: return 1;
5429 case clang::Type::RValueReference: return 1;
5430 case clang::Type::MemberPointer: return 0;
5431 case clang::Type::ConstantArray: return 0;
5432 case clang::Type::IncompleteArray: return 0;
5433 case clang::Type::VariableArray: return 0;
5434 case clang::Type::DependentSizedArray: return 0;
5435 case clang::Type::DependentSizedExtVector: return 0;
5436 case clang::Type::Vector: return 0;
5437 case clang::Type::ExtVector: return 0;
5438 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5439 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5440 case clang::Type::UnresolvedUsing: return 0;
5441 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5442 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5443 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5444 case clang::Type::TypeOfExpr: return 0;
5445 case clang::Type::TypeOf: return 0;
5446 case clang::Type::Decltype: return 0;
5447 case clang::Type::Record: return 0;
5448 case clang::Type::Enum: return 1;
5449 case clang::Type::TemplateTypeParm: return 1;
5450 case clang::Type::SubstTemplateTypeParm: return 1;
5451 case clang::Type::TemplateSpecialization: return 1;
5452 case clang::Type::InjectedClassName: return 0;
5453 case clang::Type::DependentName: return 1;
5454 case clang::Type::DependentTemplateSpecialization: return 1;
5455 case clang::Type::ObjCObject: return 0;
5456 case clang::Type::ObjCInterface: return 0;
5457 case clang::Type::ObjCObjectPointer: return 1;
5458 default:
5459 break;
5460 }
5461 return 0;
5462}
5463
5464
Greg Claytona1e5dc82015-08-11 22:53:00 +00005465CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00005466ClangASTContext::GetChildClangTypeAtIndex (void* type,
5467 ExecutionContext *exe_ctx,
5468 size_t idx,
5469 bool transparent_pointers,
5470 bool omit_empty_base_classes,
5471 bool ignore_array_bounds,
5472 std::string& child_name,
5473 uint32_t &child_byte_size,
5474 int32_t &child_byte_offset,
5475 uint32_t &child_bitfield_bit_size,
5476 uint32_t &child_bitfield_bit_offset,
5477 bool &child_is_base_class,
5478 bool &child_is_deref_of_parent,
5479 ValueObject *valobj)
Greg Claytond8d4a572015-08-11 21:38:15 +00005480{
5481 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005482 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005483
5484 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5485 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5486 child_bitfield_bit_size = 0;
5487 child_bitfield_bit_offset = 0;
5488 child_is_base_class = false;
5489
5490 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5491 uint32_t bit_offset;
5492 switch (parent_type_class)
5493 {
5494 case clang::Type::Builtin:
5495 if (idx_is_valid)
5496 {
5497 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5498 {
5499 case clang::BuiltinType::ObjCId:
5500 case clang::BuiltinType::ObjCClass:
5501 child_name = "isa";
5502 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005503 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005504
5505 default:
5506 break;
5507 }
5508 }
5509 break;
5510
5511 case clang::Type::Record:
5512 if (idx_is_valid && GetCompleteType(type))
5513 {
5514 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5515 const clang::RecordDecl *record_decl = record_type->getDecl();
5516 assert(record_decl);
5517 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5518 uint32_t child_idx = 0;
5519
5520 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5521 if (cxx_record_decl)
5522 {
5523 // We might have base classes to print out first
5524 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5525 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5526 base_class != base_class_end;
5527 ++base_class)
5528 {
5529 const clang::CXXRecordDecl *base_class_decl = nullptr;
5530
5531 // Skip empty base classes
5532 if (omit_empty_base_classes)
5533 {
5534 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5535 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5536 continue;
5537 }
5538
5539 if (idx == child_idx)
5540 {
5541 if (base_class_decl == nullptr)
5542 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5543
5544
5545 if (base_class->isVirtual())
5546 {
5547 bool handled = false;
5548 if (valobj)
5549 {
5550 Error err;
5551 AddressType addr_type = eAddressTypeInvalid;
5552 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5553
5554 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5555 {
5556
5557 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5558 Process *process = exe_ctx.GetProcessPtr();
5559 if (process)
5560 {
5561 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5562 if (vtable_ctx)
5563 {
5564 if (vtable_ctx->isMicrosoft())
5565 {
5566 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5567
5568 if (vtable_ptr_addr)
5569 {
5570 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5571
5572 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5573 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5574 {
5575 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5576 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5577 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5578 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5579 if (base_offset != UINT32_MAX)
5580 {
5581 handled = true;
5582 bit_offset = base_offset * 8;
5583 }
5584 }
5585 }
5586 }
5587 else
5588 {
5589 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5590 if (vtable_ptr_addr)
5591 {
5592 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5593 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5594 {
5595 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5596 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5597 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5598 if (base_offset != UINT32_MAX)
5599 {
5600 handled = true;
5601 bit_offset = base_offset * 8;
5602 }
5603 }
5604 }
5605 }
5606 }
5607 }
5608 }
5609
5610 }
5611 if (!handled)
5612 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5613 }
5614 else
5615 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5616
5617 // Base classes should be a multiple of 8 bits in size
5618 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005619 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005620 child_name = base_class_clang_type.GetTypeName().AsCString("");
5621 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5622
5623 // Base classes bit sizes should be a multiple of 8 bits in size
5624 assert (base_class_clang_type_bit_size % 8 == 0);
5625 child_byte_size = base_class_clang_type_bit_size / 8;
5626 child_is_base_class = true;
5627 return base_class_clang_type;
5628 }
5629 // We don't increment the child index in the for loop since we might
5630 // be skipping empty base classes
5631 ++child_idx;
5632 }
5633 }
5634 // Make sure index is in range...
5635 uint32_t field_idx = 0;
5636 clang::RecordDecl::field_iterator field, field_end;
5637 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5638 {
5639 if (idx == child_idx)
5640 {
5641 // Print the member type if requested
5642 // Print the member name and equal sign
5643 child_name.assign(field->getNameAsString().c_str());
5644
5645 // Figure out the type byte size (field_type_info.first) and
5646 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005647 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005648 assert(field_idx < record_layout.getFieldCount());
5649 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5650
5651 // Figure out the field offset within the current struct/union/class type
5652 bit_offset = record_layout.getFieldOffset (field_idx);
5653 child_byte_offset = bit_offset / 8;
5654 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5655 child_bitfield_bit_offset = bit_offset % 8;
5656
5657 return field_clang_type;
5658 }
5659 }
5660 }
5661 break;
5662
5663 case clang::Type::ObjCObject:
5664 case clang::Type::ObjCInterface:
5665 if (idx_is_valid && GetCompleteType(type))
5666 {
5667 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5668 assert (objc_class_type);
5669 if (objc_class_type)
5670 {
5671 uint32_t child_idx = 0;
5672 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5673
5674 if (class_interface_decl)
5675 {
5676
5677 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5678 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5679 if (superclass_interface_decl)
5680 {
5681 if (omit_empty_base_classes)
5682 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005683 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005684 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5685 {
5686 if (idx == 0)
5687 {
5688 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5689
5690
5691 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5692
5693 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5694
5695 child_byte_size = ivar_type_info.Width / 8;
5696 child_byte_offset = 0;
5697 child_is_base_class = true;
5698
Greg Claytona1e5dc82015-08-11 22:53:00 +00005699 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005700 }
5701
5702 ++child_idx;
5703 }
5704 }
5705 else
5706 ++child_idx;
5707 }
5708
5709 const uint32_t superclass_idx = child_idx;
5710
5711 if (idx < (child_idx + class_interface_decl->ivar_size()))
5712 {
5713 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5714
5715 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5716 {
5717 if (child_idx == idx)
5718 {
5719 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5720
5721 clang::QualType ivar_qual_type(ivar_decl->getType());
5722
5723 child_name.assign(ivar_decl->getNameAsString().c_str());
5724
5725 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5726
5727 child_byte_size = ivar_type_info.Width / 8;
5728
5729 // Figure out the field offset within the current struct/union/class type
5730 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5731 // that doesn't account for the space taken up by unbacked properties, or from
5732 // the changing size of base classes that are newer than this class.
5733 // So if we have a process around that we can ask about this object, do so.
5734 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5735 Process *process = nullptr;
5736 if (exe_ctx)
5737 process = exe_ctx->GetProcessPtr();
5738 if (process)
5739 {
5740 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5741 if (objc_runtime != nullptr)
5742 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005743 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005744 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5745 }
5746 }
5747
5748 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5749 bit_offset = UINT32_MAX;
5750
5751 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5752 {
5753 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5754 child_byte_offset = bit_offset / 8;
5755 }
5756
5757 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5758 // of a bitfield within its containing object. So regardless of where we get the byte
5759 // offset from, we still need to get the bit offset for bitfields from the layout.
5760
5761 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5762 {
5763 if (bit_offset == UINT32_MAX)
5764 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5765
5766 child_bitfield_bit_offset = bit_offset % 8;
5767 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005768 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005769 }
5770 ++child_idx;
5771 }
5772 }
5773 }
5774 }
5775 }
5776 break;
5777
5778 case clang::Type::ObjCObjectPointer:
5779 if (idx_is_valid)
5780 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005781 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005782
5783 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5784 {
5785 child_is_deref_of_parent = false;
5786 bool tmp_child_is_deref_of_parent = false;
5787 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5788 idx,
5789 transparent_pointers,
5790 omit_empty_base_classes,
5791 ignore_array_bounds,
5792 child_name,
5793 child_byte_size,
5794 child_byte_offset,
5795 child_bitfield_bit_size,
5796 child_bitfield_bit_offset,
5797 child_is_base_class,
5798 tmp_child_is_deref_of_parent,
5799 valobj);
5800 }
5801 else
5802 {
5803 child_is_deref_of_parent = true;
5804 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5805 if (parent_name)
5806 {
5807 child_name.assign(1, '*');
5808 child_name += parent_name;
5809 }
5810
5811 // We have a pointer to an simple type
5812 if (idx == 0 && pointee_clang_type.GetCompleteType())
5813 {
5814 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5815 child_byte_offset = 0;
5816 return pointee_clang_type;
5817 }
5818 }
5819 }
5820 break;
5821
5822 case clang::Type::Vector:
5823 case clang::Type::ExtVector:
5824 if (idx_is_valid)
5825 {
5826 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5827 if (array)
5828 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005829 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005830 if (element_type.GetCompleteType())
5831 {
5832 char element_name[64];
5833 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5834 child_name.assign(element_name);
5835 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5836 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5837 return element_type;
5838 }
5839 }
5840 }
5841 break;
5842
5843 case clang::Type::ConstantArray:
5844 case clang::Type::IncompleteArray:
5845 if (ignore_array_bounds || idx_is_valid)
5846 {
5847 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5848 if (array)
5849 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005850 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005851 if (element_type.GetCompleteType())
5852 {
5853 char element_name[64];
5854 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5855 child_name.assign(element_name);
5856 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5857 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5858 return element_type;
5859 }
5860 }
5861 }
5862 break;
5863
5864
5865 case clang::Type::Pointer:
5866 if (idx_is_valid)
5867 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005868 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005869
5870 // Don't dereference "void *" pointers
5871 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005872 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005873
5874 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5875 {
5876 child_is_deref_of_parent = false;
5877 bool tmp_child_is_deref_of_parent = false;
5878 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5879 idx,
5880 transparent_pointers,
5881 omit_empty_base_classes,
5882 ignore_array_bounds,
5883 child_name,
5884 child_byte_size,
5885 child_byte_offset,
5886 child_bitfield_bit_size,
5887 child_bitfield_bit_offset,
5888 child_is_base_class,
5889 tmp_child_is_deref_of_parent,
5890 valobj);
5891 }
5892 else
5893 {
5894 child_is_deref_of_parent = true;
5895
5896 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5897 if (parent_name)
5898 {
5899 child_name.assign(1, '*');
5900 child_name += parent_name;
5901 }
5902
5903 // We have a pointer to an simple type
5904 if (idx == 0)
5905 {
5906 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5907 child_byte_offset = 0;
5908 return pointee_clang_type;
5909 }
5910 }
5911 }
5912 break;
5913
5914 case clang::Type::LValueReference:
5915 case clang::Type::RValueReference:
5916 if (idx_is_valid)
5917 {
5918 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00005919 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005920 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5921 {
5922 child_is_deref_of_parent = false;
5923 bool tmp_child_is_deref_of_parent = false;
5924 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5925 idx,
5926 transparent_pointers,
5927 omit_empty_base_classes,
5928 ignore_array_bounds,
5929 child_name,
5930 child_byte_size,
5931 child_byte_offset,
5932 child_bitfield_bit_size,
5933 child_bitfield_bit_offset,
5934 child_is_base_class,
5935 tmp_child_is_deref_of_parent,
5936 valobj);
5937 }
5938 else
5939 {
5940 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5941 if (parent_name)
5942 {
5943 child_name.assign(1, '&');
5944 child_name += parent_name;
5945 }
5946
5947 // We have a pointer to an simple type
5948 if (idx == 0)
5949 {
5950 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5951 child_byte_offset = 0;
5952 return pointee_clang_type;
5953 }
5954 }
5955 }
5956 break;
5957
5958 case clang::Type::Typedef:
5959 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005960 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005961 return typedefed_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5962 idx,
5963 transparent_pointers,
5964 omit_empty_base_classes,
5965 ignore_array_bounds,
5966 child_name,
5967 child_byte_size,
5968 child_byte_offset,
5969 child_bitfield_bit_size,
5970 child_bitfield_bit_offset,
5971 child_is_base_class,
5972 child_is_deref_of_parent,
5973 valobj);
5974 }
5975 break;
5976
5977 case clang::Type::Elaborated:
5978 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005979 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005980 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5981 idx,
5982 transparent_pointers,
5983 omit_empty_base_classes,
5984 ignore_array_bounds,
5985 child_name,
5986 child_byte_size,
5987 child_byte_offset,
5988 child_bitfield_bit_size,
5989 child_bitfield_bit_offset,
5990 child_is_base_class,
5991 child_is_deref_of_parent,
5992 valobj);
5993 }
5994
5995 case clang::Type::Paren:
5996 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005997 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00005998 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5999 idx,
6000 transparent_pointers,
6001 omit_empty_base_classes,
6002 ignore_array_bounds,
6003 child_name,
6004 child_byte_size,
6005 child_byte_offset,
6006 child_bitfield_bit_size,
6007 child_bitfield_bit_offset,
6008 child_is_base_class,
6009 child_is_deref_of_parent,
6010 valobj);
6011 }
6012
6013
6014 default:
6015 break;
6016 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006017 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006018}
6019
6020static uint32_t
6021GetIndexForRecordBase
6022(
6023 const clang::RecordDecl *record_decl,
6024 const clang::CXXBaseSpecifier *base_spec,
6025 bool omit_empty_base_classes
6026 )
6027{
6028 uint32_t child_idx = 0;
6029
6030 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6031
6032 // const char *super_name = record_decl->getNameAsCString();
6033 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6034 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6035 //
6036 if (cxx_record_decl)
6037 {
6038 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6039 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6040 base_class != base_class_end;
6041 ++base_class)
6042 {
6043 if (omit_empty_base_classes)
6044 {
6045 if (BaseSpecifierIsEmpty (base_class))
6046 continue;
6047 }
6048
6049 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6050 // child_idx,
6051 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6052 //
6053 //
6054 if (base_class == base_spec)
6055 return child_idx;
6056 ++child_idx;
6057 }
6058 }
6059
6060 return UINT32_MAX;
6061}
6062
6063
6064static uint32_t
6065GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6066 clang::NamedDecl *canonical_decl,
6067 bool omit_empty_base_classes)
6068{
6069 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6070 omit_empty_base_classes);
6071
6072 clang::RecordDecl::field_iterator field, field_end;
6073 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6074 field != field_end;
6075 ++field, ++child_idx)
6076 {
6077 if (field->getCanonicalDecl() == canonical_decl)
6078 return child_idx;
6079 }
6080
6081 return UINT32_MAX;
6082}
6083
6084// Look for a child member (doesn't include base classes, but it does include
6085// their members) in the type hierarchy. Returns an index path into "clang_type"
6086// on how to reach the appropriate member.
6087//
6088// class A
6089// {
6090// public:
6091// int m_a;
6092// int m_b;
6093// };
6094//
6095// class B
6096// {
6097// };
6098//
6099// class C :
6100// public B,
6101// public A
6102// {
6103// };
6104//
6105// If we have a clang type that describes "class C", and we wanted to looked
6106// "m_b" in it:
6107//
6108// With omit_empty_base_classes == false we would get an integer array back with:
6109// { 1, 1 }
6110// The first index 1 is the child index for "class A" within class C
6111// The second index 1 is the child index for "m_b" within class A
6112//
6113// With omit_empty_base_classes == true we would get an integer array back with:
6114// { 0, 1 }
6115// 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)
6116// The second index 1 is the child index for "m_b" within class A
6117
6118size_t
6119ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6120 bool omit_empty_base_classes,
6121 std::vector<uint32_t>& child_indexes)
6122{
6123 if (type && name && name[0])
6124 {
6125 clang::QualType qual_type(GetCanonicalQualType(type));
6126 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6127 switch (type_class)
6128 {
6129 case clang::Type::Record:
6130 if (GetCompleteType(type))
6131 {
6132 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6133 const clang::RecordDecl *record_decl = record_type->getDecl();
6134
6135 assert(record_decl);
6136 uint32_t child_idx = 0;
6137
6138 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6139
6140 // Try and find a field that matches NAME
6141 clang::RecordDecl::field_iterator field, field_end;
6142 llvm::StringRef name_sref(name);
6143 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6144 field != field_end;
6145 ++field, ++child_idx)
6146 {
6147 llvm::StringRef field_name = field->getName();
6148 if (field_name.empty())
6149 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006150 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006151 child_indexes.push_back(child_idx);
6152 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6153 return child_indexes.size();
6154 child_indexes.pop_back();
6155
6156 }
6157 else if (field_name.equals (name_sref))
6158 {
6159 // We have to add on the number of base classes to this index!
6160 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6161 return child_indexes.size();
6162 }
6163 }
6164
6165 if (cxx_record_decl)
6166 {
6167 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6168
6169 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6170
6171 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6172 // Didn't find things easily, lets let clang do its thang...
6173 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6174 clang::DeclarationName decl_name(&ident_ref);
6175
6176 clang::CXXBasePaths paths;
6177 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6178 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6179 },
6180 paths))
6181 {
6182 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6183 for (path = paths.begin(); path != path_end; ++path)
6184 {
6185 const size_t num_path_elements = path->size();
6186 for (size_t e=0; e<num_path_elements; ++e)
6187 {
6188 clang::CXXBasePathElement elem = (*path)[e];
6189
6190 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6191 if (child_idx == UINT32_MAX)
6192 {
6193 child_indexes.clear();
6194 return 0;
6195 }
6196 else
6197 {
6198 child_indexes.push_back (child_idx);
6199 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6200 }
6201 }
6202 for (clang::NamedDecl *path_decl : path->Decls)
6203 {
6204 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6205 if (child_idx == UINT32_MAX)
6206 {
6207 child_indexes.clear();
6208 return 0;
6209 }
6210 else
6211 {
6212 child_indexes.push_back (child_idx);
6213 }
6214 }
6215 }
6216 return child_indexes.size();
6217 }
6218 }
6219
6220 }
6221 break;
6222
6223 case clang::Type::ObjCObject:
6224 case clang::Type::ObjCInterface:
6225 if (GetCompleteType(type))
6226 {
6227 llvm::StringRef name_sref(name);
6228 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6229 assert (objc_class_type);
6230 if (objc_class_type)
6231 {
6232 uint32_t child_idx = 0;
6233 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6234
6235 if (class_interface_decl)
6236 {
6237 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6238 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6239
6240 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6241 {
6242 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6243
6244 if (ivar_decl->getName().equals (name_sref))
6245 {
6246 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6247 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6248 ++child_idx;
6249
6250 child_indexes.push_back (child_idx);
6251 return child_indexes.size();
6252 }
6253 }
6254
6255 if (superclass_interface_decl)
6256 {
6257 // The super class index is always zero for ObjC classes,
6258 // so we push it onto the child indexes in case we find
6259 // an ivar in our superclass...
6260 child_indexes.push_back (0);
6261
Greg Claytona1e5dc82015-08-11 22:53:00 +00006262 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006263 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6264 omit_empty_base_classes,
6265 child_indexes))
6266 {
6267 // We did find an ivar in a superclass so just
6268 // return the results!
6269 return child_indexes.size();
6270 }
6271
6272 // We didn't find an ivar matching "name" in our
6273 // superclass, pop the superclass zero index that
6274 // we pushed on above.
6275 child_indexes.pop_back();
6276 }
6277 }
6278 }
6279 }
6280 break;
6281
6282 case clang::Type::ObjCObjectPointer:
6283 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006284 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006285 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6286 omit_empty_base_classes,
6287 child_indexes);
6288 }
6289 break;
6290
6291
6292 case clang::Type::ConstantArray:
6293 {
6294 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6295 // const uint64_t element_count = array->getSize().getLimitedValue();
6296 //
6297 // if (idx < element_count)
6298 // {
6299 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6300 //
6301 // char element_name[32];
6302 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6303 //
6304 // child_name.assign(element_name);
6305 // assert(field_type_info.first % 8 == 0);
6306 // child_byte_size = field_type_info.first / 8;
6307 // child_byte_offset = idx * child_byte_size;
6308 // return array->getElementType().getAsOpaquePtr();
6309 // }
6310 }
6311 break;
6312
6313 // case clang::Type::MemberPointerType:
6314 // {
6315 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6316 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6317 //
6318 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6319 // {
6320 // return GetIndexOfChildWithName (ast,
6321 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6322 // name);
6323 // }
6324 // }
6325 // break;
6326 //
6327 case clang::Type::LValueReference:
6328 case clang::Type::RValueReference:
6329 {
6330 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6331 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006332 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006333
6334 if (pointee_clang_type.IsAggregateType ())
6335 {
6336 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6337 omit_empty_base_classes,
6338 child_indexes);
6339 }
6340 }
6341 break;
6342
6343 case clang::Type::Pointer:
6344 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006345 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006346
6347 if (pointee_clang_type.IsAggregateType ())
6348 {
6349 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6350 omit_empty_base_classes,
6351 child_indexes);
6352 }
6353 }
6354 break;
6355
6356 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006357 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006358 omit_empty_base_classes,
6359 child_indexes);
6360
6361 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006362 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006363 omit_empty_base_classes,
6364 child_indexes);
6365
6366 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006367 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006368 omit_empty_base_classes,
6369 child_indexes);
6370
6371 default:
6372 break;
6373 }
6374 }
6375 return 0;
6376}
6377
6378
6379// Get the index of the child of "clang_type" whose name matches. This function
6380// doesn't descend into the children, but only looks one level deep and name
6381// matches can include base class names.
6382
6383uint32_t
6384ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6385{
6386 if (type && name && name[0])
6387 {
6388 clang::QualType qual_type(GetCanonicalQualType(type));
6389
6390 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6391
6392 switch (type_class)
6393 {
6394 case clang::Type::Record:
6395 if (GetCompleteType(type))
6396 {
6397 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6398 const clang::RecordDecl *record_decl = record_type->getDecl();
6399
6400 assert(record_decl);
6401 uint32_t child_idx = 0;
6402
6403 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6404
6405 if (cxx_record_decl)
6406 {
6407 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6408 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6409 base_class != base_class_end;
6410 ++base_class)
6411 {
6412 // Skip empty base classes
6413 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6414 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6415 continue;
6416
Greg Claytona1e5dc82015-08-11 22:53:00 +00006417 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006418 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6419 if (base_class_type_name.compare (name) == 0)
6420 return child_idx;
6421 ++child_idx;
6422 }
6423 }
6424
6425 // Try and find a field that matches NAME
6426 clang::RecordDecl::field_iterator field, field_end;
6427 llvm::StringRef name_sref(name);
6428 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6429 field != field_end;
6430 ++field, ++child_idx)
6431 {
6432 if (field->getName().equals (name_sref))
6433 return child_idx;
6434 }
6435
6436 }
6437 break;
6438
6439 case clang::Type::ObjCObject:
6440 case clang::Type::ObjCInterface:
6441 if (GetCompleteType(type))
6442 {
6443 llvm::StringRef name_sref(name);
6444 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6445 assert (objc_class_type);
6446 if (objc_class_type)
6447 {
6448 uint32_t child_idx = 0;
6449 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6450
6451 if (class_interface_decl)
6452 {
6453 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6454 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6455
6456 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6457 {
6458 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6459
6460 if (ivar_decl->getName().equals (name_sref))
6461 {
6462 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6463 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6464 ++child_idx;
6465
6466 return child_idx;
6467 }
6468 }
6469
6470 if (superclass_interface_decl)
6471 {
6472 if (superclass_interface_decl->getName().equals (name_sref))
6473 return 0;
6474 }
6475 }
6476 }
6477 }
6478 break;
6479
6480 case clang::Type::ObjCObjectPointer:
6481 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006482 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006483 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6484 }
6485 break;
6486
6487 case clang::Type::ConstantArray:
6488 {
6489 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6490 // const uint64_t element_count = array->getSize().getLimitedValue();
6491 //
6492 // if (idx < element_count)
6493 // {
6494 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6495 //
6496 // char element_name[32];
6497 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6498 //
6499 // child_name.assign(element_name);
6500 // assert(field_type_info.first % 8 == 0);
6501 // child_byte_size = field_type_info.first / 8;
6502 // child_byte_offset = idx * child_byte_size;
6503 // return array->getElementType().getAsOpaquePtr();
6504 // }
6505 }
6506 break;
6507
6508 // case clang::Type::MemberPointerType:
6509 // {
6510 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6511 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6512 //
6513 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6514 // {
6515 // return GetIndexOfChildWithName (ast,
6516 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6517 // name);
6518 // }
6519 // }
6520 // break;
6521 //
6522 case clang::Type::LValueReference:
6523 case clang::Type::RValueReference:
6524 {
6525 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006526 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006527
6528 if (pointee_type.IsAggregateType ())
6529 {
6530 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6531 }
6532 }
6533 break;
6534
6535 case clang::Type::Pointer:
6536 {
6537 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006538 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006539
6540 if (pointee_type.IsAggregateType ())
6541 {
6542 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6543 }
6544 else
6545 {
6546 // if (parent_name)
6547 // {
6548 // child_name.assign(1, '*');
6549 // child_name += parent_name;
6550 // }
6551 //
6552 // // We have a pointer to an simple type
6553 // if (idx == 0)
6554 // {
6555 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6556 // assert(clang_type_info.first % 8 == 0);
6557 // child_byte_size = clang_type_info.first / 8;
6558 // child_byte_offset = 0;
6559 // return pointee_type.getAsOpaquePtr();
6560 // }
6561 }
6562 }
6563 break;
6564
6565 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006566 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006567
6568 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006569 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006570
6571 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006572 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006573
6574 default:
6575 break;
6576 }
6577 }
6578 return UINT32_MAX;
6579}
6580
6581
6582size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006583ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006584{
6585 if (!type)
6586 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006587
6588 clang::QualType qual_type (GetCanonicalQualType(type));
6589 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6590 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006591 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006592 case clang::Type::Record:
6593 if (GetCompleteType(type))
6594 {
6595 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6596 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006597 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006598 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6599 if (template_decl)
6600 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006601 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006602 }
6603 break;
6604
6605 case clang::Type::Typedef:
6606 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6607
6608 case clang::Type::Elaborated:
6609 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6610
6611 case clang::Type::Paren:
6612 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6613
6614 default:
6615 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006616 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006617
Greg Claytond8d4a572015-08-11 21:38:15 +00006618 return 0;
6619}
6620
Greg Claytona1e5dc82015-08-11 22:53:00 +00006621CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006622ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006623{
6624 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006625 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006626
6627 clang::QualType qual_type (GetCanonicalQualType(type));
6628 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6629 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006630 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006631 case clang::Type::Record:
6632 if (GetCompleteType(type))
6633 {
6634 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6635 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006636 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006637 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6638 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006639 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006640 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6641 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006642 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006643 case clang::TemplateArgument::Null:
6644 kind = eTemplateArgumentKindNull;
6645 return CompilerType();
6646
6647 case clang::TemplateArgument::Type:
6648 kind = eTemplateArgumentKindType;
6649 return CompilerType(getASTContext(), template_arg.getAsType());
6650
6651 case clang::TemplateArgument::Declaration:
6652 kind = eTemplateArgumentKindDeclaration;
6653 return CompilerType();
6654
6655 case clang::TemplateArgument::Integral:
6656 kind = eTemplateArgumentKindIntegral;
6657 return CompilerType(getASTContext(), template_arg.getIntegralType());
6658
6659 case clang::TemplateArgument::Template:
6660 kind = eTemplateArgumentKindTemplate;
6661 return CompilerType();
6662
6663 case clang::TemplateArgument::TemplateExpansion:
6664 kind = eTemplateArgumentKindTemplateExpansion;
6665 return CompilerType();
6666
6667 case clang::TemplateArgument::Expression:
6668 kind = eTemplateArgumentKindExpression;
6669 return CompilerType();
6670
6671 case clang::TemplateArgument::Pack:
6672 kind = eTemplateArgumentKindPack;
6673 return CompilerType();
6674
6675 default:
6676 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6677 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006678 }
6679 }
6680 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006681 }
6682 break;
6683
6684 case clang::Type::Typedef:
6685 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6686
6687 case clang::Type::Elaborated:
6688 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6689
6690 case clang::Type::Paren:
6691 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6692
6693 default:
6694 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006695 }
6696 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006697 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006698}
6699
6700static bool
6701IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6702{
6703 if (name == nullptr || name[0] == '\0')
6704 return false;
6705
6706#define OPERATOR_PREFIX "operator"
6707#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6708
6709 const char *post_op_name = nullptr;
6710
6711 bool no_space = true;
6712
6713 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6714 return false;
6715
6716 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6717
6718 if (post_op_name[0] == ' ')
6719 {
6720 post_op_name++;
6721 no_space = false;
6722 }
6723
6724#undef OPERATOR_PREFIX
6725#undef OPERATOR_PREFIX_LENGTH
6726
6727 // This is an operator, set the overloaded operator kind to invalid
6728 // in case this is a conversion operator...
6729 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6730
6731 switch (post_op_name[0])
6732 {
6733 default:
6734 if (no_space)
6735 return false;
6736 break;
6737 case 'n':
6738 if (no_space)
6739 return false;
6740 if (strcmp (post_op_name, "new") == 0)
6741 op_kind = clang::OO_New;
6742 else if (strcmp (post_op_name, "new[]") == 0)
6743 op_kind = clang::OO_Array_New;
6744 break;
6745
6746 case 'd':
6747 if (no_space)
6748 return false;
6749 if (strcmp (post_op_name, "delete") == 0)
6750 op_kind = clang::OO_Delete;
6751 else if (strcmp (post_op_name, "delete[]") == 0)
6752 op_kind = clang::OO_Array_Delete;
6753 break;
6754
6755 case '+':
6756 if (post_op_name[1] == '\0')
6757 op_kind = clang::OO_Plus;
6758 else if (post_op_name[2] == '\0')
6759 {
6760 if (post_op_name[1] == '=')
6761 op_kind = clang::OO_PlusEqual;
6762 else if (post_op_name[1] == '+')
6763 op_kind = clang::OO_PlusPlus;
6764 }
6765 break;
6766
6767 case '-':
6768 if (post_op_name[1] == '\0')
6769 op_kind = clang::OO_Minus;
6770 else if (post_op_name[2] == '\0')
6771 {
6772 switch (post_op_name[1])
6773 {
6774 case '=': op_kind = clang::OO_MinusEqual; break;
6775 case '-': op_kind = clang::OO_MinusMinus; break;
6776 case '>': op_kind = clang::OO_Arrow; break;
6777 }
6778 }
6779 else if (post_op_name[3] == '\0')
6780 {
6781 if (post_op_name[2] == '*')
6782 op_kind = clang::OO_ArrowStar; break;
6783 }
6784 break;
6785
6786 case '*':
6787 if (post_op_name[1] == '\0')
6788 op_kind = clang::OO_Star;
6789 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6790 op_kind = clang::OO_StarEqual;
6791 break;
6792
6793 case '/':
6794 if (post_op_name[1] == '\0')
6795 op_kind = clang::OO_Slash;
6796 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6797 op_kind = clang::OO_SlashEqual;
6798 break;
6799
6800 case '%':
6801 if (post_op_name[1] == '\0')
6802 op_kind = clang::OO_Percent;
6803 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6804 op_kind = clang::OO_PercentEqual;
6805 break;
6806
6807
6808 case '^':
6809 if (post_op_name[1] == '\0')
6810 op_kind = clang::OO_Caret;
6811 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6812 op_kind = clang::OO_CaretEqual;
6813 break;
6814
6815 case '&':
6816 if (post_op_name[1] == '\0')
6817 op_kind = clang::OO_Amp;
6818 else if (post_op_name[2] == '\0')
6819 {
6820 switch (post_op_name[1])
6821 {
6822 case '=': op_kind = clang::OO_AmpEqual; break;
6823 case '&': op_kind = clang::OO_AmpAmp; break;
6824 }
6825 }
6826 break;
6827
6828 case '|':
6829 if (post_op_name[1] == '\0')
6830 op_kind = clang::OO_Pipe;
6831 else if (post_op_name[2] == '\0')
6832 {
6833 switch (post_op_name[1])
6834 {
6835 case '=': op_kind = clang::OO_PipeEqual; break;
6836 case '|': op_kind = clang::OO_PipePipe; break;
6837 }
6838 }
6839 break;
6840
6841 case '~':
6842 if (post_op_name[1] == '\0')
6843 op_kind = clang::OO_Tilde;
6844 break;
6845
6846 case '!':
6847 if (post_op_name[1] == '\0')
6848 op_kind = clang::OO_Exclaim;
6849 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6850 op_kind = clang::OO_ExclaimEqual;
6851 break;
6852
6853 case '=':
6854 if (post_op_name[1] == '\0')
6855 op_kind = clang::OO_Equal;
6856 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6857 op_kind = clang::OO_EqualEqual;
6858 break;
6859
6860 case '<':
6861 if (post_op_name[1] == '\0')
6862 op_kind = clang::OO_Less;
6863 else if (post_op_name[2] == '\0')
6864 {
6865 switch (post_op_name[1])
6866 {
6867 case '<': op_kind = clang::OO_LessLess; break;
6868 case '=': op_kind = clang::OO_LessEqual; break;
6869 }
6870 }
6871 else if (post_op_name[3] == '\0')
6872 {
6873 if (post_op_name[2] == '=')
6874 op_kind = clang::OO_LessLessEqual;
6875 }
6876 break;
6877
6878 case '>':
6879 if (post_op_name[1] == '\0')
6880 op_kind = clang::OO_Greater;
6881 else if (post_op_name[2] == '\0')
6882 {
6883 switch (post_op_name[1])
6884 {
6885 case '>': op_kind = clang::OO_GreaterGreater; break;
6886 case '=': op_kind = clang::OO_GreaterEqual; break;
6887 }
6888 }
6889 else if (post_op_name[1] == '>' &&
6890 post_op_name[2] == '=' &&
6891 post_op_name[3] == '\0')
6892 {
6893 op_kind = clang::OO_GreaterGreaterEqual;
6894 }
6895 break;
6896
6897 case ',':
6898 if (post_op_name[1] == '\0')
6899 op_kind = clang::OO_Comma;
6900 break;
6901
6902 case '(':
6903 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
6904 op_kind = clang::OO_Call;
6905 break;
6906
6907 case '[':
6908 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
6909 op_kind = clang::OO_Subscript;
6910 break;
6911 }
6912
6913 return true;
6914}
6915
6916clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006917ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006918{
6919 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
6920 if (enutype)
6921 return enutype->getDecl();
6922 return NULL;
6923}
6924
6925clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006926ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006927{
6928 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
6929 if (record_type)
6930 return record_type->getDecl();
6931 return nullptr;
6932}
6933
6934clang::CXXRecordDecl *
6935ClangASTContext::GetAsCXXRecordDecl (void* type)
6936{
6937 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
6938}
6939
6940clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006941ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006942{
6943 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
6944 if (objc_class_type)
6945 return objc_class_type->getInterface();
6946 return nullptr;
6947}
6948
6949clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006950ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
6951 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00006952 AccessType access,
6953 uint32_t bitfield_bit_size)
6954{
6955 if (!type.IsValid() || !field_clang_type.IsValid())
6956 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00006957 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00006958 if (!ast)
6959 return nullptr;
6960 clang::ASTContext* clang_ast = ast->getASTContext();
6961
6962 clang::FieldDecl *field = nullptr;
6963
6964 clang::Expr *bit_width = nullptr;
6965 if (bitfield_bit_size != 0)
6966 {
6967 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
6968 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
6969 }
6970
6971 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
6972 if (record_decl)
6973 {
6974 field = clang::FieldDecl::Create (*clang_ast,
6975 record_decl,
6976 clang::SourceLocation(),
6977 clang::SourceLocation(),
6978 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
6979 GetQualType(field_clang_type), // Field type
6980 nullptr, // TInfo *
6981 bit_width, // BitWidth
6982 false, // Mutable
6983 clang::ICIS_NoInit); // HasInit
6984
6985 if (!name)
6986 {
6987 // Determine whether this field corresponds to an anonymous
6988 // struct or union.
6989 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
6990 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
6991 if (!Rec->getDeclName()) {
6992 Rec->setAnonymousStructOrUnion(true);
6993 field->setImplicit();
6994
6995 }
6996 }
6997 }
6998
6999 if (field)
7000 {
7001 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7002
7003 record_decl->addDecl(field);
7004
7005#ifdef LLDB_CONFIGURATION_DEBUG
7006 VerifyDecl(field);
7007#endif
7008 }
7009 }
7010 else
7011 {
7012 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7013
7014 if (class_interface_decl)
7015 {
7016 const bool is_synthesized = false;
7017
7018 field_clang_type.GetCompleteType();
7019
7020 field = clang::ObjCIvarDecl::Create (*clang_ast,
7021 class_interface_decl,
7022 clang::SourceLocation(),
7023 clang::SourceLocation(),
7024 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7025 GetQualType(field_clang_type), // Field type
7026 nullptr, // TypeSourceInfo *
7027 ConvertAccessTypeToObjCIvarAccessControl (access),
7028 bit_width,
7029 is_synthesized);
7030
7031 if (field)
7032 {
7033 class_interface_decl->addDecl(field);
7034
7035#ifdef LLDB_CONFIGURATION_DEBUG
7036 VerifyDecl(field);
7037#endif
7038 }
7039 }
7040 }
7041 return field;
7042}
7043
7044void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007045ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007046{
Greg Claytonf73034f2015-09-08 18:15:05 +00007047 if (!type)
7048 return;
7049
7050 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007051 if (!ast)
7052 return;
7053
7054 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7055
7056 if (!record_decl)
7057 return;
7058
7059 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7060
7061 IndirectFieldVector indirect_fields;
7062 clang::RecordDecl::field_iterator field_pos;
7063 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7064 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7065 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7066 {
7067 if (field_pos->isAnonymousStructOrUnion())
7068 {
7069 clang::QualType field_qual_type = field_pos->getType();
7070
7071 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7072
7073 if (!field_record_type)
7074 continue;
7075
7076 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7077
7078 if (!field_record_decl)
7079 continue;
7080
7081 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7082 di != de;
7083 ++di)
7084 {
7085 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7086 {
7087 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7088 chain[0] = *field_pos;
7089 chain[1] = nested_field_decl;
7090 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7091 record_decl,
7092 clang::SourceLocation(),
7093 nested_field_decl->getIdentifier(),
7094 nested_field_decl->getType(),
7095 chain,
7096 2);
7097
7098 indirect_field->setImplicit();
7099
7100 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7101 nested_field_decl->getAccess()));
7102
7103 indirect_fields.push_back(indirect_field);
7104 }
7105 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7106 {
7107 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7108 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7109 chain[0] = *field_pos;
7110
7111 int chain_index = 1;
7112 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7113 nce = nested_indirect_field_decl->chain_end();
7114 nci < nce;
7115 ++nci)
7116 {
7117 chain[chain_index] = *nci;
7118 chain_index++;
7119 }
7120
7121 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7122 record_decl,
7123 clang::SourceLocation(),
7124 nested_indirect_field_decl->getIdentifier(),
7125 nested_indirect_field_decl->getType(),
7126 chain,
7127 nested_chain_size + 1);
7128
7129 indirect_field->setImplicit();
7130
7131 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7132 nested_indirect_field_decl->getAccess()));
7133
7134 indirect_fields.push_back(indirect_field);
7135 }
7136 }
7137 }
7138 }
7139
7140 // Check the last field to see if it has an incomplete array type as its
7141 // last member and if it does, the tell the record decl about it
7142 if (last_field_pos != field_end_pos)
7143 {
7144 if (last_field_pos->getType()->isIncompleteArrayType())
7145 record_decl->hasFlexibleArrayMember();
7146 }
7147
7148 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7149 ifi < ife;
7150 ++ifi)
7151 {
7152 record_decl->addDecl(*ifi);
7153 }
7154}
7155
7156void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007157ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007158{
Greg Claytonf73034f2015-09-08 18:15:05 +00007159 if (type)
7160 {
7161 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7162 if (ast)
7163 {
7164 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007165
Greg Claytonf73034f2015-09-08 18:15:05 +00007166 if (!record_decl)
7167 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007168
Greg Claytonf73034f2015-09-08 18:15:05 +00007169 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7170 }
7171 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007172}
7173
7174clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007175ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7176 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007177 AccessType access)
7178{
7179 clang::VarDecl *var_decl = nullptr;
7180
7181 if (!type.IsValid() || !var_type.IsValid())
7182 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007183 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007184 if (!ast)
7185 return nullptr;
7186
7187 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7188 if (record_decl)
7189 {
7190 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7191 record_decl, // DeclContext *
7192 clang::SourceLocation(), // clang::SourceLocation StartLoc
7193 clang::SourceLocation(), // clang::SourceLocation IdLoc
7194 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7195 GetQualType(var_type), // Variable clang::QualType
7196 nullptr, // TypeSourceInfo *
7197 clang::SC_Static); // StorageClass
7198 if (var_decl)
7199 {
7200 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7201 record_decl->addDecl(var_decl);
7202
7203#ifdef LLDB_CONFIGURATION_DEBUG
7204 VerifyDecl(var_decl);
7205#endif
7206 }
7207 }
7208 return var_decl;
7209}
7210
7211
7212clang::CXXMethodDecl *
7213ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007214 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007215 lldb::AccessType access,
7216 bool is_virtual,
7217 bool is_static,
7218 bool is_inline,
7219 bool is_explicit,
7220 bool is_attr_used,
7221 bool is_artificial)
7222{
7223 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7224 return nullptr;
7225
7226 clang::QualType record_qual_type(GetCanonicalQualType(type));
7227
7228 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7229
7230 if (cxx_record_decl == nullptr)
7231 return nullptr;
7232
7233 clang::QualType method_qual_type (GetQualType(method_clang_type));
7234
7235 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7236
7237 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7238
7239 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7240
7241 if (function_type == nullptr)
7242 return nullptr;
7243
7244 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7245
7246 if (!method_function_prototype)
7247 return nullptr;
7248
7249 unsigned int num_params = method_function_prototype->getNumParams();
7250
7251 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7252 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7253
7254 if (is_artificial)
7255 return nullptr; // skip everything artificial
7256
7257 if (name[0] == '~')
7258 {
7259 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7260 cxx_record_decl,
7261 clang::SourceLocation(),
7262 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7263 method_qual_type,
7264 nullptr,
7265 is_inline,
7266 is_artificial);
7267 cxx_method_decl = cxx_dtor_decl;
7268 }
7269 else if (decl_name == cxx_record_decl->getDeclName())
7270 {
7271 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7272 cxx_record_decl,
7273 clang::SourceLocation(),
7274 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7275 method_qual_type,
7276 nullptr, // TypeSourceInfo *
7277 is_explicit,
7278 is_inline,
7279 is_artificial,
7280 false /*is_constexpr*/);
7281 cxx_method_decl = cxx_ctor_decl;
7282 }
7283 else
7284 {
7285 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7286 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7287
7288 if (IsOperator (name, op_kind))
7289 {
7290 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7291 {
7292 // Check the number of operator parameters. Sometimes we have
7293 // seen bad DWARF that doesn't correctly describe operators and
7294 // if we try to create a method and add it to the class, clang
7295 // will assert and crash, so we need to make sure things are
7296 // acceptable.
7297 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7298 return nullptr;
7299 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7300 cxx_record_decl,
7301 clang::SourceLocation(),
7302 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7303 method_qual_type,
7304 nullptr, // TypeSourceInfo *
7305 SC,
7306 is_inline,
7307 false /*is_constexpr*/,
7308 clang::SourceLocation());
7309 }
7310 else if (num_params == 0)
7311 {
7312 // Conversion operators don't take params...
7313 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7314 cxx_record_decl,
7315 clang::SourceLocation(),
7316 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7317 method_qual_type,
7318 nullptr, // TypeSourceInfo *
7319 is_inline,
7320 is_explicit,
7321 false /*is_constexpr*/,
7322 clang::SourceLocation());
7323 }
7324 }
7325
7326 if (cxx_method_decl == nullptr)
7327 {
7328 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7329 cxx_record_decl,
7330 clang::SourceLocation(),
7331 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7332 method_qual_type,
7333 nullptr, // TypeSourceInfo *
7334 SC,
7335 is_inline,
7336 false /*is_constexpr*/,
7337 clang::SourceLocation());
7338 }
7339 }
7340
7341 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7342
7343 cxx_method_decl->setAccess (access_specifier);
7344 cxx_method_decl->setVirtualAsWritten (is_virtual);
7345
7346 if (is_attr_used)
7347 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7348
7349 // Populate the method decl with parameter decls
7350
7351 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7352
7353 for (unsigned param_index = 0;
7354 param_index < num_params;
7355 ++param_index)
7356 {
7357 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7358 cxx_method_decl,
7359 clang::SourceLocation(),
7360 clang::SourceLocation(),
7361 nullptr, // anonymous
7362 method_function_prototype->getParamType(param_index),
7363 nullptr,
7364 clang::SC_None,
7365 nullptr));
7366 }
7367
7368 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7369
7370 cxx_record_decl->addDecl (cxx_method_decl);
7371
7372 // Sometimes the debug info will mention a constructor (default/copy/move),
7373 // destructor, or assignment operator (copy/move) but there won't be any
7374 // version of this in the code. So we check if the function was artificially
7375 // generated and if it is trivial and this lets the compiler/backend know
7376 // that it can inline the IR for these when it needs to and we can avoid a
7377 // "missing function" error when running expressions.
7378
7379 if (is_artificial)
7380 {
7381 if (cxx_ctor_decl &&
7382 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7383 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7384 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7385 {
7386 cxx_ctor_decl->setDefaulted();
7387 cxx_ctor_decl->setTrivial(true);
7388 }
7389 else if (cxx_dtor_decl)
7390 {
7391 if (cxx_record_decl->hasTrivialDestructor())
7392 {
7393 cxx_dtor_decl->setDefaulted();
7394 cxx_dtor_decl->setTrivial(true);
7395 }
7396 }
7397 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7398 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7399 {
7400 cxx_method_decl->setDefaulted();
7401 cxx_method_decl->setTrivial(true);
7402 }
7403 }
7404
7405#ifdef LLDB_CONFIGURATION_DEBUG
7406 VerifyDecl(cxx_method_decl);
7407#endif
7408
7409 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7410 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7411 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7412 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7413 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7414 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7415 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7416 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7417 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7418 return cxx_method_decl;
7419}
7420
7421
7422#pragma mark C++ Base Classes
7423
7424clang::CXXBaseSpecifier *
7425ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7426{
7427 if (type)
7428 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7429 is_virtual,
7430 base_of_class,
7431 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7432 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7433 clang::SourceLocation());
7434 return nullptr;
7435}
7436
7437void
7438ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7439{
7440 for (unsigned i=0; i<num_base_classes; ++i)
7441 {
7442 delete base_classes[i];
7443 base_classes[i] = nullptr;
7444 }
7445}
7446
7447bool
7448ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7449 unsigned num_base_classes)
7450{
7451 if (type)
7452 {
7453 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7454 if (cxx_record_decl)
7455 {
7456 cxx_record_decl->setBases(base_classes, num_base_classes);
7457 return true;
7458 }
7459 }
7460 return false;
7461}
7462
7463bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007464ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007465{
Greg Claytonf73034f2015-09-08 18:15:05 +00007466 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007467 if (!ast)
7468 return false;
7469 clang::ASTContext* clang_ast = ast->getASTContext();
7470
7471 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7472 {
7473 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7474 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7475 if (class_interface_decl && super_interface_decl)
7476 {
7477 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7478 return true;
7479 }
7480 }
7481 return false;
7482}
7483
7484bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007485ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007486 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007487 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007488 clang::ObjCIvarDecl *ivar_decl,
7489 const char *property_setter_name,
7490 const char *property_getter_name,
7491 uint32_t property_attributes,
7492 ClangASTMetadata *metadata)
7493{
7494 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7495 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007496 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007497 if (!ast)
7498 return false;
7499 clang::ASTContext* clang_ast = ast->getASTContext();
7500
7501 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7502
7503 if (class_interface_decl)
7504 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007505 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007506
7507 if (property_clang_type.IsValid())
7508 property_clang_type_to_access = property_clang_type;
7509 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007510 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007511
7512 if (class_interface_decl && property_clang_type_to_access.IsValid())
7513 {
7514 clang::TypeSourceInfo *prop_type_source;
7515 if (ivar_decl)
7516 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7517 else
7518 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7519
7520 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7521 class_interface_decl,
7522 clang::SourceLocation(), // Source Location
7523 &clang_ast->Idents.get(property_name),
7524 clang::SourceLocation(), //Source Location for AT
7525 clang::SourceLocation(), //Source location for (
7526 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7527 prop_type_source);
7528
7529 if (property_decl)
7530 {
7531 if (metadata)
7532 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7533
7534 class_interface_decl->addDecl (property_decl);
7535
7536 clang::Selector setter_sel, getter_sel;
7537
7538 if (property_setter_name != nullptr)
7539 {
7540 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7541 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7542 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7543 }
7544 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7545 {
7546 std::string setter_sel_string("set");
7547 setter_sel_string.push_back(::toupper(property_name[0]));
7548 setter_sel_string.append(&property_name[1]);
7549 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7550 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7551 }
7552 property_decl->setSetterName(setter_sel);
7553 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7554
7555 if (property_getter_name != nullptr)
7556 {
7557 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7558 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7559 }
7560 else
7561 {
7562 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7563 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7564 }
7565 property_decl->setGetterName(getter_sel);
7566 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7567
7568 if (ivar_decl)
7569 property_decl->setPropertyIvarDecl (ivar_decl);
7570
7571 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7572 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7573 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7574 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7575 if (property_attributes & DW_APPLE_PROPERTY_assign)
7576 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7577 if (property_attributes & DW_APPLE_PROPERTY_retain)
7578 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7579 if (property_attributes & DW_APPLE_PROPERTY_copy)
7580 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7581 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7582 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7583
7584 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7585 {
7586 const bool isInstance = true;
7587 const bool isVariadic = false;
7588 const bool isSynthesized = false;
7589 const bool isImplicitlyDeclared = true;
7590 const bool isDefined = false;
7591 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7592 const bool HasRelatedResultType = false;
7593
7594 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7595 clang::SourceLocation(),
7596 clang::SourceLocation(),
7597 getter_sel,
7598 GetQualType(property_clang_type_to_access),
7599 nullptr,
7600 class_interface_decl,
7601 isInstance,
7602 isVariadic,
7603 isSynthesized,
7604 isImplicitlyDeclared,
7605 isDefined,
7606 impControl,
7607 HasRelatedResultType);
7608
7609 if (getter && metadata)
7610 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7611
7612 if (getter)
7613 {
7614 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7615
7616 class_interface_decl->addDecl(getter);
7617 }
7618 }
7619
7620 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7621 {
7622 clang::QualType result_type = clang_ast->VoidTy;
7623
7624 const bool isInstance = true;
7625 const bool isVariadic = false;
7626 const bool isSynthesized = false;
7627 const bool isImplicitlyDeclared = true;
7628 const bool isDefined = false;
7629 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7630 const bool HasRelatedResultType = false;
7631
7632 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7633 clang::SourceLocation(),
7634 clang::SourceLocation(),
7635 setter_sel,
7636 result_type,
7637 nullptr,
7638 class_interface_decl,
7639 isInstance,
7640 isVariadic,
7641 isSynthesized,
7642 isImplicitlyDeclared,
7643 isDefined,
7644 impControl,
7645 HasRelatedResultType);
7646
7647 if (setter && metadata)
7648 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7649
7650 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7651
7652 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7653 setter,
7654 clang::SourceLocation(),
7655 clang::SourceLocation(),
7656 nullptr, // anonymous
7657 GetQualType(property_clang_type_to_access),
7658 nullptr,
7659 clang::SC_Auto,
7660 nullptr));
7661
7662 if (setter)
7663 {
7664 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7665
7666 class_interface_decl->addDecl(setter);
7667 }
7668 }
7669
7670 return true;
7671 }
7672 }
7673 }
7674 return false;
7675}
7676
7677bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007678ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007679{
7680 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7681 if (class_interface_decl)
7682 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7683 return false;
7684}
7685
7686
7687clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007688ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007689 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007690 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007691 lldb::AccessType access,
7692 bool is_artificial)
7693{
7694 if (!type || !method_clang_type.IsValid())
7695 return nullptr;
7696
7697 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7698
7699 if (class_interface_decl == nullptr)
7700 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007701 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7702 if (lldb_ast == nullptr)
7703 return nullptr;
7704 clang::ASTContext *ast = lldb_ast->getASTContext();
7705
Greg Claytond8d4a572015-08-11 21:38:15 +00007706 const char *selector_start = ::strchr (name, ' ');
7707 if (selector_start == nullptr)
7708 return nullptr;
7709
7710 selector_start++;
7711 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7712
7713 size_t len = 0;
7714 const char *start;
7715 //printf ("name = '%s'\n", name);
7716
7717 unsigned num_selectors_with_args = 0;
7718 for (start = selector_start;
7719 start && *start != '\0' && *start != ']';
7720 start += len)
7721 {
7722 len = ::strcspn(start, ":]");
7723 bool has_arg = (start[len] == ':');
7724 if (has_arg)
7725 ++num_selectors_with_args;
7726 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7727 if (has_arg)
7728 len += 1;
7729 }
7730
7731
7732 if (selector_idents.size() == 0)
7733 return nullptr;
7734
7735 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7736 selector_idents.data());
7737
7738 clang::QualType method_qual_type (GetQualType(method_clang_type));
7739
7740 // Populate the method decl with parameter decls
7741 const clang::Type *method_type(method_qual_type.getTypePtr());
7742
7743 if (method_type == nullptr)
7744 return nullptr;
7745
7746 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7747
7748 if (!method_function_prototype)
7749 return nullptr;
7750
7751
7752 bool is_variadic = false;
7753 bool is_synthesized = false;
7754 bool is_defined = false;
7755 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7756
7757 const unsigned num_args = method_function_prototype->getNumParams();
7758
7759 if (num_args != num_selectors_with_args)
7760 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7761
7762 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7763 clang::SourceLocation(), // beginLoc,
7764 clang::SourceLocation(), // endLoc,
7765 method_selector,
7766 method_function_prototype->getReturnType(),
7767 nullptr, // TypeSourceInfo *ResultTInfo,
7768 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7769 name[0] == '-',
7770 is_variadic,
7771 is_synthesized,
7772 true, // is_implicitly_declared; we force this to true because we don't have source locations
7773 is_defined,
7774 imp_control,
7775 false /*has_related_result_type*/);
7776
7777
7778 if (objc_method_decl == nullptr)
7779 return nullptr;
7780
7781 if (num_args > 0)
7782 {
7783 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7784
7785 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7786 {
7787 params.push_back (clang::ParmVarDecl::Create (*ast,
7788 objc_method_decl,
7789 clang::SourceLocation(),
7790 clang::SourceLocation(),
7791 nullptr, // anonymous
7792 method_function_prototype->getParamType(param_index),
7793 nullptr,
7794 clang::SC_Auto,
7795 nullptr));
7796 }
7797
7798 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7799 }
7800
7801 class_interface_decl->addDecl (objc_method_decl);
7802
7803#ifdef LLDB_CONFIGURATION_DEBUG
7804 VerifyDecl(objc_method_decl);
7805#endif
7806
7807 return objc_method_decl;
7808}
7809
7810bool
7811ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7812{
7813 if (!type)
7814 return false;
7815
7816 clang::QualType qual_type (GetCanonicalQualType(type));
7817
7818 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7819 switch (type_class)
7820 {
7821 case clang::Type::Record:
7822 {
7823 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7824 if (cxx_record_decl)
7825 {
7826 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7827 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7828 return true;
7829 }
7830 }
7831 break;
7832
7833 case clang::Type::Enum:
7834 {
7835 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7836 if (enum_decl)
7837 {
7838 enum_decl->setHasExternalLexicalStorage (has_extern);
7839 enum_decl->setHasExternalVisibleStorage (has_extern);
7840 return true;
7841 }
7842 }
7843 break;
7844
7845 case clang::Type::ObjCObject:
7846 case clang::Type::ObjCInterface:
7847 {
7848 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7849 assert (objc_class_type);
7850 if (objc_class_type)
7851 {
7852 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7853
7854 if (class_interface_decl)
7855 {
7856 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7857 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7858 return true;
7859 }
7860 }
7861 }
7862 break;
7863
7864 case clang::Type::Typedef:
7865 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7866
7867 case clang::Type::Elaborated:
7868 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7869
7870 case clang::Type::Paren:
7871 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7872
7873 default:
7874 break;
7875 }
7876 return false;
7877}
7878
7879
7880#pragma mark TagDecl
7881
7882bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007883ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007884{
7885 if (type)
7886 {
7887
7888 clang::QualType qual_type (GetQualType(type));
7889 const clang::Type *t = qual_type.getTypePtr();
7890 if (t)
7891 {
7892 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7893 if (tag_type)
7894 {
7895 clang::TagDecl *tag_decl = tag_type->getDecl();
7896 if (tag_decl)
7897 {
7898 tag_decl->startDefinition();
7899 return true;
7900 }
7901 }
7902
7903 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
7904 if (object_type)
7905 {
7906 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
7907 if (interface_decl)
7908 {
7909 interface_decl->startDefinition();
7910 return true;
7911 }
7912 }
7913 }
7914 }
7915 return false;
7916}
7917
7918bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007919ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007920{
7921 if (type)
7922 {
7923 clang::QualType qual_type (GetQualType(type));
7924 if (qual_type.isNull())
7925 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007926 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7927 if (lldb_ast == nullptr)
7928 return false;
7929 clang::ASTContext *ast = lldb_ast->getASTContext();
7930
Greg Claytond8d4a572015-08-11 21:38:15 +00007931 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7932
7933 if (cxx_record_decl)
7934 {
7935 cxx_record_decl->completeDefinition();
7936
7937 return true;
7938 }
7939
7940 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
7941
7942 if (enutype)
7943 {
7944 clang::EnumDecl *enum_decl = enutype->getDecl();
7945
7946 if (enum_decl)
7947 {
7948 /// TODO This really needs to be fixed.
7949
7950 unsigned NumPositiveBits = 1;
7951 unsigned NumNegativeBits = 0;
7952
7953 clang::QualType promotion_qual_type;
7954 // If the enum integer type is less than an integer in bit width,
7955 // then we must promote it to an integer size.
7956 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
7957 {
7958 if (enum_decl->getIntegerType()->isSignedIntegerType())
7959 promotion_qual_type = ast->IntTy;
7960 else
7961 promotion_qual_type = ast->UnsignedIntTy;
7962 }
7963 else
7964 promotion_qual_type = enum_decl->getIntegerType();
7965
7966 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
7967 return true;
7968 }
7969 }
7970 }
7971 return false;
7972}
7973
Greg Claytond8d4a572015-08-11 21:38:15 +00007974bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00007975ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
7976 const CompilerType &enumerator_clang_type,
7977 const Declaration &decl,
7978 const char *name,
7979 int64_t enum_value,
7980 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00007981{
7982 if (type && enumerator_clang_type.IsValid() && name && name[0])
7983 {
7984 clang::QualType enum_qual_type (GetCanonicalQualType(type));
7985
7986 bool is_signed = false;
7987 enumerator_clang_type.IsIntegerType (is_signed);
7988 const clang::Type *clang_type = enum_qual_type.getTypePtr();
7989 if (clang_type)
7990 {
7991 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
7992
7993 if (enutype)
7994 {
7995 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
7996 enum_llvm_apsint = enum_value;
7997 clang::EnumConstantDecl *enumerator_decl =
7998 clang::EnumConstantDecl::Create (*getASTContext(),
7999 enutype->getDecl(),
8000 clang::SourceLocation(),
8001 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8002 GetQualType(enumerator_clang_type),
8003 nullptr,
8004 enum_llvm_apsint);
8005
8006 if (enumerator_decl)
8007 {
8008 enutype->getDecl()->addDecl(enumerator_decl);
8009
8010#ifdef LLDB_CONFIGURATION_DEBUG
8011 VerifyDecl(enumerator_decl);
8012#endif
8013
8014 return true;
8015 }
8016 }
8017 }
8018 }
8019 return false;
8020}
8021
Greg Claytona1e5dc82015-08-11 22:53:00 +00008022CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008023ClangASTContext::GetEnumerationIntegerType (void* type)
8024{
8025 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8026 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8027 if (clang_type)
8028 {
8029 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8030 if (enutype)
8031 {
8032 clang::EnumDecl *enum_decl = enutype->getDecl();
8033 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008034 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008035 }
8036 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008037 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008038}
8039
Greg Claytona1e5dc82015-08-11 22:53:00 +00008040CompilerType
8041ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008042{
8043 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8044 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008045 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008046 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008047 return CompilerType();
8048 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008049 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8050 GetQualType(type).getTypePtr()));
8051 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008052 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008053}
8054
8055
8056size_t
8057ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8058{
8059 if (type)
8060 {
8061 clang::QualType qual_type (GetCanonicalQualType(type));
8062 uint32_t count = 0;
8063 bool is_complex = false;
8064 if (IsFloatingPointType (type, count, is_complex))
8065 {
8066 // TODO: handle complex and vector types
8067 if (count != 1)
8068 return false;
8069
8070 llvm::StringRef s_sref(s);
8071 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8072
8073 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8074 const uint64_t byte_size = bit_size / 8;
8075 if (dst_size >= byte_size)
8076 {
8077 if (bit_size == sizeof(float)*8)
8078 {
8079 float float32 = ap_float.convertToFloat();
8080 ::memcpy (dst, &float32, byte_size);
8081 return byte_size;
8082 }
8083 else if (bit_size >= 64)
8084 {
8085 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8086 ::memcpy (dst, ap_int.getRawData(), byte_size);
8087 return byte_size;
8088 }
8089 }
8090 }
8091 }
8092 return 0;
8093}
8094
8095
8096
8097//----------------------------------------------------------------------
8098// Dumping types
8099//----------------------------------------------------------------------
8100#define DEPTH_INCREMENT 2
8101
8102void
8103ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8104 Stream *s,
8105 lldb::Format format,
8106 const lldb_private::DataExtractor &data,
8107 lldb::offset_t data_byte_offset,
8108 size_t data_byte_size,
8109 uint32_t bitfield_bit_size,
8110 uint32_t bitfield_bit_offset,
8111 bool show_types,
8112 bool show_summary,
8113 bool verbose,
8114 uint32_t depth)
8115{
8116 if (!type)
8117 return;
8118
8119 clang::QualType qual_type(GetQualType(type));
8120 switch (qual_type->getTypeClass())
8121 {
8122 case clang::Type::Record:
8123 if (GetCompleteType(type))
8124 {
8125 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8126 const clang::RecordDecl *record_decl = record_type->getDecl();
8127 assert(record_decl);
8128 uint32_t field_bit_offset = 0;
8129 uint32_t field_byte_offset = 0;
8130 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8131 uint32_t child_idx = 0;
8132
8133 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8134 if (cxx_record_decl)
8135 {
8136 // We might have base classes to print out first
8137 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8138 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8139 base_class != base_class_end;
8140 ++base_class)
8141 {
8142 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8143
8144 // Skip empty base classes
8145 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8146 continue;
8147
8148 if (base_class->isVirtual())
8149 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8150 else
8151 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8152 field_byte_offset = field_bit_offset / 8;
8153 assert (field_bit_offset % 8 == 0);
8154 if (child_idx == 0)
8155 s->PutChar('{');
8156 else
8157 s->PutChar(',');
8158
8159 clang::QualType base_class_qual_type = base_class->getType();
8160 std::string base_class_type_name(base_class_qual_type.getAsString());
8161
8162 // Indent and print the base class type name
8163 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8164
8165 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8166
8167 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008168 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008169 base_clang_type.DumpValue (exe_ctx,
8170 s, // Stream to dump to
8171 base_clang_type.GetFormat(), // The format with which to display the member
8172 data, // Data buffer containing all bytes for this type
8173 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8174 base_class_type_info.Width / 8, // Size of this type in bytes
8175 0, // Bitfield bit size
8176 0, // Bitfield bit offset
8177 show_types, // Boolean indicating if we should show the variable types
8178 show_summary, // Boolean indicating if we should show a summary for the current type
8179 verbose, // Verbose output?
8180 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8181
8182 ++child_idx;
8183 }
8184 }
8185 uint32_t field_idx = 0;
8186 clang::RecordDecl::field_iterator field, field_end;
8187 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8188 {
8189 // Print the starting squiggly bracket (if this is the
8190 // first member) or comma (for member 2 and beyond) for
8191 // the struct/union/class member.
8192 if (child_idx == 0)
8193 s->PutChar('{');
8194 else
8195 s->PutChar(',');
8196
8197 // Indent
8198 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8199
8200 clang::QualType field_type = field->getType();
8201 // Print the member type if requested
8202 // Figure out the type byte size (field_type_info.first) and
8203 // alignment (field_type_info.second) from the AST context.
8204 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8205 assert(field_idx < record_layout.getFieldCount());
8206 // Figure out the field offset within the current struct/union/class type
8207 field_bit_offset = record_layout.getFieldOffset (field_idx);
8208 field_byte_offset = field_bit_offset / 8;
8209 uint32_t field_bitfield_bit_size = 0;
8210 uint32_t field_bitfield_bit_offset = 0;
8211 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8212 field_bitfield_bit_offset = field_bit_offset % 8;
8213
8214 if (show_types)
8215 {
8216 std::string field_type_name(field_type.getAsString());
8217 if (field_bitfield_bit_size > 0)
8218 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8219 else
8220 s->Printf("(%s) ", field_type_name.c_str());
8221 }
8222 // Print the member name and equal sign
8223 s->Printf("%s = ", field->getNameAsString().c_str());
8224
8225
8226 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008227 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008228 field_clang_type.DumpValue (exe_ctx,
8229 s, // Stream to dump to
8230 field_clang_type.GetFormat(), // The format with which to display the member
8231 data, // Data buffer containing all bytes for this type
8232 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8233 field_type_info.Width / 8, // Size of this type in bytes
8234 field_bitfield_bit_size, // Bitfield bit size
8235 field_bitfield_bit_offset, // Bitfield bit offset
8236 show_types, // Boolean indicating if we should show the variable types
8237 show_summary, // Boolean indicating if we should show a summary for the current type
8238 verbose, // Verbose output?
8239 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8240 }
8241
8242 // Indent the trailing squiggly bracket
8243 if (child_idx > 0)
8244 s->Printf("\n%*s}", depth, "");
8245 }
8246 return;
8247
8248 case clang::Type::Enum:
8249 if (GetCompleteType(type))
8250 {
8251 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8252 const clang::EnumDecl *enum_decl = enutype->getDecl();
8253 assert(enum_decl);
8254 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8255 lldb::offset_t offset = data_byte_offset;
8256 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8257 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8258 {
8259 if (enum_pos->getInitVal() == enum_value)
8260 {
8261 s->Printf("%s", enum_pos->getNameAsString().c_str());
8262 return;
8263 }
8264 }
8265 // If we have gotten here we didn't get find the enumerator in the
8266 // enum decl, so just print the integer.
8267 s->Printf("%" PRIi64, enum_value);
8268 }
8269 return;
8270
8271 case clang::Type::ConstantArray:
8272 {
8273 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8274 bool is_array_of_characters = false;
8275 clang::QualType element_qual_type = array->getElementType();
8276
8277 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8278 if (canonical_type)
8279 is_array_of_characters = canonical_type->isCharType();
8280
8281 const uint64_t element_count = array->getSize().getLimitedValue();
8282
8283 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8284
8285 uint32_t element_idx = 0;
8286 uint32_t element_offset = 0;
8287 uint64_t element_byte_size = field_type_info.Width / 8;
8288 uint32_t element_stride = element_byte_size;
8289
8290 if (is_array_of_characters)
8291 {
8292 s->PutChar('"');
8293 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8294 s->PutChar('"');
8295 return;
8296 }
8297 else
8298 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008299 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008300 lldb::Format element_format = element_clang_type.GetFormat();
8301
8302 for (element_idx = 0; element_idx < element_count; ++element_idx)
8303 {
8304 // Print the starting squiggly bracket (if this is the
8305 // first member) or comman (for member 2 and beyong) for
8306 // the struct/union/class member.
8307 if (element_idx == 0)
8308 s->PutChar('{');
8309 else
8310 s->PutChar(',');
8311
8312 // Indent and print the index
8313 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8314
8315 // Figure out the field offset within the current struct/union/class type
8316 element_offset = element_idx * element_stride;
8317
8318 // Dump the value of the member
8319 element_clang_type.DumpValue (exe_ctx,
8320 s, // Stream to dump to
8321 element_format, // The format with which to display the element
8322 data, // Data buffer containing all bytes for this type
8323 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8324 element_byte_size, // Size of this type in bytes
8325 0, // Bitfield bit size
8326 0, // Bitfield bit offset
8327 show_types, // Boolean indicating if we should show the variable types
8328 show_summary, // Boolean indicating if we should show a summary for the current type
8329 verbose, // Verbose output?
8330 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8331 }
8332
8333 // Indent the trailing squiggly bracket
8334 if (element_idx > 0)
8335 s->Printf("\n%*s}", depth, "");
8336 }
8337 }
8338 return;
8339
8340 case clang::Type::Typedef:
8341 {
8342 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8343
Greg Claytona1e5dc82015-08-11 22:53:00 +00008344 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008345 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8346 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8347 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8348
8349 return typedef_clang_type.DumpValue (exe_ctx,
8350 s, // Stream to dump to
8351 typedef_format, // The format with which to display the element
8352 data, // Data buffer containing all bytes for this type
8353 data_byte_offset, // Offset into "data" where to grab value from
8354 typedef_byte_size, // Size of this type in bytes
8355 bitfield_bit_size, // Bitfield bit size
8356 bitfield_bit_offset,// Bitfield bit offset
8357 show_types, // Boolean indicating if we should show the variable types
8358 show_summary, // Boolean indicating if we should show a summary for the current type
8359 verbose, // Verbose output?
8360 depth); // Scope depth for any types that have children
8361 }
8362 break;
8363
8364 case clang::Type::Elaborated:
8365 {
8366 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008367 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008368 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8369 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8370 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8371
8372 return elaborated_clang_type.DumpValue (exe_ctx,
8373 s, // Stream to dump to
8374 elaborated_format, // The format with which to display the element
8375 data, // Data buffer containing all bytes for this type
8376 data_byte_offset, // Offset into "data" where to grab value from
8377 elaborated_byte_size, // Size of this type in bytes
8378 bitfield_bit_size, // Bitfield bit size
8379 bitfield_bit_offset,// Bitfield bit offset
8380 show_types, // Boolean indicating if we should show the variable types
8381 show_summary, // Boolean indicating if we should show a summary for the current type
8382 verbose, // Verbose output?
8383 depth); // Scope depth for any types that have children
8384 }
8385 break;
8386
8387 case clang::Type::Paren:
8388 {
8389 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008390 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008391
8392 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8393 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8394 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8395
8396 return desugar_clang_type.DumpValue (exe_ctx,
8397 s, // Stream to dump to
8398 desugar_format, // The format with which to display the element
8399 data, // Data buffer containing all bytes for this type
8400 data_byte_offset, // Offset into "data" where to grab value from
8401 desugar_byte_size, // Size of this type in bytes
8402 bitfield_bit_size, // Bitfield bit size
8403 bitfield_bit_offset,// Bitfield bit offset
8404 show_types, // Boolean indicating if we should show the variable types
8405 show_summary, // Boolean indicating if we should show a summary for the current type
8406 verbose, // Verbose output?
8407 depth); // Scope depth for any types that have children
8408 }
8409 break;
8410
8411 default:
8412 // We are down to a scalar type that we just need to display.
8413 data.Dump(s,
8414 data_byte_offset,
8415 format,
8416 data_byte_size,
8417 1,
8418 UINT32_MAX,
8419 LLDB_INVALID_ADDRESS,
8420 bitfield_bit_size,
8421 bitfield_bit_offset);
8422
8423 if (show_summary)
8424 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8425 break;
8426 }
8427}
8428
8429
8430
8431
8432bool
8433ClangASTContext::DumpTypeValue (void* type, Stream *s,
8434 lldb::Format format,
8435 const lldb_private::DataExtractor &data,
8436 lldb::offset_t byte_offset,
8437 size_t byte_size,
8438 uint32_t bitfield_bit_size,
8439 uint32_t bitfield_bit_offset,
8440 ExecutionContextScope *exe_scope)
8441{
8442 if (!type)
8443 return false;
8444 if (IsAggregateType(type))
8445 {
8446 return false;
8447 }
8448 else
8449 {
8450 clang::QualType qual_type(GetQualType(type));
8451
8452 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8453 switch (type_class)
8454 {
8455 case clang::Type::Typedef:
8456 {
8457 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008458 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008459 if (format == eFormatDefault)
8460 format = typedef_clang_type.GetFormat();
8461 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8462 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8463
8464 return typedef_clang_type.DumpTypeValue (s,
8465 format, // The format with which to display the element
8466 data, // Data buffer containing all bytes for this type
8467 byte_offset, // Offset into "data" where to grab value from
8468 typedef_byte_size, // Size of this type in bytes
8469 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8470 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8471 exe_scope);
8472 }
8473 break;
8474
8475 case clang::Type::Enum:
8476 // If our format is enum or default, show the enumeration value as
8477 // its enumeration string value, else just display it as requested.
8478 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8479 {
8480 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8481 const clang::EnumDecl *enum_decl = enutype->getDecl();
8482 assert(enum_decl);
8483 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8484 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8485 lldb::offset_t offset = byte_offset;
8486 if (is_signed)
8487 {
8488 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8489 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8490 {
8491 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8492 {
8493 s->PutCString (enum_pos->getNameAsString().c_str());
8494 return true;
8495 }
8496 }
8497 // If we have gotten here we didn't get find the enumerator in the
8498 // enum decl, so just print the integer.
8499 s->Printf("%" PRIi64, enum_svalue);
8500 }
8501 else
8502 {
8503 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8504 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8505 {
8506 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8507 {
8508 s->PutCString (enum_pos->getNameAsString().c_str());
8509 return true;
8510 }
8511 }
8512 // If we have gotten here we didn't get find the enumerator in the
8513 // enum decl, so just print the integer.
8514 s->Printf("%" PRIu64, enum_uvalue);
8515 }
8516 return true;
8517 }
8518 // format was not enum, just fall through and dump the value as requested....
8519
8520 default:
8521 // We are down to a scalar type that we just need to display.
8522 {
8523 uint32_t item_count = 1;
8524 // A few formats, we might need to modify our size and count for depending
8525 // on how we are trying to display the value...
8526 switch (format)
8527 {
8528 default:
8529 case eFormatBoolean:
8530 case eFormatBinary:
8531 case eFormatComplex:
8532 case eFormatCString: // NULL terminated C strings
8533 case eFormatDecimal:
8534 case eFormatEnum:
8535 case eFormatHex:
8536 case eFormatHexUppercase:
8537 case eFormatFloat:
8538 case eFormatOctal:
8539 case eFormatOSType:
8540 case eFormatUnsigned:
8541 case eFormatPointer:
8542 case eFormatVectorOfChar:
8543 case eFormatVectorOfSInt8:
8544 case eFormatVectorOfUInt8:
8545 case eFormatVectorOfSInt16:
8546 case eFormatVectorOfUInt16:
8547 case eFormatVectorOfSInt32:
8548 case eFormatVectorOfUInt32:
8549 case eFormatVectorOfSInt64:
8550 case eFormatVectorOfUInt64:
8551 case eFormatVectorOfFloat32:
8552 case eFormatVectorOfFloat64:
8553 case eFormatVectorOfUInt128:
8554 break;
8555
8556 case eFormatChar:
8557 case eFormatCharPrintable:
8558 case eFormatCharArray:
8559 case eFormatBytes:
8560 case eFormatBytesWithASCII:
8561 item_count = byte_size;
8562 byte_size = 1;
8563 break;
8564
8565 case eFormatUnicode16:
8566 item_count = byte_size / 2;
8567 byte_size = 2;
8568 break;
8569
8570 case eFormatUnicode32:
8571 item_count = byte_size / 4;
8572 byte_size = 4;
8573 break;
8574 }
8575 return data.Dump (s,
8576 byte_offset,
8577 format,
8578 byte_size,
8579 item_count,
8580 UINT32_MAX,
8581 LLDB_INVALID_ADDRESS,
8582 bitfield_bit_size,
8583 bitfield_bit_offset,
8584 exe_scope);
8585 }
8586 break;
8587 }
8588 }
8589 return 0;
8590}
8591
8592
8593
8594void
8595ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8596 Stream *s,
8597 const lldb_private::DataExtractor &data,
8598 lldb::offset_t data_byte_offset,
8599 size_t data_byte_size)
8600{
8601 uint32_t length = 0;
8602 if (IsCStringType (type, length))
8603 {
8604 if (exe_ctx)
8605 {
8606 Process *process = exe_ctx->GetProcessPtr();
8607 if (process)
8608 {
8609 lldb::offset_t offset = data_byte_offset;
8610 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8611 std::vector<uint8_t> buf;
8612 if (length > 0)
8613 buf.resize (length);
8614 else
8615 buf.resize (256);
8616
8617 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8618 buf.back() = '\0';
8619 size_t bytes_read;
8620 size_t total_cstr_len = 0;
8621 Error error;
8622 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8623 {
8624 const size_t len = strlen((const char *)&buf.front());
8625 if (len == 0)
8626 break;
8627 if (total_cstr_len == 0)
8628 s->PutCString (" \"");
8629 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8630 total_cstr_len += len;
8631 if (len < buf.size())
8632 break;
8633 pointer_address += total_cstr_len;
8634 }
8635 if (total_cstr_len > 0)
8636 s->PutChar ('"');
8637 }
8638 }
8639 }
8640}
8641
8642void
8643ClangASTContext::DumpTypeDescription (void* type)
8644{
8645 StreamFile s (stdout, false);
8646 DumpTypeDescription (&s);
8647 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8648 if (metadata)
8649 {
8650 metadata->Dump (&s);
8651 }
8652}
8653
8654void
8655ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8656{
8657 if (type)
8658 {
8659 clang::QualType qual_type(GetQualType(type));
8660
8661 llvm::SmallVector<char, 1024> buf;
8662 llvm::raw_svector_ostream llvm_ostrm (buf);
8663
8664 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8665 switch (type_class)
8666 {
8667 case clang::Type::ObjCObject:
8668 case clang::Type::ObjCInterface:
8669 {
8670 GetCompleteType(type);
8671
8672 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8673 assert (objc_class_type);
8674 if (objc_class_type)
8675 {
8676 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8677 if (class_interface_decl)
8678 {
8679 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8680 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8681 }
8682 }
8683 }
8684 break;
8685
8686 case clang::Type::Typedef:
8687 {
8688 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8689 if (typedef_type)
8690 {
8691 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8692 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8693 if (!clang_typedef_name.empty())
8694 {
8695 s->PutCString ("typedef ");
8696 s->PutCString (clang_typedef_name.c_str());
8697 }
8698 }
8699 }
8700 break;
8701
8702 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008703 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008704 return;
8705
8706 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008707 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008708 return;
8709
8710 case clang::Type::Record:
8711 {
8712 GetCompleteType(type);
8713
8714 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8715 const clang::RecordDecl *record_decl = record_type->getDecl();
8716 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8717
8718 if (cxx_record_decl)
8719 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8720 else
8721 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8722 }
8723 break;
8724
8725 default:
8726 {
8727 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8728 if (tag_type)
8729 {
8730 clang::TagDecl *tag_decl = tag_type->getDecl();
8731 if (tag_decl)
8732 tag_decl->print(llvm_ostrm, 0);
8733 }
8734 else
8735 {
8736 std::string clang_type_name(qual_type.getAsString());
8737 if (!clang_type_name.empty())
8738 s->PutCString (clang_type_name.c_str());
8739 }
8740 }
8741 }
8742
Greg Claytond8d4a572015-08-11 21:38:15 +00008743 if (buf.size() > 0)
8744 {
8745 s->Write (buf.data(), buf.size());
8746 }
8747 }
8748}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008749
Greg Clayton8b4edba2015-08-14 20:02:05 +00008750clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00008751ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008752 lldb::AccessType access_type,
8753 const char *parent_name,
8754 int tag_decl_kind,
8755 const ClangASTContext::TemplateParameterInfos &template_param_infos)
8756{
8757 if (template_param_infos.IsValid())
8758 {
8759 std::string template_basename(parent_name);
8760 template_basename.erase (template_basename.find('<'));
8761
8762 return CreateClassTemplateDecl (decl_ctx,
8763 access_type,
8764 template_basename.c_str(),
8765 tag_decl_kind,
8766 template_param_infos);
8767 }
8768 return NULL;
8769}
8770
Greg Clayton6dc8d582015-08-18 22:32:36 +00008771void
8772ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
8773{
8774 ClangASTContext *ast = (ClangASTContext *)baton;
8775 SymbolFile *sym_file = ast->GetSymbolFile();
8776 if (sym_file)
8777 {
8778 CompilerType clang_type = GetTypeForDecl (decl);
8779 if (clang_type)
8780 sym_file->CompleteType (clang_type);
8781 }
8782}
8783
8784void
8785ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
8786{
8787 ClangASTContext *ast = (ClangASTContext *)baton;
8788 SymbolFile *sym_file = ast->GetSymbolFile();
8789 if (sym_file)
8790 {
8791 CompilerType clang_type = GetTypeForDecl (decl);
8792 if (clang_type)
8793 sym_file->CompleteType (clang_type);
8794 }
8795}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008796
Greg Clayton261ac3f2015-08-28 01:01:03 +00008797
8798DWARFASTParser *
8799ClangASTContext::GetDWARFParser ()
8800{
8801 if (!m_dwarf_ast_parser_ap)
8802 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
8803 return m_dwarf_ast_parser_ap.get();
8804}
8805
8806
Greg Clayton8b4edba2015-08-14 20:02:05 +00008807bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00008808ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008809 const clang::RecordDecl *record_decl,
8810 uint64_t &bit_size,
8811 uint64_t &alignment,
8812 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
8813 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
8814 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
8815{
Greg Clayton6dc8d582015-08-18 22:32:36 +00008816 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00008817 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
8818 return dwarf_ast_parser->LayoutRecordType(record_decl, bit_size, alignment, field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00008819}
8820
Greg Clayton99558cc42015-08-24 23:46:31 +00008821//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00008822// CompilerDecl override functions
8823//----------------------------------------------------------------------
8824lldb::VariableSP
8825ClangASTContext::DeclGetVariable (void *opaque_decl)
8826{
8827 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
8828 {
8829 auto decl_search_it = m_decl_objects.find(opaque_decl);
8830 if (decl_search_it != m_decl_objects.end())
8831 return std::static_pointer_cast<Variable>(decl_search_it->second);
8832 }
8833 return VariableSP();
8834}
8835
8836void
8837ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
8838{
8839 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
8840 m_decl_objects.insert(std::make_pair(opaque_decl, object));
8841}
8842
8843ConstString
8844ClangASTContext::DeclGetName (void *opaque_decl)
8845{
8846 if (opaque_decl)
8847 {
8848 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
8849 if (nd != nullptr)
8850 return ConstString(nd->getName());
8851 }
8852 return ConstString();
8853}
8854
8855//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00008856// CompilerDeclContext functions
8857//----------------------------------------------------------------------
8858
Paul Hermand628cbb2015-09-15 23:44:17 +00008859std::vector<void *>
8860ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name)
8861{
8862 std::vector<void *> found_decls;
8863 if (opaque_decl_ctx)
8864 {
8865 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
8866 std::set<DeclContext *> searched;
8867 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00008868 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00008869
8870 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
8871 {
8872 search_queue.insert(std::make_pair(decl_context, decl_context));
8873
8874 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
8875 {
8876 searched.insert(it->second);
Paul Hermanea188fc2015-09-16 18:48:30 +00008877 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
8878
Paul Hermand628cbb2015-09-15 23:44:17 +00008879 for (clang::Decl *child : it->second->decls())
8880 {
Paul Hermanea188fc2015-09-16 18:48:30 +00008881 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00008882 {
8883 clang::DeclContext *from = ud->getCommonAncestor();
8884 if (searched.find(ud->getNominatedNamespace()) == searched.end())
8885 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
8886 }
8887 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
8888 {
8889 for (clang::UsingShadowDecl *usd : ud->shadows())
8890 {
8891 clang::Decl *target = usd->getTargetDecl();
8892 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
8893 {
8894 IdentifierInfo *ii = nd->getIdentifier();
8895 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
8896 found_decls.push_back(nd);
8897 }
8898 }
8899 }
Paul Hermanea188fc2015-09-16 18:48:30 +00008900 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
8901 {
8902 IdentifierInfo *ii = nd->getIdentifier();
8903 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
8904 found_decls.push_back(nd);
8905 }
Paul Hermand628cbb2015-09-15 23:44:17 +00008906 }
8907 }
8908 }
8909 }
8910 return found_decls;
8911}
8912
Greg Clayton99558cc42015-08-24 23:46:31 +00008913bool
8914ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00008915{
Greg Clayton99558cc42015-08-24 23:46:31 +00008916 if (opaque_decl_ctx)
8917 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
8918 else
8919 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00008920}
8921
Greg Clayton99558cc42015-08-24 23:46:31 +00008922ConstString
8923ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00008924{
Greg Clayton99558cc42015-08-24 23:46:31 +00008925 if (opaque_decl_ctx)
8926 {
8927 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
8928 if (named_decl)
8929 return ConstString(named_decl->getName());
8930 }
8931 return ConstString();
8932}
8933
8934bool
8935ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
8936 lldb::LanguageType *language_ptr,
8937 bool *is_instance_method_ptr,
8938 ConstString *language_object_name_ptr)
8939{
8940 if (opaque_decl_ctx)
8941 {
8942 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
8943 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
8944 {
8945 if (is_instance_method_ptr)
8946 *is_instance_method_ptr = objc_method->isInstanceMethod();
8947 if (language_ptr)
8948 *language_ptr = eLanguageTypeObjC;
8949 if (language_object_name_ptr)
8950 language_object_name_ptr->SetCString("self");
8951 return true;
8952 }
8953 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
8954 {
8955 if (is_instance_method_ptr)
8956 *is_instance_method_ptr = cxx_method->isInstance();
8957 if (language_ptr)
8958 *language_ptr = eLanguageTypeC_plus_plus;
8959 if (language_object_name_ptr)
8960 language_object_name_ptr->SetCString("this");
8961 return true;
8962 }
8963 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
8964 {
8965 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
8966 if (metadata && metadata->HasObjectPtr())
8967 {
8968 if (is_instance_method_ptr)
8969 *is_instance_method_ptr = true;
8970 if (language_ptr)
8971 *language_ptr = eLanguageTypeObjC;
8972 if (language_object_name_ptr)
8973 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
8974 return true;
8975 }
8976 }
8977 }
8978 return false;
8979}
8980
8981clang::DeclContext *
8982ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
8983{
8984 if (dc.IsClang())
8985 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
8986 return nullptr;
8987}
8988
8989
8990ObjCMethodDecl *
8991ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
8992{
8993 if (dc.IsClang())
8994 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
8995 return nullptr;
8996}
8997
8998CXXMethodDecl *
8999ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9000{
9001 if (dc.IsClang())
9002 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9003 return nullptr;
9004}
9005
9006clang::FunctionDecl *
9007ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9008{
9009 if (dc.IsClang())
9010 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9011 return nullptr;
9012}
9013
9014clang::NamespaceDecl *
9015ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9016{
9017 if (dc.IsClang())
9018 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9019 return nullptr;
9020}
9021
9022ClangASTMetadata *
9023ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9024{
9025 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9026 if (ast)
9027 return ClangASTContext::GetMetadata (ast, object);
9028 return nullptr;
9029}
9030
9031clang::ASTContext *
9032ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9033{
Greg Claytonf73034f2015-09-08 18:15:05 +00009034 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9035 if (ast)
9036 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009037 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009038}
9039
Jim Ingham151c0322015-09-15 21:13:50 +00009040ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9041 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
9042 m_target_wp(target.shared_from_this())
9043{
9044}
9045
9046UserExpression *
9047ClangASTContextForExpressions::GetUserExpression (const char *expr,
9048 const char *expr_prefix,
9049 lldb::LanguageType language,
9050 Expression::ResultType desired_type)
9051{
9052 TargetSP target_sp = m_target_wp.lock();
9053 if (!target_sp)
9054 return nullptr;
9055
9056 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type);
9057}
9058
9059FunctionCaller *
9060ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9061 const Address& function_address,
9062 const ValueList &arg_value_list,
9063 const char *name)
9064{
9065 TargetSP target_sp = m_target_wp.lock();
9066 if (!target_sp)
9067 return nullptr;
9068
9069 Process *process = target_sp->GetProcessSP().get();
9070 if (!process)
9071 return nullptr;
9072
9073 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9074}
9075
9076UtilityFunction *
9077ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9078 const char *name)
9079{
9080 TargetSP target_sp = m_target_wp.lock();
9081 if (!target_sp)
9082 return nullptr;
9083
9084 return new ClangUtilityFunction(*target_sp.get(), text, name);
9085}