blob: b2050fb08cfa6762537035101efae49d7c9b17d4 [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"
Greg Claytond8d4a572015-08-11 21:38:15 +000075#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000076#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000077#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000078#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000079#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000080#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000081#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000082#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000083#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000084#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000085#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000086#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000087#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000088#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000089#include "lldb/Target/Process.h"
90#include "lldb/Target/Target.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000091
Greg Clayton261ac3f2015-08-28 01:01:03 +000092#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
93
Eli Friedman932197d2010-06-13 19:06:42 +000094#include <stdio.h>
95
Greg Clayton1341baf2013-07-11 23:36:31 +000096#include <mutex>
97
Greg Claytonc86103d2010-08-05 01:57:25 +000098using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099using namespace lldb_private;
100using namespace llvm;
101using namespace clang;
102
Greg Clayton56939cb2015-09-17 22:23:34 +0000103namespace
104{
105 static inline bool ClangASTContextSupportsLanguage (lldb::LanguageType language)
106 {
107 return language == eLanguageTypeUnknown || // Clang is the default type system
108 Language::LanguageIsC (language) ||
109 Language::LanguageIsCPlusPlus (language) ||
Ewan Crawford75f0ff52016-02-03 09:17:03 +0000110 Language::LanguageIsObjC (language) ||
111 language == eLanguageTypeExtRenderScript;
Greg Clayton56939cb2015-09-17 22:23:34 +0000112 }
113}
114
Enrico Granata2267ad42014-09-16 17:28:40 +0000115typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext*> ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000116
117static ClangASTMap &
118GetASTMap()
119{
Enrico Granata2267ad42014-09-16 17:28:40 +0000120 static ClangASTMap *g_map_ptr = nullptr;
121 static std::once_flag g_once_flag;
122 std::call_once(g_once_flag, []() {
123 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
124 });
125 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000126}
127
128
Greg Clayton57ee3062013-07-11 22:46:58 +0000129clang::AccessSpecifier
130ClangASTContext::ConvertAccessTypeToAccessSpecifier (AccessType access)
Greg Clayton8cf05932010-07-22 18:30:50 +0000131{
132 switch (access)
133 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000134 default: break;
135 case eAccessNone: return AS_none;
136 case eAccessPublic: return AS_public;
137 case eAccessPrivate: return AS_private;
138 case eAccessProtected: return AS_protected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000139 }
140 return AS_none;
141}
142
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143static void
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000144ParseLangArgs (LangOptions &Opts, InputKind IK, const char* triple)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145{
146 // FIXME: Cleanup per-file based stuff.
147
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000148 // Set some properties which depend solely on the input kind; it would be nice
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 // to move these to the language standard, and have the driver resolve the
150 // input kind + language standard.
Greg Clayton94e5d782010-06-13 17:34:29 +0000151 if (IK == IK_Asm) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 Opts.AsmPreprocessor = 1;
Greg Clayton94e5d782010-06-13 17:34:29 +0000153 } else if (IK == IK_ObjC ||
154 IK == IK_ObjCXX ||
155 IK == IK_PreprocessedObjC ||
156 IK == IK_PreprocessedObjCXX) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 Opts.ObjC1 = Opts.ObjC2 = 1;
158 }
159
160 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
161
162 if (LangStd == LangStandard::lang_unspecified) {
163 // Based on the base language, pick one.
164 switch (IK) {
Greg Clayton94e5d782010-06-13 17:34:29 +0000165 case IK_None:
166 case IK_AST:
Sean Callananfb0b7582011-03-15 00:17:19 +0000167 case IK_LLVM_IR:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000168 assert (!"Invalid input kind!");
Greg Clayton94e5d782010-06-13 17:34:29 +0000169 case IK_OpenCL:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 LangStd = LangStandard::lang_opencl;
171 break;
Sean Callananfb0b7582011-03-15 00:17:19 +0000172 case IK_CUDA:
Artem Belevich52210ae2015-03-19 18:12:26 +0000173 case IK_PreprocessedCuda:
Sean Callananfb0b7582011-03-15 00:17:19 +0000174 LangStd = LangStandard::lang_cuda;
175 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000176 case IK_Asm:
177 case IK_C:
178 case IK_PreprocessedC:
179 case IK_ObjC:
180 case IK_PreprocessedObjC:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 LangStd = LangStandard::lang_gnu99;
182 break;
Greg Clayton94e5d782010-06-13 17:34:29 +0000183 case IK_CXX:
184 case IK_PreprocessedCXX:
185 case IK_ObjCXX:
186 case IK_PreprocessedObjCXX:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 LangStd = LangStandard::lang_gnucxx98;
188 break;
189 }
190 }
191
192 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
Filipe Cabecinhase818ca22012-11-12 21:26:32 +0000193 Opts.LineComment = Std.hasLineComments();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 Opts.C99 = Std.isC99();
195 Opts.CPlusPlus = Std.isCPlusPlus();
Chandler Carruth38336a12013-01-02 12:55:00 +0000196 Opts.CPlusPlus11 = Std.isCPlusPlus11();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 Opts.Digraphs = Std.hasDigraphs();
198 Opts.GNUMode = Std.isGNUMode();
199 Opts.GNUInline = !Std.isC99();
200 Opts.HexFloats = Std.hasHexFloats();
201 Opts.ImplicitInt = Std.hasImplicitInt();
Enrico Granatac921e342013-01-10 02:37:22 +0000202
203 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
205 // OpenCL has some additional defaults.
206 if (LangStd == LangStandard::lang_opencl) {
207 Opts.OpenCL = 1;
208 Opts.AltiVec = 1;
209 Opts.CXXOperatorNames = 1;
210 Opts.LaxVectorConversions = 1;
211 }
212
213 // OpenCL and C++ both have bool, true, false keywords.
214 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
215
216// if (Opts.CPlusPlus)
217// Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
218//
219// if (Args.hasArg(OPT_fobjc_gc_only))
220// Opts.setGCMode(LangOptions::GCOnly);
221// else if (Args.hasArg(OPT_fobjc_gc))
222// Opts.setGCMode(LangOptions::HybridGC);
223//
224// if (Args.hasArg(OPT_print_ivar_layout))
225// Opts.ObjCGCBitmapPrint = 1;
226//
227// if (Args.hasArg(OPT_faltivec))
228// Opts.AltiVec = 1;
229//
230// if (Args.hasArg(OPT_pthread))
231// Opts.POSIXThreads = 1;
232//
233// llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
234// "default");
235// if (Vis == "default")
Sean Callanan37f76e52013-02-19 19:16:37 +0000236 Opts.setValueVisibilityMode(DefaultVisibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237// else if (Vis == "hidden")
238// Opts.setVisibilityMode(LangOptions::Hidden);
239// else if (Vis == "protected")
240// Opts.setVisibilityMode(LangOptions::Protected);
241// else
242// Diags.Report(diag::err_drv_invalid_value)
243// << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
244
245// Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
246
247 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
248 // is specified, or -std is set to a conforming mode.
249 Opts.Trigraphs = !Opts.GNUMode;
250// if (Args.hasArg(OPT_trigraphs))
251// Opts.Trigraphs = 1;
252//
253// Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
254// OPT_fno_dollars_in_identifiers,
255// !Opts.AsmPreprocessor);
256// Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
257// Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
258// Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
259// if (Args.hasArg(OPT_fno_lax_vector_conversions))
260// Opts.LaxVectorConversions = 0;
261// Opts.Exceptions = Args.hasArg(OPT_fexceptions);
262// Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
263// Opts.Blocks = Args.hasArg(OPT_fblocks);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000264 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265// Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
266// Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
267// Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
268// Opts.AssumeSaneOperatorNew = !Args.hasArg(OPT_fno_assume_sane_operator_new);
269// Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
270// Opts.AccessControl = Args.hasArg(OPT_faccess_control);
271// Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
272// Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
273// Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth, 99,
274// Diags);
275// Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
276// Opts.ObjCConstantStringClass = getLastArgValue(Args,
277// OPT_fconstant_string_class);
278// Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
279// Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
280// Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
281// Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
282// Opts.Static = Args.hasArg(OPT_static_define);
283 Opts.OptimizeSize = 0;
284
285 // FIXME: Eliminate this dependency.
286// unsigned Opt =
287// Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
288// Opts.Optimize = Opt != 0;
289 unsigned Opt = 0;
290
291 // This is the __NO_INLINE__ define, which just depends on things like the
292 // optimization level and -fno-inline, not actually whether the backend has
293 // inlining enabled.
294 //
295 // FIXME: This is affected by other options (-fno-inline).
Sean Callanan3d654b32012-09-24 22:25:51 +0000296 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297
298// unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
299// switch (SSP) {
300// default:
301// Diags.Report(diag::err_drv_invalid_value)
302// << Args.getLastArg(OPT_stack_protector)->getAsString(Args) << SSP;
303// break;
304// case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
305// case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
306// case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
307// }
308}
309
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310
Greg Claytonf73034f2015-09-08 18:15:05 +0000311ClangASTContext::ClangASTContext (const char *target_triple) :
312 TypeSystem (TypeSystem::eKindClang),
Greg Clayton6dc8d582015-08-18 22:32:36 +0000313 m_target_triple (),
314 m_ast_ap (),
315 m_language_options_ap (),
316 m_source_manager_ap (),
317 m_diagnostics_engine_ap (),
318 m_target_options_rp (),
319 m_target_info_ap (),
320 m_identifier_table_ap (),
321 m_selector_table_ap (),
322 m_builtins_ap (),
Ed Masted4612ad2014-04-20 13:17:36 +0000323 m_callback_tag_decl (nullptr),
324 m_callback_objc_decl (nullptr),
325 m_callback_baton (nullptr),
Greg Claytond8d4a572015-08-11 21:38:15 +0000326 m_pointer_byte_size (0),
Greg Clayton261ac3f2015-08-28 01:01:03 +0000327 m_ast_owned (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328{
329 if (target_triple && target_triple[0])
Greg Clayton880cbb02011-07-30 01:26:02 +0000330 SetTargetTriple (target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331}
332
333//----------------------------------------------------------------------
334// Destructor
335//----------------------------------------------------------------------
336ClangASTContext::~ClangASTContext()
337{
Jim Ingham0ea010a2016-02-12 00:03:19 +0000338 Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339}
340
Greg Clayton56939cb2015-09-17 22:23:34 +0000341ConstString
342ClangASTContext::GetPluginNameStatic()
343{
344 return ConstString("clang");
345}
346
347ConstString
348ClangASTContext::GetPluginName()
349{
350 return ClangASTContext::GetPluginNameStatic();
351}
352
353uint32_t
354ClangASTContext::GetPluginVersion()
355{
356 return 1;
357}
358
359lldb::TypeSystemSP
Tamas Berghammer3a6b82b2015-10-09 12:06:10 +0000360ClangASTContext::CreateInstance (lldb::LanguageType language,
361 lldb_private::Module *module,
362 Target *target)
Greg Clayton56939cb2015-09-17 22:23:34 +0000363{
364 if (ClangASTContextSupportsLanguage(language))
365 {
Greg Clayton5beec212015-10-08 21:04:34 +0000366 ArchSpec arch;
367 if (module)
368 arch = module->GetArchitecture();
369 else if (target)
370 arch = target->GetArchitecture();
371
372 if (arch.IsValid())
Greg Clayton56939cb2015-09-17 22:23:34 +0000373 {
Greg Clayton5beec212015-10-08 21:04:34 +0000374 ArchSpec fixed_arch = arch;
375 // LLVM wants this to be set to iOS or MacOSX; if we're working on
376 // a bare-boards type image, change the triple for llvm's benefit.
377 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
378 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS)
Greg Clayton56939cb2015-09-17 22:23:34 +0000379 {
Greg Clayton5beec212015-10-08 21:04:34 +0000380 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
381 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
382 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb)
Greg Clayton56939cb2015-09-17 22:23:34 +0000383 {
Greg Clayton5beec212015-10-08 21:04:34 +0000384 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
Greg Clayton56939cb2015-09-17 22:23:34 +0000385 }
Greg Clayton5beec212015-10-08 21:04:34 +0000386 else
387 {
388 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
389 }
390 }
391
392 if (module)
393 {
394 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
395 if (ast_sp)
396 {
397 ast_sp->SetArchitecture (fixed_arch);
398 }
399 return ast_sp;
400 }
Sean Callanana3444ff2015-11-10 22:54:42 +0000401 else if (target && target->IsValid())
Greg Clayton5beec212015-10-08 21:04:34 +0000402 {
403 std::shared_ptr<ClangASTContextForExpressions> ast_sp(new ClangASTContextForExpressions(*target));
404 if (ast_sp)
405 {
406 ast_sp->SetArchitecture(fixed_arch);
407 ast_sp->m_scratch_ast_source_ap.reset (new ClangASTSource(target->shared_from_this()));
408 ast_sp->m_scratch_ast_source_ap->InstallASTContext(ast_sp->getASTContext());
409 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(ast_sp->m_scratch_ast_source_ap->CreateProxy());
410 ast_sp->SetExternalSource(proxy_ast_source);
411 return ast_sp;
412 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000413 }
414 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000415 }
416 return lldb::TypeSystemSP();
417}
418
Sean Callananfe38c852015-10-08 23:07:53 +0000419void
420ClangASTContext::EnumerateSupportedLanguages(std::set<lldb::LanguageType> &languages_for_types, std::set<lldb::LanguageType> &languages_for_expressions)
421{
422 static std::vector<lldb::LanguageType> s_supported_languages_for_types({
423 lldb::eLanguageTypeC89,
424 lldb::eLanguageTypeC,
425 lldb::eLanguageTypeC11,
426 lldb::eLanguageTypeC_plus_plus,
427 lldb::eLanguageTypeC99,
428 lldb::eLanguageTypeObjC,
429 lldb::eLanguageTypeObjC_plus_plus,
430 lldb::eLanguageTypeC_plus_plus_03,
431 lldb::eLanguageTypeC_plus_plus_11,
432 lldb::eLanguageTypeC11,
433 lldb::eLanguageTypeC_plus_plus_14});
434
435 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions({
436 lldb::eLanguageTypeC_plus_plus,
437 lldb::eLanguageTypeObjC_plus_plus,
438 lldb::eLanguageTypeC_plus_plus_03,
439 lldb::eLanguageTypeC_plus_plus_11,
440 lldb::eLanguageTypeC_plus_plus_14});
441
442 languages_for_types.insert(s_supported_languages_for_types.begin(), s_supported_languages_for_types.end());
443 languages_for_expressions.insert(s_supported_languages_for_expressions.begin(), s_supported_languages_for_expressions.end());
444}
445
Greg Clayton56939cb2015-09-17 22:23:34 +0000446
447void
448ClangASTContext::Initialize()
449{
450 PluginManager::RegisterPlugin (GetPluginNameStatic(),
451 "clang base AST context plug-in",
Sean Callananfe38c852015-10-08 23:07:53 +0000452 CreateInstance,
453 EnumerateSupportedLanguages);
Greg Clayton56939cb2015-09-17 22:23:34 +0000454}
455
456void
457ClangASTContext::Terminate()
458{
459 PluginManager::UnregisterPlugin (CreateInstance);
460}
461
Jim Ingham0ea010a2016-02-12 00:03:19 +0000462void
463ClangASTContext::Finalize()
464{
465 if (m_ast_ap.get())
466 {
467 GetASTMap().Erase(m_ast_ap.get());
468 if (!m_ast_owned)
469 m_ast_ap.release();
470 }
471
472 m_builtins_ap.reset();
473 m_selector_table_ap.reset();
474 m_identifier_table_ap.reset();
475 m_target_info_ap.reset();
476 m_target_options_rp.reset();
477 m_diagnostics_engine_ap.reset();
478 m_source_manager_ap.reset();
479 m_language_options_ap.reset();
480 m_ast_ap.reset();
481 m_scratch_ast_source_ap.reset();
482}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483
484void
485ClangASTContext::Clear()
486{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000487 m_ast_ap.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 m_language_options_ap.reset();
489 m_source_manager_ap.reset();
Sean Callanan880e6802011-10-07 23:18:13 +0000490 m_diagnostics_engine_ap.reset();
Sean Callananc5069ad2012-10-17 22:11:14 +0000491 m_target_options_rp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 m_target_info_ap.reset();
493 m_identifier_table_ap.reset();
494 m_selector_table_ap.reset();
495 m_builtins_ap.reset();
Greg Clayton57ee3062013-07-11 22:46:58 +0000496 m_pointer_byte_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
499const char *
500ClangASTContext::GetTargetTriple ()
501{
502 return m_target_triple.c_str();
503}
504
505void
506ClangASTContext::SetTargetTriple (const char *target_triple)
507{
508 Clear();
509 m_target_triple.assign(target_triple);
510}
511
Greg Clayton514487e2011-02-15 21:59:32 +0000512void
513ClangASTContext::SetArchitecture (const ArchSpec &arch)
514{
Greg Clayton880cbb02011-07-30 01:26:02 +0000515 SetTargetTriple(arch.GetTriple().str().c_str());
Greg Clayton514487e2011-02-15 21:59:32 +0000516}
517
Greg Clayton6beaaa62011-01-17 03:46:26 +0000518bool
519ClangASTContext::HasExternalSource ()
520{
521 ASTContext *ast = getASTContext();
522 if (ast)
Ed Masted4612ad2014-04-20 13:17:36 +0000523 return ast->getExternalSource () != nullptr;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000524 return false;
525}
526
527void
Todd Fiala955fe6f2014-02-27 17:18:23 +0000528ClangASTContext::SetExternalSource (llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000529{
530 ASTContext *ast = getASTContext();
531 if (ast)
532 {
533 ast->setExternalSource (ast_source_ap);
534 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
535 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
536 }
537}
538
539void
540ClangASTContext::RemoveExternalSource ()
541{
542 ASTContext *ast = getASTContext();
543
544 if (ast)
545 {
Todd Fiala955fe6f2014-02-27 17:18:23 +0000546 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000547 ast->setExternalSource (empty_ast_source_ap);
548 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
549 //ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
550 }
551}
552
Greg Claytond8d4a572015-08-11 21:38:15 +0000553void
554ClangASTContext::setASTContext(clang::ASTContext *ast_ctx)
555{
556 if (!m_ast_owned) {
557 m_ast_ap.release();
558 }
559 m_ast_owned = false;
560 m_ast_ap.reset(ast_ctx);
561 GetASTMap().Insert(ast_ctx, this);
562}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000563
564ASTContext *
565ClangASTContext::getASTContext()
566{
Ed Masted4612ad2014-04-20 13:17:36 +0000567 if (m_ast_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 {
Greg Claytond8d4a572015-08-11 21:38:15 +0000569 m_ast_owned = true;
Greg Clayton6beaaa62011-01-17 03:46:26 +0000570 m_ast_ap.reset(new ASTContext (*getLanguageOptions(),
571 *getSourceManager(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000572 *getIdentifierTable(),
573 *getSelectorTable(),
Alp Tokercf55e882014-05-03 15:05:45 +0000574 *getBuiltinContext()));
Sean Callanan6d61b632015-04-09 17:42:48 +0000575
576 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
Greg Clayton28eb7bf2015-05-07 00:07:44 +0000577
578 // This can be NULL if we don't know anything about the architecture or if the
579 // target for an architecture isn't enabled in the llvm/clang that we built
580 TargetInfo *target_info = getTargetInfo();
581 if (target_info)
582 m_ast_ap->InitBuiltinTypes(*target_info);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000583
Greg Clayton6beaaa62011-01-17 03:46:26 +0000584 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton)
585 {
586 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
587 //m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
588 }
589
Enrico Granata2267ad42014-09-16 17:28:40 +0000590 GetASTMap().Insert(m_ast_ap.get(), this);
Greg Clayton6dc8d582015-08-18 22:32:36 +0000591
592 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap (new ClangExternalASTSourceCallbacks (ClangASTContext::CompleteTagDecl,
593 ClangASTContext::CompleteObjCInterfaceDecl,
594 nullptr,
595 ClangASTContext::LayoutRecordType,
596 this));
597 SetExternalSource (ast_source_ap);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000599 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Enrico Granata5d84a692014-08-19 21:46:37 +0000602ClangASTContext*
603ClangASTContext::GetASTContext (clang::ASTContext* ast)
604{
Enrico Granata2267ad42014-09-16 17:28:40 +0000605 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
Enrico Granata5d84a692014-08-19 21:46:37 +0000606 return clang_ast;
607}
608
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609Builtin::Context *
610ClangASTContext::getBuiltinContext()
611{
Ed Masted4612ad2014-04-20 13:17:36 +0000612 if (m_builtins_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000613 m_builtins_ap.reset (new Builtin::Context());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 return m_builtins_ap.get();
615}
616
617IdentifierTable *
618ClangASTContext::getIdentifierTable()
619{
Ed Masted4612ad2014-04-20 13:17:36 +0000620 if (m_identifier_table_ap.get() == nullptr)
621 m_identifier_table_ap.reset(new IdentifierTable (*ClangASTContext::getLanguageOptions(), nullptr));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622 return m_identifier_table_ap.get();
623}
624
625LangOptions *
626ClangASTContext::getLanguageOptions()
627{
Ed Masted4612ad2014-04-20 13:17:36 +0000628 if (m_language_options_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
630 m_language_options_ap.reset(new LangOptions());
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +0000631 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
Greg Clayton94e5d782010-06-13 17:34:29 +0000632// InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634 return m_language_options_ap.get();
635}
636
637SelectorTable *
638ClangASTContext::getSelectorTable()
639{
Ed Masted4612ad2014-04-20 13:17:36 +0000640 if (m_selector_table_ap.get() == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 m_selector_table_ap.reset (new SelectorTable());
642 return m_selector_table_ap.get();
643}
644
Sean Callanan79439e82010-11-18 02:56:27 +0000645clang::FileManager *
646ClangASTContext::getFileManager()
647{
Ed Masted4612ad2014-04-20 13:17:36 +0000648 if (m_file_manager_ap.get() == nullptr)
Greg Clayton38a61402010-12-02 23:20:03 +0000649 {
650 clang::FileSystemOptions file_system_options;
651 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
652 }
Sean Callanan79439e82010-11-18 02:56:27 +0000653 return m_file_manager_ap.get();
654}
655
Greg Claytone1a916a2010-07-21 22:12:05 +0000656clang::SourceManager *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657ClangASTContext::getSourceManager()
658{
Ed Masted4612ad2014-04-20 13:17:36 +0000659 if (m_source_manager_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000660 m_source_manager_ap.reset(new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661 return m_source_manager_ap.get();
662}
663
Sean Callanan880e6802011-10-07 23:18:13 +0000664clang::DiagnosticsEngine *
665ClangASTContext::getDiagnosticsEngine()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666{
Ed Masted4612ad2014-04-20 13:17:36 +0000667 if (m_diagnostics_engine_ap.get() == nullptr)
Greg Claytona651b532010-11-19 21:46:54 +0000668 {
669 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
Sean Callananec8f1ef2012-10-25 01:00:25 +0000670 m_diagnostics_engine_ap.reset(new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
Greg Claytona651b532010-11-19 21:46:54 +0000671 }
Sean Callanan880e6802011-10-07 23:18:13 +0000672 return m_diagnostics_engine_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673}
674
Greg Claytonfe689042015-11-10 17:47:04 +0000675clang::MangleContext *
676ClangASTContext::getMangleContext()
677{
678 if (m_mangle_ctx_ap.get() == nullptr)
679 m_mangle_ctx_ap.reset (getASTContext()->createMangleContext());
680 return m_mangle_ctx_ap.get();
681}
682
Sean Callanan880e6802011-10-07 23:18:13 +0000683class NullDiagnosticConsumer : public DiagnosticConsumer
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000684{
685public:
Sean Callanan880e6802011-10-07 23:18:13 +0000686 NullDiagnosticConsumer ()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000687 {
688 m_log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
689 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000690
691 void
692 HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info)
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000693 {
694 if (m_log)
695 {
Sean Callanan5b26f272012-02-04 08:49:35 +0000696 llvm::SmallVector<char, 32> diag_str(10);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000697 info.FormatDiagnostic(diag_str);
698 diag_str.push_back('\0');
699 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
700 }
701 }
Sean Callanan880e6802011-10-07 23:18:13 +0000702
703 DiagnosticConsumer *clone (DiagnosticsEngine &Diags) const
704 {
705 return new NullDiagnosticConsumer ();
706 }
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000707private:
Greg Clayton5160ce52013-03-27 23:08:40 +0000708 Log * m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000709};
710
Sean Callanan880e6802011-10-07 23:18:13 +0000711DiagnosticConsumer *
712ClangASTContext::getDiagnosticConsumer()
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000713{
Ed Masted4612ad2014-04-20 13:17:36 +0000714 if (m_diagnostic_consumer_ap.get() == nullptr)
Sean Callanan880e6802011-10-07 23:18:13 +0000715 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000716
Sean Callanan880e6802011-10-07 23:18:13 +0000717 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000718}
719
Jason Molenda45938b92014-07-08 23:46:39 +0000720std::shared_ptr<TargetOptions> &
721ClangASTContext::getTargetOptions() {
Alp Tokeredc902f2014-07-05 03:06:05 +0000722 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 {
Alp Toker5f838642014-07-06 05:36:57 +0000724 m_target_options_rp = std::make_shared<TargetOptions>();
Alp Tokeredc902f2014-07-05 03:06:05 +0000725 if (m_target_options_rp.get() != nullptr)
Sean Callananc5069ad2012-10-17 22:11:14 +0000726 m_target_options_rp->Triple = m_target_triple;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Alp Toker5f838642014-07-06 05:36:57 +0000728 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729}
730
731
732TargetInfo *
733ClangASTContext::getTargetInfo()
734{
Greg Clayton70512312012-05-08 01:45:38 +0000735 // target_triple should be something like "x86_64-apple-macosx"
Ed Masted4612ad2014-04-20 13:17:36 +0000736 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
Greg Clayton38d880a2012-11-16 21:35:22 +0000737 m_target_info_ap.reset (TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), getTargetOptions()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 return m_target_info_ap.get();
739}
740
741#pragma mark Basic Types
742
743static inline bool
Greg Clayton6beaaa62011-01-17 03:46:26 +0000744QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext *ast, QualType qual_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000746 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 if (qual_type_bit_size == bit_size)
748 return true;
749 return false;
750}
Greg Clayton56939cb2015-09-17 22:23:34 +0000751
Greg Claytona1e5dc82015-08-11 22:53:00 +0000752CompilerType
Greg Clayton56939cb2015-09-17 22:23:34 +0000753ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (Encoding encoding, size_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754{
Greg Clayton57ee3062013-07-11 22:46:58 +0000755 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756}
757
Greg Claytona1e5dc82015-08-11 22:53:00 +0000758CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +0000759ClangASTContext::GetBuiltinTypeForEncodingAndBitSize (ASTContext *ast, Encoding encoding, uint32_t bit_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000761 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000762 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 switch (encoding)
764 {
Greg Claytonc86103d2010-08-05 01:57:25 +0000765 case eEncodingInvalid:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000766 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000767 return CompilerType (ast, ast->VoidPtrTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 break;
769
Greg Claytonc86103d2010-08-05 01:57:25 +0000770 case eEncodingUint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000771 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000772 return CompilerType (ast, ast->UnsignedCharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000773 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000774 return CompilerType (ast, ast->UnsignedShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000775 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000776 return CompilerType (ast, ast->UnsignedIntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000777 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000778 return CompilerType (ast, ast->UnsignedLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000779 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000780 return CompilerType (ast, ast->UnsignedLongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000781 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000782 return CompilerType (ast, ast->UnsignedInt128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783 break;
784
Greg Claytonc86103d2010-08-05 01:57:25 +0000785 case eEncodingSint:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000786 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000787 return CompilerType (ast, ast->CharTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000788 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000789 return CompilerType (ast, ast->ShortTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000790 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000791 return CompilerType (ast, ast->IntTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000792 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000793 return CompilerType (ast, ast->LongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000794 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000795 return CompilerType (ast, ast->LongLongTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000796 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000797 return CompilerType (ast, ast->Int128Ty);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 break;
799
Greg Claytonc86103d2010-08-05 01:57:25 +0000800 case eEncodingIEEE754:
Greg Clayton6beaaa62011-01-17 03:46:26 +0000801 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000802 return CompilerType (ast, ast->FloatTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000803 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000804 return CompilerType (ast, ast->DoubleTy);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000805 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000806 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000807 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
808 return CompilerType (ast, ast->HalfTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 break;
810
Greg Claytonc86103d2010-08-05 01:57:25 +0000811 case eEncodingVector:
Johnny Chenc79c93a2012-03-07 01:12:24 +0000812 // Sanity check that bit_size is a multiple of 8's.
813 if (bit_size && !(bit_size & 0x7u))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000814 return CompilerType (ast, ast->getExtVectorType (ast->UnsignedCharTy, bit_size/8));
Johnny Chenc79c93a2012-03-07 01:12:24 +0000815 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 }
817
Greg Claytona1e5dc82015-08-11 22:53:00 +0000818 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819}
820
Greg Clayton57ee3062013-07-11 22:46:58 +0000821
822
823lldb::BasicType
824ClangASTContext::GetBasicTypeEnumeration (const ConstString &name)
825{
826 if (name)
827 {
828 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
829 static TypeNameToBasicTypeMap g_type_map;
830 static std::once_flag g_once_flag;
831 std::call_once(g_once_flag, [](){
832 // "void"
833 g_type_map.Append(ConstString("void").GetCString(), eBasicTypeVoid);
834
835 // "char"
836 g_type_map.Append(ConstString("char").GetCString(), eBasicTypeChar);
837 g_type_map.Append(ConstString("signed char").GetCString(), eBasicTypeSignedChar);
838 g_type_map.Append(ConstString("unsigned char").GetCString(), eBasicTypeUnsignedChar);
839 g_type_map.Append(ConstString("wchar_t").GetCString(), eBasicTypeWChar);
840 g_type_map.Append(ConstString("signed wchar_t").GetCString(), eBasicTypeSignedWChar);
841 g_type_map.Append(ConstString("unsigned wchar_t").GetCString(), eBasicTypeUnsignedWChar);
842 // "short"
843 g_type_map.Append(ConstString("short").GetCString(), eBasicTypeShort);
844 g_type_map.Append(ConstString("short int").GetCString(), eBasicTypeShort);
845 g_type_map.Append(ConstString("unsigned short").GetCString(), eBasicTypeUnsignedShort);
846 g_type_map.Append(ConstString("unsigned short int").GetCString(), eBasicTypeUnsignedShort);
847
848 // "int"
849 g_type_map.Append(ConstString("int").GetCString(), eBasicTypeInt);
850 g_type_map.Append(ConstString("signed int").GetCString(), eBasicTypeInt);
851 g_type_map.Append(ConstString("unsigned int").GetCString(), eBasicTypeUnsignedInt);
852 g_type_map.Append(ConstString("unsigned").GetCString(), eBasicTypeUnsignedInt);
853
854 // "long"
855 g_type_map.Append(ConstString("long").GetCString(), eBasicTypeLong);
856 g_type_map.Append(ConstString("long int").GetCString(), eBasicTypeLong);
857 g_type_map.Append(ConstString("unsigned long").GetCString(), eBasicTypeUnsignedLong);
858 g_type_map.Append(ConstString("unsigned long int").GetCString(), eBasicTypeUnsignedLong);
859
860 // "long long"
861 g_type_map.Append(ConstString("long long").GetCString(), eBasicTypeLongLong);
862 g_type_map.Append(ConstString("long long int").GetCString(), eBasicTypeLongLong);
863 g_type_map.Append(ConstString("unsigned long long").GetCString(), eBasicTypeUnsignedLongLong);
864 g_type_map.Append(ConstString("unsigned long long int").GetCString(), eBasicTypeUnsignedLongLong);
865
866 // "int128"
867 g_type_map.Append(ConstString("__int128_t").GetCString(), eBasicTypeInt128);
868 g_type_map.Append(ConstString("__uint128_t").GetCString(), eBasicTypeUnsignedInt128);
869
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000870 // Miscellaneous
Greg Clayton57ee3062013-07-11 22:46:58 +0000871 g_type_map.Append(ConstString("bool").GetCString(), eBasicTypeBool);
872 g_type_map.Append(ConstString("float").GetCString(), eBasicTypeFloat);
873 g_type_map.Append(ConstString("double").GetCString(), eBasicTypeDouble);
874 g_type_map.Append(ConstString("long double").GetCString(), eBasicTypeLongDouble);
875 g_type_map.Append(ConstString("id").GetCString(), eBasicTypeObjCID);
876 g_type_map.Append(ConstString("SEL").GetCString(), eBasicTypeObjCSel);
877 g_type_map.Append(ConstString("nullptr").GetCString(), eBasicTypeNullPtr);
878 g_type_map.Sort();
879 });
880
881 return g_type_map.Find(name.GetCString(), eBasicTypeInvalid);
882 }
883 return eBasicTypeInvalid;
884}
885
Greg Claytona1e5dc82015-08-11 22:53:00 +0000886CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000887ClangASTContext::GetBasicType (ASTContext *ast, const ConstString &name)
888{
889 if (ast)
890 {
891 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration (name);
892 return ClangASTContext::GetBasicType (ast, basic_type);
893 }
Greg Claytona1e5dc82015-08-11 22:53:00 +0000894 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000895}
896
897uint32_t
898ClangASTContext::GetPointerByteSize ()
899{
900 if (m_pointer_byte_size == 0)
Enrico Granata1cd5e922015-01-28 00:07:51 +0000901 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid).GetPointerType().GetByteSize(nullptr);
Greg Clayton57ee3062013-07-11 22:46:58 +0000902 return m_pointer_byte_size;
903}
904
Greg Claytona1e5dc82015-08-11 22:53:00 +0000905CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000906ClangASTContext::GetBasicType (lldb::BasicType basic_type)
907{
908 return GetBasicType (getASTContext(), basic_type);
909}
910
Greg Claytona1e5dc82015-08-11 22:53:00 +0000911CompilerType
Greg Clayton57ee3062013-07-11 22:46:58 +0000912ClangASTContext::GetBasicType (ASTContext *ast, lldb::BasicType basic_type)
913{
Zachary Turner9d8a97e2016-04-01 23:20:35 +0000914 if (!ast)
915 return CompilerType();
916 lldb::opaque_compiler_type_t clang_type = GetOpaqueCompilerType(ast, basic_type);
917
918 if (clang_type)
919 return CompilerType(GetASTContext(ast), clang_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +0000920 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +0000921}
922
923
Greg Claytona1e5dc82015-08-11 22:53:00 +0000924CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize (const char *type_name, uint32_t dw_ate, uint32_t bit_size)
926{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000927 ASTContext *ast = getASTContext();
Sean Callanan38d4df52012-04-03 01:10:10 +0000928
929#define streq(a,b) strcmp(a,b) == 0
Ed Masted4612ad2014-04-20 13:17:36 +0000930 assert (ast != nullptr);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000931 if (ast)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932 {
933 switch (dw_ate)
934 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000935 default:
936 break;
Greg Clayton605684e2011-10-28 23:06:08 +0000937
Sean Callanan38d4df52012-04-03 01:10:10 +0000938 case DW_ATE_address:
939 if (QualTypeMatchesBitSize (bit_size, ast, ast->VoidPtrTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000940 return CompilerType (ast, ast->VoidPtrTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000941 break;
942
943 case DW_ATE_boolean:
944 if (QualTypeMatchesBitSize (bit_size, ast, ast->BoolTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000945 return CompilerType (ast, ast->BoolTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000946 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000947 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000948 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000949 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000950 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000951 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000952 break;
953
954 case DW_ATE_lo_user:
955 // This has been seen to mean DW_AT_complex_integer
956 if (type_name)
Greg Clayton605684e2011-10-28 23:06:08 +0000957 {
Sean Callanan38d4df52012-04-03 01:10:10 +0000958 if (::strstr(type_name, "complex"))
959 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000960 CompilerType complex_int_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("int", DW_ATE_signed, bit_size/2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +0000961 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(complex_int_clang_type)));
Sean Callanan38d4df52012-04-03 01:10:10 +0000962 }
Greg Clayton605684e2011-10-28 23:06:08 +0000963 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000964 break;
965
966 case DW_ATE_complex_float:
967 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000968 return CompilerType (ast, ast->FloatComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000969 else if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000970 return CompilerType (ast, ast->DoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000971 else if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleComplexTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000972 return CompilerType (ast, ast->LongDoubleComplexTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000973 else
Greg Clayton605684e2011-10-28 23:06:08 +0000974 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000975 CompilerType complex_float_clang_type = GetBuiltinTypeForDWARFEncodingAndBitSize ("float", DW_ATE_float, bit_size/2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +0000976 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(complex_float_clang_type)));
Greg Clayton605684e2011-10-28 23:06:08 +0000977 }
Sean Callanan38d4df52012-04-03 01:10:10 +0000978 break;
979
980 case DW_ATE_float:
Greg Clayton8012cad2014-11-17 19:39:20 +0000981 if (streq(type_name, "float") && QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000982 return CompilerType (ast, ast->FloatTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000983 if (streq(type_name, "double") && QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000984 return CompilerType (ast, ast->DoubleTy);
Greg Clayton8012cad2014-11-17 19:39:20 +0000985 if (streq(type_name, "long double") && QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000986 return CompilerType (ast, ast->LongDoubleTy);
Bruce Mitchenere171da52015-07-22 00:16:02 +0000987 // Fall back to not requiring a name match
Sean Callanan38d4df52012-04-03 01:10:10 +0000988 if (QualTypeMatchesBitSize (bit_size, ast, ast->FloatTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000989 return CompilerType (ast, ast->FloatTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000990 if (QualTypeMatchesBitSize (bit_size, ast, ast->DoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000991 return CompilerType (ast, ast->DoubleTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000992 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongDoubleTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +0000993 return CompilerType (ast, ast->LongDoubleTy);
Greg Claytondee40e72015-11-03 23:23:22 +0000994 if (QualTypeMatchesBitSize (bit_size, ast, ast->HalfTy))
995 return CompilerType (ast, ast->HalfTy);
Sean Callanan38d4df52012-04-03 01:10:10 +0000996 break;
997
998 case DW_ATE_signed:
999 if (type_name)
1000 {
1001 if (streq(type_name, "wchar_t") &&
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001002 QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy) &&
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001003 (getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001004 return CompilerType (ast, ast->WCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001005 if (streq(type_name, "void") &&
1006 QualTypeMatchesBitSize (bit_size, ast, ast->VoidTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001007 return CompilerType (ast, ast->VoidTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001008 if (strstr(type_name, "long long") &&
1009 QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001010 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001011 if (strstr(type_name, "long") &&
1012 QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001013 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001014 if (strstr(type_name, "short") &&
1015 QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001016 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001017 if (strstr(type_name, "char"))
1018 {
1019 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001020 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001021 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001022 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001023 }
1024 if (strstr(type_name, "int"))
1025 {
1026 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001027 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001028 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001029 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001030 }
1031 }
1032 // We weren't able to match up a type name, just search by size
1033 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001034 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001035 if (QualTypeMatchesBitSize (bit_size, ast, ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001036 return CompilerType (ast, ast->ShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001037 if (QualTypeMatchesBitSize (bit_size, ast, ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001038 return CompilerType (ast, ast->IntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001039 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001040 return CompilerType (ast, ast->LongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001041 if (QualTypeMatchesBitSize (bit_size, ast, ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001042 return CompilerType (ast, ast->LongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001043 if (QualTypeMatchesBitSize (bit_size, ast, ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001044 return CompilerType (ast, ast->Int128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001045 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001046
Sean Callanan38d4df52012-04-03 01:10:10 +00001047 case DW_ATE_signed_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001048 if (ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
Sean Callanan38d4df52012-04-03 01:10:10 +00001049 {
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001050 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001051 return CompilerType (ast, ast->CharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001052 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001053 if (QualTypeMatchesBitSize (bit_size, ast, ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001054 return CompilerType (ast, ast->SignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001055 break;
1056
1057 case DW_ATE_unsigned:
1058 if (type_name)
1059 {
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001060 if (streq(type_name, "wchar_t"))
1061 {
1062 if (QualTypeMatchesBitSize (bit_size, ast, ast->WCharTy))
1063 {
Greg Clayton28eb7bf2015-05-07 00:07:44 +00001064 if (!(getTargetInfo() && TargetInfo::isTypeSigned (getTargetInfo()->getWCharType())))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001065 return CompilerType (ast, ast->WCharTy);
Tamas Berghammer3c0d0052015-04-01 09:48:02 +00001066 }
1067 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001068 if (strstr(type_name, "long long"))
1069 {
1070 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001071 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001072 }
1073 else if (strstr(type_name, "long"))
1074 {
1075 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001076 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001077 }
1078 else if (strstr(type_name, "short"))
1079 {
1080 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001081 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001082 }
1083 else if (strstr(type_name, "char"))
1084 {
1085 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001086 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001087 }
1088 else if (strstr(type_name, "int"))
1089 {
1090 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001091 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001092 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001093 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001094 }
1095 }
1096 // We weren't able to match up a type name, just search by size
1097 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001098 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001099 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001100 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001101 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001102 return CompilerType (ast, ast->UnsignedIntTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001103 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001104 return CompilerType (ast, ast->UnsignedLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001105 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001106 return CompilerType (ast, ast->UnsignedLongLongTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001107 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001108 return CompilerType (ast, ast->UnsignedInt128Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001109 break;
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001110
Sean Callanan38d4df52012-04-03 01:10:10 +00001111 case DW_ATE_unsigned_char:
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001112 if (!ast->getLangOpts().CharIsSigned && type_name && streq(type_name, "char"))
1113 {
1114 if (QualTypeMatchesBitSize (bit_size, ast, ast->CharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001115 return CompilerType (ast, ast->CharTy);
Tamas Berghammerdccbfaf2015-03-31 10:21:50 +00001116 }
Sean Callanan38d4df52012-04-03 01:10:10 +00001117 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001118 return CompilerType (ast, ast->UnsignedCharTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001119 if (QualTypeMatchesBitSize (bit_size, ast, ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00001120 return CompilerType (ast, ast->UnsignedShortTy);
Sean Callanan38d4df52012-04-03 01:10:10 +00001121 break;
1122
1123 case DW_ATE_imaginary_float:
1124 break;
1125
1126 case DW_ATE_UTF:
1127 if (type_name)
1128 {
1129 if (streq(type_name, "char16_t"))
1130 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001131 return CompilerType (ast, ast->Char16Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001132 }
1133 else if (streq(type_name, "char32_t"))
1134 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00001135 return CompilerType (ast, ast->Char32Ty);
Sean Callanan38d4df52012-04-03 01:10:10 +00001136 }
1137 }
1138 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 }
1140 }
1141 // This assert should fire for anything that we don't catch above so we know
1142 // to fix any issues we run into.
Greg Claytondc968d12011-05-17 18:15:05 +00001143 if (type_name)
1144 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001145 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 +00001146 }
1147 else
1148 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00001149 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 +00001150 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001151 return CompilerType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152}
1153
Greg Claytona1e5dc82015-08-11 22:53:00 +00001154CompilerType
Sean Callanan77502262011-05-12 23:54:16 +00001155ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast)
1156{
Greg Clayton57ee3062013-07-11 22:46:58 +00001157 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001158 return CompilerType (ast, ast->UnknownAnyTy);
1159 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001160}
1161
Greg Claytona1e5dc82015-08-11 22:53:00 +00001162CompilerType
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163ClangASTContext::GetCStringType (bool is_const)
1164{
Greg Clayton57ee3062013-07-11 22:46:58 +00001165 ASTContext *ast = getASTContext();
1166 QualType char_type(ast->CharTy);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167
1168 if (is_const)
1169 char_type.addConst();
1170
Greg Claytona1e5dc82015-08-11 22:53:00 +00001171 return CompilerType (ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172}
1173
Sean Callanan09ab4b72011-11-30 22:11:59 +00001174clang::DeclContext *
1175ClangASTContext::GetTranslationUnitDecl (clang::ASTContext *ast)
1176{
1177 return ast->getTranslationUnitDecl();
1178}
1179
Greg Clayton526e5af2010-11-13 03:52:47 +00001180clang::Decl *
Greg Clayton38a61402010-12-02 23:20:03 +00001181ClangASTContext::CopyDecl (ASTContext *dst_ast,
1182 ASTContext *src_ast,
Greg Clayton526e5af2010-11-13 03:52:47 +00001183 clang::Decl *source_decl)
Sean Callanan7fddd4c2010-12-11 00:08:56 +00001184{
Sean Callanan79439e82010-11-18 02:56:27 +00001185 FileSystemOptions file_system_options;
Greg Clayton38a61402010-12-02 23:20:03 +00001186 FileManager file_manager (file_system_options);
1187 ASTImporter importer(*dst_ast, file_manager,
Sean Callanan2c777c42011-01-18 23:32:05 +00001188 *src_ast, file_manager,
1189 false);
Greg Clayton526e5af2010-11-13 03:52:47 +00001190
1191 return importer.Import(source_decl);
1192}
1193
Sean Callanan23a30272010-07-16 00:00:27 +00001194bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00001195ClangASTContext::AreTypesSame (CompilerType type1,
1196 CompilerType type2,
Greg Clayton84db9102012-03-26 23:03:23 +00001197 bool ignore_qualifiers)
Sean Callanan4dcca2622010-07-15 22:30:52 +00001198{
Greg Claytonf73034f2015-09-08 18:15:05 +00001199 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1200 if (!ast || ast != type2.GetTypeSystem())
Greg Clayton57ee3062013-07-11 22:46:58 +00001201 return false;
1202
1203 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
Greg Clayton55995eb2012-04-06 17:38:55 +00001204 return true;
1205
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001206 QualType type1_qual = ClangUtil::GetQualType(type1);
1207 QualType type2_qual = ClangUtil::GetQualType(type2);
1208
Sean Callanan5056ab02012-02-18 02:01:03 +00001209 if (ignore_qualifiers)
1210 {
1211 type1_qual = type1_qual.getUnqualifiedType();
1212 type2_qual = type2_qual.getUnqualifiedType();
1213 }
1214
Greg Claytonf73034f2015-09-08 18:15:05 +00001215 return ast->getASTContext()->hasSameType (type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216}
1217
Greg Claytona1e5dc82015-08-11 22:53:00 +00001218CompilerType
Sean Callanan9998acd2014-12-05 01:21:59 +00001219ClangASTContext::GetTypeForDecl (clang::NamedDecl *decl)
1220{
1221 if (clang::ObjCInterfaceDecl *interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1222 return GetTypeForDecl(interface_decl);
1223 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1224 return GetTypeForDecl(tag_decl);
Greg Claytona1e5dc82015-08-11 22:53:00 +00001225 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001226}
1227
Greg Clayton6beaaa62011-01-17 03:46:26 +00001228
Greg Claytona1e5dc82015-08-11 22:53:00 +00001229CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001230ClangASTContext::GetTypeForDecl (TagDecl *decl)
1231{
1232 // No need to call the getASTContext() accessor (which can create the AST
1233 // if it isn't created yet, because we can't have created a decl in this
1234 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001235 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001236 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001237 return CompilerType (ast, ast->getTagDeclType(decl));
1238 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001239}
1240
Greg Claytona1e5dc82015-08-11 22:53:00 +00001241CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00001242ClangASTContext::GetTypeForDecl (ObjCInterfaceDecl *decl)
1243{
1244 // No need to call the getASTContext() accessor (which can create the AST
1245 // if it isn't created yet, because we can't have created a decl in this
1246 // AST if our AST didn't already exist...
Sean Callanan9998acd2014-12-05 01:21:59 +00001247 ASTContext *ast = &decl->getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001248 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001249 return CompilerType (ast, ast->getObjCInterfaceType(decl));
1250 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001251}
1252
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253#pragma mark Structure, Unions, Classes
1254
Greg Claytona1e5dc82015-08-11 22:53:00 +00001255CompilerType
Greg Claytonc4ffd662013-03-08 01:37:30 +00001256ClangASTContext::CreateRecordType (DeclContext *decl_ctx,
1257 AccessType access_type,
1258 const char *name,
1259 int kind,
1260 LanguageType language,
1261 ClangASTMetadata *metadata)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001263 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001264 assert (ast != nullptr);
Sean Callananad880762012-04-18 01:06:17 +00001265
Ed Masted4612ad2014-04-20 13:17:36 +00001266 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001267 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268
Greg Clayton9e409562010-07-28 02:04:09 +00001269
Greg Claytone1be9962011-08-24 23:50:00 +00001270 if (language == eLanguageTypeObjC || language == eLanguageTypeObjC_plus_plus)
Greg Clayton9e409562010-07-28 02:04:09 +00001271 {
Greg Claytonaaf99e02010-10-11 02:25:34 +00001272 bool isForwardDecl = true;
Greg Clayton9e409562010-07-28 02:04:09 +00001273 bool isInternal = false;
Sean Callananad880762012-04-18 01:06:17 +00001274 return CreateObjCClass (name, decl_ctx, isForwardDecl, isInternal, metadata);
Greg Clayton9e409562010-07-28 02:04:09 +00001275 }
1276
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1278 // we will need to update this code. I was told to currently always use
1279 // the CXXRecordDecl class since we often don't know from debug information
1280 // if something is struct or a class, so we default to always use the more
1281 // complete definition just in case.
Sean Callanan11e32d32014-02-18 00:31:38 +00001282
1283 bool is_anonymous = (!name) || (!name[0]);
1284
Greg Claytonf0705c82011-10-22 03:33:13 +00001285 CXXRecordDecl *decl = CXXRecordDecl::Create (*ast,
1286 (TagDecl::TagKind)kind,
1287 decl_ctx,
1288 SourceLocation(),
1289 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001290 is_anonymous ? nullptr : &ast->Idents.get(name));
Sean Callanan11e32d32014-02-18 00:31:38 +00001291
1292 if (is_anonymous)
1293 decl->setAnonymousStructOrUnion(true);
Sean Callanan7282e2a2012-01-13 22:10:18 +00001294
Greg Claytonc4ffd662013-03-08 01:37:30 +00001295 if (decl)
Greg Clayton55561e92011-10-26 03:31:36 +00001296 {
Greg Claytonc4ffd662013-03-08 01:37:30 +00001297 if (metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001298 SetMetadata(ast, decl, *metadata);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001299
Greg Clayton55561e92011-10-26 03:31:36 +00001300 if (access_type != eAccessNone)
1301 decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Greg Claytonc4ffd662013-03-08 01:37:30 +00001302
1303 if (decl_ctx)
1304 decl_ctx->addDecl (decl);
1305
Greg Claytona1e5dc82015-08-11 22:53:00 +00001306 return CompilerType(ast, ast->getTagDeclType(decl));
Greg Clayton55561e92011-10-26 03:31:36 +00001307 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001308 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001309}
1310
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001311static TemplateParameterList *
1312CreateTemplateParameterList (ASTContext *ast,
Sean Callanan3d654b32012-09-24 22:25:51 +00001313 const ClangASTContext::TemplateParameterInfos &template_param_infos,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001314 llvm::SmallVector<NamedDecl *, 8> &template_param_decls)
1315{
1316 const bool parameter_pack = false;
1317 const bool is_typename = false;
1318 const unsigned depth = 0;
1319 const size_t num_template_params = template_param_infos.GetSize();
1320 for (size_t i=0; i<num_template_params; ++i)
1321 {
1322 const char *name = template_param_infos.names[i];
Greg Clayton283b2652013-04-23 22:38:02 +00001323
Ed Masted4612ad2014-04-20 13:17:36 +00001324 IdentifierInfo *identifier_info = nullptr;
Greg Clayton283b2652013-04-23 22:38:02 +00001325 if (name && name[0])
1326 identifier_info = &ast->Idents.get(name);
Sean Callanan3d654b32012-09-24 22:25:51 +00001327 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral)
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001328 {
1329 template_param_decls.push_back (NonTypeTemplateParmDecl::Create (*ast,
1330 ast->getTranslationUnitDecl(), // Is this the right decl context?, SourceLocation StartLoc,
1331 SourceLocation(),
1332 SourceLocation(),
1333 depth,
1334 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001335 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001336 template_param_infos.args[i].getIntegralType(),
1337 parameter_pack,
Ed Masted4612ad2014-04-20 13:17:36 +00001338 nullptr));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001339
1340 }
1341 else
1342 {
1343 template_param_decls.push_back (TemplateTypeParmDecl::Create (*ast,
1344 ast->getTranslationUnitDecl(), // Is this the right decl context?
1345 SourceLocation(),
1346 SourceLocation(),
1347 depth,
1348 i,
Greg Clayton283b2652013-04-23 22:38:02 +00001349 identifier_info,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001350 is_typename,
1351 parameter_pack));
1352 }
1353 }
1354
1355 TemplateParameterList *template_param_list = TemplateParameterList::Create (*ast,
1356 SourceLocation(),
1357 SourceLocation(),
David Majnemer48a065d2015-12-27 07:16:55 +00001358 template_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001359 SourceLocation());
1360 return template_param_list;
1361}
1362
1363clang::FunctionTemplateDecl *
1364ClangASTContext::CreateFunctionTemplateDecl (clang::DeclContext *decl_ctx,
1365 clang::FunctionDecl *func_decl,
1366 const char *name,
1367 const TemplateParameterInfos &template_param_infos)
1368{
1369// /// \brief Create a function template node.
1370 ASTContext *ast = getASTContext();
1371
1372 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1373
1374 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1375 template_param_infos,
1376 template_param_decls);
1377 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create (*ast,
1378 decl_ctx,
1379 func_decl->getLocation(),
1380 func_decl->getDeclName(),
1381 template_param_list,
1382 func_decl);
1383
1384 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1385 i < template_param_decl_count;
1386 ++i)
1387 {
1388 // TODO: verify which decl context we should put template_param_decls into..
1389 template_param_decls[i]->setDeclContext (func_decl);
1390 }
1391
1392 return func_tmpl_decl;
1393}
1394
1395void
1396ClangASTContext::CreateFunctionTemplateSpecializationInfo (FunctionDecl *func_decl,
1397 clang::FunctionTemplateDecl *func_tmpl_decl,
1398 const TemplateParameterInfos &infos)
1399{
1400 TemplateArgumentList template_args (TemplateArgumentList::OnStack,
1401 infos.args.data(),
1402 infos.args.size());
1403
1404 func_decl->setFunctionTemplateSpecialization (func_tmpl_decl,
1405 &template_args,
Ed Masted4612ad2014-04-20 13:17:36 +00001406 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001407}
1408
1409
Greg Claytonf0705c82011-10-22 03:33:13 +00001410ClassTemplateDecl *
1411ClangASTContext::CreateClassTemplateDecl (DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001412 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001413 const char *class_name,
1414 int kind,
1415 const TemplateParameterInfos &template_param_infos)
1416{
1417 ASTContext *ast = getASTContext();
1418
Ed Masted4612ad2014-04-20 13:17:36 +00001419 ClassTemplateDecl *class_template_decl = nullptr;
1420 if (decl_ctx == nullptr)
Greg Claytonf0705c82011-10-22 03:33:13 +00001421 decl_ctx = ast->getTranslationUnitDecl();
1422
1423 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1424 DeclarationName decl_name (&identifier_info);
1425
1426 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001427
1428 for (NamedDecl *decl : result)
Greg Claytonf0705c82011-10-22 03:33:13 +00001429 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001430 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001431 if (class_template_decl)
1432 return class_template_decl;
1433 }
1434
1435 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Claytonf0705c82011-10-22 03:33:13 +00001436
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001437 TemplateParameterList *template_param_list = CreateTemplateParameterList (ast,
1438 template_param_infos,
1439 template_param_decls);
Greg Claytonf0705c82011-10-22 03:33:13 +00001440
1441 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create (*ast,
1442 (TagDecl::TagKind)kind,
1443 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1444 SourceLocation(),
1445 SourceLocation(),
1446 &identifier_info);
Greg Claytone04741d2011-12-02 02:09:28 +00001447
1448 for (size_t i=0, template_param_decl_count = template_param_decls.size();
1449 i < template_param_decl_count;
1450 ++i)
1451 {
1452 template_param_decls[i]->setDeclContext (template_cxx_decl);
1453 }
1454
Sean Callananb5c79622011-11-19 01:35:08 +00001455 // With templated classes, we say that a class is templated with
1456 // specializations, but that the bare class has no functions.
Sean Callananfa4fab72013-02-01 06:55:48 +00001457 //template_cxx_decl->startDefinition();
1458 //template_cxx_decl->completeDefinition();
Sean Callananb5c79622011-11-19 01:35:08 +00001459
Greg Claytonf0705c82011-10-22 03:33:13 +00001460 class_template_decl = ClassTemplateDecl::Create (*ast,
1461 decl_ctx, // What decl context do we use here? TU? The actual decl context?
1462 SourceLocation(),
1463 decl_name,
1464 template_param_list,
1465 template_cxx_decl,
Ed Masted4612ad2014-04-20 13:17:36 +00001466 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001467
1468 if (class_template_decl)
Sean Callanan5e9e1992011-10-26 01:06:27 +00001469 {
Greg Clayton55561e92011-10-26 03:31:36 +00001470 if (access_type != eAccessNone)
1471 class_template_decl->setAccess (ConvertAccessTypeToAccessSpecifier (access_type));
Sean Callanan5b26f272012-02-04 08:49:35 +00001472
1473 //if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1474 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1475
Greg Claytonf0705c82011-10-22 03:33:13 +00001476 decl_ctx->addDecl (class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001477
1478#ifdef LLDB_CONFIGURATION_DEBUG
1479 VerifyDecl(class_template_decl);
1480#endif
1481 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001482
1483 return class_template_decl;
1484}
1485
1486
1487ClassTemplateSpecializationDecl *
1488ClangASTContext::CreateClassTemplateSpecializationDecl (DeclContext *decl_ctx,
1489 ClassTemplateDecl *class_template_decl,
1490 int kind,
1491 const TemplateParameterInfos &template_param_infos)
1492{
1493 ASTContext *ast = getASTContext();
1494 ClassTemplateSpecializationDecl *class_template_specialization_decl = ClassTemplateSpecializationDecl::Create (*ast,
1495 (TagDecl::TagKind)kind,
1496 decl_ctx,
1497 SourceLocation(),
1498 SourceLocation(),
1499 class_template_decl,
1500 &template_param_infos.args.front(),
1501 template_param_infos.args.size(),
Ed Masted4612ad2014-04-20 13:17:36 +00001502 nullptr);
Greg Claytonf0705c82011-10-22 03:33:13 +00001503
Sean Callananfa4fab72013-02-01 06:55:48 +00001504 class_template_specialization_decl->setSpecializationKind(TSK_ExplicitSpecialization);
1505
Greg Claytonf0705c82011-10-22 03:33:13 +00001506 return class_template_specialization_decl;
1507}
1508
Greg Claytona1e5dc82015-08-11 22:53:00 +00001509CompilerType
Greg Claytonf0705c82011-10-22 03:33:13 +00001510ClangASTContext::CreateClassTemplateSpecializationType (ClassTemplateSpecializationDecl *class_template_specialization_decl)
1511{
1512 if (class_template_specialization_decl)
1513 {
1514 ASTContext *ast = getASTContext();
1515 if (ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001516 return CompilerType(ast, ast->getTagDeclType(class_template_specialization_decl));
Greg Claytonf0705c82011-10-22 03:33:13 +00001517 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00001518 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519}
1520
Greg Clayton090d0982011-06-19 03:43:27 +00001521static inline bool
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001522check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
Greg Clayton090d0982011-06-19 03:43:27 +00001523{
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001524 // Special-case call since it can take any number of operands
1525 if(op_kind == OO_Call)
1526 return true;
1527
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001528 // The parameter count doesn't include "this"
Greg Clayton090d0982011-06-19 03:43:27 +00001529 if (num_params == 0)
1530 return unary;
1531 if (num_params == 1)
1532 return binary;
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001533 else
Greg Clayton090d0982011-06-19 03:43:27 +00001534 return false;
1535}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001536
Greg Clayton090d0982011-06-19 03:43:27 +00001537bool
1538ClangASTContext::CheckOverloadedOperatorKindParameterCount (uint32_t op_kind, uint32_t num_params)
1539{
Sean Callanan5b26f272012-02-04 08:49:35 +00001540 switch (op_kind)
1541 {
1542 default:
1543 break;
1544 // C++ standard allows any number of arguments to new/delete
1545 case OO_New:
1546 case OO_Array_New:
1547 case OO_Delete:
1548 case OO_Array_Delete:
1549 return true;
1550 }
1551
Sean Callanan6d9f5db2011-10-15 01:15:07 +00001552#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 +00001553 switch (op_kind)
1554 {
1555#include "clang/Basic/OperatorKinds.def"
1556 default: break;
1557 }
1558 return false;
1559}
1560
Greg Clayton57ee3062013-07-11 22:46:58 +00001561clang::AccessSpecifier
1562ClangASTContext::UnifyAccessSpecifiers (clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001563{
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001564 // Make the access equal to the stricter of the field and the nested field's access
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001565 if (lhs == AS_none || rhs == AS_none)
1566 return AS_none;
1567 if (lhs == AS_private || rhs == AS_private)
1568 return AS_private;
1569 if (lhs == AS_protected || rhs == AS_protected)
1570 return AS_protected;
1571 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001572}
1573
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574bool
1575ClangASTContext::FieldIsBitfield (FieldDecl* field, uint32_t& bitfield_bit_size)
1576{
1577 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1578}
1579
1580bool
1581ClangASTContext::FieldIsBitfield
1582(
Greg Clayton6beaaa62011-01-17 03:46:26 +00001583 ASTContext *ast,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584 FieldDecl* field,
1585 uint32_t& bitfield_bit_size
1586)
1587{
Ed Masted4612ad2014-04-20 13:17:36 +00001588 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 return false;
1590
1591 if (field->isBitField())
1592 {
1593 Expr* bit_width_expr = field->getBitWidth();
1594 if (bit_width_expr)
1595 {
1596 llvm::APSInt bit_width_apsint;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001597 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598 {
1599 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1600 return true;
1601 }
1602 }
1603 }
1604 return false;
1605}
1606
1607bool
1608ClangASTContext::RecordHasFields (const RecordDecl *record_decl)
1609{
Ed Masted4612ad2014-04-20 13:17:36 +00001610 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001611 return false;
1612
1613 if (!record_decl->field_empty())
1614 return true;
1615
1616 // No fields, lets check this is a CXX record and check the base classes
1617 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1618 if (cxx_record_decl)
1619 {
1620 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1621 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1622 base_class != base_class_end;
1623 ++base_class)
1624 {
1625 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
1626 if (RecordHasFields(base_class_decl))
1627 return true;
1628 }
1629 }
1630 return false;
1631}
1632
Greg Clayton8cf05932010-07-22 18:30:50 +00001633#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
Greg Claytona1e5dc82015-08-11 22:53:00 +00001635CompilerType
Sean Callananad880762012-04-18 01:06:17 +00001636ClangASTContext::CreateObjCClass
Greg Clayton8cf05932010-07-22 18:30:50 +00001637(
1638 const char *name,
1639 DeclContext *decl_ctx,
1640 bool isForwardDecl,
Sean Callananad880762012-04-18 01:06:17 +00001641 bool isInternal,
Jim Ingham379397632012-10-27 02:54:13 +00001642 ClangASTMetadata *metadata
Greg Clayton8cf05932010-07-22 18:30:50 +00001643)
1644{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001645 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001646 assert (ast != nullptr);
Greg Clayton8cf05932010-07-22 18:30:50 +00001647 assert (name && name[0]);
Ed Masted4612ad2014-04-20 13:17:36 +00001648 if (decl_ctx == nullptr)
Greg Clayton6beaaa62011-01-17 03:46:26 +00001649 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001650
Greg Clayton6beaaa62011-01-17 03:46:26 +00001651 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create (*ast,
Greg Clayton8cf05932010-07-22 18:30:50 +00001652 decl_ctx,
1653 SourceLocation(),
Greg Clayton6beaaa62011-01-17 03:46:26 +00001654 &ast->Idents.get(name),
Ed Masted4612ad2014-04-20 13:17:36 +00001655 nullptr,
Pavel Labath67add942015-07-07 10:11:16 +00001656 nullptr,
Greg Clayton8cf05932010-07-22 18:30:50 +00001657 SourceLocation(),
Sean Callanan5b26f272012-02-04 08:49:35 +00001658 /*isForwardDecl,*/
Greg Clayton8cf05932010-07-22 18:30:50 +00001659 isInternal);
Greg Clayton9e409562010-07-28 02:04:09 +00001660
Jim Ingham379397632012-10-27 02:54:13 +00001661 if (decl && metadata)
Greg Claytond0029442013-03-27 01:48:02 +00001662 SetMetadata(ast, decl, *metadata);
Sean Callananad880762012-04-18 01:06:17 +00001663
Greg Claytona1e5dc82015-08-11 22:53:00 +00001664 return CompilerType (ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665}
1666
1667static inline bool
1668BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
1669{
Greg Clayton6beaaa62011-01-17 03:46:26 +00001670 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671}
1672
Greg Clayton57ee3062013-07-11 22:46:58 +00001673uint32_t
1674ClangASTContext::GetNumBaseClasses (const CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675{
1676 uint32_t num_bases = 0;
1677 if (cxx_record_decl)
1678 {
1679 if (omit_empty_base_classes)
1680 {
1681 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1682 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
1683 base_class != base_class_end;
1684 ++base_class)
1685 {
1686 // Skip empty base classes
1687 if (omit_empty_base_classes)
1688 {
1689 if (BaseSpecifierIsEmpty (base_class))
1690 continue;
1691 }
1692 ++num_bases;
1693 }
1694 }
1695 else
1696 num_bases = cxx_record_decl->getNumBases();
1697 }
1698 return num_bases;
1699}
1700
1701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702#pragma mark Namespace Declarations
1703
1704NamespaceDecl *
Greg Clayton030a2042011-10-14 21:34:45 +00001705ClangASTContext::GetUniqueNamespaceDeclaration (const char *name, DeclContext *decl_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706{
Ed Masted4612ad2014-04-20 13:17:36 +00001707 NamespaceDecl *namespace_decl = nullptr;
Greg Clayton9d3d6882011-10-31 23:51:19 +00001708 ASTContext *ast = getASTContext();
1709 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl ();
Ed Masted4612ad2014-04-20 13:17:36 +00001710 if (decl_ctx == nullptr)
Greg Clayton9d3d6882011-10-31 23:51:19 +00001711 decl_ctx = translation_unit_decl;
1712
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 if (name)
1714 {
Greg Clayton030a2042011-10-14 21:34:45 +00001715 IdentifierInfo &identifier_info = ast->Idents.get(name);
1716 DeclarationName decl_name (&identifier_info);
1717 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001718 for (NamedDecl *decl : result)
Greg Clayton030a2042011-10-14 21:34:45 +00001719 {
Sean Callanan5deaa4c2012-12-21 21:34:42 +00001720 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
Greg Clayton030a2042011-10-14 21:34:45 +00001721 if (namespace_decl)
1722 return namespace_decl;
1723 }
1724
Sean Callanan5b26f272012-02-04 08:49:35 +00001725 namespace_decl = NamespaceDecl::Create(*ast,
1726 decl_ctx,
1727 false,
1728 SourceLocation(),
1729 SourceLocation(),
1730 &identifier_info,
Ed Masted4612ad2014-04-20 13:17:36 +00001731 nullptr);
Greg Clayton030a2042011-10-14 21:34:45 +00001732
Greg Clayton9d3d6882011-10-31 23:51:19 +00001733 decl_ctx->addDecl (namespace_decl);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001735 else
1736 {
1737 if (decl_ctx == translation_unit_decl)
1738 {
1739 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1740 if (namespace_decl)
1741 return namespace_decl;
1742
Sean Callanan5b26f272012-02-04 08:49:35 +00001743 namespace_decl = NamespaceDecl::Create(*ast,
1744 decl_ctx,
1745 false,
1746 SourceLocation(),
1747 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001748 nullptr,
1749 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001750 translation_unit_decl->setAnonymousNamespace (namespace_decl);
1751 translation_unit_decl->addDecl (namespace_decl);
1752 assert (namespace_decl == translation_unit_decl->getAnonymousNamespace());
1753 }
1754 else
1755 {
1756 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1757 if (parent_namespace_decl)
1758 {
1759 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1760 if (namespace_decl)
1761 return namespace_decl;
Sean Callanan5b26f272012-02-04 08:49:35 +00001762 namespace_decl = NamespaceDecl::Create(*ast,
1763 decl_ctx,
1764 false,
1765 SourceLocation(),
1766 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00001767 nullptr,
1768 nullptr);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001769 parent_namespace_decl->setAnonymousNamespace (namespace_decl);
1770 parent_namespace_decl->addDecl (namespace_decl);
1771 assert (namespace_decl == parent_namespace_decl->getAnonymousNamespace());
1772 }
1773 else
1774 {
1775 // BAD!!!
1776 }
1777 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001778 }
1779#ifdef LLDB_CONFIGURATION_DEBUG
1780 VerifyDecl(namespace_decl);
1781#endif
Greg Clayton030a2042011-10-14 21:34:45 +00001782 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001783}
1784
Siva Chandra03ff5c82016-02-05 19:10:04 +00001785NamespaceDecl *
1786ClangASTContext::GetUniqueNamespaceDeclaration (clang::ASTContext *ast,
1787 const char *name,
1788 clang::DeclContext *decl_ctx)
1789{
1790 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1791 if (ast_ctx == nullptr)
1792 return nullptr;
1793
1794 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1795}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796
Paul Hermand628cbb2015-09-15 23:44:17 +00001797clang::BlockDecl *
1798ClangASTContext::CreateBlockDeclaration (clang::DeclContext *ctx)
1799{
1800 if (ctx != nullptr)
1801 {
1802 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, clang::SourceLocation());
1803 ctx->addDecl(decl);
1804 return decl;
1805 }
1806 return nullptr;
1807}
1808
Paul Hermanea188fc2015-09-16 18:48:30 +00001809clang::DeclContext *
1810FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
1811{
1812 if (root == nullptr)
1813 return nullptr;
1814
1815 std::set<clang::DeclContext *> path_left;
1816 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1817 path_left.insert(d);
1818
1819 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1820 if (path_left.find(d) != path_left.end())
1821 return d;
1822
1823 return nullptr;
1824}
1825
Paul Hermand628cbb2015-09-15 23:44:17 +00001826clang::UsingDirectiveDecl *
1827ClangASTContext::CreateUsingDirectiveDeclaration (clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl)
1828{
1829 if (decl_ctx != nullptr && ns_decl != nullptr)
1830 {
Paul Hermanea188fc2015-09-16 18:48:30 +00001831 clang::TranslationUnitDecl *translation_unit = (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
Paul Hermand628cbb2015-09-15 23:44:17 +00001832 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(*getASTContext(),
1833 decl_ctx,
1834 clang::SourceLocation(),
1835 clang::SourceLocation(),
1836 clang::NestedNameSpecifierLoc(),
1837 clang::SourceLocation(),
1838 ns_decl,
Paul Hermanea188fc2015-09-16 18:48:30 +00001839 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
Paul Hermand628cbb2015-09-15 23:44:17 +00001840 decl_ctx->addDecl(using_decl);
1841 return using_decl;
1842 }
1843 return nullptr;
1844}
1845
1846clang::UsingDecl *
1847ClangASTContext::CreateUsingDeclaration (clang::DeclContext *current_decl_ctx, clang::NamedDecl *target)
1848{
1849 if (current_decl_ctx != nullptr && target != nullptr)
1850 {
1851 clang::UsingDecl *using_decl = clang::UsingDecl::Create(*getASTContext(),
1852 current_decl_ctx,
1853 clang::SourceLocation(),
1854 clang::NestedNameSpecifierLoc(),
1855 clang::DeclarationNameInfo(),
1856 false);
1857 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(*getASTContext(),
1858 current_decl_ctx,
1859 clang::SourceLocation(),
1860 using_decl,
1861 target);
1862 using_decl->addShadowDecl(shadow_decl);
1863 current_decl_ctx->addDecl(using_decl);
1864 return using_decl;
1865 }
1866 return nullptr;
1867}
1868
1869clang::VarDecl *
1870ClangASTContext::CreateVariableDeclaration (clang::DeclContext *decl_context, const char *name, clang::QualType type)
1871{
1872 if (decl_context != nullptr)
1873 {
1874 clang::VarDecl *var_decl = clang::VarDecl::Create(*getASTContext(),
1875 decl_context,
1876 clang::SourceLocation(),
1877 clang::SourceLocation(),
1878 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr,
1879 type,
1880 nullptr,
1881 clang::SC_None);
1882 var_decl->setAccess(clang::AS_public);
1883 decl_context->addDecl(var_decl);
Paul Hermand628cbb2015-09-15 23:44:17 +00001884 return var_decl;
1885 }
1886 return nullptr;
1887}
1888
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001889lldb::opaque_compiler_type_t
1890ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type)
1891{
1892 switch (basic_type)
1893 {
1894 case eBasicTypeVoid:
1895 return ast->VoidTy.getAsOpaquePtr();
1896 case eBasicTypeChar:
1897 return ast->CharTy.getAsOpaquePtr();
1898 case eBasicTypeSignedChar:
1899 return ast->SignedCharTy.getAsOpaquePtr();
1900 case eBasicTypeUnsignedChar:
1901 return ast->UnsignedCharTy.getAsOpaquePtr();
1902 case eBasicTypeWChar:
1903 return ast->getWCharType().getAsOpaquePtr();
1904 case eBasicTypeSignedWChar:
1905 return ast->getSignedWCharType().getAsOpaquePtr();
1906 case eBasicTypeUnsignedWChar:
1907 return ast->getUnsignedWCharType().getAsOpaquePtr();
1908 case eBasicTypeChar16:
1909 return ast->Char16Ty.getAsOpaquePtr();
1910 case eBasicTypeChar32:
1911 return ast->Char32Ty.getAsOpaquePtr();
1912 case eBasicTypeShort:
1913 return ast->ShortTy.getAsOpaquePtr();
1914 case eBasicTypeUnsignedShort:
1915 return ast->UnsignedShortTy.getAsOpaquePtr();
1916 case eBasicTypeInt:
1917 return ast->IntTy.getAsOpaquePtr();
1918 case eBasicTypeUnsignedInt:
1919 return ast->UnsignedIntTy.getAsOpaquePtr();
1920 case eBasicTypeLong:
1921 return ast->LongTy.getAsOpaquePtr();
1922 case eBasicTypeUnsignedLong:
1923 return ast->UnsignedLongTy.getAsOpaquePtr();
1924 case eBasicTypeLongLong:
1925 return ast->LongLongTy.getAsOpaquePtr();
1926 case eBasicTypeUnsignedLongLong:
1927 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1928 case eBasicTypeInt128:
1929 return ast->Int128Ty.getAsOpaquePtr();
1930 case eBasicTypeUnsignedInt128:
1931 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1932 case eBasicTypeBool:
1933 return ast->BoolTy.getAsOpaquePtr();
1934 case eBasicTypeHalf:
1935 return ast->HalfTy.getAsOpaquePtr();
1936 case eBasicTypeFloat:
1937 return ast->FloatTy.getAsOpaquePtr();
1938 case eBasicTypeDouble:
1939 return ast->DoubleTy.getAsOpaquePtr();
1940 case eBasicTypeLongDouble:
1941 return ast->LongDoubleTy.getAsOpaquePtr();
1942 case eBasicTypeFloatComplex:
1943 return ast->FloatComplexTy.getAsOpaquePtr();
1944 case eBasicTypeDoubleComplex:
1945 return ast->DoubleComplexTy.getAsOpaquePtr();
1946 case eBasicTypeLongDoubleComplex:
1947 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1948 case eBasicTypeObjCID:
1949 return ast->getObjCIdType().getAsOpaquePtr();
1950 case eBasicTypeObjCClass:
1951 return ast->getObjCClassType().getAsOpaquePtr();
1952 case eBasicTypeObjCSel:
1953 return ast->getObjCSelType().getAsOpaquePtr();
1954 case eBasicTypeNullPtr:
1955 return ast->NullPtrTy.getAsOpaquePtr();
1956 default:
1957 return nullptr;
1958 }
1959}
1960
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961#pragma mark Function Types
1962
1963FunctionDecl *
Greg Clayton57ee3062013-07-11 22:46:58 +00001964ClangASTContext::CreateFunctionDeclaration (DeclContext *decl_ctx,
1965 const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00001966 const CompilerType &function_clang_type,
Greg Clayton57ee3062013-07-11 22:46:58 +00001967 int storage,
1968 bool is_inline)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969{
Ed Masted4612ad2014-04-20 13:17:36 +00001970 FunctionDecl *func_decl = nullptr;
Greg Clayton147e1fa2011-10-14 22:47:18 +00001971 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00001972 if (decl_ctx == nullptr)
Greg Clayton147e1fa2011-10-14 22:47:18 +00001973 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974
Greg Clayton0d551042013-06-28 21:08:47 +00001975
1976 const bool hasWrittenPrototype = true;
1977 const bool isConstexprSpecified = false;
1978
Greg Clayton147e1fa2011-10-14 22:47:18 +00001979 if (name && name[0])
1980 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001981 func_decl = FunctionDecl::Create(
1982 *ast, decl_ctx, SourceLocation(), SourceLocation(), DeclarationName(&ast->Idents.get(name)),
1983 ClangUtil::GetQualType(function_clang_type), nullptr, (clang::StorageClass)storage, is_inline,
1984 hasWrittenPrototype, isConstexprSpecified);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985 }
Greg Clayton147e1fa2011-10-14 22:47:18 +00001986 else
1987 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001988 func_decl =
1989 FunctionDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), DeclarationName(),
1990 ClangUtil::GetQualType(function_clang_type), nullptr, (clang::StorageClass)storage,
1991 is_inline, hasWrittenPrototype, isConstexprSpecified);
Greg Clayton147e1fa2011-10-14 22:47:18 +00001992 }
1993 if (func_decl)
1994 decl_ctx->addDecl (func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001995
1996#ifdef LLDB_CONFIGURATION_DEBUG
1997 VerifyDecl(func_decl);
1998#endif
1999
Greg Clayton147e1fa2011-10-14 22:47:18 +00002000 return func_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001}
2002
Greg Claytona1e5dc82015-08-11 22:53:00 +00002003CompilerType
Greg Clayton6beaaa62011-01-17 03:46:26 +00002004ClangASTContext::CreateFunctionType (ASTContext *ast,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002005 const CompilerType& result_type,
2006 const CompilerType *args,
Sean Callananc81256a2010-09-16 20:40:25 +00002007 unsigned num_args,
2008 bool is_variadic,
2009 unsigned type_quals)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010{
Ed Masted4612ad2014-04-20 13:17:36 +00002011 assert (ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012 std::vector<QualType> qual_type_args;
2013 for (unsigned i=0; i<num_args; ++i)
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002014 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002015
2016 // TODO: Detect calling convention in DWARF?
Sean Callanan2c777c42011-01-18 23:32:05 +00002017 FunctionProtoType::ExtProtoInfo proto_info;
2018 proto_info.Variadic = is_variadic;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002019 proto_info.ExceptionSpec = EST_None;
Sean Callanan2c777c42011-01-18 23:32:05 +00002020 proto_info.TypeQuals = type_quals;
Sean Callananfb0b7582011-03-15 00:17:19 +00002021 proto_info.RefQualifier = RQ_None;
Sylvestre Ledru186ca7d2014-08-01 12:19:15 +00002022
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002023 return CompilerType(ast, ast->getFunctionType(ClangUtil::GetQualType(result_type), qual_type_args, proto_info));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024}
2025
2026ParmVarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00002027ClangASTContext::CreateParameterDeclaration (const char *name, const CompilerType &param_type, int storage)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028{
Greg Clayton6beaaa62011-01-17 03:46:26 +00002029 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002030 assert (ast != nullptr);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002031 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(), SourceLocation(), SourceLocation(),
2032 name && name[0] ? &ast->Idents.get(name) : nullptr, ClangUtil::GetQualType(param_type),
2033 nullptr, (clang::StorageClass)storage, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002034}
2035
2036void
2037ClangASTContext::SetFunctionParameters (FunctionDecl *function_decl, ParmVarDecl **params, unsigned num_params)
2038{
2039 if (function_decl)
Sean Callanan880e6802011-10-07 23:18:13 +00002040 function_decl->setParams (ArrayRef<ParmVarDecl*>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041}
2042
2043
2044#pragma mark Array Types
2045
Greg Claytona1e5dc82015-08-11 22:53:00 +00002046CompilerType
2047ClangASTContext::CreateArrayType (const CompilerType &element_type,
Greg Clayton1c8ef472013-04-05 23:27:21 +00002048 size_t element_count,
2049 bool is_vector)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002050{
Greg Clayton57ee3062013-07-11 22:46:58 +00002051 if (element_type.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002052 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002053 ASTContext *ast = getASTContext();
Ed Masted4612ad2014-04-20 13:17:36 +00002054 assert (ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002055
Greg Clayton1c8ef472013-04-05 23:27:21 +00002056 if (is_vector)
2057 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002058 return CompilerType(ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type), element_count));
Greg Clayton4ef877f2012-12-06 02:33:54 +00002059 }
2060 else
2061 {
Greg Clayton1c8ef472013-04-05 23:27:21 +00002062
2063 llvm::APInt ap_element_count (64, element_count);
2064 if (element_count == 0)
2065 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002066 return CompilerType(ast, ast->getIncompleteArrayType(ClangUtil::GetQualType(element_type),
2067 clang::ArrayType::Normal, 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002068 }
2069 else
2070 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002071 return CompilerType(ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2072 ap_element_count, clang::ArrayType::Normal, 0));
Greg Clayton1c8ef472013-04-05 23:27:21 +00002073 }
Greg Clayton4ef877f2012-12-06 02:33:54 +00002074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002076 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002077}
2078
Greg Claytona1e5dc82015-08-11 22:53:00 +00002079CompilerType
Enrico Granata76b08d52014-10-29 23:08:02 +00002080ClangASTContext::GetOrCreateStructForIdentifier (const ConstString &type_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002081 const std::initializer_list< std::pair < const char *, CompilerType > >& type_fields,
Enrico Granataa449e862014-10-30 00:53:28 +00002082 bool packed)
Enrico Granata76b08d52014-10-29 23:08:02 +00002083{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002084 CompilerType type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002085 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2086 return type;
2087 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), clang::TTK_Struct, lldb::eLanguageTypeC);
Greg Claytond8d4a572015-08-11 21:38:15 +00002088 StartTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002089 for (const auto& field : type_fields)
Greg Claytond8d4a572015-08-11 21:38:15 +00002090 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, 0);
Enrico Granataa449e862014-10-30 00:53:28 +00002091 if (packed)
Greg Claytond8d4a572015-08-11 21:38:15 +00002092 SetIsPacked(type);
2093 CompleteTagDeclarationDefinition(type);
Enrico Granata76b08d52014-10-29 23:08:02 +00002094 return type;
2095}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002096
2097#pragma mark Enumeration Types
2098
Greg Claytona1e5dc82015-08-11 22:53:00 +00002099CompilerType
Greg Claytond8d4a572015-08-11 21:38:15 +00002100ClangASTContext::CreateEnumerationType
Greg Claytonca512b32011-01-14 04:54:56 +00002101(
Greg Claytond8d4a572015-08-11 21:38:15 +00002102 const char *name,
2103 DeclContext *decl_ctx,
2104 const Declaration &decl,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002105 const CompilerType &integer_clang_type
Greg Claytond8d4a572015-08-11 21:38:15 +00002106 )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002107{
2108 // TODO: Do something intelligent with the Declaration object passed in
2109 // like maybe filling in the SourceLocation with it...
Greg Clayton6beaaa62011-01-17 03:46:26 +00002110 ASTContext *ast = getASTContext();
Greg Claytond8d4a572015-08-11 21:38:15 +00002111
Greg Claytone02b8502010-10-12 04:29:14 +00002112 // TODO: ask about these...
Greg Claytond8d4a572015-08-11 21:38:15 +00002113 // const bool IsScoped = false;
2114 // const bool IsFixed = false;
2115
Greg Clayton6beaaa62011-01-17 03:46:26 +00002116 EnumDecl *enum_decl = EnumDecl::Create (*ast,
Greg Claytonca512b32011-01-14 04:54:56 +00002117 decl_ctx,
Greg Claytone02b8502010-10-12 04:29:14 +00002118 SourceLocation(),
Greg Claytone02b8502010-10-12 04:29:14 +00002119 SourceLocation(),
Ed Masted4612ad2014-04-20 13:17:36 +00002120 name && name[0] ? &ast->Idents.get(name) : nullptr,
2121 nullptr,
Sean Callanan48114472010-12-13 01:26:27 +00002122 false, // IsScoped
2123 false, // IsScopedUsingClassTag
2124 false); // IsFixed
Sean Callanan2652ad22011-01-18 01:03:44 +00002125
2126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002127 if (enum_decl)
Greg Clayton83ff3892010-09-12 23:17:56 +00002128 {
2129 // TODO: check if we should be setting the promotion type too?
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002130 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2131
Sean Callanan2652ad22011-01-18 01:03:44 +00002132 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2133
Greg Claytona1e5dc82015-08-11 22:53:00 +00002134 return CompilerType (ast, ast->getTagDeclType(enum_decl));
Greg Clayton83ff3892010-09-12 23:17:56 +00002135 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002136 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002137}
2138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139// Disable this for now since I can't seem to get a nicely formatted float
2140// out of the APFloat class without just getting the float, double or quad
2141// and then using a formatted print on it which defeats the purpose. We ideally
2142// would like to get perfect string values for any kind of float semantics
2143// so we can support remote targets. The code below also requires a patch to
2144// llvm::APInt.
2145//bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002146//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 +00002147//{
2148// uint32_t count = 0;
2149// bool is_complex = false;
2150// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2151// {
2152// unsigned num_bytes_per_float = byte_size / count;
2153// unsigned num_bits_per_float = num_bytes_per_float * 8;
2154//
2155// float_str.clear();
2156// uint32_t i;
2157// for (i=0; i<count; i++)
2158// {
2159// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float, (APInt::ByteOrder)apint_byte_order);
2160// bool is_ieee = false;
2161// APFloat ap_float(ap_int, is_ieee);
2162// char s[1024];
2163// unsigned int hex_digits = 0;
2164// bool upper_case = false;
2165//
2166// if (ap_float.convertToHexString(s, hex_digits, upper_case, APFloat::rmNearestTiesToEven) > 0)
2167// {
2168// if (i > 0)
2169// float_str.append(", ");
2170// float_str.append(s);
2171// if (i == 1 && is_complex)
2172// float_str.append(1, 'i');
2173// }
2174// }
2175// return !float_str.empty();
2176// }
2177// return false;
2178//}
2179
Greg Claytona1e5dc82015-08-11 22:53:00 +00002180CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002181ClangASTContext::GetIntTypeFromBitSize (clang::ASTContext *ast,
2182 size_t bit_size, bool is_signed)
2183{
2184 if (ast)
2185 {
2186 if (is_signed)
2187 {
2188 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002189 return CompilerType(ast, ast->SignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002190
2191 if (bit_size == ast->getTypeSize(ast->ShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002192 return CompilerType(ast, ast->ShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002193
2194 if (bit_size == ast->getTypeSize(ast->IntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002195 return CompilerType(ast, ast->IntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002196
2197 if (bit_size == ast->getTypeSize(ast->LongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002198 return CompilerType(ast, ast->LongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002199
2200 if (bit_size == ast->getTypeSize(ast->LongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002201 return CompilerType(ast, ast->LongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002202
2203 if (bit_size == ast->getTypeSize(ast->Int128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002204 return CompilerType(ast, ast->Int128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002205 }
2206 else
2207 {
2208 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002209 return CompilerType(ast, ast->UnsignedCharTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002210
2211 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002212 return CompilerType(ast, ast->UnsignedShortTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002213
2214 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002215 return CompilerType(ast, ast->UnsignedIntTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002216
2217 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002218 return CompilerType(ast, ast->UnsignedLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002219
2220 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002221 return CompilerType(ast, ast->UnsignedLongLongTy);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002222
2223 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
Greg Claytona1e5dc82015-08-11 22:53:00 +00002224 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002225 }
2226 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00002227 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002228}
2229
Greg Claytona1e5dc82015-08-11 22:53:00 +00002230CompilerType
Enrico Granatae8bf7492014-08-15 23:00:02 +00002231ClangASTContext::GetPointerSizedIntType (clang::ASTContext *ast, bool is_signed)
2232{
2233 if (ast)
2234 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), is_signed);
Greg Claytona1e5dc82015-08-11 22:53:00 +00002235 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002236}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002237
Greg Claytone6b36cd2015-12-08 01:02:08 +00002238void
2239ClangASTContext::DumpDeclContextHiearchy (clang::DeclContext *decl_ctx)
2240{
2241 if (decl_ctx)
2242 {
2243 DumpDeclContextHiearchy (decl_ctx->getParent());
2244
2245 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2246 if (named_decl)
2247 {
2248 printf ("%20s: %s\n", decl_ctx->getDeclKindName(), named_decl->getDeclName().getAsString().c_str());
2249 }
2250 else
2251 {
2252 printf ("%20s\n", decl_ctx->getDeclKindName());
2253 }
2254 }
2255}
2256
2257void
2258ClangASTContext::DumpDeclHiearchy (clang::Decl *decl)
2259{
2260 if (decl == nullptr)
2261 return;
2262 DumpDeclContextHiearchy(decl->getDeclContext());
2263
2264 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2265 if (record_decl)
2266 {
2267 printf ("%20s: %s%s\n", decl->getDeclKindName(), record_decl->getDeclName().getAsString().c_str(), record_decl->isInjectedClassName() ? " (injected class name)" : "");
2268
2269 }
2270 else
2271 {
2272 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2273 if (named_decl)
2274 {
2275 printf ("%20s: %s\n", decl->getDeclKindName(), named_decl->getDeclName().getAsString().c_str());
2276 }
2277 else
2278 {
2279 printf ("%20s\n", decl->getDeclKindName());
2280 }
2281 }
2282}
2283
2284bool
2285ClangASTContext::DeclsAreEquivalent (clang::Decl *lhs_decl, clang::Decl *rhs_decl)
2286{
2287 if (lhs_decl && rhs_decl)
2288 {
2289 //----------------------------------------------------------------------
2290 // Make sure the decl kinds match first
2291 //----------------------------------------------------------------------
2292 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2293 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2294
2295 if (lhs_decl_kind == rhs_decl_kind)
2296 {
2297 //------------------------------------------------------------------
2298 // Now check that the decl contexts kinds are all equivalent
2299 // before we have to check any names of the decl contexts...
2300 //------------------------------------------------------------------
2301 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2302 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2303 if (lhs_decl_ctx && rhs_decl_ctx)
2304 {
2305 while (1)
2306 {
2307 if (lhs_decl_ctx && rhs_decl_ctx)
2308 {
2309 const clang::Decl::Kind lhs_decl_ctx_kind = lhs_decl_ctx->getDeclKind();
2310 const clang::Decl::Kind rhs_decl_ctx_kind = rhs_decl_ctx->getDeclKind();
2311 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind)
2312 {
2313 lhs_decl_ctx = lhs_decl_ctx->getParent();
2314 rhs_decl_ctx = rhs_decl_ctx->getParent();
2315
2316 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2317 break;
2318 }
2319 else
2320 return false;
2321 }
2322 else
2323 return false;
2324 }
2325
2326 //--------------------------------------------------------------
2327 // Now make sure the name of the decls match
2328 //--------------------------------------------------------------
2329 clang::NamedDecl *lhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2330 clang::NamedDecl *rhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2331 if (lhs_named_decl && rhs_named_decl)
2332 {
2333 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2334 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2335 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind())
2336 {
2337 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2338 return false;
2339 }
2340 else
2341 return false;
2342 }
2343 else
2344 return false;
2345
2346 //--------------------------------------------------------------
2347 // We know that the decl context kinds all match, so now we need
2348 // to make sure the names match as well
2349 //--------------------------------------------------------------
2350 lhs_decl_ctx = lhs_decl->getDeclContext();
2351 rhs_decl_ctx = rhs_decl->getDeclContext();
2352 while (1)
2353 {
2354 switch (lhs_decl_ctx->getDeclKind())
2355 {
2356 case clang::Decl::TranslationUnit:
2357 // We don't care about the translation unit names
2358 return true;
2359 default:
2360 {
2361 clang::NamedDecl *lhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2362 clang::NamedDecl *rhs_named_decl = llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2363 if (lhs_named_decl && rhs_named_decl)
2364 {
2365 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2366 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2367 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind())
2368 {
2369 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2370 return false;
2371 }
2372 else
2373 return false;
2374 }
2375 else
2376 return false;
2377 }
2378 break;
2379
2380 }
2381 lhs_decl_ctx = lhs_decl_ctx->getParent();
2382 rhs_decl_ctx = rhs_decl_ctx->getParent();
2383 }
2384 }
2385 }
2386 }
2387 return false;
2388}
Enrico Granata86027e92012-03-24 01:11:14 +00002389bool
Greg Claytona2721472011-06-25 00:44:06 +00002390ClangASTContext::GetCompleteDecl (clang::ASTContext *ast,
2391 clang::Decl *decl)
2392{
2393 if (!decl)
2394 return false;
2395
2396 ExternalASTSource *ast_source = ast->getExternalSource();
2397
2398 if (!ast_source)
2399 return false;
2400
2401 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
2402 {
Greg Clayton219cf312012-03-30 00:51:13 +00002403 if (tag_decl->isCompleteDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002404 return true;
2405
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002406 if (!tag_decl->hasExternalLexicalStorage())
2407 return false;
2408
Greg Claytona2721472011-06-25 00:44:06 +00002409 ast_source->CompleteType(tag_decl);
2410
2411 return !tag_decl->getTypeForDecl()->isIncompleteType();
2412 }
2413 else if (clang::ObjCInterfaceDecl *objc_interface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
2414 {
Sean Callanan5b26f272012-02-04 08:49:35 +00002415 if (objc_interface_decl->getDefinition())
Greg Claytona2721472011-06-25 00:44:06 +00002416 return true;
2417
2418 if (!objc_interface_decl->hasExternalLexicalStorage())
2419 return false;
2420
2421 ast_source->CompleteType(objc_interface_decl);
2422
Sean Callanan5b26f272012-02-04 08:49:35 +00002423 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
Greg Claytona2721472011-06-25 00:44:06 +00002424 }
2425 else
2426 {
2427 return false;
2428 }
2429}
2430
Sean Callanan60217122012-04-13 00:10:03 +00002431void
Greg Claytond0029442013-03-27 01:48:02 +00002432ClangASTContext::SetMetadataAsUserID (const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002433 user_id_t user_id)
2434{
2435 ClangASTMetadata meta_data;
2436 meta_data.SetUserID (user_id);
2437 SetMetadata (object, meta_data);
2438}
2439
2440void
Sean Callanan60217122012-04-13 00:10:03 +00002441ClangASTContext::SetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002442 const void *object,
Jim Ingham379397632012-10-27 02:54:13 +00002443 ClangASTMetadata &metadata)
Sean Callanan60217122012-04-13 00:10:03 +00002444{
2445 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002446 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002447
2448 if (external_source)
2449 external_source->SetMetadata(object, metadata);
2450}
2451
Jim Ingham379397632012-10-27 02:54:13 +00002452ClangASTMetadata *
Sean Callanan60217122012-04-13 00:10:03 +00002453ClangASTContext::GetMetadata (clang::ASTContext *ast,
Greg Claytond0029442013-03-27 01:48:02 +00002454 const void *object)
Sean Callanan60217122012-04-13 00:10:03 +00002455{
2456 ClangExternalASTSourceCommon *external_source =
Sean Callananceeb74e2014-12-06 01:03:30 +00002457 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
Sean Callanan60217122012-04-13 00:10:03 +00002458
2459 if (external_source && external_source->HasMetadata(object))
2460 return external_source->GetMetadata(object);
2461 else
Ed Masted4612ad2014-04-20 13:17:36 +00002462 return nullptr;
Sean Callanan60217122012-04-13 00:10:03 +00002463}
2464
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002465clang::DeclContext *
2466ClangASTContext::GetAsDeclContext (clang::CXXMethodDecl *cxx_method_decl)
2467{
Sean Callanana87bee82011-08-19 06:19:25 +00002468 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002469}
2470
2471clang::DeclContext *
2472ClangASTContext::GetAsDeclContext (clang::ObjCMethodDecl *objc_method_decl)
2473{
Sean Callanana87bee82011-08-19 06:19:25 +00002474 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00002475}
2476
Greg Claytond8d4a572015-08-11 21:38:15 +00002477bool
2478ClangASTContext::SetTagTypeKind (clang::QualType tag_qual_type, int kind) const
2479{
2480 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2481 if (clang_type)
2482 {
2483 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2484 if (tag_type)
2485 {
2486 clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2487 if (tag_decl)
2488 {
2489 tag_decl->setTagKind ((clang::TagDecl::TagKind)kind);
2490 return true;
2491 }
2492 }
2493 }
2494 return false;
2495}
2496
2497
2498bool
2499ClangASTContext::SetDefaultAccessForRecordFields (clang::RecordDecl* record_decl,
2500 int default_accessibility,
2501 int *assigned_accessibilities,
2502 size_t num_assigned_accessibilities)
2503{
2504 if (record_decl)
2505 {
2506 uint32_t field_idx;
2507 clang::RecordDecl::field_iterator field, field_end;
2508 for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
2509 field != field_end;
2510 ++field, ++field_idx)
2511 {
2512 // If no accessibility was assigned, assign the correct one
2513 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
2514 field->setAccess ((clang::AccessSpecifier)default_accessibility);
2515 }
2516 return true;
2517 }
2518 return false;
2519}
2520
2521clang::DeclContext *
Greg Clayton99558cc42015-08-24 23:46:31 +00002522ClangASTContext::GetDeclContextForType (const CompilerType& type)
2523{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002524 return GetDeclContextForType(ClangUtil::GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00002525}
2526
2527clang::DeclContext *
Greg Claytond8d4a572015-08-11 21:38:15 +00002528ClangASTContext::GetDeclContextForType (clang::QualType type)
2529{
2530 if (type.isNull())
2531 return nullptr;
2532
2533 clang::QualType qual_type = type.getCanonicalType();
2534 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2535 switch (type_class)
2536 {
Greg Claytond8d4a572015-08-11 21:38:15 +00002537 case clang::Type::ObjCInterface: return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())->getInterface();
2538 case clang::Type::ObjCObjectPointer: return GetDeclContextForType (llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
2539 case clang::Type::Record: return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2540 case clang::Type::Enum: return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2541 case clang::Type::Typedef: return GetDeclContextForType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
Enrico Granata36f51e42015-12-18 22:41:25 +00002542 case clang::Type::Auto: return GetDeclContextForType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002543 case clang::Type::Elaborated: return GetDeclContextForType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2544 case clang::Type::Paren: return GetDeclContextForType (llvm::cast<clang::ParenType>(qual_type)->desugar());
Greg Clayton99558cc42015-08-24 23:46:31 +00002545 default:
2546 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002547 }
2548 // No DeclContext in this type...
2549 return nullptr;
2550}
2551
2552static bool
2553GetCompleteQualType (clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion = true)
2554{
2555 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2556 switch (type_class)
2557 {
2558 case clang::Type::ConstantArray:
2559 case clang::Type::IncompleteArray:
2560 case clang::Type::VariableArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002561 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002562 const clang::ArrayType *array_type = llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2563
2564 if (array_type)
2565 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
2566 }
2567 break;
2568 case clang::Type::Record:
2569 {
2570 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2571 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002572 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002573 if (cxx_record_decl->hasExternalLexicalStorage())
Greg Claytond8d4a572015-08-11 21:38:15 +00002574 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002575 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2576 const bool fields_loaded = cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2577 if (is_complete && fields_loaded)
2578 return true;
2579
2580 if (!allow_completion)
2581 return false;
2582
2583 // Call the field_begin() accessor to for it to use the external source
2584 // to load the fields...
2585 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2586 if (external_ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002587 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002588 external_ast_source->CompleteType(cxx_record_decl);
2589 if (cxx_record_decl->isCompleteDefinition())
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002590 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002591 cxx_record_decl->setHasLoadedFieldsFromExternalStorage (true);
2592 cxx_record_decl->field_begin();
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002593 }
2594 }
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002595 }
2596 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002597 const clang::TagType *tag_type = llvm::cast<clang::TagType>(qual_type.getTypePtr());
2598 return !tag_type->isIncompleteType();
Greg Clayton5dfc4a42015-12-02 00:43:32 +00002599 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002600 break;
Greg Claytone6b36cd2015-12-08 01:02:08 +00002601
2602 case clang::Type::Enum:
2603 {
2604 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2605 if (tag_type)
2606 {
2607 clang::TagDecl *tag_decl = tag_type->getDecl();
2608 if (tag_decl)
2609 {
2610 if (tag_decl->getDefinition())
2611 return true;
2612
2613 if (!allow_completion)
2614 return false;
2615
2616 if (tag_decl->hasExternalLexicalStorage())
2617 {
2618 if (ast)
2619 {
2620 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2621 if (external_ast_source)
2622 {
2623 external_ast_source->CompleteType(tag_decl);
2624 return !tag_type->isIncompleteType();
2625 }
2626 }
2627 }
2628 return false;
2629 }
2630 }
2631
2632 }
2633 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00002634 case clang::Type::ObjCObject:
2635 case clang::Type::ObjCInterface:
Greg Claytond8d4a572015-08-11 21:38:15 +00002636 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002637 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2638 if (objc_class_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002639 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002640 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2641 // We currently can't complete objective C types through the newly added ASTContext
2642 // because it only supports TagDecl objects right now...
2643 if (class_interface_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002644 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002645 if (class_interface_decl->getDefinition())
2646 return true;
2647
2648 if (!allow_completion)
2649 return false;
2650
2651 if (class_interface_decl->hasExternalLexicalStorage())
Greg Claytond8d4a572015-08-11 21:38:15 +00002652 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002653 if (ast)
Greg Claytond8d4a572015-08-11 21:38:15 +00002654 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00002655 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2656 if (external_ast_source)
2657 {
2658 external_ast_source->CompleteType (class_interface_decl);
2659 return !objc_class_type->isIncompleteType();
2660 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002661 }
2662 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002663 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002664 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002665 }
2666 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002667 break;
2668
2669 case clang::Type::Typedef:
2670 return GetCompleteQualType (ast, llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
2671
Enrico Granata36f51e42015-12-18 22:41:25 +00002672 case clang::Type::Auto:
2673 return GetCompleteQualType (ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), allow_completion);
2674
Greg Claytond8d4a572015-08-11 21:38:15 +00002675 case clang::Type::Elaborated:
2676 return GetCompleteQualType (ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), allow_completion);
2677
2678 case clang::Type::Paren:
2679 return GetCompleteQualType (ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), allow_completion);
Greg Claytone6b36cd2015-12-08 01:02:08 +00002680
2681 case clang::Type::Attributed:
2682 return GetCompleteQualType (ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), allow_completion);
2683
Greg Claytond8d4a572015-08-11 21:38:15 +00002684 default:
2685 break;
2686 }
2687
2688 return true;
2689}
2690
2691static clang::ObjCIvarDecl::AccessControl
2692ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
2693{
2694 switch (access)
2695 {
2696 case eAccessNone: return clang::ObjCIvarDecl::None;
2697 case eAccessPublic: return clang::ObjCIvarDecl::Public;
2698 case eAccessPrivate: return clang::ObjCIvarDecl::Private;
2699 case eAccessProtected: return clang::ObjCIvarDecl::Protected;
2700 case eAccessPackage: return clang::ObjCIvarDecl::Package;
2701 }
2702 return clang::ObjCIvarDecl::None;
2703}
2704
2705
2706//----------------------------------------------------------------------
2707// Tests
2708//----------------------------------------------------------------------
2709
2710bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002711ClangASTContext::IsAggregateType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002712{
2713 clang::QualType qual_type (GetCanonicalQualType(type));
2714
2715 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2716 switch (type_class)
2717 {
2718 case clang::Type::IncompleteArray:
2719 case clang::Type::VariableArray:
2720 case clang::Type::ConstantArray:
2721 case clang::Type::ExtVector:
2722 case clang::Type::Vector:
2723 case clang::Type::Record:
2724 case clang::Type::ObjCObject:
2725 case clang::Type::ObjCInterface:
2726 return true;
Enrico Granata36f51e42015-12-18 22:41:25 +00002727 case clang::Type::Auto:
2728 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00002729 case clang::Type::Elaborated:
2730 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2731 case clang::Type::Typedef:
2732 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2733 case clang::Type::Paren:
2734 return IsAggregateType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2735 default:
2736 break;
2737 }
2738 // The clang type does have a value
2739 return false;
2740}
2741
2742bool
Enrico Granata7123e2b2015-11-07 02:06:57 +00002743ClangASTContext::IsAnonymousType (lldb::opaque_compiler_type_t type)
2744{
2745 clang::QualType qual_type (GetCanonicalQualType(type));
2746
2747 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2748 switch (type_class)
2749 {
2750 case clang::Type::Record:
2751 {
2752 if (const clang::RecordType *record_type = llvm::dyn_cast_or_null<clang::RecordType>(qual_type.getTypePtrOrNull()))
2753 {
2754 if (const clang::RecordDecl *record_decl = record_type->getDecl())
2755 {
2756 return record_decl->isAnonymousStructOrUnion();
2757 }
2758 }
2759 break;
2760 }
Enrico Granata36f51e42015-12-18 22:41:25 +00002761 case clang::Type::Auto:
2762 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Enrico Granata7123e2b2015-11-07 02:06:57 +00002763 case clang::Type::Elaborated:
2764 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
2765 case clang::Type::Typedef:
2766 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
2767 case clang::Type::Paren:
2768 return IsAnonymousType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2769 default:
2770 break;
2771 }
2772 // The clang type does have a value
2773 return false;
2774}
2775
2776bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002777ClangASTContext::IsArrayType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002778 CompilerType *element_type_ptr,
Greg Claytond8d4a572015-08-11 21:38:15 +00002779 uint64_t *size,
2780 bool *is_incomplete)
2781{
2782 clang::QualType qual_type (GetCanonicalQualType(type));
2783
2784 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2785 switch (type_class)
2786 {
2787 default:
2788 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002789
Greg Claytond8d4a572015-08-11 21:38:15 +00002790 case clang::Type::ConstantArray:
2791 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002792 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002793 if (size)
2794 *size = llvm::cast<clang::ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002795 if (is_incomplete)
2796 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002797 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002798
Greg Claytond8d4a572015-08-11 21:38:15 +00002799 case clang::Type::IncompleteArray:
2800 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002801 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002802 if (size)
2803 *size = 0;
2804 if (is_incomplete)
2805 *is_incomplete = true;
2806 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002807
Greg Claytond8d4a572015-08-11 21:38:15 +00002808 case clang::Type::VariableArray:
2809 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002810 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002811 if (size)
2812 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002813 if (is_incomplete)
2814 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002815 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002816
Greg Claytond8d4a572015-08-11 21:38:15 +00002817 case clang::Type::DependentSizedArray:
2818 if (element_type_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00002819 element_type_ptr->SetCompilerType (getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002820 if (size)
2821 *size = 0;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002822 if (is_incomplete)
2823 *is_incomplete = false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002824 return true;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002825
Greg Claytond8d4a572015-08-11 21:38:15 +00002826 case clang::Type::Typedef:
2827 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
2828 element_type_ptr,
2829 size,
2830 is_incomplete);
Enrico Granata36f51e42015-12-18 22:41:25 +00002831 case clang::Type::Auto:
2832 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(),
2833 element_type_ptr,
2834 size,
2835 is_incomplete);
Greg Claytond8d4a572015-08-11 21:38:15 +00002836 case clang::Type::Elaborated:
2837 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
2838 element_type_ptr,
2839 size,
2840 is_incomplete);
2841 case clang::Type::Paren:
2842 return IsArrayType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2843 element_type_ptr,
2844 size,
2845 is_incomplete);
2846 }
2847 if (element_type_ptr)
2848 element_type_ptr->Clear();
2849 if (size)
2850 *size = 0;
2851 if (is_incomplete)
2852 *is_incomplete = false;
Bruce Mitchener778e7ab2015-09-16 00:00:16 +00002853 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002854}
2855
2856bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002857ClangASTContext::IsVectorType (lldb::opaque_compiler_type_t type,
Greg Claytona1e5dc82015-08-11 22:53:00 +00002858 CompilerType *element_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00002859 uint64_t *size)
2860{
2861 clang::QualType qual_type (GetCanonicalQualType(type));
2862
2863 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2864 switch (type_class)
2865 {
2866 case clang::Type::Vector:
2867 {
2868 const clang::VectorType *vector_type = qual_type->getAs<clang::VectorType>();
2869 if (vector_type)
2870 {
2871 if (size)
2872 *size = vector_type->getNumElements();
2873 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002874 *element_type = CompilerType(getASTContext(), vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002875 }
2876 return true;
2877 }
2878 break;
2879 case clang::Type::ExtVector:
2880 {
2881 const clang::ExtVectorType *ext_vector_type = qual_type->getAs<clang::ExtVectorType>();
2882 if (ext_vector_type)
2883 {
2884 if (size)
2885 *size = ext_vector_type->getNumElements();
2886 if (element_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00002887 *element_type = CompilerType(getASTContext(), ext_vector_type->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002888 }
2889 return true;
2890 }
2891 default:
2892 break;
2893 }
2894 return false;
2895}
2896
2897bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002898ClangASTContext::IsRuntimeGeneratedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002899{
2900 clang::DeclContext* decl_ctx = ClangASTContext::GetASTContext(getASTContext())->GetDeclContextForType(GetQualType(type));
2901 if (!decl_ctx)
2902 return false;
2903
2904 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2905 return false;
2906
2907 clang::ObjCInterfaceDecl *result_iface_decl = llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2908
2909 ClangASTMetadata* ast_metadata = ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2910 if (!ast_metadata)
2911 return false;
2912 return (ast_metadata->GetISAPtr() != 0);
2913}
2914
2915bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002916ClangASTContext::IsCharType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002917{
2918 return GetQualType(type).getUnqualifiedType()->isCharType();
2919}
2920
2921
2922bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002923ClangASTContext::IsCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002924{
2925 const bool allow_completion = false;
2926 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
2927}
2928
2929bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002930ClangASTContext::IsConst(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00002931{
2932 return GetQualType(type).isConstQualified();
2933}
2934
2935bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002936ClangASTContext::IsCStringType (lldb::opaque_compiler_type_t type, uint32_t &length)
Greg Claytond8d4a572015-08-11 21:38:15 +00002937{
Greg Claytona1e5dc82015-08-11 22:53:00 +00002938 CompilerType pointee_or_element_clang_type;
Greg Claytond8d4a572015-08-11 21:38:15 +00002939 length = 0;
2940 Flags type_flags (GetTypeInfo (type, &pointee_or_element_clang_type));
2941
2942 if (!pointee_or_element_clang_type.IsValid())
2943 return false;
2944
2945 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
2946 {
2947 if (pointee_or_element_clang_type.IsCharType())
2948 {
2949 if (type_flags.Test (eTypeIsArray))
2950 {
2951 // We know the size of the array and it could be a C string
2952 // since it is an array of characters
2953 length = llvm::cast<clang::ConstantArrayType>(GetCanonicalQualType(type).getTypePtr())->getSize().getLimitedValue();
2954 }
2955 return true;
2956
2957 }
2958 }
2959 return false;
2960}
2961
2962bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00002963ClangASTContext::IsFunctionType (lldb::opaque_compiler_type_t type, bool *is_variadic_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00002964{
2965 if (type)
2966 {
2967 clang::QualType qual_type (GetCanonicalQualType(type));
2968
2969 if (qual_type->isFunctionType())
2970 {
2971 if (is_variadic_ptr)
2972 {
2973 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2974 if (function_proto_type)
2975 *is_variadic_ptr = function_proto_type->isVariadic();
2976 else
2977 *is_variadic_ptr = false;
2978 }
2979 return true;
2980 }
2981
2982 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2983 switch (type_class)
2984 {
2985 default:
2986 break;
2987 case clang::Type::Typedef:
2988 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), nullptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00002989 case clang::Type::Auto:
2990 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), nullptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00002991 case clang::Type::Elaborated:
2992 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), nullptr);
2993 case clang::Type::Paren:
2994 return IsFunctionType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), nullptr);
2995 case clang::Type::LValueReference:
2996 case clang::Type::RValueReference:
2997 {
2998 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2999 if (reference_type)
3000 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), nullptr);
3001 }
3002 break;
3003 }
3004 }
3005 return false;
3006}
3007
3008// Used to detect "Homogeneous Floating-point Aggregates"
3009uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003010ClangASTContext::IsHomogeneousAggregate (lldb::opaque_compiler_type_t type, CompilerType* base_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003011{
3012 if (!type)
3013 return 0;
3014
3015 clang::QualType qual_type(GetCanonicalQualType(type));
3016 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3017 switch (type_class)
3018 {
3019 case clang::Type::Record:
3020 if (GetCompleteType (type))
3021 {
3022 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3023 if (cxx_record_decl)
3024 {
3025 if (cxx_record_decl->getNumBases() ||
3026 cxx_record_decl->isDynamicClass())
3027 return 0;
3028 }
3029 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3030 if (record_type)
3031 {
3032 const clang::RecordDecl *record_decl = record_type->getDecl();
3033 if (record_decl)
3034 {
3035 // We are looking for a structure that contains only floating point types
3036 clang::RecordDecl::field_iterator field_pos, field_end = record_decl->field_end();
3037 uint32_t num_fields = 0;
3038 bool is_hva = false;
3039 bool is_hfa = false;
3040 clang::QualType base_qual_type;
Omair Javaid92a8ded2016-02-24 12:17:43 +00003041 uint64_t base_bitwidth = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003042 for (field_pos = record_decl->field_begin(); field_pos != field_end; ++field_pos)
3043 {
3044 clang::QualType field_qual_type = field_pos->getType();
Omair Javaid92a8ded2016-02-24 12:17:43 +00003045 uint64_t field_bitwidth = getASTContext()->getTypeSize (qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003046 if (field_qual_type->isFloatingType())
3047 {
3048 if (field_qual_type->isComplexType())
3049 return 0;
3050 else
3051 {
3052 if (num_fields == 0)
3053 base_qual_type = field_qual_type;
3054 else
3055 {
3056 if (is_hva)
3057 return 0;
3058 is_hfa = true;
3059 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3060 return 0;
3061 }
3062 }
3063 }
3064 else if (field_qual_type->isVectorType() || field_qual_type->isExtVectorType())
3065 {
Omair Javaid92a8ded2016-02-24 12:17:43 +00003066 if (num_fields == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00003067 {
Omair Javaid92a8ded2016-02-24 12:17:43 +00003068 base_qual_type = field_qual_type;
3069 base_bitwidth = field_bitwidth;
Greg Claytond8d4a572015-08-11 21:38:15 +00003070 }
3071 else
Omair Javaid92a8ded2016-02-24 12:17:43 +00003072 {
3073 if (is_hfa)
3074 return 0;
3075 is_hva = true;
3076 if (base_bitwidth != field_bitwidth)
3077 return 0;
3078 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3079 return 0;
3080 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003081 }
3082 else
3083 return 0;
3084 ++num_fields;
3085 }
3086 if (base_type_ptr)
Greg Claytona1e5dc82015-08-11 22:53:00 +00003087 *base_type_ptr = CompilerType (getASTContext(), base_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003088 return num_fields;
3089 }
3090 }
3091 }
3092 break;
3093
3094 case clang::Type::Typedef:
3095 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), base_type_ptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00003096
3097 case clang::Type::Auto:
3098 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), base_type_ptr);
Greg Claytond8d4a572015-08-11 21:38:15 +00003099
3100 case clang::Type::Elaborated:
3101 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), base_type_ptr);
3102 default:
3103 break;
3104 }
3105 return 0;
3106}
3107
3108size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003109ClangASTContext::GetNumberOfFunctionArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003110{
3111 if (type)
3112 {
3113 clang::QualType qual_type (GetCanonicalQualType(type));
3114 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3115 if (func)
3116 return func->getNumParams();
3117 }
3118 return 0;
3119}
3120
Greg Claytona1e5dc82015-08-11 22:53:00 +00003121CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003122ClangASTContext::GetFunctionArgumentAtIndex (lldb::opaque_compiler_type_t type, const size_t index)
Greg Claytond8d4a572015-08-11 21:38:15 +00003123{
3124 if (type)
3125 {
Greg Clayton0a5f8052016-03-15 22:43:26 +00003126 clang::QualType qual_type (GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00003127 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3128 if (func)
3129 {
3130 if (index < func->getNumParams())
Greg Claytona1e5dc82015-08-11 22:53:00 +00003131 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003132 }
3133 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00003134 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003135}
3136
3137bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003138ClangASTContext::IsFunctionPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003139{
3140 if (type)
3141 {
3142 clang::QualType qual_type (GetCanonicalQualType(type));
3143
3144 if (qual_type->isFunctionPointerType())
3145 return true;
3146
3147 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3148 switch (type_class)
3149 {
3150 default:
3151 break;
3152 case clang::Type::Typedef:
3153 return IsFunctionPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
Enrico Granata36f51e42015-12-18 22:41:25 +00003154 case clang::Type::Auto:
3155 return IsFunctionPointerType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003156 case clang::Type::Elaborated:
3157 return IsFunctionPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
3158 case clang::Type::Paren:
3159 return IsFunctionPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3160
3161 case clang::Type::LValueReference:
3162 case clang::Type::RValueReference:
3163 {
3164 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3165 if (reference_type)
3166 return IsFunctionPointerType(reference_type->getPointeeType().getAsOpaquePtr());
3167 }
3168 break;
3169 }
3170 }
3171 return false;
3172
3173}
3174
3175bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003176ClangASTContext::IsIntegerType (lldb::opaque_compiler_type_t type, bool &is_signed)
Greg Claytond8d4a572015-08-11 21:38:15 +00003177{
3178 if (!type)
3179 return false;
3180
3181 clang::QualType qual_type (GetCanonicalQualType(type));
3182 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3183
3184 if (builtin_type)
3185 {
3186 if (builtin_type->isInteger())
3187 {
3188 is_signed = builtin_type->isSignedInteger();
3189 return true;
3190 }
3191 }
3192
3193 return false;
3194}
3195
3196bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003197ClangASTContext::IsPointerType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003198{
3199 if (type)
3200 {
3201 clang::QualType qual_type (GetCanonicalQualType(type));
3202 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3203 switch (type_class)
3204 {
3205 case clang::Type::Builtin:
3206 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3207 {
3208 default:
3209 break;
3210 case clang::BuiltinType::ObjCId:
3211 case clang::BuiltinType::ObjCClass:
3212 return true;
3213 }
3214 return false;
3215 case clang::Type::ObjCObjectPointer:
3216 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003217 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003218 return true;
3219 case clang::Type::BlockPointer:
3220 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003221 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003222 return true;
3223 case clang::Type::Pointer:
3224 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003225 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003226 return true;
3227 case clang::Type::MemberPointer:
3228 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003229 pointee_type->SetCompilerType (getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003230 return true;
3231 case clang::Type::Typedef:
3232 return IsPointerType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
Enrico Granata36f51e42015-12-18 22:41:25 +00003233 case clang::Type::Auto:
3234 return IsPointerType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003235 case clang::Type::Elaborated:
3236 return IsPointerType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3237 case clang::Type::Paren:
3238 return IsPointerType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3239 default:
3240 break;
3241 }
3242 }
3243 if (pointee_type)
3244 pointee_type->Clear();
3245 return false;
3246}
3247
3248
3249bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003250ClangASTContext::IsPointerOrReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003251{
3252 if (type)
3253 {
3254 clang::QualType qual_type (GetCanonicalQualType(type));
3255 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3256 switch (type_class)
3257 {
3258 case clang::Type::Builtin:
3259 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3260 {
3261 default:
3262 break;
3263 case clang::BuiltinType::ObjCId:
3264 case clang::BuiltinType::ObjCClass:
3265 return true;
3266 }
3267 return false;
3268 case clang::Type::ObjCObjectPointer:
3269 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003270 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003271 return true;
3272 case clang::Type::BlockPointer:
3273 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003274 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003275 return true;
3276 case clang::Type::Pointer:
3277 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003278 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003279 return true;
3280 case clang::Type::MemberPointer:
3281 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003282 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003283 return true;
3284 case clang::Type::LValueReference:
3285 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003286 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003287 return true;
3288 case clang::Type::RValueReference:
3289 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003290 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003291 return true;
3292 case clang::Type::Typedef:
3293 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type);
Enrico Granata36f51e42015-12-18 22:41:25 +00003294 case clang::Type::Auto:
3295 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003296 case clang::Type::Elaborated:
3297 return IsPointerOrReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type);
3298 case clang::Type::Paren:
3299 return IsPointerOrReferenceType(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type);
3300 default:
3301 break;
3302 }
3303 }
3304 if (pointee_type)
3305 pointee_type->Clear();
3306 return false;
3307}
3308
3309
3310bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003311ClangASTContext::IsReferenceType (lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool* is_rvalue)
Greg Claytond8d4a572015-08-11 21:38:15 +00003312{
3313 if (type)
3314 {
3315 clang::QualType qual_type (GetCanonicalQualType(type));
3316 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3317
3318 switch (type_class)
3319 {
3320 case clang::Type::LValueReference:
3321 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003322 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003323 if (is_rvalue)
3324 *is_rvalue = false;
3325 return true;
3326 case clang::Type::RValueReference:
3327 if (pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003328 pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
Greg Claytond8d4a572015-08-11 21:38:15 +00003329 if (is_rvalue)
3330 *is_rvalue = true;
3331 return true;
3332 case clang::Type::Typedef:
3333 return IsReferenceType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), pointee_type, is_rvalue);
Enrico Granata36f51e42015-12-18 22:41:25 +00003334 case clang::Type::Auto:
3335 return IsReferenceType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), pointee_type, is_rvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00003336 case clang::Type::Elaborated:
3337 return IsReferenceType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), pointee_type, is_rvalue);
3338 case clang::Type::Paren:
3339 return IsReferenceType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), pointee_type, is_rvalue);
3340
3341 default:
3342 break;
3343 }
3344 }
3345 if (pointee_type)
3346 pointee_type->Clear();
3347 return false;
3348}
3349
3350bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003351ClangASTContext::IsFloatingPointType (lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex)
Greg Claytond8d4a572015-08-11 21:38:15 +00003352{
3353 if (type)
3354 {
3355 clang::QualType qual_type (GetCanonicalQualType(type));
3356
3357 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()))
3358 {
3359 clang::BuiltinType::Kind kind = BT->getKind();
3360 if (kind >= clang::BuiltinType::Float && kind <= clang::BuiltinType::LongDouble)
3361 {
3362 count = 1;
3363 is_complex = false;
3364 return true;
3365 }
3366 }
3367 else if (const clang::ComplexType *CT = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal()))
3368 {
3369 if (IsFloatingPointType (CT->getElementType().getAsOpaquePtr(), count, is_complex))
3370 {
3371 count = 2;
3372 is_complex = true;
3373 return true;
3374 }
3375 }
3376 else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal()))
3377 {
3378 if (IsFloatingPointType (VT->getElementType().getAsOpaquePtr(), count, is_complex))
3379 {
3380 count = VT->getNumElements();
3381 is_complex = false;
3382 return true;
3383 }
3384 }
3385 }
3386 count = 0;
3387 is_complex = false;
3388 return false;
3389}
3390
3391
3392bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003393ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003394{
3395 if (!type)
3396 return false;
3397
3398 clang::QualType qual_type(GetQualType(type));
3399 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3400 if (tag_type)
3401 {
3402 clang::TagDecl *tag_decl = tag_type->getDecl();
3403 if (tag_decl)
3404 return tag_decl->isCompleteDefinition();
3405 return false;
3406 }
3407 else
3408 {
3409 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3410 if (objc_class_type)
3411 {
3412 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3413 if (class_interface_decl)
3414 return class_interface_decl->getDefinition() != nullptr;
3415 return false;
3416 }
3417 }
3418 return true;
3419}
3420
3421bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003422ClangASTContext::IsObjCClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003423{
3424 if (type)
3425 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003426 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3427
Greg Claytond8d4a572015-08-11 21:38:15 +00003428 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3429
3430 if (obj_pointer_type)
3431 return obj_pointer_type->isObjCClassType();
3432 }
3433 return false;
3434}
3435
3436bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003437ClangASTContext::IsObjCObjectOrInterfaceType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003438{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003439 if (ClangUtil::IsClangType(type))
3440 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003441 return false;
3442}
3443
3444bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003445ClangASTContext::IsPolymorphicClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003446{
3447 if (type)
3448 {
3449 clang::QualType qual_type(GetCanonicalQualType(type));
3450 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3451 switch (type_class)
3452 {
3453 case clang::Type::Record:
3454 if (GetCompleteType(type))
3455 {
3456 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3457 const clang::RecordDecl *record_decl = record_type->getDecl();
3458 if (record_decl)
3459 {
3460 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3461 if (cxx_record_decl)
3462 return cxx_record_decl->isPolymorphic();
3463 }
3464 }
3465 break;
3466
3467 default:
3468 break;
3469 }
3470 }
3471 return false;
3472}
3473
3474bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003475ClangASTContext::IsPossibleDynamicType (lldb::opaque_compiler_type_t type, CompilerType *dynamic_pointee_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00003476 bool check_cplusplus,
3477 bool check_objc)
3478{
3479 clang::QualType pointee_qual_type;
3480 if (type)
3481 {
3482 clang::QualType qual_type (GetCanonicalQualType(type));
3483 bool success = false;
3484 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3485 switch (type_class)
3486 {
3487 case clang::Type::Builtin:
3488 if (check_objc && llvm::cast<clang::BuiltinType>(qual_type)->getKind() == clang::BuiltinType::ObjCId)
3489 {
3490 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003491 dynamic_pointee_type->SetCompilerType(this, type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003492 return true;
3493 }
3494 break;
3495
3496 case clang::Type::ObjCObjectPointer:
3497 if (check_objc)
3498 {
3499 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003500 dynamic_pointee_type->SetCompilerType(getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003501 return true;
3502 }
3503 break;
3504
3505 case clang::Type::Pointer:
3506 pointee_qual_type = llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3507 success = true;
3508 break;
3509
3510 case clang::Type::LValueReference:
3511 case clang::Type::RValueReference:
3512 pointee_qual_type = llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3513 success = true;
3514 break;
3515
3516 case clang::Type::Typedef:
3517 return IsPossibleDynamicType (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(),
3518 dynamic_pointee_type,
3519 check_cplusplus,
3520 check_objc);
Enrico Granata36f51e42015-12-18 22:41:25 +00003521
3522 case clang::Type::Auto:
3523 return IsPossibleDynamicType (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(),
3524 dynamic_pointee_type,
3525 check_cplusplus,
3526 check_objc);
Greg Claytond8d4a572015-08-11 21:38:15 +00003527
3528 case clang::Type::Elaborated:
3529 return IsPossibleDynamicType (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(),
3530 dynamic_pointee_type,
3531 check_cplusplus,
3532 check_objc);
3533
3534 case clang::Type::Paren:
3535 return IsPossibleDynamicType (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3536 dynamic_pointee_type,
3537 check_cplusplus,
3538 check_objc);
3539 default:
3540 break;
3541 }
3542
3543 if (success)
3544 {
3545 // Check to make sure what we are pointing too is a possible dynamic C++ type
3546 // We currently accept any "void *" (in case we have a class that has been
3547 // watered down to an opaque pointer) and virtual C++ classes.
3548 const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
3549 switch (pointee_type_class)
3550 {
3551 case clang::Type::Builtin:
3552 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind())
3553 {
3554 case clang::BuiltinType::UnknownAny:
3555 case clang::BuiltinType::Void:
3556 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003557 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003558 return true;
Zachary Turnerdd07e002015-09-16 18:08:45 +00003559 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00003560 break;
3561 }
3562 break;
3563
3564 case clang::Type::Record:
3565 if (check_cplusplus)
3566 {
3567 clang::CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
3568 if (cxx_record_decl)
3569 {
3570 bool is_complete = cxx_record_decl->isCompleteDefinition();
3571
3572 if (is_complete)
3573 success = cxx_record_decl->isDynamicClass();
3574 else
3575 {
3576 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), cxx_record_decl);
3577 if (metadata)
3578 success = metadata->GetIsDynamicCXXType();
3579 else
3580 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00003581 is_complete = CompilerType(getASTContext(), pointee_qual_type).GetCompleteType();
Greg Claytond8d4a572015-08-11 21:38:15 +00003582 if (is_complete)
3583 success = cxx_record_decl->isDynamicClass();
3584 else
3585 success = false;
3586 }
3587 }
3588
3589 if (success)
3590 {
3591 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003592 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003593 return true;
3594 }
3595 }
3596 }
3597 break;
3598
3599 case clang::Type::ObjCObject:
3600 case clang::Type::ObjCInterface:
3601 if (check_objc)
3602 {
3603 if (dynamic_pointee_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003604 dynamic_pointee_type->SetCompilerType(getASTContext(), pointee_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003605 return true;
3606 }
3607 break;
3608
3609 default:
3610 break;
3611 }
3612 }
3613 }
3614 if (dynamic_pointee_type)
3615 dynamic_pointee_type->Clear();
3616 return false;
3617}
3618
3619
3620bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003621ClangASTContext::IsScalarType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003622{
3623 if (!type)
3624 return false;
3625
3626 return (GetTypeInfo (type, nullptr) & eTypeIsScalar) != 0;
3627}
3628
3629bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003630ClangASTContext::IsTypedefType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003631{
3632 if (!type)
3633 return false;
3634 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3635}
3636
3637bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003638ClangASTContext::IsVoidType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003639{
3640 if (!type)
3641 return false;
3642 return GetCanonicalQualType(type)->isVoidType();
3643}
3644
3645bool
Greg Clayton56939cb2015-09-17 22:23:34 +00003646ClangASTContext::SupportsLanguage (lldb::LanguageType language)
3647{
3648 return ClangASTContextSupportsLanguage(language);
3649}
3650
3651bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003652ClangASTContext::GetCXXClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003653{
3654 if (type)
3655 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003656 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003657 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00003658 {
Ryan Brown57bee1e2015-09-14 22:45:11 +00003659 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3660 if (cxx_record_decl)
3661 {
3662 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3663 return true;
3664 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003665 }
3666 }
3667 class_name.clear();
3668 return false;
3669}
3670
3671
3672bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003673ClangASTContext::IsCXXClassType (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003674{
3675 if (!type)
3676 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003677
3678 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003679 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003680 return true;
3681 return false;
3682}
3683
3684bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003685ClangASTContext::IsBeingDefined (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003686{
3687 if (!type)
3688 return false;
3689 clang::QualType qual_type (GetCanonicalQualType(type));
3690 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3691 if (tag_type)
3692 return tag_type->isBeingDefined();
3693 return false;
3694}
3695
3696bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003697ClangASTContext::IsObjCObjectPointerType (const CompilerType& type, CompilerType *class_type_ptr)
Greg Claytond8d4a572015-08-11 21:38:15 +00003698{
3699 if (!type)
3700 return false;
3701
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003702 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Ryan Brown57bee1e2015-09-14 22:45:11 +00003703
3704 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType())
Greg Claytond8d4a572015-08-11 21:38:15 +00003705 {
3706 if (class_type_ptr)
3707 {
3708 if (!qual_type->isObjCClassType() &&
3709 !qual_type->isObjCIdType())
3710 {
3711 const clang::ObjCObjectPointerType *obj_pointer_type = llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3712 if (obj_pointer_type == nullptr)
3713 class_type_ptr->Clear();
3714 else
Greg Clayton99558cc42015-08-24 23:46:31 +00003715 class_type_ptr->SetCompilerType (type.GetTypeSystem(), clang::QualType(obj_pointer_type->getInterfaceType(), 0).getAsOpaquePtr());
Greg Claytond8d4a572015-08-11 21:38:15 +00003716 }
3717 }
3718 return true;
3719 }
3720 if (class_type_ptr)
3721 class_type_ptr->Clear();
3722 return false;
3723}
3724
3725bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00003726ClangASTContext::GetObjCClassName (const CompilerType& type, std::string &class_name)
Greg Claytond8d4a572015-08-11 21:38:15 +00003727{
3728 if (!type)
3729 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003730
3731 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3732
Greg Claytond8d4a572015-08-11 21:38:15 +00003733 const clang::ObjCObjectType *object_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3734 if (object_type)
3735 {
3736 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3737 if (interface)
3738 {
3739 class_name = interface->getNameAsString();
3740 return true;
3741 }
3742 }
3743 return false;
3744}
3745
3746
3747//----------------------------------------------------------------------
3748// Type Completion
3749//----------------------------------------------------------------------
3750
3751bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003752ClangASTContext::GetCompleteType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003753{
3754 if (!type)
3755 return false;
3756 const bool allow_completion = true;
3757 return GetCompleteQualType (getASTContext(), GetQualType(type), allow_completion);
3758}
3759
3760ConstString
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003761ClangASTContext::GetTypeName (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003762{
3763 std::string type_name;
3764 if (type)
3765 {
3766 clang::PrintingPolicy printing_policy (getASTContext()->getPrintingPolicy());
3767 clang::QualType qual_type(GetQualType(type));
3768 printing_policy.SuppressTagKeyword = true;
3769 printing_policy.LangOpts.WChar = true;
3770 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
3771 if (typedef_type)
3772 {
3773 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3774 type_name = typedef_decl->getQualifiedNameAsString();
3775 }
3776 else
3777 {
3778 type_name = qual_type.getAsString(printing_policy);
3779 }
3780 }
3781 return ConstString(type_name);
3782}
3783
3784uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003785ClangASTContext::GetTypeInfo (lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003786{
3787 if (!type)
3788 return 0;
3789
3790 if (pointee_or_element_clang_type)
3791 pointee_or_element_clang_type->Clear();
3792
3793 clang::QualType qual_type (GetQualType(type));
3794
3795 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3796 switch (type_class)
3797 {
3798 case clang::Type::Builtin:
3799 {
3800 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3801
3802 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3803 switch (builtin_type->getKind())
3804 {
3805 case clang::BuiltinType::ObjCId:
3806 case clang::BuiltinType::ObjCClass:
3807 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003808 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003809 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3810 break;
3811
3812 case clang::BuiltinType::ObjCSel:
3813 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003814 pointee_or_element_clang_type->SetCompilerType(getASTContext(), getASTContext()->CharTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003815 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3816 break;
3817
3818 case clang::BuiltinType::Bool:
3819 case clang::BuiltinType::Char_U:
3820 case clang::BuiltinType::UChar:
3821 case clang::BuiltinType::WChar_U:
3822 case clang::BuiltinType::Char16:
3823 case clang::BuiltinType::Char32:
3824 case clang::BuiltinType::UShort:
3825 case clang::BuiltinType::UInt:
3826 case clang::BuiltinType::ULong:
3827 case clang::BuiltinType::ULongLong:
3828 case clang::BuiltinType::UInt128:
3829 case clang::BuiltinType::Char_S:
3830 case clang::BuiltinType::SChar:
3831 case clang::BuiltinType::WChar_S:
3832 case clang::BuiltinType::Short:
3833 case clang::BuiltinType::Int:
3834 case clang::BuiltinType::Long:
3835 case clang::BuiltinType::LongLong:
3836 case clang::BuiltinType::Int128:
3837 case clang::BuiltinType::Float:
3838 case clang::BuiltinType::Double:
3839 case clang::BuiltinType::LongDouble:
3840 builtin_type_flags |= eTypeIsScalar;
3841 if (builtin_type->isInteger())
3842 {
3843 builtin_type_flags |= eTypeIsInteger;
3844 if (builtin_type->isSignedInteger())
3845 builtin_type_flags |= eTypeIsSigned;
3846 }
3847 else if (builtin_type->isFloatingPoint())
3848 builtin_type_flags |= eTypeIsFloat;
3849 break;
3850 default:
3851 break;
3852 }
3853 return builtin_type_flags;
3854 }
3855
3856 case clang::Type::BlockPointer:
3857 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003858 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003859 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3860
3861 case clang::Type::Complex:
3862 {
3863 uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3864 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(qual_type->getCanonicalTypeInternal());
3865 if (complex_type)
3866 {
3867 clang::QualType complex_element_type (complex_type->getElementType());
3868 if (complex_element_type->isIntegerType())
3869 complex_type_flags |= eTypeIsFloat;
3870 else if (complex_element_type->isFloatingType())
3871 complex_type_flags |= eTypeIsInteger;
3872 }
3873 return complex_type_flags;
3874 }
3875 break;
3876
3877 case clang::Type::ConstantArray:
3878 case clang::Type::DependentSizedArray:
3879 case clang::Type::IncompleteArray:
3880 case clang::Type::VariableArray:
3881 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003882 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003883 return eTypeHasChildren | eTypeIsArray;
3884
3885 case clang::Type::DependentName: return 0;
3886 case clang::Type::DependentSizedExtVector: return eTypeHasChildren | eTypeIsVector;
3887 case clang::Type::DependentTemplateSpecialization: return eTypeIsTemplate;
3888 case clang::Type::Decltype: return 0;
3889
3890 case clang::Type::Enum:
3891 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003892 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003893 return eTypeIsEnumeration | eTypeHasValue;
Enrico Granata36f51e42015-12-18 22:41:25 +00003894
3895 case clang::Type::Auto:
3896 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003897 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003898 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003899 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003900 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00003901
3902 case clang::Type::FunctionProto: return eTypeIsFuncPrototype | eTypeHasValue;
3903 case clang::Type::FunctionNoProto: return eTypeIsFuncPrototype | eTypeHasValue;
3904 case clang::Type::InjectedClassName: return 0;
3905
3906 case clang::Type::LValueReference:
3907 case clang::Type::RValueReference:
3908 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003909 pointee_or_element_clang_type->SetCompilerType(getASTContext(), llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003910 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3911
3912 case clang::Type::MemberPointer: return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3913
3914 case clang::Type::ObjCObjectPointer:
3915 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003916 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003917 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
3918
3919 case clang::Type::ObjCObject: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3920 case clang::Type::ObjCInterface: return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3921
3922 case clang::Type::Pointer:
3923 if (pointee_or_element_clang_type)
Greg Clayton99558cc42015-08-24 23:46:31 +00003924 pointee_or_element_clang_type->SetCompilerType(getASTContext(), qual_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00003925 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3926
3927 case clang::Type::Record:
3928 if (qual_type->getAsCXXRecordDecl())
3929 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3930 else
3931 return eTypeHasChildren | eTypeIsStructUnion;
3932 break;
3933 case clang::Type::SubstTemplateTypeParm: return eTypeIsTemplate;
3934 case clang::Type::TemplateTypeParm: return eTypeIsTemplate;
3935 case clang::Type::TemplateSpecialization: return eTypeIsTemplate;
3936
3937 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00003938 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 +00003939 case clang::Type::TypeOfExpr: return 0;
3940 case clang::Type::TypeOf: return 0;
3941 case clang::Type::UnresolvedUsing: return 0;
3942
3943 case clang::Type::ExtVector:
3944 case clang::Type::Vector:
3945 {
3946 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3947 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(qual_type->getCanonicalTypeInternal());
3948 if (vector_type)
3949 {
3950 if (vector_type->isIntegerType())
3951 vector_type_flags |= eTypeIsFloat;
3952 else if (vector_type->isFloatingType())
3953 vector_type_flags |= eTypeIsInteger;
3954 }
3955 return vector_type_flags;
3956 }
3957 default: return 0;
3958 }
3959 return 0;
3960}
3961
3962
3963
3964lldb::LanguageType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00003965ClangASTContext::GetMinimumLanguage (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003966{
3967 if (!type)
3968 return lldb::eLanguageTypeC;
3969
3970 // If the type is a reference, then resolve it to what it refers to first:
3971 clang::QualType qual_type (GetCanonicalQualType(type).getNonReferenceType());
3972 if (qual_type->isAnyPointerType())
3973 {
3974 if (qual_type->isObjCObjectPointerType())
3975 return lldb::eLanguageTypeObjC;
3976
3977 clang::QualType pointee_type (qual_type->getPointeeType());
3978 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
3979 return lldb::eLanguageTypeC_plus_plus;
3980 if (pointee_type->isObjCObjectOrInterfaceType())
3981 return lldb::eLanguageTypeObjC;
3982 if (pointee_type->isObjCClassType())
3983 return lldb::eLanguageTypeObjC;
3984 if (pointee_type.getTypePtr() == getASTContext()->ObjCBuiltinIdTy.getTypePtr())
3985 return lldb::eLanguageTypeObjC;
3986 }
3987 else
3988 {
3989 if (qual_type->isObjCObjectOrInterfaceType())
3990 return lldb::eLanguageTypeObjC;
3991 if (qual_type->getAsCXXRecordDecl())
3992 return lldb::eLanguageTypeC_plus_plus;
3993 switch (qual_type->getTypeClass())
3994 {
3995 default:
3996 break;
3997 case clang::Type::Builtin:
3998 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
3999 {
4000 default:
4001 case clang::BuiltinType::Void:
4002 case clang::BuiltinType::Bool:
4003 case clang::BuiltinType::Char_U:
4004 case clang::BuiltinType::UChar:
4005 case clang::BuiltinType::WChar_U:
4006 case clang::BuiltinType::Char16:
4007 case clang::BuiltinType::Char32:
4008 case clang::BuiltinType::UShort:
4009 case clang::BuiltinType::UInt:
4010 case clang::BuiltinType::ULong:
4011 case clang::BuiltinType::ULongLong:
4012 case clang::BuiltinType::UInt128:
4013 case clang::BuiltinType::Char_S:
4014 case clang::BuiltinType::SChar:
4015 case clang::BuiltinType::WChar_S:
4016 case clang::BuiltinType::Short:
4017 case clang::BuiltinType::Int:
4018 case clang::BuiltinType::Long:
4019 case clang::BuiltinType::LongLong:
4020 case clang::BuiltinType::Int128:
4021 case clang::BuiltinType::Float:
4022 case clang::BuiltinType::Double:
4023 case clang::BuiltinType::LongDouble:
4024 break;
4025
4026 case clang::BuiltinType::NullPtr:
4027 return eLanguageTypeC_plus_plus;
4028
4029 case clang::BuiltinType::ObjCId:
4030 case clang::BuiltinType::ObjCClass:
4031 case clang::BuiltinType::ObjCSel:
4032 return eLanguageTypeObjC;
4033
4034 case clang::BuiltinType::Dependent:
4035 case clang::BuiltinType::Overload:
4036 case clang::BuiltinType::BoundMember:
4037 case clang::BuiltinType::UnknownAny:
4038 break;
4039 }
4040 break;
4041 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004042 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
Greg Claytond8d4a572015-08-11 21:38:15 +00004043 }
4044 }
4045 return lldb::eLanguageTypeC;
4046}
4047
4048lldb::TypeClass
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004049ClangASTContext::GetTypeClass (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004050{
4051 if (!type)
4052 return lldb::eTypeClassInvalid;
4053
4054 clang::QualType qual_type(GetQualType(type));
4055
4056 switch (qual_type->getTypeClass())
4057 {
4058 case clang::Type::UnaryTransform: break;
4059 case clang::Type::FunctionNoProto: return lldb::eTypeClassFunction;
4060 case clang::Type::FunctionProto: return lldb::eTypeClassFunction;
4061 case clang::Type::IncompleteArray: return lldb::eTypeClassArray;
4062 case clang::Type::VariableArray: return lldb::eTypeClassArray;
4063 case clang::Type::ConstantArray: return lldb::eTypeClassArray;
4064 case clang::Type::DependentSizedArray: return lldb::eTypeClassArray;
4065 case clang::Type::DependentSizedExtVector: return lldb::eTypeClassVector;
4066 case clang::Type::ExtVector: return lldb::eTypeClassVector;
4067 case clang::Type::Vector: return lldb::eTypeClassVector;
4068 case clang::Type::Builtin: return lldb::eTypeClassBuiltin;
4069 case clang::Type::ObjCObjectPointer: return lldb::eTypeClassObjCObjectPointer;
4070 case clang::Type::BlockPointer: return lldb::eTypeClassBlockPointer;
4071 case clang::Type::Pointer: return lldb::eTypeClassPointer;
4072 case clang::Type::LValueReference: return lldb::eTypeClassReference;
4073 case clang::Type::RValueReference: return lldb::eTypeClassReference;
4074 case clang::Type::MemberPointer: return lldb::eTypeClassMemberPointer;
4075 case clang::Type::Complex:
4076 if (qual_type->isComplexType())
4077 return lldb::eTypeClassComplexFloat;
4078 else
4079 return lldb::eTypeClassComplexInteger;
4080 case clang::Type::ObjCObject: return lldb::eTypeClassObjCObject;
4081 case clang::Type::ObjCInterface: return lldb::eTypeClassObjCInterface;
4082 case clang::Type::Record:
4083 {
4084 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4085 const clang::RecordDecl *record_decl = record_type->getDecl();
4086 if (record_decl->isUnion())
4087 return lldb::eTypeClassUnion;
4088 else if (record_decl->isStruct())
4089 return lldb::eTypeClassStruct;
4090 else
4091 return lldb::eTypeClassClass;
4092 }
4093 break;
4094 case clang::Type::Enum: return lldb::eTypeClassEnumeration;
4095 case clang::Type::Typedef: return lldb::eTypeClassTypedef;
4096 case clang::Type::UnresolvedUsing: break;
4097 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004098 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetTypeClass();
Enrico Granata36f51e42015-12-18 22:41:25 +00004099 case clang::Type::Auto:
4100 return CompilerType(getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00004101 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004102 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
Greg Claytond8d4a572015-08-11 21:38:15 +00004103
4104 case clang::Type::Attributed: break;
4105 case clang::Type::TemplateTypeParm: break;
4106 case clang::Type::SubstTemplateTypeParm: break;
4107 case clang::Type::SubstTemplateTypeParmPack:break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004108 case clang::Type::InjectedClassName: break;
4109 case clang::Type::DependentName: break;
4110 case clang::Type::DependentTemplateSpecialization: break;
4111 case clang::Type::PackExpansion: break;
4112
4113 case clang::Type::TypeOfExpr: break;
4114 case clang::Type::TypeOf: break;
4115 case clang::Type::Decltype: break;
4116 case clang::Type::TemplateSpecialization: break;
4117 case clang::Type::Atomic: break;
Pavel Labath484f0a32016-01-12 08:51:28 +00004118 case clang::Type::Pipe: break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004119
4120 // pointer type decayed from an array or function type.
4121 case clang::Type::Decayed: break;
4122 case clang::Type::Adjusted: break;
4123 }
4124 // We don't know hot to display this type...
4125 return lldb::eTypeClassOther;
4126
4127}
4128
4129unsigned
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004130ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004131{
4132 if (type)
4133 return GetQualType(type).getQualifiers().getCVRQualifiers();
4134 return 0;
4135}
4136
4137//----------------------------------------------------------------------
4138// Creating related types
4139//----------------------------------------------------------------------
4140
Greg Claytona1e5dc82015-08-11 22:53:00 +00004141CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004142ClangASTContext::GetArrayElementType (lldb::opaque_compiler_type_t type, uint64_t *stride)
Greg Claytond8d4a572015-08-11 21:38:15 +00004143{
4144 if (type)
4145 {
4146 clang::QualType qual_type(GetCanonicalQualType(type));
4147
4148 const clang::Type *array_eletype = qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4149
4150 if (!array_eletype)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004151 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004152
Greg Claytona1e5dc82015-08-11 22:53:00 +00004153 CompilerType element_type (getASTContext(), array_eletype->getCanonicalTypeUnqualified());
Greg Claytond8d4a572015-08-11 21:38:15 +00004154
4155 // TODO: the real stride will be >= this value.. find the real one!
4156 if (stride)
4157 *stride = element_type.GetByteSize(nullptr);
4158
4159 return element_type;
4160
4161 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004162 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004163}
4164
Greg Claytona1e5dc82015-08-11 22:53:00 +00004165CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004166ClangASTContext::GetCanonicalType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004167{
4168 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004169 return CompilerType (getASTContext(), GetCanonicalQualType(type));
4170 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004171}
4172
4173static clang::QualType
4174GetFullyUnqualifiedType_Impl (clang::ASTContext *ast, clang::QualType qual_type)
4175{
4176 if (qual_type->isPointerType())
4177 qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4178 else
4179 qual_type = qual_type.getUnqualifiedType();
4180 qual_type.removeLocalConst();
4181 qual_type.removeLocalRestrict();
4182 qual_type.removeLocalVolatile();
4183 return qual_type;
4184}
4185
Greg Claytona1e5dc82015-08-11 22:53:00 +00004186CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004187ClangASTContext::GetFullyUnqualifiedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004188{
4189 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004190 return CompilerType(getASTContext(), GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4191 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004192}
4193
4194
4195int
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004196ClangASTContext::GetFunctionArgumentCount (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004197{
4198 if (type)
4199 {
4200 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4201 if (func)
4202 return func->getNumParams();
4203 }
4204 return -1;
4205}
4206
Greg Claytona1e5dc82015-08-11 22:53:00 +00004207CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004208ClangASTContext::GetFunctionArgumentTypeAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004209{
4210 if (type)
4211 {
Greg Clayton8b8874e2015-12-17 00:58:41 +00004212 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004213 if (func)
4214 {
4215 const uint32_t num_args = func->getNumParams();
4216 if (idx < num_args)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004217 return CompilerType(getASTContext(), func->getParamType(idx));
Greg Claytond8d4a572015-08-11 21:38:15 +00004218 }
4219 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004220 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004221}
4222
Greg Claytona1e5dc82015-08-11 22:53:00 +00004223CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004224ClangASTContext::GetFunctionReturnType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004225{
4226 if (type)
4227 {
Greg Clayton8b8874e2015-12-17 00:58:41 +00004228 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004229 const clang::FunctionProtoType* func = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4230 if (func)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004231 return CompilerType(getASTContext(), func->getReturnType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004232 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004233 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004234}
4235
4236size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004237ClangASTContext::GetNumMemberFunctions (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004238{
4239 size_t num_functions = 0;
4240 if (type)
4241 {
4242 clang::QualType qual_type(GetCanonicalQualType(type));
4243 switch (qual_type->getTypeClass()) {
4244 case clang::Type::Record:
4245 if (GetCompleteQualType (getASTContext(), qual_type))
4246 {
4247 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4248 const clang::RecordDecl *record_decl = record_type->getDecl();
4249 assert(record_decl);
4250 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4251 if (cxx_record_decl)
4252 num_functions = std::distance(cxx_record_decl->method_begin(), cxx_record_decl->method_end());
4253 }
4254 break;
4255
4256 case clang::Type::ObjCObjectPointer:
4257 if (GetCompleteType(type))
4258 {
4259 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4260 if (objc_class_type)
4261 {
4262 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4263 if (class_interface_decl)
4264 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4265 }
4266 }
4267 break;
4268
4269 case clang::Type::ObjCObject:
4270 case clang::Type::ObjCInterface:
4271 if (GetCompleteType(type))
4272 {
4273 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4274 if (objc_class_type)
4275 {
4276 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4277 if (class_interface_decl)
4278 num_functions = std::distance(class_interface_decl->meth_begin(), class_interface_decl->meth_end());
4279 }
4280 }
4281 break;
4282
4283
4284 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004285 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumMemberFunctions();
Enrico Granata36f51e42015-12-18 22:41:25 +00004286
4287 case clang::Type::Auto:
4288 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004289
4290 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004291 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004292
4293 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004294 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumMemberFunctions();
Greg Claytond8d4a572015-08-11 21:38:15 +00004295
4296 default:
4297 break;
4298 }
4299 }
4300 return num_functions;
4301}
4302
4303TypeMemberFunctionImpl
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004304ClangASTContext::GetMemberFunctionAtIndex (lldb::opaque_compiler_type_t type, size_t idx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004305{
Greg Claytonfe689042015-11-10 17:47:04 +00004306 std::string name;
Greg Claytond8d4a572015-08-11 21:38:15 +00004307 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
Greg Claytonfe689042015-11-10 17:47:04 +00004308 CompilerType clang_type;
4309 CompilerDecl clang_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00004310 if (type)
4311 {
4312 clang::QualType qual_type(GetCanonicalQualType(type));
4313 switch (qual_type->getTypeClass()) {
4314 case clang::Type::Record:
4315 if (GetCompleteQualType (getASTContext(), qual_type))
4316 {
4317 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4318 const clang::RecordDecl *record_decl = record_type->getDecl();
4319 assert(record_decl);
4320 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4321 if (cxx_record_decl)
4322 {
4323 auto method_iter = cxx_record_decl->method_begin();
4324 auto method_end = cxx_record_decl->method_end();
4325 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4326 {
4327 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004328 clang::CXXMethodDecl *cxx_method_decl = method_iter->getCanonicalDecl();
4329 if (cxx_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004330 {
Greg Claytonfe689042015-11-10 17:47:04 +00004331 name = cxx_method_decl->getDeclName().getAsString();
4332 if (cxx_method_decl->isStatic())
Greg Claytond8d4a572015-08-11 21:38:15 +00004333 kind = lldb::eMemberFunctionKindStaticMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004334 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004335 kind = lldb::eMemberFunctionKindConstructor;
Greg Claytonfe689042015-11-10 17:47:04 +00004336 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
Greg Claytond8d4a572015-08-11 21:38:15 +00004337 kind = lldb::eMemberFunctionKindDestructor;
4338 else
4339 kind = lldb::eMemberFunctionKindInstanceMethod;
Greg Claytonfe689042015-11-10 17:47:04 +00004340 clang_type = CompilerType(this, cxx_method_decl->getType().getAsOpaquePtr());
4341 clang_decl = CompilerDecl(this, cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00004342 }
4343 }
4344 }
4345 }
4346 break;
4347
4348 case clang::Type::ObjCObjectPointer:
4349 if (GetCompleteType(type))
4350 {
4351 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
4352 if (objc_class_type)
4353 {
4354 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
4355 if (class_interface_decl)
4356 {
4357 auto method_iter = class_interface_decl->meth_begin();
4358 auto method_end = class_interface_decl->meth_end();
4359 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4360 {
4361 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004362 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4363 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004364 {
Greg Claytonfe689042015-11-10 17:47:04 +00004365 clang_decl = CompilerDecl(this, objc_method_decl);
4366 name = objc_method_decl->getSelector().getAsString();
4367 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004368 kind = lldb::eMemberFunctionKindStaticMethod;
4369 else
4370 kind = lldb::eMemberFunctionKindInstanceMethod;
4371 }
4372 }
4373 }
4374 }
4375 }
4376 break;
4377
4378 case clang::Type::ObjCObject:
4379 case clang::Type::ObjCInterface:
4380 if (GetCompleteType(type))
4381 {
4382 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4383 if (objc_class_type)
4384 {
4385 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
4386 if (class_interface_decl)
4387 {
4388 auto method_iter = class_interface_decl->meth_begin();
4389 auto method_end = class_interface_decl->meth_end();
4390 if (idx < static_cast<size_t>(std::distance(method_iter, method_end)))
4391 {
4392 std::advance(method_iter, idx);
Greg Claytonfe689042015-11-10 17:47:04 +00004393 clang::ObjCMethodDecl *objc_method_decl = method_iter->getCanonicalDecl();
4394 if (objc_method_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00004395 {
Greg Claytonfe689042015-11-10 17:47:04 +00004396 clang_decl = CompilerDecl(this, objc_method_decl);
4397 name = objc_method_decl->getSelector().getAsString();
4398 if (objc_method_decl->isClassMethod())
Greg Claytond8d4a572015-08-11 21:38:15 +00004399 kind = lldb::eMemberFunctionKindStaticMethod;
4400 else
4401 kind = lldb::eMemberFunctionKindInstanceMethod;
4402 }
4403 }
4404 }
4405 }
4406 }
4407 break;
4408
4409 case clang::Type::Typedef:
4410 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx);
Enrico Granata36f51e42015-12-18 22:41:25 +00004411
4412 case clang::Type::Auto:
4413 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004414
4415 case clang::Type::Elaborated:
4416 return GetMemberFunctionAtIndex(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx);
4417
4418 case clang::Type::Paren:
4419 return GetMemberFunctionAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx);
4420
4421 default:
4422 break;
4423 }
4424 }
4425
4426 if (kind == eMemberFunctionKindUnknown)
4427 return TypeMemberFunctionImpl();
Greg Claytonfe689042015-11-10 17:47:04 +00004428 else
4429 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Claytond8d4a572015-08-11 21:38:15 +00004430}
4431
Greg Claytona1e5dc82015-08-11 22:53:00 +00004432CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004433ClangASTContext::GetNonReferenceType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004434{
4435 if (type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004436 return CompilerType(getASTContext(), GetQualType(type).getNonReferenceType());
4437 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004438}
4439
Greg Claytona1e5dc82015-08-11 22:53:00 +00004440CompilerType
4441ClangASTContext::CreateTypedefType (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00004442 const char *typedef_name,
Greg Clayton99558cc42015-08-24 23:46:31 +00004443 const CompilerDeclContext &compiler_decl_ctx)
Greg Claytond8d4a572015-08-11 21:38:15 +00004444{
4445 if (type && typedef_name && typedef_name[0])
4446 {
Greg Claytonf73034f2015-09-08 18:15:05 +00004447 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00004448 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004449 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004450 clang::ASTContext* clang_ast = ast->getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00004451 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Clayton99558cc42015-08-24 23:46:31 +00004452
4453 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
Greg Claytond8d4a572015-08-11 21:38:15 +00004454 if (decl_ctx == nullptr)
4455 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
Greg Clayton99558cc42015-08-24 23:46:31 +00004456
Greg Claytond8d4a572015-08-11 21:38:15 +00004457 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4458 decl_ctx,
4459 clang::SourceLocation(),
4460 clang::SourceLocation(),
4461 &clang_ast->Idents.get(typedef_name),
4462 clang_ast->getTrivialTypeSourceInfo(qual_type));
4463
4464 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4465
4466 // Get a uniqued clang::QualType for the typedef decl type
Greg Claytona1e5dc82015-08-11 22:53:00 +00004467 return CompilerType (clang_ast, clang_ast->getTypedefType (decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00004468 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004469 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004470
4471}
4472
Greg Claytona1e5dc82015-08-11 22:53:00 +00004473CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004474ClangASTContext::GetPointeeType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004475{
4476 if (type)
4477 {
4478 clang::QualType qual_type(GetQualType(type));
Greg Claytona1e5dc82015-08-11 22:53:00 +00004479 return CompilerType (getASTContext(), qual_type.getTypePtr()->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004480 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004481 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004482}
4483
Greg Claytona1e5dc82015-08-11 22:53:00 +00004484CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004485ClangASTContext::GetPointerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004486{
4487 if (type)
4488 {
4489 clang::QualType qual_type (GetQualType(type));
4490
4491 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4492 switch (type_class)
4493 {
4494 case clang::Type::ObjCObject:
4495 case clang::Type::ObjCInterface:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004496 return CompilerType(getASTContext(), getASTContext()->getObjCObjectPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004497
4498 default:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004499 return CompilerType(getASTContext(), getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004500 }
4501 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004502 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004503}
4504
Greg Clayton56939cb2015-09-17 22:23:34 +00004505
4506CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004507ClangASTContext::GetLValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004508{
4509 if (type)
4510 return CompilerType(this, getASTContext()->getLValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4511 else
4512 return CompilerType();
4513}
4514
4515CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004516ClangASTContext::GetRValueReferenceType (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004517{
4518 if (type)
4519 return CompilerType(this, getASTContext()->getRValueReferenceType(GetQualType(type)).getAsOpaquePtr());
4520 else
4521 return CompilerType();
4522}
4523
4524CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004525ClangASTContext::AddConstModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004526{
4527 if (type)
4528 {
4529 clang::QualType result(GetQualType(type));
4530 result.addConst();
4531 return CompilerType (this, result.getAsOpaquePtr());
4532 }
4533 return CompilerType();
4534}
4535
4536CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004537ClangASTContext::AddVolatileModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004538{
4539 if (type)
4540 {
4541 clang::QualType result(GetQualType(type));
4542 result.addVolatile();
4543 return CompilerType (this, result.getAsOpaquePtr());
4544 }
4545 return CompilerType();
4546
4547}
4548
4549CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004550ClangASTContext::AddRestrictModifier (lldb::opaque_compiler_type_t type)
Greg Clayton56939cb2015-09-17 22:23:34 +00004551{
4552 if (type)
4553 {
4554 clang::QualType result(GetQualType(type));
4555 result.addRestrict();
4556 return CompilerType (this, result.getAsOpaquePtr());
4557 }
4558 return CompilerType();
4559
4560}
4561
4562CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004563ClangASTContext::CreateTypedef (lldb::opaque_compiler_type_t type, const char *typedef_name, const CompilerDeclContext &compiler_decl_ctx)
Greg Clayton56939cb2015-09-17 22:23:34 +00004564{
4565 if (type)
4566 {
4567 clang::ASTContext* clang_ast = getASTContext();
4568 clang::QualType qual_type (GetQualType(type));
4569
4570 clang::DeclContext *decl_ctx = ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4571 if (decl_ctx == nullptr)
4572 decl_ctx = getASTContext()->getTranslationUnitDecl();
4573
4574 clang::TypedefDecl *decl = clang::TypedefDecl::Create (*clang_ast,
4575 decl_ctx,
4576 clang::SourceLocation(),
4577 clang::SourceLocation(),
4578 &clang_ast->Idents.get(typedef_name),
4579 clang_ast->getTrivialTypeSourceInfo(qual_type));
4580
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004581 clang::TagDecl *tdecl = nullptr;
4582 if (!qual_type.isNull())
4583 {
4584 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4585 tdecl = rt->getDecl();
4586 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4587 tdecl = et->getDecl();
4588 }
4589
4590 // Check whether this declaration is an anonymous struct, union, or enum, hidden behind a typedef. If so, we
4591 // try to check whether we have a typedef tag to attach to the original record declaration
4592 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4593 tdecl->setTypedefNameForAnonDecl(decl);
4594
Greg Clayton56939cb2015-09-17 22:23:34 +00004595 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4596
4597 // Get a uniqued clang::QualType for the typedef decl type
4598 return CompilerType (this, clang_ast->getTypedefType (decl).getAsOpaquePtr());
4599
4600 }
4601 return CompilerType();
4602
4603}
4604
Greg Claytona1e5dc82015-08-11 22:53:00 +00004605CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004606ClangASTContext::GetTypedefedType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004607{
4608 if (type)
4609 {
4610 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4611 if (typedef_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004612 return CompilerType (getASTContext(), typedef_type->getDecl()->getUnderlyingType());
Greg Claytond8d4a572015-08-11 21:38:15 +00004613 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00004614 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004615}
4616
Greg Claytond8d4a572015-08-11 21:38:15 +00004617
4618//----------------------------------------------------------------------
4619// Create related types using the current type's AST
4620//----------------------------------------------------------------------
4621
Greg Claytona1e5dc82015-08-11 22:53:00 +00004622CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +00004623ClangASTContext::GetBasicTypeFromAST (lldb::BasicType basic_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004624{
Greg Clayton99558cc42015-08-24 23:46:31 +00004625 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004626}
4627//----------------------------------------------------------------------
4628// Exploring the type
4629//----------------------------------------------------------------------
4630
4631uint64_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004632ClangASTContext::GetBitSize (lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
Greg Claytond8d4a572015-08-11 21:38:15 +00004633{
4634 if (GetCompleteType (type))
4635 {
4636 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Clayton6e5c1fe2016-03-29 17:36:38 +00004637 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4638 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00004639 {
Greg Clayton6e5c1fe2016-03-29 17:36:38 +00004640 case clang::Type::Record:
4641 if (GetCompleteType(type))
4642 return getASTContext()->getTypeSize(qual_type);
4643 else
4644 return 0;
4645 break;
4646
Greg Claytond8d4a572015-08-11 21:38:15 +00004647 case clang::Type::ObjCInterface:
4648 case clang::Type::ObjCObject:
4649 {
4650 ExecutionContext exe_ctx (exe_scope);
4651 Process *process = exe_ctx.GetProcessPtr();
4652 if (process)
4653 {
4654 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4655 if (objc_runtime)
4656 {
4657 uint64_t bit_size = 0;
Greg Claytona1e5dc82015-08-11 22:53:00 +00004658 if (objc_runtime->GetTypeBitSize(CompilerType(getASTContext(), qual_type), bit_size))
Greg Claytond8d4a572015-08-11 21:38:15 +00004659 return bit_size;
4660 }
4661 }
4662 else
4663 {
4664 static bool g_printed = false;
4665 if (!g_printed)
4666 {
4667 StreamString s;
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00004668 DumpTypeDescription(type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00004669
4670 llvm::outs() << "warning: trying to determine the size of type ";
4671 llvm::outs() << s.GetString() << "\n";
4672 llvm::outs() << "without a valid ExecutionContext. this is not reliable. please file a bug against LLDB.\n";
4673 llvm::outs() << "backtrace:\n";
4674 llvm::sys::PrintStackTrace(llvm::outs());
4675 llvm::outs() << "\n";
4676 g_printed = true;
4677 }
4678 }
4679 }
Jason Molenda62e06812016-02-16 04:14:33 +00004680 LLVM_FALLTHROUGH;
Greg Claytond8d4a572015-08-11 21:38:15 +00004681 default:
4682 const uint32_t bit_size = getASTContext()->getTypeSize (qual_type);
4683 if (bit_size == 0)
4684 {
4685 if (qual_type->isIncompleteArrayType())
4686 return getASTContext()->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
4687 }
4688 if (qual_type->isObjCObjectOrInterfaceType())
4689 return bit_size + getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy);
4690 return bit_size;
4691 }
4692 }
4693 return 0;
4694}
4695
4696size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004697ClangASTContext::GetTypeBitAlign (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004698{
4699 if (GetCompleteType(type))
4700 return getASTContext()->getTypeAlign(GetQualType(type));
4701 return 0;
4702}
4703
4704
4705lldb::Encoding
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004706ClangASTContext::GetEncoding (lldb::opaque_compiler_type_t type, uint64_t &count)
Greg Claytond8d4a572015-08-11 21:38:15 +00004707{
4708 if (!type)
4709 return lldb::eEncodingInvalid;
4710
4711 count = 1;
4712 clang::QualType qual_type(GetCanonicalQualType(type));
4713
4714 switch (qual_type->getTypeClass())
4715 {
4716 case clang::Type::UnaryTransform:
4717 break;
4718
4719 case clang::Type::FunctionNoProto:
4720 case clang::Type::FunctionProto:
4721 break;
4722
4723 case clang::Type::IncompleteArray:
4724 case clang::Type::VariableArray:
4725 break;
4726
4727 case clang::Type::ConstantArray:
4728 break;
4729
4730 case clang::Type::ExtVector:
4731 case clang::Type::Vector:
4732 // TODO: Set this to more than one???
4733 break;
4734
4735 case clang::Type::Builtin:
4736 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4737 {
Greg Claytond8d4a572015-08-11 21:38:15 +00004738 case clang::BuiltinType::Void:
4739 break;
4740
4741 case clang::BuiltinType::Bool:
4742 case clang::BuiltinType::Char_S:
4743 case clang::BuiltinType::SChar:
4744 case clang::BuiltinType::WChar_S:
4745 case clang::BuiltinType::Char16:
4746 case clang::BuiltinType::Char32:
4747 case clang::BuiltinType::Short:
4748 case clang::BuiltinType::Int:
4749 case clang::BuiltinType::Long:
4750 case clang::BuiltinType::LongLong:
4751 case clang::BuiltinType::Int128: return lldb::eEncodingSint;
4752
4753 case clang::BuiltinType::Char_U:
4754 case clang::BuiltinType::UChar:
4755 case clang::BuiltinType::WChar_U:
4756 case clang::BuiltinType::UShort:
4757 case clang::BuiltinType::UInt:
4758 case clang::BuiltinType::ULong:
4759 case clang::BuiltinType::ULongLong:
4760 case clang::BuiltinType::UInt128: return lldb::eEncodingUint;
4761
Greg Claytondee40e72015-11-03 23:23:22 +00004762 case clang::BuiltinType::Half:
Greg Claytond8d4a572015-08-11 21:38:15 +00004763 case clang::BuiltinType::Float:
4764 case clang::BuiltinType::Double:
4765 case clang::BuiltinType::LongDouble: return lldb::eEncodingIEEE754;
4766
4767 case clang::BuiltinType::ObjCClass:
4768 case clang::BuiltinType::ObjCId:
4769 case clang::BuiltinType::ObjCSel: return lldb::eEncodingUint;
4770
4771 case clang::BuiltinType::NullPtr: return lldb::eEncodingUint;
4772
4773 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4774 case clang::BuiltinType::Kind::BoundMember:
4775 case clang::BuiltinType::Kind::BuiltinFn:
4776 case clang::BuiltinType::Kind::Dependent:
Ed Mastec6dd6512015-09-23 18:32:34 +00004777 case clang::BuiltinType::Kind::OCLClkEvent:
Greg Claytond8d4a572015-08-11 21:38:15 +00004778 case clang::BuiltinType::Kind::OCLEvent:
4779 case clang::BuiltinType::Kind::OCLImage1d:
4780 case clang::BuiltinType::Kind::OCLImage1dArray:
4781 case clang::BuiltinType::Kind::OCLImage1dBuffer:
4782 case clang::BuiltinType::Kind::OCLImage2d:
4783 case clang::BuiltinType::Kind::OCLImage2dArray:
Ed Mastec6dd6512015-09-23 18:32:34 +00004784 case clang::BuiltinType::Kind::OCLImage2dArrayDepth:
4785 case clang::BuiltinType::Kind::OCLImage2dArrayMSAA:
4786 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepth:
4787 case clang::BuiltinType::Kind::OCLImage2dDepth:
4788 case clang::BuiltinType::Kind::OCLImage2dMSAA:
4789 case clang::BuiltinType::Kind::OCLImage2dMSAADepth:
Greg Claytond8d4a572015-08-11 21:38:15 +00004790 case clang::BuiltinType::Kind::OCLImage3d:
Ed Mastec6dd6512015-09-23 18:32:34 +00004791 case clang::BuiltinType::Kind::OCLQueue:
4792 case clang::BuiltinType::Kind::OCLNDRange:
4793 case clang::BuiltinType::Kind::OCLReserveID:
Greg Claytond8d4a572015-08-11 21:38:15 +00004794 case clang::BuiltinType::Kind::OCLSampler:
Ed Mastec6dd6512015-09-23 18:32:34 +00004795 case clang::BuiltinType::Kind::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00004796 case clang::BuiltinType::Kind::Overload:
4797 case clang::BuiltinType::Kind::PseudoObject:
4798 case clang::BuiltinType::Kind::UnknownAny:
4799 break;
4800 }
4801 break;
4802 // All pointer types are represented as unsigned integer encodings.
4803 // We may nee to add a eEncodingPointer if we ever need to know the
4804 // difference
4805 case clang::Type::ObjCObjectPointer:
4806 case clang::Type::BlockPointer:
4807 case clang::Type::Pointer:
4808 case clang::Type::LValueReference:
4809 case clang::Type::RValueReference:
4810 case clang::Type::MemberPointer: return lldb::eEncodingUint;
4811 case clang::Type::Complex:
4812 {
4813 lldb::Encoding encoding = lldb::eEncodingIEEE754;
4814 if (qual_type->isComplexType())
4815 encoding = lldb::eEncodingIEEE754;
4816 else
4817 {
4818 const clang::ComplexType *complex_type = qual_type->getAsComplexIntegerType();
4819 if (complex_type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00004820 encoding = CompilerType(getASTContext(), complex_type->getElementType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004821 else
4822 encoding = lldb::eEncodingSint;
4823 }
4824 count = 2;
4825 return encoding;
4826 }
4827
4828 case clang::Type::ObjCInterface: break;
4829 case clang::Type::Record: break;
4830 case clang::Type::Enum: return lldb::eEncodingSint;
4831 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004832 return CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
Enrico Granata36f51e42015-12-18 22:41:25 +00004833
4834 case clang::Type::Auto:
4835 return CompilerType(getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004836
4837 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004838 return CompilerType(getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004839
4840 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004841 return CompilerType(getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetEncoding(count);
Greg Claytond8d4a572015-08-11 21:38:15 +00004842
4843 case clang::Type::DependentSizedArray:
4844 case clang::Type::DependentSizedExtVector:
4845 case clang::Type::UnresolvedUsing:
4846 case clang::Type::Attributed:
4847 case clang::Type::TemplateTypeParm:
4848 case clang::Type::SubstTemplateTypeParm:
4849 case clang::Type::SubstTemplateTypeParmPack:
Greg Claytond8d4a572015-08-11 21:38:15 +00004850 case clang::Type::InjectedClassName:
4851 case clang::Type::DependentName:
4852 case clang::Type::DependentTemplateSpecialization:
4853 case clang::Type::PackExpansion:
4854 case clang::Type::ObjCObject:
4855
4856 case clang::Type::TypeOfExpr:
4857 case clang::Type::TypeOf:
4858 case clang::Type::Decltype:
4859 case clang::Type::TemplateSpecialization:
4860 case clang::Type::Atomic:
4861 case clang::Type::Adjusted:
Pavel Labath484f0a32016-01-12 08:51:28 +00004862 case clang::Type::Pipe:
Greg Claytond8d4a572015-08-11 21:38:15 +00004863 break;
4864
4865 // pointer type decayed from an array or function type.
4866 case clang::Type::Decayed:
4867 break;
4868 }
4869 count = 0;
4870 return lldb::eEncodingInvalid;
4871}
4872
4873lldb::Format
Bruce Mitchener48ea9002015-09-23 00:18:24 +00004874ClangASTContext::GetFormat (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004875{
4876 if (!type)
4877 return lldb::eFormatDefault;
4878
4879 clang::QualType qual_type(GetCanonicalQualType(type));
4880
4881 switch (qual_type->getTypeClass())
4882 {
4883 case clang::Type::UnaryTransform:
4884 break;
4885
4886 case clang::Type::FunctionNoProto:
4887 case clang::Type::FunctionProto:
4888 break;
4889
4890 case clang::Type::IncompleteArray:
4891 case clang::Type::VariableArray:
4892 break;
4893
4894 case clang::Type::ConstantArray:
4895 return lldb::eFormatVoid; // no value
4896
4897 case clang::Type::ExtVector:
4898 case clang::Type::Vector:
4899 break;
4900
4901 case clang::Type::Builtin:
4902 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
4903 {
4904 //default: assert(0 && "Unknown builtin type!");
4905 case clang::BuiltinType::UnknownAny:
4906 case clang::BuiltinType::Void:
4907 case clang::BuiltinType::BoundMember:
4908 break;
4909
4910 case clang::BuiltinType::Bool: return lldb::eFormatBoolean;
4911 case clang::BuiltinType::Char_S:
4912 case clang::BuiltinType::SChar:
4913 case clang::BuiltinType::WChar_S:
4914 case clang::BuiltinType::Char_U:
4915 case clang::BuiltinType::UChar:
4916 case clang::BuiltinType::WChar_U: return lldb::eFormatChar;
4917 case clang::BuiltinType::Char16: return lldb::eFormatUnicode16;
4918 case clang::BuiltinType::Char32: return lldb::eFormatUnicode32;
4919 case clang::BuiltinType::UShort: return lldb::eFormatUnsigned;
4920 case clang::BuiltinType::Short: return lldb::eFormatDecimal;
4921 case clang::BuiltinType::UInt: return lldb::eFormatUnsigned;
4922 case clang::BuiltinType::Int: return lldb::eFormatDecimal;
4923 case clang::BuiltinType::ULong: return lldb::eFormatUnsigned;
4924 case clang::BuiltinType::Long: return lldb::eFormatDecimal;
4925 case clang::BuiltinType::ULongLong: return lldb::eFormatUnsigned;
4926 case clang::BuiltinType::LongLong: return lldb::eFormatDecimal;
4927 case clang::BuiltinType::UInt128: return lldb::eFormatUnsigned;
4928 case clang::BuiltinType::Int128: return lldb::eFormatDecimal;
Greg Claytondee40e72015-11-03 23:23:22 +00004929 case clang::BuiltinType::Half:
4930 case clang::BuiltinType::Float:
4931 case clang::BuiltinType::Double:
Greg Claytond8d4a572015-08-11 21:38:15 +00004932 case clang::BuiltinType::LongDouble: return lldb::eFormatFloat;
Zachary Turnerdd07e002015-09-16 18:08:45 +00004933 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00004934 return lldb::eFormatHex;
4935 }
4936 break;
4937 case clang::Type::ObjCObjectPointer: return lldb::eFormatHex;
4938 case clang::Type::BlockPointer: return lldb::eFormatHex;
4939 case clang::Type::Pointer: return lldb::eFormatHex;
4940 case clang::Type::LValueReference:
4941 case clang::Type::RValueReference: return lldb::eFormatHex;
4942 case clang::Type::MemberPointer: break;
4943 case clang::Type::Complex:
4944 {
4945 if (qual_type->isComplexType())
4946 return lldb::eFormatComplex;
4947 else
4948 return lldb::eFormatComplexInteger;
4949 }
4950 case clang::Type::ObjCInterface: break;
4951 case clang::Type::Record: break;
4952 case clang::Type::Enum: return lldb::eFormatEnum;
4953 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004954 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004955 case clang::Type::Auto:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004956 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004957 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004958 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004959 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00004960 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetFormat();
Greg Claytond8d4a572015-08-11 21:38:15 +00004961 case clang::Type::DependentSizedArray:
4962 case clang::Type::DependentSizedExtVector:
4963 case clang::Type::UnresolvedUsing:
4964 case clang::Type::Attributed:
4965 case clang::Type::TemplateTypeParm:
4966 case clang::Type::SubstTemplateTypeParm:
4967 case clang::Type::SubstTemplateTypeParmPack:
4968 case clang::Type::InjectedClassName:
4969 case clang::Type::DependentName:
4970 case clang::Type::DependentTemplateSpecialization:
4971 case clang::Type::PackExpansion:
4972 case clang::Type::ObjCObject:
4973
4974 case clang::Type::TypeOfExpr:
4975 case clang::Type::TypeOf:
4976 case clang::Type::Decltype:
4977 case clang::Type::TemplateSpecialization:
4978 case clang::Type::Atomic:
4979 case clang::Type::Adjusted:
Pavel Labath484f0a32016-01-12 08:51:28 +00004980 case clang::Type::Pipe:
Greg Claytond8d4a572015-08-11 21:38:15 +00004981 break;
4982
4983 // pointer type decayed from an array or function type.
4984 case clang::Type::Decayed:
4985 break;
4986 }
4987 // We don't know hot to display this type...
4988 return lldb::eFormatBytes;
4989}
4990
4991static bool
4992ObjCDeclHasIVars (clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
4993{
4994 while (class_interface_decl)
4995 {
4996 if (class_interface_decl->ivar_size() > 0)
4997 return true;
4998
4999 if (check_superclass)
5000 class_interface_decl = class_interface_decl->getSuperClass();
5001 else
5002 break;
5003 }
5004 return false;
5005}
5006
5007uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005008ClangASTContext::GetNumChildren (lldb::opaque_compiler_type_t type, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00005009{
5010 if (!type)
5011 return 0;
5012
5013 uint32_t num_children = 0;
5014 clang::QualType qual_type(GetQualType(type));
5015 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5016 switch (type_class)
5017 {
5018 case clang::Type::Builtin:
5019 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5020 {
5021 case clang::BuiltinType::ObjCId: // child is Class
5022 case clang::BuiltinType::ObjCClass: // child is Class
5023 num_children = 1;
5024 break;
5025
5026 default:
5027 break;
5028 }
5029 break;
5030
5031 case clang::Type::Complex: return 0;
5032
5033 case clang::Type::Record:
5034 if (GetCompleteQualType (getASTContext(), qual_type))
5035 {
5036 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5037 const clang::RecordDecl *record_decl = record_type->getDecl();
5038 assert(record_decl);
5039 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5040 if (cxx_record_decl)
5041 {
5042 if (omit_empty_base_classes)
5043 {
5044 // Check each base classes to see if it or any of its
5045 // base classes contain any fields. This can help
5046 // limit the noise in variable views by not having to
5047 // show base classes that contain no members.
5048 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5049 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5050 base_class != base_class_end;
5051 ++base_class)
5052 {
5053 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5054
5055 // Skip empty base classes
5056 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5057 continue;
5058
5059 num_children++;
5060 }
5061 }
5062 else
5063 {
5064 // Include all base classes
5065 num_children += cxx_record_decl->getNumBases();
5066 }
5067
5068 }
5069 clang::RecordDecl::field_iterator field, field_end;
5070 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5071 ++num_children;
5072 }
5073 break;
5074
5075 case clang::Type::ObjCObject:
5076 case clang::Type::ObjCInterface:
5077 if (GetCompleteQualType (getASTContext(), qual_type))
5078 {
5079 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5080 assert (objc_class_type);
5081 if (objc_class_type)
5082 {
5083 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5084
5085 if (class_interface_decl)
5086 {
5087
5088 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5089 if (superclass_interface_decl)
5090 {
5091 if (omit_empty_base_classes)
5092 {
5093 if (ObjCDeclHasIVars (superclass_interface_decl, true))
5094 ++num_children;
5095 }
5096 else
5097 ++num_children;
5098 }
5099
5100 num_children += class_interface_decl->ivar_size();
5101 }
5102 }
5103 }
5104 break;
5105
5106 case clang::Type::ObjCObjectPointer:
5107 {
5108 const clang::ObjCObjectPointerType *pointer_type = llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5109 clang::QualType pointee_type = pointer_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005110 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005111 // If this type points to a simple type, then it has 1 child
5112 if (num_pointee_children == 0)
5113 num_children = 1;
5114 else
5115 num_children = num_pointee_children;
5116 }
5117 break;
5118
5119 case clang::Type::Vector:
5120 case clang::Type::ExtVector:
5121 num_children = llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5122 break;
5123
5124 case clang::Type::ConstantArray:
5125 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
5126 break;
5127
5128 case clang::Type::Pointer:
5129 {
5130 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5131 clang::QualType pointee_type (pointer_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00005132 uint32_t num_pointee_children = CompilerType (getASTContext(),pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005133 if (num_pointee_children == 0)
5134 {
5135 // We have a pointer to a pointee type that claims it has no children.
5136 // We will want to look at
5137 num_children = GetNumPointeeChildren (pointee_type);
5138 }
5139 else
5140 num_children = num_pointee_children;
5141 }
5142 break;
5143
5144 case clang::Type::LValueReference:
5145 case clang::Type::RValueReference:
5146 {
5147 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5148 clang::QualType pointee_type = reference_type->getPointeeType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005149 uint32_t num_pointee_children = CompilerType (getASTContext(), pointee_type).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005150 // If this type points to a simple type, then it has 1 child
5151 if (num_pointee_children == 0)
5152 num_children = 1;
5153 else
5154 num_children = num_pointee_children;
5155 }
5156 break;
5157
5158
5159 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005160 num_children = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005161 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005162
5163 case clang::Type::Auto:
5164 num_children = CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumChildren (omit_empty_base_classes);
5165 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00005166
5167 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005168 num_children = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005169 break;
5170
5171 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005172 num_children = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00005173 break;
5174 default:
5175 break;
5176 }
5177 return num_children;
5178}
5179
Greg Clayton56939cb2015-09-17 22:23:34 +00005180CompilerType
5181ClangASTContext::GetBuiltinTypeByName (const ConstString &name)
5182{
5183 return GetBasicType (GetBasicTypeEnumeration (name));
5184}
5185
Greg Claytond8d4a572015-08-11 21:38:15 +00005186lldb::BasicType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005187ClangASTContext::GetBasicTypeEnumeration (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005188{
5189 if (type)
5190 {
5191 clang::QualType qual_type(GetQualType(type));
5192 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5193 if (type_class == clang::Type::Builtin)
5194 {
5195 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5196 {
5197 case clang::BuiltinType::Void: return eBasicTypeVoid;
5198 case clang::BuiltinType::Bool: return eBasicTypeBool;
5199 case clang::BuiltinType::Char_S: return eBasicTypeSignedChar;
5200 case clang::BuiltinType::Char_U: return eBasicTypeUnsignedChar;
5201 case clang::BuiltinType::Char16: return eBasicTypeChar16;
5202 case clang::BuiltinType::Char32: return eBasicTypeChar32;
5203 case clang::BuiltinType::UChar: return eBasicTypeUnsignedChar;
5204 case clang::BuiltinType::SChar: return eBasicTypeSignedChar;
5205 case clang::BuiltinType::WChar_S: return eBasicTypeSignedWChar;
5206 case clang::BuiltinType::WChar_U: return eBasicTypeUnsignedWChar;
5207 case clang::BuiltinType::Short: return eBasicTypeShort;
5208 case clang::BuiltinType::UShort: return eBasicTypeUnsignedShort;
5209 case clang::BuiltinType::Int: return eBasicTypeInt;
5210 case clang::BuiltinType::UInt: return eBasicTypeUnsignedInt;
5211 case clang::BuiltinType::Long: return eBasicTypeLong;
5212 case clang::BuiltinType::ULong: return eBasicTypeUnsignedLong;
5213 case clang::BuiltinType::LongLong: return eBasicTypeLongLong;
5214 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
5215 case clang::BuiltinType::Int128: return eBasicTypeInt128;
5216 case clang::BuiltinType::UInt128: return eBasicTypeUnsignedInt128;
5217
5218 case clang::BuiltinType::Half: return eBasicTypeHalf;
5219 case clang::BuiltinType::Float: return eBasicTypeFloat;
5220 case clang::BuiltinType::Double: return eBasicTypeDouble;
5221 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
5222
5223 case clang::BuiltinType::NullPtr: return eBasicTypeNullPtr;
5224 case clang::BuiltinType::ObjCId: return eBasicTypeObjCID;
5225 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
5226 case clang::BuiltinType::ObjCSel: return eBasicTypeObjCSel;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005227 default:
Greg Claytond8d4a572015-08-11 21:38:15 +00005228 return eBasicTypeOther;
5229 }
5230 }
5231 }
5232 return eBasicTypeInvalid;
5233}
5234
Greg Clayton99558cc42015-08-24 23:46:31 +00005235void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005236ClangASTContext::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 +00005237{
5238 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5239 if (enum_type)
5240 {
5241 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5242 if (enum_decl)
5243 {
5244 CompilerType integer_type(this, enum_decl->getIntegerType().getAsOpaquePtr());
5245
5246 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5247 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5248 {
5249 ConstString name(enum_pos->getNameAsString().c_str());
5250 if (!callback (integer_type, name, enum_pos->getInitVal()))
5251 break;
5252 }
5253 }
5254 }
5255}
5256
Greg Claytond8d4a572015-08-11 21:38:15 +00005257
5258#pragma mark Aggregate Types
5259
5260uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005261ClangASTContext::GetNumFields (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00005262{
5263 if (!type)
5264 return 0;
5265
5266 uint32_t count = 0;
5267 clang::QualType qual_type(GetCanonicalQualType(type));
5268 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5269 switch (type_class)
5270 {
5271 case clang::Type::Record:
5272 if (GetCompleteType(type))
5273 {
5274 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5275 if (record_type)
5276 {
5277 clang::RecordDecl *record_decl = record_type->getDecl();
5278 if (record_decl)
5279 {
5280 uint32_t field_idx = 0;
5281 clang::RecordDecl::field_iterator field, field_end;
5282 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
5283 ++field_idx;
5284 count = field_idx;
5285 }
5286 }
5287 }
5288 break;
5289
5290 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005291 count = CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005292 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00005293
5294 case clang::Type::Auto:
5295 count = CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetNumFields();
5296 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00005297
5298 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005299 count = CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005300 break;
5301
5302 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005303 count = CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
Greg Claytond8d4a572015-08-11 21:38:15 +00005304 break;
5305
5306 case clang::Type::ObjCObjectPointer:
5307 if (GetCompleteType(type))
5308 {
5309 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5310 if (objc_class_type)
5311 {
5312 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
5313
5314 if (class_interface_decl)
5315 count = class_interface_decl->ivar_size();
5316 }
5317 }
5318 break;
5319
5320 case clang::Type::ObjCObject:
5321 case clang::Type::ObjCInterface:
5322 if (GetCompleteType(type))
5323 {
5324 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5325 if (objc_class_type)
5326 {
5327 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5328
5329 if (class_interface_decl)
5330 count = class_interface_decl->ivar_size();
5331 }
5332 }
5333 break;
5334
5335 default:
5336 break;
5337 }
5338 return count;
5339}
5340
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005341static lldb::opaque_compiler_type_t
Greg Claytond8d4a572015-08-11 21:38:15 +00005342GetObjCFieldAtIndex (clang::ASTContext *ast,
5343 clang::ObjCInterfaceDecl *class_interface_decl,
5344 size_t idx,
5345 std::string& name,
5346 uint64_t *bit_offset_ptr,
5347 uint32_t *bitfield_bit_size_ptr,
5348 bool *is_bitfield_ptr)
5349{
5350 if (class_interface_decl)
5351 {
5352 if (idx < (class_interface_decl->ivar_size()))
5353 {
5354 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
5355 uint32_t ivar_idx = 0;
5356
5357 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
5358 {
5359 if (ivar_idx == idx)
5360 {
5361 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
5362
5363 clang::QualType ivar_qual_type(ivar_decl->getType());
5364
5365 name.assign(ivar_decl->getNameAsString());
5366
5367 if (bit_offset_ptr)
5368 {
5369 const clang::ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
5370 *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
5371 }
5372
5373 const bool is_bitfield = ivar_pos->isBitField();
5374
5375 if (bitfield_bit_size_ptr)
5376 {
5377 *bitfield_bit_size_ptr = 0;
5378
5379 if (is_bitfield && ast)
5380 {
5381 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5382 llvm::APSInt bitfield_apsint;
5383 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
5384 {
5385 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5386 }
5387 }
5388 }
5389 if (is_bitfield_ptr)
5390 *is_bitfield_ptr = is_bitfield;
5391
5392 return ivar_qual_type.getAsOpaquePtr();
5393 }
5394 }
5395 }
5396 }
5397 return nullptr;
5398}
5399
Greg Claytona1e5dc82015-08-11 22:53:00 +00005400CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005401ClangASTContext::GetFieldAtIndex (lldb::opaque_compiler_type_t type, size_t idx,
Greg Claytond8d4a572015-08-11 21:38:15 +00005402 std::string& name,
5403 uint64_t *bit_offset_ptr,
5404 uint32_t *bitfield_bit_size_ptr,
5405 bool *is_bitfield_ptr)
5406{
5407 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005408 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005409
5410 clang::QualType qual_type(GetCanonicalQualType(type));
5411 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5412 switch (type_class)
5413 {
5414 case clang::Type::Record:
5415 if (GetCompleteType(type))
5416 {
5417 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5418 const clang::RecordDecl *record_decl = record_type->getDecl();
5419 uint32_t field_idx = 0;
5420 clang::RecordDecl::field_iterator field, field_end;
5421 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
5422 {
5423 if (idx == field_idx)
5424 {
5425 // Print the member type if requested
5426 // Print the member name and equal sign
5427 name.assign(field->getNameAsString());
5428
5429 // Figure out the type byte size (field_type_info.first) and
5430 // alignment (field_type_info.second) from the AST context.
5431 if (bit_offset_ptr)
5432 {
5433 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5434 *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
5435 }
5436
5437 const bool is_bitfield = field->isBitField();
5438
5439 if (bitfield_bit_size_ptr)
5440 {
5441 *bitfield_bit_size_ptr = 0;
5442
5443 if (is_bitfield)
5444 {
5445 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5446 llvm::APSInt bitfield_apsint;
5447 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *getASTContext()))
5448 {
5449 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5450 }
5451 }
5452 }
5453 if (is_bitfield_ptr)
5454 *is_bitfield_ptr = is_bitfield;
5455
Greg Claytona1e5dc82015-08-11 22:53:00 +00005456 return CompilerType (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005457 }
5458 }
5459 }
5460 break;
5461
5462 case clang::Type::ObjCObjectPointer:
5463 if (GetCompleteType(type))
5464 {
5465 const clang::ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
5466 if (objc_class_type)
5467 {
5468 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005469 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 +00005470 }
5471 }
5472 break;
5473
5474 case clang::Type::ObjCObject:
5475 case clang::Type::ObjCInterface:
5476 if (GetCompleteType(type))
5477 {
5478 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5479 assert (objc_class_type);
5480 if (objc_class_type)
5481 {
5482 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
Greg Claytona1e5dc82015-08-11 22:53:00 +00005483 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 +00005484 }
5485 }
5486 break;
5487
5488
5489 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005490 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005491 GetFieldAtIndex (idx,
5492 name,
5493 bit_offset_ptr,
5494 bitfield_bit_size_ptr,
5495 is_bitfield_ptr);
5496
Enrico Granata36f51e42015-12-18 22:41:25 +00005497 case clang::Type::Auto:
5498 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).
5499 GetFieldAtIndex (idx,
5500 name,
5501 bit_offset_ptr,
5502 bitfield_bit_size_ptr,
5503 is_bitfield_ptr);
5504
Greg Claytond8d4a572015-08-11 21:38:15 +00005505 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005506 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005507 GetFieldAtIndex (idx,
5508 name,
5509 bit_offset_ptr,
5510 bitfield_bit_size_ptr,
5511 is_bitfield_ptr);
5512
5513 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00005514 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).
Greg Claytond8d4a572015-08-11 21:38:15 +00005515 GetFieldAtIndex (idx,
5516 name,
5517 bit_offset_ptr,
5518 bitfield_bit_size_ptr,
5519 is_bitfield_ptr);
5520
5521 default:
5522 break;
5523 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00005524 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005525}
5526
Greg Clayton99558cc42015-08-24 23:46:31 +00005527uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005528ClangASTContext::GetNumDirectBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005529{
5530 uint32_t count = 0;
5531 clang::QualType qual_type(GetCanonicalQualType(type));
5532 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5533 switch (type_class)
5534 {
5535 case clang::Type::Record:
5536 if (GetCompleteType(type))
5537 {
5538 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5539 if (cxx_record_decl)
5540 count = cxx_record_decl->getNumBases();
5541 }
5542 break;
5543
5544 case clang::Type::ObjCObjectPointer:
5545 count = GetPointeeType(type).GetNumDirectBaseClasses();
5546 break;
5547
5548 case clang::Type::ObjCObject:
5549 if (GetCompleteType(type))
5550 {
5551 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5552 if (objc_class_type)
5553 {
5554 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5555
5556 if (class_interface_decl && class_interface_decl->getSuperClass())
5557 count = 1;
5558 }
5559 }
5560 break;
5561 case clang::Type::ObjCInterface:
5562 if (GetCompleteType(type))
5563 {
5564 const clang::ObjCInterfaceType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5565 if (objc_interface_type)
5566 {
5567 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5568
5569 if (class_interface_decl && class_interface_decl->getSuperClass())
5570 count = 1;
5571 }
5572 }
5573 break;
5574
5575
5576 case clang::Type::Typedef:
5577 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5578 break;
5579
Enrico Granata36f51e42015-12-18 22:41:25 +00005580 case clang::Type::Auto:
5581 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
5582 break;
5583
Greg Clayton99558cc42015-08-24 23:46:31 +00005584 case clang::Type::Elaborated:
5585 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5586 break;
5587
5588 case clang::Type::Paren:
5589 return GetNumDirectBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5590
5591 default:
5592 break;
5593 }
5594 return count;
5595
5596}
5597
5598uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005599ClangASTContext::GetNumVirtualBaseClasses (lldb::opaque_compiler_type_t type)
Greg Clayton99558cc42015-08-24 23:46:31 +00005600{
5601 uint32_t count = 0;
5602 clang::QualType qual_type(GetCanonicalQualType(type));
5603 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5604 switch (type_class)
5605 {
5606 case clang::Type::Record:
5607 if (GetCompleteType(type))
5608 {
5609 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5610 if (cxx_record_decl)
5611 count = cxx_record_decl->getNumVBases();
5612 }
5613 break;
5614
5615 case clang::Type::Typedef:
5616 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr());
5617 break;
5618
Enrico Granata36f51e42015-12-18 22:41:25 +00005619 case clang::Type::Auto:
5620 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr());
5621 break;
5622
Greg Clayton99558cc42015-08-24 23:46:31 +00005623 case clang::Type::Elaborated:
5624 count = GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr());
5625 break;
5626
5627 case clang::Type::Paren:
5628 count = GetNumVirtualBaseClasses(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5629 break;
5630
5631 default:
5632 break;
5633 }
5634 return count;
5635
5636}
5637
5638CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005639ClangASTContext::GetDirectBaseClassAtIndex (lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr)
Greg Clayton99558cc42015-08-24 23:46:31 +00005640{
5641 clang::QualType qual_type(GetCanonicalQualType(type));
5642 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5643 switch (type_class)
5644 {
5645 case clang::Type::Record:
5646 if (GetCompleteType(type))
5647 {
5648 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5649 if (cxx_record_decl)
5650 {
5651 uint32_t curr_idx = 0;
5652 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5653 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5654 base_class != base_class_end;
5655 ++base_class, ++curr_idx)
5656 {
5657 if (curr_idx == idx)
5658 {
5659 if (bit_offset_ptr)
5660 {
5661 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5662 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5663 if (base_class->isVirtual())
5664 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5665 else
5666 *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5667 }
5668 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5669 }
5670 }
5671 }
5672 }
5673 break;
5674
5675 case clang::Type::ObjCObjectPointer:
5676 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5677
5678 case clang::Type::ObjCObject:
5679 if (idx == 0 && GetCompleteType(type))
5680 {
5681 const clang::ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
5682 if (objc_class_type)
5683 {
5684 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5685
5686 if (class_interface_decl)
5687 {
5688 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5689 if (superclass_interface_decl)
5690 {
5691 if (bit_offset_ptr)
5692 *bit_offset_ptr = 0;
5693 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5694 }
5695 }
5696 }
5697 }
5698 break;
5699 case clang::Type::ObjCInterface:
5700 if (idx == 0 && GetCompleteType(type))
5701 {
5702 const clang::ObjCObjectType *objc_interface_type = qual_type->getAs<clang::ObjCInterfaceType>();
5703 if (objc_interface_type)
5704 {
5705 clang::ObjCInterfaceDecl *class_interface_decl = objc_interface_type->getInterface();
5706
5707 if (class_interface_decl)
5708 {
5709 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
5710 if (superclass_interface_decl)
5711 {
5712 if (bit_offset_ptr)
5713 *bit_offset_ptr = 0;
5714 return CompilerType (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
5715 }
5716 }
5717 }
5718 }
5719 break;
5720
5721
5722 case clang::Type::Typedef:
5723 return GetDirectBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
5724
Enrico Granata36f51e42015-12-18 22:41:25 +00005725 case clang::Type::Auto:
5726 return GetDirectBaseClassAtIndex (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5727
Greg Clayton99558cc42015-08-24 23:46:31 +00005728 case clang::Type::Elaborated:
5729 return GetDirectBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5730
5731 case clang::Type::Paren:
5732 return GetDirectBaseClassAtIndex (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx, bit_offset_ptr);
5733
5734 default:
5735 break;
5736 }
5737 return CompilerType();
5738}
5739
5740CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005741ClangASTContext::GetVirtualBaseClassAtIndex (lldb::opaque_compiler_type_t type,
Greg Clayton99558cc42015-08-24 23:46:31 +00005742 size_t idx,
5743 uint32_t *bit_offset_ptr)
5744{
5745 clang::QualType qual_type(GetCanonicalQualType(type));
5746 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5747 switch (type_class)
5748 {
5749 case clang::Type::Record:
5750 if (GetCompleteType(type))
5751 {
5752 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5753 if (cxx_record_decl)
5754 {
5755 uint32_t curr_idx = 0;
5756 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5757 for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
5758 base_class != base_class_end;
5759 ++base_class, ++curr_idx)
5760 {
5761 if (curr_idx == idx)
5762 {
5763 if (bit_offset_ptr)
5764 {
5765 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(cxx_record_decl);
5766 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5767 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5768
5769 }
5770 return CompilerType (this, base_class->getType().getAsOpaquePtr());
5771 }
5772 }
5773 }
5774 }
5775 break;
5776
5777 case clang::Type::Typedef:
5778 return GetVirtualBaseClassAtIndex (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), idx, bit_offset_ptr);
Enrico Granata36f51e42015-12-18 22:41:25 +00005779
5780 case clang::Type::Auto:
5781 return GetVirtualBaseClassAtIndex (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5782
Greg Clayton99558cc42015-08-24 23:46:31 +00005783 case clang::Type::Elaborated:
5784 return GetVirtualBaseClassAtIndex (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), idx, bit_offset_ptr);
5785
5786 case clang::Type::Paren:
Zachary Turner9d8a97e2016-04-01 23:20:35 +00005787 return GetVirtualBaseClassAtIndex(llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), idx,
5788 bit_offset_ptr);
Greg Clayton99558cc42015-08-24 23:46:31 +00005789
5790 default:
5791 break;
5792 }
5793 return CompilerType();
5794
5795}
5796
Greg Claytond8d4a572015-08-11 21:38:15 +00005797// If a pointer to a pointee type (the clang_type arg) says that it has no
5798// children, then we either need to trust it, or override it and return a
5799// different result. For example, an "int *" has one child that is an integer,
5800// but a function pointer doesn't have any children. Likewise if a Record type
5801// claims it has no children, then there really is nothing to show.
5802uint32_t
5803ClangASTContext::GetNumPointeeChildren (clang::QualType type)
5804{
5805 if (type.isNull())
5806 return 0;
5807
5808 clang::QualType qual_type(type.getCanonicalType());
5809 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5810 switch (type_class)
5811 {
5812 case clang::Type::Builtin:
5813 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind())
5814 {
5815 case clang::BuiltinType::UnknownAny:
5816 case clang::BuiltinType::Void:
5817 case clang::BuiltinType::NullPtr:
5818 case clang::BuiltinType::OCLEvent:
5819 case clang::BuiltinType::OCLImage1d:
5820 case clang::BuiltinType::OCLImage1dArray:
5821 case clang::BuiltinType::OCLImage1dBuffer:
5822 case clang::BuiltinType::OCLImage2d:
5823 case clang::BuiltinType::OCLImage2dArray:
5824 case clang::BuiltinType::OCLImage3d:
5825 case clang::BuiltinType::OCLSampler:
5826 return 0;
5827 case clang::BuiltinType::Bool:
5828 case clang::BuiltinType::Char_U:
5829 case clang::BuiltinType::UChar:
5830 case clang::BuiltinType::WChar_U:
5831 case clang::BuiltinType::Char16:
5832 case clang::BuiltinType::Char32:
5833 case clang::BuiltinType::UShort:
5834 case clang::BuiltinType::UInt:
5835 case clang::BuiltinType::ULong:
5836 case clang::BuiltinType::ULongLong:
5837 case clang::BuiltinType::UInt128:
5838 case clang::BuiltinType::Char_S:
5839 case clang::BuiltinType::SChar:
5840 case clang::BuiltinType::WChar_S:
5841 case clang::BuiltinType::Short:
5842 case clang::BuiltinType::Int:
5843 case clang::BuiltinType::Long:
5844 case clang::BuiltinType::LongLong:
5845 case clang::BuiltinType::Int128:
5846 case clang::BuiltinType::Float:
5847 case clang::BuiltinType::Double:
5848 case clang::BuiltinType::LongDouble:
5849 case clang::BuiltinType::Dependent:
5850 case clang::BuiltinType::Overload:
5851 case clang::BuiltinType::ObjCId:
5852 case clang::BuiltinType::ObjCClass:
5853 case clang::BuiltinType::ObjCSel:
5854 case clang::BuiltinType::BoundMember:
5855 case clang::BuiltinType::Half:
5856 case clang::BuiltinType::ARCUnbridgedCast:
5857 case clang::BuiltinType::PseudoObject:
5858 case clang::BuiltinType::BuiltinFn:
Zachary Turner84f5b0d2015-09-09 17:25:43 +00005859 case clang::BuiltinType::OMPArraySection:
Greg Claytond8d4a572015-08-11 21:38:15 +00005860 return 1;
Zachary Turnerdd07e002015-09-16 18:08:45 +00005861 default:
5862 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005863 }
5864 break;
5865
5866 case clang::Type::Complex: return 1;
5867 case clang::Type::Pointer: return 1;
5868 case clang::Type::BlockPointer: return 0; // If block pointers don't have debug info, then no children for them
5869 case clang::Type::LValueReference: return 1;
5870 case clang::Type::RValueReference: return 1;
5871 case clang::Type::MemberPointer: return 0;
5872 case clang::Type::ConstantArray: return 0;
5873 case clang::Type::IncompleteArray: return 0;
5874 case clang::Type::VariableArray: return 0;
5875 case clang::Type::DependentSizedArray: return 0;
5876 case clang::Type::DependentSizedExtVector: return 0;
5877 case clang::Type::Vector: return 0;
5878 case clang::Type::ExtVector: return 0;
5879 case clang::Type::FunctionProto: return 0; // When we function pointers, they have no children...
5880 case clang::Type::FunctionNoProto: return 0; // When we function pointers, they have no children...
5881 case clang::Type::UnresolvedUsing: return 0;
5882 case clang::Type::Paren: return GetNumPointeeChildren (llvm::cast<clang::ParenType>(qual_type)->desugar());
5883 case clang::Type::Typedef: return GetNumPointeeChildren (llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType());
Enrico Granata36f51e42015-12-18 22:41:25 +00005884 case clang::Type::Auto: return GetNumPointeeChildren (llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
Greg Claytond8d4a572015-08-11 21:38:15 +00005885 case clang::Type::Elaborated: return GetNumPointeeChildren (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
5886 case clang::Type::TypeOfExpr: return 0;
5887 case clang::Type::TypeOf: return 0;
5888 case clang::Type::Decltype: return 0;
5889 case clang::Type::Record: return 0;
5890 case clang::Type::Enum: return 1;
5891 case clang::Type::TemplateTypeParm: return 1;
5892 case clang::Type::SubstTemplateTypeParm: return 1;
5893 case clang::Type::TemplateSpecialization: return 1;
5894 case clang::Type::InjectedClassName: return 0;
5895 case clang::Type::DependentName: return 1;
5896 case clang::Type::DependentTemplateSpecialization: return 1;
5897 case clang::Type::ObjCObject: return 0;
5898 case clang::Type::ObjCInterface: return 0;
5899 case clang::Type::ObjCObjectPointer: return 1;
5900 default:
5901 break;
5902 }
5903 return 0;
5904}
5905
5906
Greg Claytona1e5dc82015-08-11 22:53:00 +00005907CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005908ClangASTContext::GetChildCompilerTypeAtIndex (lldb::opaque_compiler_type_t type,
Bruce Mitchener4ad83342015-09-21 16:48:48 +00005909 ExecutionContext *exe_ctx,
5910 size_t idx,
5911 bool transparent_pointers,
5912 bool omit_empty_base_classes,
5913 bool ignore_array_bounds,
5914 std::string& child_name,
5915 uint32_t &child_byte_size,
5916 int32_t &child_byte_offset,
5917 uint32_t &child_bitfield_bit_size,
5918 uint32_t &child_bitfield_bit_offset,
5919 bool &child_is_base_class,
5920 bool &child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005921 ValueObject *valobj,
5922 uint64_t &language_flags)
Greg Claytond8d4a572015-08-11 21:38:15 +00005923{
5924 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005925 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005926
5927 clang::QualType parent_qual_type(GetCanonicalQualType(type));
5928 const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
5929 child_bitfield_bit_size = 0;
5930 child_bitfield_bit_offset = 0;
5931 child_is_base_class = false;
Enrico Granatadc62ffd2015-11-09 19:27:34 +00005932 language_flags = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00005933
5934 const bool idx_is_valid = idx < GetNumChildren (type, omit_empty_base_classes);
5935 uint32_t bit_offset;
5936 switch (parent_type_class)
5937 {
5938 case clang::Type::Builtin:
5939 if (idx_is_valid)
5940 {
5941 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind())
5942 {
5943 case clang::BuiltinType::ObjCId:
5944 case clang::BuiltinType::ObjCClass:
5945 child_name = "isa";
5946 child_byte_size = getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / CHAR_BIT;
Greg Claytona1e5dc82015-08-11 22:53:00 +00005947 return CompilerType (getASTContext(), getASTContext()->ObjCBuiltinClassTy);
Greg Claytond8d4a572015-08-11 21:38:15 +00005948
5949 default:
5950 break;
5951 }
5952 }
5953 break;
5954
5955 case clang::Type::Record:
5956 if (idx_is_valid && GetCompleteType(type))
5957 {
5958 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
5959 const clang::RecordDecl *record_decl = record_type->getDecl();
5960 assert(record_decl);
5961 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
5962 uint32_t child_idx = 0;
5963
5964 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5965 if (cxx_record_decl)
5966 {
5967 // We might have base classes to print out first
5968 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5969 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5970 base_class != base_class_end;
5971 ++base_class)
5972 {
5973 const clang::CXXRecordDecl *base_class_decl = nullptr;
5974
5975 // Skip empty base classes
5976 if (omit_empty_base_classes)
5977 {
5978 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5979 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5980 continue;
5981 }
5982
5983 if (idx == child_idx)
5984 {
5985 if (base_class_decl == nullptr)
5986 base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
5987
5988
5989 if (base_class->isVirtual())
5990 {
5991 bool handled = false;
5992 if (valobj)
5993 {
5994 Error err;
5995 AddressType addr_type = eAddressTypeInvalid;
5996 lldb::addr_t vtable_ptr_addr = valobj->GetCPPVTableAddress(addr_type);
5997
5998 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && addr_type == eAddressTypeLoad)
5999 {
6000
6001 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
6002 Process *process = exe_ctx.GetProcessPtr();
6003 if (process)
6004 {
6005 clang::VTableContextBase *vtable_ctx = getASTContext()->getVTableContext();
6006 if (vtable_ctx)
6007 {
6008 if (vtable_ctx->isMicrosoft())
6009 {
6010 clang::MicrosoftVTableContext *msoft_vtable_ctx = static_cast<clang::MicrosoftVTableContext *>(vtable_ctx);
6011
6012 if (vtable_ptr_addr)
6013 {
6014 const lldb::addr_t vbtable_ptr_addr = vtable_ptr_addr + record_layout.getVBPtrOffset().getQuantity();
6015
6016 const lldb::addr_t vbtable_ptr = process->ReadPointerFromMemory(vbtable_ptr_addr, err);
6017 if (vbtable_ptr != LLDB_INVALID_ADDRESS)
6018 {
6019 // Get the index into the virtual base table. The index is the index in uint32_t from vbtable_ptr
6020 const unsigned vbtable_index = msoft_vtable_ctx->getVBTableIndex(cxx_record_decl, base_class_decl);
6021 const lldb::addr_t base_offset_addr = vbtable_ptr + vbtable_index * 4;
6022 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
6023 if (base_offset != UINT32_MAX)
6024 {
6025 handled = true;
6026 bit_offset = base_offset * 8;
6027 }
6028 }
6029 }
6030 }
6031 else
6032 {
6033 clang::ItaniumVTableContext *itanium_vtable_ctx = static_cast<clang::ItaniumVTableContext *>(vtable_ctx);
6034 if (vtable_ptr_addr)
6035 {
6036 const lldb::addr_t vtable_ptr = process->ReadPointerFromMemory(vtable_ptr_addr, err);
6037 if (vtable_ptr != LLDB_INVALID_ADDRESS)
6038 {
6039 clang::CharUnits base_offset_offset = itanium_vtable_ctx->getVirtualBaseOffsetOffset(cxx_record_decl, base_class_decl);
6040 const lldb::addr_t base_offset_addr = vtable_ptr + base_offset_offset.getQuantity();
6041 const uint32_t base_offset = process->ReadUnsignedIntegerFromMemory(base_offset_addr, 4, UINT32_MAX, err);
6042 if (base_offset != UINT32_MAX)
6043 {
6044 handled = true;
6045 bit_offset = base_offset * 8;
6046 }
6047 }
6048 }
6049 }
6050 }
6051 }
6052 }
6053
6054 }
6055 if (!handled)
6056 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
6057 }
6058 else
6059 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
6060
6061 // Base classes should be a multiple of 8 bits in size
6062 child_byte_offset = bit_offset/8;
Greg Claytona1e5dc82015-08-11 22:53:00 +00006063 CompilerType base_class_clang_type(getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006064 child_name = base_class_clang_type.GetTypeName().AsCString("");
6065 uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6066
6067 // Base classes bit sizes should be a multiple of 8 bits in size
6068 assert (base_class_clang_type_bit_size % 8 == 0);
6069 child_byte_size = base_class_clang_type_bit_size / 8;
6070 child_is_base_class = true;
6071 return base_class_clang_type;
6072 }
6073 // We don't increment the child index in the for loop since we might
6074 // be skipping empty base classes
6075 ++child_idx;
6076 }
6077 }
6078 // Make sure index is in range...
6079 uint32_t field_idx = 0;
6080 clang::RecordDecl::field_iterator field, field_end;
6081 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
6082 {
6083 if (idx == child_idx)
6084 {
6085 // Print the member type if requested
6086 // Print the member name and equal sign
6087 child_name.assign(field->getNameAsString().c_str());
6088
6089 // Figure out the type byte size (field_type_info.first) and
6090 // alignment (field_type_info.second) from the AST context.
Greg Claytona1e5dc82015-08-11 22:53:00 +00006091 CompilerType field_clang_type (getASTContext(), field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006092 assert(field_idx < record_layout.getFieldCount());
6093 child_byte_size = field_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6094
6095 // Figure out the field offset within the current struct/union/class type
6096 bit_offset = record_layout.getFieldOffset (field_idx);
6097 child_byte_offset = bit_offset / 8;
6098 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, child_bitfield_bit_size))
6099 child_bitfield_bit_offset = bit_offset % 8;
6100
6101 return field_clang_type;
6102 }
6103 }
6104 }
6105 break;
6106
6107 case clang::Type::ObjCObject:
6108 case clang::Type::ObjCInterface:
6109 if (idx_is_valid && GetCompleteType(type))
6110 {
6111 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6112 assert (objc_class_type);
6113 if (objc_class_type)
6114 {
6115 uint32_t child_idx = 0;
6116 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6117
6118 if (class_interface_decl)
6119 {
6120
6121 const clang::ASTRecordLayout &interface_layout = getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6122 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6123 if (superclass_interface_decl)
6124 {
6125 if (omit_empty_base_classes)
6126 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006127 CompilerType base_class_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006128 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
6129 {
6130 if (idx == 0)
6131 {
6132 clang::QualType ivar_qual_type(getASTContext()->getObjCInterfaceType(superclass_interface_decl));
6133
6134
6135 child_name.assign(superclass_interface_decl->getNameAsString().c_str());
6136
6137 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6138
6139 child_byte_size = ivar_type_info.Width / 8;
6140 child_byte_offset = 0;
6141 child_is_base_class = true;
6142
Greg Claytona1e5dc82015-08-11 22:53:00 +00006143 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006144 }
6145
6146 ++child_idx;
6147 }
6148 }
6149 else
6150 ++child_idx;
6151 }
6152
6153 const uint32_t superclass_idx = child_idx;
6154
6155 if (idx < (child_idx + class_interface_decl->ivar_size()))
6156 {
6157 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6158
6159 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
6160 {
6161 if (child_idx == idx)
6162 {
6163 clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6164
6165 clang::QualType ivar_qual_type(ivar_decl->getType());
6166
6167 child_name.assign(ivar_decl->getNameAsString().c_str());
6168
6169 clang::TypeInfo ivar_type_info = getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6170
6171 child_byte_size = ivar_type_info.Width / 8;
6172
6173 // Figure out the field offset within the current struct/union/class type
6174 // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
6175 // that doesn't account for the space taken up by unbacked properties, or from
6176 // the changing size of base classes that are newer than this class.
6177 // So if we have a process around that we can ask about this object, do so.
6178 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6179 Process *process = nullptr;
6180 if (exe_ctx)
6181 process = exe_ctx->GetProcessPtr();
6182 if (process)
6183 {
6184 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
6185 if (objc_runtime != nullptr)
6186 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006187 CompilerType parent_ast_type (getASTContext(), parent_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006188 child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
6189 }
6190 }
6191
6192 // Setting this to UINT32_MAX to make sure we don't compute it twice...
6193 bit_offset = UINT32_MAX;
6194
6195 if (child_byte_offset == static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET))
6196 {
6197 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
6198 child_byte_offset = bit_offset / 8;
6199 }
6200
6201 // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
6202 // of a bitfield within its containing object. So regardless of where we get the byte
6203 // offset from, we still need to get the bit offset for bitfields from the layout.
6204
6205 if (ClangASTContext::FieldIsBitfield (getASTContext(), ivar_decl, child_bitfield_bit_size))
6206 {
6207 if (bit_offset == UINT32_MAX)
6208 bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
6209
6210 child_bitfield_bit_offset = bit_offset % 8;
6211 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006212 return CompilerType (getASTContext(), ivar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006213 }
6214 ++child_idx;
6215 }
6216 }
6217 }
6218 }
6219 }
6220 break;
6221
6222 case clang::Type::ObjCObjectPointer:
6223 if (idx_is_valid)
6224 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006225 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006226
6227 if (transparent_pointers && pointee_clang_type.IsAggregateType())
6228 {
6229 child_is_deref_of_parent = false;
6230 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006231 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6232 idx,
6233 transparent_pointers,
6234 omit_empty_base_classes,
6235 ignore_array_bounds,
6236 child_name,
6237 child_byte_size,
6238 child_byte_offset,
6239 child_bitfield_bit_size,
6240 child_bitfield_bit_offset,
6241 child_is_base_class,
6242 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006243 valobj,
6244 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006245 }
6246 else
6247 {
6248 child_is_deref_of_parent = true;
6249 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6250 if (parent_name)
6251 {
6252 child_name.assign(1, '*');
6253 child_name += parent_name;
6254 }
6255
6256 // We have a pointer to an simple type
6257 if (idx == 0 && pointee_clang_type.GetCompleteType())
6258 {
6259 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6260 child_byte_offset = 0;
6261 return pointee_clang_type;
6262 }
6263 }
6264 }
6265 break;
6266
6267 case clang::Type::Vector:
6268 case clang::Type::ExtVector:
6269 if (idx_is_valid)
6270 {
6271 const clang::VectorType *array = llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6272 if (array)
6273 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006274 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006275 if (element_type.GetCompleteType())
6276 {
6277 char element_name[64];
6278 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6279 child_name.assign(element_name);
6280 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6281 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6282 return element_type;
6283 }
6284 }
6285 }
6286 break;
6287
6288 case clang::Type::ConstantArray:
6289 case clang::Type::IncompleteArray:
6290 if (ignore_array_bounds || idx_is_valid)
6291 {
6292 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6293 if (array)
6294 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006295 CompilerType element_type (getASTContext(), array->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006296 if (element_type.GetCompleteType())
6297 {
6298 char element_name[64];
6299 ::snprintf (element_name, sizeof (element_name), "[%" PRIu64 "]", static_cast<uint64_t>(idx));
6300 child_name.assign(element_name);
6301 child_byte_size = element_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6302 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6303 return element_type;
6304 }
6305 }
6306 }
6307 break;
6308
6309
6310 case clang::Type::Pointer:
6311 if (idx_is_valid)
6312 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006313 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006314
6315 // Don't dereference "void *" pointers
6316 if (pointee_clang_type.IsVoidType())
Greg Claytona1e5dc82015-08-11 22:53:00 +00006317 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006318
6319 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6320 {
6321 child_is_deref_of_parent = false;
6322 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006323 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6324 idx,
6325 transparent_pointers,
6326 omit_empty_base_classes,
6327 ignore_array_bounds,
6328 child_name,
6329 child_byte_size,
6330 child_byte_offset,
6331 child_bitfield_bit_size,
6332 child_bitfield_bit_offset,
6333 child_is_base_class,
6334 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006335 valobj,
6336 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006337 }
6338 else
6339 {
6340 child_is_deref_of_parent = true;
6341
6342 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6343 if (parent_name)
6344 {
6345 child_name.assign(1, '*');
6346 child_name += parent_name;
6347 }
6348
6349 // We have a pointer to an simple type
6350 if (idx == 0)
6351 {
6352 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6353 child_byte_offset = 0;
6354 return pointee_clang_type;
6355 }
6356 }
6357 }
6358 break;
6359
6360 case clang::Type::LValueReference:
6361 case clang::Type::RValueReference:
6362 if (idx_is_valid)
6363 {
6364 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006365 CompilerType pointee_clang_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006366 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
6367 {
6368 child_is_deref_of_parent = false;
6369 bool tmp_child_is_deref_of_parent = false;
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006370 return pointee_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6371 idx,
6372 transparent_pointers,
6373 omit_empty_base_classes,
6374 ignore_array_bounds,
6375 child_name,
6376 child_byte_size,
6377 child_byte_offset,
6378 child_bitfield_bit_size,
6379 child_bitfield_bit_offset,
6380 child_is_base_class,
6381 tmp_child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006382 valobj,
6383 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006384 }
6385 else
6386 {
6387 const char *parent_name = valobj ? valobj->GetName().GetCString() : NULL;
6388 if (parent_name)
6389 {
6390 child_name.assign(1, '&');
6391 child_name += parent_name;
6392 }
6393
6394 // We have a pointer to an simple type
6395 if (idx == 0)
6396 {
6397 child_byte_size = pointee_clang_type.GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6398 child_byte_offset = 0;
6399 return pointee_clang_type;
6400 }
6401 }
6402 }
6403 break;
6404
6405 case clang::Type::Typedef:
6406 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006407 CompilerType typedefed_clang_type (getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006408 return typedefed_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6409 idx,
6410 transparent_pointers,
6411 omit_empty_base_classes,
6412 ignore_array_bounds,
6413 child_name,
6414 child_byte_size,
6415 child_byte_offset,
6416 child_bitfield_bit_size,
6417 child_bitfield_bit_offset,
6418 child_is_base_class,
6419 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006420 valobj,
6421 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006422 }
6423 break;
6424
Enrico Granata36f51e42015-12-18 22:41:25 +00006425 case clang::Type::Auto:
6426 {
6427 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6428 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6429 idx,
6430 transparent_pointers,
6431 omit_empty_base_classes,
6432 ignore_array_bounds,
6433 child_name,
6434 child_byte_size,
6435 child_byte_offset,
6436 child_bitfield_bit_size,
6437 child_bitfield_bit_offset,
6438 child_is_base_class,
6439 child_is_deref_of_parent,
6440 valobj,
6441 language_flags);
6442 }
6443
Greg Claytond8d4a572015-08-11 21:38:15 +00006444 case clang::Type::Elaborated:
6445 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006446 CompilerType elaborated_clang_type (getASTContext(), llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006447 return elaborated_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6448 idx,
6449 transparent_pointers,
6450 omit_empty_base_classes,
6451 ignore_array_bounds,
6452 child_name,
6453 child_byte_size,
6454 child_byte_offset,
6455 child_bitfield_bit_size,
6456 child_bitfield_bit_offset,
6457 child_is_base_class,
6458 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006459 valobj,
6460 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006461 }
6462
6463 case clang::Type::Paren:
6464 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006465 CompilerType paren_clang_type (getASTContext(), llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
Bruce Mitchener4ad83342015-09-21 16:48:48 +00006466 return paren_clang_type.GetChildCompilerTypeAtIndex (exe_ctx,
6467 idx,
6468 transparent_pointers,
6469 omit_empty_base_classes,
6470 ignore_array_bounds,
6471 child_name,
6472 child_byte_size,
6473 child_byte_offset,
6474 child_bitfield_bit_size,
6475 child_bitfield_bit_offset,
6476 child_is_base_class,
6477 child_is_deref_of_parent,
Enrico Granatadc62ffd2015-11-09 19:27:34 +00006478 valobj,
6479 language_flags);
Greg Claytond8d4a572015-08-11 21:38:15 +00006480 }
6481
6482
6483 default:
6484 break;
6485 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00006486 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006487}
6488
6489static uint32_t
6490GetIndexForRecordBase
6491(
6492 const clang::RecordDecl *record_decl,
6493 const clang::CXXBaseSpecifier *base_spec,
6494 bool omit_empty_base_classes
6495 )
6496{
6497 uint32_t child_idx = 0;
6498
6499 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6500
6501 // const char *super_name = record_decl->getNameAsCString();
6502 // const char *base_name = base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6503 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6504 //
6505 if (cxx_record_decl)
6506 {
6507 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6508 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6509 base_class != base_class_end;
6510 ++base_class)
6511 {
6512 if (omit_empty_base_classes)
6513 {
6514 if (BaseSpecifierIsEmpty (base_class))
6515 continue;
6516 }
6517
6518 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
6519 // child_idx,
6520 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6521 //
6522 //
6523 if (base_class == base_spec)
6524 return child_idx;
6525 ++child_idx;
6526 }
6527 }
6528
6529 return UINT32_MAX;
6530}
6531
6532
6533static uint32_t
6534GetIndexForRecordChild (const clang::RecordDecl *record_decl,
6535 clang::NamedDecl *canonical_decl,
6536 bool omit_empty_base_classes)
6537{
6538 uint32_t child_idx = ClangASTContext::GetNumBaseClasses (llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6539 omit_empty_base_classes);
6540
6541 clang::RecordDecl::field_iterator field, field_end;
6542 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6543 field != field_end;
6544 ++field, ++child_idx)
6545 {
6546 if (field->getCanonicalDecl() == canonical_decl)
6547 return child_idx;
6548 }
6549
6550 return UINT32_MAX;
6551}
6552
6553// Look for a child member (doesn't include base classes, but it does include
6554// their members) in the type hierarchy. Returns an index path into "clang_type"
6555// on how to reach the appropriate member.
6556//
6557// class A
6558// {
6559// public:
6560// int m_a;
6561// int m_b;
6562// };
6563//
6564// class B
6565// {
6566// };
6567//
6568// class C :
6569// public B,
6570// public A
6571// {
6572// };
6573//
6574// If we have a clang type that describes "class C", and we wanted to looked
6575// "m_b" in it:
6576//
6577// With omit_empty_base_classes == false we would get an integer array back with:
6578// { 1, 1 }
6579// The first index 1 is the child index for "class A" within class C
6580// The second index 1 is the child index for "m_b" within class A
6581//
6582// With omit_empty_base_classes == true we would get an integer array back with:
6583// { 0, 1 }
6584// 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)
6585// The second index 1 is the child index for "m_b" within class A
6586
6587size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006588ClangASTContext::GetIndexOfChildMemberWithName (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytond8d4a572015-08-11 21:38:15 +00006589 bool omit_empty_base_classes,
6590 std::vector<uint32_t>& child_indexes)
6591{
6592 if (type && name && name[0])
6593 {
6594 clang::QualType qual_type(GetCanonicalQualType(type));
6595 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6596 switch (type_class)
6597 {
6598 case clang::Type::Record:
6599 if (GetCompleteType(type))
6600 {
6601 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6602 const clang::RecordDecl *record_decl = record_type->getDecl();
6603
6604 assert(record_decl);
6605 uint32_t child_idx = 0;
6606
6607 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6608
6609 // Try and find a field that matches NAME
6610 clang::RecordDecl::field_iterator field, field_end;
6611 llvm::StringRef name_sref(name);
6612 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6613 field != field_end;
6614 ++field, ++child_idx)
6615 {
6616 llvm::StringRef field_name = field->getName();
6617 if (field_name.empty())
6618 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006619 CompilerType field_type(getASTContext(),field->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006620 child_indexes.push_back(child_idx);
6621 if (field_type.GetIndexOfChildMemberWithName(name, omit_empty_base_classes, child_indexes))
6622 return child_indexes.size();
6623 child_indexes.pop_back();
6624
6625 }
6626 else if (field_name.equals (name_sref))
6627 {
6628 // We have to add on the number of base classes to this index!
6629 child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
6630 return child_indexes.size();
6631 }
6632 }
6633
6634 if (cxx_record_decl)
6635 {
6636 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6637
6638 //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
6639
6640 //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
6641 // Didn't find things easily, lets let clang do its thang...
6642 clang::IdentifierInfo & ident_ref = getASTContext()->Idents.get(name_sref);
6643 clang::DeclarationName decl_name(&ident_ref);
6644
6645 clang::CXXBasePaths paths;
6646 if (cxx_record_decl->lookupInBases([decl_name](const clang::CXXBaseSpecifier *specifier, clang::CXXBasePath &path) {
6647 return clang::CXXRecordDecl::FindOrdinaryMember(specifier, path, decl_name);
6648 },
6649 paths))
6650 {
6651 clang::CXXBasePaths::const_paths_iterator path, path_end = paths.end();
6652 for (path = paths.begin(); path != path_end; ++path)
6653 {
6654 const size_t num_path_elements = path->size();
6655 for (size_t e=0; e<num_path_elements; ++e)
6656 {
6657 clang::CXXBasePathElement elem = (*path)[e];
6658
6659 child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
6660 if (child_idx == UINT32_MAX)
6661 {
6662 child_indexes.clear();
6663 return 0;
6664 }
6665 else
6666 {
6667 child_indexes.push_back (child_idx);
6668 parent_record_decl = llvm::cast<clang::RecordDecl>(elem.Base->getType()->getAs<clang::RecordType>()->getDecl());
6669 }
6670 }
6671 for (clang::NamedDecl *path_decl : path->Decls)
6672 {
6673 child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
6674 if (child_idx == UINT32_MAX)
6675 {
6676 child_indexes.clear();
6677 return 0;
6678 }
6679 else
6680 {
6681 child_indexes.push_back (child_idx);
6682 }
6683 }
6684 }
6685 return child_indexes.size();
6686 }
6687 }
6688
6689 }
6690 break;
6691
6692 case clang::Type::ObjCObject:
6693 case clang::Type::ObjCInterface:
6694 if (GetCompleteType(type))
6695 {
6696 llvm::StringRef name_sref(name);
6697 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6698 assert (objc_class_type);
6699 if (objc_class_type)
6700 {
6701 uint32_t child_idx = 0;
6702 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6703
6704 if (class_interface_decl)
6705 {
6706 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6707 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6708
6709 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6710 {
6711 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6712
6713 if (ivar_decl->getName().equals (name_sref))
6714 {
6715 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6716 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6717 ++child_idx;
6718
6719 child_indexes.push_back (child_idx);
6720 return child_indexes.size();
6721 }
6722 }
6723
6724 if (superclass_interface_decl)
6725 {
6726 // The super class index is always zero for ObjC classes,
6727 // so we push it onto the child indexes in case we find
6728 // an ivar in our superclass...
6729 child_indexes.push_back (0);
6730
Greg Claytona1e5dc82015-08-11 22:53:00 +00006731 CompilerType superclass_clang_type (getASTContext(), getASTContext()->getObjCInterfaceType(superclass_interface_decl));
Greg Claytond8d4a572015-08-11 21:38:15 +00006732 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
6733 omit_empty_base_classes,
6734 child_indexes))
6735 {
6736 // We did find an ivar in a superclass so just
6737 // return the results!
6738 return child_indexes.size();
6739 }
6740
6741 // We didn't find an ivar matching "name" in our
6742 // superclass, pop the superclass zero index that
6743 // we pushed on above.
6744 child_indexes.pop_back();
6745 }
6746 }
6747 }
6748 }
6749 break;
6750
6751 case clang::Type::ObjCObjectPointer:
6752 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006753 CompilerType objc_object_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006754 return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
6755 omit_empty_base_classes,
6756 child_indexes);
6757 }
6758 break;
6759
6760
6761 case clang::Type::ConstantArray:
6762 {
6763 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6764 // const uint64_t element_count = array->getSize().getLimitedValue();
6765 //
6766 // if (idx < element_count)
6767 // {
6768 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6769 //
6770 // char element_name[32];
6771 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6772 //
6773 // child_name.assign(element_name);
6774 // assert(field_type_info.first % 8 == 0);
6775 // child_byte_size = field_type_info.first / 8;
6776 // child_byte_offset = idx * child_byte_size;
6777 // return array->getElementType().getAsOpaquePtr();
6778 // }
6779 }
6780 break;
6781
6782 // case clang::Type::MemberPointerType:
6783 // {
6784 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6785 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6786 //
6787 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6788 // {
6789 // return GetIndexOfChildWithName (ast,
6790 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6791 // name);
6792 // }
6793 // }
6794 // break;
6795 //
6796 case clang::Type::LValueReference:
6797 case clang::Type::RValueReference:
6798 {
6799 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6800 clang::QualType pointee_type(reference_type->getPointeeType());
Greg Claytona1e5dc82015-08-11 22:53:00 +00006801 CompilerType pointee_clang_type (getASTContext(), pointee_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00006802
6803 if (pointee_clang_type.IsAggregateType ())
6804 {
6805 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6806 omit_empty_base_classes,
6807 child_indexes);
6808 }
6809 }
6810 break;
6811
6812 case clang::Type::Pointer:
6813 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006814 CompilerType pointee_clang_type (GetPointeeType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00006815
6816 if (pointee_clang_type.IsAggregateType ())
6817 {
6818 return pointee_clang_type.GetIndexOfChildMemberWithName (name,
6819 omit_empty_base_classes,
6820 child_indexes);
6821 }
6822 }
6823 break;
6824
6825 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006826 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00006827 omit_empty_base_classes,
6828 child_indexes);
6829
6830 case clang::Type::Auto:
6831 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetIndexOfChildMemberWithName (name,
6832 omit_empty_base_classes,
6833 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006834
6835 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006836 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00006837 omit_empty_base_classes,
6838 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006839
6840 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00006841 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
Enrico Granata36f51e42015-12-18 22:41:25 +00006842 omit_empty_base_classes,
6843 child_indexes);
Greg Claytond8d4a572015-08-11 21:38:15 +00006844
6845 default:
6846 break;
6847 }
6848 }
6849 return 0;
6850}
6851
6852
6853// Get the index of the child of "clang_type" whose name matches. This function
6854// doesn't descend into the children, but only looks one level deep and name
6855// matches can include base class names.
6856
6857uint32_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00006858ClangASTContext::GetIndexOfChildWithName (lldb::opaque_compiler_type_t type, const char *name, bool omit_empty_base_classes)
Greg Claytond8d4a572015-08-11 21:38:15 +00006859{
6860 if (type && name && name[0])
6861 {
6862 clang::QualType qual_type(GetCanonicalQualType(type));
6863
6864 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6865
6866 switch (type_class)
6867 {
6868 case clang::Type::Record:
6869 if (GetCompleteType(type))
6870 {
6871 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6872 const clang::RecordDecl *record_decl = record_type->getDecl();
6873
6874 assert(record_decl);
6875 uint32_t child_idx = 0;
6876
6877 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6878
6879 if (cxx_record_decl)
6880 {
6881 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6882 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
6883 base_class != base_class_end;
6884 ++base_class)
6885 {
6886 // Skip empty base classes
6887 clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
6888 if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
6889 continue;
6890
Greg Claytona1e5dc82015-08-11 22:53:00 +00006891 CompilerType base_class_clang_type (getASTContext(), base_class->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006892 std::string base_class_type_name (base_class_clang_type.GetTypeName().AsCString(""));
6893 if (base_class_type_name.compare (name) == 0)
6894 return child_idx;
6895 ++child_idx;
6896 }
6897 }
6898
6899 // Try and find a field that matches NAME
6900 clang::RecordDecl::field_iterator field, field_end;
6901 llvm::StringRef name_sref(name);
6902 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6903 field != field_end;
6904 ++field, ++child_idx)
6905 {
6906 if (field->getName().equals (name_sref))
6907 return child_idx;
6908 }
6909
6910 }
6911 break;
6912
6913 case clang::Type::ObjCObject:
6914 case clang::Type::ObjCInterface:
6915 if (GetCompleteType(type))
6916 {
6917 llvm::StringRef name_sref(name);
6918 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6919 assert (objc_class_type);
6920 if (objc_class_type)
6921 {
6922 uint32_t child_idx = 0;
6923 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6924
6925 if (class_interface_decl)
6926 {
6927 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
6928 clang::ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
6929
6930 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
6931 {
6932 const clang::ObjCIvarDecl* ivar_decl = *ivar_pos;
6933
6934 if (ivar_decl->getName().equals (name_sref))
6935 {
6936 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6937 ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
6938 ++child_idx;
6939
6940 return child_idx;
6941 }
6942 }
6943
6944 if (superclass_interface_decl)
6945 {
6946 if (superclass_interface_decl->getName().equals (name_sref))
6947 return 0;
6948 }
6949 }
6950 }
6951 }
6952 break;
6953
6954 case clang::Type::ObjCObjectPointer:
6955 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00006956 CompilerType pointee_clang_type (getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00006957 return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
6958 }
6959 break;
6960
6961 case clang::Type::ConstantArray:
6962 {
6963 // const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6964 // const uint64_t element_count = array->getSize().getLimitedValue();
6965 //
6966 // if (idx < element_count)
6967 // {
6968 // std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
6969 //
6970 // char element_name[32];
6971 // ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
6972 //
6973 // child_name.assign(element_name);
6974 // assert(field_type_info.first % 8 == 0);
6975 // child_byte_size = field_type_info.first / 8;
6976 // child_byte_offset = idx * child_byte_size;
6977 // return array->getElementType().getAsOpaquePtr();
6978 // }
6979 }
6980 break;
6981
6982 // case clang::Type::MemberPointerType:
6983 // {
6984 // MemberPointerType *mem_ptr_type = llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6985 // clang::QualType pointee_type = mem_ptr_type->getPointeeType();
6986 //
6987 // if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
6988 // {
6989 // return GetIndexOfChildWithName (ast,
6990 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6991 // name);
6992 // }
6993 // }
6994 // break;
6995 //
6996 case clang::Type::LValueReference:
6997 case clang::Type::RValueReference:
6998 {
6999 const clang::ReferenceType *reference_type = llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00007000 CompilerType pointee_type (getASTContext(), reference_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007001
7002 if (pointee_type.IsAggregateType ())
7003 {
7004 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
7005 }
7006 }
7007 break;
7008
7009 case clang::Type::Pointer:
7010 {
7011 const clang::PointerType *pointer_type = llvm::cast<clang::PointerType>(qual_type.getTypePtr());
Greg Claytona1e5dc82015-08-11 22:53:00 +00007012 CompilerType pointee_type (getASTContext(), pointer_type->getPointeeType());
Greg Claytond8d4a572015-08-11 21:38:15 +00007013
7014 if (pointee_type.IsAggregateType ())
7015 {
7016 return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
7017 }
7018 else
7019 {
7020 // if (parent_name)
7021 // {
7022 // child_name.assign(1, '*');
7023 // child_name += parent_name;
7024 // }
7025 //
7026 // // We have a pointer to an simple type
7027 // if (idx == 0)
7028 // {
7029 // std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
7030 // assert(clang_type_info.first % 8 == 0);
7031 // child_byte_size = clang_type_info.first / 8;
7032 // child_byte_offset = 0;
7033 // return pointee_type.getAsOpaquePtr();
7034 // }
7035 }
7036 }
7037 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00007038
7039 case clang::Type::Auto:
7040 return CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007041
7042 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007043 return CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007044
7045 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007046 return CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007047
7048 case clang::Type::Typedef:
Greg Claytona1e5dc82015-08-11 22:53:00 +00007049 return CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
Greg Claytond8d4a572015-08-11 21:38:15 +00007050
7051 default:
7052 break;
7053 }
7054 }
7055 return UINT32_MAX;
7056}
7057
7058
7059size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007060ClangASTContext::GetNumTemplateArguments (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007061{
7062 if (!type)
7063 return 0;
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007064
7065 clang::QualType qual_type (GetCanonicalQualType(type));
7066 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7067 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007068 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007069 case clang::Type::Record:
7070 if (GetCompleteType(type))
7071 {
7072 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7073 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00007074 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007075 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
7076 if (template_decl)
7077 return template_decl->getTemplateArgs().size();
Greg Claytond8d4a572015-08-11 21:38:15 +00007078 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007079 }
7080 break;
7081
7082 case clang::Type::Typedef:
7083 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetNumTemplateArguments();
Enrico Granata36f51e42015-12-18 22:41:25 +00007084
7085 case clang::Type::Auto:
7086 return (CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType())).GetNumTemplateArguments();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007087
7088 case clang::Type::Elaborated:
7089 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetNumTemplateArguments();
7090
7091 case clang::Type::Paren:
7092 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetNumTemplateArguments();
7093
7094 default:
7095 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007096 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007097
Greg Claytond8d4a572015-08-11 21:38:15 +00007098 return 0;
7099}
7100
Greg Claytona1e5dc82015-08-11 22:53:00 +00007101CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007102ClangASTContext::GetTemplateArgument (lldb::opaque_compiler_type_t type, size_t arg_idx, lldb::TemplateArgumentKind &kind)
Greg Claytond8d4a572015-08-11 21:38:15 +00007103{
7104 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00007105 return CompilerType();
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007106
7107 clang::QualType qual_type (GetCanonicalQualType(type));
7108 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7109 switch (type_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007110 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007111 case clang::Type::Record:
7112 if (GetCompleteType(type))
7113 {
7114 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
7115 if (cxx_record_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00007116 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007117 const clang::ClassTemplateSpecializationDecl *template_decl = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(cxx_record_decl);
7118 if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
Greg Claytond8d4a572015-08-11 21:38:15 +00007119 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007120 const clang::TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
7121 switch (template_arg.getKind())
Greg Claytond8d4a572015-08-11 21:38:15 +00007122 {
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007123 case clang::TemplateArgument::Null:
7124 kind = eTemplateArgumentKindNull;
7125 return CompilerType();
7126
7127 case clang::TemplateArgument::Type:
7128 kind = eTemplateArgumentKindType;
7129 return CompilerType(getASTContext(), template_arg.getAsType());
7130
7131 case clang::TemplateArgument::Declaration:
7132 kind = eTemplateArgumentKindDeclaration;
7133 return CompilerType();
7134
7135 case clang::TemplateArgument::Integral:
7136 kind = eTemplateArgumentKindIntegral;
7137 return CompilerType(getASTContext(), template_arg.getIntegralType());
7138
7139 case clang::TemplateArgument::Template:
7140 kind = eTemplateArgumentKindTemplate;
7141 return CompilerType();
7142
7143 case clang::TemplateArgument::TemplateExpansion:
7144 kind = eTemplateArgumentKindTemplateExpansion;
7145 return CompilerType();
7146
7147 case clang::TemplateArgument::Expression:
7148 kind = eTemplateArgumentKindExpression;
7149 return CompilerType();
7150
7151 case clang::TemplateArgument::Pack:
7152 kind = eTemplateArgumentKindPack;
7153 return CompilerType();
7154
7155 default:
7156 assert (!"Unhandled clang::TemplateArgument::ArgKind");
7157 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007158 }
7159 }
7160 }
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007161 }
7162 break;
7163
7164 case clang::Type::Typedef:
7165 return (CompilerType (getASTContext(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType())).GetTemplateArgument(arg_idx, kind);
Enrico Granata36f51e42015-12-18 22:41:25 +00007166
7167 case clang::Type::Auto:
7168 return (CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType())).GetTemplateArgument(arg_idx, kind);
Enrico Granata53f2a4a2015-08-13 00:24:24 +00007169
7170 case clang::Type::Elaborated:
7171 return (CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())).GetTemplateArgument(arg_idx, kind);
7172
7173 case clang::Type::Paren:
7174 return (CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar())).GetTemplateArgument(arg_idx, kind);
7175
7176 default:
7177 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00007178 }
7179 kind = eTemplateArgumentKindNull;
Greg Claytona1e5dc82015-08-11 22:53:00 +00007180 return CompilerType ();
Greg Claytond8d4a572015-08-11 21:38:15 +00007181}
7182
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007183CompilerType
7184ClangASTContext::GetTypeForFormatters (void* type)
7185{
7186 if (type)
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007187 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007188 return CompilerType();
7189}
7190
Greg Claytond8d4a572015-08-11 21:38:15 +00007191static bool
7192IsOperator (const char *name, clang::OverloadedOperatorKind &op_kind)
7193{
7194 if (name == nullptr || name[0] == '\0')
7195 return false;
7196
7197#define OPERATOR_PREFIX "operator"
7198#define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
7199
7200 const char *post_op_name = nullptr;
7201
7202 bool no_space = true;
7203
7204 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
7205 return false;
7206
7207 post_op_name = name + OPERATOR_PREFIX_LENGTH;
7208
7209 if (post_op_name[0] == ' ')
7210 {
7211 post_op_name++;
7212 no_space = false;
7213 }
7214
7215#undef OPERATOR_PREFIX
7216#undef OPERATOR_PREFIX_LENGTH
7217
7218 // This is an operator, set the overloaded operator kind to invalid
7219 // in case this is a conversion operator...
7220 op_kind = clang::NUM_OVERLOADED_OPERATORS;
7221
7222 switch (post_op_name[0])
7223 {
7224 default:
7225 if (no_space)
7226 return false;
7227 break;
7228 case 'n':
7229 if (no_space)
7230 return false;
7231 if (strcmp (post_op_name, "new") == 0)
7232 op_kind = clang::OO_New;
7233 else if (strcmp (post_op_name, "new[]") == 0)
7234 op_kind = clang::OO_Array_New;
7235 break;
7236
7237 case 'd':
7238 if (no_space)
7239 return false;
7240 if (strcmp (post_op_name, "delete") == 0)
7241 op_kind = clang::OO_Delete;
7242 else if (strcmp (post_op_name, "delete[]") == 0)
7243 op_kind = clang::OO_Array_Delete;
7244 break;
7245
7246 case '+':
7247 if (post_op_name[1] == '\0')
7248 op_kind = clang::OO_Plus;
7249 else if (post_op_name[2] == '\0')
7250 {
7251 if (post_op_name[1] == '=')
7252 op_kind = clang::OO_PlusEqual;
7253 else if (post_op_name[1] == '+')
7254 op_kind = clang::OO_PlusPlus;
7255 }
7256 break;
7257
7258 case '-':
7259 if (post_op_name[1] == '\0')
7260 op_kind = clang::OO_Minus;
7261 else if (post_op_name[2] == '\0')
7262 {
7263 switch (post_op_name[1])
7264 {
7265 case '=': op_kind = clang::OO_MinusEqual; break;
7266 case '-': op_kind = clang::OO_MinusMinus; break;
7267 case '>': op_kind = clang::OO_Arrow; break;
7268 }
7269 }
7270 else if (post_op_name[3] == '\0')
7271 {
7272 if (post_op_name[2] == '*')
7273 op_kind = clang::OO_ArrowStar; break;
7274 }
7275 break;
7276
7277 case '*':
7278 if (post_op_name[1] == '\0')
7279 op_kind = clang::OO_Star;
7280 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7281 op_kind = clang::OO_StarEqual;
7282 break;
7283
7284 case '/':
7285 if (post_op_name[1] == '\0')
7286 op_kind = clang::OO_Slash;
7287 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7288 op_kind = clang::OO_SlashEqual;
7289 break;
7290
7291 case '%':
7292 if (post_op_name[1] == '\0')
7293 op_kind = clang::OO_Percent;
7294 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7295 op_kind = clang::OO_PercentEqual;
7296 break;
7297
7298
7299 case '^':
7300 if (post_op_name[1] == '\0')
7301 op_kind = clang::OO_Caret;
7302 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7303 op_kind = clang::OO_CaretEqual;
7304 break;
7305
7306 case '&':
7307 if (post_op_name[1] == '\0')
7308 op_kind = clang::OO_Amp;
7309 else if (post_op_name[2] == '\0')
7310 {
7311 switch (post_op_name[1])
7312 {
7313 case '=': op_kind = clang::OO_AmpEqual; break;
7314 case '&': op_kind = clang::OO_AmpAmp; break;
7315 }
7316 }
7317 break;
7318
7319 case '|':
7320 if (post_op_name[1] == '\0')
7321 op_kind = clang::OO_Pipe;
7322 else if (post_op_name[2] == '\0')
7323 {
7324 switch (post_op_name[1])
7325 {
7326 case '=': op_kind = clang::OO_PipeEqual; break;
7327 case '|': op_kind = clang::OO_PipePipe; break;
7328 }
7329 }
7330 break;
7331
7332 case '~':
7333 if (post_op_name[1] == '\0')
7334 op_kind = clang::OO_Tilde;
7335 break;
7336
7337 case '!':
7338 if (post_op_name[1] == '\0')
7339 op_kind = clang::OO_Exclaim;
7340 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7341 op_kind = clang::OO_ExclaimEqual;
7342 break;
7343
7344 case '=':
7345 if (post_op_name[1] == '\0')
7346 op_kind = clang::OO_Equal;
7347 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
7348 op_kind = clang::OO_EqualEqual;
7349 break;
7350
7351 case '<':
7352 if (post_op_name[1] == '\0')
7353 op_kind = clang::OO_Less;
7354 else if (post_op_name[2] == '\0')
7355 {
7356 switch (post_op_name[1])
7357 {
7358 case '<': op_kind = clang::OO_LessLess; break;
7359 case '=': op_kind = clang::OO_LessEqual; break;
7360 }
7361 }
7362 else if (post_op_name[3] == '\0')
7363 {
7364 if (post_op_name[2] == '=')
7365 op_kind = clang::OO_LessLessEqual;
7366 }
7367 break;
7368
7369 case '>':
7370 if (post_op_name[1] == '\0')
7371 op_kind = clang::OO_Greater;
7372 else if (post_op_name[2] == '\0')
7373 {
7374 switch (post_op_name[1])
7375 {
7376 case '>': op_kind = clang::OO_GreaterGreater; break;
7377 case '=': op_kind = clang::OO_GreaterEqual; break;
7378 }
7379 }
7380 else if (post_op_name[1] == '>' &&
7381 post_op_name[2] == '=' &&
7382 post_op_name[3] == '\0')
7383 {
7384 op_kind = clang::OO_GreaterGreaterEqual;
7385 }
7386 break;
7387
7388 case ',':
7389 if (post_op_name[1] == '\0')
7390 op_kind = clang::OO_Comma;
7391 break;
7392
7393 case '(':
7394 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
7395 op_kind = clang::OO_Call;
7396 break;
7397
7398 case '[':
7399 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
7400 op_kind = clang::OO_Subscript;
7401 break;
7402 }
7403
7404 return true;
7405}
7406
7407clang::EnumDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007408ClangASTContext::GetAsEnumDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007409{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007410 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007411 if (enutype)
7412 return enutype->getDecl();
7413 return NULL;
7414}
7415
7416clang::RecordDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007417ClangASTContext::GetAsRecordDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007418{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007419 const clang::RecordType *record_type = llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007420 if (record_type)
7421 return record_type->getDecl();
7422 return nullptr;
7423}
7424
Greg Claytone6b36cd2015-12-08 01:02:08 +00007425clang::TagDecl *
7426ClangASTContext::GetAsTagDecl (const CompilerType& type)
7427{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007428 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
Greg Claytone6b36cd2015-12-08 01:02:08 +00007429 if (qual_type.isNull())
7430 return nullptr;
7431 else
7432 return qual_type->getAsTagDecl();
7433}
7434
Greg Claytond8d4a572015-08-11 21:38:15 +00007435clang::CXXRecordDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007436ClangASTContext::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007437{
7438 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7439}
7440
7441clang::ObjCInterfaceDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007442ClangASTContext::GetAsObjCInterfaceDecl (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007443{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007444 const clang::ObjCObjectType *objc_class_type =
7445 llvm::dyn_cast<clang::ObjCObjectType>(ClangUtil::GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00007446 if (objc_class_type)
7447 return objc_class_type->getInterface();
7448 return nullptr;
7449}
7450
7451clang::FieldDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007452ClangASTContext::AddFieldToRecordType (const CompilerType& type, const char *name,
7453 const CompilerType &field_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007454 AccessType access,
7455 uint32_t bitfield_bit_size)
7456{
7457 if (!type.IsValid() || !field_clang_type.IsValid())
7458 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007459 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007460 if (!ast)
7461 return nullptr;
7462 clang::ASTContext* clang_ast = ast->getASTContext();
7463
7464 clang::FieldDecl *field = nullptr;
7465
7466 clang::Expr *bit_width = nullptr;
7467 if (bitfield_bit_size != 0)
7468 {
7469 llvm::APInt bitfield_bit_size_apint(clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7470 bit_width = new (*clang_ast)clang::IntegerLiteral (*clang_ast, bitfield_bit_size_apint, clang_ast->IntTy, clang::SourceLocation());
7471 }
7472
7473 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7474 if (record_decl)
7475 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007476 field = clang::FieldDecl::Create(*clang_ast, record_decl, clang::SourceLocation(), clang::SourceLocation(),
7477 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7478 ClangUtil::GetQualType(field_clang_type), // Field type
7479 nullptr, // TInfo *
7480 bit_width, // BitWidth
7481 false, // Mutable
7482 clang::ICIS_NoInit); // HasInit
7483
Greg Claytond8d4a572015-08-11 21:38:15 +00007484 if (!name)
7485 {
7486 // Determine whether this field corresponds to an anonymous
7487 // struct or union.
7488 if (const clang::TagType *TagT = field->getType()->getAs<clang::TagType>()) {
7489 if (clang::RecordDecl *Rec = llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7490 if (!Rec->getDeclName()) {
7491 Rec->setAnonymousStructOrUnion(true);
7492 field->setImplicit();
7493
7494 }
7495 }
7496 }
7497
7498 if (field)
7499 {
7500 field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7501
7502 record_decl->addDecl(field);
7503
7504#ifdef LLDB_CONFIGURATION_DEBUG
7505 VerifyDecl(field);
7506#endif
7507 }
7508 }
7509 else
7510 {
7511 clang::ObjCInterfaceDecl *class_interface_decl = ast->GetAsObjCInterfaceDecl (type);
7512
7513 if (class_interface_decl)
7514 {
7515 const bool is_synthesized = false;
7516
7517 field_clang_type.GetCompleteType();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007518
7519 field = clang::ObjCIvarDecl::Create(
7520 *clang_ast, class_interface_decl, clang::SourceLocation(), clang::SourceLocation(),
7521 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7522 ClangUtil::GetQualType(field_clang_type), // Field type
7523 nullptr, // TypeSourceInfo *
7524 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, is_synthesized);
7525
Greg Claytond8d4a572015-08-11 21:38:15 +00007526 if (field)
7527 {
7528 class_interface_decl->addDecl(field);
7529
7530#ifdef LLDB_CONFIGURATION_DEBUG
7531 VerifyDecl(field);
7532#endif
7533 }
7534 }
7535 }
7536 return field;
7537}
7538
7539void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007540ClangASTContext::BuildIndirectFields (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007541{
Greg Claytonf73034f2015-09-08 18:15:05 +00007542 if (!type)
7543 return;
7544
7545 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007546 if (!ast)
7547 return;
7548
7549 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7550
7551 if (!record_decl)
7552 return;
7553
7554 typedef llvm::SmallVector <clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7555
7556 IndirectFieldVector indirect_fields;
7557 clang::RecordDecl::field_iterator field_pos;
7558 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7559 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7560 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
7561 {
7562 if (field_pos->isAnonymousStructOrUnion())
7563 {
7564 clang::QualType field_qual_type = field_pos->getType();
7565
7566 const clang::RecordType *field_record_type = field_qual_type->getAs<clang::RecordType>();
7567
7568 if (!field_record_type)
7569 continue;
7570
7571 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7572
7573 if (!field_record_decl)
7574 continue;
7575
7576 for (clang::RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
7577 di != de;
7578 ++di)
7579 {
7580 if (clang::FieldDecl *nested_field_decl = llvm::dyn_cast<clang::FieldDecl>(*di))
7581 {
7582 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[2];
7583 chain[0] = *field_pos;
7584 chain[1] = nested_field_decl;
7585 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7586 record_decl,
7587 clang::SourceLocation(),
7588 nested_field_decl->getIdentifier(),
7589 nested_field_decl->getType(),
7590 chain,
7591 2);
7592
7593 indirect_field->setImplicit();
7594
7595 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7596 nested_field_decl->getAccess()));
7597
7598 indirect_fields.push_back(indirect_field);
7599 }
7600 else if (clang::IndirectFieldDecl *nested_indirect_field_decl = llvm::dyn_cast<clang::IndirectFieldDecl>(*di))
7601 {
7602 int nested_chain_size = nested_indirect_field_decl->getChainingSize();
7603 clang::NamedDecl **chain = new (*ast->getASTContext()) clang::NamedDecl*[nested_chain_size + 1];
7604 chain[0] = *field_pos;
7605
7606 int chain_index = 1;
7607 for (clang::IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
7608 nce = nested_indirect_field_decl->chain_end();
7609 nci < nce;
7610 ++nci)
7611 {
7612 chain[chain_index] = *nci;
7613 chain_index++;
7614 }
7615
7616 clang::IndirectFieldDecl *indirect_field = clang::IndirectFieldDecl::Create(*ast->getASTContext(),
7617 record_decl,
7618 clang::SourceLocation(),
7619 nested_indirect_field_decl->getIdentifier(),
7620 nested_indirect_field_decl->getType(),
7621 chain,
7622 nested_chain_size + 1);
7623
7624 indirect_field->setImplicit();
7625
7626 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
7627 nested_indirect_field_decl->getAccess()));
7628
7629 indirect_fields.push_back(indirect_field);
7630 }
7631 }
7632 }
7633 }
7634
7635 // Check the last field to see if it has an incomplete array type as its
7636 // last member and if it does, the tell the record decl about it
7637 if (last_field_pos != field_end_pos)
7638 {
7639 if (last_field_pos->getType()->isIncompleteArrayType())
7640 record_decl->hasFlexibleArrayMember();
7641 }
7642
7643 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
7644 ifi < ife;
7645 ++ifi)
7646 {
7647 record_decl->addDecl(*ifi);
7648 }
7649}
7650
7651void
Greg Claytona1e5dc82015-08-11 22:53:00 +00007652ClangASTContext::SetIsPacked (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007653{
Greg Claytonf73034f2015-09-08 18:15:05 +00007654 if (type)
7655 {
7656 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7657 if (ast)
7658 {
7659 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
Greg Claytond8d4a572015-08-11 21:38:15 +00007660
Greg Claytonf73034f2015-09-08 18:15:05 +00007661 if (!record_decl)
7662 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00007663
Greg Claytonf73034f2015-09-08 18:15:05 +00007664 record_decl->addAttr(clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7665 }
7666 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007667}
7668
7669clang::VarDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00007670ClangASTContext::AddVariableToRecordType (const CompilerType& type, const char *name,
7671 const CompilerType &var_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007672 AccessType access)
7673{
7674 clang::VarDecl *var_decl = nullptr;
7675
7676 if (!type.IsValid() || !var_type.IsValid())
7677 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00007678 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007679 if (!ast)
7680 return nullptr;
7681
7682 clang::RecordDecl *record_decl = ast->GetAsRecordDecl (type);
7683 if (record_decl)
7684 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007685 var_decl =
7686 clang::VarDecl::Create(*ast->getASTContext(), // ASTContext &
7687 record_decl, // DeclContext *
7688 clang::SourceLocation(), // clang::SourceLocation StartLoc
7689 clang::SourceLocation(), // clang::SourceLocation IdLoc
7690 name ? &ast->getASTContext()->Idents.get(name) : nullptr, // clang::IdentifierInfo *
7691 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7692 nullptr, // TypeSourceInfo *
7693 clang::SC_Static); // StorageClass
Greg Claytond8d4a572015-08-11 21:38:15 +00007694 if (var_decl)
7695 {
7696 var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
7697 record_decl->addDecl(var_decl);
7698
7699#ifdef LLDB_CONFIGURATION_DEBUG
7700 VerifyDecl(var_decl);
7701#endif
7702 }
7703 }
7704 return var_decl;
7705}
7706
7707
7708clang::CXXMethodDecl *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007709ClangASTContext::AddMethodToCXXRecordType (lldb::opaque_compiler_type_t type, const char *name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007710 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007711 lldb::AccessType access,
7712 bool is_virtual,
7713 bool is_static,
7714 bool is_inline,
7715 bool is_explicit,
7716 bool is_attr_used,
7717 bool is_artificial)
7718{
7719 if (!type || !method_clang_type.IsValid() || name == nullptr || name[0] == '\0')
7720 return nullptr;
7721
7722 clang::QualType record_qual_type(GetCanonicalQualType(type));
7723
7724 clang::CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
7725
7726 if (cxx_record_decl == nullptr)
7727 return nullptr;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007728
7729 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7730
Greg Claytond8d4a572015-08-11 21:38:15 +00007731 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7732
7733 clang::DeclarationName decl_name (&getASTContext()->Idents.get(name));
7734
7735 const clang::FunctionType *function_type = llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7736
7737 if (function_type == nullptr)
7738 return nullptr;
7739
7740 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7741
7742 if (!method_function_prototype)
7743 return nullptr;
7744
7745 unsigned int num_params = method_function_prototype->getNumParams();
7746
7747 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7748 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7749
7750 if (is_artificial)
7751 return nullptr; // skip everything artificial
7752
7753 if (name[0] == '~')
7754 {
7755 cxx_dtor_decl = clang::CXXDestructorDecl::Create (*getASTContext(),
7756 cxx_record_decl,
7757 clang::SourceLocation(),
7758 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXDestructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7759 method_qual_type,
7760 nullptr,
7761 is_inline,
7762 is_artificial);
7763 cxx_method_decl = cxx_dtor_decl;
7764 }
7765 else if (decl_name == cxx_record_decl->getDeclName())
7766 {
7767 cxx_ctor_decl = clang::CXXConstructorDecl::Create (*getASTContext(),
7768 cxx_record_decl,
7769 clang::SourceLocation(),
7770 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConstructorName (getASTContext()->getCanonicalType (record_qual_type)), clang::SourceLocation()),
7771 method_qual_type,
7772 nullptr, // TypeSourceInfo *
7773 is_explicit,
7774 is_inline,
7775 is_artificial,
7776 false /*is_constexpr*/);
7777 cxx_method_decl = cxx_ctor_decl;
7778 }
7779 else
7780 {
7781 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7782 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7783
7784 if (IsOperator (name, op_kind))
7785 {
7786 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
7787 {
7788 // Check the number of operator parameters. Sometimes we have
7789 // seen bad DWARF that doesn't correctly describe operators and
7790 // if we try to create a method and add it to the class, clang
7791 // will assert and crash, so we need to make sure things are
7792 // acceptable.
7793 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
7794 return nullptr;
7795 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7796 cxx_record_decl,
7797 clang::SourceLocation(),
7798 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXOperatorName (op_kind), clang::SourceLocation()),
7799 method_qual_type,
7800 nullptr, // TypeSourceInfo *
7801 SC,
7802 is_inline,
7803 false /*is_constexpr*/,
7804 clang::SourceLocation());
7805 }
7806 else if (num_params == 0)
7807 {
7808 // Conversion operators don't take params...
7809 cxx_method_decl = clang::CXXConversionDecl::Create (*getASTContext(),
7810 cxx_record_decl,
7811 clang::SourceLocation(),
7812 clang::DeclarationNameInfo (getASTContext()->DeclarationNames.getCXXConversionFunctionName (getASTContext()->getCanonicalType (function_type->getReturnType())), clang::SourceLocation()),
7813 method_qual_type,
7814 nullptr, // TypeSourceInfo *
7815 is_inline,
7816 is_explicit,
7817 false /*is_constexpr*/,
7818 clang::SourceLocation());
7819 }
7820 }
7821
7822 if (cxx_method_decl == nullptr)
7823 {
7824 cxx_method_decl = clang::CXXMethodDecl::Create (*getASTContext(),
7825 cxx_record_decl,
7826 clang::SourceLocation(),
7827 clang::DeclarationNameInfo (decl_name, clang::SourceLocation()),
7828 method_qual_type,
7829 nullptr, // TypeSourceInfo *
7830 SC,
7831 is_inline,
7832 false /*is_constexpr*/,
7833 clang::SourceLocation());
7834 }
7835 }
7836
7837 clang::AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
7838
7839 cxx_method_decl->setAccess (access_specifier);
7840 cxx_method_decl->setVirtualAsWritten (is_virtual);
7841
7842 if (is_attr_used)
7843 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
7844
7845 // Populate the method decl with parameter decls
7846
7847 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7848
7849 for (unsigned param_index = 0;
7850 param_index < num_params;
7851 ++param_index)
7852 {
7853 params.push_back (clang::ParmVarDecl::Create (*getASTContext(),
7854 cxx_method_decl,
7855 clang::SourceLocation(),
7856 clang::SourceLocation(),
7857 nullptr, // anonymous
7858 method_function_prototype->getParamType(param_index),
7859 nullptr,
7860 clang::SC_None,
7861 nullptr));
7862 }
7863
7864 cxx_method_decl->setParams (llvm::ArrayRef<clang::ParmVarDecl*>(params));
7865
7866 cxx_record_decl->addDecl (cxx_method_decl);
7867
7868 // Sometimes the debug info will mention a constructor (default/copy/move),
7869 // destructor, or assignment operator (copy/move) but there won't be any
7870 // version of this in the code. So we check if the function was artificially
7871 // generated and if it is trivial and this lets the compiler/backend know
7872 // that it can inline the IR for these when it needs to and we can avoid a
7873 // "missing function" error when running expressions.
7874
7875 if (is_artificial)
7876 {
7877 if (cxx_ctor_decl &&
7878 ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
7879 (cxx_ctor_decl->isCopyConstructor() && cxx_record_decl->hasTrivialCopyConstructor ()) ||
7880 (cxx_ctor_decl->isMoveConstructor() && cxx_record_decl->hasTrivialMoveConstructor ()) ))
7881 {
7882 cxx_ctor_decl->setDefaulted();
7883 cxx_ctor_decl->setTrivial(true);
7884 }
7885 else if (cxx_dtor_decl)
7886 {
7887 if (cxx_record_decl->hasTrivialDestructor())
7888 {
7889 cxx_dtor_decl->setDefaulted();
7890 cxx_dtor_decl->setTrivial(true);
7891 }
7892 }
7893 else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
7894 (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
7895 {
7896 cxx_method_decl->setDefaulted();
7897 cxx_method_decl->setTrivial(true);
7898 }
7899 }
7900
7901#ifdef LLDB_CONFIGURATION_DEBUG
7902 VerifyDecl(cxx_method_decl);
7903#endif
7904
7905 // printf ("decl->isPolymorphic() = %i\n", cxx_record_decl->isPolymorphic());
7906 // printf ("decl->isAggregate() = %i\n", cxx_record_decl->isAggregate());
7907 // printf ("decl->isPOD() = %i\n", cxx_record_decl->isPOD());
7908 // printf ("decl->isEmpty() = %i\n", cxx_record_decl->isEmpty());
7909 // printf ("decl->isAbstract() = %i\n", cxx_record_decl->isAbstract());
7910 // printf ("decl->hasTrivialConstructor() = %i\n", cxx_record_decl->hasTrivialConstructor());
7911 // printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
7912 // printf ("decl->hasTrivialCopyAssignment() = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
7913 // printf ("decl->hasTrivialDestructor() = %i\n", cxx_record_decl->hasTrivialDestructor());
7914 return cxx_method_decl;
7915}
7916
7917
7918#pragma mark C++ Base Classes
7919
7920clang::CXXBaseSpecifier *
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007921ClangASTContext::CreateBaseClassSpecifier (lldb::opaque_compiler_type_t type, AccessType access, bool is_virtual, bool base_of_class)
Greg Claytond8d4a572015-08-11 21:38:15 +00007922{
7923 if (type)
7924 return new clang::CXXBaseSpecifier (clang::SourceRange(),
7925 is_virtual,
7926 base_of_class,
7927 ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
7928 getASTContext()->getTrivialTypeSourceInfo (GetQualType(type)),
7929 clang::SourceLocation());
7930 return nullptr;
7931}
7932
7933void
7934ClangASTContext::DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes)
7935{
7936 for (unsigned i=0; i<num_base_classes; ++i)
7937 {
7938 delete base_classes[i];
7939 base_classes[i] = nullptr;
7940 }
7941}
7942
7943bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00007944ClangASTContext::SetBaseClassesForClassType (lldb::opaque_compiler_type_t type, clang::CXXBaseSpecifier const * const *base_classes,
Greg Claytond8d4a572015-08-11 21:38:15 +00007945 unsigned num_base_classes)
7946{
7947 if (type)
7948 {
7949 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7950 if (cxx_record_decl)
7951 {
7952 cxx_record_decl->setBases(base_classes, num_base_classes);
7953 return true;
7954 }
7955 }
7956 return false;
7957}
7958
7959bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007960ClangASTContext::SetObjCSuperClass (const CompilerType& type, const CompilerType &superclass_clang_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007961{
Greg Claytonf73034f2015-09-08 18:15:05 +00007962 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007963 if (!ast)
7964 return false;
7965 clang::ASTContext* clang_ast = ast->getASTContext();
7966
7967 if (type && superclass_clang_type.IsValid() && superclass_clang_type.GetTypeSystem() == type.GetTypeSystem())
7968 {
7969 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7970 clang::ObjCInterfaceDecl *super_interface_decl = GetAsObjCInterfaceDecl (superclass_clang_type);
7971 if (class_interface_decl && super_interface_decl)
7972 {
7973 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(clang_ast->getObjCInterfaceType(super_interface_decl)));
7974 return true;
7975 }
7976 }
7977 return false;
7978}
7979
7980bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00007981ClangASTContext::AddObjCClassProperty (const CompilerType& type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007982 const char *property_name,
Greg Claytona1e5dc82015-08-11 22:53:00 +00007983 const CompilerType &property_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00007984 clang::ObjCIvarDecl *ivar_decl,
7985 const char *property_setter_name,
7986 const char *property_getter_name,
7987 uint32_t property_attributes,
7988 ClangASTMetadata *metadata)
7989{
7990 if (!type || !property_clang_type.IsValid() || property_name == nullptr || property_name[0] == '\0')
7991 return false;
Greg Claytonf73034f2015-09-08 18:15:05 +00007992 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00007993 if (!ast)
7994 return false;
7995 clang::ASTContext* clang_ast = ast->getASTContext();
7996
7997 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
7998
7999 if (class_interface_decl)
8000 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008001 CompilerType property_clang_type_to_access;
Greg Claytond8d4a572015-08-11 21:38:15 +00008002
8003 if (property_clang_type.IsValid())
8004 property_clang_type_to_access = property_clang_type;
8005 else if (ivar_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008006 property_clang_type_to_access = CompilerType (clang_ast, ivar_decl->getType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008007
8008 if (class_interface_decl && property_clang_type_to_access.IsValid())
8009 {
8010 clang::TypeSourceInfo *prop_type_source;
8011 if (ivar_decl)
8012 prop_type_source = clang_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
8013 else
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008014 prop_type_source = clang_ast->getTrivialTypeSourceInfo(ClangUtil::GetQualType(property_clang_type));
8015
8016 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8017 *clang_ast, class_interface_decl,
8018 clang::SourceLocation(), // Source Location
8019 &clang_ast->Idents.get(property_name),
8020 clang::SourceLocation(), // Source Location for AT
8021 clang::SourceLocation(), // Source location for (
8022 ivar_decl ? ivar_decl->getType() : ClangUtil::GetQualType(property_clang_type), prop_type_source);
8023
Greg Claytond8d4a572015-08-11 21:38:15 +00008024 if (property_decl)
8025 {
8026 if (metadata)
8027 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8028
8029 class_interface_decl->addDecl (property_decl);
8030
8031 clang::Selector setter_sel, getter_sel;
8032
8033 if (property_setter_name != nullptr)
8034 {
8035 std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
8036 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(property_setter_no_colon.c_str());
8037 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8038 }
8039 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
8040 {
8041 std::string setter_sel_string("set");
8042 setter_sel_string.push_back(::toupper(property_name[0]));
8043 setter_sel_string.append(&property_name[1]);
8044 clang::IdentifierInfo *setter_ident = &clang_ast->Idents.get(setter_sel_string.c_str());
8045 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8046 }
8047 property_decl->setSetterName(setter_sel);
8048 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
8049
8050 if (property_getter_name != nullptr)
8051 {
8052 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_getter_name);
8053 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8054 }
8055 else
8056 {
8057 clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name);
8058 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8059 }
8060 property_decl->setGetterName(getter_sel);
8061 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
8062
8063 if (ivar_decl)
8064 property_decl->setPropertyIvarDecl (ivar_decl);
8065
8066 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8067 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
8068 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8069 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8070 if (property_attributes & DW_APPLE_PROPERTY_assign)
8071 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
8072 if (property_attributes & DW_APPLE_PROPERTY_retain)
8073 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
8074 if (property_attributes & DW_APPLE_PROPERTY_copy)
8075 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
8076 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8077 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8078
8079 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
8080 {
8081 const bool isInstance = true;
8082 const bool isVariadic = false;
8083 const bool isSynthesized = false;
8084 const bool isImplicitlyDeclared = true;
8085 const bool isDefined = false;
8086 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
8087 const bool HasRelatedResultType = false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008088
8089 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8090 *clang_ast, clang::SourceLocation(), clang::SourceLocation(), getter_sel,
8091 ClangUtil::GetQualType(property_clang_type_to_access), nullptr, class_interface_decl,
8092 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8093 HasRelatedResultType);
8094
Greg Claytond8d4a572015-08-11 21:38:15 +00008095 if (getter && metadata)
8096 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8097
8098 if (getter)
8099 {
8100 getter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(), llvm::ArrayRef<clang::SourceLocation>());
8101
8102 class_interface_decl->addDecl(getter);
8103 }
8104 }
8105
8106 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
8107 {
8108 clang::QualType result_type = clang_ast->VoidTy;
8109
8110 const bool isInstance = true;
8111 const bool isVariadic = false;
8112 const bool isSynthesized = false;
8113 const bool isImplicitlyDeclared = true;
8114 const bool isDefined = false;
8115 const clang::ObjCMethodDecl::ImplementationControl impControl = clang::ObjCMethodDecl::None;
8116 const bool HasRelatedResultType = false;
8117
8118 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create (*clang_ast,
8119 clang::SourceLocation(),
8120 clang::SourceLocation(),
8121 setter_sel,
8122 result_type,
8123 nullptr,
8124 class_interface_decl,
8125 isInstance,
8126 isVariadic,
8127 isSynthesized,
8128 isImplicitlyDeclared,
8129 isDefined,
8130 impControl,
8131 HasRelatedResultType);
8132
8133 if (setter && metadata)
8134 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8135
8136 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008137
8138 params.push_back(clang::ParmVarDecl::Create(
8139 *clang_ast, setter, clang::SourceLocation(), clang::SourceLocation(),
8140 nullptr, // anonymous
8141 ClangUtil::GetQualType(property_clang_type_to_access), nullptr, clang::SC_Auto, nullptr));
8142
Greg Claytond8d4a572015-08-11 21:38:15 +00008143 if (setter)
8144 {
8145 setter->setMethodParams(*clang_ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8146
8147 class_interface_decl->addDecl(setter);
8148 }
8149 }
8150
8151 return true;
8152 }
8153 }
8154 }
8155 return false;
8156}
8157
8158bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008159ClangASTContext::IsObjCClassTypeAndHasIVars (const CompilerType& type, bool check_superclass)
Greg Claytond8d4a572015-08-11 21:38:15 +00008160{
8161 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl (type);
8162 if (class_interface_decl)
8163 return ObjCDeclHasIVars (class_interface_decl, check_superclass);
8164 return false;
8165}
8166
8167
8168clang::ObjCMethodDecl *
Greg Claytona1e5dc82015-08-11 22:53:00 +00008169ClangASTContext::AddMethodToObjCObjectType (const CompilerType& type,
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008170 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 +00008171 const CompilerType &method_clang_type,
Greg Claytond8d4a572015-08-11 21:38:15 +00008172 lldb::AccessType access,
8173 bool is_artificial)
8174{
8175 if (!type || !method_clang_type.IsValid())
8176 return nullptr;
8177
8178 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8179
8180 if (class_interface_decl == nullptr)
8181 return nullptr;
Greg Claytonf73034f2015-09-08 18:15:05 +00008182 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8183 if (lldb_ast == nullptr)
8184 return nullptr;
8185 clang::ASTContext *ast = lldb_ast->getASTContext();
8186
Greg Claytond8d4a572015-08-11 21:38:15 +00008187 const char *selector_start = ::strchr (name, ' ');
8188 if (selector_start == nullptr)
8189 return nullptr;
8190
8191 selector_start++;
8192 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8193
8194 size_t len = 0;
8195 const char *start;
8196 //printf ("name = '%s'\n", name);
8197
8198 unsigned num_selectors_with_args = 0;
8199 for (start = selector_start;
8200 start && *start != '\0' && *start != ']';
8201 start += len)
8202 {
8203 len = ::strcspn(start, ":]");
8204 bool has_arg = (start[len] == ':');
8205 if (has_arg)
8206 ++num_selectors_with_args;
8207 selector_idents.push_back (&ast->Idents.get (llvm::StringRef (start, len)));
8208 if (has_arg)
8209 len += 1;
8210 }
8211
8212
8213 if (selector_idents.size() == 0)
8214 return nullptr;
8215
8216 clang::Selector method_selector = ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
8217 selector_idents.data());
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008218
8219 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8220
Greg Claytond8d4a572015-08-11 21:38:15 +00008221 // Populate the method decl with parameter decls
8222 const clang::Type *method_type(method_qual_type.getTypePtr());
8223
8224 if (method_type == nullptr)
8225 return nullptr;
8226
8227 const clang::FunctionProtoType *method_function_prototype (llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8228
8229 if (!method_function_prototype)
8230 return nullptr;
8231
8232
8233 bool is_variadic = false;
8234 bool is_synthesized = false;
8235 bool is_defined = false;
8236 clang::ObjCMethodDecl::ImplementationControl imp_control = clang::ObjCMethodDecl::None;
8237
8238 const unsigned num_args = method_function_prototype->getNumParams();
8239
8240 if (num_args != num_selectors_with_args)
8241 return nullptr; // some debug information is corrupt. We are not going to deal with it.
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008242
8243 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8244 *ast,
8245 clang::SourceLocation(), // beginLoc,
8246 clang::SourceLocation(), // endLoc,
8247 method_selector, method_function_prototype->getReturnType(),
8248 nullptr, // TypeSourceInfo *ResultTInfo,
8249 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(ClangUtil::GetQualType(type)), name[0] == '-',
8250 is_variadic, is_synthesized,
8251 true, // is_implicitly_declared; we force this to true because we don't have source locations
8252 is_defined, imp_control, false /*has_related_result_type*/);
8253
Greg Claytond8d4a572015-08-11 21:38:15 +00008254 if (objc_method_decl == nullptr)
8255 return nullptr;
8256
8257 if (num_args > 0)
8258 {
8259 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8260
8261 for (unsigned param_index = 0; param_index < num_args; ++param_index)
8262 {
8263 params.push_back (clang::ParmVarDecl::Create (*ast,
8264 objc_method_decl,
8265 clang::SourceLocation(),
8266 clang::SourceLocation(),
8267 nullptr, // anonymous
8268 method_function_prototype->getParamType(param_index),
8269 nullptr,
8270 clang::SC_Auto,
8271 nullptr));
8272 }
8273
8274 objc_method_decl->setMethodParams(*ast, llvm::ArrayRef<clang::ParmVarDecl*>(params), llvm::ArrayRef<clang::SourceLocation>());
8275 }
8276
8277 class_interface_decl->addDecl (objc_method_decl);
8278
8279#ifdef LLDB_CONFIGURATION_DEBUG
8280 VerifyDecl(objc_method_decl);
8281#endif
8282
8283 return objc_method_decl;
8284}
8285
8286bool
Greg Claytone6b36cd2015-12-08 01:02:08 +00008287ClangASTContext::GetHasExternalStorage (const CompilerType &type)
8288{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008289 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008290 return false;
8291
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008292 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008293
8294 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8295 switch (type_class)
8296 {
8297 case clang::Type::Record:
8298 {
8299 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8300 if (cxx_record_decl)
8301 return cxx_record_decl->hasExternalLexicalStorage () || cxx_record_decl->hasExternalVisibleStorage();
8302 }
8303 break;
8304
8305 case clang::Type::Enum:
8306 {
8307 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8308 if (enum_decl)
8309 return enum_decl->hasExternalLexicalStorage () || enum_decl->hasExternalVisibleStorage();
8310 }
8311 break;
8312
8313 case clang::Type::ObjCObject:
8314 case clang::Type::ObjCInterface:
8315 {
8316 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8317 assert (objc_class_type);
8318 if (objc_class_type)
8319 {
8320 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8321
8322 if (class_interface_decl)
8323 return class_interface_decl->hasExternalLexicalStorage () || class_interface_decl->hasExternalVisibleStorage ();
8324 }
8325 }
8326 break;
8327
8328 case clang::Type::Typedef:
8329 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr()));
8330
Enrico Granata36f51e42015-12-18 22:41:25 +00008331 case clang::Type::Auto:
8332 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr()));
8333
Greg Claytone6b36cd2015-12-08 01:02:08 +00008334 case clang::Type::Elaborated:
8335 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
8336
8337 case clang::Type::Paren:
8338 return GetHasExternalStorage (CompilerType(type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8339
8340 default:
8341 break;
8342 }
8343 return false;
8344}
8345
8346
8347bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008348ClangASTContext::SetHasExternalStorage (lldb::opaque_compiler_type_t type, bool has_extern)
Greg Claytond8d4a572015-08-11 21:38:15 +00008349{
8350 if (!type)
8351 return false;
8352
8353 clang::QualType qual_type (GetCanonicalQualType(type));
8354
8355 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8356 switch (type_class)
8357 {
8358 case clang::Type::Record:
8359 {
8360 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8361 if (cxx_record_decl)
8362 {
8363 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
8364 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
8365 return true;
8366 }
8367 }
8368 break;
8369
8370 case clang::Type::Enum:
8371 {
8372 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
8373 if (enum_decl)
8374 {
8375 enum_decl->setHasExternalLexicalStorage (has_extern);
8376 enum_decl->setHasExternalVisibleStorage (has_extern);
8377 return true;
8378 }
8379 }
8380 break;
8381
8382 case clang::Type::ObjCObject:
8383 case clang::Type::ObjCInterface:
8384 {
8385 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8386 assert (objc_class_type);
8387 if (objc_class_type)
8388 {
8389 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
8390
8391 if (class_interface_decl)
8392 {
8393 class_interface_decl->setHasExternalLexicalStorage (has_extern);
8394 class_interface_decl->setHasExternalVisibleStorage (has_extern);
8395 return true;
8396 }
8397 }
8398 }
8399 break;
8400
8401 case clang::Type::Typedef:
8402 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType().getAsOpaquePtr(), has_extern);
Enrico Granata36f51e42015-12-18 22:41:25 +00008403
8404 case clang::Type::Auto:
8405 return SetHasExternalStorage (llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr(), has_extern);
Greg Claytond8d4a572015-08-11 21:38:15 +00008406
8407 case clang::Type::Elaborated:
8408 return SetHasExternalStorage (llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr(), has_extern);
8409
8410 case clang::Type::Paren:
8411 return SetHasExternalStorage (llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), has_extern);
8412
8413 default:
8414 break;
8415 }
8416 return false;
8417}
8418
8419
8420#pragma mark TagDecl
8421
8422bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008423ClangASTContext::StartTagDeclarationDefinition (const CompilerType &type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008424{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008425 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008426 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008427 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008428 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8429 if (tag_type)
Greg Clayton5dfc4a42015-12-02 00:43:32 +00008430 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008431 clang::TagDecl *tag_decl = tag_type->getDecl();
8432 if (tag_decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00008433 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008434 tag_decl->startDefinition();
8435 return true;
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00008436 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00008437 }
8438
8439 const clang::ObjCObjectType *object_type = qual_type->getAs<clang::ObjCObjectType>();
8440 if (object_type)
8441 {
8442 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8443 if (interface_decl)
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00008444 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008445 interface_decl->startDefinition();
8446 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008447 }
8448 }
8449 }
8450 return false;
8451}
8452
8453bool
Greg Claytona1e5dc82015-08-11 22:53:00 +00008454ClangASTContext::CompleteTagDeclarationDefinition (const CompilerType& type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008455{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008456 clang::QualType qual_type(ClangUtil::GetQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008457 if (!qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00008458 {
Greg Claytond8d4a572015-08-11 21:38:15 +00008459 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8460
8461 if (cxx_record_decl)
8462 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008463 if (!cxx_record_decl->isCompleteDefinition())
8464 cxx_record_decl->completeDefinition();
8465 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8466 cxx_record_decl->setHasExternalLexicalStorage (false);
8467 cxx_record_decl->setHasExternalVisibleStorage (false);
Greg Claytond8d4a572015-08-11 21:38:15 +00008468 return true;
8469 }
8470
Greg Claytone6b36cd2015-12-08 01:02:08 +00008471 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
Greg Claytond8d4a572015-08-11 21:38:15 +00008472
8473 if (enutype)
8474 {
8475 clang::EnumDecl *enum_decl = enutype->getDecl();
8476
8477 if (enum_decl)
8478 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008479 if (!enum_decl->isCompleteDefinition())
Greg Claytond8d4a572015-08-11 21:38:15 +00008480 {
Greg Claytone6b36cd2015-12-08 01:02:08 +00008481 ClangASTContext *lldb_ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8482 if (lldb_ast == nullptr)
8483 return false;
8484 clang::ASTContext *ast = lldb_ast->getASTContext();
8485
8486 /// TODO This really needs to be fixed.
8487
8488 unsigned NumPositiveBits = 1;
8489 unsigned NumNegativeBits = 0;
8490
8491 clang::QualType promotion_qual_type;
8492 // If the enum integer type is less than an integer in bit width,
8493 // then we must promote it to an integer size.
8494 if (ast->getTypeSize(enum_decl->getIntegerType()) < ast->getTypeSize(ast->IntTy))
8495 {
8496 if (enum_decl->getIntegerType()->isSignedIntegerType())
8497 promotion_qual_type = ast->IntTy;
8498 else
8499 promotion_qual_type = ast->UnsignedIntTy;
8500 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008501 else
Greg Claytone6b36cd2015-12-08 01:02:08 +00008502 promotion_qual_type = enum_decl->getIntegerType();
8503
8504 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
Greg Claytond8d4a572015-08-11 21:38:15 +00008505 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008506 return true;
8507 }
8508 }
8509 }
8510 return false;
8511}
8512
Greg Claytond8d4a572015-08-11 21:38:15 +00008513bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008514ClangASTContext::AddEnumerationValueToEnumerationType (lldb::opaque_compiler_type_t type,
Greg Clayton8b4edba2015-08-14 20:02:05 +00008515 const CompilerType &enumerator_clang_type,
8516 const Declaration &decl,
8517 const char *name,
8518 int64_t enum_value,
8519 uint32_t enum_value_bit_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008520{
8521 if (type && enumerator_clang_type.IsValid() && name && name[0])
8522 {
8523 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8524
8525 bool is_signed = false;
8526 enumerator_clang_type.IsIntegerType (is_signed);
8527 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8528 if (clang_type)
8529 {
8530 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8531
8532 if (enutype)
8533 {
8534 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8535 enum_llvm_apsint = enum_value;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008536 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8537 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8538 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8539 ClangUtil::GetQualType(enumerator_clang_type), nullptr, enum_llvm_apsint);
8540
Greg Claytond8d4a572015-08-11 21:38:15 +00008541 if (enumerator_decl)
8542 {
8543 enutype->getDecl()->addDecl(enumerator_decl);
8544
8545#ifdef LLDB_CONFIGURATION_DEBUG
8546 VerifyDecl(enumerator_decl);
8547#endif
8548
8549 return true;
8550 }
8551 }
8552 }
8553 }
8554 return false;
8555}
8556
Greg Claytona1e5dc82015-08-11 22:53:00 +00008557CompilerType
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008558ClangASTContext::GetEnumerationIntegerType (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008559{
8560 clang::QualType enum_qual_type (GetCanonicalQualType(type));
8561 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8562 if (clang_type)
8563 {
8564 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8565 if (enutype)
8566 {
8567 clang::EnumDecl *enum_decl = enutype->getDecl();
8568 if (enum_decl)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008569 return CompilerType (getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008570 }
8571 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008572 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008573}
8574
Greg Claytona1e5dc82015-08-11 22:53:00 +00008575CompilerType
8576ClangASTContext::CreateMemberPointerType (const CompilerType& type, const CompilerType &pointee_type)
Greg Claytond8d4a572015-08-11 21:38:15 +00008577{
8578 if (type && pointee_type.IsValid() && type.GetTypeSystem() == pointee_type.GetTypeSystem())
8579 {
Greg Claytonf73034f2015-09-08 18:15:05 +00008580 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
Greg Claytond8d4a572015-08-11 21:38:15 +00008581 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00008582 return CompilerType();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008583 return CompilerType(ast->getASTContext(),
8584 ast->getASTContext()->getMemberPointerType(ClangUtil::GetQualType(pointee_type),
8585 ClangUtil::GetQualType(type).getTypePtr()));
Greg Claytond8d4a572015-08-11 21:38:15 +00008586 }
Greg Claytona1e5dc82015-08-11 22:53:00 +00008587 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008588}
8589
8590
8591size_t
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008592ClangASTContext::ConvertStringToFloatValue (lldb::opaque_compiler_type_t type, const char *s, uint8_t *dst, size_t dst_size)
Greg Claytond8d4a572015-08-11 21:38:15 +00008593{
8594 if (type)
8595 {
8596 clang::QualType qual_type (GetCanonicalQualType(type));
8597 uint32_t count = 0;
8598 bool is_complex = false;
8599 if (IsFloatingPointType (type, count, is_complex))
8600 {
8601 // TODO: handle complex and vector types
8602 if (count != 1)
8603 return false;
8604
8605 llvm::StringRef s_sref(s);
8606 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), s_sref);
8607
8608 const uint64_t bit_size = getASTContext()->getTypeSize (qual_type);
8609 const uint64_t byte_size = bit_size / 8;
8610 if (dst_size >= byte_size)
8611 {
8612 if (bit_size == sizeof(float)*8)
8613 {
8614 float float32 = ap_float.convertToFloat();
8615 ::memcpy (dst, &float32, byte_size);
8616 return byte_size;
8617 }
8618 else if (bit_size >= 64)
8619 {
8620 llvm::APInt ap_int(ap_float.bitcastToAPInt());
8621 ::memcpy (dst, ap_int.getRawData(), byte_size);
8622 return byte_size;
8623 }
8624 }
8625 }
8626 }
8627 return 0;
8628}
8629
8630
8631
8632//----------------------------------------------------------------------
8633// Dumping types
8634//----------------------------------------------------------------------
8635#define DEPTH_INCREMENT 2
8636
8637void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008638ClangASTContext::DumpValue (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00008639 Stream *s,
8640 lldb::Format format,
8641 const lldb_private::DataExtractor &data,
8642 lldb::offset_t data_byte_offset,
8643 size_t data_byte_size,
8644 uint32_t bitfield_bit_size,
8645 uint32_t bitfield_bit_offset,
8646 bool show_types,
8647 bool show_summary,
8648 bool verbose,
8649 uint32_t depth)
8650{
8651 if (!type)
8652 return;
8653
8654 clang::QualType qual_type(GetQualType(type));
8655 switch (qual_type->getTypeClass())
8656 {
8657 case clang::Type::Record:
8658 if (GetCompleteType(type))
8659 {
8660 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8661 const clang::RecordDecl *record_decl = record_type->getDecl();
8662 assert(record_decl);
8663 uint32_t field_bit_offset = 0;
8664 uint32_t field_byte_offset = 0;
8665 const clang::ASTRecordLayout &record_layout = getASTContext()->getASTRecordLayout(record_decl);
8666 uint32_t child_idx = 0;
8667
8668 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8669 if (cxx_record_decl)
8670 {
8671 // We might have base classes to print out first
8672 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
8673 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
8674 base_class != base_class_end;
8675 ++base_class)
8676 {
8677 const clang::CXXRecordDecl *base_class_decl = llvm::cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
8678
8679 // Skip empty base classes
8680 if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
8681 continue;
8682
8683 if (base_class->isVirtual())
8684 field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
8685 else
8686 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
8687 field_byte_offset = field_bit_offset / 8;
8688 assert (field_bit_offset % 8 == 0);
8689 if (child_idx == 0)
8690 s->PutChar('{');
8691 else
8692 s->PutChar(',');
8693
8694 clang::QualType base_class_qual_type = base_class->getType();
8695 std::string base_class_type_name(base_class_qual_type.getAsString());
8696
8697 // Indent and print the base class type name
8698 s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
8699
8700 clang::TypeInfo base_class_type_info = getASTContext()->getTypeInfo(base_class_qual_type);
8701
8702 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008703 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008704 base_clang_type.DumpValue (exe_ctx,
8705 s, // Stream to dump to
8706 base_clang_type.GetFormat(), // The format with which to display the member
8707 data, // Data buffer containing all bytes for this type
8708 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8709 base_class_type_info.Width / 8, // Size of this type in bytes
8710 0, // Bitfield bit size
8711 0, // Bitfield bit offset
8712 show_types, // Boolean indicating if we should show the variable types
8713 show_summary, // Boolean indicating if we should show a summary for the current type
8714 verbose, // Verbose output?
8715 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8716
8717 ++child_idx;
8718 }
8719 }
8720 uint32_t field_idx = 0;
8721 clang::RecordDecl::field_iterator field, field_end;
8722 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
8723 {
8724 // Print the starting squiggly bracket (if this is the
8725 // first member) or comma (for member 2 and beyond) for
8726 // the struct/union/class member.
8727 if (child_idx == 0)
8728 s->PutChar('{');
8729 else
8730 s->PutChar(',');
8731
8732 // Indent
8733 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8734
8735 clang::QualType field_type = field->getType();
8736 // Print the member type if requested
8737 // Figure out the type byte size (field_type_info.first) and
8738 // alignment (field_type_info.second) from the AST context.
8739 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(field_type);
8740 assert(field_idx < record_layout.getFieldCount());
8741 // Figure out the field offset within the current struct/union/class type
8742 field_bit_offset = record_layout.getFieldOffset (field_idx);
8743 field_byte_offset = field_bit_offset / 8;
8744 uint32_t field_bitfield_bit_size = 0;
8745 uint32_t field_bitfield_bit_offset = 0;
8746 if (ClangASTContext::FieldIsBitfield (getASTContext(), *field, field_bitfield_bit_size))
8747 field_bitfield_bit_offset = field_bit_offset % 8;
8748
8749 if (show_types)
8750 {
8751 std::string field_type_name(field_type.getAsString());
8752 if (field_bitfield_bit_size > 0)
8753 s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
8754 else
8755 s->Printf("(%s) ", field_type_name.c_str());
8756 }
8757 // Print the member name and equal sign
8758 s->Printf("%s = ", field->getNameAsString().c_str());
8759
8760
8761 // Dump the value of the member
Greg Claytona1e5dc82015-08-11 22:53:00 +00008762 CompilerType field_clang_type (getASTContext(), field_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008763 field_clang_type.DumpValue (exe_ctx,
8764 s, // Stream to dump to
8765 field_clang_type.GetFormat(), // The format with which to display the member
8766 data, // Data buffer containing all bytes for this type
8767 data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
8768 field_type_info.Width / 8, // Size of this type in bytes
8769 field_bitfield_bit_size, // Bitfield bit size
8770 field_bitfield_bit_offset, // Bitfield bit offset
8771 show_types, // Boolean indicating if we should show the variable types
8772 show_summary, // Boolean indicating if we should show a summary for the current type
8773 verbose, // Verbose output?
8774 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8775 }
8776
8777 // Indent the trailing squiggly bracket
8778 if (child_idx > 0)
8779 s->Printf("\n%*s}", depth, "");
8780 }
8781 return;
8782
8783 case clang::Type::Enum:
8784 if (GetCompleteType(type))
8785 {
8786 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8787 const clang::EnumDecl *enum_decl = enutype->getDecl();
8788 assert(enum_decl);
8789 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8790 lldb::offset_t offset = data_byte_offset;
8791 const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8792 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
8793 {
8794 if (enum_pos->getInitVal() == enum_value)
8795 {
8796 s->Printf("%s", enum_pos->getNameAsString().c_str());
8797 return;
8798 }
8799 }
8800 // If we have gotten here we didn't get find the enumerator in the
8801 // enum decl, so just print the integer.
8802 s->Printf("%" PRIi64, enum_value);
8803 }
8804 return;
8805
8806 case clang::Type::ConstantArray:
8807 {
8808 const clang::ConstantArrayType *array = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
8809 bool is_array_of_characters = false;
8810 clang::QualType element_qual_type = array->getElementType();
8811
8812 const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
8813 if (canonical_type)
8814 is_array_of_characters = canonical_type->isCharType();
8815
8816 const uint64_t element_count = array->getSize().getLimitedValue();
8817
8818 clang::TypeInfo field_type_info = getASTContext()->getTypeInfo(element_qual_type);
8819
8820 uint32_t element_idx = 0;
8821 uint32_t element_offset = 0;
8822 uint64_t element_byte_size = field_type_info.Width / 8;
8823 uint32_t element_stride = element_byte_size;
8824
8825 if (is_array_of_characters)
8826 {
8827 s->PutChar('"');
8828 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
8829 s->PutChar('"');
8830 return;
8831 }
8832 else
8833 {
Greg Claytona1e5dc82015-08-11 22:53:00 +00008834 CompilerType element_clang_type(getASTContext(), element_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008835 lldb::Format element_format = element_clang_type.GetFormat();
8836
8837 for (element_idx = 0; element_idx < element_count; ++element_idx)
8838 {
8839 // Print the starting squiggly bracket (if this is the
8840 // first member) or comman (for member 2 and beyong) for
8841 // the struct/union/class member.
8842 if (element_idx == 0)
8843 s->PutChar('{');
8844 else
8845 s->PutChar(',');
8846
8847 // Indent and print the index
8848 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
8849
8850 // Figure out the field offset within the current struct/union/class type
8851 element_offset = element_idx * element_stride;
8852
8853 // Dump the value of the member
8854 element_clang_type.DumpValue (exe_ctx,
8855 s, // Stream to dump to
8856 element_format, // The format with which to display the element
8857 data, // Data buffer containing all bytes for this type
8858 data_byte_offset + element_offset,// Offset into "data" where to grab value from
8859 element_byte_size, // Size of this type in bytes
8860 0, // Bitfield bit size
8861 0, // Bitfield bit offset
8862 show_types, // Boolean indicating if we should show the variable types
8863 show_summary, // Boolean indicating if we should show a summary for the current type
8864 verbose, // Verbose output?
8865 depth + DEPTH_INCREMENT); // Scope depth for any types that have children
8866 }
8867
8868 // Indent the trailing squiggly bracket
8869 if (element_idx > 0)
8870 s->Printf("\n%*s}", depth, "");
8871 }
8872 }
8873 return;
8874
8875 case clang::Type::Typedef:
8876 {
8877 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
8878
Greg Claytona1e5dc82015-08-11 22:53:00 +00008879 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008880 lldb::Format typedef_format = typedef_clang_type.GetFormat();
8881 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
8882 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8883
8884 return typedef_clang_type.DumpValue (exe_ctx,
8885 s, // Stream to dump to
8886 typedef_format, // The format with which to display the element
8887 data, // Data buffer containing all bytes for this type
8888 data_byte_offset, // Offset into "data" where to grab value from
8889 typedef_byte_size, // Size of this type in bytes
8890 bitfield_bit_size, // Bitfield bit size
8891 bitfield_bit_offset,// Bitfield bit offset
8892 show_types, // Boolean indicating if we should show the variable types
8893 show_summary, // Boolean indicating if we should show a summary for the current type
8894 verbose, // Verbose output?
8895 depth); // Scope depth for any types that have children
8896 }
8897 break;
8898
Enrico Granata36f51e42015-12-18 22:41:25 +00008899 case clang::Type::Auto:
8900 {
8901 clang::QualType elaborated_qual_type = llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
8902 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
8903 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8904 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8905 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8906
8907 return elaborated_clang_type.DumpValue (exe_ctx,
8908 s, // Stream to dump to
8909 elaborated_format, // The format with which to display the element
8910 data, // Data buffer containing all bytes for this type
8911 data_byte_offset, // Offset into "data" where to grab value from
8912 elaborated_byte_size, // Size of this type in bytes
8913 bitfield_bit_size, // Bitfield bit size
8914 bitfield_bit_offset,// Bitfield bit offset
8915 show_types, // Boolean indicating if we should show the variable types
8916 show_summary, // Boolean indicating if we should show a summary for the current type
8917 verbose, // Verbose output?
8918 depth); // Scope depth for any types that have children
8919 }
8920 break;
8921
Greg Claytond8d4a572015-08-11 21:38:15 +00008922 case clang::Type::Elaborated:
8923 {
8924 clang::QualType elaborated_qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008925 CompilerType elaborated_clang_type (getASTContext(), elaborated_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008926 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
8927 clang::TypeInfo elaborated_type_info = getASTContext()->getTypeInfo(elaborated_qual_type);
8928 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
8929
8930 return elaborated_clang_type.DumpValue (exe_ctx,
8931 s, // Stream to dump to
8932 elaborated_format, // The format with which to display the element
8933 data, // Data buffer containing all bytes for this type
8934 data_byte_offset, // Offset into "data" where to grab value from
8935 elaborated_byte_size, // Size of this type in bytes
8936 bitfield_bit_size, // Bitfield bit size
8937 bitfield_bit_offset,// Bitfield bit offset
8938 show_types, // Boolean indicating if we should show the variable types
8939 show_summary, // Boolean indicating if we should show a summary for the current type
8940 verbose, // Verbose output?
8941 depth); // Scope depth for any types that have children
8942 }
8943 break;
8944
8945 case clang::Type::Paren:
8946 {
8947 clang::QualType desugar_qual_type = llvm::cast<clang::ParenType>(qual_type)->desugar();
Greg Claytona1e5dc82015-08-11 22:53:00 +00008948 CompilerType desugar_clang_type (getASTContext(), desugar_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00008949
8950 lldb::Format desugar_format = desugar_clang_type.GetFormat();
8951 clang::TypeInfo desugar_type_info = getASTContext()->getTypeInfo(desugar_qual_type);
8952 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
8953
8954 return desugar_clang_type.DumpValue (exe_ctx,
8955 s, // Stream to dump to
8956 desugar_format, // The format with which to display the element
8957 data, // Data buffer containing all bytes for this type
8958 data_byte_offset, // Offset into "data" where to grab value from
8959 desugar_byte_size, // Size of this type in bytes
8960 bitfield_bit_size, // Bitfield bit size
8961 bitfield_bit_offset,// Bitfield bit offset
8962 show_types, // Boolean indicating if we should show the variable types
8963 show_summary, // Boolean indicating if we should show a summary for the current type
8964 verbose, // Verbose output?
8965 depth); // Scope depth for any types that have children
8966 }
8967 break;
8968
8969 default:
8970 // We are down to a scalar type that we just need to display.
8971 data.Dump(s,
8972 data_byte_offset,
8973 format,
8974 data_byte_size,
8975 1,
8976 UINT32_MAX,
8977 LLDB_INVALID_ADDRESS,
8978 bitfield_bit_size,
8979 bitfield_bit_offset);
8980
8981 if (show_summary)
8982 DumpSummary (type, exe_ctx, s, data, data_byte_offset, data_byte_size);
8983 break;
8984 }
8985}
8986
8987
8988
8989
8990bool
Bruce Mitchener48ea9002015-09-23 00:18:24 +00008991ClangASTContext::DumpTypeValue (lldb::opaque_compiler_type_t type, Stream *s,
Greg Claytond8d4a572015-08-11 21:38:15 +00008992 lldb::Format format,
8993 const lldb_private::DataExtractor &data,
8994 lldb::offset_t byte_offset,
8995 size_t byte_size,
8996 uint32_t bitfield_bit_size,
8997 uint32_t bitfield_bit_offset,
8998 ExecutionContextScope *exe_scope)
8999{
9000 if (!type)
9001 return false;
9002 if (IsAggregateType(type))
9003 {
9004 return false;
9005 }
9006 else
9007 {
9008 clang::QualType qual_type(GetQualType(type));
9009
9010 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9011 switch (type_class)
9012 {
9013 case clang::Type::Typedef:
9014 {
9015 clang::QualType typedef_qual_type = llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getUnderlyingType();
Greg Claytona1e5dc82015-08-11 22:53:00 +00009016 CompilerType typedef_clang_type (getASTContext(), typedef_qual_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00009017 if (format == eFormatDefault)
9018 format = typedef_clang_type.GetFormat();
9019 clang::TypeInfo typedef_type_info = getASTContext()->getTypeInfo(typedef_qual_type);
9020 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9021
9022 return typedef_clang_type.DumpTypeValue (s,
9023 format, // The format with which to display the element
9024 data, // Data buffer containing all bytes for this type
9025 byte_offset, // Offset into "data" where to grab value from
9026 typedef_byte_size, // Size of this type in bytes
9027 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't treat as a bitfield
9028 bitfield_bit_offset, // Offset in bits of a bitfield value if bitfield_bit_size != 0
9029 exe_scope);
9030 }
9031 break;
9032
9033 case clang::Type::Enum:
9034 // If our format is enum or default, show the enumeration value as
9035 // its enumeration string value, else just display it as requested.
9036 if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType(type))
9037 {
9038 const clang::EnumType *enutype = llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9039 const clang::EnumDecl *enum_decl = enutype->getDecl();
9040 assert(enum_decl);
9041 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9042 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9043 lldb::offset_t offset = byte_offset;
9044 if (is_signed)
9045 {
9046 const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9047 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
9048 {
9049 if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
9050 {
9051 s->PutCString (enum_pos->getNameAsString().c_str());
9052 return true;
9053 }
9054 }
9055 // If we have gotten here we didn't get find the enumerator in the
9056 // enum decl, so just print the integer.
9057 s->Printf("%" PRIi64, enum_svalue);
9058 }
9059 else
9060 {
9061 const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9062 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
9063 {
9064 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
9065 {
9066 s->PutCString (enum_pos->getNameAsString().c_str());
9067 return true;
9068 }
9069 }
9070 // If we have gotten here we didn't get find the enumerator in the
9071 // enum decl, so just print the integer.
9072 s->Printf("%" PRIu64, enum_uvalue);
9073 }
9074 return true;
9075 }
9076 // format was not enum, just fall through and dump the value as requested....
Jason Molenda62e06812016-02-16 04:14:33 +00009077 LLVM_FALLTHROUGH;
Greg Claytond8d4a572015-08-11 21:38:15 +00009078
9079 default:
9080 // We are down to a scalar type that we just need to display.
9081 {
9082 uint32_t item_count = 1;
9083 // A few formats, we might need to modify our size and count for depending
9084 // on how we are trying to display the value...
9085 switch (format)
9086 {
9087 default:
9088 case eFormatBoolean:
9089 case eFormatBinary:
9090 case eFormatComplex:
9091 case eFormatCString: // NULL terminated C strings
9092 case eFormatDecimal:
9093 case eFormatEnum:
9094 case eFormatHex:
9095 case eFormatHexUppercase:
9096 case eFormatFloat:
9097 case eFormatOctal:
9098 case eFormatOSType:
9099 case eFormatUnsigned:
9100 case eFormatPointer:
9101 case eFormatVectorOfChar:
9102 case eFormatVectorOfSInt8:
9103 case eFormatVectorOfUInt8:
9104 case eFormatVectorOfSInt16:
9105 case eFormatVectorOfUInt16:
9106 case eFormatVectorOfSInt32:
9107 case eFormatVectorOfUInt32:
9108 case eFormatVectorOfSInt64:
9109 case eFormatVectorOfUInt64:
9110 case eFormatVectorOfFloat32:
9111 case eFormatVectorOfFloat64:
9112 case eFormatVectorOfUInt128:
9113 break;
9114
9115 case eFormatChar:
9116 case eFormatCharPrintable:
9117 case eFormatCharArray:
9118 case eFormatBytes:
9119 case eFormatBytesWithASCII:
9120 item_count = byte_size;
9121 byte_size = 1;
9122 break;
9123
9124 case eFormatUnicode16:
9125 item_count = byte_size / 2;
9126 byte_size = 2;
9127 break;
9128
9129 case eFormatUnicode32:
9130 item_count = byte_size / 4;
9131 byte_size = 4;
9132 break;
9133 }
9134 return data.Dump (s,
9135 byte_offset,
9136 format,
9137 byte_size,
9138 item_count,
9139 UINT32_MAX,
9140 LLDB_INVALID_ADDRESS,
9141 bitfield_bit_size,
9142 bitfield_bit_offset,
9143 exe_scope);
9144 }
9145 break;
9146 }
9147 }
9148 return 0;
9149}
9150
9151
9152
9153void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009154ClangASTContext::DumpSummary (lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
Greg Claytond8d4a572015-08-11 21:38:15 +00009155 Stream *s,
9156 const lldb_private::DataExtractor &data,
9157 lldb::offset_t data_byte_offset,
9158 size_t data_byte_size)
9159{
9160 uint32_t length = 0;
9161 if (IsCStringType (type, length))
9162 {
9163 if (exe_ctx)
9164 {
9165 Process *process = exe_ctx->GetProcessPtr();
9166 if (process)
9167 {
9168 lldb::offset_t offset = data_byte_offset;
9169 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9170 std::vector<uint8_t> buf;
9171 if (length > 0)
9172 buf.resize (length);
9173 else
9174 buf.resize (256);
9175
9176 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
9177 buf.back() = '\0';
9178 size_t bytes_read;
9179 size_t total_cstr_len = 0;
9180 Error error;
9181 while ((bytes_read = process->ReadMemory (pointer_address, &buf.front(), buf.size(), error)) > 0)
9182 {
9183 const size_t len = strlen((const char *)&buf.front());
9184 if (len == 0)
9185 break;
9186 if (total_cstr_len == 0)
9187 s->PutCString (" \"");
9188 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9189 total_cstr_len += len;
9190 if (len < buf.size())
9191 break;
9192 pointer_address += total_cstr_len;
9193 }
9194 if (total_cstr_len > 0)
9195 s->PutChar ('"');
9196 }
9197 }
9198 }
9199}
9200
9201void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009202ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type)
Greg Claytond8d4a572015-08-11 21:38:15 +00009203{
9204 StreamFile s (stdout, false);
Enrico Granatac3ef0ed2015-10-14 22:44:50 +00009205 DumpTypeDescription (type, &s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009206 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (getASTContext(), type);
9207 if (metadata)
9208 {
9209 metadata->Dump (&s);
9210 }
9211}
9212
9213void
Bruce Mitchener48ea9002015-09-23 00:18:24 +00009214ClangASTContext::DumpTypeDescription (lldb::opaque_compiler_type_t type, Stream *s)
Greg Claytond8d4a572015-08-11 21:38:15 +00009215{
9216 if (type)
9217 {
9218 clang::QualType qual_type(GetQualType(type));
9219
9220 llvm::SmallVector<char, 1024> buf;
9221 llvm::raw_svector_ostream llvm_ostrm (buf);
9222
9223 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9224 switch (type_class)
9225 {
9226 case clang::Type::ObjCObject:
9227 case clang::Type::ObjCInterface:
9228 {
9229 GetCompleteType(type);
9230
9231 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9232 assert (objc_class_type);
9233 if (objc_class_type)
9234 {
9235 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9236 if (class_interface_decl)
9237 {
9238 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9239 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9240 }
9241 }
9242 }
9243 break;
9244
9245 case clang::Type::Typedef:
9246 {
9247 const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
9248 if (typedef_type)
9249 {
9250 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9251 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
9252 if (!clang_typedef_name.empty())
9253 {
9254 s->PutCString ("typedef ");
9255 s->PutCString (clang_typedef_name.c_str());
9256 }
9257 }
9258 }
9259 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00009260
9261 case clang::Type::Auto:
9262 CompilerType (getASTContext(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType()).DumpTypeDescription(s);
9263 return;
Greg Claytond8d4a572015-08-11 21:38:15 +00009264
9265 case clang::Type::Elaborated:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009266 CompilerType (getASTContext(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009267 return;
9268
9269 case clang::Type::Paren:
Greg Claytona1e5dc82015-08-11 22:53:00 +00009270 CompilerType (getASTContext(), llvm::cast<clang::ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
Greg Claytond8d4a572015-08-11 21:38:15 +00009271 return;
9272
9273 case clang::Type::Record:
9274 {
9275 GetCompleteType(type);
9276
9277 const clang::RecordType *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9278 const clang::RecordDecl *record_decl = record_type->getDecl();
9279 const clang::CXXRecordDecl *cxx_record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9280
9281 if (cxx_record_decl)
9282 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9283 else
9284 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), s->GetIndentLevel());
9285 }
9286 break;
9287
9288 default:
9289 {
9290 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9291 if (tag_type)
9292 {
9293 clang::TagDecl *tag_decl = tag_type->getDecl();
9294 if (tag_decl)
9295 tag_decl->print(llvm_ostrm, 0);
9296 }
9297 else
9298 {
9299 std::string clang_type_name(qual_type.getAsString());
9300 if (!clang_type_name.empty())
9301 s->PutCString (clang_type_name.c_str());
9302 }
9303 }
9304 }
9305
Greg Claytond8d4a572015-08-11 21:38:15 +00009306 if (buf.size() > 0)
9307 {
9308 s->Write (buf.data(), buf.size());
9309 }
9310 }
9311}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009312
Greg Claytone6b36cd2015-12-08 01:02:08 +00009313void
9314ClangASTContext::DumpTypeName (const CompilerType &type)
9315{
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009316 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00009317 {
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009318 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
Greg Claytone6b36cd2015-12-08 01:02:08 +00009319
9320 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9321 switch (type_class)
9322 {
9323 case clang::Type::Record:
9324 {
9325 const clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
9326 if (cxx_record_decl)
9327 printf("class %s", cxx_record_decl->getName().str().c_str());
9328 }
9329 break;
9330
9331 case clang::Type::Enum:
9332 {
9333 clang::EnumDecl *enum_decl = llvm::cast<clang::EnumType>(qual_type)->getDecl();
9334 if (enum_decl)
9335 {
9336 printf("enum %s", enum_decl->getName().str().c_str());
9337 }
9338 }
9339 break;
9340
9341 case clang::Type::ObjCObject:
9342 case clang::Type::ObjCInterface:
9343 {
9344 const clang::ObjCObjectType *objc_class_type = llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9345 if (objc_class_type)
9346 {
9347 clang::ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
9348 // We currently can't complete objective C types through the newly added ASTContext
9349 // because it only supports TagDecl objects right now...
9350 if (class_interface_decl)
9351 printf("@class %s", class_interface_decl->getName().str().c_str());
9352 }
9353 }
9354 break;
9355
9356
9357 case clang::Type::Typedef:
9358 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)->getDecl()->getName().str().c_str());
9359 break;
9360
Enrico Granata36f51e42015-12-18 22:41:25 +00009361 case clang::Type::Auto:
9362 printf("auto ");
9363 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)->getDeducedType().getAsOpaquePtr()));
9364
Greg Claytone6b36cd2015-12-08 01:02:08 +00009365 case clang::Type::Elaborated:
9366 printf("elaborated ");
9367 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType().getAsOpaquePtr()));
9368
9369 case clang::Type::Paren:
9370 printf("paren ");
9371 return DumpTypeName (CompilerType (type.GetTypeSystem(), llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9372
9373 default:
9374 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9375 break;
9376 }
9377 }
9378
9379}
9380
9381
9382
Greg Clayton8b4edba2015-08-14 20:02:05 +00009383clang::ClassTemplateDecl *
Greg Clayton6071e6f2015-08-26 22:57:51 +00009384ClangASTContext::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009385 lldb::AccessType access_type,
9386 const char *parent_name,
9387 int tag_decl_kind,
9388 const ClangASTContext::TemplateParameterInfos &template_param_infos)
9389{
9390 if (template_param_infos.IsValid())
9391 {
9392 std::string template_basename(parent_name);
9393 template_basename.erase (template_basename.find('<'));
9394
9395 return CreateClassTemplateDecl (decl_ctx,
9396 access_type,
9397 template_basename.c_str(),
9398 tag_decl_kind,
9399 template_param_infos);
9400 }
9401 return NULL;
9402}
9403
Greg Clayton6dc8d582015-08-18 22:32:36 +00009404void
9405ClangASTContext::CompleteTagDecl (void *baton, clang::TagDecl *decl)
9406{
9407 ClangASTContext *ast = (ClangASTContext *)baton;
9408 SymbolFile *sym_file = ast->GetSymbolFile();
9409 if (sym_file)
9410 {
9411 CompilerType clang_type = GetTypeForDecl (decl);
9412 if (clang_type)
9413 sym_file->CompleteType (clang_type);
9414 }
9415}
9416
9417void
9418ClangASTContext::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
9419{
9420 ClangASTContext *ast = (ClangASTContext *)baton;
9421 SymbolFile *sym_file = ast->GetSymbolFile();
9422 if (sym_file)
9423 {
9424 CompilerType clang_type = GetTypeForDecl (decl);
9425 if (clang_type)
9426 sym_file->CompleteType (clang_type);
9427 }
9428}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009429
Greg Clayton261ac3f2015-08-28 01:01:03 +00009430DWARFASTParser *
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009431ClangASTContext::GetDWARFParser()
Greg Clayton261ac3f2015-08-28 01:01:03 +00009432{
9433 if (!m_dwarf_ast_parser_ap)
9434 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9435 return m_dwarf_ast_parser_ap.get();
9436}
9437
9438
Greg Clayton8b4edba2015-08-14 20:02:05 +00009439bool
Greg Clayton6dc8d582015-08-18 22:32:36 +00009440ClangASTContext::LayoutRecordType(void *baton,
Greg Clayton8b4edba2015-08-14 20:02:05 +00009441 const clang::RecordDecl *record_decl,
9442 uint64_t &bit_size,
9443 uint64_t &alignment,
9444 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9445 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
9446 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
9447{
Greg Clayton6dc8d582015-08-18 22:32:36 +00009448 ClangASTContext *ast = (ClangASTContext *)baton;
Greg Clayton261ac3f2015-08-28 01:01:03 +00009449 DWARFASTParserClang *dwarf_ast_parser = (DWARFASTParserClang *)ast->GetDWARFParser();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00009450 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(record_decl, bit_size, alignment, field_offsets,
9451 base_offsets, vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009452}
9453
Greg Clayton99558cc42015-08-24 23:46:31 +00009454//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009455// CompilerDecl override functions
9456//----------------------------------------------------------------------
9457lldb::VariableSP
9458ClangASTContext::DeclGetVariable (void *opaque_decl)
9459{
9460 if (llvm::dyn_cast<clang::VarDecl>((clang::Decl *)opaque_decl))
9461 {
9462 auto decl_search_it = m_decl_objects.find(opaque_decl);
9463 if (decl_search_it != m_decl_objects.end())
9464 return std::static_pointer_cast<Variable>(decl_search_it->second);
9465 }
9466 return VariableSP();
9467}
9468
9469void
9470ClangASTContext::DeclLinkToObject (void *opaque_decl, std::shared_ptr<void> object)
9471{
9472 if (m_decl_objects.find(opaque_decl) == m_decl_objects.end())
9473 m_decl_objects.insert(std::make_pair(opaque_decl, object));
9474}
9475
9476ConstString
9477ClangASTContext::DeclGetName (void *opaque_decl)
9478{
9479 if (opaque_decl)
9480 {
9481 clang::NamedDecl *nd = llvm::dyn_cast<NamedDecl>((clang::Decl*)opaque_decl);
9482 if (nd != nullptr)
Greg Claytonfe689042015-11-10 17:47:04 +00009483 return ConstString(nd->getDeclName().getAsString());
Paul Hermand628cbb2015-09-15 23:44:17 +00009484 }
9485 return ConstString();
9486}
9487
Greg Claytonfe689042015-11-10 17:47:04 +00009488ConstString
9489ClangASTContext::DeclGetMangledName (void *opaque_decl)
9490{
9491 if (opaque_decl)
9492 {
9493 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>((clang::Decl*)opaque_decl);
9494 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd))
9495 {
9496 clang::MangleContext *mc = getMangleContext();
9497 if (mc && mc->shouldMangleCXXName(nd))
9498 {
9499 llvm::SmallVector<char, 1024> buf;
9500 llvm::raw_svector_ostream llvm_ostrm (buf);
9501 if (llvm::isa<clang::CXXConstructorDecl>(nd))
9502 {
9503 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), Ctor_Complete, llvm_ostrm);
9504 }
9505 else if (llvm::isa<clang::CXXDestructorDecl>(nd))
9506 {
9507 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), Dtor_Complete, llvm_ostrm);
9508 }
9509 else
9510 {
9511 mc->mangleName(nd, llvm_ostrm);
9512 }
9513 if (buf.size() > 0)
9514 return ConstString(buf.data(), buf.size());
9515 }
9516 }
9517 }
9518 return ConstString();
9519}
9520
9521CompilerDeclContext
9522ClangASTContext::DeclGetDeclContext (void *opaque_decl)
9523{
9524 if (opaque_decl)
9525 return CompilerDeclContext(this, ((clang::Decl*)opaque_decl)->getDeclContext());
9526 else
9527 return CompilerDeclContext();
9528}
9529
9530CompilerType
9531ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl)
9532{
9533 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9534 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9535 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9536 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9537 else
9538 return CompilerType();
9539}
9540
9541size_t
9542ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl)
9543{
9544 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9545 return func_decl->param_size();
9546 if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
Zachary Turner9d8a97e2016-04-01 23:20:35 +00009547 return objc_method->param_size();
Greg Claytonfe689042015-11-10 17:47:04 +00009548 else
9549 return 0;
9550}
9551
9552CompilerType
9553ClangASTContext::DeclGetFunctionArgumentType (void *opaque_decl, size_t idx)
9554{
9555 if (clang::FunctionDecl *func_decl = llvm::dyn_cast<clang::FunctionDecl>((clang::Decl*)opaque_decl))
9556 {
9557 if (idx < func_decl->param_size())
9558 {
9559 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9560 if (var_decl)
Zachary Turner9d8a97e2016-04-01 23:20:35 +00009561 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
Greg Claytonfe689042015-11-10 17:47:04 +00009562 }
9563 }
9564 else if (clang::ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl*)opaque_decl))
9565 {
9566 if (idx < objc_method->param_size())
9567 return CompilerType(this, objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9568 }
9569 return CompilerType();
9570}
9571
Paul Hermand628cbb2015-09-15 23:44:17 +00009572//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009573// CompilerDeclContext functions
9574//----------------------------------------------------------------------
9575
Greg Claytondfc09622015-12-08 18:39:50 +00009576std::vector<CompilerDecl>
Siva Chandra03ff5c82016-02-05 19:10:04 +00009577ClangASTContext::DeclContextFindDeclByName(void *opaque_decl_ctx,
9578 ConstString name,
9579 const bool ignore_using_decls)
Paul Hermand628cbb2015-09-15 23:44:17 +00009580{
Greg Claytondfc09622015-12-08 18:39:50 +00009581 std::vector<CompilerDecl> found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009582 if (opaque_decl_ctx)
9583 {
9584 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9585 std::set<DeclContext *> searched;
9586 std::multimap<DeclContext *, DeclContext *> search_queue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009587 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009588
9589 for (clang::DeclContext *decl_context = root_decl_ctx; decl_context != nullptr && found_decls.empty(); decl_context = decl_context->getParent())
9590 {
9591 search_queue.insert(std::make_pair(decl_context, decl_context));
9592
9593 for (auto it = search_queue.find(decl_context); it != search_queue.end(); it++)
9594 {
Eugene Leviantc1ba9fc2015-11-13 11:00:10 +00009595 if (!searched.insert(it->second).second)
9596 continue;
Paul Hermanea188fc2015-09-16 18:48:30 +00009597 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9598
Paul Hermand628cbb2015-09-15 23:44:17 +00009599 for (clang::Decl *child : it->second->decls())
9600 {
Paul Hermanea188fc2015-09-16 18:48:30 +00009601 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
Paul Hermand628cbb2015-09-15 23:44:17 +00009602 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00009603 if (ignore_using_decls)
9604 continue;
Paul Hermand628cbb2015-09-15 23:44:17 +00009605 clang::DeclContext *from = ud->getCommonAncestor();
9606 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9607 search_queue.insert(std::make_pair(from, ud->getNominatedNamespace()));
9608 }
9609 else if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9610 {
Siva Chandra03ff5c82016-02-05 19:10:04 +00009611 if (ignore_using_decls)
9612 continue;
Paul Hermand628cbb2015-09-15 23:44:17 +00009613 for (clang::UsingShadowDecl *usd : ud->shadows())
9614 {
9615 clang::Decl *target = usd->getTargetDecl();
9616 if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target))
9617 {
9618 IdentifierInfo *ii = nd->getIdentifier();
9619 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
Greg Claytondfc09622015-12-08 18:39:50 +00009620 found_decls.push_back(CompilerDecl(this, nd));
Paul Hermand628cbb2015-09-15 23:44:17 +00009621 }
9622 }
9623 }
Paul Hermanea188fc2015-09-16 18:48:30 +00009624 else if (clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(child))
9625 {
9626 IdentifierInfo *ii = nd->getIdentifier();
9627 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
Greg Claytondfc09622015-12-08 18:39:50 +00009628 found_decls.push_back(CompilerDecl(this, nd));
Paul Hermanea188fc2015-09-16 18:48:30 +00009629 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009630 }
9631 }
9632 }
9633 }
9634 return found_decls;
9635}
9636
Dawn Perchikb5925782015-12-12 19:31:41 +00009637// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9638// and return the number of levels it took to find it, or LLDB_INVALID_DECL_LEVEL
9639// if not found. If the decl was imported via a using declaration, its name and/or
9640// type, if set, will be used to check that the decl found in the scope is a match.
9641//
9642// The optional name is required by languages (like C++) to handle using declarations
9643// like:
9644//
9645// void poo();
9646// namespace ns {
9647// void foo();
9648// void goo();
9649// }
9650// void bar() {
9651// using ns::foo;
9652// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9653// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9654// }
9655//
9656// The optional type is useful in the case that there's a specific overload
9657// that we're looking for that might otherwise be shadowed, like:
9658//
9659// void foo(int);
9660// namespace ns {
9661// void foo();
9662// }
9663// void bar() {
9664// using ns::foo;
9665// // CountDeclLevels returns 0 for { 'foo', void() },
9666// // 1 for { 'foo', void(int) }, and
9667// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9668// }
9669//
9670// NOTE: Because file statics are at the TranslationUnit along with globals, a
9671// function at file scope will return the same level as a function at global scope.
9672// Ideally we'd like to treat the file scope as an additional scope just below the
9673// global scope. More work needs to be done to recognise that, if the decl we're
9674// trying to look up is static, we should compare its source file with that of the
9675// current scope and return a lower number for it.
9676uint32_t
9677ClangASTContext::CountDeclLevels (clang::DeclContext *frame_decl_ctx,
9678 clang::DeclContext *child_decl_ctx,
9679 ConstString *child_name,
9680 CompilerType *child_type)
9681{
9682 if (frame_decl_ctx)
9683 {
9684 std::set<DeclContext *> searched;
9685 std::multimap<DeclContext *, DeclContext *> search_queue;
9686 SymbolFile *symbol_file = GetSymbolFile();
9687
9688 // Get the lookup scope for the decl we're trying to find.
9689 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9690
9691 // Look for it in our scope's decl context and its parents.
9692 uint32_t level = 0;
9693 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; decl_ctx = decl_ctx->getParent())
9694 {
9695 if (!decl_ctx->isLookupContext())
9696 continue;
9697 if (decl_ctx == parent_decl_ctx)
9698 // Found it!
9699 return level;
9700 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9701 for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); it++)
9702 {
9703 if (searched.find(it->second) != searched.end())
9704 continue;
Sean Callanan8c05fb92016-02-12 21:55:05 +00009705
9706 // Currently DWARF has one shared translation unit for all Decls at top level, so this
9707 // would erroneously find using statements anywhere. So don't look at the top-level
9708 // translation unit.
9709 // TODO fix this and add a testcase that depends on it.
9710
9711 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9712 continue;
9713
Dawn Perchikb5925782015-12-12 19:31:41 +00009714 searched.insert(it->second);
9715 symbol_file->ParseDeclsForContext(CompilerDeclContext(this, it->second));
9716
9717 for (clang::Decl *child : it->second->decls())
9718 {
9719 if (clang::UsingDirectiveDecl *ud = llvm::dyn_cast<clang::UsingDirectiveDecl>(child))
9720 {
9721 clang::DeclContext *ns = ud->getNominatedNamespace();
9722 if (ns == parent_decl_ctx)
9723 // Found it!
9724 return level;
9725 clang::DeclContext *from = ud->getCommonAncestor();
9726 if (searched.find(ns) == searched.end())
9727 search_queue.insert(std::make_pair(from, ns));
9728 }
9729 else if (child_name)
9730 {
9731 if (clang::UsingDecl *ud = llvm::dyn_cast<clang::UsingDecl>(child))
9732 {
9733 for (clang::UsingShadowDecl *usd : ud->shadows())
9734 {
9735 clang::Decl *target = usd->getTargetDecl();
9736 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9737 if (!nd)
9738 continue;
9739 // Check names.
9740 IdentifierInfo *ii = nd->getIdentifier();
9741 if (ii == nullptr || !ii->getName().equals(child_name->AsCString(nullptr)))
9742 continue;
9743 // Check types, if one was provided.
9744 if (child_type)
9745 {
9746 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9747 if (!AreTypesSame(clang_type, *child_type, /*ignore_qualifiers=*/true))
9748 continue;
9749 }
9750 // Found it!
9751 return level;
9752 }
9753 }
9754 }
9755 }
9756 }
9757 ++level;
9758 }
9759 }
9760 return LLDB_INVALID_DECL_LEVEL;
9761}
9762
Greg Clayton99558cc42015-08-24 23:46:31 +00009763bool
9764ClangASTContext::DeclContextIsStructUnionOrClass (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009765{
Greg Clayton99558cc42015-08-24 23:46:31 +00009766 if (opaque_decl_ctx)
9767 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9768 else
9769 return false;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009770}
9771
Greg Clayton99558cc42015-08-24 23:46:31 +00009772ConstString
9773ClangASTContext::DeclContextGetName (void *opaque_decl_ctx)
Greg Clayton8b4edba2015-08-14 20:02:05 +00009774{
Greg Clayton99558cc42015-08-24 23:46:31 +00009775 if (opaque_decl_ctx)
9776 {
9777 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9778 if (named_decl)
9779 return ConstString(named_decl->getName());
9780 }
9781 return ConstString();
9782}
9783
Siva Chandra9293fc42016-01-07 23:32:34 +00009784ConstString
9785ClangASTContext::DeclContextGetScopeQualifiedName (void *opaque_decl_ctx)
9786{
9787 if (opaque_decl_ctx)
9788 {
9789 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9790 if (named_decl)
9791 return ConstString(llvm::StringRef(named_decl->getQualifiedNameAsString()));
9792 }
9793 return ConstString();
9794}
9795
Greg Clayton99558cc42015-08-24 23:46:31 +00009796bool
9797ClangASTContext::DeclContextIsClassMethod (void *opaque_decl_ctx,
9798 lldb::LanguageType *language_ptr,
9799 bool *is_instance_method_ptr,
9800 ConstString *language_object_name_ptr)
9801{
9802 if (opaque_decl_ctx)
9803 {
9804 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9805 if (ObjCMethodDecl *objc_method = llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx))
9806 {
9807 if (is_instance_method_ptr)
9808 *is_instance_method_ptr = objc_method->isInstanceMethod();
9809 if (language_ptr)
9810 *language_ptr = eLanguageTypeObjC;
9811 if (language_object_name_ptr)
9812 language_object_name_ptr->SetCString("self");
9813 return true;
9814 }
9815 else if (CXXMethodDecl *cxx_method = llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx))
9816 {
9817 if (is_instance_method_ptr)
9818 *is_instance_method_ptr = cxx_method->isInstance();
9819 if (language_ptr)
9820 *language_ptr = eLanguageTypeC_plus_plus;
9821 if (language_object_name_ptr)
9822 language_object_name_ptr->SetCString("this");
9823 return true;
9824 }
9825 else if (clang::FunctionDecl *function_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx))
9826 {
9827 ClangASTMetadata *metadata = GetMetadata (&decl_ctx->getParentASTContext(), function_decl);
9828 if (metadata && metadata->HasObjectPtr())
9829 {
9830 if (is_instance_method_ptr)
9831 *is_instance_method_ptr = true;
9832 if (language_ptr)
9833 *language_ptr = eLanguageTypeObjC;
9834 if (language_object_name_ptr)
9835 language_object_name_ptr->SetCString (metadata->GetObjectPtrName());
9836 return true;
9837 }
9838 }
9839 }
9840 return false;
9841}
9842
9843clang::DeclContext *
9844ClangASTContext::DeclContextGetAsDeclContext (const CompilerDeclContext &dc)
9845{
9846 if (dc.IsClang())
9847 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9848 return nullptr;
9849}
9850
9851
9852ObjCMethodDecl *
9853ClangASTContext::DeclContextGetAsObjCMethodDecl (const CompilerDeclContext &dc)
9854{
9855 if (dc.IsClang())
9856 return llvm::dyn_cast<clang::ObjCMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9857 return nullptr;
9858}
9859
9860CXXMethodDecl *
9861ClangASTContext::DeclContextGetAsCXXMethodDecl (const CompilerDeclContext &dc)
9862{
9863 if (dc.IsClang())
9864 return llvm::dyn_cast<clang::CXXMethodDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9865 return nullptr;
9866}
9867
9868clang::FunctionDecl *
9869ClangASTContext::DeclContextGetAsFunctionDecl (const CompilerDeclContext &dc)
9870{
9871 if (dc.IsClang())
9872 return llvm::dyn_cast<clang::FunctionDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9873 return nullptr;
9874}
9875
9876clang::NamespaceDecl *
9877ClangASTContext::DeclContextGetAsNamespaceDecl (const CompilerDeclContext &dc)
9878{
9879 if (dc.IsClang())
9880 return llvm::dyn_cast<clang::NamespaceDecl>((clang::DeclContext *)dc.GetOpaqueDeclContext());
9881 return nullptr;
9882}
9883
9884ClangASTMetadata *
9885ClangASTContext::DeclContextGetMetaData (const CompilerDeclContext &dc, const void *object)
9886{
9887 clang::ASTContext *ast = DeclContextGetClangASTContext (dc);
9888 if (ast)
9889 return ClangASTContext::GetMetadata (ast, object);
9890 return nullptr;
9891}
9892
9893clang::ASTContext *
9894ClangASTContext::DeclContextGetClangASTContext (const CompilerDeclContext &dc)
9895{
Greg Claytonf73034f2015-09-08 18:15:05 +00009896 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
9897 if (ast)
9898 return ast->getASTContext();
Greg Clayton99558cc42015-08-24 23:46:31 +00009899 return nullptr;
Greg Clayton8b4edba2015-08-14 20:02:05 +00009900}
9901
Jim Ingham151c0322015-09-15 21:13:50 +00009902ClangASTContextForExpressions::ClangASTContextForExpressions (Target &target) :
9903 ClangASTContext (target.GetArchitecture().GetTriple().getTriple().c_str()),
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009904 m_target_wp(target.shared_from_this()),
9905 m_persistent_variables (new ClangPersistentVariables)
Jim Ingham151c0322015-09-15 21:13:50 +00009906{
9907}
9908
9909UserExpression *
9910ClangASTContextForExpressions::GetUserExpression (const char *expr,
9911 const char *expr_prefix,
9912 lldb::LanguageType language,
Jim Ingham19a63fc2015-11-03 02:11:24 +00009913 Expression::ResultType desired_type,
9914 const EvaluateExpressionOptions &options)
Jim Ingham151c0322015-09-15 21:13:50 +00009915{
9916 TargetSP target_sp = m_target_wp.lock();
9917 if (!target_sp)
9918 return nullptr;
9919
Jim Ingham19a63fc2015-11-03 02:11:24 +00009920 return new ClangUserExpression(*target_sp.get(), expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00009921}
9922
9923FunctionCaller *
9924ClangASTContextForExpressions::GetFunctionCaller (const CompilerType &return_type,
9925 const Address& function_address,
9926 const ValueList &arg_value_list,
9927 const char *name)
9928{
9929 TargetSP target_sp = m_target_wp.lock();
9930 if (!target_sp)
9931 return nullptr;
9932
9933 Process *process = target_sp->GetProcessSP().get();
9934 if (!process)
9935 return nullptr;
9936
9937 return new ClangFunctionCaller (*process, return_type, function_address, arg_value_list, name);
9938}
9939
9940UtilityFunction *
9941ClangASTContextForExpressions::GetUtilityFunction (const char *text,
9942 const char *name)
9943{
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009944 TargetSP target_sp = m_target_wp.lock();
Jim Ingham151c0322015-09-15 21:13:50 +00009945 if (!target_sp)
9946 return nullptr;
9947
9948 return new ClangUtilityFunction(*target_sp.get(), text, name);
9949}
Sean Callanan8f1f9a12015-09-30 19:57:57 +00009950
9951PersistentExpressionState *
9952ClangASTContextForExpressions::GetPersistentExpressionState ()
9953{
9954 return m_persistent_variables.get();
9955}