blob: 901774e522742c303e9d37143f79f630c9566aa7 [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 }
1733
1734
1735 if (namespace_decl)
1736 {
1737 // If we make it here, we are creating the anonymous namespace decl
1738 // for the first time, so we need to do the using directive magic
1739 // like SEMA does
1740 UsingDirectiveDecl* using_directive_decl = UsingDirectiveDecl::Create (*ast,
1741 decl_ctx,
1742 SourceLocation(),
1743 SourceLocation(),
1744 NestedNameSpecifierLoc(),
1745 SourceLocation(),
1746 namespace_decl,
1747 decl_ctx);
1748 using_directive_decl->setImplicit();
1749 decl_ctx->addDecl(using_directive_decl);
1750 }
1751 }
1752#ifdef LLDB_CONFIGURATION_DEBUG
1753 VerifyDecl(namespace_decl);
1754#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001755 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756}
1757
1758
Paul Hermand628cbb2015-09-15 23:44:17 +00001759clang::BlockDecl *
1760ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1761{
1762 if (ctx != nullptr)
1763 {
1764 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1765 ctx->addDecl(decl);
1766 return decl;
1767 }
1768 return nullptr;
1769}
1770
1771clang::UsingDirectiveDecl *
1772ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1773{
1774 if (decl_ctx != nullptr && ns_decl != nullptr)
1775 {
1776 // TODO: run LCA between decl_tx and ns_decl
1777 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1778 decl_ctx,
1779 clang::SourceLocation(),
1780 clang::SourceLocation(),
1781 clang::NestedNameSpecifierLoc(),
1782 clang::SourceLocation(),
1783 ns_decl,
1784 GetTranslationUnitDecl(getASTContext()));
1785 decl_ctx->addDecl(using_decl);
1786 return using_decl;
1787 }
1788 return nullptr;
1789}
1790
1791clang::UsingDecl *
1792ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1793{
1794 if (current_decl_ctx != nullptr && target != nullptr)
1795 {
1796 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1797 current_decl_ctx,
1798 clang::SourceLocation(),
1799 clang::NestedNameSpecifierLoc(),
1800 clang::DeclarationNameInfo(),
1801 false);
1802 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1803 current_decl_ctx,
1804 clang::SourceLocation(),
1805 using_decl,
1806 target);
1807 using_decl->addShadowDecl(shadow_decl);
1808 current_decl_ctx->addDecl(using_decl);
1809 return using_decl;
1810 }
1811 return nullptr;
1812}
1813
1814clang::VarDecl *
1815ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1816{
1817 if (decl_context != nullptr)
1818 {
1819 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1820 decl_context,
1821 clang::SourceLocation(),
1822 clang::SourceLocation(),
1823 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1824 type,
1825 nullptr,
1826 clang::SC_None);
1827 var_decl->setAccess(clang::AS_public);
1828 decl_context->addDecl(var_decl);
1829 decl_context->makeDeclVisibleInContext(var_decl);
1830 return var_decl;
1831 }
1832 return nullptr;
1833}
1834
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835#pragma mark Function Types
1836
1837FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001838ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1839 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001840 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001841 int storage,
1842 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843{
Ed Masted4612ad2014-04-20 13:17:36 +00001844 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001845 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001846 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001847 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001848
Greg Clayton0d551042013-06-28 21:08:47 +00001849
1850 const bool hasWrittenPrototype = true;
1851 const bool isConstexprSpecified = false;
1852
Greg Clayton147e1fa2011-10-14 22:47:18 +00001853 if (name && name[0])
1854 {
1855 func_decl = FunctionDecl::Create (*ast,
1856 decl_ctx,
1857 SourceLocation(),
1858 SourceLocation(),
1859 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001860 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001861 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001862 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001863 is_inline,
1864 hasWrittenPrototype,
1865 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001867 else
1868 {
1869 func_decl = FunctionDecl::Create (*ast,
1870 decl_ctx,
1871 SourceLocation(),
1872 SourceLocation(),
1873 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00001874 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001875 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001876 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00001877 is_inline,
1878 hasWrittenPrototype,
1879 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001880 }
1881 if (func_decl)
1882 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001883
1884#ifdef LLDB_CONFIGURATION_DEBUG
1885 VerifyDecl(func_decl);
1886#endif
1887
Greg Clayton147e1fa2011-10-14 22:47:18 +00001888 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001889}
1890
Greg Claytona1e5dc82015-08-11 22:53:00 +00001891CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001892ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001893 const CompilerType& result_type,
1894 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00001895 unsigned num_args,
1896 bool is_variadic,
1897 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898{
Ed Masted4612ad2014-04-20 13:17:36 +00001899 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001900 std::vector<QualType> qual_type_args;
1901 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00001902 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903
1904 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00001905 FunctionProtoType::ExtProtoInfo proto_info;
1906 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001907 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00001908 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00001909 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00001910
Greg Claytona1e5dc82015-08-11 22:53:00 +00001911 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001912 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00001913 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914}
1915
1916ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00001917ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001919 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001920 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001921 return ParmVarDecl::Create(*ast,
1922 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001923 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00001924 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001925 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00001926 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001927 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001928 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00001929 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930}
1931
1932void
1933ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
1934{
1935 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00001936 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937}
1938
1939
1940#pragma mark Array Types
1941
Greg Claytona1e5dc82015-08-11 22:53:00 +00001942CompilerType
1943ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00001944 size_t element_count,
1945 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946{
Greg Clayton57ee3062013-07-11 22:46:58 +00001947 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001949 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001950 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00001951
Greg Clayton1c8ef472013-04-05 23:27:21 +00001952 if (is_vector)
1953 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001954 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00001955 }
1956 else
1957 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00001958
1959 llvm::APInt ap_element_count (64, element_count);
1960 if (element_count == 0)
1961 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001962 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001963 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001964 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001965 }
1966 else
1967 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001968 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00001969 ap_element_count,
1970 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00001971 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00001972 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00001973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001975 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976}
1977
Greg Claytona1e5dc82015-08-11 22:53:00 +00001978CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00001979ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001980 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00001981 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00001982{
Greg Claytona1e5dc82015-08-11 22:53:00 +00001983 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00001984 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
1985 return type;
1986 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00001987 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001988 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00001989 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00001990 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00001991 SetIsPacked(type);
1992 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00001993 return type;
1994}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001995
1996#pragma mark Enumeration Types
1997
Greg Claytona1e5dc82015-08-11 22:53:00 +00001998CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00001999ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002000(
Greg Claytond8d4a572015-08-11 21:38:15 +00002001 const char *name,
2002 DeclContext *decl_ctx,
2003 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002004 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002005 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006{
2007 // TODO: Do something intelligent with the Declaration object passed in
2008 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002009 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002010
Greg Claytone02b8502010-10-12 04:29:14 +00002011 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002012 // const bool IsScoped = false;
2013 // const bool IsFixed = false;
2014
Greg Clayton6beaaa62011-01-17 03:46:26 +00002015 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002016 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002017 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002018 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002019 name && name[0] ? &ast->Idents.get(name) : nullptr,
2020 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002021 false, // IsScoped
2022 false, // IsScopedUsingClassTag
2023 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002024
2025
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002027 {
2028 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00002029 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00002030
2031 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2032
Greg Claytona1e5dc82015-08-11 22:53:00 +00002033 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002034 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002035 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036}
2037
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002038// Disable this for now since I can't seem to get a nicely formatted float
2039// out of the APFloat class without just getting the float, double or quad
2040// and then using a formatted print on it which defeats the purpose. We ideally
2041// would like to get perfect string values for any kind of float semantics
2042// so we can support remote targets. The code below also requires a patch to
2043// llvm::APInt.
2044//bool
Greg Clayton6beaaa62011-01-17 03:46:26 +00002045//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 +00002046//{
2047// uint32_t count = 0;
2048// bool is_complex = false;
2049// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2050// {
2051// unsigned num_bytes_per_float = byte_size / count;
2052// unsigned num_bits_per_float = num_bytes_per_float * 8;
2053//
2054// float_str.clear();
2055// uint32_t i;
2056// for (i=0; i<count; i++)
2057// {
2058// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2059// bool is_ieee = false;
2060// APFloat ap_float(ap_int, is_ieee);
2061// char s[1024];
2062// unsigned int hex_digits = 0;
2063// bool upper_case = false;
2064//
2065// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2066// {
2067// if (i > 0)
2068// float_str.append(", ");
2069// float_str.append(s);
2070// if (i == 1 && is_complex)
2071// float_str.append(1, 'i');
2072// }
2073// }
2074// return !float_str.empty();
2075// }
2076// return false;
2077//}
2078
Greg Claytona1e5dc82015-08-11 22:53:00 +00002079CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002080ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2081 size_t bit_size, bool is_signed)
2082{
2083 if (ast)
2084 {
2085 if (is_signed)
2086 {
2087 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002088 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002089
2090 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002091 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002092
2093 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002094 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002095
2096 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002097 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002098
2099 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002100 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002101
2102 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002103 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002104 }
2105 else
2106 {
2107 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002108 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002109
2110 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002111 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002112
2113 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002114 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002115
2116 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002117 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002118
2119 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002120 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002121
2122 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002123 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002124 }
2125 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002126 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002127}
2128
Greg Claytona1e5dc82015-08-11 22:53:00 +00002129CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002130ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2131{
2132 if (ast)
2133 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002134 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002135}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002136
Greg Claytona1e5dc82015-08-11 22:53:00 +00002137CompilerType
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002138ClangASTContext::GetFloatTypeFromBitSize (clang::ASTContext *ast,
2139 size_t bit_size)
2140{
2141 if (ast)
2142 {
2143 if (bit_size == ast->getTypeSize(ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002144 return CompilerType(ast, ast->FloatTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002145 else if (bit_size == ast->getTypeSize(ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002146 return CompilerType(ast, ast->DoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002147 else if (bit_size == ast->getTypeSize(ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002148 return CompilerType(ast, ast->LongDoubleTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002149 else if (bit_size == ast->getTypeSize(ast->HalfTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002150 return CompilerType(ast, ast->HalfTy);
Greg Claytonbc8fc0f2013-06-11 21:56:55 +00002151 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002152 return CompilerType();
Enrico Granata86027e92012-03-24 01:11:14 +00002153}
2154
2155bool
Greg Claytona2721472011-06-25 00:44:06 +00002156ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2157 clang::Decl *decl)
2158{
2159 if (!decl)
2160 return false;
2161
2162 ExternalASTSource *ast_source = ast->getExternalSource();
2163
2164 if (!ast_source)
2165 return false;
2166
2167 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2168 {
Greg Clayton219cf312012-03-30 00:51:13 +00002169 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002170 return true;
2171
2172 if (!tag_decl->hasExternalLexicalStorage())
2173 return false;
2174
2175 ast_source->CompleteType(tag_decl);
2176
2177 return !tag_decl->getTypeForDecl()->isIncompleteType();
2178 }
2179 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2180 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002181 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002182 return true;
2183
2184 if (!objc_interface_decl->hasExternalLexicalStorage())
2185 return false;
2186
2187 ast_source->CompleteType(objc_interface_decl);
2188
Sean Callanan5b26f272012-02-04 08:49:35 +00002189 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002190 }
2191 else
2192 {
2193 return false;
2194 }
2195}
2196
Sean Callanan60217122012-04-13 00:10:03 +00002197void
Greg Claytond0029442013-03-27 01:48:02 +00002198ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002199 user_id_t user_id)
2200{
2201 ClangASTMetadata meta_data;
2202 meta_data.SetUserID (user_id);
2203 SetMetadata (object, meta_data);
2204}
2205
2206void
Sean Callanan60217122012-04-13 00:10:03 +00002207ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002208 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002209 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002210{
2211 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002212 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002213
2214 if (external_source)
2215 external_source->SetMetadata(object, metadata);
2216}
2217
Jim Ingham379397632012-10-27 02:54:13 +00002218ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002219ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002220 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002221{
2222 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002223 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002224
2225 if (external_source && external_source->HasMetadata(object))
2226 return external_source->GetMetadata(object);
2227 else
Ed Masted4612ad2014-04-20 13:17:36 +00002228 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002229}
2230
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002231clang::DeclContext *
2232ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2233{
Sean Callanana87bee82011-08-19 06:19:25 +00002234 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002235}
2236
2237clang::DeclContext *
2238ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2239{
Sean Callanana87bee82011-08-19 06:19:25 +00002240 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002241}
2242
Greg Claytond8d4a572015-08-11 21:38:15 +00002243bool
2244ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2245{
2246 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2247 if (clang_type)
2248 {
2249 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2250 if (tag_type)
2251 {
2252 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2253 if (tag_decl)
2254 {
2255 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2256 return true;
2257 }
2258 }
2259 }
2260 return false;
2261}
2262
2263
2264bool
2265ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2266 int default_accessibility,
2267 int *assigned_accessibilities,
2268 size_t num_assigned_accessibilities)
2269{
2270 if (record_decl)
2271 {
2272 uint32_t field_idx;
2273 clang::RecordDecl::field_iterator field, field_end;
2274 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2275 field != field_end;
2276 ++field, ++field_idx)
2277 {
2278 // If no accessibility was assigned, assign the correct one
2279 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2280 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2281 }
2282 return true;
2283 }
2284 return false;
2285}
2286
2287clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002288ClangASTContext::GetDeclContextForType (const CompilerType& type)
2289{
2290 return GetDeclContextForType(GetQualType(type));
2291}
2292
2293clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002294ClangASTContext::GetDeclContextForType (clang::QualType type)
2295{
2296 if (type.isNull())
2297 return nullptr;
2298
2299 clang::QualType qual_type = type.getCanonicalType();
2300 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2301 switch (type_class)
2302 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002303 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2304 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2305 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2306 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2307 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2308 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2309 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002310 default:
2311 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002312 }
2313 // No DeclContext in this type...
2314 return nullptr;
2315}
2316
2317static bool
2318GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2319{
2320 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2321 switch (type_class)
2322 {
2323 case clang::Type::ConstantArray:
2324 case clang::Type::IncompleteArray:
2325 case clang::Type::VariableArray:
2326 {
2327 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2328
2329 if (array_type)
2330 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2331 }
2332 break;
2333
2334 case clang::Type::Record:
2335 case clang::Type::Enum:
2336 {
2337 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2338 if (tag_type)
2339 {
2340 clang::TagDecl *tag_decl = tag_type->getDecl();
2341 if (tag_decl)
2342 {
2343 if (tag_decl->isCompleteDefinition())
2344 return true;
2345
2346 if (!allow_completion)
2347 return false;
2348
2349 if (tag_decl->hasExternalLexicalStorage())
2350 {
2351 if (ast)
2352 {
2353 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2354 if (external_ast_source)
2355 {
2356 external_ast_source->CompleteType(tag_decl);
2357 return !tag_type->isIncompleteType();
2358 }
2359 }
2360 }
2361 return false;
2362 }
2363 }
2364
2365 }
2366 break;
2367
2368 case clang::Type::ObjCObject:
2369 case clang::Type::ObjCInterface:
2370 {
2371 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2372 if (objc_class_type)
2373 {
2374 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2375 // We currently can't complete objective C types through the newly added ASTContext
2376 // because it only supports TagDecl objects right now...
2377 if (class_interface_decl)
2378 {
2379 if (class_interface_decl->getDefinition())
2380 return true;
2381
2382 if (!allow_completion)
2383 return false;
2384
2385 if (class_interface_decl->hasExternalLexicalStorage())
2386 {
2387 if (ast)
2388 {
2389 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2390 if (external_ast_source)
2391 {
2392 external_ast_source->CompleteType (class_interface_decl);
2393 return !objc_class_type->isIncompleteType();
2394 }
2395 }
2396 }
2397 return false;
2398 }
2399 }
2400 }
2401 break;
2402
2403 case clang::Type::Typedef:
2404 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2405
2406 case clang::Type::Elaborated:
2407 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2408
2409 case clang::Type::Paren:
2410 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2411
2412 default:
2413 break;
2414 }
2415
2416 return true;
2417}
2418
2419static clang::ObjCIvarDecl::AccessControl
2420ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2421{
2422 switch (access)
2423 {
2424 case eAccessNone: return clang::ObjCIvarDecl::None;
2425 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2426 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2427 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2428 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2429 }
2430 return clang::ObjCIvarDecl::None;
2431}
2432
2433
2434//----------------------------------------------------------------------
2435// Tests
2436//----------------------------------------------------------------------
2437
2438bool
2439ClangASTContext::IsAggregateType (void* type)
2440{
2441 clang::QualType qual_type (GetCanonicalQualType(type));
2442
2443 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2444 switch (type_class)
2445 {
2446 case clang::Type::IncompleteArray:
2447 case clang::Type::VariableArray:
2448 case clang::Type::ConstantArray:
2449 case clang::Type::ExtVector:
2450 case clang::Type::Vector:
2451 case clang::Type::Record:
2452 case clang::Type::ObjCObject:
2453 case clang::Type::ObjCInterface:
2454 return true;
2455 case clang::Type::Elaborated:
2456 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2457 case clang::Type::Typedef:
2458 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2459 case clang::Type::Paren:
2460 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2461 default:
2462 break;
2463 }
2464 // The clang type does have a value
2465 return false;
2466}
2467
2468bool
2469ClangASTContext::IsArrayType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002470 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002471 uint64_t *size,
2472 bool *is_incomplete)
2473{
2474 clang::QualType qual_type (GetCanonicalQualType(type));
2475
2476 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2477 switch (type_class)
2478 {
2479 default:
2480 break;
2481
2482 case clang::Type::ConstantArray:
2483 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002484 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002485 if (size)
2486 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
2487 return true;
2488
2489 case clang::Type::IncompleteArray:
2490 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002491 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002492 if (size)
2493 *size = 0;
2494 if (is_incomplete)
2495 *is_incomplete = true;
2496 return true;
2497
2498 case clang::Type::VariableArray:
2499 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002500 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002501 if (size)
2502 *size = 0;
2503 return true;
2504
2505 case clang::Type::DependentSizedArray:
2506 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002507 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002508 if (size)
2509 *size = 0;
2510 return true;
2511
2512 case clang::Type::Typedef:
2513 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2514 element_type_ptr,
2515 size,
2516 is_incomplete);
2517 case clang::Type::Elaborated:
2518 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2519 element_type_ptr,
2520 size,
2521 is_incomplete);
2522 case clang::Type::Paren:
2523 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2524 element_type_ptr,
2525 size,
2526 is_incomplete);
2527 }
2528 if (element_type_ptr)
2529 element_type_ptr->Clear();
2530 if (size)
2531 *size = 0;
2532 if (is_incomplete)
2533 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002534 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002535}
2536
2537bool
2538ClangASTContext::IsVectorType (void* type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002539 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002540 uint64_t *size)
2541{
2542 clang::QualType qual_type (GetCanonicalQualType(type));
2543
2544 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2545 switch (type_class)
2546 {
2547 case clang::Type::Vector:
2548 {
2549 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2550 if (vector_type)
2551 {
2552 if (size)
2553 *size = vector_type->getNumElements();
2554 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002555 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002556 }
2557 return true;
2558 }
2559 break;
2560 case clang::Type::ExtVector:
2561 {
2562 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2563 if (ext_vector_type)
2564 {
2565 if (size)
2566 *size = ext_vector_type->getNumElements();
2567 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002568 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002569 }
2570 return true;
2571 }
2572 default:
2573 break;
2574 }
2575 return false;
2576}
2577
2578bool
2579ClangASTContext::IsRuntimeGeneratedType (void* type)
2580{
2581 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2582 if (!decl_ctx)
2583 return false;
2584
2585 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2586 return false;
2587
2588 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2589
2590 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2591 if (!ast_metadata)
2592 return false;
2593 return (ast_metadata->GetISAPtr() != 0);
2594}
2595
2596bool
2597ClangASTContext::IsCharType (void* type)
2598{
2599 return GetQualType(type).getUnqualifiedType()->isCharType();
2600}
2601
2602
2603bool
2604ClangASTContext::IsCompleteType (void* type)
2605{
2606 const bool allow_completion = false;
2607 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2608}
2609
2610bool
2611ClangASTContext::IsConst(void* type)
2612{
2613 return GetQualType(type).isConstQualified();
2614}
2615
2616bool
2617ClangASTContext::IsCStringType (void* type, uint32_t &length)
2618{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002619 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002620 length = 0;
2621 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2622
2623 if (!pointee_or_element_clang_type.IsValid())
2624 return false;
2625
2626 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2627 {
2628 if (pointee_or_element_clang_type.IsCharType())
2629 {
2630 if (type_flags.Test (eTypeIsArray))
2631 {
2632 // We know the size of the array and it could be a C string
2633 // since it is an array of characters
2634 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2635 }
2636 return true;
2637
2638 }
2639 }
2640 return false;
2641}
2642
2643bool
2644ClangASTContext::IsFunctionType (void* type, bool *is_variadic_ptr)
2645{
2646 if (type)
2647 {
2648 clang::QualType qual_type (GetCanonicalQualType(type));
2649
2650 if (qual_type->isFunctionType())
2651 {
2652 if (is_variadic_ptr)
2653 {
2654 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2655 if (function_proto_type)
2656 *is_variadic_ptr = function_proto_type->isVariadic();
2657 else
2658 *is_variadic_ptr = false;
2659 }
2660 return true;
2661 }
2662
2663 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2664 switch (type_class)
2665 {
2666 default:
2667 break;
2668 case clang::Type::Typedef:
2669 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2670 case clang::Type::Elaborated:
2671 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2672 case clang::Type::Paren:
2673 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2674 case clang::Type::LValueReference:
2675 case clang::Type::RValueReference:
2676 {
2677 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2678 if (reference_type)
2679 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2680 }
2681 break;
2682 }
2683 }
2684 return false;
2685}
2686
2687// Used to detect "Homogeneous Floating-point Aggregates"
2688uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00002689ClangASTContext::IsHomogeneousAggregate (void* type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002690{
2691 if (!type)
2692 return 0;
2693
2694 clang::QualType qual_type(GetCanonicalQualType(type));
2695 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2696 switch (type_class)
2697 {
2698 case clang::Type::Record:
2699 if (GetCompleteType (type))
2700 {
2701 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2702 if (cxx_record_decl)
2703 {
2704 if (cxx_record_decl->getNumBases() ||
2705 cxx_record_decl->isDynamicClass())
2706 return 0;
2707 }
2708 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2709 if (record_type)
2710 {
2711 const clang::RecordDecl *record_decl = record_type->getDecl();
2712 if (record_decl)
2713 {
2714 // We are looking for a structure that contains only floating point types
2715 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2716 uint32_t num_fields = 0;
2717 bool is_hva = false;
2718 bool is_hfa = false;
2719 clang::QualType base_qual_type;
2720 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2721 {
2722 clang::QualType field_qual_type = field_pos->getType();
2723 if (field_qual_type->isFloatingType())
2724 {
2725 if (field_qual_type->isComplexType())
2726 return 0;
2727 else
2728 {
2729 if (num_fields == 0)
2730 base_qual_type = field_qual_type;
2731 else
2732 {
2733 if (is_hva)
2734 return 0;
2735 is_hfa = true;
2736 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2737 return 0;
2738 }
2739 }
2740 }
2741 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2742 {
2743 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2744 if (array && array->getNumElements() <= 4)
2745 {
2746 if (num_fields == 0)
2747 base_qual_type = array->getElementType();
2748 else
2749 {
2750 if (is_hfa)
2751 return 0;
2752 is_hva = true;
2753 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2754 return 0;
2755 }
2756 }
2757 else
2758 return 0;
2759 }
2760 else
2761 return 0;
2762 ++num_fields;
2763 }
2764 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002765 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002766 return num_fields;
2767 }
2768 }
2769 }
2770 break;
2771
2772 case clang::Type::Typedef:
2773 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2774
2775 case clang::Type::Elaborated:
2776 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2777 default:
2778 break;
2779 }
2780 return 0;
2781}
2782
2783size_t
2784ClangASTContext::GetNumberOfFunctionArguments (void* type)
2785{
2786 if (type)
2787 {
2788 clang::QualType qual_type (GetCanonicalQualType(type));
2789 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2790 if (func)
2791 return func->getNumParams();
2792 }
2793 return 0;
2794}
2795
Greg Claytona1e5dc82015-08-11 22:53:00 +00002796CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002797ClangASTContext::GetFunctionArgumentAtIndex (void* type, const size_t index)
2798{
2799 if (type)
2800 {
2801 clang::QualType qual_type (GetCanonicalQualType(type));
2802 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2803 if (func)
2804 {
2805 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002806 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002807 }
2808 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002809 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002810}
2811
2812bool
2813ClangASTContext::IsFunctionPointerType (void* type)
2814{
2815 if (type)
2816 {
2817 clang::QualType qual_type (GetCanonicalQualType(type));
2818
2819 if (qual_type->isFunctionPointerType())
2820 return true;
2821
2822 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2823 switch (type_class)
2824 {
2825 default:
2826 break;
2827 case clang::Type::Typedef:
2828 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2829 case clang::Type::Elaborated:
2830 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2831 case clang::Type::Paren:
2832 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2833
2834 case clang::Type::LValueReference:
2835 case clang::Type::RValueReference:
2836 {
2837 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2838 if (reference_type)
2839 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2840 }
2841 break;
2842 }
2843 }
2844 return false;
2845
2846}
2847
2848bool
2849ClangASTContext::IsIntegerType (void* type, bool &is_signed)
2850{
2851 if (!type)
2852 return false;
2853
2854 clang::QualType qual_type (GetCanonicalQualType(type));
2855 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
2856
2857 if (builtin_type)
2858 {
2859 if (builtin_type->isInteger())
2860 {
2861 is_signed = builtin_type->isSignedInteger();
2862 return true;
2863 }
2864 }
2865
2866 return false;
2867}
2868
2869bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002870ClangASTContext::IsPointerType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002871{
2872 if (type)
2873 {
2874 clang::QualType qual_type (GetCanonicalQualType(type));
2875 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2876 switch (type_class)
2877 {
2878 case clang::Type::Builtin:
2879 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2880 {
2881 default:
2882 break;
2883 case clang::BuiltinType::ObjCId:
2884 case clang::BuiltinType::ObjCClass:
2885 return true;
2886 }
2887 return false;
2888 case clang::Type::ObjCObjectPointer:
2889 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002890 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002891 return true;
2892 case clang::Type::BlockPointer:
2893 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002894 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002895 return true;
2896 case clang::Type::Pointer:
2897 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002898 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002899 return true;
2900 case clang::Type::MemberPointer:
2901 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002902 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002903 return true;
2904 case clang::Type::Typedef:
2905 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2906 case clang::Type::Elaborated:
2907 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2908 case clang::Type::Paren:
2909 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2910 default:
2911 break;
2912 }
2913 }
2914 if (pointee_type)
2915 pointee_type->Clear();
2916 return false;
2917}
2918
2919
2920bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002921ClangASTContext::IsPointerOrReferenceType (void* type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002922{
2923 if (type)
2924 {
2925 clang::QualType qual_type (GetCanonicalQualType(type));
2926 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2927 switch (type_class)
2928 {
2929 case clang::Type::Builtin:
2930 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
2931 {
2932 default:
2933 break;
2934 case clang::BuiltinType::ObjCId:
2935 case clang::BuiltinType::ObjCClass:
2936 return true;
2937 }
2938 return false;
2939 case clang::Type::ObjCObjectPointer:
2940 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002941 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002942 return true;
2943 case clang::Type::BlockPointer:
2944 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002945 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002946 return true;
2947 case clang::Type::Pointer:
2948 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002949 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002950 return true;
2951 case clang::Type::MemberPointer:
2952 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002953 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002954 return true;
2955 case clang::Type::LValueReference:
2956 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002957 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002958 return true;
2959 case clang::Type::RValueReference:
2960 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002961 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002962 return true;
2963 case clang::Type::Typedef:
2964 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
2965 case clang::Type::Elaborated:
2966 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
2967 case clang::Type::Paren:
2968 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
2969 default:
2970 break;
2971 }
2972 }
2973 if (pointee_type)
2974 pointee_type->Clear();
2975 return false;
2976}
2977
2978
2979bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00002980ClangASTContext::IsReferenceType (void* type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00002981{
2982 if (type)
2983 {
2984 clang::QualType qual_type (GetCanonicalQualType(type));
2985 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2986
2987 switch (type_class)
2988 {
2989 case clang::Type::LValueReference:
2990 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002991 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002992 if (is_rvalue)
2993 *is_rvalue = false;
2994 return true;
2995 case clang::Type::RValueReference:
2996 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00002997 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00002998 if (is_rvalue)
2999 *is_rvalue = true;
3000 return true;
3001 case clang::Type::Typedef:
3002 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3003 case clang::Type::Elaborated:
3004 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3005 case clang::Type::Paren:
3006 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3007
3008 default:
3009 break;
3010 }
3011 }
3012 if (pointee_type)
3013 pointee_type->Clear();
3014 return false;
3015}
3016
3017bool
3018ClangASTContext::IsFloatingPointType (void* type, uint32_t &count, bool &is_complex)
3019{
3020 if (type)
3021 {
3022 clang::QualType qual_type (GetCanonicalQualType(type));
3023
3024 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3025 {
3026 clang::BuiltinType::Kind kind = BT->getKind();
3027 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3028 {
3029 count = 1;
3030 is_complex = false;
3031 return true;
3032 }
3033 }
3034 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3035 {
3036 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3037 {
3038 count = 2;
3039 is_complex = true;
3040 return true;
3041 }
3042 }
3043 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3044 {
3045 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3046 {
3047 count = VT->getNumElements();
3048 is_complex = false;
3049 return true;
3050 }
3051 }
3052 }
3053 count = 0;
3054 is_complex = false;
3055 return false;
3056}
3057
3058
3059bool
3060ClangASTContext::IsDefined(void* type)
3061{
3062 if (!type)
3063 return false;
3064
3065 clang::QualType qual_type(GetQualType(type));
3066 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3067 if (tag_type)
3068 {
3069 clang::TagDecl *tag_decl = tag_type->getDecl();
3070 if (tag_decl)
3071 return tag_decl->isCompleteDefinition();
3072 return false;
3073 }
3074 else
3075 {
3076 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3077 if (objc_class_type)
3078 {
3079 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3080 if (class_interface_decl)
3081 return class_interface_decl->getDefinition() != nullptr;
3082 return false;
3083 }
3084 }
3085 return true;
3086}
3087
3088bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003089ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003090{
3091 if (type)
3092 {
3093 clang::QualType qual_type (GetCanonicalQualType(type));
3094
3095 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3096
3097 if (obj_pointer_type)
3098 return obj_pointer_type->isObjCClassType();
3099 }
3100 return false;
3101}
3102
3103bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003104ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003105{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003106 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003107 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3108 return false;
3109}
3110
3111bool
3112ClangASTContext::IsPolymorphicClass (void* type)
3113{
3114 if (type)
3115 {
3116 clang::QualType qual_type(GetCanonicalQualType(type));
3117 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3118 switch (type_class)
3119 {
3120 case clang::Type::Record:
3121 if (GetCompleteType(type))
3122 {
3123 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3124 const clang::RecordDecl *record_decl = record_type->getDecl();
3125 if (record_decl)
3126 {
3127 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3128 if (cxx_record_decl)
3129 return cxx_record_decl->isPolymorphic();
3130 }
3131 }
3132 break;
3133
3134 default:
3135 break;
3136 }
3137 }
3138 return false;
3139}
3140
3141bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003142ClangASTContext::IsPossibleDynamicType (void* type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003143 bool check_cplusplus,
3144 bool check_objc)
3145{
3146 clang::QualType pointee_qual_type;
3147 if (type)
3148 {
3149 clang::QualType qual_type (GetCanonicalQualType(type));
3150 bool success = false;
3151 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3152 switch (type_class)
3153 {
3154 case clang::Type::Builtin:
3155 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3156 {
3157 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003158 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003159 return true;
3160 }
3161 break;
3162
3163 case clang::Type::ObjCObjectPointer:
3164 if (check_objc)
3165 {
3166 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003167 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003168 return true;
3169 }
3170 break;
3171
3172 case clang::Type::Pointer:
3173 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3174 success = true;
3175 break;
3176
3177 case clang::Type::LValueReference:
3178 case clang::Type::RValueReference:
3179 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3180 success = true;
3181 break;
3182
3183 case clang::Type::Typedef:
3184 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3185 dynamic_pointee_type,
3186 check_cplusplus,
3187 check_objc);
3188
3189 case clang::Type::Elaborated:
3190 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3191 dynamic_pointee_type,
3192 check_cplusplus,
3193 check_objc);
3194
3195 case clang::Type::Paren:
3196 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3197 dynamic_pointee_type,
3198 check_cplusplus,
3199 check_objc);
3200 default:
3201 break;
3202 }
3203
3204 if (success)
3205 {
3206 // Check to make sure what we are pointing too is a possible dynamic C++ type
3207 // We currently accept any "void *" (in case we have a class that has been
3208 // watered down to an opaque pointer) and virtual C++ classes.
3209 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3210 switch (pointee_type_class)
3211 {
3212 case clang::Type::Builtin:
3213 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3214 {
3215 case clang::BuiltinType::UnknownAny:
3216 case clang::BuiltinType::Void:
3217 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003218 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003219 return true;
3220
3221 case clang::BuiltinType::NullPtr:
3222 case clang::BuiltinType::Bool:
3223 case clang::BuiltinType::Char_U:
3224 case clang::BuiltinType::UChar:
3225 case clang::BuiltinType::WChar_U:
3226 case clang::BuiltinType::Char16:
3227 case clang::BuiltinType::Char32:
3228 case clang::BuiltinType::UShort:
3229 case clang::BuiltinType::UInt:
3230 case clang::BuiltinType::ULong:
3231 case clang::BuiltinType::ULongLong:
3232 case clang::BuiltinType::UInt128:
3233 case clang::BuiltinType::Char_S:
3234 case clang::BuiltinType::SChar:
3235 case clang::BuiltinType::WChar_S:
3236 case clang::BuiltinType::Short:
3237 case clang::BuiltinType::Int:
3238 case clang::BuiltinType::Long:
3239 case clang::BuiltinType::LongLong:
3240 case clang::BuiltinType::Int128:
3241 case clang::BuiltinType::Float:
3242 case clang::BuiltinType::Double:
3243 case clang::BuiltinType::LongDouble:
3244 case clang::BuiltinType::Dependent:
3245 case clang::BuiltinType::Overload:
3246 case clang::BuiltinType::ObjCId:
3247 case clang::BuiltinType::ObjCClass:
3248 case clang::BuiltinType::ObjCSel:
3249 case clang::BuiltinType::BoundMember:
3250 case clang::BuiltinType::Half:
3251 case clang::BuiltinType::ARCUnbridgedCast:
3252 case clang::BuiltinType::PseudoObject:
3253 case clang::BuiltinType::BuiltinFn:
3254 case clang::BuiltinType::OCLEvent:
3255 case clang::BuiltinType::OCLImage1d:
3256 case clang::BuiltinType::OCLImage1dArray:
3257 case clang::BuiltinType::OCLImage1dBuffer:
3258 case clang::BuiltinType::OCLImage2d:
3259 case clang::BuiltinType::OCLImage2dArray:
3260 case clang::BuiltinType::OCLImage3d:
3261 case clang::BuiltinType::OCLSampler:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00003262 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00003263 break;
3264 }
3265 break;
3266
3267 case clang::Type::Record:
3268 if (check_cplusplus)
3269 {
3270 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3271 if (cxx_record_decl)
3272 {
3273 bool is_complete = cxx_record_decl->isCompleteDefinition();
3274
3275 if (is_complete)
3276 success = cxx_record_decl->isDynamicClass();
3277 else
3278 {
3279 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3280 if (metadata)
3281 success = metadata->GetIsDynamicCXXType();
3282 else
3283 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003284 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003285 if (is_complete)
3286 success = cxx_record_decl->isDynamicClass();
3287 else
3288 success = false;
3289 }
3290 }
3291
3292 if (success)
3293 {
3294 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003295 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003296 return true;
3297 }
3298 }
3299 }
3300 break;
3301
3302 case clang::Type::ObjCObject:
3303 case clang::Type::ObjCInterface:
3304 if (check_objc)
3305 {
3306 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003307 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003308 return true;
3309 }
3310 break;
3311
3312 default:
3313 break;
3314 }
3315 }
3316 }
3317 if (dynamic_pointee_type)
3318 dynamic_pointee_type->Clear();
3319 return false;
3320}
3321
3322
3323bool
3324ClangASTContext::IsScalarType (void* type)
3325{
3326 if (!type)
3327 return false;
3328
3329 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3330}
3331
3332bool
3333ClangASTContext::IsTypedefType (void* type)
3334{
3335 if (!type)
3336 return false;
3337 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3338}
3339
3340bool
3341ClangASTContext::IsVoidType (void* type)
3342{
3343 if (!type)
3344 return false;
3345 return GetCanonicalQualType(type)->isVoidType();
3346}
3347
3348bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003349ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003350{
3351 if (type)
3352 {
3353 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003354 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003355 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003356 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3357 if (cxx_record_decl)
3358 {
3359 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3360 return true;
3361 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003362 }
3363 }
3364 class_name.clear();
3365 return false;
3366}
3367
3368
3369bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003370ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003371{
3372 if (!type)
3373 return false;
3374
3375 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003376 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003377 return true;
3378 return false;
3379}
3380
3381bool
3382ClangASTContext::IsBeingDefined (void* type)
3383{
3384 if (!type)
3385 return false;
3386 clang::QualType qual_type (GetCanonicalQualType(type));
3387 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3388 if (tag_type)
3389 return tag_type->isBeingDefined();
3390 return false;
3391}
3392
3393bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003394ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003395{
3396 if (!type)
3397 return false;
3398
3399 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003400
3401 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003402 {
3403 if (class_type_ptr)
3404 {
3405 if (!qual_type->isObjCClassType() &&
3406 !qual_type->isObjCIdType())
3407 {
3408 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3409 if (obj_pointer_type == nullptr)
3410 class_type_ptr->Clear();
3411 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003412 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003413 }
3414 }
3415 return true;
3416 }
3417 if (class_type_ptr)
3418 class_type_ptr->Clear();
3419 return false;
3420}
3421
3422bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003423ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003424{
3425 if (!type)
3426 return false;
3427
3428 clang::QualType qual_type (GetCanonicalQualType(type));
3429
3430 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3431 if (object_type)
3432 {
3433 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3434 if (interface)
3435 {
3436 class_name = interface->getNameAsString();
3437 return true;
3438 }
3439 }
3440 return false;
3441}
3442
3443
3444//----------------------------------------------------------------------
3445// Type Completion
3446//----------------------------------------------------------------------
3447
3448bool
3449ClangASTContext::GetCompleteType (void* type)
3450{
3451 if (!type)
3452 return false;
3453 const bool allow_completion = true;
3454 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3455}
3456
3457ConstString
3458ClangASTContext::GetTypeName (void* type)
3459{
3460 std::string type_name;
3461 if (type)
3462 {
3463 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3464 clang::QualType qual_type(GetQualType(type));
3465 printing_policy.SuppressTagKeyword = true;
3466 printing_policy.LangOpts.WChar = true;
3467 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3468 if (typedef_type)
3469 {
3470 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3471 type_name = typedef_decl->getQualifiedNameAsString();
3472 }
3473 else
3474 {
3475 type_name = qual_type.getAsString(printing_policy);
3476 }
3477 }
3478 return ConstString(type_name);
3479}
3480
3481uint32_t
Greg Claytona1e5dc82015-08-11 22:53:00 +00003482ClangASTContext::GetTypeInfo (void* type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003483{
3484 if (!type)
3485 return 0;
3486
3487 if (pointee_or_element_clang_type)
3488 pointee_or_element_clang_type->Clear();
3489
3490 clang::QualType qual_type (GetQualType(type));
3491
3492 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3493 switch (type_class)
3494 {
3495 case clang::Type::Builtin:
3496 {
3497 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3498
3499 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3500 switch (builtin_type->getKind())
3501 {
3502 case clang::BuiltinType::ObjCId:
3503 case clang::BuiltinType::ObjCClass:
3504 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003505 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003506 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3507 break;
3508
3509 case clang::BuiltinType::ObjCSel:
3510 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003511 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003512 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3513 break;
3514
3515 case clang::BuiltinType::Bool:
3516 case clang::BuiltinType::Char_U:
3517 case clang::BuiltinType::UChar:
3518 case clang::BuiltinType::WChar_U:
3519 case clang::BuiltinType::Char16:
3520 case clang::BuiltinType::Char32:
3521 case clang::BuiltinType::UShort:
3522 case clang::BuiltinType::UInt:
3523 case clang::BuiltinType::ULong:
3524 case clang::BuiltinType::ULongLong:
3525 case clang::BuiltinType::UInt128:
3526 case clang::BuiltinType::Char_S:
3527 case clang::BuiltinType::SChar:
3528 case clang::BuiltinType::WChar_S:
3529 case clang::BuiltinType::Short:
3530 case clang::BuiltinType::Int:
3531 case clang::BuiltinType::Long:
3532 case clang::BuiltinType::LongLong:
3533 case clang::BuiltinType::Int128:
3534 case clang::BuiltinType::Float:
3535 case clang::BuiltinType::Double:
3536 case clang::BuiltinType::LongDouble:
3537 builtin_type_flags |= eTypeIsScalar;
3538 if (builtin_type->isInteger())
3539 {
3540 builtin_type_flags |= eTypeIsInteger;
3541 if (builtin_type->isSignedInteger())
3542 builtin_type_flags |= eTypeIsSigned;
3543 }
3544 else if (builtin_type->isFloatingPoint())
3545 builtin_type_flags |= eTypeIsFloat;
3546 break;
3547 default:
3548 break;
3549 }
3550 return builtin_type_flags;
3551 }
3552
3553 case clang::Type::BlockPointer:
3554 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003555 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003556 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3557
3558 case clang::Type::Complex:
3559 {
3560 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3561 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3562 if (complex_type)
3563 {
3564 clang::QualType complex_element_type (complex_type->getElementType());
3565 if (complex_element_type->isIntegerType())
3566 complex_type_flags |= eTypeIsFloat;
3567 else if (complex_element_type->isFloatingType())
3568 complex_type_flags |= eTypeIsInteger;
3569 }
3570 return complex_type_flags;
3571 }
3572 break;
3573
3574 case clang::Type::ConstantArray:
3575 case clang::Type::DependentSizedArray:
3576 case clang::Type::IncompleteArray:
3577 case clang::Type::VariableArray:
3578 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003579 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003580 return eTypeHasChildren | eTypeIsArray;
3581
3582 case clang::Type::DependentName: return 0;
3583 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3584 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3585 case clang::Type::Decltype: return 0;
3586
3587 case clang::Type::Enum:
3588 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003589 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003590 return eTypeIsEnumeration | eTypeHasValue;
3591
3592 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003593 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003594 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003595 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003596
3597 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3598 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3599 case clang::Type::InjectedClassName: return 0;
3600
3601 case clang::Type::LValueReference:
3602 case clang::Type::RValueReference:
3603 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003604 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003605 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3606
3607 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3608
3609 case clang::Type::ObjCObjectPointer:
3610 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003611 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003612 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3613
3614 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3615 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3616
3617 case clang::Type::Pointer:
3618 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003619 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003620 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3621
3622 case clang::Type::Record:
3623 if (qual_type->getAsCXXRecordDecl())
3624 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3625 else
3626 return eTypeHasChildren | eTypeIsStructUnion;
3627 break;
3628 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3629 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3630 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3631
3632 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003633 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 +00003634 case clang::Type::TypeOfExpr: return 0;
3635 case clang::Type::TypeOf: return 0;
3636 case clang::Type::UnresolvedUsing: return 0;
3637
3638 case clang::Type::ExtVector:
3639 case clang::Type::Vector:
3640 {
3641 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3642 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3643 if (vector_type)
3644 {
3645 if (vector_type->isIntegerType())
3646 vector_type_flags |= eTypeIsFloat;
3647 else if (vector_type->isFloatingType())
3648 vector_type_flags |= eTypeIsInteger;
3649 }
3650 return vector_type_flags;
3651 }
3652 default: return 0;
3653 }
3654 return 0;
3655}
3656
3657
3658
3659lldb::LanguageType
3660ClangASTContext::GetMinimumLanguage (void* type)
3661{
3662 if (!type)
3663 return lldb::eLanguageTypeC;
3664
3665 // If the type is a reference, then resolve it to what it refers to first:
3666 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3667 if (qual_type->isAnyPointerType())
3668 {
3669 if (qual_type->isObjCObjectPointerType())
3670 return lldb::eLanguageTypeObjC;
3671
3672 clang::QualType pointee_type (qual_type->getPointeeType());
3673 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3674 return lldb::eLanguageTypeC_plus_plus;
3675 if (pointee_type->isObjCObjectOrInterfaceType())
3676 return lldb::eLanguageTypeObjC;
3677 if (pointee_type->isObjCClassType())
3678 return lldb::eLanguageTypeObjC;
3679 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3680 return lldb::eLanguageTypeObjC;
3681 }
3682 else
3683 {
3684 if (qual_type->isObjCObjectOrInterfaceType())
3685 return lldb::eLanguageTypeObjC;
3686 if (qual_type->getAsCXXRecordDecl())
3687 return lldb::eLanguageTypeC_plus_plus;
3688 switch (qual_type->getTypeClass())
3689 {
3690 default:
3691 break;
3692 case clang::Type::Builtin:
3693 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3694 {
3695 default:
3696 case clang::BuiltinType::Void:
3697 case clang::BuiltinType::Bool:
3698 case clang::BuiltinType::Char_U:
3699 case clang::BuiltinType::UChar:
3700 case clang::BuiltinType::WChar_U:
3701 case clang::BuiltinType::Char16:
3702 case clang::BuiltinType::Char32:
3703 case clang::BuiltinType::UShort:
3704 case clang::BuiltinType::UInt:
3705 case clang::BuiltinType::ULong:
3706 case clang::BuiltinType::ULongLong:
3707 case clang::BuiltinType::UInt128:
3708 case clang::BuiltinType::Char_S:
3709 case clang::BuiltinType::SChar:
3710 case clang::BuiltinType::WChar_S:
3711 case clang::BuiltinType::Short:
3712 case clang::BuiltinType::Int:
3713 case clang::BuiltinType::Long:
3714 case clang::BuiltinType::LongLong:
3715 case clang::BuiltinType::Int128:
3716 case clang::BuiltinType::Float:
3717 case clang::BuiltinType::Double:
3718 case clang::BuiltinType::LongDouble:
3719 break;
3720
3721 case clang::BuiltinType::NullPtr:
3722 return eLanguageTypeC_plus_plus;
3723
3724 case clang::BuiltinType::ObjCId:
3725 case clang::BuiltinType::ObjCClass:
3726 case clang::BuiltinType::ObjCSel:
3727 return eLanguageTypeObjC;
3728
3729 case clang::BuiltinType::Dependent:
3730 case clang::BuiltinType::Overload:
3731 case clang::BuiltinType::BoundMember:
3732 case clang::BuiltinType::UnknownAny:
3733 break;
3734 }
3735 break;
3736 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003737 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003738 }
3739 }
3740 return lldb::eLanguageTypeC;
3741}
3742
3743lldb::TypeClass
3744ClangASTContext::GetTypeClass (void* type)
3745{
3746 if (!type)
3747 return lldb::eTypeClassInvalid;
3748
3749 clang::QualType qual_type(GetQualType(type));
3750
3751 switch (qual_type->getTypeClass())
3752 {
3753 case clang::Type::UnaryTransform: break;
3754 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3755 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3756 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3757 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3758 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3759 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3760 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3761 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3762 case clang::Type::Vector: return lldb::eTypeClassVector;
3763 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3764 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3765 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3766 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3767 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3768 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3769 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3770 case clang::Type::Complex:
3771 if (qual_type->isComplexType())
3772 return lldb::eTypeClassComplexFloat;
3773 else
3774 return lldb::eTypeClassComplexInteger;
3775 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3776 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3777 case clang::Type::Record:
3778 {
3779 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3780 const clang::RecordDecl *record_decl = record_type->getDecl();
3781 if (record_decl->isUnion())
3782 return lldb::eTypeClassUnion;
3783 else if (record_decl->isStruct())
3784 return lldb::eTypeClassStruct;
3785 else
3786 return lldb::eTypeClassClass;
3787 }
3788 break;
3789 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3790 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3791 case clang::Type::UnresolvedUsing: break;
3792 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003793 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003794 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003795 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003796
3797 case clang::Type::Attributed: break;
3798 case clang::Type::TemplateTypeParm: break;
3799 case clang::Type::SubstTemplateTypeParm: break;
3800 case clang::Type::SubstTemplateTypeParmPack:break;
3801 case clang::Type::Auto: break;
3802 case clang::Type::InjectedClassName: break;
3803 case clang::Type::DependentName: break;
3804 case clang::Type::DependentTemplateSpecialization: break;
3805 case clang::Type::PackExpansion: break;
3806
3807 case clang::Type::TypeOfExpr: break;
3808 case clang::Type::TypeOf: break;
3809 case clang::Type::Decltype: break;
3810 case clang::Type::TemplateSpecialization: break;
3811 case clang::Type::Atomic: break;
3812
3813 // pointer type decayed from an array or function type.
3814 case clang::Type::Decayed: break;
3815 case clang::Type::Adjusted: break;
3816 }
3817 // We don't know hot to display this type...
3818 return lldb::eTypeClassOther;
3819
3820}
3821
3822unsigned
3823ClangASTContext::GetTypeQualifiers(void* type)
3824{
3825 if (type)
3826 return GetQualType(type).getQualifiers().getCVRQualifiers();
3827 return 0;
3828}
3829
3830//----------------------------------------------------------------------
3831// Creating related types
3832//----------------------------------------------------------------------
3833
Greg Claytona1e5dc82015-08-11 22:53:00 +00003834CompilerType
3835ClangASTContext::AddConstModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003836{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003837 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003838 {
Greg Claytonf73034f2015-09-08 18:15:05 +00003839 // Make sure this type is a clang AST type
Greg Claytond8d4a572015-08-11 21:38:15 +00003840 clang::QualType result(GetQualType(type));
3841 result.addConst();
Greg Claytona1e5dc82015-08-11 22:53:00 +00003842 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003843 }
Greg Claytonf73034f2015-09-08 18:15:05 +00003844
Greg Claytona1e5dc82015-08-11 22:53:00 +00003845 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003846}
3847
Greg Claytona1e5dc82015-08-11 22:53:00 +00003848CompilerType
3849ClangASTContext::AddRestrictModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003850{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003851 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003852 {
3853 clang::QualType result(GetQualType(type));
3854 result.getQualifiers().setRestrict (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003855 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003856 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003857 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003858}
3859
Greg Claytona1e5dc82015-08-11 22:53:00 +00003860CompilerType
3861ClangASTContext::AddVolatileModifier (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003862{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003863 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003864 {
3865 clang::QualType result(GetQualType(type));
3866 result.getQualifiers().setVolatile (true);
Greg Claytona1e5dc82015-08-11 22:53:00 +00003867 return CompilerType (type.GetTypeSystem(), result.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003868 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003869 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003870}
3871
Greg Claytona1e5dc82015-08-11 22:53:00 +00003872CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003873ClangASTContext::GetArrayElementType (void* type, uint64_t *stride)
3874{
3875 if (type)
3876 {
3877 clang::QualType qual_type(GetCanonicalQualType(type));
3878
3879 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3880
3881 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003882 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003883
Greg Claytona1e5dc82015-08-11 22:53:00 +00003884 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003885
3886 // TODO: the real stride will be >= this value.. find the real one!
3887 if (stride)
3888 *stride = element_type.GetByteSize(nullptr);
3889
3890 return element_type;
3891
3892 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003893 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003894}
3895
Greg Claytona1e5dc82015-08-11 22:53:00 +00003896CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003897ClangASTContext::GetCanonicalType (void* type)
3898{
3899 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003900 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3901 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003902}
3903
3904static clang::QualType
3905GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3906{
3907 if (qual_type->isPointerType())
3908 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3909 else
3910 qual_type = qual_type.getUnqualifiedType();
3911 qual_type.removeLocalConst();
3912 qual_type.removeLocalRestrict();
3913 qual_type.removeLocalVolatile();
3914 return qual_type;
3915}
3916
Greg Claytona1e5dc82015-08-11 22:53:00 +00003917CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003918ClangASTContext::GetFullyUnqualifiedType (void* type)
3919{
3920 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003921 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
3922 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003923}
3924
3925
3926int
3927ClangASTContext::GetFunctionArgumentCount (void* type)
3928{
3929 if (type)
3930 {
3931 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3932 if (func)
3933 return func->getNumParams();
3934 }
3935 return -1;
3936}
3937
Greg Claytona1e5dc82015-08-11 22:53:00 +00003938CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003939ClangASTContext::GetFunctionArgumentTypeAtIndex (void* type, size_t idx)
3940{
3941 if (type)
3942 {
3943 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
3944 if (func)
3945 {
3946 const uint32_t num_args = func->getNumParams();
3947 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003948 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00003949 }
3950 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003951 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003952}
3953
Greg Claytona1e5dc82015-08-11 22:53:00 +00003954CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00003955ClangASTContext::GetFunctionReturnType (void* type)
3956{
3957 if (type)
3958 {
3959 clang::QualType qual_type(GetCanonicalQualType(type));
3960 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3961 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003962 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003963 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003964 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003965}
3966
3967size_t
3968ClangASTContext::GetNumMemberFunctions (void* type)
3969{
3970 size_t num_functions = 0;
3971 if (type)
3972 {
3973 clang::QualType qual_type(GetCanonicalQualType(type));
3974 switch (qual_type->getTypeClass()) {
3975 case clang::Type::Record:
3976 if (GetCompleteQualType (getASTContext(), qual_type))
3977 {
3978 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3979 const clang::RecordDecl *record_decl = record_type->getDecl();
3980 assert(record_decl);
3981 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3982 if (cxx_record_decl)
3983 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
3984 }
3985 break;
3986
3987 case clang::Type::ObjCObjectPointer:
3988 if (GetCompleteType(type))
3989 {
3990 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
3991 if (objc_class_type)
3992 {
3993 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
3994 if (class_interface_decl)
3995 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
3996 }
3997 }
3998 break;
3999
4000 case clang::Type::ObjCObject:
4001 case clang::Type::ObjCInterface:
4002 if (GetCompleteType(type))
4003 {
4004 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4005 if (objc_class_type)
4006 {
4007 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4008 if (class_interface_decl)
4009 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4010 }
4011 }
4012 break;
4013
4014
4015 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004016 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004017
4018 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004019 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004020
4021 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004022 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004023
4024 default:
4025 break;
4026 }
4027 }
4028 return num_functions;
4029}
4030
4031TypeMemberFunctionImpl
4032ClangASTContext::GetMemberFunctionAtIndex (void* type, size_t idx)
4033{
4034 std::string name("");
4035 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004036 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00004037 clang::ObjCMethodDecl *method_decl(nullptr);
4038 if (type)
4039 {
4040 clang::QualType qual_type(GetCanonicalQualType(type));
4041 switch (qual_type->getTypeClass()) {
4042 case clang::Type::Record:
4043 if (GetCompleteQualType (getASTContext(), qual_type))
4044 {
4045 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4046 const clang::RecordDecl *record_decl = record_type->getDecl();
4047 assert(record_decl);
4048 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4049 if (cxx_record_decl)
4050 {
4051 auto method_iter = cxx_record_decl->method_begin();
4052 auto method_end = cxx_record_decl->method_end();
4053 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4054 {
4055 std::advance(method_iter, idx);
4056 auto method_decl = method_iter->getCanonicalDecl();
4057 if (method_decl)
4058 {
4059 if (!method_decl->getName().empty())
4060 name.assign(method_decl->getName().data());
4061 else
4062 name.clear();
4063 if (method_decl->isStatic())
4064 kind = lldb::eMemberFunctionKindStaticMethod;
4065 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4066 kind = lldb::eMemberFunctionKindConstructor;
4067 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4068 kind = lldb::eMemberFunctionKindDestructor;
4069 else
4070 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004071 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004072 }
4073 }
4074 }
4075 }
4076 break;
4077
4078 case clang::Type::ObjCObjectPointer:
4079 if (GetCompleteType(type))
4080 {
4081 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4082 if (objc_class_type)
4083 {
4084 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4085 if (class_interface_decl)
4086 {
4087 auto method_iter = class_interface_decl->meth_begin();
4088 auto method_end = class_interface_decl->meth_end();
4089 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4090 {
4091 std::advance(method_iter, idx);
4092 method_decl = method_iter->getCanonicalDecl();
4093 if (method_decl)
4094 {
4095 name = method_decl->getSelector().getAsString();
4096 if (method_decl->isClassMethod())
4097 kind = lldb::eMemberFunctionKindStaticMethod;
4098 else
4099 kind = lldb::eMemberFunctionKindInstanceMethod;
4100 }
4101 }
4102 }
4103 }
4104 }
4105 break;
4106
4107 case clang::Type::ObjCObject:
4108 case clang::Type::ObjCInterface:
4109 if (GetCompleteType(type))
4110 {
4111 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4112 if (objc_class_type)
4113 {
4114 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4115 if (class_interface_decl)
4116 {
4117 auto method_iter = class_interface_decl->meth_begin();
4118 auto method_end = class_interface_decl->meth_end();
4119 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4120 {
4121 std::advance(method_iter, idx);
4122 method_decl = method_iter->getCanonicalDecl();
4123 if (method_decl)
4124 {
4125 name = method_decl->getSelector().getAsString();
4126 if (method_decl->isClassMethod())
4127 kind = lldb::eMemberFunctionKindStaticMethod;
4128 else
4129 kind = lldb::eMemberFunctionKindInstanceMethod;
4130 }
4131 }
4132 }
4133 }
4134 }
4135 break;
4136
4137 case clang::Type::Typedef:
4138 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4139
4140 case clang::Type::Elaborated:
4141 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4142
4143 case clang::Type::Paren:
4144 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4145
4146 default:
4147 break;
4148 }
4149 }
4150
4151 if (kind == eMemberFunctionKindUnknown)
4152 return TypeMemberFunctionImpl();
4153 if (method_decl)
4154 return TypeMemberFunctionImpl(method_decl, name, kind);
4155 if (type)
4156 return TypeMemberFunctionImpl(clang_type, name, kind);
4157
4158 return TypeMemberFunctionImpl();
4159}
4160
Greg Claytona1e5dc82015-08-11 22:53:00 +00004161CompilerType
4162ClangASTContext::GetLValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004163{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004164 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004165 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004166 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Ryan Brown57bee1e2015-09-14 22:45:11 +00004167 return CompilerType(ast->getASTContext(), ast->getASTContext()->getLValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004168 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004169 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004170}
4171
Greg Claytona1e5dc82015-08-11 22:53:00 +00004172CompilerType
4173ClangASTContext::GetRValueReferenceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004174{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004175 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004176 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004177 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Ryan Brown57bee1e2015-09-14 22:45:11 +00004178 return CompilerType(ast->getASTContext(), ast->getASTContext()->getRValueReferenceType(GetQualType(type)));
Greg Claytond8d4a572015-08-11 21:38:15 +00004179 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004180 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004181}
4182
Greg Claytona1e5dc82015-08-11 22:53:00 +00004183CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004184ClangASTContext::GetNonReferenceType (void* type)
4185{
4186 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004187 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4188 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004189}
4190
Greg Claytona1e5dc82015-08-11 22:53:00 +00004191CompilerType
4192ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004193 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004194 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004195{
4196 if (type && typedef_name && typedef_name[0])
4197 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004198 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004199 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004200 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004201 clang::ASTContext* clang_ast = ast->getASTContext();
4202 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004203
4204 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004205 if (decl_ctx == nullptr)
4206 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004207
Greg Claytond8d4a572015-08-11 21:38:15 +00004208 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4209 decl_ctx,
4210 clang::SourceLocation(),
4211 clang::SourceLocation(),
4212 &clang_ast->Idents.get(typedef_name),
4213 clang_ast->getTrivialTypeSourceInfo(qual_type));
4214
4215 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4216
4217 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004218 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004219 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004220 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004221
4222}
4223
Greg Claytona1e5dc82015-08-11 22:53:00 +00004224CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004225ClangASTContext::GetPointeeType (void* type)
4226{
4227 if (type)
4228 {
4229 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004230 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004231 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004232 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004233}
4234
Greg Claytona1e5dc82015-08-11 22:53:00 +00004235CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004236ClangASTContext::GetPointerType (void* type)
4237{
4238 if (type)
4239 {
4240 clang::QualType qual_type (GetQualType(type));
4241
4242 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4243 switch (type_class)
4244 {
4245 case clang::Type::ObjCObject:
4246 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004247 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004248
4249 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004250 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004251 }
4252 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004253 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004254}
4255
Greg Claytona1e5dc82015-08-11 22:53:00 +00004256CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00004257ClangASTContext::GetTypedefedType (void* type)
4258{
4259 if (type)
4260 {
4261 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4262 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004263 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004264 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004265 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004266}
4267
Greg Claytona1e5dc82015-08-11 22:53:00 +00004268CompilerType
4269ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004270{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004271 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004272 {
4273 clang::QualType qual_type(GetQualType(type));
4274 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004275 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004276 }
4277 return type;
4278}
4279
4280
4281//----------------------------------------------------------------------
4282// Create related types using the current type's AST
4283//----------------------------------------------------------------------
4284
Greg Claytona1e5dc82015-08-11 22:53:00 +00004285CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004286ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004287{
Greg Clayton99558cc42015-08-24 23:46:31 +00004288 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004289}
4290//----------------------------------------------------------------------
4291// Exploring the type
4292//----------------------------------------------------------------------
4293
4294uint64_t
4295ClangASTContext::GetBitSize (void* type, ExecutionContextScope *exe_scope)
4296{
4297 if (GetCompleteType (type))
4298 {
4299 clang::QualType qual_type(GetCanonicalQualType(type));
4300 switch (qual_type->getTypeClass())
4301 {
4302 case clang::Type::ObjCInterface:
4303 case clang::Type::ObjCObject:
4304 {
4305 ExecutionContext exe_ctx (exe_scope);
4306 Process *process = exe_ctx.GetProcessPtr();
4307 if (process)
4308 {
4309 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4310 if (objc_runtime)
4311 {
4312 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004313 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004314 return bit_size;
4315 }
4316 }
4317 else
4318 {
4319 static bool g_printed = false;
4320 if (!g_printed)
4321 {
4322 StreamString s;
4323 DumpTypeDescription(&s);
4324
4325 llvm::outs() << "warning: trying to determine the size of type ";
4326 llvm::outs() << s.GetString() << "\n";
4327 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4328 llvm::outs() << "backtrace:\n";
4329 llvm::sys::PrintStackTrace(llvm::outs());
4330 llvm::outs() << "\n";
4331 g_printed = true;
4332 }
4333 }
4334 }
4335 // fallthrough
4336 default:
4337 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4338 if (bit_size == 0)
4339 {
4340 if (qual_type->isIncompleteArrayType())
4341 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4342 }
4343 if (qual_type->isObjCObjectOrInterfaceType())
4344 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4345 return bit_size;
4346 }
4347 }
4348 return 0;
4349}
4350
4351size_t
4352ClangASTContext::GetTypeBitAlign (void* type)
4353{
4354 if (GetCompleteType(type))
4355 return getASTContext()->getTypeAlign(GetQualType(type));
4356 return 0;
4357}
4358
4359
4360lldb::Encoding
4361ClangASTContext::GetEncoding (void* type, uint64_t &count)
4362{
4363 if (!type)
4364 return lldb::eEncodingInvalid;
4365
4366 count = 1;
4367 clang::QualType qual_type(GetCanonicalQualType(type));
4368
4369 switch (qual_type->getTypeClass())
4370 {
4371 case clang::Type::UnaryTransform:
4372 break;
4373
4374 case clang::Type::FunctionNoProto:
4375 case clang::Type::FunctionProto:
4376 break;
4377
4378 case clang::Type::IncompleteArray:
4379 case clang::Type::VariableArray:
4380 break;
4381
4382 case clang::Type::ConstantArray:
4383 break;
4384
4385 case clang::Type::ExtVector:
4386 case clang::Type::Vector:
4387 // TODO: Set this to more than one???
4388 break;
4389
4390 case clang::Type::Builtin:
4391 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4392 {
4393 default: assert(0 && "Unknown builtin type!");
4394 case clang::BuiltinType::Void:
4395 break;
4396
4397 case clang::BuiltinType::Bool:
4398 case clang::BuiltinType::Char_S:
4399 case clang::BuiltinType::SChar:
4400 case clang::BuiltinType::WChar_S:
4401 case clang::BuiltinType::Char16:
4402 case clang::BuiltinType::Char32:
4403 case clang::BuiltinType::Short:
4404 case clang::BuiltinType::Int:
4405 case clang::BuiltinType::Long:
4406 case clang::BuiltinType::LongLong:
4407 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4408
4409 case clang::BuiltinType::Char_U:
4410 case clang::BuiltinType::UChar:
4411 case clang::BuiltinType::WChar_U:
4412 case clang::BuiltinType::UShort:
4413 case clang::BuiltinType::UInt:
4414 case clang::BuiltinType::ULong:
4415 case clang::BuiltinType::ULongLong:
4416 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4417
4418 case clang::BuiltinType::Float:
4419 case clang::BuiltinType::Double:
4420 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4421
4422 case clang::BuiltinType::ObjCClass:
4423 case clang::BuiltinType::ObjCId:
4424 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4425
4426 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4427
4428 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4429 case clang::BuiltinType::Kind::BoundMember:
4430 case clang::BuiltinType::Kind::BuiltinFn:
4431 case clang::BuiltinType::Kind::Dependent:
4432 case clang::BuiltinType::Kind::Half:
4433 case clang::BuiltinType::Kind::OCLEvent:
4434 case clang::BuiltinType::Kind::OCLImage1d:
4435 case clang::BuiltinType::Kind::OCLImage1dArray:
4436 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4437 case clang::BuiltinType::Kind::OCLImage2d:
4438 case clang::BuiltinType::Kind::OCLImage2dArray:
4439 case clang::BuiltinType::Kind::OCLImage3d:
4440 case clang::BuiltinType::Kind::OCLSampler:
4441 case clang::BuiltinType::Kind::Overload:
4442 case clang::BuiltinType::Kind::PseudoObject:
4443 case clang::BuiltinType::Kind::UnknownAny:
4444 break;
4445 }
4446 break;
4447 // All pointer types are represented as unsigned integer encodings.
4448 // We may nee to add a eEncodingPointer if we ever need to know the
4449 // difference
4450 case clang::Type::ObjCObjectPointer:
4451 case clang::Type::BlockPointer:
4452 case clang::Type::Pointer:
4453 case clang::Type::LValueReference:
4454 case clang::Type::RValueReference:
4455 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4456 case clang::Type::Complex:
4457 {
4458 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4459 if (qual_type->isComplexType())
4460 encoding = lldb::eEncodingIEEE754;
4461 else
4462 {
4463 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4464 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004465 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004466 else
4467 encoding = lldb::eEncodingSint;
4468 }
4469 count = 2;
4470 return encoding;
4471 }
4472
4473 case clang::Type::ObjCInterface: break;
4474 case clang::Type::Record: break;
4475 case clang::Type::Enum: return lldb::eEncodingSint;
4476 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004477 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004478
4479 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004480 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004481
4482 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004483 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004484
4485 case clang::Type::DependentSizedArray:
4486 case clang::Type::DependentSizedExtVector:
4487 case clang::Type::UnresolvedUsing:
4488 case clang::Type::Attributed:
4489 case clang::Type::TemplateTypeParm:
4490 case clang::Type::SubstTemplateTypeParm:
4491 case clang::Type::SubstTemplateTypeParmPack:
4492 case clang::Type::Auto:
4493 case clang::Type::InjectedClassName:
4494 case clang::Type::DependentName:
4495 case clang::Type::DependentTemplateSpecialization:
4496 case clang::Type::PackExpansion:
4497 case clang::Type::ObjCObject:
4498
4499 case clang::Type::TypeOfExpr:
4500 case clang::Type::TypeOf:
4501 case clang::Type::Decltype:
4502 case clang::Type::TemplateSpecialization:
4503 case clang::Type::Atomic:
4504 case clang::Type::Adjusted:
4505 break;
4506
4507 // pointer type decayed from an array or function type.
4508 case clang::Type::Decayed:
4509 break;
4510 }
4511 count = 0;
4512 return lldb::eEncodingInvalid;
4513}
4514
4515lldb::Format
4516ClangASTContext::GetFormat (void* type)
4517{
4518 if (!type)
4519 return lldb::eFormatDefault;
4520
4521 clang::QualType qual_type(GetCanonicalQualType(type));
4522
4523 switch (qual_type->getTypeClass())
4524 {
4525 case clang::Type::UnaryTransform:
4526 break;
4527
4528 case clang::Type::FunctionNoProto:
4529 case clang::Type::FunctionProto:
4530 break;
4531
4532 case clang::Type::IncompleteArray:
4533 case clang::Type::VariableArray:
4534 break;
4535
4536 case clang::Type::ConstantArray:
4537 return lldb::eFormatVoid; // no value
4538
4539 case clang::Type::ExtVector:
4540 case clang::Type::Vector:
4541 break;
4542
4543 case clang::Type::Builtin:
4544 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4545 {
4546 //default: assert(0 && "Unknown builtin type!");
4547 case clang::BuiltinType::UnknownAny:
4548 case clang::BuiltinType::Void:
4549 case clang::BuiltinType::BoundMember:
4550 break;
4551
4552 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4553 case clang::BuiltinType::Char_S:
4554 case clang::BuiltinType::SChar:
4555 case clang::BuiltinType::WChar_S:
4556 case clang::BuiltinType::Char_U:
4557 case clang::BuiltinType::UChar:
4558 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4559 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4560 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4561 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4562 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4563 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4564 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4565 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4566 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4567 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4568 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4569 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4570 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
4571 case clang::BuiltinType::Float: return lldb::eFormatFloat;
4572 case clang::BuiltinType::Double: return lldb::eFormatFloat;
4573 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
4574 case clang::BuiltinType::NullPtr:
4575 case clang::BuiltinType::Overload:
4576 case clang::BuiltinType::Dependent:
4577 case clang::BuiltinType::ObjCId:
4578 case clang::BuiltinType::ObjCClass:
4579 case clang::BuiltinType::ObjCSel:
4580 case clang::BuiltinType::Half:
4581 case clang::BuiltinType::ARCUnbridgedCast:
4582 case clang::BuiltinType::PseudoObject:
4583 case clang::BuiltinType::BuiltinFn:
4584 case clang::BuiltinType::OCLEvent:
4585 case clang::BuiltinType::OCLImage1d:
4586 case clang::BuiltinType::OCLImage1dArray:
4587 case clang::BuiltinType::OCLImage1dBuffer:
4588 case clang::BuiltinType::OCLImage2d:
4589 case clang::BuiltinType::OCLImage2dArray:
4590 case clang::BuiltinType::OCLImage3d:
4591 case clang::BuiltinType::OCLSampler:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00004592 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00004593 return lldb::eFormatHex;
4594 }
4595 break;
4596 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4597 case clang::Type::BlockPointer: return lldb::eFormatHex;
4598 case clang::Type::Pointer: return lldb::eFormatHex;
4599 case clang::Type::LValueReference:
4600 case clang::Type::RValueReference: return lldb::eFormatHex;
4601 case clang::Type::MemberPointer: break;
4602 case clang::Type::Complex:
4603 {
4604 if (qual_type->isComplexType())
4605 return lldb::eFormatComplex;
4606 else
4607 return lldb::eFormatComplexInteger;
4608 }
4609 case clang::Type::ObjCInterface: break;
4610 case clang::Type::Record: break;
4611 case clang::Type::Enum: return lldb::eFormatEnum;
4612 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004613 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004614 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004615 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004616 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004617 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004618 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004619 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004620 case clang::Type::DependentSizedArray:
4621 case clang::Type::DependentSizedExtVector:
4622 case clang::Type::UnresolvedUsing:
4623 case clang::Type::Attributed:
4624 case clang::Type::TemplateTypeParm:
4625 case clang::Type::SubstTemplateTypeParm:
4626 case clang::Type::SubstTemplateTypeParmPack:
4627 case clang::Type::InjectedClassName:
4628 case clang::Type::DependentName:
4629 case clang::Type::DependentTemplateSpecialization:
4630 case clang::Type::PackExpansion:
4631 case clang::Type::ObjCObject:
4632
4633 case clang::Type::TypeOfExpr:
4634 case clang::Type::TypeOf:
4635 case clang::Type::Decltype:
4636 case clang::Type::TemplateSpecialization:
4637 case clang::Type::Atomic:
4638 case clang::Type::Adjusted:
4639 break;
4640
4641 // pointer type decayed from an array or function type.
4642 case clang::Type::Decayed:
4643 break;
4644 }
4645 // We don't know hot to display this type...
4646 return lldb::eFormatBytes;
4647}
4648
4649static bool
4650ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4651{
4652 while (class_interface_decl)
4653 {
4654 if (class_interface_decl->ivar_size() > 0)
4655 return true;
4656
4657 if (check_superclass)
4658 class_interface_decl = class_interface_decl->getSuperClass();
4659 else
4660 break;
4661 }
4662 return false;
4663}
4664
4665uint32_t
4666ClangASTContext::GetNumChildren (void* type, bool omit_empty_base_classes)
4667{
4668 if (!type)
4669 return 0;
4670
4671 uint32_t num_children = 0;
4672 clang::QualType qual_type(GetQualType(type));
4673 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4674 switch (type_class)
4675 {
4676 case clang::Type::Builtin:
4677 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4678 {
4679 case clang::BuiltinType::ObjCId: // child is Class
4680 case clang::BuiltinType::ObjCClass: // child is Class
4681 num_children = 1;
4682 break;
4683
4684 default:
4685 break;
4686 }
4687 break;
4688
4689 case clang::Type::Complex: return 0;
4690
4691 case clang::Type::Record:
4692 if (GetCompleteQualType (getASTContext(), qual_type))
4693 {
4694 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4695 const clang::RecordDecl *record_decl = record_type->getDecl();
4696 assert(record_decl);
4697 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4698 if (cxx_record_decl)
4699 {
4700 if (omit_empty_base_classes)
4701 {
4702 // Check each base classes to see if it or any of its
4703 // base classes contain any fields. This can help
4704 // limit the noise in variable views by not having to
4705 // show base classes that contain no members.
4706 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4707 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4708 base_class != base_class_end;
4709 ++base_class)
4710 {
4711 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4712
4713 // Skip empty base classes
4714 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4715 continue;
4716
4717 num_children++;
4718 }
4719 }
4720 else
4721 {
4722 // Include all base classes
4723 num_children += cxx_record_decl->getNumBases();
4724 }
4725
4726 }
4727 clang::RecordDecl::field_iterator field, field_end;
4728 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4729 ++num_children;
4730 }
4731 break;
4732
4733 case clang::Type::ObjCObject:
4734 case clang::Type::ObjCInterface:
4735 if (GetCompleteQualType (getASTContext(), qual_type))
4736 {
4737 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4738 assert (objc_class_type);
4739 if (objc_class_type)
4740 {
4741 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4742
4743 if (class_interface_decl)
4744 {
4745
4746 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4747 if (superclass_interface_decl)
4748 {
4749 if (omit_empty_base_classes)
4750 {
4751 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4752 ++num_children;
4753 }
4754 else
4755 ++num_children;
4756 }
4757
4758 num_children += class_interface_decl->ivar_size();
4759 }
4760 }
4761 }
4762 break;
4763
4764 case clang::Type::ObjCObjectPointer:
4765 {
4766 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4767 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004768 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004769 // If this type points to a simple type, then it has 1 child
4770 if (num_pointee_children == 0)
4771 num_children = 1;
4772 else
4773 num_children = num_pointee_children;
4774 }
4775 break;
4776
4777 case clang::Type::Vector:
4778 case clang::Type::ExtVector:
4779 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4780 break;
4781
4782 case clang::Type::ConstantArray:
4783 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4784 break;
4785
4786 case clang::Type::Pointer:
4787 {
4788 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4789 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004790 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004791 if (num_pointee_children == 0)
4792 {
4793 // We have a pointer to a pointee type that claims it has no children.
4794 // We will want to look at
4795 num_children = GetNumPointeeChildren (pointee_type);
4796 }
4797 else
4798 num_children = num_pointee_children;
4799 }
4800 break;
4801
4802 case clang::Type::LValueReference:
4803 case clang::Type::RValueReference:
4804 {
4805 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4806 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004807 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004808 // If this type points to a simple type, then it has 1 child
4809 if (num_pointee_children == 0)
4810 num_children = 1;
4811 else
4812 num_children = num_pointee_children;
4813 }
4814 break;
4815
4816
4817 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004818 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004819 break;
4820
4821 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004822 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004823 break;
4824
4825 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004826 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004827 break;
4828 default:
4829 break;
4830 }
4831 return num_children;
4832}
4833
4834lldb::BasicType
4835ClangASTContext::GetBasicTypeEnumeration (void* type)
4836{
4837 if (type)
4838 {
4839 clang::QualType qual_type(GetQualType(type));
4840 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4841 if (type_class == clang::Type::Builtin)
4842 {
4843 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4844 {
4845 case clang::BuiltinType::Void: return eBasicTypeVoid;
4846 case clang::BuiltinType::Bool: return eBasicTypeBool;
4847 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4848 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4849 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4850 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4851 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4852 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4853 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
4854 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
4855 case clang::BuiltinType::Short: return eBasicTypeShort;
4856 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
4857 case clang::BuiltinType::Int: return eBasicTypeInt;
4858 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
4859 case clang::BuiltinType::Long: return eBasicTypeLong;
4860 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
4861 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
4862 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
4863 case clang::BuiltinType::Int128: return eBasicTypeInt128;
4864 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
4865
4866 case clang::BuiltinType::Half: return eBasicTypeHalf;
4867 case clang::BuiltinType::Float: return eBasicTypeFloat;
4868 case clang::BuiltinType::Double: return eBasicTypeDouble;
4869 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
4870
4871 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
4872 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
4873 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
4874 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
4875 case clang::BuiltinType::Dependent:
4876 case clang::BuiltinType::Overload:
4877 case clang::BuiltinType::BoundMember:
4878 case clang::BuiltinType::PseudoObject:
4879 case clang::BuiltinType::UnknownAny:
4880 case clang::BuiltinType::BuiltinFn:
4881 case clang::BuiltinType::ARCUnbridgedCast:
4882 case clang::BuiltinType::OCLEvent:
4883 case clang::BuiltinType::OCLImage1d:
4884 case clang::BuiltinType::OCLImage1dArray:
4885 case clang::BuiltinType::OCLImage1dBuffer:
4886 case clang::BuiltinType::OCLImage2d:
4887 case clang::BuiltinType::OCLImage2dArray:
4888 case clang::BuiltinType::OCLImage3d:
4889 case clang::BuiltinType::OCLSampler:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00004890 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00004891 return eBasicTypeOther;
4892 }
4893 }
4894 }
4895 return eBasicTypeInvalid;
4896}
4897
Greg Clayton99558cc42015-08-24 23:46:31 +00004898void
4899ClangASTContext::ForEachEnumerator (void* type, std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback)
4900{
4901 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
4902 if (enum_type)
4903 {
4904 const clang::EnumDecl *enum_decl = enum_type->getDecl();
4905 if (enum_decl)
4906 {
4907 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
4908
4909 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
4910 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
4911 {
4912 ConstString name(enum_pos->getNameAsString().c_str());
4913 if (!callback (integer_type, name, enum_pos->getInitVal()))
4914 break;
4915 }
4916 }
4917 }
4918}
4919
Greg Claytond8d4a572015-08-11 21:38:15 +00004920
4921#pragma mark Aggregate Types
4922
4923uint32_t
Greg Claytond8d4a572015-08-11 21:38:15 +00004924ClangASTContext::GetNumFields (void* type)
4925{
4926 if (!type)
4927 return 0;
4928
4929 uint32_t count = 0;
4930 clang::QualType qual_type(GetCanonicalQualType(type));
4931 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4932 switch (type_class)
4933 {
4934 case clang::Type::Record:
4935 if (GetCompleteType(type))
4936 {
4937 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
4938 if (record_type)
4939 {
4940 clang::RecordDecl *record_decl = record_type->getDecl();
4941 if (record_decl)
4942 {
4943 uint32_t field_idx = 0;
4944 clang::RecordDecl::field_iterator field, field_end;
4945 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4946 ++field_idx;
4947 count = field_idx;
4948 }
4949 }
4950 }
4951 break;
4952
4953 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004954 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004955 break;
4956
4957 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004958 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004959 break;
4960
4961 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004962 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00004963 break;
4964
4965 case clang::Type::ObjCObjectPointer:
4966 if (GetCompleteType(type))
4967 {
4968 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4969 if (objc_class_type)
4970 {
4971 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4972
4973 if (class_interface_decl)
4974 count = class_interface_decl->ivar_size();
4975 }
4976 }
4977 break;
4978
4979 case clang::Type::ObjCObject:
4980 case clang::Type::ObjCInterface:
4981 if (GetCompleteType(type))
4982 {
4983 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4984 if (objc_class_type)
4985 {
4986 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4987
4988 if (class_interface_decl)
4989 count = class_interface_decl->ivar_size();
4990 }
4991 }
4992 break;
4993
4994 default:
4995 break;
4996 }
4997 return count;
4998}
4999
Greg Claytond8d4a572015-08-11 21:38:15 +00005000static clang_type_t
5001GetObjCFieldAtIndex (clang::ASTContext *ast,
5002 clang::ObjCInterfaceDecl *class_interface_decl,
5003 size_t idx,
5004 std::string& name,
5005 uint64_t *bit_offset_ptr,
5006 uint32_t *bitfield_bit_size_ptr,
5007 bool *is_bitfield_ptr)
5008{
5009 if (class_interface_decl)
5010 {
5011 if (idx < (class_interface_decl->ivar_size()))
5012 {
5013 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5014 uint32_t ivar_idx = 0;
5015
5016 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5017 {
5018 if (ivar_idx == idx)
5019 {
5020 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5021
5022 clang::QualType ivar_qual_type(ivar_decl->getType());
5023
5024 name.assign(ivar_decl->getNameAsString());
5025
5026 if (bit_offset_ptr)
5027 {
5028 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5029 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5030 }
5031
5032 const bool is_bitfield = ivar_pos->isBitField();
5033
5034 if (bitfield_bit_size_ptr)
5035 {
5036 *bitfield_bit_size_ptr = 0;
5037
5038 if (is_bitfield && ast)
5039 {
5040 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5041 llvm::APSInt bitfield_apsint;
5042 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5043 {
5044 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5045 }
5046 }
5047 }
5048 if (is_bitfield_ptr)
5049 *is_bitfield_ptr = is_bitfield;
5050
5051 return ivar_qual_type.getAsOpaquePtr();
5052 }
5053 }
5054 }
5055 }
5056 return nullptr;
5057}
5058
Greg Claytona1e5dc82015-08-11 22:53:00 +00005059CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00005060ClangASTContext::GetFieldAtIndex (void* type, size_t idx,
5061 std::string& name,
5062 uint64_t *bit_offset_ptr,
5063 uint32_t *bitfield_bit_size_ptr,
5064 bool *is_bitfield_ptr)
5065{
5066 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005067 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005068
5069 clang::QualType qual_type(GetCanonicalQualType(type));
5070 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5071 switch (type_class)
5072 {
5073 case clang::Type::Record:
5074 if (GetCompleteType(type))
5075 {
5076 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5077 const clang::RecordDecl *record_decl = record_type->getDecl();
5078 uint32_t field_idx = 0;
5079 clang::RecordDecl::field_iterator field, field_end;
5080 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5081 {
5082 if (idx == field_idx)
5083 {
5084 // Print the member type if requested
5085 // Print the member name and equal sign
5086 name.assign(field->getNameAsString());
5087
5088 // Figure out the type byte size (field_type_info.first) and
5089 // alignment (field_type_info.second) from the AST context.
5090 if (bit_offset_ptr)
5091 {
5092 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5093 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5094 }
5095
5096 const bool is_bitfield = field->isBitField();
5097
5098 if (bitfield_bit_size_ptr)
5099 {
5100 *bitfield_bit_size_ptr = 0;
5101
5102 if (is_bitfield)
5103 {
5104 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5105 llvm::APSInt bitfield_apsint;
5106 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5107 {
5108 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5109 }
5110 }
5111 }
5112 if (is_bitfield_ptr)
5113 *is_bitfield_ptr = is_bitfield;
5114
Greg Claytona1e5dc82015-08-11 22:53:00 +00005115 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005116 }
5117 }
5118 }
5119 break;
5120
5121 case clang::Type::ObjCObjectPointer:
5122 if (GetCompleteType(type))
5123 {
5124 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5125 if (objc_class_type)
5126 {
5127 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005128 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 +00005129 }
5130 }
5131 break;
5132
5133 case clang::Type::ObjCObject:
5134 case clang::Type::ObjCInterface:
5135 if (GetCompleteType(type))
5136 {
5137 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5138 assert (objc_class_type);
5139 if (objc_class_type)
5140 {
5141 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005142 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 +00005143 }
5144 }
5145 break;
5146
5147
5148 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005149 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005150 GetFieldAtIndex (idx,
5151 name,
5152 bit_offset_ptr,
5153 bitfield_bit_size_ptr,
5154 is_bitfield_ptr);
5155
5156 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005157 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005158 GetFieldAtIndex (idx,
5159 name,
5160 bit_offset_ptr,
5161 bitfield_bit_size_ptr,
5162 is_bitfield_ptr);
5163
5164 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005165 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005166 GetFieldAtIndex (idx,
5167 name,
5168 bit_offset_ptr,
5169 bitfield_bit_size_ptr,
5170 is_bitfield_ptr);
5171
5172 default:
5173 break;
5174 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005175 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005176}
5177
Greg Clayton99558cc42015-08-24 23:46:31 +00005178uint32_t
5179ClangASTContext::GetNumDirectBaseClasses (void *type)
5180{
5181 uint32_t count = 0;
5182 clang::QualType qual_type(GetCanonicalQualType(type));
5183 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5184 switch (type_class)
5185 {
5186 case clang::Type::Record:
5187 if (GetCompleteType(type))
5188 {
5189 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5190 if (cxx_record_decl)
5191 count = cxx_record_decl->getNumBases();
5192 }
5193 break;
5194
5195 case clang::Type::ObjCObjectPointer:
5196 count = GetPointeeType(type).GetNumDirectBaseClasses();
5197 break;
5198
5199 case clang::Type::ObjCObject:
5200 if (GetCompleteType(type))
5201 {
5202 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5203 if (objc_class_type)
5204 {
5205 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5206
5207 if (class_interface_decl && class_interface_decl->getSuperClass())
5208 count = 1;
5209 }
5210 }
5211 break;
5212 case clang::Type::ObjCInterface:
5213 if (GetCompleteType(type))
5214 {
5215 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5216 if (objc_interface_type)
5217 {
5218 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5219
5220 if (class_interface_decl && class_interface_decl->getSuperClass())
5221 count = 1;
5222 }
5223 }
5224 break;
5225
5226
5227 case clang::Type::Typedef:
5228 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5229 break;
5230
5231 case clang::Type::Elaborated:
5232 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5233 break;
5234
5235 case clang::Type::Paren:
5236 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5237
5238 default:
5239 break;
5240 }
5241 return count;
5242
5243}
5244
5245uint32_t
5246ClangASTContext::GetNumVirtualBaseClasses (void *type)
5247{
5248 uint32_t count = 0;
5249 clang::QualType qual_type(GetCanonicalQualType(type));
5250 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5251 switch (type_class)
5252 {
5253 case clang::Type::Record:
5254 if (GetCompleteType(type))
5255 {
5256 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5257 if (cxx_record_decl)
5258 count = cxx_record_decl->getNumVBases();
5259 }
5260 break;
5261
5262 case clang::Type::Typedef:
5263 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5264 break;
5265
5266 case clang::Type::Elaborated:
5267 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5268 break;
5269
5270 case clang::Type::Paren:
5271 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5272 break;
5273
5274 default:
5275 break;
5276 }
5277 return count;
5278
5279}
5280
5281CompilerType
5282ClangASTContext::GetDirectBaseClassAtIndex (void *type, size_t idx, uint32_t *bit_offset_ptr)
5283{
5284 clang::QualType qual_type(GetCanonicalQualType(type));
5285 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5286 switch (type_class)
5287 {
5288 case clang::Type::Record:
5289 if (GetCompleteType(type))
5290 {
5291 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5292 if (cxx_record_decl)
5293 {
5294 uint32_t curr_idx = 0;
5295 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5296 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5297 base_class != base_class_end;
5298 ++base_class, ++curr_idx)
5299 {
5300 if (curr_idx == idx)
5301 {
5302 if (bit_offset_ptr)
5303 {
5304 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5305 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5306 if (base_class->isVirtual())
5307 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5308 else
5309 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5310 }
5311 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5312 }
5313 }
5314 }
5315 }
5316 break;
5317
5318 case clang::Type::ObjCObjectPointer:
5319 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5320
5321 case clang::Type::ObjCObject:
5322 if (idx == 0 && GetCompleteType(type))
5323 {
5324 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5325 if (objc_class_type)
5326 {
5327 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5328
5329 if (class_interface_decl)
5330 {
5331 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5332 if (superclass_interface_decl)
5333 {
5334 if (bit_offset_ptr)
5335 *bit_offset_ptr = 0;
5336 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5337 }
5338 }
5339 }
5340 }
5341 break;
5342 case clang::Type::ObjCInterface:
5343 if (idx == 0 && GetCompleteType(type))
5344 {
5345 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5346 if (objc_interface_type)
5347 {
5348 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5349
5350 if (class_interface_decl)
5351 {
5352 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5353 if (superclass_interface_decl)
5354 {
5355 if (bit_offset_ptr)
5356 *bit_offset_ptr = 0;
5357 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5358 }
5359 }
5360 }
5361 }
5362 break;
5363
5364
5365 case clang::Type::Typedef:
5366 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5367
5368 case clang::Type::Elaborated:
5369 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5370
5371 case clang::Type::Paren:
5372 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5373
5374 default:
5375 break;
5376 }
5377 return CompilerType();
5378}
5379
5380CompilerType
5381ClangASTContext::GetVirtualBaseClassAtIndex (void *type,
5382 size_t idx,
5383 uint32_t *bit_offset_ptr)
5384{
5385 clang::QualType qual_type(GetCanonicalQualType(type));
5386 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5387 switch (type_class)
5388 {
5389 case clang::Type::Record:
5390 if (GetCompleteType(type))
5391 {
5392 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5393 if (cxx_record_decl)
5394 {
5395 uint32_t curr_idx = 0;
5396 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5397 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5398 base_class != base_class_end;
5399 ++base_class, ++curr_idx)
5400 {
5401 if (curr_idx == idx)
5402 {
5403 if (bit_offset_ptr)
5404 {
5405 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5406 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5407 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5408
5409 }
5410 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5411 }
5412 }
5413 }
5414 }
5415 break;
5416
5417 case clang::Type::Typedef:
5418 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5419
5420 case clang::Type::Elaborated:
5421 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5422
5423 case clang::Type::Paren:
5424 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5425
5426 default:
5427 break;
5428 }
5429 return CompilerType();
5430
5431}
5432
Greg Claytond8d4a572015-08-11 21:38:15 +00005433// If a pointer to a pointee type (the clang_type arg) says that it has no
5434// children, then we either need to trust it, or override it and return a
5435// different result. For example, an "int *" has one child that is an integer,
5436// but a function pointer doesn't have any children. Likewise if a Record type
5437// claims it has no children, then there really is nothing to show.
5438uint32_t
5439ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5440{
5441 if (type.isNull())
5442 return 0;
5443
5444 clang::QualType qual_type(type.getCanonicalType());
5445 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5446 switch (type_class)
5447 {
5448 case clang::Type::Builtin:
5449 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5450 {
5451 case clang::BuiltinType::UnknownAny:
5452 case clang::BuiltinType::Void:
5453 case clang::BuiltinType::NullPtr:
5454 case clang::BuiltinType::OCLEvent:
5455 case clang::BuiltinType::OCLImage1d:
5456 case clang::BuiltinType::OCLImage1dArray:
5457 case clang::BuiltinType::OCLImage1dBuffer:
5458 case clang::BuiltinType::OCLImage2d:
5459 case clang::BuiltinType::OCLImage2dArray:
5460 case clang::BuiltinType::OCLImage3d:
5461 case clang::BuiltinType::OCLSampler:
5462 return 0;
5463 case clang::BuiltinType::Bool:
5464 case clang::BuiltinType::Char_U:
5465 case clang::BuiltinType::UChar:
5466 case clang::BuiltinType::WChar_U:
5467 case clang::BuiltinType::Char16:
5468 case clang::BuiltinType::Char32:
5469 case clang::BuiltinType::UShort:
5470 case clang::BuiltinType::UInt:
5471 case clang::BuiltinType::ULong:
5472 case clang::BuiltinType::ULongLong:
5473 case clang::BuiltinType::UInt128:
5474 case clang::BuiltinType::Char_S:
5475 case clang::BuiltinType::SChar:
5476 case clang::BuiltinType::WChar_S:
5477 case clang::BuiltinType::Short:
5478 case clang::BuiltinType::Int:
5479 case clang::BuiltinType::Long:
5480 case clang::BuiltinType::LongLong:
5481 case clang::BuiltinType::Int128:
5482 case clang::BuiltinType::Float:
5483 case clang::BuiltinType::Double:
5484 case clang::BuiltinType::LongDouble:
5485 case clang::BuiltinType::Dependent:
5486 case clang::BuiltinType::Overload:
5487 case clang::BuiltinType::ObjCId:
5488 case clang::BuiltinType::ObjCClass:
5489 case clang::BuiltinType::ObjCSel:
5490 case clang::BuiltinType::BoundMember:
5491 case clang::BuiltinType::Half:
5492 case clang::BuiltinType::ARCUnbridgedCast:
5493 case clang::BuiltinType::PseudoObject:
5494 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005495 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005496 return 1;
5497 }
5498 break;
5499
5500 case clang::Type::Complex: return 1;
5501 case clang::Type::Pointer: return 1;
5502 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5503 case clang::Type::LValueReference: return 1;
5504 case clang::Type::RValueReference: return 1;
5505 case clang::Type::MemberPointer: return 0;
5506 case clang::Type::ConstantArray: return 0;
5507 case clang::Type::IncompleteArray: return 0;
5508 case clang::Type::VariableArray: return 0;
5509 case clang::Type::DependentSizedArray: return 0;
5510 case clang::Type::DependentSizedExtVector: return 0;
5511 case clang::Type::Vector: return 0;
5512 case clang::Type::ExtVector: return 0;
5513 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5514 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5515 case clang::Type::UnresolvedUsing: return 0;
5516 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5517 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5518 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5519 case clang::Type::TypeOfExpr: return 0;
5520 case clang::Type::TypeOf: return 0;
5521 case clang::Type::Decltype: return 0;
5522 case clang::Type::Record: return 0;
5523 case clang::Type::Enum: return 1;
5524 case clang::Type::TemplateTypeParm: return 1;
5525 case clang::Type::SubstTemplateTypeParm: return 1;
5526 case clang::Type::TemplateSpecialization: return 1;
5527 case clang::Type::InjectedClassName: return 0;
5528 case clang::Type::DependentName: return 1;
5529 case clang::Type::DependentTemplateSpecialization: return 1;
5530 case clang::Type::ObjCObject: return 0;
5531 case clang::Type::ObjCInterface: return 0;
5532 case clang::Type::ObjCObjectPointer: return 1;
5533 default:
5534 break;
5535 }
5536 return 0;
5537}
5538
5539
Greg Claytona1e5dc82015-08-11 22:53:00 +00005540CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00005541ClangASTContext::GetChildClangTypeAtIndex (void* type,
5542 ExecutionContext *exe_ctx,
5543 size_t idx,
5544 bool transparent_pointers,
5545 bool omit_empty_base_classes,
5546 bool ignore_array_bounds,
5547 std::string& child_name,
5548 uint32_t &child_byte_size,
5549 int32_t &child_byte_offset,
5550 uint32_t &child_bitfield_bit_size,
5551 uint32_t &child_bitfield_bit_offset,
5552 bool &child_is_base_class,
5553 bool &child_is_deref_of_parent,
5554 ValueObject *valobj)
Greg Claytond8d4a572015-08-11 21:38:15 +00005555{
5556 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005557 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005558
5559 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5560 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5561 child_bitfield_bit_size = 0;
5562 child_bitfield_bit_offset = 0;
5563 child_is_base_class = false;
5564
5565 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5566 uint32_t bit_offset;
5567 switch (parent_type_class)
5568 {
5569 case clang::Type::Builtin:
5570 if (idx_is_valid)
5571 {
5572 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5573 {
5574 case clang::BuiltinType::ObjCId:
5575 case clang::BuiltinType::ObjCClass:
5576 child_name = "isa";
5577 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005578 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005579
5580 default:
5581 break;
5582 }
5583 }
5584 break;
5585
5586 case clang::Type::Record:
5587 if (idx_is_valid && GetCompleteType(type))
5588 {
5589 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5590 const clang::RecordDecl *record_decl = record_type->getDecl();
5591 assert(record_decl);
5592 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5593 uint32_t child_idx = 0;
5594
5595 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5596 if (cxx_record_decl)
5597 {
5598 // We might have base classes to print out first
5599 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5600 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5601 base_class != base_class_end;
5602 ++base_class)
5603 {
5604 const clang::CXXRecordDecl *base_class_decl = nullptr;
5605
5606 // Skip empty base classes
5607 if (omit_empty_base_classes)
5608 {
5609 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5610 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5611 continue;
5612 }
5613
5614 if (idx == child_idx)
5615 {
5616 if (base_class_decl == nullptr)
5617 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5618
5619
5620 if (base_class->isVirtual())
5621 {
5622 bool handled = false;
5623 if (valobj)
5624 {
5625 Error err;
5626 AddressType addr_type = eAddressTypeInvalid;
5627 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5628
5629 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5630 {
5631
5632 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5633 Process *process = exe_ctx.GetProcessPtr();
5634 if (process)
5635 {
5636 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5637 if (vtable_ctx)
5638 {
5639 if (vtable_ctx->isMicrosoft())
5640 {
5641 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5642
5643 if (vtable_ptr_addr)
5644 {
5645 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5646
5647 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5648 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5649 {
5650 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5651 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5652 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5653 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5654 if (base_offset != UINT32_MAX)
5655 {
5656 handled = true;
5657 bit_offset = base_offset * 8;
5658 }
5659 }
5660 }
5661 }
5662 else
5663 {
5664 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5665 if (vtable_ptr_addr)
5666 {
5667 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5668 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5669 {
5670 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5671 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5672 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5673 if (base_offset != UINT32_MAX)
5674 {
5675 handled = true;
5676 bit_offset = base_offset * 8;
5677 }
5678 }
5679 }
5680 }
5681 }
5682 }
5683 }
5684
5685 }
5686 if (!handled)
5687 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5688 }
5689 else
5690 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5691
5692 // Base classes should be a multiple of 8 bits in size
5693 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005694 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005695 child_name = base_class_clang_type.GetTypeName().AsCString("");
5696 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5697
5698 // Base classes bit sizes should be a multiple of 8 bits in size
5699 assert (base_class_clang_type_bit_size % 8 == 0);
5700 child_byte_size = base_class_clang_type_bit_size / 8;
5701 child_is_base_class = true;
5702 return base_class_clang_type;
5703 }
5704 // We don't increment the child index in the for loop since we might
5705 // be skipping empty base classes
5706 ++child_idx;
5707 }
5708 }
5709 // Make sure index is in range...
5710 uint32_t field_idx = 0;
5711 clang::RecordDecl::field_iterator field, field_end;
5712 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5713 {
5714 if (idx == child_idx)
5715 {
5716 // Print the member type if requested
5717 // Print the member name and equal sign
5718 child_name.assign(field->getNameAsString().c_str());
5719
5720 // Figure out the type byte size (field_type_info.first) and
5721 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005722 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005723 assert(field_idx < record_layout.getFieldCount());
5724 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5725
5726 // Figure out the field offset within the current struct/union/class type
5727 bit_offset = record_layout.getFieldOffset (field_idx);
5728 child_byte_offset = bit_offset / 8;
5729 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5730 child_bitfield_bit_offset = bit_offset % 8;
5731
5732 return field_clang_type;
5733 }
5734 }
5735 }
5736 break;
5737
5738 case clang::Type::ObjCObject:
5739 case clang::Type::ObjCInterface:
5740 if (idx_is_valid && GetCompleteType(type))
5741 {
5742 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5743 assert (objc_class_type);
5744 if (objc_class_type)
5745 {
5746 uint32_t child_idx = 0;
5747 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5748
5749 if (class_interface_decl)
5750 {
5751
5752 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5753 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5754 if (superclass_interface_decl)
5755 {
5756 if (omit_empty_base_classes)
5757 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005758 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005759 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5760 {
5761 if (idx == 0)
5762 {
5763 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5764
5765
5766 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5767
5768 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5769
5770 child_byte_size = ivar_type_info.Width / 8;
5771 child_byte_offset = 0;
5772 child_is_base_class = true;
5773
Greg Claytona1e5dc82015-08-11 22:53:00 +00005774 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005775 }
5776
5777 ++child_idx;
5778 }
5779 }
5780 else
5781 ++child_idx;
5782 }
5783
5784 const uint32_t superclass_idx = child_idx;
5785
5786 if (idx < (child_idx + class_interface_decl->ivar_size()))
5787 {
5788 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5789
5790 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5791 {
5792 if (child_idx == idx)
5793 {
5794 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5795
5796 clang::QualType ivar_qual_type(ivar_decl->getType());
5797
5798 child_name.assign(ivar_decl->getNameAsString().c_str());
5799
5800 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5801
5802 child_byte_size = ivar_type_info.Width / 8;
5803
5804 // Figure out the field offset within the current struct/union/class type
5805 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5806 // that doesn't account for the space taken up by unbacked properties, or from
5807 // the changing size of base classes that are newer than this class.
5808 // So if we have a process around that we can ask about this object, do so.
5809 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5810 Process *process = nullptr;
5811 if (exe_ctx)
5812 process = exe_ctx->GetProcessPtr();
5813 if (process)
5814 {
5815 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5816 if (objc_runtime != nullptr)
5817 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005818 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005819 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5820 }
5821 }
5822
5823 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5824 bit_offset = UINT32_MAX;
5825
5826 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5827 {
5828 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5829 child_byte_offset = bit_offset / 8;
5830 }
5831
5832 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5833 // of a bitfield within its containing object. So regardless of where we get the byte
5834 // offset from, we still need to get the bit offset for bitfields from the layout.
5835
5836 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5837 {
5838 if (bit_offset == UINT32_MAX)
5839 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5840
5841 child_bitfield_bit_offset = bit_offset % 8;
5842 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005843 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005844 }
5845 ++child_idx;
5846 }
5847 }
5848 }
5849 }
5850 }
5851 break;
5852
5853 case clang::Type::ObjCObjectPointer:
5854 if (idx_is_valid)
5855 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005856 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005857
5858 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5859 {
5860 child_is_deref_of_parent = false;
5861 bool tmp_child_is_deref_of_parent = false;
5862 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5863 idx,
5864 transparent_pointers,
5865 omit_empty_base_classes,
5866 ignore_array_bounds,
5867 child_name,
5868 child_byte_size,
5869 child_byte_offset,
5870 child_bitfield_bit_size,
5871 child_bitfield_bit_offset,
5872 child_is_base_class,
5873 tmp_child_is_deref_of_parent,
5874 valobj);
5875 }
5876 else
5877 {
5878 child_is_deref_of_parent = true;
5879 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5880 if (parent_name)
5881 {
5882 child_name.assign(1, '*');
5883 child_name += parent_name;
5884 }
5885
5886 // We have a pointer to an simple type
5887 if (idx == 0 && pointee_clang_type.GetCompleteType())
5888 {
5889 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5890 child_byte_offset = 0;
5891 return pointee_clang_type;
5892 }
5893 }
5894 }
5895 break;
5896
5897 case clang::Type::Vector:
5898 case clang::Type::ExtVector:
5899 if (idx_is_valid)
5900 {
5901 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
5902 if (array)
5903 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005904 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005905 if (element_type.GetCompleteType())
5906 {
5907 char element_name[64];
5908 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5909 child_name.assign(element_name);
5910 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5911 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5912 return element_type;
5913 }
5914 }
5915 }
5916 break;
5917
5918 case clang::Type::ConstantArray:
5919 case clang::Type::IncompleteArray:
5920 if (ignore_array_bounds || idx_is_valid)
5921 {
5922 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
5923 if (array)
5924 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005925 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005926 if (element_type.GetCompleteType())
5927 {
5928 char element_name[64];
5929 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
5930 child_name.assign(element_name);
5931 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5932 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
5933 return element_type;
5934 }
5935 }
5936 }
5937 break;
5938
5939
5940 case clang::Type::Pointer:
5941 if (idx_is_valid)
5942 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005943 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005944
5945 // Don't dereference "void *" pointers
5946 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00005947 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005948
5949 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5950 {
5951 child_is_deref_of_parent = false;
5952 bool tmp_child_is_deref_of_parent = false;
5953 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
5954 idx,
5955 transparent_pointers,
5956 omit_empty_base_classes,
5957 ignore_array_bounds,
5958 child_name,
5959 child_byte_size,
5960 child_byte_offset,
5961 child_bitfield_bit_size,
5962 child_bitfield_bit_offset,
5963 child_is_base_class,
5964 tmp_child_is_deref_of_parent,
5965 valobj);
5966 }
5967 else
5968 {
5969 child_is_deref_of_parent = true;
5970
5971 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
5972 if (parent_name)
5973 {
5974 child_name.assign(1, '*');
5975 child_name += parent_name;
5976 }
5977
5978 // We have a pointer to an simple type
5979 if (idx == 0)
5980 {
5981 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5982 child_byte_offset = 0;
5983 return pointee_clang_type;
5984 }
5985 }
5986 }
5987 break;
5988
5989 case clang::Type::LValueReference:
5990 case clang::Type::RValueReference:
5991 if (idx_is_valid)
5992 {
5993 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00005994 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005995 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
5996 {
5997 child_is_deref_of_parent = false;
5998 bool tmp_child_is_deref_of_parent = false;
5999 return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6000 idx,
6001 transparent_pointers,
6002 omit_empty_base_classes,
6003 ignore_array_bounds,
6004 child_name,
6005 child_byte_size,
6006 child_byte_offset,
6007 child_bitfield_bit_size,
6008 child_bitfield_bit_offset,
6009 child_is_base_class,
6010 tmp_child_is_deref_of_parent,
6011 valobj);
6012 }
6013 else
6014 {
6015 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6016 if (parent_name)
6017 {
6018 child_name.assign(1, '&');
6019 child_name += parent_name;
6020 }
6021
6022 // We have a pointer to an simple type
6023 if (idx == 0)
6024 {
6025 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6026 child_byte_offset = 0;
6027 return pointee_clang_type;
6028 }
6029 }
6030 }
6031 break;
6032
6033 case clang::Type::Typedef:
6034 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006035 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006036 return typedefed_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6037 idx,
6038 transparent_pointers,
6039 omit_empty_base_classes,
6040 ignore_array_bounds,
6041 child_name,
6042 child_byte_size,
6043 child_byte_offset,
6044 child_bitfield_bit_size,
6045 child_bitfield_bit_offset,
6046 child_is_base_class,
6047 child_is_deref_of_parent,
6048 valobj);
6049 }
6050 break;
6051
6052 case clang::Type::Elaborated:
6053 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006054 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006055 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6056 idx,
6057 transparent_pointers,
6058 omit_empty_base_classes,
6059 ignore_array_bounds,
6060 child_name,
6061 child_byte_size,
6062 child_byte_offset,
6063 child_bitfield_bit_size,
6064 child_bitfield_bit_offset,
6065 child_is_base_class,
6066 child_is_deref_of_parent,
6067 valobj);
6068 }
6069
6070 case clang::Type::Paren:
6071 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006072 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00006073 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
6074 idx,
6075 transparent_pointers,
6076 omit_empty_base_classes,
6077 ignore_array_bounds,
6078 child_name,
6079 child_byte_size,
6080 child_byte_offset,
6081 child_bitfield_bit_size,
6082 child_bitfield_bit_offset,
6083 child_is_base_class,
6084 child_is_deref_of_parent,
6085 valobj);
6086 }
6087
6088
6089 default:
6090 break;
6091 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006092 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006093}
6094
6095static uint32_t
6096GetIndexForRecordBase
6097(
6098 const clang::RecordDecl *record_decl,
6099 const clang::CXXBaseSpecifier *base_spec,
6100 bool omit_empty_base_classes
6101 )
6102{
6103 uint32_t child_idx = 0;
6104
6105 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6106
6107 // const char *super_name = record_decl->getNameAsCString();
6108 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6109 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6110 //
6111 if (cxx_record_decl)
6112 {
6113 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6114 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6115 base_class != base_class_end;
6116 ++base_class)
6117 {
6118 if (omit_empty_base_classes)
6119 {
6120 if (BaseSpecifierIsEmpty (base_class))
6121 continue;
6122 }
6123
6124 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6125 // child_idx,
6126 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6127 //
6128 //
6129 if (base_class == base_spec)
6130 return child_idx;
6131 ++child_idx;
6132 }
6133 }
6134
6135 return UINT32_MAX;
6136}
6137
6138
6139static uint32_t
6140GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6141 clang::NamedDecl *canonical_decl,
6142 bool omit_empty_base_classes)
6143{
6144 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6145 omit_empty_base_classes);
6146
6147 clang::RecordDecl::field_iterator field, field_end;
6148 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6149 field != field_end;
6150 ++field, ++child_idx)
6151 {
6152 if (field->getCanonicalDecl() == canonical_decl)
6153 return child_idx;
6154 }
6155
6156 return UINT32_MAX;
6157}
6158
6159// Look for a child member (doesn't include base classes, but it does include
6160// their members) in the type hierarchy. Returns an index path into "clang_type"
6161// on how to reach the appropriate member.
6162//
6163// class A
6164// {
6165// public:
6166// int m_a;
6167// int m_b;
6168// };
6169//
6170// class B
6171// {
6172// };
6173//
6174// class C :
6175// public B,
6176// public A
6177// {
6178// };
6179//
6180// If we have a clang type that describes "class C", and we wanted to looked
6181// "m_b" in it:
6182//
6183// With omit_empty_base_classes == false we would get an integer array back with:
6184// { 1, 1 }
6185// The first index 1 is the child index for "class A" within class C
6186// The second index 1 is the child index for "m_b" within class A
6187//
6188// With omit_empty_base_classes == true we would get an integer array back with:
6189// { 0, 1 }
6190// 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)
6191// The second index 1 is the child index for "m_b" within class A
6192
6193size_t
6194ClangASTContext::GetIndexOfChildMemberWithName (void* type, const char *name,
6195 bool omit_empty_base_classes,
6196 std::vector<uint32_t>& child_indexes)
6197{
6198 if (type && name && name[0])
6199 {
6200 clang::QualType qual_type(GetCanonicalQualType(type));
6201 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6202 switch (type_class)
6203 {
6204 case clang::Type::Record:
6205 if (GetCompleteType(type))
6206 {
6207 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6208 const clang::RecordDecl *record_decl = record_type->getDecl();
6209
6210 assert(record_decl);
6211 uint32_t child_idx = 0;
6212
6213 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6214
6215 // Try and find a field that matches NAME
6216 clang::RecordDecl::field_iterator field, field_end;
6217 llvm::StringRef name_sref(name);
6218 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6219 field != field_end;
6220 ++field, ++child_idx)
6221 {
6222 llvm::StringRef field_name = field->getName();
6223 if (field_name.empty())
6224 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006225 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006226 child_indexes.push_back(child_idx);
6227 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6228 return child_indexes.size();
6229 child_indexes.pop_back();
6230
6231 }
6232 else if (field_name.equals (name_sref))
6233 {
6234 // We have to add on the number of base classes to this index!
6235 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6236 return child_indexes.size();
6237 }
6238 }
6239
6240 if (cxx_record_decl)
6241 {
6242 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6243
6244 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6245
6246 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6247 // Didn't find things easily, lets let clang do its thang...
6248 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6249 clang::DeclarationName decl_name(&ident_ref);
6250
6251 clang::CXXBasePaths paths;
6252 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6253 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6254 },
6255 paths))
6256 {
6257 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6258 for (path = paths.begin(); path != path_end; ++path)
6259 {
6260 const size_t num_path_elements = path->size();
6261 for (size_t e=0; e<num_path_elements; ++e)
6262 {
6263 clang::CXXBasePathElement elem = (*path)[e];
6264
6265 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6266 if (child_idx == UINT32_MAX)
6267 {
6268 child_indexes.clear();
6269 return 0;
6270 }
6271 else
6272 {
6273 child_indexes.push_back (child_idx);
6274 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6275 }
6276 }
6277 for (clang::NamedDecl *path_decl : path->Decls)
6278 {
6279 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6280 if (child_idx == UINT32_MAX)
6281 {
6282 child_indexes.clear();
6283 return 0;
6284 }
6285 else
6286 {
6287 child_indexes.push_back (child_idx);
6288 }
6289 }
6290 }
6291 return child_indexes.size();
6292 }
6293 }
6294
6295 }
6296 break;
6297
6298 case clang::Type::ObjCObject:
6299 case clang::Type::ObjCInterface:
6300 if (GetCompleteType(type))
6301 {
6302 llvm::StringRef name_sref(name);
6303 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6304 assert (objc_class_type);
6305 if (objc_class_type)
6306 {
6307 uint32_t child_idx = 0;
6308 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6309
6310 if (class_interface_decl)
6311 {
6312 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6313 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6314
6315 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6316 {
6317 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6318
6319 if (ivar_decl->getName().equals (name_sref))
6320 {
6321 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6322 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6323 ++child_idx;
6324
6325 child_indexes.push_back (child_idx);
6326 return child_indexes.size();
6327 }
6328 }
6329
6330 if (superclass_interface_decl)
6331 {
6332 // The super class index is always zero for ObjC classes,
6333 // so we push it onto the child indexes in case we find
6334 // an ivar in our superclass...
6335 child_indexes.push_back (0);
6336
Greg Claytona1e5dc82015-08-11 22:53:00 +00006337 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006338 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6339 omit_empty_base_classes,
6340 child_indexes))
6341 {
6342 // We did find an ivar in a superclass so just
6343 // return the results!
6344 return child_indexes.size();
6345 }
6346
6347 // We didn't find an ivar matching "name" in our
6348 // superclass, pop the superclass zero index that
6349 // we pushed on above.
6350 child_indexes.pop_back();
6351 }
6352 }
6353 }
6354 }
6355 break;
6356
6357 case clang::Type::ObjCObjectPointer:
6358 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006359 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006360 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6361 omit_empty_base_classes,
6362 child_indexes);
6363 }
6364 break;
6365
6366
6367 case clang::Type::ConstantArray:
6368 {
6369 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6370 // const uint64_t element_count = array->getSize().getLimitedValue();
6371 //
6372 // if (idx < element_count)
6373 // {
6374 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6375 //
6376 // char element_name[32];
6377 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6378 //
6379 // child_name.assign(element_name);
6380 // assert(field_type_info.first % 8 == 0);
6381 // child_byte_size = field_type_info.first / 8;
6382 // child_byte_offset = idx * child_byte_size;
6383 // return array->getElementType().getAsOpaquePtr();
6384 // }
6385 }
6386 break;
6387
6388 // case clang::Type::MemberPointerType:
6389 // {
6390 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6391 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6392 //
6393 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6394 // {
6395 // return GetIndexOfChildWithName (ast,
6396 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6397 // name);
6398 // }
6399 // }
6400 // break;
6401 //
6402 case clang::Type::LValueReference:
6403 case clang::Type::RValueReference:
6404 {
6405 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6406 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006407 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006408
6409 if (pointee_clang_type.IsAggregateType ())
6410 {
6411 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6412 omit_empty_base_classes,
6413 child_indexes);
6414 }
6415 }
6416 break;
6417
6418 case clang::Type::Pointer:
6419 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006420 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006421
6422 if (pointee_clang_type.IsAggregateType ())
6423 {
6424 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6425 omit_empty_base_classes,
6426 child_indexes);
6427 }
6428 }
6429 break;
6430
6431 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006432 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006433 omit_empty_base_classes,
6434 child_indexes);
6435
6436 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006437 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006438 omit_empty_base_classes,
6439 child_indexes);
6440
6441 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006442 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006443 omit_empty_base_classes,
6444 child_indexes);
6445
6446 default:
6447 break;
6448 }
6449 }
6450 return 0;
6451}
6452
6453
6454// Get the index of the child of "clang_type" whose name matches. This function
6455// doesn't descend into the children, but only looks one level deep and name
6456// matches can include base class names.
6457
6458uint32_t
6459ClangASTContext::GetIndexOfChildWithName (void* type, const char *name, bool omit_empty_base_classes)
6460{
6461 if (type && name && name[0])
6462 {
6463 clang::QualType qual_type(GetCanonicalQualType(type));
6464
6465 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6466
6467 switch (type_class)
6468 {
6469 case clang::Type::Record:
6470 if (GetCompleteType(type))
6471 {
6472 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6473 const clang::RecordDecl *record_decl = record_type->getDecl();
6474
6475 assert(record_decl);
6476 uint32_t child_idx = 0;
6477
6478 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6479
6480 if (cxx_record_decl)
6481 {
6482 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6483 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6484 base_class != base_class_end;
6485 ++base_class)
6486 {
6487 // Skip empty base classes
6488 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6489 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6490 continue;
6491
Greg Claytona1e5dc82015-08-11 22:53:00 +00006492 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006493 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6494 if (base_class_type_name.compare (name) == 0)
6495 return child_idx;
6496 ++child_idx;
6497 }
6498 }
6499
6500 // Try and find a field that matches NAME
6501 clang::RecordDecl::field_iterator field, field_end;
6502 llvm::StringRef name_sref(name);
6503 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6504 field != field_end;
6505 ++field, ++child_idx)
6506 {
6507 if (field->getName().equals (name_sref))
6508 return child_idx;
6509 }
6510
6511 }
6512 break;
6513
6514 case clang::Type::ObjCObject:
6515 case clang::Type::ObjCInterface:
6516 if (GetCompleteType(type))
6517 {
6518 llvm::StringRef name_sref(name);
6519 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6520 assert (objc_class_type);
6521 if (objc_class_type)
6522 {
6523 uint32_t child_idx = 0;
6524 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6525
6526 if (class_interface_decl)
6527 {
6528 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6529 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6530
6531 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6532 {
6533 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6534
6535 if (ivar_decl->getName().equals (name_sref))
6536 {
6537 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6538 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6539 ++child_idx;
6540
6541 return child_idx;
6542 }
6543 }
6544
6545 if (superclass_interface_decl)
6546 {
6547 if (superclass_interface_decl->getName().equals (name_sref))
6548 return 0;
6549 }
6550 }
6551 }
6552 }
6553 break;
6554
6555 case clang::Type::ObjCObjectPointer:
6556 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006557 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006558 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6559 }
6560 break;
6561
6562 case clang::Type::ConstantArray:
6563 {
6564 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6565 // const uint64_t element_count = array->getSize().getLimitedValue();
6566 //
6567 // if (idx < element_count)
6568 // {
6569 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6570 //
6571 // char element_name[32];
6572 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6573 //
6574 // child_name.assign(element_name);
6575 // assert(field_type_info.first % 8 == 0);
6576 // child_byte_size = field_type_info.first / 8;
6577 // child_byte_offset = idx * child_byte_size;
6578 // return array->getElementType().getAsOpaquePtr();
6579 // }
6580 }
6581 break;
6582
6583 // case clang::Type::MemberPointerType:
6584 // {
6585 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6586 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6587 //
6588 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6589 // {
6590 // return GetIndexOfChildWithName (ast,
6591 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6592 // name);
6593 // }
6594 // }
6595 // break;
6596 //
6597 case clang::Type::LValueReference:
6598 case clang::Type::RValueReference:
6599 {
6600 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006601 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006602
6603 if (pointee_type.IsAggregateType ())
6604 {
6605 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6606 }
6607 }
6608 break;
6609
6610 case clang::Type::Pointer:
6611 {
6612 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006613 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006614
6615 if (pointee_type.IsAggregateType ())
6616 {
6617 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6618 }
6619 else
6620 {
6621 // if (parent_name)
6622 // {
6623 // child_name.assign(1, '*');
6624 // child_name += parent_name;
6625 // }
6626 //
6627 // // We have a pointer to an simple type
6628 // if (idx == 0)
6629 // {
6630 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6631 // assert(clang_type_info.first % 8 == 0);
6632 // child_byte_size = clang_type_info.first / 8;
6633 // child_byte_offset = 0;
6634 // return pointee_type.getAsOpaquePtr();
6635 // }
6636 }
6637 }
6638 break;
6639
6640 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006641 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006642
6643 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006644 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006645
6646 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006647 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006648
6649 default:
6650 break;
6651 }
6652 }
6653 return UINT32_MAX;
6654}
6655
6656
6657size_t
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006658ClangASTContext::GetNumTemplateArguments (void* type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006659{
6660 if (!type)
6661 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006662
6663 clang::QualType qual_type (GetCanonicalQualType(type));
6664 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6665 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006666 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006667 case clang::Type::Record:
6668 if (GetCompleteType(type))
6669 {
6670 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6671 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006672 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006673 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6674 if (template_decl)
6675 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006676 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006677 }
6678 break;
6679
6680 case clang::Type::Typedef:
6681 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6682
6683 case clang::Type::Elaborated:
6684 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6685
6686 case clang::Type::Paren:
6687 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6688
6689 default:
6690 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006691 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006692
Greg Claytond8d4a572015-08-11 21:38:15 +00006693 return 0;
6694}
6695
Greg Claytona1e5dc82015-08-11 22:53:00 +00006696CompilerType
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006697ClangASTContext::GetTemplateArgument (void* type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006698{
6699 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006700 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006701
6702 clang::QualType qual_type (GetCanonicalQualType(type));
6703 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6704 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006705 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006706 case clang::Type::Record:
6707 if (GetCompleteType(type))
6708 {
6709 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6710 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006711 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006712 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6713 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006714 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006715 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6716 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006717 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006718 case clang::TemplateArgument::Null:
6719 kind = eTemplateArgumentKindNull;
6720 return CompilerType();
6721
6722 case clang::TemplateArgument::Type:
6723 kind = eTemplateArgumentKindType;
6724 return CompilerType(getASTContext(), template_arg.getAsType());
6725
6726 case clang::TemplateArgument::Declaration:
6727 kind = eTemplateArgumentKindDeclaration;
6728 return CompilerType();
6729
6730 case clang::TemplateArgument::Integral:
6731 kind = eTemplateArgumentKindIntegral;
6732 return CompilerType(getASTContext(), template_arg.getIntegralType());
6733
6734 case clang::TemplateArgument::Template:
6735 kind = eTemplateArgumentKindTemplate;
6736 return CompilerType();
6737
6738 case clang::TemplateArgument::TemplateExpansion:
6739 kind = eTemplateArgumentKindTemplateExpansion;
6740 return CompilerType();
6741
6742 case clang::TemplateArgument::Expression:
6743 kind = eTemplateArgumentKindExpression;
6744 return CompilerType();
6745
6746 case clang::TemplateArgument::Pack:
6747 kind = eTemplateArgumentKindPack;
6748 return CompilerType();
6749
6750 default:
6751 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6752 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006753 }
6754 }
6755 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006756 }
6757 break;
6758
6759 case clang::Type::Typedef:
6760 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6761
6762 case clang::Type::Elaborated:
6763 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6764
6765 case clang::Type::Paren:
6766 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6767
6768 default:
6769 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006770 }
6771 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006772 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006773}
6774
6775static bool
6776IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6777{
6778 if (name == nullptr || name[0] == '\0')
6779 return false;
6780
6781#define OPERATOR_PREFIX "operator"
6782#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6783
6784 const char *post_op_name = nullptr;
6785
6786 bool no_space = true;
6787
6788 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6789 return false;
6790
6791 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6792
6793 if (post_op_name[0] == ' ')
6794 {
6795 post_op_name++;
6796 no_space = false;
6797 }
6798
6799#undef OPERATOR_PREFIX
6800#undef OPERATOR_PREFIX_LENGTH
6801
6802 // This is an operator, set the overloaded operator kind to invalid
6803 // in case this is a conversion operator...
6804 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6805
6806 switch (post_op_name[0])
6807 {
6808 default:
6809 if (no_space)
6810 return false;
6811 break;
6812 case 'n':
6813 if (no_space)
6814 return false;
6815 if (strcmp (post_op_name, "new") == 0)
6816 op_kind = clang::OO_New;
6817 else if (strcmp (post_op_name, "new[]") == 0)
6818 op_kind = clang::OO_Array_New;
6819 break;
6820
6821 case 'd':
6822 if (no_space)
6823 return false;
6824 if (strcmp (post_op_name, "delete") == 0)
6825 op_kind = clang::OO_Delete;
6826 else if (strcmp (post_op_name, "delete[]") == 0)
6827 op_kind = clang::OO_Array_Delete;
6828 break;
6829
6830 case '+':
6831 if (post_op_name[1] == '\0')
6832 op_kind = clang::OO_Plus;
6833 else if (post_op_name[2] == '\0')
6834 {
6835 if (post_op_name[1] == '=')
6836 op_kind = clang::OO_PlusEqual;
6837 else if (post_op_name[1] == '+')
6838 op_kind = clang::OO_PlusPlus;
6839 }
6840 break;
6841
6842 case '-':
6843 if (post_op_name[1] == '\0')
6844 op_kind = clang::OO_Minus;
6845 else if (post_op_name[2] == '\0')
6846 {
6847 switch (post_op_name[1])
6848 {
6849 case '=': op_kind = clang::OO_MinusEqual; break;
6850 case '-': op_kind = clang::OO_MinusMinus; break;
6851 case '>': op_kind = clang::OO_Arrow; break;
6852 }
6853 }
6854 else if (post_op_name[3] == '\0')
6855 {
6856 if (post_op_name[2] == '*')
6857 op_kind = clang::OO_ArrowStar; break;
6858 }
6859 break;
6860
6861 case '*':
6862 if (post_op_name[1] == '\0')
6863 op_kind = clang::OO_Star;
6864 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6865 op_kind = clang::OO_StarEqual;
6866 break;
6867
6868 case '/':
6869 if (post_op_name[1] == '\0')
6870 op_kind = clang::OO_Slash;
6871 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6872 op_kind = clang::OO_SlashEqual;
6873 break;
6874
6875 case '%':
6876 if (post_op_name[1] == '\0')
6877 op_kind = clang::OO_Percent;
6878 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6879 op_kind = clang::OO_PercentEqual;
6880 break;
6881
6882
6883 case '^':
6884 if (post_op_name[1] == '\0')
6885 op_kind = clang::OO_Caret;
6886 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6887 op_kind = clang::OO_CaretEqual;
6888 break;
6889
6890 case '&':
6891 if (post_op_name[1] == '\0')
6892 op_kind = clang::OO_Amp;
6893 else if (post_op_name[2] == '\0')
6894 {
6895 switch (post_op_name[1])
6896 {
6897 case '=': op_kind = clang::OO_AmpEqual; break;
6898 case '&': op_kind = clang::OO_AmpAmp; break;
6899 }
6900 }
6901 break;
6902
6903 case '|':
6904 if (post_op_name[1] == '\0')
6905 op_kind = clang::OO_Pipe;
6906 else if (post_op_name[2] == '\0')
6907 {
6908 switch (post_op_name[1])
6909 {
6910 case '=': op_kind = clang::OO_PipeEqual; break;
6911 case '|': op_kind = clang::OO_PipePipe; break;
6912 }
6913 }
6914 break;
6915
6916 case '~':
6917 if (post_op_name[1] == '\0')
6918 op_kind = clang::OO_Tilde;
6919 break;
6920
6921 case '!':
6922 if (post_op_name[1] == '\0')
6923 op_kind = clang::OO_Exclaim;
6924 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6925 op_kind = clang::OO_ExclaimEqual;
6926 break;
6927
6928 case '=':
6929 if (post_op_name[1] == '\0')
6930 op_kind = clang::OO_Equal;
6931 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
6932 op_kind = clang::OO_EqualEqual;
6933 break;
6934
6935 case '<':
6936 if (post_op_name[1] == '\0')
6937 op_kind = clang::OO_Less;
6938 else if (post_op_name[2] == '\0')
6939 {
6940 switch (post_op_name[1])
6941 {
6942 case '<': op_kind = clang::OO_LessLess; break;
6943 case '=': op_kind = clang::OO_LessEqual; break;
6944 }
6945 }
6946 else if (post_op_name[3] == '\0')
6947 {
6948 if (post_op_name[2] == '=')
6949 op_kind = clang::OO_LessLessEqual;
6950 }
6951 break;
6952
6953 case '>':
6954 if (post_op_name[1] == '\0')
6955 op_kind = clang::OO_Greater;
6956 else if (post_op_name[2] == '\0')
6957 {
6958 switch (post_op_name[1])
6959 {
6960 case '>': op_kind = clang::OO_GreaterGreater; break;
6961 case '=': op_kind = clang::OO_GreaterEqual; break;
6962 }
6963 }
6964 else if (post_op_name[1] == '>' &&
6965 post_op_name[2] == '=' &&
6966 post_op_name[3] == '\0')
6967 {
6968 op_kind = clang::OO_GreaterGreaterEqual;
6969 }
6970 break;
6971
6972 case ',':
6973 if (post_op_name[1] == '\0')
6974 op_kind = clang::OO_Comma;
6975 break;
6976
6977 case '(':
6978 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
6979 op_kind = clang::OO_Call;
6980 break;
6981
6982 case '[':
6983 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
6984 op_kind = clang::OO_Subscript;
6985 break;
6986 }
6987
6988 return true;
6989}
6990
6991clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00006992ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006993{
6994 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
6995 if (enutype)
6996 return enutype->getDecl();
6997 return NULL;
6998}
6999
7000clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007001ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007002{
7003 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7004 if (record_type)
7005 return record_type->getDecl();
7006 return nullptr;
7007}
7008
7009clang::CXXRecordDecl *
7010ClangASTContext::GetAsCXXRecordDecl (void* type)
7011{
7012 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7013}
7014
7015clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007016ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007017{
7018 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7019 if (objc_class_type)
7020 return objc_class_type->getInterface();
7021 return nullptr;
7022}
7023
7024clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007025ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7026 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007027 AccessType access,
7028 uint32_t bitfield_bit_size)
7029{
7030 if (!type.IsValid() || !field_clang_type.IsValid())
7031 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007032 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007033 if (!ast)
7034 return nullptr;
7035 clang::ASTContext* clang_ast = ast->getASTContext();
7036
7037 clang::FieldDecl *field = nullptr;
7038
7039 clang::Expr *bit_width = nullptr;
7040 if (bitfield_bit_size != 0)
7041 {
7042 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7043 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7044 }
7045
7046 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7047 if (record_decl)
7048 {
7049 field = clang::FieldDecl::Create (*clang_ast,
7050 record_decl,
7051 clang::SourceLocation(),
7052 clang::SourceLocation(),
7053 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7054 GetQualType(field_clang_type), // Field type
7055 nullptr, // TInfo *
7056 bit_width, // BitWidth
7057 false, // Mutable
7058 clang::ICIS_NoInit); // HasInit
7059
7060 if (!name)
7061 {
7062 // Determine whether this field corresponds to an anonymous
7063 // struct or union.
7064 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7065 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7066 if (!Rec->getDeclName()) {
7067 Rec->setAnonymousStructOrUnion(true);
7068 field->setImplicit();
7069
7070 }
7071 }
7072 }
7073
7074 if (field)
7075 {
7076 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7077
7078 record_decl->addDecl(field);
7079
7080#ifdef LLDB_CONFIGURATION_DEBUG
7081 VerifyDecl(field);
7082#endif
7083 }
7084 }
7085 else
7086 {
7087 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7088
7089 if (class_interface_decl)
7090 {
7091 const bool is_synthesized = false;
7092
7093 field_clang_type.GetCompleteType();
7094
7095 field = clang::ObjCIvarDecl::Create (*clang_ast,
7096 class_interface_decl,
7097 clang::SourceLocation(),
7098 clang::SourceLocation(),
7099 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7100 GetQualType(field_clang_type), // Field type
7101 nullptr, // TypeSourceInfo *
7102 ConvertAccessTypeToObjCIvarAccessControl (access),
7103 bit_width,
7104 is_synthesized);
7105
7106 if (field)
7107 {
7108 class_interface_decl->addDecl(field);
7109
7110#ifdef LLDB_CONFIGURATION_DEBUG
7111 VerifyDecl(field);
7112#endif
7113 }
7114 }
7115 }
7116 return field;
7117}
7118
7119void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007120ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007121{
Greg Claytonf73034f2015-09-08 18:15:05 +00007122 if (!type)
7123 return;
7124
7125 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007126 if (!ast)
7127 return;
7128
7129 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7130
7131 if (!record_decl)
7132 return;
7133
7134 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7135
7136 IndirectFieldVector indirect_fields;
7137 clang::RecordDecl::field_iterator field_pos;
7138 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7139 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7140 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7141 {
7142 if (field_pos->isAnonymousStructOrUnion())
7143 {
7144 clang::QualType field_qual_type = field_pos->getType();
7145
7146 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7147
7148 if (!field_record_type)
7149 continue;
7150
7151 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7152
7153 if (!field_record_decl)
7154 continue;
7155
7156 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7157 di != de;
7158 ++di)
7159 {
7160 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7161 {
7162 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7163 chain[0] = *field_pos;
7164 chain[1] = nested_field_decl;
7165 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7166 record_decl,
7167 clang::SourceLocation(),
7168 nested_field_decl->getIdentifier(),
7169 nested_field_decl->getType(),
7170 chain,
7171 2);
7172
7173 indirect_field->setImplicit();
7174
7175 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7176 nested_field_decl->getAccess()));
7177
7178 indirect_fields.push_back(indirect_field);
7179 }
7180 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7181 {
7182 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7183 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7184 chain[0] = *field_pos;
7185
7186 int chain_index = 1;
7187 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7188 nce = nested_indirect_field_decl->chain_end();
7189 nci < nce;
7190 ++nci)
7191 {
7192 chain[chain_index] = *nci;
7193 chain_index++;
7194 }
7195
7196 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7197 record_decl,
7198 clang::SourceLocation(),
7199 nested_indirect_field_decl->getIdentifier(),
7200 nested_indirect_field_decl->getType(),
7201 chain,
7202 nested_chain_size + 1);
7203
7204 indirect_field->setImplicit();
7205
7206 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7207 nested_indirect_field_decl->getAccess()));
7208
7209 indirect_fields.push_back(indirect_field);
7210 }
7211 }
7212 }
7213 }
7214
7215 // Check the last field to see if it has an incomplete array type as its
7216 // last member and if it does, the tell the record decl about it
7217 if (last_field_pos != field_end_pos)
7218 {
7219 if (last_field_pos->getType()->isIncompleteArrayType())
7220 record_decl->hasFlexibleArrayMember();
7221 }
7222
7223 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7224 ifi < ife;
7225 ++ifi)
7226 {
7227 record_decl->addDecl(*ifi);
7228 }
7229}
7230
7231void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007232ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007233{
Greg Claytonf73034f2015-09-08 18:15:05 +00007234 if (type)
7235 {
7236 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7237 if (ast)
7238 {
7239 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007240
Greg Claytonf73034f2015-09-08 18:15:05 +00007241 if (!record_decl)
7242 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007243
Greg Claytonf73034f2015-09-08 18:15:05 +00007244 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7245 }
7246 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007247}
7248
7249clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007250ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7251 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007252 AccessType access)
7253{
7254 clang::VarDecl *var_decl = nullptr;
7255
7256 if (!type.IsValid() || !var_type.IsValid())
7257 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007258 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007259 if (!ast)
7260 return nullptr;
7261
7262 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7263 if (record_decl)
7264 {
7265 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7266 record_decl, // DeclContext *
7267 clang::SourceLocation(), // clang::SourceLocation StartLoc
7268 clang::SourceLocation(), // clang::SourceLocation IdLoc
7269 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7270 GetQualType(var_type), // Variable clang::QualType
7271 nullptr, // TypeSourceInfo *
7272 clang::SC_Static); // StorageClass
7273 if (var_decl)
7274 {
7275 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7276 record_decl->addDecl(var_decl);
7277
7278#ifdef LLDB_CONFIGURATION_DEBUG
7279 VerifyDecl(var_decl);
7280#endif
7281 }
7282 }
7283 return var_decl;
7284}
7285
7286
7287clang::CXXMethodDecl *
7288ClangASTContext::AddMethodToCXXRecordType (void* type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007289 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007290 lldb::AccessType access,
7291 bool is_virtual,
7292 bool is_static,
7293 bool is_inline,
7294 bool is_explicit,
7295 bool is_attr_used,
7296 bool is_artificial)
7297{
7298 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7299 return nullptr;
7300
7301 clang::QualType record_qual_type(GetCanonicalQualType(type));
7302
7303 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7304
7305 if (cxx_record_decl == nullptr)
7306 return nullptr;
7307
7308 clang::QualType method_qual_type (GetQualType(method_clang_type));
7309
7310 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7311
7312 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7313
7314 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7315
7316 if (function_type == nullptr)
7317 return nullptr;
7318
7319 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7320
7321 if (!method_function_prototype)
7322 return nullptr;
7323
7324 unsigned int num_params = method_function_prototype->getNumParams();
7325
7326 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7327 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7328
7329 if (is_artificial)
7330 return nullptr; // skip everything artificial
7331
7332 if (name[0] == '~')
7333 {
7334 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7335 cxx_record_decl,
7336 clang::SourceLocation(),
7337 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7338 method_qual_type,
7339 nullptr,
7340 is_inline,
7341 is_artificial);
7342 cxx_method_decl = cxx_dtor_decl;
7343 }
7344 else if (decl_name == cxx_record_decl->getDeclName())
7345 {
7346 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7347 cxx_record_decl,
7348 clang::SourceLocation(),
7349 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7350 method_qual_type,
7351 nullptr, // TypeSourceInfo *
7352 is_explicit,
7353 is_inline,
7354 is_artificial,
7355 false /*is_constexpr*/);
7356 cxx_method_decl = cxx_ctor_decl;
7357 }
7358 else
7359 {
7360 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7361 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7362
7363 if (IsOperator (name, op_kind))
7364 {
7365 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7366 {
7367 // Check the number of operator parameters. Sometimes we have
7368 // seen bad DWARF that doesn't correctly describe operators and
7369 // if we try to create a method and add it to the class, clang
7370 // will assert and crash, so we need to make sure things are
7371 // acceptable.
7372 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7373 return nullptr;
7374 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7375 cxx_record_decl,
7376 clang::SourceLocation(),
7377 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7378 method_qual_type,
7379 nullptr, // TypeSourceInfo *
7380 SC,
7381 is_inline,
7382 false /*is_constexpr*/,
7383 clang::SourceLocation());
7384 }
7385 else if (num_params == 0)
7386 {
7387 // Conversion operators don't take params...
7388 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7389 cxx_record_decl,
7390 clang::SourceLocation(),
7391 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7392 method_qual_type,
7393 nullptr, // TypeSourceInfo *
7394 is_inline,
7395 is_explicit,
7396 false /*is_constexpr*/,
7397 clang::SourceLocation());
7398 }
7399 }
7400
7401 if (cxx_method_decl == nullptr)
7402 {
7403 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7404 cxx_record_decl,
7405 clang::SourceLocation(),
7406 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7407 method_qual_type,
7408 nullptr, // TypeSourceInfo *
7409 SC,
7410 is_inline,
7411 false /*is_constexpr*/,
7412 clang::SourceLocation());
7413 }
7414 }
7415
7416 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7417
7418 cxx_method_decl->setAccess (access_specifier);
7419 cxx_method_decl->setVirtualAsWritten (is_virtual);
7420
7421 if (is_attr_used)
7422 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7423
7424 // Populate the method decl with parameter decls
7425
7426 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7427
7428 for (unsigned param_index = 0;
7429 param_index < num_params;
7430 ++param_index)
7431 {
7432 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7433 cxx_method_decl,
7434 clang::SourceLocation(),
7435 clang::SourceLocation(),
7436 nullptr, // anonymous
7437 method_function_prototype->getParamType(param_index),
7438 nullptr,
7439 clang::SC_None,
7440 nullptr));
7441 }
7442
7443 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7444
7445 cxx_record_decl->addDecl (cxx_method_decl);
7446
7447 // Sometimes the debug info will mention a constructor (default/copy/move),
7448 // destructor, or assignment operator (copy/move) but there won't be any
7449 // version of this in the code. So we check if the function was artificially
7450 // generated and if it is trivial and this lets the compiler/backend know
7451 // that it can inline the IR for these when it needs to and we can avoid a
7452 // "missing function" error when running expressions.
7453
7454 if (is_artificial)
7455 {
7456 if (cxx_ctor_decl &&
7457 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7458 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7459 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7460 {
7461 cxx_ctor_decl->setDefaulted();
7462 cxx_ctor_decl->setTrivial(true);
7463 }
7464 else if (cxx_dtor_decl)
7465 {
7466 if (cxx_record_decl->hasTrivialDestructor())
7467 {
7468 cxx_dtor_decl->setDefaulted();
7469 cxx_dtor_decl->setTrivial(true);
7470 }
7471 }
7472 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7473 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7474 {
7475 cxx_method_decl->setDefaulted();
7476 cxx_method_decl->setTrivial(true);
7477 }
7478 }
7479
7480#ifdef LLDB_CONFIGURATION_DEBUG
7481 VerifyDecl(cxx_method_decl);
7482#endif
7483
7484 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7485 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7486 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7487 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7488 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7489 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7490 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7491 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7492 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7493 return cxx_method_decl;
7494}
7495
7496
7497#pragma mark C++ Base Classes
7498
7499clang::CXXBaseSpecifier *
7500ClangASTContext::CreateBaseClassSpecifier (void* type, AccessType access, bool is_virtual, bool base_of_class)
7501{
7502 if (type)
7503 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7504 is_virtual,
7505 base_of_class,
7506 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7507 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7508 clang::SourceLocation());
7509 return nullptr;
7510}
7511
7512void
7513ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7514{
7515 for (unsigned i=0; i<num_base_classes; ++i)
7516 {
7517 delete base_classes[i];
7518 base_classes[i] = nullptr;
7519 }
7520}
7521
7522bool
7523ClangASTContext::SetBaseClassesForClassType (void* type, clang::CXXBaseSpecifier const * const *base_classes,
7524 unsigned num_base_classes)
7525{
7526 if (type)
7527 {
7528 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7529 if (cxx_record_decl)
7530 {
7531 cxx_record_decl->setBases(base_classes, num_base_classes);
7532 return true;
7533 }
7534 }
7535 return false;
7536}
7537
7538bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007539ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007540{
Greg Claytonf73034f2015-09-08 18:15:05 +00007541 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007542 if (!ast)
7543 return false;
7544 clang::ASTContext* clang_ast = ast->getASTContext();
7545
7546 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7547 {
7548 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7549 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7550 if (class_interface_decl && super_interface_decl)
7551 {
7552 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7553 return true;
7554 }
7555 }
7556 return false;
7557}
7558
7559bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007560ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007561 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007562 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007563 clang::ObjCIvarDecl *ivar_decl,
7564 const char *property_setter_name,
7565 const char *property_getter_name,
7566 uint32_t property_attributes,
7567 ClangASTMetadata *metadata)
7568{
7569 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7570 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007571 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007572 if (!ast)
7573 return false;
7574 clang::ASTContext* clang_ast = ast->getASTContext();
7575
7576 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7577
7578 if (class_interface_decl)
7579 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007580 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007581
7582 if (property_clang_type.IsValid())
7583 property_clang_type_to_access = property_clang_type;
7584 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007585 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007586
7587 if (class_interface_decl && property_clang_type_to_access.IsValid())
7588 {
7589 clang::TypeSourceInfo *prop_type_source;
7590 if (ivar_decl)
7591 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7592 else
7593 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7594
7595 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7596 class_interface_decl,
7597 clang::SourceLocation(), // Source Location
7598 &clang_ast->Idents.get(property_name),
7599 clang::SourceLocation(), //Source Location for AT
7600 clang::SourceLocation(), //Source location for (
7601 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7602 prop_type_source);
7603
7604 if (property_decl)
7605 {
7606 if (metadata)
7607 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7608
7609 class_interface_decl->addDecl (property_decl);
7610
7611 clang::Selector setter_sel, getter_sel;
7612
7613 if (property_setter_name != nullptr)
7614 {
7615 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7616 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7617 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7618 }
7619 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7620 {
7621 std::string setter_sel_string("set");
7622 setter_sel_string.push_back(::toupper(property_name[0]));
7623 setter_sel_string.append(&property_name[1]);
7624 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7625 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7626 }
7627 property_decl->setSetterName(setter_sel);
7628 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7629
7630 if (property_getter_name != nullptr)
7631 {
7632 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7633 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7634 }
7635 else
7636 {
7637 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7638 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7639 }
7640 property_decl->setGetterName(getter_sel);
7641 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7642
7643 if (ivar_decl)
7644 property_decl->setPropertyIvarDecl (ivar_decl);
7645
7646 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7647 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7648 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7649 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7650 if (property_attributes & DW_APPLE_PROPERTY_assign)
7651 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7652 if (property_attributes & DW_APPLE_PROPERTY_retain)
7653 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7654 if (property_attributes & DW_APPLE_PROPERTY_copy)
7655 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7656 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7657 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7658
7659 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7660 {
7661 const bool isInstance = true;
7662 const bool isVariadic = false;
7663 const bool isSynthesized = false;
7664 const bool isImplicitlyDeclared = true;
7665 const bool isDefined = false;
7666 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7667 const bool HasRelatedResultType = false;
7668
7669 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7670 clang::SourceLocation(),
7671 clang::SourceLocation(),
7672 getter_sel,
7673 GetQualType(property_clang_type_to_access),
7674 nullptr,
7675 class_interface_decl,
7676 isInstance,
7677 isVariadic,
7678 isSynthesized,
7679 isImplicitlyDeclared,
7680 isDefined,
7681 impControl,
7682 HasRelatedResultType);
7683
7684 if (getter && metadata)
7685 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7686
7687 if (getter)
7688 {
7689 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7690
7691 class_interface_decl->addDecl(getter);
7692 }
7693 }
7694
7695 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7696 {
7697 clang::QualType result_type = clang_ast->VoidTy;
7698
7699 const bool isInstance = true;
7700 const bool isVariadic = false;
7701 const bool isSynthesized = false;
7702 const bool isImplicitlyDeclared = true;
7703 const bool isDefined = false;
7704 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7705 const bool HasRelatedResultType = false;
7706
7707 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7708 clang::SourceLocation(),
7709 clang::SourceLocation(),
7710 setter_sel,
7711 result_type,
7712 nullptr,
7713 class_interface_decl,
7714 isInstance,
7715 isVariadic,
7716 isSynthesized,
7717 isImplicitlyDeclared,
7718 isDefined,
7719 impControl,
7720 HasRelatedResultType);
7721
7722 if (setter && metadata)
7723 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7724
7725 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7726
7727 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7728 setter,
7729 clang::SourceLocation(),
7730 clang::SourceLocation(),
7731 nullptr, // anonymous
7732 GetQualType(property_clang_type_to_access),
7733 nullptr,
7734 clang::SC_Auto,
7735 nullptr));
7736
7737 if (setter)
7738 {
7739 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7740
7741 class_interface_decl->addDecl(setter);
7742 }
7743 }
7744
7745 return true;
7746 }
7747 }
7748 }
7749 return false;
7750}
7751
7752bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007753ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007754{
7755 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7756 if (class_interface_decl)
7757 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7758 return false;
7759}
7760
7761
7762clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007763ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007764 const char *name, // the full symbol name as seen in the symbol table (void* type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007765 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007766 lldb::AccessType access,
7767 bool is_artificial)
7768{
7769 if (!type || !method_clang_type.IsValid())
7770 return nullptr;
7771
7772 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7773
7774 if (class_interface_decl == nullptr)
7775 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007776 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7777 if (lldb_ast == nullptr)
7778 return nullptr;
7779 clang::ASTContext *ast = lldb_ast->getASTContext();
7780
Greg Claytond8d4a572015-08-11 21:38:15 +00007781 const char *selector_start = ::strchr (name, ' ');
7782 if (selector_start == nullptr)
7783 return nullptr;
7784
7785 selector_start++;
7786 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7787
7788 size_t len = 0;
7789 const char *start;
7790 //printf ("name = '%s'\n", name);
7791
7792 unsigned num_selectors_with_args = 0;
7793 for (start = selector_start;
7794 start && *start != '\0' && *start != ']';
7795 start += len)
7796 {
7797 len = ::strcspn(start, ":]");
7798 bool has_arg = (start[len] == ':');
7799 if (has_arg)
7800 ++num_selectors_with_args;
7801 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7802 if (has_arg)
7803 len += 1;
7804 }
7805
7806
7807 if (selector_idents.size() == 0)
7808 return nullptr;
7809
7810 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7811 selector_idents.data());
7812
7813 clang::QualType method_qual_type (GetQualType(method_clang_type));
7814
7815 // Populate the method decl with parameter decls
7816 const clang::Type *method_type(method_qual_type.getTypePtr());
7817
7818 if (method_type == nullptr)
7819 return nullptr;
7820
7821 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7822
7823 if (!method_function_prototype)
7824 return nullptr;
7825
7826
7827 bool is_variadic = false;
7828 bool is_synthesized = false;
7829 bool is_defined = false;
7830 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7831
7832 const unsigned num_args = method_function_prototype->getNumParams();
7833
7834 if (num_args != num_selectors_with_args)
7835 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7836
7837 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7838 clang::SourceLocation(), // beginLoc,
7839 clang::SourceLocation(), // endLoc,
7840 method_selector,
7841 method_function_prototype->getReturnType(),
7842 nullptr, // TypeSourceInfo *ResultTInfo,
7843 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7844 name[0] == '-',
7845 is_variadic,
7846 is_synthesized,
7847 true, // is_implicitly_declared; we force this to true because we don't have source locations
7848 is_defined,
7849 imp_control,
7850 false /*has_related_result_type*/);
7851
7852
7853 if (objc_method_decl == nullptr)
7854 return nullptr;
7855
7856 if (num_args > 0)
7857 {
7858 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7859
7860 for (unsigned param_index = 0; param_index < num_args; ++param_index)
7861 {
7862 params.push_back (clang::ParmVarDecl::Create (*ast,
7863 objc_method_decl,
7864 clang::SourceLocation(),
7865 clang::SourceLocation(),
7866 nullptr, // anonymous
7867 method_function_prototype->getParamType(param_index),
7868 nullptr,
7869 clang::SC_Auto,
7870 nullptr));
7871 }
7872
7873 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7874 }
7875
7876 class_interface_decl->addDecl (objc_method_decl);
7877
7878#ifdef LLDB_CONFIGURATION_DEBUG
7879 VerifyDecl(objc_method_decl);
7880#endif
7881
7882 return objc_method_decl;
7883}
7884
7885bool
7886ClangASTContext::SetHasExternalStorage (void* type, bool has_extern)
7887{
7888 if (!type)
7889 return false;
7890
7891 clang::QualType qual_type (GetCanonicalQualType(type));
7892
7893 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7894 switch (type_class)
7895 {
7896 case clang::Type::Record:
7897 {
7898 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7899 if (cxx_record_decl)
7900 {
7901 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
7902 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
7903 return true;
7904 }
7905 }
7906 break;
7907
7908 case clang::Type::Enum:
7909 {
7910 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
7911 if (enum_decl)
7912 {
7913 enum_decl->setHasExternalLexicalStorage (has_extern);
7914 enum_decl->setHasExternalVisibleStorage (has_extern);
7915 return true;
7916 }
7917 }
7918 break;
7919
7920 case clang::Type::ObjCObject:
7921 case clang::Type::ObjCInterface:
7922 {
7923 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7924 assert (objc_class_type);
7925 if (objc_class_type)
7926 {
7927 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7928
7929 if (class_interface_decl)
7930 {
7931 class_interface_decl->setHasExternalLexicalStorage (has_extern);
7932 class_interface_decl->setHasExternalVisibleStorage (has_extern);
7933 return true;
7934 }
7935 }
7936 }
7937 break;
7938
7939 case clang::Type::Typedef:
7940 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
7941
7942 case clang::Type::Elaborated:
7943 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
7944
7945 case clang::Type::Paren:
7946 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
7947
7948 default:
7949 break;
7950 }
7951 return false;
7952}
7953
7954
7955#pragma mark TagDecl
7956
7957bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007958ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007959{
7960 if (type)
7961 {
7962
7963 clang::QualType qual_type (GetQualType(type));
7964 const clang::Type *t = qual_type.getTypePtr();
7965 if (t)
7966 {
7967 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
7968 if (tag_type)
7969 {
7970 clang::TagDecl *tag_decl = tag_type->getDecl();
7971 if (tag_decl)
7972 {
7973 tag_decl->startDefinition();
7974 return true;
7975 }
7976 }
7977
7978 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
7979 if (object_type)
7980 {
7981 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
7982 if (interface_decl)
7983 {
7984 interface_decl->startDefinition();
7985 return true;
7986 }
7987 }
7988 }
7989 }
7990 return false;
7991}
7992
7993bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007994ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007995{
7996 if (type)
7997 {
7998 clang::QualType qual_type (GetQualType(type));
7999 if (qual_type.isNull())
8000 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00008001 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8002 if (lldb_ast == nullptr)
8003 return false;
8004 clang::ASTContext *ast = lldb_ast->getASTContext();
8005
Greg Claytond8d4a572015-08-11 21:38:15 +00008006 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8007
8008 if (cxx_record_decl)
8009 {
8010 cxx_record_decl->completeDefinition();
8011
8012 return true;
8013 }
8014
8015 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
8016
8017 if (enutype)
8018 {
8019 clang::EnumDecl *enum_decl = enutype->getDecl();
8020
8021 if (enum_decl)
8022 {
8023 /// TODO This really needs to be fixed.
8024
8025 unsigned NumPositiveBits = 1;
8026 unsigned NumNegativeBits = 0;
8027
8028 clang::QualType promotion_qual_type;
8029 // If the enum integer type is less than an integer in bit width,
8030 // then we must promote it to an integer size.
8031 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8032 {
8033 if (enum_decl->getIntegerType()->isSignedIntegerType())
8034 promotion_qual_type = ast->IntTy;
8035 else
8036 promotion_qual_type = ast->UnsignedIntTy;
8037 }
8038 else
8039 promotion_qual_type = enum_decl->getIntegerType();
8040
8041 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
8042 return true;
8043 }
8044 }
8045 }
8046 return false;
8047}
8048
Greg Claytond8d4a572015-08-11 21:38:15 +00008049bool
Greg Clayton8b4edba2015-08-14 20:02:05 +00008050ClangASTContext::AddEnumerationValueToEnumerationType (void* type,
8051 const CompilerType &enumerator_clang_type,
8052 const Declaration &decl,
8053 const char *name,
8054 int64_t enum_value,
8055 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008056{
8057 if (type && enumerator_clang_type.IsValid() && name && name[0])
8058 {
8059 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8060
8061 bool is_signed = false;
8062 enumerator_clang_type.IsIntegerType (is_signed);
8063 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8064 if (clang_type)
8065 {
8066 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8067
8068 if (enutype)
8069 {
8070 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8071 enum_llvm_apsint = enum_value;
8072 clang::EnumConstantDecl *enumerator_decl =
8073 clang::EnumConstantDecl::Create (*getASTContext(),
8074 enutype->getDecl(),
8075 clang::SourceLocation(),
8076 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8077 GetQualType(enumerator_clang_type),
8078 nullptr,
8079 enum_llvm_apsint);
8080
8081 if (enumerator_decl)
8082 {
8083 enutype->getDecl()->addDecl(enumerator_decl);
8084
8085#ifdef LLDB_CONFIGURATION_DEBUG
8086 VerifyDecl(enumerator_decl);
8087#endif
8088
8089 return true;
8090 }
8091 }
8092 }
8093 }
8094 return false;
8095}
8096
Greg Claytona1e5dc82015-08-11 22:53:00 +00008097CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00008098ClangASTContext::GetEnumerationIntegerType (void* type)
8099{
8100 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8101 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8102 if (clang_type)
8103 {
8104 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8105 if (enutype)
8106 {
8107 clang::EnumDecl *enum_decl = enutype->getDecl();
8108 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008109 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008110 }
8111 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008112 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008113}
8114
Greg Claytona1e5dc82015-08-11 22:53:00 +00008115CompilerType
8116ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008117{
8118 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8119 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008120 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008121 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008122 return CompilerType();
8123 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008124 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8125 GetQualType(type).getTypePtr()));
8126 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008127 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008128}
8129
8130
8131size_t
8132ClangASTContext::ConvertStringToFloatValue (void* type, const char *s, uint8_t *dst, size_t dst_size)
8133{
8134 if (type)
8135 {
8136 clang::QualType qual_type (GetCanonicalQualType(type));
8137 uint32_t count = 0;
8138 bool is_complex = false;
8139 if (IsFloatingPointType (type, count, is_complex))
8140 {
8141 // TODO: handle complex and vector types
8142 if (count != 1)
8143 return false;
8144
8145 llvm::StringRef s_sref(s);
8146 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8147
8148 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8149 const uint64_t byte_size = bit_size / 8;
8150 if (dst_size >= byte_size)
8151 {
8152 if (bit_size == sizeof(float)*8)
8153 {
8154 float float32 = ap_float.convertToFloat();
8155 ::memcpy (dst, &float32, byte_size);
8156 return byte_size;
8157 }
8158 else if (bit_size >= 64)
8159 {
8160 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8161 ::memcpy (dst, ap_int.getRawData(), byte_size);
8162 return byte_size;
8163 }
8164 }
8165 }
8166 }
8167 return 0;
8168}
8169
8170
8171
8172//----------------------------------------------------------------------
8173// Dumping types
8174//----------------------------------------------------------------------
8175#define DEPTH_INCREMENT 2
8176
8177void
8178ClangASTContext::DumpValue (void* type, ExecutionContext *exe_ctx,
8179 Stream *s,
8180 lldb::Format format,
8181 const lldb_private::DataExtractor &data,
8182 lldb::offset_t data_byte_offset,
8183 size_t data_byte_size,
8184 uint32_t bitfield_bit_size,
8185 uint32_t bitfield_bit_offset,
8186 bool show_types,
8187 bool show_summary,
8188 bool verbose,
8189 uint32_t depth)
8190{
8191 if (!type)
8192 return;
8193
8194 clang::QualType qual_type(GetQualType(type));
8195 switch (qual_type->getTypeClass())
8196 {
8197 case clang::Type::Record:
8198 if (GetCompleteType(type))
8199 {
8200 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8201 const clang::RecordDecl *record_decl = record_type->getDecl();
8202 assert(record_decl);
8203 uint32_t field_bit_offset = 0;
8204 uint32_t field_byte_offset = 0;
8205 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8206 uint32_t child_idx = 0;
8207
8208 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8209 if (cxx_record_decl)
8210 {
8211 // We might have base classes to print out first
8212 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8213 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8214 base_class != base_class_end;
8215 ++base_class)
8216 {
8217 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8218
8219 // Skip empty base classes
8220 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8221 continue;
8222
8223 if (base_class->isVirtual())
8224 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8225 else
8226 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8227 field_byte_offset = field_bit_offset / 8;
8228 assert (field_bit_offset % 8 == 0);
8229 if (child_idx == 0)
8230 s->PutChar('{');
8231 else
8232 s->PutChar(',');
8233
8234 clang::QualType base_class_qual_type = base_class->getType();
8235 std::string base_class_type_name(base_class_qual_type.getAsString());
8236
8237 // Indent and print the base class type name
8238 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8239
8240 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8241
8242 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008243 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008244 base_clang_type.DumpValue (exe_ctx,
8245 s, // Stream to dump to
8246 base_clang_type.GetFormat(), // The format with which to display the member
8247 data, // Data buffer containing all bytes for this type
8248 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8249 base_class_type_info.Width / 8, // Size of this type in bytes
8250 0, // Bitfield bit size
8251 0, // Bitfield bit offset
8252 show_types, // Boolean indicating if we should show the variable types
8253 show_summary, // Boolean indicating if we should show a summary for the current type
8254 verbose, // Verbose output?
8255 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8256
8257 ++child_idx;
8258 }
8259 }
8260 uint32_t field_idx = 0;
8261 clang::RecordDecl::field_iterator field, field_end;
8262 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8263 {
8264 // Print the starting squiggly bracket (if this is the
8265 // first member) or comma (for member 2 and beyond) for
8266 // the struct/union/class member.
8267 if (child_idx == 0)
8268 s->PutChar('{');
8269 else
8270 s->PutChar(',');
8271
8272 // Indent
8273 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8274
8275 clang::QualType field_type = field->getType();
8276 // Print the member type if requested
8277 // Figure out the type byte size (field_type_info.first) and
8278 // alignment (field_type_info.second) from the AST context.
8279 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8280 assert(field_idx < record_layout.getFieldCount());
8281 // Figure out the field offset within the current struct/union/class type
8282 field_bit_offset = record_layout.getFieldOffset (field_idx);
8283 field_byte_offset = field_bit_offset / 8;
8284 uint32_t field_bitfield_bit_size = 0;
8285 uint32_t field_bitfield_bit_offset = 0;
8286 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8287 field_bitfield_bit_offset = field_bit_offset % 8;
8288
8289 if (show_types)
8290 {
8291 std::string field_type_name(field_type.getAsString());
8292 if (field_bitfield_bit_size > 0)
8293 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8294 else
8295 s->Printf("(%s) ", field_type_name.c_str());
8296 }
8297 // Print the member name and equal sign
8298 s->Printf("%s = ", field->getNameAsString().c_str());
8299
8300
8301 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008302 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008303 field_clang_type.DumpValue (exe_ctx,
8304 s, // Stream to dump to
8305 field_clang_type.GetFormat(), // The format with which to display the member
8306 data, // Data buffer containing all bytes for this type
8307 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8308 field_type_info.Width / 8, // Size of this type in bytes
8309 field_bitfield_bit_size, // Bitfield bit size
8310 field_bitfield_bit_offset, // Bitfield bit offset
8311 show_types, // Boolean indicating if we should show the variable types
8312 show_summary, // Boolean indicating if we should show a summary for the current type
8313 verbose, // Verbose output?
8314 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8315 }
8316
8317 // Indent the trailing squiggly bracket
8318 if (child_idx > 0)
8319 s->Printf("\n%*s}", depth, "");
8320 }
8321 return;
8322
8323 case clang::Type::Enum:
8324 if (GetCompleteType(type))
8325 {
8326 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8327 const clang::EnumDecl *enum_decl = enutype->getDecl();
8328 assert(enum_decl);
8329 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8330 lldb::offset_t offset = data_byte_offset;
8331 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8332 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8333 {
8334 if (enum_pos->getInitVal() == enum_value)
8335 {
8336 s->Printf("%s", enum_pos->getNameAsString().c_str());
8337 return;
8338 }
8339 }
8340 // If we have gotten here we didn't get find the enumerator in the
8341 // enum decl, so just print the integer.
8342 s->Printf("%" PRIi64, enum_value);
8343 }
8344 return;
8345
8346 case clang::Type::ConstantArray:
8347 {
8348 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8349 bool is_array_of_characters = false;
8350 clang::QualType element_qual_type = array->getElementType();
8351
8352 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8353 if (canonical_type)
8354 is_array_of_characters = canonical_type->isCharType();
8355
8356 const uint64_t element_count = array->getSize().getLimitedValue();
8357
8358 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8359
8360 uint32_t element_idx = 0;
8361 uint32_t element_offset = 0;
8362 uint64_t element_byte_size = field_type_info.Width / 8;
8363 uint32_t element_stride = element_byte_size;
8364
8365 if (is_array_of_characters)
8366 {
8367 s->PutChar('"');
8368 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8369 s->PutChar('"');
8370 return;
8371 }
8372 else
8373 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008374 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008375 lldb::Format element_format = element_clang_type.GetFormat();
8376
8377 for (element_idx = 0; element_idx < element_count; ++element_idx)
8378 {
8379 // Print the starting squiggly bracket (if this is the
8380 // first member) or comman (for member 2 and beyong) for
8381 // the struct/union/class member.
8382 if (element_idx == 0)
8383 s->PutChar('{');
8384 else
8385 s->PutChar(',');
8386
8387 // Indent and print the index
8388 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8389
8390 // Figure out the field offset within the current struct/union/class type
8391 element_offset = element_idx * element_stride;
8392
8393 // Dump the value of the member
8394 element_clang_type.DumpValue (exe_ctx,
8395 s, // Stream to dump to
8396 element_format, // The format with which to display the element
8397 data, // Data buffer containing all bytes for this type
8398 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8399 element_byte_size, // Size of this type in bytes
8400 0, // Bitfield bit size
8401 0, // Bitfield bit offset
8402 show_types, // Boolean indicating if we should show the variable types
8403 show_summary, // Boolean indicating if we should show a summary for the current type
8404 verbose, // Verbose output?
8405 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8406 }
8407
8408 // Indent the trailing squiggly bracket
8409 if (element_idx > 0)
8410 s->Printf("\n%*s}", depth, "");
8411 }
8412 }
8413 return;
8414
8415 case clang::Type::Typedef:
8416 {
8417 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8418
Greg Claytona1e5dc82015-08-11 22:53:00 +00008419 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008420 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8421 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8422 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8423
8424 return typedef_clang_type.DumpValue (exe_ctx,
8425 s, // Stream to dump to
8426 typedef_format, // The format with which to display the element
8427 data, // Data buffer containing all bytes for this type
8428 data_byte_offset, // Offset into "data" where to grab value from
8429 typedef_byte_size, // Size of this type in bytes
8430 bitfield_bit_size, // Bitfield bit size
8431 bitfield_bit_offset,// Bitfield bit offset
8432 show_types, // Boolean indicating if we should show the variable types
8433 show_summary, // Boolean indicating if we should show a summary for the current type
8434 verbose, // Verbose output?
8435 depth); // Scope depth for any types that have children
8436 }
8437 break;
8438
8439 case clang::Type::Elaborated:
8440 {
8441 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008442 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008443 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8444 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8445 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8446
8447 return elaborated_clang_type.DumpValue (exe_ctx,
8448 s, // Stream to dump to
8449 elaborated_format, // The format with which to display the element
8450 data, // Data buffer containing all bytes for this type
8451 data_byte_offset, // Offset into "data" where to grab value from
8452 elaborated_byte_size, // Size of this type in bytes
8453 bitfield_bit_size, // Bitfield bit size
8454 bitfield_bit_offset,// Bitfield bit offset
8455 show_types, // Boolean indicating if we should show the variable types
8456 show_summary, // Boolean indicating if we should show a summary for the current type
8457 verbose, // Verbose output?
8458 depth); // Scope depth for any types that have children
8459 }
8460 break;
8461
8462 case clang::Type::Paren:
8463 {
8464 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008465 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008466
8467 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8468 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8469 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8470
8471 return desugar_clang_type.DumpValue (exe_ctx,
8472 s, // Stream to dump to
8473 desugar_format, // The format with which to display the element
8474 data, // Data buffer containing all bytes for this type
8475 data_byte_offset, // Offset into "data" where to grab value from
8476 desugar_byte_size, // Size of this type in bytes
8477 bitfield_bit_size, // Bitfield bit size
8478 bitfield_bit_offset,// Bitfield bit offset
8479 show_types, // Boolean indicating if we should show the variable types
8480 show_summary, // Boolean indicating if we should show a summary for the current type
8481 verbose, // Verbose output?
8482 depth); // Scope depth for any types that have children
8483 }
8484 break;
8485
8486 default:
8487 // We are down to a scalar type that we just need to display.
8488 data.Dump(s,
8489 data_byte_offset,
8490 format,
8491 data_byte_size,
8492 1,
8493 UINT32_MAX,
8494 LLDB_INVALID_ADDRESS,
8495 bitfield_bit_size,
8496 bitfield_bit_offset);
8497
8498 if (show_summary)
8499 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8500 break;
8501 }
8502}
8503
8504
8505
8506
8507bool
8508ClangASTContext::DumpTypeValue (void* type, Stream *s,
8509 lldb::Format format,
8510 const lldb_private::DataExtractor &data,
8511 lldb::offset_t byte_offset,
8512 size_t byte_size,
8513 uint32_t bitfield_bit_size,
8514 uint32_t bitfield_bit_offset,
8515 ExecutionContextScope *exe_scope)
8516{
8517 if (!type)
8518 return false;
8519 if (IsAggregateType(type))
8520 {
8521 return false;
8522 }
8523 else
8524 {
8525 clang::QualType qual_type(GetQualType(type));
8526
8527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8528 switch (type_class)
8529 {
8530 case clang::Type::Typedef:
8531 {
8532 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008533 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008534 if (format == eFormatDefault)
8535 format = typedef_clang_type.GetFormat();
8536 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8537 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8538
8539 return typedef_clang_type.DumpTypeValue (s,
8540 format, // The format with which to display the element
8541 data, // Data buffer containing all bytes for this type
8542 byte_offset, // Offset into "data" where to grab value from
8543 typedef_byte_size, // Size of this type in bytes
8544 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8545 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8546 exe_scope);
8547 }
8548 break;
8549
8550 case clang::Type::Enum:
8551 // If our format is enum or default, show the enumeration value as
8552 // its enumeration string value, else just display it as requested.
8553 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8554 {
8555 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8556 const clang::EnumDecl *enum_decl = enutype->getDecl();
8557 assert(enum_decl);
8558 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8559 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8560 lldb::offset_t offset = byte_offset;
8561 if (is_signed)
8562 {
8563 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8564 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8565 {
8566 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8567 {
8568 s->PutCString (enum_pos->getNameAsString().c_str());
8569 return true;
8570 }
8571 }
8572 // If we have gotten here we didn't get find the enumerator in the
8573 // enum decl, so just print the integer.
8574 s->Printf("%" PRIi64, enum_svalue);
8575 }
8576 else
8577 {
8578 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8579 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8580 {
8581 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8582 {
8583 s->PutCString (enum_pos->getNameAsString().c_str());
8584 return true;
8585 }
8586 }
8587 // If we have gotten here we didn't get find the enumerator in the
8588 // enum decl, so just print the integer.
8589 s->Printf("%" PRIu64, enum_uvalue);
8590 }
8591 return true;
8592 }
8593 // format was not enum, just fall through and dump the value as requested....
8594
8595 default:
8596 // We are down to a scalar type that we just need to display.
8597 {
8598 uint32_t item_count = 1;
8599 // A few formats, we might need to modify our size and count for depending
8600 // on how we are trying to display the value...
8601 switch (format)
8602 {
8603 default:
8604 case eFormatBoolean:
8605 case eFormatBinary:
8606 case eFormatComplex:
8607 case eFormatCString: // NULL terminated C strings
8608 case eFormatDecimal:
8609 case eFormatEnum:
8610 case eFormatHex:
8611 case eFormatHexUppercase:
8612 case eFormatFloat:
8613 case eFormatOctal:
8614 case eFormatOSType:
8615 case eFormatUnsigned:
8616 case eFormatPointer:
8617 case eFormatVectorOfChar:
8618 case eFormatVectorOfSInt8:
8619 case eFormatVectorOfUInt8:
8620 case eFormatVectorOfSInt16:
8621 case eFormatVectorOfUInt16:
8622 case eFormatVectorOfSInt32:
8623 case eFormatVectorOfUInt32:
8624 case eFormatVectorOfSInt64:
8625 case eFormatVectorOfUInt64:
8626 case eFormatVectorOfFloat32:
8627 case eFormatVectorOfFloat64:
8628 case eFormatVectorOfUInt128:
8629 break;
8630
8631 case eFormatChar:
8632 case eFormatCharPrintable:
8633 case eFormatCharArray:
8634 case eFormatBytes:
8635 case eFormatBytesWithASCII:
8636 item_count = byte_size;
8637 byte_size = 1;
8638 break;
8639
8640 case eFormatUnicode16:
8641 item_count = byte_size / 2;
8642 byte_size = 2;
8643 break;
8644
8645 case eFormatUnicode32:
8646 item_count = byte_size / 4;
8647 byte_size = 4;
8648 break;
8649 }
8650 return data.Dump (s,
8651 byte_offset,
8652 format,
8653 byte_size,
8654 item_count,
8655 UINT32_MAX,
8656 LLDB_INVALID_ADDRESS,
8657 bitfield_bit_size,
8658 bitfield_bit_offset,
8659 exe_scope);
8660 }
8661 break;
8662 }
8663 }
8664 return 0;
8665}
8666
8667
8668
8669void
8670ClangASTContext::DumpSummary (void* type, ExecutionContext *exe_ctx,
8671 Stream *s,
8672 const lldb_private::DataExtractor &data,
8673 lldb::offset_t data_byte_offset,
8674 size_t data_byte_size)
8675{
8676 uint32_t length = 0;
8677 if (IsCStringType (type, length))
8678 {
8679 if (exe_ctx)
8680 {
8681 Process *process = exe_ctx->GetProcessPtr();
8682 if (process)
8683 {
8684 lldb::offset_t offset = data_byte_offset;
8685 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8686 std::vector<uint8_t> buf;
8687 if (length > 0)
8688 buf.resize (length);
8689 else
8690 buf.resize (256);
8691
8692 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8693 buf.back() = '\0';
8694 size_t bytes_read;
8695 size_t total_cstr_len = 0;
8696 Error error;
8697 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8698 {
8699 const size_t len = strlen((const char *)&buf.front());
8700 if (len == 0)
8701 break;
8702 if (total_cstr_len == 0)
8703 s->PutCString (" \"");
8704 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8705 total_cstr_len += len;
8706 if (len < buf.size())
8707 break;
8708 pointer_address += total_cstr_len;
8709 }
8710 if (total_cstr_len > 0)
8711 s->PutChar ('"');
8712 }
8713 }
8714 }
8715}
8716
8717void
8718ClangASTContext::DumpTypeDescription (void* type)
8719{
8720 StreamFile s (stdout, false);
8721 DumpTypeDescription (&s);
8722 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8723 if (metadata)
8724 {
8725 metadata->Dump (&s);
8726 }
8727}
8728
8729void
8730ClangASTContext::DumpTypeDescription (void* type, Stream *s)
8731{
8732 if (type)
8733 {
8734 clang::QualType qual_type(GetQualType(type));
8735
8736 llvm::SmallVector<char, 1024> buf;
8737 llvm::raw_svector_ostream llvm_ostrm (buf);
8738
8739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8740 switch (type_class)
8741 {
8742 case clang::Type::ObjCObject:
8743 case clang::Type::ObjCInterface:
8744 {
8745 GetCompleteType(type);
8746
8747 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8748 assert (objc_class_type);
8749 if (objc_class_type)
8750 {
8751 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8752 if (class_interface_decl)
8753 {
8754 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8755 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8756 }
8757 }
8758 }
8759 break;
8760
8761 case clang::Type::Typedef:
8762 {
8763 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8764 if (typedef_type)
8765 {
8766 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8767 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8768 if (!clang_typedef_name.empty())
8769 {
8770 s->PutCString ("typedef ");
8771 s->PutCString (clang_typedef_name.c_str());
8772 }
8773 }
8774 }
8775 break;
8776
8777 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008778 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008779 return;
8780
8781 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008782 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008783 return;
8784
8785 case clang::Type::Record:
8786 {
8787 GetCompleteType(type);
8788
8789 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8790 const clang::RecordDecl *record_decl = record_type->getDecl();
8791 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8792
8793 if (cxx_record_decl)
8794 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8795 else
8796 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8797 }
8798 break;
8799
8800 default:
8801 {
8802 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8803 if (tag_type)
8804 {
8805 clang::TagDecl *tag_decl = tag_type->getDecl();
8806 if (tag_decl)
8807 tag_decl->print(llvm_ostrm, 0);
8808 }
8809 else
8810 {
8811 std::string clang_type_name(qual_type.getAsString());
8812 if (!clang_type_name.empty())
8813 s->PutCString (clang_type_name.c_str());
8814 }
8815 }
8816 }
8817
Greg Claytond8d4a572015-08-11 21:38:15 +00008818 if (buf.size() > 0)
8819 {
8820 s->Write (buf.data(), buf.size());
8821 }
8822 }
8823}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008824
Greg Clayton8b4edba2015-08-14 20:02:05 +00008825clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00008826ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008827 lldb::AccessType access_type,
8828 const char *parent_name,
8829 int tag_decl_kind,
8830 const ClangASTContext::TemplateParameterInfos &template_param_infos)
8831{
8832 if (template_param_infos.IsValid())
8833 {
8834 std::string template_basename(parent_name);
8835 template_basename.erase (template_basename.find('<'));
8836
8837 return CreateClassTemplateDecl (decl_ctx,
8838 access_type,
8839 template_basename.c_str(),
8840 tag_decl_kind,
8841 template_param_infos);
8842 }
8843 return NULL;
8844}
8845
Greg Clayton6dc8d582015-08-18 22:32:36 +00008846void
8847ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
8848{
8849 ClangASTContext *ast = (ClangASTContext *)baton;
8850 SymbolFile *sym_file = ast->GetSymbolFile();
8851 if (sym_file)
8852 {
8853 CompilerType clang_type = GetTypeForDecl (decl);
8854 if (clang_type)
8855 sym_file->CompleteType (clang_type);
8856 }
8857}
8858
8859void
8860ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
8861{
8862 ClangASTContext *ast = (ClangASTContext *)baton;
8863 SymbolFile *sym_file = ast->GetSymbolFile();
8864 if (sym_file)
8865 {
8866 CompilerType clang_type = GetTypeForDecl (decl);
8867 if (clang_type)
8868 sym_file->CompleteType (clang_type);
8869 }
8870}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008871
Greg Clayton261ac3f2015-08-28 01:01:03 +00008872
8873DWARFASTParser *
8874ClangASTContext::GetDWARFParser ()
8875{
8876 if (!m_dwarf_ast_parser_ap)
8877 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
8878 return m_dwarf_ast_parser_ap.get();
8879}
8880
8881
Greg Clayton8b4edba2015-08-14 20:02:05 +00008882bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00008883ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008884 const clang::RecordDecl *record_decl,
8885 uint64_t &bit_size,
8886 uint64_t &alignment,
8887 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
8888 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
8889 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
8890{
Greg Clayton6dc8d582015-08-18 22:32:36 +00008891 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00008892 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
8893 return dwarf_ast_parser->LayoutRecordType(record_decl, bit_size, alignment, field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00008894}
8895
Greg Clayton99558cc42015-08-24 23:46:31 +00008896//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00008897// CompilerDecl override functions
8898//----------------------------------------------------------------------
8899lldb::VariableSP
8900ClangASTContext::DeclGetVariable (void *opaque_decl)
8901{
8902 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
8903 {
8904 auto decl_search_it = m_decl_objects.find(opaque_decl);
8905 if (decl_search_it != m_decl_objects.end())
8906 return std::static_pointer_cast<Variable>(decl_search_it->second);
8907 }
8908 return VariableSP();
8909}
8910
8911void
8912ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
8913{
8914 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
8915 m_decl_objects.insert(std::make_pair(opaque_decl, object));
8916}
8917
8918ConstString
8919ClangASTContext::DeclGetName (void *opaque_decl)
8920{
8921 if (opaque_decl)
8922 {
8923 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
8924 if (nd != nullptr)
8925 return ConstString(nd->getName());
8926 }
8927 return ConstString();
8928}
8929
8930//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00008931// CompilerDeclContext functions
8932//----------------------------------------------------------------------
8933
Paul Hermand628cbb2015-09-15 23:44:17 +00008934std::vector<void *>
8935ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name)
8936{
8937 std::vector<void *> found_decls;
8938 if (opaque_decl_ctx)
8939 {
8940 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
8941 std::set<DeclContext *> searched;
8942 std::multimap<DeclContext *, DeclContext *> search_queue;
8943
8944 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
8945 {
8946 search_queue.insert(std::make_pair(decl_context, decl_context));
8947
8948 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
8949 {
8950 searched.insert(it->second);
8951 for (clang::Decl *child : it->second->decls())
8952 {
8953 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
8954 {
8955 IdentifierInfo *ii = nd->getIdentifier();
8956 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
8957 found_decls.push_back(nd);
8958 }
8959 else if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
8960 {
8961 clang::DeclContext *from = ud->getCommonAncestor();
8962 if (searched.find(ud->getNominatedNamespace()) == searched.end())
8963 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
8964 }
8965 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
8966 {
8967 for (clang::UsingShadowDecl *usd : ud->shadows())
8968 {
8969 clang::Decl *target = usd->getTargetDecl();
8970 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
8971 {
8972 IdentifierInfo *ii = nd->getIdentifier();
8973 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
8974 found_decls.push_back(nd);
8975 }
8976 }
8977 }
8978 }
8979 }
8980 }
8981 }
8982 return found_decls;
8983}
8984
Greg Clayton99558cc42015-08-24 23:46:31 +00008985bool
8986ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00008987{
Greg Clayton99558cc42015-08-24 23:46:31 +00008988 if (opaque_decl_ctx)
8989 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
8990 else
8991 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00008992}
8993
Greg Clayton99558cc42015-08-24 23:46:31 +00008994ConstString
8995ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00008996{
Greg Clayton99558cc42015-08-24 23:46:31 +00008997 if (opaque_decl_ctx)
8998 {
8999 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9000 if (named_decl)
9001 return ConstString(named_decl->getName());
9002 }
9003 return ConstString();
9004}
9005
9006bool
9007ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9008 lldb::LanguageType *language_ptr,
9009 bool *is_instance_method_ptr,
9010 ConstString *language_object_name_ptr)
9011{
9012 if (opaque_decl_ctx)
9013 {
9014 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9015 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9016 {
9017 if (is_instance_method_ptr)
9018 *is_instance_method_ptr = objc_method->isInstanceMethod();
9019 if (language_ptr)
9020 *language_ptr = eLanguageTypeObjC;
9021 if (language_object_name_ptr)
9022 language_object_name_ptr->SetCString("self");
9023 return true;
9024 }
9025 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9026 {
9027 if (is_instance_method_ptr)
9028 *is_instance_method_ptr = cxx_method->isInstance();
9029 if (language_ptr)
9030 *language_ptr = eLanguageTypeC_plus_plus;
9031 if (language_object_name_ptr)
9032 language_object_name_ptr->SetCString("this");
9033 return true;
9034 }
9035 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9036 {
9037 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9038 if (metadata && metadata->HasObjectPtr())
9039 {
9040 if (is_instance_method_ptr)
9041 *is_instance_method_ptr = true;
9042 if (language_ptr)
9043 *language_ptr = eLanguageTypeObjC;
9044 if (language_object_name_ptr)
9045 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9046 return true;
9047 }
9048 }
9049 }
9050 return false;
9051}
9052
9053clang::DeclContext *
9054ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9055{
9056 if (dc.IsClang())
9057 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9058 return nullptr;
9059}
9060
9061
9062ObjCMethodDecl *
9063ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
9064{
9065 if (dc.IsClang())
9066 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9067 return nullptr;
9068}
9069
9070CXXMethodDecl *
9071ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9072{
9073 if (dc.IsClang())
9074 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9075 return nullptr;
9076}
9077
9078clang::FunctionDecl *
9079ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9080{
9081 if (dc.IsClang())
9082 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9083 return nullptr;
9084}
9085
9086clang::NamespaceDecl *
9087ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9088{
9089 if (dc.IsClang())
9090 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9091 return nullptr;
9092}
9093
9094ClangASTMetadata *
9095ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9096{
9097 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9098 if (ast)
9099 return ClangASTContext::GetMetadata (ast, object);
9100 return nullptr;
9101}
9102
9103clang::ASTContext *
9104ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9105{
Greg Claytonf73034f2015-09-08 18:15:05 +00009106 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9107 if (ast)
9108 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009109 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009110}
9111
Jim Ingham151c0322015-09-15 21:13:50 +00009112ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9113 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
9114 m_target_wp(target.shared_from_this())
9115{
9116}
9117
9118UserExpression *
9119ClangASTContextForExpressions::GetUserExpression (const char *expr,
9120 const char *expr_prefix,
9121 lldb::LanguageType language,
9122 Expression::ResultType desired_type)
9123{
9124 TargetSP target_sp = m_target_wp.lock();
9125 if (!target_sp)
9126 return nullptr;
9127
9128 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type);
9129}
9130
9131FunctionCaller *
9132ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9133 const Address& function_address,
9134 const ValueList &arg_value_list,
9135 const char *name)
9136{
9137 TargetSP target_sp = m_target_wp.lock();
9138 if (!target_sp)
9139 return nullptr;
9140
9141 Process *process = target_sp->GetProcessSP().get();
9142 if (!process)
9143 return nullptr;
9144
9145 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9146}
9147
9148UtilityFunction *
9149ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9150 const char *name)
9151{
9152 TargetSP target_sp = m_target_wp.lock();
9153 if (!target_sp)
9154 return nullptr;
9155
9156 return new ClangUtilityFunction(*target_sp.get(), text, name);
9157}