blob: 68507af1d63a86b53abae53cb027ad172d56aa8f [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
Zachary Turnerd133f6a2016-03-28 22:53:41 +000066#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
67#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
68#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Greg Clayton514487e2011-02-15 21:59:32 +000069#include "lldb/Core/ArchSpec.h"
Greg Clayton73b472d2010-10-27 03:32:59 +000070#include "lldb/Core/Flags.h"
Sean Callananfb8b7092010-10-28 18:19:36 +000071#include "lldb/Core/Log.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000072#include "lldb/Core/Module.h"
73#include "lldb/Core/PluginManager.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000074#include "lldb/Core/RegularExpression.h"
Ulrich Weigand9521ad22016-04-15 09:55:52 +000075#include "lldb/Core/Scalar.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000076#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000077#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000078#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000079#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000080#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000081#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000082#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000083#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000084#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000085#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000086#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000087#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000089#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000090#include "lldb/Target/Process.h"
91#include "lldb/Target/Target.h"
Sean Callananc530ba92016-05-02 21:15:31 +000092#include "lldb/Utility/LLDBAssert.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000093
Greg Clayton261ac3f2015-08-28 01:01:03 +000094#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +000095#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000096
Eli Friedman932197d2010-06-13 19:06:42 +000097#include <stdio.h>
98
Greg Clayton1341baf2013-07-11 23:36:31 +000099#include <mutex>
100
Greg Claytonc86103d2010-08-05 01:57:25 +0000101using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102using namespace lldb_private;
103using namespace llvm;
104using namespace clang;
105
Greg Clayton56939cb2015-09-17 22:23:34 +0000106namespace
107{
108 static inline bool ClangASTContextSupportsLanguage (lldb::LanguageType language)
109 {
110 return language == eLanguageTypeUnknown || // Clang is the default type system
111 Language::LanguageIsC (language) ||
112 Language::LanguageIsCPlusPlus (language) ||
Ewan Crawford75f0ff52016-02-03 09:17:03 +0000113 Language::LanguageIsObjC (language) ||
Jason Molendad8f24a92016-04-27 04:50:51 +0000114 // Use Clang for Rust until there is a proper language plugin for it
115 language == eLanguageTypeRust ||
Ewan Crawford75f0ff52016-02-03 09:17:03 +0000116 language == eLanguageTypeExtRenderScript;
Greg Clayton56939cb2015-09-17 22:23:34 +0000117 }
118}
119
Enrico Granata2267ad42014-09-16 17:28:40 +0000120typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000121
122static ClangASTMap &
123GetASTMap()
124{
Enrico Granata2267ad42014-09-16 17:28:40 +0000125 static ClangASTMap *g_map_ptr = nullptr;
126 static std::once_flag g_once_flag;
127 std::call_once(g_once_flag, []() {
128 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
129 });
130 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000131}
132
133
Greg Clayton57ee3062013-07-11 22:46:58 +0000134clang::AccessSpecifier
135ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000136{
137 switch (access)
138 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000139 default: break;
140 case eAccessNone: return AS_none;
141 case eAccessPublic: return AS_public;
142 case eAccessPrivate: return AS_private;
143 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000144 }
145 return AS_none;
146}
147
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000149ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150{
151 // FIXME: Cleanup per-file based stuff.
152
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000153 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 // to move these to the language standard, and have the driver resolve the
155 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000156 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000158 } else if (IK == IK_ObjC ||
159 IK == IK_ObjCXX ||
160 IK == IK_PreprocessedObjC ||
161 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 Opts.ObjC1 = Opts.ObjC2 = 1;
163 }
164
165 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
166
167 if (LangStd == LangStandard::lang_unspecified) {
168 // Based on the base language, pick one.
169 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000170 case IK_None:
171 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000172 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000173 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000174 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 LangStd = LangStandard::lang_opencl;
176 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000177 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000178 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000179 LangStd = LangStandard::lang_cuda;
180 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000181 case IK_Asm:
182 case IK_C:
183 case IK_PreprocessedC:
184 case IK_ObjC:
185 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 LangStd = LangStandard::lang_gnu99;
187 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000188 case IK_CXX:
189 case IK_PreprocessedCXX:
190 case IK_ObjCXX:
191 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 LangStd = LangStandard::lang_gnucxx98;
193 break;
194 }
195 }
196
197 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000198 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 Opts.C99 = Std.isC99();
200 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000201 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 Opts.Digraphs = Std.hasDigraphs();
203 Opts.GNUMode = Std.isGNUMode();
204 Opts.GNUInline = !Std.isC99();
205 Opts.HexFloats = Std.hasHexFloats();
206 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000207
208 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209
210 // OpenCL has some additional defaults.
211 if (LangStd == LangStandard::lang_opencl) {
212 Opts.OpenCL = 1;
213 Opts.AltiVec = 1;
214 Opts.CXXOperatorNames = 1;
215 Opts.LaxVectorConversions = 1;
216 }
217
218 // OpenCL and C++ both have bool, true, false keywords.
219 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
220
221// if (Opts.CPlusPlus)
222// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
223//
224// if (Args.hasArg(OPT_fobjc_gc_only))
225// Opts.setGCMode(LangOptions::GCOnly);
226// else if (Args.hasArg(OPT_fobjc_gc))
227// Opts.setGCMode(LangOptions::HybridGC);
228//
229// if (Args.hasArg(OPT_print_ivar_layout))
230// Opts.ObjCGCBitmapPrint = 1;
231//
232// if (Args.hasArg(OPT_faltivec))
233// Opts.AltiVec = 1;
234//
235// if (Args.hasArg(OPT_pthread))
236// Opts.POSIXThreads = 1;
237//
238// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
239// "default");
240// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000241 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242// else if (Vis == "hidden")
243// Opts.setVisibilityMode(LangOptions::Hidden);
244// else if (Vis == "protected")
245// Opts.setVisibilityMode(LangOptions::Protected);
246// else
247// Diags.Report(diag::err_drv_invalid_value)
248// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
249
250// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
251
252 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
253 // is specified, or -std is set to a conforming mode.
254 Opts.Trigraphs = !Opts.GNUMode;
255// if (Args.hasArg(OPT_trigraphs))
256// Opts.Trigraphs = 1;
257//
258// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
259// OPT_fno_dollars_in_identifiers,
260// !Opts.AsmPreprocessor);
261// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
262// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
263// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
264// if (Args.hasArg(OPT_fno_lax_vector_conversions))
265// Opts.LaxVectorConversions = 0;
266// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
267// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
268// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000269 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
271// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
272// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
273// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
274// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
275// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
276// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
277// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
278// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
279// Diags);
280// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
281// Opts.ObjCConstantStringClass = getLastArgValue(Args,
282// OPT_fconstant_string_class);
283// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
284// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
285// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
286// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
287// Opts.Static = Args.hasArg(OPT_static_define);
288 Opts.OptimizeSize = 0;
289
290 // FIXME: Eliminate this dependency.
291// unsigned Opt =
292// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
293// Opts.Optimize = Opt != 0;
294 unsigned Opt = 0;
295
296 // This is the __NO_INLINE__ define, which just depends on things like the
297 // optimization level and -fno-inline, not actually whether the backend has
298 // inlining enabled.
299 //
300 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000301 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302
303// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
304// switch (SSP) {
305// default:
306// Diags.Report(diag::err_drv_invalid_value)
307// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
308// break;
309// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
310// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
311// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
312// }
313}
314
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315
Greg Claytonf73034f2015-09-08 18:15:05 +0000316ClangASTContext::ClangASTContext (const char *target_triple) :
317 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000318 m_target_triple (),
319 m_ast_ap (),
320 m_language_options_ap (),
321 m_source_manager_ap (),
322 m_diagnostics_engine_ap (),
323 m_target_options_rp (),
324 m_target_info_ap (),
325 m_identifier_table_ap (),
326 m_selector_table_ap (),
327 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000328 m_callback_tag_decl (nullptr),
329 m_callback_objc_decl (nullptr),
330 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000331 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000332 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333{
334 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000335 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336}
337
338//----------------------------------------------------------------------
339// Destructor
340//----------------------------------------------------------------------
341ClangASTContext::~ClangASTContext()
342{
Jim Ingham0ea010a2016-02-12 00:03:19 +0000343 Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344}
345
Greg Clayton56939cb2015-09-17 22:23:34 +0000346ConstString
347ClangASTContext::GetPluginNameStatic()
348{
349 return ConstString("clang");
350}
351
352ConstString
353ClangASTContext::GetPluginName()
354{
355 return ClangASTContext::GetPluginNameStatic();
356}
357
358uint32_t
359ClangASTContext::GetPluginVersion()
360{
361 return 1;
362}
363
364lldb::TypeSystemSP
Tamas Berghammer3a6b82b2015-10-09 12:06:10 +0000365ClangASTContext::CreateInstance (lldb::LanguageType language,
366 lldb_private::Module *module,
367 Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000368{
369 if (ClangASTContextSupportsLanguage(language))
370 {
Greg Clayton5beec212015-10-08 21:04:34 +0000371 ArchSpec arch;
372 if (module)
373 arch = module->GetArchitecture();
374 else if (target)
375 arch = target->GetArchitecture();
376
377 if (arch.IsValid())
Greg Clayton56939cb2015-09-17 22:23:34 +0000378 {
Greg Clayton5beec212015-10-08 21:04:34 +0000379 ArchSpec fixed_arch = arch;
380 // LLVM wants this to be set to iOS or MacOSX; if we're working on
381 // a bare-boards type image, change the triple for llvm's benefit.
382 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
383 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
Greg Clayton56939cb2015-09-17 22:23:34 +0000384 {
Greg Clayton5beec212015-10-08 21:04:34 +0000385 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
386 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
387 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb)
Greg Clayton56939cb2015-09-17 22:23:34 +0000388 {
Greg Clayton5beec212015-10-08 21:04:34 +0000389 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
Greg Clayton56939cb2015-09-17 22:23:34 +0000390 }
Greg Clayton5beec212015-10-08 21:04:34 +0000391 else
392 {
393 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
394 }
395 }
396
397 if (module)
398 {
399 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
400 if (ast_sp)
401 {
402 ast_sp->SetArchitecture (fixed_arch);
403 }
404 return ast_sp;
405 }
Sean Callanana3444ff2015-11-10 22:54:42 +0000406 else if (target && target->IsValid())
Greg Clayton5beec212015-10-08 21:04:34 +0000407 {
408 std::shared_ptr<ClangASTContextForExpressions> ast_sp(new ClangASTContextForExpressions(*target));
409 if (ast_sp)
410 {
411 ast_sp->SetArchitecture(fixed_arch);
412 ast_sp->m_scratch_ast_source_ap.reset (new ClangASTSource(target->shared_from_this()));
413 ast_sp->m_scratch_ast_source_ap->InstallASTContext(ast_sp->getASTContext());
414 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(ast_sp->m_scratch_ast_source_ap->CreateProxy());
415 ast_sp->SetExternalSource(proxy_ast_source);
416 return ast_sp;
417 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000418 }
419 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000420 }
421 return lldb::TypeSystemSP();
422}
423
Sean Callananfe38c852015-10-08 23:07:53 +0000424void
425ClangASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
426{
427 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
428 lldb::eLanguageTypeC89,
429 lldb::eLanguageTypeC,
430 lldb::eLanguageTypeC11,
431 lldb::eLanguageTypeC_plus_plus,
432 lldb::eLanguageTypeC99,
433 lldb::eLanguageTypeObjC,
434 lldb::eLanguageTypeObjC_plus_plus,
435 lldb::eLanguageTypeC_plus_plus_03,
436 lldb::eLanguageTypeC_plus_plus_11,
437 lldb::eLanguageTypeC11,
438 lldb::eLanguageTypeC_plus_plus_14});
439
440 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({
441 lldb::eLanguageTypeC_plus_plus,
442 lldb::eLanguageTypeObjC_plus_plus,
443 lldb::eLanguageTypeC_plus_plus_03,
444 lldb::eLanguageTypeC_plus_plus_11,
445 lldb::eLanguageTypeC_plus_plus_14});
446
447 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
448 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
449}
450
Greg Clayton56939cb2015-09-17 22:23:34 +0000451
452void
453ClangASTContext::Initialize()
454{
455 PluginManager::RegisterPlugin (GetPluginNameStatic(),
456 "clang base AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000457 CreateInstance,
458 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000459}
460
461void
462ClangASTContext::Terminate()
463{
464 PluginManager::UnregisterPlugin (CreateInstance);
465}
466
Jim Ingham0ea010a2016-02-12 00:03:19 +0000467void
468ClangASTContext::Finalize()
469{
470 if (m_ast_ap.get())
471 {
472 GetASTMap().Erase(m_ast_ap.get());
473 if (!m_ast_owned)
474 m_ast_ap.release();
475 }
476
477 m_builtins_ap.reset();
478 m_selector_table_ap.reset();
479 m_identifier_table_ap.reset();
480 m_target_info_ap.reset();
481 m_target_options_rp.reset();
482 m_diagnostics_engine_ap.reset();
483 m_source_manager_ap.reset();
484 m_language_options_ap.reset();
485 m_ast_ap.reset();
486 m_scratch_ast_source_ap.reset();
487}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
489void
490ClangASTContext::Clear()
491{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000492 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 m_language_options_ap.reset();
494 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000495 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000496 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497 m_target_info_ap.reset();
498 m_identifier_table_ap.reset();
499 m_selector_table_ap.reset();
500 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000501 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000502}
503
504const char *
505ClangASTContext::GetTargetTriple ()
506{
507 return m_target_triple.c_str();
508}
509
510void
511ClangASTContext::SetTargetTriple (const char *target_triple)
512{
513 Clear();
514 m_target_triple.assign(target_triple);
515}
516
Greg Clayton514487e2011-02-15 21:59:32 +0000517void
518ClangASTContext::SetArchitecture (const ArchSpec &arch)
519{
Greg Clayton880cbb02011-07-30 01:26:02 +0000520 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000521}
522
Greg Clayton6beaaa62011-01-17 03:46:26 +0000523bool
524ClangASTContext::HasExternalSource ()
525{
526 ASTContext *ast = getASTContext();
527 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000528 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000529 return false;
530}
531
532void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000533ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000534{
535 ASTContext *ast = getASTContext();
536 if (ast)
537 {
538 ast->setExternalSource (ast_source_ap);
539 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
540 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
541 }
542}
543
544void
545ClangASTContext::RemoveExternalSource ()
546{
547 ASTContext *ast = getASTContext();
548
549 if (ast)
550 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000551 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000552 ast->setExternalSource (empty_ast_source_ap);
553 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
554 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
555 }
556}
557
Greg Claytond8d4a572015-08-11 21:38:15 +0000558void
559ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
560{
561 if (!m_ast_owned) {
562 m_ast_ap.release();
563 }
564 m_ast_owned = false;
565 m_ast_ap.reset(ast_ctx);
566 GetASTMap().Insert(ast_ctx, this);
567}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568
569ASTContext *
570ClangASTContext::getASTContext()
571{
Ed Masted4612ad2014-04-20 13:17:36 +0000572 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000574 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000575 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
576 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000577 *getIdentifierTable(),
578 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000579 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000580
581 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000582
583 // This can be NULL if we don't know anything about the architecture or if the
584 // target for an architecture isn't enabled in the llvm/clang that we built
585 TargetInfo *target_info = getTargetInfo();
586 if (target_info)
587 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000588
Greg Clayton6beaaa62011-01-17 03:46:26 +0000589 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
590 {
591 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
592 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
593 }
594
Enrico Granata2267ad42014-09-16 17:28:40 +0000595 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000596
597 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
598 ClangASTContext::CompleteObjCInterfaceDecl,
599 nullptr,
600 ClangASTContext::LayoutRecordType,
601 this));
602 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000604 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}
606
Enrico Granata5d84a692014-08-19 21:46:37 +0000607ClangASTContext*
608ClangASTContext::GetASTContext (clang::ASTContext* ast)
609{
Enrico Granata2267ad42014-09-16 17:28:40 +0000610 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000611 return clang_ast;
612}
613
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614Builtin::Context *
615ClangASTContext::getBuiltinContext()
616{
Ed Masted4612ad2014-04-20 13:17:36 +0000617 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000618 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 return m_builtins_ap.get();
620}
621
622IdentifierTable *
623ClangASTContext::getIdentifierTable()
624{
Ed Masted4612ad2014-04-20 13:17:36 +0000625 if (m_identifier_table_ap.get() == nullptr)
626 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 return m_identifier_table_ap.get();
628}
629
630LangOptions *
631ClangASTContext::getLanguageOptions()
632{
Ed Masted4612ad2014-04-20 13:17:36 +0000633 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 {
635 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000636 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000637// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 }
639 return m_language_options_ap.get();
640}
641
642SelectorTable *
643ClangASTContext::getSelectorTable()
644{
Ed Masted4612ad2014-04-20 13:17:36 +0000645 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 m_selector_table_ap.reset (new SelectorTable());
647 return m_selector_table_ap.get();
648}
649
Sean Callanan79439e82010-11-18 02:56:27 +0000650clang::FileManager *
651ClangASTContext::getFileManager()
652{
Ed Masted4612ad2014-04-20 13:17:36 +0000653 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000654 {
655 clang::FileSystemOptions file_system_options;
656 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
657 }
Sean Callanan79439e82010-11-18 02:56:27 +0000658 return m_file_manager_ap.get();
659}
660
Greg Claytone1a916a2010-07-21 22:12:05 +0000661clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662ClangASTContext::getSourceManager()
663{
Ed Masted4612ad2014-04-20 13:17:36 +0000664 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000665 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 return m_source_manager_ap.get();
667}
668
Sean Callanan880e6802011-10-07 23:18:13 +0000669clang::DiagnosticsEngine *
670ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000671{
Ed Masted4612ad2014-04-20 13:17:36 +0000672 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000673 {
674 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000675 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000676 }
Sean Callanan880e6802011-10-07 23:18:13 +0000677 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678}
679
Greg Claytonfe689042015-11-10 17:47:04 +0000680clang::MangleContext *
681ClangASTContext::getMangleContext()
682{
683 if (m_mangle_ctx_ap.get() == nullptr)
684 m_mangle_ctx_ap.reset (getASTContext()->createMangleContext());
685 return m_mangle_ctx_ap.get();
686}
687
Sean Callanan880e6802011-10-07 23:18:13 +0000688class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000689{
690public:
Sean Callanan880e6802011-10-07 23:18:13 +0000691 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000692 {
693 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
694 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000695
696 void
697 HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000698 {
699 if (m_log)
700 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000701 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000702 info.FormatDiagnostic(diag_str);
703 diag_str.push_back('\0');
704 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
705 }
706 }
Sean Callanan880e6802011-10-07 23:18:13 +0000707
708 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
709 {
710 return new NullDiagnosticConsumer ();
711 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000712private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000713 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000714};
715
Sean Callanan880e6802011-10-07 23:18:13 +0000716DiagnosticConsumer *
717ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000718{
Ed Masted4612ad2014-04-20 13:17:36 +0000719 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000720 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000721
Sean Callanan880e6802011-10-07 23:18:13 +0000722 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000723}
724
Saleem Abdulrasool8bd973c2016-04-07 06:51:10 +0000725std::shared_ptr<clang::TargetOptions> &
Jason Molenda45938b92014-07-08 23:46:39 +0000726ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000727 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 {
Saleem Abdulrasool8bd973c2016-04-07 06:51:10 +0000729 m_target_options_rp = std::make_shared<clang::TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000730 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000731 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
Alp Toker5f838642014-07-06 05:36:57 +0000733 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734}
735
736
737TargetInfo *
738ClangASTContext::getTargetInfo()
739{
Greg Clayton70512312012-05-08 01:45:38 +0000740 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000741 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000742 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 return m_target_info_ap.get();
744}
745
746#pragma mark Basic Types
747
748static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000749QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000751 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 if (qual_type_bit_size == bit_size)
753 return true;
754 return false;
755}
Greg Clayton56939cb2015-09-17 22:23:34 +0000756
Greg Claytona1e5dc82015-08-11 22:53:00 +0000757CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +0000758ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, size_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759{
Greg Clayton57ee3062013-07-11 22:46:58 +0000760 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761}
762
Greg Claytona1e5dc82015-08-11 22:53:00 +0000763CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000764ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000766 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000767 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 switch (encoding)
769 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000770 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000771 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000772 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 break;
774
Greg Claytonc86103d2010-08-05 01:57:25 +0000775 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000776 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000777 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000778 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000779 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000780 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000781 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000782 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000783 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000784 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000785 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000786 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000787 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 break;
789
Greg Claytonc86103d2010-08-05 01:57:25 +0000790 case eEncodingSint:
Ulrich Weigand377e4212016-04-14 14:30:12 +0000791 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
792 return CompilerType (ast, ast->SignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000793 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000794 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000795 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000796 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000797 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000798 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000799 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000800 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000801 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000802 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 break;
804
Greg Claytonc86103d2010-08-05 01:57:25 +0000805 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000806 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000807 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000808 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000809 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000810 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000811 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000812 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
813 return CompilerType (ast, ast->HalfTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 break;
815
Greg Claytonc86103d2010-08-05 01:57:25 +0000816 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000817 // Sanity check that bit_size is a multiple of 8's.
818 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000819 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000820 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 }
822
Greg Claytona1e5dc82015-08-11 22:53:00 +0000823 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824}
825
Greg Clayton57ee3062013-07-11 22:46:58 +0000826
827
828lldb::BasicType
829ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
830{
831 if (name)
832 {
833 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
834 static TypeNameToBasicTypeMap g_type_map;
835 static std::once_flag g_once_flag;
836 std::call_once(g_once_flag, [](){
837 // "void"
838 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
839
840 // "char"
841 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
842 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
843 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
844 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
845 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
846 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
847 // "short"
848 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
849 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
850 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
851 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
852
853 // "int"
854 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
855 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
856 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
857 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
858
859 // "long"
860 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
861 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
862 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
863 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
864
865 // "long long"
866 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
867 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
868 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
869 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
870
871 // "int128"
872 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
873 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
874
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000875 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000876 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
877 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
878 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
879 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
880 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
881 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
882 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
883 g_type_map.Sort();
884 });
885
886 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
887 }
888 return eBasicTypeInvalid;
889}
890
Greg Claytona1e5dc82015-08-11 22:53:00 +0000891CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000892ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
893{
894 if (ast)
895 {
896 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
897 return ClangASTContext::GetBasicType (ast, basic_type);
898 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000899 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000900}
901
902uint32_t
903ClangASTContext::GetPointerByteSize ()
904{
905 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000906 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000907 return m_pointer_byte_size;
908}
909
Greg Claytona1e5dc82015-08-11 22:53:00 +0000910CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000911ClangASTContext::GetBasicType (lldb::BasicType basic_type)
912{
913 return GetBasicType (getASTContext(), basic_type);
914}
915
Greg Claytona1e5dc82015-08-11 22:53:00 +0000916CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000917ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
918{
Zachary Turner9d8a97e2016-04-01 23:20:35 +0000919 if (!ast)
920 return CompilerType();
921 lldb::opaque_compiler_type_t clang_type = GetOpaqueCompilerType(ast, basic_type);
922
923 if (clang_type)
924 return CompilerType(GetASTContext(ast), clang_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +0000925 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000926}
927
928
Greg Claytona1e5dc82015-08-11 22:53:00 +0000929CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
931{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000932 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000933
934#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000935 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000936 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 {
938 switch (dw_ate)
939 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000940 default:
941 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000942
Sean Callanan38d4df52012-04-03 01:10:10 +0000943 case DW_ATE_address:
944 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000945 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000946 break;
947
948 case DW_ATE_boolean:
949 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000950 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000951 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000952 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000953 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000954 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000955 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000956 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000957 break;
958
959 case DW_ATE_lo_user:
960 // This has been seen to mean DW_AT_complex_integer
961 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000962 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000963 if (::strstr(type_name, "complex"))
964 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000965 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +0000966 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000967 }
Greg Clayton605684e2011-10-28 23:06:08 +0000968 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000969 break;
970
971 case DW_ATE_complex_float:
972 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000973 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000974 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000975 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000976 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000977 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 else
Greg Clayton605684e2011-10-28 23:06:08 +0000979 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000980 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +0000981 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +0000982 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000983 break;
984
985 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +0000986 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000987 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000988 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000989 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000990 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000991 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000992 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +0000993 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000994 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000995 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000996 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000997 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000998 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000999 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
1000 return CompilerType (ast, ast->HalfTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001001 break;
1002
1003 case DW_ATE_signed:
1004 if (type_name)
1005 {
1006 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001007 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001008 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001009 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001010 if (streq(type_name, "void") &&
1011 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001012 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001013 if (strstr(type_name, "long long") &&
1014 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001015 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001016 if (strstr(type_name, "long") &&
1017 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001018 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001019 if (strstr(type_name, "short") &&
1020 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001021 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001022 if (strstr(type_name, "char"))
1023 {
1024 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001025 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001026 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001027 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001028 }
1029 if (strstr(type_name, "int"))
1030 {
1031 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001032 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001033 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001034 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001035 }
1036 }
1037 // We weren't able to match up a type name, just search by size
1038 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001039 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001040 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001041 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001042 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001043 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001044 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001045 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001046 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001047 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001048 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001049 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001050 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001051
Sean Callanan38d4df52012-04-03 01:10:10 +00001052 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001053 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +00001054 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001055 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001056 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001057 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001058 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001059 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001060 break;
1061
1062 case DW_ATE_unsigned:
1063 if (type_name)
1064 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001065 if (streq(type_name, "wchar_t"))
1066 {
1067 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1068 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001069 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001070 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001071 }
1072 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001073 if (strstr(type_name, "long long"))
1074 {
1075 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001076 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001077 }
1078 else if (strstr(type_name, "long"))
1079 {
1080 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001081 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001082 }
1083 else if (strstr(type_name, "short"))
1084 {
1085 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001086 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001087 }
1088 else if (strstr(type_name, "char"))
1089 {
1090 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001091 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001092 }
1093 else if (strstr(type_name, "int"))
1094 {
1095 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001096 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001097 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001098 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001099 }
1100 }
1101 // We weren't able to match up a type name, just search by size
1102 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001103 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001104 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001105 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001106 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001107 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001108 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001109 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001110 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001111 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001112 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001113 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001114 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001115
Sean Callanan38d4df52012-04-03 01:10:10 +00001116 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001117 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1118 {
1119 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001120 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001121 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001122 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001123 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001124 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001125 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001126 break;
1127
1128 case DW_ATE_imaginary_float:
1129 break;
1130
1131 case DW_ATE_UTF:
1132 if (type_name)
1133 {
1134 if (streq(type_name, "char16_t"))
1135 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001136 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001137 }
1138 else if (streq(type_name, "char32_t"))
1139 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001140 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001141 }
1142 }
1143 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 }
1145 }
1146 // This assert should fire for anything that we don't catch above so we know
1147 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001148 if (type_name)
1149 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001150 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 +00001151 }
1152 else
1153 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001154 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 +00001155 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001156 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157}
1158
Greg Claytona1e5dc82015-08-11 22:53:00 +00001159CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001160ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1161{
Greg Clayton57ee3062013-07-11 22:46:58 +00001162 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001163 return CompilerType (ast, ast->UnknownAnyTy);
1164 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001165}
1166
Greg Claytona1e5dc82015-08-11 22:53:00 +00001167CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168ClangASTContext::GetCStringType (bool is_const)
1169{
Greg Clayton57ee3062013-07-11 22:46:58 +00001170 ASTContext *ast = getASTContext();
1171 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172
1173 if (is_const)
1174 char_type.addConst();
1175
Greg Claytona1e5dc82015-08-11 22:53:00 +00001176 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177}
1178
Sean Callanan09ab4b72011-11-30 22:11:59 +00001179clang::DeclContext *
1180ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1181{
1182 return ast->getTranslationUnitDecl();
1183}
1184
Greg Clayton526e5af2010-11-13 03:52:47 +00001185clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001186ClangASTContext::CopyDecl (ASTContext *dst_ast,
1187 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001188 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001189{
Sean Callanan79439e82010-11-18 02:56:27 +00001190 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001191 FileManager file_manager (file_system_options);
1192 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001193 *src_ast, file_manager,
1194 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001195
1196 return importer.Import(source_decl);
1197}
1198
Sean Callanan23a30272010-07-16 00:00:27 +00001199bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001200ClangASTContext::AreTypesSame (CompilerType type1,
1201 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001202 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001203{
Greg Claytonf73034f2015-09-08 18:15:05 +00001204 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1205 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001206 return false;
1207
1208 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001209 return true;
1210
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001211 QualType type1_qual = ClangUtil::GetQualType(type1);
1212 QualType type2_qual = ClangUtil::GetQualType(type2);
1213
Sean Callanan5056ab02012-02-18 02:01:03 +00001214 if (ignore_qualifiers)
1215 {
1216 type1_qual = type1_qual.getUnqualifiedType();
1217 type2_qual = type2_qual.getUnqualifiedType();
1218 }
1219
Greg Claytonf73034f2015-09-08 18:15:05 +00001220 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221}
1222
Greg Claytona1e5dc82015-08-11 22:53:00 +00001223CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001224ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1225{
1226 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1227 return GetTypeForDecl(interface_decl);
1228 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1229 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001230 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001231}
1232
Greg Clayton6beaaa62011-01-17 03:46:26 +00001233
Greg Claytona1e5dc82015-08-11 22:53:00 +00001234CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001235ClangASTContext::GetTypeForDecl (TagDecl *decl)
1236{
1237 // No need to call the getASTContext() accessor (which can create the AST
1238 // if it isn't created yet, because we can't have created a decl in this
1239 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001240 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001241 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001242 return CompilerType (ast, ast->getTagDeclType(decl));
1243 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001244}
1245
Greg Claytona1e5dc82015-08-11 22:53:00 +00001246CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001247ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1248{
1249 // No need to call the getASTContext() accessor (which can create the AST
1250 // if it isn't created yet, because we can't have created a decl in this
1251 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001252 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001253 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001254 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1255 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001256}
1257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258#pragma mark Structure, Unions, Classes
1259
Greg Claytona1e5dc82015-08-11 22:53:00 +00001260CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001261ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1262 AccessType access_type,
1263 const char *name,
1264 int kind,
1265 LanguageType language,
1266 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001268 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001269 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001270
Ed Masted4612ad2014-04-20 13:17:36 +00001271 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001272 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273
Greg Clayton9e409562010-07-28 02:04:09 +00001274
Greg Claytone1be9962011-08-24 23:50:00 +00001275 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001276 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001277 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001278 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001279 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001280 }
1281
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1283 // we will need to update this code. I was told to currently always use
1284 // the CXXRecordDecl class since we often don't know from debug information
1285 // if something is struct or a class, so we default to always use the more
1286 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001287
1288 bool is_anonymous = (!name) || (!name[0]);
1289
Greg Claytonf0705c82011-10-22 03:33:13 +00001290 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1291 (TagDecl::TagKind)kind,
1292 decl_ctx,
1293 SourceLocation(),
1294 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001295 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001296
1297 if (is_anonymous)
1298 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001299
Greg Claytonc4ffd662013-03-08 01:37:30 +00001300 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001301 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001302 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001303 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001304
Greg Clayton55561e92011-10-26 03:31:36 +00001305 if (access_type != eAccessNone)
1306 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001307
1308 if (decl_ctx)
1309 decl_ctx->addDecl (decl);
1310
Greg Claytona1e5dc82015-08-11 22:53:00 +00001311 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001312 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001313 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001314}
1315
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001316static TemplateParameterList *
1317CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001318 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001319 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1320{
1321 const bool parameter_pack = false;
1322 const bool is_typename = false;
1323 const unsigned depth = 0;
1324 const size_t num_template_params = template_param_infos.GetSize();
1325 for (size_t i=0; i<num_template_params; ++i)
1326 {
1327 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001328
Ed Masted4612ad2014-04-20 13:17:36 +00001329 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001330 if (name && name[0])
1331 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001332 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001333 {
1334 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1335 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1336 SourceLocation(),
1337 SourceLocation(),
1338 depth,
1339 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001340 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001341 template_param_infos.args[i].getIntegralType(),
1342 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001343 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001344
1345 }
1346 else
1347 {
1348 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1349 ast->getTranslationUnitDecl(), // Is this the right decl context?
1350 SourceLocation(),
1351 SourceLocation(),
1352 depth,
1353 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001354 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001355 is_typename,
1356 parameter_pack));
1357 }
1358 }
1359
1360 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1361 SourceLocation(),
1362 SourceLocation(),
David Majnemer48a065d2015-12-27 07:16:55 +00001363 template_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001364 SourceLocation());
1365 return template_param_list;
1366}
1367
1368clang::FunctionTemplateDecl *
1369ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1370 clang::FunctionDecl *func_decl,
1371 const char *name,
1372 const TemplateParameterInfos &template_param_infos)
1373{
1374// /// \brief Create a function template node.
1375 ASTContext *ast = getASTContext();
1376
1377 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1378
1379 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1380 template_param_infos,
1381 template_param_decls);
1382 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1383 decl_ctx,
1384 func_decl->getLocation(),
1385 func_decl->getDeclName(),
1386 template_param_list,
1387 func_decl);
1388
1389 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1390 i < template_param_decl_count;
1391 ++i)
1392 {
1393 // TODO: verify which decl context we should put template_param_decls into..
1394 template_param_decls[i]->setDeclContext (func_decl);
1395 }
1396
1397 return func_tmpl_decl;
1398}
1399
1400void
1401ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1402 clang::FunctionTemplateDecl *func_tmpl_decl,
1403 const TemplateParameterInfos &infos)
1404{
1405 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1406 infos.args.data(),
1407 infos.args.size());
1408
1409 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1410 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001411 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001412}
1413
1414
Greg Claytonf0705c82011-10-22 03:33:13 +00001415ClassTemplateDecl *
1416ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001417 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001418 const char *class_name,
1419 int kind,
1420 const TemplateParameterInfos &template_param_infos)
1421{
1422 ASTContext *ast = getASTContext();
1423
Ed Masted4612ad2014-04-20 13:17:36 +00001424 ClassTemplateDecl *class_template_decl = nullptr;
1425 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001426 decl_ctx = ast->getTranslationUnitDecl();
1427
1428 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1429 DeclarationName decl_name (&identifier_info);
1430
1431 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001432
1433 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001434 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001435 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001436 if (class_template_decl)
1437 return class_template_decl;
1438 }
1439
1440 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001441
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001442 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1443 template_param_infos,
1444 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001445
1446 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1447 (TagDecl::TagKind)kind,
1448 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1449 SourceLocation(),
1450 SourceLocation(),
1451 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001452
1453 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1454 i < template_param_decl_count;
1455 ++i)
1456 {
1457 template_param_decls[i]->setDeclContext (template_cxx_decl);
1458 }
1459
Sean Callananb5c79622011-11-19 01:35:08 +00001460 // With templated classes, we say that a class is templated with
1461 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001462 //template_cxx_decl->startDefinition();
1463 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001464
Greg Claytonf0705c82011-10-22 03:33:13 +00001465 class_template_decl = ClassTemplateDecl::Create (*ast,
1466 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1467 SourceLocation(),
1468 decl_name,
1469 template_param_list,
1470 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001471 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001472
1473 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001474 {
Greg Clayton55561e92011-10-26 03:31:36 +00001475 if (access_type != eAccessNone)
1476 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001477
1478 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1479 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1480
Greg Claytonf0705c82011-10-22 03:33:13 +00001481 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001482
1483#ifdef LLDB_CONFIGURATION_DEBUG
1484 VerifyDecl(class_template_decl);
1485#endif
1486 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001487
1488 return class_template_decl;
1489}
1490
1491
1492ClassTemplateSpecializationDecl *
1493ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1494 ClassTemplateDecl *class_template_decl,
1495 int kind,
1496 const TemplateParameterInfos &template_param_infos)
1497{
1498 ASTContext *ast = getASTContext();
1499 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1500 (TagDecl::TagKind)kind,
1501 decl_ctx,
1502 SourceLocation(),
1503 SourceLocation(),
1504 class_template_decl,
1505 &template_param_infos.args.front(),
1506 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001507 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001508
Sean Callananfa4fab72013-02-01 06:55:48 +00001509 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1510
Greg Claytonf0705c82011-10-22 03:33:13 +00001511 return class_template_specialization_decl;
1512}
1513
Greg Claytona1e5dc82015-08-11 22:53:00 +00001514CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001515ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1516{
1517 if (class_template_specialization_decl)
1518 {
1519 ASTContext *ast = getASTContext();
1520 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001521 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001522 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001523 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524}
1525
Greg Clayton090d0982011-06-19 03:43:27 +00001526static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001527check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001528{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001529 // Special-case call since it can take any number of operands
1530 if(op_kind == OO_Call)
1531 return true;
1532
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001533 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001534 if (num_params == 0)
1535 return unary;
1536 if (num_params == 1)
1537 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001538 else
Greg Clayton090d0982011-06-19 03:43:27 +00001539 return false;
1540}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001541
Greg Clayton090d0982011-06-19 03:43:27 +00001542bool
1543ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1544{
Sean Callanan5b26f272012-02-04 08:49:35 +00001545 switch (op_kind)
1546 {
1547 default:
1548 break;
1549 // C++ standard allows any number of arguments to new/delete
1550 case OO_New:
1551 case OO_Array_New:
1552 case OO_Delete:
1553 case OO_Array_Delete:
1554 return true;
1555 }
1556
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001557#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 +00001558 switch (op_kind)
1559 {
1560#include "clang/Basic/OperatorKinds.def"
1561 default: break;
1562 }
1563 return false;
1564}
1565
Greg Clayton57ee3062013-07-11 22:46:58 +00001566clang::AccessSpecifier
1567ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001568{
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001569 // Make the access equal to the stricter of the field and the nested field's access
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001570 if (lhs == AS_none || rhs == AS_none)
1571 return AS_none;
1572 if (lhs == AS_private || rhs == AS_private)
1573 return AS_private;
1574 if (lhs == AS_protected || rhs == AS_protected)
1575 return AS_protected;
1576 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001577}
1578
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579bool
1580ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1581{
1582 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1583}
1584
1585bool
1586ClangASTContext::FieldIsBitfield
1587(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001588 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 FieldDecl* field,
1590 uint32_t& bitfield_bit_size
1591)
1592{
Ed Masted4612ad2014-04-20 13:17:36 +00001593 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594 return false;
1595
1596 if (field->isBitField())
1597 {
1598 Expr* bit_width_expr = field->getBitWidth();
1599 if (bit_width_expr)
1600 {
1601 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001602 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001603 {
1604 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1605 return true;
1606 }
1607 }
1608 }
1609 return false;
1610}
1611
1612bool
1613ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1614{
Ed Masted4612ad2014-04-20 13:17:36 +00001615 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616 return false;
1617
1618 if (!record_decl->field_empty())
1619 return true;
1620
1621 // No fields, lets check this is a CXX record and check the base classes
1622 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1623 if (cxx_record_decl)
1624 {
1625 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1626 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1627 base_class != base_class_end;
1628 ++base_class)
1629 {
1630 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1631 if (RecordHasFields(base_class_decl))
1632 return true;
1633 }
1634 }
1635 return false;
1636}
1637
Greg Clayton8cf05932010-07-22 18:30:50 +00001638#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639
Greg Claytona1e5dc82015-08-11 22:53:00 +00001640CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001641ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001642(
1643 const char *name,
1644 DeclContext *decl_ctx,
1645 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001646 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001647 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001648)
1649{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001650 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001651 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001652 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001653 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001654 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001655
Greg Clayton6beaaa62011-01-17 03:46:26 +00001656 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001657 decl_ctx,
1658 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001659 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001660 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001661 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001662 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001663 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001664 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001665
Jim Ingham379397632012-10-27 02:54:13 +00001666 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001667 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001668
Greg Claytona1e5dc82015-08-11 22:53:00 +00001669 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670}
1671
1672static inline bool
1673BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1674{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001675 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001676}
1677
Greg Clayton57ee3062013-07-11 22:46:58 +00001678uint32_t
1679ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680{
1681 uint32_t num_bases = 0;
1682 if (cxx_record_decl)
1683 {
1684 if (omit_empty_base_classes)
1685 {
1686 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1687 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1688 base_class != base_class_end;
1689 ++base_class)
1690 {
1691 // Skip empty base classes
1692 if (omit_empty_base_classes)
1693 {
1694 if (BaseSpecifierIsEmpty (base_class))
1695 continue;
1696 }
1697 ++num_bases;
1698 }
1699 }
1700 else
1701 num_bases = cxx_record_decl->getNumBases();
1702 }
1703 return num_bases;
1704}
1705
1706
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707#pragma mark Namespace Declarations
1708
1709NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001710ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001711{
Ed Masted4612ad2014-04-20 13:17:36 +00001712 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001713 ASTContext *ast = getASTContext();
1714 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001715 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001716 decl_ctx = translation_unit_decl;
1717
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 if (name)
1719 {
Greg Clayton030a2042011-10-14 21:34:45 +00001720 IdentifierInfo &identifier_info = ast->Idents.get(name);
1721 DeclarationName decl_name (&identifier_info);
1722 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001723 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001724 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001725 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001726 if (namespace_decl)
1727 return namespace_decl;
1728 }
1729
Sean Callanan5b26f272012-02-04 08:49:35 +00001730 namespace_decl = NamespaceDecl::Create(*ast,
1731 decl_ctx,
1732 false,
1733 SourceLocation(),
1734 SourceLocation(),
1735 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001736 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001737
Greg Clayton9d3d6882011-10-31 23:51:19 +00001738 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001740 else
1741 {
1742 if (decl_ctx == translation_unit_decl)
1743 {
1744 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1745 if (namespace_decl)
1746 return namespace_decl;
1747
Sean Callanan5b26f272012-02-04 08:49:35 +00001748 namespace_decl = NamespaceDecl::Create(*ast,
1749 decl_ctx,
1750 false,
1751 SourceLocation(),
1752 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001753 nullptr,
1754 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001755 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1756 translation_unit_decl->addDecl (namespace_decl);
1757 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1758 }
1759 else
1760 {
1761 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1762 if (parent_namespace_decl)
1763 {
1764 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1765 if (namespace_decl)
1766 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001767 namespace_decl = NamespaceDecl::Create(*ast,
1768 decl_ctx,
1769 false,
1770 SourceLocation(),
1771 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001772 nullptr,
1773 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001774 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1775 parent_namespace_decl->addDecl (namespace_decl);
1776 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1777 }
1778 else
1779 {
1780 // BAD!!!
1781 }
1782 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001783 }
1784#ifdef LLDB_CONFIGURATION_DEBUG
1785 VerifyDecl(namespace_decl);
1786#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001787 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788}
1789
Siva Chandra03ff5c82016-02-05 19:10:04 +00001790NamespaceDecl *
1791ClangASTContext::GetUniqueNamespaceDeclaration (clang::ASTContext *ast,
1792 const char *name,
1793 clang::DeclContext *decl_ctx)
1794{
1795 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1796 if (ast_ctx == nullptr)
1797 return nullptr;
1798
1799 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1800}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001801
Paul Hermand628cbb2015-09-15 23:44:17 +00001802clang::BlockDecl *
1803ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1804{
1805 if (ctx != nullptr)
1806 {
1807 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1808 ctx->addDecl(decl);
1809 return decl;
1810 }
1811 return nullptr;
1812}
1813
Paul Hermanea188fc2015-09-16 18:48:30 +00001814clang::DeclContext *
1815FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1816{
1817 if (root == nullptr)
1818 return nullptr;
1819
1820 std::set<clang::DeclContext *> path_left;
1821 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1822 path_left.insert(d);
1823
1824 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1825 if (path_left.find(d) != path_left.end())
1826 return d;
1827
1828 return nullptr;
1829}
1830
Paul Hermand628cbb2015-09-15 23:44:17 +00001831clang::UsingDirectiveDecl *
1832ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1833{
1834 if (decl_ctx != nullptr && ns_decl != nullptr)
1835 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001836 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001837 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1838 decl_ctx,
1839 clang::SourceLocation(),
1840 clang::SourceLocation(),
1841 clang::NestedNameSpecifierLoc(),
1842 clang::SourceLocation(),
1843 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001844 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001845 decl_ctx->addDecl(using_decl);
1846 return using_decl;
1847 }
1848 return nullptr;
1849}
1850
1851clang::UsingDecl *
1852ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1853{
1854 if (current_decl_ctx != nullptr && target != nullptr)
1855 {
1856 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1857 current_decl_ctx,
1858 clang::SourceLocation(),
1859 clang::NestedNameSpecifierLoc(),
1860 clang::DeclarationNameInfo(),
1861 false);
1862 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1863 current_decl_ctx,
1864 clang::SourceLocation(),
1865 using_decl,
1866 target);
1867 using_decl->addShadowDecl(shadow_decl);
1868 current_decl_ctx->addDecl(using_decl);
1869 return using_decl;
1870 }
1871 return nullptr;
1872}
1873
1874clang::VarDecl *
1875ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1876{
1877 if (decl_context != nullptr)
1878 {
1879 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1880 decl_context,
1881 clang::SourceLocation(),
1882 clang::SourceLocation(),
1883 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1884 type,
1885 nullptr,
1886 clang::SC_None);
1887 var_decl->setAccess(clang::AS_public);
1888 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001889 return var_decl;
1890 }
1891 return nullptr;
1892}
1893
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001894lldb::opaque_compiler_type_t
1895ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type)
1896{
1897 switch (basic_type)
1898 {
1899 case eBasicTypeVoid:
1900 return ast->VoidTy.getAsOpaquePtr();
1901 case eBasicTypeChar:
1902 return ast->CharTy.getAsOpaquePtr();
1903 case eBasicTypeSignedChar:
1904 return ast->SignedCharTy.getAsOpaquePtr();
1905 case eBasicTypeUnsignedChar:
1906 return ast->UnsignedCharTy.getAsOpaquePtr();
1907 case eBasicTypeWChar:
1908 return ast->getWCharType().getAsOpaquePtr();
1909 case eBasicTypeSignedWChar:
1910 return ast->getSignedWCharType().getAsOpaquePtr();
1911 case eBasicTypeUnsignedWChar:
1912 return ast->getUnsignedWCharType().getAsOpaquePtr();
1913 case eBasicTypeChar16:
1914 return ast->Char16Ty.getAsOpaquePtr();
1915 case eBasicTypeChar32:
1916 return ast->Char32Ty.getAsOpaquePtr();
1917 case eBasicTypeShort:
1918 return ast->ShortTy.getAsOpaquePtr();
1919 case eBasicTypeUnsignedShort:
1920 return ast->UnsignedShortTy.getAsOpaquePtr();
1921 case eBasicTypeInt:
1922 return ast->IntTy.getAsOpaquePtr();
1923 case eBasicTypeUnsignedInt:
1924 return ast->UnsignedIntTy.getAsOpaquePtr();
1925 case eBasicTypeLong:
1926 return ast->LongTy.getAsOpaquePtr();
1927 case eBasicTypeUnsignedLong:
1928 return ast->UnsignedLongTy.getAsOpaquePtr();
1929 case eBasicTypeLongLong:
1930 return ast->LongLongTy.getAsOpaquePtr();
1931 case eBasicTypeUnsignedLongLong:
1932 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1933 case eBasicTypeInt128:
1934 return ast->Int128Ty.getAsOpaquePtr();
1935 case eBasicTypeUnsignedInt128:
1936 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1937 case eBasicTypeBool:
1938 return ast->BoolTy.getAsOpaquePtr();
1939 case eBasicTypeHalf:
1940 return ast->HalfTy.getAsOpaquePtr();
1941 case eBasicTypeFloat:
1942 return ast->FloatTy.getAsOpaquePtr();
1943 case eBasicTypeDouble:
1944 return ast->DoubleTy.getAsOpaquePtr();
1945 case eBasicTypeLongDouble:
1946 return ast->LongDoubleTy.getAsOpaquePtr();
1947 case eBasicTypeFloatComplex:
1948 return ast->FloatComplexTy.getAsOpaquePtr();
1949 case eBasicTypeDoubleComplex:
1950 return ast->DoubleComplexTy.getAsOpaquePtr();
1951 case eBasicTypeLongDoubleComplex:
1952 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1953 case eBasicTypeObjCID:
1954 return ast->getObjCIdType().getAsOpaquePtr();
1955 case eBasicTypeObjCClass:
1956 return ast->getObjCClassType().getAsOpaquePtr();
1957 case eBasicTypeObjCSel:
1958 return ast->getObjCSelType().getAsOpaquePtr();
1959 case eBasicTypeNullPtr:
1960 return ast->NullPtrTy.getAsOpaquePtr();
1961 default:
1962 return nullptr;
1963 }
1964}
1965
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001966#pragma mark Function Types
1967
1968FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001969ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1970 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001971 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001972 int storage,
1973 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974{
Ed Masted4612ad2014-04-20 13:17:36 +00001975 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001976 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001977 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001978 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979
Greg Clayton0d551042013-06-28 21:08:47 +00001980
1981 const bool hasWrittenPrototype = true;
1982 const bool isConstexprSpecified = false;
1983
Greg Clayton147e1fa2011-10-14 22:47:18 +00001984 if (name && name[0])
1985 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001986 func_decl = FunctionDecl::Create(
1987 *ast, decl_ctx, SourceLocation(), SourceLocation(), DeclarationName(&ast->Idents.get(name)),
1988 ClangUtil::GetQualType(function_clang_type), nullptr, (clang::StorageClass)storage, is_inline,
1989 hasWrittenPrototype, isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001991 else
1992 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001993 func_decl =
1994 FunctionDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), DeclarationName(),
1995 ClangUtil::GetQualType(function_clang_type), nullptr, (clang::StorageClass)storage,
1996 is_inline, hasWrittenPrototype, isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001997 }
1998 if (func_decl)
1999 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00002000
2001#ifdef LLDB_CONFIGURATION_DEBUG
2002 VerifyDecl(func_decl);
2003#endif
2004
Greg Clayton147e1fa2011-10-14 22:47:18 +00002005 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006}
2007
Greg Claytona1e5dc82015-08-11 22:53:00 +00002008CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00002009ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002010 const CompilerType& result_type,
2011 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00002012 unsigned num_args,
2013 bool is_variadic,
2014 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002015{
Greg Claytonceeb5212016-05-26 22:33:25 +00002016 if (ast == nullptr)
2017 return CompilerType(); // invalid AST
2018
2019 if (!result_type || !ClangUtil::IsClangType(result_type))
2020 return CompilerType(); // invalid return type
2021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022 std::vector<QualType> qual_type_args;
Greg Claytonceeb5212016-05-26 22:33:25 +00002023 if (num_args > 0 && args == nullptr)
2024 return CompilerType(); // invalid argument array passed in
2025
2026 // Verify that all arguments are valid and the right type
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002027 for (unsigned i=0; i<num_args; ++i)
Greg Claytonceeb5212016-05-26 22:33:25 +00002028 {
2029 if (args[i])
2030 {
2031 // Make sure we have a clang type in args[i] and not a type from another
2032 // language whose name might match
2033 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2034 lldbassert(is_clang_type);
2035 if (is_clang_type)
2036 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2037 else
2038 return CompilerType(); // invalid argument type (must be a clang type)
2039 }
2040 else
2041 return CompilerType(); // invalid argument type (empty)
2042 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043
2044 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00002045 FunctionProtoType::ExtProtoInfo proto_info;
2046 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002047 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00002048 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00002049 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002050
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002051 return CompilerType(ast, ast->getFunctionType(ClangUtil::GetQualType(result_type), qual_type_args, proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002052}
2053
2054ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00002055ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002057 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002058 assert (ast != nullptr);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002059 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
2060 name && name[0] ? &ast->Idents.get(name) : nullptr, ClangUtil::GetQualType(param_type),
2061 nullptr, (clang::StorageClass)storage, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002062}
2063
2064void
2065ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2066{
2067 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002068 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002069}
2070
Sean Callananc530ba92016-05-02 21:15:31 +00002071CompilerType
2072ClangASTContext::CreateBlockPointerType (const CompilerType &function_type)
2073{
2074 QualType block_type = m_ast_ap->getBlockPointerType(clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2075
2076 return CompilerType (this, block_type.getAsOpaquePtr());
2077}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002078
2079#pragma mark Array Types
2080
Greg Claytona1e5dc82015-08-11 22:53:00 +00002081CompilerType
2082ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002083 size_t element_count,
2084 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085{
Greg Clayton57ee3062013-07-11 22:46:58 +00002086 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002087 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002088 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002089 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002090
Greg Clayton1c8ef472013-04-05 23:27:21 +00002091 if (is_vector)
2092 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002093 return CompilerType(ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00002094 }
2095 else
2096 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002097
2098 llvm::APInt ap_element_count (64, element_count);
2099 if (element_count == 0)
2100 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002101 return CompilerType(ast, ast->getIncompleteArrayType(ClangUtil::GetQualType(element_type),
2102 clang::ArrayType::Normal, 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002103 }
2104 else
2105 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002106 return CompilerType(ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2107 ap_element_count, clang::ArrayType::Normal, 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002108 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002110 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002111 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112}
2113
Greg Claytona1e5dc82015-08-11 22:53:00 +00002114CompilerType
Sean Callananc530ba92016-05-02 21:15:31 +00002115ClangASTContext::CreateStructForIdentifier (const ConstString &type_name,
2116 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
2117 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00002118{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002119 CompilerType type;
Sean Callananc530ba92016-05-02 21:15:31 +00002120 if (!type_name.IsEmpty() && (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2121 {
2122 lldbassert("Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002123 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002124 }
2125
Enrico Granata76b08d52014-10-29 23:08:02 +00002126 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00002127 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002128 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00002129 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00002130 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00002131 SetIsPacked(type);
2132 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002133 return type;
2134}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002135
Sean Callananc530ba92016-05-02 21:15:31 +00002136CompilerType
2137ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
2138 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
2139 bool packed)
2140{
2141 CompilerType type;
2142 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2143 return type;
2144
2145 return CreateStructForIdentifier (type_name,
2146 type_fields,
2147 packed);
2148}
2149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150#pragma mark Enumeration Types
2151
Greg Claytona1e5dc82015-08-11 22:53:00 +00002152CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002153ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002154(
Greg Claytond8d4a572015-08-11 21:38:15 +00002155 const char *name,
2156 DeclContext *decl_ctx,
2157 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002158 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002159 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002160{
2161 // TODO: Do something intelligent with the Declaration object passed in
2162 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002163 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002164
Greg Claytone02b8502010-10-12 04:29:14 +00002165 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002166 // const bool IsScoped = false;
2167 // const bool IsFixed = false;
2168
Greg Clayton6beaaa62011-01-17 03:46:26 +00002169 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002170 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002171 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002172 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002173 name && name[0] ? &ast->Idents.get(name) : nullptr,
2174 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002175 false, // IsScoped
2176 false, // IsScopedUsingClassTag
2177 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002178
2179
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002181 {
2182 // TODO: check if we should be setting the promotion type too?
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002183 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2184
Sean Callanan2652ad22011-01-18 01:03:44 +00002185 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2186
Greg Claytona1e5dc82015-08-11 22:53:00 +00002187 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002188 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002189 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190}
2191
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002192// Disable this for now since I can't seem to get a nicely formatted float
2193// out of the APFloat class without just getting the float, double or quad
2194// and then using a formatted print on it which defeats the purpose. We ideally
2195// would like to get perfect string values for any kind of float semantics
2196// so we can support remote targets. The code below also requires a patch to
2197// llvm::APInt.
2198//bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002199//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 +00002200//{
2201// uint32_t count = 0;
2202// bool is_complex = false;
2203// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2204// {
2205// unsigned num_bytes_per_float = byte_size / count;
2206// unsigned num_bits_per_float = num_bytes_per_float * 8;
2207//
2208// float_str.clear();
2209// uint32_t i;
2210// for (i=0; i<count; i++)
2211// {
2212// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2213// bool is_ieee = false;
2214// APFloat ap_float(ap_int, is_ieee);
2215// char s[1024];
2216// unsigned int hex_digits = 0;
2217// bool upper_case = false;
2218//
2219// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2220// {
2221// if (i > 0)
2222// float_str.append(", ");
2223// float_str.append(s);
2224// if (i == 1 && is_complex)
2225// float_str.append(1, 'i');
2226// }
2227// }
2228// return !float_str.empty();
2229// }
2230// return false;
2231//}
2232
Greg Claytona1e5dc82015-08-11 22:53:00 +00002233CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002234ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2235 size_t bit_size, bool is_signed)
2236{
2237 if (ast)
2238 {
2239 if (is_signed)
2240 {
2241 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002242 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002243
2244 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002245 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002246
2247 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002248 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002249
2250 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002251 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002252
2253 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002254 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002255
2256 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002257 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002258 }
2259 else
2260 {
2261 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002262 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002263
2264 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002265 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002266
2267 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002268 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002269
2270 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002271 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002272
2273 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002274 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002275
2276 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002277 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002278 }
2279 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002280 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002281}
2282
Greg Claytona1e5dc82015-08-11 22:53:00 +00002283CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002284ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2285{
2286 if (ast)
2287 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002288 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002289}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002290
Greg Claytone6b36cd2015-12-08 01:02:08 +00002291void
2292ClangASTContext::DumpDeclContextHiearchy (clang::DeclContext *decl_ctx)
2293{
2294 if (decl_ctx)
2295 {
2296 DumpDeclContextHiearchy (decl_ctx->getParent());
2297
2298 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2299 if (named_decl)
2300 {
2301 printf ("%20s: %s\n", decl_ctx->getDeclKindName(), named_decl->getDeclName().getAsString().c_str());
2302 }
2303 else
2304 {
2305 printf ("%20s\n", decl_ctx->getDeclKindName());
2306 }
2307 }
2308}
2309
2310void
2311ClangASTContext::DumpDeclHiearchy (clang::Decl *decl)
2312{
2313 if (decl == nullptr)
2314 return;
2315 DumpDeclContextHiearchy(decl->getDeclContext());
2316
2317 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2318 if (record_decl)
2319 {
2320 printf ("%20s: %s%s\n", decl->getDeclKindName(), record_decl->getDeclName().getAsString().c_str(), record_decl->isInjectedClassName() ? " (injected class name)" : "");
2321
2322 }
2323 else
2324 {
2325 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2326 if (named_decl)
2327 {
2328 printf ("%20s: %s\n", decl->getDeclKindName(), named_decl->getDeclName().getAsString().c_str());
2329 }
2330 else
2331 {
2332 printf ("%20s\n", decl->getDeclKindName());
2333 }
2334 }
2335}
2336
2337bool
2338ClangASTContext::DeclsAreEquivalent (clang::Decl *lhs_decl, clang::Decl *rhs_decl)
2339{
2340 if (lhs_decl && rhs_decl)
2341 {
2342 //----------------------------------------------------------------------
2343 // Make sure the decl kinds match first
2344 //----------------------------------------------------------------------
2345 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2346 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2347
2348 if (lhs_decl_kind == rhs_decl_kind)
2349 {
2350 //------------------------------------------------------------------
2351 // Now check that the decl contexts kinds are all equivalent
2352 // before we have to check any names of the decl contexts...
2353 //------------------------------------------------------------------
2354 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2355 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2356 if (lhs_decl_ctx && rhs_decl_ctx)
2357 {
2358 while (1)
2359 {
2360 if (lhs_decl_ctx && rhs_decl_ctx)
2361 {
2362 const clang::Decl::Kind lhs_decl_ctx_kind = lhs_decl_ctx->getDeclKind();
2363 const clang::Decl::Kind rhs_decl_ctx_kind = rhs_decl_ctx->getDeclKind();
2364 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind)
2365 {
2366 lhs_decl_ctx = lhs_decl_ctx->getParent();
2367 rhs_decl_ctx = rhs_decl_ctx->getParent();
2368
2369 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2370 break;
2371 }
2372 else
2373 return false;
2374 }
2375 else
2376 return false;
2377 }
2378
2379 //--------------------------------------------------------------
2380 // Now make sure the name of the decls match
2381 //--------------------------------------------------------------
2382 clang::NamedDecl *lhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2383 clang::NamedDecl *rhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2384 if (lhs_named_decl && rhs_named_decl)
2385 {
2386 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2387 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2388 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind())
2389 {
2390 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2391 return false;
2392 }
2393 else
2394 return false;
2395 }
2396 else
2397 return false;
2398
2399 //--------------------------------------------------------------
2400 // We know that the decl context kinds all match, so now we need
2401 // to make sure the names match as well
2402 //--------------------------------------------------------------
2403 lhs_decl_ctx = lhs_decl->getDeclContext();
2404 rhs_decl_ctx = rhs_decl->getDeclContext();
2405 while (1)
2406 {
2407 switch (lhs_decl_ctx->getDeclKind())
2408 {
2409 case clang::Decl::TranslationUnit:
2410 // We don't care about the translation unit names
2411 return true;
2412 default:
2413 {
2414 clang::NamedDecl *lhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2415 clang::NamedDecl *rhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2416 if (lhs_named_decl && rhs_named_decl)
2417 {
2418 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2419 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2420 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind())
2421 {
2422 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2423 return false;
2424 }
2425 else
2426 return false;
2427 }
2428 else
2429 return false;
2430 }
2431 break;
2432
2433 }
2434 lhs_decl_ctx = lhs_decl_ctx->getParent();
2435 rhs_decl_ctx = rhs_decl_ctx->getParent();
2436 }
2437 }
2438 }
2439 }
2440 return false;
2441}
Enrico Granata86027e92012-03-24 01:11:14 +00002442bool
Greg Claytona2721472011-06-25 00:44:06 +00002443ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2444 clang::Decl *decl)
2445{
2446 if (!decl)
2447 return false;
2448
2449 ExternalASTSource *ast_source = ast->getExternalSource();
2450
2451 if (!ast_source)
2452 return false;
2453
2454 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2455 {
Greg Clayton219cf312012-03-30 00:51:13 +00002456 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002457 return true;
2458
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002459 if (!tag_decl->hasExternalLexicalStorage())
2460 return false;
2461
Greg Claytona2721472011-06-25 00:44:06 +00002462 ast_source->CompleteType(tag_decl);
2463
2464 return !tag_decl->getTypeForDecl()->isIncompleteType();
2465 }
2466 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2467 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002468 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002469 return true;
2470
2471 if (!objc_interface_decl->hasExternalLexicalStorage())
2472 return false;
2473
2474 ast_source->CompleteType(objc_interface_decl);
2475
Sean Callanan5b26f272012-02-04 08:49:35 +00002476 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002477 }
2478 else
2479 {
2480 return false;
2481 }
2482}
2483
Sean Callanan60217122012-04-13 00:10:03 +00002484void
Greg Claytond0029442013-03-27 01:48:02 +00002485ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002486 user_id_t user_id)
2487{
2488 ClangASTMetadata meta_data;
2489 meta_data.SetUserID (user_id);
2490 SetMetadata (object, meta_data);
2491}
2492
2493void
Sean Callanan60217122012-04-13 00:10:03 +00002494ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002495 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002496 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002497{
2498 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002499 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002500
2501 if (external_source)
2502 external_source->SetMetadata(object, metadata);
2503}
2504
Jim Ingham379397632012-10-27 02:54:13 +00002505ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002506ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002507 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002508{
2509 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002510 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002511
2512 if (external_source && external_source->HasMetadata(object))
2513 return external_source->GetMetadata(object);
2514 else
Ed Masted4612ad2014-04-20 13:17:36 +00002515 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002516}
2517
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002518clang::DeclContext *
2519ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2520{
Sean Callanana87bee82011-08-19 06:19:25 +00002521 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002522}
2523
2524clang::DeclContext *
2525ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2526{
Sean Callanana87bee82011-08-19 06:19:25 +00002527 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002528}
2529
Greg Claytond8d4a572015-08-11 21:38:15 +00002530bool
2531ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2532{
2533 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2534 if (clang_type)
2535 {
2536 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2537 if (tag_type)
2538 {
2539 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2540 if (tag_decl)
2541 {
2542 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2543 return true;
2544 }
2545 }
2546 }
2547 return false;
2548}
2549
2550
2551bool
2552ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2553 int default_accessibility,
2554 int *assigned_accessibilities,
2555 size_t num_assigned_accessibilities)
2556{
2557 if (record_decl)
2558 {
2559 uint32_t field_idx;
2560 clang::RecordDecl::field_iterator field, field_end;
2561 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2562 field != field_end;
2563 ++field, ++field_idx)
2564 {
2565 // If no accessibility was assigned, assign the correct one
2566 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2567 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2568 }
2569 return true;
2570 }
2571 return false;
2572}
2573
2574clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002575ClangASTContext::GetDeclContextForType (const CompilerType& type)
2576{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002577 return GetDeclContextForType(ClangUtil::GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00002578}
2579
2580clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002581ClangASTContext::GetDeclContextForType (clang::QualType type)
2582{
2583 if (type.isNull())
2584 return nullptr;
2585
2586 clang::QualType qual_type = type.getCanonicalType();
2587 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2588 switch (type_class)
2589 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002590 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2591 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2592 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2593 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2594 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
Enrico Granata36f51e42015-12-18 22:41:25 +00002595 case clang::Type::Auto: return GetDeclContextForType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002596 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2597 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002598 default:
2599 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002600 }
2601 // No DeclContext in this type...
2602 return nullptr;
2603}
2604
2605static bool
2606GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2607{
2608 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2609 switch (type_class)
2610 {
2611 case clang::Type::ConstantArray:
2612 case clang::Type::IncompleteArray:
2613 case clang::Type::VariableArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002614 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002615 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2616
2617 if (array_type)
2618 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2619 }
2620 break;
2621 case clang::Type::Record:
2622 {
2623 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2624 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002625 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002626 if (cxx_record_decl->hasExternalLexicalStorage())
Greg Claytond8d4a572015-08-11 21:38:15 +00002627 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002628 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2629 const bool fields_loaded = cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2630 if (is_complete && fields_loaded)
2631 return true;
2632
2633 if (!allow_completion)
2634 return false;
2635
2636 // Call the field_begin() accessor to for it to use the external source
2637 // to load the fields...
2638 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2639 if (external_ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002640 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002641 external_ast_source->CompleteType(cxx_record_decl);
2642 if (cxx_record_decl->isCompleteDefinition())
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002643 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002644 cxx_record_decl->field_begin();
Jim Inghamc44644d2016-05-04 00:06:23 +00002645 cxx_record_decl->setHasLoadedFieldsFromExternalStorage (true);
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002646 }
2647 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002648 }
2649 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002650 const clang::TagType *tag_type = llvm::cast<clang::TagType>(qual_type.getTypePtr());
2651 return !tag_type->isIncompleteType();
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002652 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002653 break;
Greg Claytone6b36cd2015-12-08 01:02:08 +00002654
2655 case clang::Type::Enum:
2656 {
2657 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2658 if (tag_type)
2659 {
2660 clang::TagDecl *tag_decl = tag_type->getDecl();
2661 if (tag_decl)
2662 {
2663 if (tag_decl->getDefinition())
2664 return true;
2665
2666 if (!allow_completion)
2667 return false;
2668
2669 if (tag_decl->hasExternalLexicalStorage())
2670 {
2671 if (ast)
2672 {
2673 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2674 if (external_ast_source)
2675 {
2676 external_ast_source->CompleteType(tag_decl);
2677 return !tag_type->isIncompleteType();
2678 }
2679 }
2680 }
2681 return false;
2682 }
2683 }
2684
2685 }
2686 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002687 case clang::Type::ObjCObject:
2688 case clang::Type::ObjCInterface:
Greg Claytond8d4a572015-08-11 21:38:15 +00002689 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002690 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2691 if (objc_class_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002692 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002693 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2694 // We currently can't complete objective C types through the newly added ASTContext
2695 // because it only supports TagDecl objects right now...
2696 if (class_interface_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002697 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002698 if (class_interface_decl->getDefinition())
2699 return true;
2700
2701 if (!allow_completion)
2702 return false;
2703
2704 if (class_interface_decl->hasExternalLexicalStorage())
Greg Claytond8d4a572015-08-11 21:38:15 +00002705 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002706 if (ast)
Greg Claytond8d4a572015-08-11 21:38:15 +00002707 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002708 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2709 if (external_ast_source)
2710 {
2711 external_ast_source->CompleteType (class_interface_decl);
2712 return !objc_class_type->isIncompleteType();
2713 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002714 }
2715 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002716 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002717 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002718 }
2719 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002720 break;
2721
2722 case clang::Type::Typedef:
2723 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2724
Enrico Granata36f51e42015-12-18 22:41:25 +00002725 case clang::Type::Auto:
2726 return GetCompleteQualType (ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), allow_completion);
2727
Greg Claytond8d4a572015-08-11 21:38:15 +00002728 case clang::Type::Elaborated:
2729 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2730
2731 case clang::Type::Paren:
2732 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
Greg Claytone6b36cd2015-12-08 01:02:08 +00002733
2734 case clang::Type::Attributed:
2735 return GetCompleteQualType (ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), allow_completion);
2736
Greg Claytond8d4a572015-08-11 21:38:15 +00002737 default:
2738 break;
2739 }
2740
2741 return true;
2742}
2743
2744static clang::ObjCIvarDecl::AccessControl
2745ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2746{
2747 switch (access)
2748 {
2749 case eAccessNone: return clang::ObjCIvarDecl::None;
2750 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2751 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2752 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2753 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2754 }
2755 return clang::ObjCIvarDecl::None;
2756}
2757
2758
2759//----------------------------------------------------------------------
2760// Tests
2761//----------------------------------------------------------------------
2762
2763bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002764ClangASTContext::IsAggregateType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002765{
2766 clang::QualType qual_type (GetCanonicalQualType(type));
2767
2768 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2769 switch (type_class)
2770 {
2771 case clang::Type::IncompleteArray:
2772 case clang::Type::VariableArray:
2773 case clang::Type::ConstantArray:
2774 case clang::Type::ExtVector:
2775 case clang::Type::Vector:
2776 case clang::Type::Record:
2777 case clang::Type::ObjCObject:
2778 case clang::Type::ObjCInterface:
2779 return true;
Enrico Granata36f51e42015-12-18 22:41:25 +00002780 case clang::Type::Auto:
2781 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00002782 case clang::Type::Elaborated:
2783 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2784 case clang::Type::Typedef:
2785 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2786 case clang::Type::Paren:
2787 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2788 default:
2789 break;
2790 }
2791 // The clang type does have a value
2792 return false;
2793}
2794
2795bool
Enrico Granata7123e2b2015-11-07 02:06:57 +00002796ClangASTContext::IsAnonymousType (lldb::opaque_compiler_type_t type)
2797{
2798 clang::QualType qual_type (GetCanonicalQualType(type));
2799
2800 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2801 switch (type_class)
2802 {
2803 case clang::Type::Record:
2804 {
2805 if (const clang::RecordType *record_type = llvm::dyn_cast_or_null<clang::RecordType>(qual_type.getTypePtrOrNull()))
2806 {
2807 if (const clang::RecordDecl *record_decl = record_type->getDecl())
2808 {
2809 return record_decl->isAnonymousStructOrUnion();
2810 }
2811 }
2812 break;
2813 }
Enrico Granata36f51e42015-12-18 22:41:25 +00002814 case clang::Type::Auto:
2815 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Enrico Granata7123e2b2015-11-07 02:06:57 +00002816 case clang::Type::Elaborated:
2817 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2818 case clang::Type::Typedef:
2819 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2820 case clang::Type::Paren:
2821 return IsAnonymousType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2822 default:
2823 break;
2824 }
2825 // The clang type does have a value
2826 return false;
2827}
2828
2829bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002830ClangASTContext::IsArrayType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002831 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002832 uint64_t *size,
2833 bool *is_incomplete)
2834{
2835 clang::QualType qual_type (GetCanonicalQualType(type));
2836
2837 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2838 switch (type_class)
2839 {
2840 default:
2841 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002842
Greg Claytond8d4a572015-08-11 21:38:15 +00002843 case clang::Type::ConstantArray:
2844 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002845 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002846 if (size)
2847 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002848 if (is_incomplete)
2849 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002850 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002851
Greg Claytond8d4a572015-08-11 21:38:15 +00002852 case clang::Type::IncompleteArray:
2853 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002854 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002855 if (size)
2856 *size = 0;
2857 if (is_incomplete)
2858 *is_incomplete = true;
2859 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002860
Greg Claytond8d4a572015-08-11 21:38:15 +00002861 case clang::Type::VariableArray:
2862 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002863 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002864 if (size)
2865 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002866 if (is_incomplete)
2867 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002868 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002869
Greg Claytond8d4a572015-08-11 21:38:15 +00002870 case clang::Type::DependentSizedArray:
2871 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002872 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002873 if (size)
2874 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002875 if (is_incomplete)
2876 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002877 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002878
Greg Claytond8d4a572015-08-11 21:38:15 +00002879 case clang::Type::Typedef:
2880 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2881 element_type_ptr,
2882 size,
2883 is_incomplete);
Enrico Granata36f51e42015-12-18 22:41:25 +00002884 case clang::Type::Auto:
2885 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(),
2886 element_type_ptr,
2887 size,
2888 is_incomplete);
Greg Claytond8d4a572015-08-11 21:38:15 +00002889 case clang::Type::Elaborated:
2890 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2891 element_type_ptr,
2892 size,
2893 is_incomplete);
2894 case clang::Type::Paren:
2895 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2896 element_type_ptr,
2897 size,
2898 is_incomplete);
2899 }
2900 if (element_type_ptr)
2901 element_type_ptr->Clear();
2902 if (size)
2903 *size = 0;
2904 if (is_incomplete)
2905 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002906 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002907}
2908
2909bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002910ClangASTContext::IsVectorType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002911 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002912 uint64_t *size)
2913{
2914 clang::QualType qual_type (GetCanonicalQualType(type));
2915
2916 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2917 switch (type_class)
2918 {
2919 case clang::Type::Vector:
2920 {
2921 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2922 if (vector_type)
2923 {
2924 if (size)
2925 *size = vector_type->getNumElements();
2926 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002927 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002928 }
2929 return true;
2930 }
2931 break;
2932 case clang::Type::ExtVector:
2933 {
2934 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2935 if (ext_vector_type)
2936 {
2937 if (size)
2938 *size = ext_vector_type->getNumElements();
2939 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002940 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002941 }
2942 return true;
2943 }
2944 default:
2945 break;
2946 }
2947 return false;
2948}
2949
2950bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002951ClangASTContext::IsRuntimeGeneratedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002952{
2953 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2954 if (!decl_ctx)
2955 return false;
2956
2957 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2958 return false;
2959
2960 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2961
2962 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2963 if (!ast_metadata)
2964 return false;
2965 return (ast_metadata->GetISAPtr() != 0);
2966}
2967
2968bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002969ClangASTContext::IsCharType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002970{
2971 return GetQualType(type).getUnqualifiedType()->isCharType();
2972}
2973
2974
2975bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002976ClangASTContext::IsCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002977{
2978 const bool allow_completion = false;
2979 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2980}
2981
2982bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002983ClangASTContext::IsConst(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002984{
2985 return GetQualType(type).isConstQualified();
2986}
2987
2988bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002989ClangASTContext::IsCStringType (lldb::opaque_compiler_type_t type, uint32_t &length)
Greg Claytond8d4a572015-08-11 21:38:15 +00002990{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002991 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002992 length = 0;
2993 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2994
2995 if (!pointee_or_element_clang_type.IsValid())
2996 return false;
2997
2998 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2999 {
3000 if (pointee_or_element_clang_type.IsCharType())
3001 {
3002 if (type_flags.Test (eTypeIsArray))
3003 {
3004 // We know the size of the array and it could be a C string
3005 // since it is an array of characters
3006 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
3007 }
3008 return true;
3009
3010 }
3011 }
3012 return false;
3013}
3014
3015bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003016ClangASTContext::IsFunctionType (lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003017{
3018 if (type)
3019 {
3020 clang::QualType qual_type (GetCanonicalQualType(type));
3021
3022 if (qual_type->isFunctionType())
3023 {
3024 if (is_variadic_ptr)
3025 {
3026 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3027 if (function_proto_type)
3028 *is_variadic_ptr = function_proto_type->isVariadic();
3029 else
3030 *is_variadic_ptr = false;
3031 }
3032 return true;
3033 }
3034
3035 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3036 switch (type_class)
3037 {
3038 default:
3039 break;
3040 case clang::Type::Typedef:
3041 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00003042 case clang::Type::Auto:
3043 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), nullptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00003044 case clang::Type::Elaborated:
3045 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
3046 case clang::Type::Paren:
3047 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
3048 case clang::Type::LValueReference:
3049 case clang::Type::RValueReference:
3050 {
3051 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3052 if (reference_type)
3053 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
3054 }
3055 break;
3056 }
3057 }
3058 return false;
3059}
3060
3061// Used to detect "Homogeneous Floating-point Aggregates"
3062uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003063ClangASTContext::IsHomogeneousAggregate (lldb::opaque_compiler_type_t type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003064{
3065 if (!type)
3066 return 0;
3067
3068 clang::QualType qual_type(GetCanonicalQualType(type));
3069 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3070 switch (type_class)
3071 {
3072 case clang::Type::Record:
3073 if (GetCompleteType (type))
3074 {
3075 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3076 if (cxx_record_decl)
3077 {
3078 if (cxx_record_decl->getNumBases() ||
3079 cxx_record_decl->isDynamicClass())
3080 return 0;
3081 }
3082 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3083 if (record_type)
3084 {
3085 const clang::RecordDecl *record_decl = record_type->getDecl();
3086 if (record_decl)
3087 {
3088 // We are looking for a structure that contains only floating point types
3089 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
3090 uint32_t num_fields = 0;
3091 bool is_hva = false;
3092 bool is_hfa = false;
3093 clang::QualType base_qual_type;
Omair Javaid92a8ded2016-02-24 12:17:43 +00003094 uint64_t base_bitwidth = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003095 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
3096 {
3097 clang::QualType field_qual_type = field_pos->getType();
Omair Javaid92a8ded2016-02-24 12:17:43 +00003098 uint64_t field_bitwidth = getASTContext()->getTypeSize (qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003099 if (field_qual_type->isFloatingType())
3100 {
3101 if (field_qual_type->isComplexType())
3102 return 0;
3103 else
3104 {
3105 if (num_fields == 0)
3106 base_qual_type = field_qual_type;
3107 else
3108 {
3109 if (is_hva)
3110 return 0;
3111 is_hfa = true;
3112 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3113 return 0;
3114 }
3115 }
3116 }
3117 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
3118 {
Omair Javaid92a8ded2016-02-24 12:17:43 +00003119 if (num_fields == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00003120 {
Omair Javaid92a8ded2016-02-24 12:17:43 +00003121 base_qual_type = field_qual_type;
3122 base_bitwidth = field_bitwidth;
Greg Claytond8d4a572015-08-11 21:38:15 +00003123 }
3124 else
Omair Javaid92a8ded2016-02-24 12:17:43 +00003125 {
3126 if (is_hfa)
3127 return 0;
3128 is_hva = true;
3129 if (base_bitwidth != field_bitwidth)
3130 return 0;
3131 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3132 return 0;
3133 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003134 }
3135 else
3136 return 0;
3137 ++num_fields;
3138 }
3139 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003140 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003141 return num_fields;
3142 }
3143 }
3144 }
3145 break;
3146
3147 case clang::Type::Typedef:
3148 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00003149
3150 case clang::Type::Auto:
3151 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), base_type_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00003152
3153 case clang::Type::Elaborated:
3154 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
3155 default:
3156 break;
3157 }
3158 return 0;
3159}
3160
3161size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003162ClangASTContext::GetNumberOfFunctionArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003163{
3164 if (type)
3165 {
3166 clang::QualType qual_type (GetCanonicalQualType(type));
3167 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3168 if (func)
3169 return func->getNumParams();
3170 }
3171 return 0;
3172}
3173
Greg Claytona1e5dc82015-08-11 22:53:00 +00003174CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003175ClangASTContext::GetFunctionArgumentAtIndex (lldb::opaque_compiler_type_t type, const size_t index)
Greg Claytond8d4a572015-08-11 21:38:15 +00003176{
3177 if (type)
3178 {
Greg Clayton0a5f8052016-03-15 22:43:26 +00003179 clang::QualType qual_type (GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00003180 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3181 if (func)
3182 {
3183 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00003184 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003185 }
3186 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003187 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003188}
3189
3190bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003191ClangASTContext::IsFunctionPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003192{
3193 if (type)
3194 {
3195 clang::QualType qual_type (GetCanonicalQualType(type));
3196
3197 if (qual_type->isFunctionPointerType())
3198 return true;
3199
3200 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3201 switch (type_class)
3202 {
3203 default:
3204 break;
3205 case clang::Type::Typedef:
3206 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Enrico Granata36f51e42015-12-18 22:41:25 +00003207 case clang::Type::Auto:
3208 return IsFunctionPointerType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003209 case clang::Type::Elaborated:
3210 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3211 case clang::Type::Paren:
3212 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3213
3214 case clang::Type::LValueReference:
3215 case clang::Type::RValueReference:
3216 {
3217 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3218 if (reference_type)
3219 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
3220 }
3221 break;
3222 }
3223 }
3224 return false;
3225
3226}
3227
3228bool
Sean Callananc530ba92016-05-02 21:15:31 +00003229ClangASTContext::IsBlockPointerType (lldb::opaque_compiler_type_t type, CompilerType *function_pointer_type_ptr)
3230{
3231 if (type)
3232 {
3233 clang::QualType qual_type (GetCanonicalQualType(type));
3234
3235 if (qual_type->isBlockPointerType())
3236 {
3237 if (function_pointer_type_ptr)
3238 {
3239 const clang::BlockPointerType *block_pointer_type = qual_type->getAs<clang::BlockPointerType>();
3240 QualType pointee_type = block_pointer_type->getPointeeType();
3241 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3242 *function_pointer_type_ptr = CompilerType (getASTContext(), function_pointer_type);
3243 }
3244 return true;
3245 }
3246
3247 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3248 switch (type_class)
3249 {
3250 default:
3251 break;
3252 case clang::Type::Typedef:
3253 return IsBlockPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), function_pointer_type_ptr);
3254 case clang::Type::Auto:
3255 return IsBlockPointerType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), function_pointer_type_ptr);
3256 case clang::Type::Elaborated:
3257 return IsBlockPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), function_pointer_type_ptr);
3258 case clang::Type::Paren:
3259 return IsBlockPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), function_pointer_type_ptr);
3260
3261 case clang::Type::LValueReference:
3262 case clang::Type::RValueReference:
3263 {
3264 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3265 if (reference_type)
3266 return IsBlockPointerType(reference_type->getPointeeType().getAsOpaquePtr(), function_pointer_type_ptr);
3267 }
3268 break;
3269 }
3270 }
3271 return false;
3272}
3273
3274bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003275ClangASTContext::IsIntegerType (lldb::opaque_compiler_type_t type, bool &is_signed)
Greg Claytond8d4a572015-08-11 21:38:15 +00003276{
3277 if (!type)
3278 return false;
3279
3280 clang::QualType qual_type (GetCanonicalQualType(type));
3281 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3282
3283 if (builtin_type)
3284 {
3285 if (builtin_type->isInteger())
3286 {
3287 is_signed = builtin_type->isSignedInteger();
3288 return true;
3289 }
3290 }
3291
3292 return false;
3293}
3294
3295bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003296ClangASTContext::IsPointerType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003297{
3298 if (type)
3299 {
3300 clang::QualType qual_type (GetCanonicalQualType(type));
3301 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3302 switch (type_class)
3303 {
3304 case clang::Type::Builtin:
3305 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3306 {
3307 default:
3308 break;
3309 case clang::BuiltinType::ObjCId:
3310 case clang::BuiltinType::ObjCClass:
3311 return true;
3312 }
3313 return false;
3314 case clang::Type::ObjCObjectPointer:
3315 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003316 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003317 return true;
3318 case clang::Type::BlockPointer:
3319 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003320 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003321 return true;
3322 case clang::Type::Pointer:
3323 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003324 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003325 return true;
3326 case clang::Type::MemberPointer:
3327 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003328 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003329 return true;
3330 case clang::Type::Typedef:
3331 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
Enrico Granata36f51e42015-12-18 22:41:25 +00003332 case clang::Type::Auto:
3333 return IsPointerType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003334 case clang::Type::Elaborated:
3335 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3336 case clang::Type::Paren:
3337 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3338 default:
3339 break;
3340 }
3341 }
3342 if (pointee_type)
3343 pointee_type->Clear();
3344 return false;
3345}
3346
3347
3348bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003349ClangASTContext::IsPointerOrReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003350{
3351 if (type)
3352 {
3353 clang::QualType qual_type (GetCanonicalQualType(type));
3354 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3355 switch (type_class)
3356 {
3357 case clang::Type::Builtin:
3358 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3359 {
3360 default:
3361 break;
3362 case clang::BuiltinType::ObjCId:
3363 case clang::BuiltinType::ObjCClass:
3364 return true;
3365 }
3366 return false;
3367 case clang::Type::ObjCObjectPointer:
3368 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003369 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003370 return true;
3371 case clang::Type::BlockPointer:
3372 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003373 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003374 return true;
3375 case clang::Type::Pointer:
3376 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003377 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003378 return true;
3379 case clang::Type::MemberPointer:
3380 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003381 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003382 return true;
3383 case clang::Type::LValueReference:
3384 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003385 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003386 return true;
3387 case clang::Type::RValueReference:
3388 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003389 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003390 return true;
3391 case clang::Type::Typedef:
3392 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
Enrico Granata36f51e42015-12-18 22:41:25 +00003393 case clang::Type::Auto:
3394 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003395 case clang::Type::Elaborated:
3396 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3397 case clang::Type::Paren:
3398 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3399 default:
3400 break;
3401 }
3402 }
3403 if (pointee_type)
3404 pointee_type->Clear();
3405 return false;
3406}
3407
3408
3409bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003410ClangASTContext::IsReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003411{
3412 if (type)
3413 {
3414 clang::QualType qual_type (GetCanonicalQualType(type));
3415 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3416
3417 switch (type_class)
3418 {
3419 case clang::Type::LValueReference:
3420 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003421 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003422 if (is_rvalue)
3423 *is_rvalue = false;
3424 return true;
3425 case clang::Type::RValueReference:
3426 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003427 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003428 if (is_rvalue)
3429 *is_rvalue = true;
3430 return true;
3431 case clang::Type::Typedef:
3432 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
Enrico Granata36f51e42015-12-18 22:41:25 +00003433 case clang::Type::Auto:
3434 return IsReferenceType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type, is_rvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00003435 case clang::Type::Elaborated:
3436 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3437 case clang::Type::Paren:
3438 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3439
3440 default:
3441 break;
3442 }
3443 }
3444 if (pointee_type)
3445 pointee_type->Clear();
3446 return false;
3447}
3448
3449bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003450ClangASTContext::IsFloatingPointType (lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Greg Claytond8d4a572015-08-11 21:38:15 +00003451{
3452 if (type)
3453 {
3454 clang::QualType qual_type (GetCanonicalQualType(type));
3455
3456 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3457 {
3458 clang::BuiltinType::Kind kind = BT->getKind();
3459 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3460 {
3461 count = 1;
3462 is_complex = false;
3463 return true;
3464 }
3465 }
3466 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3467 {
3468 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3469 {
3470 count = 2;
3471 is_complex = true;
3472 return true;
3473 }
3474 }
3475 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3476 {
3477 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3478 {
3479 count = VT->getNumElements();
3480 is_complex = false;
3481 return true;
3482 }
3483 }
3484 }
3485 count = 0;
3486 is_complex = false;
3487 return false;
3488}
3489
3490
3491bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003492ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003493{
3494 if (!type)
3495 return false;
3496
3497 clang::QualType qual_type(GetQualType(type));
3498 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3499 if (tag_type)
3500 {
3501 clang::TagDecl *tag_decl = tag_type->getDecl();
3502 if (tag_decl)
3503 return tag_decl->isCompleteDefinition();
3504 return false;
3505 }
3506 else
3507 {
3508 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3509 if (objc_class_type)
3510 {
3511 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3512 if (class_interface_decl)
3513 return class_interface_decl->getDefinition() != nullptr;
3514 return false;
3515 }
3516 }
3517 return true;
3518}
3519
3520bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003521ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003522{
3523 if (type)
3524 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003525 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3526
Greg Claytond8d4a572015-08-11 21:38:15 +00003527 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3528
3529 if (obj_pointer_type)
3530 return obj_pointer_type->isObjCClassType();
3531 }
3532 return false;
3533}
3534
3535bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003536ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003537{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003538 if (ClangUtil::IsClangType(type))
3539 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003540 return false;
3541}
3542
3543bool
Zachary Turner42dff792016-04-15 00:21:26 +00003544ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type)
3545{
3546 if (!type)
3547 return false;
3548 clang::QualType qual_type(GetCanonicalQualType(type));
3549 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3550 return (type_class == clang::Type::Record);
3551}
3552
3553bool
3554ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type)
3555{
3556 if (!type)
3557 return false;
3558 clang::QualType qual_type(GetCanonicalQualType(type));
3559 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3560 return (type_class == clang::Type::Enum);
3561}
3562
3563bool
3564ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003565{
3566 if (type)
3567 {
3568 clang::QualType qual_type(GetCanonicalQualType(type));
3569 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3570 switch (type_class)
3571 {
3572 case clang::Type::Record:
3573 if (GetCompleteType(type))
3574 {
3575 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3576 const clang::RecordDecl *record_decl = record_type->getDecl();
3577 if (record_decl)
3578 {
3579 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3580 if (cxx_record_decl)
3581 return cxx_record_decl->isPolymorphic();
3582 }
3583 }
3584 break;
3585
3586 default:
3587 break;
3588 }
3589 }
3590 return false;
3591}
3592
3593bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003594ClangASTContext::IsPossibleDynamicType (lldb::opaque_compiler_type_t type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003595 bool check_cplusplus,
3596 bool check_objc)
3597{
3598 clang::QualType pointee_qual_type;
3599 if (type)
3600 {
3601 clang::QualType qual_type (GetCanonicalQualType(type));
3602 bool success = false;
3603 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3604 switch (type_class)
3605 {
3606 case clang::Type::Builtin:
3607 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3608 {
3609 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003610 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003611 return true;
3612 }
3613 break;
3614
3615 case clang::Type::ObjCObjectPointer:
3616 if (check_objc)
3617 {
3618 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003619 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003620 return true;
3621 }
3622 break;
3623
3624 case clang::Type::Pointer:
3625 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3626 success = true;
3627 break;
3628
3629 case clang::Type::LValueReference:
3630 case clang::Type::RValueReference:
3631 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3632 success = true;
3633 break;
3634
3635 case clang::Type::Typedef:
3636 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3637 dynamic_pointee_type,
3638 check_cplusplus,
3639 check_objc);
Enrico Granata36f51e42015-12-18 22:41:25 +00003640
3641 case clang::Type::Auto:
3642 return IsPossibleDynamicType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(),
3643 dynamic_pointee_type,
3644 check_cplusplus,
3645 check_objc);
Greg Claytond8d4a572015-08-11 21:38:15 +00003646
3647 case clang::Type::Elaborated:
3648 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3649 dynamic_pointee_type,
3650 check_cplusplus,
3651 check_objc);
3652
3653 case clang::Type::Paren:
3654 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3655 dynamic_pointee_type,
3656 check_cplusplus,
3657 check_objc);
3658 default:
3659 break;
3660 }
3661
3662 if (success)
3663 {
3664 // Check to make sure what we are pointing too is a possible dynamic C++ type
3665 // We currently accept any "void *" (in case we have a class that has been
3666 // watered down to an opaque pointer) and virtual C++ classes.
3667 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3668 switch (pointee_type_class)
3669 {
3670 case clang::Type::Builtin:
3671 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3672 {
3673 case clang::BuiltinType::UnknownAny:
3674 case clang::BuiltinType::Void:
3675 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003676 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003677 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003678 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003679 break;
3680 }
3681 break;
3682
3683 case clang::Type::Record:
3684 if (check_cplusplus)
3685 {
3686 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3687 if (cxx_record_decl)
3688 {
3689 bool is_complete = cxx_record_decl->isCompleteDefinition();
3690
3691 if (is_complete)
3692 success = cxx_record_decl->isDynamicClass();
3693 else
3694 {
3695 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3696 if (metadata)
3697 success = metadata->GetIsDynamicCXXType();
3698 else
3699 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003700 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003701 if (is_complete)
3702 success = cxx_record_decl->isDynamicClass();
3703 else
3704 success = false;
3705 }
3706 }
3707
3708 if (success)
3709 {
3710 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003711 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003712 return true;
3713 }
3714 }
3715 }
3716 break;
3717
3718 case clang::Type::ObjCObject:
3719 case clang::Type::ObjCInterface:
3720 if (check_objc)
3721 {
3722 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003723 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003724 return true;
3725 }
3726 break;
3727
3728 default:
3729 break;
3730 }
3731 }
3732 }
3733 if (dynamic_pointee_type)
3734 dynamic_pointee_type->Clear();
3735 return false;
3736}
3737
3738
3739bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003740ClangASTContext::IsScalarType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003741{
3742 if (!type)
3743 return false;
3744
3745 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3746}
3747
3748bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003749ClangASTContext::IsTypedefType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003750{
3751 if (!type)
3752 return false;
3753 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3754}
3755
3756bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003757ClangASTContext::IsVoidType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003758{
3759 if (!type)
3760 return false;
3761 return GetCanonicalQualType(type)->isVoidType();
3762}
3763
3764bool
Greg Clayton56939cb2015-09-17 22:23:34 +00003765ClangASTContext::SupportsLanguage (lldb::LanguageType language)
3766{
3767 return ClangASTContextSupportsLanguage(language);
3768}
3769
3770bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003771ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003772{
3773 if (type)
3774 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003775 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003776 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003777 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003778 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3779 if (cxx_record_decl)
3780 {
3781 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3782 return true;
3783 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003784 }
3785 }
3786 class_name.clear();
3787 return false;
3788}
3789
3790
3791bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003792ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003793{
3794 if (!type)
3795 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003796
3797 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003798 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003799 return true;
3800 return false;
3801}
3802
3803bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003804ClangASTContext::IsBeingDefined (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003805{
3806 if (!type)
3807 return false;
3808 clang::QualType qual_type (GetCanonicalQualType(type));
3809 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3810 if (tag_type)
3811 return tag_type->isBeingDefined();
3812 return false;
3813}
3814
3815bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003816ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003817{
3818 if (!type)
3819 return false;
3820
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003821 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003822
3823 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003824 {
3825 if (class_type_ptr)
3826 {
3827 if (!qual_type->isObjCClassType() &&
3828 !qual_type->isObjCIdType())
3829 {
3830 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3831 if (obj_pointer_type == nullptr)
3832 class_type_ptr->Clear();
3833 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003834 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003835 }
3836 }
3837 return true;
3838 }
3839 if (class_type_ptr)
3840 class_type_ptr->Clear();
3841 return false;
3842}
3843
3844bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003845ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003846{
3847 if (!type)
3848 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003849
3850 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3851
Greg Claytond8d4a572015-08-11 21:38:15 +00003852 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3853 if (object_type)
3854 {
3855 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3856 if (interface)
3857 {
3858 class_name = interface->getNameAsString();
3859 return true;
3860 }
3861 }
3862 return false;
3863}
3864
3865
3866//----------------------------------------------------------------------
3867// Type Completion
3868//----------------------------------------------------------------------
3869
3870bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003871ClangASTContext::GetCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003872{
3873 if (!type)
3874 return false;
3875 const bool allow_completion = true;
3876 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3877}
3878
3879ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003880ClangASTContext::GetTypeName (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003881{
3882 std::string type_name;
3883 if (type)
3884 {
3885 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3886 clang::QualType qual_type(GetQualType(type));
3887 printing_policy.SuppressTagKeyword = true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003888 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3889 if (typedef_type)
3890 {
3891 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3892 type_name = typedef_decl->getQualifiedNameAsString();
3893 }
3894 else
3895 {
3896 type_name = qual_type.getAsString(printing_policy);
3897 }
3898 }
3899 return ConstString(type_name);
3900}
3901
3902uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003903ClangASTContext::GetTypeInfo (lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003904{
3905 if (!type)
3906 return 0;
3907
3908 if (pointee_or_element_clang_type)
3909 pointee_or_element_clang_type->Clear();
3910
3911 clang::QualType qual_type (GetQualType(type));
3912
3913 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3914 switch (type_class)
3915 {
3916 case clang::Type::Builtin:
3917 {
3918 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3919
3920 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3921 switch (builtin_type->getKind())
3922 {
3923 case clang::BuiltinType::ObjCId:
3924 case clang::BuiltinType::ObjCClass:
3925 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003926 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003927 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3928 break;
3929
3930 case clang::BuiltinType::ObjCSel:
3931 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003932 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003933 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3934 break;
3935
3936 case clang::BuiltinType::Bool:
3937 case clang::BuiltinType::Char_U:
3938 case clang::BuiltinType::UChar:
3939 case clang::BuiltinType::WChar_U:
3940 case clang::BuiltinType::Char16:
3941 case clang::BuiltinType::Char32:
3942 case clang::BuiltinType::UShort:
3943 case clang::BuiltinType::UInt:
3944 case clang::BuiltinType::ULong:
3945 case clang::BuiltinType::ULongLong:
3946 case clang::BuiltinType::UInt128:
3947 case clang::BuiltinType::Char_S:
3948 case clang::BuiltinType::SChar:
3949 case clang::BuiltinType::WChar_S:
3950 case clang::BuiltinType::Short:
3951 case clang::BuiltinType::Int:
3952 case clang::BuiltinType::Long:
3953 case clang::BuiltinType::LongLong:
3954 case clang::BuiltinType::Int128:
3955 case clang::BuiltinType::Float:
3956 case clang::BuiltinType::Double:
3957 case clang::BuiltinType::LongDouble:
3958 builtin_type_flags |= eTypeIsScalar;
3959 if (builtin_type->isInteger())
3960 {
3961 builtin_type_flags |= eTypeIsInteger;
3962 if (builtin_type->isSignedInteger())
3963 builtin_type_flags |= eTypeIsSigned;
3964 }
3965 else if (builtin_type->isFloatingPoint())
3966 builtin_type_flags |= eTypeIsFloat;
3967 break;
3968 default:
3969 break;
3970 }
3971 return builtin_type_flags;
3972 }
3973
3974 case clang::Type::BlockPointer:
3975 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003976 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003977 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3978
3979 case clang::Type::Complex:
3980 {
3981 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3982 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3983 if (complex_type)
3984 {
3985 clang::QualType complex_element_type (complex_type->getElementType());
3986 if (complex_element_type->isIntegerType())
3987 complex_type_flags |= eTypeIsFloat;
3988 else if (complex_element_type->isFloatingType())
3989 complex_type_flags |= eTypeIsInteger;
3990 }
3991 return complex_type_flags;
3992 }
3993 break;
3994
3995 case clang::Type::ConstantArray:
3996 case clang::Type::DependentSizedArray:
3997 case clang::Type::IncompleteArray:
3998 case clang::Type::VariableArray:
3999 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00004000 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004001 return eTypeHasChildren | eTypeIsArray;
4002
4003 case clang::Type::DependentName: return 0;
4004 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
4005 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
4006 case clang::Type::Decltype: return 0;
4007
4008 case clang::Type::Enum:
4009 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00004010 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004011 return eTypeIsEnumeration | eTypeHasValue;
Enrico Granata36f51e42015-12-18 22:41:25 +00004012
4013 case clang::Type::Auto:
4014 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004015 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004016 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004017 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004018 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004019
4020 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
4021 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
4022 case clang::Type::InjectedClassName: return 0;
4023
4024 case clang::Type::LValueReference:
4025 case clang::Type::RValueReference:
4026 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00004027 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004028 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4029
4030 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4031
4032 case clang::Type::ObjCObjectPointer:
4033 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00004034 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004035 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
4036
4037 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4038 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4039
4040 case clang::Type::Pointer:
4041 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00004042 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004043 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4044
4045 case clang::Type::Record:
4046 if (qual_type->getAsCXXRecordDecl())
4047 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4048 else
4049 return eTypeHasChildren | eTypeIsStructUnion;
4050 break;
4051 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
4052 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
4053 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
4054
4055 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004056 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 +00004057 case clang::Type::TypeOfExpr: return 0;
4058 case clang::Type::TypeOf: return 0;
4059 case clang::Type::UnresolvedUsing: return 0;
4060
4061 case clang::Type::ExtVector:
4062 case clang::Type::Vector:
4063 {
4064 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4065 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
4066 if (vector_type)
4067 {
4068 if (vector_type->isIntegerType())
4069 vector_type_flags |= eTypeIsFloat;
4070 else if (vector_type->isFloatingType())
4071 vector_type_flags |= eTypeIsInteger;
4072 }
4073 return vector_type_flags;
4074 }
4075 default: return 0;
4076 }
4077 return 0;
4078}
4079
4080
4081
4082lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004083ClangASTContext::GetMinimumLanguage (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004084{
4085 if (!type)
4086 return lldb::eLanguageTypeC;
4087
4088 // If the type is a reference, then resolve it to what it refers to first:
4089 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
4090 if (qual_type->isAnyPointerType())
4091 {
4092 if (qual_type->isObjCObjectPointerType())
4093 return lldb::eLanguageTypeObjC;
4094
4095 clang::QualType pointee_type (qual_type->getPointeeType());
4096 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4097 return lldb::eLanguageTypeC_plus_plus;
4098 if (pointee_type->isObjCObjectOrInterfaceType())
4099 return lldb::eLanguageTypeObjC;
4100 if (pointee_type->isObjCClassType())
4101 return lldb::eLanguageTypeObjC;
4102 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4103 return lldb::eLanguageTypeObjC;
4104 }
4105 else
4106 {
4107 if (qual_type->isObjCObjectOrInterfaceType())
4108 return lldb::eLanguageTypeObjC;
4109 if (qual_type->getAsCXXRecordDecl())
4110 return lldb::eLanguageTypeC_plus_plus;
4111 switch (qual_type->getTypeClass())
4112 {
4113 default:
4114 break;
4115 case clang::Type::Builtin:
4116 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4117 {
4118 default:
4119 case clang::BuiltinType::Void:
4120 case clang::BuiltinType::Bool:
4121 case clang::BuiltinType::Char_U:
4122 case clang::BuiltinType::UChar:
4123 case clang::BuiltinType::WChar_U:
4124 case clang::BuiltinType::Char16:
4125 case clang::BuiltinType::Char32:
4126 case clang::BuiltinType::UShort:
4127 case clang::BuiltinType::UInt:
4128 case clang::BuiltinType::ULong:
4129 case clang::BuiltinType::ULongLong:
4130 case clang::BuiltinType::UInt128:
4131 case clang::BuiltinType::Char_S:
4132 case clang::BuiltinType::SChar:
4133 case clang::BuiltinType::WChar_S:
4134 case clang::BuiltinType::Short:
4135 case clang::BuiltinType::Int:
4136 case clang::BuiltinType::Long:
4137 case clang::BuiltinType::LongLong:
4138 case clang::BuiltinType::Int128:
4139 case clang::BuiltinType::Float:
4140 case clang::BuiltinType::Double:
4141 case clang::BuiltinType::LongDouble:
4142 break;
4143
4144 case clang::BuiltinType::NullPtr:
4145 return eLanguageTypeC_plus_plus;
4146
4147 case clang::BuiltinType::ObjCId:
4148 case clang::BuiltinType::ObjCClass:
4149 case clang::BuiltinType::ObjCSel:
4150 return eLanguageTypeObjC;
4151
4152 case clang::BuiltinType::Dependent:
4153 case clang::BuiltinType::Overload:
4154 case clang::BuiltinType::BoundMember:
4155 case clang::BuiltinType::UnknownAny:
4156 break;
4157 }
4158 break;
4159 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004160 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00004161 }
4162 }
4163 return lldb::eLanguageTypeC;
4164}
4165
4166lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004167ClangASTContext::GetTypeClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004168{
4169 if (!type)
4170 return lldb::eTypeClassInvalid;
4171
4172 clang::QualType qual_type(GetQualType(type));
4173
4174 switch (qual_type->getTypeClass())
4175 {
4176 case clang::Type::UnaryTransform: break;
4177 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
4178 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
4179 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
4180 case clang::Type::VariableArray: return lldb::eTypeClassArray;
4181 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
4182 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
4183 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
4184 case clang::Type::ExtVector: return lldb::eTypeClassVector;
4185 case clang::Type::Vector: return lldb::eTypeClassVector;
4186 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
4187 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
4188 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
4189 case clang::Type::Pointer: return lldb::eTypeClassPointer;
4190 case clang::Type::LValueReference: return lldb::eTypeClassReference;
4191 case clang::Type::RValueReference: return lldb::eTypeClassReference;
4192 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
4193 case clang::Type::Complex:
4194 if (qual_type->isComplexType())
4195 return lldb::eTypeClassComplexFloat;
4196 else
4197 return lldb::eTypeClassComplexInteger;
4198 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
4199 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
4200 case clang::Type::Record:
4201 {
4202 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4203 const clang::RecordDecl *record_decl = record_type->getDecl();
4204 if (record_decl->isUnion())
4205 return lldb::eTypeClassUnion;
4206 else if (record_decl->isStruct())
4207 return lldb::eTypeClassStruct;
4208 else
4209 return lldb::eTypeClassClass;
4210 }
4211 break;
4212 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
4213 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
4214 case clang::Type::UnresolvedUsing: break;
4215 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004216 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Enrico Granata36f51e42015-12-18 22:41:25 +00004217 case clang::Type::Auto:
4218 return CompilerType(getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00004219 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004220 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00004221
4222 case clang::Type::Attributed: break;
4223 case clang::Type::TemplateTypeParm: break;
4224 case clang::Type::SubstTemplateTypeParm: break;
4225 case clang::Type::SubstTemplateTypeParmPack:break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004226 case clang::Type::InjectedClassName: break;
4227 case clang::Type::DependentName: break;
4228 case clang::Type::DependentTemplateSpecialization: break;
4229 case clang::Type::PackExpansion: break;
4230
4231 case clang::Type::TypeOfExpr: break;
4232 case clang::Type::TypeOf: break;
4233 case clang::Type::Decltype: break;
4234 case clang::Type::TemplateSpecialization: break;
4235 case clang::Type::Atomic: break;
Pavel Labath484f0a32016-01-12 08:51:28 +00004236 case clang::Type::Pipe: break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004237
4238 // pointer type decayed from an array or function type.
4239 case clang::Type::Decayed: break;
4240 case clang::Type::Adjusted: break;
4241 }
4242 // We don't know hot to display this type...
4243 return lldb::eTypeClassOther;
4244
4245}
4246
4247unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004248ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004249{
4250 if (type)
4251 return GetQualType(type).getQualifiers().getCVRQualifiers();
4252 return 0;
4253}
4254
4255//----------------------------------------------------------------------
4256// Creating related types
4257//----------------------------------------------------------------------
4258
Greg Claytona1e5dc82015-08-11 22:53:00 +00004259CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004260ClangASTContext::GetArrayElementType (lldb::opaque_compiler_type_t type, uint64_t *stride)
Greg Claytond8d4a572015-08-11 21:38:15 +00004261{
4262 if (type)
4263 {
4264 clang::QualType qual_type(GetCanonicalQualType(type));
4265
4266 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4267
4268 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004269 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004270
Greg Claytona1e5dc82015-08-11 22:53:00 +00004271 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00004272
4273 // TODO: the real stride will be >= this value.. find the real one!
4274 if (stride)
4275 *stride = element_type.GetByteSize(nullptr);
4276
4277 return element_type;
4278
4279 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004280 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004281}
4282
Greg Claytona1e5dc82015-08-11 22:53:00 +00004283CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004284ClangASTContext::GetCanonicalType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004285{
4286 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004287 return CompilerType (getASTContext(), GetCanonicalQualType(type));
4288 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004289}
4290
4291static clang::QualType
4292GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
4293{
4294 if (qual_type->isPointerType())
4295 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4296 else
4297 qual_type = qual_type.getUnqualifiedType();
4298 qual_type.removeLocalConst();
4299 qual_type.removeLocalRestrict();
4300 qual_type.removeLocalVolatile();
4301 return qual_type;
4302}
4303
Greg Claytona1e5dc82015-08-11 22:53:00 +00004304CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004305ClangASTContext::GetFullyUnqualifiedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004306{
4307 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004308 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4309 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004310}
4311
4312
4313int
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004314ClangASTContext::GetFunctionArgumentCount (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004315{
4316 if (type)
4317 {
4318 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4319 if (func)
4320 return func->getNumParams();
4321 }
4322 return -1;
4323}
4324
Greg Claytona1e5dc82015-08-11 22:53:00 +00004325CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004326ClangASTContext::GetFunctionArgumentTypeAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004327{
4328 if (type)
4329 {
Greg Clayton8b8874e2015-12-17 00:58:41 +00004330 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004331 if (func)
4332 {
4333 const uint32_t num_args = func->getNumParams();
4334 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004335 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00004336 }
4337 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004338 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004339}
4340
Greg Claytona1e5dc82015-08-11 22:53:00 +00004341CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004342ClangASTContext::GetFunctionReturnType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004343{
4344 if (type)
4345 {
Greg Clayton8b8874e2015-12-17 00:58:41 +00004346 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004347 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4348 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004349 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004350 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004351 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004352}
4353
4354size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004355ClangASTContext::GetNumMemberFunctions (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004356{
4357 size_t num_functions = 0;
4358 if (type)
4359 {
4360 clang::QualType qual_type(GetCanonicalQualType(type));
4361 switch (qual_type->getTypeClass()) {
4362 case clang::Type::Record:
4363 if (GetCompleteQualType (getASTContext(), qual_type))
4364 {
4365 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4366 const clang::RecordDecl *record_decl = record_type->getDecl();
4367 assert(record_decl);
4368 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4369 if (cxx_record_decl)
4370 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4371 }
4372 break;
4373
4374 case clang::Type::ObjCObjectPointer:
4375 if (GetCompleteType(type))
4376 {
4377 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4378 if (objc_class_type)
4379 {
4380 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4381 if (class_interface_decl)
4382 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4383 }
4384 }
4385 break;
4386
4387 case clang::Type::ObjCObject:
4388 case clang::Type::ObjCInterface:
4389 if (GetCompleteType(type))
4390 {
4391 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4392 if (objc_class_type)
4393 {
4394 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4395 if (class_interface_decl)
4396 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4397 }
4398 }
4399 break;
4400
4401
4402 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004403 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Enrico Granata36f51e42015-12-18 22:41:25 +00004404
4405 case clang::Type::Auto:
4406 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004407
4408 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004409 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004410
4411 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004412 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004413
4414 default:
4415 break;
4416 }
4417 }
4418 return num_functions;
4419}
4420
4421TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004422ClangASTContext::GetMemberFunctionAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004423{
Greg Claytonfe689042015-11-10 17:47:04 +00004424 std::string name;
Greg Claytond8d4a572015-08-11 21:38:15 +00004425 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytonfe689042015-11-10 17:47:04 +00004426 CompilerType clang_type;
4427 CompilerDecl clang_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00004428 if (type)
4429 {
4430 clang::QualType qual_type(GetCanonicalQualType(type));
4431 switch (qual_type->getTypeClass()) {
4432 case clang::Type::Record:
4433 if (GetCompleteQualType (getASTContext(), qual_type))
4434 {
4435 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4436 const clang::RecordDecl *record_decl = record_type->getDecl();
4437 assert(record_decl);
4438 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4439 if (cxx_record_decl)
4440 {
4441 auto method_iter = cxx_record_decl->method_begin();
4442 auto method_end = cxx_record_decl->method_end();
4443 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4444 {
4445 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004446 clang::CXXMethodDecl *cxx_method_decl = method_iter->getCanonicalDecl();
4447 if (cxx_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004448 {
Greg Claytonfe689042015-11-10 17:47:04 +00004449 name = cxx_method_decl->getDeclName().getAsString();
4450 if (cxx_method_decl->isStatic())
Greg Claytond8d4a572015-08-11 21:38:15 +00004451 kind = lldb::eMemberFunctionKindStaticMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004452 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004453 kind = lldb::eMemberFunctionKindConstructor;
Greg Claytonfe689042015-11-10 17:47:04 +00004454 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004455 kind = lldb::eMemberFunctionKindDestructor;
4456 else
4457 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004458 clang_type = CompilerType(this, cxx_method_decl->getType().getAsOpaquePtr());
4459 clang_decl = CompilerDecl(this, cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00004460 }
4461 }
4462 }
4463 }
4464 break;
4465
4466 case clang::Type::ObjCObjectPointer:
4467 if (GetCompleteType(type))
4468 {
4469 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4470 if (objc_class_type)
4471 {
4472 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4473 if (class_interface_decl)
4474 {
4475 auto method_iter = class_interface_decl->meth_begin();
4476 auto method_end = class_interface_decl->meth_end();
4477 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4478 {
4479 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004480 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4481 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004482 {
Greg Claytonfe689042015-11-10 17:47:04 +00004483 clang_decl = CompilerDecl(this, objc_method_decl);
4484 name = objc_method_decl->getSelector().getAsString();
4485 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004486 kind = lldb::eMemberFunctionKindStaticMethod;
4487 else
4488 kind = lldb::eMemberFunctionKindInstanceMethod;
4489 }
4490 }
4491 }
4492 }
4493 }
4494 break;
4495
4496 case clang::Type::ObjCObject:
4497 case clang::Type::ObjCInterface:
4498 if (GetCompleteType(type))
4499 {
4500 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4501 if (objc_class_type)
4502 {
4503 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4504 if (class_interface_decl)
4505 {
4506 auto method_iter = class_interface_decl->meth_begin();
4507 auto method_end = class_interface_decl->meth_end();
4508 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4509 {
4510 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004511 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4512 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004513 {
Greg Claytonfe689042015-11-10 17:47:04 +00004514 clang_decl = CompilerDecl(this, objc_method_decl);
4515 name = objc_method_decl->getSelector().getAsString();
4516 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004517 kind = lldb::eMemberFunctionKindStaticMethod;
4518 else
4519 kind = lldb::eMemberFunctionKindInstanceMethod;
4520 }
4521 }
4522 }
4523 }
4524 }
4525 break;
4526
4527 case clang::Type::Typedef:
4528 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
Enrico Granata36f51e42015-12-18 22:41:25 +00004529
4530 case clang::Type::Auto:
4531 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004532
4533 case clang::Type::Elaborated:
4534 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4535
4536 case clang::Type::Paren:
4537 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4538
4539 default:
4540 break;
4541 }
4542 }
4543
4544 if (kind == eMemberFunctionKindUnknown)
4545 return TypeMemberFunctionImpl();
Greg Claytonfe689042015-11-10 17:47:04 +00004546 else
4547 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Claytond8d4a572015-08-11 21:38:15 +00004548}
4549
Greg Claytona1e5dc82015-08-11 22:53:00 +00004550CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004551ClangASTContext::GetNonReferenceType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004552{
4553 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004554 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4555 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004556}
4557
Greg Claytona1e5dc82015-08-11 22:53:00 +00004558CompilerType
4559ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004560 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004561 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004562{
4563 if (type && typedef_name && typedef_name[0])
4564 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004565 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004566 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004567 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004568 clang::ASTContext* clang_ast = ast->getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00004569 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004570
4571 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004572 if (decl_ctx == nullptr)
4573 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004574
Greg Claytond8d4a572015-08-11 21:38:15 +00004575 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4576 decl_ctx,
4577 clang::SourceLocation(),
4578 clang::SourceLocation(),
4579 &clang_ast->Idents.get(typedef_name),
4580 clang_ast->getTrivialTypeSourceInfo(qual_type));
4581
4582 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4583
4584 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004585 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004586 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004587 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004588
4589}
4590
Greg Claytona1e5dc82015-08-11 22:53:00 +00004591CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004592ClangASTContext::GetPointeeType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004593{
4594 if (type)
4595 {
4596 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004597 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004598 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004599 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004600}
4601
Greg Claytona1e5dc82015-08-11 22:53:00 +00004602CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004603ClangASTContext::GetPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004604{
4605 if (type)
4606 {
4607 clang::QualType qual_type (GetQualType(type));
4608
4609 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4610 switch (type_class)
4611 {
4612 case clang::Type::ObjCObject:
4613 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004614 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004615
4616 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004617 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004618 }
4619 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004620 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004621}
4622
Greg Clayton56939cb2015-09-17 22:23:34 +00004623
4624CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004625ClangASTContext::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004626{
4627 if (type)
4628 return CompilerType(this, getASTContext()->getLValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4629 else
4630 return CompilerType();
4631}
4632
4633CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004634ClangASTContext::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004635{
4636 if (type)
4637 return CompilerType(this, getASTContext()->getRValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4638 else
4639 return CompilerType();
4640}
4641
4642CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004643ClangASTContext::AddConstModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004644{
4645 if (type)
4646 {
4647 clang::QualType result(GetQualType(type));
4648 result.addConst();
4649 return CompilerType (this, result.getAsOpaquePtr());
4650 }
4651 return CompilerType();
4652}
4653
4654CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004655ClangASTContext::AddVolatileModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004656{
4657 if (type)
4658 {
4659 clang::QualType result(GetQualType(type));
4660 result.addVolatile();
4661 return CompilerType (this, result.getAsOpaquePtr());
4662 }
4663 return CompilerType();
4664
4665}
4666
4667CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004668ClangASTContext::AddRestrictModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004669{
4670 if (type)
4671 {
4672 clang::QualType result(GetQualType(type));
4673 result.addRestrict();
4674 return CompilerType (this, result.getAsOpaquePtr());
4675 }
4676 return CompilerType();
4677
4678}
4679
4680CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004681ClangASTContext::CreateTypedef (lldb::opaque_compiler_type_t type, const char *typedef_name, const CompilerDeclContext &compiler_decl_ctx)
Greg Clayton56939cb2015-09-17 22:23:34 +00004682{
4683 if (type)
4684 {
4685 clang::ASTContext* clang_ast = getASTContext();
4686 clang::QualType qual_type (GetQualType(type));
4687
4688 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4689 if (decl_ctx == nullptr)
4690 decl_ctx = getASTContext()->getTranslationUnitDecl();
4691
4692 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4693 decl_ctx,
4694 clang::SourceLocation(),
4695 clang::SourceLocation(),
4696 &clang_ast->Idents.get(typedef_name),
4697 clang_ast->getTrivialTypeSourceInfo(qual_type));
4698
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004699 clang::TagDecl *tdecl = nullptr;
4700 if (!qual_type.isNull())
4701 {
4702 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4703 tdecl = rt->getDecl();
4704 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4705 tdecl = et->getDecl();
4706 }
4707
4708 // Check whether this declaration is an anonymous struct, union, or enum, hidden behind a typedef. If so, we
4709 // try to check whether we have a typedef tag to attach to the original record declaration
4710 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4711 tdecl->setTypedefNameForAnonDecl(decl);
4712
Greg Clayton56939cb2015-09-17 22:23:34 +00004713 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4714
4715 // Get a uniqued clang::QualType for the typedef decl type
4716 return CompilerType (this, clang_ast->getTypedefType (decl).getAsOpaquePtr());
4717
4718 }
4719 return CompilerType();
4720
4721}
4722
Greg Claytona1e5dc82015-08-11 22:53:00 +00004723CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004724ClangASTContext::GetTypedefedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004725{
4726 if (type)
4727 {
4728 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4729 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004730 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004731 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004732 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004733}
4734
Greg Claytond8d4a572015-08-11 21:38:15 +00004735
4736//----------------------------------------------------------------------
4737// Create related types using the current type's AST
4738//----------------------------------------------------------------------
4739
Greg Claytona1e5dc82015-08-11 22:53:00 +00004740CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004741ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004742{
Greg Clayton99558cc42015-08-24 23:46:31 +00004743 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004744}
4745//----------------------------------------------------------------------
4746// Exploring the type
4747//----------------------------------------------------------------------
4748
4749uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004750ClangASTContext::GetBitSize (lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Greg Claytond8d4a572015-08-11 21:38:15 +00004751{
4752 if (GetCompleteType (type))
4753 {
4754 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Clayton6e5c1fe2016-03-29 17:36:38 +00004755 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4756 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00004757 {
Greg Clayton6e5c1fe2016-03-29 17:36:38 +00004758 case clang::Type::Record:
4759 if (GetCompleteType(type))
4760 return getASTContext()->getTypeSize(qual_type);
4761 else
4762 return 0;
4763 break;
4764
Greg Claytond8d4a572015-08-11 21:38:15 +00004765 case clang::Type::ObjCInterface:
4766 case clang::Type::ObjCObject:
4767 {
4768 ExecutionContext exe_ctx (exe_scope);
4769 Process *process = exe_ctx.GetProcessPtr();
4770 if (process)
4771 {
4772 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4773 if (objc_runtime)
4774 {
4775 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004776 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004777 return bit_size;
4778 }
4779 }
4780 else
4781 {
4782 static bool g_printed = false;
4783 if (!g_printed)
4784 {
4785 StreamString s;
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00004786 DumpTypeDescription(type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00004787
4788 llvm::outs() << "warning: trying to determine the size of type ";
4789 llvm::outs() << s.GetString() << "\n";
4790 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4791 llvm::outs() << "backtrace:\n";
4792 llvm::sys::PrintStackTrace(llvm::outs());
4793 llvm::outs() << "\n";
4794 g_printed = true;
4795 }
4796 }
4797 }
Jason Molenda62e06812016-02-16 04:14:33 +00004798 LLVM_FALLTHROUGH;
Greg Claytond8d4a572015-08-11 21:38:15 +00004799 default:
4800 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4801 if (bit_size == 0)
4802 {
4803 if (qual_type->isIncompleteArrayType())
4804 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4805 }
4806 if (qual_type->isObjCObjectOrInterfaceType())
4807 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4808 return bit_size;
4809 }
4810 }
4811 return 0;
4812}
4813
4814size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004815ClangASTContext::GetTypeBitAlign (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004816{
4817 if (GetCompleteType(type))
4818 return getASTContext()->getTypeAlign(GetQualType(type));
4819 return 0;
4820}
4821
4822
4823lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004824ClangASTContext::GetEncoding (lldb::opaque_compiler_type_t type, uint64_t &count)
Greg Claytond8d4a572015-08-11 21:38:15 +00004825{
4826 if (!type)
4827 return lldb::eEncodingInvalid;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004828
Greg Claytond8d4a572015-08-11 21:38:15 +00004829 count = 1;
4830 clang::QualType qual_type(GetCanonicalQualType(type));
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004831
Greg Claytond8d4a572015-08-11 21:38:15 +00004832 switch (qual_type->getTypeClass())
4833 {
4834 case clang::Type::UnaryTransform:
4835 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004836
Greg Claytond8d4a572015-08-11 21:38:15 +00004837 case clang::Type::FunctionNoProto:
4838 case clang::Type::FunctionProto:
4839 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004840
Greg Claytond8d4a572015-08-11 21:38:15 +00004841 case clang::Type::IncompleteArray:
4842 case clang::Type::VariableArray:
4843 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004844
Greg Claytond8d4a572015-08-11 21:38:15 +00004845 case clang::Type::ConstantArray:
4846 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004847
Greg Claytond8d4a572015-08-11 21:38:15 +00004848 case clang::Type::ExtVector:
4849 case clang::Type::Vector:
4850 // TODO: Set this to more than one???
4851 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004852
Greg Claytond8d4a572015-08-11 21:38:15 +00004853 case clang::Type::Builtin:
4854 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004855 {
4856 case clang::BuiltinType::Void:
4857 break;
4858
4859 case clang::BuiltinType::Bool:
4860 case clang::BuiltinType::Char_S:
4861 case clang::BuiltinType::SChar:
4862 case clang::BuiltinType::WChar_S:
4863 case clang::BuiltinType::Char16:
4864 case clang::BuiltinType::Char32:
4865 case clang::BuiltinType::Short:
4866 case clang::BuiltinType::Int:
4867 case clang::BuiltinType::Long:
4868 case clang::BuiltinType::LongLong:
4869 case clang::BuiltinType::Int128:
4870 return lldb::eEncodingSint;
4871
4872 case clang::BuiltinType::Char_U:
4873 case clang::BuiltinType::UChar:
4874 case clang::BuiltinType::WChar_U:
4875 case clang::BuiltinType::UShort:
4876 case clang::BuiltinType::UInt:
4877 case clang::BuiltinType::ULong:
4878 case clang::BuiltinType::ULongLong:
4879 case clang::BuiltinType::UInt128:
4880 return lldb::eEncodingUint;
4881
4882 case clang::BuiltinType::Half:
4883 case clang::BuiltinType::Float:
4884 case clang::BuiltinType::Float128:
4885 case clang::BuiltinType::Double:
4886 case clang::BuiltinType::LongDouble:
4887 return lldb::eEncodingIEEE754;
4888
4889 case clang::BuiltinType::ObjCClass:
4890 case clang::BuiltinType::ObjCId:
4891 case clang::BuiltinType::ObjCSel:
4892 return lldb::eEncodingUint;
4893
4894 case clang::BuiltinType::NullPtr:
4895 return lldb::eEncodingUint;
4896
4897 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4898 case clang::BuiltinType::Kind::BoundMember:
4899 case clang::BuiltinType::Kind::BuiltinFn:
4900 case clang::BuiltinType::Kind::Dependent:
4901 case clang::BuiltinType::Kind::OCLClkEvent:
4902 case clang::BuiltinType::Kind::OCLEvent:
4903 case clang::BuiltinType::Kind::OCLImage1dRO:
4904 case clang::BuiltinType::Kind::OCLImage1dWO:
4905 case clang::BuiltinType::Kind::OCLImage1dRW:
4906 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4907 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4908 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4909 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4910 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4911 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4912 case clang::BuiltinType::Kind::OCLImage2dRO:
4913 case clang::BuiltinType::Kind::OCLImage2dWO:
4914 case clang::BuiltinType::Kind::OCLImage2dRW:
4915 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4916 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4917 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4918 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4919 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4920 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4921 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4922 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4923 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4924 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4925 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4926 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4927 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4928 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4929 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4930 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4931 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4932 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4933 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4934 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4935 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4936 case clang::BuiltinType::Kind::OCLImage3dRO:
4937 case clang::BuiltinType::Kind::OCLImage3dWO:
4938 case clang::BuiltinType::Kind::OCLImage3dRW:
4939 case clang::BuiltinType::Kind::OCLQueue:
4940 case clang::BuiltinType::Kind::OCLNDRange:
4941 case clang::BuiltinType::Kind::OCLReserveID:
4942 case clang::BuiltinType::Kind::OCLSampler:
4943 case clang::BuiltinType::Kind::OMPArraySection:
4944 case clang::BuiltinType::Kind::Overload:
4945 case clang::BuiltinType::Kind::PseudoObject:
4946 case clang::BuiltinType::Kind::UnknownAny:
4947 break;
4948 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004949 break;
4950 // All pointer types are represented as unsigned integer encodings.
4951 // We may nee to add a eEncodingPointer if we ever need to know the
4952 // difference
4953 case clang::Type::ObjCObjectPointer:
4954 case clang::Type::BlockPointer:
4955 case clang::Type::Pointer:
4956 case clang::Type::LValueReference:
4957 case clang::Type::RValueReference:
4958 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4959 case clang::Type::Complex:
4960 {
4961 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4962 if (qual_type->isComplexType())
4963 encoding = lldb::eEncodingIEEE754;
4964 else
4965 {
4966 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4967 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004968 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004969 else
4970 encoding = lldb::eEncodingSint;
4971 }
4972 count = 2;
4973 return encoding;
4974 }
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004975
Greg Claytond8d4a572015-08-11 21:38:15 +00004976 case clang::Type::ObjCInterface: break;
4977 case clang::Type::Record: break;
4978 case clang::Type::Enum: return lldb::eEncodingSint;
4979 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004980 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Enrico Granata36f51e42015-12-18 22:41:25 +00004981
4982 case clang::Type::Auto:
4983 return CompilerType(getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetEncoding(count);
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004984
Greg Claytond8d4a572015-08-11 21:38:15 +00004985 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004986 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004987
Greg Claytond8d4a572015-08-11 21:38:15 +00004988 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004989 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00004990
Greg Claytond8d4a572015-08-11 21:38:15 +00004991 case clang::Type::DependentSizedArray:
4992 case clang::Type::DependentSizedExtVector:
4993 case clang::Type::UnresolvedUsing:
4994 case clang::Type::Attributed:
4995 case clang::Type::TemplateTypeParm:
4996 case clang::Type::SubstTemplateTypeParm:
4997 case clang::Type::SubstTemplateTypeParmPack:
Greg Claytond8d4a572015-08-11 21:38:15 +00004998 case clang::Type::InjectedClassName:
4999 case clang::Type::DependentName:
5000 case clang::Type::DependentTemplateSpecialization:
5001 case clang::Type::PackExpansion:
5002 case clang::Type::ObjCObject:
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00005003
Greg Claytond8d4a572015-08-11 21:38:15 +00005004 case clang::Type::TypeOfExpr:
5005 case clang::Type::TypeOf:
5006 case clang::Type::Decltype:
5007 case clang::Type::TemplateSpecialization:
5008 case clang::Type::Atomic:
5009 case clang::Type::Adjusted:
Pavel Labath484f0a32016-01-12 08:51:28 +00005010 case clang::Type::Pipe:
Greg Claytond8d4a572015-08-11 21:38:15 +00005011 break;
Saleem Abdulrasoolf431e682016-05-15 18:18:16 +00005012
Greg Claytond8d4a572015-08-11 21:38:15 +00005013 // pointer type decayed from an array or function type.
5014 case clang::Type::Decayed:
5015 break;
5016 }
5017 count = 0;
5018 return lldb::eEncodingInvalid;
5019}
5020
5021lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005022ClangASTContext::GetFormat (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005023{
5024 if (!type)
5025 return lldb::eFormatDefault;
5026
5027 clang::QualType qual_type(GetCanonicalQualType(type));
5028
5029 switch (qual_type->getTypeClass())
5030 {
5031 case clang::Type::UnaryTransform:
5032 break;
5033
5034 case clang::Type::FunctionNoProto:
5035 case clang::Type::FunctionProto:
5036 break;
5037
5038 case clang::Type::IncompleteArray:
5039 case clang::Type::VariableArray:
5040 break;
5041
5042 case clang::Type::ConstantArray:
5043 return lldb::eFormatVoid; // no value
5044
5045 case clang::Type::ExtVector:
5046 case clang::Type::Vector:
5047 break;
5048
5049 case clang::Type::Builtin:
5050 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5051 {
5052 //default: assert(0 && "Unknown builtin type!");
5053 case clang::BuiltinType::UnknownAny:
5054 case clang::BuiltinType::Void:
5055 case clang::BuiltinType::BoundMember:
5056 break;
5057
5058 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
5059 case clang::BuiltinType::Char_S:
5060 case clang::BuiltinType::SChar:
5061 case clang::BuiltinType::WChar_S:
5062 case clang::BuiltinType::Char_U:
5063 case clang::BuiltinType::UChar:
5064 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
5065 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
5066 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
5067 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
5068 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
5069 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
5070 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
5071 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
5072 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
5073 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
5074 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
5075 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
5076 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
Greg Claytondee40e72015-11-03 23:23:22 +00005077 case clang::BuiltinType::Half:
5078 case clang::BuiltinType::Float:
5079 case clang::BuiltinType::Double:
Greg Claytond8d4a572015-08-11 21:38:15 +00005080 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005081 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00005082 return lldb::eFormatHex;
5083 }
5084 break;
5085 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
5086 case clang::Type::BlockPointer: return lldb::eFormatHex;
5087 case clang::Type::Pointer: return lldb::eFormatHex;
5088 case clang::Type::LValueReference:
5089 case clang::Type::RValueReference: return lldb::eFormatHex;
5090 case clang::Type::MemberPointer: break;
5091 case clang::Type::Complex:
5092 {
5093 if (qual_type->isComplexType())
5094 return lldb::eFormatComplex;
5095 else
5096 return lldb::eFormatComplexInteger;
5097 }
5098 case clang::Type::ObjCInterface: break;
5099 case clang::Type::Record: break;
5100 case clang::Type::Enum: return lldb::eFormatEnum;
5101 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005102 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00005103 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005104 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00005105 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005106 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00005107 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005108 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00005109 case clang::Type::DependentSizedArray:
5110 case clang::Type::DependentSizedExtVector:
5111 case clang::Type::UnresolvedUsing:
5112 case clang::Type::Attributed:
5113 case clang::Type::TemplateTypeParm:
5114 case clang::Type::SubstTemplateTypeParm:
5115 case clang::Type::SubstTemplateTypeParmPack:
5116 case clang::Type::InjectedClassName:
5117 case clang::Type::DependentName:
5118 case clang::Type::DependentTemplateSpecialization:
5119 case clang::Type::PackExpansion:
5120 case clang::Type::ObjCObject:
5121
5122 case clang::Type::TypeOfExpr:
5123 case clang::Type::TypeOf:
5124 case clang::Type::Decltype:
5125 case clang::Type::TemplateSpecialization:
5126 case clang::Type::Atomic:
5127 case clang::Type::Adjusted:
Pavel Labath484f0a32016-01-12 08:51:28 +00005128 case clang::Type::Pipe:
Greg Claytond8d4a572015-08-11 21:38:15 +00005129 break;
5130
5131 // pointer type decayed from an array or function type.
5132 case clang::Type::Decayed:
5133 break;
5134 }
5135 // We don't know hot to display this type...
5136 return lldb::eFormatBytes;
5137}
5138
5139static bool
5140ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
5141{
5142 while (class_interface_decl)
5143 {
5144 if (class_interface_decl->ivar_size() > 0)
5145 return true;
5146
5147 if (check_superclass)
5148 class_interface_decl = class_interface_decl->getSuperClass();
5149 else
5150 break;
5151 }
5152 return false;
5153}
5154
5155uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005156ClangASTContext::GetNumChildren (lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00005157{
5158 if (!type)
5159 return 0;
5160
5161 uint32_t num_children = 0;
5162 clang::QualType qual_type(GetQualType(type));
5163 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5164 switch (type_class)
5165 {
5166 case clang::Type::Builtin:
5167 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5168 {
5169 case clang::BuiltinType::ObjCId: // child is Class
5170 case clang::BuiltinType::ObjCClass: // child is Class
5171 num_children = 1;
5172 break;
5173
5174 default:
5175 break;
5176 }
5177 break;
5178
5179 case clang::Type::Complex: return 0;
5180
5181 case clang::Type::Record:
5182 if (GetCompleteQualType (getASTContext(), qual_type))
5183 {
5184 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5185 const clang::RecordDecl *record_decl = record_type->getDecl();
5186 assert(record_decl);
5187 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5188 if (cxx_record_decl)
5189 {
5190 if (omit_empty_base_classes)
5191 {
5192 // Check each base classes to see if it or any of its
5193 // base classes contain any fields. This can help
5194 // limit the noise in variable views by not having to
5195 // show base classes that contain no members.
5196 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5197 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5198 base_class != base_class_end;
5199 ++base_class)
5200 {
5201 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5202
5203 // Skip empty base classes
5204 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5205 continue;
5206
5207 num_children++;
5208 }
5209 }
5210 else
5211 {
5212 // Include all base classes
5213 num_children += cxx_record_decl->getNumBases();
5214 }
5215
5216 }
5217 clang::RecordDecl::field_iterator field, field_end;
5218 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5219 ++num_children;
5220 }
5221 break;
5222
5223 case clang::Type::ObjCObject:
5224 case clang::Type::ObjCInterface:
5225 if (GetCompleteQualType (getASTContext(), qual_type))
5226 {
5227 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5228 assert (objc_class_type);
5229 if (objc_class_type)
5230 {
5231 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5232
5233 if (class_interface_decl)
5234 {
5235
5236 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5237 if (superclass_interface_decl)
5238 {
5239 if (omit_empty_base_classes)
5240 {
5241 if (ObjCDeclHasIVars (superclass_interface_decl, true))
5242 ++num_children;
5243 }
5244 else
5245 ++num_children;
5246 }
5247
5248 num_children += class_interface_decl->ivar_size();
5249 }
5250 }
5251 }
5252 break;
5253
5254 case clang::Type::ObjCObjectPointer:
5255 {
5256 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5257 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005258 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005259 // If this type points to a simple type, then it has 1 child
5260 if (num_pointee_children == 0)
5261 num_children = 1;
5262 else
5263 num_children = num_pointee_children;
5264 }
5265 break;
5266
5267 case clang::Type::Vector:
5268 case clang::Type::ExtVector:
5269 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5270 break;
5271
5272 case clang::Type::ConstantArray:
5273 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
5274 break;
5275
5276 case clang::Type::Pointer:
5277 {
5278 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5279 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00005280 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005281 if (num_pointee_children == 0)
5282 {
5283 // We have a pointer to a pointee type that claims it has no children.
5284 // We will want to look at
5285 num_children = GetNumPointeeChildren (pointee_type);
5286 }
5287 else
5288 num_children = num_pointee_children;
5289 }
5290 break;
5291
5292 case clang::Type::LValueReference:
5293 case clang::Type::RValueReference:
5294 {
5295 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5296 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005297 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005298 // If this type points to a simple type, then it has 1 child
5299 if (num_pointee_children == 0)
5300 num_children = 1;
5301 else
5302 num_children = num_pointee_children;
5303 }
5304 break;
5305
5306
5307 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005308 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005309 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005310
5311 case clang::Type::Auto:
5312 num_children = CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumChildren (omit_empty_base_classes);
5313 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00005314
5315 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005316 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005317 break;
5318
5319 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005320 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005321 break;
5322 default:
5323 break;
5324 }
5325 return num_children;
5326}
5327
Greg Clayton56939cb2015-09-17 22:23:34 +00005328CompilerType
5329ClangASTContext::GetBuiltinTypeByName (const ConstString &name)
5330{
Zachary Turner42dff792016-04-15 00:21:26 +00005331 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005332}
5333
Greg Claytond8d4a572015-08-11 21:38:15 +00005334lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005335ClangASTContext::GetBasicTypeEnumeration (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005336{
5337 if (type)
5338 {
5339 clang::QualType qual_type(GetQualType(type));
5340 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5341 if (type_class == clang::Type::Builtin)
5342 {
5343 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5344 {
5345 case clang::BuiltinType::Void: return eBasicTypeVoid;
5346 case clang::BuiltinType::Bool: return eBasicTypeBool;
5347 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
5348 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
5349 case clang::BuiltinType::Char16: return eBasicTypeChar16;
5350 case clang::BuiltinType::Char32: return eBasicTypeChar32;
5351 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
5352 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
5353 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
5354 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
5355 case clang::BuiltinType::Short: return eBasicTypeShort;
5356 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
5357 case clang::BuiltinType::Int: return eBasicTypeInt;
5358 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
5359 case clang::BuiltinType::Long: return eBasicTypeLong;
5360 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
5361 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
5362 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
5363 case clang::BuiltinType::Int128: return eBasicTypeInt128;
5364 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
5365
5366 case clang::BuiltinType::Half: return eBasicTypeHalf;
5367 case clang::BuiltinType::Float: return eBasicTypeFloat;
5368 case clang::BuiltinType::Double: return eBasicTypeDouble;
5369 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
5370
5371 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
5372 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
5373 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
5374 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005375 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00005376 return eBasicTypeOther;
5377 }
5378 }
5379 }
5380 return eBasicTypeInvalid;
5381}
5382
Greg Clayton99558cc42015-08-24 23:46:31 +00005383void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005384ClangASTContext::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 +00005385{
5386 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5387 if (enum_type)
5388 {
5389 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5390 if (enum_decl)
5391 {
5392 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
5393
5394 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5395 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5396 {
5397 ConstString name(enum_pos->getNameAsString().c_str());
5398 if (!callback (integer_type, name, enum_pos->getInitVal()))
5399 break;
5400 }
5401 }
5402 }
5403}
5404
Greg Claytond8d4a572015-08-11 21:38:15 +00005405
5406#pragma mark Aggregate Types
5407
5408uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005409ClangASTContext::GetNumFields (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005410{
5411 if (!type)
5412 return 0;
5413
5414 uint32_t count = 0;
5415 clang::QualType qual_type(GetCanonicalQualType(type));
5416 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5417 switch (type_class)
5418 {
5419 case clang::Type::Record:
5420 if (GetCompleteType(type))
5421 {
5422 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5423 if (record_type)
5424 {
5425 clang::RecordDecl *record_decl = record_type->getDecl();
5426 if (record_decl)
5427 {
5428 uint32_t field_idx = 0;
5429 clang::RecordDecl::field_iterator field, field_end;
5430 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5431 ++field_idx;
5432 count = field_idx;
5433 }
5434 }
5435 }
5436 break;
5437
5438 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005439 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005440 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005441
5442 case clang::Type::Auto:
5443 count = CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumFields();
5444 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00005445
5446 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005447 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005448 break;
5449
5450 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005451 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005452 break;
5453
5454 case clang::Type::ObjCObjectPointer:
5455 if (GetCompleteType(type))
5456 {
5457 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5458 if (objc_class_type)
5459 {
5460 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5461
5462 if (class_interface_decl)
5463 count = class_interface_decl->ivar_size();
5464 }
5465 }
5466 break;
5467
5468 case clang::Type::ObjCObject:
5469 case clang::Type::ObjCInterface:
5470 if (GetCompleteType(type))
5471 {
5472 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5473 if (objc_class_type)
5474 {
5475 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5476
5477 if (class_interface_decl)
5478 count = class_interface_decl->ivar_size();
5479 }
5480 }
5481 break;
5482
5483 default:
5484 break;
5485 }
5486 return count;
5487}
5488
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005489static lldb::opaque_compiler_type_t
Greg Claytond8d4a572015-08-11 21:38:15 +00005490GetObjCFieldAtIndex (clang::ASTContext *ast,
5491 clang::ObjCInterfaceDecl *class_interface_decl,
5492 size_t idx,
5493 std::string& name,
5494 uint64_t *bit_offset_ptr,
5495 uint32_t *bitfield_bit_size_ptr,
5496 bool *is_bitfield_ptr)
5497{
5498 if (class_interface_decl)
5499 {
5500 if (idx < (class_interface_decl->ivar_size()))
5501 {
5502 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5503 uint32_t ivar_idx = 0;
5504
5505 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5506 {
5507 if (ivar_idx == idx)
5508 {
5509 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5510
5511 clang::QualType ivar_qual_type(ivar_decl->getType());
5512
5513 name.assign(ivar_decl->getNameAsString());
5514
5515 if (bit_offset_ptr)
5516 {
5517 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5518 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5519 }
5520
5521 const bool is_bitfield = ivar_pos->isBitField();
5522
5523 if (bitfield_bit_size_ptr)
5524 {
5525 *bitfield_bit_size_ptr = 0;
5526
5527 if (is_bitfield && ast)
5528 {
5529 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5530 llvm::APSInt bitfield_apsint;
5531 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5532 {
5533 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5534 }
5535 }
5536 }
5537 if (is_bitfield_ptr)
5538 *is_bitfield_ptr = is_bitfield;
5539
5540 return ivar_qual_type.getAsOpaquePtr();
5541 }
5542 }
5543 }
5544 }
5545 return nullptr;
5546}
5547
Greg Claytona1e5dc82015-08-11 22:53:00 +00005548CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005549ClangASTContext::GetFieldAtIndex (lldb::opaque_compiler_type_t type, size_t idx,
Greg Claytond8d4a572015-08-11 21:38:15 +00005550 std::string& name,
5551 uint64_t *bit_offset_ptr,
5552 uint32_t *bitfield_bit_size_ptr,
5553 bool *is_bitfield_ptr)
5554{
5555 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005556 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005557
5558 clang::QualType qual_type(GetCanonicalQualType(type));
5559 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5560 switch (type_class)
5561 {
5562 case clang::Type::Record:
5563 if (GetCompleteType(type))
5564 {
5565 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5566 const clang::RecordDecl *record_decl = record_type->getDecl();
5567 uint32_t field_idx = 0;
5568 clang::RecordDecl::field_iterator field, field_end;
5569 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5570 {
5571 if (idx == field_idx)
5572 {
5573 // Print the member type if requested
5574 // Print the member name and equal sign
5575 name.assign(field->getNameAsString());
5576
5577 // Figure out the type byte size (field_type_info.first) and
5578 // alignment (field_type_info.second) from the AST context.
5579 if (bit_offset_ptr)
5580 {
5581 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5582 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5583 }
5584
5585 const bool is_bitfield = field->isBitField();
5586
5587 if (bitfield_bit_size_ptr)
5588 {
5589 *bitfield_bit_size_ptr = 0;
5590
5591 if (is_bitfield)
5592 {
5593 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5594 llvm::APSInt bitfield_apsint;
5595 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5596 {
5597 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5598 }
5599 }
5600 }
5601 if (is_bitfield_ptr)
5602 *is_bitfield_ptr = is_bitfield;
5603
Greg Claytona1e5dc82015-08-11 22:53:00 +00005604 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005605 }
5606 }
5607 }
5608 break;
5609
5610 case clang::Type::ObjCObjectPointer:
5611 if (GetCompleteType(type))
5612 {
5613 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5614 if (objc_class_type)
5615 {
5616 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005617 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 +00005618 }
5619 }
5620 break;
5621
5622 case clang::Type::ObjCObject:
5623 case clang::Type::ObjCInterface:
5624 if (GetCompleteType(type))
5625 {
5626 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5627 assert (objc_class_type);
5628 if (objc_class_type)
5629 {
5630 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005631 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 +00005632 }
5633 }
5634 break;
5635
5636
5637 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005638 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005639 GetFieldAtIndex (idx,
5640 name,
5641 bit_offset_ptr,
5642 bitfield_bit_size_ptr,
5643 is_bitfield_ptr);
5644
Enrico Granata36f51e42015-12-18 22:41:25 +00005645 case clang::Type::Auto:
5646 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).
5647 GetFieldAtIndex (idx,
5648 name,
5649 bit_offset_ptr,
5650 bitfield_bit_size_ptr,
5651 is_bitfield_ptr);
5652
Greg Claytond8d4a572015-08-11 21:38:15 +00005653 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005654 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005655 GetFieldAtIndex (idx,
5656 name,
5657 bit_offset_ptr,
5658 bitfield_bit_size_ptr,
5659 is_bitfield_ptr);
5660
5661 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005662 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005663 GetFieldAtIndex (idx,
5664 name,
5665 bit_offset_ptr,
5666 bitfield_bit_size_ptr,
5667 is_bitfield_ptr);
5668
5669 default:
5670 break;
5671 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005672 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005673}
5674
Greg Clayton99558cc42015-08-24 23:46:31 +00005675uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005676ClangASTContext::GetNumDirectBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005677{
5678 uint32_t count = 0;
5679 clang::QualType qual_type(GetCanonicalQualType(type));
5680 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5681 switch (type_class)
5682 {
5683 case clang::Type::Record:
5684 if (GetCompleteType(type))
5685 {
5686 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5687 if (cxx_record_decl)
5688 count = cxx_record_decl->getNumBases();
5689 }
5690 break;
5691
5692 case clang::Type::ObjCObjectPointer:
5693 count = GetPointeeType(type).GetNumDirectBaseClasses();
5694 break;
5695
5696 case clang::Type::ObjCObject:
5697 if (GetCompleteType(type))
5698 {
5699 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5700 if (objc_class_type)
5701 {
5702 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5703
5704 if (class_interface_decl && class_interface_decl->getSuperClass())
5705 count = 1;
5706 }
5707 }
5708 break;
5709 case clang::Type::ObjCInterface:
5710 if (GetCompleteType(type))
5711 {
5712 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5713 if (objc_interface_type)
5714 {
5715 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5716
5717 if (class_interface_decl && class_interface_decl->getSuperClass())
5718 count = 1;
5719 }
5720 }
5721 break;
5722
5723
5724 case clang::Type::Typedef:
5725 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5726 break;
5727
Enrico Granata36f51e42015-12-18 22:41:25 +00005728 case clang::Type::Auto:
5729 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
5730 break;
5731
Greg Clayton99558cc42015-08-24 23:46:31 +00005732 case clang::Type::Elaborated:
5733 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5734 break;
5735
5736 case clang::Type::Paren:
5737 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5738
5739 default:
5740 break;
5741 }
5742 return count;
5743
5744}
5745
5746uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005747ClangASTContext::GetNumVirtualBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005748{
5749 uint32_t count = 0;
5750 clang::QualType qual_type(GetCanonicalQualType(type));
5751 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5752 switch (type_class)
5753 {
5754 case clang::Type::Record:
5755 if (GetCompleteType(type))
5756 {
5757 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5758 if (cxx_record_decl)
5759 count = cxx_record_decl->getNumVBases();
5760 }
5761 break;
5762
5763 case clang::Type::Typedef:
5764 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5765 break;
5766
Enrico Granata36f51e42015-12-18 22:41:25 +00005767 case clang::Type::Auto:
5768 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
5769 break;
5770
Greg Clayton99558cc42015-08-24 23:46:31 +00005771 case clang::Type::Elaborated:
5772 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5773 break;
5774
5775 case clang::Type::Paren:
5776 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5777 break;
5778
5779 default:
5780 break;
5781 }
5782 return count;
5783
5784}
5785
5786CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005787ClangASTContext::GetDirectBaseClassAtIndex (lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00005788{
5789 clang::QualType qual_type(GetCanonicalQualType(type));
5790 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5791 switch (type_class)
5792 {
5793 case clang::Type::Record:
5794 if (GetCompleteType(type))
5795 {
5796 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5797 if (cxx_record_decl)
5798 {
5799 uint32_t curr_idx = 0;
5800 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5801 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5802 base_class != base_class_end;
5803 ++base_class, ++curr_idx)
5804 {
5805 if (curr_idx == idx)
5806 {
5807 if (bit_offset_ptr)
5808 {
5809 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5810 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5811 if (base_class->isVirtual())
5812 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5813 else
5814 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5815 }
5816 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5817 }
5818 }
5819 }
5820 }
5821 break;
5822
5823 case clang::Type::ObjCObjectPointer:
5824 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5825
5826 case clang::Type::ObjCObject:
5827 if (idx == 0 && GetCompleteType(type))
5828 {
5829 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5830 if (objc_class_type)
5831 {
5832 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5833
5834 if (class_interface_decl)
5835 {
5836 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5837 if (superclass_interface_decl)
5838 {
5839 if (bit_offset_ptr)
5840 *bit_offset_ptr = 0;
5841 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5842 }
5843 }
5844 }
5845 }
5846 break;
5847 case clang::Type::ObjCInterface:
5848 if (idx == 0 && GetCompleteType(type))
5849 {
5850 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5851 if (objc_interface_type)
5852 {
5853 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5854
5855 if (class_interface_decl)
5856 {
5857 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5858 if (superclass_interface_decl)
5859 {
5860 if (bit_offset_ptr)
5861 *bit_offset_ptr = 0;
5862 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5863 }
5864 }
5865 }
5866 }
5867 break;
5868
5869
5870 case clang::Type::Typedef:
5871 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5872
Enrico Granata36f51e42015-12-18 22:41:25 +00005873 case clang::Type::Auto:
5874 return GetDirectBaseClassAtIndex (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5875
Greg Clayton99558cc42015-08-24 23:46:31 +00005876 case clang::Type::Elaborated:
5877 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5878
5879 case clang::Type::Paren:
5880 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5881
5882 default:
5883 break;
5884 }
5885 return CompilerType();
5886}
5887
5888CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005889ClangASTContext::GetVirtualBaseClassAtIndex (lldb::opaque_compiler_type_t type,
Greg Clayton99558cc42015-08-24 23:46:31 +00005890 size_t idx,
5891 uint32_t *bit_offset_ptr)
5892{
5893 clang::QualType qual_type(GetCanonicalQualType(type));
5894 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5895 switch (type_class)
5896 {
5897 case clang::Type::Record:
5898 if (GetCompleteType(type))
5899 {
5900 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5901 if (cxx_record_decl)
5902 {
5903 uint32_t curr_idx = 0;
5904 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5905 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5906 base_class != base_class_end;
5907 ++base_class, ++curr_idx)
5908 {
5909 if (curr_idx == idx)
5910 {
5911 if (bit_offset_ptr)
5912 {
5913 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5914 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5915 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5916
5917 }
5918 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5919 }
5920 }
5921 }
5922 }
5923 break;
5924
5925 case clang::Type::Typedef:
5926 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00005927
5928 case clang::Type::Auto:
5929 return GetVirtualBaseClassAtIndex (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5930
Greg Clayton99558cc42015-08-24 23:46:31 +00005931 case clang::Type::Elaborated:
5932 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5933
5934 case clang::Type::Paren:
Zachary Turner9d8a97e2016-04-01 23:20:35 +00005935 return GetVirtualBaseClassAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx,
5936 bit_offset_ptr);
Greg Clayton99558cc42015-08-24 23:46:31 +00005937
5938 default:
5939 break;
5940 }
5941 return CompilerType();
5942
5943}
5944
Greg Claytond8d4a572015-08-11 21:38:15 +00005945// If a pointer to a pointee type (the clang_type arg) says that it has no
5946// children, then we either need to trust it, or override it and return a
5947// different result. For example, an "int *" has one child that is an integer,
5948// but a function pointer doesn't have any children. Likewise if a Record type
5949// claims it has no children, then there really is nothing to show.
5950uint32_t
5951ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5952{
5953 if (type.isNull())
5954 return 0;
5955
5956 clang::QualType qual_type(type.getCanonicalType());
5957 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5958 switch (type_class)
5959 {
5960 case clang::Type::Builtin:
5961 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5962 {
5963 case clang::BuiltinType::UnknownAny:
5964 case clang::BuiltinType::Void:
5965 case clang::BuiltinType::NullPtr:
5966 case clang::BuiltinType::OCLEvent:
Tamas Berghammerb905e9d2016-04-08 14:31:13 +00005967 case clang::BuiltinType::OCLImage1dRO:
5968 case clang::BuiltinType::OCLImage1dWO:
5969 case clang::BuiltinType::OCLImage1dRW:
5970 case clang::BuiltinType::OCLImage1dArrayRO:
5971 case clang::BuiltinType::OCLImage1dArrayWO:
5972 case clang::BuiltinType::OCLImage1dArrayRW:
5973 case clang::BuiltinType::OCLImage1dBufferRO:
5974 case clang::BuiltinType::OCLImage1dBufferWO:
5975 case clang::BuiltinType::OCLImage1dBufferRW:
5976 case clang::BuiltinType::OCLImage2dRO:
5977 case clang::BuiltinType::OCLImage2dWO:
5978 case clang::BuiltinType::OCLImage2dRW:
5979 case clang::BuiltinType::OCLImage2dArrayRO:
5980 case clang::BuiltinType::OCLImage2dArrayWO:
5981 case clang::BuiltinType::OCLImage2dArrayRW:
5982 case clang::BuiltinType::OCLImage3dRO:
5983 case clang::BuiltinType::OCLImage3dWO:
5984 case clang::BuiltinType::OCLImage3dRW:
Greg Claytond8d4a572015-08-11 21:38:15 +00005985 case clang::BuiltinType::OCLSampler:
5986 return 0;
5987 case clang::BuiltinType::Bool:
5988 case clang::BuiltinType::Char_U:
5989 case clang::BuiltinType::UChar:
5990 case clang::BuiltinType::WChar_U:
5991 case clang::BuiltinType::Char16:
5992 case clang::BuiltinType::Char32:
5993 case clang::BuiltinType::UShort:
5994 case clang::BuiltinType::UInt:
5995 case clang::BuiltinType::ULong:
5996 case clang::BuiltinType::ULongLong:
5997 case clang::BuiltinType::UInt128:
5998 case clang::BuiltinType::Char_S:
5999 case clang::BuiltinType::SChar:
6000 case clang::BuiltinType::WChar_S:
6001 case clang::BuiltinType::Short:
6002 case clang::BuiltinType::Int:
6003 case clang::BuiltinType::Long:
6004 case clang::BuiltinType::LongLong:
6005 case clang::BuiltinType::Int128:
6006 case clang::BuiltinType::Float:
6007 case clang::BuiltinType::Double:
6008 case clang::BuiltinType::LongDouble:
6009 case clang::BuiltinType::Dependent:
6010 case clang::BuiltinType::Overload:
6011 case clang::BuiltinType::ObjCId:
6012 case clang::BuiltinType::ObjCClass:
6013 case clang::BuiltinType::ObjCSel:
6014 case clang::BuiltinType::BoundMember:
6015 case clang::BuiltinType::Half:
6016 case clang::BuiltinType::ARCUnbridgedCast:
6017 case clang::BuiltinType::PseudoObject:
6018 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00006019 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00006020 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00006021 default:
6022 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006023 }
6024 break;
6025
6026 case clang::Type::Complex: return 1;
6027 case clang::Type::Pointer: return 1;
6028 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
6029 case clang::Type::LValueReference: return 1;
6030 case clang::Type::RValueReference: return 1;
6031 case clang::Type::MemberPointer: return 0;
6032 case clang::Type::ConstantArray: return 0;
6033 case clang::Type::IncompleteArray: return 0;
6034 case clang::Type::VariableArray: return 0;
6035 case clang::Type::DependentSizedArray: return 0;
6036 case clang::Type::DependentSizedExtVector: return 0;
6037 case clang::Type::Vector: return 0;
6038 case clang::Type::ExtVector: return 0;
6039 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
6040 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
6041 case clang::Type::UnresolvedUsing: return 0;
6042 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
6043 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
Enrico Granata36f51e42015-12-18 22:41:25 +00006044 case clang::Type::Auto: return GetNumPointeeChildren (llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006045 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6046 case clang::Type::TypeOfExpr: return 0;
6047 case clang::Type::TypeOf: return 0;
6048 case clang::Type::Decltype: return 0;
6049 case clang::Type::Record: return 0;
6050 case clang::Type::Enum: return 1;
6051 case clang::Type::TemplateTypeParm: return 1;
6052 case clang::Type::SubstTemplateTypeParm: return 1;
6053 case clang::Type::TemplateSpecialization: return 1;
6054 case clang::Type::InjectedClassName: return 0;
6055 case clang::Type::DependentName: return 1;
6056 case clang::Type::DependentTemplateSpecialization: return 1;
6057 case clang::Type::ObjCObject: return 0;
6058 case clang::Type::ObjCInterface: return 0;
6059 case clang::Type::ObjCObjectPointer: return 1;
6060 default:
6061 break;
6062 }
6063 return 0;
6064}
6065
6066
Greg Claytona1e5dc82015-08-11 22:53:00 +00006067CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006068ClangASTContext::GetChildCompilerTypeAtIndex (lldb::opaque_compiler_type_t type,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006069 ExecutionContext *exe_ctx,
6070 size_t idx,
6071 bool transparent_pointers,
6072 bool omit_empty_base_classes,
6073 bool ignore_array_bounds,
6074 std::string& child_name,
6075 uint32_t &child_byte_size,
6076 int32_t &child_byte_offset,
6077 uint32_t &child_bitfield_bit_size,
6078 uint32_t &child_bitfield_bit_offset,
6079 bool &child_is_base_class,
6080 bool &child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006081 ValueObject *valobj,
6082 uint64_t &language_flags)
Greg Claytond8d4a572015-08-11 21:38:15 +00006083{
6084 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006085 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006086
6087 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6088 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
6089 child_bitfield_bit_size = 0;
6090 child_bitfield_bit_offset = 0;
6091 child_is_base_class = false;
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006092 language_flags = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006093
6094 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
6095 uint32_t bit_offset;
6096 switch (parent_type_class)
6097 {
6098 case clang::Type::Builtin:
6099 if (idx_is_valid)
6100 {
6101 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
6102 {
6103 case clang::BuiltinType::ObjCId:
6104 case clang::BuiltinType::ObjCClass:
6105 child_name = "isa";
6106 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006107 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00006108
6109 default:
6110 break;
6111 }
6112 }
6113 break;
6114
6115 case clang::Type::Record:
6116 if (idx_is_valid && GetCompleteType(type))
6117 {
6118 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6119 const clang::RecordDecl *record_decl = record_type->getDecl();
6120 assert(record_decl);
6121 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
6122 uint32_t child_idx = 0;
6123
6124 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6125 if (cxx_record_decl)
6126 {
6127 // We might have base classes to print out first
6128 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6129 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6130 base_class != base_class_end;
6131 ++base_class)
6132 {
6133 const clang::CXXRecordDecl *base_class_decl = nullptr;
6134
6135 // Skip empty base classes
6136 if (omit_empty_base_classes)
6137 {
6138 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6139 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6140 continue;
6141 }
6142
6143 if (idx == child_idx)
6144 {
6145 if (base_class_decl == nullptr)
6146 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6147
6148
6149 if (base_class->isVirtual())
6150 {
6151 bool handled = false;
6152 if (valobj)
6153 {
6154 Error err;
6155 AddressType addr_type = eAddressTypeInvalid;
6156 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
6157
6158 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
6159 {
6160
6161 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
6162 Process *process = exe_ctx.GetProcessPtr();
6163 if (process)
6164 {
6165 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
6166 if (vtable_ctx)
6167 {
6168 if (vtable_ctx->isMicrosoft())
6169 {
6170 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
6171
6172 if (vtable_ptr_addr)
6173 {
6174 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
6175
6176 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
6177 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
6178 {
6179 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
6180 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
6181 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
6182 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
6183 if (base_offset != UINT32_MAX)
6184 {
6185 handled = true;
6186 bit_offset = base_offset * 8;
6187 }
6188 }
6189 }
6190 }
6191 else
6192 {
6193 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
6194 if (vtable_ptr_addr)
6195 {
6196 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
6197 if (vtable_ptr != LLDB_INVALID_ADDRESS)
6198 {
6199 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
6200 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
Ulrich Weigand0501eeb2016-04-14 14:33:47 +00006201 const uint32_t base_offset_size = process->GetAddressByteSize();
6202 const uint64_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, base_offset_size, UINT32_MAX, err);
6203 if (base_offset < UINT32_MAX)
Greg Claytond8d4a572015-08-11 21:38:15 +00006204 {
6205 handled = true;
6206 bit_offset = base_offset * 8;
6207 }
6208 }
6209 }
6210 }
6211 }
6212 }
6213 }
6214
6215 }
6216 if (!handled)
6217 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
6218 }
6219 else
6220 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
6221
6222 // Base classes should be a multiple of 8 bits in size
6223 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006224 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006225 child_name = base_class_clang_type.GetTypeName().AsCString("");
6226 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6227
6228 // Base classes bit sizes should be a multiple of 8 bits in size
6229 assert (base_class_clang_type_bit_size % 8 == 0);
6230 child_byte_size = base_class_clang_type_bit_size / 8;
6231 child_is_base_class = true;
6232 return base_class_clang_type;
6233 }
6234 // We don't increment the child index in the for loop since we might
6235 // be skipping empty base classes
6236 ++child_idx;
6237 }
6238 }
6239 // Make sure index is in range...
6240 uint32_t field_idx = 0;
6241 clang::RecordDecl::field_iterator field, field_end;
6242 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
6243 {
6244 if (idx == child_idx)
6245 {
6246 // Print the member type if requested
6247 // Print the member name and equal sign
6248 child_name.assign(field->getNameAsString().c_str());
6249
6250 // Figure out the type byte size (field_type_info.first) and
6251 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00006252 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006253 assert(field_idx < record_layout.getFieldCount());
6254 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6255
6256 // Figure out the field offset within the current struct/union/class type
6257 bit_offset = record_layout.getFieldOffset (field_idx);
6258 child_byte_offset = bit_offset / 8;
6259 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
6260 child_bitfield_bit_offset = bit_offset % 8;
6261
6262 return field_clang_type;
6263 }
6264 }
6265 }
6266 break;
6267
6268 case clang::Type::ObjCObject:
6269 case clang::Type::ObjCInterface:
6270 if (idx_is_valid && GetCompleteType(type))
6271 {
6272 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6273 assert (objc_class_type);
6274 if (objc_class_type)
6275 {
6276 uint32_t child_idx = 0;
6277 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6278
6279 if (class_interface_decl)
6280 {
6281
6282 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6283 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6284 if (superclass_interface_decl)
6285 {
6286 if (omit_empty_base_classes)
6287 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006288 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006289 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
6290 {
6291 if (idx == 0)
6292 {
6293 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
6294
6295
6296 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
6297
6298 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6299
6300 child_byte_size = ivar_type_info.Width / 8;
6301 child_byte_offset = 0;
6302 child_is_base_class = true;
6303
Greg Claytona1e5dc82015-08-11 22:53:00 +00006304 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006305 }
6306
6307 ++child_idx;
6308 }
6309 }
6310 else
6311 ++child_idx;
6312 }
6313
6314 const uint32_t superclass_idx = child_idx;
6315
6316 if (idx < (child_idx + class_interface_decl->ivar_size()))
6317 {
6318 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6319
6320 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
6321 {
6322 if (child_idx == idx)
6323 {
6324 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6325
6326 clang::QualType ivar_qual_type(ivar_decl->getType());
6327
6328 child_name.assign(ivar_decl->getNameAsString().c_str());
6329
6330 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6331
6332 child_byte_size = ivar_type_info.Width / 8;
6333
6334 // Figure out the field offset within the current struct/union/class type
6335 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
6336 // that doesn't account for the space taken up by unbacked properties, or from
6337 // the changing size of base classes that are newer than this class.
6338 // So if we have a process around that we can ask about this object, do so.
6339 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6340 Process *process = nullptr;
6341 if (exe_ctx)
6342 process = exe_ctx->GetProcessPtr();
6343 if (process)
6344 {
6345 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
6346 if (objc_runtime != nullptr)
6347 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006348 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006349 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
6350 }
6351 }
6352
6353 // Setting this to UINT32_MAX to make sure we don't compute it twice...
6354 bit_offset = UINT32_MAX;
6355
6356 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
6357 {
6358 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
6359 child_byte_offset = bit_offset / 8;
6360 }
6361
6362 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
6363 // of a bitfield within its containing object. So regardless of where we get the byte
6364 // offset from, we still need to get the bit offset for bitfields from the layout.
6365
6366 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
6367 {
6368 if (bit_offset == UINT32_MAX)
6369 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
6370
6371 child_bitfield_bit_offset = bit_offset % 8;
6372 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006373 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006374 }
6375 ++child_idx;
6376 }
6377 }
6378 }
6379 }
6380 }
6381 break;
6382
6383 case clang::Type::ObjCObjectPointer:
6384 if (idx_is_valid)
6385 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006386 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006387
6388 if (transparent_pointers && pointee_clang_type.IsAggregateType())
6389 {
6390 child_is_deref_of_parent = false;
6391 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006392 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6393 idx,
6394 transparent_pointers,
6395 omit_empty_base_classes,
6396 ignore_array_bounds,
6397 child_name,
6398 child_byte_size,
6399 child_byte_offset,
6400 child_bitfield_bit_size,
6401 child_bitfield_bit_offset,
6402 child_is_base_class,
6403 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006404 valobj,
6405 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006406 }
6407 else
6408 {
6409 child_is_deref_of_parent = true;
6410 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6411 if (parent_name)
6412 {
6413 child_name.assign(1, '*');
6414 child_name += parent_name;
6415 }
6416
6417 // We have a pointer to an simple type
6418 if (idx == 0 && pointee_clang_type.GetCompleteType())
6419 {
6420 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6421 child_byte_offset = 0;
6422 return pointee_clang_type;
6423 }
6424 }
6425 }
6426 break;
6427
6428 case clang::Type::Vector:
6429 case clang::Type::ExtVector:
6430 if (idx_is_valid)
6431 {
6432 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6433 if (array)
6434 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006435 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006436 if (element_type.GetCompleteType())
6437 {
6438 char element_name[64];
6439 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6440 child_name.assign(element_name);
6441 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6442 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6443 return element_type;
6444 }
6445 }
6446 }
6447 break;
6448
6449 case clang::Type::ConstantArray:
6450 case clang::Type::IncompleteArray:
6451 if (ignore_array_bounds || idx_is_valid)
6452 {
6453 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6454 if (array)
6455 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006456 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006457 if (element_type.GetCompleteType())
6458 {
6459 char element_name[64];
6460 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6461 child_name.assign(element_name);
6462 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6463 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6464 return element_type;
6465 }
6466 }
6467 }
6468 break;
6469
6470
6471 case clang::Type::Pointer:
6472 if (idx_is_valid)
6473 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006474 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006475
6476 // Don't dereference "void *" pointers
6477 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00006478 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006479
6480 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6481 {
6482 child_is_deref_of_parent = false;
6483 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006484 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6485 idx,
6486 transparent_pointers,
6487 omit_empty_base_classes,
6488 ignore_array_bounds,
6489 child_name,
6490 child_byte_size,
6491 child_byte_offset,
6492 child_bitfield_bit_size,
6493 child_bitfield_bit_offset,
6494 child_is_base_class,
6495 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006496 valobj,
6497 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006498 }
6499 else
6500 {
6501 child_is_deref_of_parent = true;
6502
6503 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6504 if (parent_name)
6505 {
6506 child_name.assign(1, '*');
6507 child_name += parent_name;
6508 }
6509
6510 // We have a pointer to an simple type
6511 if (idx == 0)
6512 {
6513 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6514 child_byte_offset = 0;
6515 return pointee_clang_type;
6516 }
6517 }
6518 }
6519 break;
6520
6521 case clang::Type::LValueReference:
6522 case clang::Type::RValueReference:
6523 if (idx_is_valid)
6524 {
6525 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006526 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006527 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6528 {
6529 child_is_deref_of_parent = false;
6530 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006531 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6532 idx,
6533 transparent_pointers,
6534 omit_empty_base_classes,
6535 ignore_array_bounds,
6536 child_name,
6537 child_byte_size,
6538 child_byte_offset,
6539 child_bitfield_bit_size,
6540 child_bitfield_bit_offset,
6541 child_is_base_class,
6542 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006543 valobj,
6544 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006545 }
6546 else
6547 {
6548 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6549 if (parent_name)
6550 {
6551 child_name.assign(1, '&');
6552 child_name += parent_name;
6553 }
6554
6555 // We have a pointer to an simple type
6556 if (idx == 0)
6557 {
6558 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6559 child_byte_offset = 0;
6560 return pointee_clang_type;
6561 }
6562 }
6563 }
6564 break;
6565
6566 case clang::Type::Typedef:
6567 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006568 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006569 return typedefed_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6570 idx,
6571 transparent_pointers,
6572 omit_empty_base_classes,
6573 ignore_array_bounds,
6574 child_name,
6575 child_byte_size,
6576 child_byte_offset,
6577 child_bitfield_bit_size,
6578 child_bitfield_bit_offset,
6579 child_is_base_class,
6580 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006581 valobj,
6582 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006583 }
6584 break;
6585
Enrico Granata36f51e42015-12-18 22:41:25 +00006586 case clang::Type::Auto:
6587 {
6588 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6589 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6590 idx,
6591 transparent_pointers,
6592 omit_empty_base_classes,
6593 ignore_array_bounds,
6594 child_name,
6595 child_byte_size,
6596 child_byte_offset,
6597 child_bitfield_bit_size,
6598 child_bitfield_bit_offset,
6599 child_is_base_class,
6600 child_is_deref_of_parent,
6601 valobj,
6602 language_flags);
6603 }
6604
Greg Claytond8d4a572015-08-11 21:38:15 +00006605 case clang::Type::Elaborated:
6606 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006607 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006608 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6609 idx,
6610 transparent_pointers,
6611 omit_empty_base_classes,
6612 ignore_array_bounds,
6613 child_name,
6614 child_byte_size,
6615 child_byte_offset,
6616 child_bitfield_bit_size,
6617 child_bitfield_bit_offset,
6618 child_is_base_class,
6619 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006620 valobj,
6621 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006622 }
6623
6624 case clang::Type::Paren:
6625 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006626 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006627 return paren_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6628 idx,
6629 transparent_pointers,
6630 omit_empty_base_classes,
6631 ignore_array_bounds,
6632 child_name,
6633 child_byte_size,
6634 child_byte_offset,
6635 child_bitfield_bit_size,
6636 child_bitfield_bit_offset,
6637 child_is_base_class,
6638 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006639 valobj,
6640 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006641 }
6642
6643
6644 default:
6645 break;
6646 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006647 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006648}
6649
6650static uint32_t
6651GetIndexForRecordBase
6652(
6653 const clang::RecordDecl *record_decl,
6654 const clang::CXXBaseSpecifier *base_spec,
6655 bool omit_empty_base_classes
6656 )
6657{
6658 uint32_t child_idx = 0;
6659
6660 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6661
6662 // const char *super_name = record_decl->getNameAsCString();
6663 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6664 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6665 //
6666 if (cxx_record_decl)
6667 {
6668 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6669 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6670 base_class != base_class_end;
6671 ++base_class)
6672 {
6673 if (omit_empty_base_classes)
6674 {
6675 if (BaseSpecifierIsEmpty (base_class))
6676 continue;
6677 }
6678
6679 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6680 // child_idx,
6681 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6682 //
6683 //
6684 if (base_class == base_spec)
6685 return child_idx;
6686 ++child_idx;
6687 }
6688 }
6689
6690 return UINT32_MAX;
6691}
6692
6693
6694static uint32_t
6695GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6696 clang::NamedDecl *canonical_decl,
6697 bool omit_empty_base_classes)
6698{
6699 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6700 omit_empty_base_classes);
6701
6702 clang::RecordDecl::field_iterator field, field_end;
6703 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6704 field != field_end;
6705 ++field, ++child_idx)
6706 {
6707 if (field->getCanonicalDecl() == canonical_decl)
6708 return child_idx;
6709 }
6710
6711 return UINT32_MAX;
6712}
6713
6714// Look for a child member (doesn't include base classes, but it does include
6715// their members) in the type hierarchy. Returns an index path into "clang_type"
6716// on how to reach the appropriate member.
6717//
6718// class A
6719// {
6720// public:
6721// int m_a;
6722// int m_b;
6723// };
6724//
6725// class B
6726// {
6727// };
6728//
6729// class C :
6730// public B,
6731// public A
6732// {
6733// };
6734//
6735// If we have a clang type that describes "class C", and we wanted to looked
6736// "m_b" in it:
6737//
6738// With omit_empty_base_classes == false we would get an integer array back with:
6739// { 1, 1 }
6740// The first index 1 is the child index for "class A" within class C
6741// The second index 1 is the child index for "m_b" within class A
6742//
6743// With omit_empty_base_classes == true we would get an integer array back with:
6744// { 0, 1 }
6745// 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)
6746// The second index 1 is the child index for "m_b" within class A
6747
6748size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006749ClangASTContext::GetIndexOfChildMemberWithName (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006750 bool omit_empty_base_classes,
6751 std::vector<uint32_t>& child_indexes)
6752{
6753 if (type && name && name[0])
6754 {
6755 clang::QualType qual_type(GetCanonicalQualType(type));
6756 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6757 switch (type_class)
6758 {
6759 case clang::Type::Record:
6760 if (GetCompleteType(type))
6761 {
6762 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6763 const clang::RecordDecl *record_decl = record_type->getDecl();
6764
6765 assert(record_decl);
6766 uint32_t child_idx = 0;
6767
6768 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6769
6770 // Try and find a field that matches NAME
6771 clang::RecordDecl::field_iterator field, field_end;
6772 llvm::StringRef name_sref(name);
6773 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6774 field != field_end;
6775 ++field, ++child_idx)
6776 {
6777 llvm::StringRef field_name = field->getName();
6778 if (field_name.empty())
6779 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006780 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006781 child_indexes.push_back(child_idx);
6782 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6783 return child_indexes.size();
6784 child_indexes.pop_back();
6785
6786 }
6787 else if (field_name.equals (name_sref))
6788 {
6789 // We have to add on the number of base classes to this index!
6790 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6791 return child_indexes.size();
6792 }
6793 }
6794
6795 if (cxx_record_decl)
6796 {
6797 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6798
6799 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6800
6801 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6802 // Didn't find things easily, lets let clang do its thang...
6803 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6804 clang::DeclarationName decl_name(&ident_ref);
6805
6806 clang::CXXBasePaths paths;
6807 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6808 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6809 },
6810 paths))
6811 {
6812 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6813 for (path = paths.begin(); path != path_end; ++path)
6814 {
6815 const size_t num_path_elements = path->size();
6816 for (size_t e=0; e<num_path_elements; ++e)
6817 {
6818 clang::CXXBasePathElement elem = (*path)[e];
6819
6820 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6821 if (child_idx == UINT32_MAX)
6822 {
6823 child_indexes.clear();
6824 return 0;
6825 }
6826 else
6827 {
6828 child_indexes.push_back (child_idx);
6829 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6830 }
6831 }
6832 for (clang::NamedDecl *path_decl : path->Decls)
6833 {
6834 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6835 if (child_idx == UINT32_MAX)
6836 {
6837 child_indexes.clear();
6838 return 0;
6839 }
6840 else
6841 {
6842 child_indexes.push_back (child_idx);
6843 }
6844 }
6845 }
6846 return child_indexes.size();
6847 }
6848 }
6849
6850 }
6851 break;
6852
6853 case clang::Type::ObjCObject:
6854 case clang::Type::ObjCInterface:
6855 if (GetCompleteType(type))
6856 {
6857 llvm::StringRef name_sref(name);
6858 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6859 assert (objc_class_type);
6860 if (objc_class_type)
6861 {
6862 uint32_t child_idx = 0;
6863 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6864
6865 if (class_interface_decl)
6866 {
6867 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6868 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6869
6870 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6871 {
6872 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6873
6874 if (ivar_decl->getName().equals (name_sref))
6875 {
6876 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6877 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6878 ++child_idx;
6879
6880 child_indexes.push_back (child_idx);
6881 return child_indexes.size();
6882 }
6883 }
6884
6885 if (superclass_interface_decl)
6886 {
6887 // The super class index is always zero for ObjC classes,
6888 // so we push it onto the child indexes in case we find
6889 // an ivar in our superclass...
6890 child_indexes.push_back (0);
6891
Greg Claytona1e5dc82015-08-11 22:53:00 +00006892 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006893 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6894 omit_empty_base_classes,
6895 child_indexes))
6896 {
6897 // We did find an ivar in a superclass so just
6898 // return the results!
6899 return child_indexes.size();
6900 }
6901
6902 // We didn't find an ivar matching "name" in our
6903 // superclass, pop the superclass zero index that
6904 // we pushed on above.
6905 child_indexes.pop_back();
6906 }
6907 }
6908 }
6909 }
6910 break;
6911
6912 case clang::Type::ObjCObjectPointer:
6913 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006914 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006915 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6916 omit_empty_base_classes,
6917 child_indexes);
6918 }
6919 break;
6920
6921
6922 case clang::Type::ConstantArray:
6923 {
6924 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6925 // const uint64_t element_count = array->getSize().getLimitedValue();
6926 //
6927 // if (idx < element_count)
6928 // {
6929 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6930 //
6931 // char element_name[32];
6932 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6933 //
6934 // child_name.assign(element_name);
6935 // assert(field_type_info.first % 8 == 0);
6936 // child_byte_size = field_type_info.first / 8;
6937 // child_byte_offset = idx * child_byte_size;
6938 // return array->getElementType().getAsOpaquePtr();
6939 // }
6940 }
6941 break;
6942
6943 // case clang::Type::MemberPointerType:
6944 // {
6945 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6946 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6947 //
6948 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6949 // {
6950 // return GetIndexOfChildWithName (ast,
6951 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6952 // name);
6953 // }
6954 // }
6955 // break;
6956 //
6957 case clang::Type::LValueReference:
6958 case clang::Type::RValueReference:
6959 {
6960 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6961 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006962 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006963
6964 if (pointee_clang_type.IsAggregateType ())
6965 {
6966 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6967 omit_empty_base_classes,
6968 child_indexes);
6969 }
6970 }
6971 break;
6972
6973 case clang::Type::Pointer:
6974 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006975 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006976
6977 if (pointee_clang_type.IsAggregateType ())
6978 {
6979 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6980 omit_empty_base_classes,
6981 child_indexes);
6982 }
6983 }
6984 break;
6985
6986 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006987 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00006988 omit_empty_base_classes,
6989 child_indexes);
6990
6991 case clang::Type::Auto:
6992 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetIndexOfChildMemberWithName (name,
6993 omit_empty_base_classes,
6994 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006995
6996 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006997 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00006998 omit_empty_base_classes,
6999 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007000
7001 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007002 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00007003 omit_empty_base_classes,
7004 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007005
7006 default:
7007 break;
7008 }
7009 }
7010 return 0;
7011}
7012
7013
7014// Get the index of the child of "clang_type" whose name matches. This function
7015// doesn't descend into the children, but only looks one level deep and name
7016// matches can include base class names.
7017
7018uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007019ClangASTContext::GetIndexOfChildWithName (lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00007020{
7021 if (type && name && name[0])
7022 {
7023 clang::QualType qual_type(GetCanonicalQualType(type));
7024
7025 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7026
7027 switch (type_class)
7028 {
7029 case clang::Type::Record:
7030 if (GetCompleteType(type))
7031 {
7032 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7033 const clang::RecordDecl *record_decl = record_type->getDecl();
7034
7035 assert(record_decl);
7036 uint32_t child_idx = 0;
7037
7038 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7039
7040 if (cxx_record_decl)
7041 {
7042 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7043 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
7044 base_class != base_class_end;
7045 ++base_class)
7046 {
7047 // Skip empty base classes
7048 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
7049 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
7050 continue;
7051
Greg Claytona1e5dc82015-08-11 22:53:00 +00007052 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007053 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
7054 if (base_class_type_name.compare (name) == 0)
7055 return child_idx;
7056 ++child_idx;
7057 }
7058 }
7059
7060 // Try and find a field that matches NAME
7061 clang::RecordDecl::field_iterator field, field_end;
7062 llvm::StringRef name_sref(name);
7063 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7064 field != field_end;
7065 ++field, ++child_idx)
7066 {
7067 if (field->getName().equals (name_sref))
7068 return child_idx;
7069 }
7070
7071 }
7072 break;
7073
7074 case clang::Type::ObjCObject:
7075 case clang::Type::ObjCInterface:
7076 if (GetCompleteType(type))
7077 {
7078 llvm::StringRef name_sref(name);
7079 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7080 assert (objc_class_type);
7081 if (objc_class_type)
7082 {
7083 uint32_t child_idx = 0;
7084 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
7085
7086 if (class_interface_decl)
7087 {
7088 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
7089 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
7090
7091 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
7092 {
7093 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
7094
7095 if (ivar_decl->getName().equals (name_sref))
7096 {
7097 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7098 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
7099 ++child_idx;
7100
7101 return child_idx;
7102 }
7103 }
7104
7105 if (superclass_interface_decl)
7106 {
7107 if (superclass_interface_decl->getName().equals (name_sref))
7108 return 0;
7109 }
7110 }
7111 }
7112 }
7113 break;
7114
7115 case clang::Type::ObjCObjectPointer:
7116 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00007117 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007118 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
7119 }
7120 break;
7121
7122 case clang::Type::ConstantArray:
7123 {
7124 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7125 // const uint64_t element_count = array->getSize().getLimitedValue();
7126 //
7127 // if (idx < element_count)
7128 // {
7129 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
7130 //
7131 // char element_name[32];
7132 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
7133 //
7134 // child_name.assign(element_name);
7135 // assert(field_type_info.first % 8 == 0);
7136 // child_byte_size = field_type_info.first / 8;
7137 // child_byte_offset = idx * child_byte_size;
7138 // return array->getElementType().getAsOpaquePtr();
7139 // }
7140 }
7141 break;
7142
7143 // case clang::Type::MemberPointerType:
7144 // {
7145 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7146 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
7147 //
7148 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
7149 // {
7150 // return GetIndexOfChildWithName (ast,
7151 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7152 // name);
7153 // }
7154 // }
7155 // break;
7156 //
7157 case clang::Type::LValueReference:
7158 case clang::Type::RValueReference:
7159 {
7160 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00007161 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007162
7163 if (pointee_type.IsAggregateType ())
7164 {
7165 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
7166 }
7167 }
7168 break;
7169
7170 case clang::Type::Pointer:
7171 {
7172 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00007173 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007174
7175 if (pointee_type.IsAggregateType ())
7176 {
7177 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
7178 }
7179 else
7180 {
7181 // if (parent_name)
7182 // {
7183 // child_name.assign(1, '*');
7184 // child_name += parent_name;
7185 // }
7186 //
7187 // // We have a pointer to an simple type
7188 // if (idx == 0)
7189 // {
7190 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
7191 // assert(clang_type_info.first % 8 == 0);
7192 // child_byte_size = clang_type_info.first / 8;
7193 // child_byte_offset = 0;
7194 // return pointee_type.getAsOpaquePtr();
7195 // }
7196 }
7197 }
7198 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00007199
7200 case clang::Type::Auto:
7201 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007202
7203 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007204 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007205
7206 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007207 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007208
7209 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007210 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007211
7212 default:
7213 break;
7214 }
7215 }
7216 return UINT32_MAX;
7217}
7218
7219
7220size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007221ClangASTContext::GetNumTemplateArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007222{
7223 if (!type)
7224 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007225
7226 clang::QualType qual_type (GetCanonicalQualType(type));
7227 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7228 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007229 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007230 case clang::Type::Record:
7231 if (GetCompleteType(type))
7232 {
7233 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7234 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00007235 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007236 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
7237 if (template_decl)
7238 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00007239 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007240 }
7241 break;
7242
7243 case clang::Type::Typedef:
7244 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
Enrico Granata36f51e42015-12-18 22:41:25 +00007245
7246 case clang::Type::Auto:
7247 return (CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType())).GetNumTemplateArguments();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007248
7249 case clang::Type::Elaborated:
7250 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
7251
7252 case clang::Type::Paren:
7253 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
7254
7255 default:
7256 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007257 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007258
Greg Claytond8d4a572015-08-11 21:38:15 +00007259 return 0;
7260}
7261
Greg Claytona1e5dc82015-08-11 22:53:00 +00007262CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007263ClangASTContext::GetTemplateArgument (lldb::opaque_compiler_type_t type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00007264{
7265 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007266 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007267
7268 clang::QualType qual_type (GetCanonicalQualType(type));
7269 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7270 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007271 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007272 case clang::Type::Record:
7273 if (GetCompleteType(type))
7274 {
7275 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7276 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00007277 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007278 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
7279 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00007280 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007281 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
7282 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00007283 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007284 case clang::TemplateArgument::Null:
7285 kind = eTemplateArgumentKindNull;
7286 return CompilerType();
7287
7288 case clang::TemplateArgument::Type:
7289 kind = eTemplateArgumentKindType;
7290 return CompilerType(getASTContext(), template_arg.getAsType());
7291
7292 case clang::TemplateArgument::Declaration:
7293 kind = eTemplateArgumentKindDeclaration;
7294 return CompilerType();
7295
7296 case clang::TemplateArgument::Integral:
7297 kind = eTemplateArgumentKindIntegral;
7298 return CompilerType(getASTContext(), template_arg.getIntegralType());
7299
7300 case clang::TemplateArgument::Template:
7301 kind = eTemplateArgumentKindTemplate;
7302 return CompilerType();
7303
7304 case clang::TemplateArgument::TemplateExpansion:
7305 kind = eTemplateArgumentKindTemplateExpansion;
7306 return CompilerType();
7307
7308 case clang::TemplateArgument::Expression:
7309 kind = eTemplateArgumentKindExpression;
7310 return CompilerType();
7311
7312 case clang::TemplateArgument::Pack:
7313 kind = eTemplateArgumentKindPack;
7314 return CompilerType();
7315
7316 default:
7317 assert (!"Unhandled clang::TemplateArgument::ArgKind");
7318 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007319 }
7320 }
7321 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007322 }
7323 break;
7324
7325 case clang::Type::Typedef:
7326 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
Enrico Granata36f51e42015-12-18 22:41:25 +00007327
7328 case clang::Type::Auto:
7329 return (CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType())).GetTemplateArgument(arg_idx, kind);
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007330
7331 case clang::Type::Elaborated:
7332 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
7333
7334 case clang::Type::Paren:
7335 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
7336
7337 default:
7338 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007339 }
7340 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00007341 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00007342}
7343
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007344CompilerType
7345ClangASTContext::GetTypeForFormatters (void* type)
7346{
7347 if (type)
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007348 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007349 return CompilerType();
7350}
7351
Greg Claytond8d4a572015-08-11 21:38:15 +00007352static bool
7353IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
7354{
7355 if (name == nullptr || name[0] == '\0')
7356 return false;
7357
7358#define OPERATOR_PREFIX "operator"
7359#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
7360
7361 const char *post_op_name = nullptr;
7362
7363 bool no_space = true;
7364
7365 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
7366 return false;
7367
7368 post_op_name = name + OPERATOR_PREFIX_LENGTH;
7369
7370 if (post_op_name[0] == ' ')
7371 {
7372 post_op_name++;
7373 no_space = false;
7374 }
7375
7376#undef OPERATOR_PREFIX
7377#undef OPERATOR_PREFIX_LENGTH
7378
7379 // This is an operator, set the overloaded operator kind to invalid
7380 // in case this is a conversion operator...
7381 op_kind = clang::NUM_OVERLOADED_OPERATORS;
7382
7383 switch (post_op_name[0])
7384 {
7385 default:
7386 if (no_space)
7387 return false;
7388 break;
7389 case 'n':
7390 if (no_space)
7391 return false;
7392 if (strcmp (post_op_name, "new") == 0)
7393 op_kind = clang::OO_New;
7394 else if (strcmp (post_op_name, "new[]") == 0)
7395 op_kind = clang::OO_Array_New;
7396 break;
7397
7398 case 'd':
7399 if (no_space)
7400 return false;
7401 if (strcmp (post_op_name, "delete") == 0)
7402 op_kind = clang::OO_Delete;
7403 else if (strcmp (post_op_name, "delete[]") == 0)
7404 op_kind = clang::OO_Array_Delete;
7405 break;
7406
7407 case '+':
7408 if (post_op_name[1] == '\0')
7409 op_kind = clang::OO_Plus;
7410 else if (post_op_name[2] == '\0')
7411 {
7412 if (post_op_name[1] == '=')
7413 op_kind = clang::OO_PlusEqual;
7414 else if (post_op_name[1] == '+')
7415 op_kind = clang::OO_PlusPlus;
7416 }
7417 break;
7418
7419 case '-':
7420 if (post_op_name[1] == '\0')
7421 op_kind = clang::OO_Minus;
7422 else if (post_op_name[2] == '\0')
7423 {
7424 switch (post_op_name[1])
7425 {
7426 case '=': op_kind = clang::OO_MinusEqual; break;
7427 case '-': op_kind = clang::OO_MinusMinus; break;
7428 case '>': op_kind = clang::OO_Arrow; break;
7429 }
7430 }
7431 else if (post_op_name[3] == '\0')
7432 {
7433 if (post_op_name[2] == '*')
7434 op_kind = clang::OO_ArrowStar; break;
7435 }
7436 break;
7437
7438 case '*':
7439 if (post_op_name[1] == '\0')
7440 op_kind = clang::OO_Star;
7441 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7442 op_kind = clang::OO_StarEqual;
7443 break;
7444
7445 case '/':
7446 if (post_op_name[1] == '\0')
7447 op_kind = clang::OO_Slash;
7448 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7449 op_kind = clang::OO_SlashEqual;
7450 break;
7451
7452 case '%':
7453 if (post_op_name[1] == '\0')
7454 op_kind = clang::OO_Percent;
7455 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7456 op_kind = clang::OO_PercentEqual;
7457 break;
7458
7459
7460 case '^':
7461 if (post_op_name[1] == '\0')
7462 op_kind = clang::OO_Caret;
7463 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7464 op_kind = clang::OO_CaretEqual;
7465 break;
7466
7467 case '&':
7468 if (post_op_name[1] == '\0')
7469 op_kind = clang::OO_Amp;
7470 else if (post_op_name[2] == '\0')
7471 {
7472 switch (post_op_name[1])
7473 {
7474 case '=': op_kind = clang::OO_AmpEqual; break;
7475 case '&': op_kind = clang::OO_AmpAmp; break;
7476 }
7477 }
7478 break;
7479
7480 case '|':
7481 if (post_op_name[1] == '\0')
7482 op_kind = clang::OO_Pipe;
7483 else if (post_op_name[2] == '\0')
7484 {
7485 switch (post_op_name[1])
7486 {
7487 case '=': op_kind = clang::OO_PipeEqual; break;
7488 case '|': op_kind = clang::OO_PipePipe; break;
7489 }
7490 }
7491 break;
7492
7493 case '~':
7494 if (post_op_name[1] == '\0')
7495 op_kind = clang::OO_Tilde;
7496 break;
7497
7498 case '!':
7499 if (post_op_name[1] == '\0')
7500 op_kind = clang::OO_Exclaim;
7501 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7502 op_kind = clang::OO_ExclaimEqual;
7503 break;
7504
7505 case '=':
7506 if (post_op_name[1] == '\0')
7507 op_kind = clang::OO_Equal;
7508 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7509 op_kind = clang::OO_EqualEqual;
7510 break;
7511
7512 case '<':
7513 if (post_op_name[1] == '\0')
7514 op_kind = clang::OO_Less;
7515 else if (post_op_name[2] == '\0')
7516 {
7517 switch (post_op_name[1])
7518 {
7519 case '<': op_kind = clang::OO_LessLess; break;
7520 case '=': op_kind = clang::OO_LessEqual; break;
7521 }
7522 }
7523 else if (post_op_name[3] == '\0')
7524 {
7525 if (post_op_name[2] == '=')
7526 op_kind = clang::OO_LessLessEqual;
7527 }
7528 break;
7529
7530 case '>':
7531 if (post_op_name[1] == '\0')
7532 op_kind = clang::OO_Greater;
7533 else if (post_op_name[2] == '\0')
7534 {
7535 switch (post_op_name[1])
7536 {
7537 case '>': op_kind = clang::OO_GreaterGreater; break;
7538 case '=': op_kind = clang::OO_GreaterEqual; break;
7539 }
7540 }
7541 else if (post_op_name[1] == '>' &&
7542 post_op_name[2] == '=' &&
7543 post_op_name[3] == '\0')
7544 {
7545 op_kind = clang::OO_GreaterGreaterEqual;
7546 }
7547 break;
7548
7549 case ',':
7550 if (post_op_name[1] == '\0')
7551 op_kind = clang::OO_Comma;
7552 break;
7553
7554 case '(':
7555 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7556 op_kind = clang::OO_Call;
7557 break;
7558
7559 case '[':
7560 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7561 op_kind = clang::OO_Subscript;
7562 break;
7563 }
7564
7565 return true;
7566}
7567
7568clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007569ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007570{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007571 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007572 if (enutype)
7573 return enutype->getDecl();
7574 return NULL;
7575}
7576
7577clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007578ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007579{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007580 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007581 if (record_type)
7582 return record_type->getDecl();
7583 return nullptr;
7584}
7585
Greg Claytone6b36cd2015-12-08 01:02:08 +00007586clang::TagDecl *
7587ClangASTContext::GetAsTagDecl (const CompilerType& type)
7588{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007589 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007590 if (qual_type.isNull())
7591 return nullptr;
7592 else
7593 return qual_type->getAsTagDecl();
7594}
7595
Greg Claytond8d4a572015-08-11 21:38:15 +00007596clang::CXXRecordDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007597ClangASTContext::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007598{
7599 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7600}
7601
7602clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007603ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007604{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007605 const clang::ObjCObjectType *objc_class_type =
7606 llvm::dyn_cast<clang::ObjCObjectType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007607 if (objc_class_type)
7608 return objc_class_type->getInterface();
7609 return nullptr;
7610}
7611
7612clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007613ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7614 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007615 AccessType access,
7616 uint32_t bitfield_bit_size)
7617{
7618 if (!type.IsValid() || !field_clang_type.IsValid())
7619 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007620 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007621 if (!ast)
7622 return nullptr;
7623 clang::ASTContext* clang_ast = ast->getASTContext();
7624
7625 clang::FieldDecl *field = nullptr;
7626
7627 clang::Expr *bit_width = nullptr;
7628 if (bitfield_bit_size != 0)
7629 {
7630 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7631 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7632 }
7633
7634 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7635 if (record_decl)
7636 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007637 field = clang::FieldDecl::Create(*clang_ast, record_decl, clang::SourceLocation(), clang::SourceLocation(),
7638 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7639 ClangUtil::GetQualType(field_clang_type), // Field type
7640 nullptr, // TInfo *
7641 bit_width, // BitWidth
7642 false, // Mutable
7643 clang::ICIS_NoInit); // HasInit
7644
Greg Claytond8d4a572015-08-11 21:38:15 +00007645 if (!name)
7646 {
7647 // Determine whether this field corresponds to an anonymous
7648 // struct or union.
7649 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7650 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7651 if (!Rec->getDeclName()) {
7652 Rec->setAnonymousStructOrUnion(true);
7653 field->setImplicit();
7654
7655 }
7656 }
7657 }
7658
7659 if (field)
7660 {
7661 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7662
7663 record_decl->addDecl(field);
7664
7665#ifdef LLDB_CONFIGURATION_DEBUG
7666 VerifyDecl(field);
7667#endif
7668 }
7669 }
7670 else
7671 {
7672 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7673
7674 if (class_interface_decl)
7675 {
7676 const bool is_synthesized = false;
7677
7678 field_clang_type.GetCompleteType();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007679
7680 field = clang::ObjCIvarDecl::Create(
7681 *clang_ast, class_interface_decl, clang::SourceLocation(), clang::SourceLocation(),
7682 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7683 ClangUtil::GetQualType(field_clang_type), // Field type
7684 nullptr, // TypeSourceInfo *
7685 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, is_synthesized);
7686
Greg Claytond8d4a572015-08-11 21:38:15 +00007687 if (field)
7688 {
7689 class_interface_decl->addDecl(field);
7690
7691#ifdef LLDB_CONFIGURATION_DEBUG
7692 VerifyDecl(field);
7693#endif
7694 }
7695 }
7696 }
7697 return field;
7698}
7699
7700void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007701ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007702{
Greg Claytonf73034f2015-09-08 18:15:05 +00007703 if (!type)
7704 return;
7705
7706 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007707 if (!ast)
7708 return;
7709
7710 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7711
7712 if (!record_decl)
7713 return;
7714
7715 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7716
7717 IndirectFieldVector indirect_fields;
7718 clang::RecordDecl::field_iterator field_pos;
7719 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7720 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7721 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7722 {
7723 if (field_pos->isAnonymousStructOrUnion())
7724 {
7725 clang::QualType field_qual_type = field_pos->getType();
7726
7727 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7728
7729 if (!field_record_type)
7730 continue;
7731
7732 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7733
7734 if (!field_record_decl)
7735 continue;
7736
7737 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7738 di != de;
7739 ++di)
7740 {
7741 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7742 {
7743 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7744 chain[0] = *field_pos;
7745 chain[1] = nested_field_decl;
7746 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7747 record_decl,
7748 clang::SourceLocation(),
7749 nested_field_decl->getIdentifier(),
7750 nested_field_decl->getType(),
David Majnemer68303762016-06-24 04:39:22 +00007751 {chain, 2});
Greg Claytond8d4a572015-08-11 21:38:15 +00007752
7753 indirect_field->setImplicit();
7754
7755 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7756 nested_field_decl->getAccess()));
7757
7758 indirect_fields.push_back(indirect_field);
7759 }
7760 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7761 {
7762 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7763 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7764 chain[0] = *field_pos;
7765
7766 int chain_index = 1;
7767 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7768 nce = nested_indirect_field_decl->chain_end();
7769 nci < nce;
7770 ++nci)
7771 {
7772 chain[chain_index] = *nci;
7773 chain_index++;
7774 }
7775
7776 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7777 record_decl,
7778 clang::SourceLocation(),
7779 nested_indirect_field_decl->getIdentifier(),
7780 nested_indirect_field_decl->getType(),
David Majnemer68303762016-06-24 04:39:22 +00007781 {chain, nested_chain_size + 1});
Greg Claytond8d4a572015-08-11 21:38:15 +00007782
7783 indirect_field->setImplicit();
7784
7785 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7786 nested_indirect_field_decl->getAccess()));
7787
7788 indirect_fields.push_back(indirect_field);
7789 }
7790 }
7791 }
7792 }
7793
7794 // Check the last field to see if it has an incomplete array type as its
7795 // last member and if it does, the tell the record decl about it
7796 if (last_field_pos != field_end_pos)
7797 {
7798 if (last_field_pos->getType()->isIncompleteArrayType())
7799 record_decl->hasFlexibleArrayMember();
7800 }
7801
7802 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7803 ifi < ife;
7804 ++ifi)
7805 {
7806 record_decl->addDecl(*ifi);
7807 }
7808}
7809
7810void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007811ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007812{
Greg Claytonf73034f2015-09-08 18:15:05 +00007813 if (type)
7814 {
7815 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7816 if (ast)
7817 {
7818 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007819
Greg Claytonf73034f2015-09-08 18:15:05 +00007820 if (!record_decl)
7821 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007822
Greg Claytonf73034f2015-09-08 18:15:05 +00007823 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7824 }
7825 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007826}
7827
7828clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007829ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7830 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007831 AccessType access)
7832{
7833 clang::VarDecl *var_decl = nullptr;
7834
7835 if (!type.IsValid() || !var_type.IsValid())
7836 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007837 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007838 if (!ast)
7839 return nullptr;
7840
7841 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7842 if (record_decl)
7843 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007844 var_decl =
7845 clang::VarDecl::Create(*ast->getASTContext(), // ASTContext &
7846 record_decl, // DeclContext *
7847 clang::SourceLocation(), // clang::SourceLocation StartLoc
7848 clang::SourceLocation(), // clang::SourceLocation IdLoc
7849 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7850 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7851 nullptr, // TypeSourceInfo *
7852 clang::SC_Static); // StorageClass
Greg Claytond8d4a572015-08-11 21:38:15 +00007853 if (var_decl)
7854 {
7855 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7856 record_decl->addDecl(var_decl);
7857
7858#ifdef LLDB_CONFIGURATION_DEBUG
7859 VerifyDecl(var_decl);
7860#endif
7861 }
7862 }
7863 return var_decl;
7864}
7865
7866
7867clang::CXXMethodDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007868ClangASTContext::AddMethodToCXXRecordType (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007869 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007870 lldb::AccessType access,
7871 bool is_virtual,
7872 bool is_static,
7873 bool is_inline,
7874 bool is_explicit,
7875 bool is_attr_used,
7876 bool is_artificial)
7877{
7878 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7879 return nullptr;
7880
7881 clang::QualType record_qual_type(GetCanonicalQualType(type));
7882
7883 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7884
7885 if (cxx_record_decl == nullptr)
7886 return nullptr;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007887
7888 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7889
Greg Claytond8d4a572015-08-11 21:38:15 +00007890 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7891
7892 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7893
7894 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7895
7896 if (function_type == nullptr)
7897 return nullptr;
7898
7899 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7900
7901 if (!method_function_prototype)
7902 return nullptr;
7903
7904 unsigned int num_params = method_function_prototype->getNumParams();
7905
7906 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7907 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7908
7909 if (is_artificial)
7910 return nullptr; // skip everything artificial
7911
7912 if (name[0] == '~')
7913 {
7914 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7915 cxx_record_decl,
7916 clang::SourceLocation(),
7917 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7918 method_qual_type,
7919 nullptr,
7920 is_inline,
7921 is_artificial);
7922 cxx_method_decl = cxx_dtor_decl;
7923 }
7924 else if (decl_name == cxx_record_decl->getDeclName())
7925 {
7926 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7927 cxx_record_decl,
7928 clang::SourceLocation(),
7929 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7930 method_qual_type,
7931 nullptr, // TypeSourceInfo *
7932 is_explicit,
7933 is_inline,
7934 is_artificial,
7935 false /*is_constexpr*/);
7936 cxx_method_decl = cxx_ctor_decl;
7937 }
7938 else
7939 {
7940 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7941 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7942
7943 if (IsOperator (name, op_kind))
7944 {
7945 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7946 {
7947 // Check the number of operator parameters. Sometimes we have
7948 // seen bad DWARF that doesn't correctly describe operators and
7949 // if we try to create a method and add it to the class, clang
7950 // will assert and crash, so we need to make sure things are
7951 // acceptable.
7952 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7953 return nullptr;
7954 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7955 cxx_record_decl,
7956 clang::SourceLocation(),
7957 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7958 method_qual_type,
7959 nullptr, // TypeSourceInfo *
7960 SC,
7961 is_inline,
7962 false /*is_constexpr*/,
7963 clang::SourceLocation());
7964 }
7965 else if (num_params == 0)
7966 {
7967 // Conversion operators don't take params...
7968 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7969 cxx_record_decl,
7970 clang::SourceLocation(),
7971 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7972 method_qual_type,
7973 nullptr, // TypeSourceInfo *
7974 is_inline,
7975 is_explicit,
7976 false /*is_constexpr*/,
7977 clang::SourceLocation());
7978 }
7979 }
7980
7981 if (cxx_method_decl == nullptr)
7982 {
7983 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7984 cxx_record_decl,
7985 clang::SourceLocation(),
7986 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7987 method_qual_type,
7988 nullptr, // TypeSourceInfo *
7989 SC,
7990 is_inline,
7991 false /*is_constexpr*/,
7992 clang::SourceLocation());
7993 }
7994 }
7995
7996 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7997
7998 cxx_method_decl->setAccess (access_specifier);
7999 cxx_method_decl->setVirtualAsWritten (is_virtual);
8000
8001 if (is_attr_used)
8002 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8003
8004 // Populate the method decl with parameter decls
8005
8006 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8007
8008 for (unsigned param_index = 0;
8009 param_index < num_params;
8010 ++param_index)
8011 {
8012 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
8013 cxx_method_decl,
8014 clang::SourceLocation(),
8015 clang::SourceLocation(),
8016 nullptr, // anonymous
8017 method_function_prototype->getParamType(param_index),
8018 nullptr,
8019 clang::SC_None,
8020 nullptr));
8021 }
8022
8023 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
8024
8025 cxx_record_decl->addDecl (cxx_method_decl);
8026
8027 // Sometimes the debug info will mention a constructor (default/copy/move),
8028 // destructor, or assignment operator (copy/move) but there won't be any
8029 // version of this in the code. So we check if the function was artificially
8030 // generated and if it is trivial and this lets the compiler/backend know
8031 // that it can inline the IR for these when it needs to and we can avoid a
8032 // "missing function" error when running expressions.
8033
8034 if (is_artificial)
8035 {
8036 if (cxx_ctor_decl &&
8037 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
8038 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
8039 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
8040 {
8041 cxx_ctor_decl->setDefaulted();
8042 cxx_ctor_decl->setTrivial(true);
8043 }
8044 else if (cxx_dtor_decl)
8045 {
8046 if (cxx_record_decl->hasTrivialDestructor())
8047 {
8048 cxx_dtor_decl->setDefaulted();
8049 cxx_dtor_decl->setTrivial(true);
8050 }
8051 }
8052 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
8053 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
8054 {
8055 cxx_method_decl->setDefaulted();
8056 cxx_method_decl->setTrivial(true);
8057 }
8058 }
8059
8060#ifdef LLDB_CONFIGURATION_DEBUG
8061 VerifyDecl(cxx_method_decl);
8062#endif
8063
8064 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
8065 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
8066 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
8067 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
8068 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
8069 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
8070 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
8071 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
8072 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
8073 return cxx_method_decl;
8074}
8075
8076
8077#pragma mark C++ Base Classes
8078
8079clang::CXXBaseSpecifier *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008080ClangASTContext::CreateBaseClassSpecifier (lldb::opaque_compiler_type_t type, AccessType access, bool is_virtual, bool base_of_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00008081{
8082 if (type)
8083 return new clang::CXXBaseSpecifier (clang::SourceRange(),
8084 is_virtual,
8085 base_of_class,
8086 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
8087 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
8088 clang::SourceLocation());
8089 return nullptr;
8090}
8091
8092void
8093ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
8094{
8095 for (unsigned i=0; i<num_base_classes; ++i)
8096 {
8097 delete base_classes[i];
8098 base_classes[i] = nullptr;
8099 }
8100}
8101
8102bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008103ClangASTContext::SetBaseClassesForClassType (lldb::opaque_compiler_type_t type, clang::CXXBaseSpecifier const * const *base_classes,
Greg Claytond8d4a572015-08-11 21:38:15 +00008104 unsigned num_base_classes)
8105{
8106 if (type)
8107 {
8108 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8109 if (cxx_record_decl)
8110 {
8111 cxx_record_decl->setBases(base_classes, num_base_classes);
8112 return true;
8113 }
8114 }
8115 return false;
8116}
8117
8118bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008119ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008120{
Greg Claytonf73034f2015-09-08 18:15:05 +00008121 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008122 if (!ast)
8123 return false;
8124 clang::ASTContext* clang_ast = ast->getASTContext();
8125
8126 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
8127 {
8128 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
8129 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
8130 if (class_interface_decl && super_interface_decl)
8131 {
8132 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
8133 return true;
8134 }
8135 }
8136 return false;
8137}
8138
8139bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008140ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00008141 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00008142 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00008143 clang::ObjCIvarDecl *ivar_decl,
8144 const char *property_setter_name,
8145 const char *property_getter_name,
8146 uint32_t property_attributes,
8147 ClangASTMetadata *metadata)
8148{
8149 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
8150 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00008151 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008152 if (!ast)
8153 return false;
8154 clang::ASTContext* clang_ast = ast->getASTContext();
8155
8156 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
8157
8158 if (class_interface_decl)
8159 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008160 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00008161
8162 if (property_clang_type.IsValid())
8163 property_clang_type_to_access = property_clang_type;
8164 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008165 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008166
8167 if (class_interface_decl && property_clang_type_to_access.IsValid())
8168 {
8169 clang::TypeSourceInfo *prop_type_source;
8170 if (ivar_decl)
8171 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
8172 else
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008173 prop_type_source = clang_ast->getTrivialTypeSourceInfo(ClangUtil::GetQualType(property_clang_type));
8174
8175 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8176 *clang_ast, class_interface_decl,
8177 clang::SourceLocation(), // Source Location
8178 &clang_ast->Idents.get(property_name),
8179 clang::SourceLocation(), // Source Location for AT
8180 clang::SourceLocation(), // Source location for (
8181 ivar_decl ? ivar_decl->getType() : ClangUtil::GetQualType(property_clang_type), prop_type_source);
8182
Greg Claytond8d4a572015-08-11 21:38:15 +00008183 if (property_decl)
8184 {
8185 if (metadata)
8186 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8187
8188 class_interface_decl->addDecl (property_decl);
8189
8190 clang::Selector setter_sel, getter_sel;
8191
8192 if (property_setter_name != nullptr)
8193 {
8194 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
8195 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
8196 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8197 }
8198 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
8199 {
8200 std::string setter_sel_string("set");
8201 setter_sel_string.push_back(::toupper(property_name[0]));
8202 setter_sel_string.append(&property_name[1]);
8203 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
8204 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8205 }
8206 property_decl->setSetterName(setter_sel);
8207 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
8208
8209 if (property_getter_name != nullptr)
8210 {
8211 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
8212 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8213 }
8214 else
8215 {
8216 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
8217 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8218 }
8219 property_decl->setGetterName(getter_sel);
8220 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
8221
8222 if (ivar_decl)
8223 property_decl->setPropertyIvarDecl (ivar_decl);
8224
8225 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8226 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
8227 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8228 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8229 if (property_attributes & DW_APPLE_PROPERTY_assign)
8230 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
8231 if (property_attributes & DW_APPLE_PROPERTY_retain)
8232 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
8233 if (property_attributes & DW_APPLE_PROPERTY_copy)
8234 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
8235 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8236 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8237
8238 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
8239 {
8240 const bool isInstance = true;
8241 const bool isVariadic = false;
8242 const bool isSynthesized = false;
8243 const bool isImplicitlyDeclared = true;
8244 const bool isDefined = false;
8245 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
8246 const bool HasRelatedResultType = false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008247
8248 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8249 *clang_ast, clang::SourceLocation(), clang::SourceLocation(), getter_sel,
8250 ClangUtil::GetQualType(property_clang_type_to_access), nullptr, class_interface_decl,
8251 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8252 HasRelatedResultType);
8253
Greg Claytond8d4a572015-08-11 21:38:15 +00008254 if (getter && metadata)
8255 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8256
8257 if (getter)
8258 {
8259 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
8260
8261 class_interface_decl->addDecl(getter);
8262 }
8263 }
8264
8265 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
8266 {
8267 clang::QualType result_type = clang_ast->VoidTy;
8268
8269 const bool isInstance = true;
8270 const bool isVariadic = false;
8271 const bool isSynthesized = false;
8272 const bool isImplicitlyDeclared = true;
8273 const bool isDefined = false;
8274 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
8275 const bool HasRelatedResultType = false;
8276
8277 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
8278 clang::SourceLocation(),
8279 clang::SourceLocation(),
8280 setter_sel,
8281 result_type,
8282 nullptr,
8283 class_interface_decl,
8284 isInstance,
8285 isVariadic,
8286 isSynthesized,
8287 isImplicitlyDeclared,
8288 isDefined,
8289 impControl,
8290 HasRelatedResultType);
8291
8292 if (setter && metadata)
8293 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8294
8295 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008296
8297 params.push_back(clang::ParmVarDecl::Create(
8298 *clang_ast, setter, clang::SourceLocation(), clang::SourceLocation(),
8299 nullptr, // anonymous
8300 ClangUtil::GetQualType(property_clang_type_to_access), nullptr, clang::SC_Auto, nullptr));
8301
Greg Claytond8d4a572015-08-11 21:38:15 +00008302 if (setter)
8303 {
8304 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8305
8306 class_interface_decl->addDecl(setter);
8307 }
8308 }
8309
8310 return true;
8311 }
8312 }
8313 }
8314 return false;
8315}
8316
8317bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008318ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00008319{
8320 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
8321 if (class_interface_decl)
8322 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
8323 return false;
8324}
8325
8326
8327clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00008328ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008329 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 +00008330 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00008331 lldb::AccessType access,
Sean Callanan5b42f4b2016-06-24 00:24:40 +00008332 bool is_artificial,
8333 bool is_variadic)
Greg Claytond8d4a572015-08-11 21:38:15 +00008334{
8335 if (!type || !method_clang_type.IsValid())
8336 return nullptr;
8337
8338 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8339
8340 if (class_interface_decl == nullptr)
8341 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00008342 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8343 if (lldb_ast == nullptr)
8344 return nullptr;
8345 clang::ASTContext *ast = lldb_ast->getASTContext();
8346
Greg Claytond8d4a572015-08-11 21:38:15 +00008347 const char *selector_start = ::strchr (name, ' ');
8348 if (selector_start == nullptr)
8349 return nullptr;
8350
8351 selector_start++;
8352 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8353
8354 size_t len = 0;
8355 const char *start;
8356 //printf ("name = '%s'\n", name);
8357
8358 unsigned num_selectors_with_args = 0;
8359 for (start = selector_start;
8360 start && *start != '\0' && *start != ']';
8361 start += len)
8362 {
8363 len = ::strcspn(start, ":]");
8364 bool has_arg = (start[len] == ':');
8365 if (has_arg)
8366 ++num_selectors_with_args;
8367 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
8368 if (has_arg)
8369 len += 1;
8370 }
8371
8372
8373 if (selector_idents.size() == 0)
8374 return nullptr;
8375
8376 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
8377 selector_idents.data());
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008378
8379 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8380
Greg Claytond8d4a572015-08-11 21:38:15 +00008381 // Populate the method decl with parameter decls
8382 const clang::Type *method_type(method_qual_type.getTypePtr());
8383
8384 if (method_type == nullptr)
8385 return nullptr;
8386
8387 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8388
8389 if (!method_function_prototype)
8390 return nullptr;
8391
8392
Greg Claytond8d4a572015-08-11 21:38:15 +00008393 bool is_synthesized = false;
8394 bool is_defined = false;
8395 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
8396
8397 const unsigned num_args = method_function_prototype->getNumParams();
8398
8399 if (num_args != num_selectors_with_args)
8400 return nullptr; // some debug information is corrupt. We are not going to deal with it.
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008401
8402 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8403 *ast,
8404 clang::SourceLocation(), // beginLoc,
8405 clang::SourceLocation(), // endLoc,
8406 method_selector, method_function_prototype->getReturnType(),
8407 nullptr, // TypeSourceInfo *ResultTInfo,
8408 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(ClangUtil::GetQualType(type)), name[0] == '-',
8409 is_variadic, is_synthesized,
8410 true, // is_implicitly_declared; we force this to true because we don't have source locations
8411 is_defined, imp_control, false /*has_related_result_type*/);
8412
Greg Claytond8d4a572015-08-11 21:38:15 +00008413 if (objc_method_decl == nullptr)
8414 return nullptr;
8415
8416 if (num_args > 0)
8417 {
8418 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8419
8420 for (unsigned param_index = 0; param_index < num_args; ++param_index)
8421 {
8422 params.push_back (clang::ParmVarDecl::Create (*ast,
8423 objc_method_decl,
8424 clang::SourceLocation(),
8425 clang::SourceLocation(),
8426 nullptr, // anonymous
8427 method_function_prototype->getParamType(param_index),
8428 nullptr,
8429 clang::SC_Auto,
8430 nullptr));
8431 }
8432
8433 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8434 }
8435
8436 class_interface_decl->addDecl (objc_method_decl);
8437
8438#ifdef LLDB_CONFIGURATION_DEBUG
8439 VerifyDecl(objc_method_decl);
8440#endif
8441
8442 return objc_method_decl;
8443}
8444
8445bool
Greg Claytone6b36cd2015-12-08 01:02:08 +00008446ClangASTContext::GetHasExternalStorage (const CompilerType &type)
8447{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008448 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008449 return false;
8450
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008451 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008452
8453 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8454 switch (type_class)
8455 {
8456 case clang::Type::Record:
8457 {
8458 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8459 if (cxx_record_decl)
8460 return cxx_record_decl->hasExternalLexicalStorage () || cxx_record_decl->hasExternalVisibleStorage();
8461 }
8462 break;
8463
8464 case clang::Type::Enum:
8465 {
8466 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8467 if (enum_decl)
8468 return enum_decl->hasExternalLexicalStorage () || enum_decl->hasExternalVisibleStorage();
8469 }
8470 break;
8471
8472 case clang::Type::ObjCObject:
8473 case clang::Type::ObjCInterface:
8474 {
8475 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8476 assert (objc_class_type);
8477 if (objc_class_type)
8478 {
8479 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8480
8481 if (class_interface_decl)
8482 return class_interface_decl->hasExternalLexicalStorage () || class_interface_decl->hasExternalVisibleStorage ();
8483 }
8484 }
8485 break;
8486
8487 case clang::Type::Typedef:
8488 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()));
8489
Enrico Granata36f51e42015-12-18 22:41:25 +00008490 case clang::Type::Auto:
8491 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr()));
8492
Greg Claytone6b36cd2015-12-08 01:02:08 +00008493 case clang::Type::Elaborated:
8494 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
8495
8496 case clang::Type::Paren:
8497 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8498
8499 default:
8500 break;
8501 }
8502 return false;
8503}
8504
8505
8506bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008507ClangASTContext::SetHasExternalStorage (lldb::opaque_compiler_type_t type, bool has_extern)
Greg Claytond8d4a572015-08-11 21:38:15 +00008508{
8509 if (!type)
8510 return false;
8511
8512 clang::QualType qual_type (GetCanonicalQualType(type));
8513
8514 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8515 switch (type_class)
8516 {
8517 case clang::Type::Record:
8518 {
8519 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8520 if (cxx_record_decl)
8521 {
8522 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
8523 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
8524 return true;
8525 }
8526 }
8527 break;
8528
8529 case clang::Type::Enum:
8530 {
8531 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8532 if (enum_decl)
8533 {
8534 enum_decl->setHasExternalLexicalStorage (has_extern);
8535 enum_decl->setHasExternalVisibleStorage (has_extern);
8536 return true;
8537 }
8538 }
8539 break;
8540
8541 case clang::Type::ObjCObject:
8542 case clang::Type::ObjCInterface:
8543 {
8544 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8545 assert (objc_class_type);
8546 if (objc_class_type)
8547 {
8548 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8549
8550 if (class_interface_decl)
8551 {
8552 class_interface_decl->setHasExternalLexicalStorage (has_extern);
8553 class_interface_decl->setHasExternalVisibleStorage (has_extern);
8554 return true;
8555 }
8556 }
8557 }
8558 break;
8559
8560 case clang::Type::Typedef:
8561 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Enrico Granata36f51e42015-12-18 22:41:25 +00008562
8563 case clang::Type::Auto:
8564 return SetHasExternalStorage (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), has_extern);
Greg Claytond8d4a572015-08-11 21:38:15 +00008565
8566 case clang::Type::Elaborated:
8567 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
8568
8569 case clang::Type::Paren:
8570 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
8571
8572 default:
8573 break;
8574 }
8575 return false;
8576}
8577
8578
8579#pragma mark TagDecl
8580
8581bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008582ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008583{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008584 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008585 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008586 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008587 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8588 if (tag_type)
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008589 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008590 clang::TagDecl *tag_decl = tag_type->getDecl();
8591 if (tag_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00008592 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008593 tag_decl->startDefinition();
8594 return true;
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00008595 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00008596 }
8597
8598 const clang::ObjCObjectType *object_type = qual_type->getAs<clang::ObjCObjectType>();
8599 if (object_type)
8600 {
8601 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8602 if (interface_decl)
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00008603 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008604 interface_decl->startDefinition();
8605 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008606 }
8607 }
8608 }
8609 return false;
8610}
8611
8612bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008613ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008614{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008615 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008616 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008617 {
Greg Clayton23c12ca2016-05-26 19:24:02 +00008618 // Make sure we use the same methodology as ClangASTContext::StartTagDeclarationDefinition()
8619 // as to how we start/end the definition. Previously we were calling
8620 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8621 if (tag_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008622 {
Greg Clayton23c12ca2016-05-26 19:24:02 +00008623 clang::TagDecl *tag_decl = tag_type->getDecl();
8624 if (tag_decl)
8625 {
8626 clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8627
8628 if (cxx_record_decl)
8629 {
8630 if (!cxx_record_decl->isCompleteDefinition())
8631 cxx_record_decl->completeDefinition();
8632 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8633 cxx_record_decl->setHasExternalLexicalStorage (false);
8634 cxx_record_decl->setHasExternalVisibleStorage (false);
8635 return true;
8636 }
8637 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008638 }
Greg Clayton23c12ca2016-05-26 19:24:02 +00008639
Greg Claytone6b36cd2015-12-08 01:02:08 +00008640 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
Greg Claytond8d4a572015-08-11 21:38:15 +00008641
8642 if (enutype)
8643 {
8644 clang::EnumDecl *enum_decl = enutype->getDecl();
8645
8646 if (enum_decl)
8647 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008648 if (!enum_decl->isCompleteDefinition())
Greg Claytond8d4a572015-08-11 21:38:15 +00008649 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008650 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8651 if (lldb_ast == nullptr)
8652 return false;
8653 clang::ASTContext *ast = lldb_ast->getASTContext();
8654
8655 /// TODO This really needs to be fixed.
Greg Clayton909b277842016-04-29 20:48:39 +00008656
8657 QualType integer_type(enum_decl->getIntegerType());
8658 if (!integer_type.isNull())
Greg Claytone6b36cd2015-12-08 01:02:08 +00008659 {
Greg Clayton909b277842016-04-29 20:48:39 +00008660 unsigned NumPositiveBits = 1;
8661 unsigned NumNegativeBits = 0;
8662
8663 clang::QualType promotion_qual_type;
8664 // If the enum integer type is less than an integer in bit width,
8665 // then we must promote it to an integer size.
8666 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8667 {
8668 if (enum_decl->getIntegerType()->isSignedIntegerType())
8669 promotion_qual_type = ast->IntTy;
8670 else
8671 promotion_qual_type = ast->UnsignedIntTy;
8672 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00008673 else
Greg Clayton909b277842016-04-29 20:48:39 +00008674 promotion_qual_type = enum_decl->getIntegerType();
8675
8676 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Claytone6b36cd2015-12-08 01:02:08 +00008677 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008678 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008679 return true;
8680 }
8681 }
8682 }
8683 return false;
8684}
8685
Greg Claytond8d4a572015-08-11 21:38:15 +00008686bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008687ClangASTContext::AddEnumerationValueToEnumerationType (lldb::opaque_compiler_type_t type,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008688 const CompilerType &enumerator_clang_type,
8689 const Declaration &decl,
8690 const char *name,
8691 int64_t enum_value,
8692 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008693{
8694 if (type && enumerator_clang_type.IsValid() && name && name[0])
8695 {
8696 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8697
8698 bool is_signed = false;
8699 enumerator_clang_type.IsIntegerType (is_signed);
8700 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8701 if (clang_type)
8702 {
8703 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8704
8705 if (enutype)
8706 {
8707 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8708 enum_llvm_apsint = enum_value;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008709 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8710 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8711 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8712 ClangUtil::GetQualType(enumerator_clang_type), nullptr, enum_llvm_apsint);
8713
Greg Claytond8d4a572015-08-11 21:38:15 +00008714 if (enumerator_decl)
8715 {
8716 enutype->getDecl()->addDecl(enumerator_decl);
8717
8718#ifdef LLDB_CONFIGURATION_DEBUG
8719 VerifyDecl(enumerator_decl);
8720#endif
8721
8722 return true;
8723 }
8724 }
8725 }
8726 }
8727 return false;
8728}
8729
Greg Claytona1e5dc82015-08-11 22:53:00 +00008730CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008731ClangASTContext::GetEnumerationIntegerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008732{
8733 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8734 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8735 if (clang_type)
8736 {
8737 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8738 if (enutype)
8739 {
8740 clang::EnumDecl *enum_decl = enutype->getDecl();
8741 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008742 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008743 }
8744 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008745 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008746}
8747
Greg Claytona1e5dc82015-08-11 22:53:00 +00008748CompilerType
8749ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008750{
8751 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8752 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008753 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008754 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008755 return CompilerType();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008756 return CompilerType(ast->getASTContext(),
8757 ast->getASTContext()->getMemberPointerType(ClangUtil::GetQualType(pointee_type),
8758 ClangUtil::GetQualType(type).getTypePtr()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008759 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008760 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008761}
8762
8763
8764size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008765ClangASTContext::ConvertStringToFloatValue (lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008766{
8767 if (type)
8768 {
8769 clang::QualType qual_type (GetCanonicalQualType(type));
8770 uint32_t count = 0;
8771 bool is_complex = false;
8772 if (IsFloatingPointType (type, count, is_complex))
8773 {
8774 // TODO: handle complex and vector types
8775 if (count != 1)
8776 return false;
8777
8778 llvm::StringRef s_sref(s);
8779 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8780
8781 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8782 const uint64_t byte_size = bit_size / 8;
8783 if (dst_size >= byte_size)
8784 {
Ulrich Weigand9521ad22016-04-15 09:55:52 +00008785 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(llvm::NextPowerOf2(byte_size) * 8);
8786 lldb_private::Error get_data_error;
8787 if (scalar.GetAsMemoryData(dst, byte_size, lldb_private::endian::InlHostByteOrder(), get_data_error))
Greg Claytond8d4a572015-08-11 21:38:15 +00008788 return byte_size;
Greg Claytond8d4a572015-08-11 21:38:15 +00008789 }
8790 }
8791 }
8792 return 0;
8793}
8794
8795
8796
8797//----------------------------------------------------------------------
8798// Dumping types
8799//----------------------------------------------------------------------
8800#define DEPTH_INCREMENT 2
8801
8802void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008803ClangASTContext::DumpValue (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00008804 Stream *s,
8805 lldb::Format format,
8806 const lldb_private::DataExtractor &data,
8807 lldb::offset_t data_byte_offset,
8808 size_t data_byte_size,
8809 uint32_t bitfield_bit_size,
8810 uint32_t bitfield_bit_offset,
8811 bool show_types,
8812 bool show_summary,
8813 bool verbose,
8814 uint32_t depth)
8815{
8816 if (!type)
8817 return;
8818
8819 clang::QualType qual_type(GetQualType(type));
8820 switch (qual_type->getTypeClass())
8821 {
8822 case clang::Type::Record:
8823 if (GetCompleteType(type))
8824 {
8825 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8826 const clang::RecordDecl *record_decl = record_type->getDecl();
8827 assert(record_decl);
8828 uint32_t field_bit_offset = 0;
8829 uint32_t field_byte_offset = 0;
8830 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8831 uint32_t child_idx = 0;
8832
8833 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8834 if (cxx_record_decl)
8835 {
8836 // We might have base classes to print out first
8837 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8838 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8839 base_class != base_class_end;
8840 ++base_class)
8841 {
8842 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8843
8844 // Skip empty base classes
8845 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8846 continue;
8847
8848 if (base_class->isVirtual())
8849 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8850 else
8851 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8852 field_byte_offset = field_bit_offset / 8;
8853 assert (field_bit_offset % 8 == 0);
8854 if (child_idx == 0)
8855 s->PutChar('{');
8856 else
8857 s->PutChar(',');
8858
8859 clang::QualType base_class_qual_type = base_class->getType();
8860 std::string base_class_type_name(base_class_qual_type.getAsString());
8861
8862 // Indent and print the base class type name
8863 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8864
8865 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8866
8867 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008868 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008869 base_clang_type.DumpValue (exe_ctx,
8870 s, // Stream to dump to
8871 base_clang_type.GetFormat(), // The format with which to display the member
8872 data, // Data buffer containing all bytes for this type
8873 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8874 base_class_type_info.Width / 8, // Size of this type in bytes
8875 0, // Bitfield bit size
8876 0, // Bitfield bit offset
8877 show_types, // Boolean indicating if we should show the variable types
8878 show_summary, // Boolean indicating if we should show a summary for the current type
8879 verbose, // Verbose output?
8880 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8881
8882 ++child_idx;
8883 }
8884 }
8885 uint32_t field_idx = 0;
8886 clang::RecordDecl::field_iterator field, field_end;
8887 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8888 {
8889 // Print the starting squiggly bracket (if this is the
8890 // first member) or comma (for member 2 and beyond) for
8891 // the struct/union/class member.
8892 if (child_idx == 0)
8893 s->PutChar('{');
8894 else
8895 s->PutChar(',');
8896
8897 // Indent
8898 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8899
8900 clang::QualType field_type = field->getType();
8901 // Print the member type if requested
8902 // Figure out the type byte size (field_type_info.first) and
8903 // alignment (field_type_info.second) from the AST context.
8904 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8905 assert(field_idx < record_layout.getFieldCount());
8906 // Figure out the field offset within the current struct/union/class type
8907 field_bit_offset = record_layout.getFieldOffset (field_idx);
8908 field_byte_offset = field_bit_offset / 8;
8909 uint32_t field_bitfield_bit_size = 0;
8910 uint32_t field_bitfield_bit_offset = 0;
8911 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8912 field_bitfield_bit_offset = field_bit_offset % 8;
8913
8914 if (show_types)
8915 {
8916 std::string field_type_name(field_type.getAsString());
8917 if (field_bitfield_bit_size > 0)
8918 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8919 else
8920 s->Printf("(%s) ", field_type_name.c_str());
8921 }
8922 // Print the member name and equal sign
8923 s->Printf("%s = ", field->getNameAsString().c_str());
8924
8925
8926 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008927 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008928 field_clang_type.DumpValue (exe_ctx,
8929 s, // Stream to dump to
8930 field_clang_type.GetFormat(), // The format with which to display the member
8931 data, // Data buffer containing all bytes for this type
8932 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8933 field_type_info.Width / 8, // Size of this type in bytes
8934 field_bitfield_bit_size, // Bitfield bit size
8935 field_bitfield_bit_offset, // Bitfield bit offset
8936 show_types, // Boolean indicating if we should show the variable types
8937 show_summary, // Boolean indicating if we should show a summary for the current type
8938 verbose, // Verbose output?
8939 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8940 }
8941
8942 // Indent the trailing squiggly bracket
8943 if (child_idx > 0)
8944 s->Printf("\n%*s}", depth, "");
8945 }
8946 return;
8947
8948 case clang::Type::Enum:
8949 if (GetCompleteType(type))
8950 {
8951 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8952 const clang::EnumDecl *enum_decl = enutype->getDecl();
8953 assert(enum_decl);
8954 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8955 lldb::offset_t offset = data_byte_offset;
8956 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8957 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8958 {
8959 if (enum_pos->getInitVal() == enum_value)
8960 {
8961 s->Printf("%s", enum_pos->getNameAsString().c_str());
8962 return;
8963 }
8964 }
8965 // If we have gotten here we didn't get find the enumerator in the
8966 // enum decl, so just print the integer.
8967 s->Printf("%" PRIi64, enum_value);
8968 }
8969 return;
8970
8971 case clang::Type::ConstantArray:
8972 {
8973 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8974 bool is_array_of_characters = false;
8975 clang::QualType element_qual_type = array->getElementType();
8976
8977 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8978 if (canonical_type)
8979 is_array_of_characters = canonical_type->isCharType();
8980
8981 const uint64_t element_count = array->getSize().getLimitedValue();
8982
8983 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8984
8985 uint32_t element_idx = 0;
8986 uint32_t element_offset = 0;
8987 uint64_t element_byte_size = field_type_info.Width / 8;
8988 uint32_t element_stride = element_byte_size;
8989
8990 if (is_array_of_characters)
8991 {
8992 s->PutChar('"');
8993 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8994 s->PutChar('"');
8995 return;
8996 }
8997 else
8998 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008999 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009000 lldb::Format element_format = element_clang_type.GetFormat();
9001
9002 for (element_idx = 0; element_idx < element_count; ++element_idx)
9003 {
9004 // Print the starting squiggly bracket (if this is the
9005 // first member) or comman (for member 2 and beyong) for
9006 // the struct/union/class member.
9007 if (element_idx == 0)
9008 s->PutChar('{');
9009 else
9010 s->PutChar(',');
9011
9012 // Indent and print the index
9013 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9014
9015 // Figure out the field offset within the current struct/union/class type
9016 element_offset = element_idx * element_stride;
9017
9018 // Dump the value of the member
9019 element_clang_type.DumpValue (exe_ctx,
9020 s, // Stream to dump to
9021 element_format, // The format with which to display the element
9022 data, // Data buffer containing all bytes for this type
9023 data_byte_offset + element_offset,// Offset into "data" where to grab value from
9024 element_byte_size, // Size of this type in bytes
9025 0, // Bitfield bit size
9026 0, // Bitfield bit offset
9027 show_types, // Boolean indicating if we should show the variable types
9028 show_summary, // Boolean indicating if we should show a summary for the current type
9029 verbose, // Verbose output?
9030 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
9031 }
9032
9033 // Indent the trailing squiggly bracket
9034 if (element_idx > 0)
9035 s->Printf("\n%*s}", depth, "");
9036 }
9037 }
9038 return;
9039
9040 case clang::Type::Typedef:
9041 {
9042 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
9043
Greg Claytona1e5dc82015-08-11 22:53:00 +00009044 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009045 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9046 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
9047 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9048
9049 return typedef_clang_type.DumpValue (exe_ctx,
9050 s, // Stream to dump to
9051 typedef_format, // The format with which to display the element
9052 data, // Data buffer containing all bytes for this type
9053 data_byte_offset, // Offset into "data" where to grab value from
9054 typedef_byte_size, // Size of this type in bytes
9055 bitfield_bit_size, // Bitfield bit size
9056 bitfield_bit_offset,// Bitfield bit offset
9057 show_types, // Boolean indicating if we should show the variable types
9058 show_summary, // Boolean indicating if we should show a summary for the current type
9059 verbose, // Verbose output?
9060 depth); // Scope depth for any types that have children
9061 }
9062 break;
9063
Enrico Granata36f51e42015-12-18 22:41:25 +00009064 case clang::Type::Auto:
9065 {
9066 clang::QualType elaborated_qual_type = llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9067 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
9068 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9069 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
9070 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9071
9072 return elaborated_clang_type.DumpValue (exe_ctx,
9073 s, // Stream to dump to
9074 elaborated_format, // The format with which to display the element
9075 data, // Data buffer containing all bytes for this type
9076 data_byte_offset, // Offset into "data" where to grab value from
9077 elaborated_byte_size, // Size of this type in bytes
9078 bitfield_bit_size, // Bitfield bit size
9079 bitfield_bit_offset,// Bitfield bit offset
9080 show_types, // Boolean indicating if we should show the variable types
9081 show_summary, // Boolean indicating if we should show a summary for the current type
9082 verbose, // Verbose output?
9083 depth); // Scope depth for any types that have children
9084 }
9085 break;
9086
Greg Claytond8d4a572015-08-11 21:38:15 +00009087 case clang::Type::Elaborated:
9088 {
9089 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00009090 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009091 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9092 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
9093 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9094
9095 return elaborated_clang_type.DumpValue (exe_ctx,
9096 s, // Stream to dump to
9097 elaborated_format, // The format with which to display the element
9098 data, // Data buffer containing all bytes for this type
9099 data_byte_offset, // Offset into "data" where to grab value from
9100 elaborated_byte_size, // Size of this type in bytes
9101 bitfield_bit_size, // Bitfield bit size
9102 bitfield_bit_offset,// Bitfield bit offset
9103 show_types, // Boolean indicating if we should show the variable types
9104 show_summary, // Boolean indicating if we should show a summary for the current type
9105 verbose, // Verbose output?
9106 depth); // Scope depth for any types that have children
9107 }
9108 break;
9109
9110 case clang::Type::Paren:
9111 {
9112 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00009113 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009114
9115 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9116 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
9117 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9118
9119 return desugar_clang_type.DumpValue (exe_ctx,
9120 s, // Stream to dump to
9121 desugar_format, // The format with which to display the element
9122 data, // Data buffer containing all bytes for this type
9123 data_byte_offset, // Offset into "data" where to grab value from
9124 desugar_byte_size, // Size of this type in bytes
9125 bitfield_bit_size, // Bitfield bit size
9126 bitfield_bit_offset,// Bitfield bit offset
9127 show_types, // Boolean indicating if we should show the variable types
9128 show_summary, // Boolean indicating if we should show a summary for the current type
9129 verbose, // Verbose output?
9130 depth); // Scope depth for any types that have children
9131 }
9132 break;
9133
9134 default:
9135 // We are down to a scalar type that we just need to display.
9136 data.Dump(s,
9137 data_byte_offset,
9138 format,
9139 data_byte_size,
9140 1,
9141 UINT32_MAX,
9142 LLDB_INVALID_ADDRESS,
9143 bitfield_bit_size,
9144 bitfield_bit_offset);
9145
9146 if (show_summary)
9147 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9148 break;
9149 }
9150}
9151
9152
9153
9154
9155bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009156ClangASTContext::DumpTypeValue (lldb::opaque_compiler_type_t type, Stream *s,
Greg Claytond8d4a572015-08-11 21:38:15 +00009157 lldb::Format format,
9158 const lldb_private::DataExtractor &data,
9159 lldb::offset_t byte_offset,
9160 size_t byte_size,
9161 uint32_t bitfield_bit_size,
9162 uint32_t bitfield_bit_offset,
9163 ExecutionContextScope *exe_scope)
9164{
9165 if (!type)
9166 return false;
9167 if (IsAggregateType(type))
9168 {
9169 return false;
9170 }
9171 else
9172 {
9173 clang::QualType qual_type(GetQualType(type));
9174
9175 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9176 switch (type_class)
9177 {
9178 case clang::Type::Typedef:
9179 {
9180 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00009181 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009182 if (format == eFormatDefault)
9183 format = typedef_clang_type.GetFormat();
9184 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
9185 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9186
9187 return typedef_clang_type.DumpTypeValue (s,
9188 format, // The format with which to display the element
9189 data, // Data buffer containing all bytes for this type
9190 byte_offset, // Offset into "data" where to grab value from
9191 typedef_byte_size, // Size of this type in bytes
9192 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
9193 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
9194 exe_scope);
9195 }
9196 break;
9197
9198 case clang::Type::Enum:
9199 // If our format is enum or default, show the enumeration value as
9200 // its enumeration string value, else just display it as requested.
9201 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
9202 {
9203 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9204 const clang::EnumDecl *enum_decl = enutype->getDecl();
9205 assert(enum_decl);
9206 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9207 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9208 lldb::offset_t offset = byte_offset;
9209 if (is_signed)
9210 {
9211 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9212 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
9213 {
9214 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
9215 {
9216 s->PutCString (enum_pos->getNameAsString().c_str());
9217 return true;
9218 }
9219 }
9220 // If we have gotten here we didn't get find the enumerator in the
9221 // enum decl, so just print the integer.
9222 s->Printf("%" PRIi64, enum_svalue);
9223 }
9224 else
9225 {
9226 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9227 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
9228 {
9229 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
9230 {
9231 s->PutCString (enum_pos->getNameAsString().c_str());
9232 return true;
9233 }
9234 }
9235 // If we have gotten here we didn't get find the enumerator in the
9236 // enum decl, so just print the integer.
9237 s->Printf("%" PRIu64, enum_uvalue);
9238 }
9239 return true;
9240 }
9241 // format was not enum, just fall through and dump the value as requested....
Jason Molenda62e06812016-02-16 04:14:33 +00009242 LLVM_FALLTHROUGH;
Greg Claytond8d4a572015-08-11 21:38:15 +00009243
9244 default:
9245 // We are down to a scalar type that we just need to display.
9246 {
9247 uint32_t item_count = 1;
9248 // A few formats, we might need to modify our size and count for depending
9249 // on how we are trying to display the value...
9250 switch (format)
9251 {
9252 default:
9253 case eFormatBoolean:
9254 case eFormatBinary:
9255 case eFormatComplex:
9256 case eFormatCString: // NULL terminated C strings
9257 case eFormatDecimal:
9258 case eFormatEnum:
9259 case eFormatHex:
9260 case eFormatHexUppercase:
9261 case eFormatFloat:
9262 case eFormatOctal:
9263 case eFormatOSType:
9264 case eFormatUnsigned:
9265 case eFormatPointer:
9266 case eFormatVectorOfChar:
9267 case eFormatVectorOfSInt8:
9268 case eFormatVectorOfUInt8:
9269 case eFormatVectorOfSInt16:
9270 case eFormatVectorOfUInt16:
9271 case eFormatVectorOfSInt32:
9272 case eFormatVectorOfUInt32:
9273 case eFormatVectorOfSInt64:
9274 case eFormatVectorOfUInt64:
9275 case eFormatVectorOfFloat32:
9276 case eFormatVectorOfFloat64:
9277 case eFormatVectorOfUInt128:
9278 break;
9279
9280 case eFormatChar:
9281 case eFormatCharPrintable:
9282 case eFormatCharArray:
9283 case eFormatBytes:
9284 case eFormatBytesWithASCII:
9285 item_count = byte_size;
9286 byte_size = 1;
9287 break;
9288
9289 case eFormatUnicode16:
9290 item_count = byte_size / 2;
9291 byte_size = 2;
9292 break;
9293
9294 case eFormatUnicode32:
9295 item_count = byte_size / 4;
9296 byte_size = 4;
9297 break;
9298 }
9299 return data.Dump (s,
9300 byte_offset,
9301 format,
9302 byte_size,
9303 item_count,
9304 UINT32_MAX,
9305 LLDB_INVALID_ADDRESS,
9306 bitfield_bit_size,
9307 bitfield_bit_offset,
9308 exe_scope);
9309 }
9310 break;
9311 }
9312 }
9313 return 0;
9314}
9315
9316
9317
9318void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009319ClangASTContext::DumpSummary (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00009320 Stream *s,
9321 const lldb_private::DataExtractor &data,
9322 lldb::offset_t data_byte_offset,
9323 size_t data_byte_size)
9324{
9325 uint32_t length = 0;
9326 if (IsCStringType (type, length))
9327 {
9328 if (exe_ctx)
9329 {
9330 Process *process = exe_ctx->GetProcessPtr();
9331 if (process)
9332 {
9333 lldb::offset_t offset = data_byte_offset;
9334 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9335 std::vector<uint8_t> buf;
9336 if (length > 0)
9337 buf.resize (length);
9338 else
9339 buf.resize (256);
9340
9341 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
9342 buf.back() = '\0';
9343 size_t bytes_read;
9344 size_t total_cstr_len = 0;
9345 Error error;
9346 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
9347 {
9348 const size_t len = strlen((const char *)&buf.front());
9349 if (len == 0)
9350 break;
9351 if (total_cstr_len == 0)
9352 s->PutCString (" \"");
9353 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9354 total_cstr_len += len;
9355 if (len < buf.size())
9356 break;
9357 pointer_address += total_cstr_len;
9358 }
9359 if (total_cstr_len > 0)
9360 s->PutChar ('"');
9361 }
9362 }
9363 }
9364}
9365
9366void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009367ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00009368{
9369 StreamFile s (stdout, false);
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00009370 DumpTypeDescription (type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009371 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
9372 if (metadata)
9373 {
9374 metadata->Dump (&s);
9375 }
9376}
9377
9378void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009379ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type, Stream *s)
Greg Claytond8d4a572015-08-11 21:38:15 +00009380{
9381 if (type)
9382 {
9383 clang::QualType qual_type(GetQualType(type));
9384
9385 llvm::SmallVector<char, 1024> buf;
9386 llvm::raw_svector_ostream llvm_ostrm (buf);
9387
9388 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9389 switch (type_class)
9390 {
9391 case clang::Type::ObjCObject:
9392 case clang::Type::ObjCInterface:
9393 {
9394 GetCompleteType(type);
9395
9396 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9397 assert (objc_class_type);
9398 if (objc_class_type)
9399 {
9400 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9401 if (class_interface_decl)
9402 {
9403 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9404 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9405 }
9406 }
9407 }
9408 break;
9409
9410 case clang::Type::Typedef:
9411 {
9412 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
9413 if (typedef_type)
9414 {
9415 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9416 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
9417 if (!clang_typedef_name.empty())
9418 {
9419 s->PutCString ("typedef ");
9420 s->PutCString (clang_typedef_name.c_str());
9421 }
9422 }
9423 }
9424 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00009425
9426 case clang::Type::Auto:
9427 CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).DumpTypeDescription(s);
9428 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00009429
9430 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009431 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009432 return;
9433
9434 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009435 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009436 return;
9437
9438 case clang::Type::Record:
9439 {
9440 GetCompleteType(type);
9441
9442 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9443 const clang::RecordDecl *record_decl = record_type->getDecl();
9444 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9445
9446 if (cxx_record_decl)
9447 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9448 else
9449 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9450 }
9451 break;
9452
9453 default:
9454 {
9455 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9456 if (tag_type)
9457 {
9458 clang::TagDecl *tag_decl = tag_type->getDecl();
9459 if (tag_decl)
9460 tag_decl->print(llvm_ostrm, 0);
9461 }
9462 else
9463 {
9464 std::string clang_type_name(qual_type.getAsString());
9465 if (!clang_type_name.empty())
9466 s->PutCString (clang_type_name.c_str());
9467 }
9468 }
9469 }
9470
Greg Claytond8d4a572015-08-11 21:38:15 +00009471 if (buf.size() > 0)
9472 {
9473 s->Write (buf.data(), buf.size());
9474 }
9475 }
9476}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009477
Greg Claytone6b36cd2015-12-08 01:02:08 +00009478void
9479ClangASTContext::DumpTypeName (const CompilerType &type)
9480{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009481 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00009482 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009483 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
Greg Claytone6b36cd2015-12-08 01:02:08 +00009484
9485 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9486 switch (type_class)
9487 {
9488 case clang::Type::Record:
9489 {
9490 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
9491 if (cxx_record_decl)
9492 printf("class %s", cxx_record_decl->getName().str().c_str());
9493 }
9494 break;
9495
9496 case clang::Type::Enum:
9497 {
9498 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
9499 if (enum_decl)
9500 {
9501 printf("enum %s", enum_decl->getName().str().c_str());
9502 }
9503 }
9504 break;
9505
9506 case clang::Type::ObjCObject:
9507 case clang::Type::ObjCInterface:
9508 {
9509 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9510 if (objc_class_type)
9511 {
9512 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9513 // We currently can't complete objective C types through the newly added ASTContext
9514 // because it only supports TagDecl objects right now...
9515 if (class_interface_decl)
9516 printf("@class %s", class_interface_decl->getName().str().c_str());
9517 }
9518 }
9519 break;
9520
9521
9522 case clang::Type::Typedef:
9523 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getName().str().c_str());
9524 break;
9525
Enrico Granata36f51e42015-12-18 22:41:25 +00009526 case clang::Type::Auto:
9527 printf("auto ");
9528 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr()));
9529
Greg Claytone6b36cd2015-12-08 01:02:08 +00009530 case clang::Type::Elaborated:
9531 printf("elaborated ");
9532 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
9533
9534 case clang::Type::Paren:
9535 printf("paren ");
9536 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9537
9538 default:
9539 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9540 break;
9541 }
9542 }
9543
9544}
9545
9546
9547
Greg Clayton8b4edba2015-08-14 20:02:05 +00009548clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00009549ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009550 lldb::AccessType access_type,
9551 const char *parent_name,
9552 int tag_decl_kind,
9553 const ClangASTContext::TemplateParameterInfos &template_param_infos)
9554{
9555 if (template_param_infos.IsValid())
9556 {
9557 std::string template_basename(parent_name);
9558 template_basename.erase (template_basename.find('<'));
9559
9560 return CreateClassTemplateDecl (decl_ctx,
9561 access_type,
9562 template_basename.c_str(),
9563 tag_decl_kind,
9564 template_param_infos);
9565 }
9566 return NULL;
9567}
9568
Greg Clayton6dc8d582015-08-18 22:32:36 +00009569void
9570ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
9571{
9572 ClangASTContext *ast = (ClangASTContext *)baton;
9573 SymbolFile *sym_file = ast->GetSymbolFile();
9574 if (sym_file)
9575 {
9576 CompilerType clang_type = GetTypeForDecl (decl);
9577 if (clang_type)
9578 sym_file->CompleteType (clang_type);
9579 }
9580}
9581
9582void
9583ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9584{
9585 ClangASTContext *ast = (ClangASTContext *)baton;
9586 SymbolFile *sym_file = ast->GetSymbolFile();
9587 if (sym_file)
9588 {
9589 CompilerType clang_type = GetTypeForDecl (decl);
9590 if (clang_type)
9591 sym_file->CompleteType (clang_type);
9592 }
9593}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009594
Greg Clayton261ac3f2015-08-28 01:01:03 +00009595DWARFASTParser *
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009596ClangASTContext::GetDWARFParser()
Greg Clayton261ac3f2015-08-28 01:01:03 +00009597{
9598 if (!m_dwarf_ast_parser_ap)
9599 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9600 return m_dwarf_ast_parser_ap.get();
9601}
9602
Zachary Turner42dff792016-04-15 00:21:26 +00009603PDBASTParser *
9604ClangASTContext::GetPDBParser()
9605{
9606 if (!m_pdb_ast_parser_ap)
9607 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9608 return m_pdb_ast_parser_ap.get();
9609}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009610
Greg Clayton8b4edba2015-08-14 20:02:05 +00009611bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009612ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009613 const clang::RecordDecl *record_decl,
9614 uint64_t &bit_size,
9615 uint64_t &alignment,
9616 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9617 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9618 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9619{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009620 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00009621 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009622 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(record_decl, bit_size, alignment, field_offsets,
9623 base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009624}
9625
Greg Clayton99558cc42015-08-24 23:46:31 +00009626//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009627// CompilerDecl override functions
9628//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009629
9630ConstString
9631ClangASTContext::DeclGetName (void *opaque_decl)
9632{
9633 if (opaque_decl)
9634 {
9635 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
9636 if (nd != nullptr)
Greg Claytonfe689042015-11-10 17:47:04 +00009637 return ConstString(nd->getDeclName().getAsString());
Paul Hermand628cbb2015-09-15 23:44:17 +00009638 }
9639 return ConstString();
9640}
9641
Greg Claytonfe689042015-11-10 17:47:04 +00009642ConstString
9643ClangASTContext::DeclGetMangledName (void *opaque_decl)
9644{
9645 if (opaque_decl)
9646 {
9647 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>((clang::Decl*)opaque_decl);
9648 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd))
9649 {
9650 clang::MangleContext *mc = getMangleContext();
9651 if (mc && mc->shouldMangleCXXName(nd))
9652 {
9653 llvm::SmallVector<char, 1024> buf;
9654 llvm::raw_svector_ostream llvm_ostrm (buf);
9655 if (llvm::isa<clang::CXXConstructorDecl>(nd))
9656 {
9657 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), Ctor_Complete, llvm_ostrm);
9658 }
9659 else if (llvm::isa<clang::CXXDestructorDecl>(nd))
9660 {
9661 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), Dtor_Complete, llvm_ostrm);
9662 }
9663 else
9664 {
9665 mc->mangleName(nd, llvm_ostrm);
9666 }
9667 if (buf.size() > 0)
9668 return ConstString(buf.data(), buf.size());
9669 }
9670 }
9671 }
9672 return ConstString();
9673}
9674
9675CompilerDeclContext
9676ClangASTContext::DeclGetDeclContext (void *opaque_decl)
9677{
9678 if (opaque_decl)
9679 return CompilerDeclContext(this, ((clang::Decl*)opaque_decl)->getDeclContext());
9680 else
9681 return CompilerDeclContext();
9682}
9683
9684CompilerType
9685ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl)
9686{
9687 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9688 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9689 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9690 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9691 else
9692 return CompilerType();
9693}
9694
9695size_t
9696ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl)
9697{
9698 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9699 return func_decl->param_size();
9700 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
Zachary Turner9d8a97e2016-04-01 23:20:35 +00009701 return objc_method->param_size();
Greg Claytonfe689042015-11-10 17:47:04 +00009702 else
9703 return 0;
9704}
9705
9706CompilerType
9707ClangASTContext::DeclGetFunctionArgumentType (void *opaque_decl, size_t idx)
9708{
9709 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9710 {
9711 if (idx < func_decl->param_size())
9712 {
9713 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9714 if (var_decl)
Zachary Turner9d8a97e2016-04-01 23:20:35 +00009715 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
Greg Claytonfe689042015-11-10 17:47:04 +00009716 }
9717 }
9718 else if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9719 {
9720 if (idx < objc_method->param_size())
9721 return CompilerType(this, objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9722 }
9723 return CompilerType();
9724}
9725
Paul Hermand628cbb2015-09-15 23:44:17 +00009726//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009727// CompilerDeclContext functions
9728//----------------------------------------------------------------------
9729
Greg Claytondfc09622015-12-08 18:39:50 +00009730std::vector<CompilerDecl>
Siva Chandra03ff5c82016-02-05 19:10:04 +00009731ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx,
9732 ConstString name,
9733 const bool ignore_using_decls)
Paul Hermand628cbb2015-09-15 23:44:17 +00009734{
Greg Claytondfc09622015-12-08 18:39:50 +00009735 std::vector<CompilerDecl> found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009736 if (opaque_decl_ctx)
9737 {
9738 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9739 std::set<DeclContext *> searched;
9740 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009741 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009742
9743 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
9744 {
9745 search_queue.insert(std::make_pair(decl_context, decl_context));
9746
9747 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
9748 {
Eugene Leviantc1ba9fc2015-11-13 11:00:10 +00009749 if (!searched.insert(it->second).second)
9750 continue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009751 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9752
Paul Hermand628cbb2015-09-15 23:44:17 +00009753 for (clang::Decl *child : it->second->decls())
9754 {
Paul Hermanea188fc2015-09-16 18:48:30 +00009755 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00009756 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00009757 if (ignore_using_decls)
9758 continue;
Paul Hermand628cbb2015-09-15 23:44:17 +00009759 clang::DeclContext *from = ud->getCommonAncestor();
9760 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9761 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
9762 }
9763 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9764 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00009765 if (ignore_using_decls)
9766 continue;
Paul Hermand628cbb2015-09-15 23:44:17 +00009767 for (clang::UsingShadowDecl *usd : ud->shadows())
9768 {
9769 clang::Decl *target = usd->getTargetDecl();
9770 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
9771 {
9772 IdentifierInfo *ii = nd->getIdentifier();
9773 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
Greg Claytondfc09622015-12-08 18:39:50 +00009774 found_decls.push_back(CompilerDecl(this, nd));
Paul Hermand628cbb2015-09-15 23:44:17 +00009775 }
9776 }
9777 }
Paul Hermanea188fc2015-09-16 18:48:30 +00009778 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
9779 {
9780 IdentifierInfo *ii = nd->getIdentifier();
9781 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
Greg Claytondfc09622015-12-08 18:39:50 +00009782 found_decls.push_back(CompilerDecl(this, nd));
Paul Hermanea188fc2015-09-16 18:48:30 +00009783 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009784 }
9785 }
9786 }
9787 }
9788 return found_decls;
9789}
9790
Dawn Perchikb5925782015-12-12 19:31:41 +00009791// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9792// and return the number of levels it took to find it, or LLDB_INVALID_DECL_LEVEL
9793// if not found. If the decl was imported via a using declaration, its name and/or
9794// type, if set, will be used to check that the decl found in the scope is a match.
9795//
9796// The optional name is required by languages (like C++) to handle using declarations
9797// like:
9798//
9799// void poo();
9800// namespace ns {
9801// void foo();
9802// void goo();
9803// }
9804// void bar() {
9805// using ns::foo;
9806// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9807// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9808// }
9809//
9810// The optional type is useful in the case that there's a specific overload
9811// that we're looking for that might otherwise be shadowed, like:
9812//
9813// void foo(int);
9814// namespace ns {
9815// void foo();
9816// }
9817// void bar() {
9818// using ns::foo;
9819// // CountDeclLevels returns 0 for { 'foo', void() },
9820// // 1 for { 'foo', void(int) }, and
9821// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9822// }
9823//
9824// NOTE: Because file statics are at the TranslationUnit along with globals, a
9825// function at file scope will return the same level as a function at global scope.
9826// Ideally we'd like to treat the file scope as an additional scope just below the
9827// global scope. More work needs to be done to recognise that, if the decl we're
9828// trying to look up is static, we should compare its source file with that of the
9829// current scope and return a lower number for it.
9830uint32_t
9831ClangASTContext::CountDeclLevels (clang::DeclContext *frame_decl_ctx,
9832 clang::DeclContext *child_decl_ctx,
9833 ConstString *child_name,
9834 CompilerType *child_type)
9835{
9836 if (frame_decl_ctx)
9837 {
9838 std::set<DeclContext *> searched;
9839 std::multimap<DeclContext *, DeclContext *> search_queue;
9840 SymbolFile *symbol_file = GetSymbolFile();
9841
9842 // Get the lookup scope for the decl we're trying to find.
9843 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9844
9845 // Look for it in our scope's decl context and its parents.
9846 uint32_t level = 0;
9847 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; decl_ctx = decl_ctx->getParent())
9848 {
9849 if (!decl_ctx->isLookupContext())
9850 continue;
9851 if (decl_ctx == parent_decl_ctx)
9852 // Found it!
9853 return level;
9854 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9855 for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); it++)
9856 {
9857 if (searched.find(it->second) != searched.end())
9858 continue;
Sean Callanan8c05fb92016-02-12 21:55:05 +00009859
9860 // Currently DWARF has one shared translation unit for all Decls at top level, so this
9861 // would erroneously find using statements anywhere. So don't look at the top-level
9862 // translation unit.
9863 // TODO fix this and add a testcase that depends on it.
9864
9865 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9866 continue;
9867
Dawn Perchikb5925782015-12-12 19:31:41 +00009868 searched.insert(it->second);
9869 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9870
9871 for (clang::Decl *child : it->second->decls())
9872 {
9873 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
9874 {
9875 clang::DeclContext *ns = ud->getNominatedNamespace();
9876 if (ns == parent_decl_ctx)
9877 // Found it!
9878 return level;
9879 clang::DeclContext *from = ud->getCommonAncestor();
9880 if (searched.find(ns) == searched.end())
9881 search_queue.insert(std::make_pair(from, ns));
9882 }
9883 else if (child_name)
9884 {
9885 if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9886 {
9887 for (clang::UsingShadowDecl *usd : ud->shadows())
9888 {
9889 clang::Decl *target = usd->getTargetDecl();
9890 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9891 if (!nd)
9892 continue;
9893 // Check names.
9894 IdentifierInfo *ii = nd->getIdentifier();
9895 if (ii == nullptr || !ii->getName().equals(child_name->AsCString(nullptr)))
9896 continue;
9897 // Check types, if one was provided.
9898 if (child_type)
9899 {
9900 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9901 if (!AreTypesSame(clang_type, *child_type, /*ignore_qualifiers=*/true))
9902 continue;
9903 }
9904 // Found it!
9905 return level;
9906 }
9907 }
9908 }
9909 }
9910 }
9911 ++level;
9912 }
9913 }
9914 return LLDB_INVALID_DECL_LEVEL;
9915}
9916
Greg Clayton99558cc42015-08-24 23:46:31 +00009917bool
9918ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009919{
Greg Clayton99558cc42015-08-24 23:46:31 +00009920 if (opaque_decl_ctx)
9921 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9922 else
9923 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009924}
9925
Greg Clayton99558cc42015-08-24 23:46:31 +00009926ConstString
9927ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009928{
Greg Clayton99558cc42015-08-24 23:46:31 +00009929 if (opaque_decl_ctx)
9930 {
9931 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9932 if (named_decl)
9933 return ConstString(named_decl->getName());
9934 }
9935 return ConstString();
9936}
9937
Siva Chandra9293fc42016-01-07 23:32:34 +00009938ConstString
9939ClangASTContext::DeclContextGetScopeQualifiedName (void *opaque_decl_ctx)
9940{
9941 if (opaque_decl_ctx)
9942 {
9943 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9944 if (named_decl)
9945 return ConstString(llvm::StringRef(named_decl->getQualifiedNameAsString()));
9946 }
9947 return ConstString();
9948}
9949
Greg Clayton99558cc42015-08-24 23:46:31 +00009950bool
9951ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9952 lldb::LanguageType *language_ptr,
9953 bool *is_instance_method_ptr,
9954 ConstString *language_object_name_ptr)
9955{
9956 if (opaque_decl_ctx)
9957 {
9958 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9959 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9960 {
9961 if (is_instance_method_ptr)
9962 *is_instance_method_ptr = objc_method->isInstanceMethod();
9963 if (language_ptr)
9964 *language_ptr = eLanguageTypeObjC;
9965 if (language_object_name_ptr)
9966 language_object_name_ptr->SetCString("self");
9967 return true;
9968 }
9969 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9970 {
9971 if (is_instance_method_ptr)
9972 *is_instance_method_ptr = cxx_method->isInstance();
9973 if (language_ptr)
9974 *language_ptr = eLanguageTypeC_plus_plus;
9975 if (language_object_name_ptr)
9976 language_object_name_ptr->SetCString("this");
9977 return true;
9978 }
9979 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9980 {
9981 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9982 if (metadata && metadata->HasObjectPtr())
9983 {
9984 if (is_instance_method_ptr)
9985 *is_instance_method_ptr = true;
9986 if (language_ptr)
9987 *language_ptr = eLanguageTypeObjC;
9988 if (language_object_name_ptr)
9989 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9990 return true;
9991 }
9992 }
9993 }
9994 return false;
9995}
9996
9997clang::DeclContext *
9998ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9999{
10000 if (dc.IsClang())
10001 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10002 return nullptr;
10003}
10004
10005
10006ObjCMethodDecl *
10007ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
10008{
10009 if (dc.IsClang())
10010 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10011 return nullptr;
10012}
10013
10014CXXMethodDecl *
10015ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
10016{
10017 if (dc.IsClang())
10018 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10019 return nullptr;
10020}
10021
10022clang::FunctionDecl *
10023ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
10024{
10025 if (dc.IsClang())
10026 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10027 return nullptr;
10028}
10029
10030clang::NamespaceDecl *
10031ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
10032{
10033 if (dc.IsClang())
10034 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
10035 return nullptr;
10036}
10037
10038ClangASTMetadata *
10039ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
10040{
10041 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
10042 if (ast)
10043 return ClangASTContext::GetMetadata (ast, object);
10044 return nullptr;
10045}
10046
10047clang::ASTContext *
10048ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
10049{
Greg Claytonf73034f2015-09-08 18:15:05 +000010050 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10051 if (ast)
10052 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +000010053 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +000010054}
10055
Jim Ingham151c0322015-09-15 21:13:50 +000010056ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
10057 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010058 m_target_wp(target.shared_from_this()),
10059 m_persistent_variables (new ClangPersistentVariables)
Jim Ingham151c0322015-09-15 21:13:50 +000010060{
10061}
10062
10063UserExpression *
10064ClangASTContextForExpressions::GetUserExpression (const char *expr,
10065 const char *expr_prefix,
10066 lldb::LanguageType language,
Jim Ingham19a63fc2015-11-03 02:11:24 +000010067 Expression::ResultType desired_type,
10068 const EvaluateExpressionOptions &options)
Jim Ingham151c0322015-09-15 21:13:50 +000010069{
10070 TargetSP target_sp = m_target_wp.lock();
10071 if (!target_sp)
10072 return nullptr;
10073
Jim Ingham19a63fc2015-11-03 02:11:24 +000010074 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +000010075}
10076
10077FunctionCaller *
10078ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
10079 const Address& function_address,
10080 const ValueList &arg_value_list,
10081 const char *name)
10082{
10083 TargetSP target_sp = m_target_wp.lock();
10084 if (!target_sp)
10085 return nullptr;
10086
10087 Process *process = target_sp->GetProcessSP().get();
10088 if (!process)
10089 return nullptr;
10090
10091 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
10092}
10093
10094UtilityFunction *
10095ClangASTContextForExpressions::GetUtilityFunction (const char *text,
10096 const char *name)
10097{
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010098 TargetSP target_sp = m_target_wp.lock();
Jim Ingham151c0322015-09-15 21:13:50 +000010099 if (!target_sp)
10100 return nullptr;
10101
10102 return new ClangUtilityFunction(*target_sp.get(), text, name);
10103}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010104
10105PersistentExpressionState *
10106ClangASTContextForExpressions::GetPersistentExpressionState ()
10107{
10108 return m_persistent_variables.get();
10109}