blob: 3716da82cd35c499596a4c9ee9b7b5a78f3987d9 [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>
Sean Callananfe38c852015-10-08 23:07:53 +000016#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
18// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000019
20// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000021// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000022// or another. This is bad because it means that if clang was built in release
23// mode, it assumes that you are building in release mode which is not always
24// the case. You can end up with functions that are defined as empty in header
25// files when NDEBUG is not defined, and this can cause link errors with the
26// clang .a files that you have since you might be missing functions in the .a
27// file. So we have to define NDEBUG when including clang headers to avoid any
28// mismatches. This is covered by rdar://problem/8691220
29
Sean Callanan3b1d4f62011-10-26 17:46:51 +000030#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000031#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000032#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000033// Need to include assert.h so it is as clang would expect it to be (disabled)
34#include <assert.h>
35#endif
36
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "clang/AST/ASTContext.h"
38#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000039#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000041#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000042#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/RecordLayout.h"
44#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000045#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000047#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000049#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/SourceManager.h"
51#include "clang/Basic/TargetInfo.h"
52#include "clang/Basic/TargetOptions.h"
53#include "clang/Frontend/FrontendOptions.h"
54#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000055
56#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000057#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000058#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
59// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
60#include <assert.h>
61#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Greg Claytond8d4a572015-08-11 21:38:15 +000063#include "llvm/Support/Signals.h"
64
Greg Clayton514487e2011-02-15 21:59:32 +000065#include "lldb/Core/ArchSpec.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000066#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000067#include "lldb/Core/Log.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000068#include "lldb/Core/Module.h"
69#include "lldb/Core/PluginManager.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000070#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000071#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000072#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000073#include "lldb/Core/UniqueCStringMap.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000074#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
75#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
76#include "Plugins/ExpressionParser/Clang/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 Clayton56939cb2015-09-17 22:23:34 +000080#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000081#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000082#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000083#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000085#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000086#include "lldb/Target/Process.h"
87#include "lldb/Target/Target.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000088
Greg Clayton261ac3f2015-08-28 01:01:03 +000089#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
90
Eli Friedman932197d2010-06-13 19:06:42 +000091#include <stdio.h>
92
Greg Clayton1341baf2013-07-11 23:36:31 +000093#include <mutex>
94
Greg Claytonc86103d2010-08-05 01:57:25 +000095using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096using namespace lldb_private;
97using namespace llvm;
98using namespace clang;
99
Greg Clayton56939cb2015-09-17 22:23:34 +0000100namespace
101{
102 static inline bool ClangASTContextSupportsLanguage (lldb::LanguageType language)
103 {
104 return language == eLanguageTypeUnknown || // Clang is the default type system
105 Language::LanguageIsC (language) ||
106 Language::LanguageIsCPlusPlus (language) ||
107 Language::LanguageIsObjC (language);
108 }
109}
110
Enrico Granata2267ad42014-09-16 17:28:40 +0000111typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000112
113static ClangASTMap &
114GetASTMap()
115{
Enrico Granata2267ad42014-09-16 17:28:40 +0000116 static ClangASTMap *g_map_ptr = nullptr;
117 static std::once_flag g_once_flag;
118 std::call_once(g_once_flag, []() {
119 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
120 });
121 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000122}
123
124
Greg Clayton57ee3062013-07-11 22:46:58 +0000125clang::AccessSpecifier
126ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000127{
128 switch (access)
129 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000130 default: break;
131 case eAccessNone: return AS_none;
132 case eAccessPublic: return AS_public;
133 case eAccessPrivate: return AS_private;
134 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000135 }
136 return AS_none;
137}
138
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000140ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141{
142 // FIXME: Cleanup per-file based stuff.
143
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000144 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 // to move these to the language standard, and have the driver resolve the
146 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000147 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000149 } else if (IK == IK_ObjC ||
150 IK == IK_ObjCXX ||
151 IK == IK_PreprocessedObjC ||
152 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 Opts.ObjC1 = Opts.ObjC2 = 1;
154 }
155
156 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
157
158 if (LangStd == LangStandard::lang_unspecified) {
159 // Based on the base language, pick one.
160 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000161 case IK_None:
162 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000163 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000164 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000165 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 LangStd = LangStandard::lang_opencl;
167 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000168 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000169 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000170 LangStd = LangStandard::lang_cuda;
171 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000172 case IK_Asm:
173 case IK_C:
174 case IK_PreprocessedC:
175 case IK_ObjC:
176 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 LangStd = LangStandard::lang_gnu99;
178 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000179 case IK_CXX:
180 case IK_PreprocessedCXX:
181 case IK_ObjCXX:
182 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 LangStd = LangStandard::lang_gnucxx98;
184 break;
185 }
186 }
187
188 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000189 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 Opts.C99 = Std.isC99();
191 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000192 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 Opts.Digraphs = Std.hasDigraphs();
194 Opts.GNUMode = Std.isGNUMode();
195 Opts.GNUInline = !Std.isC99();
196 Opts.HexFloats = Std.hasHexFloats();
197 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000198
199 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
201 // OpenCL has some additional defaults.
202 if (LangStd == LangStandard::lang_opencl) {
203 Opts.OpenCL = 1;
204 Opts.AltiVec = 1;
205 Opts.CXXOperatorNames = 1;
206 Opts.LaxVectorConversions = 1;
207 }
208
209 // OpenCL and C++ both have bool, true, false keywords.
210 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
211
212// if (Opts.CPlusPlus)
213// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
214//
215// if (Args.hasArg(OPT_fobjc_gc_only))
216// Opts.setGCMode(LangOptions::GCOnly);
217// else if (Args.hasArg(OPT_fobjc_gc))
218// Opts.setGCMode(LangOptions::HybridGC);
219//
220// if (Args.hasArg(OPT_print_ivar_layout))
221// Opts.ObjCGCBitmapPrint = 1;
222//
223// if (Args.hasArg(OPT_faltivec))
224// Opts.AltiVec = 1;
225//
226// if (Args.hasArg(OPT_pthread))
227// Opts.POSIXThreads = 1;
228//
229// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
230// "default");
231// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000232 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233// else if (Vis == "hidden")
234// Opts.setVisibilityMode(LangOptions::Hidden);
235// else if (Vis == "protected")
236// Opts.setVisibilityMode(LangOptions::Protected);
237// else
238// Diags.Report(diag::err_drv_invalid_value)
239// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
240
241// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
242
243 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
244 // is specified, or -std is set to a conforming mode.
245 Opts.Trigraphs = !Opts.GNUMode;
246// if (Args.hasArg(OPT_trigraphs))
247// Opts.Trigraphs = 1;
248//
249// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
250// OPT_fno_dollars_in_identifiers,
251// !Opts.AsmPreprocessor);
252// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
253// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
254// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
255// if (Args.hasArg(OPT_fno_lax_vector_conversions))
256// Opts.LaxVectorConversions = 0;
257// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
258// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
259// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000260 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
262// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
263// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
264// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
265// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
266// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
267// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
268// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
269// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
270// Diags);
271// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
272// Opts.ObjCConstantStringClass = getLastArgValue(Args,
273// OPT_fconstant_string_class);
274// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
275// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
276// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
277// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
278// Opts.Static = Args.hasArg(OPT_static_define);
279 Opts.OptimizeSize = 0;
280
281 // FIXME: Eliminate this dependency.
282// unsigned Opt =
283// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
284// Opts.Optimize = Opt != 0;
285 unsigned Opt = 0;
286
287 // This is the __NO_INLINE__ define, which just depends on things like the
288 // optimization level and -fno-inline, not actually whether the backend has
289 // inlining enabled.
290 //
291 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000292 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293
294// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
295// switch (SSP) {
296// default:
297// Diags.Report(diag::err_drv_invalid_value)
298// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
299// break;
300// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
301// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
302// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
303// }
304}
305
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Greg Claytonf73034f2015-09-08 18:15:05 +0000307ClangASTContext::ClangASTContext (const char *target_triple) :
308 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000309 m_target_triple (),
310 m_ast_ap (),
311 m_language_options_ap (),
312 m_source_manager_ap (),
313 m_diagnostics_engine_ap (),
314 m_target_options_rp (),
315 m_target_info_ap (),
316 m_identifier_table_ap (),
317 m_selector_table_ap (),
318 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000319 m_callback_tag_decl (nullptr),
320 m_callback_objc_decl (nullptr),
321 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000322 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000323 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324{
325 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000326 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327}
328
329//----------------------------------------------------------------------
330// Destructor
331//----------------------------------------------------------------------
332ClangASTContext::~ClangASTContext()
333{
Enrico Granata5d84a692014-08-19 21:46:37 +0000334 if (m_ast_ap.get())
335 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000336 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000337 if (!m_ast_owned)
338 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000339 }
340
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 m_builtins_ap.reset();
342 m_selector_table_ap.reset();
343 m_identifier_table_ap.reset();
344 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000345 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000346 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 m_source_manager_ap.reset();
348 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000349 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Greg Clayton56939cb2015-09-17 22:23:34 +0000352ConstString
353ClangASTContext::GetPluginNameStatic()
354{
355 return ConstString("clang");
356}
357
358ConstString
359ClangASTContext::GetPluginName()
360{
361 return ClangASTContext::GetPluginNameStatic();
362}
363
364uint32_t
365ClangASTContext::GetPluginVersion()
366{
367 return 1;
368}
369
370lldb::TypeSystemSP
Tamas Berghammer3a6b82b2015-10-09 12:06:10 +0000371ClangASTContext::CreateInstance (lldb::LanguageType language,
372 lldb_private::Module *module,
373 Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000374{
375 if (ClangASTContextSupportsLanguage(language))
376 {
Greg Clayton5beec212015-10-08 21:04:34 +0000377 ArchSpec arch;
378 if (module)
379 arch = module->GetArchitecture();
380 else if (target)
381 arch = target->GetArchitecture();
382
383 if (arch.IsValid())
Greg Clayton56939cb2015-09-17 22:23:34 +0000384 {
Greg Clayton5beec212015-10-08 21:04:34 +0000385 ArchSpec fixed_arch = arch;
386 // LLVM wants this to be set to iOS or MacOSX; if we're working on
387 // a bare-boards type image, change the triple for llvm's benefit.
388 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
389 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
Greg Clayton56939cb2015-09-17 22:23:34 +0000390 {
Greg Clayton5beec212015-10-08 21:04:34 +0000391 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
392 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
393 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb)
Greg Clayton56939cb2015-09-17 22:23:34 +0000394 {
Greg Clayton5beec212015-10-08 21:04:34 +0000395 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
Greg Clayton56939cb2015-09-17 22:23:34 +0000396 }
Greg Clayton5beec212015-10-08 21:04:34 +0000397 else
398 {
399 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
400 }
401 }
402
403 if (module)
404 {
405 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
406 if (ast_sp)
407 {
408 ast_sp->SetArchitecture (fixed_arch);
409 }
410 return ast_sp;
411 }
412 else if (target)
413 {
414 std::shared_ptr<ClangASTContextForExpressions> ast_sp(new ClangASTContextForExpressions(*target));
415 if (ast_sp)
416 {
417 ast_sp->SetArchitecture(fixed_arch);
418 ast_sp->m_scratch_ast_source_ap.reset (new ClangASTSource(target->shared_from_this()));
419 ast_sp->m_scratch_ast_source_ap->InstallASTContext(ast_sp->getASTContext());
420 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(ast_sp->m_scratch_ast_source_ap->CreateProxy());
421 ast_sp->SetExternalSource(proxy_ast_source);
422 return ast_sp;
423 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000424 }
425 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000426 }
427 return lldb::TypeSystemSP();
428}
429
Sean Callananfe38c852015-10-08 23:07:53 +0000430void
431ClangASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
432{
433 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
434 lldb::eLanguageTypeC89,
435 lldb::eLanguageTypeC,
436 lldb::eLanguageTypeC11,
437 lldb::eLanguageTypeC_plus_plus,
438 lldb::eLanguageTypeC99,
439 lldb::eLanguageTypeObjC,
440 lldb::eLanguageTypeObjC_plus_plus,
441 lldb::eLanguageTypeC_plus_plus_03,
442 lldb::eLanguageTypeC_plus_plus_11,
443 lldb::eLanguageTypeC11,
444 lldb::eLanguageTypeC_plus_plus_14});
445
446 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({
447 lldb::eLanguageTypeC_plus_plus,
448 lldb::eLanguageTypeObjC_plus_plus,
449 lldb::eLanguageTypeC_plus_plus_03,
450 lldb::eLanguageTypeC_plus_plus_11,
451 lldb::eLanguageTypeC_plus_plus_14});
452
453 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
454 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
455}
456
Greg Clayton56939cb2015-09-17 22:23:34 +0000457
458void
459ClangASTContext::Initialize()
460{
461 PluginManager::RegisterPlugin (GetPluginNameStatic(),
462 "clang base AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000463 CreateInstance,
464 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000465}
466
467void
468ClangASTContext::Terminate()
469{
470 PluginManager::UnregisterPlugin (CreateInstance);
471}
472
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473
474void
475ClangASTContext::Clear()
476{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000477 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 m_language_options_ap.reset();
479 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000480 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000481 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 m_target_info_ap.reset();
483 m_identifier_table_ap.reset();
484 m_selector_table_ap.reset();
485 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000486 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487}
488
489const char *
490ClangASTContext::GetTargetTriple ()
491{
492 return m_target_triple.c_str();
493}
494
495void
496ClangASTContext::SetTargetTriple (const char *target_triple)
497{
498 Clear();
499 m_target_triple.assign(target_triple);
500}
501
Greg Clayton514487e2011-02-15 21:59:32 +0000502void
503ClangASTContext::SetArchitecture (const ArchSpec &arch)
504{
Greg Clayton880cbb02011-07-30 01:26:02 +0000505 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000506}
507
Greg Clayton6beaaa62011-01-17 03:46:26 +0000508bool
509ClangASTContext::HasExternalSource ()
510{
511 ASTContext *ast = getASTContext();
512 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000513 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000514 return false;
515}
516
517void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000518ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000519{
520 ASTContext *ast = getASTContext();
521 if (ast)
522 {
523 ast->setExternalSource (ast_source_ap);
524 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
525 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
526 }
527}
528
529void
530ClangASTContext::RemoveExternalSource ()
531{
532 ASTContext *ast = getASTContext();
533
534 if (ast)
535 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000536 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000537 ast->setExternalSource (empty_ast_source_ap);
538 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
539 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
540 }
541}
542
Greg Claytond8d4a572015-08-11 21:38:15 +0000543void
544ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
545{
546 if (!m_ast_owned) {
547 m_ast_ap.release();
548 }
549 m_ast_owned = false;
550 m_ast_ap.reset(ast_ctx);
551 GetASTMap().Insert(ast_ctx, this);
552}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553
554ASTContext *
555ClangASTContext::getASTContext()
556{
Ed Masted4612ad2014-04-20 13:17:36 +0000557 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000559 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000560 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
561 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000562 *getIdentifierTable(),
563 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000564 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000565
566 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000567
568 // This can be NULL if we don't know anything about the architecture or if the
569 // target for an architecture isn't enabled in the llvm/clang that we built
570 TargetInfo *target_info = getTargetInfo();
571 if (target_info)
572 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000573
Greg Clayton6beaaa62011-01-17 03:46:26 +0000574 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
575 {
576 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
577 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
578 }
579
Enrico Granata2267ad42014-09-16 17:28:40 +0000580 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000581
582 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
583 ClangASTContext::CompleteObjCInterfaceDecl,
584 nullptr,
585 ClangASTContext::LayoutRecordType,
586 this));
587 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000589 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590}
591
Enrico Granata5d84a692014-08-19 21:46:37 +0000592ClangASTContext*
593ClangASTContext::GetASTContext (clang::ASTContext* ast)
594{
Enrico Granata2267ad42014-09-16 17:28:40 +0000595 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000596 return clang_ast;
597}
598
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599Builtin::Context *
600ClangASTContext::getBuiltinContext()
601{
Ed Masted4612ad2014-04-20 13:17:36 +0000602 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000603 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 return m_builtins_ap.get();
605}
606
607IdentifierTable *
608ClangASTContext::getIdentifierTable()
609{
Ed Masted4612ad2014-04-20 13:17:36 +0000610 if (m_identifier_table_ap.get() == nullptr)
611 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 return m_identifier_table_ap.get();
613}
614
615LangOptions *
616ClangASTContext::getLanguageOptions()
617{
Ed Masted4612ad2014-04-20 13:17:36 +0000618 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 {
620 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000621 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000622// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 }
624 return m_language_options_ap.get();
625}
626
627SelectorTable *
628ClangASTContext::getSelectorTable()
629{
Ed Masted4612ad2014-04-20 13:17:36 +0000630 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 m_selector_table_ap.reset (new SelectorTable());
632 return m_selector_table_ap.get();
633}
634
Sean Callanan79439e82010-11-18 02:56:27 +0000635clang::FileManager *
636ClangASTContext::getFileManager()
637{
Ed Masted4612ad2014-04-20 13:17:36 +0000638 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000639 {
640 clang::FileSystemOptions file_system_options;
641 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
642 }
Sean Callanan79439e82010-11-18 02:56:27 +0000643 return m_file_manager_ap.get();
644}
645
Greg Claytone1a916a2010-07-21 22:12:05 +0000646clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647ClangASTContext::getSourceManager()
648{
Ed Masted4612ad2014-04-20 13:17:36 +0000649 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000650 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651 return m_source_manager_ap.get();
652}
653
Sean Callanan880e6802011-10-07 23:18:13 +0000654clang::DiagnosticsEngine *
655ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656{
Ed Masted4612ad2014-04-20 13:17:36 +0000657 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000658 {
659 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000660 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000661 }
Sean Callanan880e6802011-10-07 23:18:13 +0000662 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663}
664
Sean Callanan880e6802011-10-07 23:18:13 +0000665class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000666{
667public:
Sean Callanan880e6802011-10-07 23:18:13 +0000668 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000669 {
670 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
671 }
672
Sean Callanan880e6802011-10-07 23:18:13 +0000673 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000674 {
675 if (m_log)
676 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000677 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000678 info.FormatDiagnostic(diag_str);
679 diag_str.push_back('\0');
680 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
681 }
682 }
Sean Callanan880e6802011-10-07 23:18:13 +0000683
684 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
685 {
686 return new NullDiagnosticConsumer ();
687 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000688private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000689 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000690};
691
Sean Callanan880e6802011-10-07 23:18:13 +0000692DiagnosticConsumer *
693ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000694{
Ed Masted4612ad2014-04-20 13:17:36 +0000695 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000696 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000697
Sean Callanan880e6802011-10-07 23:18:13 +0000698 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000699}
700
Jason Molenda45938b92014-07-08 23:46:39 +0000701std::shared_ptr<TargetOptions> &
702ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000703 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 {
Alp Toker5f838642014-07-06 05:36:57 +0000705 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000706 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000707 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 }
Alp Toker5f838642014-07-06 05:36:57 +0000709 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710}
711
712
713TargetInfo *
714ClangASTContext::getTargetInfo()
715{
Greg Clayton70512312012-05-08 01:45:38 +0000716 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000717 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000718 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 return m_target_info_ap.get();
720}
721
722#pragma mark Basic Types
723
724static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000725QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000727 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 if (qual_type_bit_size == bit_size)
729 return true;
730 return false;
731}
Greg Clayton56939cb2015-09-17 22:23:34 +0000732
Greg Claytona1e5dc82015-08-11 22:53:00 +0000733CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +0000734ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, size_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735{
Greg Clayton57ee3062013-07-11 22:46:58 +0000736 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737}
738
Greg Claytona1e5dc82015-08-11 22:53:00 +0000739CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000740ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000742 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000743 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 switch (encoding)
745 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000746 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000747 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000748 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 break;
750
Greg Claytonc86103d2010-08-05 01:57:25 +0000751 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000752 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000753 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000754 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000755 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000756 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000757 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000758 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000759 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000760 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000761 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000762 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000763 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 break;
765
Greg Claytonc86103d2010-08-05 01:57:25 +0000766 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000767 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000768 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000769 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000770 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000771 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000772 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000773 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000774 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000775 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000776 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000777 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000778 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 break;
780
Greg Claytonc86103d2010-08-05 01:57:25 +0000781 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000782 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000783 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000784 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000785 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000786 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000787 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000788 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
789 return CompilerType (ast, ast->HalfTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 break;
791
Greg Claytonc86103d2010-08-05 01:57:25 +0000792 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000793 // Sanity check that bit_size is a multiple of 8's.
794 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000795 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000796 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 }
798
Greg Claytona1e5dc82015-08-11 22:53:00 +0000799 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800}
801
Greg Clayton57ee3062013-07-11 22:46:58 +0000802
803
804lldb::BasicType
805ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
806{
807 if (name)
808 {
809 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
810 static TypeNameToBasicTypeMap g_type_map;
811 static std::once_flag g_once_flag;
812 std::call_once(g_once_flag, [](){
813 // "void"
814 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
815
816 // "char"
817 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
818 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
819 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
820 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
821 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
822 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
823 // "short"
824 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
825 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
826 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
827 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
828
829 // "int"
830 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
831 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
832 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
833 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
834
835 // "long"
836 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
837 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
838 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
839 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
840
841 // "long long"
842 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
843 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
844 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
845 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
846
847 // "int128"
848 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
849 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
850
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000851 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000852 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
853 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
854 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
855 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
856 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
857 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
858 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
859 g_type_map.Sort();
860 });
861
862 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
863 }
864 return eBasicTypeInvalid;
865}
866
Greg Claytona1e5dc82015-08-11 22:53:00 +0000867CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000868ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
869{
870 if (ast)
871 {
872 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
873 return ClangASTContext::GetBasicType (ast, basic_type);
874 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000875 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000876}
877
878uint32_t
879ClangASTContext::GetPointerByteSize ()
880{
881 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000882 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000883 return m_pointer_byte_size;
884}
885
Greg Claytona1e5dc82015-08-11 22:53:00 +0000886CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000887ClangASTContext::GetBasicType (lldb::BasicType basic_type)
888{
889 return GetBasicType (getASTContext(), basic_type);
890}
891
Greg Claytona1e5dc82015-08-11 22:53:00 +0000892CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000893ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
894{
895 if (ast)
896 {
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000897 lldb::opaque_compiler_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000898
899 switch (basic_type)
900 {
901 case eBasicTypeInvalid:
902 case eBasicTypeOther:
903 break;
904 case eBasicTypeVoid:
905 clang_type = ast->VoidTy.getAsOpaquePtr();
906 break;
907 case eBasicTypeChar:
908 clang_type = ast->CharTy.getAsOpaquePtr();
909 break;
910 case eBasicTypeSignedChar:
911 clang_type = ast->SignedCharTy.getAsOpaquePtr();
912 break;
913 case eBasicTypeUnsignedChar:
914 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
915 break;
916 case eBasicTypeWChar:
917 clang_type = ast->getWCharType().getAsOpaquePtr();
918 break;
919 case eBasicTypeSignedWChar:
920 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
921 break;
922 case eBasicTypeUnsignedWChar:
923 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
924 break;
925 case eBasicTypeChar16:
926 clang_type = ast->Char16Ty.getAsOpaquePtr();
927 break;
928 case eBasicTypeChar32:
929 clang_type = ast->Char32Ty.getAsOpaquePtr();
930 break;
931 case eBasicTypeShort:
932 clang_type = ast->ShortTy.getAsOpaquePtr();
933 break;
934 case eBasicTypeUnsignedShort:
935 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
936 break;
937 case eBasicTypeInt:
938 clang_type = ast->IntTy.getAsOpaquePtr();
939 break;
940 case eBasicTypeUnsignedInt:
941 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
942 break;
943 case eBasicTypeLong:
944 clang_type = ast->LongTy.getAsOpaquePtr();
945 break;
946 case eBasicTypeUnsignedLong:
947 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
948 break;
949 case eBasicTypeLongLong:
950 clang_type = ast->LongLongTy.getAsOpaquePtr();
951 break;
952 case eBasicTypeUnsignedLongLong:
953 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
954 break;
955 case eBasicTypeInt128:
956 clang_type = ast->Int128Ty.getAsOpaquePtr();
957 break;
958 case eBasicTypeUnsignedInt128:
959 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
960 break;
961 case eBasicTypeBool:
962 clang_type = ast->BoolTy.getAsOpaquePtr();
963 break;
964 case eBasicTypeHalf:
965 clang_type = ast->HalfTy.getAsOpaquePtr();
966 break;
967 case eBasicTypeFloat:
968 clang_type = ast->FloatTy.getAsOpaquePtr();
969 break;
970 case eBasicTypeDouble:
971 clang_type = ast->DoubleTy.getAsOpaquePtr();
972 break;
973 case eBasicTypeLongDouble:
974 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
975 break;
976 case eBasicTypeFloatComplex:
977 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
978 break;
979 case eBasicTypeDoubleComplex:
980 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
981 break;
982 case eBasicTypeLongDoubleComplex:
983 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
984 break;
985 case eBasicTypeObjCID:
986 clang_type = ast->getObjCIdType().getAsOpaquePtr();
987 break;
988 case eBasicTypeObjCClass:
989 clang_type = ast->getObjCClassType().getAsOpaquePtr();
990 break;
991 case eBasicTypeObjCSel:
992 clang_type = ast->getObjCSelType().getAsOpaquePtr();
993 break;
994 case eBasicTypeNullPtr:
995 clang_type = ast->NullPtrTy.getAsOpaquePtr();
996 break;
997 }
998
999 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001000 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001001 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001002 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001003}
1004
1005
Greg Claytona1e5dc82015-08-11 22:53:00 +00001006CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
1008{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001009 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +00001010
1011#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +00001012 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001013 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014 {
1015 switch (dw_ate)
1016 {
Sean Callanan38d4df52012-04-03 01:10:10 +00001017 default:
1018 break;
Greg Clayton605684e2011-10-28 23:06:08 +00001019
Sean Callanan38d4df52012-04-03 01:10:10 +00001020 case DW_ATE_address:
1021 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001022 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001023 break;
1024
1025 case DW_ATE_boolean:
1026 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001027 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001028 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 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001031 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001032 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001033 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001034 break;
1035
1036 case DW_ATE_lo_user:
1037 // This has been seen to mean DW_AT_complex_integer
1038 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +00001039 {
Sean Callanan38d4df52012-04-03 01:10:10 +00001040 if (::strstr(type_name, "complex"))
1041 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001042 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
1043 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +00001044 }
Greg Clayton605684e2011-10-28 23:06:08 +00001045 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001046 break;
1047
1048 case DW_ATE_complex_float:
1049 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001050 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001051 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001052 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001053 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001054 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001055 else
Greg Clayton605684e2011-10-28 23:06:08 +00001056 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001057 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
1058 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +00001059 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001060 break;
1061
1062 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +00001063 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001064 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001065 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001066 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001067 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001068 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +00001069 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +00001070 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001071 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001072 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001073 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001074 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001075 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +00001076 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
1077 return CompilerType (ast, ast->HalfTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001078 break;
1079
1080 case DW_ATE_signed:
1081 if (type_name)
1082 {
1083 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001084 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001085 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001086 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001087 if (streq(type_name, "void") &&
1088 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001089 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001090 if (strstr(type_name, "long long") &&
1091 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001092 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001093 if (strstr(type_name, "long") &&
1094 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001095 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001096 if (strstr(type_name, "short") &&
1097 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001098 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001099 if (strstr(type_name, "char"))
1100 {
1101 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001102 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001103 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001104 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001105 }
1106 if (strstr(type_name, "int"))
1107 {
1108 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001109 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001110 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001111 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001112 }
1113 }
1114 // We weren't able to match up a type name, just search by size
1115 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001116 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001117 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001118 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001119 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001120 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001121 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001122 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001123 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001124 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001125 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001126 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001127 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001128
Sean Callanan38d4df52012-04-03 01:10:10 +00001129 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001130 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +00001131 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001132 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001133 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001134 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001135 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001136 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001137 break;
1138
1139 case DW_ATE_unsigned:
1140 if (type_name)
1141 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001142 if (streq(type_name, "wchar_t"))
1143 {
1144 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1145 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001146 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001147 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001148 }
1149 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001150 if (strstr(type_name, "long long"))
1151 {
1152 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001153 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001154 }
1155 else if (strstr(type_name, "long"))
1156 {
1157 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001158 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001159 }
1160 else if (strstr(type_name, "short"))
1161 {
1162 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001164 }
1165 else if (strstr(type_name, "char"))
1166 {
1167 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001168 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001169 }
1170 else if (strstr(type_name, "int"))
1171 {
1172 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001173 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001174 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001175 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001176 }
1177 }
1178 // We weren't able to match up a type name, just search by size
1179 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001180 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001181 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001182 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001183 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001184 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001185 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001186 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001187 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001188 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001189 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001190 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001191 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001192
Sean Callanan38d4df52012-04-03 01:10:10 +00001193 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001194 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1195 {
1196 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001197 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001198 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001199 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001200 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001201 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001202 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001203 break;
1204
1205 case DW_ATE_imaginary_float:
1206 break;
1207
1208 case DW_ATE_UTF:
1209 if (type_name)
1210 {
1211 if (streq(type_name, "char16_t"))
1212 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001213 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001214 }
1215 else if (streq(type_name, "char32_t"))
1216 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001217 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001218 }
1219 }
1220 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 }
1222 }
1223 // This assert should fire for anything that we don't catch above so we know
1224 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001225 if (type_name)
1226 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001227 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 +00001228 }
1229 else
1230 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001231 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 +00001232 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001233 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234}
1235
Greg Claytona1e5dc82015-08-11 22:53:00 +00001236CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001237ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1238{
Greg Clayton57ee3062013-07-11 22:46:58 +00001239 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001240 return CompilerType (ast, ast->UnknownAnyTy);
1241 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001242}
1243
Greg Claytona1e5dc82015-08-11 22:53:00 +00001244CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245ClangASTContext::GetCStringType (bool is_const)
1246{
Greg Clayton57ee3062013-07-11 22:46:58 +00001247 ASTContext *ast = getASTContext();
1248 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249
1250 if (is_const)
1251 char_type.addConst();
1252
Greg Claytona1e5dc82015-08-11 22:53:00 +00001253 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254}
1255
Sean Callanan09ab4b72011-11-30 22:11:59 +00001256clang::DeclContext *
1257ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1258{
1259 return ast->getTranslationUnitDecl();
1260}
1261
Greg Clayton526e5af2010-11-13 03:52:47 +00001262clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001263ClangASTContext::CopyDecl (ASTContext *dst_ast,
1264 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001265 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001266{
Sean Callanan79439e82010-11-18 02:56:27 +00001267 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001268 FileManager file_manager (file_system_options);
1269 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001270 *src_ast, file_manager,
1271 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001272
1273 return importer.Import(source_decl);
1274}
1275
Sean Callanan23a30272010-07-16 00:00:27 +00001276bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001277ClangASTContext::AreTypesSame (CompilerType type1,
1278 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001279 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001280{
Greg Claytonf73034f2015-09-08 18:15:05 +00001281 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1282 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001283 return false;
1284
1285 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001286 return true;
1287
Greg Claytond8d4a572015-08-11 21:38:15 +00001288 QualType type1_qual = GetQualType(type1);
1289 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001290
1291 if (ignore_qualifiers)
1292 {
1293 type1_qual = type1_qual.getUnqualifiedType();
1294 type2_qual = type2_qual.getUnqualifiedType();
1295 }
1296
Greg Claytonf73034f2015-09-08 18:15:05 +00001297 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298}
1299
Greg Claytona1e5dc82015-08-11 22:53:00 +00001300CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001301ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1302{
1303 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1304 return GetTypeForDecl(interface_decl);
1305 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1306 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001307 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001308}
1309
Greg Clayton6beaaa62011-01-17 03:46:26 +00001310
Greg Claytona1e5dc82015-08-11 22:53:00 +00001311CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001312ClangASTContext::GetTypeForDecl (TagDecl *decl)
1313{
1314 // No need to call the getASTContext() accessor (which can create the AST
1315 // if it isn't created yet, because we can't have created a decl in this
1316 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001317 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001318 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001319 return CompilerType (ast, ast->getTagDeclType(decl));
1320 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001321}
1322
Greg Claytona1e5dc82015-08-11 22:53:00 +00001323CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001324ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1325{
1326 // No need to call the getASTContext() accessor (which can create the AST
1327 // if it isn't created yet, because we can't have created a decl in this
1328 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001329 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001330 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001331 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1332 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001333}
1334
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335#pragma mark Structure, Unions, Classes
1336
Greg Claytona1e5dc82015-08-11 22:53:00 +00001337CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001338ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1339 AccessType access_type,
1340 const char *name,
1341 int kind,
1342 LanguageType language,
1343 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001345 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001346 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001347
Ed Masted4612ad2014-04-20 13:17:36 +00001348 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001349 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350
Greg Clayton9e409562010-07-28 02:04:09 +00001351
Greg Claytone1be9962011-08-24 23:50:00 +00001352 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001353 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001354 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001355 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001356 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001357 }
1358
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1360 // we will need to update this code. I was told to currently always use
1361 // the CXXRecordDecl class since we often don't know from debug information
1362 // if something is struct or a class, so we default to always use the more
1363 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001364
1365 bool is_anonymous = (!name) || (!name[0]);
1366
Greg Claytonf0705c82011-10-22 03:33:13 +00001367 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1368 (TagDecl::TagKind)kind,
1369 decl_ctx,
1370 SourceLocation(),
1371 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001372 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001373
1374 if (is_anonymous)
1375 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001376
Greg Claytonc4ffd662013-03-08 01:37:30 +00001377 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001378 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001379 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001380 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001381
Greg Clayton55561e92011-10-26 03:31:36 +00001382 if (access_type != eAccessNone)
1383 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001384
1385 if (decl_ctx)
1386 decl_ctx->addDecl (decl);
1387
Greg Claytona1e5dc82015-08-11 22:53:00 +00001388 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001389 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001390 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001391}
1392
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001393static TemplateParameterList *
1394CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001395 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001396 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1397{
1398 const bool parameter_pack = false;
1399 const bool is_typename = false;
1400 const unsigned depth = 0;
1401 const size_t num_template_params = template_param_infos.GetSize();
1402 for (size_t i=0; i<num_template_params; ++i)
1403 {
1404 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001405
Ed Masted4612ad2014-04-20 13:17:36 +00001406 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001407 if (name && name[0])
1408 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001409 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001410 {
1411 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1412 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1413 SourceLocation(),
1414 SourceLocation(),
1415 depth,
1416 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001417 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001418 template_param_infos.args[i].getIntegralType(),
1419 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001420 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001421
1422 }
1423 else
1424 {
1425 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1426 ast->getTranslationUnitDecl(), // Is this the right decl context?
1427 SourceLocation(),
1428 SourceLocation(),
1429 depth,
1430 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001431 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001432 is_typename,
1433 parameter_pack));
1434 }
1435 }
1436
1437 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1438 SourceLocation(),
1439 SourceLocation(),
1440 &template_param_decls.front(),
1441 template_param_decls.size(),
1442 SourceLocation());
1443 return template_param_list;
1444}
1445
1446clang::FunctionTemplateDecl *
1447ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1448 clang::FunctionDecl *func_decl,
1449 const char *name,
1450 const TemplateParameterInfos &template_param_infos)
1451{
1452// /// \brief Create a function template node.
1453 ASTContext *ast = getASTContext();
1454
1455 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1456
1457 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1458 template_param_infos,
1459 template_param_decls);
1460 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1461 decl_ctx,
1462 func_decl->getLocation(),
1463 func_decl->getDeclName(),
1464 template_param_list,
1465 func_decl);
1466
1467 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1468 i < template_param_decl_count;
1469 ++i)
1470 {
1471 // TODO: verify which decl context we should put template_param_decls into..
1472 template_param_decls[i]->setDeclContext (func_decl);
1473 }
1474
1475 return func_tmpl_decl;
1476}
1477
1478void
1479ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1480 clang::FunctionTemplateDecl *func_tmpl_decl,
1481 const TemplateParameterInfos &infos)
1482{
1483 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1484 infos.args.data(),
1485 infos.args.size());
1486
1487 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1488 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001489 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001490}
1491
1492
Greg Claytonf0705c82011-10-22 03:33:13 +00001493ClassTemplateDecl *
1494ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001495 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001496 const char *class_name,
1497 int kind,
1498 const TemplateParameterInfos &template_param_infos)
1499{
1500 ASTContext *ast = getASTContext();
1501
Ed Masted4612ad2014-04-20 13:17:36 +00001502 ClassTemplateDecl *class_template_decl = nullptr;
1503 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001504 decl_ctx = ast->getTranslationUnitDecl();
1505
1506 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1507 DeclarationName decl_name (&identifier_info);
1508
1509 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001510
1511 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001512 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001513 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001514 if (class_template_decl)
1515 return class_template_decl;
1516 }
1517
1518 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001519
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001520 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1521 template_param_infos,
1522 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001523
1524 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1525 (TagDecl::TagKind)kind,
1526 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1527 SourceLocation(),
1528 SourceLocation(),
1529 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001530
1531 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1532 i < template_param_decl_count;
1533 ++i)
1534 {
1535 template_param_decls[i]->setDeclContext (template_cxx_decl);
1536 }
1537
Sean Callananb5c79622011-11-19 01:35:08 +00001538 // With templated classes, we say that a class is templated with
1539 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001540 //template_cxx_decl->startDefinition();
1541 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001542
Greg Claytonf0705c82011-10-22 03:33:13 +00001543 class_template_decl = ClassTemplateDecl::Create (*ast,
1544 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1545 SourceLocation(),
1546 decl_name,
1547 template_param_list,
1548 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001549 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001550
1551 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001552 {
Greg Clayton55561e92011-10-26 03:31:36 +00001553 if (access_type != eAccessNone)
1554 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001555
1556 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1557 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1558
Greg Claytonf0705c82011-10-22 03:33:13 +00001559 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001560
1561#ifdef LLDB_CONFIGURATION_DEBUG
1562 VerifyDecl(class_template_decl);
1563#endif
1564 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001565
1566 return class_template_decl;
1567}
1568
1569
1570ClassTemplateSpecializationDecl *
1571ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1572 ClassTemplateDecl *class_template_decl,
1573 int kind,
1574 const TemplateParameterInfos &template_param_infos)
1575{
1576 ASTContext *ast = getASTContext();
1577 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1578 (TagDecl::TagKind)kind,
1579 decl_ctx,
1580 SourceLocation(),
1581 SourceLocation(),
1582 class_template_decl,
1583 &template_param_infos.args.front(),
1584 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001585 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001586
Sean Callananfa4fab72013-02-01 06:55:48 +00001587 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1588
Greg Claytonf0705c82011-10-22 03:33:13 +00001589 return class_template_specialization_decl;
1590}
1591
Greg Claytona1e5dc82015-08-11 22:53:00 +00001592CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001593ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1594{
1595 if (class_template_specialization_decl)
1596 {
1597 ASTContext *ast = getASTContext();
1598 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001599 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001600 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001601 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001602}
1603
Greg Clayton090d0982011-06-19 03:43:27 +00001604static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001605check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001606{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001607 // Special-case call since it can take any number of operands
1608 if(op_kind == OO_Call)
1609 return true;
1610
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001611 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001612 if (num_params == 0)
1613 return unary;
1614 if (num_params == 1)
1615 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001616 else
Greg Clayton090d0982011-06-19 03:43:27 +00001617 return false;
1618}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001619
Greg Clayton090d0982011-06-19 03:43:27 +00001620bool
1621ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1622{
Sean Callanan5b26f272012-02-04 08:49:35 +00001623 switch (op_kind)
1624 {
1625 default:
1626 break;
1627 // C++ standard allows any number of arguments to new/delete
1628 case OO_New:
1629 case OO_Array_New:
1630 case OO_Delete:
1631 case OO_Array_Delete:
1632 return true;
1633 }
1634
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001635#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 +00001636 switch (op_kind)
1637 {
1638#include "clang/Basic/OperatorKinds.def"
1639 default: break;
1640 }
1641 return false;
1642}
1643
Greg Clayton57ee3062013-07-11 22:46:58 +00001644clang::AccessSpecifier
1645ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001646{
1647 clang::AccessSpecifier ret = lhs;
1648
1649 // Make the access equal to the stricter of the field and the nested field's access
1650 switch (ret)
1651 {
1652 case clang::AS_none:
1653 break;
1654 case clang::AS_private:
1655 break;
1656 case clang::AS_protected:
1657 if (rhs == AS_private)
1658 ret = AS_private;
1659 break;
1660 case clang::AS_public:
1661 ret = rhs;
1662 break;
1663 }
1664
1665 return ret;
1666}
1667
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668bool
1669ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1670{
1671 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1672}
1673
1674bool
1675ClangASTContext::FieldIsBitfield
1676(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001677 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678 FieldDecl* field,
1679 uint32_t& bitfield_bit_size
1680)
1681{
Ed Masted4612ad2014-04-20 13:17:36 +00001682 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 return false;
1684
1685 if (field->isBitField())
1686 {
1687 Expr* bit_width_expr = field->getBitWidth();
1688 if (bit_width_expr)
1689 {
1690 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001691 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692 {
1693 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1694 return true;
1695 }
1696 }
1697 }
1698 return false;
1699}
1700
1701bool
1702ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1703{
Ed Masted4612ad2014-04-20 13:17:36 +00001704 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705 return false;
1706
1707 if (!record_decl->field_empty())
1708 return true;
1709
1710 // No fields, lets check this is a CXX record and check the base classes
1711 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1712 if (cxx_record_decl)
1713 {
1714 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1715 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1716 base_class != base_class_end;
1717 ++base_class)
1718 {
1719 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1720 if (RecordHasFields(base_class_decl))
1721 return true;
1722 }
1723 }
1724 return false;
1725}
1726
Greg Clayton8cf05932010-07-22 18:30:50 +00001727#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728
Greg Claytona1e5dc82015-08-11 22:53:00 +00001729CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001730ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001731(
1732 const char *name,
1733 DeclContext *decl_ctx,
1734 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001735 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001736 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001737)
1738{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001739 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001740 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001741 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001742 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001743 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001744
Greg Clayton6beaaa62011-01-17 03:46:26 +00001745 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001746 decl_ctx,
1747 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001748 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001749 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001750 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001751 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001752 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001753 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001754
Jim Ingham379397632012-10-27 02:54:13 +00001755 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001756 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001757
Greg Claytona1e5dc82015-08-11 22:53:00 +00001758 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759}
1760
1761static inline bool
1762BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1763{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001764 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765}
1766
Greg Clayton57ee3062013-07-11 22:46:58 +00001767uint32_t
1768ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769{
1770 uint32_t num_bases = 0;
1771 if (cxx_record_decl)
1772 {
1773 if (omit_empty_base_classes)
1774 {
1775 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1776 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1777 base_class != base_class_end;
1778 ++base_class)
1779 {
1780 // Skip empty base classes
1781 if (omit_empty_base_classes)
1782 {
1783 if (BaseSpecifierIsEmpty (base_class))
1784 continue;
1785 }
1786 ++num_bases;
1787 }
1788 }
1789 else
1790 num_bases = cxx_record_decl->getNumBases();
1791 }
1792 return num_bases;
1793}
1794
1795
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796#pragma mark Namespace Declarations
1797
1798NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001799ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800{
Ed Masted4612ad2014-04-20 13:17:36 +00001801 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001802 ASTContext *ast = getASTContext();
1803 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001804 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001805 decl_ctx = translation_unit_decl;
1806
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807 if (name)
1808 {
Greg Clayton030a2042011-10-14 21:34:45 +00001809 IdentifierInfo &identifier_info = ast->Idents.get(name);
1810 DeclarationName decl_name (&identifier_info);
1811 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001812 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001813 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001814 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001815 if (namespace_decl)
1816 return namespace_decl;
1817 }
1818
Sean Callanan5b26f272012-02-04 08:49:35 +00001819 namespace_decl = NamespaceDecl::Create(*ast,
1820 decl_ctx,
1821 false,
1822 SourceLocation(),
1823 SourceLocation(),
1824 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001825 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001826
Greg Clayton9d3d6882011-10-31 23:51:19 +00001827 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001829 else
1830 {
1831 if (decl_ctx == translation_unit_decl)
1832 {
1833 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1834 if (namespace_decl)
1835 return namespace_decl;
1836
Sean Callanan5b26f272012-02-04 08:49:35 +00001837 namespace_decl = NamespaceDecl::Create(*ast,
1838 decl_ctx,
1839 false,
1840 SourceLocation(),
1841 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001842 nullptr,
1843 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001844 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1845 translation_unit_decl->addDecl (namespace_decl);
1846 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1847 }
1848 else
1849 {
1850 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1851 if (parent_namespace_decl)
1852 {
1853 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1854 if (namespace_decl)
1855 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001856 namespace_decl = NamespaceDecl::Create(*ast,
1857 decl_ctx,
1858 false,
1859 SourceLocation(),
1860 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001861 nullptr,
1862 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001863 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1864 parent_namespace_decl->addDecl (namespace_decl);
1865 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1866 }
1867 else
1868 {
1869 // BAD!!!
1870 }
1871 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001872 }
1873#ifdef LLDB_CONFIGURATION_DEBUG
1874 VerifyDecl(namespace_decl);
1875#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001876 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877}
1878
1879
Paul Hermand628cbb2015-09-15 23:44:17 +00001880clang::BlockDecl *
1881ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1882{
1883 if (ctx != nullptr)
1884 {
1885 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1886 ctx->addDecl(decl);
1887 return decl;
1888 }
1889 return nullptr;
1890}
1891
Paul Hermanea188fc2015-09-16 18:48:30 +00001892clang::DeclContext *
1893FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1894{
1895 if (root == nullptr)
1896 return nullptr;
1897
1898 std::set<clang::DeclContext *> path_left;
1899 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1900 path_left.insert(d);
1901
1902 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1903 if (path_left.find(d) != path_left.end())
1904 return d;
1905
1906 return nullptr;
1907}
1908
Paul Hermand628cbb2015-09-15 23:44:17 +00001909clang::UsingDirectiveDecl *
1910ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1911{
1912 if (decl_ctx != nullptr && ns_decl != nullptr)
1913 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001914 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001915 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1916 decl_ctx,
1917 clang::SourceLocation(),
1918 clang::SourceLocation(),
1919 clang::NestedNameSpecifierLoc(),
1920 clang::SourceLocation(),
1921 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001922 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001923 decl_ctx->addDecl(using_decl);
1924 return using_decl;
1925 }
1926 return nullptr;
1927}
1928
1929clang::UsingDecl *
1930ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1931{
1932 if (current_decl_ctx != nullptr && target != nullptr)
1933 {
1934 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1935 current_decl_ctx,
1936 clang::SourceLocation(),
1937 clang::NestedNameSpecifierLoc(),
1938 clang::DeclarationNameInfo(),
1939 false);
1940 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1941 current_decl_ctx,
1942 clang::SourceLocation(),
1943 using_decl,
1944 target);
1945 using_decl->addShadowDecl(shadow_decl);
1946 current_decl_ctx->addDecl(using_decl);
1947 return using_decl;
1948 }
1949 return nullptr;
1950}
1951
1952clang::VarDecl *
1953ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1954{
1955 if (decl_context != nullptr)
1956 {
1957 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1958 decl_context,
1959 clang::SourceLocation(),
1960 clang::SourceLocation(),
1961 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1962 type,
1963 nullptr,
1964 clang::SC_None);
1965 var_decl->setAccess(clang::AS_public);
1966 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001967 return var_decl;
1968 }
1969 return nullptr;
1970}
1971
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972#pragma mark Function Types
1973
1974FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001975ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1976 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001977 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001978 int storage,
1979 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980{
Ed Masted4612ad2014-04-20 13:17:36 +00001981 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001982 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001983 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001984 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985
Greg Clayton0d551042013-06-28 21:08:47 +00001986
1987 const bool hasWrittenPrototype = true;
1988 const bool isConstexprSpecified = false;
1989
Greg Clayton147e1fa2011-10-14 22:47:18 +00001990 if (name && name[0])
1991 {
1992 func_decl = FunctionDecl::Create (*ast,
1993 decl_ctx,
1994 SourceLocation(),
1995 SourceLocation(),
1996 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00001997 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00001998 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00001999 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00002000 is_inline,
2001 hasWrittenPrototype,
2002 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00002004 else
2005 {
2006 func_decl = FunctionDecl::Create (*ast,
2007 decl_ctx,
2008 SourceLocation(),
2009 SourceLocation(),
2010 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00002011 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002012 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002013 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00002014 is_inline,
2015 hasWrittenPrototype,
2016 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00002017 }
2018 if (func_decl)
2019 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002020
2021#ifdef LLDB_CONFIGURATION_DEBUG
2022 VerifyDecl(func_decl);
2023#endif
2024
Greg Clayton147e1fa2011-10-14 22:47:18 +00002025 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026}
2027
Greg Claytona1e5dc82015-08-11 22:53:00 +00002028CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00002029ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002030 const CompilerType& result_type,
2031 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00002032 unsigned num_args,
2033 bool is_variadic,
2034 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035{
Ed Masted4612ad2014-04-20 13:17:36 +00002036 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037 std::vector<QualType> qual_type_args;
2038 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00002039 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002040
2041 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00002042 FunctionProtoType::ExtProtoInfo proto_info;
2043 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002044 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00002045 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00002046 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002047
Greg Claytona1e5dc82015-08-11 22:53:00 +00002048 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002049 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00002050 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051}
2052
2053ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00002054ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002056 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002057 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00002058 return ParmVarDecl::Create(*ast,
2059 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002061 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002062 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002063 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002064 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002065 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00002066 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067}
2068
2069void
2070ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2071{
2072 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002073 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002074}
2075
2076
2077#pragma mark Array Types
2078
Greg Claytona1e5dc82015-08-11 22:53:00 +00002079CompilerType
2080ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002081 size_t element_count,
2082 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002083{
Greg Clayton57ee3062013-07-11 22:46:58 +00002084 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002086 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002087 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002088
Greg Clayton1c8ef472013-04-05 23:27:21 +00002089 if (is_vector)
2090 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002091 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00002092 }
2093 else
2094 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002095
2096 llvm::APInt ap_element_count (64, element_count);
2097 if (element_count == 0)
2098 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002099 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002100 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002101 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002102 }
2103 else
2104 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002105 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002106 ap_element_count,
2107 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002108 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002109 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002111 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002112 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113}
2114
Greg Claytona1e5dc82015-08-11 22:53:00 +00002115CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00002116ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002117 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00002118 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00002119{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002120 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002121 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2122 return type;
2123 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00002124 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002125 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00002126 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00002127 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00002128 SetIsPacked(type);
2129 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002130 return type;
2131}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002132
2133#pragma mark Enumeration Types
2134
Greg Claytona1e5dc82015-08-11 22:53:00 +00002135CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002136ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002137(
Greg Claytond8d4a572015-08-11 21:38:15 +00002138 const char *name,
2139 DeclContext *decl_ctx,
2140 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002141 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002142 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143{
2144 // TODO: Do something intelligent with the Declaration object passed in
2145 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002146 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002147
Greg Claytone02b8502010-10-12 04:29:14 +00002148 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002149 // const bool IsScoped = false;
2150 // const bool IsFixed = false;
2151
Greg Clayton6beaaa62011-01-17 03:46:26 +00002152 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002153 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002154 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002155 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002156 name && name[0] ? &ast->Idents.get(name) : nullptr,
2157 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002158 false, // IsScoped
2159 false, // IsScopedUsingClassTag
2160 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002161
2162
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002164 {
2165 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00002166 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00002167
2168 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2169
Greg Claytona1e5dc82015-08-11 22:53:00 +00002170 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002171 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002172 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002173}
2174
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002175// Disable this for now since I can't seem to get a nicely formatted float
2176// out of the APFloat class without just getting the float, double or quad
2177// and then using a formatted print on it which defeats the purpose. We ideally
2178// would like to get perfect string values for any kind of float semantics
2179// so we can support remote targets. The code below also requires a patch to
2180// llvm::APInt.
2181//bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002182//ClangASTContext::ConvertFloatValueToString (ASTContext *ast, lldb::opaque_compiler_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 +00002183//{
2184// uint32_t count = 0;
2185// bool is_complex = false;
2186// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2187// {
2188// unsigned num_bytes_per_float = byte_size / count;
2189// unsigned num_bits_per_float = num_bytes_per_float * 8;
2190//
2191// float_str.clear();
2192// uint32_t i;
2193// for (i=0; i<count; i++)
2194// {
2195// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2196// bool is_ieee = false;
2197// APFloat ap_float(ap_int, is_ieee);
2198// char s[1024];
2199// unsigned int hex_digits = 0;
2200// bool upper_case = false;
2201//
2202// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2203// {
2204// if (i > 0)
2205// float_str.append(", ");
2206// float_str.append(s);
2207// if (i == 1 && is_complex)
2208// float_str.append(1, 'i');
2209// }
2210// }
2211// return !float_str.empty();
2212// }
2213// return false;
2214//}
2215
Greg Claytona1e5dc82015-08-11 22:53:00 +00002216CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002217ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2218 size_t bit_size, bool is_signed)
2219{
2220 if (ast)
2221 {
2222 if (is_signed)
2223 {
2224 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002225 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002226
2227 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002228 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002229
2230 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002231 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002232
2233 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002234 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002235
2236 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002237 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002238
2239 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002240 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002241 }
2242 else
2243 {
2244 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002245 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002246
2247 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002248 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002249
2250 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002251 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002252
2253 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002254 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002257 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002260 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002261 }
2262 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002263 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002264}
2265
Greg Claytona1e5dc82015-08-11 22:53:00 +00002266CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002267ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2268{
2269 if (ast)
2270 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002271 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002272}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002273
Enrico Granata86027e92012-03-24 01:11:14 +00002274bool
Greg Claytona2721472011-06-25 00:44:06 +00002275ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2276 clang::Decl *decl)
2277{
2278 if (!decl)
2279 return false;
2280
2281 ExternalASTSource *ast_source = ast->getExternalSource();
2282
2283 if (!ast_source)
2284 return false;
2285
2286 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2287 {
Greg Clayton219cf312012-03-30 00:51:13 +00002288 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002289 return true;
2290
2291 if (!tag_decl->hasExternalLexicalStorage())
2292 return false;
2293
2294 ast_source->CompleteType(tag_decl);
2295
2296 return !tag_decl->getTypeForDecl()->isIncompleteType();
2297 }
2298 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2299 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002300 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002301 return true;
2302
2303 if (!objc_interface_decl->hasExternalLexicalStorage())
2304 return false;
2305
2306 ast_source->CompleteType(objc_interface_decl);
2307
Sean Callanan5b26f272012-02-04 08:49:35 +00002308 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002309 }
2310 else
2311 {
2312 return false;
2313 }
2314}
2315
Sean Callanan60217122012-04-13 00:10:03 +00002316void
Greg Claytond0029442013-03-27 01:48:02 +00002317ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002318 user_id_t user_id)
2319{
2320 ClangASTMetadata meta_data;
2321 meta_data.SetUserID (user_id);
2322 SetMetadata (object, meta_data);
2323}
2324
2325void
Sean Callanan60217122012-04-13 00:10:03 +00002326ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002327 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002328 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002329{
2330 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002331 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002332
2333 if (external_source)
2334 external_source->SetMetadata(object, metadata);
2335}
2336
Jim Ingham379397632012-10-27 02:54:13 +00002337ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002338ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002339 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002340{
2341 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002342 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002343
2344 if (external_source && external_source->HasMetadata(object))
2345 return external_source->GetMetadata(object);
2346 else
Ed Masted4612ad2014-04-20 13:17:36 +00002347 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002348}
2349
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002350clang::DeclContext *
2351ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2352{
Sean Callanana87bee82011-08-19 06:19:25 +00002353 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002354}
2355
2356clang::DeclContext *
2357ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2358{
Sean Callanana87bee82011-08-19 06:19:25 +00002359 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002360}
2361
Greg Claytond8d4a572015-08-11 21:38:15 +00002362bool
2363ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2364{
2365 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2366 if (clang_type)
2367 {
2368 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2369 if (tag_type)
2370 {
2371 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2372 if (tag_decl)
2373 {
2374 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2375 return true;
2376 }
2377 }
2378 }
2379 return false;
2380}
2381
2382
2383bool
2384ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2385 int default_accessibility,
2386 int *assigned_accessibilities,
2387 size_t num_assigned_accessibilities)
2388{
2389 if (record_decl)
2390 {
2391 uint32_t field_idx;
2392 clang::RecordDecl::field_iterator field, field_end;
2393 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2394 field != field_end;
2395 ++field, ++field_idx)
2396 {
2397 // If no accessibility was assigned, assign the correct one
2398 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2399 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2400 }
2401 return true;
2402 }
2403 return false;
2404}
2405
2406clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002407ClangASTContext::GetDeclContextForType (const CompilerType& type)
2408{
2409 return GetDeclContextForType(GetQualType(type));
2410}
2411
2412clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002413ClangASTContext::GetDeclContextForType (clang::QualType type)
2414{
2415 if (type.isNull())
2416 return nullptr;
2417
2418 clang::QualType qual_type = type.getCanonicalType();
2419 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2420 switch (type_class)
2421 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002422 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2423 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2424 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2425 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2426 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2427 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2428 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002429 default:
2430 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002431 }
2432 // No DeclContext in this type...
2433 return nullptr;
2434}
2435
2436static bool
2437GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2438{
2439 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2440 switch (type_class)
2441 {
2442 case clang::Type::ConstantArray:
2443 case clang::Type::IncompleteArray:
2444 case clang::Type::VariableArray:
2445 {
2446 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2447
2448 if (array_type)
2449 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2450 }
2451 break;
2452
2453 case clang::Type::Record:
2454 case clang::Type::Enum:
2455 {
2456 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2457 if (tag_type)
2458 {
2459 clang::TagDecl *tag_decl = tag_type->getDecl();
2460 if (tag_decl)
2461 {
2462 if (tag_decl->isCompleteDefinition())
2463 return true;
2464
2465 if (!allow_completion)
2466 return false;
2467
2468 if (tag_decl->hasExternalLexicalStorage())
2469 {
2470 if (ast)
2471 {
2472 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2473 if (external_ast_source)
2474 {
2475 external_ast_source->CompleteType(tag_decl);
2476 return !tag_type->isIncompleteType();
2477 }
2478 }
2479 }
2480 return false;
2481 }
2482 }
2483
2484 }
2485 break;
2486
2487 case clang::Type::ObjCObject:
2488 case clang::Type::ObjCInterface:
2489 {
2490 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2491 if (objc_class_type)
2492 {
2493 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2494 // We currently can't complete objective C types through the newly added ASTContext
2495 // because it only supports TagDecl objects right now...
2496 if (class_interface_decl)
2497 {
2498 if (class_interface_decl->getDefinition())
2499 return true;
2500
2501 if (!allow_completion)
2502 return false;
2503
2504 if (class_interface_decl->hasExternalLexicalStorage())
2505 {
2506 if (ast)
2507 {
2508 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2509 if (external_ast_source)
2510 {
2511 external_ast_source->CompleteType (class_interface_decl);
2512 return !objc_class_type->isIncompleteType();
2513 }
2514 }
2515 }
2516 return false;
2517 }
2518 }
2519 }
2520 break;
2521
2522 case clang::Type::Typedef:
2523 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2524
2525 case clang::Type::Elaborated:
2526 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2527
2528 case clang::Type::Paren:
2529 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
2530
2531 default:
2532 break;
2533 }
2534
2535 return true;
2536}
2537
2538static clang::ObjCIvarDecl::AccessControl
2539ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2540{
2541 switch (access)
2542 {
2543 case eAccessNone: return clang::ObjCIvarDecl::None;
2544 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2545 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2546 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2547 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2548 }
2549 return clang::ObjCIvarDecl::None;
2550}
2551
2552
2553//----------------------------------------------------------------------
2554// Tests
2555//----------------------------------------------------------------------
2556
2557bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002558ClangASTContext::IsAggregateType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002559{
2560 clang::QualType qual_type (GetCanonicalQualType(type));
2561
2562 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2563 switch (type_class)
2564 {
2565 case clang::Type::IncompleteArray:
2566 case clang::Type::VariableArray:
2567 case clang::Type::ConstantArray:
2568 case clang::Type::ExtVector:
2569 case clang::Type::Vector:
2570 case clang::Type::Record:
2571 case clang::Type::ObjCObject:
2572 case clang::Type::ObjCInterface:
2573 return true;
2574 case clang::Type::Elaborated:
2575 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2576 case clang::Type::Typedef:
2577 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2578 case clang::Type::Paren:
2579 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2580 default:
2581 break;
2582 }
2583 // The clang type does have a value
2584 return false;
2585}
2586
2587bool
Enrico Granata7123e2b2015-11-07 02:06:57 +00002588ClangASTContext::IsAnonymousType (lldb::opaque_compiler_type_t type)
2589{
2590 clang::QualType qual_type (GetCanonicalQualType(type));
2591
2592 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2593 switch (type_class)
2594 {
2595 case clang::Type::Record:
2596 {
2597 if (const clang::RecordType *record_type = llvm::dyn_cast_or_null<clang::RecordType>(qual_type.getTypePtrOrNull()))
2598 {
2599 if (const clang::RecordDecl *record_decl = record_type->getDecl())
2600 {
2601 return record_decl->isAnonymousStructOrUnion();
2602 }
2603 }
2604 break;
2605 }
2606 case clang::Type::Elaborated:
2607 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2608 case clang::Type::Typedef:
2609 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2610 case clang::Type::Paren:
2611 return IsAnonymousType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2612 default:
2613 break;
2614 }
2615 // The clang type does have a value
2616 return false;
2617}
2618
2619bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002620ClangASTContext::IsArrayType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002621 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002622 uint64_t *size,
2623 bool *is_incomplete)
2624{
2625 clang::QualType qual_type (GetCanonicalQualType(type));
2626
2627 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2628 switch (type_class)
2629 {
2630 default:
2631 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002632
Greg Claytond8d4a572015-08-11 21:38:15 +00002633 case clang::Type::ConstantArray:
2634 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002635 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002636 if (size)
2637 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002638 if (is_incomplete)
2639 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002640 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002641
Greg Claytond8d4a572015-08-11 21:38:15 +00002642 case clang::Type::IncompleteArray:
2643 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002644 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002645 if (size)
2646 *size = 0;
2647 if (is_incomplete)
2648 *is_incomplete = true;
2649 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002650
Greg Claytond8d4a572015-08-11 21:38:15 +00002651 case clang::Type::VariableArray:
2652 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002653 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002654 if (size)
2655 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002656 if (is_incomplete)
2657 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002658 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002659
Greg Claytond8d4a572015-08-11 21:38:15 +00002660 case clang::Type::DependentSizedArray:
2661 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002662 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002663 if (size)
2664 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002665 if (is_incomplete)
2666 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002667 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002668
Greg Claytond8d4a572015-08-11 21:38:15 +00002669 case clang::Type::Typedef:
2670 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2671 element_type_ptr,
2672 size,
2673 is_incomplete);
2674 case clang::Type::Elaborated:
2675 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2676 element_type_ptr,
2677 size,
2678 is_incomplete);
2679 case clang::Type::Paren:
2680 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2681 element_type_ptr,
2682 size,
2683 is_incomplete);
2684 }
2685 if (element_type_ptr)
2686 element_type_ptr->Clear();
2687 if (size)
2688 *size = 0;
2689 if (is_incomplete)
2690 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002691 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002692}
2693
2694bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002695ClangASTContext::IsVectorType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002696 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002697 uint64_t *size)
2698{
2699 clang::QualType qual_type (GetCanonicalQualType(type));
2700
2701 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2702 switch (type_class)
2703 {
2704 case clang::Type::Vector:
2705 {
2706 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2707 if (vector_type)
2708 {
2709 if (size)
2710 *size = vector_type->getNumElements();
2711 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002712 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002713 }
2714 return true;
2715 }
2716 break;
2717 case clang::Type::ExtVector:
2718 {
2719 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2720 if (ext_vector_type)
2721 {
2722 if (size)
2723 *size = ext_vector_type->getNumElements();
2724 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002725 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002726 }
2727 return true;
2728 }
2729 default:
2730 break;
2731 }
2732 return false;
2733}
2734
2735bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002736ClangASTContext::IsRuntimeGeneratedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002737{
2738 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2739 if (!decl_ctx)
2740 return false;
2741
2742 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2743 return false;
2744
2745 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2746
2747 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2748 if (!ast_metadata)
2749 return false;
2750 return (ast_metadata->GetISAPtr() != 0);
2751}
2752
2753bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002754ClangASTContext::IsCharType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002755{
2756 return GetQualType(type).getUnqualifiedType()->isCharType();
2757}
2758
2759
2760bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002761ClangASTContext::IsCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002762{
2763 const bool allow_completion = false;
2764 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2765}
2766
2767bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002768ClangASTContext::IsConst(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002769{
2770 return GetQualType(type).isConstQualified();
2771}
2772
2773bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002774ClangASTContext::IsCStringType (lldb::opaque_compiler_type_t type, uint32_t &length)
Greg Claytond8d4a572015-08-11 21:38:15 +00002775{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002776 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002777 length = 0;
2778 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2779
2780 if (!pointee_or_element_clang_type.IsValid())
2781 return false;
2782
2783 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2784 {
2785 if (pointee_or_element_clang_type.IsCharType())
2786 {
2787 if (type_flags.Test (eTypeIsArray))
2788 {
2789 // We know the size of the array and it could be a C string
2790 // since it is an array of characters
2791 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2792 }
2793 return true;
2794
2795 }
2796 }
2797 return false;
2798}
2799
2800bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002801ClangASTContext::IsFunctionType (lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002802{
2803 if (type)
2804 {
2805 clang::QualType qual_type (GetCanonicalQualType(type));
2806
2807 if (qual_type->isFunctionType())
2808 {
2809 if (is_variadic_ptr)
2810 {
2811 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2812 if (function_proto_type)
2813 *is_variadic_ptr = function_proto_type->isVariadic();
2814 else
2815 *is_variadic_ptr = false;
2816 }
2817 return true;
2818 }
2819
2820 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2821 switch (type_class)
2822 {
2823 default:
2824 break;
2825 case clang::Type::Typedef:
2826 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2827 case clang::Type::Elaborated:
2828 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2829 case clang::Type::Paren:
2830 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2831 case clang::Type::LValueReference:
2832 case clang::Type::RValueReference:
2833 {
2834 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2835 if (reference_type)
2836 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2837 }
2838 break;
2839 }
2840 }
2841 return false;
2842}
2843
2844// Used to detect "Homogeneous Floating-point Aggregates"
2845uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002846ClangASTContext::IsHomogeneousAggregate (lldb::opaque_compiler_type_t type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002847{
2848 if (!type)
2849 return 0;
2850
2851 clang::QualType qual_type(GetCanonicalQualType(type));
2852 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2853 switch (type_class)
2854 {
2855 case clang::Type::Record:
2856 if (GetCompleteType (type))
2857 {
2858 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2859 if (cxx_record_decl)
2860 {
2861 if (cxx_record_decl->getNumBases() ||
2862 cxx_record_decl->isDynamicClass())
2863 return 0;
2864 }
2865 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2866 if (record_type)
2867 {
2868 const clang::RecordDecl *record_decl = record_type->getDecl();
2869 if (record_decl)
2870 {
2871 // We are looking for a structure that contains only floating point types
2872 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2873 uint32_t num_fields = 0;
2874 bool is_hva = false;
2875 bool is_hfa = false;
2876 clang::QualType base_qual_type;
2877 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2878 {
2879 clang::QualType field_qual_type = field_pos->getType();
2880 if (field_qual_type->isFloatingType())
2881 {
2882 if (field_qual_type->isComplexType())
2883 return 0;
2884 else
2885 {
2886 if (num_fields == 0)
2887 base_qual_type = field_qual_type;
2888 else
2889 {
2890 if (is_hva)
2891 return 0;
2892 is_hfa = true;
2893 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2894 return 0;
2895 }
2896 }
2897 }
2898 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2899 {
2900 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2901 if (array && array->getNumElements() <= 4)
2902 {
2903 if (num_fields == 0)
2904 base_qual_type = array->getElementType();
2905 else
2906 {
2907 if (is_hfa)
2908 return 0;
2909 is_hva = true;
2910 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2911 return 0;
2912 }
2913 }
2914 else
2915 return 0;
2916 }
2917 else
2918 return 0;
2919 ++num_fields;
2920 }
2921 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002922 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002923 return num_fields;
2924 }
2925 }
2926 }
2927 break;
2928
2929 case clang::Type::Typedef:
2930 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2931
2932 case clang::Type::Elaborated:
2933 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2934 default:
2935 break;
2936 }
2937 return 0;
2938}
2939
2940size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002941ClangASTContext::GetNumberOfFunctionArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002942{
2943 if (type)
2944 {
2945 clang::QualType qual_type (GetCanonicalQualType(type));
2946 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2947 if (func)
2948 return func->getNumParams();
2949 }
2950 return 0;
2951}
2952
Greg Claytona1e5dc82015-08-11 22:53:00 +00002953CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002954ClangASTContext::GetFunctionArgumentAtIndex (lldb::opaque_compiler_type_t type, const size_t index)
Greg Claytond8d4a572015-08-11 21:38:15 +00002955{
2956 if (type)
2957 {
2958 clang::QualType qual_type (GetCanonicalQualType(type));
2959 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2960 if (func)
2961 {
2962 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00002963 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00002964 }
2965 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002966 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00002967}
2968
2969bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002970ClangASTContext::IsFunctionPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002971{
2972 if (type)
2973 {
2974 clang::QualType qual_type (GetCanonicalQualType(type));
2975
2976 if (qual_type->isFunctionPointerType())
2977 return true;
2978
2979 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2980 switch (type_class)
2981 {
2982 default:
2983 break;
2984 case clang::Type::Typedef:
2985 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2986 case clang::Type::Elaborated:
2987 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2988 case clang::Type::Paren:
2989 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2990
2991 case clang::Type::LValueReference:
2992 case clang::Type::RValueReference:
2993 {
2994 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2995 if (reference_type)
2996 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
2997 }
2998 break;
2999 }
3000 }
3001 return false;
3002
3003}
3004
3005bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003006ClangASTContext::IsIntegerType (lldb::opaque_compiler_type_t type, bool &is_signed)
Greg Claytond8d4a572015-08-11 21:38:15 +00003007{
3008 if (!type)
3009 return false;
3010
3011 clang::QualType qual_type (GetCanonicalQualType(type));
3012 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3013
3014 if (builtin_type)
3015 {
3016 if (builtin_type->isInteger())
3017 {
3018 is_signed = builtin_type->isSignedInteger();
3019 return true;
3020 }
3021 }
3022
3023 return false;
3024}
3025
3026bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003027ClangASTContext::IsPointerType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003028{
3029 if (type)
3030 {
3031 clang::QualType qual_type (GetCanonicalQualType(type));
3032 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3033 switch (type_class)
3034 {
3035 case clang::Type::Builtin:
3036 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3037 {
3038 default:
3039 break;
3040 case clang::BuiltinType::ObjCId:
3041 case clang::BuiltinType::ObjCClass:
3042 return true;
3043 }
3044 return false;
3045 case clang::Type::ObjCObjectPointer:
3046 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003047 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003048 return true;
3049 case clang::Type::BlockPointer:
3050 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003051 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003052 return true;
3053 case clang::Type::Pointer:
3054 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003055 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003056 return true;
3057 case clang::Type::MemberPointer:
3058 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003059 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003060 return true;
3061 case clang::Type::Typedef:
3062 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3063 case clang::Type::Elaborated:
3064 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3065 case clang::Type::Paren:
3066 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3067 default:
3068 break;
3069 }
3070 }
3071 if (pointee_type)
3072 pointee_type->Clear();
3073 return false;
3074}
3075
3076
3077bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003078ClangASTContext::IsPointerOrReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003079{
3080 if (type)
3081 {
3082 clang::QualType qual_type (GetCanonicalQualType(type));
3083 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3084 switch (type_class)
3085 {
3086 case clang::Type::Builtin:
3087 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3088 {
3089 default:
3090 break;
3091 case clang::BuiltinType::ObjCId:
3092 case clang::BuiltinType::ObjCClass:
3093 return true;
3094 }
3095 return false;
3096 case clang::Type::ObjCObjectPointer:
3097 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003098 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003099 return true;
3100 case clang::Type::BlockPointer:
3101 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003102 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003103 return true;
3104 case clang::Type::Pointer:
3105 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003106 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003107 return true;
3108 case clang::Type::MemberPointer:
3109 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003110 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003111 return true;
3112 case clang::Type::LValueReference:
3113 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003114 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003115 return true;
3116 case clang::Type::RValueReference:
3117 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003118 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003119 return true;
3120 case clang::Type::Typedef:
3121 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3122 case clang::Type::Elaborated:
3123 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3124 case clang::Type::Paren:
3125 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3126 default:
3127 break;
3128 }
3129 }
3130 if (pointee_type)
3131 pointee_type->Clear();
3132 return false;
3133}
3134
3135
3136bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003137ClangASTContext::IsReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003138{
3139 if (type)
3140 {
3141 clang::QualType qual_type (GetCanonicalQualType(type));
3142 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3143
3144 switch (type_class)
3145 {
3146 case clang::Type::LValueReference:
3147 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003148 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003149 if (is_rvalue)
3150 *is_rvalue = false;
3151 return true;
3152 case clang::Type::RValueReference:
3153 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003154 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003155 if (is_rvalue)
3156 *is_rvalue = true;
3157 return true;
3158 case clang::Type::Typedef:
3159 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3160 case clang::Type::Elaborated:
3161 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3162 case clang::Type::Paren:
3163 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3164
3165 default:
3166 break;
3167 }
3168 }
3169 if (pointee_type)
3170 pointee_type->Clear();
3171 return false;
3172}
3173
3174bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003175ClangASTContext::IsFloatingPointType (lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Greg Claytond8d4a572015-08-11 21:38:15 +00003176{
3177 if (type)
3178 {
3179 clang::QualType qual_type (GetCanonicalQualType(type));
3180
3181 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3182 {
3183 clang::BuiltinType::Kind kind = BT->getKind();
3184 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3185 {
3186 count = 1;
3187 is_complex = false;
3188 return true;
3189 }
3190 }
3191 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3192 {
3193 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3194 {
3195 count = 2;
3196 is_complex = true;
3197 return true;
3198 }
3199 }
3200 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3201 {
3202 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3203 {
3204 count = VT->getNumElements();
3205 is_complex = false;
3206 return true;
3207 }
3208 }
3209 }
3210 count = 0;
3211 is_complex = false;
3212 return false;
3213}
3214
3215
3216bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003217ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003218{
3219 if (!type)
3220 return false;
3221
3222 clang::QualType qual_type(GetQualType(type));
3223 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3224 if (tag_type)
3225 {
3226 clang::TagDecl *tag_decl = tag_type->getDecl();
3227 if (tag_decl)
3228 return tag_decl->isCompleteDefinition();
3229 return false;
3230 }
3231 else
3232 {
3233 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3234 if (objc_class_type)
3235 {
3236 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3237 if (class_interface_decl)
3238 return class_interface_decl->getDefinition() != nullptr;
3239 return false;
3240 }
3241 }
3242 return true;
3243}
3244
3245bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003246ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003247{
3248 if (type)
3249 {
3250 clang::QualType qual_type (GetCanonicalQualType(type));
3251
3252 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3253
3254 if (obj_pointer_type)
3255 return obj_pointer_type->isObjCClassType();
3256 }
3257 return false;
3258}
3259
3260bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003261ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003262{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003263 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003264 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3265 return false;
3266}
3267
3268bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003269ClangASTContext::IsPolymorphicClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003270{
3271 if (type)
3272 {
3273 clang::QualType qual_type(GetCanonicalQualType(type));
3274 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3275 switch (type_class)
3276 {
3277 case clang::Type::Record:
3278 if (GetCompleteType(type))
3279 {
3280 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3281 const clang::RecordDecl *record_decl = record_type->getDecl();
3282 if (record_decl)
3283 {
3284 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3285 if (cxx_record_decl)
3286 return cxx_record_decl->isPolymorphic();
3287 }
3288 }
3289 break;
3290
3291 default:
3292 break;
3293 }
3294 }
3295 return false;
3296}
3297
3298bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003299ClangASTContext::IsPossibleDynamicType (lldb::opaque_compiler_type_t type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003300 bool check_cplusplus,
3301 bool check_objc)
3302{
3303 clang::QualType pointee_qual_type;
3304 if (type)
3305 {
3306 clang::QualType qual_type (GetCanonicalQualType(type));
3307 bool success = false;
3308 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3309 switch (type_class)
3310 {
3311 case clang::Type::Builtin:
3312 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3313 {
3314 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003315 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003316 return true;
3317 }
3318 break;
3319
3320 case clang::Type::ObjCObjectPointer:
3321 if (check_objc)
3322 {
3323 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003324 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003325 return true;
3326 }
3327 break;
3328
3329 case clang::Type::Pointer:
3330 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3331 success = true;
3332 break;
3333
3334 case clang::Type::LValueReference:
3335 case clang::Type::RValueReference:
3336 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3337 success = true;
3338 break;
3339
3340 case clang::Type::Typedef:
3341 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3342 dynamic_pointee_type,
3343 check_cplusplus,
3344 check_objc);
3345
3346 case clang::Type::Elaborated:
3347 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3348 dynamic_pointee_type,
3349 check_cplusplus,
3350 check_objc);
3351
3352 case clang::Type::Paren:
3353 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3354 dynamic_pointee_type,
3355 check_cplusplus,
3356 check_objc);
3357 default:
3358 break;
3359 }
3360
3361 if (success)
3362 {
3363 // Check to make sure what we are pointing too is a possible dynamic C++ type
3364 // We currently accept any "void *" (in case we have a class that has been
3365 // watered down to an opaque pointer) and virtual C++ classes.
3366 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3367 switch (pointee_type_class)
3368 {
3369 case clang::Type::Builtin:
3370 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3371 {
3372 case clang::BuiltinType::UnknownAny:
3373 case clang::BuiltinType::Void:
3374 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003375 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003376 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003377 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003378 break;
3379 }
3380 break;
3381
3382 case clang::Type::Record:
3383 if (check_cplusplus)
3384 {
3385 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3386 if (cxx_record_decl)
3387 {
3388 bool is_complete = cxx_record_decl->isCompleteDefinition();
3389
3390 if (is_complete)
3391 success = cxx_record_decl->isDynamicClass();
3392 else
3393 {
3394 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3395 if (metadata)
3396 success = metadata->GetIsDynamicCXXType();
3397 else
3398 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003399 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003400 if (is_complete)
3401 success = cxx_record_decl->isDynamicClass();
3402 else
3403 success = false;
3404 }
3405 }
3406
3407 if (success)
3408 {
3409 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003410 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003411 return true;
3412 }
3413 }
3414 }
3415 break;
3416
3417 case clang::Type::ObjCObject:
3418 case clang::Type::ObjCInterface:
3419 if (check_objc)
3420 {
3421 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003422 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003423 return true;
3424 }
3425 break;
3426
3427 default:
3428 break;
3429 }
3430 }
3431 }
3432 if (dynamic_pointee_type)
3433 dynamic_pointee_type->Clear();
3434 return false;
3435}
3436
3437
3438bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003439ClangASTContext::IsScalarType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003440{
3441 if (!type)
3442 return false;
3443
3444 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3445}
3446
3447bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003448ClangASTContext::IsTypedefType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003449{
3450 if (!type)
3451 return false;
3452 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3453}
3454
3455bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003456ClangASTContext::IsVoidType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003457{
3458 if (!type)
3459 return false;
3460 return GetCanonicalQualType(type)->isVoidType();
3461}
3462
3463bool
Greg Clayton56939cb2015-09-17 22:23:34 +00003464ClangASTContext::SupportsLanguage (lldb::LanguageType language)
3465{
3466 return ClangASTContextSupportsLanguage(language);
3467}
3468
3469bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003470ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003471{
3472 if (type)
3473 {
3474 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003475 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003476 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003477 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3478 if (cxx_record_decl)
3479 {
3480 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3481 return true;
3482 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003483 }
3484 }
3485 class_name.clear();
3486 return false;
3487}
3488
3489
3490bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003491ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003492{
3493 if (!type)
3494 return false;
3495
3496 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003497 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003498 return true;
3499 return false;
3500}
3501
3502bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003503ClangASTContext::IsBeingDefined (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003504{
3505 if (!type)
3506 return false;
3507 clang::QualType qual_type (GetCanonicalQualType(type));
3508 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3509 if (tag_type)
3510 return tag_type->isBeingDefined();
3511 return false;
3512}
3513
3514bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003515ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003516{
3517 if (!type)
3518 return false;
3519
3520 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003521
3522 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003523 {
3524 if (class_type_ptr)
3525 {
3526 if (!qual_type->isObjCClassType() &&
3527 !qual_type->isObjCIdType())
3528 {
3529 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3530 if (obj_pointer_type == nullptr)
3531 class_type_ptr->Clear();
3532 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003533 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003534 }
3535 }
3536 return true;
3537 }
3538 if (class_type_ptr)
3539 class_type_ptr->Clear();
3540 return false;
3541}
3542
3543bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003544ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003545{
3546 if (!type)
3547 return false;
3548
3549 clang::QualType qual_type (GetCanonicalQualType(type));
3550
3551 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3552 if (object_type)
3553 {
3554 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3555 if (interface)
3556 {
3557 class_name = interface->getNameAsString();
3558 return true;
3559 }
3560 }
3561 return false;
3562}
3563
3564
3565//----------------------------------------------------------------------
3566// Type Completion
3567//----------------------------------------------------------------------
3568
3569bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003570ClangASTContext::GetCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003571{
3572 if (!type)
3573 return false;
3574 const bool allow_completion = true;
3575 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3576}
3577
3578ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003579ClangASTContext::GetTypeName (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003580{
3581 std::string type_name;
3582 if (type)
3583 {
3584 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3585 clang::QualType qual_type(GetQualType(type));
3586 printing_policy.SuppressTagKeyword = true;
3587 printing_policy.LangOpts.WChar = true;
3588 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3589 if (typedef_type)
3590 {
3591 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3592 type_name = typedef_decl->getQualifiedNameAsString();
3593 }
3594 else
3595 {
3596 type_name = qual_type.getAsString(printing_policy);
3597 }
3598 }
3599 return ConstString(type_name);
3600}
3601
3602uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003603ClangASTContext::GetTypeInfo (lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003604{
3605 if (!type)
3606 return 0;
3607
3608 if (pointee_or_element_clang_type)
3609 pointee_or_element_clang_type->Clear();
3610
3611 clang::QualType qual_type (GetQualType(type));
3612
3613 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3614 switch (type_class)
3615 {
3616 case clang::Type::Builtin:
3617 {
3618 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3619
3620 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3621 switch (builtin_type->getKind())
3622 {
3623 case clang::BuiltinType::ObjCId:
3624 case clang::BuiltinType::ObjCClass:
3625 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003626 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003627 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3628 break;
3629
3630 case clang::BuiltinType::ObjCSel:
3631 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003632 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003633 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3634 break;
3635
3636 case clang::BuiltinType::Bool:
3637 case clang::BuiltinType::Char_U:
3638 case clang::BuiltinType::UChar:
3639 case clang::BuiltinType::WChar_U:
3640 case clang::BuiltinType::Char16:
3641 case clang::BuiltinType::Char32:
3642 case clang::BuiltinType::UShort:
3643 case clang::BuiltinType::UInt:
3644 case clang::BuiltinType::ULong:
3645 case clang::BuiltinType::ULongLong:
3646 case clang::BuiltinType::UInt128:
3647 case clang::BuiltinType::Char_S:
3648 case clang::BuiltinType::SChar:
3649 case clang::BuiltinType::WChar_S:
3650 case clang::BuiltinType::Short:
3651 case clang::BuiltinType::Int:
3652 case clang::BuiltinType::Long:
3653 case clang::BuiltinType::LongLong:
3654 case clang::BuiltinType::Int128:
3655 case clang::BuiltinType::Float:
3656 case clang::BuiltinType::Double:
3657 case clang::BuiltinType::LongDouble:
3658 builtin_type_flags |= eTypeIsScalar;
3659 if (builtin_type->isInteger())
3660 {
3661 builtin_type_flags |= eTypeIsInteger;
3662 if (builtin_type->isSignedInteger())
3663 builtin_type_flags |= eTypeIsSigned;
3664 }
3665 else if (builtin_type->isFloatingPoint())
3666 builtin_type_flags |= eTypeIsFloat;
3667 break;
3668 default:
3669 break;
3670 }
3671 return builtin_type_flags;
3672 }
3673
3674 case clang::Type::BlockPointer:
3675 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003676 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003677 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3678
3679 case clang::Type::Complex:
3680 {
3681 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3682 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3683 if (complex_type)
3684 {
3685 clang::QualType complex_element_type (complex_type->getElementType());
3686 if (complex_element_type->isIntegerType())
3687 complex_type_flags |= eTypeIsFloat;
3688 else if (complex_element_type->isFloatingType())
3689 complex_type_flags |= eTypeIsInteger;
3690 }
3691 return complex_type_flags;
3692 }
3693 break;
3694
3695 case clang::Type::ConstantArray:
3696 case clang::Type::DependentSizedArray:
3697 case clang::Type::IncompleteArray:
3698 case clang::Type::VariableArray:
3699 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003700 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003701 return eTypeHasChildren | eTypeIsArray;
3702
3703 case clang::Type::DependentName: return 0;
3704 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3705 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3706 case clang::Type::Decltype: return 0;
3707
3708 case clang::Type::Enum:
3709 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003710 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003711 return eTypeIsEnumeration | eTypeHasValue;
3712
3713 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003714 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003715 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003716 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003717
3718 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3719 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3720 case clang::Type::InjectedClassName: return 0;
3721
3722 case clang::Type::LValueReference:
3723 case clang::Type::RValueReference:
3724 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003725 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003726 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3727
3728 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3729
3730 case clang::Type::ObjCObjectPointer:
3731 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003732 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003733 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3734
3735 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3736 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3737
3738 case clang::Type::Pointer:
3739 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003740 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003741 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3742
3743 case clang::Type::Record:
3744 if (qual_type->getAsCXXRecordDecl())
3745 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3746 else
3747 return eTypeHasChildren | eTypeIsStructUnion;
3748 break;
3749 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3750 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3751 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3752
3753 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003754 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 +00003755 case clang::Type::TypeOfExpr: return 0;
3756 case clang::Type::TypeOf: return 0;
3757 case clang::Type::UnresolvedUsing: return 0;
3758
3759 case clang::Type::ExtVector:
3760 case clang::Type::Vector:
3761 {
3762 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3763 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3764 if (vector_type)
3765 {
3766 if (vector_type->isIntegerType())
3767 vector_type_flags |= eTypeIsFloat;
3768 else if (vector_type->isFloatingType())
3769 vector_type_flags |= eTypeIsInteger;
3770 }
3771 return vector_type_flags;
3772 }
3773 default: return 0;
3774 }
3775 return 0;
3776}
3777
3778
3779
3780lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003781ClangASTContext::GetMinimumLanguage (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003782{
3783 if (!type)
3784 return lldb::eLanguageTypeC;
3785
3786 // If the type is a reference, then resolve it to what it refers to first:
3787 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3788 if (qual_type->isAnyPointerType())
3789 {
3790 if (qual_type->isObjCObjectPointerType())
3791 return lldb::eLanguageTypeObjC;
3792
3793 clang::QualType pointee_type (qual_type->getPointeeType());
3794 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3795 return lldb::eLanguageTypeC_plus_plus;
3796 if (pointee_type->isObjCObjectOrInterfaceType())
3797 return lldb::eLanguageTypeObjC;
3798 if (pointee_type->isObjCClassType())
3799 return lldb::eLanguageTypeObjC;
3800 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3801 return lldb::eLanguageTypeObjC;
3802 }
3803 else
3804 {
3805 if (qual_type->isObjCObjectOrInterfaceType())
3806 return lldb::eLanguageTypeObjC;
3807 if (qual_type->getAsCXXRecordDecl())
3808 return lldb::eLanguageTypeC_plus_plus;
3809 switch (qual_type->getTypeClass())
3810 {
3811 default:
3812 break;
3813 case clang::Type::Builtin:
3814 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3815 {
3816 default:
3817 case clang::BuiltinType::Void:
3818 case clang::BuiltinType::Bool:
3819 case clang::BuiltinType::Char_U:
3820 case clang::BuiltinType::UChar:
3821 case clang::BuiltinType::WChar_U:
3822 case clang::BuiltinType::Char16:
3823 case clang::BuiltinType::Char32:
3824 case clang::BuiltinType::UShort:
3825 case clang::BuiltinType::UInt:
3826 case clang::BuiltinType::ULong:
3827 case clang::BuiltinType::ULongLong:
3828 case clang::BuiltinType::UInt128:
3829 case clang::BuiltinType::Char_S:
3830 case clang::BuiltinType::SChar:
3831 case clang::BuiltinType::WChar_S:
3832 case clang::BuiltinType::Short:
3833 case clang::BuiltinType::Int:
3834 case clang::BuiltinType::Long:
3835 case clang::BuiltinType::LongLong:
3836 case clang::BuiltinType::Int128:
3837 case clang::BuiltinType::Float:
3838 case clang::BuiltinType::Double:
3839 case clang::BuiltinType::LongDouble:
3840 break;
3841
3842 case clang::BuiltinType::NullPtr:
3843 return eLanguageTypeC_plus_plus;
3844
3845 case clang::BuiltinType::ObjCId:
3846 case clang::BuiltinType::ObjCClass:
3847 case clang::BuiltinType::ObjCSel:
3848 return eLanguageTypeObjC;
3849
3850 case clang::BuiltinType::Dependent:
3851 case clang::BuiltinType::Overload:
3852 case clang::BuiltinType::BoundMember:
3853 case clang::BuiltinType::UnknownAny:
3854 break;
3855 }
3856 break;
3857 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003858 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003859 }
3860 }
3861 return lldb::eLanguageTypeC;
3862}
3863
3864lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003865ClangASTContext::GetTypeClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003866{
3867 if (!type)
3868 return lldb::eTypeClassInvalid;
3869
3870 clang::QualType qual_type(GetQualType(type));
3871
3872 switch (qual_type->getTypeClass())
3873 {
3874 case clang::Type::UnaryTransform: break;
3875 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3876 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3877 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3878 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3879 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3880 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3881 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3882 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3883 case clang::Type::Vector: return lldb::eTypeClassVector;
3884 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3885 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3886 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3887 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3888 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3889 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3890 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3891 case clang::Type::Complex:
3892 if (qual_type->isComplexType())
3893 return lldb::eTypeClassComplexFloat;
3894 else
3895 return lldb::eTypeClassComplexInteger;
3896 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3897 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3898 case clang::Type::Record:
3899 {
3900 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3901 const clang::RecordDecl *record_decl = record_type->getDecl();
3902 if (record_decl->isUnion())
3903 return lldb::eTypeClassUnion;
3904 else if (record_decl->isStruct())
3905 return lldb::eTypeClassStruct;
3906 else
3907 return lldb::eTypeClassClass;
3908 }
3909 break;
3910 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3911 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3912 case clang::Type::UnresolvedUsing: break;
3913 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003914 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003915 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003916 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003917
3918 case clang::Type::Attributed: break;
3919 case clang::Type::TemplateTypeParm: break;
3920 case clang::Type::SubstTemplateTypeParm: break;
3921 case clang::Type::SubstTemplateTypeParmPack:break;
3922 case clang::Type::Auto: break;
3923 case clang::Type::InjectedClassName: break;
3924 case clang::Type::DependentName: break;
3925 case clang::Type::DependentTemplateSpecialization: break;
3926 case clang::Type::PackExpansion: break;
3927
3928 case clang::Type::TypeOfExpr: break;
3929 case clang::Type::TypeOf: break;
3930 case clang::Type::Decltype: break;
3931 case clang::Type::TemplateSpecialization: break;
3932 case clang::Type::Atomic: break;
3933
3934 // pointer type decayed from an array or function type.
3935 case clang::Type::Decayed: break;
3936 case clang::Type::Adjusted: break;
3937 }
3938 // We don't know hot to display this type...
3939 return lldb::eTypeClassOther;
3940
3941}
3942
3943unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003944ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003945{
3946 if (type)
3947 return GetQualType(type).getQualifiers().getCVRQualifiers();
3948 return 0;
3949}
3950
3951//----------------------------------------------------------------------
3952// Creating related types
3953//----------------------------------------------------------------------
3954
Greg Claytona1e5dc82015-08-11 22:53:00 +00003955CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003956ClangASTContext::GetArrayElementType (lldb::opaque_compiler_type_t type, uint64_t *stride)
Greg Claytond8d4a572015-08-11 21:38:15 +00003957{
3958 if (type)
3959 {
3960 clang::QualType qual_type(GetCanonicalQualType(type));
3961
3962 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
3963
3964 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003965 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003966
Greg Claytona1e5dc82015-08-11 22:53:00 +00003967 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00003968
3969 // TODO: the real stride will be >= this value.. find the real one!
3970 if (stride)
3971 *stride = element_type.GetByteSize(nullptr);
3972
3973 return element_type;
3974
3975 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003976 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003977}
3978
Greg Claytona1e5dc82015-08-11 22:53:00 +00003979CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003980ClangASTContext::GetCanonicalType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003981{
3982 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003983 return CompilerType (getASTContext(), GetCanonicalQualType(type));
3984 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003985}
3986
3987static clang::QualType
3988GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
3989{
3990 if (qual_type->isPointerType())
3991 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
3992 else
3993 qual_type = qual_type.getUnqualifiedType();
3994 qual_type.removeLocalConst();
3995 qual_type.removeLocalRestrict();
3996 qual_type.removeLocalVolatile();
3997 return qual_type;
3998}
3999
Greg Claytona1e5dc82015-08-11 22:53:00 +00004000CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004001ClangASTContext::GetFullyUnqualifiedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004002{
4003 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004004 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4005 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004006}
4007
4008
4009int
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004010ClangASTContext::GetFunctionArgumentCount (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004011{
4012 if (type)
4013 {
4014 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4015 if (func)
4016 return func->getNumParams();
4017 }
4018 return -1;
4019}
4020
Greg Claytona1e5dc82015-08-11 22:53:00 +00004021CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004022ClangASTContext::GetFunctionArgumentTypeAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004023{
4024 if (type)
4025 {
4026 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4027 if (func)
4028 {
4029 const uint32_t num_args = func->getNumParams();
4030 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004031 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00004032 }
4033 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004034 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004035}
4036
Greg Claytona1e5dc82015-08-11 22:53:00 +00004037CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004038ClangASTContext::GetFunctionReturnType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004039{
4040 if (type)
4041 {
4042 clang::QualType qual_type(GetCanonicalQualType(type));
4043 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4044 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004045 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004046 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004047 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004048}
4049
4050size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004051ClangASTContext::GetNumMemberFunctions (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004052{
4053 size_t num_functions = 0;
4054 if (type)
4055 {
4056 clang::QualType qual_type(GetCanonicalQualType(type));
4057 switch (qual_type->getTypeClass()) {
4058 case clang::Type::Record:
4059 if (GetCompleteQualType (getASTContext(), qual_type))
4060 {
4061 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4062 const clang::RecordDecl *record_decl = record_type->getDecl();
4063 assert(record_decl);
4064 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4065 if (cxx_record_decl)
4066 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4067 }
4068 break;
4069
4070 case clang::Type::ObjCObjectPointer:
4071 if (GetCompleteType(type))
4072 {
4073 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4074 if (objc_class_type)
4075 {
4076 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4077 if (class_interface_decl)
4078 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4079 }
4080 }
4081 break;
4082
4083 case clang::Type::ObjCObject:
4084 case clang::Type::ObjCInterface:
4085 if (GetCompleteType(type))
4086 {
4087 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4088 if (objc_class_type)
4089 {
4090 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4091 if (class_interface_decl)
4092 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4093 }
4094 }
4095 break;
4096
4097
4098 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004099 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004100
4101 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004102 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004103
4104 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004105 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004106
4107 default:
4108 break;
4109 }
4110 }
4111 return num_functions;
4112}
4113
4114TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004115ClangASTContext::GetMemberFunctionAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004116{
4117 std::string name("");
4118 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytona1e5dc82015-08-11 22:53:00 +00004119 CompilerType clang_type{};
Greg Claytond8d4a572015-08-11 21:38:15 +00004120 clang::ObjCMethodDecl *method_decl(nullptr);
4121 if (type)
4122 {
4123 clang::QualType qual_type(GetCanonicalQualType(type));
4124 switch (qual_type->getTypeClass()) {
4125 case clang::Type::Record:
4126 if (GetCompleteQualType (getASTContext(), qual_type))
4127 {
4128 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4129 const clang::RecordDecl *record_decl = record_type->getDecl();
4130 assert(record_decl);
4131 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4132 if (cxx_record_decl)
4133 {
4134 auto method_iter = cxx_record_decl->method_begin();
4135 auto method_end = cxx_record_decl->method_end();
4136 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4137 {
4138 std::advance(method_iter, idx);
4139 auto method_decl = method_iter->getCanonicalDecl();
4140 if (method_decl)
4141 {
4142 if (!method_decl->getName().empty())
4143 name.assign(method_decl->getName().data());
4144 else
4145 name.clear();
4146 if (method_decl->isStatic())
4147 kind = lldb::eMemberFunctionKindStaticMethod;
4148 else if (llvm::isa<clang::CXXConstructorDecl>(method_decl))
4149 kind = lldb::eMemberFunctionKindConstructor;
4150 else if (llvm::isa<clang::CXXDestructorDecl>(method_decl))
4151 kind = lldb::eMemberFunctionKindDestructor;
4152 else
4153 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004154 clang_type = CompilerType(getASTContext(),method_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004155 }
4156 }
4157 }
4158 }
4159 break;
4160
4161 case clang::Type::ObjCObjectPointer:
4162 if (GetCompleteType(type))
4163 {
4164 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4165 if (objc_class_type)
4166 {
4167 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4168 if (class_interface_decl)
4169 {
4170 auto method_iter = class_interface_decl->meth_begin();
4171 auto method_end = class_interface_decl->meth_end();
4172 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4173 {
4174 std::advance(method_iter, idx);
4175 method_decl = method_iter->getCanonicalDecl();
4176 if (method_decl)
4177 {
4178 name = method_decl->getSelector().getAsString();
4179 if (method_decl->isClassMethod())
4180 kind = lldb::eMemberFunctionKindStaticMethod;
4181 else
4182 kind = lldb::eMemberFunctionKindInstanceMethod;
4183 }
4184 }
4185 }
4186 }
4187 }
4188 break;
4189
4190 case clang::Type::ObjCObject:
4191 case clang::Type::ObjCInterface:
4192 if (GetCompleteType(type))
4193 {
4194 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4195 if (objc_class_type)
4196 {
4197 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4198 if (class_interface_decl)
4199 {
4200 auto method_iter = class_interface_decl->meth_begin();
4201 auto method_end = class_interface_decl->meth_end();
4202 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4203 {
4204 std::advance(method_iter, idx);
4205 method_decl = method_iter->getCanonicalDecl();
4206 if (method_decl)
4207 {
4208 name = method_decl->getSelector().getAsString();
4209 if (method_decl->isClassMethod())
4210 kind = lldb::eMemberFunctionKindStaticMethod;
4211 else
4212 kind = lldb::eMemberFunctionKindInstanceMethod;
4213 }
4214 }
4215 }
4216 }
4217 }
4218 break;
4219
4220 case clang::Type::Typedef:
4221 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4222
4223 case clang::Type::Elaborated:
4224 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4225
4226 case clang::Type::Paren:
4227 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4228
4229 default:
4230 break;
4231 }
4232 }
4233
4234 if (kind == eMemberFunctionKindUnknown)
4235 return TypeMemberFunctionImpl();
4236 if (method_decl)
4237 return TypeMemberFunctionImpl(method_decl, name, kind);
4238 if (type)
4239 return TypeMemberFunctionImpl(clang_type, name, kind);
4240
4241 return TypeMemberFunctionImpl();
4242}
4243
Greg Claytona1e5dc82015-08-11 22:53:00 +00004244CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004245ClangASTContext::GetNonReferenceType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004246{
4247 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004248 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4249 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004250}
4251
Greg Claytona1e5dc82015-08-11 22:53:00 +00004252CompilerType
4253ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004254 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004255 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004256{
4257 if (type && typedef_name && typedef_name[0])
4258 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004259 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004260 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004261 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004262 clang::ASTContext* clang_ast = ast->getASTContext();
4263 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004264
4265 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004266 if (decl_ctx == nullptr)
4267 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004268
Greg Claytond8d4a572015-08-11 21:38:15 +00004269 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4270 decl_ctx,
4271 clang::SourceLocation(),
4272 clang::SourceLocation(),
4273 &clang_ast->Idents.get(typedef_name),
4274 clang_ast->getTrivialTypeSourceInfo(qual_type));
4275
4276 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4277
4278 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004279 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004280 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004281 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004282
4283}
4284
Greg Claytona1e5dc82015-08-11 22:53:00 +00004285CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004286ClangASTContext::GetPointeeType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004287{
4288 if (type)
4289 {
4290 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004291 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004292 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004293 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004294}
4295
Greg Claytona1e5dc82015-08-11 22:53:00 +00004296CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004297ClangASTContext::GetPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004298{
4299 if (type)
4300 {
4301 clang::QualType qual_type (GetQualType(type));
4302
4303 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4304 switch (type_class)
4305 {
4306 case clang::Type::ObjCObject:
4307 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004308 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004309
4310 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004311 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004312 }
4313 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004314 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004315}
4316
Greg Clayton56939cb2015-09-17 22:23:34 +00004317
4318CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004319ClangASTContext::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004320{
4321 if (type)
4322 return CompilerType(this, getASTContext()->getLValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4323 else
4324 return CompilerType();
4325}
4326
4327CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004328ClangASTContext::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004329{
4330 if (type)
4331 return CompilerType(this, getASTContext()->getRValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4332 else
4333 return CompilerType();
4334}
4335
4336CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004337ClangASTContext::AddConstModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004338{
4339 if (type)
4340 {
4341 clang::QualType result(GetQualType(type));
4342 result.addConst();
4343 return CompilerType (this, result.getAsOpaquePtr());
4344 }
4345 return CompilerType();
4346}
4347
4348CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004349ClangASTContext::AddVolatileModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004350{
4351 if (type)
4352 {
4353 clang::QualType result(GetQualType(type));
4354 result.addVolatile();
4355 return CompilerType (this, result.getAsOpaquePtr());
4356 }
4357 return CompilerType();
4358
4359}
4360
4361CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004362ClangASTContext::AddRestrictModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004363{
4364 if (type)
4365 {
4366 clang::QualType result(GetQualType(type));
4367 result.addRestrict();
4368 return CompilerType (this, result.getAsOpaquePtr());
4369 }
4370 return CompilerType();
4371
4372}
4373
4374CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004375ClangASTContext::CreateTypedef (lldb::opaque_compiler_type_t type, const char *typedef_name, const CompilerDeclContext &compiler_decl_ctx)
Greg Clayton56939cb2015-09-17 22:23:34 +00004376{
4377 if (type)
4378 {
4379 clang::ASTContext* clang_ast = getASTContext();
4380 clang::QualType qual_type (GetQualType(type));
4381
4382 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4383 if (decl_ctx == nullptr)
4384 decl_ctx = getASTContext()->getTranslationUnitDecl();
4385
4386 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4387 decl_ctx,
4388 clang::SourceLocation(),
4389 clang::SourceLocation(),
4390 &clang_ast->Idents.get(typedef_name),
4391 clang_ast->getTrivialTypeSourceInfo(qual_type));
4392
4393 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4394
4395 // Get a uniqued clang::QualType for the typedef decl type
4396 return CompilerType (this, clang_ast->getTypedefType (decl).getAsOpaquePtr());
4397
4398 }
4399 return CompilerType();
4400
4401}
4402
Greg Claytona1e5dc82015-08-11 22:53:00 +00004403CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004404ClangASTContext::GetTypedefedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004405{
4406 if (type)
4407 {
4408 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4409 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004410 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004411 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004412 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004413}
4414
Greg Claytona1e5dc82015-08-11 22:53:00 +00004415CompilerType
4416ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004417{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004418 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004419 {
4420 clang::QualType qual_type(GetQualType(type));
4421 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004422 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004423 }
4424 return type;
4425}
4426
4427
4428//----------------------------------------------------------------------
4429// Create related types using the current type's AST
4430//----------------------------------------------------------------------
4431
Greg Claytona1e5dc82015-08-11 22:53:00 +00004432CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004433ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004434{
Greg Clayton99558cc42015-08-24 23:46:31 +00004435 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004436}
4437//----------------------------------------------------------------------
4438// Exploring the type
4439//----------------------------------------------------------------------
4440
4441uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004442ClangASTContext::GetBitSize (lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Greg Claytond8d4a572015-08-11 21:38:15 +00004443{
4444 if (GetCompleteType (type))
4445 {
4446 clang::QualType qual_type(GetCanonicalQualType(type));
4447 switch (qual_type->getTypeClass())
4448 {
4449 case clang::Type::ObjCInterface:
4450 case clang::Type::ObjCObject:
4451 {
4452 ExecutionContext exe_ctx (exe_scope);
4453 Process *process = exe_ctx.GetProcessPtr();
4454 if (process)
4455 {
4456 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4457 if (objc_runtime)
4458 {
4459 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004460 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004461 return bit_size;
4462 }
4463 }
4464 else
4465 {
4466 static bool g_printed = false;
4467 if (!g_printed)
4468 {
4469 StreamString s;
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00004470 DumpTypeDescription(type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00004471
4472 llvm::outs() << "warning: trying to determine the size of type ";
4473 llvm::outs() << s.GetString() << "\n";
4474 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4475 llvm::outs() << "backtrace:\n";
4476 llvm::sys::PrintStackTrace(llvm::outs());
4477 llvm::outs() << "\n";
4478 g_printed = true;
4479 }
4480 }
4481 }
4482 // fallthrough
4483 default:
4484 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4485 if (bit_size == 0)
4486 {
4487 if (qual_type->isIncompleteArrayType())
4488 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4489 }
4490 if (qual_type->isObjCObjectOrInterfaceType())
4491 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4492 return bit_size;
4493 }
4494 }
4495 return 0;
4496}
4497
4498size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004499ClangASTContext::GetTypeBitAlign (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004500{
4501 if (GetCompleteType(type))
4502 return getASTContext()->getTypeAlign(GetQualType(type));
4503 return 0;
4504}
4505
4506
4507lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004508ClangASTContext::GetEncoding (lldb::opaque_compiler_type_t type, uint64_t &count)
Greg Claytond8d4a572015-08-11 21:38:15 +00004509{
4510 if (!type)
4511 return lldb::eEncodingInvalid;
4512
4513 count = 1;
4514 clang::QualType qual_type(GetCanonicalQualType(type));
4515
4516 switch (qual_type->getTypeClass())
4517 {
4518 case clang::Type::UnaryTransform:
4519 break;
4520
4521 case clang::Type::FunctionNoProto:
4522 case clang::Type::FunctionProto:
4523 break;
4524
4525 case clang::Type::IncompleteArray:
4526 case clang::Type::VariableArray:
4527 break;
4528
4529 case clang::Type::ConstantArray:
4530 break;
4531
4532 case clang::Type::ExtVector:
4533 case clang::Type::Vector:
4534 // TODO: Set this to more than one???
4535 break;
4536
4537 case clang::Type::Builtin:
4538 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4539 {
Greg Claytond8d4a572015-08-11 21:38:15 +00004540 case clang::BuiltinType::Void:
4541 break;
4542
4543 case clang::BuiltinType::Bool:
4544 case clang::BuiltinType::Char_S:
4545 case clang::BuiltinType::SChar:
4546 case clang::BuiltinType::WChar_S:
4547 case clang::BuiltinType::Char16:
4548 case clang::BuiltinType::Char32:
4549 case clang::BuiltinType::Short:
4550 case clang::BuiltinType::Int:
4551 case clang::BuiltinType::Long:
4552 case clang::BuiltinType::LongLong:
4553 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4554
4555 case clang::BuiltinType::Char_U:
4556 case clang::BuiltinType::UChar:
4557 case clang::BuiltinType::WChar_U:
4558 case clang::BuiltinType::UShort:
4559 case clang::BuiltinType::UInt:
4560 case clang::BuiltinType::ULong:
4561 case clang::BuiltinType::ULongLong:
4562 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4563
Greg Claytondee40e72015-11-03 23:23:22 +00004564 case clang::BuiltinType::Half:
Greg Claytond8d4a572015-08-11 21:38:15 +00004565 case clang::BuiltinType::Float:
4566 case clang::BuiltinType::Double:
4567 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4568
4569 case clang::BuiltinType::ObjCClass:
4570 case clang::BuiltinType::ObjCId:
4571 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4572
4573 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4574
4575 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4576 case clang::BuiltinType::Kind::BoundMember:
4577 case clang::BuiltinType::Kind::BuiltinFn:
4578 case clang::BuiltinType::Kind::Dependent:
Ed Mastec6dd6512015-09-23 18:32:34 +00004579 case clang::BuiltinType::Kind::OCLClkEvent:
Greg Claytond8d4a572015-08-11 21:38:15 +00004580 case clang::BuiltinType::Kind::OCLEvent:
4581 case clang::BuiltinType::Kind::OCLImage1d:
4582 case clang::BuiltinType::Kind::OCLImage1dArray:
4583 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4584 case clang::BuiltinType::Kind::OCLImage2d:
4585 case clang::BuiltinType::Kind::OCLImage2dArray:
Ed Mastec6dd6512015-09-23 18:32:34 +00004586 case clang::BuiltinType::Kind::OCLImage2dArrayDepth:
4587 case clang::BuiltinType::Kind::OCLImage2dArrayMSAA:
4588 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepth:
4589 case clang::BuiltinType::Kind::OCLImage2dDepth:
4590 case clang::BuiltinType::Kind::OCLImage2dMSAA:
4591 case clang::BuiltinType::Kind::OCLImage2dMSAADepth:
Greg Claytond8d4a572015-08-11 21:38:15 +00004592 case clang::BuiltinType::Kind::OCLImage3d:
Ed Mastec6dd6512015-09-23 18:32:34 +00004593 case clang::BuiltinType::Kind::OCLQueue:
4594 case clang::BuiltinType::Kind::OCLNDRange:
4595 case clang::BuiltinType::Kind::OCLReserveID:
Greg Claytond8d4a572015-08-11 21:38:15 +00004596 case clang::BuiltinType::Kind::OCLSampler:
Ed Mastec6dd6512015-09-23 18:32:34 +00004597 case clang::BuiltinType::Kind::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00004598 case clang::BuiltinType::Kind::Overload:
4599 case clang::BuiltinType::Kind::PseudoObject:
4600 case clang::BuiltinType::Kind::UnknownAny:
4601 break;
4602 }
4603 break;
4604 // All pointer types are represented as unsigned integer encodings.
4605 // We may nee to add a eEncodingPointer if we ever need to know the
4606 // difference
4607 case clang::Type::ObjCObjectPointer:
4608 case clang::Type::BlockPointer:
4609 case clang::Type::Pointer:
4610 case clang::Type::LValueReference:
4611 case clang::Type::RValueReference:
4612 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4613 case clang::Type::Complex:
4614 {
4615 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4616 if (qual_type->isComplexType())
4617 encoding = lldb::eEncodingIEEE754;
4618 else
4619 {
4620 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4621 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004622 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004623 else
4624 encoding = lldb::eEncodingSint;
4625 }
4626 count = 2;
4627 return encoding;
4628 }
4629
4630 case clang::Type::ObjCInterface: break;
4631 case clang::Type::Record: break;
4632 case clang::Type::Enum: return lldb::eEncodingSint;
4633 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004634 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004635
4636 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004637 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004638
4639 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004640 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004641
4642 case clang::Type::DependentSizedArray:
4643 case clang::Type::DependentSizedExtVector:
4644 case clang::Type::UnresolvedUsing:
4645 case clang::Type::Attributed:
4646 case clang::Type::TemplateTypeParm:
4647 case clang::Type::SubstTemplateTypeParm:
4648 case clang::Type::SubstTemplateTypeParmPack:
4649 case clang::Type::Auto:
4650 case clang::Type::InjectedClassName:
4651 case clang::Type::DependentName:
4652 case clang::Type::DependentTemplateSpecialization:
4653 case clang::Type::PackExpansion:
4654 case clang::Type::ObjCObject:
4655
4656 case clang::Type::TypeOfExpr:
4657 case clang::Type::TypeOf:
4658 case clang::Type::Decltype:
4659 case clang::Type::TemplateSpecialization:
4660 case clang::Type::Atomic:
4661 case clang::Type::Adjusted:
4662 break;
4663
4664 // pointer type decayed from an array or function type.
4665 case clang::Type::Decayed:
4666 break;
4667 }
4668 count = 0;
4669 return lldb::eEncodingInvalid;
4670}
4671
4672lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004673ClangASTContext::GetFormat (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004674{
4675 if (!type)
4676 return lldb::eFormatDefault;
4677
4678 clang::QualType qual_type(GetCanonicalQualType(type));
4679
4680 switch (qual_type->getTypeClass())
4681 {
4682 case clang::Type::UnaryTransform:
4683 break;
4684
4685 case clang::Type::FunctionNoProto:
4686 case clang::Type::FunctionProto:
4687 break;
4688
4689 case clang::Type::IncompleteArray:
4690 case clang::Type::VariableArray:
4691 break;
4692
4693 case clang::Type::ConstantArray:
4694 return lldb::eFormatVoid; // no value
4695
4696 case clang::Type::ExtVector:
4697 case clang::Type::Vector:
4698 break;
4699
4700 case clang::Type::Builtin:
4701 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4702 {
4703 //default: assert(0 && "Unknown builtin type!");
4704 case clang::BuiltinType::UnknownAny:
4705 case clang::BuiltinType::Void:
4706 case clang::BuiltinType::BoundMember:
4707 break;
4708
4709 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4710 case clang::BuiltinType::Char_S:
4711 case clang::BuiltinType::SChar:
4712 case clang::BuiltinType::WChar_S:
4713 case clang::BuiltinType::Char_U:
4714 case clang::BuiltinType::UChar:
4715 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4716 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4717 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4718 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4719 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4720 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4721 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4722 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4723 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4724 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4725 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4726 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4727 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
Greg Claytondee40e72015-11-03 23:23:22 +00004728 case clang::BuiltinType::Half:
4729 case clang::BuiltinType::Float:
4730 case clang::BuiltinType::Double:
Greg Claytond8d4a572015-08-11 21:38:15 +00004731 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004732 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004733 return lldb::eFormatHex;
4734 }
4735 break;
4736 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4737 case clang::Type::BlockPointer: return lldb::eFormatHex;
4738 case clang::Type::Pointer: return lldb::eFormatHex;
4739 case clang::Type::LValueReference:
4740 case clang::Type::RValueReference: return lldb::eFormatHex;
4741 case clang::Type::MemberPointer: break;
4742 case clang::Type::Complex:
4743 {
4744 if (qual_type->isComplexType())
4745 return lldb::eFormatComplex;
4746 else
4747 return lldb::eFormatComplexInteger;
4748 }
4749 case clang::Type::ObjCInterface: break;
4750 case clang::Type::Record: break;
4751 case clang::Type::Enum: return lldb::eFormatEnum;
4752 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004753 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004754 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004755 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004756 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004757 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004758 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004759 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004760 case clang::Type::DependentSizedArray:
4761 case clang::Type::DependentSizedExtVector:
4762 case clang::Type::UnresolvedUsing:
4763 case clang::Type::Attributed:
4764 case clang::Type::TemplateTypeParm:
4765 case clang::Type::SubstTemplateTypeParm:
4766 case clang::Type::SubstTemplateTypeParmPack:
4767 case clang::Type::InjectedClassName:
4768 case clang::Type::DependentName:
4769 case clang::Type::DependentTemplateSpecialization:
4770 case clang::Type::PackExpansion:
4771 case clang::Type::ObjCObject:
4772
4773 case clang::Type::TypeOfExpr:
4774 case clang::Type::TypeOf:
4775 case clang::Type::Decltype:
4776 case clang::Type::TemplateSpecialization:
4777 case clang::Type::Atomic:
4778 case clang::Type::Adjusted:
4779 break;
4780
4781 // pointer type decayed from an array or function type.
4782 case clang::Type::Decayed:
4783 break;
4784 }
4785 // We don't know hot to display this type...
4786 return lldb::eFormatBytes;
4787}
4788
4789static bool
4790ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4791{
4792 while (class_interface_decl)
4793 {
4794 if (class_interface_decl->ivar_size() > 0)
4795 return true;
4796
4797 if (check_superclass)
4798 class_interface_decl = class_interface_decl->getSuperClass();
4799 else
4800 break;
4801 }
4802 return false;
4803}
4804
4805uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004806ClangASTContext::GetNumChildren (lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00004807{
4808 if (!type)
4809 return 0;
4810
4811 uint32_t num_children = 0;
4812 clang::QualType qual_type(GetQualType(type));
4813 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4814 switch (type_class)
4815 {
4816 case clang::Type::Builtin:
4817 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4818 {
4819 case clang::BuiltinType::ObjCId: // child is Class
4820 case clang::BuiltinType::ObjCClass: // child is Class
4821 num_children = 1;
4822 break;
4823
4824 default:
4825 break;
4826 }
4827 break;
4828
4829 case clang::Type::Complex: return 0;
4830
4831 case clang::Type::Record:
4832 if (GetCompleteQualType (getASTContext(), qual_type))
4833 {
4834 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4835 const clang::RecordDecl *record_decl = record_type->getDecl();
4836 assert(record_decl);
4837 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4838 if (cxx_record_decl)
4839 {
4840 if (omit_empty_base_classes)
4841 {
4842 // Check each base classes to see if it or any of its
4843 // base classes contain any fields. This can help
4844 // limit the noise in variable views by not having to
4845 // show base classes that contain no members.
4846 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4847 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4848 base_class != base_class_end;
4849 ++base_class)
4850 {
4851 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4852
4853 // Skip empty base classes
4854 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4855 continue;
4856
4857 num_children++;
4858 }
4859 }
4860 else
4861 {
4862 // Include all base classes
4863 num_children += cxx_record_decl->getNumBases();
4864 }
4865
4866 }
4867 clang::RecordDecl::field_iterator field, field_end;
4868 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4869 ++num_children;
4870 }
4871 break;
4872
4873 case clang::Type::ObjCObject:
4874 case clang::Type::ObjCInterface:
4875 if (GetCompleteQualType (getASTContext(), qual_type))
4876 {
4877 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4878 assert (objc_class_type);
4879 if (objc_class_type)
4880 {
4881 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4882
4883 if (class_interface_decl)
4884 {
4885
4886 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4887 if (superclass_interface_decl)
4888 {
4889 if (omit_empty_base_classes)
4890 {
4891 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4892 ++num_children;
4893 }
4894 else
4895 ++num_children;
4896 }
4897
4898 num_children += class_interface_decl->ivar_size();
4899 }
4900 }
4901 }
4902 break;
4903
4904 case clang::Type::ObjCObjectPointer:
4905 {
4906 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4907 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004908 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004909 // If this type points to a simple type, then it has 1 child
4910 if (num_pointee_children == 0)
4911 num_children = 1;
4912 else
4913 num_children = num_pointee_children;
4914 }
4915 break;
4916
4917 case clang::Type::Vector:
4918 case clang::Type::ExtVector:
4919 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4920 break;
4921
4922 case clang::Type::ConstantArray:
4923 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4924 break;
4925
4926 case clang::Type::Pointer:
4927 {
4928 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4929 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004930 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004931 if (num_pointee_children == 0)
4932 {
4933 // We have a pointer to a pointee type that claims it has no children.
4934 // We will want to look at
4935 num_children = GetNumPointeeChildren (pointee_type);
4936 }
4937 else
4938 num_children = num_pointee_children;
4939 }
4940 break;
4941
4942 case clang::Type::LValueReference:
4943 case clang::Type::RValueReference:
4944 {
4945 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4946 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004947 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004948 // If this type points to a simple type, then it has 1 child
4949 if (num_pointee_children == 0)
4950 num_children = 1;
4951 else
4952 num_children = num_pointee_children;
4953 }
4954 break;
4955
4956
4957 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004958 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004959 break;
4960
4961 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004962 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004963 break;
4964
4965 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004966 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004967 break;
4968 default:
4969 break;
4970 }
4971 return num_children;
4972}
4973
Greg Clayton56939cb2015-09-17 22:23:34 +00004974CompilerType
4975ClangASTContext::GetBuiltinTypeByName (const ConstString &name)
4976{
4977 return GetBasicType (GetBasicTypeEnumeration (name));
4978}
4979
Greg Claytond8d4a572015-08-11 21:38:15 +00004980lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004981ClangASTContext::GetBasicTypeEnumeration (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004982{
4983 if (type)
4984 {
4985 clang::QualType qual_type(GetQualType(type));
4986 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4987 if (type_class == clang::Type::Builtin)
4988 {
4989 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4990 {
4991 case clang::BuiltinType::Void: return eBasicTypeVoid;
4992 case clang::BuiltinType::Bool: return eBasicTypeBool;
4993 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
4994 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
4995 case clang::BuiltinType::Char16: return eBasicTypeChar16;
4996 case clang::BuiltinType::Char32: return eBasicTypeChar32;
4997 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
4998 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
4999 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
5000 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
5001 case clang::BuiltinType::Short: return eBasicTypeShort;
5002 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
5003 case clang::BuiltinType::Int: return eBasicTypeInt;
5004 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
5005 case clang::BuiltinType::Long: return eBasicTypeLong;
5006 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
5007 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
5008 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
5009 case clang::BuiltinType::Int128: return eBasicTypeInt128;
5010 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
5011
5012 case clang::BuiltinType::Half: return eBasicTypeHalf;
5013 case clang::BuiltinType::Float: return eBasicTypeFloat;
5014 case clang::BuiltinType::Double: return eBasicTypeDouble;
5015 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
5016
5017 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
5018 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
5019 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
5020 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005021 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00005022 return eBasicTypeOther;
5023 }
5024 }
5025 }
5026 return eBasicTypeInvalid;
5027}
5028
Greg Clayton99558cc42015-08-24 23:46:31 +00005029void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005030ClangASTContext::ForEachEnumerator (lldb::opaque_compiler_type_t type, std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback)
Greg Clayton99558cc42015-08-24 23:46:31 +00005031{
5032 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5033 if (enum_type)
5034 {
5035 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5036 if (enum_decl)
5037 {
5038 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
5039
5040 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5041 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5042 {
5043 ConstString name(enum_pos->getNameAsString().c_str());
5044 if (!callback (integer_type, name, enum_pos->getInitVal()))
5045 break;
5046 }
5047 }
5048 }
5049}
5050
Greg Claytond8d4a572015-08-11 21:38:15 +00005051
5052#pragma mark Aggregate Types
5053
5054uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005055ClangASTContext::GetNumFields (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005056{
5057 if (!type)
5058 return 0;
5059
5060 uint32_t count = 0;
5061 clang::QualType qual_type(GetCanonicalQualType(type));
5062 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5063 switch (type_class)
5064 {
5065 case clang::Type::Record:
5066 if (GetCompleteType(type))
5067 {
5068 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5069 if (record_type)
5070 {
5071 clang::RecordDecl *record_decl = record_type->getDecl();
5072 if (record_decl)
5073 {
5074 uint32_t field_idx = 0;
5075 clang::RecordDecl::field_iterator field, field_end;
5076 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5077 ++field_idx;
5078 count = field_idx;
5079 }
5080 }
5081 }
5082 break;
5083
5084 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005085 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005086 break;
5087
5088 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005089 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005090 break;
5091
5092 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005093 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005094 break;
5095
5096 case clang::Type::ObjCObjectPointer:
5097 if (GetCompleteType(type))
5098 {
5099 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5100 if (objc_class_type)
5101 {
5102 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5103
5104 if (class_interface_decl)
5105 count = class_interface_decl->ivar_size();
5106 }
5107 }
5108 break;
5109
5110 case clang::Type::ObjCObject:
5111 case clang::Type::ObjCInterface:
5112 if (GetCompleteType(type))
5113 {
5114 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5115 if (objc_class_type)
5116 {
5117 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5118
5119 if (class_interface_decl)
5120 count = class_interface_decl->ivar_size();
5121 }
5122 }
5123 break;
5124
5125 default:
5126 break;
5127 }
5128 return count;
5129}
5130
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005131static lldb::opaque_compiler_type_t
Greg Claytond8d4a572015-08-11 21:38:15 +00005132GetObjCFieldAtIndex (clang::ASTContext *ast,
5133 clang::ObjCInterfaceDecl *class_interface_decl,
5134 size_t idx,
5135 std::string& name,
5136 uint64_t *bit_offset_ptr,
5137 uint32_t *bitfield_bit_size_ptr,
5138 bool *is_bitfield_ptr)
5139{
5140 if (class_interface_decl)
5141 {
5142 if (idx < (class_interface_decl->ivar_size()))
5143 {
5144 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5145 uint32_t ivar_idx = 0;
5146
5147 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5148 {
5149 if (ivar_idx == idx)
5150 {
5151 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5152
5153 clang::QualType ivar_qual_type(ivar_decl->getType());
5154
5155 name.assign(ivar_decl->getNameAsString());
5156
5157 if (bit_offset_ptr)
5158 {
5159 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5160 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5161 }
5162
5163 const bool is_bitfield = ivar_pos->isBitField();
5164
5165 if (bitfield_bit_size_ptr)
5166 {
5167 *bitfield_bit_size_ptr = 0;
5168
5169 if (is_bitfield && ast)
5170 {
5171 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5172 llvm::APSInt bitfield_apsint;
5173 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5174 {
5175 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5176 }
5177 }
5178 }
5179 if (is_bitfield_ptr)
5180 *is_bitfield_ptr = is_bitfield;
5181
5182 return ivar_qual_type.getAsOpaquePtr();
5183 }
5184 }
5185 }
5186 }
5187 return nullptr;
5188}
5189
Greg Claytona1e5dc82015-08-11 22:53:00 +00005190CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005191ClangASTContext::GetFieldAtIndex (lldb::opaque_compiler_type_t type, size_t idx,
Greg Claytond8d4a572015-08-11 21:38:15 +00005192 std::string& name,
5193 uint64_t *bit_offset_ptr,
5194 uint32_t *bitfield_bit_size_ptr,
5195 bool *is_bitfield_ptr)
5196{
5197 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005198 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005199
5200 clang::QualType qual_type(GetCanonicalQualType(type));
5201 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5202 switch (type_class)
5203 {
5204 case clang::Type::Record:
5205 if (GetCompleteType(type))
5206 {
5207 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5208 const clang::RecordDecl *record_decl = record_type->getDecl();
5209 uint32_t field_idx = 0;
5210 clang::RecordDecl::field_iterator field, field_end;
5211 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5212 {
5213 if (idx == field_idx)
5214 {
5215 // Print the member type if requested
5216 // Print the member name and equal sign
5217 name.assign(field->getNameAsString());
5218
5219 // Figure out the type byte size (field_type_info.first) and
5220 // alignment (field_type_info.second) from the AST context.
5221 if (bit_offset_ptr)
5222 {
5223 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5224 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5225 }
5226
5227 const bool is_bitfield = field->isBitField();
5228
5229 if (bitfield_bit_size_ptr)
5230 {
5231 *bitfield_bit_size_ptr = 0;
5232
5233 if (is_bitfield)
5234 {
5235 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5236 llvm::APSInt bitfield_apsint;
5237 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5238 {
5239 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5240 }
5241 }
5242 }
5243 if (is_bitfield_ptr)
5244 *is_bitfield_ptr = is_bitfield;
5245
Greg Claytona1e5dc82015-08-11 22:53:00 +00005246 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005247 }
5248 }
5249 }
5250 break;
5251
5252 case clang::Type::ObjCObjectPointer:
5253 if (GetCompleteType(type))
5254 {
5255 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5256 if (objc_class_type)
5257 {
5258 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005259 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 +00005260 }
5261 }
5262 break;
5263
5264 case clang::Type::ObjCObject:
5265 case clang::Type::ObjCInterface:
5266 if (GetCompleteType(type))
5267 {
5268 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5269 assert (objc_class_type);
5270 if (objc_class_type)
5271 {
5272 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005273 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 +00005274 }
5275 }
5276 break;
5277
5278
5279 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005280 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005281 GetFieldAtIndex (idx,
5282 name,
5283 bit_offset_ptr,
5284 bitfield_bit_size_ptr,
5285 is_bitfield_ptr);
5286
5287 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005288 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005289 GetFieldAtIndex (idx,
5290 name,
5291 bit_offset_ptr,
5292 bitfield_bit_size_ptr,
5293 is_bitfield_ptr);
5294
5295 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005296 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005297 GetFieldAtIndex (idx,
5298 name,
5299 bit_offset_ptr,
5300 bitfield_bit_size_ptr,
5301 is_bitfield_ptr);
5302
5303 default:
5304 break;
5305 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005306 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005307}
5308
Greg Clayton99558cc42015-08-24 23:46:31 +00005309uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005310ClangASTContext::GetNumDirectBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005311{
5312 uint32_t count = 0;
5313 clang::QualType qual_type(GetCanonicalQualType(type));
5314 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5315 switch (type_class)
5316 {
5317 case clang::Type::Record:
5318 if (GetCompleteType(type))
5319 {
5320 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5321 if (cxx_record_decl)
5322 count = cxx_record_decl->getNumBases();
5323 }
5324 break;
5325
5326 case clang::Type::ObjCObjectPointer:
5327 count = GetPointeeType(type).GetNumDirectBaseClasses();
5328 break;
5329
5330 case clang::Type::ObjCObject:
5331 if (GetCompleteType(type))
5332 {
5333 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5334 if (objc_class_type)
5335 {
5336 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5337
5338 if (class_interface_decl && class_interface_decl->getSuperClass())
5339 count = 1;
5340 }
5341 }
5342 break;
5343 case clang::Type::ObjCInterface:
5344 if (GetCompleteType(type))
5345 {
5346 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5347 if (objc_interface_type)
5348 {
5349 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5350
5351 if (class_interface_decl && class_interface_decl->getSuperClass())
5352 count = 1;
5353 }
5354 }
5355 break;
5356
5357
5358 case clang::Type::Typedef:
5359 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5360 break;
5361
5362 case clang::Type::Elaborated:
5363 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5364 break;
5365
5366 case clang::Type::Paren:
5367 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5368
5369 default:
5370 break;
5371 }
5372 return count;
5373
5374}
5375
5376uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005377ClangASTContext::GetNumVirtualBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005378{
5379 uint32_t count = 0;
5380 clang::QualType qual_type(GetCanonicalQualType(type));
5381 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5382 switch (type_class)
5383 {
5384 case clang::Type::Record:
5385 if (GetCompleteType(type))
5386 {
5387 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5388 if (cxx_record_decl)
5389 count = cxx_record_decl->getNumVBases();
5390 }
5391 break;
5392
5393 case clang::Type::Typedef:
5394 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5395 break;
5396
5397 case clang::Type::Elaborated:
5398 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5399 break;
5400
5401 case clang::Type::Paren:
5402 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5403 break;
5404
5405 default:
5406 break;
5407 }
5408 return count;
5409
5410}
5411
5412CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005413ClangASTContext::GetDirectBaseClassAtIndex (lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00005414{
5415 clang::QualType qual_type(GetCanonicalQualType(type));
5416 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5417 switch (type_class)
5418 {
5419 case clang::Type::Record:
5420 if (GetCompleteType(type))
5421 {
5422 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5423 if (cxx_record_decl)
5424 {
5425 uint32_t curr_idx = 0;
5426 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5427 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5428 base_class != base_class_end;
5429 ++base_class, ++curr_idx)
5430 {
5431 if (curr_idx == idx)
5432 {
5433 if (bit_offset_ptr)
5434 {
5435 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5436 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5437 if (base_class->isVirtual())
5438 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5439 else
5440 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5441 }
5442 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5443 }
5444 }
5445 }
5446 }
5447 break;
5448
5449 case clang::Type::ObjCObjectPointer:
5450 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5451
5452 case clang::Type::ObjCObject:
5453 if (idx == 0 && GetCompleteType(type))
5454 {
5455 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5456 if (objc_class_type)
5457 {
5458 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5459
5460 if (class_interface_decl)
5461 {
5462 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5463 if (superclass_interface_decl)
5464 {
5465 if (bit_offset_ptr)
5466 *bit_offset_ptr = 0;
5467 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5468 }
5469 }
5470 }
5471 }
5472 break;
5473 case clang::Type::ObjCInterface:
5474 if (idx == 0 && GetCompleteType(type))
5475 {
5476 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5477 if (objc_interface_type)
5478 {
5479 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5480
5481 if (class_interface_decl)
5482 {
5483 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5484 if (superclass_interface_decl)
5485 {
5486 if (bit_offset_ptr)
5487 *bit_offset_ptr = 0;
5488 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5489 }
5490 }
5491 }
5492 }
5493 break;
5494
5495
5496 case clang::Type::Typedef:
5497 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5498
5499 case clang::Type::Elaborated:
5500 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5501
5502 case clang::Type::Paren:
5503 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5504
5505 default:
5506 break;
5507 }
5508 return CompilerType();
5509}
5510
5511CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005512ClangASTContext::GetVirtualBaseClassAtIndex (lldb::opaque_compiler_type_t type,
Greg Clayton99558cc42015-08-24 23:46:31 +00005513 size_t idx,
5514 uint32_t *bit_offset_ptr)
5515{
5516 clang::QualType qual_type(GetCanonicalQualType(type));
5517 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5518 switch (type_class)
5519 {
5520 case clang::Type::Record:
5521 if (GetCompleteType(type))
5522 {
5523 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5524 if (cxx_record_decl)
5525 {
5526 uint32_t curr_idx = 0;
5527 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5528 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5529 base_class != base_class_end;
5530 ++base_class, ++curr_idx)
5531 {
5532 if (curr_idx == idx)
5533 {
5534 if (bit_offset_ptr)
5535 {
5536 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5537 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5538 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5539
5540 }
5541 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5542 }
5543 }
5544 }
5545 }
5546 break;
5547
5548 case clang::Type::Typedef:
5549 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5550
5551 case clang::Type::Elaborated:
5552 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5553
5554 case clang::Type::Paren:
5555 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5556
5557 default:
5558 break;
5559 }
5560 return CompilerType();
5561
5562}
5563
Greg Claytond8d4a572015-08-11 21:38:15 +00005564// If a pointer to a pointee type (the clang_type arg) says that it has no
5565// children, then we either need to trust it, or override it and return a
5566// different result. For example, an "int *" has one child that is an integer,
5567// but a function pointer doesn't have any children. Likewise if a Record type
5568// claims it has no children, then there really is nothing to show.
5569uint32_t
5570ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5571{
5572 if (type.isNull())
5573 return 0;
5574
5575 clang::QualType qual_type(type.getCanonicalType());
5576 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5577 switch (type_class)
5578 {
5579 case clang::Type::Builtin:
5580 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5581 {
5582 case clang::BuiltinType::UnknownAny:
5583 case clang::BuiltinType::Void:
5584 case clang::BuiltinType::NullPtr:
5585 case clang::BuiltinType::OCLEvent:
5586 case clang::BuiltinType::OCLImage1d:
5587 case clang::BuiltinType::OCLImage1dArray:
5588 case clang::BuiltinType::OCLImage1dBuffer:
5589 case clang::BuiltinType::OCLImage2d:
5590 case clang::BuiltinType::OCLImage2dArray:
5591 case clang::BuiltinType::OCLImage3d:
5592 case clang::BuiltinType::OCLSampler:
5593 return 0;
5594 case clang::BuiltinType::Bool:
5595 case clang::BuiltinType::Char_U:
5596 case clang::BuiltinType::UChar:
5597 case clang::BuiltinType::WChar_U:
5598 case clang::BuiltinType::Char16:
5599 case clang::BuiltinType::Char32:
5600 case clang::BuiltinType::UShort:
5601 case clang::BuiltinType::UInt:
5602 case clang::BuiltinType::ULong:
5603 case clang::BuiltinType::ULongLong:
5604 case clang::BuiltinType::UInt128:
5605 case clang::BuiltinType::Char_S:
5606 case clang::BuiltinType::SChar:
5607 case clang::BuiltinType::WChar_S:
5608 case clang::BuiltinType::Short:
5609 case clang::BuiltinType::Int:
5610 case clang::BuiltinType::Long:
5611 case clang::BuiltinType::LongLong:
5612 case clang::BuiltinType::Int128:
5613 case clang::BuiltinType::Float:
5614 case clang::BuiltinType::Double:
5615 case clang::BuiltinType::LongDouble:
5616 case clang::BuiltinType::Dependent:
5617 case clang::BuiltinType::Overload:
5618 case clang::BuiltinType::ObjCId:
5619 case clang::BuiltinType::ObjCClass:
5620 case clang::BuiltinType::ObjCSel:
5621 case clang::BuiltinType::BoundMember:
5622 case clang::BuiltinType::Half:
5623 case clang::BuiltinType::ARCUnbridgedCast:
5624 case clang::BuiltinType::PseudoObject:
5625 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005626 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005627 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005628 default:
5629 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005630 }
5631 break;
5632
5633 case clang::Type::Complex: return 1;
5634 case clang::Type::Pointer: return 1;
5635 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5636 case clang::Type::LValueReference: return 1;
5637 case clang::Type::RValueReference: return 1;
5638 case clang::Type::MemberPointer: return 0;
5639 case clang::Type::ConstantArray: return 0;
5640 case clang::Type::IncompleteArray: return 0;
5641 case clang::Type::VariableArray: return 0;
5642 case clang::Type::DependentSizedArray: return 0;
5643 case clang::Type::DependentSizedExtVector: return 0;
5644 case clang::Type::Vector: return 0;
5645 case clang::Type::ExtVector: return 0;
5646 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5647 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5648 case clang::Type::UnresolvedUsing: return 0;
5649 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5650 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5651 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5652 case clang::Type::TypeOfExpr: return 0;
5653 case clang::Type::TypeOf: return 0;
5654 case clang::Type::Decltype: return 0;
5655 case clang::Type::Record: return 0;
5656 case clang::Type::Enum: return 1;
5657 case clang::Type::TemplateTypeParm: return 1;
5658 case clang::Type::SubstTemplateTypeParm: return 1;
5659 case clang::Type::TemplateSpecialization: return 1;
5660 case clang::Type::InjectedClassName: return 0;
5661 case clang::Type::DependentName: return 1;
5662 case clang::Type::DependentTemplateSpecialization: return 1;
5663 case clang::Type::ObjCObject: return 0;
5664 case clang::Type::ObjCInterface: return 0;
5665 case clang::Type::ObjCObjectPointer: return 1;
5666 default:
5667 break;
5668 }
5669 return 0;
5670}
5671
5672
Greg Claytona1e5dc82015-08-11 22:53:00 +00005673CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005674ClangASTContext::GetChildCompilerTypeAtIndex (lldb::opaque_compiler_type_t type,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005675 ExecutionContext *exe_ctx,
5676 size_t idx,
5677 bool transparent_pointers,
5678 bool omit_empty_base_classes,
5679 bool ignore_array_bounds,
5680 std::string& child_name,
5681 uint32_t &child_byte_size,
5682 int32_t &child_byte_offset,
5683 uint32_t &child_bitfield_bit_size,
5684 uint32_t &child_bitfield_bit_offset,
5685 bool &child_is_base_class,
5686 bool &child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005687 ValueObject *valobj,
5688 uint64_t &language_flags)
Greg Claytond8d4a572015-08-11 21:38:15 +00005689{
5690 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005691 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005692
5693 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5694 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5695 child_bitfield_bit_size = 0;
5696 child_bitfield_bit_offset = 0;
5697 child_is_base_class = false;
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005698 language_flags = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005699
5700 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5701 uint32_t bit_offset;
5702 switch (parent_type_class)
5703 {
5704 case clang::Type::Builtin:
5705 if (idx_is_valid)
5706 {
5707 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5708 {
5709 case clang::BuiltinType::ObjCId:
5710 case clang::BuiltinType::ObjCClass:
5711 child_name = "isa";
5712 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005713 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005714
5715 default:
5716 break;
5717 }
5718 }
5719 break;
5720
5721 case clang::Type::Record:
5722 if (idx_is_valid && GetCompleteType(type))
5723 {
5724 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5725 const clang::RecordDecl *record_decl = record_type->getDecl();
5726 assert(record_decl);
5727 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5728 uint32_t child_idx = 0;
5729
5730 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5731 if (cxx_record_decl)
5732 {
5733 // We might have base classes to print out first
5734 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5735 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5736 base_class != base_class_end;
5737 ++base_class)
5738 {
5739 const clang::CXXRecordDecl *base_class_decl = nullptr;
5740
5741 // Skip empty base classes
5742 if (omit_empty_base_classes)
5743 {
5744 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5745 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5746 continue;
5747 }
5748
5749 if (idx == child_idx)
5750 {
5751 if (base_class_decl == nullptr)
5752 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5753
5754
5755 if (base_class->isVirtual())
5756 {
5757 bool handled = false;
5758 if (valobj)
5759 {
5760 Error err;
5761 AddressType addr_type = eAddressTypeInvalid;
5762 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5763
5764 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5765 {
5766
5767 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5768 Process *process = exe_ctx.GetProcessPtr();
5769 if (process)
5770 {
5771 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5772 if (vtable_ctx)
5773 {
5774 if (vtable_ctx->isMicrosoft())
5775 {
5776 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5777
5778 if (vtable_ptr_addr)
5779 {
5780 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5781
5782 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5783 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5784 {
5785 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5786 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5787 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5788 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5789 if (base_offset != UINT32_MAX)
5790 {
5791 handled = true;
5792 bit_offset = base_offset * 8;
5793 }
5794 }
5795 }
5796 }
5797 else
5798 {
5799 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5800 if (vtable_ptr_addr)
5801 {
5802 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5803 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5804 {
5805 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5806 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5807 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5808 if (base_offset != UINT32_MAX)
5809 {
5810 handled = true;
5811 bit_offset = base_offset * 8;
5812 }
5813 }
5814 }
5815 }
5816 }
5817 }
5818 }
5819
5820 }
5821 if (!handled)
5822 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5823 }
5824 else
5825 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5826
5827 // Base classes should be a multiple of 8 bits in size
5828 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005829 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005830 child_name = base_class_clang_type.GetTypeName().AsCString("");
5831 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5832
5833 // Base classes bit sizes should be a multiple of 8 bits in size
5834 assert (base_class_clang_type_bit_size % 8 == 0);
5835 child_byte_size = base_class_clang_type_bit_size / 8;
5836 child_is_base_class = true;
5837 return base_class_clang_type;
5838 }
5839 // We don't increment the child index in the for loop since we might
5840 // be skipping empty base classes
5841 ++child_idx;
5842 }
5843 }
5844 // Make sure index is in range...
5845 uint32_t field_idx = 0;
5846 clang::RecordDecl::field_iterator field, field_end;
5847 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5848 {
5849 if (idx == child_idx)
5850 {
5851 // Print the member type if requested
5852 // Print the member name and equal sign
5853 child_name.assign(field->getNameAsString().c_str());
5854
5855 // Figure out the type byte size (field_type_info.first) and
5856 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005857 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005858 assert(field_idx < record_layout.getFieldCount());
5859 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5860
5861 // Figure out the field offset within the current struct/union/class type
5862 bit_offset = record_layout.getFieldOffset (field_idx);
5863 child_byte_offset = bit_offset / 8;
5864 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5865 child_bitfield_bit_offset = bit_offset % 8;
5866
5867 return field_clang_type;
5868 }
5869 }
5870 }
5871 break;
5872
5873 case clang::Type::ObjCObject:
5874 case clang::Type::ObjCInterface:
5875 if (idx_is_valid && GetCompleteType(type))
5876 {
5877 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5878 assert (objc_class_type);
5879 if (objc_class_type)
5880 {
5881 uint32_t child_idx = 0;
5882 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5883
5884 if (class_interface_decl)
5885 {
5886
5887 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5888 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5889 if (superclass_interface_decl)
5890 {
5891 if (omit_empty_base_classes)
5892 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005893 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005894 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5895 {
5896 if (idx == 0)
5897 {
5898 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5899
5900
5901 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5902
5903 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5904
5905 child_byte_size = ivar_type_info.Width / 8;
5906 child_byte_offset = 0;
5907 child_is_base_class = true;
5908
Greg Claytona1e5dc82015-08-11 22:53:00 +00005909 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005910 }
5911
5912 ++child_idx;
5913 }
5914 }
5915 else
5916 ++child_idx;
5917 }
5918
5919 const uint32_t superclass_idx = child_idx;
5920
5921 if (idx < (child_idx + class_interface_decl->ivar_size()))
5922 {
5923 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5924
5925 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5926 {
5927 if (child_idx == idx)
5928 {
5929 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5930
5931 clang::QualType ivar_qual_type(ivar_decl->getType());
5932
5933 child_name.assign(ivar_decl->getNameAsString().c_str());
5934
5935 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5936
5937 child_byte_size = ivar_type_info.Width / 8;
5938
5939 // Figure out the field offset within the current struct/union/class type
5940 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5941 // that doesn't account for the space taken up by unbacked properties, or from
5942 // the changing size of base classes that are newer than this class.
5943 // So if we have a process around that we can ask about this object, do so.
5944 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5945 Process *process = nullptr;
5946 if (exe_ctx)
5947 process = exe_ctx->GetProcessPtr();
5948 if (process)
5949 {
5950 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5951 if (objc_runtime != nullptr)
5952 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005953 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005954 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5955 }
5956 }
5957
5958 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5959 bit_offset = UINT32_MAX;
5960
5961 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5962 {
5963 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5964 child_byte_offset = bit_offset / 8;
5965 }
5966
5967 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
5968 // of a bitfield within its containing object. So regardless of where we get the byte
5969 // offset from, we still need to get the bit offset for bitfields from the layout.
5970
5971 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
5972 {
5973 if (bit_offset == UINT32_MAX)
5974 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5975
5976 child_bitfield_bit_offset = bit_offset % 8;
5977 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005978 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005979 }
5980 ++child_idx;
5981 }
5982 }
5983 }
5984 }
5985 }
5986 break;
5987
5988 case clang::Type::ObjCObjectPointer:
5989 if (idx_is_valid)
5990 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005991 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00005992
5993 if (transparent_pointers && pointee_clang_type.IsAggregateType())
5994 {
5995 child_is_deref_of_parent = false;
5996 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005997 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
5998 idx,
5999 transparent_pointers,
6000 omit_empty_base_classes,
6001 ignore_array_bounds,
6002 child_name,
6003 child_byte_size,
6004 child_byte_offset,
6005 child_bitfield_bit_size,
6006 child_bitfield_bit_offset,
6007 child_is_base_class,
6008 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006009 valobj,
6010 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006011 }
6012 else
6013 {
6014 child_is_deref_of_parent = true;
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 && pointee_clang_type.GetCompleteType())
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::Vector:
6034 case clang::Type::ExtVector:
6035 if (idx_is_valid)
6036 {
6037 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6038 if (array)
6039 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006040 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006041 if (element_type.GetCompleteType())
6042 {
6043 char element_name[64];
6044 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6045 child_name.assign(element_name);
6046 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6047 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6048 return element_type;
6049 }
6050 }
6051 }
6052 break;
6053
6054 case clang::Type::ConstantArray:
6055 case clang::Type::IncompleteArray:
6056 if (ignore_array_bounds || idx_is_valid)
6057 {
6058 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6059 if (array)
6060 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006061 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006062 if (element_type.GetCompleteType())
6063 {
6064 char element_name[64];
6065 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6066 child_name.assign(element_name);
6067 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6068 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6069 return element_type;
6070 }
6071 }
6072 }
6073 break;
6074
6075
6076 case clang::Type::Pointer:
6077 if (idx_is_valid)
6078 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006079 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006080
6081 // Don't dereference "void *" pointers
6082 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00006083 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006084
6085 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6086 {
6087 child_is_deref_of_parent = false;
6088 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006089 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6090 idx,
6091 transparent_pointers,
6092 omit_empty_base_classes,
6093 ignore_array_bounds,
6094 child_name,
6095 child_byte_size,
6096 child_byte_offset,
6097 child_bitfield_bit_size,
6098 child_bitfield_bit_offset,
6099 child_is_base_class,
6100 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006101 valobj,
6102 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006103 }
6104 else
6105 {
6106 child_is_deref_of_parent = true;
6107
6108 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6109 if (parent_name)
6110 {
6111 child_name.assign(1, '*');
6112 child_name += parent_name;
6113 }
6114
6115 // We have a pointer to an simple type
6116 if (idx == 0)
6117 {
6118 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6119 child_byte_offset = 0;
6120 return pointee_clang_type;
6121 }
6122 }
6123 }
6124 break;
6125
6126 case clang::Type::LValueReference:
6127 case clang::Type::RValueReference:
6128 if (idx_is_valid)
6129 {
6130 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006131 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006132 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6133 {
6134 child_is_deref_of_parent = false;
6135 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006136 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6137 idx,
6138 transparent_pointers,
6139 omit_empty_base_classes,
6140 ignore_array_bounds,
6141 child_name,
6142 child_byte_size,
6143 child_byte_offset,
6144 child_bitfield_bit_size,
6145 child_bitfield_bit_offset,
6146 child_is_base_class,
6147 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006148 valobj,
6149 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006150 }
6151 else
6152 {
6153 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6154 if (parent_name)
6155 {
6156 child_name.assign(1, '&');
6157 child_name += parent_name;
6158 }
6159
6160 // We have a pointer to an simple type
6161 if (idx == 0)
6162 {
6163 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6164 child_byte_offset = 0;
6165 return pointee_clang_type;
6166 }
6167 }
6168 }
6169 break;
6170
6171 case clang::Type::Typedef:
6172 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006173 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006174 return typedefed_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6175 idx,
6176 transparent_pointers,
6177 omit_empty_base_classes,
6178 ignore_array_bounds,
6179 child_name,
6180 child_byte_size,
6181 child_byte_offset,
6182 child_bitfield_bit_size,
6183 child_bitfield_bit_offset,
6184 child_is_base_class,
6185 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006186 valobj,
6187 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006188 }
6189 break;
6190
6191 case clang::Type::Elaborated:
6192 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006193 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006194 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6195 idx,
6196 transparent_pointers,
6197 omit_empty_base_classes,
6198 ignore_array_bounds,
6199 child_name,
6200 child_byte_size,
6201 child_byte_offset,
6202 child_bitfield_bit_size,
6203 child_bitfield_bit_offset,
6204 child_is_base_class,
6205 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006206 valobj,
6207 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006208 }
6209
6210 case clang::Type::Paren:
6211 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006212 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006213 return paren_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6214 idx,
6215 transparent_pointers,
6216 omit_empty_base_classes,
6217 ignore_array_bounds,
6218 child_name,
6219 child_byte_size,
6220 child_byte_offset,
6221 child_bitfield_bit_size,
6222 child_bitfield_bit_offset,
6223 child_is_base_class,
6224 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006225 valobj,
6226 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006227 }
6228
6229
6230 default:
6231 break;
6232 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006233 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006234}
6235
6236static uint32_t
6237GetIndexForRecordBase
6238(
6239 const clang::RecordDecl *record_decl,
6240 const clang::CXXBaseSpecifier *base_spec,
6241 bool omit_empty_base_classes
6242 )
6243{
6244 uint32_t child_idx = 0;
6245
6246 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6247
6248 // const char *super_name = record_decl->getNameAsCString();
6249 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6250 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6251 //
6252 if (cxx_record_decl)
6253 {
6254 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6255 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6256 base_class != base_class_end;
6257 ++base_class)
6258 {
6259 if (omit_empty_base_classes)
6260 {
6261 if (BaseSpecifierIsEmpty (base_class))
6262 continue;
6263 }
6264
6265 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6266 // child_idx,
6267 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6268 //
6269 //
6270 if (base_class == base_spec)
6271 return child_idx;
6272 ++child_idx;
6273 }
6274 }
6275
6276 return UINT32_MAX;
6277}
6278
6279
6280static uint32_t
6281GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6282 clang::NamedDecl *canonical_decl,
6283 bool omit_empty_base_classes)
6284{
6285 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6286 omit_empty_base_classes);
6287
6288 clang::RecordDecl::field_iterator field, field_end;
6289 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6290 field != field_end;
6291 ++field, ++child_idx)
6292 {
6293 if (field->getCanonicalDecl() == canonical_decl)
6294 return child_idx;
6295 }
6296
6297 return UINT32_MAX;
6298}
6299
6300// Look for a child member (doesn't include base classes, but it does include
6301// their members) in the type hierarchy. Returns an index path into "clang_type"
6302// on how to reach the appropriate member.
6303//
6304// class A
6305// {
6306// public:
6307// int m_a;
6308// int m_b;
6309// };
6310//
6311// class B
6312// {
6313// };
6314//
6315// class C :
6316// public B,
6317// public A
6318// {
6319// };
6320//
6321// If we have a clang type that describes "class C", and we wanted to looked
6322// "m_b" in it:
6323//
6324// With omit_empty_base_classes == false we would get an integer array back with:
6325// { 1, 1 }
6326// The first index 1 is the child index for "class A" within class C
6327// The second index 1 is the child index for "m_b" within class A
6328//
6329// With omit_empty_base_classes == true we would get an integer array back with:
6330// { 0, 1 }
6331// 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)
6332// The second index 1 is the child index for "m_b" within class A
6333
6334size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006335ClangASTContext::GetIndexOfChildMemberWithName (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006336 bool omit_empty_base_classes,
6337 std::vector<uint32_t>& child_indexes)
6338{
6339 if (type && name && name[0])
6340 {
6341 clang::QualType qual_type(GetCanonicalQualType(type));
6342 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6343 switch (type_class)
6344 {
6345 case clang::Type::Record:
6346 if (GetCompleteType(type))
6347 {
6348 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6349 const clang::RecordDecl *record_decl = record_type->getDecl();
6350
6351 assert(record_decl);
6352 uint32_t child_idx = 0;
6353
6354 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6355
6356 // Try and find a field that matches NAME
6357 clang::RecordDecl::field_iterator field, field_end;
6358 llvm::StringRef name_sref(name);
6359 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6360 field != field_end;
6361 ++field, ++child_idx)
6362 {
6363 llvm::StringRef field_name = field->getName();
6364 if (field_name.empty())
6365 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006366 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006367 child_indexes.push_back(child_idx);
6368 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6369 return child_indexes.size();
6370 child_indexes.pop_back();
6371
6372 }
6373 else if (field_name.equals (name_sref))
6374 {
6375 // We have to add on the number of base classes to this index!
6376 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6377 return child_indexes.size();
6378 }
6379 }
6380
6381 if (cxx_record_decl)
6382 {
6383 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6384
6385 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6386
6387 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6388 // Didn't find things easily, lets let clang do its thang...
6389 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6390 clang::DeclarationName decl_name(&ident_ref);
6391
6392 clang::CXXBasePaths paths;
6393 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6394 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6395 },
6396 paths))
6397 {
6398 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6399 for (path = paths.begin(); path != path_end; ++path)
6400 {
6401 const size_t num_path_elements = path->size();
6402 for (size_t e=0; e<num_path_elements; ++e)
6403 {
6404 clang::CXXBasePathElement elem = (*path)[e];
6405
6406 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6407 if (child_idx == UINT32_MAX)
6408 {
6409 child_indexes.clear();
6410 return 0;
6411 }
6412 else
6413 {
6414 child_indexes.push_back (child_idx);
6415 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6416 }
6417 }
6418 for (clang::NamedDecl *path_decl : path->Decls)
6419 {
6420 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6421 if (child_idx == UINT32_MAX)
6422 {
6423 child_indexes.clear();
6424 return 0;
6425 }
6426 else
6427 {
6428 child_indexes.push_back (child_idx);
6429 }
6430 }
6431 }
6432 return child_indexes.size();
6433 }
6434 }
6435
6436 }
6437 break;
6438
6439 case clang::Type::ObjCObject:
6440 case clang::Type::ObjCInterface:
6441 if (GetCompleteType(type))
6442 {
6443 llvm::StringRef name_sref(name);
6444 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6445 assert (objc_class_type);
6446 if (objc_class_type)
6447 {
6448 uint32_t child_idx = 0;
6449 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6450
6451 if (class_interface_decl)
6452 {
6453 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6454 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6455
6456 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6457 {
6458 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6459
6460 if (ivar_decl->getName().equals (name_sref))
6461 {
6462 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6463 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6464 ++child_idx;
6465
6466 child_indexes.push_back (child_idx);
6467 return child_indexes.size();
6468 }
6469 }
6470
6471 if (superclass_interface_decl)
6472 {
6473 // The super class index is always zero for ObjC classes,
6474 // so we push it onto the child indexes in case we find
6475 // an ivar in our superclass...
6476 child_indexes.push_back (0);
6477
Greg Claytona1e5dc82015-08-11 22:53:00 +00006478 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006479 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6480 omit_empty_base_classes,
6481 child_indexes))
6482 {
6483 // We did find an ivar in a superclass so just
6484 // return the results!
6485 return child_indexes.size();
6486 }
6487
6488 // We didn't find an ivar matching "name" in our
6489 // superclass, pop the superclass zero index that
6490 // we pushed on above.
6491 child_indexes.pop_back();
6492 }
6493 }
6494 }
6495 }
6496 break;
6497
6498 case clang::Type::ObjCObjectPointer:
6499 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006500 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006501 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6502 omit_empty_base_classes,
6503 child_indexes);
6504 }
6505 break;
6506
6507
6508 case clang::Type::ConstantArray:
6509 {
6510 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6511 // const uint64_t element_count = array->getSize().getLimitedValue();
6512 //
6513 // if (idx < element_count)
6514 // {
6515 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6516 //
6517 // char element_name[32];
6518 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6519 //
6520 // child_name.assign(element_name);
6521 // assert(field_type_info.first % 8 == 0);
6522 // child_byte_size = field_type_info.first / 8;
6523 // child_byte_offset = idx * child_byte_size;
6524 // return array->getElementType().getAsOpaquePtr();
6525 // }
6526 }
6527 break;
6528
6529 // case clang::Type::MemberPointerType:
6530 // {
6531 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6532 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6533 //
6534 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6535 // {
6536 // return GetIndexOfChildWithName (ast,
6537 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6538 // name);
6539 // }
6540 // }
6541 // break;
6542 //
6543 case clang::Type::LValueReference:
6544 case clang::Type::RValueReference:
6545 {
6546 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6547 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006548 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006549
6550 if (pointee_clang_type.IsAggregateType ())
6551 {
6552 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6553 omit_empty_base_classes,
6554 child_indexes);
6555 }
6556 }
6557 break;
6558
6559 case clang::Type::Pointer:
6560 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006561 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006562
6563 if (pointee_clang_type.IsAggregateType ())
6564 {
6565 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6566 omit_empty_base_classes,
6567 child_indexes);
6568 }
6569 }
6570 break;
6571
6572 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006573 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006574 omit_empty_base_classes,
6575 child_indexes);
6576
6577 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006578 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006579 omit_empty_base_classes,
6580 child_indexes);
6581
6582 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006583 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006584 omit_empty_base_classes,
6585 child_indexes);
6586
6587 default:
6588 break;
6589 }
6590 }
6591 return 0;
6592}
6593
6594
6595// Get the index of the child of "clang_type" whose name matches. This function
6596// doesn't descend into the children, but only looks one level deep and name
6597// matches can include base class names.
6598
6599uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006600ClangASTContext::GetIndexOfChildWithName (lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00006601{
6602 if (type && name && name[0])
6603 {
6604 clang::QualType qual_type(GetCanonicalQualType(type));
6605
6606 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6607
6608 switch (type_class)
6609 {
6610 case clang::Type::Record:
6611 if (GetCompleteType(type))
6612 {
6613 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6614 const clang::RecordDecl *record_decl = record_type->getDecl();
6615
6616 assert(record_decl);
6617 uint32_t child_idx = 0;
6618
6619 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6620
6621 if (cxx_record_decl)
6622 {
6623 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6624 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6625 base_class != base_class_end;
6626 ++base_class)
6627 {
6628 // Skip empty base classes
6629 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6630 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6631 continue;
6632
Greg Claytona1e5dc82015-08-11 22:53:00 +00006633 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006634 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6635 if (base_class_type_name.compare (name) == 0)
6636 return child_idx;
6637 ++child_idx;
6638 }
6639 }
6640
6641 // Try and find a field that matches NAME
6642 clang::RecordDecl::field_iterator field, field_end;
6643 llvm::StringRef name_sref(name);
6644 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6645 field != field_end;
6646 ++field, ++child_idx)
6647 {
6648 if (field->getName().equals (name_sref))
6649 return child_idx;
6650 }
6651
6652 }
6653 break;
6654
6655 case clang::Type::ObjCObject:
6656 case clang::Type::ObjCInterface:
6657 if (GetCompleteType(type))
6658 {
6659 llvm::StringRef name_sref(name);
6660 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6661 assert (objc_class_type);
6662 if (objc_class_type)
6663 {
6664 uint32_t child_idx = 0;
6665 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6666
6667 if (class_interface_decl)
6668 {
6669 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6670 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6671
6672 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6673 {
6674 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6675
6676 if (ivar_decl->getName().equals (name_sref))
6677 {
6678 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6679 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6680 ++child_idx;
6681
6682 return child_idx;
6683 }
6684 }
6685
6686 if (superclass_interface_decl)
6687 {
6688 if (superclass_interface_decl->getName().equals (name_sref))
6689 return 0;
6690 }
6691 }
6692 }
6693 }
6694 break;
6695
6696 case clang::Type::ObjCObjectPointer:
6697 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006698 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006699 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6700 }
6701 break;
6702
6703 case clang::Type::ConstantArray:
6704 {
6705 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6706 // const uint64_t element_count = array->getSize().getLimitedValue();
6707 //
6708 // if (idx < element_count)
6709 // {
6710 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6711 //
6712 // char element_name[32];
6713 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6714 //
6715 // child_name.assign(element_name);
6716 // assert(field_type_info.first % 8 == 0);
6717 // child_byte_size = field_type_info.first / 8;
6718 // child_byte_offset = idx * child_byte_size;
6719 // return array->getElementType().getAsOpaquePtr();
6720 // }
6721 }
6722 break;
6723
6724 // case clang::Type::MemberPointerType:
6725 // {
6726 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6727 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6728 //
6729 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6730 // {
6731 // return GetIndexOfChildWithName (ast,
6732 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6733 // name);
6734 // }
6735 // }
6736 // break;
6737 //
6738 case clang::Type::LValueReference:
6739 case clang::Type::RValueReference:
6740 {
6741 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006742 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006743
6744 if (pointee_type.IsAggregateType ())
6745 {
6746 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6747 }
6748 }
6749 break;
6750
6751 case clang::Type::Pointer:
6752 {
6753 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006754 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006755
6756 if (pointee_type.IsAggregateType ())
6757 {
6758 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6759 }
6760 else
6761 {
6762 // if (parent_name)
6763 // {
6764 // child_name.assign(1, '*');
6765 // child_name += parent_name;
6766 // }
6767 //
6768 // // We have a pointer to an simple type
6769 // if (idx == 0)
6770 // {
6771 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6772 // assert(clang_type_info.first % 8 == 0);
6773 // child_byte_size = clang_type_info.first / 8;
6774 // child_byte_offset = 0;
6775 // return pointee_type.getAsOpaquePtr();
6776 // }
6777 }
6778 }
6779 break;
6780
6781 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006782 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006783
6784 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006785 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006786
6787 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006788 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006789
6790 default:
6791 break;
6792 }
6793 }
6794 return UINT32_MAX;
6795}
6796
6797
6798size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006799ClangASTContext::GetNumTemplateArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006800{
6801 if (!type)
6802 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006803
6804 clang::QualType qual_type (GetCanonicalQualType(type));
6805 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6806 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006807 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006808 case clang::Type::Record:
6809 if (GetCompleteType(type))
6810 {
6811 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6812 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006813 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006814 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6815 if (template_decl)
6816 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006817 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006818 }
6819 break;
6820
6821 case clang::Type::Typedef:
6822 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6823
6824 case clang::Type::Elaborated:
6825 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6826
6827 case clang::Type::Paren:
6828 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6829
6830 default:
6831 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006832 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006833
Greg Claytond8d4a572015-08-11 21:38:15 +00006834 return 0;
6835}
6836
Greg Claytona1e5dc82015-08-11 22:53:00 +00006837CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006838ClangASTContext::GetTemplateArgument (lldb::opaque_compiler_type_t type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006839{
6840 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006841 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006842
6843 clang::QualType qual_type (GetCanonicalQualType(type));
6844 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6845 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006846 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006847 case clang::Type::Record:
6848 if (GetCompleteType(type))
6849 {
6850 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6851 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006852 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006853 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6854 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006855 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006856 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6857 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006858 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006859 case clang::TemplateArgument::Null:
6860 kind = eTemplateArgumentKindNull;
6861 return CompilerType();
6862
6863 case clang::TemplateArgument::Type:
6864 kind = eTemplateArgumentKindType;
6865 return CompilerType(getASTContext(), template_arg.getAsType());
6866
6867 case clang::TemplateArgument::Declaration:
6868 kind = eTemplateArgumentKindDeclaration;
6869 return CompilerType();
6870
6871 case clang::TemplateArgument::Integral:
6872 kind = eTemplateArgumentKindIntegral;
6873 return CompilerType(getASTContext(), template_arg.getIntegralType());
6874
6875 case clang::TemplateArgument::Template:
6876 kind = eTemplateArgumentKindTemplate;
6877 return CompilerType();
6878
6879 case clang::TemplateArgument::TemplateExpansion:
6880 kind = eTemplateArgumentKindTemplateExpansion;
6881 return CompilerType();
6882
6883 case clang::TemplateArgument::Expression:
6884 kind = eTemplateArgumentKindExpression;
6885 return CompilerType();
6886
6887 case clang::TemplateArgument::Pack:
6888 kind = eTemplateArgumentKindPack;
6889 return CompilerType();
6890
6891 default:
6892 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6893 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006894 }
6895 }
6896 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006897 }
6898 break;
6899
6900 case clang::Type::Typedef:
6901 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6902
6903 case clang::Type::Elaborated:
6904 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6905
6906 case clang::Type::Paren:
6907 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6908
6909 default:
6910 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006911 }
6912 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006913 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006914}
6915
Enrico Granatac6bf2e22015-09-23 01:39:46 +00006916CompilerType
6917ClangASTContext::GetTypeForFormatters (void* type)
6918{
6919 if (type)
6920 return RemoveFastQualifiers(CompilerType(this, type));
6921 return CompilerType();
6922}
6923
Greg Claytond8d4a572015-08-11 21:38:15 +00006924static bool
6925IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6926{
6927 if (name == nullptr || name[0] == '\0')
6928 return false;
6929
6930#define OPERATOR_PREFIX "operator"
6931#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6932
6933 const char *post_op_name = nullptr;
6934
6935 bool no_space = true;
6936
6937 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6938 return false;
6939
6940 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6941
6942 if (post_op_name[0] == ' ')
6943 {
6944 post_op_name++;
6945 no_space = false;
6946 }
6947
6948#undef OPERATOR_PREFIX
6949#undef OPERATOR_PREFIX_LENGTH
6950
6951 // This is an operator, set the overloaded operator kind to invalid
6952 // in case this is a conversion operator...
6953 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6954
6955 switch (post_op_name[0])
6956 {
6957 default:
6958 if (no_space)
6959 return false;
6960 break;
6961 case 'n':
6962 if (no_space)
6963 return false;
6964 if (strcmp (post_op_name, "new") == 0)
6965 op_kind = clang::OO_New;
6966 else if (strcmp (post_op_name, "new[]") == 0)
6967 op_kind = clang::OO_Array_New;
6968 break;
6969
6970 case 'd':
6971 if (no_space)
6972 return false;
6973 if (strcmp (post_op_name, "delete") == 0)
6974 op_kind = clang::OO_Delete;
6975 else if (strcmp (post_op_name, "delete[]") == 0)
6976 op_kind = clang::OO_Array_Delete;
6977 break;
6978
6979 case '+':
6980 if (post_op_name[1] == '\0')
6981 op_kind = clang::OO_Plus;
6982 else if (post_op_name[2] == '\0')
6983 {
6984 if (post_op_name[1] == '=')
6985 op_kind = clang::OO_PlusEqual;
6986 else if (post_op_name[1] == '+')
6987 op_kind = clang::OO_PlusPlus;
6988 }
6989 break;
6990
6991 case '-':
6992 if (post_op_name[1] == '\0')
6993 op_kind = clang::OO_Minus;
6994 else if (post_op_name[2] == '\0')
6995 {
6996 switch (post_op_name[1])
6997 {
6998 case '=': op_kind = clang::OO_MinusEqual; break;
6999 case '-': op_kind = clang::OO_MinusMinus; break;
7000 case '>': op_kind = clang::OO_Arrow; break;
7001 }
7002 }
7003 else if (post_op_name[3] == '\0')
7004 {
7005 if (post_op_name[2] == '*')
7006 op_kind = clang::OO_ArrowStar; break;
7007 }
7008 break;
7009
7010 case '*':
7011 if (post_op_name[1] == '\0')
7012 op_kind = clang::OO_Star;
7013 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7014 op_kind = clang::OO_StarEqual;
7015 break;
7016
7017 case '/':
7018 if (post_op_name[1] == '\0')
7019 op_kind = clang::OO_Slash;
7020 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7021 op_kind = clang::OO_SlashEqual;
7022 break;
7023
7024 case '%':
7025 if (post_op_name[1] == '\0')
7026 op_kind = clang::OO_Percent;
7027 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7028 op_kind = clang::OO_PercentEqual;
7029 break;
7030
7031
7032 case '^':
7033 if (post_op_name[1] == '\0')
7034 op_kind = clang::OO_Caret;
7035 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7036 op_kind = clang::OO_CaretEqual;
7037 break;
7038
7039 case '&':
7040 if (post_op_name[1] == '\0')
7041 op_kind = clang::OO_Amp;
7042 else if (post_op_name[2] == '\0')
7043 {
7044 switch (post_op_name[1])
7045 {
7046 case '=': op_kind = clang::OO_AmpEqual; break;
7047 case '&': op_kind = clang::OO_AmpAmp; break;
7048 }
7049 }
7050 break;
7051
7052 case '|':
7053 if (post_op_name[1] == '\0')
7054 op_kind = clang::OO_Pipe;
7055 else if (post_op_name[2] == '\0')
7056 {
7057 switch (post_op_name[1])
7058 {
7059 case '=': op_kind = clang::OO_PipeEqual; break;
7060 case '|': op_kind = clang::OO_PipePipe; break;
7061 }
7062 }
7063 break;
7064
7065 case '~':
7066 if (post_op_name[1] == '\0')
7067 op_kind = clang::OO_Tilde;
7068 break;
7069
7070 case '!':
7071 if (post_op_name[1] == '\0')
7072 op_kind = clang::OO_Exclaim;
7073 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7074 op_kind = clang::OO_ExclaimEqual;
7075 break;
7076
7077 case '=':
7078 if (post_op_name[1] == '\0')
7079 op_kind = clang::OO_Equal;
7080 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7081 op_kind = clang::OO_EqualEqual;
7082 break;
7083
7084 case '<':
7085 if (post_op_name[1] == '\0')
7086 op_kind = clang::OO_Less;
7087 else if (post_op_name[2] == '\0')
7088 {
7089 switch (post_op_name[1])
7090 {
7091 case '<': op_kind = clang::OO_LessLess; break;
7092 case '=': op_kind = clang::OO_LessEqual; break;
7093 }
7094 }
7095 else if (post_op_name[3] == '\0')
7096 {
7097 if (post_op_name[2] == '=')
7098 op_kind = clang::OO_LessLessEqual;
7099 }
7100 break;
7101
7102 case '>':
7103 if (post_op_name[1] == '\0')
7104 op_kind = clang::OO_Greater;
7105 else if (post_op_name[2] == '\0')
7106 {
7107 switch (post_op_name[1])
7108 {
7109 case '>': op_kind = clang::OO_GreaterGreater; break;
7110 case '=': op_kind = clang::OO_GreaterEqual; break;
7111 }
7112 }
7113 else if (post_op_name[1] == '>' &&
7114 post_op_name[2] == '=' &&
7115 post_op_name[3] == '\0')
7116 {
7117 op_kind = clang::OO_GreaterGreaterEqual;
7118 }
7119 break;
7120
7121 case ',':
7122 if (post_op_name[1] == '\0')
7123 op_kind = clang::OO_Comma;
7124 break;
7125
7126 case '(':
7127 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7128 op_kind = clang::OO_Call;
7129 break;
7130
7131 case '[':
7132 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7133 op_kind = clang::OO_Subscript;
7134 break;
7135 }
7136
7137 return true;
7138}
7139
7140clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007141ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007142{
7143 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
7144 if (enutype)
7145 return enutype->getDecl();
7146 return NULL;
7147}
7148
7149clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007150ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007151{
7152 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7153 if (record_type)
7154 return record_type->getDecl();
7155 return nullptr;
7156}
7157
7158clang::CXXRecordDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007159ClangASTContext::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007160{
7161 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7162}
7163
7164clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007165ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007166{
7167 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7168 if (objc_class_type)
7169 return objc_class_type->getInterface();
7170 return nullptr;
7171}
7172
7173clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007174ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7175 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007176 AccessType access,
7177 uint32_t bitfield_bit_size)
7178{
7179 if (!type.IsValid() || !field_clang_type.IsValid())
7180 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007181 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007182 if (!ast)
7183 return nullptr;
7184 clang::ASTContext* clang_ast = ast->getASTContext();
7185
7186 clang::FieldDecl *field = nullptr;
7187
7188 clang::Expr *bit_width = nullptr;
7189 if (bitfield_bit_size != 0)
7190 {
7191 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7192 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7193 }
7194
7195 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7196 if (record_decl)
7197 {
7198 field = clang::FieldDecl::Create (*clang_ast,
7199 record_decl,
7200 clang::SourceLocation(),
7201 clang::SourceLocation(),
7202 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7203 GetQualType(field_clang_type), // Field type
7204 nullptr, // TInfo *
7205 bit_width, // BitWidth
7206 false, // Mutable
7207 clang::ICIS_NoInit); // HasInit
7208
7209 if (!name)
7210 {
7211 // Determine whether this field corresponds to an anonymous
7212 // struct or union.
7213 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7214 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7215 if (!Rec->getDeclName()) {
7216 Rec->setAnonymousStructOrUnion(true);
7217 field->setImplicit();
7218
7219 }
7220 }
7221 }
7222
7223 if (field)
7224 {
7225 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7226
7227 record_decl->addDecl(field);
7228
7229#ifdef LLDB_CONFIGURATION_DEBUG
7230 VerifyDecl(field);
7231#endif
7232 }
7233 }
7234 else
7235 {
7236 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7237
7238 if (class_interface_decl)
7239 {
7240 const bool is_synthesized = false;
7241
7242 field_clang_type.GetCompleteType();
7243
7244 field = clang::ObjCIvarDecl::Create (*clang_ast,
7245 class_interface_decl,
7246 clang::SourceLocation(),
7247 clang::SourceLocation(),
7248 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7249 GetQualType(field_clang_type), // Field type
7250 nullptr, // TypeSourceInfo *
7251 ConvertAccessTypeToObjCIvarAccessControl (access),
7252 bit_width,
7253 is_synthesized);
7254
7255 if (field)
7256 {
7257 class_interface_decl->addDecl(field);
7258
7259#ifdef LLDB_CONFIGURATION_DEBUG
7260 VerifyDecl(field);
7261#endif
7262 }
7263 }
7264 }
7265 return field;
7266}
7267
7268void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007269ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007270{
Greg Claytonf73034f2015-09-08 18:15:05 +00007271 if (!type)
7272 return;
7273
7274 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007275 if (!ast)
7276 return;
7277
7278 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7279
7280 if (!record_decl)
7281 return;
7282
7283 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7284
7285 IndirectFieldVector indirect_fields;
7286 clang::RecordDecl::field_iterator field_pos;
7287 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7288 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7289 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7290 {
7291 if (field_pos->isAnonymousStructOrUnion())
7292 {
7293 clang::QualType field_qual_type = field_pos->getType();
7294
7295 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7296
7297 if (!field_record_type)
7298 continue;
7299
7300 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7301
7302 if (!field_record_decl)
7303 continue;
7304
7305 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7306 di != de;
7307 ++di)
7308 {
7309 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7310 {
7311 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7312 chain[0] = *field_pos;
7313 chain[1] = nested_field_decl;
7314 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7315 record_decl,
7316 clang::SourceLocation(),
7317 nested_field_decl->getIdentifier(),
7318 nested_field_decl->getType(),
7319 chain,
7320 2);
7321
7322 indirect_field->setImplicit();
7323
7324 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7325 nested_field_decl->getAccess()));
7326
7327 indirect_fields.push_back(indirect_field);
7328 }
7329 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7330 {
7331 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7332 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7333 chain[0] = *field_pos;
7334
7335 int chain_index = 1;
7336 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7337 nce = nested_indirect_field_decl->chain_end();
7338 nci < nce;
7339 ++nci)
7340 {
7341 chain[chain_index] = *nci;
7342 chain_index++;
7343 }
7344
7345 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7346 record_decl,
7347 clang::SourceLocation(),
7348 nested_indirect_field_decl->getIdentifier(),
7349 nested_indirect_field_decl->getType(),
7350 chain,
7351 nested_chain_size + 1);
7352
7353 indirect_field->setImplicit();
7354
7355 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7356 nested_indirect_field_decl->getAccess()));
7357
7358 indirect_fields.push_back(indirect_field);
7359 }
7360 }
7361 }
7362 }
7363
7364 // Check the last field to see if it has an incomplete array type as its
7365 // last member and if it does, the tell the record decl about it
7366 if (last_field_pos != field_end_pos)
7367 {
7368 if (last_field_pos->getType()->isIncompleteArrayType())
7369 record_decl->hasFlexibleArrayMember();
7370 }
7371
7372 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7373 ifi < ife;
7374 ++ifi)
7375 {
7376 record_decl->addDecl(*ifi);
7377 }
7378}
7379
7380void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007381ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007382{
Greg Claytonf73034f2015-09-08 18:15:05 +00007383 if (type)
7384 {
7385 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7386 if (ast)
7387 {
7388 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007389
Greg Claytonf73034f2015-09-08 18:15:05 +00007390 if (!record_decl)
7391 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007392
Greg Claytonf73034f2015-09-08 18:15:05 +00007393 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7394 }
7395 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007396}
7397
7398clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007399ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7400 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007401 AccessType access)
7402{
7403 clang::VarDecl *var_decl = nullptr;
7404
7405 if (!type.IsValid() || !var_type.IsValid())
7406 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007407 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007408 if (!ast)
7409 return nullptr;
7410
7411 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7412 if (record_decl)
7413 {
7414 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7415 record_decl, // DeclContext *
7416 clang::SourceLocation(), // clang::SourceLocation StartLoc
7417 clang::SourceLocation(), // clang::SourceLocation IdLoc
7418 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7419 GetQualType(var_type), // Variable clang::QualType
7420 nullptr, // TypeSourceInfo *
7421 clang::SC_Static); // StorageClass
7422 if (var_decl)
7423 {
7424 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7425 record_decl->addDecl(var_decl);
7426
7427#ifdef LLDB_CONFIGURATION_DEBUG
7428 VerifyDecl(var_decl);
7429#endif
7430 }
7431 }
7432 return var_decl;
7433}
7434
7435
7436clang::CXXMethodDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007437ClangASTContext::AddMethodToCXXRecordType (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007438 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007439 lldb::AccessType access,
7440 bool is_virtual,
7441 bool is_static,
7442 bool is_inline,
7443 bool is_explicit,
7444 bool is_attr_used,
7445 bool is_artificial)
7446{
7447 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7448 return nullptr;
7449
7450 clang::QualType record_qual_type(GetCanonicalQualType(type));
7451
7452 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7453
7454 if (cxx_record_decl == nullptr)
7455 return nullptr;
7456
7457 clang::QualType method_qual_type (GetQualType(method_clang_type));
7458
7459 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7460
7461 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7462
7463 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7464
7465 if (function_type == nullptr)
7466 return nullptr;
7467
7468 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7469
7470 if (!method_function_prototype)
7471 return nullptr;
7472
7473 unsigned int num_params = method_function_prototype->getNumParams();
7474
7475 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7476 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7477
7478 if (is_artificial)
7479 return nullptr; // skip everything artificial
7480
7481 if (name[0] == '~')
7482 {
7483 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7484 cxx_record_decl,
7485 clang::SourceLocation(),
7486 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7487 method_qual_type,
7488 nullptr,
7489 is_inline,
7490 is_artificial);
7491 cxx_method_decl = cxx_dtor_decl;
7492 }
7493 else if (decl_name == cxx_record_decl->getDeclName())
7494 {
7495 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7496 cxx_record_decl,
7497 clang::SourceLocation(),
7498 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7499 method_qual_type,
7500 nullptr, // TypeSourceInfo *
7501 is_explicit,
7502 is_inline,
7503 is_artificial,
7504 false /*is_constexpr*/);
7505 cxx_method_decl = cxx_ctor_decl;
7506 }
7507 else
7508 {
7509 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7510 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7511
7512 if (IsOperator (name, op_kind))
7513 {
7514 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7515 {
7516 // Check the number of operator parameters. Sometimes we have
7517 // seen bad DWARF that doesn't correctly describe operators and
7518 // if we try to create a method and add it to the class, clang
7519 // will assert and crash, so we need to make sure things are
7520 // acceptable.
7521 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7522 return nullptr;
7523 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7524 cxx_record_decl,
7525 clang::SourceLocation(),
7526 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7527 method_qual_type,
7528 nullptr, // TypeSourceInfo *
7529 SC,
7530 is_inline,
7531 false /*is_constexpr*/,
7532 clang::SourceLocation());
7533 }
7534 else if (num_params == 0)
7535 {
7536 // Conversion operators don't take params...
7537 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7538 cxx_record_decl,
7539 clang::SourceLocation(),
7540 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7541 method_qual_type,
7542 nullptr, // TypeSourceInfo *
7543 is_inline,
7544 is_explicit,
7545 false /*is_constexpr*/,
7546 clang::SourceLocation());
7547 }
7548 }
7549
7550 if (cxx_method_decl == nullptr)
7551 {
7552 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7553 cxx_record_decl,
7554 clang::SourceLocation(),
7555 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7556 method_qual_type,
7557 nullptr, // TypeSourceInfo *
7558 SC,
7559 is_inline,
7560 false /*is_constexpr*/,
7561 clang::SourceLocation());
7562 }
7563 }
7564
7565 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7566
7567 cxx_method_decl->setAccess (access_specifier);
7568 cxx_method_decl->setVirtualAsWritten (is_virtual);
7569
7570 if (is_attr_used)
7571 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7572
7573 // Populate the method decl with parameter decls
7574
7575 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7576
7577 for (unsigned param_index = 0;
7578 param_index < num_params;
7579 ++param_index)
7580 {
7581 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7582 cxx_method_decl,
7583 clang::SourceLocation(),
7584 clang::SourceLocation(),
7585 nullptr, // anonymous
7586 method_function_prototype->getParamType(param_index),
7587 nullptr,
7588 clang::SC_None,
7589 nullptr));
7590 }
7591
7592 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7593
7594 cxx_record_decl->addDecl (cxx_method_decl);
7595
7596 // Sometimes the debug info will mention a constructor (default/copy/move),
7597 // destructor, or assignment operator (copy/move) but there won't be any
7598 // version of this in the code. So we check if the function was artificially
7599 // generated and if it is trivial and this lets the compiler/backend know
7600 // that it can inline the IR for these when it needs to and we can avoid a
7601 // "missing function" error when running expressions.
7602
7603 if (is_artificial)
7604 {
7605 if (cxx_ctor_decl &&
7606 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7607 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7608 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7609 {
7610 cxx_ctor_decl->setDefaulted();
7611 cxx_ctor_decl->setTrivial(true);
7612 }
7613 else if (cxx_dtor_decl)
7614 {
7615 if (cxx_record_decl->hasTrivialDestructor())
7616 {
7617 cxx_dtor_decl->setDefaulted();
7618 cxx_dtor_decl->setTrivial(true);
7619 }
7620 }
7621 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7622 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7623 {
7624 cxx_method_decl->setDefaulted();
7625 cxx_method_decl->setTrivial(true);
7626 }
7627 }
7628
7629#ifdef LLDB_CONFIGURATION_DEBUG
7630 VerifyDecl(cxx_method_decl);
7631#endif
7632
7633 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7634 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7635 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7636 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7637 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7638 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7639 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7640 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7641 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7642 return cxx_method_decl;
7643}
7644
7645
7646#pragma mark C++ Base Classes
7647
7648clang::CXXBaseSpecifier *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007649ClangASTContext::CreateBaseClassSpecifier (lldb::opaque_compiler_type_t type, AccessType access, bool is_virtual, bool base_of_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007650{
7651 if (type)
7652 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7653 is_virtual,
7654 base_of_class,
7655 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7656 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7657 clang::SourceLocation());
7658 return nullptr;
7659}
7660
7661void
7662ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7663{
7664 for (unsigned i=0; i<num_base_classes; ++i)
7665 {
7666 delete base_classes[i];
7667 base_classes[i] = nullptr;
7668 }
7669}
7670
7671bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007672ClangASTContext::SetBaseClassesForClassType (lldb::opaque_compiler_type_t type, clang::CXXBaseSpecifier const * const *base_classes,
Greg Claytond8d4a572015-08-11 21:38:15 +00007673 unsigned num_base_classes)
7674{
7675 if (type)
7676 {
7677 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7678 if (cxx_record_decl)
7679 {
7680 cxx_record_decl->setBases(base_classes, num_base_classes);
7681 return true;
7682 }
7683 }
7684 return false;
7685}
7686
7687bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007688ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007689{
Greg Claytonf73034f2015-09-08 18:15:05 +00007690 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007691 if (!ast)
7692 return false;
7693 clang::ASTContext* clang_ast = ast->getASTContext();
7694
7695 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7696 {
7697 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7698 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7699 if (class_interface_decl && super_interface_decl)
7700 {
7701 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7702 return true;
7703 }
7704 }
7705 return false;
7706}
7707
7708bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007709ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007710 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007711 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007712 clang::ObjCIvarDecl *ivar_decl,
7713 const char *property_setter_name,
7714 const char *property_getter_name,
7715 uint32_t property_attributes,
7716 ClangASTMetadata *metadata)
7717{
7718 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7719 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007720 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007721 if (!ast)
7722 return false;
7723 clang::ASTContext* clang_ast = ast->getASTContext();
7724
7725 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7726
7727 if (class_interface_decl)
7728 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007729 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007730
7731 if (property_clang_type.IsValid())
7732 property_clang_type_to_access = property_clang_type;
7733 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007734 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007735
7736 if (class_interface_decl && property_clang_type_to_access.IsValid())
7737 {
7738 clang::TypeSourceInfo *prop_type_source;
7739 if (ivar_decl)
7740 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7741 else
7742 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7743
7744 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7745 class_interface_decl,
7746 clang::SourceLocation(), // Source Location
7747 &clang_ast->Idents.get(property_name),
7748 clang::SourceLocation(), //Source Location for AT
7749 clang::SourceLocation(), //Source location for (
7750 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7751 prop_type_source);
7752
7753 if (property_decl)
7754 {
7755 if (metadata)
7756 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7757
7758 class_interface_decl->addDecl (property_decl);
7759
7760 clang::Selector setter_sel, getter_sel;
7761
7762 if (property_setter_name != nullptr)
7763 {
7764 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7765 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7766 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7767 }
7768 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7769 {
7770 std::string setter_sel_string("set");
7771 setter_sel_string.push_back(::toupper(property_name[0]));
7772 setter_sel_string.append(&property_name[1]);
7773 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7774 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7775 }
7776 property_decl->setSetterName(setter_sel);
7777 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7778
7779 if (property_getter_name != nullptr)
7780 {
7781 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7782 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7783 }
7784 else
7785 {
7786 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7787 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7788 }
7789 property_decl->setGetterName(getter_sel);
7790 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7791
7792 if (ivar_decl)
7793 property_decl->setPropertyIvarDecl (ivar_decl);
7794
7795 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7796 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7797 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7798 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7799 if (property_attributes & DW_APPLE_PROPERTY_assign)
7800 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7801 if (property_attributes & DW_APPLE_PROPERTY_retain)
7802 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7803 if (property_attributes & DW_APPLE_PROPERTY_copy)
7804 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7805 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7806 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7807
7808 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7809 {
7810 const bool isInstance = true;
7811 const bool isVariadic = false;
7812 const bool isSynthesized = false;
7813 const bool isImplicitlyDeclared = true;
7814 const bool isDefined = false;
7815 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7816 const bool HasRelatedResultType = false;
7817
7818 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7819 clang::SourceLocation(),
7820 clang::SourceLocation(),
7821 getter_sel,
7822 GetQualType(property_clang_type_to_access),
7823 nullptr,
7824 class_interface_decl,
7825 isInstance,
7826 isVariadic,
7827 isSynthesized,
7828 isImplicitlyDeclared,
7829 isDefined,
7830 impControl,
7831 HasRelatedResultType);
7832
7833 if (getter && metadata)
7834 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7835
7836 if (getter)
7837 {
7838 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7839
7840 class_interface_decl->addDecl(getter);
7841 }
7842 }
7843
7844 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7845 {
7846 clang::QualType result_type = clang_ast->VoidTy;
7847
7848 const bool isInstance = true;
7849 const bool isVariadic = false;
7850 const bool isSynthesized = false;
7851 const bool isImplicitlyDeclared = true;
7852 const bool isDefined = false;
7853 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7854 const bool HasRelatedResultType = false;
7855
7856 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7857 clang::SourceLocation(),
7858 clang::SourceLocation(),
7859 setter_sel,
7860 result_type,
7861 nullptr,
7862 class_interface_decl,
7863 isInstance,
7864 isVariadic,
7865 isSynthesized,
7866 isImplicitlyDeclared,
7867 isDefined,
7868 impControl,
7869 HasRelatedResultType);
7870
7871 if (setter && metadata)
7872 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7873
7874 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7875
7876 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7877 setter,
7878 clang::SourceLocation(),
7879 clang::SourceLocation(),
7880 nullptr, // anonymous
7881 GetQualType(property_clang_type_to_access),
7882 nullptr,
7883 clang::SC_Auto,
7884 nullptr));
7885
7886 if (setter)
7887 {
7888 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7889
7890 class_interface_decl->addDecl(setter);
7891 }
7892 }
7893
7894 return true;
7895 }
7896 }
7897 }
7898 return false;
7899}
7900
7901bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007902ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007903{
7904 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7905 if (class_interface_decl)
7906 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7907 return false;
7908}
7909
7910
7911clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007912ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007913 const char *name, // the full symbol name as seen in the symbol table (lldb::opaque_compiler_type_t type, "-[NString stringWithCString:]")
Greg Claytona1e5dc82015-08-11 22:53:00 +00007914 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007915 lldb::AccessType access,
7916 bool is_artificial)
7917{
7918 if (!type || !method_clang_type.IsValid())
7919 return nullptr;
7920
7921 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7922
7923 if (class_interface_decl == nullptr)
7924 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007925 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7926 if (lldb_ast == nullptr)
7927 return nullptr;
7928 clang::ASTContext *ast = lldb_ast->getASTContext();
7929
Greg Claytond8d4a572015-08-11 21:38:15 +00007930 const char *selector_start = ::strchr (name, ' ');
7931 if (selector_start == nullptr)
7932 return nullptr;
7933
7934 selector_start++;
7935 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7936
7937 size_t len = 0;
7938 const char *start;
7939 //printf ("name = '%s'\n", name);
7940
7941 unsigned num_selectors_with_args = 0;
7942 for (start = selector_start;
7943 start && *start != '\0' && *start != ']';
7944 start += len)
7945 {
7946 len = ::strcspn(start, ":]");
7947 bool has_arg = (start[len] == ':');
7948 if (has_arg)
7949 ++num_selectors_with_args;
7950 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7951 if (has_arg)
7952 len += 1;
7953 }
7954
7955
7956 if (selector_idents.size() == 0)
7957 return nullptr;
7958
7959 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
7960 selector_idents.data());
7961
7962 clang::QualType method_qual_type (GetQualType(method_clang_type));
7963
7964 // Populate the method decl with parameter decls
7965 const clang::Type *method_type(method_qual_type.getTypePtr());
7966
7967 if (method_type == nullptr)
7968 return nullptr;
7969
7970 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
7971
7972 if (!method_function_prototype)
7973 return nullptr;
7974
7975
7976 bool is_variadic = false;
7977 bool is_synthesized = false;
7978 bool is_defined = false;
7979 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
7980
7981 const unsigned num_args = method_function_prototype->getNumParams();
7982
7983 if (num_args != num_selectors_with_args)
7984 return nullptr; // some debug information is corrupt. We are not going to deal with it.
7985
7986 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
7987 clang::SourceLocation(), // beginLoc,
7988 clang::SourceLocation(), // endLoc,
7989 method_selector,
7990 method_function_prototype->getReturnType(),
7991 nullptr, // TypeSourceInfo *ResultTInfo,
7992 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
7993 name[0] == '-',
7994 is_variadic,
7995 is_synthesized,
7996 true, // is_implicitly_declared; we force this to true because we don't have source locations
7997 is_defined,
7998 imp_control,
7999 false /*has_related_result_type*/);
8000
8001
8002 if (objc_method_decl == nullptr)
8003 return nullptr;
8004
8005 if (num_args > 0)
8006 {
8007 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8008
8009 for (unsigned param_index = 0; param_index < num_args; ++param_index)
8010 {
8011 params.push_back (clang::ParmVarDecl::Create (*ast,
8012 objc_method_decl,
8013 clang::SourceLocation(),
8014 clang::SourceLocation(),
8015 nullptr, // anonymous
8016 method_function_prototype->getParamType(param_index),
8017 nullptr,
8018 clang::SC_Auto,
8019 nullptr));
8020 }
8021
8022 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8023 }
8024
8025 class_interface_decl->addDecl (objc_method_decl);
8026
8027#ifdef LLDB_CONFIGURATION_DEBUG
8028 VerifyDecl(objc_method_decl);
8029#endif
8030
8031 return objc_method_decl;
8032}
8033
8034bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008035ClangASTContext::SetHasExternalStorage (lldb::opaque_compiler_type_t type, bool has_extern)
Greg Claytond8d4a572015-08-11 21:38:15 +00008036{
8037 if (!type)
8038 return false;
8039
8040 clang::QualType qual_type (GetCanonicalQualType(type));
8041
8042 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8043 switch (type_class)
8044 {
8045 case clang::Type::Record:
8046 {
8047 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8048 if (cxx_record_decl)
8049 {
8050 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
8051 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
8052 return true;
8053 }
8054 }
8055 break;
8056
8057 case clang::Type::Enum:
8058 {
8059 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8060 if (enum_decl)
8061 {
8062 enum_decl->setHasExternalLexicalStorage (has_extern);
8063 enum_decl->setHasExternalVisibleStorage (has_extern);
8064 return true;
8065 }
8066 }
8067 break;
8068
8069 case clang::Type::ObjCObject:
8070 case clang::Type::ObjCInterface:
8071 {
8072 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8073 assert (objc_class_type);
8074 if (objc_class_type)
8075 {
8076 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8077
8078 if (class_interface_decl)
8079 {
8080 class_interface_decl->setHasExternalLexicalStorage (has_extern);
8081 class_interface_decl->setHasExternalVisibleStorage (has_extern);
8082 return true;
8083 }
8084 }
8085 }
8086 break;
8087
8088 case clang::Type::Typedef:
8089 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
8090
8091 case clang::Type::Elaborated:
8092 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
8093
8094 case clang::Type::Paren:
8095 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
8096
8097 default:
8098 break;
8099 }
8100 return false;
8101}
8102
8103
8104#pragma mark TagDecl
8105
8106bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008107ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008108{
8109 if (type)
8110 {
8111
8112 clang::QualType qual_type (GetQualType(type));
8113 const clang::Type *t = qual_type.getTypePtr();
8114 if (t)
8115 {
8116 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(t);
8117 if (tag_type)
8118 {
8119 clang::TagDecl *tag_decl = tag_type->getDecl();
8120 if (tag_decl)
8121 {
8122 tag_decl->startDefinition();
8123 return true;
8124 }
8125 }
8126
8127 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(t);
8128 if (object_type)
8129 {
8130 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8131 if (interface_decl)
8132 {
8133 interface_decl->startDefinition();
8134 return true;
8135 }
8136 }
8137 }
8138 }
8139 return false;
8140}
8141
8142bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008143ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008144{
8145 if (type)
8146 {
8147 clang::QualType qual_type (GetQualType(type));
8148 if (qual_type.isNull())
8149 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00008150 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8151 if (lldb_ast == nullptr)
8152 return false;
8153 clang::ASTContext *ast = lldb_ast->getASTContext();
8154
Greg Claytond8d4a572015-08-11 21:38:15 +00008155 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8156
8157 if (cxx_record_decl)
8158 {
8159 cxx_record_decl->completeDefinition();
8160
8161 return true;
8162 }
8163
8164 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(qual_type.getTypePtr());
8165
8166 if (enutype)
8167 {
8168 clang::EnumDecl *enum_decl = enutype->getDecl();
8169
8170 if (enum_decl)
8171 {
8172 /// TODO This really needs to be fixed.
8173
8174 unsigned NumPositiveBits = 1;
8175 unsigned NumNegativeBits = 0;
8176
8177 clang::QualType promotion_qual_type;
8178 // If the enum integer type is less than an integer in bit width,
8179 // then we must promote it to an integer size.
8180 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8181 {
8182 if (enum_decl->getIntegerType()->isSignedIntegerType())
8183 promotion_qual_type = ast->IntTy;
8184 else
8185 promotion_qual_type = ast->UnsignedIntTy;
8186 }
8187 else
8188 promotion_qual_type = enum_decl->getIntegerType();
8189
8190 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
8191 return true;
8192 }
8193 }
8194 }
8195 return false;
8196}
8197
Greg Claytond8d4a572015-08-11 21:38:15 +00008198bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008199ClangASTContext::AddEnumerationValueToEnumerationType (lldb::opaque_compiler_type_t type,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008200 const CompilerType &enumerator_clang_type,
8201 const Declaration &decl,
8202 const char *name,
8203 int64_t enum_value,
8204 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008205{
8206 if (type && enumerator_clang_type.IsValid() && name && name[0])
8207 {
8208 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8209
8210 bool is_signed = false;
8211 enumerator_clang_type.IsIntegerType (is_signed);
8212 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8213 if (clang_type)
8214 {
8215 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8216
8217 if (enutype)
8218 {
8219 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8220 enum_llvm_apsint = enum_value;
8221 clang::EnumConstantDecl *enumerator_decl =
8222 clang::EnumConstantDecl::Create (*getASTContext(),
8223 enutype->getDecl(),
8224 clang::SourceLocation(),
8225 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8226 GetQualType(enumerator_clang_type),
8227 nullptr,
8228 enum_llvm_apsint);
8229
8230 if (enumerator_decl)
8231 {
8232 enutype->getDecl()->addDecl(enumerator_decl);
8233
8234#ifdef LLDB_CONFIGURATION_DEBUG
8235 VerifyDecl(enumerator_decl);
8236#endif
8237
8238 return true;
8239 }
8240 }
8241 }
8242 }
8243 return false;
8244}
8245
Greg Claytona1e5dc82015-08-11 22:53:00 +00008246CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008247ClangASTContext::GetEnumerationIntegerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008248{
8249 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8250 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8251 if (clang_type)
8252 {
8253 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8254 if (enutype)
8255 {
8256 clang::EnumDecl *enum_decl = enutype->getDecl();
8257 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008258 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008259 }
8260 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008261 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008262}
8263
Greg Claytona1e5dc82015-08-11 22:53:00 +00008264CompilerType
8265ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008266{
8267 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8268 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008269 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008270 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008271 return CompilerType();
8272 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008273 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8274 GetQualType(type).getTypePtr()));
8275 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008276 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008277}
8278
8279
8280size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008281ClangASTContext::ConvertStringToFloatValue (lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008282{
8283 if (type)
8284 {
8285 clang::QualType qual_type (GetCanonicalQualType(type));
8286 uint32_t count = 0;
8287 bool is_complex = false;
8288 if (IsFloatingPointType (type, count, is_complex))
8289 {
8290 // TODO: handle complex and vector types
8291 if (count != 1)
8292 return false;
8293
8294 llvm::StringRef s_sref(s);
8295 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8296
8297 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8298 const uint64_t byte_size = bit_size / 8;
8299 if (dst_size >= byte_size)
8300 {
8301 if (bit_size == sizeof(float)*8)
8302 {
8303 float float32 = ap_float.convertToFloat();
8304 ::memcpy (dst, &float32, byte_size);
8305 return byte_size;
8306 }
8307 else if (bit_size >= 64)
8308 {
8309 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8310 ::memcpy (dst, ap_int.getRawData(), byte_size);
8311 return byte_size;
8312 }
8313 }
8314 }
8315 }
8316 return 0;
8317}
8318
8319
8320
8321//----------------------------------------------------------------------
8322// Dumping types
8323//----------------------------------------------------------------------
8324#define DEPTH_INCREMENT 2
8325
8326void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008327ClangASTContext::DumpValue (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00008328 Stream *s,
8329 lldb::Format format,
8330 const lldb_private::DataExtractor &data,
8331 lldb::offset_t data_byte_offset,
8332 size_t data_byte_size,
8333 uint32_t bitfield_bit_size,
8334 uint32_t bitfield_bit_offset,
8335 bool show_types,
8336 bool show_summary,
8337 bool verbose,
8338 uint32_t depth)
8339{
8340 if (!type)
8341 return;
8342
8343 clang::QualType qual_type(GetQualType(type));
8344 switch (qual_type->getTypeClass())
8345 {
8346 case clang::Type::Record:
8347 if (GetCompleteType(type))
8348 {
8349 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8350 const clang::RecordDecl *record_decl = record_type->getDecl();
8351 assert(record_decl);
8352 uint32_t field_bit_offset = 0;
8353 uint32_t field_byte_offset = 0;
8354 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8355 uint32_t child_idx = 0;
8356
8357 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8358 if (cxx_record_decl)
8359 {
8360 // We might have base classes to print out first
8361 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8362 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8363 base_class != base_class_end;
8364 ++base_class)
8365 {
8366 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8367
8368 // Skip empty base classes
8369 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8370 continue;
8371
8372 if (base_class->isVirtual())
8373 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8374 else
8375 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8376 field_byte_offset = field_bit_offset / 8;
8377 assert (field_bit_offset % 8 == 0);
8378 if (child_idx == 0)
8379 s->PutChar('{');
8380 else
8381 s->PutChar(',');
8382
8383 clang::QualType base_class_qual_type = base_class->getType();
8384 std::string base_class_type_name(base_class_qual_type.getAsString());
8385
8386 // Indent and print the base class type name
8387 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8388
8389 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8390
8391 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008392 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008393 base_clang_type.DumpValue (exe_ctx,
8394 s, // Stream to dump to
8395 base_clang_type.GetFormat(), // The format with which to display the member
8396 data, // Data buffer containing all bytes for this type
8397 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8398 base_class_type_info.Width / 8, // Size of this type in bytes
8399 0, // Bitfield bit size
8400 0, // Bitfield bit offset
8401 show_types, // Boolean indicating if we should show the variable types
8402 show_summary, // Boolean indicating if we should show a summary for the current type
8403 verbose, // Verbose output?
8404 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8405
8406 ++child_idx;
8407 }
8408 }
8409 uint32_t field_idx = 0;
8410 clang::RecordDecl::field_iterator field, field_end;
8411 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8412 {
8413 // Print the starting squiggly bracket (if this is the
8414 // first member) or comma (for member 2 and beyond) for
8415 // the struct/union/class member.
8416 if (child_idx == 0)
8417 s->PutChar('{');
8418 else
8419 s->PutChar(',');
8420
8421 // Indent
8422 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8423
8424 clang::QualType field_type = field->getType();
8425 // Print the member type if requested
8426 // Figure out the type byte size (field_type_info.first) and
8427 // alignment (field_type_info.second) from the AST context.
8428 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8429 assert(field_idx < record_layout.getFieldCount());
8430 // Figure out the field offset within the current struct/union/class type
8431 field_bit_offset = record_layout.getFieldOffset (field_idx);
8432 field_byte_offset = field_bit_offset / 8;
8433 uint32_t field_bitfield_bit_size = 0;
8434 uint32_t field_bitfield_bit_offset = 0;
8435 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8436 field_bitfield_bit_offset = field_bit_offset % 8;
8437
8438 if (show_types)
8439 {
8440 std::string field_type_name(field_type.getAsString());
8441 if (field_bitfield_bit_size > 0)
8442 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8443 else
8444 s->Printf("(%s) ", field_type_name.c_str());
8445 }
8446 // Print the member name and equal sign
8447 s->Printf("%s = ", field->getNameAsString().c_str());
8448
8449
8450 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008451 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008452 field_clang_type.DumpValue (exe_ctx,
8453 s, // Stream to dump to
8454 field_clang_type.GetFormat(), // The format with which to display the member
8455 data, // Data buffer containing all bytes for this type
8456 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8457 field_type_info.Width / 8, // Size of this type in bytes
8458 field_bitfield_bit_size, // Bitfield bit size
8459 field_bitfield_bit_offset, // Bitfield bit offset
8460 show_types, // Boolean indicating if we should show the variable types
8461 show_summary, // Boolean indicating if we should show a summary for the current type
8462 verbose, // Verbose output?
8463 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8464 }
8465
8466 // Indent the trailing squiggly bracket
8467 if (child_idx > 0)
8468 s->Printf("\n%*s}", depth, "");
8469 }
8470 return;
8471
8472 case clang::Type::Enum:
8473 if (GetCompleteType(type))
8474 {
8475 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8476 const clang::EnumDecl *enum_decl = enutype->getDecl();
8477 assert(enum_decl);
8478 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8479 lldb::offset_t offset = data_byte_offset;
8480 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8481 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8482 {
8483 if (enum_pos->getInitVal() == enum_value)
8484 {
8485 s->Printf("%s", enum_pos->getNameAsString().c_str());
8486 return;
8487 }
8488 }
8489 // If we have gotten here we didn't get find the enumerator in the
8490 // enum decl, so just print the integer.
8491 s->Printf("%" PRIi64, enum_value);
8492 }
8493 return;
8494
8495 case clang::Type::ConstantArray:
8496 {
8497 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8498 bool is_array_of_characters = false;
8499 clang::QualType element_qual_type = array->getElementType();
8500
8501 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8502 if (canonical_type)
8503 is_array_of_characters = canonical_type->isCharType();
8504
8505 const uint64_t element_count = array->getSize().getLimitedValue();
8506
8507 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8508
8509 uint32_t element_idx = 0;
8510 uint32_t element_offset = 0;
8511 uint64_t element_byte_size = field_type_info.Width / 8;
8512 uint32_t element_stride = element_byte_size;
8513
8514 if (is_array_of_characters)
8515 {
8516 s->PutChar('"');
8517 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8518 s->PutChar('"');
8519 return;
8520 }
8521 else
8522 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008523 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008524 lldb::Format element_format = element_clang_type.GetFormat();
8525
8526 for (element_idx = 0; element_idx < element_count; ++element_idx)
8527 {
8528 // Print the starting squiggly bracket (if this is the
8529 // first member) or comman (for member 2 and beyong) for
8530 // the struct/union/class member.
8531 if (element_idx == 0)
8532 s->PutChar('{');
8533 else
8534 s->PutChar(',');
8535
8536 // Indent and print the index
8537 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8538
8539 // Figure out the field offset within the current struct/union/class type
8540 element_offset = element_idx * element_stride;
8541
8542 // Dump the value of the member
8543 element_clang_type.DumpValue (exe_ctx,
8544 s, // Stream to dump to
8545 element_format, // The format with which to display the element
8546 data, // Data buffer containing all bytes for this type
8547 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8548 element_byte_size, // Size of this type in bytes
8549 0, // Bitfield bit size
8550 0, // Bitfield bit offset
8551 show_types, // Boolean indicating if we should show the variable types
8552 show_summary, // Boolean indicating if we should show a summary for the current type
8553 verbose, // Verbose output?
8554 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8555 }
8556
8557 // Indent the trailing squiggly bracket
8558 if (element_idx > 0)
8559 s->Printf("\n%*s}", depth, "");
8560 }
8561 }
8562 return;
8563
8564 case clang::Type::Typedef:
8565 {
8566 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8567
Greg Claytona1e5dc82015-08-11 22:53:00 +00008568 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008569 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8570 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8571 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8572
8573 return typedef_clang_type.DumpValue (exe_ctx,
8574 s, // Stream to dump to
8575 typedef_format, // The format with which to display the element
8576 data, // Data buffer containing all bytes for this type
8577 data_byte_offset, // Offset into "data" where to grab value from
8578 typedef_byte_size, // Size of this type in bytes
8579 bitfield_bit_size, // Bitfield bit size
8580 bitfield_bit_offset,// Bitfield bit offset
8581 show_types, // Boolean indicating if we should show the variable types
8582 show_summary, // Boolean indicating if we should show a summary for the current type
8583 verbose, // Verbose output?
8584 depth); // Scope depth for any types that have children
8585 }
8586 break;
8587
8588 case clang::Type::Elaborated:
8589 {
8590 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008591 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008592 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8593 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8594 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8595
8596 return elaborated_clang_type.DumpValue (exe_ctx,
8597 s, // Stream to dump to
8598 elaborated_format, // The format with which to display the element
8599 data, // Data buffer containing all bytes for this type
8600 data_byte_offset, // Offset into "data" where to grab value from
8601 elaborated_byte_size, // Size of this type in bytes
8602 bitfield_bit_size, // Bitfield bit size
8603 bitfield_bit_offset,// Bitfield bit offset
8604 show_types, // Boolean indicating if we should show the variable types
8605 show_summary, // Boolean indicating if we should show a summary for the current type
8606 verbose, // Verbose output?
8607 depth); // Scope depth for any types that have children
8608 }
8609 break;
8610
8611 case clang::Type::Paren:
8612 {
8613 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008614 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008615
8616 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8617 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8618 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8619
8620 return desugar_clang_type.DumpValue (exe_ctx,
8621 s, // Stream to dump to
8622 desugar_format, // The format with which to display the element
8623 data, // Data buffer containing all bytes for this type
8624 data_byte_offset, // Offset into "data" where to grab value from
8625 desugar_byte_size, // Size of this type in bytes
8626 bitfield_bit_size, // Bitfield bit size
8627 bitfield_bit_offset,// Bitfield bit offset
8628 show_types, // Boolean indicating if we should show the variable types
8629 show_summary, // Boolean indicating if we should show a summary for the current type
8630 verbose, // Verbose output?
8631 depth); // Scope depth for any types that have children
8632 }
8633 break;
8634
8635 default:
8636 // We are down to a scalar type that we just need to display.
8637 data.Dump(s,
8638 data_byte_offset,
8639 format,
8640 data_byte_size,
8641 1,
8642 UINT32_MAX,
8643 LLDB_INVALID_ADDRESS,
8644 bitfield_bit_size,
8645 bitfield_bit_offset);
8646
8647 if (show_summary)
8648 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8649 break;
8650 }
8651}
8652
8653
8654
8655
8656bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008657ClangASTContext::DumpTypeValue (lldb::opaque_compiler_type_t type, Stream *s,
Greg Claytond8d4a572015-08-11 21:38:15 +00008658 lldb::Format format,
8659 const lldb_private::DataExtractor &data,
8660 lldb::offset_t byte_offset,
8661 size_t byte_size,
8662 uint32_t bitfield_bit_size,
8663 uint32_t bitfield_bit_offset,
8664 ExecutionContextScope *exe_scope)
8665{
8666 if (!type)
8667 return false;
8668 if (IsAggregateType(type))
8669 {
8670 return false;
8671 }
8672 else
8673 {
8674 clang::QualType qual_type(GetQualType(type));
8675
8676 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8677 switch (type_class)
8678 {
8679 case clang::Type::Typedef:
8680 {
8681 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008682 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008683 if (format == eFormatDefault)
8684 format = typedef_clang_type.GetFormat();
8685 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8686 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8687
8688 return typedef_clang_type.DumpTypeValue (s,
8689 format, // The format with which to display the element
8690 data, // Data buffer containing all bytes for this type
8691 byte_offset, // Offset into "data" where to grab value from
8692 typedef_byte_size, // Size of this type in bytes
8693 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8694 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8695 exe_scope);
8696 }
8697 break;
8698
8699 case clang::Type::Enum:
8700 // If our format is enum or default, show the enumeration value as
8701 // its enumeration string value, else just display it as requested.
8702 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8703 {
8704 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8705 const clang::EnumDecl *enum_decl = enutype->getDecl();
8706 assert(enum_decl);
8707 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8708 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8709 lldb::offset_t offset = byte_offset;
8710 if (is_signed)
8711 {
8712 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8713 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8714 {
8715 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8716 {
8717 s->PutCString (enum_pos->getNameAsString().c_str());
8718 return true;
8719 }
8720 }
8721 // If we have gotten here we didn't get find the enumerator in the
8722 // enum decl, so just print the integer.
8723 s->Printf("%" PRIi64, enum_svalue);
8724 }
8725 else
8726 {
8727 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8728 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8729 {
8730 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8731 {
8732 s->PutCString (enum_pos->getNameAsString().c_str());
8733 return true;
8734 }
8735 }
8736 // If we have gotten here we didn't get find the enumerator in the
8737 // enum decl, so just print the integer.
8738 s->Printf("%" PRIu64, enum_uvalue);
8739 }
8740 return true;
8741 }
8742 // format was not enum, just fall through and dump the value as requested....
8743
8744 default:
8745 // We are down to a scalar type that we just need to display.
8746 {
8747 uint32_t item_count = 1;
8748 // A few formats, we might need to modify our size and count for depending
8749 // on how we are trying to display the value...
8750 switch (format)
8751 {
8752 default:
8753 case eFormatBoolean:
8754 case eFormatBinary:
8755 case eFormatComplex:
8756 case eFormatCString: // NULL terminated C strings
8757 case eFormatDecimal:
8758 case eFormatEnum:
8759 case eFormatHex:
8760 case eFormatHexUppercase:
8761 case eFormatFloat:
8762 case eFormatOctal:
8763 case eFormatOSType:
8764 case eFormatUnsigned:
8765 case eFormatPointer:
8766 case eFormatVectorOfChar:
8767 case eFormatVectorOfSInt8:
8768 case eFormatVectorOfUInt8:
8769 case eFormatVectorOfSInt16:
8770 case eFormatVectorOfUInt16:
8771 case eFormatVectorOfSInt32:
8772 case eFormatVectorOfUInt32:
8773 case eFormatVectorOfSInt64:
8774 case eFormatVectorOfUInt64:
8775 case eFormatVectorOfFloat32:
8776 case eFormatVectorOfFloat64:
8777 case eFormatVectorOfUInt128:
8778 break;
8779
8780 case eFormatChar:
8781 case eFormatCharPrintable:
8782 case eFormatCharArray:
8783 case eFormatBytes:
8784 case eFormatBytesWithASCII:
8785 item_count = byte_size;
8786 byte_size = 1;
8787 break;
8788
8789 case eFormatUnicode16:
8790 item_count = byte_size / 2;
8791 byte_size = 2;
8792 break;
8793
8794 case eFormatUnicode32:
8795 item_count = byte_size / 4;
8796 byte_size = 4;
8797 break;
8798 }
8799 return data.Dump (s,
8800 byte_offset,
8801 format,
8802 byte_size,
8803 item_count,
8804 UINT32_MAX,
8805 LLDB_INVALID_ADDRESS,
8806 bitfield_bit_size,
8807 bitfield_bit_offset,
8808 exe_scope);
8809 }
8810 break;
8811 }
8812 }
8813 return 0;
8814}
8815
8816
8817
8818void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008819ClangASTContext::DumpSummary (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00008820 Stream *s,
8821 const lldb_private::DataExtractor &data,
8822 lldb::offset_t data_byte_offset,
8823 size_t data_byte_size)
8824{
8825 uint32_t length = 0;
8826 if (IsCStringType (type, length))
8827 {
8828 if (exe_ctx)
8829 {
8830 Process *process = exe_ctx->GetProcessPtr();
8831 if (process)
8832 {
8833 lldb::offset_t offset = data_byte_offset;
8834 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
8835 std::vector<uint8_t> buf;
8836 if (length > 0)
8837 buf.resize (length);
8838 else
8839 buf.resize (256);
8840
8841 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
8842 buf.back() = '\0';
8843 size_t bytes_read;
8844 size_t total_cstr_len = 0;
8845 Error error;
8846 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
8847 {
8848 const size_t len = strlen((const char *)&buf.front());
8849 if (len == 0)
8850 break;
8851 if (total_cstr_len == 0)
8852 s->PutCString (" \"");
8853 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8854 total_cstr_len += len;
8855 if (len < buf.size())
8856 break;
8857 pointer_address += total_cstr_len;
8858 }
8859 if (total_cstr_len > 0)
8860 s->PutChar ('"');
8861 }
8862 }
8863 }
8864}
8865
8866void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008867ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008868{
8869 StreamFile s (stdout, false);
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00008870 DumpTypeDescription (type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008871 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
8872 if (metadata)
8873 {
8874 metadata->Dump (&s);
8875 }
8876}
8877
8878void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008879ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type, Stream *s)
Greg Claytond8d4a572015-08-11 21:38:15 +00008880{
8881 if (type)
8882 {
8883 clang::QualType qual_type(GetQualType(type));
8884
8885 llvm::SmallVector<char, 1024> buf;
8886 llvm::raw_svector_ostream llvm_ostrm (buf);
8887
8888 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8889 switch (type_class)
8890 {
8891 case clang::Type::ObjCObject:
8892 case clang::Type::ObjCInterface:
8893 {
8894 GetCompleteType(type);
8895
8896 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8897 assert (objc_class_type);
8898 if (objc_class_type)
8899 {
8900 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8901 if (class_interface_decl)
8902 {
8903 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
8904 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
8905 }
8906 }
8907 }
8908 break;
8909
8910 case clang::Type::Typedef:
8911 {
8912 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
8913 if (typedef_type)
8914 {
8915 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8916 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
8917 if (!clang_typedef_name.empty())
8918 {
8919 s->PutCString ("typedef ");
8920 s->PutCString (clang_typedef_name.c_str());
8921 }
8922 }
8923 }
8924 break;
8925
8926 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008927 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008928 return;
8929
8930 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00008931 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00008932 return;
8933
8934 case clang::Type::Record:
8935 {
8936 GetCompleteType(type);
8937
8938 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8939 const clang::RecordDecl *record_decl = record_type->getDecl();
8940 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8941
8942 if (cxx_record_decl)
8943 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8944 else
8945 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
8946 }
8947 break;
8948
8949 default:
8950 {
8951 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
8952 if (tag_type)
8953 {
8954 clang::TagDecl *tag_decl = tag_type->getDecl();
8955 if (tag_decl)
8956 tag_decl->print(llvm_ostrm, 0);
8957 }
8958 else
8959 {
8960 std::string clang_type_name(qual_type.getAsString());
8961 if (!clang_type_name.empty())
8962 s->PutCString (clang_type_name.c_str());
8963 }
8964 }
8965 }
8966
Greg Claytond8d4a572015-08-11 21:38:15 +00008967 if (buf.size() > 0)
8968 {
8969 s->Write (buf.data(), buf.size());
8970 }
8971 }
8972}
Greg Clayton8b4edba2015-08-14 20:02:05 +00008973
Greg Clayton8b4edba2015-08-14 20:02:05 +00008974clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00008975ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008976 lldb::AccessType access_type,
8977 const char *parent_name,
8978 int tag_decl_kind,
8979 const ClangASTContext::TemplateParameterInfos &template_param_infos)
8980{
8981 if (template_param_infos.IsValid())
8982 {
8983 std::string template_basename(parent_name);
8984 template_basename.erase (template_basename.find('<'));
8985
8986 return CreateClassTemplateDecl (decl_ctx,
8987 access_type,
8988 template_basename.c_str(),
8989 tag_decl_kind,
8990 template_param_infos);
8991 }
8992 return NULL;
8993}
8994
Greg Clayton6dc8d582015-08-18 22:32:36 +00008995void
8996ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
8997{
8998 ClangASTContext *ast = (ClangASTContext *)baton;
8999 SymbolFile *sym_file = ast->GetSymbolFile();
9000 if (sym_file)
9001 {
9002 CompilerType clang_type = GetTypeForDecl (decl);
9003 if (clang_type)
9004 sym_file->CompleteType (clang_type);
9005 }
9006}
9007
9008void
9009ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9010{
9011 ClangASTContext *ast = (ClangASTContext *)baton;
9012 SymbolFile *sym_file = ast->GetSymbolFile();
9013 if (sym_file)
9014 {
9015 CompilerType clang_type = GetTypeForDecl (decl);
9016 if (clang_type)
9017 sym_file->CompleteType (clang_type);
9018 }
9019}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009020
Greg Clayton261ac3f2015-08-28 01:01:03 +00009021
9022DWARFASTParser *
9023ClangASTContext::GetDWARFParser ()
9024{
9025 if (!m_dwarf_ast_parser_ap)
9026 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9027 return m_dwarf_ast_parser_ap.get();
9028}
9029
9030
Greg Clayton8b4edba2015-08-14 20:02:05 +00009031bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009032ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009033 const clang::RecordDecl *record_decl,
9034 uint64_t &bit_size,
9035 uint64_t &alignment,
9036 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9037 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9038 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9039{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009040 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00009041 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
9042 return dwarf_ast_parser->LayoutRecordType(record_decl, bit_size, alignment, field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009043}
9044
Greg Clayton99558cc42015-08-24 23:46:31 +00009045//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009046// CompilerDecl override functions
9047//----------------------------------------------------------------------
9048lldb::VariableSP
9049ClangASTContext::DeclGetVariable (void *opaque_decl)
9050{
9051 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
9052 {
9053 auto decl_search_it = m_decl_objects.find(opaque_decl);
9054 if (decl_search_it != m_decl_objects.end())
9055 return std::static_pointer_cast<Variable>(decl_search_it->second);
9056 }
9057 return VariableSP();
9058}
9059
9060void
9061ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
9062{
9063 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
9064 m_decl_objects.insert(std::make_pair(opaque_decl, object));
9065}
9066
9067ConstString
9068ClangASTContext::DeclGetName (void *opaque_decl)
9069{
9070 if (opaque_decl)
9071 {
9072 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
9073 if (nd != nullptr)
9074 return ConstString(nd->getName());
9075 }
9076 return ConstString();
9077}
9078
9079//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009080// CompilerDeclContext functions
9081//----------------------------------------------------------------------
9082
Paul Hermand628cbb2015-09-15 23:44:17 +00009083std::vector<void *>
9084ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name)
9085{
9086 std::vector<void *> found_decls;
9087 if (opaque_decl_ctx)
9088 {
9089 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9090 std::set<DeclContext *> searched;
9091 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009092 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009093
9094 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
9095 {
9096 search_queue.insert(std::make_pair(decl_context, decl_context));
9097
9098 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
9099 {
9100 searched.insert(it->second);
Paul Hermanea188fc2015-09-16 18:48:30 +00009101 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9102
Paul Hermand628cbb2015-09-15 23:44:17 +00009103 for (clang::Decl *child : it->second->decls())
9104 {
Paul Hermanea188fc2015-09-16 18:48:30 +00009105 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00009106 {
9107 clang::DeclContext *from = ud->getCommonAncestor();
9108 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9109 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
9110 }
9111 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9112 {
9113 for (clang::UsingShadowDecl *usd : ud->shadows())
9114 {
9115 clang::Decl *target = usd->getTargetDecl();
9116 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
9117 {
9118 IdentifierInfo *ii = nd->getIdentifier();
9119 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9120 found_decls.push_back(nd);
9121 }
9122 }
9123 }
Paul Hermanea188fc2015-09-16 18:48:30 +00009124 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
9125 {
9126 IdentifierInfo *ii = nd->getIdentifier();
9127 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9128 found_decls.push_back(nd);
9129 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009130 }
9131 }
9132 }
9133 }
9134 return found_decls;
9135}
9136
Greg Clayton99558cc42015-08-24 23:46:31 +00009137bool
9138ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009139{
Greg Clayton99558cc42015-08-24 23:46:31 +00009140 if (opaque_decl_ctx)
9141 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9142 else
9143 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009144}
9145
Greg Clayton99558cc42015-08-24 23:46:31 +00009146ConstString
9147ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009148{
Greg Clayton99558cc42015-08-24 23:46:31 +00009149 if (opaque_decl_ctx)
9150 {
9151 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9152 if (named_decl)
9153 return ConstString(named_decl->getName());
9154 }
9155 return ConstString();
9156}
9157
9158bool
9159ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9160 lldb::LanguageType *language_ptr,
9161 bool *is_instance_method_ptr,
9162 ConstString *language_object_name_ptr)
9163{
9164 if (opaque_decl_ctx)
9165 {
9166 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9167 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9168 {
9169 if (is_instance_method_ptr)
9170 *is_instance_method_ptr = objc_method->isInstanceMethod();
9171 if (language_ptr)
9172 *language_ptr = eLanguageTypeObjC;
9173 if (language_object_name_ptr)
9174 language_object_name_ptr->SetCString("self");
9175 return true;
9176 }
9177 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9178 {
9179 if (is_instance_method_ptr)
9180 *is_instance_method_ptr = cxx_method->isInstance();
9181 if (language_ptr)
9182 *language_ptr = eLanguageTypeC_plus_plus;
9183 if (language_object_name_ptr)
9184 language_object_name_ptr->SetCString("this");
9185 return true;
9186 }
9187 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9188 {
9189 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9190 if (metadata && metadata->HasObjectPtr())
9191 {
9192 if (is_instance_method_ptr)
9193 *is_instance_method_ptr = true;
9194 if (language_ptr)
9195 *language_ptr = eLanguageTypeObjC;
9196 if (language_object_name_ptr)
9197 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9198 return true;
9199 }
9200 }
9201 }
9202 return false;
9203}
9204
9205clang::DeclContext *
9206ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9207{
9208 if (dc.IsClang())
9209 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9210 return nullptr;
9211}
9212
9213
9214ObjCMethodDecl *
9215ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
9216{
9217 if (dc.IsClang())
9218 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9219 return nullptr;
9220}
9221
9222CXXMethodDecl *
9223ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9224{
9225 if (dc.IsClang())
9226 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9227 return nullptr;
9228}
9229
9230clang::FunctionDecl *
9231ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9232{
9233 if (dc.IsClang())
9234 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9235 return nullptr;
9236}
9237
9238clang::NamespaceDecl *
9239ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9240{
9241 if (dc.IsClang())
9242 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9243 return nullptr;
9244}
9245
9246ClangASTMetadata *
9247ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9248{
9249 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9250 if (ast)
9251 return ClangASTContext::GetMetadata (ast, object);
9252 return nullptr;
9253}
9254
9255clang::ASTContext *
9256ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9257{
Greg Claytonf73034f2015-09-08 18:15:05 +00009258 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9259 if (ast)
9260 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009261 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009262}
9263
Jim Ingham151c0322015-09-15 21:13:50 +00009264ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9265 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009266 m_target_wp(target.shared_from_this()),
9267 m_persistent_variables (new ClangPersistentVariables)
Jim Ingham151c0322015-09-15 21:13:50 +00009268{
9269}
9270
9271UserExpression *
9272ClangASTContextForExpressions::GetUserExpression (const char *expr,
9273 const char *expr_prefix,
9274 lldb::LanguageType language,
Jim Ingham19a63fc2015-11-03 02:11:24 +00009275 Expression::ResultType desired_type,
9276 const EvaluateExpressionOptions &options)
Jim Ingham151c0322015-09-15 21:13:50 +00009277{
9278 TargetSP target_sp = m_target_wp.lock();
9279 if (!target_sp)
9280 return nullptr;
9281
Jim Ingham19a63fc2015-11-03 02:11:24 +00009282 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00009283}
9284
9285FunctionCaller *
9286ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9287 const Address& function_address,
9288 const ValueList &arg_value_list,
9289 const char *name)
9290{
9291 TargetSP target_sp = m_target_wp.lock();
9292 if (!target_sp)
9293 return nullptr;
9294
9295 Process *process = target_sp->GetProcessSP().get();
9296 if (!process)
9297 return nullptr;
9298
9299 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9300}
9301
9302UtilityFunction *
9303ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9304 const char *name)
9305{
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009306 TargetSP target_sp = m_target_wp.lock();
Jim Ingham151c0322015-09-15 21:13:50 +00009307 if (!target_sp)
9308 return nullptr;
9309
9310 return new ClangUtilityFunction(*target_sp.get(), text, name);
9311}
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009312
9313PersistentExpressionState *
9314ClangASTContextForExpressions::GetPersistentExpressionState ()
9315{
9316 return m_persistent_variables.get();
9317}