1 //===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/ClangASTContext.h"
12 #include "llvm/Support/FormatAdapters.h"
13 #include "llvm/Support/FormatVariadic.h"
17 #include <mutex> // std::once
21 // Other libraries and framework includes
23 // Clang headers like to use NDEBUG inside of them to enable/disable debug
24 // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
25 // or another. This is bad because it means that if clang was built in release
26 // mode, it assumes that you are building in release mode which is not always
27 // the case. You can end up with functions that are defined as empty in header
28 // files when NDEBUG is not defined, and this can cause link errors with the
29 // clang .a files that you have since you might be missing functions in the .a
30 // file. So we have to define NDEBUG when including clang headers to avoid any
31 // mismatches. This is covered by rdar://problem/8691220
33 #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
34 #define LLDB_DEFINED_NDEBUG_FOR_CLANG
36 // Need to include assert.h so it is as clang would expect it to be (disabled)
40 #include "clang/AST/ASTContext.h"
41 #include "clang/AST/ASTImporter.h"
42 #include "clang/AST/Attr.h"
43 #include "clang/AST/CXXInheritance.h"
44 #include "clang/AST/DeclObjC.h"
45 #include "clang/AST/DeclTemplate.h"
46 #include "clang/AST/Mangle.h"
47 #include "clang/AST/RecordLayout.h"
48 #include "clang/AST/Type.h"
49 #include "clang/AST/VTableBuilder.h"
50 #include "clang/Basic/Builtins.h"
51 #include "clang/Basic/Diagnostic.h"
52 #include "clang/Basic/FileManager.h"
53 #include "clang/Basic/FileSystemOptions.h"
54 #include "clang/Basic/SourceManager.h"
55 #include "clang/Basic/TargetInfo.h"
56 #include "clang/Basic/TargetOptions.h"
57 #include "clang/Frontend/FrontendOptions.h"
58 #include "clang/Frontend/LangStandard.h"
60 #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
62 #undef LLDB_DEFINED_NDEBUG_FOR_CLANG
63 // Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
67 #include "llvm/Support/Signals.h"
69 #include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
70 #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
71 #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
72 #include "lldb/Core/ArchSpec.h"
73 #include "lldb/Core/Flags.h"
74 #include "lldb/Core/Log.h"
75 #include "lldb/Core/Module.h"
76 #include "lldb/Core/PluginManager.h"
77 #include "lldb/Core/RegularExpression.h"
78 #include "lldb/Core/Scalar.h"
79 #include "lldb/Core/StreamFile.h"
80 #include "lldb/Core/ThreadSafeDenseMap.h"
81 #include "lldb/Core/UniqueCStringMap.h"
82 #include "lldb/Symbol/ClangASTContext.h"
83 #include "lldb/Symbol/ClangASTImporter.h"
84 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
85 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
86 #include "lldb/Symbol/ClangUtil.h"
87 #include "lldb/Symbol/ObjectFile.h"
88 #include "lldb/Symbol/SymbolFile.h"
89 #include "lldb/Symbol/VerifyDecl.h"
90 #include "lldb/Target/ExecutionContext.h"
91 #include "lldb/Target/Language.h"
92 #include "lldb/Target/ObjCLanguageRuntime.h"
93 #include "lldb/Target/Process.h"
94 #include "lldb/Target/Target.h"
95 #include "lldb/Utility/LLDBAssert.h"
97 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
98 //#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
104 using namespace lldb;
105 using namespace lldb_private;
106 using namespace llvm;
107 using namespace clang;
111 ClangASTContextSupportsLanguage(lldb::LanguageType language) {
112 return language == eLanguageTypeUnknown || // Clang is the default type system
113 Language::LanguageIsC(language) ||
114 Language::LanguageIsCPlusPlus(language) ||
115 Language::LanguageIsObjC(language) ||
116 Language::LanguageIsPascal(language) ||
117 // Use Clang for Rust until there is a proper language plugin for it
118 language == eLanguageTypeRust ||
119 language == eLanguageTypeExtRenderScript ||
120 // Use Clang for D until there is a proper language plugin for it
121 language == eLanguageTypeD;
125 typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
128 static ClangASTMap &GetASTMap() {
129 static ClangASTMap *g_map_ptr = nullptr;
130 static std::once_flag g_once_flag;
131 std::call_once(g_once_flag, []() {
132 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
137 static bool IsOperator(const char *name,
138 clang::OverloadedOperatorKind &op_kind) {
139 if (name == nullptr || name[0] == '\0')
142 #define OPERATOR_PREFIX "operator"
143 #define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
145 const char *post_op_name = nullptr;
147 bool no_space = true;
149 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
152 post_op_name = name + OPERATOR_PREFIX_LENGTH;
154 if (post_op_name[0] == ' ') {
159 #undef OPERATOR_PREFIX
160 #undef OPERATOR_PREFIX_LENGTH
162 // This is an operator, set the overloaded operator kind to invalid
163 // in case this is a conversion operator...
164 op_kind = clang::NUM_OVERLOADED_OPERATORS;
166 switch (post_op_name[0]) {
174 if (strcmp(post_op_name, "new") == 0)
175 op_kind = clang::OO_New;
176 else if (strcmp(post_op_name, "new[]") == 0)
177 op_kind = clang::OO_Array_New;
183 if (strcmp(post_op_name, "delete") == 0)
184 op_kind = clang::OO_Delete;
185 else if (strcmp(post_op_name, "delete[]") == 0)
186 op_kind = clang::OO_Array_Delete;
190 if (post_op_name[1] == '\0')
191 op_kind = clang::OO_Plus;
192 else if (post_op_name[2] == '\0') {
193 if (post_op_name[1] == '=')
194 op_kind = clang::OO_PlusEqual;
195 else if (post_op_name[1] == '+')
196 op_kind = clang::OO_PlusPlus;
201 if (post_op_name[1] == '\0')
202 op_kind = clang::OO_Minus;
203 else if (post_op_name[2] == '\0') {
204 switch (post_op_name[1]) {
206 op_kind = clang::OO_MinusEqual;
209 op_kind = clang::OO_MinusMinus;
212 op_kind = clang::OO_Arrow;
215 } else if (post_op_name[3] == '\0') {
216 if (post_op_name[2] == '*')
217 op_kind = clang::OO_ArrowStar;
223 if (post_op_name[1] == '\0')
224 op_kind = clang::OO_Star;
225 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
226 op_kind = clang::OO_StarEqual;
230 if (post_op_name[1] == '\0')
231 op_kind = clang::OO_Slash;
232 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
233 op_kind = clang::OO_SlashEqual;
237 if (post_op_name[1] == '\0')
238 op_kind = clang::OO_Percent;
239 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
240 op_kind = clang::OO_PercentEqual;
244 if (post_op_name[1] == '\0')
245 op_kind = clang::OO_Caret;
246 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
247 op_kind = clang::OO_CaretEqual;
251 if (post_op_name[1] == '\0')
252 op_kind = clang::OO_Amp;
253 else if (post_op_name[2] == '\0') {
254 switch (post_op_name[1]) {
256 op_kind = clang::OO_AmpEqual;
259 op_kind = clang::OO_AmpAmp;
266 if (post_op_name[1] == '\0')
267 op_kind = clang::OO_Pipe;
268 else if (post_op_name[2] == '\0') {
269 switch (post_op_name[1]) {
271 op_kind = clang::OO_PipeEqual;
274 op_kind = clang::OO_PipePipe;
281 if (post_op_name[1] == '\0')
282 op_kind = clang::OO_Tilde;
286 if (post_op_name[1] == '\0')
287 op_kind = clang::OO_Exclaim;
288 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
289 op_kind = clang::OO_ExclaimEqual;
293 if (post_op_name[1] == '\0')
294 op_kind = clang::OO_Equal;
295 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
296 op_kind = clang::OO_EqualEqual;
300 if (post_op_name[1] == '\0')
301 op_kind = clang::OO_Less;
302 else if (post_op_name[2] == '\0') {
303 switch (post_op_name[1]) {
305 op_kind = clang::OO_LessLess;
308 op_kind = clang::OO_LessEqual;
311 } else if (post_op_name[3] == '\0') {
312 if (post_op_name[2] == '=')
313 op_kind = clang::OO_LessLessEqual;
318 if (post_op_name[1] == '\0')
319 op_kind = clang::OO_Greater;
320 else if (post_op_name[2] == '\0') {
321 switch (post_op_name[1]) {
323 op_kind = clang::OO_GreaterGreater;
326 op_kind = clang::OO_GreaterEqual;
329 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
330 post_op_name[3] == '\0') {
331 op_kind = clang::OO_GreaterGreaterEqual;
336 if (post_op_name[1] == '\0')
337 op_kind = clang::OO_Comma;
341 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
342 op_kind = clang::OO_Call;
346 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
347 op_kind = clang::OO_Subscript;
354 clang::AccessSpecifier
355 ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
365 case eAccessProtected:
371 static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
372 // FIXME: Cleanup per-file based stuff.
374 // Set some properties which depend solely on the input kind; it would be nice
375 // to move these to the language standard, and have the driver resolve the
376 // input kind + language standard.
378 Opts.AsmPreprocessor = 1;
379 } else if (IK == IK_ObjC || IK == IK_ObjCXX || IK == IK_PreprocessedObjC ||
380 IK == IK_PreprocessedObjCXX) {
381 Opts.ObjC1 = Opts.ObjC2 = 1;
384 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
386 if (LangStd == LangStandard::lang_unspecified) {
387 // Based on the base language, pick one.
392 case IK_RenderScript:
393 llvm_unreachable("Invalid input kind!");
395 LangStd = LangStandard::lang_opencl;
398 case IK_PreprocessedCuda:
399 LangStd = LangStandard::lang_cuda;
403 case IK_PreprocessedC:
405 case IK_PreprocessedObjC:
406 LangStd = LangStandard::lang_gnu99;
409 case IK_PreprocessedCXX:
411 case IK_PreprocessedObjCXX:
412 LangStd = LangStandard::lang_gnucxx98;
417 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
418 Opts.LineComment = Std.hasLineComments();
419 Opts.C99 = Std.isC99();
420 Opts.CPlusPlus = Std.isCPlusPlus();
421 Opts.CPlusPlus11 = Std.isCPlusPlus11();
422 Opts.Digraphs = Std.hasDigraphs();
423 Opts.GNUMode = Std.isGNUMode();
424 Opts.GNUInline = !Std.isC99();
425 Opts.HexFloats = Std.hasHexFloats();
426 Opts.ImplicitInt = Std.hasImplicitInt();
430 // OpenCL has some additional defaults.
431 if (LangStd == LangStandard::lang_opencl) {
434 Opts.CXXOperatorNames = 1;
435 Opts.LaxVectorConversions = 1;
438 // OpenCL and C++ both have bool, true, false keywords.
439 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
441 // if (Opts.CPlusPlus)
442 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
444 // if (Args.hasArg(OPT_fobjc_gc_only))
445 // Opts.setGCMode(LangOptions::GCOnly);
446 // else if (Args.hasArg(OPT_fobjc_gc))
447 // Opts.setGCMode(LangOptions::HybridGC);
449 // if (Args.hasArg(OPT_print_ivar_layout))
450 // Opts.ObjCGCBitmapPrint = 1;
452 // if (Args.hasArg(OPT_faltivec))
455 // if (Args.hasArg(OPT_pthread))
456 // Opts.POSIXThreads = 1;
458 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
460 // if (Vis == "default")
461 Opts.setValueVisibilityMode(DefaultVisibility);
462 // else if (Vis == "hidden")
463 // Opts.setVisibilityMode(LangOptions::Hidden);
464 // else if (Vis == "protected")
465 // Opts.setVisibilityMode(LangOptions::Protected);
467 // Diags.Report(diag::err_drv_invalid_value)
468 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
470 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
472 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
473 // is specified, or -std is set to a conforming mode.
474 Opts.Trigraphs = !Opts.GNUMode;
475 // if (Args.hasArg(OPT_trigraphs))
476 // Opts.Trigraphs = 1;
478 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
479 // OPT_fno_dollars_in_identifiers,
480 // !Opts.AsmPreprocessor);
481 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
482 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
483 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
484 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
485 // Opts.LaxVectorConversions = 0;
486 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
487 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
488 // Opts.Blocks = Args.hasArg(OPT_fblocks);
489 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
490 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
491 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
492 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
493 // Opts.AssumeSaneOperatorNew =
494 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
495 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
496 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
497 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
498 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
499 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
502 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
503 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
504 // OPT_fconstant_string_class);
505 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
506 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
507 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
508 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
509 // Opts.Static = Args.hasArg(OPT_static_define);
510 Opts.OptimizeSize = 0;
512 // FIXME: Eliminate this dependency.
514 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
515 // Opts.Optimize = Opt != 0;
518 // This is the __NO_INLINE__ define, which just depends on things like the
519 // optimization level and -fno-inline, not actually whether the backend has
522 // FIXME: This is affected by other options (-fno-inline).
523 Opts.NoInlineDefine = !Opt;
525 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
528 // Diags.Report(diag::err_drv_invalid_value)
529 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
532 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
533 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
534 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
538 ClangASTContext::ClangASTContext(const char *target_triple)
539 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
540 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
541 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
542 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
543 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
544 m_pointer_byte_size(0), m_ast_owned(false) {
545 if (target_triple && target_triple[0])
546 SetTargetTriple(target_triple);
549 //----------------------------------------------------------------------
551 //----------------------------------------------------------------------
552 ClangASTContext::~ClangASTContext() { Finalize(); }
554 ConstString ClangASTContext::GetPluginNameStatic() {
555 return ConstString("clang");
558 ConstString ClangASTContext::GetPluginName() {
559 return ClangASTContext::GetPluginNameStatic();
562 uint32_t ClangASTContext::GetPluginVersion() { return 1; }
564 lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
565 lldb_private::Module *module,
567 if (ClangASTContextSupportsLanguage(language)) {
570 arch = module->GetArchitecture();
572 arch = target->GetArchitecture();
574 if (arch.IsValid()) {
575 ArchSpec fixed_arch = arch;
576 // LLVM wants this to be set to iOS or MacOSX; if we're working on
577 // a bare-boards type image, change the triple for llvm's benefit.
578 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
579 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
580 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
581 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
582 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
583 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
585 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
590 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
592 ast_sp->SetArchitecture(fixed_arch);
595 } else if (target && target->IsValid()) {
596 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
597 new ClangASTContextForExpressions(*target));
599 ast_sp->SetArchitecture(fixed_arch);
600 ast_sp->m_scratch_ast_source_ap.reset(
601 new ClangASTSource(target->shared_from_this()));
602 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
603 ast_sp->getASTContext());
604 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
605 ast_sp->m_scratch_ast_source_ap->CreateProxy());
606 ast_sp->SetExternalSource(proxy_ast_source);
612 return lldb::TypeSystemSP();
615 void ClangASTContext::EnumerateSupportedLanguages(
616 std::set<lldb::LanguageType> &languages_for_types,
617 std::set<lldb::LanguageType> &languages_for_expressions) {
618 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
619 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
620 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
621 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
622 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
623 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
625 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
626 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
627 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
628 lldb::eLanguageTypeC_plus_plus_14});
630 languages_for_types.insert(s_supported_languages_for_types.begin(),
631 s_supported_languages_for_types.end());
632 languages_for_expressions.insert(
633 s_supported_languages_for_expressions.begin(),
634 s_supported_languages_for_expressions.end());
637 void ClangASTContext::Initialize() {
638 PluginManager::RegisterPlugin(GetPluginNameStatic(),
639 "clang base AST context plug-in",
640 CreateInstance, EnumerateSupportedLanguages);
643 void ClangASTContext::Terminate() {
644 PluginManager::UnregisterPlugin(CreateInstance);
647 void ClangASTContext::Finalize() {
648 if (m_ast_ap.get()) {
649 GetASTMap().Erase(m_ast_ap.get());
654 m_builtins_ap.reset();
655 m_selector_table_ap.reset();
656 m_identifier_table_ap.reset();
657 m_target_info_ap.reset();
658 m_target_options_rp.reset();
659 m_diagnostics_engine_ap.reset();
660 m_source_manager_ap.reset();
661 m_language_options_ap.reset();
663 m_scratch_ast_source_ap.reset();
666 void ClangASTContext::Clear() {
668 m_language_options_ap.reset();
669 m_source_manager_ap.reset();
670 m_diagnostics_engine_ap.reset();
671 m_target_options_rp.reset();
672 m_target_info_ap.reset();
673 m_identifier_table_ap.reset();
674 m_selector_table_ap.reset();
675 m_builtins_ap.reset();
676 m_pointer_byte_size = 0;
679 const char *ClangASTContext::GetTargetTriple() {
680 return m_target_triple.c_str();
683 void ClangASTContext::SetTargetTriple(const char *target_triple) {
685 m_target_triple.assign(target_triple);
688 void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
689 SetTargetTriple(arch.GetTriple().str().c_str());
692 bool ClangASTContext::HasExternalSource() {
693 ASTContext *ast = getASTContext();
695 return ast->getExternalSource() != nullptr;
699 void ClangASTContext::SetExternalSource(
700 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
701 ASTContext *ast = getASTContext();
703 ast->setExternalSource(ast_source_ap);
704 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
705 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
709 void ClangASTContext::RemoveExternalSource() {
710 ASTContext *ast = getASTContext();
713 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
714 ast->setExternalSource(empty_ast_source_ap);
715 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
716 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
720 void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
725 m_ast_ap.reset(ast_ctx);
726 GetASTMap().Insert(ast_ctx, this);
729 ASTContext *ClangASTContext::getASTContext() {
730 if (m_ast_ap.get() == nullptr) {
732 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
733 *getIdentifierTable(), *getSelectorTable(),
734 *getBuiltinContext()));
736 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
738 // This can be NULL if we don't know anything about the architecture or if
740 // target for an architecture isn't enabled in the llvm/clang that we built
741 TargetInfo *target_info = getTargetInfo();
743 m_ast_ap->InitBuiltinTypes(*target_info);
745 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
746 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
747 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
750 GetASTMap().Insert(m_ast_ap.get(), this);
752 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
753 new ClangExternalASTSourceCallbacks(
754 ClangASTContext::CompleteTagDecl,
755 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
756 ClangASTContext::LayoutRecordType, this));
757 SetExternalSource(ast_source_ap);
759 return m_ast_ap.get();
762 ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
763 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
767 Builtin::Context *ClangASTContext::getBuiltinContext() {
768 if (m_builtins_ap.get() == nullptr)
769 m_builtins_ap.reset(new Builtin::Context());
770 return m_builtins_ap.get();
773 IdentifierTable *ClangASTContext::getIdentifierTable() {
774 if (m_identifier_table_ap.get() == nullptr)
775 m_identifier_table_ap.reset(
776 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
777 return m_identifier_table_ap.get();
780 LangOptions *ClangASTContext::getLanguageOptions() {
781 if (m_language_options_ap.get() == nullptr) {
782 m_language_options_ap.reset(new LangOptions());
783 ParseLangArgs(*m_language_options_ap, IK_ObjCXX, GetTargetTriple());
784 // InitializeLangOptions(*m_language_options_ap, IK_ObjCXX);
786 return m_language_options_ap.get();
789 SelectorTable *ClangASTContext::getSelectorTable() {
790 if (m_selector_table_ap.get() == nullptr)
791 m_selector_table_ap.reset(new SelectorTable());
792 return m_selector_table_ap.get();
795 clang::FileManager *ClangASTContext::getFileManager() {
796 if (m_file_manager_ap.get() == nullptr) {
797 clang::FileSystemOptions file_system_options;
798 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
800 return m_file_manager_ap.get();
803 clang::SourceManager *ClangASTContext::getSourceManager() {
804 if (m_source_manager_ap.get() == nullptr)
805 m_source_manager_ap.reset(
806 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
807 return m_source_manager_ap.get();
810 clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
811 if (m_diagnostics_engine_ap.get() == nullptr) {
812 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
813 m_diagnostics_engine_ap.reset(
814 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
816 return m_diagnostics_engine_ap.get();
819 clang::MangleContext *ClangASTContext::getMangleContext() {
820 if (m_mangle_ctx_ap.get() == nullptr)
821 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
822 return m_mangle_ctx_ap.get();
825 class NullDiagnosticConsumer : public DiagnosticConsumer {
827 NullDiagnosticConsumer() {
828 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
831 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
832 const clang::Diagnostic &info) {
834 llvm::SmallVector<char, 32> diag_str(10);
835 info.FormatDiagnostic(diag_str);
836 diag_str.push_back('\0');
837 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
841 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
842 return new NullDiagnosticConsumer();
849 DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
850 if (m_diagnostic_consumer_ap.get() == nullptr)
851 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
853 return m_diagnostic_consumer_ap.get();
856 std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
857 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
858 m_target_options_rp = std::make_shared<clang::TargetOptions>();
859 if (m_target_options_rp.get() != nullptr)
860 m_target_options_rp->Triple = m_target_triple;
862 return m_target_options_rp;
865 TargetInfo *ClangASTContext::getTargetInfo() {
866 // target_triple should be something like "x86_64-apple-macosx"
867 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
868 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
869 getTargetOptions()));
870 return m_target_info_ap.get();
873 #pragma mark Basic Types
875 static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
876 ASTContext *ast, QualType qual_type) {
877 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
878 if (qual_type_bit_size == bit_size)
884 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
886 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
887 getASTContext(), encoding, bit_size);
890 CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
891 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
893 return CompilerType();
895 case eEncodingInvalid:
896 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
897 return CompilerType(ast, ast->VoidPtrTy);
901 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
902 return CompilerType(ast, ast->UnsignedCharTy);
903 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
904 return CompilerType(ast, ast->UnsignedShortTy);
905 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
906 return CompilerType(ast, ast->UnsignedIntTy);
907 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
908 return CompilerType(ast, ast->UnsignedLongTy);
909 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
910 return CompilerType(ast, ast->UnsignedLongLongTy);
911 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
912 return CompilerType(ast, ast->UnsignedInt128Ty);
916 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
917 return CompilerType(ast, ast->SignedCharTy);
918 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
919 return CompilerType(ast, ast->ShortTy);
920 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
921 return CompilerType(ast, ast->IntTy);
922 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
923 return CompilerType(ast, ast->LongTy);
924 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
925 return CompilerType(ast, ast->LongLongTy);
926 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
927 return CompilerType(ast, ast->Int128Ty);
930 case eEncodingIEEE754:
931 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
932 return CompilerType(ast, ast->FloatTy);
933 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
934 return CompilerType(ast, ast->DoubleTy);
935 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
936 return CompilerType(ast, ast->LongDoubleTy);
937 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
938 return CompilerType(ast, ast->HalfTy);
941 case eEncodingVector:
942 // Sanity check that bit_size is a multiple of 8's.
943 if (bit_size && !(bit_size & 0x7u))
945 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
949 return CompilerType();
953 ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
955 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
956 static TypeNameToBasicTypeMap g_type_map;
957 static std::once_flag g_once_flag;
958 std::call_once(g_once_flag, []() {
960 g_type_map.Append(ConstString("void").GetStringRef(), eBasicTypeVoid);
963 g_type_map.Append(ConstString("char").GetStringRef(), eBasicTypeChar);
964 g_type_map.Append(ConstString("signed char").GetStringRef(),
965 eBasicTypeSignedChar);
966 g_type_map.Append(ConstString("unsigned char").GetStringRef(),
967 eBasicTypeUnsignedChar);
968 g_type_map.Append(ConstString("wchar_t").GetStringRef(), eBasicTypeWChar);
969 g_type_map.Append(ConstString("signed wchar_t").GetStringRef(),
970 eBasicTypeSignedWChar);
971 g_type_map.Append(ConstString("unsigned wchar_t").GetStringRef(),
972 eBasicTypeUnsignedWChar);
974 g_type_map.Append(ConstString("short").GetStringRef(), eBasicTypeShort);
975 g_type_map.Append(ConstString("short int").GetStringRef(),
977 g_type_map.Append(ConstString("unsigned short").GetStringRef(),
978 eBasicTypeUnsignedShort);
979 g_type_map.Append(ConstString("unsigned short int").GetStringRef(),
980 eBasicTypeUnsignedShort);
983 g_type_map.Append(ConstString("int").GetStringRef(), eBasicTypeInt);
984 g_type_map.Append(ConstString("signed int").GetStringRef(),
986 g_type_map.Append(ConstString("unsigned int").GetStringRef(),
987 eBasicTypeUnsignedInt);
988 g_type_map.Append(ConstString("unsigned").GetStringRef(),
989 eBasicTypeUnsignedInt);
992 g_type_map.Append(ConstString("long").GetStringRef(), eBasicTypeLong);
993 g_type_map.Append(ConstString("long int").GetStringRef(), eBasicTypeLong);
994 g_type_map.Append(ConstString("unsigned long").GetStringRef(),
995 eBasicTypeUnsignedLong);
996 g_type_map.Append(ConstString("unsigned long int").GetStringRef(),
997 eBasicTypeUnsignedLong);
1000 g_type_map.Append(ConstString("long long").GetStringRef(),
1001 eBasicTypeLongLong);
1002 g_type_map.Append(ConstString("long long int").GetStringRef(),
1003 eBasicTypeLongLong);
1004 g_type_map.Append(ConstString("unsigned long long").GetStringRef(),
1005 eBasicTypeUnsignedLongLong);
1006 g_type_map.Append(ConstString("unsigned long long int").GetStringRef(),
1007 eBasicTypeUnsignedLongLong);
1010 g_type_map.Append(ConstString("__int128_t").GetStringRef(),
1012 g_type_map.Append(ConstString("__uint128_t").GetStringRef(),
1013 eBasicTypeUnsignedInt128);
1016 g_type_map.Append(ConstString("bool").GetStringRef(), eBasicTypeBool);
1017 g_type_map.Append(ConstString("float").GetStringRef(), eBasicTypeFloat);
1018 g_type_map.Append(ConstString("double").GetStringRef(), eBasicTypeDouble);
1019 g_type_map.Append(ConstString("long double").GetStringRef(),
1020 eBasicTypeLongDouble);
1021 g_type_map.Append(ConstString("id").GetStringRef(), eBasicTypeObjCID);
1022 g_type_map.Append(ConstString("SEL").GetStringRef(), eBasicTypeObjCSel);
1023 g_type_map.Append(ConstString("nullptr").GetStringRef(),
1028 return g_type_map.Find(name.GetStringRef(), eBasicTypeInvalid);
1030 return eBasicTypeInvalid;
1033 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1034 const ConstString &name) {
1036 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1037 return ClangASTContext::GetBasicType(ast, basic_type);
1039 return CompilerType();
1042 uint32_t ClangASTContext::GetPointerByteSize() {
1043 if (m_pointer_byte_size == 0)
1044 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1046 .GetByteSize(nullptr);
1047 return m_pointer_byte_size;
1050 CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1051 return GetBasicType(getASTContext(), basic_type);
1054 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1055 lldb::BasicType basic_type) {
1057 return CompilerType();
1058 lldb::opaque_compiler_type_t clang_type =
1059 GetOpaqueCompilerType(ast, basic_type);
1062 return CompilerType(GetASTContext(ast), clang_type);
1063 return CompilerType();
1066 CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1067 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1068 ASTContext *ast = getASTContext();
1070 #define streq(a, b) strcmp(a, b) == 0
1071 assert(ast != nullptr);
1077 case DW_ATE_address:
1078 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1079 return CompilerType(ast, ast->VoidPtrTy);
1082 case DW_ATE_boolean:
1083 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1084 return CompilerType(ast, ast->BoolTy);
1085 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1086 return CompilerType(ast, ast->UnsignedCharTy);
1087 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1088 return CompilerType(ast, ast->UnsignedShortTy);
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1090 return CompilerType(ast, ast->UnsignedIntTy);
1093 case DW_ATE_lo_user:
1094 // This has been seen to mean DW_AT_complex_integer
1096 if (::strstr(type_name, "complex")) {
1097 CompilerType complex_int_clang_type =
1098 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1100 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1101 complex_int_clang_type)));
1106 case DW_ATE_complex_float:
1107 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1108 return CompilerType(ast, ast->FloatComplexTy);
1109 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1110 return CompilerType(ast, ast->DoubleComplexTy);
1111 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1112 return CompilerType(ast, ast->LongDoubleComplexTy);
1114 CompilerType complex_float_clang_type =
1115 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1117 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1118 complex_float_clang_type)));
1123 if (streq(type_name, "float") &&
1124 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1125 return CompilerType(ast, ast->FloatTy);
1126 if (streq(type_name, "double") &&
1127 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1128 return CompilerType(ast, ast->DoubleTy);
1129 if (streq(type_name, "long double") &&
1130 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1131 return CompilerType(ast, ast->LongDoubleTy);
1132 // Fall back to not requiring a name match
1133 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1134 return CompilerType(ast, ast->FloatTy);
1135 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1136 return CompilerType(ast, ast->DoubleTy);
1137 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1138 return CompilerType(ast, ast->LongDoubleTy);
1139 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1140 return CompilerType(ast, ast->HalfTy);
1145 if (streq(type_name, "wchar_t") &&
1146 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1148 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1149 return CompilerType(ast, ast->WCharTy);
1150 if (streq(type_name, "void") &&
1151 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1152 return CompilerType(ast, ast->VoidTy);
1153 if (strstr(type_name, "long long") &&
1154 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1155 return CompilerType(ast, ast->LongLongTy);
1156 if (strstr(type_name, "long") &&
1157 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1158 return CompilerType(ast, ast->LongTy);
1159 if (strstr(type_name, "short") &&
1160 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1161 return CompilerType(ast, ast->ShortTy);
1162 if (strstr(type_name, "char")) {
1163 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1164 return CompilerType(ast, ast->CharTy);
1165 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1166 return CompilerType(ast, ast->SignedCharTy);
1168 if (strstr(type_name, "int")) {
1169 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1170 return CompilerType(ast, ast->IntTy);
1171 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1172 return CompilerType(ast, ast->Int128Ty);
1175 // We weren't able to match up a type name, just search by size
1176 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1177 return CompilerType(ast, ast->CharTy);
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1179 return CompilerType(ast, ast->ShortTy);
1180 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1181 return CompilerType(ast, ast->IntTy);
1182 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1183 return CompilerType(ast, ast->LongTy);
1184 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1185 return CompilerType(ast, ast->LongLongTy);
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1187 return CompilerType(ast, ast->Int128Ty);
1190 case DW_ATE_signed_char:
1191 if (ast->getLangOpts().CharIsSigned && type_name &&
1192 streq(type_name, "char")) {
1193 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1194 return CompilerType(ast, ast->CharTy);
1196 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1197 return CompilerType(ast, ast->SignedCharTy);
1200 case DW_ATE_unsigned:
1202 if (streq(type_name, "wchar_t")) {
1203 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1204 if (!(getTargetInfo() &&
1205 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1206 return CompilerType(ast, ast->WCharTy);
1209 if (strstr(type_name, "long long")) {
1210 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1211 return CompilerType(ast, ast->UnsignedLongLongTy);
1212 } else if (strstr(type_name, "long")) {
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1214 return CompilerType(ast, ast->UnsignedLongTy);
1215 } else if (strstr(type_name, "short")) {
1216 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1217 return CompilerType(ast, ast->UnsignedShortTy);
1218 } else if (strstr(type_name, "char")) {
1219 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1220 return CompilerType(ast, ast->UnsignedCharTy);
1221 } else if (strstr(type_name, "int")) {
1222 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1223 return CompilerType(ast, ast->UnsignedIntTy);
1224 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1225 return CompilerType(ast, ast->UnsignedInt128Ty);
1228 // We weren't able to match up a type name, just search by size
1229 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1230 return CompilerType(ast, ast->UnsignedCharTy);
1231 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1232 return CompilerType(ast, ast->UnsignedShortTy);
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1234 return CompilerType(ast, ast->UnsignedIntTy);
1235 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1236 return CompilerType(ast, ast->UnsignedLongTy);
1237 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1238 return CompilerType(ast, ast->UnsignedLongLongTy);
1239 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1240 return CompilerType(ast, ast->UnsignedInt128Ty);
1243 case DW_ATE_unsigned_char:
1244 if (!ast->getLangOpts().CharIsSigned && type_name &&
1245 streq(type_name, "char")) {
1246 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1247 return CompilerType(ast, ast->CharTy);
1249 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1250 return CompilerType(ast, ast->UnsignedCharTy);
1251 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1252 return CompilerType(ast, ast->UnsignedShortTy);
1255 case DW_ATE_imaginary_float:
1260 if (streq(type_name, "char16_t")) {
1261 return CompilerType(ast, ast->Char16Ty);
1262 } else if (streq(type_name, "char32_t")) {
1263 return CompilerType(ast, ast->Char32Ty);
1269 // This assert should fire for anything that we don't catch above so we know
1270 // to fix any issues we run into.
1272 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1273 "DW_TAG_base_type '%s' encoded with "
1274 "DW_ATE = 0x%x, bit_size = %u\n",
1275 type_name, dw_ate, bit_size);
1277 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1278 "DW_TAG_base_type encoded with "
1279 "DW_ATE = 0x%x, bit_size = %u\n",
1282 return CompilerType();
1285 CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1287 return CompilerType(ast, ast->UnknownAnyTy);
1288 return CompilerType();
1291 CompilerType ClangASTContext::GetCStringType(bool is_const) {
1292 ASTContext *ast = getASTContext();
1293 QualType char_type(ast->CharTy);
1296 char_type.addConst();
1298 return CompilerType(ast, ast->getPointerType(char_type));
1301 clang::DeclContext *
1302 ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1303 return ast->getTranslationUnitDecl();
1306 clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1307 clang::Decl *source_decl) {
1308 FileSystemOptions file_system_options;
1309 FileManager file_manager(file_system_options);
1310 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1312 return importer.Import(source_decl);
1315 bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1316 bool ignore_qualifiers) {
1317 ClangASTContext *ast =
1318 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1319 if (!ast || ast != type2.GetTypeSystem())
1322 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1325 QualType type1_qual = ClangUtil::GetQualType(type1);
1326 QualType type2_qual = ClangUtil::GetQualType(type2);
1328 if (ignore_qualifiers) {
1329 type1_qual = type1_qual.getUnqualifiedType();
1330 type2_qual = type2_qual.getUnqualifiedType();
1333 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1336 CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1337 if (clang::ObjCInterfaceDecl *interface_decl =
1338 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1339 return GetTypeForDecl(interface_decl);
1340 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1341 return GetTypeForDecl(tag_decl);
1342 return CompilerType();
1345 CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1346 // No need to call the getASTContext() accessor (which can create the AST
1347 // if it isn't created yet, because we can't have created a decl in this
1348 // AST if our AST didn't already exist...
1349 ASTContext *ast = &decl->getASTContext();
1351 return CompilerType(ast, ast->getTagDeclType(decl));
1352 return CompilerType();
1355 CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1356 // No need to call the getASTContext() accessor (which can create the AST
1357 // if it isn't created yet, because we can't have created a decl in this
1358 // AST if our AST didn't already exist...
1359 ASTContext *ast = &decl->getASTContext();
1361 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1362 return CompilerType();
1365 #pragma mark Structure, Unions, Classes
1367 CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1368 AccessType access_type,
1369 const char *name, int kind,
1370 LanguageType language,
1371 ClangASTMetadata *metadata) {
1372 ASTContext *ast = getASTContext();
1373 assert(ast != nullptr);
1375 if (decl_ctx == nullptr)
1376 decl_ctx = ast->getTranslationUnitDecl();
1378 if (language == eLanguageTypeObjC ||
1379 language == eLanguageTypeObjC_plus_plus) {
1380 bool isForwardDecl = true;
1381 bool isInternal = false;
1382 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1385 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1386 // we will need to update this code. I was told to currently always use
1387 // the CXXRecordDecl class since we often don't know from debug information
1388 // if something is struct or a class, so we default to always use the more
1389 // complete definition just in case.
1391 bool is_anonymous = (!name) || (!name[0]);
1393 CXXRecordDecl *decl = CXXRecordDecl::Create(
1394 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1395 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1398 decl->setAnonymousStructOrUnion(true);
1402 SetMetadata(ast, decl, *metadata);
1404 if (access_type != eAccessNone)
1405 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1408 decl_ctx->addDecl(decl);
1410 return CompilerType(ast, ast->getTagDeclType(decl));
1412 return CompilerType();
1415 static TemplateParameterList *CreateTemplateParameterList(
1417 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1418 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1419 const bool parameter_pack = false;
1420 const bool is_typename = false;
1421 const unsigned depth = 0;
1422 const size_t num_template_params = template_param_infos.GetSize();
1423 for (size_t i = 0; i < num_template_params; ++i) {
1424 const char *name = template_param_infos.names[i];
1426 IdentifierInfo *identifier_info = nullptr;
1427 if (name && name[0])
1428 identifier_info = &ast->Idents.get(name);
1429 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) {
1430 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1432 ast->getTranslationUnitDecl(), // Is this the right decl context?,
1433 // SourceLocation StartLoc,
1434 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1435 template_param_infos.args[i].getIntegralType(), parameter_pack,
1439 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1441 ast->getTranslationUnitDecl(), // Is this the right decl context?
1442 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1443 is_typename, parameter_pack));
1447 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1448 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1449 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1450 SourceLocation(), requires_clause);
1451 return template_param_list;
1454 clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1455 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1456 const char *name, const TemplateParameterInfos &template_param_infos) {
1457 // /// \brief Create a function template node.
1458 ASTContext *ast = getASTContext();
1460 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1462 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1463 ast, template_param_infos, template_param_decls);
1464 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1465 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1466 template_param_list, func_decl);
1468 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1469 i < template_param_decl_count; ++i) {
1470 // TODO: verify which decl context we should put template_param_decls into..
1471 template_param_decls[i]->setDeclContext(func_decl);
1474 return func_tmpl_decl;
1477 void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1478 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1479 const TemplateParameterInfos &infos) {
1480 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1482 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1486 ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1487 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1488 int kind, const TemplateParameterInfos &template_param_infos) {
1489 ASTContext *ast = getASTContext();
1491 ClassTemplateDecl *class_template_decl = nullptr;
1492 if (decl_ctx == nullptr)
1493 decl_ctx = ast->getTranslationUnitDecl();
1495 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1496 DeclarationName decl_name(&identifier_info);
1498 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1500 for (NamedDecl *decl : result) {
1501 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1502 if (class_template_decl)
1503 return class_template_decl;
1506 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1508 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1509 ast, template_param_infos, template_param_decls);
1511 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1512 *ast, (TagDecl::TagKind)kind,
1513 decl_ctx, // What decl context do we use here? TU? The actual decl
1515 SourceLocation(), SourceLocation(), &identifier_info);
1517 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1518 i < template_param_decl_count; ++i) {
1519 template_param_decls[i]->setDeclContext(template_cxx_decl);
1522 // With templated classes, we say that a class is templated with
1523 // specializations, but that the bare class has no functions.
1524 // template_cxx_decl->startDefinition();
1525 // template_cxx_decl->completeDefinition();
1527 class_template_decl = ClassTemplateDecl::Create(
1529 decl_ctx, // What decl context do we use here? TU? The actual decl
1531 SourceLocation(), decl_name, template_param_list, template_cxx_decl,
1534 if (class_template_decl) {
1535 if (access_type != eAccessNone)
1536 class_template_decl->setAccess(
1537 ConvertAccessTypeToAccessSpecifier(access_type));
1539 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1540 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1542 decl_ctx->addDecl(class_template_decl);
1544 #ifdef LLDB_CONFIGURATION_DEBUG
1545 VerifyDecl(class_template_decl);
1549 return class_template_decl;
1552 ClassTemplateSpecializationDecl *
1553 ClangASTContext::CreateClassTemplateSpecializationDecl(
1554 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1555 const TemplateParameterInfos &template_param_infos) {
1556 ASTContext *ast = getASTContext();
1557 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1558 ClassTemplateSpecializationDecl::Create(
1559 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1560 SourceLocation(), class_template_decl, template_param_infos.args,
1563 class_template_specialization_decl->setSpecializationKind(
1564 TSK_ExplicitSpecialization);
1566 return class_template_specialization_decl;
1569 CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1570 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1571 if (class_template_specialization_decl) {
1572 ASTContext *ast = getASTContext();
1574 return CompilerType(
1575 ast, ast->getTagDeclType(class_template_specialization_decl));
1577 return CompilerType();
1580 static inline bool check_op_param(bool is_method,
1581 clang::OverloadedOperatorKind op_kind,
1582 bool unary, bool binary,
1583 uint32_t num_params) {
1584 // Special-case call since it can take any number of operands
1585 if (op_kind == OO_Call)
1588 // The parameter count doesn't include "this"
1591 if (num_params == 1)
1593 if (num_params == 2)
1599 bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1600 bool is_method, clang::OverloadedOperatorKind op_kind,
1601 uint32_t num_params) {
1605 // C++ standard allows any number of arguments to new/delete
1609 case OO_Array_Delete:
1613 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1615 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1617 #include "clang/Basic/OperatorKinds.def"
1624 clang::AccessSpecifier
1625 ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1626 clang::AccessSpecifier rhs) {
1627 // Make the access equal to the stricter of the field and the nested field's
1629 if (lhs == AS_none || rhs == AS_none)
1631 if (lhs == AS_private || rhs == AS_private)
1633 if (lhs == AS_protected || rhs == AS_protected)
1634 return AS_protected;
1638 bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1639 uint32_t &bitfield_bit_size) {
1640 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1643 bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1644 uint32_t &bitfield_bit_size) {
1645 if (ast == nullptr || field == nullptr)
1648 if (field->isBitField()) {
1649 Expr *bit_width_expr = field->getBitWidth();
1650 if (bit_width_expr) {
1651 llvm::APSInt bit_width_apsint;
1652 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1653 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1661 bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1662 if (record_decl == nullptr)
1665 if (!record_decl->field_empty())
1668 // No fields, lets check this is a CXX record and check the base classes
1669 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1670 if (cxx_record_decl) {
1671 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1672 for (base_class = cxx_record_decl->bases_begin(),
1673 base_class_end = cxx_record_decl->bases_end();
1674 base_class != base_class_end; ++base_class) {
1675 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1676 base_class->getType()->getAs<RecordType>()->getDecl());
1677 if (RecordHasFields(base_class_decl))
1684 #pragma mark Objective C Classes
1686 CompilerType ClangASTContext::CreateObjCClass(const char *name,
1687 DeclContext *decl_ctx,
1690 ClangASTMetadata *metadata) {
1691 ASTContext *ast = getASTContext();
1692 assert(ast != nullptr);
1693 assert(name && name[0]);
1694 if (decl_ctx == nullptr)
1695 decl_ctx = ast->getTranslationUnitDecl();
1697 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1698 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1699 nullptr, SourceLocation(),
1703 if (decl && metadata)
1704 SetMetadata(ast, decl, *metadata);
1706 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1709 static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1710 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1715 ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1716 bool omit_empty_base_classes) {
1717 uint32_t num_bases = 0;
1718 if (cxx_record_decl) {
1719 if (omit_empty_base_classes) {
1720 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1721 for (base_class = cxx_record_decl->bases_begin(),
1722 base_class_end = cxx_record_decl->bases_end();
1723 base_class != base_class_end; ++base_class) {
1724 // Skip empty base classes
1725 if (omit_empty_base_classes) {
1726 if (BaseSpecifierIsEmpty(base_class))
1732 num_bases = cxx_record_decl->getNumBases();
1737 #pragma mark Namespace Declarations
1740 ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1741 DeclContext *decl_ctx) {
1742 NamespaceDecl *namespace_decl = nullptr;
1743 ASTContext *ast = getASTContext();
1744 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1745 if (decl_ctx == nullptr)
1746 decl_ctx = translation_unit_decl;
1749 IdentifierInfo &identifier_info = ast->Idents.get(name);
1750 DeclarationName decl_name(&identifier_info);
1751 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1752 for (NamedDecl *decl : result) {
1753 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1755 return namespace_decl;
1759 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1760 SourceLocation(), &identifier_info, nullptr);
1762 decl_ctx->addDecl(namespace_decl);
1764 if (decl_ctx == translation_unit_decl) {
1765 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1767 return namespace_decl;
1770 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1771 SourceLocation(), nullptr, nullptr);
1772 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1773 translation_unit_decl->addDecl(namespace_decl);
1774 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1776 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1777 if (parent_namespace_decl) {
1778 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1780 return namespace_decl;
1782 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1783 SourceLocation(), nullptr, nullptr);
1784 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1785 parent_namespace_decl->addDecl(namespace_decl);
1786 assert(namespace_decl ==
1787 parent_namespace_decl->getAnonymousNamespace());
1793 #ifdef LLDB_CONFIGURATION_DEBUG
1794 VerifyDecl(namespace_decl);
1796 return namespace_decl;
1799 NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1800 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1801 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1802 if (ast_ctx == nullptr)
1805 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1809 ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1810 if (ctx != nullptr) {
1811 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1812 clang::SourceLocation());
1819 clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1820 clang::DeclContext *right,
1821 clang::DeclContext *root) {
1822 if (root == nullptr)
1825 std::set<clang::DeclContext *> path_left;
1826 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1827 path_left.insert(d);
1829 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1830 if (path_left.find(d) != path_left.end())
1836 clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1837 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1838 if (decl_ctx != nullptr && ns_decl != nullptr) {
1839 clang::TranslationUnitDecl *translation_unit =
1840 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1841 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1842 *getASTContext(), decl_ctx, clang::SourceLocation(),
1843 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1844 clang::SourceLocation(), ns_decl,
1845 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1846 decl_ctx->addDecl(using_decl);
1853 ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1854 clang::NamedDecl *target) {
1855 if (current_decl_ctx != nullptr && target != nullptr) {
1856 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1857 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1858 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1859 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1860 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1862 using_decl->addShadowDecl(shadow_decl);
1863 current_decl_ctx->addDecl(using_decl);
1869 clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1870 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1871 if (decl_context != nullptr) {
1872 clang::VarDecl *var_decl = clang::VarDecl::Create(
1873 *getASTContext(), decl_context, clang::SourceLocation(),
1874 clang::SourceLocation(),
1875 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1876 nullptr, clang::SC_None);
1877 var_decl->setAccess(clang::AS_public);
1878 decl_context->addDecl(var_decl);
1884 lldb::opaque_compiler_type_t
1885 ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1886 lldb::BasicType basic_type) {
1887 switch (basic_type) {
1888 case eBasicTypeVoid:
1889 return ast->VoidTy.getAsOpaquePtr();
1890 case eBasicTypeChar:
1891 return ast->CharTy.getAsOpaquePtr();
1892 case eBasicTypeSignedChar:
1893 return ast->SignedCharTy.getAsOpaquePtr();
1894 case eBasicTypeUnsignedChar:
1895 return ast->UnsignedCharTy.getAsOpaquePtr();
1896 case eBasicTypeWChar:
1897 return ast->getWCharType().getAsOpaquePtr();
1898 case eBasicTypeSignedWChar:
1899 return ast->getSignedWCharType().getAsOpaquePtr();
1900 case eBasicTypeUnsignedWChar:
1901 return ast->getUnsignedWCharType().getAsOpaquePtr();
1902 case eBasicTypeChar16:
1903 return ast->Char16Ty.getAsOpaquePtr();
1904 case eBasicTypeChar32:
1905 return ast->Char32Ty.getAsOpaquePtr();
1906 case eBasicTypeShort:
1907 return ast->ShortTy.getAsOpaquePtr();
1908 case eBasicTypeUnsignedShort:
1909 return ast->UnsignedShortTy.getAsOpaquePtr();
1911 return ast->IntTy.getAsOpaquePtr();
1912 case eBasicTypeUnsignedInt:
1913 return ast->UnsignedIntTy.getAsOpaquePtr();
1914 case eBasicTypeLong:
1915 return ast->LongTy.getAsOpaquePtr();
1916 case eBasicTypeUnsignedLong:
1917 return ast->UnsignedLongTy.getAsOpaquePtr();
1918 case eBasicTypeLongLong:
1919 return ast->LongLongTy.getAsOpaquePtr();
1920 case eBasicTypeUnsignedLongLong:
1921 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1922 case eBasicTypeInt128:
1923 return ast->Int128Ty.getAsOpaquePtr();
1924 case eBasicTypeUnsignedInt128:
1925 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1926 case eBasicTypeBool:
1927 return ast->BoolTy.getAsOpaquePtr();
1928 case eBasicTypeHalf:
1929 return ast->HalfTy.getAsOpaquePtr();
1930 case eBasicTypeFloat:
1931 return ast->FloatTy.getAsOpaquePtr();
1932 case eBasicTypeDouble:
1933 return ast->DoubleTy.getAsOpaquePtr();
1934 case eBasicTypeLongDouble:
1935 return ast->LongDoubleTy.getAsOpaquePtr();
1936 case eBasicTypeFloatComplex:
1937 return ast->FloatComplexTy.getAsOpaquePtr();
1938 case eBasicTypeDoubleComplex:
1939 return ast->DoubleComplexTy.getAsOpaquePtr();
1940 case eBasicTypeLongDoubleComplex:
1941 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1942 case eBasicTypeObjCID:
1943 return ast->getObjCIdType().getAsOpaquePtr();
1944 case eBasicTypeObjCClass:
1945 return ast->getObjCClassType().getAsOpaquePtr();
1946 case eBasicTypeObjCSel:
1947 return ast->getObjCSelType().getAsOpaquePtr();
1948 case eBasicTypeNullPtr:
1949 return ast->NullPtrTy.getAsOpaquePtr();
1955 #pragma mark Function Types
1957 clang::DeclarationName
1958 ClangASTContext::GetDeclarationName(const char *name,
1959 const CompilerType &function_clang_type) {
1960 if (!name || !name[0])
1961 return clang::DeclarationName();
1963 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1964 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1965 return DeclarationName(&getASTContext()->Idents.get(
1966 name)); // Not operator, but a regular function.
1968 // Check the number of operator parameters. Sometimes we have
1969 // seen bad DWARF that doesn't correctly describe operators and
1970 // if we try to create a method and add it to the class, clang
1971 // will assert and crash, so we need to make sure things are
1973 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1974 const clang::FunctionProtoType *function_type =
1975 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1976 if (function_type == nullptr)
1977 return clang::DeclarationName();
1979 const bool is_method = false;
1980 const unsigned int num_params = function_type->getNumParams();
1981 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1982 is_method, op_kind, num_params))
1983 return clang::DeclarationName();
1985 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
1988 FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1989 DeclContext *decl_ctx, const char *name,
1990 const CompilerType &function_clang_type, int storage, bool is_inline) {
1991 FunctionDecl *func_decl = nullptr;
1992 ASTContext *ast = getASTContext();
1993 if (decl_ctx == nullptr)
1994 decl_ctx = ast->getTranslationUnitDecl();
1996 const bool hasWrittenPrototype = true;
1997 const bool isConstexprSpecified = false;
1999 clang::DeclarationName declarationName =
2000 GetDeclarationName(name, function_clang_type);
2001 func_decl = FunctionDecl::Create(
2002 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2003 ClangUtil::GetQualType(function_clang_type), nullptr,
2004 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2005 isConstexprSpecified);
2007 decl_ctx->addDecl(func_decl);
2009 #ifdef LLDB_CONFIGURATION_DEBUG
2010 VerifyDecl(func_decl);
2016 CompilerType ClangASTContext::CreateFunctionType(
2017 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2018 unsigned num_args, bool is_variadic, unsigned type_quals) {
2020 return CompilerType(); // invalid AST
2022 if (!result_type || !ClangUtil::IsClangType(result_type))
2023 return CompilerType(); // invalid return type
2025 std::vector<QualType> qual_type_args;
2026 if (num_args > 0 && args == nullptr)
2027 return CompilerType(); // invalid argument array passed in
2029 // Verify that all arguments are valid and the right type
2030 for (unsigned i = 0; i < num_args; ++i) {
2032 // Make sure we have a clang type in args[i] and not a type from another
2033 // language whose name might match
2034 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2035 lldbassert(is_clang_type);
2037 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2039 return CompilerType(); // invalid argument type (must be a clang type)
2041 return CompilerType(); // invalid argument type (empty)
2044 // TODO: Detect calling convention in DWARF?
2045 FunctionProtoType::ExtProtoInfo proto_info;
2046 proto_info.Variadic = is_variadic;
2047 proto_info.ExceptionSpec = EST_None;
2048 proto_info.TypeQuals = type_quals;
2049 proto_info.RefQualifier = RQ_None;
2051 return CompilerType(ast,
2052 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2053 qual_type_args, proto_info));
2056 ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2057 const char *name, const CompilerType ¶m_type, int storage) {
2058 ASTContext *ast = getASTContext();
2059 assert(ast != nullptr);
2060 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2061 SourceLocation(), SourceLocation(),
2062 name && name[0] ? &ast->Idents.get(name) : nullptr,
2063 ClangUtil::GetQualType(param_type), nullptr,
2064 (clang::StorageClass)storage, nullptr);
2067 void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2068 ParmVarDecl **params,
2069 unsigned num_params) {
2071 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2075 ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2076 QualType block_type = m_ast_ap->getBlockPointerType(
2077 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2079 return CompilerType(this, block_type.getAsOpaquePtr());
2082 #pragma mark Array Types
2084 CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2085 size_t element_count,
2087 if (element_type.IsValid()) {
2088 ASTContext *ast = getASTContext();
2089 assert(ast != nullptr);
2092 return CompilerType(
2093 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2097 llvm::APInt ap_element_count(64, element_count);
2098 if (element_count == 0) {
2099 return CompilerType(ast, ast->getIncompleteArrayType(
2100 ClangUtil::GetQualType(element_type),
2101 clang::ArrayType::Normal, 0));
2103 return CompilerType(
2104 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2106 clang::ArrayType::Normal, 0));
2110 return CompilerType();
2113 CompilerType ClangASTContext::CreateStructForIdentifier(
2114 const ConstString &type_name,
2115 const std::initializer_list<std::pair<const char *, CompilerType>>
2119 if (!type_name.IsEmpty() &&
2120 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2122 lldbassert(0 && "Trying to create a type for an existing name");
2126 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2127 clang::TTK_Struct, lldb::eLanguageTypeC);
2128 StartTagDeclarationDefinition(type);
2129 for (const auto &field : type_fields)
2130 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2134 CompleteTagDeclarationDefinition(type);
2138 CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2139 const ConstString &type_name,
2140 const std::initializer_list<std::pair<const char *, CompilerType>>
2144 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2147 return CreateStructForIdentifier(type_name, type_fields, packed);
2150 #pragma mark Enumeration Types
2153 ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2154 const Declaration &decl,
2155 const CompilerType &integer_clang_type) {
2156 // TODO: Do something intelligent with the Declaration object passed in
2157 // like maybe filling in the SourceLocation with it...
2158 ASTContext *ast = getASTContext();
2160 // TODO: ask about these...
2161 // const bool IsScoped = false;
2162 // const bool IsFixed = false;
2164 EnumDecl *enum_decl = EnumDecl::Create(
2165 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2166 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2168 false, // IsScopedUsingClassTag
2172 // TODO: check if we should be setting the promotion type too?
2173 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2175 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2177 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2179 return CompilerType();
2182 // Disable this for now since I can't seem to get a nicely formatted float
2183 // out of the APFloat class without just getting the float, double or quad
2184 // and then using a formatted print on it which defeats the purpose. We ideally
2185 // would like to get perfect string values for any kind of float semantics
2186 // so we can support remote targets. The code below also requires a patch to
2189 // ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2190 // lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2191 // byte_size, int apint_byte_order, std::string &float_str)
2193 // uint32_t count = 0;
2194 // bool is_complex = false;
2195 // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2197 // unsigned num_bytes_per_float = byte_size / count;
2198 // unsigned num_bits_per_float = num_bytes_per_float * 8;
2200 // float_str.clear();
2202 // for (i=0; i<count; i++)
2204 // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2205 // (APInt::ByteOrder)apint_byte_order);
2206 // bool is_ieee = false;
2207 // APFloat ap_float(ap_int, is_ieee);
2209 // unsigned int hex_digits = 0;
2210 // bool upper_case = false;
2212 // if (ap_float.convertToHexString(s, hex_digits, upper_case,
2213 // APFloat::rmNearestTiesToEven) > 0)
2216 // float_str.append(", ");
2217 // float_str.append(s);
2218 // if (i == 1 && is_complex)
2219 // float_str.append(1, 'i');
2222 // return !float_str.empty();
2227 CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2232 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2233 return CompilerType(ast, ast->SignedCharTy);
2235 if (bit_size == ast->getTypeSize(ast->ShortTy))
2236 return CompilerType(ast, ast->ShortTy);
2238 if (bit_size == ast->getTypeSize(ast->IntTy))
2239 return CompilerType(ast, ast->IntTy);
2241 if (bit_size == ast->getTypeSize(ast->LongTy))
2242 return CompilerType(ast, ast->LongTy);
2244 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2245 return CompilerType(ast, ast->LongLongTy);
2247 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2248 return CompilerType(ast, ast->Int128Ty);
2250 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2251 return CompilerType(ast, ast->UnsignedCharTy);
2253 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2254 return CompilerType(ast, ast->UnsignedShortTy);
2256 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2257 return CompilerType(ast, ast->UnsignedIntTy);
2259 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2260 return CompilerType(ast, ast->UnsignedLongTy);
2262 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2263 return CompilerType(ast, ast->UnsignedLongLongTy);
2265 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2266 return CompilerType(ast, ast->UnsignedInt128Ty);
2269 return CompilerType();
2272 CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2275 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2277 return CompilerType();
2280 void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2282 DumpDeclContextHiearchy(decl_ctx->getParent());
2284 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2286 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2287 named_decl->getDeclName().getAsString().c_str());
2289 printf("%20s\n", decl_ctx->getDeclKindName());
2294 void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2295 if (decl == nullptr)
2297 DumpDeclContextHiearchy(decl->getDeclContext());
2299 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2301 printf("%20s: %s%s\n", decl->getDeclKindName(),
2302 record_decl->getDeclName().getAsString().c_str(),
2303 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2306 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2308 printf("%20s: %s\n", decl->getDeclKindName(),
2309 named_decl->getDeclName().getAsString().c_str());
2311 printf("%20s\n", decl->getDeclKindName());
2316 bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2317 clang::Decl *rhs_decl) {
2318 if (lhs_decl && rhs_decl) {
2319 //----------------------------------------------------------------------
2320 // Make sure the decl kinds match first
2321 //----------------------------------------------------------------------
2322 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2323 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2325 if (lhs_decl_kind == rhs_decl_kind) {
2326 //------------------------------------------------------------------
2327 // Now check that the decl contexts kinds are all equivalent
2328 // before we have to check any names of the decl contexts...
2329 //------------------------------------------------------------------
2330 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2331 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2332 if (lhs_decl_ctx && rhs_decl_ctx) {
2334 if (lhs_decl_ctx && rhs_decl_ctx) {
2335 const clang::Decl::Kind lhs_decl_ctx_kind =
2336 lhs_decl_ctx->getDeclKind();
2337 const clang::Decl::Kind rhs_decl_ctx_kind =
2338 rhs_decl_ctx->getDeclKind();
2339 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2340 lhs_decl_ctx = lhs_decl_ctx->getParent();
2341 rhs_decl_ctx = rhs_decl_ctx->getParent();
2343 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2351 //--------------------------------------------------------------
2352 // Now make sure the name of the decls match
2353 //--------------------------------------------------------------
2354 clang::NamedDecl *lhs_named_decl =
2355 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2356 clang::NamedDecl *rhs_named_decl =
2357 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2358 if (lhs_named_decl && rhs_named_decl) {
2359 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2360 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2361 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2362 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2369 //--------------------------------------------------------------
2370 // We know that the decl context kinds all match, so now we need
2371 // to make sure the names match as well
2372 //--------------------------------------------------------------
2373 lhs_decl_ctx = lhs_decl->getDeclContext();
2374 rhs_decl_ctx = rhs_decl->getDeclContext();
2376 switch (lhs_decl_ctx->getDeclKind()) {
2377 case clang::Decl::TranslationUnit:
2378 // We don't care about the translation unit names
2381 clang::NamedDecl *lhs_named_decl =
2382 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2383 clang::NamedDecl *rhs_named_decl =
2384 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2385 if (lhs_named_decl && rhs_named_decl) {
2386 clang::DeclarationName lhs_decl_name =
2387 lhs_named_decl->getDeclName();
2388 clang::DeclarationName rhs_decl_name =
2389 rhs_named_decl->getDeclName();
2390 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2391 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2399 lhs_decl_ctx = lhs_decl_ctx->getParent();
2400 rhs_decl_ctx = rhs_decl_ctx->getParent();
2407 bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2408 clang::Decl *decl) {
2412 ExternalASTSource *ast_source = ast->getExternalSource();
2417 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2418 if (tag_decl->isCompleteDefinition())
2421 if (!tag_decl->hasExternalLexicalStorage())
2424 ast_source->CompleteType(tag_decl);
2426 return !tag_decl->getTypeForDecl()->isIncompleteType();
2427 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2428 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2429 if (objc_interface_decl->getDefinition())
2432 if (!objc_interface_decl->hasExternalLexicalStorage())
2435 ast_source->CompleteType(objc_interface_decl);
2437 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2443 void ClangASTContext::SetMetadataAsUserID(const void *object,
2444 user_id_t user_id) {
2445 ClangASTMetadata meta_data;
2446 meta_data.SetUserID(user_id);
2447 SetMetadata(object, meta_data);
2450 void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2451 ClangASTMetadata &metadata) {
2452 ClangExternalASTSourceCommon *external_source =
2453 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2455 if (external_source)
2456 external_source->SetMetadata(object, metadata);
2459 ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2460 const void *object) {
2461 ClangExternalASTSourceCommon *external_source =
2462 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2464 if (external_source && external_source->HasMetadata(object))
2465 return external_source->GetMetadata(object);
2470 clang::DeclContext *
2471 ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2472 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2475 clang::DeclContext *
2476 ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2477 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2480 bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2482 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2484 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2486 clang::TagDecl *tag_decl =
2487 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2489 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2497 bool ClangASTContext::SetDefaultAccessForRecordFields(
2498 clang::RecordDecl *record_decl, int default_accessibility,
2499 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2502 clang::RecordDecl::field_iterator field, field_end;
2503 for (field = record_decl->field_begin(),
2504 field_end = record_decl->field_end(), field_idx = 0;
2505 field != field_end; ++field, ++field_idx) {
2506 // If no accessibility was assigned, assign the correct one
2507 if (field_idx < num_assigned_accessibilities &&
2508 assigned_accessibilities[field_idx] == clang::AS_none)
2509 field->setAccess((clang::AccessSpecifier)default_accessibility);
2516 clang::DeclContext *
2517 ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2518 return GetDeclContextForType(ClangUtil::GetQualType(type));
2521 clang::DeclContext *
2522 ClangASTContext::GetDeclContextForType(clang::QualType type) {
2526 clang::QualType qual_type = type.getCanonicalType();
2527 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2528 switch (type_class) {
2529 case clang::Type::ObjCInterface:
2530 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2532 case clang::Type::ObjCObjectPointer:
2533 return GetDeclContextForType(
2534 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2535 ->getPointeeType());
2536 case clang::Type::Record:
2537 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2538 case clang::Type::Enum:
2539 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2540 case clang::Type::Typedef:
2541 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2543 ->getUnderlyingType());
2544 case clang::Type::Auto:
2545 return GetDeclContextForType(
2546 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2547 case clang::Type::Elaborated:
2548 return GetDeclContextForType(
2549 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2550 case clang::Type::Paren:
2551 return GetDeclContextForType(
2552 llvm::cast<clang::ParenType>(qual_type)->desugar());
2556 // No DeclContext in this type...
2560 static bool GetCompleteQualType(clang::ASTContext *ast,
2561 clang::QualType qual_type,
2562 bool allow_completion = true) {
2563 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2564 switch (type_class) {
2565 case clang::Type::ConstantArray:
2566 case clang::Type::IncompleteArray:
2567 case clang::Type::VariableArray: {
2568 const clang::ArrayType *array_type =
2569 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2572 return GetCompleteQualType(ast, array_type->getElementType(),
2575 case clang::Type::Record: {
2576 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2577 if (cxx_record_decl) {
2578 if (cxx_record_decl->hasExternalLexicalStorage()) {
2579 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2580 const bool fields_loaded =
2581 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2582 if (is_complete && fields_loaded)
2585 if (!allow_completion)
2588 // Call the field_begin() accessor to for it to use the external source
2589 // to load the fields...
2590 clang::ExternalASTSource *external_ast_source =
2591 ast->getExternalSource();
2592 if (external_ast_source) {
2593 external_ast_source->CompleteType(cxx_record_decl);
2594 if (cxx_record_decl->isCompleteDefinition()) {
2595 cxx_record_decl->field_begin();
2596 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2601 const clang::TagType *tag_type =
2602 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2603 return !tag_type->isIncompleteType();
2606 case clang::Type::Enum: {
2607 const clang::TagType *tag_type =
2608 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2610 clang::TagDecl *tag_decl = tag_type->getDecl();
2612 if (tag_decl->getDefinition())
2615 if (!allow_completion)
2618 if (tag_decl->hasExternalLexicalStorage()) {
2620 clang::ExternalASTSource *external_ast_source =
2621 ast->getExternalSource();
2622 if (external_ast_source) {
2623 external_ast_source->CompleteType(tag_decl);
2624 return !tag_type->isIncompleteType();
2633 case clang::Type::ObjCObject:
2634 case clang::Type::ObjCInterface: {
2635 const clang::ObjCObjectType *objc_class_type =
2636 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2637 if (objc_class_type) {
2638 clang::ObjCInterfaceDecl *class_interface_decl =
2639 objc_class_type->getInterface();
2640 // We currently can't complete objective C types through the newly added
2642 // because it only supports TagDecl objects right now...
2643 if (class_interface_decl) {
2644 if (class_interface_decl->getDefinition())
2647 if (!allow_completion)
2650 if (class_interface_decl->hasExternalLexicalStorage()) {
2652 clang::ExternalASTSource *external_ast_source =
2653 ast->getExternalSource();
2654 if (external_ast_source) {
2655 external_ast_source->CompleteType(class_interface_decl);
2656 return !objc_class_type->isIncompleteType();
2665 case clang::Type::Typedef:
2666 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2668 ->getUnderlyingType(),
2671 case clang::Type::Auto:
2672 return GetCompleteQualType(
2673 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2676 case clang::Type::Elaborated:
2677 return GetCompleteQualType(
2678 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2681 case clang::Type::Paren:
2682 return GetCompleteQualType(
2683 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2686 case clang::Type::Attributed:
2687 return GetCompleteQualType(
2688 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2698 static clang::ObjCIvarDecl::AccessControl
2699 ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2702 return clang::ObjCIvarDecl::None;
2704 return clang::ObjCIvarDecl::Public;
2705 case eAccessPrivate:
2706 return clang::ObjCIvarDecl::Private;
2707 case eAccessProtected:
2708 return clang::ObjCIvarDecl::Protected;
2709 case eAccessPackage:
2710 return clang::ObjCIvarDecl::Package;
2712 return clang::ObjCIvarDecl::None;
2715 //----------------------------------------------------------------------
2717 //----------------------------------------------------------------------
2719 bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2720 clang::QualType qual_type(GetCanonicalQualType(type));
2722 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2723 switch (type_class) {
2724 case clang::Type::IncompleteArray:
2725 case clang::Type::VariableArray:
2726 case clang::Type::ConstantArray:
2727 case clang::Type::ExtVector:
2728 case clang::Type::Vector:
2729 case clang::Type::Record:
2730 case clang::Type::ObjCObject:
2731 case clang::Type::ObjCInterface:
2733 case clang::Type::Auto:
2734 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2737 case clang::Type::Elaborated:
2738 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2741 case clang::Type::Typedef:
2742 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2744 ->getUnderlyingType()
2746 case clang::Type::Paren:
2747 return IsAggregateType(
2748 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2752 // The clang type does have a value
2756 bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2757 clang::QualType qual_type(GetCanonicalQualType(type));
2759 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2760 switch (type_class) {
2761 case clang::Type::Record: {
2762 if (const clang::RecordType *record_type =
2763 llvm::dyn_cast_or_null<clang::RecordType>(
2764 qual_type.getTypePtrOrNull())) {
2765 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2766 return record_decl->isAnonymousStructOrUnion();
2771 case clang::Type::Auto:
2772 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2775 case clang::Type::Elaborated:
2776 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2779 case clang::Type::Typedef:
2780 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2782 ->getUnderlyingType()
2784 case clang::Type::Paren:
2785 return IsAnonymousType(
2786 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2790 // The clang type does have a value
2794 bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2795 CompilerType *element_type_ptr,
2796 uint64_t *size, bool *is_incomplete) {
2797 clang::QualType qual_type(GetCanonicalQualType(type));
2799 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2800 switch (type_class) {
2804 case clang::Type::ConstantArray:
2805 if (element_type_ptr)
2806 element_type_ptr->SetCompilerType(
2808 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2810 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2812 .getLimitedValue(ULLONG_MAX);
2814 *is_incomplete = false;
2817 case clang::Type::IncompleteArray:
2818 if (element_type_ptr)
2819 element_type_ptr->SetCompilerType(
2821 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2825 *is_incomplete = true;
2828 case clang::Type::VariableArray:
2829 if (element_type_ptr)
2830 element_type_ptr->SetCompilerType(
2832 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2836 *is_incomplete = false;
2839 case clang::Type::DependentSizedArray:
2840 if (element_type_ptr)
2841 element_type_ptr->SetCompilerType(
2842 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2843 ->getElementType());
2847 *is_incomplete = false;
2850 case clang::Type::Typedef:
2851 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2853 ->getUnderlyingType()
2855 element_type_ptr, size, is_incomplete);
2856 case clang::Type::Auto:
2857 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2860 element_type_ptr, size, is_incomplete);
2861 case clang::Type::Elaborated:
2862 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2865 element_type_ptr, size, is_incomplete);
2866 case clang::Type::Paren:
2868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2869 element_type_ptr, size, is_incomplete);
2871 if (element_type_ptr)
2872 element_type_ptr->Clear();
2876 *is_incomplete = false;
2880 bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2881 CompilerType *element_type, uint64_t *size) {
2882 clang::QualType qual_type(GetCanonicalQualType(type));
2884 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2885 switch (type_class) {
2886 case clang::Type::Vector: {
2887 const clang::VectorType *vector_type =
2888 qual_type->getAs<clang::VectorType>();
2891 *size = vector_type->getNumElements();
2894 CompilerType(getASTContext(), vector_type->getElementType());
2898 case clang::Type::ExtVector: {
2899 const clang::ExtVectorType *ext_vector_type =
2900 qual_type->getAs<clang::ExtVectorType>();
2901 if (ext_vector_type) {
2903 *size = ext_vector_type->getNumElements();
2906 CompilerType(getASTContext(), ext_vector_type->getElementType());
2916 bool ClangASTContext::IsRuntimeGeneratedType(
2917 lldb::opaque_compiler_type_t type) {
2918 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2919 ->GetDeclContextForType(GetQualType(type));
2923 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2926 clang::ObjCInterfaceDecl *result_iface_decl =
2927 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2929 ClangASTMetadata *ast_metadata =
2930 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2933 return (ast_metadata->GetISAPtr() != 0);
2936 bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2937 return GetQualType(type).getUnqualifiedType()->isCharType();
2940 bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2941 const bool allow_completion = false;
2942 return GetCompleteQualType(getASTContext(), GetQualType(type),
2946 bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2947 return GetQualType(type).isConstQualified();
2950 bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2952 CompilerType pointee_or_element_clang_type;
2954 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2956 if (!pointee_or_element_clang_type.IsValid())
2959 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2960 if (pointee_or_element_clang_type.IsCharType()) {
2961 if (type_flags.Test(eTypeIsArray)) {
2962 // We know the size of the array and it could be a C string
2963 // since it is an array of characters
2964 length = llvm::cast<clang::ConstantArrayType>(
2965 GetCanonicalQualType(type).getTypePtr())
2975 bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2976 bool *is_variadic_ptr) {
2978 clang::QualType qual_type(GetCanonicalQualType(type));
2980 if (qual_type->isFunctionType()) {
2981 if (is_variadic_ptr) {
2982 const clang::FunctionProtoType *function_proto_type =
2983 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2984 if (function_proto_type)
2985 *is_variadic_ptr = function_proto_type->isVariadic();
2987 *is_variadic_ptr = false;
2992 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2993 switch (type_class) {
2996 case clang::Type::Typedef:
2997 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2999 ->getUnderlyingType()
3002 case clang::Type::Auto:
3003 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3007 case clang::Type::Elaborated:
3008 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3012 case clang::Type::Paren:
3013 return IsFunctionType(
3014 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3016 case clang::Type::LValueReference:
3017 case clang::Type::RValueReference: {
3018 const clang::ReferenceType *reference_type =
3019 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3021 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3029 // Used to detect "Homogeneous Floating-point Aggregates"
3031 ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3032 CompilerType *base_type_ptr) {
3036 clang::QualType qual_type(GetCanonicalQualType(type));
3037 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3038 switch (type_class) {
3039 case clang::Type::Record:
3040 if (GetCompleteType(type)) {
3041 const clang::CXXRecordDecl *cxx_record_decl =
3042 qual_type->getAsCXXRecordDecl();
3043 if (cxx_record_decl) {
3044 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3047 const clang::RecordType *record_type =
3048 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3050 const clang::RecordDecl *record_decl = record_type->getDecl();
3052 // We are looking for a structure that contains only floating point
3054 clang::RecordDecl::field_iterator field_pos,
3055 field_end = record_decl->field_end();
3056 uint32_t num_fields = 0;
3057 bool is_hva = false;
3058 bool is_hfa = false;
3059 clang::QualType base_qual_type;
3060 uint64_t base_bitwidth = 0;
3061 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3063 clang::QualType field_qual_type = field_pos->getType();
3064 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3065 if (field_qual_type->isFloatingType()) {
3066 if (field_qual_type->isComplexType())
3069 if (num_fields == 0)
3070 base_qual_type = field_qual_type;
3075 if (field_qual_type.getTypePtr() !=
3076 base_qual_type.getTypePtr())
3080 } else if (field_qual_type->isVectorType() ||
3081 field_qual_type->isExtVectorType()) {
3082 if (num_fields == 0) {
3083 base_qual_type = field_qual_type;
3084 base_bitwidth = field_bitwidth;
3089 if (base_bitwidth != field_bitwidth)
3091 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3099 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3106 case clang::Type::Typedef:
3107 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3109 ->getUnderlyingType()
3113 case clang::Type::Auto:
3114 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3119 case clang::Type::Elaborated:
3120 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3130 size_t ClangASTContext::GetNumberOfFunctionArguments(
3131 lldb::opaque_compiler_type_t type) {
3133 clang::QualType qual_type(GetCanonicalQualType(type));
3134 const clang::FunctionProtoType *func =
3135 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3137 return func->getNumParams();
3143 ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3144 const size_t index) {
3146 clang::QualType qual_type(GetQualType(type));
3147 const clang::FunctionProtoType *func =
3148 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3150 if (index < func->getNumParams())
3151 return CompilerType(getASTContext(), func->getParamType(index));
3154 return CompilerType();
3157 bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3159 clang::QualType qual_type(GetCanonicalQualType(type));
3161 if (qual_type->isFunctionPointerType())
3164 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3165 switch (type_class) {
3168 case clang::Type::Typedef:
3169 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3171 ->getUnderlyingType()
3173 case clang::Type::Auto:
3174 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3177 case clang::Type::Elaborated:
3178 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3181 case clang::Type::Paren:
3182 return IsFunctionPointerType(
3183 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3185 case clang::Type::LValueReference:
3186 case clang::Type::RValueReference: {
3187 const clang::ReferenceType *reference_type =
3188 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3190 return IsFunctionPointerType(
3191 reference_type->getPointeeType().getAsOpaquePtr());
3198 bool ClangASTContext::IsBlockPointerType(
3199 lldb::opaque_compiler_type_t type,
3200 CompilerType *function_pointer_type_ptr) {
3202 clang::QualType qual_type(GetCanonicalQualType(type));
3204 if (qual_type->isBlockPointerType()) {
3205 if (function_pointer_type_ptr) {
3206 const clang::BlockPointerType *block_pointer_type =
3207 qual_type->getAs<clang::BlockPointerType>();
3208 QualType pointee_type = block_pointer_type->getPointeeType();
3209 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3210 *function_pointer_type_ptr =
3211 CompilerType(getASTContext(), function_pointer_type);
3216 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3217 switch (type_class) {
3220 case clang::Type::Typedef:
3221 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3223 ->getUnderlyingType()
3225 function_pointer_type_ptr);
3226 case clang::Type::Auto:
3227 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3230 function_pointer_type_ptr);
3231 case clang::Type::Elaborated:
3232 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3235 function_pointer_type_ptr);
3236 case clang::Type::Paren:
3237 return IsBlockPointerType(
3238 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3239 function_pointer_type_ptr);
3241 case clang::Type::LValueReference:
3242 case clang::Type::RValueReference: {
3243 const clang::ReferenceType *reference_type =
3244 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3246 return IsBlockPointerType(
3247 reference_type->getPointeeType().getAsOpaquePtr(),
3248 function_pointer_type_ptr);
3255 bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3260 clang::QualType qual_type(GetCanonicalQualType(type));
3261 const clang::BuiltinType *builtin_type =
3262 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3265 if (builtin_type->isInteger()) {
3266 is_signed = builtin_type->isSignedInteger();
3274 bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3277 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3278 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3281 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3290 bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3291 CompilerType *pointee_type) {
3293 clang::QualType qual_type(GetCanonicalQualType(type));
3294 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3295 switch (type_class) {
3296 case clang::Type::Builtin:
3297 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3300 case clang::BuiltinType::ObjCId:
3301 case clang::BuiltinType::ObjCClass:
3305 case clang::Type::ObjCObjectPointer:
3307 pointee_type->SetCompilerType(
3308 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3309 ->getPointeeType());
3311 case clang::Type::BlockPointer:
3313 pointee_type->SetCompilerType(
3315 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3317 case clang::Type::Pointer:
3319 pointee_type->SetCompilerType(
3321 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3323 case clang::Type::MemberPointer:
3325 pointee_type->SetCompilerType(
3327 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3329 case clang::Type::Typedef:
3330 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3332 ->getUnderlyingType()
3335 case clang::Type::Auto:
3336 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3340 case clang::Type::Elaborated:
3341 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3345 case clang::Type::Paren:
3346 return IsPointerType(
3347 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3354 pointee_type->Clear();
3358 bool ClangASTContext::IsPointerOrReferenceType(
3359 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3361 clang::QualType qual_type(GetCanonicalQualType(type));
3362 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3363 switch (type_class) {
3364 case clang::Type::Builtin:
3365 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3368 case clang::BuiltinType::ObjCId:
3369 case clang::BuiltinType::ObjCClass:
3373 case clang::Type::ObjCObjectPointer:
3375 pointee_type->SetCompilerType(
3376 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3377 ->getPointeeType());
3379 case clang::Type::BlockPointer:
3381 pointee_type->SetCompilerType(
3383 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3385 case clang::Type::Pointer:
3387 pointee_type->SetCompilerType(
3389 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3391 case clang::Type::MemberPointer:
3393 pointee_type->SetCompilerType(
3395 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3397 case clang::Type::LValueReference:
3399 pointee_type->SetCompilerType(
3401 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3403 case clang::Type::RValueReference:
3405 pointee_type->SetCompilerType(
3407 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3409 case clang::Type::Typedef:
3410 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3412 ->getUnderlyingType()
3415 case clang::Type::Auto:
3416 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3420 case clang::Type::Elaborated:
3421 return IsPointerOrReferenceType(
3422 llvm::cast<clang::ElaboratedType>(qual_type)
3426 case clang::Type::Paren:
3427 return IsPointerOrReferenceType(
3428 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3435 pointee_type->Clear();
3439 bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3440 CompilerType *pointee_type,
3443 clang::QualType qual_type(GetCanonicalQualType(type));
3444 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3446 switch (type_class) {
3447 case clang::Type::LValueReference:
3449 pointee_type->SetCompilerType(
3451 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3455 case clang::Type::RValueReference:
3457 pointee_type->SetCompilerType(
3459 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3463 case clang::Type::Typedef:
3464 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3466 ->getUnderlyingType()
3468 pointee_type, is_rvalue);
3469 case clang::Type::Auto:
3470 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3473 pointee_type, is_rvalue);
3474 case clang::Type::Elaborated:
3475 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3478 pointee_type, is_rvalue);
3479 case clang::Type::Paren:
3480 return IsReferenceType(
3481 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3482 pointee_type, is_rvalue);
3489 pointee_type->Clear();
3493 bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3494 uint32_t &count, bool &is_complex) {
3496 clang::QualType qual_type(GetCanonicalQualType(type));
3498 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3499 qual_type->getCanonicalTypeInternal())) {
3500 clang::BuiltinType::Kind kind = BT->getKind();
3501 if (kind >= clang::BuiltinType::Float &&
3502 kind <= clang::BuiltinType::LongDouble) {
3507 } else if (const clang::ComplexType *CT =
3508 llvm::dyn_cast<clang::ComplexType>(
3509 qual_type->getCanonicalTypeInternal())) {
3510 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3516 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3517 qual_type->getCanonicalTypeInternal())) {
3518 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3520 count = VT->getNumElements();
3531 bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3535 clang::QualType qual_type(GetQualType(type));
3536 const clang::TagType *tag_type =
3537 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3539 clang::TagDecl *tag_decl = tag_type->getDecl();
3541 return tag_decl->isCompleteDefinition();
3544 const clang::ObjCObjectType *objc_class_type =
3545 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3546 if (objc_class_type) {
3547 clang::ObjCInterfaceDecl *class_interface_decl =
3548 objc_class_type->getInterface();
3549 if (class_interface_decl)
3550 return class_interface_decl->getDefinition() != nullptr;
3557 bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3559 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3561 const clang::ObjCObjectPointerType *obj_pointer_type =
3562 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3564 if (obj_pointer_type)
3565 return obj_pointer_type->isObjCClassType();
3570 bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3571 if (ClangUtil::IsClangType(type))
3572 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3576 bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3579 clang::QualType qual_type(GetCanonicalQualType(type));
3580 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3581 return (type_class == clang::Type::Record);
3584 bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3587 clang::QualType qual_type(GetCanonicalQualType(type));
3588 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3589 return (type_class == clang::Type::Enum);
3592 bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3594 clang::QualType qual_type(GetCanonicalQualType(type));
3595 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3596 switch (type_class) {
3597 case clang::Type::Record:
3598 if (GetCompleteType(type)) {
3599 const clang::RecordType *record_type =
3600 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3601 const clang::RecordDecl *record_decl = record_type->getDecl();
3603 const clang::CXXRecordDecl *cxx_record_decl =
3604 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3605 if (cxx_record_decl)
3606 return cxx_record_decl->isPolymorphic();
3618 bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3619 CompilerType *dynamic_pointee_type,
3620 bool check_cplusplus,
3622 clang::QualType pointee_qual_type;
3624 clang::QualType qual_type(GetCanonicalQualType(type));
3625 bool success = false;
3626 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3627 switch (type_class) {
3628 case clang::Type::Builtin:
3630 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3631 clang::BuiltinType::ObjCId) {
3632 if (dynamic_pointee_type)
3633 dynamic_pointee_type->SetCompilerType(this, type);
3638 case clang::Type::ObjCObjectPointer:
3640 if (auto objc_pointee_type =
3641 qual_type->getPointeeType().getTypePtrOrNull()) {
3642 if (auto objc_object_type =
3643 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3644 objc_pointee_type)) {
3645 if (objc_object_type->isObjCClass())
3649 if (dynamic_pointee_type)
3650 dynamic_pointee_type->SetCompilerType(
3652 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3653 ->getPointeeType());
3658 case clang::Type::Pointer:
3660 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3664 case clang::Type::LValueReference:
3665 case clang::Type::RValueReference:
3667 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3671 case clang::Type::Typedef:
3672 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3674 ->getUnderlyingType()
3676 dynamic_pointee_type, check_cplusplus,
3679 case clang::Type::Auto:
3680 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3683 dynamic_pointee_type, check_cplusplus,
3686 case clang::Type::Elaborated:
3687 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3690 dynamic_pointee_type, check_cplusplus,
3693 case clang::Type::Paren:
3694 return IsPossibleDynamicType(
3695 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3696 dynamic_pointee_type, check_cplusplus, check_objc);
3702 // Check to make sure what we are pointing too is a possible dynamic C++
3704 // We currently accept any "void *" (in case we have a class that has been
3705 // watered down to an opaque pointer) and virtual C++ classes.
3706 const clang::Type::TypeClass pointee_type_class =
3707 pointee_qual_type.getCanonicalType()->getTypeClass();
3708 switch (pointee_type_class) {
3709 case clang::Type::Builtin:
3710 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3711 case clang::BuiltinType::UnknownAny:
3712 case clang::BuiltinType::Void:
3713 if (dynamic_pointee_type)
3714 dynamic_pointee_type->SetCompilerType(getASTContext(),
3722 case clang::Type::Record:
3723 if (check_cplusplus) {
3724 clang::CXXRecordDecl *cxx_record_decl =
3725 pointee_qual_type->getAsCXXRecordDecl();
3726 if (cxx_record_decl) {
3727 bool is_complete = cxx_record_decl->isCompleteDefinition();
3730 success = cxx_record_decl->isDynamicClass();
3732 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3733 getASTContext(), cxx_record_decl);
3735 success = metadata->GetIsDynamicCXXType();
3737 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3740 success = cxx_record_decl->isDynamicClass();
3747 if (dynamic_pointee_type)
3748 dynamic_pointee_type->SetCompilerType(getASTContext(),
3756 case clang::Type::ObjCObject:
3757 case clang::Type::ObjCInterface:
3759 if (dynamic_pointee_type)
3760 dynamic_pointee_type->SetCompilerType(getASTContext(),
3771 if (dynamic_pointee_type)
3772 dynamic_pointee_type->Clear();
3776 bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3780 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3783 bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3786 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3789 bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3792 return GetCanonicalQualType(type)->isVoidType();
3795 bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3796 return ClangASTContextSupportsLanguage(language);
3799 bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3800 std::string &class_name) {
3802 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3803 if (!qual_type.isNull()) {
3804 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3805 if (cxx_record_decl) {
3806 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3815 bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3819 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3820 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3825 bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3828 clang::QualType qual_type(GetCanonicalQualType(type));
3829 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3831 return tag_type->isBeingDefined();
3835 bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3836 CompilerType *class_type_ptr) {
3840 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3842 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3843 if (class_type_ptr) {
3844 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3845 const clang::ObjCObjectPointerType *obj_pointer_type =
3846 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3847 if (obj_pointer_type == nullptr)
3848 class_type_ptr->Clear();
3850 class_type_ptr->SetCompilerType(
3851 type.GetTypeSystem(),
3852 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3859 class_type_ptr->Clear();
3863 bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3864 std::string &class_name) {
3868 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3870 const clang::ObjCObjectType *object_type =
3871 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3873 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3875 class_name = interface->getNameAsString();
3882 //----------------------------------------------------------------------
3884 //----------------------------------------------------------------------
3886 bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3889 const bool allow_completion = true;
3890 return GetCompleteQualType(getASTContext(), GetQualType(type),
3894 ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3895 std::string type_name;
3897 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3898 clang::QualType qual_type(GetQualType(type));
3899 printing_policy.SuppressTagKeyword = true;
3900 const clang::TypedefType *typedef_type =
3901 qual_type->getAs<clang::TypedefType>();
3903 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3904 type_name = typedef_decl->getQualifiedNameAsString();
3906 type_name = qual_type.getAsString(printing_policy);
3909 return ConstString(type_name);
3913 ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3914 CompilerType *pointee_or_element_clang_type) {
3918 if (pointee_or_element_clang_type)
3919 pointee_or_element_clang_type->Clear();
3921 clang::QualType qual_type(GetQualType(type));
3923 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3924 switch (type_class) {
3925 case clang::Type::Builtin: {
3926 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3927 qual_type->getCanonicalTypeInternal());
3929 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3930 switch (builtin_type->getKind()) {
3931 case clang::BuiltinType::ObjCId:
3932 case clang::BuiltinType::ObjCClass:
3933 if (pointee_or_element_clang_type)
3934 pointee_or_element_clang_type->SetCompilerType(
3935 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3936 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3939 case clang::BuiltinType::ObjCSel:
3940 if (pointee_or_element_clang_type)
3941 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3942 getASTContext()->CharTy);
3943 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3946 case clang::BuiltinType::Bool:
3947 case clang::BuiltinType::Char_U:
3948 case clang::BuiltinType::UChar:
3949 case clang::BuiltinType::WChar_U:
3950 case clang::BuiltinType::Char16:
3951 case clang::BuiltinType::Char32:
3952 case clang::BuiltinType::UShort:
3953 case clang::BuiltinType::UInt:
3954 case clang::BuiltinType::ULong:
3955 case clang::BuiltinType::ULongLong:
3956 case clang::BuiltinType::UInt128:
3957 case clang::BuiltinType::Char_S:
3958 case clang::BuiltinType::SChar:
3959 case clang::BuiltinType::WChar_S:
3960 case clang::BuiltinType::Short:
3961 case clang::BuiltinType::Int:
3962 case clang::BuiltinType::Long:
3963 case clang::BuiltinType::LongLong:
3964 case clang::BuiltinType::Int128:
3965 case clang::BuiltinType::Float:
3966 case clang::BuiltinType::Double:
3967 case clang::BuiltinType::LongDouble:
3968 builtin_type_flags |= eTypeIsScalar;
3969 if (builtin_type->isInteger()) {
3970 builtin_type_flags |= eTypeIsInteger;
3971 if (builtin_type->isSignedInteger())
3972 builtin_type_flags |= eTypeIsSigned;
3973 } else if (builtin_type->isFloatingPoint())
3974 builtin_type_flags |= eTypeIsFloat;
3979 return builtin_type_flags;
3982 case clang::Type::BlockPointer:
3983 if (pointee_or_element_clang_type)
3984 pointee_or_element_clang_type->SetCompilerType(
3985 getASTContext(), qual_type->getPointeeType());
3986 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3988 case clang::Type::Complex: {
3989 uint32_t complex_type_flags =
3990 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3991 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3992 qual_type->getCanonicalTypeInternal());
3994 clang::QualType complex_element_type(complex_type->getElementType());
3995 if (complex_element_type->isIntegerType())
3996 complex_type_flags |= eTypeIsFloat;
3997 else if (complex_element_type->isFloatingType())
3998 complex_type_flags |= eTypeIsInteger;
4000 return complex_type_flags;
4003 case clang::Type::ConstantArray:
4004 case clang::Type::DependentSizedArray:
4005 case clang::Type::IncompleteArray:
4006 case clang::Type::VariableArray:
4007 if (pointee_or_element_clang_type)
4008 pointee_or_element_clang_type->SetCompilerType(
4009 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4010 ->getElementType());
4011 return eTypeHasChildren | eTypeIsArray;
4013 case clang::Type::DependentName:
4015 case clang::Type::DependentSizedExtVector:
4016 return eTypeHasChildren | eTypeIsVector;
4017 case clang::Type::DependentTemplateSpecialization:
4018 return eTypeIsTemplate;
4019 case clang::Type::Decltype:
4022 case clang::Type::Enum:
4023 if (pointee_or_element_clang_type)
4024 pointee_or_element_clang_type->SetCompilerType(
4026 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4027 return eTypeIsEnumeration | eTypeHasValue;
4029 case clang::Type::Auto:
4030 return CompilerType(
4032 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4033 .GetTypeInfo(pointee_or_element_clang_type);
4034 case clang::Type::Elaborated:
4035 return CompilerType(
4037 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4038 .GetTypeInfo(pointee_or_element_clang_type);
4039 case clang::Type::Paren:
4040 return CompilerType(getASTContext(),
4041 llvm::cast<clang::ParenType>(qual_type)->desugar())
4042 .GetTypeInfo(pointee_or_element_clang_type);
4044 case clang::Type::FunctionProto:
4045 return eTypeIsFuncPrototype | eTypeHasValue;
4046 case clang::Type::FunctionNoProto:
4047 return eTypeIsFuncPrototype | eTypeHasValue;
4048 case clang::Type::InjectedClassName:
4051 case clang::Type::LValueReference:
4052 case clang::Type::RValueReference:
4053 if (pointee_or_element_clang_type)
4054 pointee_or_element_clang_type->SetCompilerType(
4056 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4057 ->getPointeeType());
4058 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4060 case clang::Type::MemberPointer:
4061 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4063 case clang::Type::ObjCObjectPointer:
4064 if (pointee_or_element_clang_type)
4065 pointee_or_element_clang_type->SetCompilerType(
4066 getASTContext(), qual_type->getPointeeType());
4067 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4070 case clang::Type::ObjCObject:
4071 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4072 case clang::Type::ObjCInterface:
4073 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4075 case clang::Type::Pointer:
4076 if (pointee_or_element_clang_type)
4077 pointee_or_element_clang_type->SetCompilerType(
4078 getASTContext(), qual_type->getPointeeType());
4079 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4081 case clang::Type::Record:
4082 if (qual_type->getAsCXXRecordDecl())
4083 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4085 return eTypeHasChildren | eTypeIsStructUnion;
4087 case clang::Type::SubstTemplateTypeParm:
4088 return eTypeIsTemplate;
4089 case clang::Type::TemplateTypeParm:
4090 return eTypeIsTemplate;
4091 case clang::Type::TemplateSpecialization:
4092 return eTypeIsTemplate;
4094 case clang::Type::Typedef:
4095 return eTypeIsTypedef |
4096 CompilerType(getASTContext(),
4097 llvm::cast<clang::TypedefType>(qual_type)
4099 ->getUnderlyingType())
4100 .GetTypeInfo(pointee_or_element_clang_type);
4101 case clang::Type::TypeOfExpr:
4103 case clang::Type::TypeOf:
4105 case clang::Type::UnresolvedUsing:
4108 case clang::Type::ExtVector:
4109 case clang::Type::Vector: {
4110 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4111 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4112 qual_type->getCanonicalTypeInternal());
4114 if (vector_type->isIntegerType())
4115 vector_type_flags |= eTypeIsFloat;
4116 else if (vector_type->isFloatingType())
4117 vector_type_flags |= eTypeIsInteger;
4119 return vector_type_flags;
4128 ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4130 return lldb::eLanguageTypeC;
4132 // If the type is a reference, then resolve it to what it refers to first:
4133 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4134 if (qual_type->isAnyPointerType()) {
4135 if (qual_type->isObjCObjectPointerType())
4136 return lldb::eLanguageTypeObjC;
4138 clang::QualType pointee_type(qual_type->getPointeeType());
4139 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4140 return lldb::eLanguageTypeC_plus_plus;
4141 if (pointee_type->isObjCObjectOrInterfaceType())
4142 return lldb::eLanguageTypeObjC;
4143 if (pointee_type->isObjCClassType())
4144 return lldb::eLanguageTypeObjC;
4145 if (pointee_type.getTypePtr() ==
4146 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4147 return lldb::eLanguageTypeObjC;
4149 if (qual_type->isObjCObjectOrInterfaceType())
4150 return lldb::eLanguageTypeObjC;
4151 if (qual_type->getAsCXXRecordDecl())
4152 return lldb::eLanguageTypeC_plus_plus;
4153 switch (qual_type->getTypeClass()) {
4156 case clang::Type::Builtin:
4157 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4159 case clang::BuiltinType::Void:
4160 case clang::BuiltinType::Bool:
4161 case clang::BuiltinType::Char_U:
4162 case clang::BuiltinType::UChar:
4163 case clang::BuiltinType::WChar_U:
4164 case clang::BuiltinType::Char16:
4165 case clang::BuiltinType::Char32:
4166 case clang::BuiltinType::UShort:
4167 case clang::BuiltinType::UInt:
4168 case clang::BuiltinType::ULong:
4169 case clang::BuiltinType::ULongLong:
4170 case clang::BuiltinType::UInt128:
4171 case clang::BuiltinType::Char_S:
4172 case clang::BuiltinType::SChar:
4173 case clang::BuiltinType::WChar_S:
4174 case clang::BuiltinType::Short:
4175 case clang::BuiltinType::Int:
4176 case clang::BuiltinType::Long:
4177 case clang::BuiltinType::LongLong:
4178 case clang::BuiltinType::Int128:
4179 case clang::BuiltinType::Float:
4180 case clang::BuiltinType::Double:
4181 case clang::BuiltinType::LongDouble:
4184 case clang::BuiltinType::NullPtr:
4185 return eLanguageTypeC_plus_plus;
4187 case clang::BuiltinType::ObjCId:
4188 case clang::BuiltinType::ObjCClass:
4189 case clang::BuiltinType::ObjCSel:
4190 return eLanguageTypeObjC;
4192 case clang::BuiltinType::Dependent:
4193 case clang::BuiltinType::Overload:
4194 case clang::BuiltinType::BoundMember:
4195 case clang::BuiltinType::UnknownAny:
4199 case clang::Type::Typedef:
4200 return CompilerType(getASTContext(),
4201 llvm::cast<clang::TypedefType>(qual_type)
4203 ->getUnderlyingType())
4204 .GetMinimumLanguage();
4207 return lldb::eLanguageTypeC;
4211 ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4213 return lldb::eTypeClassInvalid;
4215 clang::QualType qual_type(GetQualType(type));
4217 switch (qual_type->getTypeClass()) {
4218 case clang::Type::UnaryTransform:
4220 case clang::Type::FunctionNoProto:
4221 return lldb::eTypeClassFunction;
4222 case clang::Type::FunctionProto:
4223 return lldb::eTypeClassFunction;
4224 case clang::Type::IncompleteArray:
4225 return lldb::eTypeClassArray;
4226 case clang::Type::VariableArray:
4227 return lldb::eTypeClassArray;
4228 case clang::Type::ConstantArray:
4229 return lldb::eTypeClassArray;
4230 case clang::Type::DependentSizedArray:
4231 return lldb::eTypeClassArray;
4232 case clang::Type::DependentSizedExtVector:
4233 return lldb::eTypeClassVector;
4234 case clang::Type::ExtVector:
4235 return lldb::eTypeClassVector;
4236 case clang::Type::Vector:
4237 return lldb::eTypeClassVector;
4238 case clang::Type::Builtin:
4239 return lldb::eTypeClassBuiltin;
4240 case clang::Type::ObjCObjectPointer:
4241 return lldb::eTypeClassObjCObjectPointer;
4242 case clang::Type::BlockPointer:
4243 return lldb::eTypeClassBlockPointer;
4244 case clang::Type::Pointer:
4245 return lldb::eTypeClassPointer;
4246 case clang::Type::LValueReference:
4247 return lldb::eTypeClassReference;
4248 case clang::Type::RValueReference:
4249 return lldb::eTypeClassReference;
4250 case clang::Type::MemberPointer:
4251 return lldb::eTypeClassMemberPointer;
4252 case clang::Type::Complex:
4253 if (qual_type->isComplexType())
4254 return lldb::eTypeClassComplexFloat;
4256 return lldb::eTypeClassComplexInteger;
4257 case clang::Type::ObjCObject:
4258 return lldb::eTypeClassObjCObject;
4259 case clang::Type::ObjCInterface:
4260 return lldb::eTypeClassObjCInterface;
4261 case clang::Type::Record: {
4262 const clang::RecordType *record_type =
4263 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4264 const clang::RecordDecl *record_decl = record_type->getDecl();
4265 if (record_decl->isUnion())
4266 return lldb::eTypeClassUnion;
4267 else if (record_decl->isStruct())
4268 return lldb::eTypeClassStruct;
4270 return lldb::eTypeClassClass;
4272 case clang::Type::Enum:
4273 return lldb::eTypeClassEnumeration;
4274 case clang::Type::Typedef:
4275 return lldb::eTypeClassTypedef;
4276 case clang::Type::UnresolvedUsing:
4278 case clang::Type::Paren:
4279 return CompilerType(getASTContext(),
4280 llvm::cast<clang::ParenType>(qual_type)->desugar())
4282 case clang::Type::Auto:
4283 return CompilerType(
4285 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4287 case clang::Type::Elaborated:
4288 return CompilerType(
4290 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4293 case clang::Type::Attributed:
4295 case clang::Type::TemplateTypeParm:
4297 case clang::Type::SubstTemplateTypeParm:
4299 case clang::Type::SubstTemplateTypeParmPack:
4301 case clang::Type::InjectedClassName:
4303 case clang::Type::DependentName:
4305 case clang::Type::DependentTemplateSpecialization:
4307 case clang::Type::PackExpansion:
4310 case clang::Type::TypeOfExpr:
4312 case clang::Type::TypeOf:
4314 case clang::Type::Decltype:
4316 case clang::Type::TemplateSpecialization:
4318 case clang::Type::Atomic:
4320 case clang::Type::Pipe:
4323 // pointer type decayed from an array or function type.
4324 case clang::Type::Decayed:
4326 case clang::Type::Adjusted:
4328 case clang::Type::ObjCTypeParam:
4331 // We don't know hot to display this type...
4332 return lldb::eTypeClassOther;
4335 unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4337 return GetQualType(type).getQualifiers().getCVRQualifiers();
4341 //----------------------------------------------------------------------
4342 // Creating related types
4343 //----------------------------------------------------------------------
4346 ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4349 clang::QualType qual_type(GetCanonicalQualType(type));
4351 const clang::Type *array_eletype =
4352 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4355 return CompilerType();
4357 CompilerType element_type(getASTContext(),
4358 array_eletype->getCanonicalTypeUnqualified());
4360 // TODO: the real stride will be >= this value.. find the real one!
4362 *stride = element_type.GetByteSize(nullptr);
4364 return element_type;
4366 return CompilerType();
4369 CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4372 clang::QualType qual_type(GetCanonicalQualType(type));
4373 if (clang::ASTContext *ast_ctx = getASTContext()) {
4375 return CompilerType(
4376 ast_ctx, ast_ctx->getConstantArrayType(
4377 qual_type, llvm::APInt(64, size),
4378 clang::ArrayType::ArraySizeModifier::Normal, 0));
4380 return CompilerType(
4382 ast_ctx->getIncompleteArrayType(
4383 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4387 return CompilerType();
4391 ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4393 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4394 return CompilerType();
4397 static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4398 clang::QualType qual_type) {
4399 if (qual_type->isPointerType())
4400 qual_type = ast->getPointerType(
4401 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4403 qual_type = qual_type.getUnqualifiedType();
4404 qual_type.removeLocalConst();
4405 qual_type.removeLocalRestrict();
4406 qual_type.removeLocalVolatile();
4411 ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4413 return CompilerType(
4415 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4416 return CompilerType();
4419 int ClangASTContext::GetFunctionArgumentCount(
4420 lldb::opaque_compiler_type_t type) {
4422 const clang::FunctionProtoType *func =
4423 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4425 return func->getNumParams();
4430 CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4431 lldb::opaque_compiler_type_t type, size_t idx) {
4433 const clang::FunctionProtoType *func =
4434 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4436 const uint32_t num_args = func->getNumParams();
4438 return CompilerType(getASTContext(), func->getParamType(idx));
4441 return CompilerType();
4445 ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4447 clang::QualType qual_type(GetQualType(type));
4448 const clang::FunctionProtoType *func =
4449 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4451 return CompilerType(getASTContext(), func->getReturnType());
4453 return CompilerType();
4457 ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4458 size_t num_functions = 0;
4460 clang::QualType qual_type(GetCanonicalQualType(type));
4461 switch (qual_type->getTypeClass()) {
4462 case clang::Type::Record:
4463 if (GetCompleteQualType(getASTContext(), qual_type)) {
4464 const clang::RecordType *record_type =
4465 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4466 const clang::RecordDecl *record_decl = record_type->getDecl();
4467 assert(record_decl);
4468 const clang::CXXRecordDecl *cxx_record_decl =
4469 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4470 if (cxx_record_decl)
4471 num_functions = std::distance(cxx_record_decl->method_begin(),
4472 cxx_record_decl->method_end());
4476 case clang::Type::ObjCObjectPointer: {
4477 const clang::ObjCObjectPointerType *objc_class_type =
4478 qual_type->getAsObjCInterfacePointerType();
4479 const clang::ObjCInterfaceType *objc_interface_type =
4480 objc_class_type->getInterfaceType();
4481 if (objc_interface_type &&
4482 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4483 clang::ObjCInterfaceDecl *class_interface_decl =
4484 objc_interface_type->getDecl();
4485 if (class_interface_decl) {
4486 num_functions = std::distance(class_interface_decl->meth_begin(),
4487 class_interface_decl->meth_end());
4493 case clang::Type::ObjCObject:
4494 case clang::Type::ObjCInterface:
4495 if (GetCompleteType(type)) {
4496 const clang::ObjCObjectType *objc_class_type =
4497 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4498 if (objc_class_type) {
4499 clang::ObjCInterfaceDecl *class_interface_decl =
4500 objc_class_type->getInterface();
4501 if (class_interface_decl)
4502 num_functions = std::distance(class_interface_decl->meth_begin(),
4503 class_interface_decl->meth_end());
4508 case clang::Type::Typedef:
4509 return CompilerType(getASTContext(),
4510 llvm::cast<clang::TypedefType>(qual_type)
4512 ->getUnderlyingType())
4513 .GetNumMemberFunctions();
4515 case clang::Type::Auto:
4516 return CompilerType(
4518 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4519 .GetNumMemberFunctions();
4521 case clang::Type::Elaborated:
4522 return CompilerType(
4524 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4525 .GetNumMemberFunctions();
4527 case clang::Type::Paren:
4528 return CompilerType(getASTContext(),
4529 llvm::cast<clang::ParenType>(qual_type)->desugar())
4530 .GetNumMemberFunctions();
4536 return num_functions;
4539 TypeMemberFunctionImpl
4540 ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4543 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4544 CompilerType clang_type;
4545 CompilerDecl clang_decl;
4547 clang::QualType qual_type(GetCanonicalQualType(type));
4548 switch (qual_type->getTypeClass()) {
4549 case clang::Type::Record:
4550 if (GetCompleteQualType(getASTContext(), qual_type)) {
4551 const clang::RecordType *record_type =
4552 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4553 const clang::RecordDecl *record_decl = record_type->getDecl();
4554 assert(record_decl);
4555 const clang::CXXRecordDecl *cxx_record_decl =
4556 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4557 if (cxx_record_decl) {
4558 auto method_iter = cxx_record_decl->method_begin();
4559 auto method_end = cxx_record_decl->method_end();
4561 static_cast<size_t>(std::distance(method_iter, method_end))) {
4562 std::advance(method_iter, idx);
4563 clang::CXXMethodDecl *cxx_method_decl =
4564 method_iter->getCanonicalDecl();
4565 if (cxx_method_decl) {
4566 name = cxx_method_decl->getDeclName().getAsString();
4567 if (cxx_method_decl->isStatic())
4568 kind = lldb::eMemberFunctionKindStaticMethod;
4569 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4570 kind = lldb::eMemberFunctionKindConstructor;
4571 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4572 kind = lldb::eMemberFunctionKindDestructor;
4574 kind = lldb::eMemberFunctionKindInstanceMethod;
4575 clang_type = CompilerType(
4576 this, cxx_method_decl->getType().getAsOpaquePtr());
4577 clang_decl = CompilerDecl(this, cxx_method_decl);
4584 case clang::Type::ObjCObjectPointer: {
4585 const clang::ObjCObjectPointerType *objc_class_type =
4586 qual_type->getAsObjCInterfacePointerType();
4587 const clang::ObjCInterfaceType *objc_interface_type =
4588 objc_class_type->getInterfaceType();
4589 if (objc_interface_type &&
4590 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
4591 clang::ObjCInterfaceDecl *class_interface_decl =
4592 objc_interface_type->getDecl();
4593 if (class_interface_decl) {
4594 auto method_iter = class_interface_decl->meth_begin();
4595 auto method_end = class_interface_decl->meth_end();
4597 static_cast<size_t>(std::distance(method_iter, method_end))) {
4598 std::advance(method_iter, idx);
4599 clang::ObjCMethodDecl *objc_method_decl =
4600 method_iter->getCanonicalDecl();
4601 if (objc_method_decl) {
4602 clang_decl = CompilerDecl(this, objc_method_decl);
4603 name = objc_method_decl->getSelector().getAsString();
4604 if (objc_method_decl->isClassMethod())
4605 kind = lldb::eMemberFunctionKindStaticMethod;
4607 kind = lldb::eMemberFunctionKindInstanceMethod;
4615 case clang::Type::ObjCObject:
4616 case clang::Type::ObjCInterface:
4617 if (GetCompleteType(type)) {
4618 const clang::ObjCObjectType *objc_class_type =
4619 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4620 if (objc_class_type) {
4621 clang::ObjCInterfaceDecl *class_interface_decl =
4622 objc_class_type->getInterface();
4623 if (class_interface_decl) {
4624 auto method_iter = class_interface_decl->meth_begin();
4625 auto method_end = class_interface_decl->meth_end();
4627 static_cast<size_t>(std::distance(method_iter, method_end))) {
4628 std::advance(method_iter, idx);
4629 clang::ObjCMethodDecl *objc_method_decl =
4630 method_iter->getCanonicalDecl();
4631 if (objc_method_decl) {
4632 clang_decl = CompilerDecl(this, objc_method_decl);
4633 name = objc_method_decl->getSelector().getAsString();
4634 if (objc_method_decl->isClassMethod())
4635 kind = lldb::eMemberFunctionKindStaticMethod;
4637 kind = lldb::eMemberFunctionKindInstanceMethod;
4645 case clang::Type::Typedef:
4646 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4648 ->getUnderlyingType()
4652 case clang::Type::Auto:
4653 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4658 case clang::Type::Elaborated:
4659 return GetMemberFunctionAtIndex(
4660 llvm::cast<clang::ElaboratedType>(qual_type)
4665 case clang::Type::Paren:
4666 return GetMemberFunctionAtIndex(
4667 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4675 if (kind == eMemberFunctionKindUnknown)
4676 return TypeMemberFunctionImpl();
4678 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4682 ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4684 return CompilerType(getASTContext(),
4685 GetQualType(type).getNonReferenceType());
4686 return CompilerType();
4689 CompilerType ClangASTContext::CreateTypedefType(
4690 const CompilerType &type, const char *typedef_name,
4691 const CompilerDeclContext &compiler_decl_ctx) {
4692 if (type && typedef_name && typedef_name[0]) {
4693 ClangASTContext *ast =
4694 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4696 return CompilerType();
4697 clang::ASTContext *clang_ast = ast->getASTContext();
4698 clang::QualType qual_type(ClangUtil::GetQualType(type));
4700 clang::DeclContext *decl_ctx =
4701 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4702 if (decl_ctx == nullptr)
4703 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4705 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4706 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4707 &clang_ast->Idents.get(typedef_name),
4708 clang_ast->getTrivialTypeSourceInfo(qual_type));
4710 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4712 // Get a uniqued clang::QualType for the typedef decl type
4713 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4715 return CompilerType();
4719 ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4721 clang::QualType qual_type(GetQualType(type));
4722 return CompilerType(getASTContext(),
4723 qual_type.getTypePtr()->getPointeeType());
4725 return CompilerType();
4729 ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4731 clang::QualType qual_type(GetQualType(type));
4733 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4734 switch (type_class) {
4735 case clang::Type::ObjCObject:
4736 case clang::Type::ObjCInterface:
4737 return CompilerType(getASTContext(),
4738 getASTContext()->getObjCObjectPointerType(qual_type));
4741 return CompilerType(getASTContext(),
4742 getASTContext()->getPointerType(qual_type));
4745 return CompilerType();
4749 ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4751 return CompilerType(this, getASTContext()
4752 ->getLValueReferenceType(GetQualType(type))
4755 return CompilerType();
4759 ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4761 return CompilerType(this, getASTContext()
4762 ->getRValueReferenceType(GetQualType(type))
4765 return CompilerType();
4769 ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4771 clang::QualType result(GetQualType(type));
4773 return CompilerType(this, result.getAsOpaquePtr());
4775 return CompilerType();
4779 ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4781 clang::QualType result(GetQualType(type));
4782 result.addVolatile();
4783 return CompilerType(this, result.getAsOpaquePtr());
4785 return CompilerType();
4789 ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4791 clang::QualType result(GetQualType(type));
4792 result.addRestrict();
4793 return CompilerType(this, result.getAsOpaquePtr());
4795 return CompilerType();
4799 ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4800 const char *typedef_name,
4801 const CompilerDeclContext &compiler_decl_ctx) {
4803 clang::ASTContext *clang_ast = getASTContext();
4804 clang::QualType qual_type(GetQualType(type));
4806 clang::DeclContext *decl_ctx =
4807 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4808 if (decl_ctx == nullptr)
4809 decl_ctx = getASTContext()->getTranslationUnitDecl();
4811 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4812 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4813 &clang_ast->Idents.get(typedef_name),
4814 clang_ast->getTrivialTypeSourceInfo(qual_type));
4816 clang::TagDecl *tdecl = nullptr;
4817 if (!qual_type.isNull()) {
4818 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4819 tdecl = rt->getDecl();
4820 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4821 tdecl = et->getDecl();
4824 // Check whether this declaration is an anonymous struct, union, or enum,
4825 // hidden behind a typedef. If so, we
4826 // try to check whether we have a typedef tag to attach to the original
4827 // record declaration
4828 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4829 tdecl->setTypedefNameForAnonDecl(decl);
4831 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4833 // Get a uniqued clang::QualType for the typedef decl type
4834 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4836 return CompilerType();
4840 ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4842 const clang::TypedefType *typedef_type =
4843 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4845 return CompilerType(getASTContext(),
4846 typedef_type->getDecl()->getUnderlyingType());
4848 return CompilerType();
4851 //----------------------------------------------------------------------
4852 // Create related types using the current type's AST
4853 //----------------------------------------------------------------------
4855 CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4856 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4858 //----------------------------------------------------------------------
4859 // Exploring the type
4860 //----------------------------------------------------------------------
4862 uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4863 ExecutionContextScope *exe_scope) {
4864 if (GetCompleteType(type)) {
4865 clang::QualType qual_type(GetCanonicalQualType(type));
4866 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4867 switch (type_class) {
4868 case clang::Type::Record:
4869 if (GetCompleteType(type))
4870 return getASTContext()->getTypeSize(qual_type);
4875 case clang::Type::ObjCInterface:
4876 case clang::Type::ObjCObject: {
4877 ExecutionContext exe_ctx(exe_scope);
4878 Process *process = exe_ctx.GetProcessPtr();
4880 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4882 uint64_t bit_size = 0;
4883 if (objc_runtime->GetTypeBitSize(
4884 CompilerType(getASTContext(), qual_type), bit_size))
4888 static bool g_printed = false;
4891 DumpTypeDescription(type, &s);
4893 llvm::outs() << "warning: trying to determine the size of type ";
4894 llvm::outs() << s.GetString() << "\n";
4895 llvm::outs() << "without a valid ExecutionContext. this is not "
4896 "reliable. please file a bug against LLDB.\n";
4897 llvm::outs() << "backtrace:\n";
4898 llvm::sys::PrintStackTrace(llvm::outs());
4899 llvm::outs() << "\n";
4906 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4907 if (bit_size == 0) {
4908 if (qual_type->isIncompleteArrayType())
4909 return getASTContext()->getTypeSize(
4910 qual_type->getArrayElementTypeNoTypeQual()
4911 ->getCanonicalTypeUnqualified());
4913 if (qual_type->isObjCObjectOrInterfaceType())
4915 getASTContext()->getTypeSize(
4916 getASTContext()->ObjCBuiltinClassTy);
4923 size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4924 if (GetCompleteType(type))
4925 return getASTContext()->getTypeAlign(GetQualType(type));
4929 lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4932 return lldb::eEncodingInvalid;
4935 clang::QualType qual_type(GetCanonicalQualType(type));
4937 switch (qual_type->getTypeClass()) {
4938 case clang::Type::UnaryTransform:
4941 case clang::Type::FunctionNoProto:
4942 case clang::Type::FunctionProto:
4945 case clang::Type::IncompleteArray:
4946 case clang::Type::VariableArray:
4949 case clang::Type::ConstantArray:
4952 case clang::Type::ExtVector:
4953 case clang::Type::Vector:
4954 // TODO: Set this to more than one???
4957 case clang::Type::Builtin:
4958 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4959 case clang::BuiltinType::Void:
4962 case clang::BuiltinType::Bool:
4963 case clang::BuiltinType::Char_S:
4964 case clang::BuiltinType::SChar:
4965 case clang::BuiltinType::WChar_S:
4966 case clang::BuiltinType::Char16:
4967 case clang::BuiltinType::Char32:
4968 case clang::BuiltinType::Short:
4969 case clang::BuiltinType::Int:
4970 case clang::BuiltinType::Long:
4971 case clang::BuiltinType::LongLong:
4972 case clang::BuiltinType::Int128:
4973 return lldb::eEncodingSint;
4975 case clang::BuiltinType::Char_U:
4976 case clang::BuiltinType::UChar:
4977 case clang::BuiltinType::WChar_U:
4978 case clang::BuiltinType::UShort:
4979 case clang::BuiltinType::UInt:
4980 case clang::BuiltinType::ULong:
4981 case clang::BuiltinType::ULongLong:
4982 case clang::BuiltinType::UInt128:
4983 return lldb::eEncodingUint;
4985 case clang::BuiltinType::Half:
4986 case clang::BuiltinType::Float:
4987 case clang::BuiltinType::Float128:
4988 case clang::BuiltinType::Double:
4989 case clang::BuiltinType::LongDouble:
4990 return lldb::eEncodingIEEE754;
4992 case clang::BuiltinType::ObjCClass:
4993 case clang::BuiltinType::ObjCId:
4994 case clang::BuiltinType::ObjCSel:
4995 return lldb::eEncodingUint;
4997 case clang::BuiltinType::NullPtr:
4998 return lldb::eEncodingUint;
5000 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5001 case clang::BuiltinType::Kind::BoundMember:
5002 case clang::BuiltinType::Kind::BuiltinFn:
5003 case clang::BuiltinType::Kind::Dependent:
5004 case clang::BuiltinType::Kind::OCLClkEvent:
5005 case clang::BuiltinType::Kind::OCLEvent:
5006 case clang::BuiltinType::Kind::OCLImage1dRO:
5007 case clang::BuiltinType::Kind::OCLImage1dWO:
5008 case clang::BuiltinType::Kind::OCLImage1dRW:
5009 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5010 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5011 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5012 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5013 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5014 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5015 case clang::BuiltinType::Kind::OCLImage2dRO:
5016 case clang::BuiltinType::Kind::OCLImage2dWO:
5017 case clang::BuiltinType::Kind::OCLImage2dRW:
5018 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5019 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5020 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5021 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5022 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5023 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5024 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5025 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5026 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5027 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5028 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5029 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5030 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5031 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5032 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5033 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5034 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5035 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5036 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5037 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5038 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5039 case clang::BuiltinType::Kind::OCLImage3dRO:
5040 case clang::BuiltinType::Kind::OCLImage3dWO:
5041 case clang::BuiltinType::Kind::OCLImage3dRW:
5042 case clang::BuiltinType::Kind::OCLQueue:
5043 case clang::BuiltinType::Kind::OCLNDRange:
5044 case clang::BuiltinType::Kind::OCLReserveID:
5045 case clang::BuiltinType::Kind::OCLSampler:
5046 case clang::BuiltinType::Kind::OMPArraySection:
5047 case clang::BuiltinType::Kind::Overload:
5048 case clang::BuiltinType::Kind::PseudoObject:
5049 case clang::BuiltinType::Kind::UnknownAny:
5053 // All pointer types are represented as unsigned integer encodings.
5054 // We may nee to add a eEncodingPointer if we ever need to know the
5056 case clang::Type::ObjCObjectPointer:
5057 case clang::Type::BlockPointer:
5058 case clang::Type::Pointer:
5059 case clang::Type::LValueReference:
5060 case clang::Type::RValueReference:
5061 case clang::Type::MemberPointer:
5062 return lldb::eEncodingUint;
5063 case clang::Type::Complex: {
5064 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5065 if (qual_type->isComplexType())
5066 encoding = lldb::eEncodingIEEE754;
5068 const clang::ComplexType *complex_type =
5069 qual_type->getAsComplexIntegerType();
5071 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5072 .GetEncoding(count);
5074 encoding = lldb::eEncodingSint;
5080 case clang::Type::ObjCInterface:
5082 case clang::Type::Record:
5084 case clang::Type::Enum:
5085 return lldb::eEncodingSint;
5086 case clang::Type::Typedef:
5087 return CompilerType(getASTContext(),
5088 llvm::cast<clang::TypedefType>(qual_type)
5090 ->getUnderlyingType())
5091 .GetEncoding(count);
5093 case clang::Type::Auto:
5094 return CompilerType(
5096 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5097 .GetEncoding(count);
5099 case clang::Type::Elaborated:
5100 return CompilerType(
5102 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5103 .GetEncoding(count);
5105 case clang::Type::Paren:
5106 return CompilerType(getASTContext(),
5107 llvm::cast<clang::ParenType>(qual_type)->desugar())
5108 .GetEncoding(count);
5110 case clang::Type::DependentSizedArray:
5111 case clang::Type::DependentSizedExtVector:
5112 case clang::Type::UnresolvedUsing:
5113 case clang::Type::Attributed:
5114 case clang::Type::TemplateTypeParm:
5115 case clang::Type::SubstTemplateTypeParm:
5116 case clang::Type::SubstTemplateTypeParmPack:
5117 case clang::Type::InjectedClassName:
5118 case clang::Type::DependentName:
5119 case clang::Type::DependentTemplateSpecialization:
5120 case clang::Type::PackExpansion:
5121 case clang::Type::ObjCObject:
5123 case clang::Type::TypeOfExpr:
5124 case clang::Type::TypeOf:
5125 case clang::Type::Decltype:
5126 case clang::Type::TemplateSpecialization:
5127 case clang::Type::Atomic:
5128 case clang::Type::Adjusted:
5129 case clang::Type::Pipe:
5132 // pointer type decayed from an array or function type.
5133 case clang::Type::Decayed:
5135 case clang::Type::ObjCTypeParam:
5139 return lldb::eEncodingInvalid;
5142 lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5144 return lldb::eFormatDefault;
5146 clang::QualType qual_type(GetCanonicalQualType(type));
5148 switch (qual_type->getTypeClass()) {
5149 case clang::Type::UnaryTransform:
5152 case clang::Type::FunctionNoProto:
5153 case clang::Type::FunctionProto:
5156 case clang::Type::IncompleteArray:
5157 case clang::Type::VariableArray:
5160 case clang::Type::ConstantArray:
5161 return lldb::eFormatVoid; // no value
5163 case clang::Type::ExtVector:
5164 case clang::Type::Vector:
5167 case clang::Type::Builtin:
5168 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5169 // default: assert(0 && "Unknown builtin type!");
5170 case clang::BuiltinType::UnknownAny:
5171 case clang::BuiltinType::Void:
5172 case clang::BuiltinType::BoundMember:
5175 case clang::BuiltinType::Bool:
5176 return lldb::eFormatBoolean;
5177 case clang::BuiltinType::Char_S:
5178 case clang::BuiltinType::SChar:
5179 case clang::BuiltinType::WChar_S:
5180 case clang::BuiltinType::Char_U:
5181 case clang::BuiltinType::UChar:
5182 case clang::BuiltinType::WChar_U:
5183 return lldb::eFormatChar;
5184 case clang::BuiltinType::Char16:
5185 return lldb::eFormatUnicode16;
5186 case clang::BuiltinType::Char32:
5187 return lldb::eFormatUnicode32;
5188 case clang::BuiltinType::UShort:
5189 return lldb::eFormatUnsigned;
5190 case clang::BuiltinType::Short:
5191 return lldb::eFormatDecimal;
5192 case clang::BuiltinType::UInt:
5193 return lldb::eFormatUnsigned;
5194 case clang::BuiltinType::Int:
5195 return lldb::eFormatDecimal;
5196 case clang::BuiltinType::ULong:
5197 return lldb::eFormatUnsigned;
5198 case clang::BuiltinType::Long:
5199 return lldb::eFormatDecimal;
5200 case clang::BuiltinType::ULongLong:
5201 return lldb::eFormatUnsigned;
5202 case clang::BuiltinType::LongLong:
5203 return lldb::eFormatDecimal;
5204 case clang::BuiltinType::UInt128:
5205 return lldb::eFormatUnsigned;
5206 case clang::BuiltinType::Int128:
5207 return lldb::eFormatDecimal;
5208 case clang::BuiltinType::Half:
5209 case clang::BuiltinType::Float:
5210 case clang::BuiltinType::Double:
5211 case clang::BuiltinType::LongDouble:
5212 return lldb::eFormatFloat;
5214 return lldb::eFormatHex;
5217 case clang::Type::ObjCObjectPointer:
5218 return lldb::eFormatHex;
5219 case clang::Type::BlockPointer:
5220 return lldb::eFormatHex;
5221 case clang::Type::Pointer:
5222 return lldb::eFormatHex;
5223 case clang::Type::LValueReference:
5224 case clang::Type::RValueReference:
5225 return lldb::eFormatHex;
5226 case clang::Type::MemberPointer:
5228 case clang::Type::Complex: {
5229 if (qual_type->isComplexType())
5230 return lldb::eFormatComplex;
5232 return lldb::eFormatComplexInteger;
5234 case clang::Type::ObjCInterface:
5236 case clang::Type::Record:
5238 case clang::Type::Enum:
5239 return lldb::eFormatEnum;
5240 case clang::Type::Typedef:
5241 return CompilerType(getASTContext(),
5242 llvm::cast<clang::TypedefType>(qual_type)
5244 ->getUnderlyingType())
5246 case clang::Type::Auto:
5247 return CompilerType(getASTContext(),
5248 llvm::cast<clang::AutoType>(qual_type)->desugar())
5250 case clang::Type::Paren:
5251 return CompilerType(getASTContext(),
5252 llvm::cast<clang::ParenType>(qual_type)->desugar())
5254 case clang::Type::Elaborated:
5255 return CompilerType(
5257 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5259 case clang::Type::DependentSizedArray:
5260 case clang::Type::DependentSizedExtVector:
5261 case clang::Type::UnresolvedUsing:
5262 case clang::Type::Attributed:
5263 case clang::Type::TemplateTypeParm:
5264 case clang::Type::SubstTemplateTypeParm:
5265 case clang::Type::SubstTemplateTypeParmPack:
5266 case clang::Type::InjectedClassName:
5267 case clang::Type::DependentName:
5268 case clang::Type::DependentTemplateSpecialization:
5269 case clang::Type::PackExpansion:
5270 case clang::Type::ObjCObject:
5272 case clang::Type::TypeOfExpr:
5273 case clang::Type::TypeOf:
5274 case clang::Type::Decltype:
5275 case clang::Type::TemplateSpecialization:
5276 case clang::Type::Atomic:
5277 case clang::Type::Adjusted:
5278 case clang::Type::Pipe:
5281 // pointer type decayed from an array or function type.
5282 case clang::Type::Decayed:
5284 case clang::Type::ObjCTypeParam:
5287 // We don't know hot to display this type...
5288 return lldb::eFormatBytes;
5291 static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5292 bool check_superclass) {
5293 while (class_interface_decl) {
5294 if (class_interface_decl->ivar_size() > 0)
5297 if (check_superclass)
5298 class_interface_decl = class_interface_decl->getSuperClass();
5305 uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5306 bool omit_empty_base_classes) {
5310 uint32_t num_children = 0;
5311 clang::QualType qual_type(GetQualType(type));
5312 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5313 switch (type_class) {
5314 case clang::Type::Builtin:
5315 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5316 case clang::BuiltinType::ObjCId: // child is Class
5317 case clang::BuiltinType::ObjCClass: // child is Class
5326 case clang::Type::Complex:
5329 case clang::Type::Record:
5330 if (GetCompleteQualType(getASTContext(), qual_type)) {
5331 const clang::RecordType *record_type =
5332 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5333 const clang::RecordDecl *record_decl = record_type->getDecl();
5334 assert(record_decl);
5335 const clang::CXXRecordDecl *cxx_record_decl =
5336 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5337 if (cxx_record_decl) {
5338 if (omit_empty_base_classes) {
5339 // Check each base classes to see if it or any of its
5340 // base classes contain any fields. This can help
5341 // limit the noise in variable views by not having to
5342 // show base classes that contain no members.
5343 clang::CXXRecordDecl::base_class_const_iterator base_class,
5345 for (base_class = cxx_record_decl->bases_begin(),
5346 base_class_end = cxx_record_decl->bases_end();
5347 base_class != base_class_end; ++base_class) {
5348 const clang::CXXRecordDecl *base_class_decl =
5349 llvm::cast<clang::CXXRecordDecl>(
5350 base_class->getType()
5351 ->getAs<clang::RecordType>()
5354 // Skip empty base classes
5355 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5361 // Include all base classes
5362 num_children += cxx_record_decl->getNumBases();
5365 clang::RecordDecl::field_iterator field, field_end;
5366 for (field = record_decl->field_begin(),
5367 field_end = record_decl->field_end();
5368 field != field_end; ++field)
5373 case clang::Type::ObjCObject:
5374 case clang::Type::ObjCInterface:
5375 if (GetCompleteQualType(getASTContext(), qual_type)) {
5376 const clang::ObjCObjectType *objc_class_type =
5377 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5378 assert(objc_class_type);
5379 if (objc_class_type) {
5380 clang::ObjCInterfaceDecl *class_interface_decl =
5381 objc_class_type->getInterface();
5383 if (class_interface_decl) {
5385 clang::ObjCInterfaceDecl *superclass_interface_decl =
5386 class_interface_decl->getSuperClass();
5387 if (superclass_interface_decl) {
5388 if (omit_empty_base_classes) {
5389 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5395 num_children += class_interface_decl->ivar_size();
5401 case clang::Type::ObjCObjectPointer: {
5402 const clang::ObjCObjectPointerType *pointer_type =
5403 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5404 clang::QualType pointee_type = pointer_type->getPointeeType();
5405 uint32_t num_pointee_children =
5406 CompilerType(getASTContext(), pointee_type)
5407 .GetNumChildren(omit_empty_base_classes);
5408 // If this type points to a simple type, then it has 1 child
5409 if (num_pointee_children == 0)
5412 num_children = num_pointee_children;
5415 case clang::Type::Vector:
5416 case clang::Type::ExtVector:
5418 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5421 case clang::Type::ConstantArray:
5422 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5427 case clang::Type::Pointer: {
5428 const clang::PointerType *pointer_type =
5429 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5430 clang::QualType pointee_type(pointer_type->getPointeeType());
5431 uint32_t num_pointee_children =
5432 CompilerType(getASTContext(), pointee_type)
5433 .GetNumChildren(omit_empty_base_classes);
5434 if (num_pointee_children == 0) {
5435 // We have a pointer to a pointee type that claims it has no children.
5436 // We will want to look at
5437 num_children = GetNumPointeeChildren(pointee_type);
5439 num_children = num_pointee_children;
5442 case clang::Type::LValueReference:
5443 case clang::Type::RValueReference: {
5444 const clang::ReferenceType *reference_type =
5445 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5446 clang::QualType pointee_type = reference_type->getPointeeType();
5447 uint32_t num_pointee_children =
5448 CompilerType(getASTContext(), pointee_type)
5449 .GetNumChildren(omit_empty_base_classes);
5450 // If this type points to a simple type, then it has 1 child
5451 if (num_pointee_children == 0)
5454 num_children = num_pointee_children;
5457 case clang::Type::Typedef:
5459 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5461 ->getUnderlyingType())
5462 .GetNumChildren(omit_empty_base_classes);
5465 case clang::Type::Auto:
5467 CompilerType(getASTContext(),
5468 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5469 .GetNumChildren(omit_empty_base_classes);
5472 case clang::Type::Elaborated:
5476 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5477 .GetNumChildren(omit_empty_base_classes);
5480 case clang::Type::Paren:
5482 CompilerType(getASTContext(),
5483 llvm::cast<clang::ParenType>(qual_type)->desugar())
5484 .GetNumChildren(omit_empty_base_classes);
5489 return num_children;
5492 CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5493 return GetBasicType(GetBasicTypeEnumeration(name));
5497 ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5499 clang::QualType qual_type(GetQualType(type));
5500 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5501 if (type_class == clang::Type::Builtin) {
5502 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5503 case clang::BuiltinType::Void:
5504 return eBasicTypeVoid;
5505 case clang::BuiltinType::Bool:
5506 return eBasicTypeBool;
5507 case clang::BuiltinType::Char_S:
5508 return eBasicTypeSignedChar;
5509 case clang::BuiltinType::Char_U:
5510 return eBasicTypeUnsignedChar;
5511 case clang::BuiltinType::Char16:
5512 return eBasicTypeChar16;
5513 case clang::BuiltinType::Char32:
5514 return eBasicTypeChar32;
5515 case clang::BuiltinType::UChar:
5516 return eBasicTypeUnsignedChar;
5517 case clang::BuiltinType::SChar:
5518 return eBasicTypeSignedChar;
5519 case clang::BuiltinType::WChar_S:
5520 return eBasicTypeSignedWChar;
5521 case clang::BuiltinType::WChar_U:
5522 return eBasicTypeUnsignedWChar;
5523 case clang::BuiltinType::Short:
5524 return eBasicTypeShort;
5525 case clang::BuiltinType::UShort:
5526 return eBasicTypeUnsignedShort;
5527 case clang::BuiltinType::Int:
5528 return eBasicTypeInt;
5529 case clang::BuiltinType::UInt:
5530 return eBasicTypeUnsignedInt;
5531 case clang::BuiltinType::Long:
5532 return eBasicTypeLong;
5533 case clang::BuiltinType::ULong:
5534 return eBasicTypeUnsignedLong;
5535 case clang::BuiltinType::LongLong:
5536 return eBasicTypeLongLong;
5537 case clang::BuiltinType::ULongLong:
5538 return eBasicTypeUnsignedLongLong;
5539 case clang::BuiltinType::Int128:
5540 return eBasicTypeInt128;
5541 case clang::BuiltinType::UInt128:
5542 return eBasicTypeUnsignedInt128;
5544 case clang::BuiltinType::Half:
5545 return eBasicTypeHalf;
5546 case clang::BuiltinType::Float:
5547 return eBasicTypeFloat;
5548 case clang::BuiltinType::Double:
5549 return eBasicTypeDouble;
5550 case clang::BuiltinType::LongDouble:
5551 return eBasicTypeLongDouble;
5553 case clang::BuiltinType::NullPtr:
5554 return eBasicTypeNullPtr;
5555 case clang::BuiltinType::ObjCId:
5556 return eBasicTypeObjCID;
5557 case clang::BuiltinType::ObjCClass:
5558 return eBasicTypeObjCClass;
5559 case clang::BuiltinType::ObjCSel:
5560 return eBasicTypeObjCSel;
5562 return eBasicTypeOther;
5566 return eBasicTypeInvalid;
5569 void ClangASTContext::ForEachEnumerator(
5570 lldb::opaque_compiler_type_t type,
5571 std::function<bool(const CompilerType &integer_type,
5572 const ConstString &name,
5573 const llvm::APSInt &value)> const &callback) {
5574 const clang::EnumType *enum_type =
5575 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5577 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5579 CompilerType integer_type(this,
5580 enum_decl->getIntegerType().getAsOpaquePtr());
5582 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5583 for (enum_pos = enum_decl->enumerator_begin(),
5584 enum_end_pos = enum_decl->enumerator_end();
5585 enum_pos != enum_end_pos; ++enum_pos) {
5586 ConstString name(enum_pos->getNameAsString().c_str());
5587 if (!callback(integer_type, name, enum_pos->getInitVal()))
5594 #pragma mark Aggregate Types
5596 uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5601 clang::QualType qual_type(GetCanonicalQualType(type));
5602 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5603 switch (type_class) {
5604 case clang::Type::Record:
5605 if (GetCompleteType(type)) {
5606 const clang::RecordType *record_type =
5607 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5609 clang::RecordDecl *record_decl = record_type->getDecl();
5611 uint32_t field_idx = 0;
5612 clang::RecordDecl::field_iterator field, field_end;
5613 for (field = record_decl->field_begin(),
5614 field_end = record_decl->field_end();
5615 field != field_end; ++field)
5623 case clang::Type::Typedef:
5625 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5627 ->getUnderlyingType())
5631 case clang::Type::Auto:
5633 CompilerType(getASTContext(),
5634 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5638 case clang::Type::Elaborated:
5639 count = CompilerType(
5641 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5645 case clang::Type::Paren:
5646 count = CompilerType(getASTContext(),
5647 llvm::cast<clang::ParenType>(qual_type)->desugar())
5651 case clang::Type::ObjCObjectPointer: {
5652 const clang::ObjCObjectPointerType *objc_class_type =
5653 qual_type->getAsObjCInterfacePointerType();
5654 const clang::ObjCInterfaceType *objc_interface_type =
5655 objc_class_type->getInterfaceType();
5656 if (objc_interface_type &&
5657 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5658 clang::ObjCInterfaceDecl *class_interface_decl =
5659 objc_interface_type->getDecl();
5660 if (class_interface_decl) {
5661 count = class_interface_decl->ivar_size();
5667 case clang::Type::ObjCObject:
5668 case clang::Type::ObjCInterface:
5669 if (GetCompleteType(type)) {
5670 const clang::ObjCObjectType *objc_class_type =
5671 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5672 if (objc_class_type) {
5673 clang::ObjCInterfaceDecl *class_interface_decl =
5674 objc_class_type->getInterface();
5676 if (class_interface_decl)
5677 count = class_interface_decl->ivar_size();
5688 static lldb::opaque_compiler_type_t
5689 GetObjCFieldAtIndex(clang::ASTContext *ast,
5690 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5691 std::string &name, uint64_t *bit_offset_ptr,
5692 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5693 if (class_interface_decl) {
5694 if (idx < (class_interface_decl->ivar_size())) {
5695 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5696 ivar_end = class_interface_decl->ivar_end();
5697 uint32_t ivar_idx = 0;
5699 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5700 ++ivar_pos, ++ivar_idx) {
5701 if (ivar_idx == idx) {
5702 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5704 clang::QualType ivar_qual_type(ivar_decl->getType());
5706 name.assign(ivar_decl->getNameAsString());
5708 if (bit_offset_ptr) {
5709 const clang::ASTRecordLayout &interface_layout =
5710 ast->getASTObjCInterfaceLayout(class_interface_decl);
5711 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5714 const bool is_bitfield = ivar_pos->isBitField();
5716 if (bitfield_bit_size_ptr) {
5717 *bitfield_bit_size_ptr = 0;
5719 if (is_bitfield && ast) {
5720 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5721 llvm::APSInt bitfield_apsint;
5722 if (bitfield_bit_size_expr &&
5723 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5725 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5729 if (is_bitfield_ptr)
5730 *is_bitfield_ptr = is_bitfield;
5732 return ivar_qual_type.getAsOpaquePtr();
5740 CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5741 size_t idx, std::string &name,
5742 uint64_t *bit_offset_ptr,
5743 uint32_t *bitfield_bit_size_ptr,
5744 bool *is_bitfield_ptr) {
5746 return CompilerType();
5748 clang::QualType qual_type(GetCanonicalQualType(type));
5749 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5750 switch (type_class) {
5751 case clang::Type::Record:
5752 if (GetCompleteType(type)) {
5753 const clang::RecordType *record_type =
5754 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5755 const clang::RecordDecl *record_decl = record_type->getDecl();
5756 uint32_t field_idx = 0;
5757 clang::RecordDecl::field_iterator field, field_end;
5758 for (field = record_decl->field_begin(),
5759 field_end = record_decl->field_end();
5760 field != field_end; ++field, ++field_idx) {
5761 if (idx == field_idx) {
5762 // Print the member type if requested
5763 // Print the member name and equal sign
5764 name.assign(field->getNameAsString());
5766 // Figure out the type byte size (field_type_info.first) and
5767 // alignment (field_type_info.second) from the AST context.
5768 if (bit_offset_ptr) {
5769 const clang::ASTRecordLayout &record_layout =
5770 getASTContext()->getASTRecordLayout(record_decl);
5771 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5774 const bool is_bitfield = field->isBitField();
5776 if (bitfield_bit_size_ptr) {
5777 *bitfield_bit_size_ptr = 0;
5780 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5781 llvm::APSInt bitfield_apsint;
5782 if (bitfield_bit_size_expr &&
5783 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5784 *getASTContext())) {
5785 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5789 if (is_bitfield_ptr)
5790 *is_bitfield_ptr = is_bitfield;
5792 return CompilerType(getASTContext(), field->getType());
5798 case clang::Type::ObjCObjectPointer: {
5799 const clang::ObjCObjectPointerType *objc_class_type =
5800 qual_type->getAsObjCInterfacePointerType();
5801 const clang::ObjCInterfaceType *objc_interface_type =
5802 objc_class_type->getInterfaceType();
5803 if (objc_interface_type &&
5804 GetCompleteType((lldb::opaque_compiler_type_t)objc_interface_type)) {
5805 clang::ObjCInterfaceDecl *class_interface_decl =
5806 objc_interface_type->getDecl();
5807 if (class_interface_decl) {
5808 return CompilerType(
5809 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5810 idx, name, bit_offset_ptr,
5811 bitfield_bit_size_ptr, is_bitfield_ptr));
5817 case clang::Type::ObjCObject:
5818 case clang::Type::ObjCInterface:
5819 if (GetCompleteType(type)) {
5820 const clang::ObjCObjectType *objc_class_type =
5821 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5822 assert(objc_class_type);
5823 if (objc_class_type) {
5824 clang::ObjCInterfaceDecl *class_interface_decl =
5825 objc_class_type->getInterface();
5826 return CompilerType(
5827 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5828 idx, name, bit_offset_ptr,
5829 bitfield_bit_size_ptr, is_bitfield_ptr));
5834 case clang::Type::Typedef:
5835 return CompilerType(getASTContext(),
5836 llvm::cast<clang::TypedefType>(qual_type)
5838 ->getUnderlyingType())
5839 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5842 case clang::Type::Auto:
5843 return CompilerType(
5845 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5846 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5849 case clang::Type::Elaborated:
5850 return CompilerType(
5852 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5853 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5856 case clang::Type::Paren:
5857 return CompilerType(getASTContext(),
5858 llvm::cast<clang::ParenType>(qual_type)->desugar())
5859 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5865 return CompilerType();
5869 ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5871 clang::QualType qual_type(GetCanonicalQualType(type));
5872 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5873 switch (type_class) {
5874 case clang::Type::Record:
5875 if (GetCompleteType(type)) {
5876 const clang::CXXRecordDecl *cxx_record_decl =
5877 qual_type->getAsCXXRecordDecl();
5878 if (cxx_record_decl)
5879 count = cxx_record_decl->getNumBases();
5883 case clang::Type::ObjCObjectPointer:
5884 count = GetPointeeType(type).GetNumDirectBaseClasses();
5887 case clang::Type::ObjCObject:
5888 if (GetCompleteType(type)) {
5889 const clang::ObjCObjectType *objc_class_type =
5890 qual_type->getAsObjCQualifiedInterfaceType();
5891 if (objc_class_type) {
5892 clang::ObjCInterfaceDecl *class_interface_decl =
5893 objc_class_type->getInterface();
5895 if (class_interface_decl && class_interface_decl->getSuperClass())
5900 case clang::Type::ObjCInterface:
5901 if (GetCompleteType(type)) {
5902 const clang::ObjCInterfaceType *objc_interface_type =
5903 qual_type->getAs<clang::ObjCInterfaceType>();
5904 if (objc_interface_type) {
5905 clang::ObjCInterfaceDecl *class_interface_decl =
5906 objc_interface_type->getInterface();
5908 if (class_interface_decl && class_interface_decl->getSuperClass())
5914 case clang::Type::Typedef:
5915 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5917 ->getUnderlyingType()
5921 case clang::Type::Auto:
5922 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5927 case clang::Type::Elaborated:
5928 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5933 case clang::Type::Paren:
5934 return GetNumDirectBaseClasses(
5935 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5944 ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5946 clang::QualType qual_type(GetCanonicalQualType(type));
5947 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5948 switch (type_class) {
5949 case clang::Type::Record:
5950 if (GetCompleteType(type)) {
5951 const clang::CXXRecordDecl *cxx_record_decl =
5952 qual_type->getAsCXXRecordDecl();
5953 if (cxx_record_decl)
5954 count = cxx_record_decl->getNumVBases();
5958 case clang::Type::Typedef:
5959 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5961 ->getUnderlyingType()
5965 case clang::Type::Auto:
5966 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5971 case clang::Type::Elaborated:
5973 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5978 case clang::Type::Paren:
5979 count = GetNumVirtualBaseClasses(
5980 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5989 CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5990 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5991 clang::QualType qual_type(GetCanonicalQualType(type));
5992 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5993 switch (type_class) {
5994 case clang::Type::Record:
5995 if (GetCompleteType(type)) {
5996 const clang::CXXRecordDecl *cxx_record_decl =
5997 qual_type->getAsCXXRecordDecl();
5998 if (cxx_record_decl) {
5999 uint32_t curr_idx = 0;
6000 clang::CXXRecordDecl::base_class_const_iterator base_class,
6002 for (base_class = cxx_record_decl->bases_begin(),
6003 base_class_end = cxx_record_decl->bases_end();
6004 base_class != base_class_end; ++base_class, ++curr_idx) {
6005 if (curr_idx == idx) {
6006 if (bit_offset_ptr) {
6007 const clang::ASTRecordLayout &record_layout =
6008 getASTContext()->getASTRecordLayout(cxx_record_decl);
6009 const clang::CXXRecordDecl *base_class_decl =
6010 llvm::cast<clang::CXXRecordDecl>(
6011 base_class->getType()
6012 ->getAs<clang::RecordType>()
6014 if (base_class->isVirtual())
6016 record_layout.getVBaseClassOffset(base_class_decl)
6021 record_layout.getBaseClassOffset(base_class_decl)
6025 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6032 case clang::Type::ObjCObjectPointer:
6033 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6035 case clang::Type::ObjCObject:
6036 if (idx == 0 && GetCompleteType(type)) {
6037 const clang::ObjCObjectType *objc_class_type =
6038 qual_type->getAsObjCQualifiedInterfaceType();
6039 if (objc_class_type) {
6040 clang::ObjCInterfaceDecl *class_interface_decl =
6041 objc_class_type->getInterface();
6043 if (class_interface_decl) {
6044 clang::ObjCInterfaceDecl *superclass_interface_decl =
6045 class_interface_decl->getSuperClass();
6046 if (superclass_interface_decl) {
6048 *bit_offset_ptr = 0;
6049 return CompilerType(getASTContext(),
6050 getASTContext()->getObjCInterfaceType(
6051 superclass_interface_decl));
6057 case clang::Type::ObjCInterface:
6058 if (idx == 0 && GetCompleteType(type)) {
6059 const clang::ObjCObjectType *objc_interface_type =
6060 qual_type->getAs<clang::ObjCInterfaceType>();
6061 if (objc_interface_type) {
6062 clang::ObjCInterfaceDecl *class_interface_decl =
6063 objc_interface_type->getInterface();
6065 if (class_interface_decl) {
6066 clang::ObjCInterfaceDecl *superclass_interface_decl =
6067 class_interface_decl->getSuperClass();
6068 if (superclass_interface_decl) {
6070 *bit_offset_ptr = 0;
6071 return CompilerType(getASTContext(),
6072 getASTContext()->getObjCInterfaceType(
6073 superclass_interface_decl));
6080 case clang::Type::Typedef:
6081 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6083 ->getUnderlyingType()
6085 idx, bit_offset_ptr);
6087 case clang::Type::Auto:
6088 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6091 idx, bit_offset_ptr);
6093 case clang::Type::Elaborated:
6094 return GetDirectBaseClassAtIndex(
6095 llvm::cast<clang::ElaboratedType>(qual_type)
6098 idx, bit_offset_ptr);
6100 case clang::Type::Paren:
6101 return GetDirectBaseClassAtIndex(
6102 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6103 idx, bit_offset_ptr);
6108 return CompilerType();
6111 CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6112 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6113 clang::QualType qual_type(GetCanonicalQualType(type));
6114 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6115 switch (type_class) {
6116 case clang::Type::Record:
6117 if (GetCompleteType(type)) {
6118 const clang::CXXRecordDecl *cxx_record_decl =
6119 qual_type->getAsCXXRecordDecl();
6120 if (cxx_record_decl) {
6121 uint32_t curr_idx = 0;
6122 clang::CXXRecordDecl::base_class_const_iterator base_class,
6124 for (base_class = cxx_record_decl->vbases_begin(),
6125 base_class_end = cxx_record_decl->vbases_end();
6126 base_class != base_class_end; ++base_class, ++curr_idx) {
6127 if (curr_idx == idx) {
6128 if (bit_offset_ptr) {
6129 const clang::ASTRecordLayout &record_layout =
6130 getASTContext()->getASTRecordLayout(cxx_record_decl);
6131 const clang::CXXRecordDecl *base_class_decl =
6132 llvm::cast<clang::CXXRecordDecl>(
6133 base_class->getType()
6134 ->getAs<clang::RecordType>()
6137 record_layout.getVBaseClassOffset(base_class_decl)
6141 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6148 case clang::Type::Typedef:
6149 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6151 ->getUnderlyingType()
6153 idx, bit_offset_ptr);
6155 case clang::Type::Auto:
6156 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6159 idx, bit_offset_ptr);
6161 case clang::Type::Elaborated:
6162 return GetVirtualBaseClassAtIndex(
6163 llvm::cast<clang::ElaboratedType>(qual_type)
6166 idx, bit_offset_ptr);
6168 case clang::Type::Paren:
6169 return GetVirtualBaseClassAtIndex(
6170 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6171 idx, bit_offset_ptr);
6176 return CompilerType();
6179 // If a pointer to a pointee type (the clang_type arg) says that it has no
6180 // children, then we either need to trust it, or override it and return a
6181 // different result. For example, an "int *" has one child that is an integer,
6182 // but a function pointer doesn't have any children. Likewise if a Record type
6183 // claims it has no children, then there really is nothing to show.
6184 uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6188 clang::QualType qual_type(type.getCanonicalType());
6189 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6190 switch (type_class) {
6191 case clang::Type::Builtin:
6192 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6193 case clang::BuiltinType::UnknownAny:
6194 case clang::BuiltinType::Void:
6195 case clang::BuiltinType::NullPtr:
6196 case clang::BuiltinType::OCLEvent:
6197 case clang::BuiltinType::OCLImage1dRO:
6198 case clang::BuiltinType::OCLImage1dWO:
6199 case clang::BuiltinType::OCLImage1dRW:
6200 case clang::BuiltinType::OCLImage1dArrayRO:
6201 case clang::BuiltinType::OCLImage1dArrayWO:
6202 case clang::BuiltinType::OCLImage1dArrayRW:
6203 case clang::BuiltinType::OCLImage1dBufferRO:
6204 case clang::BuiltinType::OCLImage1dBufferWO:
6205 case clang::BuiltinType::OCLImage1dBufferRW:
6206 case clang::BuiltinType::OCLImage2dRO:
6207 case clang::BuiltinType::OCLImage2dWO:
6208 case clang::BuiltinType::OCLImage2dRW:
6209 case clang::BuiltinType::OCLImage2dArrayRO:
6210 case clang::BuiltinType::OCLImage2dArrayWO:
6211 case clang::BuiltinType::OCLImage2dArrayRW:
6212 case clang::BuiltinType::OCLImage3dRO:
6213 case clang::BuiltinType::OCLImage3dWO:
6214 case clang::BuiltinType::OCLImage3dRW:
6215 case clang::BuiltinType::OCLSampler:
6217 case clang::BuiltinType::Bool:
6218 case clang::BuiltinType::Char_U:
6219 case clang::BuiltinType::UChar:
6220 case clang::BuiltinType::WChar_U:
6221 case clang::BuiltinType::Char16:
6222 case clang::BuiltinType::Char32:
6223 case clang::BuiltinType::UShort:
6224 case clang::BuiltinType::UInt:
6225 case clang::BuiltinType::ULong:
6226 case clang::BuiltinType::ULongLong:
6227 case clang::BuiltinType::UInt128:
6228 case clang::BuiltinType::Char_S:
6229 case clang::BuiltinType::SChar:
6230 case clang::BuiltinType::WChar_S:
6231 case clang::BuiltinType::Short:
6232 case clang::BuiltinType::Int:
6233 case clang::BuiltinType::Long:
6234 case clang::BuiltinType::LongLong:
6235 case clang::BuiltinType::Int128:
6236 case clang::BuiltinType::Float:
6237 case clang::BuiltinType::Double:
6238 case clang::BuiltinType::LongDouble:
6239 case clang::BuiltinType::Dependent:
6240 case clang::BuiltinType::Overload:
6241 case clang::BuiltinType::ObjCId:
6242 case clang::BuiltinType::ObjCClass:
6243 case clang::BuiltinType::ObjCSel:
6244 case clang::BuiltinType::BoundMember:
6245 case clang::BuiltinType::Half:
6246 case clang::BuiltinType::ARCUnbridgedCast:
6247 case clang::BuiltinType::PseudoObject:
6248 case clang::BuiltinType::BuiltinFn:
6249 case clang::BuiltinType::OMPArraySection:
6256 case clang::Type::Complex:
6258 case clang::Type::Pointer:
6260 case clang::Type::BlockPointer:
6261 return 0; // If block pointers don't have debug info, then no children for
6263 case clang::Type::LValueReference:
6265 case clang::Type::RValueReference:
6267 case clang::Type::MemberPointer:
6269 case clang::Type::ConstantArray:
6271 case clang::Type::IncompleteArray:
6273 case clang::Type::VariableArray:
6275 case clang::Type::DependentSizedArray:
6277 case clang::Type::DependentSizedExtVector:
6279 case clang::Type::Vector:
6281 case clang::Type::ExtVector:
6283 case clang::Type::FunctionProto:
6284 return 0; // When we function pointers, they have no children...
6285 case clang::Type::FunctionNoProto:
6286 return 0; // When we function pointers, they have no children...
6287 case clang::Type::UnresolvedUsing:
6289 case clang::Type::Paren:
6290 return GetNumPointeeChildren(
6291 llvm::cast<clang::ParenType>(qual_type)->desugar());
6292 case clang::Type::Typedef:
6293 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6295 ->getUnderlyingType());
6296 case clang::Type::Auto:
6297 return GetNumPointeeChildren(
6298 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6299 case clang::Type::Elaborated:
6300 return GetNumPointeeChildren(
6301 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6302 case clang::Type::TypeOfExpr:
6304 case clang::Type::TypeOf:
6306 case clang::Type::Decltype:
6308 case clang::Type::Record:
6310 case clang::Type::Enum:
6312 case clang::Type::TemplateTypeParm:
6314 case clang::Type::SubstTemplateTypeParm:
6316 case clang::Type::TemplateSpecialization:
6318 case clang::Type::InjectedClassName:
6320 case clang::Type::DependentName:
6322 case clang::Type::DependentTemplateSpecialization:
6324 case clang::Type::ObjCObject:
6326 case clang::Type::ObjCInterface:
6328 case clang::Type::ObjCObjectPointer:
6336 CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6337 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6338 bool transparent_pointers, bool omit_empty_base_classes,
6339 bool ignore_array_bounds, std::string &child_name,
6340 uint32_t &child_byte_size, int32_t &child_byte_offset,
6341 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6342 bool &child_is_base_class, bool &child_is_deref_of_parent,
6343 ValueObject *valobj, uint64_t &language_flags) {
6345 return CompilerType();
6347 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6348 const clang::Type::TypeClass parent_type_class =
6349 parent_qual_type->getTypeClass();
6350 child_bitfield_bit_size = 0;
6351 child_bitfield_bit_offset = 0;
6352 child_is_base_class = false;
6355 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6356 uint32_t bit_offset;
6357 switch (parent_type_class) {
6358 case clang::Type::Builtin:
6360 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6361 case clang::BuiltinType::ObjCId:
6362 case clang::BuiltinType::ObjCClass:
6365 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6367 return CompilerType(getASTContext(),
6368 getASTContext()->ObjCBuiltinClassTy);
6376 case clang::Type::Record:
6377 if (idx_is_valid && GetCompleteType(type)) {
6378 const clang::RecordType *record_type =
6379 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6380 const clang::RecordDecl *record_decl = record_type->getDecl();
6381 assert(record_decl);
6382 const clang::ASTRecordLayout &record_layout =
6383 getASTContext()->getASTRecordLayout(record_decl);
6384 uint32_t child_idx = 0;
6386 const clang::CXXRecordDecl *cxx_record_decl =
6387 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6388 if (cxx_record_decl) {
6389 // We might have base classes to print out first
6390 clang::CXXRecordDecl::base_class_const_iterator base_class,
6392 for (base_class = cxx_record_decl->bases_begin(),
6393 base_class_end = cxx_record_decl->bases_end();
6394 base_class != base_class_end; ++base_class) {
6395 const clang::CXXRecordDecl *base_class_decl = nullptr;
6397 // Skip empty base classes
6398 if (omit_empty_base_classes) {
6399 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6400 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6401 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6405 if (idx == child_idx) {
6406 if (base_class_decl == nullptr)
6407 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6408 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6410 if (base_class->isVirtual()) {
6411 bool handled = false;
6414 AddressType addr_type = eAddressTypeInvalid;
6415 lldb::addr_t vtable_ptr_addr =
6416 valobj->GetCPPVTableAddress(addr_type);
6418 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6419 addr_type == eAddressTypeLoad) {
6421 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6422 Process *process = exe_ctx.GetProcessPtr();
6424 clang::VTableContextBase *vtable_ctx =
6425 getASTContext()->getVTableContext();
6427 if (vtable_ctx->isMicrosoft()) {
6428 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6429 static_cast<clang::MicrosoftVTableContext *>(
6432 if (vtable_ptr_addr) {
6433 const lldb::addr_t vbtable_ptr_addr =
6435 record_layout.getVBPtrOffset().getQuantity();
6437 const lldb::addr_t vbtable_ptr =
6438 process->ReadPointerFromMemory(vbtable_ptr_addr,
6440 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6441 // Get the index into the virtual base table. The
6442 // index is the index in uint32_t from vbtable_ptr
6443 const unsigned vbtable_index =
6444 msoft_vtable_ctx->getVBTableIndex(
6445 cxx_record_decl, base_class_decl);
6446 const lldb::addr_t base_offset_addr =
6447 vbtable_ptr + vbtable_index * 4;
6448 const uint32_t base_offset =
6449 process->ReadUnsignedIntegerFromMemory(
6450 base_offset_addr, 4, UINT32_MAX, err);
6451 if (base_offset != UINT32_MAX) {
6453 bit_offset = base_offset * 8;
6458 clang::ItaniumVTableContext *itanium_vtable_ctx =
6459 static_cast<clang::ItaniumVTableContext *>(
6461 if (vtable_ptr_addr) {
6462 const lldb::addr_t vtable_ptr =
6463 process->ReadPointerFromMemory(vtable_ptr_addr,
6465 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6466 clang::CharUnits base_offset_offset =
6467 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6468 cxx_record_decl, base_class_decl);
6469 const lldb::addr_t base_offset_addr =
6470 vtable_ptr + base_offset_offset.getQuantity();
6471 const uint32_t base_offset_size =
6472 process->GetAddressByteSize();
6473 const uint64_t base_offset =
6474 process->ReadUnsignedIntegerFromMemory(
6475 base_offset_addr, base_offset_size,
6477 if (base_offset < UINT32_MAX) {
6479 bit_offset = base_offset * 8;
6489 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6493 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6497 // Base classes should be a multiple of 8 bits in size
6498 child_byte_offset = bit_offset / 8;
6499 CompilerType base_class_clang_type(getASTContext(),
6500 base_class->getType());
6501 child_name = base_class_clang_type.GetTypeName().AsCString("");
6502 uint64_t base_class_clang_type_bit_size =
6503 base_class_clang_type.GetBitSize(
6504 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6506 // Base classes bit sizes should be a multiple of 8 bits in size
6507 assert(base_class_clang_type_bit_size % 8 == 0);
6508 child_byte_size = base_class_clang_type_bit_size / 8;
6509 child_is_base_class = true;
6510 return base_class_clang_type;
6512 // We don't increment the child index in the for loop since we might
6513 // be skipping empty base classes
6517 // Make sure index is in range...
6518 uint32_t field_idx = 0;
6519 clang::RecordDecl::field_iterator field, field_end;
6520 for (field = record_decl->field_begin(),
6521 field_end = record_decl->field_end();
6522 field != field_end; ++field, ++field_idx, ++child_idx) {
6523 if (idx == child_idx) {
6524 // Print the member type if requested
6525 // Print the member name and equal sign
6526 child_name.assign(field->getNameAsString());
6528 // Figure out the type byte size (field_type_info.first) and
6529 // alignment (field_type_info.second) from the AST context.
6530 CompilerType field_clang_type(getASTContext(), field->getType());
6531 assert(field_idx < record_layout.getFieldCount());
6532 child_byte_size = field_clang_type.GetByteSize(
6533 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6534 const uint32_t child_bit_size = child_byte_size * 8;
6536 // Figure out the field offset within the current struct/union/class
6538 bit_offset = record_layout.getFieldOffset(field_idx);
6539 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6540 child_bitfield_bit_size)) {
6541 child_bitfield_bit_offset = bit_offset % child_bit_size;
6542 const uint32_t child_bit_offset =
6543 bit_offset - child_bitfield_bit_offset;
6544 child_byte_offset = child_bit_offset / 8;
6546 child_byte_offset = bit_offset / 8;
6549 return field_clang_type;
6555 case clang::Type::ObjCObject:
6556 case clang::Type::ObjCInterface:
6557 if (idx_is_valid && GetCompleteType(type)) {
6558 const clang::ObjCObjectType *objc_class_type =
6559 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6560 assert(objc_class_type);
6561 if (objc_class_type) {
6562 uint32_t child_idx = 0;
6563 clang::ObjCInterfaceDecl *class_interface_decl =
6564 objc_class_type->getInterface();
6566 if (class_interface_decl) {
6568 const clang::ASTRecordLayout &interface_layout =
6569 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6570 clang::ObjCInterfaceDecl *superclass_interface_decl =
6571 class_interface_decl->getSuperClass();
6572 if (superclass_interface_decl) {
6573 if (omit_empty_base_classes) {
6574 CompilerType base_class_clang_type(
6575 getASTContext(), getASTContext()->getObjCInterfaceType(
6576 superclass_interface_decl));
6577 if (base_class_clang_type.GetNumChildren(
6578 omit_empty_base_classes) > 0) {
6580 clang::QualType ivar_qual_type(
6581 getASTContext()->getObjCInterfaceType(
6582 superclass_interface_decl));
6585 superclass_interface_decl->getNameAsString());
6587 clang::TypeInfo ivar_type_info =
6588 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6590 child_byte_size = ivar_type_info.Width / 8;
6591 child_byte_offset = 0;
6592 child_is_base_class = true;
6594 return CompilerType(getASTContext(), ivar_qual_type);
6603 const uint32_t superclass_idx = child_idx;
6605 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6606 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6607 ivar_end = class_interface_decl->ivar_end();
6609 for (ivar_pos = class_interface_decl->ivar_begin();
6610 ivar_pos != ivar_end; ++ivar_pos) {
6611 if (child_idx == idx) {
6612 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6614 clang::QualType ivar_qual_type(ivar_decl->getType());
6616 child_name.assign(ivar_decl->getNameAsString());
6618 clang::TypeInfo ivar_type_info =
6619 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6621 child_byte_size = ivar_type_info.Width / 8;
6623 // Figure out the field offset within the current
6624 // struct/union/class type
6625 // For ObjC objects, we can't trust the bit offset we get from
6626 // the Clang AST, since
6627 // that doesn't account for the space taken up by unbacked
6628 // properties, or from
6629 // the changing size of base classes that are newer than this
6631 // So if we have a process around that we can ask about this
6633 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6634 Process *process = nullptr;
6636 process = exe_ctx->GetProcessPtr();
6638 ObjCLanguageRuntime *objc_runtime =
6639 process->GetObjCLanguageRuntime();
6640 if (objc_runtime != nullptr) {
6641 CompilerType parent_ast_type(getASTContext(),
6643 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6644 parent_ast_type, ivar_decl->getNameAsString().c_str());
6648 // Setting this to UINT32_MAX to make sure we don't compute it
6650 bit_offset = UINT32_MAX;
6652 if (child_byte_offset ==
6653 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6654 bit_offset = interface_layout.getFieldOffset(child_idx -
6656 child_byte_offset = bit_offset / 8;
6659 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6660 // account for the bit offset
6661 // of a bitfield within its containing object. So regardless of
6662 // where we get the byte
6663 // offset from, we still need to get the bit offset for
6664 // bitfields from the layout.
6666 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6667 child_bitfield_bit_size)) {
6668 if (bit_offset == UINT32_MAX)
6669 bit_offset = interface_layout.getFieldOffset(
6670 child_idx - superclass_idx);
6672 child_bitfield_bit_offset = bit_offset % 8;
6674 return CompilerType(getASTContext(), ivar_qual_type);
6684 case clang::Type::ObjCObjectPointer:
6686 CompilerType pointee_clang_type(GetPointeeType(type));
6688 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6689 child_is_deref_of_parent = false;
6690 bool tmp_child_is_deref_of_parent = false;
6691 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6692 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6693 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6694 child_bitfield_bit_size, child_bitfield_bit_offset,
6695 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6698 child_is_deref_of_parent = true;
6699 const char *parent_name =
6700 valobj ? valobj->GetName().GetCString() : NULL;
6702 child_name.assign(1, '*');
6703 child_name += parent_name;
6706 // We have a pointer to an simple type
6707 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6708 child_byte_size = pointee_clang_type.GetByteSize(
6709 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6710 child_byte_offset = 0;
6711 return pointee_clang_type;
6717 case clang::Type::Vector:
6718 case clang::Type::ExtVector:
6720 const clang::VectorType *array =
6721 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6723 CompilerType element_type(getASTContext(), array->getElementType());
6724 if (element_type.GetCompleteType()) {
6725 char element_name[64];
6726 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6727 static_cast<uint64_t>(idx));
6728 child_name.assign(element_name);
6729 child_byte_size = element_type.GetByteSize(
6730 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6731 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6732 return element_type;
6738 case clang::Type::ConstantArray:
6739 case clang::Type::IncompleteArray:
6740 if (ignore_array_bounds || idx_is_valid) {
6741 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6743 CompilerType element_type(getASTContext(), array->getElementType());
6744 if (element_type.GetCompleteType()) {
6745 child_name = llvm::formatv("[{0}]", idx);
6746 child_byte_size = element_type.GetByteSize(
6747 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6748 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6749 return element_type;
6755 case clang::Type::Pointer:
6757 CompilerType pointee_clang_type(GetPointeeType(type));
6759 // Don't dereference "void *" pointers
6760 if (pointee_clang_type.IsVoidType())
6761 return CompilerType();
6763 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6764 child_is_deref_of_parent = false;
6765 bool tmp_child_is_deref_of_parent = false;
6766 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6767 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6768 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6769 child_bitfield_bit_size, child_bitfield_bit_offset,
6770 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6773 child_is_deref_of_parent = true;
6775 const char *parent_name =
6776 valobj ? valobj->GetName().GetCString() : NULL;
6778 child_name.assign(1, '*');
6779 child_name += parent_name;
6782 // We have a pointer to an simple type
6784 child_byte_size = pointee_clang_type.GetByteSize(
6785 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6786 child_byte_offset = 0;
6787 return pointee_clang_type;
6793 case clang::Type::LValueReference:
6794 case clang::Type::RValueReference:
6796 const clang::ReferenceType *reference_type =
6797 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6798 CompilerType pointee_clang_type(getASTContext(),
6799 reference_type->getPointeeType());
6800 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6801 child_is_deref_of_parent = false;
6802 bool tmp_child_is_deref_of_parent = false;
6803 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6804 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6805 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6806 child_bitfield_bit_size, child_bitfield_bit_offset,
6807 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6810 const char *parent_name =
6811 valobj ? valobj->GetName().GetCString() : NULL;
6813 child_name.assign(1, '&');
6814 child_name += parent_name;
6817 // We have a pointer to an simple type
6819 child_byte_size = pointee_clang_type.GetByteSize(
6820 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6821 child_byte_offset = 0;
6822 return pointee_clang_type;
6828 case clang::Type::Typedef: {
6829 CompilerType typedefed_clang_type(
6830 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6832 ->getUnderlyingType());
6833 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6834 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6835 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6836 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6837 child_is_deref_of_parent, valobj, language_flags);
6840 case clang::Type::Auto: {
6841 CompilerType elaborated_clang_type(
6843 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6844 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6845 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6846 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6847 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6848 child_is_deref_of_parent, valobj, language_flags);
6851 case clang::Type::Elaborated: {
6852 CompilerType elaborated_clang_type(
6854 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6855 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6856 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6857 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6858 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6859 child_is_deref_of_parent, valobj, language_flags);
6862 case clang::Type::Paren: {
6863 CompilerType paren_clang_type(
6865 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6866 return paren_clang_type.GetChildCompilerTypeAtIndex(
6867 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6868 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6869 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6870 child_is_deref_of_parent, valobj, language_flags);
6876 return CompilerType();
6879 static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6880 const clang::CXXBaseSpecifier *base_spec,
6881 bool omit_empty_base_classes) {
6882 uint32_t child_idx = 0;
6884 const clang::CXXRecordDecl *cxx_record_decl =
6885 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6887 // const char *super_name = record_decl->getNameAsCString();
6888 // const char *base_name =
6889 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6890 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6892 if (cxx_record_decl) {
6893 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6894 for (base_class = cxx_record_decl->bases_begin(),
6895 base_class_end = cxx_record_decl->bases_end();
6896 base_class != base_class_end; ++base_class) {
6897 if (omit_empty_base_classes) {
6898 if (BaseSpecifierIsEmpty(base_class))
6902 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6903 // super_name, base_name,
6905 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6908 if (base_class == base_spec)
6917 static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6918 clang::NamedDecl *canonical_decl,
6919 bool omit_empty_base_classes) {
6920 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6921 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6922 omit_empty_base_classes);
6924 clang::RecordDecl::field_iterator field, field_end;
6925 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6926 field != field_end; ++field, ++child_idx) {
6927 if (field->getCanonicalDecl() == canonical_decl)
6934 // Look for a child member (doesn't include base classes, but it does include
6935 // their members) in the type hierarchy. Returns an index path into "clang_type"
6936 // on how to reach the appropriate member.
6955 // If we have a clang type that describes "class C", and we wanted to looked
6958 // With omit_empty_base_classes == false we would get an integer array back
6961 // The first index 1 is the child index for "class A" within class C
6962 // The second index 1 is the child index for "m_b" within class A
6964 // With omit_empty_base_classes == true we would get an integer array back with:
6966 // The first index 0 is the child index for "class A" within class C (since
6967 // class B doesn't have any members it doesn't count)
6968 // The second index 1 is the child index for "m_b" within class A
6970 size_t ClangASTContext::GetIndexOfChildMemberWithName(
6971 lldb::opaque_compiler_type_t type, const char *name,
6972 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6973 if (type && name && name[0]) {
6974 clang::QualType qual_type(GetCanonicalQualType(type));
6975 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6976 switch (type_class) {
6977 case clang::Type::Record:
6978 if (GetCompleteType(type)) {
6979 const clang::RecordType *record_type =
6980 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6981 const clang::RecordDecl *record_decl = record_type->getDecl();
6983 assert(record_decl);
6984 uint32_t child_idx = 0;
6986 const clang::CXXRecordDecl *cxx_record_decl =
6987 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6989 // Try and find a field that matches NAME
6990 clang::RecordDecl::field_iterator field, field_end;
6991 llvm::StringRef name_sref(name);
6992 for (field = record_decl->field_begin(),
6993 field_end = record_decl->field_end();
6994 field != field_end; ++field, ++child_idx) {
6995 llvm::StringRef field_name = field->getName();
6996 if (field_name.empty()) {
6997 CompilerType field_type(getASTContext(), field->getType());
6998 child_indexes.push_back(child_idx);
6999 if (field_type.GetIndexOfChildMemberWithName(
7000 name, omit_empty_base_classes, child_indexes))
7001 return child_indexes.size();
7002 child_indexes.pop_back();
7004 } else if (field_name.equals(name_sref)) {
7005 // We have to add on the number of base classes to this index!
7006 child_indexes.push_back(
7007 child_idx + ClangASTContext::GetNumBaseClasses(
7008 cxx_record_decl, omit_empty_base_classes));
7009 return child_indexes.size();
7013 if (cxx_record_decl) {
7014 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7016 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7018 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7019 // Didn't find things easily, lets let clang do its thang...
7020 clang::IdentifierInfo &ident_ref =
7021 getASTContext()->Idents.get(name_sref);
7022 clang::DeclarationName decl_name(&ident_ref);
7024 clang::CXXBasePaths paths;
7025 if (cxx_record_decl->lookupInBases(
7026 [decl_name](const clang::CXXBaseSpecifier *specifier,
7027 clang::CXXBasePath &path) {
7028 return clang::CXXRecordDecl::FindOrdinaryMember(
7029 specifier, path, decl_name);
7032 clang::CXXBasePaths::const_paths_iterator path,
7033 path_end = paths.end();
7034 for (path = paths.begin(); path != path_end; ++path) {
7035 const size_t num_path_elements = path->size();
7036 for (size_t e = 0; e < num_path_elements; ++e) {
7037 clang::CXXBasePathElement elem = (*path)[e];
7039 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7040 omit_empty_base_classes);
7041 if (child_idx == UINT32_MAX) {
7042 child_indexes.clear();
7045 child_indexes.push_back(child_idx);
7046 parent_record_decl = llvm::cast<clang::RecordDecl>(
7047 elem.Base->getType()
7048 ->getAs<clang::RecordType>()
7052 for (clang::NamedDecl *path_decl : path->Decls) {
7053 child_idx = GetIndexForRecordChild(
7054 parent_record_decl, path_decl, omit_empty_base_classes);
7055 if (child_idx == UINT32_MAX) {
7056 child_indexes.clear();
7059 child_indexes.push_back(child_idx);
7063 return child_indexes.size();
7069 case clang::Type::ObjCObject:
7070 case clang::Type::ObjCInterface:
7071 if (GetCompleteType(type)) {
7072 llvm::StringRef name_sref(name);
7073 const clang::ObjCObjectType *objc_class_type =
7074 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7075 assert(objc_class_type);
7076 if (objc_class_type) {
7077 uint32_t child_idx = 0;
7078 clang::ObjCInterfaceDecl *class_interface_decl =
7079 objc_class_type->getInterface();
7081 if (class_interface_decl) {
7082 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7083 ivar_end = class_interface_decl->ivar_end();
7084 clang::ObjCInterfaceDecl *superclass_interface_decl =
7085 class_interface_decl->getSuperClass();
7087 for (ivar_pos = class_interface_decl->ivar_begin();
7088 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7089 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7091 if (ivar_decl->getName().equals(name_sref)) {
7092 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7093 (omit_empty_base_classes &&
7094 ObjCDeclHasIVars(superclass_interface_decl, true)))
7097 child_indexes.push_back(child_idx);
7098 return child_indexes.size();
7102 if (superclass_interface_decl) {
7103 // The super class index is always zero for ObjC classes,
7104 // so we push it onto the child indexes in case we find
7105 // an ivar in our superclass...
7106 child_indexes.push_back(0);
7108 CompilerType superclass_clang_type(
7109 getASTContext(), getASTContext()->getObjCInterfaceType(
7110 superclass_interface_decl));
7111 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7112 name, omit_empty_base_classes, child_indexes)) {
7113 // We did find an ivar in a superclass so just
7114 // return the results!
7115 return child_indexes.size();
7118 // We didn't find an ivar matching "name" in our
7119 // superclass, pop the superclass zero index that
7120 // we pushed on above.
7121 child_indexes.pop_back();
7128 case clang::Type::ObjCObjectPointer: {
7129 CompilerType objc_object_clang_type(
7131 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7132 ->getPointeeType());
7133 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7134 name, omit_empty_base_classes, child_indexes);
7137 case clang::Type::ConstantArray: {
7138 // const clang::ConstantArrayType *array =
7139 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7140 // const uint64_t element_count =
7141 // array->getSize().getLimitedValue();
7143 // if (idx < element_count)
7145 // std::pair<uint64_t, unsigned> field_type_info =
7146 // ast->getTypeInfo(array->getElementType());
7148 // char element_name[32];
7149 // ::snprintf (element_name, sizeof (element_name),
7150 // "%s[%u]", parent_name ? parent_name : "", idx);
7152 // child_name.assign(element_name);
7153 // assert(field_type_info.first % 8 == 0);
7154 // child_byte_size = field_type_info.first / 8;
7155 // child_byte_offset = idx * child_byte_size;
7156 // return array->getElementType().getAsOpaquePtr();
7160 // case clang::Type::MemberPointerType:
7162 // MemberPointerType *mem_ptr_type =
7163 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7164 // clang::QualType pointee_type =
7165 // mem_ptr_type->getPointeeType();
7167 // if (ClangASTContext::IsAggregateType
7168 // (pointee_type.getAsOpaquePtr()))
7170 // return GetIndexOfChildWithName (ast,
7171 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7177 case clang::Type::LValueReference:
7178 case clang::Type::RValueReference: {
7179 const clang::ReferenceType *reference_type =
7180 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7181 clang::QualType pointee_type(reference_type->getPointeeType());
7182 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7184 if (pointee_clang_type.IsAggregateType()) {
7185 return pointee_clang_type.GetIndexOfChildMemberWithName(
7186 name, omit_empty_base_classes, child_indexes);
7190 case clang::Type::Pointer: {
7191 CompilerType pointee_clang_type(GetPointeeType(type));
7193 if (pointee_clang_type.IsAggregateType()) {
7194 return pointee_clang_type.GetIndexOfChildMemberWithName(
7195 name, omit_empty_base_classes, child_indexes);
7199 case clang::Type::Typedef:
7200 return CompilerType(getASTContext(),
7201 llvm::cast<clang::TypedefType>(qual_type)
7203 ->getUnderlyingType())
7204 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7207 case clang::Type::Auto:
7208 return CompilerType(
7210 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7211 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7214 case clang::Type::Elaborated:
7215 return CompilerType(
7217 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7218 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7221 case clang::Type::Paren:
7222 return CompilerType(getASTContext(),
7223 llvm::cast<clang::ParenType>(qual_type)->desugar())
7224 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7234 // Get the index of the child of "clang_type" whose name matches. This function
7235 // doesn't descend into the children, but only looks one level deep and name
7236 // matches can include base class names.
7239 ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7241 bool omit_empty_base_classes) {
7242 if (type && name && name[0]) {
7243 clang::QualType qual_type(GetCanonicalQualType(type));
7245 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7247 switch (type_class) {
7248 case clang::Type::Record:
7249 if (GetCompleteType(type)) {
7250 const clang::RecordType *record_type =
7251 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7252 const clang::RecordDecl *record_decl = record_type->getDecl();
7254 assert(record_decl);
7255 uint32_t child_idx = 0;
7257 const clang::CXXRecordDecl *cxx_record_decl =
7258 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7260 if (cxx_record_decl) {
7261 clang::CXXRecordDecl::base_class_const_iterator base_class,
7263 for (base_class = cxx_record_decl->bases_begin(),
7264 base_class_end = cxx_record_decl->bases_end();
7265 base_class != base_class_end; ++base_class) {
7266 // Skip empty base classes
7267 clang::CXXRecordDecl *base_class_decl =
7268 llvm::cast<clang::CXXRecordDecl>(
7269 base_class->getType()
7270 ->getAs<clang::RecordType>()
7272 if (omit_empty_base_classes &&
7273 ClangASTContext::RecordHasFields(base_class_decl) == false)
7276 CompilerType base_class_clang_type(getASTContext(),
7277 base_class->getType());
7278 std::string base_class_type_name(
7279 base_class_clang_type.GetTypeName().AsCString(""));
7280 if (base_class_type_name.compare(name) == 0)
7286 // Try and find a field that matches NAME
7287 clang::RecordDecl::field_iterator field, field_end;
7288 llvm::StringRef name_sref(name);
7289 for (field = record_decl->field_begin(),
7290 field_end = record_decl->field_end();
7291 field != field_end; ++field, ++child_idx) {
7292 if (field->getName().equals(name_sref))
7298 case clang::Type::ObjCObject:
7299 case clang::Type::ObjCInterface:
7300 if (GetCompleteType(type)) {
7301 llvm::StringRef name_sref(name);
7302 const clang::ObjCObjectType *objc_class_type =
7303 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7304 assert(objc_class_type);
7305 if (objc_class_type) {
7306 uint32_t child_idx = 0;
7307 clang::ObjCInterfaceDecl *class_interface_decl =
7308 objc_class_type->getInterface();
7310 if (class_interface_decl) {
7311 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7312 ivar_end = class_interface_decl->ivar_end();
7313 clang::ObjCInterfaceDecl *superclass_interface_decl =
7314 class_interface_decl->getSuperClass();
7316 for (ivar_pos = class_interface_decl->ivar_begin();
7317 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7318 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7320 if (ivar_decl->getName().equals(name_sref)) {
7321 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7322 (omit_empty_base_classes &&
7323 ObjCDeclHasIVars(superclass_interface_decl, true)))
7330 if (superclass_interface_decl) {
7331 if (superclass_interface_decl->getName().equals(name_sref))
7339 case clang::Type::ObjCObjectPointer: {
7340 CompilerType pointee_clang_type(
7342 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7343 ->getPointeeType());
7344 return pointee_clang_type.GetIndexOfChildWithName(
7345 name, omit_empty_base_classes);
7348 case clang::Type::ConstantArray: {
7349 // const clang::ConstantArrayType *array =
7350 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7351 // const uint64_t element_count =
7352 // array->getSize().getLimitedValue();
7354 // if (idx < element_count)
7356 // std::pair<uint64_t, unsigned> field_type_info =
7357 // ast->getTypeInfo(array->getElementType());
7359 // char element_name[32];
7360 // ::snprintf (element_name, sizeof (element_name),
7361 // "%s[%u]", parent_name ? parent_name : "", idx);
7363 // child_name.assign(element_name);
7364 // assert(field_type_info.first % 8 == 0);
7365 // child_byte_size = field_type_info.first / 8;
7366 // child_byte_offset = idx * child_byte_size;
7367 // return array->getElementType().getAsOpaquePtr();
7371 // case clang::Type::MemberPointerType:
7373 // MemberPointerType *mem_ptr_type =
7374 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7375 // clang::QualType pointee_type =
7376 // mem_ptr_type->getPointeeType();
7378 // if (ClangASTContext::IsAggregateType
7379 // (pointee_type.getAsOpaquePtr()))
7381 // return GetIndexOfChildWithName (ast,
7382 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7388 case clang::Type::LValueReference:
7389 case clang::Type::RValueReference: {
7390 const clang::ReferenceType *reference_type =
7391 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7392 CompilerType pointee_type(getASTContext(),
7393 reference_type->getPointeeType());
7395 if (pointee_type.IsAggregateType()) {
7396 return pointee_type.GetIndexOfChildWithName(name,
7397 omit_empty_base_classes);
7401 case clang::Type::Pointer: {
7402 const clang::PointerType *pointer_type =
7403 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7404 CompilerType pointee_type(getASTContext(),
7405 pointer_type->getPointeeType());
7407 if (pointee_type.IsAggregateType()) {
7408 return pointee_type.GetIndexOfChildWithName(name,
7409 omit_empty_base_classes);
7413 // child_name.assign(1, '*');
7414 // child_name += parent_name;
7417 // // We have a pointer to an simple type
7420 // std::pair<uint64_t, unsigned> clang_type_info
7421 // = ast->getTypeInfo(pointee_type);
7422 // assert(clang_type_info.first % 8 == 0);
7423 // child_byte_size = clang_type_info.first / 8;
7424 // child_byte_offset = 0;
7425 // return pointee_type.getAsOpaquePtr();
7430 case clang::Type::Auto:
7431 return CompilerType(
7433 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7434 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7436 case clang::Type::Elaborated:
7437 return CompilerType(
7439 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7440 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7442 case clang::Type::Paren:
7443 return CompilerType(getASTContext(),
7444 llvm::cast<clang::ParenType>(qual_type)->desugar())
7445 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7447 case clang::Type::Typedef:
7448 return CompilerType(getASTContext(),
7449 llvm::cast<clang::TypedefType>(qual_type)
7451 ->getUnderlyingType())
7452 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7462 ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7466 clang::QualType qual_type(GetCanonicalQualType(type));
7467 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7468 switch (type_class) {
7469 case clang::Type::Record:
7470 if (GetCompleteType(type)) {
7471 const clang::CXXRecordDecl *cxx_record_decl =
7472 qual_type->getAsCXXRecordDecl();
7473 if (cxx_record_decl) {
7474 const clang::ClassTemplateSpecializationDecl *template_decl =
7475 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7478 return template_decl->getTemplateArgs().size();
7483 case clang::Type::Typedef:
7484 return (CompilerType(getASTContext(),
7485 llvm::cast<clang::TypedefType>(qual_type)
7487 ->getUnderlyingType()))
7488 .GetNumTemplateArguments();
7490 case clang::Type::Auto:
7491 return (CompilerType(
7493 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7494 .GetNumTemplateArguments();
7496 case clang::Type::Elaborated:
7497 return (CompilerType(
7499 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7500 .GetNumTemplateArguments();
7502 case clang::Type::Paren:
7503 return (CompilerType(getASTContext(),
7504 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7505 .GetNumTemplateArguments();
7515 ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7517 lldb::TemplateArgumentKind &kind) {
7519 return CompilerType();
7521 clang::QualType qual_type(GetCanonicalQualType(type));
7522 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7523 switch (type_class) {
7524 case clang::Type::Record:
7525 if (GetCompleteType(type)) {
7526 const clang::CXXRecordDecl *cxx_record_decl =
7527 qual_type->getAsCXXRecordDecl();
7528 if (cxx_record_decl) {
7529 const clang::ClassTemplateSpecializationDecl *template_decl =
7530 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7532 if (template_decl &&
7533 arg_idx < template_decl->getTemplateArgs().size()) {
7534 const clang::TemplateArgument &template_arg =
7535 template_decl->getTemplateArgs()[arg_idx];
7536 switch (template_arg.getKind()) {
7537 case clang::TemplateArgument::Null:
7538 kind = eTemplateArgumentKindNull;
7539 return CompilerType();
7541 case clang::TemplateArgument::Type:
7542 kind = eTemplateArgumentKindType;
7543 return CompilerType(getASTContext(), template_arg.getAsType());
7545 case clang::TemplateArgument::Declaration:
7546 kind = eTemplateArgumentKindDeclaration;
7547 return CompilerType();
7549 case clang::TemplateArgument::Integral:
7550 kind = eTemplateArgumentKindIntegral;
7551 return CompilerType(getASTContext(),
7552 template_arg.getIntegralType());
7554 case clang::TemplateArgument::Template:
7555 kind = eTemplateArgumentKindTemplate;
7556 return CompilerType();
7558 case clang::TemplateArgument::TemplateExpansion:
7559 kind = eTemplateArgumentKindTemplateExpansion;
7560 return CompilerType();
7562 case clang::TemplateArgument::Expression:
7563 kind = eTemplateArgumentKindExpression;
7564 return CompilerType();
7566 case clang::TemplateArgument::Pack:
7567 kind = eTemplateArgumentKindPack;
7568 return CompilerType();
7571 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7578 case clang::Type::Typedef:
7579 return (CompilerType(getASTContext(),
7580 llvm::cast<clang::TypedefType>(qual_type)
7582 ->getUnderlyingType()))
7583 .GetTemplateArgument(arg_idx, kind);
7585 case clang::Type::Auto:
7586 return (CompilerType(
7588 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7589 .GetTemplateArgument(arg_idx, kind);
7591 case clang::Type::Elaborated:
7592 return (CompilerType(
7594 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7595 .GetTemplateArgument(arg_idx, kind);
7597 case clang::Type::Paren:
7598 return (CompilerType(getASTContext(),
7599 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7600 .GetTemplateArgument(arg_idx, kind);
7605 kind = eTemplateArgumentKindNull;
7606 return CompilerType();
7609 CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7611 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7612 return CompilerType();
7615 clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7616 const clang::EnumType *enutype =
7617 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7619 return enutype->getDecl();
7623 clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7624 const clang::RecordType *record_type =
7625 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7627 return record_type->getDecl();
7631 clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7632 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7633 if (qual_type.isNull())
7636 return qual_type->getAsTagDecl();
7639 clang::CXXRecordDecl *
7640 ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7641 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7644 clang::ObjCInterfaceDecl *
7645 ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7646 const clang::ObjCObjectType *objc_class_type =
7647 llvm::dyn_cast<clang::ObjCObjectType>(
7648 ClangUtil::GetCanonicalQualType(type));
7649 if (objc_class_type)
7650 return objc_class_type->getInterface();
7654 clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7655 const CompilerType &type, const char *name,
7656 const CompilerType &field_clang_type, AccessType access,
7657 uint32_t bitfield_bit_size) {
7658 if (!type.IsValid() || !field_clang_type.IsValid())
7660 ClangASTContext *ast =
7661 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7664 clang::ASTContext *clang_ast = ast->getASTContext();
7666 clang::FieldDecl *field = nullptr;
7668 clang::Expr *bit_width = nullptr;
7669 if (bitfield_bit_size != 0) {
7670 llvm::APInt bitfield_bit_size_apint(
7671 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7672 bit_width = new (*clang_ast)
7673 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7674 clang_ast->IntTy, clang::SourceLocation());
7677 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7679 field = clang::FieldDecl::Create(
7680 *clang_ast, record_decl, clang::SourceLocation(),
7681 clang::SourceLocation(),
7682 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7683 ClangUtil::GetQualType(field_clang_type), // Field type
7685 bit_width, // BitWidth
7687 clang::ICIS_NoInit); // HasInit
7690 // Determine whether this field corresponds to an anonymous
7692 if (const clang::TagType *TagT =
7693 field->getType()->getAs<clang::TagType>()) {
7694 if (clang::RecordDecl *Rec =
7695 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7696 if (!Rec->getDeclName()) {
7697 Rec->setAnonymousStructOrUnion(true);
7698 field->setImplicit();
7705 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7707 record_decl->addDecl(field);
7709 #ifdef LLDB_CONFIGURATION_DEBUG
7714 clang::ObjCInterfaceDecl *class_interface_decl =
7715 ast->GetAsObjCInterfaceDecl(type);
7717 if (class_interface_decl) {
7718 const bool is_synthesized = false;
7720 field_clang_type.GetCompleteType();
7722 field = clang::ObjCIvarDecl::Create(
7723 *clang_ast, class_interface_decl, clang::SourceLocation(),
7724 clang::SourceLocation(),
7725 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7726 ClangUtil::GetQualType(field_clang_type), // Field type
7727 nullptr, // TypeSourceInfo *
7728 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7732 class_interface_decl->addDecl(field);
7734 #ifdef LLDB_CONFIGURATION_DEBUG
7743 void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7747 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7751 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7756 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7758 IndirectFieldVector indirect_fields;
7759 clang::RecordDecl::field_iterator field_pos;
7760 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7761 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7762 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7763 last_field_pos = field_pos++) {
7764 if (field_pos->isAnonymousStructOrUnion()) {
7765 clang::QualType field_qual_type = field_pos->getType();
7767 const clang::RecordType *field_record_type =
7768 field_qual_type->getAs<clang::RecordType>();
7770 if (!field_record_type)
7773 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7775 if (!field_record_decl)
7778 for (clang::RecordDecl::decl_iterator
7779 di = field_record_decl->decls_begin(),
7780 de = field_record_decl->decls_end();
7782 if (clang::FieldDecl *nested_field_decl =
7783 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7784 clang::NamedDecl **chain =
7785 new (*ast->getASTContext()) clang::NamedDecl *[2];
7786 chain[0] = *field_pos;
7787 chain[1] = nested_field_decl;
7788 clang::IndirectFieldDecl *indirect_field =
7789 clang::IndirectFieldDecl::Create(
7790 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7791 nested_field_decl->getIdentifier(),
7792 nested_field_decl->getType(), {chain, 2});
7794 indirect_field->setImplicit();
7796 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7797 field_pos->getAccess(), nested_field_decl->getAccess()));
7799 indirect_fields.push_back(indirect_field);
7800 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7801 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7802 size_t nested_chain_size =
7803 nested_indirect_field_decl->getChainingSize();
7804 clang::NamedDecl **chain = new (*ast->getASTContext())
7805 clang::NamedDecl *[nested_chain_size + 1];
7806 chain[0] = *field_pos;
7808 int chain_index = 1;
7809 for (clang::IndirectFieldDecl::chain_iterator
7810 nci = nested_indirect_field_decl->chain_begin(),
7811 nce = nested_indirect_field_decl->chain_end();
7813 chain[chain_index] = *nci;
7817 clang::IndirectFieldDecl *indirect_field =
7818 clang::IndirectFieldDecl::Create(
7819 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7820 nested_indirect_field_decl->getIdentifier(),
7821 nested_indirect_field_decl->getType(),
7822 {chain, nested_chain_size + 1});
7824 indirect_field->setImplicit();
7826 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7827 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7829 indirect_fields.push_back(indirect_field);
7835 // Check the last field to see if it has an incomplete array type as its
7836 // last member and if it does, the tell the record decl about it
7837 if (last_field_pos != field_end_pos) {
7838 if (last_field_pos->getType()->isIncompleteArrayType())
7839 record_decl->hasFlexibleArrayMember();
7842 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7843 ife = indirect_fields.end();
7845 record_decl->addDecl(*ifi);
7849 void ClangASTContext::SetIsPacked(const CompilerType &type) {
7851 ClangASTContext *ast =
7852 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7854 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7859 record_decl->addAttr(
7860 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7865 clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7866 const CompilerType &type, const char *name, const CompilerType &var_type,
7867 AccessType access) {
7868 clang::VarDecl *var_decl = nullptr;
7870 if (!type.IsValid() || !var_type.IsValid())
7872 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7876 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7878 var_decl = clang::VarDecl::Create(
7879 *ast->getASTContext(), // ASTContext &
7880 record_decl, // DeclContext *
7881 clang::SourceLocation(), // clang::SourceLocation StartLoc
7882 clang::SourceLocation(), // clang::SourceLocation IdLoc
7883 name ? &ast->getASTContext()->Idents.get(name)
7884 : nullptr, // clang::IdentifierInfo *
7885 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7886 nullptr, // TypeSourceInfo *
7887 clang::SC_Static); // StorageClass
7889 var_decl->setAccess(
7890 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7891 record_decl->addDecl(var_decl);
7893 #ifdef LLDB_CONFIGURATION_DEBUG
7894 VerifyDecl(var_decl);
7901 clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7902 lldb::opaque_compiler_type_t type, const char *name,
7903 const CompilerType &method_clang_type, lldb::AccessType access,
7904 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7905 bool is_attr_used, bool is_artificial) {
7906 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7910 clang::QualType record_qual_type(GetCanonicalQualType(type));
7912 clang::CXXRecordDecl *cxx_record_decl =
7913 record_qual_type->getAsCXXRecordDecl();
7915 if (cxx_record_decl == nullptr)
7918 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7920 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7922 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7924 const clang::FunctionType *function_type =
7925 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7927 if (function_type == nullptr)
7930 const clang::FunctionProtoType *method_function_prototype(
7931 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7933 if (!method_function_prototype)
7936 unsigned int num_params = method_function_prototype->getNumParams();
7938 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7939 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7942 return nullptr; // skip everything artificial
7944 if (name[0] == '~') {
7945 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7946 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7947 clang::DeclarationNameInfo(
7948 getASTContext()->DeclarationNames.getCXXDestructorName(
7949 getASTContext()->getCanonicalType(record_qual_type)),
7950 clang::SourceLocation()),
7951 method_qual_type, nullptr, is_inline, is_artificial);
7952 cxx_method_decl = cxx_dtor_decl;
7953 } else if (decl_name == cxx_record_decl->getDeclName()) {
7954 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7955 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7956 clang::DeclarationNameInfo(
7957 getASTContext()->DeclarationNames.getCXXConstructorName(
7958 getASTContext()->getCanonicalType(record_qual_type)),
7959 clang::SourceLocation()),
7961 nullptr, // TypeSourceInfo *
7962 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7963 cxx_method_decl = cxx_ctor_decl;
7965 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7966 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7968 if (IsOperator(name, op_kind)) {
7969 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7970 // Check the number of operator parameters. Sometimes we have
7971 // seen bad DWARF that doesn't correctly describe operators and
7972 // if we try to create a method and add it to the class, clang
7973 // will assert and crash, so we need to make sure things are
7975 const bool is_method = true;
7976 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7977 is_method, op_kind, num_params))
7979 cxx_method_decl = clang::CXXMethodDecl::Create(
7980 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7981 clang::DeclarationNameInfo(
7982 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7983 clang::SourceLocation()),
7985 nullptr, // TypeSourceInfo *
7986 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7987 } else if (num_params == 0) {
7988 // Conversion operators don't take params...
7989 cxx_method_decl = clang::CXXConversionDecl::Create(
7990 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7991 clang::DeclarationNameInfo(
7992 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7993 getASTContext()->getCanonicalType(
7994 function_type->getReturnType())),
7995 clang::SourceLocation()),
7997 nullptr, // TypeSourceInfo *
7998 is_inline, is_explicit, false /*is_constexpr*/,
7999 clang::SourceLocation());
8003 if (cxx_method_decl == nullptr) {
8004 cxx_method_decl = clang::CXXMethodDecl::Create(
8005 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8006 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8008 nullptr, // TypeSourceInfo *
8009 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8013 clang::AccessSpecifier access_specifier =
8014 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8016 cxx_method_decl->setAccess(access_specifier);
8017 cxx_method_decl->setVirtualAsWritten(is_virtual);
8020 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8022 // Populate the method decl with parameter decls
8024 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8026 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8027 params.push_back(clang::ParmVarDecl::Create(
8028 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8029 clang::SourceLocation(),
8030 nullptr, // anonymous
8031 method_function_prototype->getParamType(param_index), nullptr,
8032 clang::SC_None, nullptr));
8035 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8037 cxx_record_decl->addDecl(cxx_method_decl);
8039 // Sometimes the debug info will mention a constructor (default/copy/move),
8040 // destructor, or assignment operator (copy/move) but there won't be any
8041 // version of this in the code. So we check if the function was artificially
8042 // generated and if it is trivial and this lets the compiler/backend know
8043 // that it can inline the IR for these when it needs to and we can avoid a
8044 // "missing function" error when running expressions.
8046 if (is_artificial) {
8047 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8048 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8049 (cxx_ctor_decl->isCopyConstructor() &&
8050 cxx_record_decl->hasTrivialCopyConstructor()) ||
8051 (cxx_ctor_decl->isMoveConstructor() &&
8052 cxx_record_decl->hasTrivialMoveConstructor()))) {
8053 cxx_ctor_decl->setDefaulted();
8054 cxx_ctor_decl->setTrivial(true);
8055 } else if (cxx_dtor_decl) {
8056 if (cxx_record_decl->hasTrivialDestructor()) {
8057 cxx_dtor_decl->setDefaulted();
8058 cxx_dtor_decl->setTrivial(true);
8060 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8061 cxx_record_decl->hasTrivialCopyAssignment()) ||
8062 (cxx_method_decl->isMoveAssignmentOperator() &&
8063 cxx_record_decl->hasTrivialMoveAssignment())) {
8064 cxx_method_decl->setDefaulted();
8065 cxx_method_decl->setTrivial(true);
8069 #ifdef LLDB_CONFIGURATION_DEBUG
8070 VerifyDecl(cxx_method_decl);
8073 // printf ("decl->isPolymorphic() = %i\n",
8074 // cxx_record_decl->isPolymorphic());
8075 // printf ("decl->isAggregate() = %i\n",
8076 // cxx_record_decl->isAggregate());
8077 // printf ("decl->isPOD() = %i\n",
8078 // cxx_record_decl->isPOD());
8079 // printf ("decl->isEmpty() = %i\n",
8080 // cxx_record_decl->isEmpty());
8081 // printf ("decl->isAbstract() = %i\n",
8082 // cxx_record_decl->isAbstract());
8083 // printf ("decl->hasTrivialConstructor() = %i\n",
8084 // cxx_record_decl->hasTrivialConstructor());
8085 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8086 // cxx_record_decl->hasTrivialCopyConstructor());
8087 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8088 // cxx_record_decl->hasTrivialCopyAssignment());
8089 // printf ("decl->hasTrivialDestructor() = %i\n",
8090 // cxx_record_decl->hasTrivialDestructor());
8091 return cxx_method_decl;
8094 #pragma mark C++ Base Classes
8096 clang::CXXBaseSpecifier *
8097 ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8098 AccessType access, bool is_virtual,
8099 bool base_of_class) {
8101 return new clang::CXXBaseSpecifier(
8102 clang::SourceRange(), is_virtual, base_of_class,
8103 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8104 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8105 clang::SourceLocation());
8109 void ClangASTContext::DeleteBaseClassSpecifiers(
8110 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8111 for (unsigned i = 0; i < num_base_classes; ++i) {
8112 delete base_classes[i];
8113 base_classes[i] = nullptr;
8117 bool ClangASTContext::SetBaseClassesForClassType(
8118 lldb::opaque_compiler_type_t type,
8119 clang::CXXBaseSpecifier const *const *base_classes,
8120 unsigned num_base_classes) {
8122 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8123 if (cxx_record_decl) {
8124 cxx_record_decl->setBases(base_classes, num_base_classes);
8131 bool ClangASTContext::SetObjCSuperClass(
8132 const CompilerType &type, const CompilerType &superclass_clang_type) {
8133 ClangASTContext *ast =
8134 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8137 clang::ASTContext *clang_ast = ast->getASTContext();
8139 if (type && superclass_clang_type.IsValid() &&
8140 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8141 clang::ObjCInterfaceDecl *class_interface_decl =
8142 GetAsObjCInterfaceDecl(type);
8143 clang::ObjCInterfaceDecl *super_interface_decl =
8144 GetAsObjCInterfaceDecl(superclass_clang_type);
8145 if (class_interface_decl && super_interface_decl) {
8146 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8147 clang_ast->getObjCInterfaceType(super_interface_decl)));
8154 bool ClangASTContext::AddObjCClassProperty(
8155 const CompilerType &type, const char *property_name,
8156 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8157 const char *property_setter_name, const char *property_getter_name,
8158 uint32_t property_attributes, ClangASTMetadata *metadata) {
8159 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8160 property_name[0] == '\0')
8162 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8165 clang::ASTContext *clang_ast = ast->getASTContext();
8167 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8169 if (class_interface_decl) {
8170 CompilerType property_clang_type_to_access;
8172 if (property_clang_type.IsValid())
8173 property_clang_type_to_access = property_clang_type;
8175 property_clang_type_to_access =
8176 CompilerType(clang_ast, ivar_decl->getType());
8178 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8179 clang::TypeSourceInfo *prop_type_source;
8182 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8184 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8185 ClangUtil::GetQualType(property_clang_type));
8187 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8188 *clang_ast, class_interface_decl,
8189 clang::SourceLocation(), // Source Location
8190 &clang_ast->Idents.get(property_name),
8191 clang::SourceLocation(), // Source Location for AT
8192 clang::SourceLocation(), // Source location for (
8193 ivar_decl ? ivar_decl->getType()
8194 : ClangUtil::GetQualType(property_clang_type),
8197 if (property_decl) {
8199 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8201 class_interface_decl->addDecl(property_decl);
8203 clang::Selector setter_sel, getter_sel;
8205 if (property_setter_name != nullptr) {
8206 std::string property_setter_no_colon(
8207 property_setter_name, strlen(property_setter_name) - 1);
8208 clang::IdentifierInfo *setter_ident =
8209 &clang_ast->Idents.get(property_setter_no_colon);
8210 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8211 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8212 std::string setter_sel_string("set");
8213 setter_sel_string.push_back(::toupper(property_name[0]));
8214 setter_sel_string.append(&property_name[1]);
8215 clang::IdentifierInfo *setter_ident =
8216 &clang_ast->Idents.get(setter_sel_string);
8217 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8219 property_decl->setSetterName(setter_sel);
8220 property_decl->setPropertyAttributes(
8221 clang::ObjCPropertyDecl::OBJC_PR_setter);
8223 if (property_getter_name != nullptr) {
8224 clang::IdentifierInfo *getter_ident =
8225 &clang_ast->Idents.get(property_getter_name);
8226 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8228 clang::IdentifierInfo *getter_ident =
8229 &clang_ast->Idents.get(property_name);
8230 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8232 property_decl->setGetterName(getter_sel);
8233 property_decl->setPropertyAttributes(
8234 clang::ObjCPropertyDecl::OBJC_PR_getter);
8237 property_decl->setPropertyIvarDecl(ivar_decl);
8239 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8240 property_decl->setPropertyAttributes(
8241 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8242 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8243 property_decl->setPropertyAttributes(
8244 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8245 if (property_attributes & DW_APPLE_PROPERTY_assign)
8246 property_decl->setPropertyAttributes(
8247 clang::ObjCPropertyDecl::OBJC_PR_assign);
8248 if (property_attributes & DW_APPLE_PROPERTY_retain)
8249 property_decl->setPropertyAttributes(
8250 clang::ObjCPropertyDecl::OBJC_PR_retain);
8251 if (property_attributes & DW_APPLE_PROPERTY_copy)
8252 property_decl->setPropertyAttributes(
8253 clang::ObjCPropertyDecl::OBJC_PR_copy);
8254 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8255 property_decl->setPropertyAttributes(
8256 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8257 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8258 property_decl->setPropertyAttributes(
8259 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8260 if (property_attributes &
8261 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8262 property_decl->setPropertyAttributes(
8263 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8264 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8265 property_decl->setPropertyAttributes(
8266 clang::ObjCPropertyDecl::OBJC_PR_class);
8268 const bool isInstance =
8269 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8271 if (!getter_sel.isNull() &&
8273 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8274 : class_interface_decl->lookupClassMethod(getter_sel))) {
8275 const bool isVariadic = false;
8276 const bool isSynthesized = false;
8277 const bool isImplicitlyDeclared = true;
8278 const bool isDefined = false;
8279 const clang::ObjCMethodDecl::ImplementationControl impControl =
8280 clang::ObjCMethodDecl::None;
8281 const bool HasRelatedResultType = false;
8283 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8284 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8285 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8286 nullptr, class_interface_decl, isInstance, isVariadic,
8287 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8288 HasRelatedResultType);
8290 if (getter && metadata)
8291 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8294 getter->setMethodParams(*clang_ast,
8295 llvm::ArrayRef<clang::ParmVarDecl *>(),
8296 llvm::ArrayRef<clang::SourceLocation>());
8298 class_interface_decl->addDecl(getter);
8302 if (!setter_sel.isNull() &&
8304 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8305 : class_interface_decl->lookupClassMethod(setter_sel))) {
8306 clang::QualType result_type = clang_ast->VoidTy;
8307 const bool isVariadic = false;
8308 const bool isSynthesized = false;
8309 const bool isImplicitlyDeclared = true;
8310 const bool isDefined = false;
8311 const clang::ObjCMethodDecl::ImplementationControl impControl =
8312 clang::ObjCMethodDecl::None;
8313 const bool HasRelatedResultType = false;
8315 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8316 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8317 setter_sel, result_type, nullptr, class_interface_decl,
8318 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8319 isDefined, impControl, HasRelatedResultType);
8321 if (setter && metadata)
8322 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8324 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8326 params.push_back(clang::ParmVarDecl::Create(
8327 *clang_ast, setter, clang::SourceLocation(),
8328 clang::SourceLocation(),
8329 nullptr, // anonymous
8330 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8331 clang::SC_Auto, nullptr));
8334 setter->setMethodParams(
8335 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8336 llvm::ArrayRef<clang::SourceLocation>());
8338 class_interface_decl->addDecl(setter);
8349 bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8350 bool check_superclass) {
8351 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8352 if (class_interface_decl)
8353 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8357 clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8358 const CompilerType &type,
8359 const char *name, // the full symbol name as seen in the symbol table
8360 // (lldb::opaque_compiler_type_t type, "-[NString
8361 // stringWithCString:]")
8362 const CompilerType &method_clang_type, lldb::AccessType access,
8363 bool is_artificial, bool is_variadic) {
8364 if (!type || !method_clang_type.IsValid())
8367 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8369 if (class_interface_decl == nullptr)
8371 ClangASTContext *lldb_ast =
8372 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8373 if (lldb_ast == nullptr)
8375 clang::ASTContext *ast = lldb_ast->getASTContext();
8377 const char *selector_start = ::strchr(name, ' ');
8378 if (selector_start == nullptr)
8382 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8386 // printf ("name = '%s'\n", name);
8388 unsigned num_selectors_with_args = 0;
8389 for (start = selector_start; start && *start != '\0' && *start != ']';
8391 len = ::strcspn(start, ":]");
8392 bool has_arg = (start[len] == ':');
8394 ++num_selectors_with_args;
8395 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8400 if (selector_idents.size() == 0)
8403 clang::Selector method_selector = ast->Selectors.getSelector(
8404 num_selectors_with_args ? selector_idents.size() : 0,
8405 selector_idents.data());
8407 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8409 // Populate the method decl with parameter decls
8410 const clang::Type *method_type(method_qual_type.getTypePtr());
8412 if (method_type == nullptr)
8415 const clang::FunctionProtoType *method_function_prototype(
8416 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8418 if (!method_function_prototype)
8421 bool is_synthesized = false;
8422 bool is_defined = false;
8423 clang::ObjCMethodDecl::ImplementationControl imp_control =
8424 clang::ObjCMethodDecl::None;
8426 const unsigned num_args = method_function_prototype->getNumParams();
8428 if (num_args != num_selectors_with_args)
8429 return nullptr; // some debug information is corrupt. We are not going to
8432 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8434 clang::SourceLocation(), // beginLoc,
8435 clang::SourceLocation(), // endLoc,
8436 method_selector, method_function_prototype->getReturnType(),
8437 nullptr, // TypeSourceInfo *ResultTInfo,
8438 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8439 ClangUtil::GetQualType(type)),
8440 name[0] == '-', is_variadic, is_synthesized,
8441 true, // is_implicitly_declared; we force this to true because we don't
8442 // have source locations
8443 is_defined, imp_control, false /*has_related_result_type*/);
8445 if (objc_method_decl == nullptr)
8449 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8451 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8452 params.push_back(clang::ParmVarDecl::Create(
8453 *ast, objc_method_decl, clang::SourceLocation(),
8454 clang::SourceLocation(),
8455 nullptr, // anonymous
8456 method_function_prototype->getParamType(param_index), nullptr,
8457 clang::SC_Auto, nullptr));
8460 objc_method_decl->setMethodParams(
8461 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8462 llvm::ArrayRef<clang::SourceLocation>());
8465 class_interface_decl->addDecl(objc_method_decl);
8467 #ifdef LLDB_CONFIGURATION_DEBUG
8468 VerifyDecl(objc_method_decl);
8471 return objc_method_decl;
8474 bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8475 if (ClangUtil::IsClangType(type))
8478 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
8480 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8481 switch (type_class) {
8482 case clang::Type::Record: {
8483 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8484 if (cxx_record_decl)
8485 return cxx_record_decl->hasExternalLexicalStorage() ||
8486 cxx_record_decl->hasExternalVisibleStorage();
8489 case clang::Type::Enum: {
8490 clang::EnumDecl *enum_decl =
8491 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8493 return enum_decl->hasExternalLexicalStorage() ||
8494 enum_decl->hasExternalVisibleStorage();
8497 case clang::Type::ObjCObject:
8498 case clang::Type::ObjCInterface: {
8499 const clang::ObjCObjectType *objc_class_type =
8500 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8501 assert(objc_class_type);
8502 if (objc_class_type) {
8503 clang::ObjCInterfaceDecl *class_interface_decl =
8504 objc_class_type->getInterface();
8506 if (class_interface_decl)
8507 return class_interface_decl->hasExternalLexicalStorage() ||
8508 class_interface_decl->hasExternalVisibleStorage();
8512 case clang::Type::Typedef:
8513 return GetHasExternalStorage(CompilerType(
8514 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8516 ->getUnderlyingType()
8517 .getAsOpaquePtr()));
8519 case clang::Type::Auto:
8520 return GetHasExternalStorage(CompilerType(
8521 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8523 .getAsOpaquePtr()));
8525 case clang::Type::Elaborated:
8526 return GetHasExternalStorage(CompilerType(
8527 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8529 .getAsOpaquePtr()));
8531 case clang::Type::Paren:
8532 return GetHasExternalStorage(CompilerType(
8533 type.GetTypeSystem(),
8534 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8542 bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8547 clang::QualType qual_type(GetCanonicalQualType(type));
8549 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8550 switch (type_class) {
8551 case clang::Type::Record: {
8552 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8553 if (cxx_record_decl) {
8554 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8555 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8560 case clang::Type::Enum: {
8561 clang::EnumDecl *enum_decl =
8562 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8564 enum_decl->setHasExternalLexicalStorage(has_extern);
8565 enum_decl->setHasExternalVisibleStorage(has_extern);
8570 case clang::Type::ObjCObject:
8571 case clang::Type::ObjCInterface: {
8572 const clang::ObjCObjectType *objc_class_type =
8573 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8574 assert(objc_class_type);
8575 if (objc_class_type) {
8576 clang::ObjCInterfaceDecl *class_interface_decl =
8577 objc_class_type->getInterface();
8579 if (class_interface_decl) {
8580 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8581 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8587 case clang::Type::Typedef:
8588 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8590 ->getUnderlyingType()
8594 case clang::Type::Auto:
8595 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8600 case clang::Type::Elaborated:
8601 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8606 case clang::Type::Paren:
8607 return SetHasExternalStorage(
8608 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8617 #pragma mark TagDecl
8619 bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8620 clang::QualType qual_type(ClangUtil::GetQualType(type));
8621 if (!qual_type.isNull()) {
8622 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8624 clang::TagDecl *tag_decl = tag_type->getDecl();
8626 tag_decl->startDefinition();
8631 const clang::ObjCObjectType *object_type =
8632 qual_type->getAs<clang::ObjCObjectType>();
8634 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8635 if (interface_decl) {
8636 interface_decl->startDefinition();
8644 bool ClangASTContext::CompleteTagDeclarationDefinition(
8645 const CompilerType &type) {
8646 clang::QualType qual_type(ClangUtil::GetQualType(type));
8647 if (!qual_type.isNull()) {
8648 // Make sure we use the same methodology as
8649 // ClangASTContext::StartTagDeclarationDefinition()
8650 // as to how we start/end the definition. Previously we were calling
8651 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8653 clang::TagDecl *tag_decl = tag_type->getDecl();
8655 clang::CXXRecordDecl *cxx_record_decl =
8656 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8658 if (cxx_record_decl) {
8659 if (!cxx_record_decl->isCompleteDefinition())
8660 cxx_record_decl->completeDefinition();
8661 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8662 cxx_record_decl->setHasExternalLexicalStorage(false);
8663 cxx_record_decl->setHasExternalVisibleStorage(false);
8669 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8672 clang::EnumDecl *enum_decl = enutype->getDecl();
8675 if (!enum_decl->isCompleteDefinition()) {
8676 ClangASTContext *lldb_ast =
8677 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8678 if (lldb_ast == nullptr)
8680 clang::ASTContext *ast = lldb_ast->getASTContext();
8682 /// TODO This really needs to be fixed.
8684 QualType integer_type(enum_decl->getIntegerType());
8685 if (!integer_type.isNull()) {
8686 unsigned NumPositiveBits = 1;
8687 unsigned NumNegativeBits = 0;
8689 clang::QualType promotion_qual_type;
8690 // If the enum integer type is less than an integer in bit width,
8691 // then we must promote it to an integer size.
8692 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8693 ast->getTypeSize(ast->IntTy)) {
8694 if (enum_decl->getIntegerType()->isSignedIntegerType())
8695 promotion_qual_type = ast->IntTy;
8697 promotion_qual_type = ast->UnsignedIntTy;
8699 promotion_qual_type = enum_decl->getIntegerType();
8701 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8702 promotion_qual_type, NumPositiveBits,
8713 bool ClangASTContext::AddEnumerationValueToEnumerationType(
8714 lldb::opaque_compiler_type_t type,
8715 const CompilerType &enumerator_clang_type, const Declaration &decl,
8716 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8717 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8718 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8720 bool is_signed = false;
8721 enumerator_clang_type.IsIntegerType(is_signed);
8722 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8724 const clang::EnumType *enutype =
8725 llvm::dyn_cast<clang::EnumType>(clang_type);
8728 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8729 enum_llvm_apsint = enum_value;
8730 clang::EnumConstantDecl *enumerator_decl =
8731 clang::EnumConstantDecl::Create(
8732 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8733 name ? &getASTContext()->Idents.get(name)
8734 : nullptr, // Identifier
8735 ClangUtil::GetQualType(enumerator_clang_type),
8736 nullptr, enum_llvm_apsint);
8738 if (enumerator_decl) {
8739 enutype->getDecl()->addDecl(enumerator_decl);
8741 #ifdef LLDB_CONFIGURATION_DEBUG
8742 VerifyDecl(enumerator_decl);
8754 ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8755 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8756 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8758 const clang::EnumType *enutype =
8759 llvm::dyn_cast<clang::EnumType>(clang_type);
8761 clang::EnumDecl *enum_decl = enutype->getDecl();
8763 return CompilerType(getASTContext(), enum_decl->getIntegerType());
8766 return CompilerType();
8770 ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8771 const CompilerType &pointee_type) {
8772 if (type && pointee_type.IsValid() &&
8773 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8774 ClangASTContext *ast =
8775 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8777 return CompilerType();
8778 return CompilerType(ast->getASTContext(),
8779 ast->getASTContext()->getMemberPointerType(
8780 ClangUtil::GetQualType(pointee_type),
8781 ClangUtil::GetQualType(type).getTypePtr()));
8783 return CompilerType();
8787 ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8788 const char *s, uint8_t *dst,
8791 clang::QualType qual_type(GetCanonicalQualType(type));
8793 bool is_complex = false;
8794 if (IsFloatingPointType(type, count, is_complex)) {
8795 // TODO: handle complex and vector types
8799 llvm::StringRef s_sref(s);
8800 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8803 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8804 const uint64_t byte_size = bit_size / 8;
8805 if (dst_size >= byte_size) {
8806 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8807 llvm::NextPowerOf2(byte_size) * 8);
8808 lldb_private::Error get_data_error;
8809 if (scalar.GetAsMemoryData(dst, byte_size,
8810 lldb_private::endian::InlHostByteOrder(),
8819 //----------------------------------------------------------------------
8821 //----------------------------------------------------------------------
8822 #define DEPTH_INCREMENT 2
8824 void ClangASTContext::DumpValue(
8825 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
8826 lldb::Format format, const lldb_private::DataExtractor &data,
8827 lldb::offset_t data_byte_offset, size_t data_byte_size,
8828 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8829 bool show_summary, bool verbose, uint32_t depth) {
8833 clang::QualType qual_type(GetQualType(type));
8834 switch (qual_type->getTypeClass()) {
8835 case clang::Type::Record:
8836 if (GetCompleteType(type)) {
8837 const clang::RecordType *record_type =
8838 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8839 const clang::RecordDecl *record_decl = record_type->getDecl();
8840 assert(record_decl);
8841 uint32_t field_bit_offset = 0;
8842 uint32_t field_byte_offset = 0;
8843 const clang::ASTRecordLayout &record_layout =
8844 getASTContext()->getASTRecordLayout(record_decl);
8845 uint32_t child_idx = 0;
8847 const clang::CXXRecordDecl *cxx_record_decl =
8848 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8849 if (cxx_record_decl) {
8850 // We might have base classes to print out first
8851 clang::CXXRecordDecl::base_class_const_iterator base_class,
8853 for (base_class = cxx_record_decl->bases_begin(),
8854 base_class_end = cxx_record_decl->bases_end();
8855 base_class != base_class_end; ++base_class) {
8856 const clang::CXXRecordDecl *base_class_decl =
8857 llvm::cast<clang::CXXRecordDecl>(
8858 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8860 // Skip empty base classes
8861 if (verbose == false &&
8862 ClangASTContext::RecordHasFields(base_class_decl) == false)
8865 if (base_class->isVirtual())
8867 record_layout.getVBaseClassOffset(base_class_decl)
8871 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8874 field_byte_offset = field_bit_offset / 8;
8875 assert(field_bit_offset % 8 == 0);
8881 clang::QualType base_class_qual_type = base_class->getType();
8882 std::string base_class_type_name(base_class_qual_type.getAsString());
8884 // Indent and print the base class type name
8885 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8886 base_class_type_name);
8888 clang::TypeInfo base_class_type_info =
8889 getASTContext()->getTypeInfo(base_class_qual_type);
8891 // Dump the value of the member
8892 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8893 base_clang_type.DumpValue(
8895 s, // Stream to dump to
8897 .GetFormat(), // The format with which to display the member
8898 data, // Data buffer containing all bytes for this type
8899 data_byte_offset + field_byte_offset, // Offset into "data" where
8900 // to grab value from
8901 base_class_type_info.Width / 8, // Size of this type in bytes
8902 0, // Bitfield bit size
8903 0, // Bitfield bit offset
8904 show_types, // Boolean indicating if we should show the variable
8906 show_summary, // Boolean indicating if we should show a summary
8907 // for the current type
8908 verbose, // Verbose output?
8909 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8915 uint32_t field_idx = 0;
8916 clang::RecordDecl::field_iterator field, field_end;
8917 for (field = record_decl->field_begin(),
8918 field_end = record_decl->field_end();
8919 field != field_end; ++field, ++field_idx, ++child_idx) {
8920 // Print the starting squiggly bracket (if this is the
8921 // first member) or comma (for member 2 and beyond) for
8922 // the struct/union/class member.
8929 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8931 clang::QualType field_type = field->getType();
8932 // Print the member type if requested
8933 // Figure out the type byte size (field_type_info.first) and
8934 // alignment (field_type_info.second) from the AST context.
8935 clang::TypeInfo field_type_info =
8936 getASTContext()->getTypeInfo(field_type);
8937 assert(field_idx < record_layout.getFieldCount());
8938 // Figure out the field offset within the current struct/union/class
8940 field_bit_offset = record_layout.getFieldOffset(field_idx);
8941 field_byte_offset = field_bit_offset / 8;
8942 uint32_t field_bitfield_bit_size = 0;
8943 uint32_t field_bitfield_bit_offset = 0;
8944 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8945 field_bitfield_bit_size))
8946 field_bitfield_bit_offset = field_bit_offset % 8;
8949 std::string field_type_name(field_type.getAsString());
8950 if (field_bitfield_bit_size > 0)
8951 s->Printf("(%s:%u) ", field_type_name.c_str(),
8952 field_bitfield_bit_size);
8954 s->Printf("(%s) ", field_type_name.c_str());
8956 // Print the member name and equal sign
8957 s->Printf("%s = ", field->getNameAsString().c_str());
8959 // Dump the value of the member
8960 CompilerType field_clang_type(getASTContext(), field_type);
8961 field_clang_type.DumpValue(
8963 s, // Stream to dump to
8965 .GetFormat(), // The format with which to display the member
8966 data, // Data buffer containing all bytes for this type
8967 data_byte_offset + field_byte_offset, // Offset into "data" where to
8969 field_type_info.Width / 8, // Size of this type in bytes
8970 field_bitfield_bit_size, // Bitfield bit size
8971 field_bitfield_bit_offset, // Bitfield bit offset
8972 show_types, // Boolean indicating if we should show the variable
8974 show_summary, // Boolean indicating if we should show a summary for
8976 verbose, // Verbose output?
8977 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8981 // Indent the trailing squiggly bracket
8983 s->Printf("\n%*s}", depth, "");
8987 case clang::Type::Enum:
8988 if (GetCompleteType(type)) {
8989 const clang::EnumType *enutype =
8990 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8991 const clang::EnumDecl *enum_decl = enutype->getDecl();
8993 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8994 lldb::offset_t offset = data_byte_offset;
8995 const int64_t enum_value = data.GetMaxU64Bitfield(
8996 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8997 for (enum_pos = enum_decl->enumerator_begin(),
8998 enum_end_pos = enum_decl->enumerator_end();
8999 enum_pos != enum_end_pos; ++enum_pos) {
9000 if (enum_pos->getInitVal() == enum_value) {
9001 s->Printf("%s", enum_pos->getNameAsString().c_str());
9005 // If we have gotten here we didn't get find the enumerator in the
9006 // enum decl, so just print the integer.
9007 s->Printf("%" PRIi64, enum_value);
9011 case clang::Type::ConstantArray: {
9012 const clang::ConstantArrayType *array =
9013 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9014 bool is_array_of_characters = false;
9015 clang::QualType element_qual_type = array->getElementType();
9017 const clang::Type *canonical_type =
9018 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9020 is_array_of_characters = canonical_type->isCharType();
9022 const uint64_t element_count = array->getSize().getLimitedValue();
9024 clang::TypeInfo field_type_info =
9025 getASTContext()->getTypeInfo(element_qual_type);
9027 uint32_t element_idx = 0;
9028 uint32_t element_offset = 0;
9029 uint64_t element_byte_size = field_type_info.Width / 8;
9030 uint32_t element_stride = element_byte_size;
9032 if (is_array_of_characters) {
9034 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size,
9035 element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9039 CompilerType element_clang_type(getASTContext(), element_qual_type);
9040 lldb::Format element_format = element_clang_type.GetFormat();
9042 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9043 // Print the starting squiggly bracket (if this is the
9044 // first member) or comman (for member 2 and beyong) for
9045 // the struct/union/class member.
9046 if (element_idx == 0)
9051 // Indent and print the index
9052 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9054 // Figure out the field offset within the current struct/union/class
9056 element_offset = element_idx * element_stride;
9058 // Dump the value of the member
9059 element_clang_type.DumpValue(
9061 s, // Stream to dump to
9062 element_format, // The format with which to display the element
9063 data, // Data buffer containing all bytes for this type
9065 element_offset, // Offset into "data" where to grab value from
9066 element_byte_size, // Size of this type in bytes
9067 0, // Bitfield bit size
9068 0, // Bitfield bit offset
9069 show_types, // Boolean indicating if we should show the variable
9071 show_summary, // Boolean indicating if we should show a summary for
9073 verbose, // Verbose output?
9074 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9078 // Indent the trailing squiggly bracket
9079 if (element_idx > 0)
9080 s->Printf("\n%*s}", depth, "");
9085 case clang::Type::Typedef: {
9086 clang::QualType typedef_qual_type =
9087 llvm::cast<clang::TypedefType>(qual_type)
9089 ->getUnderlyingType();
9091 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9092 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9093 clang::TypeInfo typedef_type_info =
9094 getASTContext()->getTypeInfo(typedef_qual_type);
9095 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9097 return typedef_clang_type.DumpValue(
9099 s, // Stream to dump to
9100 typedef_format, // The format with which to display the element
9101 data, // Data buffer containing all bytes for this type
9102 data_byte_offset, // Offset into "data" where to grab value from
9103 typedef_byte_size, // Size of this type in bytes
9104 bitfield_bit_size, // Bitfield bit size
9105 bitfield_bit_offset, // Bitfield bit offset
9106 show_types, // Boolean indicating if we should show the variable types
9107 show_summary, // Boolean indicating if we should show a summary for the
9109 verbose, // Verbose output?
9110 depth); // Scope depth for any types that have children
9113 case clang::Type::Auto: {
9114 clang::QualType elaborated_qual_type =
9115 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9116 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9117 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9118 clang::TypeInfo elaborated_type_info =
9119 getASTContext()->getTypeInfo(elaborated_qual_type);
9120 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9122 return elaborated_clang_type.DumpValue(
9124 s, // Stream to dump to
9125 elaborated_format, // The format with which to display the element
9126 data, // Data buffer containing all bytes for this type
9127 data_byte_offset, // Offset into "data" where to grab value from
9128 elaborated_byte_size, // Size of this type in bytes
9129 bitfield_bit_size, // Bitfield bit size
9130 bitfield_bit_offset, // Bitfield bit offset
9131 show_types, // Boolean indicating if we should show the variable types
9132 show_summary, // Boolean indicating if we should show a summary for the
9134 verbose, // Verbose output?
9135 depth); // Scope depth for any types that have children
9138 case clang::Type::Elaborated: {
9139 clang::QualType elaborated_qual_type =
9140 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9141 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9142 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9143 clang::TypeInfo elaborated_type_info =
9144 getASTContext()->getTypeInfo(elaborated_qual_type);
9145 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9147 return elaborated_clang_type.DumpValue(
9149 s, // Stream to dump to
9150 elaborated_format, // The format with which to display the element
9151 data, // Data buffer containing all bytes for this type
9152 data_byte_offset, // Offset into "data" where to grab value from
9153 elaborated_byte_size, // Size of this type in bytes
9154 bitfield_bit_size, // Bitfield bit size
9155 bitfield_bit_offset, // Bitfield bit offset
9156 show_types, // Boolean indicating if we should show the variable types
9157 show_summary, // Boolean indicating if we should show a summary for the
9159 verbose, // Verbose output?
9160 depth); // Scope depth for any types that have children
9163 case clang::Type::Paren: {
9164 clang::QualType desugar_qual_type =
9165 llvm::cast<clang::ParenType>(qual_type)->desugar();
9166 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9168 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9169 clang::TypeInfo desugar_type_info =
9170 getASTContext()->getTypeInfo(desugar_qual_type);
9171 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9173 return desugar_clang_type.DumpValue(
9175 s, // Stream to dump to
9176 desugar_format, // The format with which to display the element
9177 data, // Data buffer containing all bytes for this type
9178 data_byte_offset, // Offset into "data" where to grab value from
9179 desugar_byte_size, // Size of this type in bytes
9180 bitfield_bit_size, // Bitfield bit size
9181 bitfield_bit_offset, // Bitfield bit offset
9182 show_types, // Boolean indicating if we should show the variable types
9183 show_summary, // Boolean indicating if we should show a summary for the
9185 verbose, // Verbose output?
9186 depth); // Scope depth for any types that have children
9190 // We are down to a scalar type that we just need to display.
9191 data.Dump(s, data_byte_offset, format, data_byte_size, 1, UINT32_MAX,
9192 LLDB_INVALID_ADDRESS, bitfield_bit_size, bitfield_bit_offset);
9195 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9200 bool ClangASTContext::DumpTypeValue(
9201 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9202 const lldb_private::DataExtractor &data, lldb::offset_t byte_offset,
9203 size_t byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9204 ExecutionContextScope *exe_scope) {
9207 if (IsAggregateType(type)) {
9210 clang::QualType qual_type(GetQualType(type));
9212 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9213 switch (type_class) {
9214 case clang::Type::Typedef: {
9215 clang::QualType typedef_qual_type =
9216 llvm::cast<clang::TypedefType>(qual_type)
9218 ->getUnderlyingType();
9219 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9220 if (format == eFormatDefault)
9221 format = typedef_clang_type.GetFormat();
9222 clang::TypeInfo typedef_type_info =
9223 getASTContext()->getTypeInfo(typedef_qual_type);
9224 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9226 return typedef_clang_type.DumpTypeValue(
9228 format, // The format with which to display the element
9229 data, // Data buffer containing all bytes for this type
9230 byte_offset, // Offset into "data" where to grab value from
9231 typedef_byte_size, // Size of this type in bytes
9232 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9233 // treat as a bitfield
9234 bitfield_bit_offset, // Offset in bits of a bitfield value if
9235 // bitfield_bit_size != 0
9239 case clang::Type::Enum:
9240 // If our format is enum or default, show the enumeration value as
9241 // its enumeration string value, else just display it as requested.
9242 if ((format == eFormatEnum || format == eFormatDefault) &&
9243 GetCompleteType(type)) {
9244 const clang::EnumType *enutype =
9245 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9246 const clang::EnumDecl *enum_decl = enutype->getDecl();
9248 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9249 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9250 lldb::offset_t offset = byte_offset;
9252 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9253 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9254 for (enum_pos = enum_decl->enumerator_begin(),
9255 enum_end_pos = enum_decl->enumerator_end();
9256 enum_pos != enum_end_pos; ++enum_pos) {
9257 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9258 s->PutCString(enum_pos->getNameAsString());
9262 // If we have gotten here we didn't get find the enumerator in the
9263 // enum decl, so just print the integer.
9264 s->Printf("%" PRIi64, enum_svalue);
9266 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9267 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9268 for (enum_pos = enum_decl->enumerator_begin(),
9269 enum_end_pos = enum_decl->enumerator_end();
9270 enum_pos != enum_end_pos; ++enum_pos) {
9271 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9272 s->PutCString(enum_pos->getNameAsString());
9276 // If we have gotten here we didn't get find the enumerator in the
9277 // enum decl, so just print the integer.
9278 s->Printf("%" PRIu64, enum_uvalue);
9282 // format was not enum, just fall through and dump the value as
9287 // We are down to a scalar type that we just need to display.
9289 uint32_t item_count = 1;
9290 // A few formats, we might need to modify our size and count for
9292 // on how we are trying to display the value...
9295 case eFormatBoolean:
9297 case eFormatComplex:
9298 case eFormatCString: // NULL terminated C strings
9299 case eFormatDecimal:
9302 case eFormatHexUppercase:
9306 case eFormatUnsigned:
9307 case eFormatPointer:
9308 case eFormatVectorOfChar:
9309 case eFormatVectorOfSInt8:
9310 case eFormatVectorOfUInt8:
9311 case eFormatVectorOfSInt16:
9312 case eFormatVectorOfUInt16:
9313 case eFormatVectorOfSInt32:
9314 case eFormatVectorOfUInt32:
9315 case eFormatVectorOfSInt64:
9316 case eFormatVectorOfUInt64:
9317 case eFormatVectorOfFloat32:
9318 case eFormatVectorOfFloat64:
9319 case eFormatVectorOfUInt128:
9323 case eFormatCharPrintable:
9324 case eFormatCharArray:
9326 case eFormatBytesWithASCII:
9327 item_count = byte_size;
9331 case eFormatUnicode16:
9332 item_count = byte_size / 2;
9336 case eFormatUnicode32:
9337 item_count = byte_size / 4;
9341 return data.Dump(s, byte_offset, format, byte_size, item_count,
9342 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9343 bitfield_bit_offset, exe_scope);
9351 void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9352 ExecutionContext *exe_ctx, Stream *s,
9353 const lldb_private::DataExtractor &data,
9354 lldb::offset_t data_byte_offset,
9355 size_t data_byte_size) {
9356 uint32_t length = 0;
9357 if (IsCStringType(type, length)) {
9359 Process *process = exe_ctx->GetProcessPtr();
9361 lldb::offset_t offset = data_byte_offset;
9362 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9363 std::vector<uint8_t> buf;
9369 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(),
9370 process->GetByteOrder(), 4);
9373 size_t total_cstr_len = 0;
9375 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9376 buf.size(), error)) > 0) {
9377 const size_t len = strlen((const char *)&buf.front());
9380 if (total_cstr_len == 0)
9381 s->PutCString(" \"");
9382 cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX,
9383 LLDB_INVALID_ADDRESS, 0, 0);
9384 total_cstr_len += len;
9385 if (len < buf.size())
9387 pointer_address += total_cstr_len;
9389 if (total_cstr_len > 0)
9396 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9397 StreamFile s(stdout, false);
9398 DumpTypeDescription(type, &s);
9399 ClangASTMetadata *metadata =
9400 ClangASTContext::GetMetadata(getASTContext(), type);
9406 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9409 clang::QualType qual_type(GetQualType(type));
9411 llvm::SmallVector<char, 1024> buf;
9412 llvm::raw_svector_ostream llvm_ostrm(buf);
9414 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9415 switch (type_class) {
9416 case clang::Type::ObjCObject:
9417 case clang::Type::ObjCInterface: {
9418 GetCompleteType(type);
9420 const clang::ObjCObjectType *objc_class_type =
9421 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9422 assert(objc_class_type);
9423 if (objc_class_type) {
9424 clang::ObjCInterfaceDecl *class_interface_decl =
9425 objc_class_type->getInterface();
9426 if (class_interface_decl) {
9427 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9428 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9433 case clang::Type::Typedef: {
9434 const clang::TypedefType *typedef_type =
9435 qual_type->getAs<clang::TypedefType>();
9437 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9438 std::string clang_typedef_name(
9439 typedef_decl->getQualifiedNameAsString());
9440 if (!clang_typedef_name.empty()) {
9441 s->PutCString("typedef ");
9442 s->PutCString(clang_typedef_name);
9447 case clang::Type::Auto:
9448 CompilerType(getASTContext(),
9449 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9450 .DumpTypeDescription(s);
9453 case clang::Type::Elaborated:
9454 CompilerType(getASTContext(),
9455 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9456 .DumpTypeDescription(s);
9459 case clang::Type::Paren:
9460 CompilerType(getASTContext(),
9461 llvm::cast<clang::ParenType>(qual_type)->desugar())
9462 .DumpTypeDescription(s);
9465 case clang::Type::Record: {
9466 GetCompleteType(type);
9468 const clang::RecordType *record_type =
9469 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9470 const clang::RecordDecl *record_decl = record_type->getDecl();
9471 const clang::CXXRecordDecl *cxx_record_decl =
9472 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9474 if (cxx_record_decl)
9475 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9476 s->GetIndentLevel());
9478 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9479 s->GetIndentLevel());
9483 const clang::TagType *tag_type =
9484 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9486 clang::TagDecl *tag_decl = tag_type->getDecl();
9488 tag_decl->print(llvm_ostrm, 0);
9490 std::string clang_type_name(qual_type.getAsString());
9491 if (!clang_type_name.empty())
9492 s->PutCString(clang_type_name);
9497 if (buf.size() > 0) {
9498 s->Write(buf.data(), buf.size());
9503 void ClangASTContext::DumpTypeName(const CompilerType &type) {
9504 if (ClangUtil::IsClangType(type)) {
9505 clang::QualType qual_type(
9506 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9508 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9509 switch (type_class) {
9510 case clang::Type::Record: {
9511 const clang::CXXRecordDecl *cxx_record_decl =
9512 qual_type->getAsCXXRecordDecl();
9513 if (cxx_record_decl)
9514 printf("class %s", cxx_record_decl->getName().str().c_str());
9517 case clang::Type::Enum: {
9518 clang::EnumDecl *enum_decl =
9519 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9521 printf("enum %s", enum_decl->getName().str().c_str());
9525 case clang::Type::ObjCObject:
9526 case clang::Type::ObjCInterface: {
9527 const clang::ObjCObjectType *objc_class_type =
9528 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9529 if (objc_class_type) {
9530 clang::ObjCInterfaceDecl *class_interface_decl =
9531 objc_class_type->getInterface();
9532 // We currently can't complete objective C types through the newly added
9534 // because it only supports TagDecl objects right now...
9535 if (class_interface_decl)
9536 printf("@class %s", class_interface_decl->getName().str().c_str());
9540 case clang::Type::Typedef:
9541 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9548 case clang::Type::Auto:
9550 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9551 llvm::cast<clang::AutoType>(qual_type)
9553 .getAsOpaquePtr()));
9555 case clang::Type::Elaborated:
9556 printf("elaborated ");
9557 return DumpTypeName(CompilerType(
9558 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9560 .getAsOpaquePtr()));
9562 case clang::Type::Paren:
9564 return DumpTypeName(CompilerType(
9565 type.GetTypeSystem(),
9566 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9569 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9575 clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9576 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9577 const char *parent_name, int tag_decl_kind,
9578 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9579 if (template_param_infos.IsValid()) {
9580 std::string template_basename(parent_name);
9581 template_basename.erase(template_basename.find('<'));
9583 return CreateClassTemplateDecl(decl_ctx, access_type,
9584 template_basename.c_str(), tag_decl_kind,
9585 template_param_infos);
9590 void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9591 ClangASTContext *ast = (ClangASTContext *)baton;
9592 SymbolFile *sym_file = ast->GetSymbolFile();
9594 CompilerType clang_type = GetTypeForDecl(decl);
9596 sym_file->CompleteType(clang_type);
9600 void ClangASTContext::CompleteObjCInterfaceDecl(
9601 void *baton, clang::ObjCInterfaceDecl *decl) {
9602 ClangASTContext *ast = (ClangASTContext *)baton;
9603 SymbolFile *sym_file = ast->GetSymbolFile();
9605 CompilerType clang_type = GetTypeForDecl(decl);
9607 sym_file->CompleteType(clang_type);
9611 DWARFASTParser *ClangASTContext::GetDWARFParser() {
9612 if (!m_dwarf_ast_parser_ap)
9613 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9614 return m_dwarf_ast_parser_ap.get();
9618 PDBASTParser *ClangASTContext::GetPDBParser() {
9619 if (!m_pdb_ast_parser_ap)
9620 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9621 return m_pdb_ast_parser_ap.get();
9625 bool ClangASTContext::LayoutRecordType(
9626 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9627 uint64_t &alignment,
9628 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9629 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9631 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9633 ClangASTContext *ast = (ClangASTContext *)baton;
9634 DWARFASTParserClang *dwarf_ast_parser =
9635 (DWARFASTParserClang *)ast->GetDWARFParser();
9636 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9637 record_decl, bit_size, alignment, field_offsets, base_offsets,
9641 //----------------------------------------------------------------------
9642 // CompilerDecl override functions
9643 //----------------------------------------------------------------------
9645 ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9647 clang::NamedDecl *nd =
9648 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9650 return ConstString(nd->getDeclName().getAsString());
9652 return ConstString();
9655 ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9657 clang::NamedDecl *nd =
9658 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9659 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9660 clang::MangleContext *mc = getMangleContext();
9661 if (mc && mc->shouldMangleCXXName(nd)) {
9662 llvm::SmallVector<char, 1024> buf;
9663 llvm::raw_svector_ostream llvm_ostrm(buf);
9664 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9665 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9666 Ctor_Complete, llvm_ostrm);
9667 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9668 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9669 Dtor_Complete, llvm_ostrm);
9671 mc->mangleName(nd, llvm_ostrm);
9674 return ConstString(buf.data(), buf.size());
9678 return ConstString();
9681 CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9683 return CompilerDeclContext(this,
9684 ((clang::Decl *)opaque_decl)->getDeclContext());
9686 return CompilerDeclContext();
9689 CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9690 if (clang::FunctionDecl *func_decl =
9691 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9692 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9693 if (clang::ObjCMethodDecl *objc_method =
9694 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9695 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9697 return CompilerType();
9700 size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9701 if (clang::FunctionDecl *func_decl =
9702 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9703 return func_decl->param_size();
9704 if (clang::ObjCMethodDecl *objc_method =
9705 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9706 return objc_method->param_size();
9711 CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9713 if (clang::FunctionDecl *func_decl =
9714 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9715 if (idx < func_decl->param_size()) {
9716 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9718 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9720 } else if (clang::ObjCMethodDecl *objc_method =
9721 llvm::dyn_cast<clang::ObjCMethodDecl>(
9722 (clang::Decl *)opaque_decl)) {
9723 if (idx < objc_method->param_size())
9724 return CompilerType(
9726 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9728 return CompilerType();
9731 //----------------------------------------------------------------------
9732 // CompilerDeclContext functions
9733 //----------------------------------------------------------------------
9735 std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9736 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9737 std::vector<CompilerDecl> found_decls;
9738 if (opaque_decl_ctx) {
9739 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9740 std::set<DeclContext *> searched;
9741 std::multimap<DeclContext *, DeclContext *> search_queue;
9742 SymbolFile *symbol_file = GetSymbolFile();
9744 for (clang::DeclContext *decl_context = root_decl_ctx;
9745 decl_context != nullptr && found_decls.empty();
9746 decl_context = decl_context->getParent()) {
9747 search_queue.insert(std::make_pair(decl_context, decl_context));
9749 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9751 if (!searched.insert(it->second).second)
9753 symbol_file->ParseDeclsForContext(
9754 CompilerDeclContext(this, it->second));
9756 for (clang::Decl *child : it->second->decls()) {
9757 if (clang::UsingDirectiveDecl *ud =
9758 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9759 if (ignore_using_decls)
9761 clang::DeclContext *from = ud->getCommonAncestor();
9762 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9763 search_queue.insert(
9764 std::make_pair(from, ud->getNominatedNamespace()));
9765 } else if (clang::UsingDecl *ud =
9766 llvm::dyn_cast<clang::UsingDecl>(child)) {
9767 if (ignore_using_decls)
9769 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9770 clang::Decl *target = usd->getTargetDecl();
9771 if (clang::NamedDecl *nd =
9772 llvm::dyn_cast<clang::NamedDecl>(target)) {
9773 IdentifierInfo *ii = nd->getIdentifier();
9774 if (ii != nullptr &&
9775 ii->getName().equals(name.AsCString(nullptr)))
9776 found_decls.push_back(CompilerDecl(this, nd));
9779 } else if (clang::NamedDecl *nd =
9780 llvm::dyn_cast<clang::NamedDecl>(child)) {
9781 IdentifierInfo *ii = nd->getIdentifier();
9782 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9783 found_decls.push_back(CompilerDecl(this, nd));
9792 // Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9793 // and return the number of levels it took to find it, or
9794 // LLDB_INVALID_DECL_LEVEL
9795 // if not found. If the decl was imported via a using declaration, its name
9797 // type, if set, will be used to check that the decl found in the scope is a
9800 // The optional name is required by languages (like C++) to handle using
9811 // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9812 // // LLDB_INVALID_DECL_LEVEL for 'goo'.
9815 // The optional type is useful in the case that there's a specific overload
9816 // that we're looking for that might otherwise be shadowed, like:
9824 // // CountDeclLevels returns 0 for { 'foo', void() },
9825 // // 1 for { 'foo', void(int) }, and
9826 // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9829 // NOTE: Because file statics are at the TranslationUnit along with globals, a
9830 // function at file scope will return the same level as a function at global
9832 // Ideally we'd like to treat the file scope as an additional scope just below
9834 // global scope. More work needs to be done to recognise that, if the decl
9836 // trying to look up is static, we should compare its source file with that of
9838 // current scope and return a lower number for it.
9839 uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9840 clang::DeclContext *child_decl_ctx,
9841 ConstString *child_name,
9842 CompilerType *child_type) {
9843 if (frame_decl_ctx) {
9844 std::set<DeclContext *> searched;
9845 std::multimap<DeclContext *, DeclContext *> search_queue;
9846 SymbolFile *symbol_file = GetSymbolFile();
9848 // Get the lookup scope for the decl we're trying to find.
9849 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9851 // Look for it in our scope's decl context and its parents.
9853 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9854 decl_ctx = decl_ctx->getParent()) {
9855 if (!decl_ctx->isLookupContext())
9857 if (decl_ctx == parent_decl_ctx)
9860 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9861 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9863 if (searched.find(it->second) != searched.end())
9866 // Currently DWARF has one shared translation unit for all Decls at top
9868 // would erroneously find using statements anywhere. So don't look at
9870 // translation unit.
9871 // TODO fix this and add a testcase that depends on it.
9873 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9876 searched.insert(it->second);
9877 symbol_file->ParseDeclsForContext(
9878 CompilerDeclContext(this, it->second));
9880 for (clang::Decl *child : it->second->decls()) {
9881 if (clang::UsingDirectiveDecl *ud =
9882 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9883 clang::DeclContext *ns = ud->getNominatedNamespace();
9884 if (ns == parent_decl_ctx)
9887 clang::DeclContext *from = ud->getCommonAncestor();
9888 if (searched.find(ns) == searched.end())
9889 search_queue.insert(std::make_pair(from, ns));
9890 } else if (child_name) {
9891 if (clang::UsingDecl *ud =
9892 llvm::dyn_cast<clang::UsingDecl>(child)) {
9893 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9894 clang::Decl *target = usd->getTargetDecl();
9895 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9899 IdentifierInfo *ii = nd->getIdentifier();
9900 if (ii == nullptr ||
9901 !ii->getName().equals(child_name->AsCString(nullptr)))
9903 // Check types, if one was provided.
9905 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9906 if (!AreTypesSame(clang_type, *child_type,
9907 /*ignore_qualifiers=*/true))
9920 return LLDB_INVALID_DECL_LEVEL;
9923 bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9924 if (opaque_decl_ctx)
9925 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9930 ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9931 if (opaque_decl_ctx) {
9932 clang::NamedDecl *named_decl =
9933 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9935 return ConstString(named_decl->getName());
9937 return ConstString();
9941 ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9942 if (opaque_decl_ctx) {
9943 clang::NamedDecl *named_decl =
9944 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9947 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9949 return ConstString();
9952 bool ClangASTContext::DeclContextIsClassMethod(
9953 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9954 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9955 if (opaque_decl_ctx) {
9956 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9957 if (ObjCMethodDecl *objc_method =
9958 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9959 if (is_instance_method_ptr)
9960 *is_instance_method_ptr = objc_method->isInstanceMethod();
9962 *language_ptr = eLanguageTypeObjC;
9963 if (language_object_name_ptr)
9964 language_object_name_ptr->SetCString("self");
9966 } else if (CXXMethodDecl *cxx_method =
9967 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9968 if (is_instance_method_ptr)
9969 *is_instance_method_ptr = cxx_method->isInstance();
9971 *language_ptr = eLanguageTypeC_plus_plus;
9972 if (language_object_name_ptr)
9973 language_object_name_ptr->SetCString("this");
9975 } else if (clang::FunctionDecl *function_decl =
9976 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9977 ClangASTMetadata *metadata =
9978 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9979 if (metadata && metadata->HasObjectPtr()) {
9980 if (is_instance_method_ptr)
9981 *is_instance_method_ptr = true;
9983 *language_ptr = eLanguageTypeObjC;
9984 if (language_object_name_ptr)
9985 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9993 clang::DeclContext *
9994 ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9996 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10001 ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10003 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10004 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10009 ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10011 return llvm::dyn_cast<clang::CXXMethodDecl>(
10012 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10016 clang::FunctionDecl *
10017 ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10019 return llvm::dyn_cast<clang::FunctionDecl>(
10020 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10024 clang::NamespaceDecl *
10025 ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10027 return llvm::dyn_cast<clang::NamespaceDecl>(
10028 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10033 ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10034 const void *object) {
10035 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10037 return ClangASTContext::GetMetadata(ast, object);
10041 clang::ASTContext *
10042 ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10043 ClangASTContext *ast =
10044 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10046 return ast->getASTContext();
10050 ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10051 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10052 m_target_wp(target.shared_from_this()),
10053 m_persistent_variables(new ClangPersistentVariables) {}
10055 UserExpression *ClangASTContextForExpressions::GetUserExpression(
10056 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
10057 Expression::ResultType desired_type,
10058 const EvaluateExpressionOptions &options) {
10059 TargetSP target_sp = m_target_wp.lock();
10063 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
10064 desired_type, options);
10067 FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10068 const CompilerType &return_type, const Address &function_address,
10069 const ValueList &arg_value_list, const char *name) {
10070 TargetSP target_sp = m_target_wp.lock();
10074 Process *process = target_sp->GetProcessSP().get();
10078 return new ClangFunctionCaller(*process, return_type, function_address,
10079 arg_value_list, name);
10083 ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10084 const char *name) {
10085 TargetSP target_sp = m_target_wp.lock();
10089 return new ClangUtilityFunction(*target_sp.get(), text, name);
10092 PersistentExpressionState *
10093 ClangASTContextForExpressions::GetPersistentExpressionState() {
10094 return m_persistent_variables.get();