blob: ecf37ff32f24c8d762c2c478aef2bb8b9a82478a [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"
Greg Claytonfe689042015-11-10 17:47:04 +000043#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "clang/AST/RecordLayout.h"
45#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000046#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000048#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000050#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "clang/Basic/SourceManager.h"
52#include "clang/Basic/TargetInfo.h"
53#include "clang/Basic/TargetOptions.h"
54#include "clang/Frontend/FrontendOptions.h"
55#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000056
57#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000058#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000059#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
61#include <assert.h>
62#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Greg Claytond8d4a572015-08-11 21:38:15 +000064#include "llvm/Support/Signals.h"
65
Greg Clayton514487e2011-02-15 21:59:32 +000066#include "lldb/Core/ArchSpec.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000067#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000068#include "lldb/Core/Log.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000069#include "lldb/Core/Module.h"
70#include "lldb/Core/PluginManager.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000071#include "lldb/Core/RegularExpression.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000072#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000073#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000074#include "lldb/Core/UniqueCStringMap.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000075#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
76#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
77#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000078#include "lldb/Symbol/ClangASTContext.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000079#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000080#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000081#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000082#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000083#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000084#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000085#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000086#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000087#include "lldb/Target/Process.h"
88#include "lldb/Target/Target.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089
Greg Clayton261ac3f2015-08-28 01:01:03 +000090#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
91
Eli Friedman932197d2010-06-13 19:06:42 +000092#include <stdio.h>
93
Greg Clayton1341baf2013-07-11 23:36:31 +000094#include <mutex>
95
Greg Claytonc86103d2010-08-05 01:57:25 +000096using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097using namespace lldb_private;
98using namespace llvm;
99using namespace clang;
100
Greg Clayton56939cb2015-09-17 22:23:34 +0000101namespace
102{
103 static inline bool ClangASTContextSupportsLanguage (lldb::LanguageType language)
104 {
105 return language == eLanguageTypeUnknown || // Clang is the default type system
106 Language::LanguageIsC (language) ||
107 Language::LanguageIsCPlusPlus (language) ||
108 Language::LanguageIsObjC (language);
109 }
110}
111
Enrico Granata2267ad42014-09-16 17:28:40 +0000112typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000113
114static ClangASTMap &
115GetASTMap()
116{
Enrico Granata2267ad42014-09-16 17:28:40 +0000117 static ClangASTMap *g_map_ptr = nullptr;
118 static std::once_flag g_once_flag;
119 std::call_once(g_once_flag, []() {
120 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
121 });
122 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000123}
124
125
Greg Clayton57ee3062013-07-11 22:46:58 +0000126clang::AccessSpecifier
127ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000128{
129 switch (access)
130 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000131 default: break;
132 case eAccessNone: return AS_none;
133 case eAccessPublic: return AS_public;
134 case eAccessPrivate: return AS_private;
135 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000136 }
137 return AS_none;
138}
139
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000141ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142{
143 // FIXME: Cleanup per-file based stuff.
144
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000145 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146 // to move these to the language standard, and have the driver resolve the
147 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000148 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000150 } else if (IK == IK_ObjC ||
151 IK == IK_ObjCXX ||
152 IK == IK_PreprocessedObjC ||
153 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 Opts.ObjC1 = Opts.ObjC2 = 1;
155 }
156
157 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
158
159 if (LangStd == LangStandard::lang_unspecified) {
160 // Based on the base language, pick one.
161 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000162 case IK_None:
163 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000164 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000165 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000166 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 LangStd = LangStandard::lang_opencl;
168 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000169 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000170 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000171 LangStd = LangStandard::lang_cuda;
172 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000173 case IK_Asm:
174 case IK_C:
175 case IK_PreprocessedC:
176 case IK_ObjC:
177 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178 LangStd = LangStandard::lang_gnu99;
179 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000180 case IK_CXX:
181 case IK_PreprocessedCXX:
182 case IK_ObjCXX:
183 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 LangStd = LangStandard::lang_gnucxx98;
185 break;
186 }
187 }
188
189 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000190 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 Opts.C99 = Std.isC99();
192 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000193 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 Opts.Digraphs = Std.hasDigraphs();
195 Opts.GNUMode = Std.isGNUMode();
196 Opts.GNUInline = !Std.isC99();
197 Opts.HexFloats = Std.hasHexFloats();
198 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000199
200 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
202 // OpenCL has some additional defaults.
203 if (LangStd == LangStandard::lang_opencl) {
204 Opts.OpenCL = 1;
205 Opts.AltiVec = 1;
206 Opts.CXXOperatorNames = 1;
207 Opts.LaxVectorConversions = 1;
208 }
209
210 // OpenCL and C++ both have bool, true, false keywords.
211 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
212
213// if (Opts.CPlusPlus)
214// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
215//
216// if (Args.hasArg(OPT_fobjc_gc_only))
217// Opts.setGCMode(LangOptions::GCOnly);
218// else if (Args.hasArg(OPT_fobjc_gc))
219// Opts.setGCMode(LangOptions::HybridGC);
220//
221// if (Args.hasArg(OPT_print_ivar_layout))
222// Opts.ObjCGCBitmapPrint = 1;
223//
224// if (Args.hasArg(OPT_faltivec))
225// Opts.AltiVec = 1;
226//
227// if (Args.hasArg(OPT_pthread))
228// Opts.POSIXThreads = 1;
229//
230// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
231// "default");
232// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000233 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234// else if (Vis == "hidden")
235// Opts.setVisibilityMode(LangOptions::Hidden);
236// else if (Vis == "protected")
237// Opts.setVisibilityMode(LangOptions::Protected);
238// else
239// Diags.Report(diag::err_drv_invalid_value)
240// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
241
242// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
243
244 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
245 // is specified, or -std is set to a conforming mode.
246 Opts.Trigraphs = !Opts.GNUMode;
247// if (Args.hasArg(OPT_trigraphs))
248// Opts.Trigraphs = 1;
249//
250// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
251// OPT_fno_dollars_in_identifiers,
252// !Opts.AsmPreprocessor);
253// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
254// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
255// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
256// if (Args.hasArg(OPT_fno_lax_vector_conversions))
257// Opts.LaxVectorConversions = 0;
258// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
259// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
260// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000261 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
263// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
264// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
265// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
266// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
267// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
268// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
269// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
270// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
271// Diags);
272// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
273// Opts.ObjCConstantStringClass = getLastArgValue(Args,
274// OPT_fconstant_string_class);
275// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
276// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
277// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
278// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
279// Opts.Static = Args.hasArg(OPT_static_define);
280 Opts.OptimizeSize = 0;
281
282 // FIXME: Eliminate this dependency.
283// unsigned Opt =
284// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
285// Opts.Optimize = Opt != 0;
286 unsigned Opt = 0;
287
288 // This is the __NO_INLINE__ define, which just depends on things like the
289 // optimization level and -fno-inline, not actually whether the backend has
290 // inlining enabled.
291 //
292 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000293 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
295// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
296// switch (SSP) {
297// default:
298// Diags.Report(diag::err_drv_invalid_value)
299// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
300// break;
301// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
302// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
303// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
304// }
305}
306
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
Greg Claytonf73034f2015-09-08 18:15:05 +0000308ClangASTContext::ClangASTContext (const char *target_triple) :
309 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000310 m_target_triple (),
311 m_ast_ap (),
312 m_language_options_ap (),
313 m_source_manager_ap (),
314 m_diagnostics_engine_ap (),
315 m_target_options_rp (),
316 m_target_info_ap (),
317 m_identifier_table_ap (),
318 m_selector_table_ap (),
319 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000320 m_callback_tag_decl (nullptr),
321 m_callback_objc_decl (nullptr),
322 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000323 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000324 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325{
326 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000327 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328}
329
330//----------------------------------------------------------------------
331// Destructor
332//----------------------------------------------------------------------
333ClangASTContext::~ClangASTContext()
334{
Enrico Granata5d84a692014-08-19 21:46:37 +0000335 if (m_ast_ap.get())
336 {
Enrico Granata2267ad42014-09-16 17:28:40 +0000337 GetASTMap().Erase(m_ast_ap.get());
Greg Claytond8d4a572015-08-11 21:38:15 +0000338 if (!m_ast_owned)
339 m_ast_ap.release();
Enrico Granata5d84a692014-08-19 21:46:37 +0000340 }
341
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342 m_builtins_ap.reset();
343 m_selector_table_ap.reset();
344 m_identifier_table_ap.reset();
345 m_target_info_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000346 m_target_options_rp.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000347 m_diagnostics_engine_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 m_source_manager_ap.reset();
349 m_language_options_ap.reset();
Greg Clayton6beaaa62011-01-17 03:46:26 +0000350 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
Greg Clayton56939cb2015-09-17 22:23:34 +0000353ConstString
354ClangASTContext::GetPluginNameStatic()
355{
356 return ConstString("clang");
357}
358
359ConstString
360ClangASTContext::GetPluginName()
361{
362 return ClangASTContext::GetPluginNameStatic();
363}
364
365uint32_t
366ClangASTContext::GetPluginVersion()
367{
368 return 1;
369}
370
371lldb::TypeSystemSP
Tamas Berghammer3a6b82b2015-10-09 12:06:10 +0000372ClangASTContext::CreateInstance (lldb::LanguageType language,
373 lldb_private::Module *module,
374 Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000375{
376 if (ClangASTContextSupportsLanguage(language))
377 {
Greg Clayton5beec212015-10-08 21:04:34 +0000378 ArchSpec arch;
379 if (module)
380 arch = module->GetArchitecture();
381 else if (target)
382 arch = target->GetArchitecture();
383
384 if (arch.IsValid())
Greg Clayton56939cb2015-09-17 22:23:34 +0000385 {
Greg Clayton5beec212015-10-08 21:04:34 +0000386 ArchSpec fixed_arch = arch;
387 // LLVM wants this to be set to iOS or MacOSX; if we're working on
388 // a bare-boards type image, change the triple for llvm's benefit.
389 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
390 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
Greg Clayton56939cb2015-09-17 22:23:34 +0000391 {
Greg Clayton5beec212015-10-08 21:04:34 +0000392 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
393 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
394 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb)
Greg Clayton56939cb2015-09-17 22:23:34 +0000395 {
Greg Clayton5beec212015-10-08 21:04:34 +0000396 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
Greg Clayton56939cb2015-09-17 22:23:34 +0000397 }
Greg Clayton5beec212015-10-08 21:04:34 +0000398 else
399 {
400 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
401 }
402 }
403
404 if (module)
405 {
406 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
407 if (ast_sp)
408 {
409 ast_sp->SetArchitecture (fixed_arch);
410 }
411 return ast_sp;
412 }
Sean Callanana3444ff2015-11-10 22:54:42 +0000413 else if (target && target->IsValid())
Greg Clayton5beec212015-10-08 21:04:34 +0000414 {
415 std::shared_ptr<ClangASTContextForExpressions> ast_sp(new ClangASTContextForExpressions(*target));
416 if (ast_sp)
417 {
418 ast_sp->SetArchitecture(fixed_arch);
419 ast_sp->m_scratch_ast_source_ap.reset (new ClangASTSource(target->shared_from_this()));
420 ast_sp->m_scratch_ast_source_ap->InstallASTContext(ast_sp->getASTContext());
421 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(ast_sp->m_scratch_ast_source_ap->CreateProxy());
422 ast_sp->SetExternalSource(proxy_ast_source);
423 return ast_sp;
424 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000425 }
426 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000427 }
428 return lldb::TypeSystemSP();
429}
430
Sean Callananfe38c852015-10-08 23:07:53 +0000431void
432ClangASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
433{
434 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
435 lldb::eLanguageTypeC89,
436 lldb::eLanguageTypeC,
437 lldb::eLanguageTypeC11,
438 lldb::eLanguageTypeC_plus_plus,
439 lldb::eLanguageTypeC99,
440 lldb::eLanguageTypeObjC,
441 lldb::eLanguageTypeObjC_plus_plus,
442 lldb::eLanguageTypeC_plus_plus_03,
443 lldb::eLanguageTypeC_plus_plus_11,
444 lldb::eLanguageTypeC11,
445 lldb::eLanguageTypeC_plus_plus_14});
446
447 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({
448 lldb::eLanguageTypeC_plus_plus,
449 lldb::eLanguageTypeObjC_plus_plus,
450 lldb::eLanguageTypeC_plus_plus_03,
451 lldb::eLanguageTypeC_plus_plus_11,
452 lldb::eLanguageTypeC_plus_plus_14});
453
454 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
455 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
456}
457
Greg Clayton56939cb2015-09-17 22:23:34 +0000458
459void
460ClangASTContext::Initialize()
461{
462 PluginManager::RegisterPlugin (GetPluginNameStatic(),
463 "clang base AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000464 CreateInstance,
465 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000466}
467
468void
469ClangASTContext::Terminate()
470{
471 PluginManager::UnregisterPlugin (CreateInstance);
472}
473
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474
475void
476ClangASTContext::Clear()
477{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000478 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 m_language_options_ap.reset();
480 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000481 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000482 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 m_target_info_ap.reset();
484 m_identifier_table_ap.reset();
485 m_selector_table_ap.reset();
486 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000487 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
490const char *
491ClangASTContext::GetTargetTriple ()
492{
493 return m_target_triple.c_str();
494}
495
496void
497ClangASTContext::SetTargetTriple (const char *target_triple)
498{
499 Clear();
500 m_target_triple.assign(target_triple);
501}
502
Greg Clayton514487e2011-02-15 21:59:32 +0000503void
504ClangASTContext::SetArchitecture (const ArchSpec &arch)
505{
Greg Clayton880cbb02011-07-30 01:26:02 +0000506 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000507}
508
Greg Clayton6beaaa62011-01-17 03:46:26 +0000509bool
510ClangASTContext::HasExternalSource ()
511{
512 ASTContext *ast = getASTContext();
513 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000514 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000515 return false;
516}
517
518void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000519ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000520{
521 ASTContext *ast = getASTContext();
522 if (ast)
523 {
524 ast->setExternalSource (ast_source_ap);
525 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
526 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
527 }
528}
529
530void
531ClangASTContext::RemoveExternalSource ()
532{
533 ASTContext *ast = getASTContext();
534
535 if (ast)
536 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000537 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000538 ast->setExternalSource (empty_ast_source_ap);
539 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
540 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
541 }
542}
543
Greg Claytond8d4a572015-08-11 21:38:15 +0000544void
545ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
546{
547 if (!m_ast_owned) {
548 m_ast_ap.release();
549 }
550 m_ast_owned = false;
551 m_ast_ap.reset(ast_ctx);
552 GetASTMap().Insert(ast_ctx, this);
553}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554
555ASTContext *
556ClangASTContext::getASTContext()
557{
Ed Masted4612ad2014-04-20 13:17:36 +0000558 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000560 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000561 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
562 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000563 *getIdentifierTable(),
564 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000565 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000566
567 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000568
569 // This can be NULL if we don't know anything about the architecture or if the
570 // target for an architecture isn't enabled in the llvm/clang that we built
571 TargetInfo *target_info = getTargetInfo();
572 if (target_info)
573 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000574
Greg Clayton6beaaa62011-01-17 03:46:26 +0000575 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
576 {
577 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
578 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
579 }
580
Enrico Granata2267ad42014-09-16 17:28:40 +0000581 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000582
583 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
584 ClangASTContext::CompleteObjCInterfaceDecl,
585 nullptr,
586 ClangASTContext::LayoutRecordType,
587 this));
588 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000590 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591}
592
Enrico Granata5d84a692014-08-19 21:46:37 +0000593ClangASTContext*
594ClangASTContext::GetASTContext (clang::ASTContext* ast)
595{
Enrico Granata2267ad42014-09-16 17:28:40 +0000596 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000597 return clang_ast;
598}
599
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600Builtin::Context *
601ClangASTContext::getBuiltinContext()
602{
Ed Masted4612ad2014-04-20 13:17:36 +0000603 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000604 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 return m_builtins_ap.get();
606}
607
608IdentifierTable *
609ClangASTContext::getIdentifierTable()
610{
Ed Masted4612ad2014-04-20 13:17:36 +0000611 if (m_identifier_table_ap.get() == nullptr)
612 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000613 return m_identifier_table_ap.get();
614}
615
616LangOptions *
617ClangASTContext::getLanguageOptions()
618{
Ed Masted4612ad2014-04-20 13:17:36 +0000619 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 {
621 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000622 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000623// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 }
625 return m_language_options_ap.get();
626}
627
628SelectorTable *
629ClangASTContext::getSelectorTable()
630{
Ed Masted4612ad2014-04-20 13:17:36 +0000631 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 m_selector_table_ap.reset (new SelectorTable());
633 return m_selector_table_ap.get();
634}
635
Sean Callanan79439e82010-11-18 02:56:27 +0000636clang::FileManager *
637ClangASTContext::getFileManager()
638{
Ed Masted4612ad2014-04-20 13:17:36 +0000639 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000640 {
641 clang::FileSystemOptions file_system_options;
642 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
643 }
Sean Callanan79439e82010-11-18 02:56:27 +0000644 return m_file_manager_ap.get();
645}
646
Greg Claytone1a916a2010-07-21 22:12:05 +0000647clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648ClangASTContext::getSourceManager()
649{
Ed Masted4612ad2014-04-20 13:17:36 +0000650 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000651 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 return m_source_manager_ap.get();
653}
654
Sean Callanan880e6802011-10-07 23:18:13 +0000655clang::DiagnosticsEngine *
656ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
Ed Masted4612ad2014-04-20 13:17:36 +0000658 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000659 {
660 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000661 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000662 }
Sean Callanan880e6802011-10-07 23:18:13 +0000663 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664}
665
Greg Claytonfe689042015-11-10 17:47:04 +0000666clang::MangleContext *
667ClangASTContext::getMangleContext()
668{
669 if (m_mangle_ctx_ap.get() == nullptr)
670 m_mangle_ctx_ap.reset (getASTContext()->createMangleContext());
671 return m_mangle_ctx_ap.get();
672}
673
Sean Callanan880e6802011-10-07 23:18:13 +0000674class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000675{
676public:
Sean Callanan880e6802011-10-07 23:18:13 +0000677 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000678 {
679 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
680 }
681
Sean Callanan880e6802011-10-07 23:18:13 +0000682 void HandleDiagnostic (DiagnosticsEngine::Level DiagLevel, const Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000683 {
684 if (m_log)
685 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000686 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000687 info.FormatDiagnostic(diag_str);
688 diag_str.push_back('\0');
689 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
690 }
691 }
Sean Callanan880e6802011-10-07 23:18:13 +0000692
693 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
694 {
695 return new NullDiagnosticConsumer ();
696 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000697private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000698 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000699};
700
Sean Callanan880e6802011-10-07 23:18:13 +0000701DiagnosticConsumer *
702ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000703{
Ed Masted4612ad2014-04-20 13:17:36 +0000704 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000705 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000706
Sean Callanan880e6802011-10-07 23:18:13 +0000707 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000708}
709
Jason Molenda45938b92014-07-08 23:46:39 +0000710std::shared_ptr<TargetOptions> &
711ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000712 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 {
Alp Toker5f838642014-07-06 05:36:57 +0000714 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000715 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000716 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
Alp Toker5f838642014-07-06 05:36:57 +0000718 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719}
720
721
722TargetInfo *
723ClangASTContext::getTargetInfo()
724{
Greg Clayton70512312012-05-08 01:45:38 +0000725 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000726 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000727 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 return m_target_info_ap.get();
729}
730
731#pragma mark Basic Types
732
733static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000734QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000736 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 if (qual_type_bit_size == bit_size)
738 return true;
739 return false;
740}
Greg Clayton56939cb2015-09-17 22:23:34 +0000741
Greg Claytona1e5dc82015-08-11 22:53:00 +0000742CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +0000743ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, size_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744{
Greg Clayton57ee3062013-07-11 22:46:58 +0000745 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
747
Greg Claytona1e5dc82015-08-11 22:53:00 +0000748CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000749ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000751 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000752 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753 switch (encoding)
754 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000755 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000756 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000757 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 break;
759
Greg Claytonc86103d2010-08-05 01:57:25 +0000760 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000761 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000762 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000763 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000764 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000765 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000766 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000767 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000768 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000769 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000770 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000771 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000772 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 break;
774
Greg Claytonc86103d2010-08-05 01:57:25 +0000775 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000776 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000777 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000778 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000779 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000780 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000781 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000782 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000783 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000784 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000785 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000786 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000787 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 break;
789
Greg Claytonc86103d2010-08-05 01:57:25 +0000790 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000791 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000792 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000793 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000794 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000795 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000796 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000797 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
798 return CompilerType (ast, ast->HalfTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 break;
800
Greg Claytonc86103d2010-08-05 01:57:25 +0000801 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000802 // Sanity check that bit_size is a multiple of 8's.
803 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000804 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000805 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 }
807
Greg Claytona1e5dc82015-08-11 22:53:00 +0000808 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809}
810
Greg Clayton57ee3062013-07-11 22:46:58 +0000811
812
813lldb::BasicType
814ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
815{
816 if (name)
817 {
818 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
819 static TypeNameToBasicTypeMap g_type_map;
820 static std::once_flag g_once_flag;
821 std::call_once(g_once_flag, [](){
822 // "void"
823 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
824
825 // "char"
826 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
827 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
828 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
829 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
830 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
831 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
832 // "short"
833 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
834 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
835 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
836 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
837
838 // "int"
839 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
840 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
841 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
842 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
843
844 // "long"
845 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
846 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
847 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
848 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
849
850 // "long long"
851 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
852 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
853 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
854 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
855
856 // "int128"
857 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
858 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
859
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000860 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000861 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
862 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
863 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
864 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
865 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
866 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
867 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
868 g_type_map.Sort();
869 });
870
871 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
872 }
873 return eBasicTypeInvalid;
874}
875
Greg Claytona1e5dc82015-08-11 22:53:00 +0000876CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000877ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
878{
879 if (ast)
880 {
881 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
882 return ClangASTContext::GetBasicType (ast, basic_type);
883 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000884 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000885}
886
887uint32_t
888ClangASTContext::GetPointerByteSize ()
889{
890 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000891 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000892 return m_pointer_byte_size;
893}
894
Greg Claytona1e5dc82015-08-11 22:53:00 +0000895CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000896ClangASTContext::GetBasicType (lldb::BasicType basic_type)
897{
898 return GetBasicType (getASTContext(), basic_type);
899}
900
Greg Claytona1e5dc82015-08-11 22:53:00 +0000901CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000902ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
903{
904 if (ast)
905 {
Bruce Mitchener48ea9002015-09-23 00:18:24 +0000906 lldb::opaque_compiler_type_t clang_type = nullptr;
Greg Clayton57ee3062013-07-11 22:46:58 +0000907
908 switch (basic_type)
909 {
910 case eBasicTypeInvalid:
911 case eBasicTypeOther:
912 break;
913 case eBasicTypeVoid:
914 clang_type = ast->VoidTy.getAsOpaquePtr();
915 break;
916 case eBasicTypeChar:
917 clang_type = ast->CharTy.getAsOpaquePtr();
918 break;
919 case eBasicTypeSignedChar:
920 clang_type = ast->SignedCharTy.getAsOpaquePtr();
921 break;
922 case eBasicTypeUnsignedChar:
923 clang_type = ast->UnsignedCharTy.getAsOpaquePtr();
924 break;
925 case eBasicTypeWChar:
926 clang_type = ast->getWCharType().getAsOpaquePtr();
927 break;
928 case eBasicTypeSignedWChar:
929 clang_type = ast->getSignedWCharType().getAsOpaquePtr();
930 break;
931 case eBasicTypeUnsignedWChar:
932 clang_type = ast->getUnsignedWCharType().getAsOpaquePtr();
933 break;
934 case eBasicTypeChar16:
935 clang_type = ast->Char16Ty.getAsOpaquePtr();
936 break;
937 case eBasicTypeChar32:
938 clang_type = ast->Char32Ty.getAsOpaquePtr();
939 break;
940 case eBasicTypeShort:
941 clang_type = ast->ShortTy.getAsOpaquePtr();
942 break;
943 case eBasicTypeUnsignedShort:
944 clang_type = ast->UnsignedShortTy.getAsOpaquePtr();
945 break;
946 case eBasicTypeInt:
947 clang_type = ast->IntTy.getAsOpaquePtr();
948 break;
949 case eBasicTypeUnsignedInt:
950 clang_type = ast->UnsignedIntTy.getAsOpaquePtr();
951 break;
952 case eBasicTypeLong:
953 clang_type = ast->LongTy.getAsOpaquePtr();
954 break;
955 case eBasicTypeUnsignedLong:
956 clang_type = ast->UnsignedLongTy.getAsOpaquePtr();
957 break;
958 case eBasicTypeLongLong:
959 clang_type = ast->LongLongTy.getAsOpaquePtr();
960 break;
961 case eBasicTypeUnsignedLongLong:
962 clang_type = ast->UnsignedLongLongTy.getAsOpaquePtr();
963 break;
964 case eBasicTypeInt128:
965 clang_type = ast->Int128Ty.getAsOpaquePtr();
966 break;
967 case eBasicTypeUnsignedInt128:
968 clang_type = ast->UnsignedInt128Ty.getAsOpaquePtr();
969 break;
970 case eBasicTypeBool:
971 clang_type = ast->BoolTy.getAsOpaquePtr();
972 break;
973 case eBasicTypeHalf:
974 clang_type = ast->HalfTy.getAsOpaquePtr();
975 break;
976 case eBasicTypeFloat:
977 clang_type = ast->FloatTy.getAsOpaquePtr();
978 break;
979 case eBasicTypeDouble:
980 clang_type = ast->DoubleTy.getAsOpaquePtr();
981 break;
982 case eBasicTypeLongDouble:
983 clang_type = ast->LongDoubleTy.getAsOpaquePtr();
984 break;
985 case eBasicTypeFloatComplex:
986 clang_type = ast->FloatComplexTy.getAsOpaquePtr();
987 break;
988 case eBasicTypeDoubleComplex:
989 clang_type = ast->DoubleComplexTy.getAsOpaquePtr();
990 break;
991 case eBasicTypeLongDoubleComplex:
992 clang_type = ast->LongDoubleComplexTy.getAsOpaquePtr();
993 break;
994 case eBasicTypeObjCID:
995 clang_type = ast->getObjCIdType().getAsOpaquePtr();
996 break;
997 case eBasicTypeObjCClass:
998 clang_type = ast->getObjCClassType().getAsOpaquePtr();
999 break;
1000 case eBasicTypeObjCSel:
1001 clang_type = ast->getObjCSelType().getAsOpaquePtr();
1002 break;
1003 case eBasicTypeNullPtr:
1004 clang_type = ast->NullPtrTy.getAsOpaquePtr();
1005 break;
1006 }
1007
1008 if (clang_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001009 return CompilerType (GetASTContext(ast), clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +00001010 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001011 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001012}
1013
1014
Greg Claytona1e5dc82015-08-11 22:53:00 +00001015CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
1017{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001018 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +00001019
1020#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +00001021 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00001022 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 {
1024 switch (dw_ate)
1025 {
Sean Callanan38d4df52012-04-03 01:10:10 +00001026 default:
1027 break;
Greg Clayton605684e2011-10-28 23:06:08 +00001028
Sean Callanan38d4df52012-04-03 01:10:10 +00001029 case DW_ATE_address:
1030 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001031 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001032 break;
1033
1034 case DW_ATE_boolean:
1035 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001036 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001037 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001038 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001039 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001040 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001041 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001042 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001043 break;
1044
1045 case DW_ATE_lo_user:
1046 // This has been seen to mean DW_AT_complex_integer
1047 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +00001048 {
Sean Callanan38d4df52012-04-03 01:10:10 +00001049 if (::strstr(type_name, "complex"))
1050 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001051 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
1052 return CompilerType (ast, ast->getComplexType (GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +00001053 }
Greg Clayton605684e2011-10-28 23:06:08 +00001054 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001055 break;
1056
1057 case DW_ATE_complex_float:
1058 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001059 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001060 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001061 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001062 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001063 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001064 else
Greg Clayton605684e2011-10-28 23:06:08 +00001065 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001066 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
1067 return CompilerType (ast, ast->getComplexType (GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +00001068 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001069 break;
1070
1071 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +00001072 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001073 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001074 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001075 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +00001076 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001077 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +00001078 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +00001079 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001080 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001081 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001082 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001083 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001084 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +00001085 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
1086 return CompilerType (ast, ast->HalfTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001087 break;
1088
1089 case DW_ATE_signed:
1090 if (type_name)
1091 {
1092 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001093 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001094 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001095 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001096 if (streq(type_name, "void") &&
1097 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001098 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001099 if (strstr(type_name, "long long") &&
1100 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001101 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001102 if (strstr(type_name, "long") &&
1103 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001104 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001105 if (strstr(type_name, "short") &&
1106 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001107 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001108 if (strstr(type_name, "char"))
1109 {
1110 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001111 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001112 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001113 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001114 }
1115 if (strstr(type_name, "int"))
1116 {
1117 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001118 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001119 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001120 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001121 }
1122 }
1123 // We weren't able to match up a type name, just search by size
1124 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001125 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001126 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001127 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001128 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001129 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001130 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001131 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001132 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001133 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001134 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001135 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001136 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001137
Sean Callanan38d4df52012-04-03 01:10:10 +00001138 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001139 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +00001140 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001141 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001142 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001143 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001144 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001145 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001146 break;
1147
1148 case DW_ATE_unsigned:
1149 if (type_name)
1150 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001151 if (streq(type_name, "wchar_t"))
1152 {
1153 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1154 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001155 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001156 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001157 }
1158 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001159 if (strstr(type_name, "long long"))
1160 {
1161 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001162 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001163 }
1164 else if (strstr(type_name, "long"))
1165 {
1166 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001167 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001168 }
1169 else if (strstr(type_name, "short"))
1170 {
1171 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001172 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001173 }
1174 else if (strstr(type_name, "char"))
1175 {
1176 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001177 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001178 }
1179 else if (strstr(type_name, "int"))
1180 {
1181 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001182 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001183 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001184 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001185 }
1186 }
1187 // We weren't able to match up a type name, just search by size
1188 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001189 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001190 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001191 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001192 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001193 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001194 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001195 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001196 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001197 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001198 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001199 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001200 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001201
Sean Callanan38d4df52012-04-03 01:10:10 +00001202 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001203 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1204 {
1205 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001206 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001207 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001208 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001209 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001210 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001211 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001212 break;
1213
1214 case DW_ATE_imaginary_float:
1215 break;
1216
1217 case DW_ATE_UTF:
1218 if (type_name)
1219 {
1220 if (streq(type_name, "char16_t"))
1221 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001222 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001223 }
1224 else if (streq(type_name, "char32_t"))
1225 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001226 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001227 }
1228 }
1229 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 }
1231 }
1232 // This assert should fire for anything that we don't catch above so we know
1233 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001234 if (type_name)
1235 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001236 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 +00001237 }
1238 else
1239 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001240 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 +00001241 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001242 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243}
1244
Greg Claytona1e5dc82015-08-11 22:53:00 +00001245CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001246ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1247{
Greg Clayton57ee3062013-07-11 22:46:58 +00001248 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001249 return CompilerType (ast, ast->UnknownAnyTy);
1250 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001251}
1252
Greg Claytona1e5dc82015-08-11 22:53:00 +00001253CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254ClangASTContext::GetCStringType (bool is_const)
1255{
Greg Clayton57ee3062013-07-11 22:46:58 +00001256 ASTContext *ast = getASTContext();
1257 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258
1259 if (is_const)
1260 char_type.addConst();
1261
Greg Claytona1e5dc82015-08-11 22:53:00 +00001262 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263}
1264
Sean Callanan09ab4b72011-11-30 22:11:59 +00001265clang::DeclContext *
1266ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1267{
1268 return ast->getTranslationUnitDecl();
1269}
1270
Greg Clayton526e5af2010-11-13 03:52:47 +00001271clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001272ClangASTContext::CopyDecl (ASTContext *dst_ast,
1273 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001274 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001275{
Sean Callanan79439e82010-11-18 02:56:27 +00001276 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001277 FileManager file_manager (file_system_options);
1278 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001279 *src_ast, file_manager,
1280 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001281
1282 return importer.Import(source_decl);
1283}
1284
Sean Callanan23a30272010-07-16 00:00:27 +00001285bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001286ClangASTContext::AreTypesSame (CompilerType type1,
1287 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001288 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001289{
Greg Claytonf73034f2015-09-08 18:15:05 +00001290 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1291 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001292 return false;
1293
1294 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001295 return true;
1296
Greg Claytond8d4a572015-08-11 21:38:15 +00001297 QualType type1_qual = GetQualType(type1);
1298 QualType type2_qual = GetQualType(type2);
Sean Callanan5056ab02012-02-18 02:01:03 +00001299
1300 if (ignore_qualifiers)
1301 {
1302 type1_qual = type1_qual.getUnqualifiedType();
1303 type2_qual = type2_qual.getUnqualifiedType();
1304 }
1305
Greg Claytonf73034f2015-09-08 18:15:05 +00001306 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307}
1308
Greg Claytona1e5dc82015-08-11 22:53:00 +00001309CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001310ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1311{
1312 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1313 return GetTypeForDecl(interface_decl);
1314 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1315 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001316 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001317}
1318
Greg Clayton6beaaa62011-01-17 03:46:26 +00001319
Greg Claytona1e5dc82015-08-11 22:53:00 +00001320CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001321ClangASTContext::GetTypeForDecl (TagDecl *decl)
1322{
1323 // No need to call the getASTContext() accessor (which can create the AST
1324 // if it isn't created yet, because we can't have created a decl in this
1325 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001326 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001327 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001328 return CompilerType (ast, ast->getTagDeclType(decl));
1329 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001330}
1331
Greg Claytona1e5dc82015-08-11 22:53:00 +00001332CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001333ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1334{
1335 // No need to call the getASTContext() accessor (which can create the AST
1336 // if it isn't created yet, because we can't have created a decl in this
1337 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001338 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001339 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001340 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1341 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001342}
1343
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344#pragma mark Structure, Unions, Classes
1345
Greg Claytona1e5dc82015-08-11 22:53:00 +00001346CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001347ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1348 AccessType access_type,
1349 const char *name,
1350 int kind,
1351 LanguageType language,
1352 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001353{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001354 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001355 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001356
Ed Masted4612ad2014-04-20 13:17:36 +00001357 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001358 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359
Greg Clayton9e409562010-07-28 02:04:09 +00001360
Greg Claytone1be9962011-08-24 23:50:00 +00001361 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001362 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001363 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001364 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001365 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001366 }
1367
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1369 // we will need to update this code. I was told to currently always use
1370 // the CXXRecordDecl class since we often don't know from debug information
1371 // if something is struct or a class, so we default to always use the more
1372 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001373
1374 bool is_anonymous = (!name) || (!name[0]);
1375
Greg Claytonf0705c82011-10-22 03:33:13 +00001376 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1377 (TagDecl::TagKind)kind,
1378 decl_ctx,
1379 SourceLocation(),
1380 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001381 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001382
1383 if (is_anonymous)
1384 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001385
Greg Claytonc4ffd662013-03-08 01:37:30 +00001386 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001387 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001388 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001389 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001390
Greg Clayton55561e92011-10-26 03:31:36 +00001391 if (access_type != eAccessNone)
1392 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001393
1394 if (decl_ctx)
1395 decl_ctx->addDecl (decl);
1396
Greg Claytona1e5dc82015-08-11 22:53:00 +00001397 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001398 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001399 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001400}
1401
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001402static TemplateParameterList *
1403CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001404 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001405 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1406{
1407 const bool parameter_pack = false;
1408 const bool is_typename = false;
1409 const unsigned depth = 0;
1410 const size_t num_template_params = template_param_infos.GetSize();
1411 for (size_t i=0; i<num_template_params; ++i)
1412 {
1413 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001414
Ed Masted4612ad2014-04-20 13:17:36 +00001415 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001416 if (name && name[0])
1417 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001418 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001419 {
1420 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1421 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1422 SourceLocation(),
1423 SourceLocation(),
1424 depth,
1425 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001426 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001427 template_param_infos.args[i].getIntegralType(),
1428 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001429 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001430
1431 }
1432 else
1433 {
1434 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1435 ast->getTranslationUnitDecl(), // Is this the right decl context?
1436 SourceLocation(),
1437 SourceLocation(),
1438 depth,
1439 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001440 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001441 is_typename,
1442 parameter_pack));
1443 }
1444 }
1445
1446 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1447 SourceLocation(),
1448 SourceLocation(),
1449 &template_param_decls.front(),
1450 template_param_decls.size(),
1451 SourceLocation());
1452 return template_param_list;
1453}
1454
1455clang::FunctionTemplateDecl *
1456ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1457 clang::FunctionDecl *func_decl,
1458 const char *name,
1459 const TemplateParameterInfos &template_param_infos)
1460{
1461// /// \brief Create a function template node.
1462 ASTContext *ast = getASTContext();
1463
1464 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1465
1466 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1467 template_param_infos,
1468 template_param_decls);
1469 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1470 decl_ctx,
1471 func_decl->getLocation(),
1472 func_decl->getDeclName(),
1473 template_param_list,
1474 func_decl);
1475
1476 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1477 i < template_param_decl_count;
1478 ++i)
1479 {
1480 // TODO: verify which decl context we should put template_param_decls into..
1481 template_param_decls[i]->setDeclContext (func_decl);
1482 }
1483
1484 return func_tmpl_decl;
1485}
1486
1487void
1488ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1489 clang::FunctionTemplateDecl *func_tmpl_decl,
1490 const TemplateParameterInfos &infos)
1491{
1492 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1493 infos.args.data(),
1494 infos.args.size());
1495
1496 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1497 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001498 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001499}
1500
1501
Greg Claytonf0705c82011-10-22 03:33:13 +00001502ClassTemplateDecl *
1503ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001504 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001505 const char *class_name,
1506 int kind,
1507 const TemplateParameterInfos &template_param_infos)
1508{
1509 ASTContext *ast = getASTContext();
1510
Ed Masted4612ad2014-04-20 13:17:36 +00001511 ClassTemplateDecl *class_template_decl = nullptr;
1512 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001513 decl_ctx = ast->getTranslationUnitDecl();
1514
1515 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1516 DeclarationName decl_name (&identifier_info);
1517
1518 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001519
1520 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001521 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001522 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001523 if (class_template_decl)
1524 return class_template_decl;
1525 }
1526
1527 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001528
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001529 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1530 template_param_infos,
1531 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001532
1533 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1534 (TagDecl::TagKind)kind,
1535 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1536 SourceLocation(),
1537 SourceLocation(),
1538 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001539
1540 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1541 i < template_param_decl_count;
1542 ++i)
1543 {
1544 template_param_decls[i]->setDeclContext (template_cxx_decl);
1545 }
1546
Sean Callananb5c79622011-11-19 01:35:08 +00001547 // With templated classes, we say that a class is templated with
1548 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001549 //template_cxx_decl->startDefinition();
1550 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001551
Greg Claytonf0705c82011-10-22 03:33:13 +00001552 class_template_decl = ClassTemplateDecl::Create (*ast,
1553 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1554 SourceLocation(),
1555 decl_name,
1556 template_param_list,
1557 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001558 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001559
1560 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001561 {
Greg Clayton55561e92011-10-26 03:31:36 +00001562 if (access_type != eAccessNone)
1563 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001564
1565 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1566 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1567
Greg Claytonf0705c82011-10-22 03:33:13 +00001568 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001569
1570#ifdef LLDB_CONFIGURATION_DEBUG
1571 VerifyDecl(class_template_decl);
1572#endif
1573 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001574
1575 return class_template_decl;
1576}
1577
1578
1579ClassTemplateSpecializationDecl *
1580ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1581 ClassTemplateDecl *class_template_decl,
1582 int kind,
1583 const TemplateParameterInfos &template_param_infos)
1584{
1585 ASTContext *ast = getASTContext();
1586 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1587 (TagDecl::TagKind)kind,
1588 decl_ctx,
1589 SourceLocation(),
1590 SourceLocation(),
1591 class_template_decl,
1592 &template_param_infos.args.front(),
1593 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001594 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001595
Sean Callananfa4fab72013-02-01 06:55:48 +00001596 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1597
Greg Claytonf0705c82011-10-22 03:33:13 +00001598 return class_template_specialization_decl;
1599}
1600
Greg Claytona1e5dc82015-08-11 22:53:00 +00001601CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001602ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1603{
1604 if (class_template_specialization_decl)
1605 {
1606 ASTContext *ast = getASTContext();
1607 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001608 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001609 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001610 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001611}
1612
Greg Clayton090d0982011-06-19 03:43:27 +00001613static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001614check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001615{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001616 // Special-case call since it can take any number of operands
1617 if(op_kind == OO_Call)
1618 return true;
1619
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001620 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001621 if (num_params == 0)
1622 return unary;
1623 if (num_params == 1)
1624 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001625 else
Greg Clayton090d0982011-06-19 03:43:27 +00001626 return false;
1627}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001628
Greg Clayton090d0982011-06-19 03:43:27 +00001629bool
1630ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1631{
Sean Callanan5b26f272012-02-04 08:49:35 +00001632 switch (op_kind)
1633 {
1634 default:
1635 break;
1636 // C++ standard allows any number of arguments to new/delete
1637 case OO_New:
1638 case OO_Array_New:
1639 case OO_Delete:
1640 case OO_Array_Delete:
1641 return true;
1642 }
1643
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001644#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 +00001645 switch (op_kind)
1646 {
1647#include "clang/Basic/OperatorKinds.def"
1648 default: break;
1649 }
1650 return false;
1651}
1652
Greg Clayton57ee3062013-07-11 22:46:58 +00001653clang::AccessSpecifier
1654ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001655{
1656 clang::AccessSpecifier ret = lhs;
1657
1658 // Make the access equal to the stricter of the field and the nested field's access
1659 switch (ret)
1660 {
1661 case clang::AS_none:
1662 break;
1663 case clang::AS_private:
1664 break;
1665 case clang::AS_protected:
1666 if (rhs == AS_private)
1667 ret = AS_private;
1668 break;
1669 case clang::AS_public:
1670 ret = rhs;
1671 break;
1672 }
1673
1674 return ret;
1675}
1676
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001677bool
1678ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1679{
1680 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1681}
1682
1683bool
1684ClangASTContext::FieldIsBitfield
1685(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001686 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 FieldDecl* field,
1688 uint32_t& bitfield_bit_size
1689)
1690{
Ed Masted4612ad2014-04-20 13:17:36 +00001691 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692 return false;
1693
1694 if (field->isBitField())
1695 {
1696 Expr* bit_width_expr = field->getBitWidth();
1697 if (bit_width_expr)
1698 {
1699 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001700 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 {
1702 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1703 return true;
1704 }
1705 }
1706 }
1707 return false;
1708}
1709
1710bool
1711ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1712{
Ed Masted4612ad2014-04-20 13:17:36 +00001713 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001714 return false;
1715
1716 if (!record_decl->field_empty())
1717 return true;
1718
1719 // No fields, lets check this is a CXX record and check the base classes
1720 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1721 if (cxx_record_decl)
1722 {
1723 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1724 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1725 base_class != base_class_end;
1726 ++base_class)
1727 {
1728 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1729 if (RecordHasFields(base_class_decl))
1730 return true;
1731 }
1732 }
1733 return false;
1734}
1735
Greg Clayton8cf05932010-07-22 18:30:50 +00001736#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737
Greg Claytona1e5dc82015-08-11 22:53:00 +00001738CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001739ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001740(
1741 const char *name,
1742 DeclContext *decl_ctx,
1743 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001744 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001745 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001746)
1747{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001748 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001749 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001750 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001751 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001752 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001753
Greg Clayton6beaaa62011-01-17 03:46:26 +00001754 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001755 decl_ctx,
1756 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001757 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001758 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001759 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001760 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001761 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001762 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001763
Jim Ingham379397632012-10-27 02:54:13 +00001764 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001765 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001766
Greg Claytona1e5dc82015-08-11 22:53:00 +00001767 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768}
1769
1770static inline bool
1771BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1772{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001773 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774}
1775
Greg Clayton57ee3062013-07-11 22:46:58 +00001776uint32_t
1777ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778{
1779 uint32_t num_bases = 0;
1780 if (cxx_record_decl)
1781 {
1782 if (omit_empty_base_classes)
1783 {
1784 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1785 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1786 base_class != base_class_end;
1787 ++base_class)
1788 {
1789 // Skip empty base classes
1790 if (omit_empty_base_classes)
1791 {
1792 if (BaseSpecifierIsEmpty (base_class))
1793 continue;
1794 }
1795 ++num_bases;
1796 }
1797 }
1798 else
1799 num_bases = cxx_record_decl->getNumBases();
1800 }
1801 return num_bases;
1802}
1803
1804
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805#pragma mark Namespace Declarations
1806
1807NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001808ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809{
Ed Masted4612ad2014-04-20 13:17:36 +00001810 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001811 ASTContext *ast = getASTContext();
1812 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001813 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001814 decl_ctx = translation_unit_decl;
1815
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816 if (name)
1817 {
Greg Clayton030a2042011-10-14 21:34:45 +00001818 IdentifierInfo &identifier_info = ast->Idents.get(name);
1819 DeclarationName decl_name (&identifier_info);
1820 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001821 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001822 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001823 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001824 if (namespace_decl)
1825 return namespace_decl;
1826 }
1827
Sean Callanan5b26f272012-02-04 08:49:35 +00001828 namespace_decl = NamespaceDecl::Create(*ast,
1829 decl_ctx,
1830 false,
1831 SourceLocation(),
1832 SourceLocation(),
1833 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001834 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001835
Greg Clayton9d3d6882011-10-31 23:51:19 +00001836 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001838 else
1839 {
1840 if (decl_ctx == translation_unit_decl)
1841 {
1842 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1843 if (namespace_decl)
1844 return namespace_decl;
1845
Sean Callanan5b26f272012-02-04 08:49:35 +00001846 namespace_decl = NamespaceDecl::Create(*ast,
1847 decl_ctx,
1848 false,
1849 SourceLocation(),
1850 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001851 nullptr,
1852 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001853 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1854 translation_unit_decl->addDecl (namespace_decl);
1855 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1856 }
1857 else
1858 {
1859 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1860 if (parent_namespace_decl)
1861 {
1862 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1863 if (namespace_decl)
1864 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001865 namespace_decl = NamespaceDecl::Create(*ast,
1866 decl_ctx,
1867 false,
1868 SourceLocation(),
1869 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001870 nullptr,
1871 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001872 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1873 parent_namespace_decl->addDecl (namespace_decl);
1874 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1875 }
1876 else
1877 {
1878 // BAD!!!
1879 }
1880 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001881 }
1882#ifdef LLDB_CONFIGURATION_DEBUG
1883 VerifyDecl(namespace_decl);
1884#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001885 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001886}
1887
1888
Paul Hermand628cbb2015-09-15 23:44:17 +00001889clang::BlockDecl *
1890ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1891{
1892 if (ctx != nullptr)
1893 {
1894 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1895 ctx->addDecl(decl);
1896 return decl;
1897 }
1898 return nullptr;
1899}
1900
Paul Hermanea188fc2015-09-16 18:48:30 +00001901clang::DeclContext *
1902FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1903{
1904 if (root == nullptr)
1905 return nullptr;
1906
1907 std::set<clang::DeclContext *> path_left;
1908 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1909 path_left.insert(d);
1910
1911 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1912 if (path_left.find(d) != path_left.end())
1913 return d;
1914
1915 return nullptr;
1916}
1917
Paul Hermand628cbb2015-09-15 23:44:17 +00001918clang::UsingDirectiveDecl *
1919ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1920{
1921 if (decl_ctx != nullptr && ns_decl != nullptr)
1922 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001923 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001924 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1925 decl_ctx,
1926 clang::SourceLocation(),
1927 clang::SourceLocation(),
1928 clang::NestedNameSpecifierLoc(),
1929 clang::SourceLocation(),
1930 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001931 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001932 decl_ctx->addDecl(using_decl);
1933 return using_decl;
1934 }
1935 return nullptr;
1936}
1937
1938clang::UsingDecl *
1939ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1940{
1941 if (current_decl_ctx != nullptr && target != nullptr)
1942 {
1943 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1944 current_decl_ctx,
1945 clang::SourceLocation(),
1946 clang::NestedNameSpecifierLoc(),
1947 clang::DeclarationNameInfo(),
1948 false);
1949 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1950 current_decl_ctx,
1951 clang::SourceLocation(),
1952 using_decl,
1953 target);
1954 using_decl->addShadowDecl(shadow_decl);
1955 current_decl_ctx->addDecl(using_decl);
1956 return using_decl;
1957 }
1958 return nullptr;
1959}
1960
1961clang::VarDecl *
1962ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1963{
1964 if (decl_context != nullptr)
1965 {
1966 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1967 decl_context,
1968 clang::SourceLocation(),
1969 clang::SourceLocation(),
1970 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1971 type,
1972 nullptr,
1973 clang::SC_None);
1974 var_decl->setAccess(clang::AS_public);
1975 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001976 return var_decl;
1977 }
1978 return nullptr;
1979}
1980
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981#pragma mark Function Types
1982
1983FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001984ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1985 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001986 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001987 int storage,
1988 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989{
Ed Masted4612ad2014-04-20 13:17:36 +00001990 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001991 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001992 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001993 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001994
Greg Clayton0d551042013-06-28 21:08:47 +00001995
1996 const bool hasWrittenPrototype = true;
1997 const bool isConstexprSpecified = false;
1998
Greg Clayton147e1fa2011-10-14 22:47:18 +00001999 if (name && name[0])
2000 {
2001 func_decl = FunctionDecl::Create (*ast,
2002 decl_ctx,
2003 SourceLocation(),
2004 SourceLocation(),
2005 DeclarationName (&ast->Idents.get(name)),
Greg Claytond8d4a572015-08-11 21:38:15 +00002006 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002007 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002008 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00002009 is_inline,
2010 hasWrittenPrototype,
2011 isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00002013 else
2014 {
2015 func_decl = FunctionDecl::Create (*ast,
2016 decl_ctx,
2017 SourceLocation(),
2018 SourceLocation(),
2019 DeclarationName (),
Greg Claytond8d4a572015-08-11 21:38:15 +00002020 GetQualType(function_clang_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002021 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002022 (clang::StorageClass)storage,
Greg Clayton0d551042013-06-28 21:08:47 +00002023 is_inline,
2024 hasWrittenPrototype,
2025 isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00002026 }
2027 if (func_decl)
2028 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002029
2030#ifdef LLDB_CONFIGURATION_DEBUG
2031 VerifyDecl(func_decl);
2032#endif
2033
Greg Clayton147e1fa2011-10-14 22:47:18 +00002034 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035}
2036
Greg Claytona1e5dc82015-08-11 22:53:00 +00002037CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00002038ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002039 const CompilerType& result_type,
2040 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00002041 unsigned num_args,
2042 bool is_variadic,
2043 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002044{
Ed Masted4612ad2014-04-20 13:17:36 +00002045 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046 std::vector<QualType> qual_type_args;
2047 for (unsigned i=0; i<num_args; ++i)
Greg Claytond8d4a572015-08-11 21:38:15 +00002048 qual_type_args.push_back (GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049
2050 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00002051 FunctionProtoType::ExtProtoInfo proto_info;
2052 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002053 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00002054 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00002055 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002056
Greg Claytona1e5dc82015-08-11 22:53:00 +00002057 return CompilerType (ast, ast->getFunctionType (GetQualType(result_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002058 qual_type_args,
Greg Claytond8d4a572015-08-11 21:38:15 +00002059 proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060}
2061
2062ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00002063ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002064{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002065 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002066 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +00002067 return ParmVarDecl::Create(*ast,
2068 ast->getTranslationUnitDecl(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002069 SourceLocation(),
Sean Callananfb0b7582011-03-15 00:17:19 +00002070 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002071 name && name[0] ? &ast->Idents.get(name) : nullptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002072 GetQualType(param_type),
Ed Masted4612ad2014-04-20 13:17:36 +00002073 nullptr,
Shawn Best3ab672d2014-10-31 23:20:13 +00002074 (clang::StorageClass)storage,
Ed Masted4612ad2014-04-20 13:17:36 +00002075 nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076}
2077
2078void
2079ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2080{
2081 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002082 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002083}
2084
2085
2086#pragma mark Array Types
2087
Greg Claytona1e5dc82015-08-11 22:53:00 +00002088CompilerType
2089ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002090 size_t element_count,
2091 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092{
Greg Clayton57ee3062013-07-11 22:46:58 +00002093 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002095 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002096 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002097
Greg Clayton1c8ef472013-04-05 23:27:21 +00002098 if (is_vector)
2099 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002100 return CompilerType (ast, ast->getExtVectorType(GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00002101 }
2102 else
2103 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002104
2105 llvm::APInt ap_element_count (64, element_count);
2106 if (element_count == 0)
2107 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002108 return CompilerType (ast, ast->getIncompleteArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002109 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002110 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002111 }
2112 else
2113 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00002114 return CompilerType (ast, ast->getConstantArrayType (GetQualType(element_type),
Greg Clayton57ee3062013-07-11 22:46:58 +00002115 ap_element_count,
2116 ArrayType::Normal,
Greg Claytond8d4a572015-08-11 21:38:15 +00002117 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002118 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002119 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002121 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002122}
2123
Greg Claytona1e5dc82015-08-11 22:53:00 +00002124CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00002125ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002126 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00002127 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00002128{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002129 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002130 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2131 return type;
2132 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00002133 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002134 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00002135 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00002136 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00002137 SetIsPacked(type);
2138 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002139 return type;
2140}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141
2142#pragma mark Enumeration Types
2143
Greg Claytona1e5dc82015-08-11 22:53:00 +00002144CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002145ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002146(
Greg Claytond8d4a572015-08-11 21:38:15 +00002147 const char *name,
2148 DeclContext *decl_ctx,
2149 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002150 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002151 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152{
2153 // TODO: Do something intelligent with the Declaration object passed in
2154 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002155 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002156
Greg Claytone02b8502010-10-12 04:29:14 +00002157 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002158 // const bool IsScoped = false;
2159 // const bool IsFixed = false;
2160
Greg Clayton6beaaa62011-01-17 03:46:26 +00002161 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002162 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002163 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002164 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002165 name && name[0] ? &ast->Idents.get(name) : nullptr,
2166 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002167 false, // IsScoped
2168 false, // IsScopedUsingClassTag
2169 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002170
2171
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002172 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002173 {
2174 // TODO: check if we should be setting the promotion type too?
Greg Claytond8d4a572015-08-11 21:38:15 +00002175 enum_decl->setIntegerType(GetQualType(integer_clang_type));
Sean Callanan2652ad22011-01-18 01:03:44 +00002176
2177 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2178
Greg Claytona1e5dc82015-08-11 22:53:00 +00002179 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002180 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002181 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002182}
2183
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184// Disable this for now since I can't seem to get a nicely formatted float
2185// out of the APFloat class without just getting the float, double or quad
2186// and then using a formatted print on it which defeats the purpose. We ideally
2187// would like to get perfect string values for any kind of float semantics
2188// so we can support remote targets. The code below also requires a patch to
2189// llvm::APInt.
2190//bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002191//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 +00002192//{
2193// uint32_t count = 0;
2194// bool is_complex = false;
2195// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2196// {
2197// unsigned num_bytes_per_float = byte_size / count;
2198// unsigned num_bits_per_float = num_bytes_per_float * 8;
2199//
2200// float_str.clear();
2201// uint32_t i;
2202// for (i=0; i<count; i++)
2203// {
2204// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2205// bool is_ieee = false;
2206// APFloat ap_float(ap_int, is_ieee);
2207// char s[1024];
2208// unsigned int hex_digits = 0;
2209// bool upper_case = false;
2210//
2211// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2212// {
2213// if (i > 0)
2214// float_str.append(", ");
2215// float_str.append(s);
2216// if (i == 1 && is_complex)
2217// float_str.append(1, 'i');
2218// }
2219// }
2220// return !float_str.empty();
2221// }
2222// return false;
2223//}
2224
Greg Claytona1e5dc82015-08-11 22:53:00 +00002225CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002226ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2227 size_t bit_size, bool is_signed)
2228{
2229 if (ast)
2230 {
2231 if (is_signed)
2232 {
2233 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002234 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002235
2236 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002237 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002238
2239 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002240 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002241
2242 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002243 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002244
2245 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002246 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002247
2248 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002249 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002250 }
2251 else
2252 {
2253 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002254 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002255
2256 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002257 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002258
2259 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002260 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002261
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002263 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002264
2265 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002266 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002267
2268 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002269 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002270 }
2271 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002272 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002273}
2274
Greg Claytona1e5dc82015-08-11 22:53:00 +00002275CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002276ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2277{
2278 if (ast)
2279 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002280 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002281}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282
Enrico Granata86027e92012-03-24 01:11:14 +00002283bool
Greg Claytona2721472011-06-25 00:44:06 +00002284ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2285 clang::Decl *decl)
2286{
2287 if (!decl)
2288 return false;
2289
2290 ExternalASTSource *ast_source = ast->getExternalSource();
2291
2292 if (!ast_source)
2293 return false;
2294
2295 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2296 {
Greg Clayton219cf312012-03-30 00:51:13 +00002297 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002298 return true;
2299
Greg Claytona2721472011-06-25 00:44:06 +00002300 ast_source->CompleteType(tag_decl);
2301
2302 return !tag_decl->getTypeForDecl()->isIncompleteType();
2303 }
2304 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2305 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002306 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002307 return true;
2308
2309 if (!objc_interface_decl->hasExternalLexicalStorage())
2310 return false;
2311
2312 ast_source->CompleteType(objc_interface_decl);
2313
Sean Callanan5b26f272012-02-04 08:49:35 +00002314 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002315 }
2316 else
2317 {
2318 return false;
2319 }
2320}
2321
Sean Callanan60217122012-04-13 00:10:03 +00002322void
Greg Claytond0029442013-03-27 01:48:02 +00002323ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002324 user_id_t user_id)
2325{
2326 ClangASTMetadata meta_data;
2327 meta_data.SetUserID (user_id);
2328 SetMetadata (object, meta_data);
2329}
2330
2331void
Sean Callanan60217122012-04-13 00:10:03 +00002332ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002333 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002334 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002335{
2336 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002337 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002338
2339 if (external_source)
2340 external_source->SetMetadata(object, metadata);
2341}
2342
Jim Ingham379397632012-10-27 02:54:13 +00002343ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002344ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002345 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002346{
2347 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002348 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002349
2350 if (external_source && external_source->HasMetadata(object))
2351 return external_source->GetMetadata(object);
2352 else
Ed Masted4612ad2014-04-20 13:17:36 +00002353 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002354}
2355
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002356clang::DeclContext *
2357ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2358{
Sean Callanana87bee82011-08-19 06:19:25 +00002359 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002360}
2361
2362clang::DeclContext *
2363ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2364{
Sean Callanana87bee82011-08-19 06:19:25 +00002365 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002366}
2367
Greg Claytond8d4a572015-08-11 21:38:15 +00002368bool
2369ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2370{
2371 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2372 if (clang_type)
2373 {
2374 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2375 if (tag_type)
2376 {
2377 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2378 if (tag_decl)
2379 {
2380 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2381 return true;
2382 }
2383 }
2384 }
2385 return false;
2386}
2387
2388
2389bool
2390ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2391 int default_accessibility,
2392 int *assigned_accessibilities,
2393 size_t num_assigned_accessibilities)
2394{
2395 if (record_decl)
2396 {
2397 uint32_t field_idx;
2398 clang::RecordDecl::field_iterator field, field_end;
2399 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2400 field != field_end;
2401 ++field, ++field_idx)
2402 {
2403 // If no accessibility was assigned, assign the correct one
2404 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2405 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2406 }
2407 return true;
2408 }
2409 return false;
2410}
2411
2412clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002413ClangASTContext::GetDeclContextForType (const CompilerType& type)
2414{
2415 return GetDeclContextForType(GetQualType(type));
2416}
2417
2418clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002419ClangASTContext::GetDeclContextForType (clang::QualType type)
2420{
2421 if (type.isNull())
2422 return nullptr;
2423
2424 clang::QualType qual_type = type.getCanonicalType();
2425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2426 switch (type_class)
2427 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002428 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2429 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2430 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2431 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2432 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
2433 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2434 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002435 default:
2436 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002437 }
2438 // No DeclContext in this type...
2439 return nullptr;
2440}
2441
2442static bool
2443GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2444{
2445 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2446 switch (type_class)
2447 {
2448 case clang::Type::ConstantArray:
2449 case clang::Type::IncompleteArray:
2450 case clang::Type::VariableArray:
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002451 {
2452 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2453
2454 if (array_type)
2455 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2456 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002457 break;
2458
2459 case clang::Type::Record:
Greg Claytond8d4a572015-08-11 21:38:15 +00002460 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002461 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2462 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002463 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002464 bool fields_loaded = cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2465 if (cxx_record_decl->isCompleteDefinition() && fields_loaded)
Greg Claytond8d4a572015-08-11 21:38:15 +00002466 return true;
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002467
Greg Claytond8d4a572015-08-11 21:38:15 +00002468 if (!allow_completion)
2469 return false;
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002470
2471 // Call the field_begin() accessor to for it to use the external source
2472 // to load the fields...
2473 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2474 if (external_ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002475 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002476 external_ast_source->CompleteType(cxx_record_decl);
2477 if (cxx_record_decl->isCompleteDefinition())
Greg Claytond8d4a572015-08-11 21:38:15 +00002478 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002479 cxx_record_decl->setHasLoadedFieldsFromExternalStorage (true);
2480 cxx_record_decl->field_begin();
2481 return cxx_record_decl->hasLoadedFieldsFromExternalStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00002482 }
2483 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002484 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002485 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002486 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002487 break;
2488
2489 case clang::Type::Enum:
2490 {
2491 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2492 if (tag_type)
2493 {
2494 clang::TagDecl *tag_decl = tag_type->getDecl();
2495 if (tag_decl)
2496 {
2497 if (tag_decl->getDefinition())
2498 return true;
2499
2500 if (!allow_completion)
2501 return false;
2502
2503 if (tag_decl->hasExternalLexicalStorage())
2504 {
2505 if (ast)
2506 {
2507 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2508 if (external_ast_source)
2509 {
2510 external_ast_source->CompleteType(tag_decl);
2511 return !tag_type->isIncompleteType();
2512 }
2513 }
2514 }
2515 return false;
2516 }
2517 }
2518
2519 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002520 break;
2521
2522 case clang::Type::ObjCObject:
2523 case clang::Type::ObjCInterface:
Greg Claytond8d4a572015-08-11 21:38:15 +00002524 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002525 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2526 if (objc_class_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002527 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002528 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2529 // We currently can't complete objective C types through the newly added ASTContext
2530 // because it only supports TagDecl objects right now...
2531 if (class_interface_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002532 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002533 if (class_interface_decl->getDefinition())
2534 return true;
2535
2536 if (!allow_completion)
2537 return false;
2538
2539 if (class_interface_decl->hasExternalLexicalStorage())
Greg Claytond8d4a572015-08-11 21:38:15 +00002540 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002541 if (ast)
Greg Claytond8d4a572015-08-11 21:38:15 +00002542 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002543 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2544 if (external_ast_source)
2545 {
2546 external_ast_source->CompleteType (class_interface_decl);
2547 return !objc_class_type->isIncompleteType();
2548 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002549 }
2550 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002551 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002552 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002553 }
2554 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002555 break;
2556
2557 case clang::Type::Typedef:
2558 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2559
2560 case clang::Type::Elaborated:
2561 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2562
2563 case clang::Type::Paren:
2564 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002565
2566 case clang::Type::Attributed:
2567 return GetCompleteQualType (ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), allow_completion);
2568
Greg Claytond8d4a572015-08-11 21:38:15 +00002569 default:
2570 break;
2571 }
2572
2573 return true;
2574}
2575
2576static clang::ObjCIvarDecl::AccessControl
2577ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2578{
2579 switch (access)
2580 {
2581 case eAccessNone: return clang::ObjCIvarDecl::None;
2582 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2583 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2584 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2585 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2586 }
2587 return clang::ObjCIvarDecl::None;
2588}
2589
2590
2591//----------------------------------------------------------------------
2592// Tests
2593//----------------------------------------------------------------------
2594
2595bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002596ClangASTContext::IsAggregateType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002597{
2598 clang::QualType qual_type (GetCanonicalQualType(type));
2599
2600 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2601 switch (type_class)
2602 {
2603 case clang::Type::IncompleteArray:
2604 case clang::Type::VariableArray:
2605 case clang::Type::ConstantArray:
2606 case clang::Type::ExtVector:
2607 case clang::Type::Vector:
2608 case clang::Type::Record:
2609 case clang::Type::ObjCObject:
2610 case clang::Type::ObjCInterface:
2611 return true;
2612 case clang::Type::Elaborated:
2613 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2614 case clang::Type::Typedef:
2615 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2616 case clang::Type::Paren:
2617 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2618 default:
2619 break;
2620 }
2621 // The clang type does have a value
2622 return false;
2623}
2624
2625bool
Enrico Granata7123e2b2015-11-07 02:06:57 +00002626ClangASTContext::IsAnonymousType (lldb::opaque_compiler_type_t type)
2627{
2628 clang::QualType qual_type (GetCanonicalQualType(type));
2629
2630 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2631 switch (type_class)
2632 {
2633 case clang::Type::Record:
2634 {
2635 if (const clang::RecordType *record_type = llvm::dyn_cast_or_null<clang::RecordType>(qual_type.getTypePtrOrNull()))
2636 {
2637 if (const clang::RecordDecl *record_decl = record_type->getDecl())
2638 {
2639 return record_decl->isAnonymousStructOrUnion();
2640 }
2641 }
2642 break;
2643 }
2644 case clang::Type::Elaborated:
2645 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2646 case clang::Type::Typedef:
2647 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2648 case clang::Type::Paren:
2649 return IsAnonymousType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2650 default:
2651 break;
2652 }
2653 // The clang type does have a value
2654 return false;
2655}
2656
2657bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002658ClangASTContext::IsArrayType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002659 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002660 uint64_t *size,
2661 bool *is_incomplete)
2662{
2663 clang::QualType qual_type (GetCanonicalQualType(type));
2664
2665 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2666 switch (type_class)
2667 {
2668 default:
2669 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002670
Greg Claytond8d4a572015-08-11 21:38:15 +00002671 case clang::Type::ConstantArray:
2672 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002673 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002674 if (size)
2675 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002676 if (is_incomplete)
2677 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002678 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002679
Greg Claytond8d4a572015-08-11 21:38:15 +00002680 case clang::Type::IncompleteArray:
2681 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002682 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002683 if (size)
2684 *size = 0;
2685 if (is_incomplete)
2686 *is_incomplete = true;
2687 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002688
Greg Claytond8d4a572015-08-11 21:38:15 +00002689 case clang::Type::VariableArray:
2690 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002691 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002692 if (size)
2693 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002694 if (is_incomplete)
2695 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002696 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002697
Greg Claytond8d4a572015-08-11 21:38:15 +00002698 case clang::Type::DependentSizedArray:
2699 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002700 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002701 if (size)
2702 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002703 if (is_incomplete)
2704 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002705 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002706
Greg Claytond8d4a572015-08-11 21:38:15 +00002707 case clang::Type::Typedef:
2708 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2709 element_type_ptr,
2710 size,
2711 is_incomplete);
2712 case clang::Type::Elaborated:
2713 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2714 element_type_ptr,
2715 size,
2716 is_incomplete);
2717 case clang::Type::Paren:
2718 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2719 element_type_ptr,
2720 size,
2721 is_incomplete);
2722 }
2723 if (element_type_ptr)
2724 element_type_ptr->Clear();
2725 if (size)
2726 *size = 0;
2727 if (is_incomplete)
2728 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002729 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002730}
2731
2732bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002733ClangASTContext::IsVectorType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002734 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002735 uint64_t *size)
2736{
2737 clang::QualType qual_type (GetCanonicalQualType(type));
2738
2739 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2740 switch (type_class)
2741 {
2742 case clang::Type::Vector:
2743 {
2744 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2745 if (vector_type)
2746 {
2747 if (size)
2748 *size = vector_type->getNumElements();
2749 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002750 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002751 }
2752 return true;
2753 }
2754 break;
2755 case clang::Type::ExtVector:
2756 {
2757 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2758 if (ext_vector_type)
2759 {
2760 if (size)
2761 *size = ext_vector_type->getNumElements();
2762 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002763 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002764 }
2765 return true;
2766 }
2767 default:
2768 break;
2769 }
2770 return false;
2771}
2772
2773bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002774ClangASTContext::IsRuntimeGeneratedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002775{
2776 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2777 if (!decl_ctx)
2778 return false;
2779
2780 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2781 return false;
2782
2783 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2784
2785 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2786 if (!ast_metadata)
2787 return false;
2788 return (ast_metadata->GetISAPtr() != 0);
2789}
2790
2791bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002792ClangASTContext::IsCharType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002793{
2794 return GetQualType(type).getUnqualifiedType()->isCharType();
2795}
2796
2797
2798bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002799ClangASTContext::IsCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002800{
2801 const bool allow_completion = false;
2802 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2803}
2804
2805bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002806ClangASTContext::IsConst(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002807{
2808 return GetQualType(type).isConstQualified();
2809}
2810
2811bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002812ClangASTContext::IsCStringType (lldb::opaque_compiler_type_t type, uint32_t &length)
Greg Claytond8d4a572015-08-11 21:38:15 +00002813{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002814 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002815 length = 0;
2816 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2817
2818 if (!pointee_or_element_clang_type.IsValid())
2819 return false;
2820
2821 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2822 {
2823 if (pointee_or_element_clang_type.IsCharType())
2824 {
2825 if (type_flags.Test (eTypeIsArray))
2826 {
2827 // We know the size of the array and it could be a C string
2828 // since it is an array of characters
2829 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2830 }
2831 return true;
2832
2833 }
2834 }
2835 return false;
2836}
2837
2838bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002839ClangASTContext::IsFunctionType (lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002840{
2841 if (type)
2842 {
2843 clang::QualType qual_type (GetCanonicalQualType(type));
2844
2845 if (qual_type->isFunctionType())
2846 {
2847 if (is_variadic_ptr)
2848 {
2849 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2850 if (function_proto_type)
2851 *is_variadic_ptr = function_proto_type->isVariadic();
2852 else
2853 *is_variadic_ptr = false;
2854 }
2855 return true;
2856 }
2857
2858 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2859 switch (type_class)
2860 {
2861 default:
2862 break;
2863 case clang::Type::Typedef:
2864 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
2865 case clang::Type::Elaborated:
2866 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2867 case clang::Type::Paren:
2868 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2869 case clang::Type::LValueReference:
2870 case clang::Type::RValueReference:
2871 {
2872 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2873 if (reference_type)
2874 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
2875 }
2876 break;
2877 }
2878 }
2879 return false;
2880}
2881
2882// Used to detect "Homogeneous Floating-point Aggregates"
2883uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002884ClangASTContext::IsHomogeneousAggregate (lldb::opaque_compiler_type_t type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002885{
2886 if (!type)
2887 return 0;
2888
2889 clang::QualType qual_type(GetCanonicalQualType(type));
2890 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2891 switch (type_class)
2892 {
2893 case clang::Type::Record:
2894 if (GetCompleteType (type))
2895 {
2896 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2897 if (cxx_record_decl)
2898 {
2899 if (cxx_record_decl->getNumBases() ||
2900 cxx_record_decl->isDynamicClass())
2901 return 0;
2902 }
2903 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2904 if (record_type)
2905 {
2906 const clang::RecordDecl *record_decl = record_type->getDecl();
2907 if (record_decl)
2908 {
2909 // We are looking for a structure that contains only floating point types
2910 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
2911 uint32_t num_fields = 0;
2912 bool is_hva = false;
2913 bool is_hfa = false;
2914 clang::QualType base_qual_type;
2915 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
2916 {
2917 clang::QualType field_qual_type = field_pos->getType();
2918 if (field_qual_type->isFloatingType())
2919 {
2920 if (field_qual_type->isComplexType())
2921 return 0;
2922 else
2923 {
2924 if (num_fields == 0)
2925 base_qual_type = field_qual_type;
2926 else
2927 {
2928 if (is_hva)
2929 return 0;
2930 is_hfa = true;
2931 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2932 return 0;
2933 }
2934 }
2935 }
2936 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
2937 {
2938 const clang::VectorType *array = field_qual_type.getTypePtr()->getAs<clang::VectorType>();
2939 if (array && array->getNumElements() <= 4)
2940 {
2941 if (num_fields == 0)
2942 base_qual_type = array->getElementType();
2943 else
2944 {
2945 if (is_hfa)
2946 return 0;
2947 is_hva = true;
2948 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2949 return 0;
2950 }
2951 }
2952 else
2953 return 0;
2954 }
2955 else
2956 return 0;
2957 ++num_fields;
2958 }
2959 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002960 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00002961 return num_fields;
2962 }
2963 }
2964 }
2965 break;
2966
2967 case clang::Type::Typedef:
2968 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
2969
2970 case clang::Type::Elaborated:
2971 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
2972 default:
2973 break;
2974 }
2975 return 0;
2976}
2977
2978size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002979ClangASTContext::GetNumberOfFunctionArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002980{
2981 if (type)
2982 {
2983 clang::QualType qual_type (GetCanonicalQualType(type));
2984 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2985 if (func)
2986 return func->getNumParams();
2987 }
2988 return 0;
2989}
2990
Greg Claytona1e5dc82015-08-11 22:53:00 +00002991CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002992ClangASTContext::GetFunctionArgumentAtIndex (lldb::opaque_compiler_type_t type, const size_t index)
Greg Claytond8d4a572015-08-11 21:38:15 +00002993{
2994 if (type)
2995 {
2996 clang::QualType qual_type (GetCanonicalQualType(type));
2997 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2998 if (func)
2999 {
3000 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00003001 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003002 }
3003 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003004 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003005}
3006
3007bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003008ClangASTContext::IsFunctionPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003009{
3010 if (type)
3011 {
3012 clang::QualType qual_type (GetCanonicalQualType(type));
3013
3014 if (qual_type->isFunctionPointerType())
3015 return true;
3016
3017 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3018 switch (type_class)
3019 {
3020 default:
3021 break;
3022 case clang::Type::Typedef:
3023 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
3024 case clang::Type::Elaborated:
3025 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3026 case clang::Type::Paren:
3027 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3028
3029 case clang::Type::LValueReference:
3030 case clang::Type::RValueReference:
3031 {
3032 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3033 if (reference_type)
3034 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
3035 }
3036 break;
3037 }
3038 }
3039 return false;
3040
3041}
3042
3043bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003044ClangASTContext::IsIntegerType (lldb::opaque_compiler_type_t type, bool &is_signed)
Greg Claytond8d4a572015-08-11 21:38:15 +00003045{
3046 if (!type)
3047 return false;
3048
3049 clang::QualType qual_type (GetCanonicalQualType(type));
3050 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3051
3052 if (builtin_type)
3053 {
3054 if (builtin_type->isInteger())
3055 {
3056 is_signed = builtin_type->isSignedInteger();
3057 return true;
3058 }
3059 }
3060
3061 return false;
3062}
3063
3064bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003065ClangASTContext::IsPointerType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003066{
3067 if (type)
3068 {
3069 clang::QualType qual_type (GetCanonicalQualType(type));
3070 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3071 switch (type_class)
3072 {
3073 case clang::Type::Builtin:
3074 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3075 {
3076 default:
3077 break;
3078 case clang::BuiltinType::ObjCId:
3079 case clang::BuiltinType::ObjCClass:
3080 return true;
3081 }
3082 return false;
3083 case clang::Type::ObjCObjectPointer:
3084 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003085 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003086 return true;
3087 case clang::Type::BlockPointer:
3088 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003089 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003090 return true;
3091 case clang::Type::Pointer:
3092 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003093 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003094 return true;
3095 case clang::Type::MemberPointer:
3096 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003097 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003098 return true;
3099 case clang::Type::Typedef:
3100 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3101 case clang::Type::Elaborated:
3102 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3103 case clang::Type::Paren:
3104 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3105 default:
3106 break;
3107 }
3108 }
3109 if (pointee_type)
3110 pointee_type->Clear();
3111 return false;
3112}
3113
3114
3115bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003116ClangASTContext::IsPointerOrReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003117{
3118 if (type)
3119 {
3120 clang::QualType qual_type (GetCanonicalQualType(type));
3121 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3122 switch (type_class)
3123 {
3124 case clang::Type::Builtin:
3125 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3126 {
3127 default:
3128 break;
3129 case clang::BuiltinType::ObjCId:
3130 case clang::BuiltinType::ObjCClass:
3131 return true;
3132 }
3133 return false;
3134 case clang::Type::ObjCObjectPointer:
3135 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003136 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003137 return true;
3138 case clang::Type::BlockPointer:
3139 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003140 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003141 return true;
3142 case clang::Type::Pointer:
3143 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003144 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003145 return true;
3146 case clang::Type::MemberPointer:
3147 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003148 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003149 return true;
3150 case clang::Type::LValueReference:
3151 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003152 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003153 return true;
3154 case clang::Type::RValueReference:
3155 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003156 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003157 return true;
3158 case clang::Type::Typedef:
3159 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
3160 case clang::Type::Elaborated:
3161 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3162 case clang::Type::Paren:
3163 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3164 default:
3165 break;
3166 }
3167 }
3168 if (pointee_type)
3169 pointee_type->Clear();
3170 return false;
3171}
3172
3173
3174bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003175ClangASTContext::IsReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003176{
3177 if (type)
3178 {
3179 clang::QualType qual_type (GetCanonicalQualType(type));
3180 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3181
3182 switch (type_class)
3183 {
3184 case clang::Type::LValueReference:
3185 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003186 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003187 if (is_rvalue)
3188 *is_rvalue = false;
3189 return true;
3190 case clang::Type::RValueReference:
3191 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003192 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003193 if (is_rvalue)
3194 *is_rvalue = true;
3195 return true;
3196 case clang::Type::Typedef:
3197 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
3198 case clang::Type::Elaborated:
3199 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3200 case clang::Type::Paren:
3201 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3202
3203 default:
3204 break;
3205 }
3206 }
3207 if (pointee_type)
3208 pointee_type->Clear();
3209 return false;
3210}
3211
3212bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003213ClangASTContext::IsFloatingPointType (lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Greg Claytond8d4a572015-08-11 21:38:15 +00003214{
3215 if (type)
3216 {
3217 clang::QualType qual_type (GetCanonicalQualType(type));
3218
3219 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3220 {
3221 clang::BuiltinType::Kind kind = BT->getKind();
3222 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3223 {
3224 count = 1;
3225 is_complex = false;
3226 return true;
3227 }
3228 }
3229 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3230 {
3231 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3232 {
3233 count = 2;
3234 is_complex = true;
3235 return true;
3236 }
3237 }
3238 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3239 {
3240 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3241 {
3242 count = VT->getNumElements();
3243 is_complex = false;
3244 return true;
3245 }
3246 }
3247 }
3248 count = 0;
3249 is_complex = false;
3250 return false;
3251}
3252
3253
3254bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003255ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003256{
3257 if (!type)
3258 return false;
3259
3260 clang::QualType qual_type(GetQualType(type));
3261 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3262 if (tag_type)
3263 {
3264 clang::TagDecl *tag_decl = tag_type->getDecl();
3265 if (tag_decl)
3266 return tag_decl->isCompleteDefinition();
3267 return false;
3268 }
3269 else
3270 {
3271 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3272 if (objc_class_type)
3273 {
3274 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3275 if (class_interface_decl)
3276 return class_interface_decl->getDefinition() != nullptr;
3277 return false;
3278 }
3279 }
3280 return true;
3281}
3282
3283bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003284ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003285{
3286 if (type)
3287 {
3288 clang::QualType qual_type (GetCanonicalQualType(type));
3289
3290 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3291
3292 if (obj_pointer_type)
3293 return obj_pointer_type->isObjCClassType();
3294 }
3295 return false;
3296}
3297
3298bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003299ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003300{
Ryan Brown57bee1e2015-09-14 22:45:11 +00003301 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00003302 return GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3303 return false;
3304}
3305
3306bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003307ClangASTContext::IsPolymorphicClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003308{
3309 if (type)
3310 {
3311 clang::QualType qual_type(GetCanonicalQualType(type));
3312 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3313 switch (type_class)
3314 {
3315 case clang::Type::Record:
3316 if (GetCompleteType(type))
3317 {
3318 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3319 const clang::RecordDecl *record_decl = record_type->getDecl();
3320 if (record_decl)
3321 {
3322 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3323 if (cxx_record_decl)
3324 return cxx_record_decl->isPolymorphic();
3325 }
3326 }
3327 break;
3328
3329 default:
3330 break;
3331 }
3332 }
3333 return false;
3334}
3335
3336bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003337ClangASTContext::IsPossibleDynamicType (lldb::opaque_compiler_type_t type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003338 bool check_cplusplus,
3339 bool check_objc)
3340{
3341 clang::QualType pointee_qual_type;
3342 if (type)
3343 {
3344 clang::QualType qual_type (GetCanonicalQualType(type));
3345 bool success = false;
3346 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3347 switch (type_class)
3348 {
3349 case clang::Type::Builtin:
3350 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3351 {
3352 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003353 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003354 return true;
3355 }
3356 break;
3357
3358 case clang::Type::ObjCObjectPointer:
3359 if (check_objc)
3360 {
3361 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003362 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003363 return true;
3364 }
3365 break;
3366
3367 case clang::Type::Pointer:
3368 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3369 success = true;
3370 break;
3371
3372 case clang::Type::LValueReference:
3373 case clang::Type::RValueReference:
3374 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3375 success = true;
3376 break;
3377
3378 case clang::Type::Typedef:
3379 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3380 dynamic_pointee_type,
3381 check_cplusplus,
3382 check_objc);
3383
3384 case clang::Type::Elaborated:
3385 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3386 dynamic_pointee_type,
3387 check_cplusplus,
3388 check_objc);
3389
3390 case clang::Type::Paren:
3391 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3392 dynamic_pointee_type,
3393 check_cplusplus,
3394 check_objc);
3395 default:
3396 break;
3397 }
3398
3399 if (success)
3400 {
3401 // Check to make sure what we are pointing too is a possible dynamic C++ type
3402 // We currently accept any "void *" (in case we have a class that has been
3403 // watered down to an opaque pointer) and virtual C++ classes.
3404 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3405 switch (pointee_type_class)
3406 {
3407 case clang::Type::Builtin:
3408 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3409 {
3410 case clang::BuiltinType::UnknownAny:
3411 case clang::BuiltinType::Void:
3412 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003413 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003414 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003415 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003416 break;
3417 }
3418 break;
3419
3420 case clang::Type::Record:
3421 if (check_cplusplus)
3422 {
3423 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3424 if (cxx_record_decl)
3425 {
3426 bool is_complete = cxx_record_decl->isCompleteDefinition();
3427
3428 if (is_complete)
3429 success = cxx_record_decl->isDynamicClass();
3430 else
3431 {
3432 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3433 if (metadata)
3434 success = metadata->GetIsDynamicCXXType();
3435 else
3436 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003437 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003438 if (is_complete)
3439 success = cxx_record_decl->isDynamicClass();
3440 else
3441 success = false;
3442 }
3443 }
3444
3445 if (success)
3446 {
3447 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003448 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003449 return true;
3450 }
3451 }
3452 }
3453 break;
3454
3455 case clang::Type::ObjCObject:
3456 case clang::Type::ObjCInterface:
3457 if (check_objc)
3458 {
3459 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003460 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003461 return true;
3462 }
3463 break;
3464
3465 default:
3466 break;
3467 }
3468 }
3469 }
3470 if (dynamic_pointee_type)
3471 dynamic_pointee_type->Clear();
3472 return false;
3473}
3474
3475
3476bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003477ClangASTContext::IsScalarType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003478{
3479 if (!type)
3480 return false;
3481
3482 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3483}
3484
3485bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003486ClangASTContext::IsTypedefType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003487{
3488 if (!type)
3489 return false;
3490 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3491}
3492
3493bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003494ClangASTContext::IsVoidType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003495{
3496 if (!type)
3497 return false;
3498 return GetCanonicalQualType(type)->isVoidType();
3499}
3500
3501bool
Greg Clayton56939cb2015-09-17 22:23:34 +00003502ClangASTContext::SupportsLanguage (lldb::LanguageType language)
3503{
3504 return ClangASTContextSupportsLanguage(language);
3505}
3506
3507bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003508ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003509{
3510 if (type)
3511 {
3512 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003513 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003514 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003515 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3516 if (cxx_record_decl)
3517 {
3518 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3519 return true;
3520 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003521 }
3522 }
3523 class_name.clear();
3524 return false;
3525}
3526
3527
3528bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003529ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003530{
3531 if (!type)
3532 return false;
3533
3534 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003535 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003536 return true;
3537 return false;
3538}
3539
3540bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003541ClangASTContext::IsBeingDefined (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003542{
3543 if (!type)
3544 return false;
3545 clang::QualType qual_type (GetCanonicalQualType(type));
3546 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3547 if (tag_type)
3548 return tag_type->isBeingDefined();
3549 return false;
3550}
3551
3552bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003553ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003554{
3555 if (!type)
3556 return false;
3557
3558 clang::QualType qual_type (GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003559
3560 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003561 {
3562 if (class_type_ptr)
3563 {
3564 if (!qual_type->isObjCClassType() &&
3565 !qual_type->isObjCIdType())
3566 {
3567 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3568 if (obj_pointer_type == nullptr)
3569 class_type_ptr->Clear();
3570 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003571 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003572 }
3573 }
3574 return true;
3575 }
3576 if (class_type_ptr)
3577 class_type_ptr->Clear();
3578 return false;
3579}
3580
3581bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003582ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003583{
3584 if (!type)
3585 return false;
3586
3587 clang::QualType qual_type (GetCanonicalQualType(type));
3588
3589 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3590 if (object_type)
3591 {
3592 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3593 if (interface)
3594 {
3595 class_name = interface->getNameAsString();
3596 return true;
3597 }
3598 }
3599 return false;
3600}
3601
3602
3603//----------------------------------------------------------------------
3604// Type Completion
3605//----------------------------------------------------------------------
3606
3607bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003608ClangASTContext::GetCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003609{
3610 if (!type)
3611 return false;
3612 const bool allow_completion = true;
3613 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3614}
3615
3616ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003617ClangASTContext::GetTypeName (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003618{
3619 std::string type_name;
3620 if (type)
3621 {
3622 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3623 clang::QualType qual_type(GetQualType(type));
3624 printing_policy.SuppressTagKeyword = true;
3625 printing_policy.LangOpts.WChar = true;
3626 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3627 if (typedef_type)
3628 {
3629 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3630 type_name = typedef_decl->getQualifiedNameAsString();
3631 }
3632 else
3633 {
3634 type_name = qual_type.getAsString(printing_policy);
3635 }
3636 }
3637 return ConstString(type_name);
3638}
3639
3640uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003641ClangASTContext::GetTypeInfo (lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003642{
3643 if (!type)
3644 return 0;
3645
3646 if (pointee_or_element_clang_type)
3647 pointee_or_element_clang_type->Clear();
3648
3649 clang::QualType qual_type (GetQualType(type));
3650
3651 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3652 switch (type_class)
3653 {
3654 case clang::Type::Builtin:
3655 {
3656 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3657
3658 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3659 switch (builtin_type->getKind())
3660 {
3661 case clang::BuiltinType::ObjCId:
3662 case clang::BuiltinType::ObjCClass:
3663 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003664 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003665 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3666 break;
3667
3668 case clang::BuiltinType::ObjCSel:
3669 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003670 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003671 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3672 break;
3673
3674 case clang::BuiltinType::Bool:
3675 case clang::BuiltinType::Char_U:
3676 case clang::BuiltinType::UChar:
3677 case clang::BuiltinType::WChar_U:
3678 case clang::BuiltinType::Char16:
3679 case clang::BuiltinType::Char32:
3680 case clang::BuiltinType::UShort:
3681 case clang::BuiltinType::UInt:
3682 case clang::BuiltinType::ULong:
3683 case clang::BuiltinType::ULongLong:
3684 case clang::BuiltinType::UInt128:
3685 case clang::BuiltinType::Char_S:
3686 case clang::BuiltinType::SChar:
3687 case clang::BuiltinType::WChar_S:
3688 case clang::BuiltinType::Short:
3689 case clang::BuiltinType::Int:
3690 case clang::BuiltinType::Long:
3691 case clang::BuiltinType::LongLong:
3692 case clang::BuiltinType::Int128:
3693 case clang::BuiltinType::Float:
3694 case clang::BuiltinType::Double:
3695 case clang::BuiltinType::LongDouble:
3696 builtin_type_flags |= eTypeIsScalar;
3697 if (builtin_type->isInteger())
3698 {
3699 builtin_type_flags |= eTypeIsInteger;
3700 if (builtin_type->isSignedInteger())
3701 builtin_type_flags |= eTypeIsSigned;
3702 }
3703 else if (builtin_type->isFloatingPoint())
3704 builtin_type_flags |= eTypeIsFloat;
3705 break;
3706 default:
3707 break;
3708 }
3709 return builtin_type_flags;
3710 }
3711
3712 case clang::Type::BlockPointer:
3713 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003714 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003715 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3716
3717 case clang::Type::Complex:
3718 {
3719 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3720 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3721 if (complex_type)
3722 {
3723 clang::QualType complex_element_type (complex_type->getElementType());
3724 if (complex_element_type->isIntegerType())
3725 complex_type_flags |= eTypeIsFloat;
3726 else if (complex_element_type->isFloatingType())
3727 complex_type_flags |= eTypeIsInteger;
3728 }
3729 return complex_type_flags;
3730 }
3731 break;
3732
3733 case clang::Type::ConstantArray:
3734 case clang::Type::DependentSizedArray:
3735 case clang::Type::IncompleteArray:
3736 case clang::Type::VariableArray:
3737 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003738 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003739 return eTypeHasChildren | eTypeIsArray;
3740
3741 case clang::Type::DependentName: return 0;
3742 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3743 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3744 case clang::Type::Decltype: return 0;
3745
3746 case clang::Type::Enum:
3747 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003748 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003749 return eTypeIsEnumeration | eTypeHasValue;
3750
3751 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003752 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003753 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003754 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003755
3756 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3757 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3758 case clang::Type::InjectedClassName: return 0;
3759
3760 case clang::Type::LValueReference:
3761 case clang::Type::RValueReference:
3762 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003763 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003764 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3765
3766 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3767
3768 case clang::Type::ObjCObjectPointer:
3769 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003770 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003771 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3772
3773 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3774 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3775
3776 case clang::Type::Pointer:
3777 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003778 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003779 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3780
3781 case clang::Type::Record:
3782 if (qual_type->getAsCXXRecordDecl())
3783 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3784 else
3785 return eTypeHasChildren | eTypeIsStructUnion;
3786 break;
3787 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3788 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3789 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3790
3791 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003792 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 +00003793 case clang::Type::TypeOfExpr: return 0;
3794 case clang::Type::TypeOf: return 0;
3795 case clang::Type::UnresolvedUsing: return 0;
3796
3797 case clang::Type::ExtVector:
3798 case clang::Type::Vector:
3799 {
3800 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3801 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3802 if (vector_type)
3803 {
3804 if (vector_type->isIntegerType())
3805 vector_type_flags |= eTypeIsFloat;
3806 else if (vector_type->isFloatingType())
3807 vector_type_flags |= eTypeIsInteger;
3808 }
3809 return vector_type_flags;
3810 }
3811 default: return 0;
3812 }
3813 return 0;
3814}
3815
3816
3817
3818lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003819ClangASTContext::GetMinimumLanguage (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003820{
3821 if (!type)
3822 return lldb::eLanguageTypeC;
3823
3824 // If the type is a reference, then resolve it to what it refers to first:
3825 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3826 if (qual_type->isAnyPointerType())
3827 {
3828 if (qual_type->isObjCObjectPointerType())
3829 return lldb::eLanguageTypeObjC;
3830
3831 clang::QualType pointee_type (qual_type->getPointeeType());
3832 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3833 return lldb::eLanguageTypeC_plus_plus;
3834 if (pointee_type->isObjCObjectOrInterfaceType())
3835 return lldb::eLanguageTypeObjC;
3836 if (pointee_type->isObjCClassType())
3837 return lldb::eLanguageTypeObjC;
3838 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3839 return lldb::eLanguageTypeObjC;
3840 }
3841 else
3842 {
3843 if (qual_type->isObjCObjectOrInterfaceType())
3844 return lldb::eLanguageTypeObjC;
3845 if (qual_type->getAsCXXRecordDecl())
3846 return lldb::eLanguageTypeC_plus_plus;
3847 switch (qual_type->getTypeClass())
3848 {
3849 default:
3850 break;
3851 case clang::Type::Builtin:
3852 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3853 {
3854 default:
3855 case clang::BuiltinType::Void:
3856 case clang::BuiltinType::Bool:
3857 case clang::BuiltinType::Char_U:
3858 case clang::BuiltinType::UChar:
3859 case clang::BuiltinType::WChar_U:
3860 case clang::BuiltinType::Char16:
3861 case clang::BuiltinType::Char32:
3862 case clang::BuiltinType::UShort:
3863 case clang::BuiltinType::UInt:
3864 case clang::BuiltinType::ULong:
3865 case clang::BuiltinType::ULongLong:
3866 case clang::BuiltinType::UInt128:
3867 case clang::BuiltinType::Char_S:
3868 case clang::BuiltinType::SChar:
3869 case clang::BuiltinType::WChar_S:
3870 case clang::BuiltinType::Short:
3871 case clang::BuiltinType::Int:
3872 case clang::BuiltinType::Long:
3873 case clang::BuiltinType::LongLong:
3874 case clang::BuiltinType::Int128:
3875 case clang::BuiltinType::Float:
3876 case clang::BuiltinType::Double:
3877 case clang::BuiltinType::LongDouble:
3878 break;
3879
3880 case clang::BuiltinType::NullPtr:
3881 return eLanguageTypeC_plus_plus;
3882
3883 case clang::BuiltinType::ObjCId:
3884 case clang::BuiltinType::ObjCClass:
3885 case clang::BuiltinType::ObjCSel:
3886 return eLanguageTypeObjC;
3887
3888 case clang::BuiltinType::Dependent:
3889 case clang::BuiltinType::Overload:
3890 case clang::BuiltinType::BoundMember:
3891 case clang::BuiltinType::UnknownAny:
3892 break;
3893 }
3894 break;
3895 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003896 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00003897 }
3898 }
3899 return lldb::eLanguageTypeC;
3900}
3901
3902lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003903ClangASTContext::GetTypeClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003904{
3905 if (!type)
3906 return lldb::eTypeClassInvalid;
3907
3908 clang::QualType qual_type(GetQualType(type));
3909
3910 switch (qual_type->getTypeClass())
3911 {
3912 case clang::Type::UnaryTransform: break;
3913 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
3914 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
3915 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
3916 case clang::Type::VariableArray: return lldb::eTypeClassArray;
3917 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
3918 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
3919 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
3920 case clang::Type::ExtVector: return lldb::eTypeClassVector;
3921 case clang::Type::Vector: return lldb::eTypeClassVector;
3922 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
3923 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
3924 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
3925 case clang::Type::Pointer: return lldb::eTypeClassPointer;
3926 case clang::Type::LValueReference: return lldb::eTypeClassReference;
3927 case clang::Type::RValueReference: return lldb::eTypeClassReference;
3928 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
3929 case clang::Type::Complex:
3930 if (qual_type->isComplexType())
3931 return lldb::eTypeClassComplexFloat;
3932 else
3933 return lldb::eTypeClassComplexInteger;
3934 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
3935 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
3936 case clang::Type::Record:
3937 {
3938 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3939 const clang::RecordDecl *record_decl = record_type->getDecl();
3940 if (record_decl->isUnion())
3941 return lldb::eTypeClassUnion;
3942 else if (record_decl->isStruct())
3943 return lldb::eTypeClassStruct;
3944 else
3945 return lldb::eTypeClassClass;
3946 }
3947 break;
3948 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
3949 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
3950 case clang::Type::UnresolvedUsing: break;
3951 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003952 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003953 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003954 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00003955
3956 case clang::Type::Attributed: break;
3957 case clang::Type::TemplateTypeParm: break;
3958 case clang::Type::SubstTemplateTypeParm: break;
3959 case clang::Type::SubstTemplateTypeParmPack:break;
3960 case clang::Type::Auto: break;
3961 case clang::Type::InjectedClassName: break;
3962 case clang::Type::DependentName: break;
3963 case clang::Type::DependentTemplateSpecialization: break;
3964 case clang::Type::PackExpansion: break;
3965
3966 case clang::Type::TypeOfExpr: break;
3967 case clang::Type::TypeOf: break;
3968 case clang::Type::Decltype: break;
3969 case clang::Type::TemplateSpecialization: break;
3970 case clang::Type::Atomic: break;
3971
3972 // pointer type decayed from an array or function type.
3973 case clang::Type::Decayed: break;
3974 case clang::Type::Adjusted: break;
3975 }
3976 // We don't know hot to display this type...
3977 return lldb::eTypeClassOther;
3978
3979}
3980
3981unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003982ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003983{
3984 if (type)
3985 return GetQualType(type).getQualifiers().getCVRQualifiers();
3986 return 0;
3987}
3988
3989//----------------------------------------------------------------------
3990// Creating related types
3991//----------------------------------------------------------------------
3992
Greg Claytona1e5dc82015-08-11 22:53:00 +00003993CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003994ClangASTContext::GetArrayElementType (lldb::opaque_compiler_type_t type, uint64_t *stride)
Greg Claytond8d4a572015-08-11 21:38:15 +00003995{
3996 if (type)
3997 {
3998 clang::QualType qual_type(GetCanonicalQualType(type));
3999
4000 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4001
4002 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004003 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004004
Greg Claytona1e5dc82015-08-11 22:53:00 +00004005 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00004006
4007 // TODO: the real stride will be >= this value.. find the real one!
4008 if (stride)
4009 *stride = element_type.GetByteSize(nullptr);
4010
4011 return element_type;
4012
4013 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004014 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004015}
4016
Greg Claytona1e5dc82015-08-11 22:53:00 +00004017CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004018ClangASTContext::GetCanonicalType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004019{
4020 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004021 return CompilerType (getASTContext(), GetCanonicalQualType(type));
4022 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004023}
4024
4025static clang::QualType
4026GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
4027{
4028 if (qual_type->isPointerType())
4029 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4030 else
4031 qual_type = qual_type.getUnqualifiedType();
4032 qual_type.removeLocalConst();
4033 qual_type.removeLocalRestrict();
4034 qual_type.removeLocalVolatile();
4035 return qual_type;
4036}
4037
Greg Claytona1e5dc82015-08-11 22:53:00 +00004038CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004039ClangASTContext::GetFullyUnqualifiedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004040{
4041 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004042 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4043 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004044}
4045
4046
4047int
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004048ClangASTContext::GetFunctionArgumentCount (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004049{
4050 if (type)
4051 {
4052 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4053 if (func)
4054 return func->getNumParams();
4055 }
4056 return -1;
4057}
4058
Greg Claytona1e5dc82015-08-11 22:53:00 +00004059CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004060ClangASTContext::GetFunctionArgumentTypeAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004061{
4062 if (type)
4063 {
4064 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4065 if (func)
4066 {
4067 const uint32_t num_args = func->getNumParams();
4068 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004069 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00004070 }
4071 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004072 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004073}
4074
Greg Claytona1e5dc82015-08-11 22:53:00 +00004075CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004076ClangASTContext::GetFunctionReturnType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004077{
4078 if (type)
4079 {
4080 clang::QualType qual_type(GetCanonicalQualType(type));
4081 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4082 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004083 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004084 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004085 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004086}
4087
4088size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004089ClangASTContext::GetNumMemberFunctions (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004090{
4091 size_t num_functions = 0;
4092 if (type)
4093 {
4094 clang::QualType qual_type(GetCanonicalQualType(type));
4095 switch (qual_type->getTypeClass()) {
4096 case clang::Type::Record:
4097 if (GetCompleteQualType (getASTContext(), qual_type))
4098 {
4099 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4100 const clang::RecordDecl *record_decl = record_type->getDecl();
4101 assert(record_decl);
4102 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4103 if (cxx_record_decl)
4104 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4105 }
4106 break;
4107
4108 case clang::Type::ObjCObjectPointer:
4109 if (GetCompleteType(type))
4110 {
4111 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4112 if (objc_class_type)
4113 {
4114 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4115 if (class_interface_decl)
4116 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4117 }
4118 }
4119 break;
4120
4121 case clang::Type::ObjCObject:
4122 case clang::Type::ObjCInterface:
4123 if (GetCompleteType(type))
4124 {
4125 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4126 if (objc_class_type)
4127 {
4128 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4129 if (class_interface_decl)
4130 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4131 }
4132 }
4133 break;
4134
4135
4136 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004137 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004138
4139 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004140 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004141
4142 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004143 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004144
4145 default:
4146 break;
4147 }
4148 }
4149 return num_functions;
4150}
4151
4152TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004153ClangASTContext::GetMemberFunctionAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004154{
Greg Claytonfe689042015-11-10 17:47:04 +00004155 std::string name;
Greg Claytond8d4a572015-08-11 21:38:15 +00004156 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytonfe689042015-11-10 17:47:04 +00004157 CompilerType clang_type;
4158 CompilerDecl clang_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00004159 if (type)
4160 {
4161 clang::QualType qual_type(GetCanonicalQualType(type));
4162 switch (qual_type->getTypeClass()) {
4163 case clang::Type::Record:
4164 if (GetCompleteQualType (getASTContext(), qual_type))
4165 {
4166 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4167 const clang::RecordDecl *record_decl = record_type->getDecl();
4168 assert(record_decl);
4169 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4170 if (cxx_record_decl)
4171 {
4172 auto method_iter = cxx_record_decl->method_begin();
4173 auto method_end = cxx_record_decl->method_end();
4174 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4175 {
4176 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004177 clang::CXXMethodDecl *cxx_method_decl = method_iter->getCanonicalDecl();
4178 if (cxx_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004179 {
Greg Claytonfe689042015-11-10 17:47:04 +00004180 name = cxx_method_decl->getDeclName().getAsString();
4181 if (cxx_method_decl->isStatic())
Greg Claytond8d4a572015-08-11 21:38:15 +00004182 kind = lldb::eMemberFunctionKindStaticMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004183 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004184 kind = lldb::eMemberFunctionKindConstructor;
Greg Claytonfe689042015-11-10 17:47:04 +00004185 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004186 kind = lldb::eMemberFunctionKindDestructor;
4187 else
4188 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004189 clang_type = CompilerType(this, cxx_method_decl->getType().getAsOpaquePtr());
4190 clang_decl = CompilerDecl(this, cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00004191 }
4192 }
4193 }
4194 }
4195 break;
4196
4197 case clang::Type::ObjCObjectPointer:
4198 if (GetCompleteType(type))
4199 {
4200 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4201 if (objc_class_type)
4202 {
4203 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4204 if (class_interface_decl)
4205 {
4206 auto method_iter = class_interface_decl->meth_begin();
4207 auto method_end = class_interface_decl->meth_end();
4208 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4209 {
4210 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004211 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4212 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004213 {
Greg Claytonfe689042015-11-10 17:47:04 +00004214 clang_decl = CompilerDecl(this, objc_method_decl);
4215 name = objc_method_decl->getSelector().getAsString();
4216 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004217 kind = lldb::eMemberFunctionKindStaticMethod;
4218 else
4219 kind = lldb::eMemberFunctionKindInstanceMethod;
4220 }
4221 }
4222 }
4223 }
4224 }
4225 break;
4226
4227 case clang::Type::ObjCObject:
4228 case clang::Type::ObjCInterface:
4229 if (GetCompleteType(type))
4230 {
4231 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4232 if (objc_class_type)
4233 {
4234 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4235 if (class_interface_decl)
4236 {
4237 auto method_iter = class_interface_decl->meth_begin();
4238 auto method_end = class_interface_decl->meth_end();
4239 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4240 {
4241 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004242 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4243 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004244 {
Greg Claytonfe689042015-11-10 17:47:04 +00004245 clang_decl = CompilerDecl(this, objc_method_decl);
4246 name = objc_method_decl->getSelector().getAsString();
4247 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004248 kind = lldb::eMemberFunctionKindStaticMethod;
4249 else
4250 kind = lldb::eMemberFunctionKindInstanceMethod;
4251 }
4252 }
4253 }
4254 }
4255 }
4256 break;
4257
4258 case clang::Type::Typedef:
4259 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
4260
4261 case clang::Type::Elaborated:
4262 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4263
4264 case clang::Type::Paren:
4265 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4266
4267 default:
4268 break;
4269 }
4270 }
4271
4272 if (kind == eMemberFunctionKindUnknown)
4273 return TypeMemberFunctionImpl();
Greg Claytonfe689042015-11-10 17:47:04 +00004274 else
4275 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Claytond8d4a572015-08-11 21:38:15 +00004276}
4277
Greg Claytona1e5dc82015-08-11 22:53:00 +00004278CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004279ClangASTContext::GetNonReferenceType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004280{
4281 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004282 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4283 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004284}
4285
Greg Claytona1e5dc82015-08-11 22:53:00 +00004286CompilerType
4287ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004288 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004289 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004290{
4291 if (type && typedef_name && typedef_name[0])
4292 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004293 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004294 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004295 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004296 clang::ASTContext* clang_ast = ast->getASTContext();
4297 clang::QualType qual_type (GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004298
4299 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004300 if (decl_ctx == nullptr)
4301 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004302
Greg Claytond8d4a572015-08-11 21:38:15 +00004303 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4304 decl_ctx,
4305 clang::SourceLocation(),
4306 clang::SourceLocation(),
4307 &clang_ast->Idents.get(typedef_name),
4308 clang_ast->getTrivialTypeSourceInfo(qual_type));
4309
4310 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4311
4312 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004313 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004314 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004315 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004316
4317}
4318
Greg Claytona1e5dc82015-08-11 22:53:00 +00004319CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004320ClangASTContext::GetPointeeType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004321{
4322 if (type)
4323 {
4324 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004325 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004326 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004327 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004328}
4329
Greg Claytona1e5dc82015-08-11 22:53:00 +00004330CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004331ClangASTContext::GetPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004332{
4333 if (type)
4334 {
4335 clang::QualType qual_type (GetQualType(type));
4336
4337 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4338 switch (type_class)
4339 {
4340 case clang::Type::ObjCObject:
4341 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004342 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004343
4344 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004345 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004346 }
4347 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004348 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004349}
4350
Greg Clayton56939cb2015-09-17 22:23:34 +00004351
4352CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004353ClangASTContext::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004354{
4355 if (type)
4356 return CompilerType(this, getASTContext()->getLValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4357 else
4358 return CompilerType();
4359}
4360
4361CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004362ClangASTContext::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004363{
4364 if (type)
4365 return CompilerType(this, getASTContext()->getRValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4366 else
4367 return CompilerType();
4368}
4369
4370CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004371ClangASTContext::AddConstModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004372{
4373 if (type)
4374 {
4375 clang::QualType result(GetQualType(type));
4376 result.addConst();
4377 return CompilerType (this, result.getAsOpaquePtr());
4378 }
4379 return CompilerType();
4380}
4381
4382CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004383ClangASTContext::AddVolatileModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004384{
4385 if (type)
4386 {
4387 clang::QualType result(GetQualType(type));
4388 result.addVolatile();
4389 return CompilerType (this, result.getAsOpaquePtr());
4390 }
4391 return CompilerType();
4392
4393}
4394
4395CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004396ClangASTContext::AddRestrictModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004397{
4398 if (type)
4399 {
4400 clang::QualType result(GetQualType(type));
4401 result.addRestrict();
4402 return CompilerType (this, result.getAsOpaquePtr());
4403 }
4404 return CompilerType();
4405
4406}
4407
4408CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004409ClangASTContext::CreateTypedef (lldb::opaque_compiler_type_t type, const char *typedef_name, const CompilerDeclContext &compiler_decl_ctx)
Greg Clayton56939cb2015-09-17 22:23:34 +00004410{
4411 if (type)
4412 {
4413 clang::ASTContext* clang_ast = getASTContext();
4414 clang::QualType qual_type (GetQualType(type));
4415
4416 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4417 if (decl_ctx == nullptr)
4418 decl_ctx = getASTContext()->getTranslationUnitDecl();
4419
4420 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4421 decl_ctx,
4422 clang::SourceLocation(),
4423 clang::SourceLocation(),
4424 &clang_ast->Idents.get(typedef_name),
4425 clang_ast->getTrivialTypeSourceInfo(qual_type));
4426
4427 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4428
4429 // Get a uniqued clang::QualType for the typedef decl type
4430 return CompilerType (this, clang_ast->getTypedefType (decl).getAsOpaquePtr());
4431
4432 }
4433 return CompilerType();
4434
4435}
4436
Greg Claytona1e5dc82015-08-11 22:53:00 +00004437CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004438ClangASTContext::GetTypedefedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004439{
4440 if (type)
4441 {
4442 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4443 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004444 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004445 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004446 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004447}
4448
Greg Claytona1e5dc82015-08-11 22:53:00 +00004449CompilerType
4450ClangASTContext::RemoveFastQualifiers (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004451{
Ryan Brown57bee1e2015-09-14 22:45:11 +00004452 if (IsClangType(type))
Greg Claytond8d4a572015-08-11 21:38:15 +00004453 {
4454 clang::QualType qual_type(GetQualType(type));
4455 qual_type.getQualifiers().removeFastQualifiers();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004456 return CompilerType (type.GetTypeSystem(), qual_type.getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00004457 }
4458 return type;
4459}
4460
4461
4462//----------------------------------------------------------------------
4463// Create related types using the current type's AST
4464//----------------------------------------------------------------------
4465
Greg Claytona1e5dc82015-08-11 22:53:00 +00004466CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004467ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004468{
Greg Clayton99558cc42015-08-24 23:46:31 +00004469 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004470}
4471//----------------------------------------------------------------------
4472// Exploring the type
4473//----------------------------------------------------------------------
4474
4475uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004476ClangASTContext::GetBitSize (lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Greg Claytond8d4a572015-08-11 21:38:15 +00004477{
4478 if (GetCompleteType (type))
4479 {
4480 clang::QualType qual_type(GetCanonicalQualType(type));
4481 switch (qual_type->getTypeClass())
4482 {
4483 case clang::Type::ObjCInterface:
4484 case clang::Type::ObjCObject:
4485 {
4486 ExecutionContext exe_ctx (exe_scope);
4487 Process *process = exe_ctx.GetProcessPtr();
4488 if (process)
4489 {
4490 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4491 if (objc_runtime)
4492 {
4493 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004494 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004495 return bit_size;
4496 }
4497 }
4498 else
4499 {
4500 static bool g_printed = false;
4501 if (!g_printed)
4502 {
4503 StreamString s;
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00004504 DumpTypeDescription(type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00004505
4506 llvm::outs() << "warning: trying to determine the size of type ";
4507 llvm::outs() << s.GetString() << "\n";
4508 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4509 llvm::outs() << "backtrace:\n";
4510 llvm::sys::PrintStackTrace(llvm::outs());
4511 llvm::outs() << "\n";
4512 g_printed = true;
4513 }
4514 }
4515 }
4516 // fallthrough
4517 default:
4518 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4519 if (bit_size == 0)
4520 {
4521 if (qual_type->isIncompleteArrayType())
4522 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4523 }
4524 if (qual_type->isObjCObjectOrInterfaceType())
4525 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4526 return bit_size;
4527 }
4528 }
4529 return 0;
4530}
4531
4532size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004533ClangASTContext::GetTypeBitAlign (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004534{
4535 if (GetCompleteType(type))
4536 return getASTContext()->getTypeAlign(GetQualType(type));
4537 return 0;
4538}
4539
4540
4541lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004542ClangASTContext::GetEncoding (lldb::opaque_compiler_type_t type, uint64_t &count)
Greg Claytond8d4a572015-08-11 21:38:15 +00004543{
4544 if (!type)
4545 return lldb::eEncodingInvalid;
4546
4547 count = 1;
4548 clang::QualType qual_type(GetCanonicalQualType(type));
4549
4550 switch (qual_type->getTypeClass())
4551 {
4552 case clang::Type::UnaryTransform:
4553 break;
4554
4555 case clang::Type::FunctionNoProto:
4556 case clang::Type::FunctionProto:
4557 break;
4558
4559 case clang::Type::IncompleteArray:
4560 case clang::Type::VariableArray:
4561 break;
4562
4563 case clang::Type::ConstantArray:
4564 break;
4565
4566 case clang::Type::ExtVector:
4567 case clang::Type::Vector:
4568 // TODO: Set this to more than one???
4569 break;
4570
4571 case clang::Type::Builtin:
4572 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4573 {
Greg Claytond8d4a572015-08-11 21:38:15 +00004574 case clang::BuiltinType::Void:
4575 break;
4576
4577 case clang::BuiltinType::Bool:
4578 case clang::BuiltinType::Char_S:
4579 case clang::BuiltinType::SChar:
4580 case clang::BuiltinType::WChar_S:
4581 case clang::BuiltinType::Char16:
4582 case clang::BuiltinType::Char32:
4583 case clang::BuiltinType::Short:
4584 case clang::BuiltinType::Int:
4585 case clang::BuiltinType::Long:
4586 case clang::BuiltinType::LongLong:
4587 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4588
4589 case clang::BuiltinType::Char_U:
4590 case clang::BuiltinType::UChar:
4591 case clang::BuiltinType::WChar_U:
4592 case clang::BuiltinType::UShort:
4593 case clang::BuiltinType::UInt:
4594 case clang::BuiltinType::ULong:
4595 case clang::BuiltinType::ULongLong:
4596 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4597
Greg Claytondee40e72015-11-03 23:23:22 +00004598 case clang::BuiltinType::Half:
Greg Claytond8d4a572015-08-11 21:38:15 +00004599 case clang::BuiltinType::Float:
4600 case clang::BuiltinType::Double:
4601 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4602
4603 case clang::BuiltinType::ObjCClass:
4604 case clang::BuiltinType::ObjCId:
4605 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4606
4607 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4608
4609 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4610 case clang::BuiltinType::Kind::BoundMember:
4611 case clang::BuiltinType::Kind::BuiltinFn:
4612 case clang::BuiltinType::Kind::Dependent:
Ed Mastec6dd6512015-09-23 18:32:34 +00004613 case clang::BuiltinType::Kind::OCLClkEvent:
Greg Claytond8d4a572015-08-11 21:38:15 +00004614 case clang::BuiltinType::Kind::OCLEvent:
4615 case clang::BuiltinType::Kind::OCLImage1d:
4616 case clang::BuiltinType::Kind::OCLImage1dArray:
4617 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4618 case clang::BuiltinType::Kind::OCLImage2d:
4619 case clang::BuiltinType::Kind::OCLImage2dArray:
Ed Mastec6dd6512015-09-23 18:32:34 +00004620 case clang::BuiltinType::Kind::OCLImage2dArrayDepth:
4621 case clang::BuiltinType::Kind::OCLImage2dArrayMSAA:
4622 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepth:
4623 case clang::BuiltinType::Kind::OCLImage2dDepth:
4624 case clang::BuiltinType::Kind::OCLImage2dMSAA:
4625 case clang::BuiltinType::Kind::OCLImage2dMSAADepth:
Greg Claytond8d4a572015-08-11 21:38:15 +00004626 case clang::BuiltinType::Kind::OCLImage3d:
Ed Mastec6dd6512015-09-23 18:32:34 +00004627 case clang::BuiltinType::Kind::OCLQueue:
4628 case clang::BuiltinType::Kind::OCLNDRange:
4629 case clang::BuiltinType::Kind::OCLReserveID:
Greg Claytond8d4a572015-08-11 21:38:15 +00004630 case clang::BuiltinType::Kind::OCLSampler:
Ed Mastec6dd6512015-09-23 18:32:34 +00004631 case clang::BuiltinType::Kind::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00004632 case clang::BuiltinType::Kind::Overload:
4633 case clang::BuiltinType::Kind::PseudoObject:
4634 case clang::BuiltinType::Kind::UnknownAny:
4635 break;
4636 }
4637 break;
4638 // All pointer types are represented as unsigned integer encodings.
4639 // We may nee to add a eEncodingPointer if we ever need to know the
4640 // difference
4641 case clang::Type::ObjCObjectPointer:
4642 case clang::Type::BlockPointer:
4643 case clang::Type::Pointer:
4644 case clang::Type::LValueReference:
4645 case clang::Type::RValueReference:
4646 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4647 case clang::Type::Complex:
4648 {
4649 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4650 if (qual_type->isComplexType())
4651 encoding = lldb::eEncodingIEEE754;
4652 else
4653 {
4654 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4655 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004656 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004657 else
4658 encoding = lldb::eEncodingSint;
4659 }
4660 count = 2;
4661 return encoding;
4662 }
4663
4664 case clang::Type::ObjCInterface: break;
4665 case clang::Type::Record: break;
4666 case clang::Type::Enum: return lldb::eEncodingSint;
4667 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004668 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004669
4670 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004671 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004672
4673 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004674 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004675
4676 case clang::Type::DependentSizedArray:
4677 case clang::Type::DependentSizedExtVector:
4678 case clang::Type::UnresolvedUsing:
4679 case clang::Type::Attributed:
4680 case clang::Type::TemplateTypeParm:
4681 case clang::Type::SubstTemplateTypeParm:
4682 case clang::Type::SubstTemplateTypeParmPack:
4683 case clang::Type::Auto:
4684 case clang::Type::InjectedClassName:
4685 case clang::Type::DependentName:
4686 case clang::Type::DependentTemplateSpecialization:
4687 case clang::Type::PackExpansion:
4688 case clang::Type::ObjCObject:
4689
4690 case clang::Type::TypeOfExpr:
4691 case clang::Type::TypeOf:
4692 case clang::Type::Decltype:
4693 case clang::Type::TemplateSpecialization:
4694 case clang::Type::Atomic:
4695 case clang::Type::Adjusted:
4696 break;
4697
4698 // pointer type decayed from an array or function type.
4699 case clang::Type::Decayed:
4700 break;
4701 }
4702 count = 0;
4703 return lldb::eEncodingInvalid;
4704}
4705
4706lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004707ClangASTContext::GetFormat (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004708{
4709 if (!type)
4710 return lldb::eFormatDefault;
4711
4712 clang::QualType qual_type(GetCanonicalQualType(type));
4713
4714 switch (qual_type->getTypeClass())
4715 {
4716 case clang::Type::UnaryTransform:
4717 break;
4718
4719 case clang::Type::FunctionNoProto:
4720 case clang::Type::FunctionProto:
4721 break;
4722
4723 case clang::Type::IncompleteArray:
4724 case clang::Type::VariableArray:
4725 break;
4726
4727 case clang::Type::ConstantArray:
4728 return lldb::eFormatVoid; // no value
4729
4730 case clang::Type::ExtVector:
4731 case clang::Type::Vector:
4732 break;
4733
4734 case clang::Type::Builtin:
4735 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4736 {
4737 //default: assert(0 && "Unknown builtin type!");
4738 case clang::BuiltinType::UnknownAny:
4739 case clang::BuiltinType::Void:
4740 case clang::BuiltinType::BoundMember:
4741 break;
4742
4743 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4744 case clang::BuiltinType::Char_S:
4745 case clang::BuiltinType::SChar:
4746 case clang::BuiltinType::WChar_S:
4747 case clang::BuiltinType::Char_U:
4748 case clang::BuiltinType::UChar:
4749 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4750 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4751 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4752 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4753 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4754 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4755 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4756 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4757 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4758 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4759 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4760 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4761 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
Greg Claytondee40e72015-11-03 23:23:22 +00004762 case clang::BuiltinType::Half:
4763 case clang::BuiltinType::Float:
4764 case clang::BuiltinType::Double:
Greg Claytond8d4a572015-08-11 21:38:15 +00004765 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004766 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004767 return lldb::eFormatHex;
4768 }
4769 break;
4770 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4771 case clang::Type::BlockPointer: return lldb::eFormatHex;
4772 case clang::Type::Pointer: return lldb::eFormatHex;
4773 case clang::Type::LValueReference:
4774 case clang::Type::RValueReference: return lldb::eFormatHex;
4775 case clang::Type::MemberPointer: break;
4776 case clang::Type::Complex:
4777 {
4778 if (qual_type->isComplexType())
4779 return lldb::eFormatComplex;
4780 else
4781 return lldb::eFormatComplexInteger;
4782 }
4783 case clang::Type::ObjCInterface: break;
4784 case clang::Type::Record: break;
4785 case clang::Type::Enum: return lldb::eFormatEnum;
4786 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004787 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004788 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004789 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004790 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004791 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004792 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004793 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004794 case clang::Type::DependentSizedArray:
4795 case clang::Type::DependentSizedExtVector:
4796 case clang::Type::UnresolvedUsing:
4797 case clang::Type::Attributed:
4798 case clang::Type::TemplateTypeParm:
4799 case clang::Type::SubstTemplateTypeParm:
4800 case clang::Type::SubstTemplateTypeParmPack:
4801 case clang::Type::InjectedClassName:
4802 case clang::Type::DependentName:
4803 case clang::Type::DependentTemplateSpecialization:
4804 case clang::Type::PackExpansion:
4805 case clang::Type::ObjCObject:
4806
4807 case clang::Type::TypeOfExpr:
4808 case clang::Type::TypeOf:
4809 case clang::Type::Decltype:
4810 case clang::Type::TemplateSpecialization:
4811 case clang::Type::Atomic:
4812 case clang::Type::Adjusted:
4813 break;
4814
4815 // pointer type decayed from an array or function type.
4816 case clang::Type::Decayed:
4817 break;
4818 }
4819 // We don't know hot to display this type...
4820 return lldb::eFormatBytes;
4821}
4822
4823static bool
4824ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4825{
4826 while (class_interface_decl)
4827 {
4828 if (class_interface_decl->ivar_size() > 0)
4829 return true;
4830
4831 if (check_superclass)
4832 class_interface_decl = class_interface_decl->getSuperClass();
4833 else
4834 break;
4835 }
4836 return false;
4837}
4838
4839uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004840ClangASTContext::GetNumChildren (lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00004841{
4842 if (!type)
4843 return 0;
4844
4845 uint32_t num_children = 0;
4846 clang::QualType qual_type(GetQualType(type));
4847 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4848 switch (type_class)
4849 {
4850 case clang::Type::Builtin:
4851 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4852 {
4853 case clang::BuiltinType::ObjCId: // child is Class
4854 case clang::BuiltinType::ObjCClass: // child is Class
4855 num_children = 1;
4856 break;
4857
4858 default:
4859 break;
4860 }
4861 break;
4862
4863 case clang::Type::Complex: return 0;
4864
4865 case clang::Type::Record:
4866 if (GetCompleteQualType (getASTContext(), qual_type))
4867 {
4868 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4869 const clang::RecordDecl *record_decl = record_type->getDecl();
4870 assert(record_decl);
4871 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4872 if (cxx_record_decl)
4873 {
4874 if (omit_empty_base_classes)
4875 {
4876 // Check each base classes to see if it or any of its
4877 // base classes contain any fields. This can help
4878 // limit the noise in variable views by not having to
4879 // show base classes that contain no members.
4880 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
4881 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
4882 base_class != base_class_end;
4883 ++base_class)
4884 {
4885 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
4886
4887 // Skip empty base classes
4888 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
4889 continue;
4890
4891 num_children++;
4892 }
4893 }
4894 else
4895 {
4896 // Include all base classes
4897 num_children += cxx_record_decl->getNumBases();
4898 }
4899
4900 }
4901 clang::RecordDecl::field_iterator field, field_end;
4902 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
4903 ++num_children;
4904 }
4905 break;
4906
4907 case clang::Type::ObjCObject:
4908 case clang::Type::ObjCInterface:
4909 if (GetCompleteQualType (getASTContext(), qual_type))
4910 {
4911 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4912 assert (objc_class_type);
4913 if (objc_class_type)
4914 {
4915 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4916
4917 if (class_interface_decl)
4918 {
4919
4920 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
4921 if (superclass_interface_decl)
4922 {
4923 if (omit_empty_base_classes)
4924 {
4925 if (ObjCDeclHasIVars (superclass_interface_decl, true))
4926 ++num_children;
4927 }
4928 else
4929 ++num_children;
4930 }
4931
4932 num_children += class_interface_decl->ivar_size();
4933 }
4934 }
4935 }
4936 break;
4937
4938 case clang::Type::ObjCObjectPointer:
4939 {
4940 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
4941 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004942 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004943 // If this type points to a simple type, then it has 1 child
4944 if (num_pointee_children == 0)
4945 num_children = 1;
4946 else
4947 num_children = num_pointee_children;
4948 }
4949 break;
4950
4951 case clang::Type::Vector:
4952 case clang::Type::ExtVector:
4953 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
4954 break;
4955
4956 case clang::Type::ConstantArray:
4957 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
4958 break;
4959
4960 case clang::Type::Pointer:
4961 {
4962 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
4963 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00004964 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004965 if (num_pointee_children == 0)
4966 {
4967 // We have a pointer to a pointee type that claims it has no children.
4968 // We will want to look at
4969 num_children = GetNumPointeeChildren (pointee_type);
4970 }
4971 else
4972 num_children = num_pointee_children;
4973 }
4974 break;
4975
4976 case clang::Type::LValueReference:
4977 case clang::Type::RValueReference:
4978 {
4979 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
4980 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00004981 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004982 // If this type points to a simple type, then it has 1 child
4983 if (num_pointee_children == 0)
4984 num_children = 1;
4985 else
4986 num_children = num_pointee_children;
4987 }
4988 break;
4989
4990
4991 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004992 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004993 break;
4994
4995 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004996 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00004997 break;
4998
4999 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005000 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005001 break;
5002 default:
5003 break;
5004 }
5005 return num_children;
5006}
5007
Greg Clayton56939cb2015-09-17 22:23:34 +00005008CompilerType
5009ClangASTContext::GetBuiltinTypeByName (const ConstString &name)
5010{
5011 return GetBasicType (GetBasicTypeEnumeration (name));
5012}
5013
Greg Claytond8d4a572015-08-11 21:38:15 +00005014lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005015ClangASTContext::GetBasicTypeEnumeration (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005016{
5017 if (type)
5018 {
5019 clang::QualType qual_type(GetQualType(type));
5020 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5021 if (type_class == clang::Type::Builtin)
5022 {
5023 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5024 {
5025 case clang::BuiltinType::Void: return eBasicTypeVoid;
5026 case clang::BuiltinType::Bool: return eBasicTypeBool;
5027 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
5028 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
5029 case clang::BuiltinType::Char16: return eBasicTypeChar16;
5030 case clang::BuiltinType::Char32: return eBasicTypeChar32;
5031 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
5032 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
5033 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
5034 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
5035 case clang::BuiltinType::Short: return eBasicTypeShort;
5036 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
5037 case clang::BuiltinType::Int: return eBasicTypeInt;
5038 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
5039 case clang::BuiltinType::Long: return eBasicTypeLong;
5040 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
5041 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
5042 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
5043 case clang::BuiltinType::Int128: return eBasicTypeInt128;
5044 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
5045
5046 case clang::BuiltinType::Half: return eBasicTypeHalf;
5047 case clang::BuiltinType::Float: return eBasicTypeFloat;
5048 case clang::BuiltinType::Double: return eBasicTypeDouble;
5049 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
5050
5051 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
5052 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
5053 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
5054 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005055 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00005056 return eBasicTypeOther;
5057 }
5058 }
5059 }
5060 return eBasicTypeInvalid;
5061}
5062
Greg Clayton99558cc42015-08-24 23:46:31 +00005063void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005064ClangASTContext::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 +00005065{
5066 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5067 if (enum_type)
5068 {
5069 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5070 if (enum_decl)
5071 {
5072 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
5073
5074 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5075 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5076 {
5077 ConstString name(enum_pos->getNameAsString().c_str());
5078 if (!callback (integer_type, name, enum_pos->getInitVal()))
5079 break;
5080 }
5081 }
5082 }
5083}
5084
Greg Claytond8d4a572015-08-11 21:38:15 +00005085
5086#pragma mark Aggregate Types
5087
5088uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005089ClangASTContext::GetNumFields (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005090{
5091 if (!type)
5092 return 0;
5093
5094 uint32_t count = 0;
5095 clang::QualType qual_type(GetCanonicalQualType(type));
5096 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5097 switch (type_class)
5098 {
5099 case clang::Type::Record:
5100 if (GetCompleteType(type))
5101 {
5102 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5103 if (record_type)
5104 {
5105 clang::RecordDecl *record_decl = record_type->getDecl();
5106 if (record_decl)
5107 {
5108 uint32_t field_idx = 0;
5109 clang::RecordDecl::field_iterator field, field_end;
5110 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5111 ++field_idx;
5112 count = field_idx;
5113 }
5114 }
5115 }
5116 break;
5117
5118 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005119 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005120 break;
5121
5122 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005123 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005124 break;
5125
5126 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005127 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005128 break;
5129
5130 case clang::Type::ObjCObjectPointer:
5131 if (GetCompleteType(type))
5132 {
5133 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5134 if (objc_class_type)
5135 {
5136 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5137
5138 if (class_interface_decl)
5139 count = class_interface_decl->ivar_size();
5140 }
5141 }
5142 break;
5143
5144 case clang::Type::ObjCObject:
5145 case clang::Type::ObjCInterface:
5146 if (GetCompleteType(type))
5147 {
5148 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5149 if (objc_class_type)
5150 {
5151 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5152
5153 if (class_interface_decl)
5154 count = class_interface_decl->ivar_size();
5155 }
5156 }
5157 break;
5158
5159 default:
5160 break;
5161 }
5162 return count;
5163}
5164
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005165static lldb::opaque_compiler_type_t
Greg Claytond8d4a572015-08-11 21:38:15 +00005166GetObjCFieldAtIndex (clang::ASTContext *ast,
5167 clang::ObjCInterfaceDecl *class_interface_decl,
5168 size_t idx,
5169 std::string& name,
5170 uint64_t *bit_offset_ptr,
5171 uint32_t *bitfield_bit_size_ptr,
5172 bool *is_bitfield_ptr)
5173{
5174 if (class_interface_decl)
5175 {
5176 if (idx < (class_interface_decl->ivar_size()))
5177 {
5178 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5179 uint32_t ivar_idx = 0;
5180
5181 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5182 {
5183 if (ivar_idx == idx)
5184 {
5185 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5186
5187 clang::QualType ivar_qual_type(ivar_decl->getType());
5188
5189 name.assign(ivar_decl->getNameAsString());
5190
5191 if (bit_offset_ptr)
5192 {
5193 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5194 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5195 }
5196
5197 const bool is_bitfield = ivar_pos->isBitField();
5198
5199 if (bitfield_bit_size_ptr)
5200 {
5201 *bitfield_bit_size_ptr = 0;
5202
5203 if (is_bitfield && ast)
5204 {
5205 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5206 llvm::APSInt bitfield_apsint;
5207 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5208 {
5209 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5210 }
5211 }
5212 }
5213 if (is_bitfield_ptr)
5214 *is_bitfield_ptr = is_bitfield;
5215
5216 return ivar_qual_type.getAsOpaquePtr();
5217 }
5218 }
5219 }
5220 }
5221 return nullptr;
5222}
5223
Greg Claytona1e5dc82015-08-11 22:53:00 +00005224CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005225ClangASTContext::GetFieldAtIndex (lldb::opaque_compiler_type_t type, size_t idx,
Greg Claytond8d4a572015-08-11 21:38:15 +00005226 std::string& name,
5227 uint64_t *bit_offset_ptr,
5228 uint32_t *bitfield_bit_size_ptr,
5229 bool *is_bitfield_ptr)
5230{
5231 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005232 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005233
5234 clang::QualType qual_type(GetCanonicalQualType(type));
5235 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5236 switch (type_class)
5237 {
5238 case clang::Type::Record:
5239 if (GetCompleteType(type))
5240 {
5241 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5242 const clang::RecordDecl *record_decl = record_type->getDecl();
5243 uint32_t field_idx = 0;
5244 clang::RecordDecl::field_iterator field, field_end;
5245 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5246 {
5247 if (idx == field_idx)
5248 {
5249 // Print the member type if requested
5250 // Print the member name and equal sign
5251 name.assign(field->getNameAsString());
5252
5253 // Figure out the type byte size (field_type_info.first) and
5254 // alignment (field_type_info.second) from the AST context.
5255 if (bit_offset_ptr)
5256 {
5257 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5258 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5259 }
5260
5261 const bool is_bitfield = field->isBitField();
5262
5263 if (bitfield_bit_size_ptr)
5264 {
5265 *bitfield_bit_size_ptr = 0;
5266
5267 if (is_bitfield)
5268 {
5269 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5270 llvm::APSInt bitfield_apsint;
5271 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5272 {
5273 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5274 }
5275 }
5276 }
5277 if (is_bitfield_ptr)
5278 *is_bitfield_ptr = is_bitfield;
5279
Greg Claytona1e5dc82015-08-11 22:53:00 +00005280 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005281 }
5282 }
5283 }
5284 break;
5285
5286 case clang::Type::ObjCObjectPointer:
5287 if (GetCompleteType(type))
5288 {
5289 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5290 if (objc_class_type)
5291 {
5292 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005293 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 +00005294 }
5295 }
5296 break;
5297
5298 case clang::Type::ObjCObject:
5299 case clang::Type::ObjCInterface:
5300 if (GetCompleteType(type))
5301 {
5302 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5303 assert (objc_class_type);
5304 if (objc_class_type)
5305 {
5306 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005307 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 +00005308 }
5309 }
5310 break;
5311
5312
5313 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005314 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005315 GetFieldAtIndex (idx,
5316 name,
5317 bit_offset_ptr,
5318 bitfield_bit_size_ptr,
5319 is_bitfield_ptr);
5320
5321 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005322 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005323 GetFieldAtIndex (idx,
5324 name,
5325 bit_offset_ptr,
5326 bitfield_bit_size_ptr,
5327 is_bitfield_ptr);
5328
5329 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005330 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005331 GetFieldAtIndex (idx,
5332 name,
5333 bit_offset_ptr,
5334 bitfield_bit_size_ptr,
5335 is_bitfield_ptr);
5336
5337 default:
5338 break;
5339 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005340 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005341}
5342
Greg Clayton99558cc42015-08-24 23:46:31 +00005343uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005344ClangASTContext::GetNumDirectBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005345{
5346 uint32_t count = 0;
5347 clang::QualType qual_type(GetCanonicalQualType(type));
5348 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5349 switch (type_class)
5350 {
5351 case clang::Type::Record:
5352 if (GetCompleteType(type))
5353 {
5354 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5355 if (cxx_record_decl)
5356 count = cxx_record_decl->getNumBases();
5357 }
5358 break;
5359
5360 case clang::Type::ObjCObjectPointer:
5361 count = GetPointeeType(type).GetNumDirectBaseClasses();
5362 break;
5363
5364 case clang::Type::ObjCObject:
5365 if (GetCompleteType(type))
5366 {
5367 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5368 if (objc_class_type)
5369 {
5370 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5371
5372 if (class_interface_decl && class_interface_decl->getSuperClass())
5373 count = 1;
5374 }
5375 }
5376 break;
5377 case clang::Type::ObjCInterface:
5378 if (GetCompleteType(type))
5379 {
5380 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5381 if (objc_interface_type)
5382 {
5383 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5384
5385 if (class_interface_decl && class_interface_decl->getSuperClass())
5386 count = 1;
5387 }
5388 }
5389 break;
5390
5391
5392 case clang::Type::Typedef:
5393 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5394 break;
5395
5396 case clang::Type::Elaborated:
5397 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5398 break;
5399
5400 case clang::Type::Paren:
5401 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5402
5403 default:
5404 break;
5405 }
5406 return count;
5407
5408}
5409
5410uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005411ClangASTContext::GetNumVirtualBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005412{
5413 uint32_t count = 0;
5414 clang::QualType qual_type(GetCanonicalQualType(type));
5415 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5416 switch (type_class)
5417 {
5418 case clang::Type::Record:
5419 if (GetCompleteType(type))
5420 {
5421 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5422 if (cxx_record_decl)
5423 count = cxx_record_decl->getNumVBases();
5424 }
5425 break;
5426
5427 case clang::Type::Typedef:
5428 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5429 break;
5430
5431 case clang::Type::Elaborated:
5432 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5433 break;
5434
5435 case clang::Type::Paren:
5436 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5437 break;
5438
5439 default:
5440 break;
5441 }
5442 return count;
5443
5444}
5445
5446CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005447ClangASTContext::GetDirectBaseClassAtIndex (lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00005448{
5449 clang::QualType qual_type(GetCanonicalQualType(type));
5450 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5451 switch (type_class)
5452 {
5453 case clang::Type::Record:
5454 if (GetCompleteType(type))
5455 {
5456 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5457 if (cxx_record_decl)
5458 {
5459 uint32_t curr_idx = 0;
5460 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5461 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5462 base_class != base_class_end;
5463 ++base_class, ++curr_idx)
5464 {
5465 if (curr_idx == idx)
5466 {
5467 if (bit_offset_ptr)
5468 {
5469 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5470 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5471 if (base_class->isVirtual())
5472 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5473 else
5474 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5475 }
5476 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5477 }
5478 }
5479 }
5480 }
5481 break;
5482
5483 case clang::Type::ObjCObjectPointer:
5484 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5485
5486 case clang::Type::ObjCObject:
5487 if (idx == 0 && GetCompleteType(type))
5488 {
5489 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5490 if (objc_class_type)
5491 {
5492 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5493
5494 if (class_interface_decl)
5495 {
5496 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5497 if (superclass_interface_decl)
5498 {
5499 if (bit_offset_ptr)
5500 *bit_offset_ptr = 0;
5501 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5502 }
5503 }
5504 }
5505 }
5506 break;
5507 case clang::Type::ObjCInterface:
5508 if (idx == 0 && GetCompleteType(type))
5509 {
5510 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5511 if (objc_interface_type)
5512 {
5513 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5514
5515 if (class_interface_decl)
5516 {
5517 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5518 if (superclass_interface_decl)
5519 {
5520 if (bit_offset_ptr)
5521 *bit_offset_ptr = 0;
5522 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5523 }
5524 }
5525 }
5526 }
5527 break;
5528
5529
5530 case clang::Type::Typedef:
5531 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5532
5533 case clang::Type::Elaborated:
5534 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5535
5536 case clang::Type::Paren:
5537 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5538
5539 default:
5540 break;
5541 }
5542 return CompilerType();
5543}
5544
5545CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005546ClangASTContext::GetVirtualBaseClassAtIndex (lldb::opaque_compiler_type_t type,
Greg Clayton99558cc42015-08-24 23:46:31 +00005547 size_t idx,
5548 uint32_t *bit_offset_ptr)
5549{
5550 clang::QualType qual_type(GetCanonicalQualType(type));
5551 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5552 switch (type_class)
5553 {
5554 case clang::Type::Record:
5555 if (GetCompleteType(type))
5556 {
5557 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5558 if (cxx_record_decl)
5559 {
5560 uint32_t curr_idx = 0;
5561 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5562 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5563 base_class != base_class_end;
5564 ++base_class, ++curr_idx)
5565 {
5566 if (curr_idx == idx)
5567 {
5568 if (bit_offset_ptr)
5569 {
5570 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5571 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5572 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5573
5574 }
5575 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5576 }
5577 }
5578 }
5579 }
5580 break;
5581
5582 case clang::Type::Typedef:
5583 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5584
5585 case clang::Type::Elaborated:
5586 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5587
5588 case clang::Type::Paren:
5589 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5590
5591 default:
5592 break;
5593 }
5594 return CompilerType();
5595
5596}
5597
Greg Claytond8d4a572015-08-11 21:38:15 +00005598// If a pointer to a pointee type (the clang_type arg) says that it has no
5599// children, then we either need to trust it, or override it and return a
5600// different result. For example, an "int *" has one child that is an integer,
5601// but a function pointer doesn't have any children. Likewise if a Record type
5602// claims it has no children, then there really is nothing to show.
5603uint32_t
5604ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5605{
5606 if (type.isNull())
5607 return 0;
5608
5609 clang::QualType qual_type(type.getCanonicalType());
5610 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5611 switch (type_class)
5612 {
5613 case clang::Type::Builtin:
5614 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5615 {
5616 case clang::BuiltinType::UnknownAny:
5617 case clang::BuiltinType::Void:
5618 case clang::BuiltinType::NullPtr:
5619 case clang::BuiltinType::OCLEvent:
5620 case clang::BuiltinType::OCLImage1d:
5621 case clang::BuiltinType::OCLImage1dArray:
5622 case clang::BuiltinType::OCLImage1dBuffer:
5623 case clang::BuiltinType::OCLImage2d:
5624 case clang::BuiltinType::OCLImage2dArray:
5625 case clang::BuiltinType::OCLImage3d:
5626 case clang::BuiltinType::OCLSampler:
5627 return 0;
5628 case clang::BuiltinType::Bool:
5629 case clang::BuiltinType::Char_U:
5630 case clang::BuiltinType::UChar:
5631 case clang::BuiltinType::WChar_U:
5632 case clang::BuiltinType::Char16:
5633 case clang::BuiltinType::Char32:
5634 case clang::BuiltinType::UShort:
5635 case clang::BuiltinType::UInt:
5636 case clang::BuiltinType::ULong:
5637 case clang::BuiltinType::ULongLong:
5638 case clang::BuiltinType::UInt128:
5639 case clang::BuiltinType::Char_S:
5640 case clang::BuiltinType::SChar:
5641 case clang::BuiltinType::WChar_S:
5642 case clang::BuiltinType::Short:
5643 case clang::BuiltinType::Int:
5644 case clang::BuiltinType::Long:
5645 case clang::BuiltinType::LongLong:
5646 case clang::BuiltinType::Int128:
5647 case clang::BuiltinType::Float:
5648 case clang::BuiltinType::Double:
5649 case clang::BuiltinType::LongDouble:
5650 case clang::BuiltinType::Dependent:
5651 case clang::BuiltinType::Overload:
5652 case clang::BuiltinType::ObjCId:
5653 case clang::BuiltinType::ObjCClass:
5654 case clang::BuiltinType::ObjCSel:
5655 case clang::BuiltinType::BoundMember:
5656 case clang::BuiltinType::Half:
5657 case clang::BuiltinType::ARCUnbridgedCast:
5658 case clang::BuiltinType::PseudoObject:
5659 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005660 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005661 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005662 default:
5663 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005664 }
5665 break;
5666
5667 case clang::Type::Complex: return 1;
5668 case clang::Type::Pointer: return 1;
5669 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5670 case clang::Type::LValueReference: return 1;
5671 case clang::Type::RValueReference: return 1;
5672 case clang::Type::MemberPointer: return 0;
5673 case clang::Type::ConstantArray: return 0;
5674 case clang::Type::IncompleteArray: return 0;
5675 case clang::Type::VariableArray: return 0;
5676 case clang::Type::DependentSizedArray: return 0;
5677 case clang::Type::DependentSizedExtVector: return 0;
5678 case clang::Type::Vector: return 0;
5679 case clang::Type::ExtVector: return 0;
5680 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5681 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5682 case clang::Type::UnresolvedUsing: return 0;
5683 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5684 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
5685 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5686 case clang::Type::TypeOfExpr: return 0;
5687 case clang::Type::TypeOf: return 0;
5688 case clang::Type::Decltype: return 0;
5689 case clang::Type::Record: return 0;
5690 case clang::Type::Enum: return 1;
5691 case clang::Type::TemplateTypeParm: return 1;
5692 case clang::Type::SubstTemplateTypeParm: return 1;
5693 case clang::Type::TemplateSpecialization: return 1;
5694 case clang::Type::InjectedClassName: return 0;
5695 case clang::Type::DependentName: return 1;
5696 case clang::Type::DependentTemplateSpecialization: return 1;
5697 case clang::Type::ObjCObject: return 0;
5698 case clang::Type::ObjCInterface: return 0;
5699 case clang::Type::ObjCObjectPointer: return 1;
5700 default:
5701 break;
5702 }
5703 return 0;
5704}
5705
5706
Greg Claytona1e5dc82015-08-11 22:53:00 +00005707CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005708ClangASTContext::GetChildCompilerTypeAtIndex (lldb::opaque_compiler_type_t type,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005709 ExecutionContext *exe_ctx,
5710 size_t idx,
5711 bool transparent_pointers,
5712 bool omit_empty_base_classes,
5713 bool ignore_array_bounds,
5714 std::string& child_name,
5715 uint32_t &child_byte_size,
5716 int32_t &child_byte_offset,
5717 uint32_t &child_bitfield_bit_size,
5718 uint32_t &child_bitfield_bit_offset,
5719 bool &child_is_base_class,
5720 bool &child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005721 ValueObject *valobj,
5722 uint64_t &language_flags)
Greg Claytond8d4a572015-08-11 21:38:15 +00005723{
5724 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005725 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005726
5727 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5728 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5729 child_bitfield_bit_size = 0;
5730 child_bitfield_bit_offset = 0;
5731 child_is_base_class = false;
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005732 language_flags = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005733
5734 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5735 uint32_t bit_offset;
5736 switch (parent_type_class)
5737 {
5738 case clang::Type::Builtin:
5739 if (idx_is_valid)
5740 {
5741 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5742 {
5743 case clang::BuiltinType::ObjCId:
5744 case clang::BuiltinType::ObjCClass:
5745 child_name = "isa";
5746 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005747 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005748
5749 default:
5750 break;
5751 }
5752 }
5753 break;
5754
5755 case clang::Type::Record:
5756 if (idx_is_valid && GetCompleteType(type))
5757 {
5758 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5759 const clang::RecordDecl *record_decl = record_type->getDecl();
5760 assert(record_decl);
5761 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5762 uint32_t child_idx = 0;
5763
5764 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5765 if (cxx_record_decl)
5766 {
5767 // We might have base classes to print out first
5768 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5769 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5770 base_class != base_class_end;
5771 ++base_class)
5772 {
5773 const clang::CXXRecordDecl *base_class_decl = nullptr;
5774
5775 // Skip empty base classes
5776 if (omit_empty_base_classes)
5777 {
5778 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5779 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5780 continue;
5781 }
5782
5783 if (idx == child_idx)
5784 {
5785 if (base_class_decl == nullptr)
5786 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5787
5788
5789 if (base_class->isVirtual())
5790 {
5791 bool handled = false;
5792 if (valobj)
5793 {
5794 Error err;
5795 AddressType addr_type = eAddressTypeInvalid;
5796 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5797
5798 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5799 {
5800
5801 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
5802 Process *process = exe_ctx.GetProcessPtr();
5803 if (process)
5804 {
5805 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
5806 if (vtable_ctx)
5807 {
5808 if (vtable_ctx->isMicrosoft())
5809 {
5810 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
5811
5812 if (vtable_ptr_addr)
5813 {
5814 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
5815
5816 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
5817 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
5818 {
5819 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
5820 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
5821 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
5822 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5823 if (base_offset != UINT32_MAX)
5824 {
5825 handled = true;
5826 bit_offset = base_offset * 8;
5827 }
5828 }
5829 }
5830 }
5831 else
5832 {
5833 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
5834 if (vtable_ptr_addr)
5835 {
5836 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
5837 if (vtable_ptr != LLDB_INVALID_ADDRESS)
5838 {
5839 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
5840 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
5841 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
5842 if (base_offset != UINT32_MAX)
5843 {
5844 handled = true;
5845 bit_offset = base_offset * 8;
5846 }
5847 }
5848 }
5849 }
5850 }
5851 }
5852 }
5853
5854 }
5855 if (!handled)
5856 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5857 }
5858 else
5859 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5860
5861 // Base classes should be a multiple of 8 bits in size
5862 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005863 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005864 child_name = base_class_clang_type.GetTypeName().AsCString("");
5865 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5866
5867 // Base classes bit sizes should be a multiple of 8 bits in size
5868 assert (base_class_clang_type_bit_size % 8 == 0);
5869 child_byte_size = base_class_clang_type_bit_size / 8;
5870 child_is_base_class = true;
5871 return base_class_clang_type;
5872 }
5873 // We don't increment the child index in the for loop since we might
5874 // be skipping empty base classes
5875 ++child_idx;
5876 }
5877 }
5878 // Make sure index is in range...
5879 uint32_t field_idx = 0;
5880 clang::RecordDecl::field_iterator field, field_end;
5881 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5882 {
5883 if (idx == child_idx)
5884 {
5885 // Print the member type if requested
5886 // Print the member name and equal sign
5887 child_name.assign(field->getNameAsString().c_str());
5888
5889 // Figure out the type byte size (field_type_info.first) and
5890 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00005891 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005892 assert(field_idx < record_layout.getFieldCount());
5893 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
5894
5895 // Figure out the field offset within the current struct/union/class type
5896 bit_offset = record_layout.getFieldOffset (field_idx);
5897 child_byte_offset = bit_offset / 8;
5898 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
5899 child_bitfield_bit_offset = bit_offset % 8;
5900
5901 return field_clang_type;
5902 }
5903 }
5904 }
5905 break;
5906
5907 case clang::Type::ObjCObject:
5908 case clang::Type::ObjCInterface:
5909 if (idx_is_valid && GetCompleteType(type))
5910 {
5911 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
5912 assert (objc_class_type);
5913 if (objc_class_type)
5914 {
5915 uint32_t child_idx = 0;
5916 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5917
5918 if (class_interface_decl)
5919 {
5920
5921 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
5922 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5923 if (superclass_interface_decl)
5924 {
5925 if (omit_empty_base_classes)
5926 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005927 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00005928 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
5929 {
5930 if (idx == 0)
5931 {
5932 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5933
5934
5935 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
5936
5937 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5938
5939 child_byte_size = ivar_type_info.Width / 8;
5940 child_byte_offset = 0;
5941 child_is_base_class = true;
5942
Greg Claytona1e5dc82015-08-11 22:53:00 +00005943 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005944 }
5945
5946 ++child_idx;
5947 }
5948 }
5949 else
5950 ++child_idx;
5951 }
5952
5953 const uint32_t superclass_idx = child_idx;
5954
5955 if (idx < (child_idx + class_interface_decl->ivar_size()))
5956 {
5957 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5958
5959 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
5960 {
5961 if (child_idx == idx)
5962 {
5963 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5964
5965 clang::QualType ivar_qual_type(ivar_decl->getType());
5966
5967 child_name.assign(ivar_decl->getNameAsString().c_str());
5968
5969 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
5970
5971 child_byte_size = ivar_type_info.Width / 8;
5972
5973 // Figure out the field offset within the current struct/union/class type
5974 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
5975 // that doesn't account for the space taken up by unbacked properties, or from
5976 // the changing size of base classes that are newer than this class.
5977 // So if we have a process around that we can ask about this object, do so.
5978 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
5979 Process *process = nullptr;
5980 if (exe_ctx)
5981 process = exe_ctx->GetProcessPtr();
5982 if (process)
5983 {
5984 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5985 if (objc_runtime != nullptr)
5986 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00005987 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00005988 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
5989 }
5990 }
5991
5992 // Setting this to UINT32_MAX to make sure we don't compute it twice...
5993 bit_offset = UINT32_MAX;
5994
5995 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
5996 {
5997 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
5998 child_byte_offset = bit_offset / 8;
5999 }
6000
6001 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
6002 // of a bitfield within its containing object. So regardless of where we get the byte
6003 // offset from, we still need to get the bit offset for bitfields from the layout.
6004
6005 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
6006 {
6007 if (bit_offset == UINT32_MAX)
6008 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
6009
6010 child_bitfield_bit_offset = bit_offset % 8;
6011 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006012 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006013 }
6014 ++child_idx;
6015 }
6016 }
6017 }
6018 }
6019 }
6020 break;
6021
6022 case clang::Type::ObjCObjectPointer:
6023 if (idx_is_valid)
6024 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006025 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006026
6027 if (transparent_pointers && pointee_clang_type.IsAggregateType())
6028 {
6029 child_is_deref_of_parent = false;
6030 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006031 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6032 idx,
6033 transparent_pointers,
6034 omit_empty_base_classes,
6035 ignore_array_bounds,
6036 child_name,
6037 child_byte_size,
6038 child_byte_offset,
6039 child_bitfield_bit_size,
6040 child_bitfield_bit_offset,
6041 child_is_base_class,
6042 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006043 valobj,
6044 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006045 }
6046 else
6047 {
6048 child_is_deref_of_parent = true;
6049 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6050 if (parent_name)
6051 {
6052 child_name.assign(1, '*');
6053 child_name += parent_name;
6054 }
6055
6056 // We have a pointer to an simple type
6057 if (idx == 0 && pointee_clang_type.GetCompleteType())
6058 {
6059 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6060 child_byte_offset = 0;
6061 return pointee_clang_type;
6062 }
6063 }
6064 }
6065 break;
6066
6067 case clang::Type::Vector:
6068 case clang::Type::ExtVector:
6069 if (idx_is_valid)
6070 {
6071 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6072 if (array)
6073 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006074 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006075 if (element_type.GetCompleteType())
6076 {
6077 char element_name[64];
6078 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6079 child_name.assign(element_name);
6080 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6081 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6082 return element_type;
6083 }
6084 }
6085 }
6086 break;
6087
6088 case clang::Type::ConstantArray:
6089 case clang::Type::IncompleteArray:
6090 if (ignore_array_bounds || idx_is_valid)
6091 {
6092 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6093 if (array)
6094 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006095 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006096 if (element_type.GetCompleteType())
6097 {
6098 char element_name[64];
6099 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6100 child_name.assign(element_name);
6101 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6102 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6103 return element_type;
6104 }
6105 }
6106 }
6107 break;
6108
6109
6110 case clang::Type::Pointer:
6111 if (idx_is_valid)
6112 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006113 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006114
6115 // Don't dereference "void *" pointers
6116 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00006117 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006118
6119 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6120 {
6121 child_is_deref_of_parent = false;
6122 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006123 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6124 idx,
6125 transparent_pointers,
6126 omit_empty_base_classes,
6127 ignore_array_bounds,
6128 child_name,
6129 child_byte_size,
6130 child_byte_offset,
6131 child_bitfield_bit_size,
6132 child_bitfield_bit_offset,
6133 child_is_base_class,
6134 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006135 valobj,
6136 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006137 }
6138 else
6139 {
6140 child_is_deref_of_parent = true;
6141
6142 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6143 if (parent_name)
6144 {
6145 child_name.assign(1, '*');
6146 child_name += parent_name;
6147 }
6148
6149 // We have a pointer to an simple type
6150 if (idx == 0)
6151 {
6152 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6153 child_byte_offset = 0;
6154 return pointee_clang_type;
6155 }
6156 }
6157 }
6158 break;
6159
6160 case clang::Type::LValueReference:
6161 case clang::Type::RValueReference:
6162 if (idx_is_valid)
6163 {
6164 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006165 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006166 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6167 {
6168 child_is_deref_of_parent = false;
6169 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006170 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6171 idx,
6172 transparent_pointers,
6173 omit_empty_base_classes,
6174 ignore_array_bounds,
6175 child_name,
6176 child_byte_size,
6177 child_byte_offset,
6178 child_bitfield_bit_size,
6179 child_bitfield_bit_offset,
6180 child_is_base_class,
6181 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006182 valobj,
6183 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006184 }
6185 else
6186 {
6187 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6188 if (parent_name)
6189 {
6190 child_name.assign(1, '&');
6191 child_name += parent_name;
6192 }
6193
6194 // We have a pointer to an simple type
6195 if (idx == 0)
6196 {
6197 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6198 child_byte_offset = 0;
6199 return pointee_clang_type;
6200 }
6201 }
6202 }
6203 break;
6204
6205 case clang::Type::Typedef:
6206 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006207 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006208 return typedefed_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6209 idx,
6210 transparent_pointers,
6211 omit_empty_base_classes,
6212 ignore_array_bounds,
6213 child_name,
6214 child_byte_size,
6215 child_byte_offset,
6216 child_bitfield_bit_size,
6217 child_bitfield_bit_offset,
6218 child_is_base_class,
6219 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006220 valobj,
6221 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006222 }
6223 break;
6224
6225 case clang::Type::Elaborated:
6226 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006227 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006228 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6229 idx,
6230 transparent_pointers,
6231 omit_empty_base_classes,
6232 ignore_array_bounds,
6233 child_name,
6234 child_byte_size,
6235 child_byte_offset,
6236 child_bitfield_bit_size,
6237 child_bitfield_bit_offset,
6238 child_is_base_class,
6239 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006240 valobj,
6241 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006242 }
6243
6244 case clang::Type::Paren:
6245 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006246 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006247 return paren_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6248 idx,
6249 transparent_pointers,
6250 omit_empty_base_classes,
6251 ignore_array_bounds,
6252 child_name,
6253 child_byte_size,
6254 child_byte_offset,
6255 child_bitfield_bit_size,
6256 child_bitfield_bit_offset,
6257 child_is_base_class,
6258 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006259 valobj,
6260 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006261 }
6262
6263
6264 default:
6265 break;
6266 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006267 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006268}
6269
6270static uint32_t
6271GetIndexForRecordBase
6272(
6273 const clang::RecordDecl *record_decl,
6274 const clang::CXXBaseSpecifier *base_spec,
6275 bool omit_empty_base_classes
6276 )
6277{
6278 uint32_t child_idx = 0;
6279
6280 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6281
6282 // const char *super_name = record_decl->getNameAsCString();
6283 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6284 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6285 //
6286 if (cxx_record_decl)
6287 {
6288 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6289 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6290 base_class != base_class_end;
6291 ++base_class)
6292 {
6293 if (omit_empty_base_classes)
6294 {
6295 if (BaseSpecifierIsEmpty (base_class))
6296 continue;
6297 }
6298
6299 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6300 // child_idx,
6301 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6302 //
6303 //
6304 if (base_class == base_spec)
6305 return child_idx;
6306 ++child_idx;
6307 }
6308 }
6309
6310 return UINT32_MAX;
6311}
6312
6313
6314static uint32_t
6315GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6316 clang::NamedDecl *canonical_decl,
6317 bool omit_empty_base_classes)
6318{
6319 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6320 omit_empty_base_classes);
6321
6322 clang::RecordDecl::field_iterator field, field_end;
6323 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6324 field != field_end;
6325 ++field, ++child_idx)
6326 {
6327 if (field->getCanonicalDecl() == canonical_decl)
6328 return child_idx;
6329 }
6330
6331 return UINT32_MAX;
6332}
6333
6334// Look for a child member (doesn't include base classes, but it does include
6335// their members) in the type hierarchy. Returns an index path into "clang_type"
6336// on how to reach the appropriate member.
6337//
6338// class A
6339// {
6340// public:
6341// int m_a;
6342// int m_b;
6343// };
6344//
6345// class B
6346// {
6347// };
6348//
6349// class C :
6350// public B,
6351// public A
6352// {
6353// };
6354//
6355// If we have a clang type that describes "class C", and we wanted to looked
6356// "m_b" in it:
6357//
6358// With omit_empty_base_classes == false we would get an integer array back with:
6359// { 1, 1 }
6360// The first index 1 is the child index for "class A" within class C
6361// The second index 1 is the child index for "m_b" within class A
6362//
6363// With omit_empty_base_classes == true we would get an integer array back with:
6364// { 0, 1 }
6365// 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)
6366// The second index 1 is the child index for "m_b" within class A
6367
6368size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006369ClangASTContext::GetIndexOfChildMemberWithName (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006370 bool omit_empty_base_classes,
6371 std::vector<uint32_t>& child_indexes)
6372{
6373 if (type && name && name[0])
6374 {
6375 clang::QualType qual_type(GetCanonicalQualType(type));
6376 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6377 switch (type_class)
6378 {
6379 case clang::Type::Record:
6380 if (GetCompleteType(type))
6381 {
6382 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6383 const clang::RecordDecl *record_decl = record_type->getDecl();
6384
6385 assert(record_decl);
6386 uint32_t child_idx = 0;
6387
6388 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6389
6390 // Try and find a field that matches NAME
6391 clang::RecordDecl::field_iterator field, field_end;
6392 llvm::StringRef name_sref(name);
6393 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6394 field != field_end;
6395 ++field, ++child_idx)
6396 {
6397 llvm::StringRef field_name = field->getName();
6398 if (field_name.empty())
6399 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006400 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006401 child_indexes.push_back(child_idx);
6402 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6403 return child_indexes.size();
6404 child_indexes.pop_back();
6405
6406 }
6407 else if (field_name.equals (name_sref))
6408 {
6409 // We have to add on the number of base classes to this index!
6410 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6411 return child_indexes.size();
6412 }
6413 }
6414
6415 if (cxx_record_decl)
6416 {
6417 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6418
6419 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6420
6421 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6422 // Didn't find things easily, lets let clang do its thang...
6423 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6424 clang::DeclarationName decl_name(&ident_ref);
6425
6426 clang::CXXBasePaths paths;
6427 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6428 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6429 },
6430 paths))
6431 {
6432 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6433 for (path = paths.begin(); path != path_end; ++path)
6434 {
6435 const size_t num_path_elements = path->size();
6436 for (size_t e=0; e<num_path_elements; ++e)
6437 {
6438 clang::CXXBasePathElement elem = (*path)[e];
6439
6440 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6441 if (child_idx == UINT32_MAX)
6442 {
6443 child_indexes.clear();
6444 return 0;
6445 }
6446 else
6447 {
6448 child_indexes.push_back (child_idx);
6449 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6450 }
6451 }
6452 for (clang::NamedDecl *path_decl : path->Decls)
6453 {
6454 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6455 if (child_idx == UINT32_MAX)
6456 {
6457 child_indexes.clear();
6458 return 0;
6459 }
6460 else
6461 {
6462 child_indexes.push_back (child_idx);
6463 }
6464 }
6465 }
6466 return child_indexes.size();
6467 }
6468 }
6469
6470 }
6471 break;
6472
6473 case clang::Type::ObjCObject:
6474 case clang::Type::ObjCInterface:
6475 if (GetCompleteType(type))
6476 {
6477 llvm::StringRef name_sref(name);
6478 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6479 assert (objc_class_type);
6480 if (objc_class_type)
6481 {
6482 uint32_t child_idx = 0;
6483 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6484
6485 if (class_interface_decl)
6486 {
6487 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6488 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6489
6490 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6491 {
6492 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6493
6494 if (ivar_decl->getName().equals (name_sref))
6495 {
6496 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6497 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6498 ++child_idx;
6499
6500 child_indexes.push_back (child_idx);
6501 return child_indexes.size();
6502 }
6503 }
6504
6505 if (superclass_interface_decl)
6506 {
6507 // The super class index is always zero for ObjC classes,
6508 // so we push it onto the child indexes in case we find
6509 // an ivar in our superclass...
6510 child_indexes.push_back (0);
6511
Greg Claytona1e5dc82015-08-11 22:53:00 +00006512 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006513 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6514 omit_empty_base_classes,
6515 child_indexes))
6516 {
6517 // We did find an ivar in a superclass so just
6518 // return the results!
6519 return child_indexes.size();
6520 }
6521
6522 // We didn't find an ivar matching "name" in our
6523 // superclass, pop the superclass zero index that
6524 // we pushed on above.
6525 child_indexes.pop_back();
6526 }
6527 }
6528 }
6529 }
6530 break;
6531
6532 case clang::Type::ObjCObjectPointer:
6533 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006534 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006535 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6536 omit_empty_base_classes,
6537 child_indexes);
6538 }
6539 break;
6540
6541
6542 case clang::Type::ConstantArray:
6543 {
6544 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6545 // const uint64_t element_count = array->getSize().getLimitedValue();
6546 //
6547 // if (idx < element_count)
6548 // {
6549 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6550 //
6551 // char element_name[32];
6552 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6553 //
6554 // child_name.assign(element_name);
6555 // assert(field_type_info.first % 8 == 0);
6556 // child_byte_size = field_type_info.first / 8;
6557 // child_byte_offset = idx * child_byte_size;
6558 // return array->getElementType().getAsOpaquePtr();
6559 // }
6560 }
6561 break;
6562
6563 // case clang::Type::MemberPointerType:
6564 // {
6565 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6566 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6567 //
6568 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6569 // {
6570 // return GetIndexOfChildWithName (ast,
6571 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6572 // name);
6573 // }
6574 // }
6575 // break;
6576 //
6577 case clang::Type::LValueReference:
6578 case clang::Type::RValueReference:
6579 {
6580 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6581 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006582 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006583
6584 if (pointee_clang_type.IsAggregateType ())
6585 {
6586 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6587 omit_empty_base_classes,
6588 child_indexes);
6589 }
6590 }
6591 break;
6592
6593 case clang::Type::Pointer:
6594 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006595 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006596
6597 if (pointee_clang_type.IsAggregateType ())
6598 {
6599 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6600 omit_empty_base_classes,
6601 child_indexes);
6602 }
6603 }
6604 break;
6605
6606 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006607 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006608 omit_empty_base_classes,
6609 child_indexes);
6610
6611 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006612 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006613 omit_empty_base_classes,
6614 child_indexes);
6615
6616 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006617 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006618 omit_empty_base_classes,
6619 child_indexes);
6620
6621 default:
6622 break;
6623 }
6624 }
6625 return 0;
6626}
6627
6628
6629// Get the index of the child of "clang_type" whose name matches. This function
6630// doesn't descend into the children, but only looks one level deep and name
6631// matches can include base class names.
6632
6633uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006634ClangASTContext::GetIndexOfChildWithName (lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00006635{
6636 if (type && name && name[0])
6637 {
6638 clang::QualType qual_type(GetCanonicalQualType(type));
6639
6640 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6641
6642 switch (type_class)
6643 {
6644 case clang::Type::Record:
6645 if (GetCompleteType(type))
6646 {
6647 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6648 const clang::RecordDecl *record_decl = record_type->getDecl();
6649
6650 assert(record_decl);
6651 uint32_t child_idx = 0;
6652
6653 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6654
6655 if (cxx_record_decl)
6656 {
6657 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6658 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6659 base_class != base_class_end;
6660 ++base_class)
6661 {
6662 // Skip empty base classes
6663 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6664 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6665 continue;
6666
Greg Claytona1e5dc82015-08-11 22:53:00 +00006667 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006668 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6669 if (base_class_type_name.compare (name) == 0)
6670 return child_idx;
6671 ++child_idx;
6672 }
6673 }
6674
6675 // Try and find a field that matches NAME
6676 clang::RecordDecl::field_iterator field, field_end;
6677 llvm::StringRef name_sref(name);
6678 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6679 field != field_end;
6680 ++field, ++child_idx)
6681 {
6682 if (field->getName().equals (name_sref))
6683 return child_idx;
6684 }
6685
6686 }
6687 break;
6688
6689 case clang::Type::ObjCObject:
6690 case clang::Type::ObjCInterface:
6691 if (GetCompleteType(type))
6692 {
6693 llvm::StringRef name_sref(name);
6694 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6695 assert (objc_class_type);
6696 if (objc_class_type)
6697 {
6698 uint32_t child_idx = 0;
6699 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6700
6701 if (class_interface_decl)
6702 {
6703 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6704 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6705
6706 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6707 {
6708 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6709
6710 if (ivar_decl->getName().equals (name_sref))
6711 {
6712 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6713 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6714 ++child_idx;
6715
6716 return child_idx;
6717 }
6718 }
6719
6720 if (superclass_interface_decl)
6721 {
6722 if (superclass_interface_decl->getName().equals (name_sref))
6723 return 0;
6724 }
6725 }
6726 }
6727 }
6728 break;
6729
6730 case clang::Type::ObjCObjectPointer:
6731 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006732 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006733 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6734 }
6735 break;
6736
6737 case clang::Type::ConstantArray:
6738 {
6739 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6740 // const uint64_t element_count = array->getSize().getLimitedValue();
6741 //
6742 // if (idx < element_count)
6743 // {
6744 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6745 //
6746 // char element_name[32];
6747 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6748 //
6749 // child_name.assign(element_name);
6750 // assert(field_type_info.first % 8 == 0);
6751 // child_byte_size = field_type_info.first / 8;
6752 // child_byte_offset = idx * child_byte_size;
6753 // return array->getElementType().getAsOpaquePtr();
6754 // }
6755 }
6756 break;
6757
6758 // case clang::Type::MemberPointerType:
6759 // {
6760 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6761 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6762 //
6763 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6764 // {
6765 // return GetIndexOfChildWithName (ast,
6766 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6767 // name);
6768 // }
6769 // }
6770 // break;
6771 //
6772 case clang::Type::LValueReference:
6773 case clang::Type::RValueReference:
6774 {
6775 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006776 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006777
6778 if (pointee_type.IsAggregateType ())
6779 {
6780 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6781 }
6782 }
6783 break;
6784
6785 case clang::Type::Pointer:
6786 {
6787 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006788 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006789
6790 if (pointee_type.IsAggregateType ())
6791 {
6792 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6793 }
6794 else
6795 {
6796 // if (parent_name)
6797 // {
6798 // child_name.assign(1, '*');
6799 // child_name += parent_name;
6800 // }
6801 //
6802 // // We have a pointer to an simple type
6803 // if (idx == 0)
6804 // {
6805 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
6806 // assert(clang_type_info.first % 8 == 0);
6807 // child_byte_size = clang_type_info.first / 8;
6808 // child_byte_offset = 0;
6809 // return pointee_type.getAsOpaquePtr();
6810 // }
6811 }
6812 }
6813 break;
6814
6815 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006816 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006817
6818 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006819 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006820
6821 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006822 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006823
6824 default:
6825 break;
6826 }
6827 }
6828 return UINT32_MAX;
6829}
6830
6831
6832size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006833ClangASTContext::GetNumTemplateArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00006834{
6835 if (!type)
6836 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006837
6838 clang::QualType qual_type (GetCanonicalQualType(type));
6839 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6840 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006841 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006842 case clang::Type::Record:
6843 if (GetCompleteType(type))
6844 {
6845 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6846 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006847 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006848 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6849 if (template_decl)
6850 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00006851 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006852 }
6853 break;
6854
6855 case clang::Type::Typedef:
6856 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
6857
6858 case clang::Type::Elaborated:
6859 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
6860
6861 case clang::Type::Paren:
6862 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
6863
6864 default:
6865 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006866 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006867
Greg Claytond8d4a572015-08-11 21:38:15 +00006868 return 0;
6869}
6870
Greg Claytona1e5dc82015-08-11 22:53:00 +00006871CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006872ClangASTContext::GetTemplateArgument (lldb::opaque_compiler_type_t type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00006873{
6874 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006875 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006876
6877 clang::QualType qual_type (GetCanonicalQualType(type));
6878 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6879 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00006880 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006881 case clang::Type::Record:
6882 if (GetCompleteType(type))
6883 {
6884 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
6885 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00006886 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006887 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
6888 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00006889 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006890 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
6891 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00006892 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006893 case clang::TemplateArgument::Null:
6894 kind = eTemplateArgumentKindNull;
6895 return CompilerType();
6896
6897 case clang::TemplateArgument::Type:
6898 kind = eTemplateArgumentKindType;
6899 return CompilerType(getASTContext(), template_arg.getAsType());
6900
6901 case clang::TemplateArgument::Declaration:
6902 kind = eTemplateArgumentKindDeclaration;
6903 return CompilerType();
6904
6905 case clang::TemplateArgument::Integral:
6906 kind = eTemplateArgumentKindIntegral;
6907 return CompilerType(getASTContext(), template_arg.getIntegralType());
6908
6909 case clang::TemplateArgument::Template:
6910 kind = eTemplateArgumentKindTemplate;
6911 return CompilerType();
6912
6913 case clang::TemplateArgument::TemplateExpansion:
6914 kind = eTemplateArgumentKindTemplateExpansion;
6915 return CompilerType();
6916
6917 case clang::TemplateArgument::Expression:
6918 kind = eTemplateArgumentKindExpression;
6919 return CompilerType();
6920
6921 case clang::TemplateArgument::Pack:
6922 kind = eTemplateArgumentKindPack;
6923 return CompilerType();
6924
6925 default:
6926 assert (!"Unhandled clang::TemplateArgument::ArgKind");
6927 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006928 }
6929 }
6930 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00006931 }
6932 break;
6933
6934 case clang::Type::Typedef:
6935 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
6936
6937 case clang::Type::Elaborated:
6938 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
6939
6940 case clang::Type::Paren:
6941 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
6942
6943 default:
6944 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00006945 }
6946 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006947 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00006948}
6949
Enrico Granatac6bf2e22015-09-23 01:39:46 +00006950CompilerType
6951ClangASTContext::GetTypeForFormatters (void* type)
6952{
6953 if (type)
6954 return RemoveFastQualifiers(CompilerType(this, type));
6955 return CompilerType();
6956}
6957
Greg Claytond8d4a572015-08-11 21:38:15 +00006958static bool
6959IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
6960{
6961 if (name == nullptr || name[0] == '\0')
6962 return false;
6963
6964#define OPERATOR_PREFIX "operator"
6965#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
6966
6967 const char *post_op_name = nullptr;
6968
6969 bool no_space = true;
6970
6971 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
6972 return false;
6973
6974 post_op_name = name + OPERATOR_PREFIX_LENGTH;
6975
6976 if (post_op_name[0] == ' ')
6977 {
6978 post_op_name++;
6979 no_space = false;
6980 }
6981
6982#undef OPERATOR_PREFIX
6983#undef OPERATOR_PREFIX_LENGTH
6984
6985 // This is an operator, set the overloaded operator kind to invalid
6986 // in case this is a conversion operator...
6987 op_kind = clang::NUM_OVERLOADED_OPERATORS;
6988
6989 switch (post_op_name[0])
6990 {
6991 default:
6992 if (no_space)
6993 return false;
6994 break;
6995 case 'n':
6996 if (no_space)
6997 return false;
6998 if (strcmp (post_op_name, "new") == 0)
6999 op_kind = clang::OO_New;
7000 else if (strcmp (post_op_name, "new[]") == 0)
7001 op_kind = clang::OO_Array_New;
7002 break;
7003
7004 case 'd':
7005 if (no_space)
7006 return false;
7007 if (strcmp (post_op_name, "delete") == 0)
7008 op_kind = clang::OO_Delete;
7009 else if (strcmp (post_op_name, "delete[]") == 0)
7010 op_kind = clang::OO_Array_Delete;
7011 break;
7012
7013 case '+':
7014 if (post_op_name[1] == '\0')
7015 op_kind = clang::OO_Plus;
7016 else if (post_op_name[2] == '\0')
7017 {
7018 if (post_op_name[1] == '=')
7019 op_kind = clang::OO_PlusEqual;
7020 else if (post_op_name[1] == '+')
7021 op_kind = clang::OO_PlusPlus;
7022 }
7023 break;
7024
7025 case '-':
7026 if (post_op_name[1] == '\0')
7027 op_kind = clang::OO_Minus;
7028 else if (post_op_name[2] == '\0')
7029 {
7030 switch (post_op_name[1])
7031 {
7032 case '=': op_kind = clang::OO_MinusEqual; break;
7033 case '-': op_kind = clang::OO_MinusMinus; break;
7034 case '>': op_kind = clang::OO_Arrow; break;
7035 }
7036 }
7037 else if (post_op_name[3] == '\0')
7038 {
7039 if (post_op_name[2] == '*')
7040 op_kind = clang::OO_ArrowStar; break;
7041 }
7042 break;
7043
7044 case '*':
7045 if (post_op_name[1] == '\0')
7046 op_kind = clang::OO_Star;
7047 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7048 op_kind = clang::OO_StarEqual;
7049 break;
7050
7051 case '/':
7052 if (post_op_name[1] == '\0')
7053 op_kind = clang::OO_Slash;
7054 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7055 op_kind = clang::OO_SlashEqual;
7056 break;
7057
7058 case '%':
7059 if (post_op_name[1] == '\0')
7060 op_kind = clang::OO_Percent;
7061 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7062 op_kind = clang::OO_PercentEqual;
7063 break;
7064
7065
7066 case '^':
7067 if (post_op_name[1] == '\0')
7068 op_kind = clang::OO_Caret;
7069 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7070 op_kind = clang::OO_CaretEqual;
7071 break;
7072
7073 case '&':
7074 if (post_op_name[1] == '\0')
7075 op_kind = clang::OO_Amp;
7076 else if (post_op_name[2] == '\0')
7077 {
7078 switch (post_op_name[1])
7079 {
7080 case '=': op_kind = clang::OO_AmpEqual; break;
7081 case '&': op_kind = clang::OO_AmpAmp; break;
7082 }
7083 }
7084 break;
7085
7086 case '|':
7087 if (post_op_name[1] == '\0')
7088 op_kind = clang::OO_Pipe;
7089 else if (post_op_name[2] == '\0')
7090 {
7091 switch (post_op_name[1])
7092 {
7093 case '=': op_kind = clang::OO_PipeEqual; break;
7094 case '|': op_kind = clang::OO_PipePipe; break;
7095 }
7096 }
7097 break;
7098
7099 case '~':
7100 if (post_op_name[1] == '\0')
7101 op_kind = clang::OO_Tilde;
7102 break;
7103
7104 case '!':
7105 if (post_op_name[1] == '\0')
7106 op_kind = clang::OO_Exclaim;
7107 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7108 op_kind = clang::OO_ExclaimEqual;
7109 break;
7110
7111 case '=':
7112 if (post_op_name[1] == '\0')
7113 op_kind = clang::OO_Equal;
7114 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7115 op_kind = clang::OO_EqualEqual;
7116 break;
7117
7118 case '<':
7119 if (post_op_name[1] == '\0')
7120 op_kind = clang::OO_Less;
7121 else if (post_op_name[2] == '\0')
7122 {
7123 switch (post_op_name[1])
7124 {
7125 case '<': op_kind = clang::OO_LessLess; break;
7126 case '=': op_kind = clang::OO_LessEqual; break;
7127 }
7128 }
7129 else if (post_op_name[3] == '\0')
7130 {
7131 if (post_op_name[2] == '=')
7132 op_kind = clang::OO_LessLessEqual;
7133 }
7134 break;
7135
7136 case '>':
7137 if (post_op_name[1] == '\0')
7138 op_kind = clang::OO_Greater;
7139 else if (post_op_name[2] == '\0')
7140 {
7141 switch (post_op_name[1])
7142 {
7143 case '>': op_kind = clang::OO_GreaterGreater; break;
7144 case '=': op_kind = clang::OO_GreaterEqual; break;
7145 }
7146 }
7147 else if (post_op_name[1] == '>' &&
7148 post_op_name[2] == '=' &&
7149 post_op_name[3] == '\0')
7150 {
7151 op_kind = clang::OO_GreaterGreaterEqual;
7152 }
7153 break;
7154
7155 case ',':
7156 if (post_op_name[1] == '\0')
7157 op_kind = clang::OO_Comma;
7158 break;
7159
7160 case '(':
7161 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7162 op_kind = clang::OO_Call;
7163 break;
7164
7165 case '[':
7166 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7167 op_kind = clang::OO_Subscript;
7168 break;
7169 }
7170
7171 return true;
7172}
7173
7174clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007175ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007176{
7177 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
7178 if (enutype)
7179 return enutype->getDecl();
7180 return NULL;
7181}
7182
7183clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007184ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007185{
7186 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(GetCanonicalQualType(type));
7187 if (record_type)
7188 return record_type->getDecl();
7189 return nullptr;
7190}
7191
Greg Clayton5dfc4a42015-12-02 00:43:32 +00007192clang::TagDecl *
7193ClangASTContext::GetAsTagDecl (const CompilerType& type)
7194{
7195 clang::QualType qual_type = GetCanonicalQualType(type);
7196 if (qual_type.isNull())
7197 return nullptr;
7198 else
7199 return qual_type->getAsTagDecl();
7200}
7201
Greg Claytond8d4a572015-08-11 21:38:15 +00007202clang::CXXRecordDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007203ClangASTContext::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007204{
7205 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7206}
7207
7208clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007209ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007210{
7211 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(GetCanonicalQualType(type));
7212 if (objc_class_type)
7213 return objc_class_type->getInterface();
7214 return nullptr;
7215}
7216
7217clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007218ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7219 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007220 AccessType access,
7221 uint32_t bitfield_bit_size)
7222{
7223 if (!type.IsValid() || !field_clang_type.IsValid())
7224 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007225 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007226 if (!ast)
7227 return nullptr;
7228 clang::ASTContext* clang_ast = ast->getASTContext();
7229
7230 clang::FieldDecl *field = nullptr;
7231
7232 clang::Expr *bit_width = nullptr;
7233 if (bitfield_bit_size != 0)
7234 {
7235 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7236 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7237 }
7238
7239 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7240 if (record_decl)
7241 {
7242 field = clang::FieldDecl::Create (*clang_ast,
7243 record_decl,
7244 clang::SourceLocation(),
7245 clang::SourceLocation(),
7246 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7247 GetQualType(field_clang_type), // Field type
7248 nullptr, // TInfo *
7249 bit_width, // BitWidth
7250 false, // Mutable
7251 clang::ICIS_NoInit); // HasInit
7252
7253 if (!name)
7254 {
7255 // Determine whether this field corresponds to an anonymous
7256 // struct or union.
7257 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7258 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7259 if (!Rec->getDeclName()) {
7260 Rec->setAnonymousStructOrUnion(true);
7261 field->setImplicit();
7262
7263 }
7264 }
7265 }
7266
7267 if (field)
7268 {
7269 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7270
7271 record_decl->addDecl(field);
7272
7273#ifdef LLDB_CONFIGURATION_DEBUG
7274 VerifyDecl(field);
7275#endif
7276 }
7277 }
7278 else
7279 {
7280 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7281
7282 if (class_interface_decl)
7283 {
7284 const bool is_synthesized = false;
7285
7286 field_clang_type.GetCompleteType();
7287
7288 field = clang::ObjCIvarDecl::Create (*clang_ast,
7289 class_interface_decl,
7290 clang::SourceLocation(),
7291 clang::SourceLocation(),
7292 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7293 GetQualType(field_clang_type), // Field type
7294 nullptr, // TypeSourceInfo *
7295 ConvertAccessTypeToObjCIvarAccessControl (access),
7296 bit_width,
7297 is_synthesized);
7298
7299 if (field)
7300 {
7301 class_interface_decl->addDecl(field);
7302
7303#ifdef LLDB_CONFIGURATION_DEBUG
7304 VerifyDecl(field);
7305#endif
7306 }
7307 }
7308 }
7309 return field;
7310}
7311
7312void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007313ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007314{
Greg Claytonf73034f2015-09-08 18:15:05 +00007315 if (!type)
7316 return;
7317
7318 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007319 if (!ast)
7320 return;
7321
7322 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7323
7324 if (!record_decl)
7325 return;
7326
7327 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7328
7329 IndirectFieldVector indirect_fields;
7330 clang::RecordDecl::field_iterator field_pos;
7331 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7332 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7333 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7334 {
7335 if (field_pos->isAnonymousStructOrUnion())
7336 {
7337 clang::QualType field_qual_type = field_pos->getType();
7338
7339 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7340
7341 if (!field_record_type)
7342 continue;
7343
7344 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7345
7346 if (!field_record_decl)
7347 continue;
7348
7349 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7350 di != de;
7351 ++di)
7352 {
7353 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7354 {
7355 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7356 chain[0] = *field_pos;
7357 chain[1] = nested_field_decl;
7358 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7359 record_decl,
7360 clang::SourceLocation(),
7361 nested_field_decl->getIdentifier(),
7362 nested_field_decl->getType(),
7363 chain,
7364 2);
7365
7366 indirect_field->setImplicit();
7367
7368 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7369 nested_field_decl->getAccess()));
7370
7371 indirect_fields.push_back(indirect_field);
7372 }
7373 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7374 {
7375 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7376 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7377 chain[0] = *field_pos;
7378
7379 int chain_index = 1;
7380 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7381 nce = nested_indirect_field_decl->chain_end();
7382 nci < nce;
7383 ++nci)
7384 {
7385 chain[chain_index] = *nci;
7386 chain_index++;
7387 }
7388
7389 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7390 record_decl,
7391 clang::SourceLocation(),
7392 nested_indirect_field_decl->getIdentifier(),
7393 nested_indirect_field_decl->getType(),
7394 chain,
7395 nested_chain_size + 1);
7396
7397 indirect_field->setImplicit();
7398
7399 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7400 nested_indirect_field_decl->getAccess()));
7401
7402 indirect_fields.push_back(indirect_field);
7403 }
7404 }
7405 }
7406 }
7407
7408 // Check the last field to see if it has an incomplete array type as its
7409 // last member and if it does, the tell the record decl about it
7410 if (last_field_pos != field_end_pos)
7411 {
7412 if (last_field_pos->getType()->isIncompleteArrayType())
7413 record_decl->hasFlexibleArrayMember();
7414 }
7415
7416 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7417 ifi < ife;
7418 ++ifi)
7419 {
7420 record_decl->addDecl(*ifi);
7421 }
7422}
7423
7424void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007425ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007426{
Greg Claytonf73034f2015-09-08 18:15:05 +00007427 if (type)
7428 {
7429 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7430 if (ast)
7431 {
7432 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007433
Greg Claytonf73034f2015-09-08 18:15:05 +00007434 if (!record_decl)
7435 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007436
Greg Claytonf73034f2015-09-08 18:15:05 +00007437 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7438 }
7439 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007440}
7441
7442clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007443ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7444 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007445 AccessType access)
7446{
7447 clang::VarDecl *var_decl = nullptr;
7448
7449 if (!type.IsValid() || !var_type.IsValid())
7450 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007451 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007452 if (!ast)
7453 return nullptr;
7454
7455 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7456 if (record_decl)
7457 {
7458 var_decl = clang::VarDecl::Create (*ast->getASTContext(), // ASTContext &
7459 record_decl, // DeclContext *
7460 clang::SourceLocation(), // clang::SourceLocation StartLoc
7461 clang::SourceLocation(), // clang::SourceLocation IdLoc
7462 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7463 GetQualType(var_type), // Variable clang::QualType
7464 nullptr, // TypeSourceInfo *
7465 clang::SC_Static); // StorageClass
7466 if (var_decl)
7467 {
7468 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7469 record_decl->addDecl(var_decl);
7470
7471#ifdef LLDB_CONFIGURATION_DEBUG
7472 VerifyDecl(var_decl);
7473#endif
7474 }
7475 }
7476 return var_decl;
7477}
7478
7479
7480clang::CXXMethodDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007481ClangASTContext::AddMethodToCXXRecordType (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007482 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007483 lldb::AccessType access,
7484 bool is_virtual,
7485 bool is_static,
7486 bool is_inline,
7487 bool is_explicit,
7488 bool is_attr_used,
7489 bool is_artificial)
7490{
7491 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7492 return nullptr;
7493
7494 clang::QualType record_qual_type(GetCanonicalQualType(type));
7495
7496 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7497
7498 if (cxx_record_decl == nullptr)
7499 return nullptr;
7500
7501 clang::QualType method_qual_type (GetQualType(method_clang_type));
7502
7503 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7504
7505 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7506
7507 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7508
7509 if (function_type == nullptr)
7510 return nullptr;
7511
7512 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7513
7514 if (!method_function_prototype)
7515 return nullptr;
7516
7517 unsigned int num_params = method_function_prototype->getNumParams();
7518
7519 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7520 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7521
7522 if (is_artificial)
7523 return nullptr; // skip everything artificial
7524
7525 if (name[0] == '~')
7526 {
7527 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7528 cxx_record_decl,
7529 clang::SourceLocation(),
7530 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7531 method_qual_type,
7532 nullptr,
7533 is_inline,
7534 is_artificial);
7535 cxx_method_decl = cxx_dtor_decl;
7536 }
7537 else if (decl_name == cxx_record_decl->getDeclName())
7538 {
7539 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7540 cxx_record_decl,
7541 clang::SourceLocation(),
7542 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7543 method_qual_type,
7544 nullptr, // TypeSourceInfo *
7545 is_explicit,
7546 is_inline,
7547 is_artificial,
7548 false /*is_constexpr*/);
7549 cxx_method_decl = cxx_ctor_decl;
7550 }
7551 else
7552 {
7553 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7554 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7555
7556 if (IsOperator (name, op_kind))
7557 {
7558 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7559 {
7560 // Check the number of operator parameters. Sometimes we have
7561 // seen bad DWARF that doesn't correctly describe operators and
7562 // if we try to create a method and add it to the class, clang
7563 // will assert and crash, so we need to make sure things are
7564 // acceptable.
7565 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7566 return nullptr;
7567 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7568 cxx_record_decl,
7569 clang::SourceLocation(),
7570 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7571 method_qual_type,
7572 nullptr, // TypeSourceInfo *
7573 SC,
7574 is_inline,
7575 false /*is_constexpr*/,
7576 clang::SourceLocation());
7577 }
7578 else if (num_params == 0)
7579 {
7580 // Conversion operators don't take params...
7581 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7582 cxx_record_decl,
7583 clang::SourceLocation(),
7584 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7585 method_qual_type,
7586 nullptr, // TypeSourceInfo *
7587 is_inline,
7588 is_explicit,
7589 false /*is_constexpr*/,
7590 clang::SourceLocation());
7591 }
7592 }
7593
7594 if (cxx_method_decl == nullptr)
7595 {
7596 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7597 cxx_record_decl,
7598 clang::SourceLocation(),
7599 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7600 method_qual_type,
7601 nullptr, // TypeSourceInfo *
7602 SC,
7603 is_inline,
7604 false /*is_constexpr*/,
7605 clang::SourceLocation());
7606 }
7607 }
7608
7609 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7610
7611 cxx_method_decl->setAccess (access_specifier);
7612 cxx_method_decl->setVirtualAsWritten (is_virtual);
7613
7614 if (is_attr_used)
7615 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7616
7617 // Populate the method decl with parameter decls
7618
7619 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7620
7621 for (unsigned param_index = 0;
7622 param_index < num_params;
7623 ++param_index)
7624 {
7625 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7626 cxx_method_decl,
7627 clang::SourceLocation(),
7628 clang::SourceLocation(),
7629 nullptr, // anonymous
7630 method_function_prototype->getParamType(param_index),
7631 nullptr,
7632 clang::SC_None,
7633 nullptr));
7634 }
7635
7636 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7637
7638 cxx_record_decl->addDecl (cxx_method_decl);
7639
7640 // Sometimes the debug info will mention a constructor (default/copy/move),
7641 // destructor, or assignment operator (copy/move) but there won't be any
7642 // version of this in the code. So we check if the function was artificially
7643 // generated and if it is trivial and this lets the compiler/backend know
7644 // that it can inline the IR for these when it needs to and we can avoid a
7645 // "missing function" error when running expressions.
7646
7647 if (is_artificial)
7648 {
7649 if (cxx_ctor_decl &&
7650 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7651 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7652 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7653 {
7654 cxx_ctor_decl->setDefaulted();
7655 cxx_ctor_decl->setTrivial(true);
7656 }
7657 else if (cxx_dtor_decl)
7658 {
7659 if (cxx_record_decl->hasTrivialDestructor())
7660 {
7661 cxx_dtor_decl->setDefaulted();
7662 cxx_dtor_decl->setTrivial(true);
7663 }
7664 }
7665 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7666 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7667 {
7668 cxx_method_decl->setDefaulted();
7669 cxx_method_decl->setTrivial(true);
7670 }
7671 }
7672
7673#ifdef LLDB_CONFIGURATION_DEBUG
7674 VerifyDecl(cxx_method_decl);
7675#endif
7676
7677 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7678 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7679 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7680 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7681 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7682 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7683 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7684 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7685 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7686 return cxx_method_decl;
7687}
7688
7689
7690#pragma mark C++ Base Classes
7691
7692clang::CXXBaseSpecifier *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007693ClangASTContext::CreateBaseClassSpecifier (lldb::opaque_compiler_type_t type, AccessType access, bool is_virtual, bool base_of_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007694{
7695 if (type)
7696 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7697 is_virtual,
7698 base_of_class,
7699 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7700 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7701 clang::SourceLocation());
7702 return nullptr;
7703}
7704
7705void
7706ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7707{
7708 for (unsigned i=0; i<num_base_classes; ++i)
7709 {
7710 delete base_classes[i];
7711 base_classes[i] = nullptr;
7712 }
7713}
7714
7715bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007716ClangASTContext::SetBaseClassesForClassType (lldb::opaque_compiler_type_t type, clang::CXXBaseSpecifier const * const *base_classes,
Greg Claytond8d4a572015-08-11 21:38:15 +00007717 unsigned num_base_classes)
7718{
7719 if (type)
7720 {
7721 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7722 if (cxx_record_decl)
7723 {
7724 cxx_record_decl->setBases(base_classes, num_base_classes);
7725 return true;
7726 }
7727 }
7728 return false;
7729}
7730
7731bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007732ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007733{
Greg Claytonf73034f2015-09-08 18:15:05 +00007734 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007735 if (!ast)
7736 return false;
7737 clang::ASTContext* clang_ast = ast->getASTContext();
7738
7739 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7740 {
7741 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7742 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7743 if (class_interface_decl && super_interface_decl)
7744 {
7745 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7746 return true;
7747 }
7748 }
7749 return false;
7750}
7751
7752bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007753ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007754 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007755 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007756 clang::ObjCIvarDecl *ivar_decl,
7757 const char *property_setter_name,
7758 const char *property_getter_name,
7759 uint32_t property_attributes,
7760 ClangASTMetadata *metadata)
7761{
7762 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7763 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007764 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007765 if (!ast)
7766 return false;
7767 clang::ASTContext* clang_ast = ast->getASTContext();
7768
7769 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7770
7771 if (class_interface_decl)
7772 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007773 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00007774
7775 if (property_clang_type.IsValid())
7776 property_clang_type_to_access = property_clang_type;
7777 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007778 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007779
7780 if (class_interface_decl && property_clang_type_to_access.IsValid())
7781 {
7782 clang::TypeSourceInfo *prop_type_source;
7783 if (ivar_decl)
7784 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
7785 else
7786 prop_type_source = clang_ast->getTrivialTypeSourceInfo (GetQualType(property_clang_type));
7787
7788 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create (*clang_ast,
7789 class_interface_decl,
7790 clang::SourceLocation(), // Source Location
7791 &clang_ast->Idents.get(property_name),
7792 clang::SourceLocation(), //Source Location for AT
7793 clang::SourceLocation(), //Source location for (
7794 ivar_decl ? ivar_decl->getType() : ClangASTContext::GetQualType(property_clang_type),
7795 prop_type_source);
7796
7797 if (property_decl)
7798 {
7799 if (metadata)
7800 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
7801
7802 class_interface_decl->addDecl (property_decl);
7803
7804 clang::Selector setter_sel, getter_sel;
7805
7806 if (property_setter_name != nullptr)
7807 {
7808 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
7809 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
7810 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7811 }
7812 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
7813 {
7814 std::string setter_sel_string("set");
7815 setter_sel_string.push_back(::toupper(property_name[0]));
7816 setter_sel_string.append(&property_name[1]);
7817 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
7818 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
7819 }
7820 property_decl->setSetterName(setter_sel);
7821 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
7822
7823 if (property_getter_name != nullptr)
7824 {
7825 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
7826 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7827 }
7828 else
7829 {
7830 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
7831 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
7832 }
7833 property_decl->setGetterName(getter_sel);
7834 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
7835
7836 if (ivar_decl)
7837 property_decl->setPropertyIvarDecl (ivar_decl);
7838
7839 if (property_attributes & DW_APPLE_PROPERTY_readonly)
7840 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
7841 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
7842 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
7843 if (property_attributes & DW_APPLE_PROPERTY_assign)
7844 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
7845 if (property_attributes & DW_APPLE_PROPERTY_retain)
7846 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
7847 if (property_attributes & DW_APPLE_PROPERTY_copy)
7848 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
7849 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
7850 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
7851
7852 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
7853 {
7854 const bool isInstance = true;
7855 const bool isVariadic = false;
7856 const bool isSynthesized = false;
7857 const bool isImplicitlyDeclared = true;
7858 const bool isDefined = false;
7859 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7860 const bool HasRelatedResultType = false;
7861
7862 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create (*clang_ast,
7863 clang::SourceLocation(),
7864 clang::SourceLocation(),
7865 getter_sel,
7866 GetQualType(property_clang_type_to_access),
7867 nullptr,
7868 class_interface_decl,
7869 isInstance,
7870 isVariadic,
7871 isSynthesized,
7872 isImplicitlyDeclared,
7873 isDefined,
7874 impControl,
7875 HasRelatedResultType);
7876
7877 if (getter && metadata)
7878 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
7879
7880 if (getter)
7881 {
7882 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
7883
7884 class_interface_decl->addDecl(getter);
7885 }
7886 }
7887
7888 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
7889 {
7890 clang::QualType result_type = clang_ast->VoidTy;
7891
7892 const bool isInstance = true;
7893 const bool isVariadic = false;
7894 const bool isSynthesized = false;
7895 const bool isImplicitlyDeclared = true;
7896 const bool isDefined = false;
7897 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
7898 const bool HasRelatedResultType = false;
7899
7900 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
7901 clang::SourceLocation(),
7902 clang::SourceLocation(),
7903 setter_sel,
7904 result_type,
7905 nullptr,
7906 class_interface_decl,
7907 isInstance,
7908 isVariadic,
7909 isSynthesized,
7910 isImplicitlyDeclared,
7911 isDefined,
7912 impControl,
7913 HasRelatedResultType);
7914
7915 if (setter && metadata)
7916 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
7917
7918 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
7919
7920 params.push_back (clang::ParmVarDecl::Create (*clang_ast,
7921 setter,
7922 clang::SourceLocation(),
7923 clang::SourceLocation(),
7924 nullptr, // anonymous
7925 GetQualType(property_clang_type_to_access),
7926 nullptr,
7927 clang::SC_Auto,
7928 nullptr));
7929
7930 if (setter)
7931 {
7932 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
7933
7934 class_interface_decl->addDecl(setter);
7935 }
7936 }
7937
7938 return true;
7939 }
7940 }
7941 }
7942 return false;
7943}
7944
7945bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007946ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00007947{
7948 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7949 if (class_interface_decl)
7950 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
7951 return false;
7952}
7953
7954
7955clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007956ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007957 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 +00007958 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007959 lldb::AccessType access,
7960 bool is_artificial)
7961{
7962 if (!type || !method_clang_type.IsValid())
7963 return nullptr;
7964
7965 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7966
7967 if (class_interface_decl == nullptr)
7968 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007969 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7970 if (lldb_ast == nullptr)
7971 return nullptr;
7972 clang::ASTContext *ast = lldb_ast->getASTContext();
7973
Greg Claytond8d4a572015-08-11 21:38:15 +00007974 const char *selector_start = ::strchr (name, ' ');
7975 if (selector_start == nullptr)
7976 return nullptr;
7977
7978 selector_start++;
7979 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
7980
7981 size_t len = 0;
7982 const char *start;
7983 //printf ("name = '%s'\n", name);
7984
7985 unsigned num_selectors_with_args = 0;
7986 for (start = selector_start;
7987 start && *start != '\0' && *start != ']';
7988 start += len)
7989 {
7990 len = ::strcspn(start, ":]");
7991 bool has_arg = (start[len] == ':');
7992 if (has_arg)
7993 ++num_selectors_with_args;
7994 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
7995 if (has_arg)
7996 len += 1;
7997 }
7998
7999
8000 if (selector_idents.size() == 0)
8001 return nullptr;
8002
8003 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
8004 selector_idents.data());
8005
8006 clang::QualType method_qual_type (GetQualType(method_clang_type));
8007
8008 // Populate the method decl with parameter decls
8009 const clang::Type *method_type(method_qual_type.getTypePtr());
8010
8011 if (method_type == nullptr)
8012 return nullptr;
8013
8014 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8015
8016 if (!method_function_prototype)
8017 return nullptr;
8018
8019
8020 bool is_variadic = false;
8021 bool is_synthesized = false;
8022 bool is_defined = false;
8023 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
8024
8025 const unsigned num_args = method_function_prototype->getNumParams();
8026
8027 if (num_args != num_selectors_with_args)
8028 return nullptr; // some debug information is corrupt. We are not going to deal with it.
8029
8030 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create (*ast,
8031 clang::SourceLocation(), // beginLoc,
8032 clang::SourceLocation(), // endLoc,
8033 method_selector,
8034 method_function_prototype->getReturnType(),
8035 nullptr, // TypeSourceInfo *ResultTInfo,
8036 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(GetQualType(type)),
8037 name[0] == '-',
8038 is_variadic,
8039 is_synthesized,
8040 true, // is_implicitly_declared; we force this to true because we don't have source locations
8041 is_defined,
8042 imp_control,
8043 false /*has_related_result_type*/);
8044
8045
8046 if (objc_method_decl == nullptr)
8047 return nullptr;
8048
8049 if (num_args > 0)
8050 {
8051 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8052
8053 for (unsigned param_index = 0; param_index < num_args; ++param_index)
8054 {
8055 params.push_back (clang::ParmVarDecl::Create (*ast,
8056 objc_method_decl,
8057 clang::SourceLocation(),
8058 clang::SourceLocation(),
8059 nullptr, // anonymous
8060 method_function_prototype->getParamType(param_index),
8061 nullptr,
8062 clang::SC_Auto,
8063 nullptr));
8064 }
8065
8066 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8067 }
8068
8069 class_interface_decl->addDecl (objc_method_decl);
8070
8071#ifdef LLDB_CONFIGURATION_DEBUG
8072 VerifyDecl(objc_method_decl);
8073#endif
8074
8075 return objc_method_decl;
8076}
8077
8078bool
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008079ClangASTContext::GetHasExternalStorage (const CompilerType &type)
8080{
8081 if (IsClangType(type))
8082 return false;
8083
8084 clang::QualType qual_type (GetCanonicalQualType(type));
8085
8086 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8087 switch (type_class)
8088 {
8089 case clang::Type::Record:
8090 {
8091 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8092 if (cxx_record_decl)
8093 return cxx_record_decl->hasExternalLexicalStorage () || cxx_record_decl->hasExternalVisibleStorage();
8094 }
8095 break;
8096
8097 case clang::Type::Enum:
8098 {
8099 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8100 if (enum_decl)
8101 return enum_decl->hasExternalLexicalStorage () || enum_decl->hasExternalVisibleStorage();
8102 }
8103 break;
8104
8105 case clang::Type::ObjCObject:
8106 case clang::Type::ObjCInterface:
8107 {
8108 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8109 assert (objc_class_type);
8110 if (objc_class_type)
8111 {
8112 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8113
8114 if (class_interface_decl)
8115 return class_interface_decl->hasExternalLexicalStorage () || class_interface_decl->hasExternalVisibleStorage ();
8116 }
8117 }
8118 break;
8119
8120 case clang::Type::Typedef:
8121 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()));
8122
8123 case clang::Type::Elaborated:
8124 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
8125
8126 case clang::Type::Paren:
8127 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8128
8129 default:
8130 break;
8131 }
8132 return false;
8133}
8134
8135
8136bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008137ClangASTContext::SetHasExternalStorage (lldb::opaque_compiler_type_t type, bool has_extern)
Greg Claytond8d4a572015-08-11 21:38:15 +00008138{
8139 if (!type)
8140 return false;
8141
8142 clang::QualType qual_type (GetCanonicalQualType(type));
8143
8144 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8145 switch (type_class)
8146 {
8147 case clang::Type::Record:
8148 {
8149 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8150 if (cxx_record_decl)
8151 {
8152 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
8153 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
8154 return true;
8155 }
8156 }
8157 break;
8158
8159 case clang::Type::Enum:
8160 {
8161 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8162 if (enum_decl)
8163 {
8164 enum_decl->setHasExternalLexicalStorage (has_extern);
8165 enum_decl->setHasExternalVisibleStorage (has_extern);
8166 return true;
8167 }
8168 }
8169 break;
8170
8171 case clang::Type::ObjCObject:
8172 case clang::Type::ObjCInterface:
8173 {
8174 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8175 assert (objc_class_type);
8176 if (objc_class_type)
8177 {
8178 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8179
8180 if (class_interface_decl)
8181 {
8182 class_interface_decl->setHasExternalLexicalStorage (has_extern);
8183 class_interface_decl->setHasExternalVisibleStorage (has_extern);
8184 return true;
8185 }
8186 }
8187 }
8188 break;
8189
8190 case clang::Type::Typedef:
8191 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
8192
8193 case clang::Type::Elaborated:
8194 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
8195
8196 case clang::Type::Paren:
8197 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
8198
8199 default:
8200 break;
8201 }
8202 return false;
8203}
8204
8205
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008206bool
8207ClangASTContext::CanImport (const CompilerType &type, lldb_private::ClangASTImporter &importer)
8208{
8209 if (IsClangType(type))
8210 {
8211 // TODO: remove external completion BOOL
8212 // CompleteAndFetchChildren should get the Decl out and check for the
8213
8214 clang::QualType qual_type(GetCanonicalQualType(RemoveFastQualifiers(type)));
8215
8216 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8217 switch (type_class)
8218 {
8219 case clang::Type::Record:
8220 {
8221 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8222 if (cxx_record_decl)
8223 {
8224 if (importer.ResolveDeclOrigin (cxx_record_decl, NULL, NULL))
8225 return true;
8226 }
8227 }
8228 break;
8229
8230 case clang::Type::Enum:
8231 {
8232 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8233 if (enum_decl)
8234 {
8235 if (importer.ResolveDeclOrigin (enum_decl, NULL, NULL))
8236 return true;
8237 }
8238 }
8239 break;
8240
8241 case clang::Type::ObjCObject:
8242 case clang::Type::ObjCInterface:
8243 {
8244 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
8245 if (objc_class_type)
8246 {
8247 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8248 // We currently can't complete objective C types through the newly added ASTContext
8249 // because it only supports TagDecl objects right now...
8250 if (class_interface_decl)
8251 {
8252 if (importer.ResolveDeclOrigin (class_interface_decl, NULL, NULL))
8253 return true;
8254 }
8255 }
8256 }
8257 break;
8258
8259
8260 case clang::Type::Typedef:
8261 return CanImport(CompilerType (type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), importer);
8262
8263 case clang::Type::Elaborated:
8264 return CanImport(CompilerType (type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), importer);
8265
8266 case clang::Type::Paren:
8267 return CanImport(CompilerType (type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), importer);
8268
8269 default:
8270 break;
8271 }
8272 }
8273 return false;
8274}
8275bool
8276ClangASTContext::Import (const CompilerType &type, lldb_private::ClangASTImporter &importer)
8277{
8278 if (IsClangType(type))
8279 {
8280 // TODO: remove external completion BOOL
8281 // CompleteAndFetchChildren should get the Decl out and check for the
8282
8283 clang::QualType qual_type(GetCanonicalQualType(RemoveFastQualifiers(type)));
8284
8285 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8286 switch (type_class)
8287 {
8288 case clang::Type::Record:
8289 {
8290 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8291 if (cxx_record_decl)
8292 {
8293 if (importer.ResolveDeclOrigin (cxx_record_decl, NULL, NULL))
8294 return importer.CompleteAndFetchChildren(qual_type);
8295 }
8296 }
8297 break;
8298
8299 case clang::Type::Enum:
8300 {
8301 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8302 if (enum_decl)
8303 {
8304 if (importer.ResolveDeclOrigin (enum_decl, NULL, NULL))
8305 return importer.CompleteAndFetchChildren(qual_type);
8306 }
8307 }
8308 break;
8309
8310 case clang::Type::ObjCObject:
8311 case clang::Type::ObjCInterface:
8312 {
8313 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
8314 if (objc_class_type)
8315 {
8316 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8317 // We currently can't complete objective C types through the newly added ASTContext
8318 // because it only supports TagDecl objects right now...
8319 if (class_interface_decl)
8320 {
8321 if (importer.ResolveDeclOrigin (class_interface_decl, NULL, NULL))
8322 return importer.CompleteAndFetchChildren(qual_type);
8323 }
8324 }
8325 }
8326 break;
8327
8328
8329 case clang::Type::Typedef:
8330 return Import (CompilerType(type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()), importer);
8331
8332 case clang::Type::Elaborated:
8333 return Import (CompilerType(type.GetTypeSystem(),llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()), importer);
8334
8335 case clang::Type::Paren:
8336 return Import (CompilerType(type.GetTypeSystem(),llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()), importer);
8337
8338 default:
8339 break;
8340 }
8341 }
8342 return false;
8343}
8344
8345
Greg Claytond8d4a572015-08-11 21:38:15 +00008346#pragma mark TagDecl
8347
8348bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008349ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008350{
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008351 clang::QualType qual_type (ClangASTContext::GetQualType(type));
8352 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008353 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008354 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8355 if (tag_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008356 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008357 clang::TagDecl *tag_decl = tag_type->getDecl();
8358 if (tag_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00008359 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008360 tag_decl->startDefinition();
8361 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008362 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008363 }
8364
8365 const clang::ObjCObjectType *object_type = qual_type->getAs<clang::ObjCObjectType>();
8366 if (object_type)
8367 {
8368 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8369 if (interface_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00008370 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008371 interface_decl->startDefinition();
8372 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008373 }
8374 }
8375 }
8376 return false;
8377}
8378
8379bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008380ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008381{
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008382 clang::QualType qual_type (ClangASTContext::GetQualType(type));
8383 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008384 {
Greg Claytond8d4a572015-08-11 21:38:15 +00008385 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8386
8387 if (cxx_record_decl)
8388 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008389 if (!cxx_record_decl->isCompleteDefinition())
8390 cxx_record_decl->completeDefinition();
8391 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8392 cxx_record_decl->setHasExternalLexicalStorage (false);
8393 cxx_record_decl->setHasExternalVisibleStorage (false);
Greg Claytond8d4a572015-08-11 21:38:15 +00008394 return true;
8395 }
8396
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008397 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
Greg Claytond8d4a572015-08-11 21:38:15 +00008398
8399 if (enutype)
8400 {
8401 clang::EnumDecl *enum_decl = enutype->getDecl();
8402
8403 if (enum_decl)
8404 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008405 if (!enum_decl->isCompleteDefinition())
Greg Claytond8d4a572015-08-11 21:38:15 +00008406 {
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008407 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8408 if (lldb_ast == nullptr)
8409 return false;
8410 clang::ASTContext *ast = lldb_ast->getASTContext();
8411
8412 /// TODO This really needs to be fixed.
8413
8414 unsigned NumPositiveBits = 1;
8415 unsigned NumNegativeBits = 0;
8416
8417 clang::QualType promotion_qual_type;
8418 // If the enum integer type is less than an integer in bit width,
8419 // then we must promote it to an integer size.
8420 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8421 {
8422 if (enum_decl->getIntegerType()->isSignedIntegerType())
8423 promotion_qual_type = ast->IntTy;
8424 else
8425 promotion_qual_type = ast->UnsignedIntTy;
8426 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008427 else
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008428 promotion_qual_type = enum_decl->getIntegerType();
8429
8430 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Claytond8d4a572015-08-11 21:38:15 +00008431 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008432 return true;
8433 }
8434 }
8435 }
8436 return false;
8437}
8438
Greg Claytond8d4a572015-08-11 21:38:15 +00008439bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008440ClangASTContext::AddEnumerationValueToEnumerationType (lldb::opaque_compiler_type_t type,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008441 const CompilerType &enumerator_clang_type,
8442 const Declaration &decl,
8443 const char *name,
8444 int64_t enum_value,
8445 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008446{
8447 if (type && enumerator_clang_type.IsValid() && name && name[0])
8448 {
8449 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8450
8451 bool is_signed = false;
8452 enumerator_clang_type.IsIntegerType (is_signed);
8453 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8454 if (clang_type)
8455 {
8456 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8457
8458 if (enutype)
8459 {
8460 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8461 enum_llvm_apsint = enum_value;
8462 clang::EnumConstantDecl *enumerator_decl =
8463 clang::EnumConstantDecl::Create (*getASTContext(),
8464 enutype->getDecl(),
8465 clang::SourceLocation(),
8466 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8467 GetQualType(enumerator_clang_type),
8468 nullptr,
8469 enum_llvm_apsint);
8470
8471 if (enumerator_decl)
8472 {
8473 enutype->getDecl()->addDecl(enumerator_decl);
8474
8475#ifdef LLDB_CONFIGURATION_DEBUG
8476 VerifyDecl(enumerator_decl);
8477#endif
8478
8479 return true;
8480 }
8481 }
8482 }
8483 }
8484 return false;
8485}
8486
Greg Claytona1e5dc82015-08-11 22:53:00 +00008487CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008488ClangASTContext::GetEnumerationIntegerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008489{
8490 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8491 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8492 if (clang_type)
8493 {
8494 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8495 if (enutype)
8496 {
8497 clang::EnumDecl *enum_decl = enutype->getDecl();
8498 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008499 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008500 }
8501 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008502 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008503}
8504
Greg Claytona1e5dc82015-08-11 22:53:00 +00008505CompilerType
8506ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008507{
8508 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8509 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008510 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008511 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008512 return CompilerType();
8513 return CompilerType (ast->getASTContext(),
Greg Claytond8d4a572015-08-11 21:38:15 +00008514 ast->getASTContext()->getMemberPointerType (GetQualType(pointee_type),
8515 GetQualType(type).getTypePtr()));
8516 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008517 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008518}
8519
8520
8521size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008522ClangASTContext::ConvertStringToFloatValue (lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008523{
8524 if (type)
8525 {
8526 clang::QualType qual_type (GetCanonicalQualType(type));
8527 uint32_t count = 0;
8528 bool is_complex = false;
8529 if (IsFloatingPointType (type, count, is_complex))
8530 {
8531 // TODO: handle complex and vector types
8532 if (count != 1)
8533 return false;
8534
8535 llvm::StringRef s_sref(s);
8536 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8537
8538 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8539 const uint64_t byte_size = bit_size / 8;
8540 if (dst_size >= byte_size)
8541 {
8542 if (bit_size == sizeof(float)*8)
8543 {
8544 float float32 = ap_float.convertToFloat();
8545 ::memcpy (dst, &float32, byte_size);
8546 return byte_size;
8547 }
8548 else if (bit_size >= 64)
8549 {
8550 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8551 ::memcpy (dst, ap_int.getRawData(), byte_size);
8552 return byte_size;
8553 }
8554 }
8555 }
8556 }
8557 return 0;
8558}
8559
8560
8561
8562//----------------------------------------------------------------------
8563// Dumping types
8564//----------------------------------------------------------------------
8565#define DEPTH_INCREMENT 2
8566
8567void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008568ClangASTContext::DumpValue (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00008569 Stream *s,
8570 lldb::Format format,
8571 const lldb_private::DataExtractor &data,
8572 lldb::offset_t data_byte_offset,
8573 size_t data_byte_size,
8574 uint32_t bitfield_bit_size,
8575 uint32_t bitfield_bit_offset,
8576 bool show_types,
8577 bool show_summary,
8578 bool verbose,
8579 uint32_t depth)
8580{
8581 if (!type)
8582 return;
8583
8584 clang::QualType qual_type(GetQualType(type));
8585 switch (qual_type->getTypeClass())
8586 {
8587 case clang::Type::Record:
8588 if (GetCompleteType(type))
8589 {
8590 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8591 const clang::RecordDecl *record_decl = record_type->getDecl();
8592 assert(record_decl);
8593 uint32_t field_bit_offset = 0;
8594 uint32_t field_byte_offset = 0;
8595 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8596 uint32_t child_idx = 0;
8597
8598 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8599 if (cxx_record_decl)
8600 {
8601 // We might have base classes to print out first
8602 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8603 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8604 base_class != base_class_end;
8605 ++base_class)
8606 {
8607 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8608
8609 // Skip empty base classes
8610 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8611 continue;
8612
8613 if (base_class->isVirtual())
8614 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8615 else
8616 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8617 field_byte_offset = field_bit_offset / 8;
8618 assert (field_bit_offset % 8 == 0);
8619 if (child_idx == 0)
8620 s->PutChar('{');
8621 else
8622 s->PutChar(',');
8623
8624 clang::QualType base_class_qual_type = base_class->getType();
8625 std::string base_class_type_name(base_class_qual_type.getAsString());
8626
8627 // Indent and print the base class type name
8628 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8629
8630 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8631
8632 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008633 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008634 base_clang_type.DumpValue (exe_ctx,
8635 s, // Stream to dump to
8636 base_clang_type.GetFormat(), // The format with which to display the member
8637 data, // Data buffer containing all bytes for this type
8638 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8639 base_class_type_info.Width / 8, // Size of this type in bytes
8640 0, // Bitfield bit size
8641 0, // Bitfield bit offset
8642 show_types, // Boolean indicating if we should show the variable types
8643 show_summary, // Boolean indicating if we should show a summary for the current type
8644 verbose, // Verbose output?
8645 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8646
8647 ++child_idx;
8648 }
8649 }
8650 uint32_t field_idx = 0;
8651 clang::RecordDecl::field_iterator field, field_end;
8652 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8653 {
8654 // Print the starting squiggly bracket (if this is the
8655 // first member) or comma (for member 2 and beyond) for
8656 // the struct/union/class member.
8657 if (child_idx == 0)
8658 s->PutChar('{');
8659 else
8660 s->PutChar(',');
8661
8662 // Indent
8663 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8664
8665 clang::QualType field_type = field->getType();
8666 // Print the member type if requested
8667 // Figure out the type byte size (field_type_info.first) and
8668 // alignment (field_type_info.second) from the AST context.
8669 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8670 assert(field_idx < record_layout.getFieldCount());
8671 // Figure out the field offset within the current struct/union/class type
8672 field_bit_offset = record_layout.getFieldOffset (field_idx);
8673 field_byte_offset = field_bit_offset / 8;
8674 uint32_t field_bitfield_bit_size = 0;
8675 uint32_t field_bitfield_bit_offset = 0;
8676 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8677 field_bitfield_bit_offset = field_bit_offset % 8;
8678
8679 if (show_types)
8680 {
8681 std::string field_type_name(field_type.getAsString());
8682 if (field_bitfield_bit_size > 0)
8683 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8684 else
8685 s->Printf("(%s) ", field_type_name.c_str());
8686 }
8687 // Print the member name and equal sign
8688 s->Printf("%s = ", field->getNameAsString().c_str());
8689
8690
8691 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008692 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008693 field_clang_type.DumpValue (exe_ctx,
8694 s, // Stream to dump to
8695 field_clang_type.GetFormat(), // The format with which to display the member
8696 data, // Data buffer containing all bytes for this type
8697 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8698 field_type_info.Width / 8, // Size of this type in bytes
8699 field_bitfield_bit_size, // Bitfield bit size
8700 field_bitfield_bit_offset, // Bitfield bit offset
8701 show_types, // Boolean indicating if we should show the variable types
8702 show_summary, // Boolean indicating if we should show a summary for the current type
8703 verbose, // Verbose output?
8704 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8705 }
8706
8707 // Indent the trailing squiggly bracket
8708 if (child_idx > 0)
8709 s->Printf("\n%*s}", depth, "");
8710 }
8711 return;
8712
8713 case clang::Type::Enum:
8714 if (GetCompleteType(type))
8715 {
8716 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8717 const clang::EnumDecl *enum_decl = enutype->getDecl();
8718 assert(enum_decl);
8719 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8720 lldb::offset_t offset = data_byte_offset;
8721 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8722 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8723 {
8724 if (enum_pos->getInitVal() == enum_value)
8725 {
8726 s->Printf("%s", enum_pos->getNameAsString().c_str());
8727 return;
8728 }
8729 }
8730 // If we have gotten here we didn't get find the enumerator in the
8731 // enum decl, so just print the integer.
8732 s->Printf("%" PRIi64, enum_value);
8733 }
8734 return;
8735
8736 case clang::Type::ConstantArray:
8737 {
8738 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8739 bool is_array_of_characters = false;
8740 clang::QualType element_qual_type = array->getElementType();
8741
8742 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8743 if (canonical_type)
8744 is_array_of_characters = canonical_type->isCharType();
8745
8746 const uint64_t element_count = array->getSize().getLimitedValue();
8747
8748 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8749
8750 uint32_t element_idx = 0;
8751 uint32_t element_offset = 0;
8752 uint64_t element_byte_size = field_type_info.Width / 8;
8753 uint32_t element_stride = element_byte_size;
8754
8755 if (is_array_of_characters)
8756 {
8757 s->PutChar('"');
8758 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8759 s->PutChar('"');
8760 return;
8761 }
8762 else
8763 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008764 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008765 lldb::Format element_format = element_clang_type.GetFormat();
8766
8767 for (element_idx = 0; element_idx < element_count; ++element_idx)
8768 {
8769 // Print the starting squiggly bracket (if this is the
8770 // first member) or comman (for member 2 and beyong) for
8771 // the struct/union/class member.
8772 if (element_idx == 0)
8773 s->PutChar('{');
8774 else
8775 s->PutChar(',');
8776
8777 // Indent and print the index
8778 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8779
8780 // Figure out the field offset within the current struct/union/class type
8781 element_offset = element_idx * element_stride;
8782
8783 // Dump the value of the member
8784 element_clang_type.DumpValue (exe_ctx,
8785 s, // Stream to dump to
8786 element_format, // The format with which to display the element
8787 data, // Data buffer containing all bytes for this type
8788 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8789 element_byte_size, // Size of this type in bytes
8790 0, // Bitfield bit size
8791 0, // Bitfield bit offset
8792 show_types, // Boolean indicating if we should show the variable types
8793 show_summary, // Boolean indicating if we should show a summary for the current type
8794 verbose, // Verbose output?
8795 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8796 }
8797
8798 // Indent the trailing squiggly bracket
8799 if (element_idx > 0)
8800 s->Printf("\n%*s}", depth, "");
8801 }
8802 }
8803 return;
8804
8805 case clang::Type::Typedef:
8806 {
8807 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8808
Greg Claytona1e5dc82015-08-11 22:53:00 +00008809 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008810 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8811 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8812 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8813
8814 return typedef_clang_type.DumpValue (exe_ctx,
8815 s, // Stream to dump to
8816 typedef_format, // The format with which to display the element
8817 data, // Data buffer containing all bytes for this type
8818 data_byte_offset, // Offset into "data" where to grab value from
8819 typedef_byte_size, // Size of this type in bytes
8820 bitfield_bit_size, // Bitfield bit size
8821 bitfield_bit_offset,// Bitfield bit offset
8822 show_types, // Boolean indicating if we should show the variable types
8823 show_summary, // Boolean indicating if we should show a summary for the current type
8824 verbose, // Verbose output?
8825 depth); // Scope depth for any types that have children
8826 }
8827 break;
8828
8829 case clang::Type::Elaborated:
8830 {
8831 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008832 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008833 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8834 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8835 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8836
8837 return elaborated_clang_type.DumpValue (exe_ctx,
8838 s, // Stream to dump to
8839 elaborated_format, // The format with which to display the element
8840 data, // Data buffer containing all bytes for this type
8841 data_byte_offset, // Offset into "data" where to grab value from
8842 elaborated_byte_size, // Size of this type in bytes
8843 bitfield_bit_size, // Bitfield bit size
8844 bitfield_bit_offset,// Bitfield bit offset
8845 show_types, // Boolean indicating if we should show the variable types
8846 show_summary, // Boolean indicating if we should show a summary for the current type
8847 verbose, // Verbose output?
8848 depth); // Scope depth for any types that have children
8849 }
8850 break;
8851
8852 case clang::Type::Paren:
8853 {
8854 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008855 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008856
8857 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8858 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8859 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8860
8861 return desugar_clang_type.DumpValue (exe_ctx,
8862 s, // Stream to dump to
8863 desugar_format, // The format with which to display the element
8864 data, // Data buffer containing all bytes for this type
8865 data_byte_offset, // Offset into "data" where to grab value from
8866 desugar_byte_size, // Size of this type in bytes
8867 bitfield_bit_size, // Bitfield bit size
8868 bitfield_bit_offset,// Bitfield bit offset
8869 show_types, // Boolean indicating if we should show the variable types
8870 show_summary, // Boolean indicating if we should show a summary for the current type
8871 verbose, // Verbose output?
8872 depth); // Scope depth for any types that have children
8873 }
8874 break;
8875
8876 default:
8877 // We are down to a scalar type that we just need to display.
8878 data.Dump(s,
8879 data_byte_offset,
8880 format,
8881 data_byte_size,
8882 1,
8883 UINT32_MAX,
8884 LLDB_INVALID_ADDRESS,
8885 bitfield_bit_size,
8886 bitfield_bit_offset);
8887
8888 if (show_summary)
8889 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8890 break;
8891 }
8892}
8893
8894
8895
8896
8897bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008898ClangASTContext::DumpTypeValue (lldb::opaque_compiler_type_t type, Stream *s,
Greg Claytond8d4a572015-08-11 21:38:15 +00008899 lldb::Format format,
8900 const lldb_private::DataExtractor &data,
8901 lldb::offset_t byte_offset,
8902 size_t byte_size,
8903 uint32_t bitfield_bit_size,
8904 uint32_t bitfield_bit_offset,
8905 ExecutionContextScope *exe_scope)
8906{
8907 if (!type)
8908 return false;
8909 if (IsAggregateType(type))
8910 {
8911 return false;
8912 }
8913 else
8914 {
8915 clang::QualType qual_type(GetQualType(type));
8916
8917 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8918 switch (type_class)
8919 {
8920 case clang::Type::Typedef:
8921 {
8922 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008923 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008924 if (format == eFormatDefault)
8925 format = typedef_clang_type.GetFormat();
8926 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8927 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8928
8929 return typedef_clang_type.DumpTypeValue (s,
8930 format, // The format with which to display the element
8931 data, // Data buffer containing all bytes for this type
8932 byte_offset, // Offset into "data" where to grab value from
8933 typedef_byte_size, // Size of this type in bytes
8934 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
8935 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
8936 exe_scope);
8937 }
8938 break;
8939
8940 case clang::Type::Enum:
8941 // If our format is enum or default, show the enumeration value as
8942 // its enumeration string value, else just display it as requested.
8943 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
8944 {
8945 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8946 const clang::EnumDecl *enum_decl = enutype->getDecl();
8947 assert(enum_decl);
8948 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8949 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
8950 lldb::offset_t offset = byte_offset;
8951 if (is_signed)
8952 {
8953 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8954 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8955 {
8956 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
8957 {
8958 s->PutCString (enum_pos->getNameAsString().c_str());
8959 return true;
8960 }
8961 }
8962 // If we have gotten here we didn't get find the enumerator in the
8963 // enum decl, so just print the integer.
8964 s->Printf("%" PRIi64, enum_svalue);
8965 }
8966 else
8967 {
8968 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8969 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8970 {
8971 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
8972 {
8973 s->PutCString (enum_pos->getNameAsString().c_str());
8974 return true;
8975 }
8976 }
8977 // If we have gotten here we didn't get find the enumerator in the
8978 // enum decl, so just print the integer.
8979 s->Printf("%" PRIu64, enum_uvalue);
8980 }
8981 return true;
8982 }
8983 // format was not enum, just fall through and dump the value as requested....
8984
8985 default:
8986 // We are down to a scalar type that we just need to display.
8987 {
8988 uint32_t item_count = 1;
8989 // A few formats, we might need to modify our size and count for depending
8990 // on how we are trying to display the value...
8991 switch (format)
8992 {
8993 default:
8994 case eFormatBoolean:
8995 case eFormatBinary:
8996 case eFormatComplex:
8997 case eFormatCString: // NULL terminated C strings
8998 case eFormatDecimal:
8999 case eFormatEnum:
9000 case eFormatHex:
9001 case eFormatHexUppercase:
9002 case eFormatFloat:
9003 case eFormatOctal:
9004 case eFormatOSType:
9005 case eFormatUnsigned:
9006 case eFormatPointer:
9007 case eFormatVectorOfChar:
9008 case eFormatVectorOfSInt8:
9009 case eFormatVectorOfUInt8:
9010 case eFormatVectorOfSInt16:
9011 case eFormatVectorOfUInt16:
9012 case eFormatVectorOfSInt32:
9013 case eFormatVectorOfUInt32:
9014 case eFormatVectorOfSInt64:
9015 case eFormatVectorOfUInt64:
9016 case eFormatVectorOfFloat32:
9017 case eFormatVectorOfFloat64:
9018 case eFormatVectorOfUInt128:
9019 break;
9020
9021 case eFormatChar:
9022 case eFormatCharPrintable:
9023 case eFormatCharArray:
9024 case eFormatBytes:
9025 case eFormatBytesWithASCII:
9026 item_count = byte_size;
9027 byte_size = 1;
9028 break;
9029
9030 case eFormatUnicode16:
9031 item_count = byte_size / 2;
9032 byte_size = 2;
9033 break;
9034
9035 case eFormatUnicode32:
9036 item_count = byte_size / 4;
9037 byte_size = 4;
9038 break;
9039 }
9040 return data.Dump (s,
9041 byte_offset,
9042 format,
9043 byte_size,
9044 item_count,
9045 UINT32_MAX,
9046 LLDB_INVALID_ADDRESS,
9047 bitfield_bit_size,
9048 bitfield_bit_offset,
9049 exe_scope);
9050 }
9051 break;
9052 }
9053 }
9054 return 0;
9055}
9056
9057
9058
9059void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009060ClangASTContext::DumpSummary (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00009061 Stream *s,
9062 const lldb_private::DataExtractor &data,
9063 lldb::offset_t data_byte_offset,
9064 size_t data_byte_size)
9065{
9066 uint32_t length = 0;
9067 if (IsCStringType (type, length))
9068 {
9069 if (exe_ctx)
9070 {
9071 Process *process = exe_ctx->GetProcessPtr();
9072 if (process)
9073 {
9074 lldb::offset_t offset = data_byte_offset;
9075 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9076 std::vector<uint8_t> buf;
9077 if (length > 0)
9078 buf.resize (length);
9079 else
9080 buf.resize (256);
9081
9082 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
9083 buf.back() = '\0';
9084 size_t bytes_read;
9085 size_t total_cstr_len = 0;
9086 Error error;
9087 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
9088 {
9089 const size_t len = strlen((const char *)&buf.front());
9090 if (len == 0)
9091 break;
9092 if (total_cstr_len == 0)
9093 s->PutCString (" \"");
9094 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9095 total_cstr_len += len;
9096 if (len < buf.size())
9097 break;
9098 pointer_address += total_cstr_len;
9099 }
9100 if (total_cstr_len > 0)
9101 s->PutChar ('"');
9102 }
9103 }
9104 }
9105}
9106
9107void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009108ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00009109{
9110 StreamFile s (stdout, false);
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00009111 DumpTypeDescription (type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009112 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
9113 if (metadata)
9114 {
9115 metadata->Dump (&s);
9116 }
9117}
9118
9119void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009120ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type, Stream *s)
Greg Claytond8d4a572015-08-11 21:38:15 +00009121{
9122 if (type)
9123 {
9124 clang::QualType qual_type(GetQualType(type));
9125
9126 llvm::SmallVector<char, 1024> buf;
9127 llvm::raw_svector_ostream llvm_ostrm (buf);
9128
9129 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9130 switch (type_class)
9131 {
9132 case clang::Type::ObjCObject:
9133 case clang::Type::ObjCInterface:
9134 {
9135 GetCompleteType(type);
9136
9137 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9138 assert (objc_class_type);
9139 if (objc_class_type)
9140 {
9141 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9142 if (class_interface_decl)
9143 {
9144 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9145 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9146 }
9147 }
9148 }
9149 break;
9150
9151 case clang::Type::Typedef:
9152 {
9153 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
9154 if (typedef_type)
9155 {
9156 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9157 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
9158 if (!clang_typedef_name.empty())
9159 {
9160 s->PutCString ("typedef ");
9161 s->PutCString (clang_typedef_name.c_str());
9162 }
9163 }
9164 }
9165 break;
9166
9167 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009168 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009169 return;
9170
9171 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009172 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009173 return;
9174
9175 case clang::Type::Record:
9176 {
9177 GetCompleteType(type);
9178
9179 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9180 const clang::RecordDecl *record_decl = record_type->getDecl();
9181 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9182
9183 if (cxx_record_decl)
9184 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9185 else
9186 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9187 }
9188 break;
9189
9190 default:
9191 {
9192 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9193 if (tag_type)
9194 {
9195 clang::TagDecl *tag_decl = tag_type->getDecl();
9196 if (tag_decl)
9197 tag_decl->print(llvm_ostrm, 0);
9198 }
9199 else
9200 {
9201 std::string clang_type_name(qual_type.getAsString());
9202 if (!clang_type_name.empty())
9203 s->PutCString (clang_type_name.c_str());
9204 }
9205 }
9206 }
9207
Greg Claytond8d4a572015-08-11 21:38:15 +00009208 if (buf.size() > 0)
9209 {
9210 s->Write (buf.data(), buf.size());
9211 }
9212 }
9213}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009214
Greg Clayton5dfc4a42015-12-02 00:43:32 +00009215void
9216ClangASTContext::DumpTypeName (const CompilerType &type)
9217{
9218 if (IsClangType(type))
9219 {
9220 clang::QualType qual_type(GetCanonicalQualType(RemoveFastQualifiers(type)));
9221
9222 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9223 switch (type_class)
9224 {
9225 case clang::Type::Record:
9226 {
9227 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
9228 if (cxx_record_decl)
9229 printf("class %s", cxx_record_decl->getName().str().c_str());
9230 }
9231 break;
9232
9233 case clang::Type::Enum:
9234 {
9235 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
9236 if (enum_decl)
9237 {
9238 printf("enum %s", enum_decl->getName().str().c_str());
9239 }
9240 }
9241 break;
9242
9243 case clang::Type::ObjCObject:
9244 case clang::Type::ObjCInterface:
9245 {
9246 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9247 if (objc_class_type)
9248 {
9249 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9250 // We currently can't complete objective C types through the newly added ASTContext
9251 // because it only supports TagDecl objects right now...
9252 if (class_interface_decl)
9253 printf("@class %s", class_interface_decl->getName().str().c_str());
9254 }
9255 }
9256 break;
9257
9258
9259 case clang::Type::Typedef:
9260 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getName().str().c_str());
9261 break;
9262
9263 case clang::Type::Elaborated:
9264 printf("elaborated ");
9265 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
9266
9267 case clang::Type::Paren:
9268 printf("paren ");
9269 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9270
9271 default:
9272 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9273 break;
9274 }
9275 }
9276
9277}
9278
9279
9280
Greg Clayton8b4edba2015-08-14 20:02:05 +00009281clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00009282ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009283 lldb::AccessType access_type,
9284 const char *parent_name,
9285 int tag_decl_kind,
9286 const ClangASTContext::TemplateParameterInfos &template_param_infos)
9287{
9288 if (template_param_infos.IsValid())
9289 {
9290 std::string template_basename(parent_name);
9291 template_basename.erase (template_basename.find('<'));
9292
9293 return CreateClassTemplateDecl (decl_ctx,
9294 access_type,
9295 template_basename.c_str(),
9296 tag_decl_kind,
9297 template_param_infos);
9298 }
9299 return NULL;
9300}
9301
Greg Clayton6dc8d582015-08-18 22:32:36 +00009302void
9303ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
9304{
9305 ClangASTContext *ast = (ClangASTContext *)baton;
9306 SymbolFile *sym_file = ast->GetSymbolFile();
9307 if (sym_file)
9308 {
9309 CompilerType clang_type = GetTypeForDecl (decl);
9310 if (clang_type)
9311 sym_file->CompleteType (clang_type);
9312 }
9313}
9314
9315void
9316ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9317{
9318 ClangASTContext *ast = (ClangASTContext *)baton;
9319 SymbolFile *sym_file = ast->GetSymbolFile();
9320 if (sym_file)
9321 {
9322 CompilerType clang_type = GetTypeForDecl (decl);
9323 if (clang_type)
9324 sym_file->CompleteType (clang_type);
9325 }
9326}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009327
Greg Clayton261ac3f2015-08-28 01:01:03 +00009328
9329DWARFASTParser *
9330ClangASTContext::GetDWARFParser ()
9331{
9332 if (!m_dwarf_ast_parser_ap)
9333 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9334 return m_dwarf_ast_parser_ap.get();
9335}
9336
9337
Greg Clayton8b4edba2015-08-14 20:02:05 +00009338bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009339ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009340 const clang::RecordDecl *record_decl,
9341 uint64_t &bit_size,
9342 uint64_t &alignment,
9343 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9344 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9345 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9346{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009347 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00009348 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
9349 return dwarf_ast_parser->LayoutRecordType(record_decl, bit_size, alignment, field_offsets, base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009350}
9351
Greg Clayton99558cc42015-08-24 23:46:31 +00009352//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009353// CompilerDecl override functions
9354//----------------------------------------------------------------------
9355lldb::VariableSP
9356ClangASTContext::DeclGetVariable (void *opaque_decl)
9357{
9358 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
9359 {
9360 auto decl_search_it = m_decl_objects.find(opaque_decl);
9361 if (decl_search_it != m_decl_objects.end())
9362 return std::static_pointer_cast<Variable>(decl_search_it->second);
9363 }
9364 return VariableSP();
9365}
9366
9367void
9368ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
9369{
9370 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
9371 m_decl_objects.insert(std::make_pair(opaque_decl, object));
9372}
9373
9374ConstString
9375ClangASTContext::DeclGetName (void *opaque_decl)
9376{
9377 if (opaque_decl)
9378 {
9379 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
9380 if (nd != nullptr)
Greg Claytonfe689042015-11-10 17:47:04 +00009381 return ConstString(nd->getDeclName().getAsString());
Paul Hermand628cbb2015-09-15 23:44:17 +00009382 }
9383 return ConstString();
9384}
9385
Greg Claytonfe689042015-11-10 17:47:04 +00009386ConstString
9387ClangASTContext::DeclGetMangledName (void *opaque_decl)
9388{
9389 if (opaque_decl)
9390 {
9391 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>((clang::Decl*)opaque_decl);
9392 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd))
9393 {
9394 clang::MangleContext *mc = getMangleContext();
9395 if (mc && mc->shouldMangleCXXName(nd))
9396 {
9397 llvm::SmallVector<char, 1024> buf;
9398 llvm::raw_svector_ostream llvm_ostrm (buf);
9399 if (llvm::isa<clang::CXXConstructorDecl>(nd))
9400 {
9401 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), Ctor_Complete, llvm_ostrm);
9402 }
9403 else if (llvm::isa<clang::CXXDestructorDecl>(nd))
9404 {
9405 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), Dtor_Complete, llvm_ostrm);
9406 }
9407 else
9408 {
9409 mc->mangleName(nd, llvm_ostrm);
9410 }
9411 if (buf.size() > 0)
9412 return ConstString(buf.data(), buf.size());
9413 }
9414 }
9415 }
9416 return ConstString();
9417}
9418
9419CompilerDeclContext
9420ClangASTContext::DeclGetDeclContext (void *opaque_decl)
9421{
9422 if (opaque_decl)
9423 return CompilerDeclContext(this, ((clang::Decl*)opaque_decl)->getDeclContext());
9424 else
9425 return CompilerDeclContext();
9426}
9427
9428CompilerType
9429ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl)
9430{
9431 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9432 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9433 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9434 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9435 else
9436 return CompilerType();
9437}
9438
9439size_t
9440ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl)
9441{
9442 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9443 return func_decl->param_size();
9444 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9445 return objc_method->param_size();
9446 else
9447 return 0;
9448}
9449
9450CompilerType
9451ClangASTContext::DeclGetFunctionArgumentType (void *opaque_decl, size_t idx)
9452{
9453 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9454 {
9455 if (idx < func_decl->param_size())
9456 {
9457 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9458 if (var_decl)
9459 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9460 }
9461 }
9462 else if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9463 {
9464 if (idx < objc_method->param_size())
9465 return CompilerType(this, objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9466 }
9467 return CompilerType();
9468}
9469
Paul Hermand628cbb2015-09-15 23:44:17 +00009470//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009471// CompilerDeclContext functions
9472//----------------------------------------------------------------------
9473
Paul Hermand628cbb2015-09-15 23:44:17 +00009474std::vector<void *>
9475ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name)
9476{
9477 std::vector<void *> found_decls;
9478 if (opaque_decl_ctx)
9479 {
9480 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9481 std::set<DeclContext *> searched;
9482 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009483 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009484
9485 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
9486 {
9487 search_queue.insert(std::make_pair(decl_context, decl_context));
9488
9489 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
9490 {
Eugene Leviantc1ba9fc2015-11-13 11:00:10 +00009491 if (!searched.insert(it->second).second)
9492 continue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009493 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9494
Paul Hermand628cbb2015-09-15 23:44:17 +00009495 for (clang::Decl *child : it->second->decls())
9496 {
Paul Hermanea188fc2015-09-16 18:48:30 +00009497 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00009498 {
9499 clang::DeclContext *from = ud->getCommonAncestor();
9500 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9501 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
9502 }
9503 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9504 {
9505 for (clang::UsingShadowDecl *usd : ud->shadows())
9506 {
9507 clang::Decl *target = usd->getTargetDecl();
9508 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
9509 {
9510 IdentifierInfo *ii = nd->getIdentifier();
9511 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9512 found_decls.push_back(nd);
9513 }
9514 }
9515 }
Paul Hermanea188fc2015-09-16 18:48:30 +00009516 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
9517 {
9518 IdentifierInfo *ii = nd->getIdentifier();
9519 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9520 found_decls.push_back(nd);
9521 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009522 }
9523 }
9524 }
9525 }
9526 return found_decls;
9527}
9528
Greg Clayton99558cc42015-08-24 23:46:31 +00009529bool
9530ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009531{
Greg Clayton99558cc42015-08-24 23:46:31 +00009532 if (opaque_decl_ctx)
9533 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9534 else
9535 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009536}
9537
Greg Clayton99558cc42015-08-24 23:46:31 +00009538ConstString
9539ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009540{
Greg Clayton99558cc42015-08-24 23:46:31 +00009541 if (opaque_decl_ctx)
9542 {
9543 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9544 if (named_decl)
9545 return ConstString(named_decl->getName());
9546 }
9547 return ConstString();
9548}
9549
9550bool
9551ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9552 lldb::LanguageType *language_ptr,
9553 bool *is_instance_method_ptr,
9554 ConstString *language_object_name_ptr)
9555{
9556 if (opaque_decl_ctx)
9557 {
9558 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9559 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9560 {
9561 if (is_instance_method_ptr)
9562 *is_instance_method_ptr = objc_method->isInstanceMethod();
9563 if (language_ptr)
9564 *language_ptr = eLanguageTypeObjC;
9565 if (language_object_name_ptr)
9566 language_object_name_ptr->SetCString("self");
9567 return true;
9568 }
9569 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9570 {
9571 if (is_instance_method_ptr)
9572 *is_instance_method_ptr = cxx_method->isInstance();
9573 if (language_ptr)
9574 *language_ptr = eLanguageTypeC_plus_plus;
9575 if (language_object_name_ptr)
9576 language_object_name_ptr->SetCString("this");
9577 return true;
9578 }
9579 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9580 {
9581 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9582 if (metadata && metadata->HasObjectPtr())
9583 {
9584 if (is_instance_method_ptr)
9585 *is_instance_method_ptr = true;
9586 if (language_ptr)
9587 *language_ptr = eLanguageTypeObjC;
9588 if (language_object_name_ptr)
9589 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9590 return true;
9591 }
9592 }
9593 }
9594 return false;
9595}
9596
9597clang::DeclContext *
9598ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9599{
9600 if (dc.IsClang())
9601 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9602 return nullptr;
9603}
9604
9605
9606ObjCMethodDecl *
9607ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
9608{
9609 if (dc.IsClang())
9610 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9611 return nullptr;
9612}
9613
9614CXXMethodDecl *
9615ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9616{
9617 if (dc.IsClang())
9618 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9619 return nullptr;
9620}
9621
9622clang::FunctionDecl *
9623ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9624{
9625 if (dc.IsClang())
9626 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9627 return nullptr;
9628}
9629
9630clang::NamespaceDecl *
9631ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9632{
9633 if (dc.IsClang())
9634 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9635 return nullptr;
9636}
9637
9638ClangASTMetadata *
9639ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9640{
9641 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9642 if (ast)
9643 return ClangASTContext::GetMetadata (ast, object);
9644 return nullptr;
9645}
9646
9647clang::ASTContext *
9648ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9649{
Greg Claytonf73034f2015-09-08 18:15:05 +00009650 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9651 if (ast)
9652 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009653 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009654}
9655
Jim Ingham151c0322015-09-15 21:13:50 +00009656ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9657 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009658 m_target_wp(target.shared_from_this()),
9659 m_persistent_variables (new ClangPersistentVariables)
Jim Ingham151c0322015-09-15 21:13:50 +00009660{
9661}
9662
9663UserExpression *
9664ClangASTContextForExpressions::GetUserExpression (const char *expr,
9665 const char *expr_prefix,
9666 lldb::LanguageType language,
Jim Ingham19a63fc2015-11-03 02:11:24 +00009667 Expression::ResultType desired_type,
9668 const EvaluateExpressionOptions &options)
Jim Ingham151c0322015-09-15 21:13:50 +00009669{
9670 TargetSP target_sp = m_target_wp.lock();
9671 if (!target_sp)
9672 return nullptr;
9673
Jim Ingham19a63fc2015-11-03 02:11:24 +00009674 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00009675}
9676
9677FunctionCaller *
9678ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9679 const Address& function_address,
9680 const ValueList &arg_value_list,
9681 const char *name)
9682{
9683 TargetSP target_sp = m_target_wp.lock();
9684 if (!target_sp)
9685 return nullptr;
9686
9687 Process *process = target_sp->GetProcessSP().get();
9688 if (!process)
9689 return nullptr;
9690
9691 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9692}
9693
9694UtilityFunction *
9695ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9696 const char *name)
9697{
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009698 TargetSP target_sp = m_target_wp.lock();
Jim Ingham151c0322015-09-15 21:13:50 +00009699 if (!target_sp)
9700 return nullptr;
9701
9702 return new ClangUtilityFunction(*target_sp.get(), text, name);
9703}
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009704
9705PersistentExpressionState *
9706ClangASTContextForExpressions::GetPersistentExpressionState ()
9707{
9708 return m_persistent_variables.get();
9709}