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"
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"
68 #include "llvm/Support/Threading.h"
70 #include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
71 #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
72 #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
73 #include "lldb/Core/ArchSpec.h"
74 #include "lldb/Utility/Flags.h"
76 #include "lldb/Core/DumpDataExtractor.h"
77 #include "lldb/Core/Module.h"
78 #include "lldb/Core/PluginManager.h"
79 #include "lldb/Core/Scalar.h"
80 #include "lldb/Core/StreamFile.h"
81 #include "lldb/Core/ThreadSafeDenseMap.h"
82 #include "lldb/Core/UniqueCStringMap.h"
83 #include "lldb/Symbol/ClangASTContext.h"
84 #include "lldb/Symbol/ClangASTImporter.h"
85 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
86 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
87 #include "lldb/Symbol/ClangUtil.h"
88 #include "lldb/Symbol/ObjectFile.h"
89 #include "lldb/Symbol/SymbolFile.h"
90 #include "lldb/Symbol/VerifyDecl.h"
91 #include "lldb/Target/ExecutionContext.h"
92 #include "lldb/Target/Language.h"
93 #include "lldb/Target/ObjCLanguageRuntime.h"
94 #include "lldb/Target/Process.h"
95 #include "lldb/Target/Target.h"
96 #include "lldb/Utility/DataExtractor.h"
97 #include "lldb/Utility/LLDBAssert.h"
98 #include "lldb/Utility/Log.h"
99 #include "lldb/Utility/RegularExpression.h"
101 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
102 //#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
108 using namespace lldb;
109 using namespace lldb_private;
110 using namespace llvm;
111 using namespace clang;
115 ClangASTContextSupportsLanguage(lldb::LanguageType language) {
116 return language == eLanguageTypeUnknown || // Clang is the default type system
117 Language::LanguageIsC(language) ||
118 Language::LanguageIsCPlusPlus(language) ||
119 Language::LanguageIsObjC(language) ||
120 Language::LanguageIsPascal(language) ||
121 // Use Clang for Rust until there is a proper language plugin for it
122 language == eLanguageTypeRust ||
123 language == eLanguageTypeExtRenderScript ||
124 // Use Clang for D until there is a proper language plugin for it
125 language == eLanguageTypeD;
129 typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
132 static ClangASTMap &GetASTMap() {
133 static ClangASTMap *g_map_ptr = nullptr;
134 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
136 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
141 static bool IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
143 if (name == nullptr || name[0] == '\0')
146 #define OPERATOR_PREFIX "operator"
147 #define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
149 const char *post_op_name = nullptr;
151 bool no_space = true;
153 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
156 post_op_name = name + OPERATOR_PREFIX_LENGTH;
158 if (post_op_name[0] == ' ') {
163 #undef OPERATOR_PREFIX
164 #undef OPERATOR_PREFIX_LENGTH
166 // This is an operator, set the overloaded operator kind to invalid
167 // in case this is a conversion operator...
168 op_kind = clang::NUM_OVERLOADED_OPERATORS;
170 switch (post_op_name[0]) {
178 if (strcmp(post_op_name, "new") == 0)
179 op_kind = clang::OO_New;
180 else if (strcmp(post_op_name, "new[]") == 0)
181 op_kind = clang::OO_Array_New;
187 if (strcmp(post_op_name, "delete") == 0)
188 op_kind = clang::OO_Delete;
189 else if (strcmp(post_op_name, "delete[]") == 0)
190 op_kind = clang::OO_Array_Delete;
194 if (post_op_name[1] == '\0')
195 op_kind = clang::OO_Plus;
196 else if (post_op_name[2] == '\0') {
197 if (post_op_name[1] == '=')
198 op_kind = clang::OO_PlusEqual;
199 else if (post_op_name[1] == '+')
200 op_kind = clang::OO_PlusPlus;
205 if (post_op_name[1] == '\0')
206 op_kind = clang::OO_Minus;
207 else if (post_op_name[2] == '\0') {
208 switch (post_op_name[1]) {
210 op_kind = clang::OO_MinusEqual;
213 op_kind = clang::OO_MinusMinus;
216 op_kind = clang::OO_Arrow;
219 } else if (post_op_name[3] == '\0') {
220 if (post_op_name[2] == '*')
221 op_kind = clang::OO_ArrowStar;
227 if (post_op_name[1] == '\0')
228 op_kind = clang::OO_Star;
229 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
230 op_kind = clang::OO_StarEqual;
234 if (post_op_name[1] == '\0')
235 op_kind = clang::OO_Slash;
236 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
237 op_kind = clang::OO_SlashEqual;
241 if (post_op_name[1] == '\0')
242 op_kind = clang::OO_Percent;
243 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
244 op_kind = clang::OO_PercentEqual;
248 if (post_op_name[1] == '\0')
249 op_kind = clang::OO_Caret;
250 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
251 op_kind = clang::OO_CaretEqual;
255 if (post_op_name[1] == '\0')
256 op_kind = clang::OO_Amp;
257 else if (post_op_name[2] == '\0') {
258 switch (post_op_name[1]) {
260 op_kind = clang::OO_AmpEqual;
263 op_kind = clang::OO_AmpAmp;
270 if (post_op_name[1] == '\0')
271 op_kind = clang::OO_Pipe;
272 else if (post_op_name[2] == '\0') {
273 switch (post_op_name[1]) {
275 op_kind = clang::OO_PipeEqual;
278 op_kind = clang::OO_PipePipe;
285 if (post_op_name[1] == '\0')
286 op_kind = clang::OO_Tilde;
290 if (post_op_name[1] == '\0')
291 op_kind = clang::OO_Exclaim;
292 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
293 op_kind = clang::OO_ExclaimEqual;
297 if (post_op_name[1] == '\0')
298 op_kind = clang::OO_Equal;
299 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
300 op_kind = clang::OO_EqualEqual;
304 if (post_op_name[1] == '\0')
305 op_kind = clang::OO_Less;
306 else if (post_op_name[2] == '\0') {
307 switch (post_op_name[1]) {
309 op_kind = clang::OO_LessLess;
312 op_kind = clang::OO_LessEqual;
315 } else if (post_op_name[3] == '\0') {
316 if (post_op_name[2] == '=')
317 op_kind = clang::OO_LessLessEqual;
322 if (post_op_name[1] == '\0')
323 op_kind = clang::OO_Greater;
324 else if (post_op_name[2] == '\0') {
325 switch (post_op_name[1]) {
327 op_kind = clang::OO_GreaterGreater;
330 op_kind = clang::OO_GreaterEqual;
333 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
334 post_op_name[3] == '\0') {
335 op_kind = clang::OO_GreaterGreaterEqual;
340 if (post_op_name[1] == '\0')
341 op_kind = clang::OO_Comma;
345 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
346 op_kind = clang::OO_Call;
350 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
351 op_kind = clang::OO_Subscript;
358 clang::AccessSpecifier
359 ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
369 case eAccessProtected:
375 static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
376 // FIXME: Cleanup per-file based stuff.
378 // Set some properties which depend solely on the input kind; it would be nice
379 // to move these to the language standard, and have the driver resolve the
380 // input kind + language standard.
381 if (IK.getLanguage() == InputKind::Asm) {
382 Opts.AsmPreprocessor = 1;
383 } else if (IK.isObjectiveC()) {
384 Opts.ObjC1 = Opts.ObjC2 = 1;
387 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
389 if (LangStd == LangStandard::lang_unspecified) {
390 // Based on the base language, pick one.
391 switch (IK.getLanguage()) {
392 case InputKind::Unknown:
393 case InputKind::LLVM_IR:
394 case InputKind::RenderScript:
395 llvm_unreachable("Invalid input kind!");
396 case InputKind::OpenCL:
397 LangStd = LangStandard::lang_opencl10;
399 case InputKind::CUDA:
400 LangStd = LangStandard::lang_cuda;
404 case InputKind::ObjC:
405 LangStd = LangStandard::lang_gnu99;
408 case InputKind::ObjCXX:
409 LangStd = LangStandard::lang_gnucxx98;
414 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
415 Opts.LineComment = Std.hasLineComments();
416 Opts.C99 = Std.isC99();
417 Opts.CPlusPlus = Std.isCPlusPlus();
418 Opts.CPlusPlus11 = Std.isCPlusPlus11();
419 Opts.Digraphs = Std.hasDigraphs();
420 Opts.GNUMode = Std.isGNUMode();
421 Opts.GNUInline = !Std.isC99();
422 Opts.HexFloats = Std.hasHexFloats();
423 Opts.ImplicitInt = Std.hasImplicitInt();
427 // OpenCL has some additional defaults.
428 if (LangStd == LangStandard::lang_opencl10) {
431 Opts.CXXOperatorNames = 1;
432 Opts.LaxVectorConversions = 1;
435 // OpenCL and C++ both have bool, true, false keywords.
436 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
438 // if (Opts.CPlusPlus)
439 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
441 // if (Args.hasArg(OPT_fobjc_gc_only))
442 // Opts.setGCMode(LangOptions::GCOnly);
443 // else if (Args.hasArg(OPT_fobjc_gc))
444 // Opts.setGCMode(LangOptions::HybridGC);
446 // if (Args.hasArg(OPT_print_ivar_layout))
447 // Opts.ObjCGCBitmapPrint = 1;
449 // if (Args.hasArg(OPT_faltivec))
452 // if (Args.hasArg(OPT_pthread))
453 // Opts.POSIXThreads = 1;
455 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
457 // if (Vis == "default")
458 Opts.setValueVisibilityMode(DefaultVisibility);
459 // else if (Vis == "hidden")
460 // Opts.setVisibilityMode(LangOptions::Hidden);
461 // else if (Vis == "protected")
462 // Opts.setVisibilityMode(LangOptions::Protected);
464 // Diags.Report(diag::err_drv_invalid_value)
465 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
467 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
469 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
470 // is specified, or -std is set to a conforming mode.
471 Opts.Trigraphs = !Opts.GNUMode;
472 // if (Args.hasArg(OPT_trigraphs))
473 // Opts.Trigraphs = 1;
475 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
476 // OPT_fno_dollars_in_identifiers,
477 // !Opts.AsmPreprocessor);
478 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
479 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
480 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
481 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
482 // Opts.LaxVectorConversions = 0;
483 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
484 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
485 // Opts.Blocks = Args.hasArg(OPT_fblocks);
486 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
487 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
488 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
489 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
490 // Opts.AssumeSaneOperatorNew =
491 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
492 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
493 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
494 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
495 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
496 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
499 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
500 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
501 // OPT_fconstant_string_class);
502 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
503 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
504 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
505 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
506 // Opts.Static = Args.hasArg(OPT_static_define);
507 Opts.OptimizeSize = 0;
509 // FIXME: Eliminate this dependency.
511 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
512 // Opts.Optimize = Opt != 0;
515 // This is the __NO_INLINE__ define, which just depends on things like the
516 // optimization level and -fno-inline, not actually whether the backend has
519 // FIXME: This is affected by other options (-fno-inline).
520 Opts.NoInlineDefine = !Opt;
522 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
525 // Diags.Report(diag::err_drv_invalid_value)
526 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
529 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
530 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
531 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
535 ClangASTContext::ClangASTContext(const char *target_triple)
536 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
537 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
538 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
539 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
540 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
541 m_pointer_byte_size(0), m_ast_owned(false) {
542 if (target_triple && target_triple[0])
543 SetTargetTriple(target_triple);
546 //----------------------------------------------------------------------
548 //----------------------------------------------------------------------
549 ClangASTContext::~ClangASTContext() { Finalize(); }
551 ConstString ClangASTContext::GetPluginNameStatic() {
552 return ConstString("clang");
555 ConstString ClangASTContext::GetPluginName() {
556 return ClangASTContext::GetPluginNameStatic();
559 uint32_t ClangASTContext::GetPluginVersion() { return 1; }
561 lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
562 lldb_private::Module *module,
564 if (ClangASTContextSupportsLanguage(language)) {
567 arch = module->GetArchitecture();
569 arch = target->GetArchitecture();
571 if (arch.IsValid()) {
572 ArchSpec fixed_arch = arch;
573 // LLVM wants this to be set to iOS or MacOSX; if we're working on
574 // a bare-boards type image, change the triple for llvm's benefit.
575 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
576 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
577 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
578 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
579 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
580 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
582 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
587 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
589 ast_sp->SetArchitecture(fixed_arch);
592 } else if (target && target->IsValid()) {
593 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
594 new ClangASTContextForExpressions(*target));
596 ast_sp->SetArchitecture(fixed_arch);
597 ast_sp->m_scratch_ast_source_ap.reset(
598 new ClangASTSource(target->shared_from_this()));
599 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
600 ast_sp->getASTContext());
601 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
602 ast_sp->m_scratch_ast_source_ap->CreateProxy());
603 ast_sp->SetExternalSource(proxy_ast_source);
609 return lldb::TypeSystemSP();
612 void ClangASTContext::EnumerateSupportedLanguages(
613 std::set<lldb::LanguageType> &languages_for_types,
614 std::set<lldb::LanguageType> &languages_for_expressions) {
615 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
616 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
617 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
618 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
619 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
620 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
622 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
623 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
624 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
625 lldb::eLanguageTypeC_plus_plus_14});
627 languages_for_types.insert(s_supported_languages_for_types.begin(),
628 s_supported_languages_for_types.end());
629 languages_for_expressions.insert(
630 s_supported_languages_for_expressions.begin(),
631 s_supported_languages_for_expressions.end());
634 void ClangASTContext::Initialize() {
635 PluginManager::RegisterPlugin(GetPluginNameStatic(),
636 "clang base AST context plug-in",
637 CreateInstance, EnumerateSupportedLanguages);
640 void ClangASTContext::Terminate() {
641 PluginManager::UnregisterPlugin(CreateInstance);
644 void ClangASTContext::Finalize() {
645 if (m_ast_ap.get()) {
646 GetASTMap().Erase(m_ast_ap.get());
651 m_builtins_ap.reset();
652 m_selector_table_ap.reset();
653 m_identifier_table_ap.reset();
654 m_target_info_ap.reset();
655 m_target_options_rp.reset();
656 m_diagnostics_engine_ap.reset();
657 m_source_manager_ap.reset();
658 m_language_options_ap.reset();
660 m_scratch_ast_source_ap.reset();
663 void ClangASTContext::Clear() {
665 m_language_options_ap.reset();
666 m_source_manager_ap.reset();
667 m_diagnostics_engine_ap.reset();
668 m_target_options_rp.reset();
669 m_target_info_ap.reset();
670 m_identifier_table_ap.reset();
671 m_selector_table_ap.reset();
672 m_builtins_ap.reset();
673 m_pointer_byte_size = 0;
676 const char *ClangASTContext::GetTargetTriple() {
677 return m_target_triple.c_str();
680 void ClangASTContext::SetTargetTriple(const char *target_triple) {
682 m_target_triple.assign(target_triple);
685 void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
686 SetTargetTriple(arch.GetTriple().str().c_str());
689 bool ClangASTContext::HasExternalSource() {
690 ASTContext *ast = getASTContext();
692 return ast->getExternalSource() != nullptr;
696 void ClangASTContext::SetExternalSource(
697 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
698 ASTContext *ast = getASTContext();
700 ast->setExternalSource(ast_source_ap);
701 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
702 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
706 void ClangASTContext::RemoveExternalSource() {
707 ASTContext *ast = getASTContext();
710 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
711 ast->setExternalSource(empty_ast_source_ap);
712 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
713 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
717 void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
722 m_ast_ap.reset(ast_ctx);
723 GetASTMap().Insert(ast_ctx, this);
726 ASTContext *ClangASTContext::getASTContext() {
727 if (m_ast_ap.get() == nullptr) {
729 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
730 *getIdentifierTable(), *getSelectorTable(),
731 *getBuiltinContext()));
733 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
735 // This can be NULL if we don't know anything about the architecture or if
737 // target for an architecture isn't enabled in the llvm/clang that we built
738 TargetInfo *target_info = getTargetInfo();
740 m_ast_ap->InitBuiltinTypes(*target_info);
742 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
743 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
744 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
747 GetASTMap().Insert(m_ast_ap.get(), this);
749 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
750 new ClangExternalASTSourceCallbacks(
751 ClangASTContext::CompleteTagDecl,
752 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
753 ClangASTContext::LayoutRecordType, this));
754 SetExternalSource(ast_source_ap);
756 return m_ast_ap.get();
759 ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
760 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
764 Builtin::Context *ClangASTContext::getBuiltinContext() {
765 if (m_builtins_ap.get() == nullptr)
766 m_builtins_ap.reset(new Builtin::Context());
767 return m_builtins_ap.get();
770 IdentifierTable *ClangASTContext::getIdentifierTable() {
771 if (m_identifier_table_ap.get() == nullptr)
772 m_identifier_table_ap.reset(
773 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
774 return m_identifier_table_ap.get();
777 LangOptions *ClangASTContext::getLanguageOptions() {
778 if (m_language_options_ap.get() == nullptr) {
779 m_language_options_ap.reset(new LangOptions());
780 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
781 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
783 return m_language_options_ap.get();
786 SelectorTable *ClangASTContext::getSelectorTable() {
787 if (m_selector_table_ap.get() == nullptr)
788 m_selector_table_ap.reset(new SelectorTable());
789 return m_selector_table_ap.get();
792 clang::FileManager *ClangASTContext::getFileManager() {
793 if (m_file_manager_ap.get() == nullptr) {
794 clang::FileSystemOptions file_system_options;
795 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
797 return m_file_manager_ap.get();
800 clang::SourceManager *ClangASTContext::getSourceManager() {
801 if (m_source_manager_ap.get() == nullptr)
802 m_source_manager_ap.reset(
803 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
804 return m_source_manager_ap.get();
807 clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
808 if (m_diagnostics_engine_ap.get() == nullptr) {
809 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
810 m_diagnostics_engine_ap.reset(
811 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
813 return m_diagnostics_engine_ap.get();
816 clang::MangleContext *ClangASTContext::getMangleContext() {
817 if (m_mangle_ctx_ap.get() == nullptr)
818 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
819 return m_mangle_ctx_ap.get();
822 class NullDiagnosticConsumer : public DiagnosticConsumer {
824 NullDiagnosticConsumer() {
825 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
828 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
829 const clang::Diagnostic &info) {
831 llvm::SmallVector<char, 32> diag_str(10);
832 info.FormatDiagnostic(diag_str);
833 diag_str.push_back('\0');
834 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
838 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
839 return new NullDiagnosticConsumer();
846 DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
847 if (m_diagnostic_consumer_ap.get() == nullptr)
848 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
850 return m_diagnostic_consumer_ap.get();
853 std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
854 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
855 m_target_options_rp = std::make_shared<clang::TargetOptions>();
856 if (m_target_options_rp.get() != nullptr)
857 m_target_options_rp->Triple = m_target_triple;
859 return m_target_options_rp;
862 TargetInfo *ClangASTContext::getTargetInfo() {
863 // target_triple should be something like "x86_64-apple-macosx"
864 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
865 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
866 getTargetOptions()));
867 return m_target_info_ap.get();
870 #pragma mark Basic Types
872 static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
873 ASTContext *ast, QualType qual_type) {
874 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
875 if (qual_type_bit_size == bit_size)
881 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
883 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
884 getASTContext(), encoding, bit_size);
887 CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
888 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
890 return CompilerType();
892 case eEncodingInvalid:
893 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
894 return CompilerType(ast, ast->VoidPtrTy);
898 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
899 return CompilerType(ast, ast->UnsignedCharTy);
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
901 return CompilerType(ast, ast->UnsignedShortTy);
902 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
903 return CompilerType(ast, ast->UnsignedIntTy);
904 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
905 return CompilerType(ast, ast->UnsignedLongTy);
906 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
907 return CompilerType(ast, ast->UnsignedLongLongTy);
908 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
909 return CompilerType(ast, ast->UnsignedInt128Ty);
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
914 return CompilerType(ast, ast->SignedCharTy);
915 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
916 return CompilerType(ast, ast->ShortTy);
917 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
918 return CompilerType(ast, ast->IntTy);
919 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
920 return CompilerType(ast, ast->LongTy);
921 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
922 return CompilerType(ast, ast->LongLongTy);
923 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
924 return CompilerType(ast, ast->Int128Ty);
927 case eEncodingIEEE754:
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
929 return CompilerType(ast, ast->FloatTy);
930 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
931 return CompilerType(ast, ast->DoubleTy);
932 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
933 return CompilerType(ast, ast->LongDoubleTy);
934 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
935 return CompilerType(ast, ast->HalfTy);
938 case eEncodingVector:
939 // Sanity check that bit_size is a multiple of 8's.
940 if (bit_size && !(bit_size & 0x7u))
942 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
946 return CompilerType();
950 ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
952 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
953 static TypeNameToBasicTypeMap g_type_map;
954 static llvm::once_flag g_once_flag;
955 llvm::call_once(g_once_flag, []() {
957 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
960 g_type_map.Append(ConstString("char"), eBasicTypeChar);
961 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
962 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
963 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
964 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
965 g_type_map.Append(ConstString("unsigned wchar_t"),
966 eBasicTypeUnsignedWChar);
968 g_type_map.Append(ConstString("short"), eBasicTypeShort);
969 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
970 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
971 g_type_map.Append(ConstString("unsigned short int"),
972 eBasicTypeUnsignedShort);
975 g_type_map.Append(ConstString("int"), eBasicTypeInt);
976 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
977 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
978 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
981 g_type_map.Append(ConstString("long"), eBasicTypeLong);
982 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
983 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
984 g_type_map.Append(ConstString("unsigned long int"),
985 eBasicTypeUnsignedLong);
988 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
989 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
990 g_type_map.Append(ConstString("unsigned long long"),
991 eBasicTypeUnsignedLongLong);
992 g_type_map.Append(ConstString("unsigned long long int"),
993 eBasicTypeUnsignedLongLong);
996 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
997 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
1000 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1001 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1002 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1003 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1004 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1005 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1006 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
1010 return g_type_map.Find(name, eBasicTypeInvalid);
1012 return eBasicTypeInvalid;
1015 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1016 const ConstString &name) {
1018 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1019 return ClangASTContext::GetBasicType(ast, basic_type);
1021 return CompilerType();
1024 uint32_t ClangASTContext::GetPointerByteSize() {
1025 if (m_pointer_byte_size == 0)
1026 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1028 .GetByteSize(nullptr);
1029 return m_pointer_byte_size;
1032 CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1033 return GetBasicType(getASTContext(), basic_type);
1036 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1037 lldb::BasicType basic_type) {
1039 return CompilerType();
1040 lldb::opaque_compiler_type_t clang_type =
1041 GetOpaqueCompilerType(ast, basic_type);
1044 return CompilerType(GetASTContext(ast), clang_type);
1045 return CompilerType();
1048 CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1049 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1050 ASTContext *ast = getASTContext();
1052 #define streq(a, b) strcmp(a, b) == 0
1053 assert(ast != nullptr);
1059 case DW_ATE_address:
1060 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1061 return CompilerType(ast, ast->VoidPtrTy);
1064 case DW_ATE_boolean:
1065 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1066 return CompilerType(ast, ast->BoolTy);
1067 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1068 return CompilerType(ast, ast->UnsignedCharTy);
1069 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1070 return CompilerType(ast, ast->UnsignedShortTy);
1071 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1072 return CompilerType(ast, ast->UnsignedIntTy);
1075 case DW_ATE_lo_user:
1076 // This has been seen to mean DW_AT_complex_integer
1078 if (::strstr(type_name, "complex")) {
1079 CompilerType complex_int_clang_type =
1080 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1082 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1083 complex_int_clang_type)));
1088 case DW_ATE_complex_float:
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1090 return CompilerType(ast, ast->FloatComplexTy);
1091 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1092 return CompilerType(ast, ast->DoubleComplexTy);
1093 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1094 return CompilerType(ast, ast->LongDoubleComplexTy);
1096 CompilerType complex_float_clang_type =
1097 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1099 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1100 complex_float_clang_type)));
1105 if (streq(type_name, "float") &&
1106 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1107 return CompilerType(ast, ast->FloatTy);
1108 if (streq(type_name, "double") &&
1109 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1110 return CompilerType(ast, ast->DoubleTy);
1111 if (streq(type_name, "long double") &&
1112 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1113 return CompilerType(ast, ast->LongDoubleTy);
1114 // Fall back to not requiring a name match
1115 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1116 return CompilerType(ast, ast->FloatTy);
1117 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1118 return CompilerType(ast, ast->DoubleTy);
1119 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1120 return CompilerType(ast, ast->LongDoubleTy);
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1122 return CompilerType(ast, ast->HalfTy);
1127 if (streq(type_name, "wchar_t") &&
1128 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1130 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1131 return CompilerType(ast, ast->WCharTy);
1132 if (streq(type_name, "void") &&
1133 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1134 return CompilerType(ast, ast->VoidTy);
1135 if (strstr(type_name, "long long") &&
1136 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1137 return CompilerType(ast, ast->LongLongTy);
1138 if (strstr(type_name, "long") &&
1139 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1140 return CompilerType(ast, ast->LongTy);
1141 if (strstr(type_name, "short") &&
1142 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1143 return CompilerType(ast, ast->ShortTy);
1144 if (strstr(type_name, "char")) {
1145 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1146 return CompilerType(ast, ast->CharTy);
1147 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1148 return CompilerType(ast, ast->SignedCharTy);
1150 if (strstr(type_name, "int")) {
1151 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1152 return CompilerType(ast, ast->IntTy);
1153 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1154 return CompilerType(ast, ast->Int128Ty);
1157 // We weren't able to match up a type name, just search by size
1158 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1159 return CompilerType(ast, ast->CharTy);
1160 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1161 return CompilerType(ast, ast->ShortTy);
1162 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1163 return CompilerType(ast, ast->IntTy);
1164 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1165 return CompilerType(ast, ast->LongTy);
1166 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1167 return CompilerType(ast, ast->LongLongTy);
1168 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1169 return CompilerType(ast, ast->Int128Ty);
1172 case DW_ATE_signed_char:
1173 if (ast->getLangOpts().CharIsSigned && type_name &&
1174 streq(type_name, "char")) {
1175 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1176 return CompilerType(ast, ast->CharTy);
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1179 return CompilerType(ast, ast->SignedCharTy);
1182 case DW_ATE_unsigned:
1184 if (streq(type_name, "wchar_t")) {
1185 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1186 if (!(getTargetInfo() &&
1187 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1188 return CompilerType(ast, ast->WCharTy);
1191 if (strstr(type_name, "long long")) {
1192 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1193 return CompilerType(ast, ast->UnsignedLongLongTy);
1194 } else if (strstr(type_name, "long")) {
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1196 return CompilerType(ast, ast->UnsignedLongTy);
1197 } else if (strstr(type_name, "short")) {
1198 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1199 return CompilerType(ast, ast->UnsignedShortTy);
1200 } else if (strstr(type_name, "char")) {
1201 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1202 return CompilerType(ast, ast->UnsignedCharTy);
1203 } else if (strstr(type_name, "int")) {
1204 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1205 return CompilerType(ast, ast->UnsignedIntTy);
1206 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1207 return CompilerType(ast, ast->UnsignedInt128Ty);
1210 // We weren't able to match up a type name, just search by size
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1212 return CompilerType(ast, ast->UnsignedCharTy);
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1214 return CompilerType(ast, ast->UnsignedShortTy);
1215 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1216 return CompilerType(ast, ast->UnsignedIntTy);
1217 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1218 return CompilerType(ast, ast->UnsignedLongTy);
1219 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1220 return CompilerType(ast, ast->UnsignedLongLongTy);
1221 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1222 return CompilerType(ast, ast->UnsignedInt128Ty);
1225 case DW_ATE_unsigned_char:
1226 if (!ast->getLangOpts().CharIsSigned && type_name &&
1227 streq(type_name, "char")) {
1228 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1229 return CompilerType(ast, ast->CharTy);
1231 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1232 return CompilerType(ast, ast->UnsignedCharTy);
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1234 return CompilerType(ast, ast->UnsignedShortTy);
1237 case DW_ATE_imaginary_float:
1242 if (streq(type_name, "char16_t")) {
1243 return CompilerType(ast, ast->Char16Ty);
1244 } else if (streq(type_name, "char32_t")) {
1245 return CompilerType(ast, ast->Char32Ty);
1251 // This assert should fire for anything that we don't catch above so we know
1252 // to fix any issues we run into.
1254 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1255 "DW_TAG_base_type '%s' encoded with "
1256 "DW_ATE = 0x%x, bit_size = %u\n",
1257 type_name, dw_ate, bit_size);
1259 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1260 "DW_TAG_base_type encoded with "
1261 "DW_ATE = 0x%x, bit_size = %u\n",
1264 return CompilerType();
1267 CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1269 return CompilerType(ast, ast->UnknownAnyTy);
1270 return CompilerType();
1273 CompilerType ClangASTContext::GetCStringType(bool is_const) {
1274 ASTContext *ast = getASTContext();
1275 QualType char_type(ast->CharTy);
1278 char_type.addConst();
1280 return CompilerType(ast, ast->getPointerType(char_type));
1283 clang::DeclContext *
1284 ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1285 return ast->getTranslationUnitDecl();
1288 clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1289 clang::Decl *source_decl) {
1290 FileSystemOptions file_system_options;
1291 FileManager file_manager(file_system_options);
1292 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1294 return importer.Import(source_decl);
1297 bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1298 bool ignore_qualifiers) {
1299 ClangASTContext *ast =
1300 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1301 if (!ast || ast != type2.GetTypeSystem())
1304 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1307 QualType type1_qual = ClangUtil::GetQualType(type1);
1308 QualType type2_qual = ClangUtil::GetQualType(type2);
1310 if (ignore_qualifiers) {
1311 type1_qual = type1_qual.getUnqualifiedType();
1312 type2_qual = type2_qual.getUnqualifiedType();
1315 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1318 CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1319 if (clang::ObjCInterfaceDecl *interface_decl =
1320 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1321 return GetTypeForDecl(interface_decl);
1322 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1323 return GetTypeForDecl(tag_decl);
1324 return CompilerType();
1327 CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1328 // No need to call the getASTContext() accessor (which can create the AST
1329 // if it isn't created yet, because we can't have created a decl in this
1330 // AST if our AST didn't already exist...
1331 ASTContext *ast = &decl->getASTContext();
1333 return CompilerType(ast, ast->getTagDeclType(decl));
1334 return CompilerType();
1337 CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1338 // No need to call the getASTContext() accessor (which can create the AST
1339 // if it isn't created yet, because we can't have created a decl in this
1340 // AST if our AST didn't already exist...
1341 ASTContext *ast = &decl->getASTContext();
1343 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1344 return CompilerType();
1347 #pragma mark Structure, Unions, Classes
1349 CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1350 AccessType access_type,
1351 const char *name, int kind,
1352 LanguageType language,
1353 ClangASTMetadata *metadata) {
1354 ASTContext *ast = getASTContext();
1355 assert(ast != nullptr);
1357 if (decl_ctx == nullptr)
1358 decl_ctx = ast->getTranslationUnitDecl();
1360 if (language == eLanguageTypeObjC ||
1361 language == eLanguageTypeObjC_plus_plus) {
1362 bool isForwardDecl = true;
1363 bool isInternal = false;
1364 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1367 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1368 // we will need to update this code. I was told to currently always use
1369 // the CXXRecordDecl class since we often don't know from debug information
1370 // if something is struct or a class, so we default to always use the more
1371 // complete definition just in case.
1373 bool is_anonymous = (!name) || (!name[0]);
1375 CXXRecordDecl *decl = CXXRecordDecl::Create(
1376 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1377 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1380 decl->setAnonymousStructOrUnion(true);
1384 SetMetadata(ast, decl, *metadata);
1386 if (access_type != eAccessNone)
1387 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1390 decl_ctx->addDecl(decl);
1392 return CompilerType(ast, ast->getTagDeclType(decl));
1394 return CompilerType();
1397 static TemplateParameterList *CreateTemplateParameterList(
1399 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1400 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1401 const bool parameter_pack = false;
1402 const bool is_typename = false;
1403 const unsigned depth = 0;
1404 const size_t num_template_params = template_param_infos.GetSize();
1405 for (size_t i = 0; i < num_template_params; ++i) {
1406 const char *name = template_param_infos.names[i];
1408 IdentifierInfo *identifier_info = nullptr;
1409 if (name && name[0])
1410 identifier_info = &ast->Idents.get(name);
1411 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) {
1412 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1414 ast->getTranslationUnitDecl(), // Is this the right decl context?,
1415 // SourceLocation StartLoc,
1416 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1417 template_param_infos.args[i].getIntegralType(), parameter_pack,
1421 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1423 ast->getTranslationUnitDecl(), // Is this the right decl context?
1424 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1425 is_typename, parameter_pack));
1429 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1430 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1431 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1432 SourceLocation(), requires_clause);
1433 return template_param_list;
1436 clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1437 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1438 const char *name, const TemplateParameterInfos &template_param_infos) {
1439 // /// \brief Create a function template node.
1440 ASTContext *ast = getASTContext();
1442 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1444 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1445 ast, template_param_infos, template_param_decls);
1446 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1447 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1448 template_param_list, func_decl);
1450 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1451 i < template_param_decl_count; ++i) {
1452 // TODO: verify which decl context we should put template_param_decls into..
1453 template_param_decls[i]->setDeclContext(func_decl);
1456 return func_tmpl_decl;
1459 void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1460 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1461 const TemplateParameterInfos &infos) {
1462 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1464 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1468 ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1469 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1470 int kind, const TemplateParameterInfos &template_param_infos) {
1471 ASTContext *ast = getASTContext();
1473 ClassTemplateDecl *class_template_decl = nullptr;
1474 if (decl_ctx == nullptr)
1475 decl_ctx = ast->getTranslationUnitDecl();
1477 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1478 DeclarationName decl_name(&identifier_info);
1480 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1482 for (NamedDecl *decl : result) {
1483 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1484 if (class_template_decl)
1485 return class_template_decl;
1488 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1490 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1491 ast, template_param_infos, template_param_decls);
1493 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1494 *ast, (TagDecl::TagKind)kind,
1495 decl_ctx, // What decl context do we use here? TU? The actual decl
1497 SourceLocation(), SourceLocation(), &identifier_info);
1499 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1500 i < template_param_decl_count; ++i) {
1501 template_param_decls[i]->setDeclContext(template_cxx_decl);
1504 // With templated classes, we say that a class is templated with
1505 // specializations, but that the bare class has no functions.
1506 // template_cxx_decl->startDefinition();
1507 // template_cxx_decl->completeDefinition();
1509 class_template_decl = ClassTemplateDecl::Create(
1511 decl_ctx, // What decl context do we use here? TU? The actual decl
1513 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
1515 if (class_template_decl) {
1516 if (access_type != eAccessNone)
1517 class_template_decl->setAccess(
1518 ConvertAccessTypeToAccessSpecifier(access_type));
1520 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1521 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1523 decl_ctx->addDecl(class_template_decl);
1525 #ifdef LLDB_CONFIGURATION_DEBUG
1526 VerifyDecl(class_template_decl);
1530 return class_template_decl;
1533 ClassTemplateSpecializationDecl *
1534 ClangASTContext::CreateClassTemplateSpecializationDecl(
1535 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1536 const TemplateParameterInfos &template_param_infos) {
1537 ASTContext *ast = getASTContext();
1538 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1539 ClassTemplateSpecializationDecl::Create(
1540 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1541 SourceLocation(), class_template_decl, template_param_infos.args,
1544 class_template_specialization_decl->setSpecializationKind(
1545 TSK_ExplicitSpecialization);
1547 return class_template_specialization_decl;
1550 CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1551 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1552 if (class_template_specialization_decl) {
1553 ASTContext *ast = getASTContext();
1555 return CompilerType(
1556 ast, ast->getTagDeclType(class_template_specialization_decl));
1558 return CompilerType();
1561 static inline bool check_op_param(bool is_method,
1562 clang::OverloadedOperatorKind op_kind,
1563 bool unary, bool binary,
1564 uint32_t num_params) {
1565 // Special-case call since it can take any number of operands
1566 if (op_kind == OO_Call)
1569 // The parameter count doesn't include "this"
1572 if (num_params == 1)
1574 if (num_params == 2)
1580 bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1581 bool is_method, clang::OverloadedOperatorKind op_kind,
1582 uint32_t num_params) {
1586 // C++ standard allows any number of arguments to new/delete
1590 case OO_Array_Delete:
1594 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1596 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1598 #include "clang/Basic/OperatorKinds.def"
1605 clang::AccessSpecifier
1606 ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1607 clang::AccessSpecifier rhs) {
1608 // Make the access equal to the stricter of the field and the nested field's
1610 if (lhs == AS_none || rhs == AS_none)
1612 if (lhs == AS_private || rhs == AS_private)
1614 if (lhs == AS_protected || rhs == AS_protected)
1615 return AS_protected;
1619 bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1620 uint32_t &bitfield_bit_size) {
1621 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1624 bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1625 uint32_t &bitfield_bit_size) {
1626 if (ast == nullptr || field == nullptr)
1629 if (field->isBitField()) {
1630 Expr *bit_width_expr = field->getBitWidth();
1631 if (bit_width_expr) {
1632 llvm::APSInt bit_width_apsint;
1633 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1634 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1642 bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1643 if (record_decl == nullptr)
1646 if (!record_decl->field_empty())
1649 // No fields, lets check this is a CXX record and check the base classes
1650 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1651 if (cxx_record_decl) {
1652 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1653 for (base_class = cxx_record_decl->bases_begin(),
1654 base_class_end = cxx_record_decl->bases_end();
1655 base_class != base_class_end; ++base_class) {
1656 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1657 base_class->getType()->getAs<RecordType>()->getDecl());
1658 if (RecordHasFields(base_class_decl))
1665 #pragma mark Objective C Classes
1667 CompilerType ClangASTContext::CreateObjCClass(const char *name,
1668 DeclContext *decl_ctx,
1671 ClangASTMetadata *metadata) {
1672 ASTContext *ast = getASTContext();
1673 assert(ast != nullptr);
1674 assert(name && name[0]);
1675 if (decl_ctx == nullptr)
1676 decl_ctx = ast->getTranslationUnitDecl();
1678 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1679 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1680 nullptr, SourceLocation(),
1684 if (decl && metadata)
1685 SetMetadata(ast, decl, *metadata);
1687 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1690 static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1691 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1696 ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1697 bool omit_empty_base_classes) {
1698 uint32_t num_bases = 0;
1699 if (cxx_record_decl) {
1700 if (omit_empty_base_classes) {
1701 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1702 for (base_class = cxx_record_decl->bases_begin(),
1703 base_class_end = cxx_record_decl->bases_end();
1704 base_class != base_class_end; ++base_class) {
1705 // Skip empty base classes
1706 if (omit_empty_base_classes) {
1707 if (BaseSpecifierIsEmpty(base_class))
1713 num_bases = cxx_record_decl->getNumBases();
1718 #pragma mark Namespace Declarations
1721 ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1722 DeclContext *decl_ctx) {
1723 NamespaceDecl *namespace_decl = nullptr;
1724 ASTContext *ast = getASTContext();
1725 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1726 if (decl_ctx == nullptr)
1727 decl_ctx = translation_unit_decl;
1730 IdentifierInfo &identifier_info = ast->Idents.get(name);
1731 DeclarationName decl_name(&identifier_info);
1732 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1733 for (NamedDecl *decl : result) {
1734 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1736 return namespace_decl;
1740 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1741 SourceLocation(), &identifier_info, nullptr);
1743 decl_ctx->addDecl(namespace_decl);
1745 if (decl_ctx == translation_unit_decl) {
1746 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1748 return namespace_decl;
1751 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1752 SourceLocation(), nullptr, nullptr);
1753 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1754 translation_unit_decl->addDecl(namespace_decl);
1755 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1757 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1758 if (parent_namespace_decl) {
1759 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1761 return namespace_decl;
1763 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1764 SourceLocation(), nullptr, nullptr);
1765 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1766 parent_namespace_decl->addDecl(namespace_decl);
1767 assert(namespace_decl ==
1768 parent_namespace_decl->getAnonymousNamespace());
1774 #ifdef LLDB_CONFIGURATION_DEBUG
1775 VerifyDecl(namespace_decl);
1777 return namespace_decl;
1780 NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1781 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1782 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1783 if (ast_ctx == nullptr)
1786 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1790 ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1791 if (ctx != nullptr) {
1792 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1793 clang::SourceLocation());
1800 clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1801 clang::DeclContext *right,
1802 clang::DeclContext *root) {
1803 if (root == nullptr)
1806 std::set<clang::DeclContext *> path_left;
1807 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1808 path_left.insert(d);
1810 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1811 if (path_left.find(d) != path_left.end())
1817 clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1818 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1819 if (decl_ctx != nullptr && ns_decl != nullptr) {
1820 clang::TranslationUnitDecl *translation_unit =
1821 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1822 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1823 *getASTContext(), decl_ctx, clang::SourceLocation(),
1824 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1825 clang::SourceLocation(), ns_decl,
1826 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1827 decl_ctx->addDecl(using_decl);
1834 ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1835 clang::NamedDecl *target) {
1836 if (current_decl_ctx != nullptr && target != nullptr) {
1837 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1838 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1839 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1840 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1841 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1843 using_decl->addShadowDecl(shadow_decl);
1844 current_decl_ctx->addDecl(using_decl);
1850 clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1851 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1852 if (decl_context != nullptr) {
1853 clang::VarDecl *var_decl = clang::VarDecl::Create(
1854 *getASTContext(), decl_context, clang::SourceLocation(),
1855 clang::SourceLocation(),
1856 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1857 nullptr, clang::SC_None);
1858 var_decl->setAccess(clang::AS_public);
1859 decl_context->addDecl(var_decl);
1865 lldb::opaque_compiler_type_t
1866 ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1867 lldb::BasicType basic_type) {
1868 switch (basic_type) {
1869 case eBasicTypeVoid:
1870 return ast->VoidTy.getAsOpaquePtr();
1871 case eBasicTypeChar:
1872 return ast->CharTy.getAsOpaquePtr();
1873 case eBasicTypeSignedChar:
1874 return ast->SignedCharTy.getAsOpaquePtr();
1875 case eBasicTypeUnsignedChar:
1876 return ast->UnsignedCharTy.getAsOpaquePtr();
1877 case eBasicTypeWChar:
1878 return ast->getWCharType().getAsOpaquePtr();
1879 case eBasicTypeSignedWChar:
1880 return ast->getSignedWCharType().getAsOpaquePtr();
1881 case eBasicTypeUnsignedWChar:
1882 return ast->getUnsignedWCharType().getAsOpaquePtr();
1883 case eBasicTypeChar16:
1884 return ast->Char16Ty.getAsOpaquePtr();
1885 case eBasicTypeChar32:
1886 return ast->Char32Ty.getAsOpaquePtr();
1887 case eBasicTypeShort:
1888 return ast->ShortTy.getAsOpaquePtr();
1889 case eBasicTypeUnsignedShort:
1890 return ast->UnsignedShortTy.getAsOpaquePtr();
1892 return ast->IntTy.getAsOpaquePtr();
1893 case eBasicTypeUnsignedInt:
1894 return ast->UnsignedIntTy.getAsOpaquePtr();
1895 case eBasicTypeLong:
1896 return ast->LongTy.getAsOpaquePtr();
1897 case eBasicTypeUnsignedLong:
1898 return ast->UnsignedLongTy.getAsOpaquePtr();
1899 case eBasicTypeLongLong:
1900 return ast->LongLongTy.getAsOpaquePtr();
1901 case eBasicTypeUnsignedLongLong:
1902 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1903 case eBasicTypeInt128:
1904 return ast->Int128Ty.getAsOpaquePtr();
1905 case eBasicTypeUnsignedInt128:
1906 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1907 case eBasicTypeBool:
1908 return ast->BoolTy.getAsOpaquePtr();
1909 case eBasicTypeHalf:
1910 return ast->HalfTy.getAsOpaquePtr();
1911 case eBasicTypeFloat:
1912 return ast->FloatTy.getAsOpaquePtr();
1913 case eBasicTypeDouble:
1914 return ast->DoubleTy.getAsOpaquePtr();
1915 case eBasicTypeLongDouble:
1916 return ast->LongDoubleTy.getAsOpaquePtr();
1917 case eBasicTypeFloatComplex:
1918 return ast->FloatComplexTy.getAsOpaquePtr();
1919 case eBasicTypeDoubleComplex:
1920 return ast->DoubleComplexTy.getAsOpaquePtr();
1921 case eBasicTypeLongDoubleComplex:
1922 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1923 case eBasicTypeObjCID:
1924 return ast->getObjCIdType().getAsOpaquePtr();
1925 case eBasicTypeObjCClass:
1926 return ast->getObjCClassType().getAsOpaquePtr();
1927 case eBasicTypeObjCSel:
1928 return ast->getObjCSelType().getAsOpaquePtr();
1929 case eBasicTypeNullPtr:
1930 return ast->NullPtrTy.getAsOpaquePtr();
1936 #pragma mark Function Types
1938 clang::DeclarationName
1939 ClangASTContext::GetDeclarationName(const char *name,
1940 const CompilerType &function_clang_type) {
1941 if (!name || !name[0])
1942 return clang::DeclarationName();
1944 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1945 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1946 return DeclarationName(&getASTContext()->Idents.get(
1947 name)); // Not operator, but a regular function.
1949 // Check the number of operator parameters. Sometimes we have
1950 // seen bad DWARF that doesn't correctly describe operators and
1951 // if we try to create a method and add it to the class, clang
1952 // will assert and crash, so we need to make sure things are
1954 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1955 const clang::FunctionProtoType *function_type =
1956 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1957 if (function_type == nullptr)
1958 return clang::DeclarationName();
1960 const bool is_method = false;
1961 const unsigned int num_params = function_type->getNumParams();
1962 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1963 is_method, op_kind, num_params))
1964 return clang::DeclarationName();
1966 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
1969 FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1970 DeclContext *decl_ctx, const char *name,
1971 const CompilerType &function_clang_type, int storage, bool is_inline) {
1972 FunctionDecl *func_decl = nullptr;
1973 ASTContext *ast = getASTContext();
1974 if (decl_ctx == nullptr)
1975 decl_ctx = ast->getTranslationUnitDecl();
1977 const bool hasWrittenPrototype = true;
1978 const bool isConstexprSpecified = false;
1980 clang::DeclarationName declarationName =
1981 GetDeclarationName(name, function_clang_type);
1982 func_decl = FunctionDecl::Create(
1983 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1984 ClangUtil::GetQualType(function_clang_type), nullptr,
1985 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1986 isConstexprSpecified);
1988 decl_ctx->addDecl(func_decl);
1990 #ifdef LLDB_CONFIGURATION_DEBUG
1991 VerifyDecl(func_decl);
1997 CompilerType ClangASTContext::CreateFunctionType(
1998 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1999 unsigned num_args, bool is_variadic, unsigned type_quals) {
2001 return CompilerType(); // invalid AST
2003 if (!result_type || !ClangUtil::IsClangType(result_type))
2004 return CompilerType(); // invalid return type
2006 std::vector<QualType> qual_type_args;
2007 if (num_args > 0 && args == nullptr)
2008 return CompilerType(); // invalid argument array passed in
2010 // Verify that all arguments are valid and the right type
2011 for (unsigned i = 0; i < num_args; ++i) {
2013 // Make sure we have a clang type in args[i] and not a type from another
2014 // language whose name might match
2015 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2016 lldbassert(is_clang_type);
2018 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2020 return CompilerType(); // invalid argument type (must be a clang type)
2022 return CompilerType(); // invalid argument type (empty)
2025 // TODO: Detect calling convention in DWARF?
2026 FunctionProtoType::ExtProtoInfo proto_info;
2027 proto_info.Variadic = is_variadic;
2028 proto_info.ExceptionSpec = EST_None;
2029 proto_info.TypeQuals = type_quals;
2030 proto_info.RefQualifier = RQ_None;
2032 return CompilerType(ast,
2033 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2034 qual_type_args, proto_info));
2037 ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2038 const char *name, const CompilerType ¶m_type, int storage) {
2039 ASTContext *ast = getASTContext();
2040 assert(ast != nullptr);
2041 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2042 SourceLocation(), SourceLocation(),
2043 name && name[0] ? &ast->Idents.get(name) : nullptr,
2044 ClangUtil::GetQualType(param_type), nullptr,
2045 (clang::StorageClass)storage, nullptr);
2048 void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2049 ParmVarDecl **params,
2050 unsigned num_params) {
2052 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2056 ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2057 QualType block_type = m_ast_ap->getBlockPointerType(
2058 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2060 return CompilerType(this, block_type.getAsOpaquePtr());
2063 #pragma mark Array Types
2065 CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2066 size_t element_count,
2068 if (element_type.IsValid()) {
2069 ASTContext *ast = getASTContext();
2070 assert(ast != nullptr);
2073 return CompilerType(
2074 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2078 llvm::APInt ap_element_count(64, element_count);
2079 if (element_count == 0) {
2080 return CompilerType(ast, ast->getIncompleteArrayType(
2081 ClangUtil::GetQualType(element_type),
2082 clang::ArrayType::Normal, 0));
2084 return CompilerType(
2085 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2087 clang::ArrayType::Normal, 0));
2091 return CompilerType();
2094 CompilerType ClangASTContext::CreateStructForIdentifier(
2095 const ConstString &type_name,
2096 const std::initializer_list<std::pair<const char *, CompilerType>>
2100 if (!type_name.IsEmpty() &&
2101 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2103 lldbassert(0 && "Trying to create a type for an existing name");
2107 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2108 clang::TTK_Struct, lldb::eLanguageTypeC);
2109 StartTagDeclarationDefinition(type);
2110 for (const auto &field : type_fields)
2111 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2115 CompleteTagDeclarationDefinition(type);
2119 CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2120 const ConstString &type_name,
2121 const std::initializer_list<std::pair<const char *, CompilerType>>
2125 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2128 return CreateStructForIdentifier(type_name, type_fields, packed);
2131 #pragma mark Enumeration Types
2134 ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2135 const Declaration &decl,
2136 const CompilerType &integer_clang_type) {
2137 // TODO: Do something intelligent with the Declaration object passed in
2138 // like maybe filling in the SourceLocation with it...
2139 ASTContext *ast = getASTContext();
2141 // TODO: ask about these...
2142 // const bool IsScoped = false;
2143 // const bool IsFixed = false;
2145 EnumDecl *enum_decl = EnumDecl::Create(
2146 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2147 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2149 false, // IsScopedUsingClassTag
2153 // TODO: check if we should be setting the promotion type too?
2154 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2156 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2158 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2160 return CompilerType();
2163 // Disable this for now since I can't seem to get a nicely formatted float
2164 // out of the APFloat class without just getting the float, double or quad
2165 // and then using a formatted print on it which defeats the purpose. We ideally
2166 // would like to get perfect string values for any kind of float semantics
2167 // so we can support remote targets. The code below also requires a patch to
2170 // ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2171 // lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2172 // byte_size, int apint_byte_order, std::string &float_str)
2174 // uint32_t count = 0;
2175 // bool is_complex = false;
2176 // if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2178 // unsigned num_bytes_per_float = byte_size / count;
2179 // unsigned num_bits_per_float = num_bytes_per_float * 8;
2181 // float_str.clear();
2183 // for (i=0; i<count; i++)
2185 // APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2186 // (APInt::ByteOrder)apint_byte_order);
2187 // bool is_ieee = false;
2188 // APFloat ap_float(ap_int, is_ieee);
2190 // unsigned int hex_digits = 0;
2191 // bool upper_case = false;
2193 // if (ap_float.convertToHexString(s, hex_digits, upper_case,
2194 // APFloat::rmNearestTiesToEven) > 0)
2197 // float_str.append(", ");
2198 // float_str.append(s);
2199 // if (i == 1 && is_complex)
2200 // float_str.append(1, 'i');
2203 // return !float_str.empty();
2208 CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2213 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2214 return CompilerType(ast, ast->SignedCharTy);
2216 if (bit_size == ast->getTypeSize(ast->ShortTy))
2217 return CompilerType(ast, ast->ShortTy);
2219 if (bit_size == ast->getTypeSize(ast->IntTy))
2220 return CompilerType(ast, ast->IntTy);
2222 if (bit_size == ast->getTypeSize(ast->LongTy))
2223 return CompilerType(ast, ast->LongTy);
2225 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2226 return CompilerType(ast, ast->LongLongTy);
2228 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2229 return CompilerType(ast, ast->Int128Ty);
2231 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2232 return CompilerType(ast, ast->UnsignedCharTy);
2234 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2235 return CompilerType(ast, ast->UnsignedShortTy);
2237 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2238 return CompilerType(ast, ast->UnsignedIntTy);
2240 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2241 return CompilerType(ast, ast->UnsignedLongTy);
2243 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2244 return CompilerType(ast, ast->UnsignedLongLongTy);
2246 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2247 return CompilerType(ast, ast->UnsignedInt128Ty);
2250 return CompilerType();
2253 CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2256 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2258 return CompilerType();
2261 void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2263 DumpDeclContextHiearchy(decl_ctx->getParent());
2265 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2267 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2268 named_decl->getDeclName().getAsString().c_str());
2270 printf("%20s\n", decl_ctx->getDeclKindName());
2275 void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2276 if (decl == nullptr)
2278 DumpDeclContextHiearchy(decl->getDeclContext());
2280 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2282 printf("%20s: %s%s\n", decl->getDeclKindName(),
2283 record_decl->getDeclName().getAsString().c_str(),
2284 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2287 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2289 printf("%20s: %s\n", decl->getDeclKindName(),
2290 named_decl->getDeclName().getAsString().c_str());
2292 printf("%20s\n", decl->getDeclKindName());
2297 bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2298 clang::Decl *rhs_decl) {
2299 if (lhs_decl && rhs_decl) {
2300 //----------------------------------------------------------------------
2301 // Make sure the decl kinds match first
2302 //----------------------------------------------------------------------
2303 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2304 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2306 if (lhs_decl_kind == rhs_decl_kind) {
2307 //------------------------------------------------------------------
2308 // Now check that the decl contexts kinds are all equivalent
2309 // before we have to check any names of the decl contexts...
2310 //------------------------------------------------------------------
2311 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2312 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2313 if (lhs_decl_ctx && rhs_decl_ctx) {
2315 if (lhs_decl_ctx && rhs_decl_ctx) {
2316 const clang::Decl::Kind lhs_decl_ctx_kind =
2317 lhs_decl_ctx->getDeclKind();
2318 const clang::Decl::Kind rhs_decl_ctx_kind =
2319 rhs_decl_ctx->getDeclKind();
2320 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2321 lhs_decl_ctx = lhs_decl_ctx->getParent();
2322 rhs_decl_ctx = rhs_decl_ctx->getParent();
2324 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2332 //--------------------------------------------------------------
2333 // Now make sure the name of the decls match
2334 //--------------------------------------------------------------
2335 clang::NamedDecl *lhs_named_decl =
2336 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2337 clang::NamedDecl *rhs_named_decl =
2338 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2339 if (lhs_named_decl && rhs_named_decl) {
2340 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2341 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2342 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2343 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2350 //--------------------------------------------------------------
2351 // We know that the decl context kinds all match, so now we need
2352 // to make sure the names match as well
2353 //--------------------------------------------------------------
2354 lhs_decl_ctx = lhs_decl->getDeclContext();
2355 rhs_decl_ctx = rhs_decl->getDeclContext();
2357 switch (lhs_decl_ctx->getDeclKind()) {
2358 case clang::Decl::TranslationUnit:
2359 // We don't care about the translation unit names
2362 clang::NamedDecl *lhs_named_decl =
2363 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2364 clang::NamedDecl *rhs_named_decl =
2365 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2366 if (lhs_named_decl && rhs_named_decl) {
2367 clang::DeclarationName lhs_decl_name =
2368 lhs_named_decl->getDeclName();
2369 clang::DeclarationName rhs_decl_name =
2370 rhs_named_decl->getDeclName();
2371 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2372 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2380 lhs_decl_ctx = lhs_decl_ctx->getParent();
2381 rhs_decl_ctx = rhs_decl_ctx->getParent();
2388 bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2389 clang::Decl *decl) {
2393 ExternalASTSource *ast_source = ast->getExternalSource();
2398 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2399 if (tag_decl->isCompleteDefinition())
2402 if (!tag_decl->hasExternalLexicalStorage())
2405 ast_source->CompleteType(tag_decl);
2407 return !tag_decl->getTypeForDecl()->isIncompleteType();
2408 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2409 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2410 if (objc_interface_decl->getDefinition())
2413 if (!objc_interface_decl->hasExternalLexicalStorage())
2416 ast_source->CompleteType(objc_interface_decl);
2418 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2424 void ClangASTContext::SetMetadataAsUserID(const void *object,
2425 user_id_t user_id) {
2426 ClangASTMetadata meta_data;
2427 meta_data.SetUserID(user_id);
2428 SetMetadata(object, meta_data);
2431 void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2432 ClangASTMetadata &metadata) {
2433 ClangExternalASTSourceCommon *external_source =
2434 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2436 if (external_source)
2437 external_source->SetMetadata(object, metadata);
2440 ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2441 const void *object) {
2442 ClangExternalASTSourceCommon *external_source =
2443 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2445 if (external_source && external_source->HasMetadata(object))
2446 return external_source->GetMetadata(object);
2451 clang::DeclContext *
2452 ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2453 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2456 clang::DeclContext *
2457 ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2458 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2461 bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2463 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2465 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2467 clang::TagDecl *tag_decl =
2468 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2470 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2478 bool ClangASTContext::SetDefaultAccessForRecordFields(
2479 clang::RecordDecl *record_decl, int default_accessibility,
2480 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2483 clang::RecordDecl::field_iterator field, field_end;
2484 for (field = record_decl->field_begin(),
2485 field_end = record_decl->field_end(), field_idx = 0;
2486 field != field_end; ++field, ++field_idx) {
2487 // If no accessibility was assigned, assign the correct one
2488 if (field_idx < num_assigned_accessibilities &&
2489 assigned_accessibilities[field_idx] == clang::AS_none)
2490 field->setAccess((clang::AccessSpecifier)default_accessibility);
2497 clang::DeclContext *
2498 ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2499 return GetDeclContextForType(ClangUtil::GetQualType(type));
2502 clang::DeclContext *
2503 ClangASTContext::GetDeclContextForType(clang::QualType type) {
2507 clang::QualType qual_type = type.getCanonicalType();
2508 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2509 switch (type_class) {
2510 case clang::Type::ObjCInterface:
2511 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2513 case clang::Type::ObjCObjectPointer:
2514 return GetDeclContextForType(
2515 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2516 ->getPointeeType());
2517 case clang::Type::Record:
2518 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2519 case clang::Type::Enum:
2520 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2521 case clang::Type::Typedef:
2522 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2524 ->getUnderlyingType());
2525 case clang::Type::Auto:
2526 return GetDeclContextForType(
2527 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2528 case clang::Type::Elaborated:
2529 return GetDeclContextForType(
2530 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2531 case clang::Type::Paren:
2532 return GetDeclContextForType(
2533 llvm::cast<clang::ParenType>(qual_type)->desugar());
2537 // No DeclContext in this type...
2541 static bool GetCompleteQualType(clang::ASTContext *ast,
2542 clang::QualType qual_type,
2543 bool allow_completion = true) {
2544 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2545 switch (type_class) {
2546 case clang::Type::ConstantArray:
2547 case clang::Type::IncompleteArray:
2548 case clang::Type::VariableArray: {
2549 const clang::ArrayType *array_type =
2550 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2553 return GetCompleteQualType(ast, array_type->getElementType(),
2556 case clang::Type::Record: {
2557 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2558 if (cxx_record_decl) {
2559 if (cxx_record_decl->hasExternalLexicalStorage()) {
2560 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2561 const bool fields_loaded =
2562 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2563 if (is_complete && fields_loaded)
2566 if (!allow_completion)
2569 // Call the field_begin() accessor to for it to use the external source
2570 // to load the fields...
2571 clang::ExternalASTSource *external_ast_source =
2572 ast->getExternalSource();
2573 if (external_ast_source) {
2574 external_ast_source->CompleteType(cxx_record_decl);
2575 if (cxx_record_decl->isCompleteDefinition()) {
2576 cxx_record_decl->field_begin();
2577 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2582 const clang::TagType *tag_type =
2583 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2584 return !tag_type->isIncompleteType();
2587 case clang::Type::Enum: {
2588 const clang::TagType *tag_type =
2589 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2591 clang::TagDecl *tag_decl = tag_type->getDecl();
2593 if (tag_decl->getDefinition())
2596 if (!allow_completion)
2599 if (tag_decl->hasExternalLexicalStorage()) {
2601 clang::ExternalASTSource *external_ast_source =
2602 ast->getExternalSource();
2603 if (external_ast_source) {
2604 external_ast_source->CompleteType(tag_decl);
2605 return !tag_type->isIncompleteType();
2614 case clang::Type::ObjCObject:
2615 case clang::Type::ObjCInterface: {
2616 const clang::ObjCObjectType *objc_class_type =
2617 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2618 if (objc_class_type) {
2619 clang::ObjCInterfaceDecl *class_interface_decl =
2620 objc_class_type->getInterface();
2621 // We currently can't complete objective C types through the newly added
2623 // because it only supports TagDecl objects right now...
2624 if (class_interface_decl) {
2625 if (class_interface_decl->getDefinition())
2628 if (!allow_completion)
2631 if (class_interface_decl->hasExternalLexicalStorage()) {
2633 clang::ExternalASTSource *external_ast_source =
2634 ast->getExternalSource();
2635 if (external_ast_source) {
2636 external_ast_source->CompleteType(class_interface_decl);
2637 return !objc_class_type->isIncompleteType();
2646 case clang::Type::Typedef:
2647 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2649 ->getUnderlyingType(),
2652 case clang::Type::Auto:
2653 return GetCompleteQualType(
2654 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2657 case clang::Type::Elaborated:
2658 return GetCompleteQualType(
2659 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2662 case clang::Type::Paren:
2663 return GetCompleteQualType(
2664 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2667 case clang::Type::Attributed:
2668 return GetCompleteQualType(
2669 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2679 static clang::ObjCIvarDecl::AccessControl
2680 ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2683 return clang::ObjCIvarDecl::None;
2685 return clang::ObjCIvarDecl::Public;
2686 case eAccessPrivate:
2687 return clang::ObjCIvarDecl::Private;
2688 case eAccessProtected:
2689 return clang::ObjCIvarDecl::Protected;
2690 case eAccessPackage:
2691 return clang::ObjCIvarDecl::Package;
2693 return clang::ObjCIvarDecl::None;
2696 //----------------------------------------------------------------------
2698 //----------------------------------------------------------------------
2700 bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2701 clang::QualType qual_type(GetCanonicalQualType(type));
2703 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2704 switch (type_class) {
2705 case clang::Type::IncompleteArray:
2706 case clang::Type::VariableArray:
2707 case clang::Type::ConstantArray:
2708 case clang::Type::ExtVector:
2709 case clang::Type::Vector:
2710 case clang::Type::Record:
2711 case clang::Type::ObjCObject:
2712 case clang::Type::ObjCInterface:
2714 case clang::Type::Auto:
2715 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2718 case clang::Type::Elaborated:
2719 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2722 case clang::Type::Typedef:
2723 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2725 ->getUnderlyingType()
2727 case clang::Type::Paren:
2728 return IsAggregateType(
2729 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2733 // The clang type does have a value
2737 bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2738 clang::QualType qual_type(GetCanonicalQualType(type));
2740 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2741 switch (type_class) {
2742 case clang::Type::Record: {
2743 if (const clang::RecordType *record_type =
2744 llvm::dyn_cast_or_null<clang::RecordType>(
2745 qual_type.getTypePtrOrNull())) {
2746 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2747 return record_decl->isAnonymousStructOrUnion();
2752 case clang::Type::Auto:
2753 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2756 case clang::Type::Elaborated:
2757 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2760 case clang::Type::Typedef:
2761 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2763 ->getUnderlyingType()
2765 case clang::Type::Paren:
2766 return IsAnonymousType(
2767 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2771 // The clang type does have a value
2775 bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2776 CompilerType *element_type_ptr,
2777 uint64_t *size, bool *is_incomplete) {
2778 clang::QualType qual_type(GetCanonicalQualType(type));
2780 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2781 switch (type_class) {
2785 case clang::Type::ConstantArray:
2786 if (element_type_ptr)
2787 element_type_ptr->SetCompilerType(
2789 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2791 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2793 .getLimitedValue(ULLONG_MAX);
2795 *is_incomplete = false;
2798 case clang::Type::IncompleteArray:
2799 if (element_type_ptr)
2800 element_type_ptr->SetCompilerType(
2802 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2806 *is_incomplete = true;
2809 case clang::Type::VariableArray:
2810 if (element_type_ptr)
2811 element_type_ptr->SetCompilerType(
2813 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2817 *is_incomplete = false;
2820 case clang::Type::DependentSizedArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
2823 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2824 ->getElementType());
2828 *is_incomplete = false;
2831 case clang::Type::Typedef:
2832 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2834 ->getUnderlyingType()
2836 element_type_ptr, size, is_incomplete);
2837 case clang::Type::Auto:
2838 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2841 element_type_ptr, size, is_incomplete);
2842 case clang::Type::Elaborated:
2843 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2846 element_type_ptr, size, is_incomplete);
2847 case clang::Type::Paren:
2849 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2850 element_type_ptr, size, is_incomplete);
2852 if (element_type_ptr)
2853 element_type_ptr->Clear();
2857 *is_incomplete = false;
2861 bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2862 CompilerType *element_type, uint64_t *size) {
2863 clang::QualType qual_type(GetCanonicalQualType(type));
2865 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2866 switch (type_class) {
2867 case clang::Type::Vector: {
2868 const clang::VectorType *vector_type =
2869 qual_type->getAs<clang::VectorType>();
2872 *size = vector_type->getNumElements();
2875 CompilerType(getASTContext(), vector_type->getElementType());
2879 case clang::Type::ExtVector: {
2880 const clang::ExtVectorType *ext_vector_type =
2881 qual_type->getAs<clang::ExtVectorType>();
2882 if (ext_vector_type) {
2884 *size = ext_vector_type->getNumElements();
2887 CompilerType(getASTContext(), ext_vector_type->getElementType());
2897 bool ClangASTContext::IsRuntimeGeneratedType(
2898 lldb::opaque_compiler_type_t type) {
2899 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2900 ->GetDeclContextForType(GetQualType(type));
2904 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2907 clang::ObjCInterfaceDecl *result_iface_decl =
2908 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2910 ClangASTMetadata *ast_metadata =
2911 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2914 return (ast_metadata->GetISAPtr() != 0);
2917 bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2918 return GetQualType(type).getUnqualifiedType()->isCharType();
2921 bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2922 const bool allow_completion = false;
2923 return GetCompleteQualType(getASTContext(), GetQualType(type),
2927 bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2928 return GetQualType(type).isConstQualified();
2931 bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2933 CompilerType pointee_or_element_clang_type;
2935 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2937 if (!pointee_or_element_clang_type.IsValid())
2940 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2941 if (pointee_or_element_clang_type.IsCharType()) {
2942 if (type_flags.Test(eTypeIsArray)) {
2943 // We know the size of the array and it could be a C string
2944 // since it is an array of characters
2945 length = llvm::cast<clang::ConstantArrayType>(
2946 GetCanonicalQualType(type).getTypePtr())
2956 bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2957 bool *is_variadic_ptr) {
2959 clang::QualType qual_type(GetCanonicalQualType(type));
2961 if (qual_type->isFunctionType()) {
2962 if (is_variadic_ptr) {
2963 const clang::FunctionProtoType *function_proto_type =
2964 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2965 if (function_proto_type)
2966 *is_variadic_ptr = function_proto_type->isVariadic();
2968 *is_variadic_ptr = false;
2973 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2974 switch (type_class) {
2977 case clang::Type::Typedef:
2978 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2980 ->getUnderlyingType()
2983 case clang::Type::Auto:
2984 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2988 case clang::Type::Elaborated:
2989 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2993 case clang::Type::Paren:
2994 return IsFunctionType(
2995 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2997 case clang::Type::LValueReference:
2998 case clang::Type::RValueReference: {
2999 const clang::ReferenceType *reference_type =
3000 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3002 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3010 // Used to detect "Homogeneous Floating-point Aggregates"
3012 ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3013 CompilerType *base_type_ptr) {
3017 clang::QualType qual_type(GetCanonicalQualType(type));
3018 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3019 switch (type_class) {
3020 case clang::Type::Record:
3021 if (GetCompleteType(type)) {
3022 const clang::CXXRecordDecl *cxx_record_decl =
3023 qual_type->getAsCXXRecordDecl();
3024 if (cxx_record_decl) {
3025 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3028 const clang::RecordType *record_type =
3029 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3031 const clang::RecordDecl *record_decl = record_type->getDecl();
3033 // We are looking for a structure that contains only floating point
3035 clang::RecordDecl::field_iterator field_pos,
3036 field_end = record_decl->field_end();
3037 uint32_t num_fields = 0;
3038 bool is_hva = false;
3039 bool is_hfa = false;
3040 clang::QualType base_qual_type;
3041 uint64_t base_bitwidth = 0;
3042 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3044 clang::QualType field_qual_type = field_pos->getType();
3045 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3046 if (field_qual_type->isFloatingType()) {
3047 if (field_qual_type->isComplexType())
3050 if (num_fields == 0)
3051 base_qual_type = field_qual_type;
3056 if (field_qual_type.getTypePtr() !=
3057 base_qual_type.getTypePtr())
3061 } else if (field_qual_type->isVectorType() ||
3062 field_qual_type->isExtVectorType()) {
3063 if (num_fields == 0) {
3064 base_qual_type = field_qual_type;
3065 base_bitwidth = field_bitwidth;
3070 if (base_bitwidth != field_bitwidth)
3072 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3080 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3087 case clang::Type::Typedef:
3088 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3090 ->getUnderlyingType()
3094 case clang::Type::Auto:
3095 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3100 case clang::Type::Elaborated:
3101 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3111 size_t ClangASTContext::GetNumberOfFunctionArguments(
3112 lldb::opaque_compiler_type_t type) {
3114 clang::QualType qual_type(GetCanonicalQualType(type));
3115 const clang::FunctionProtoType *func =
3116 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3118 return func->getNumParams();
3124 ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3125 const size_t index) {
3127 clang::QualType qual_type(GetQualType(type));
3128 const clang::FunctionProtoType *func =
3129 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3131 if (index < func->getNumParams())
3132 return CompilerType(getASTContext(), func->getParamType(index));
3135 return CompilerType();
3138 bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3140 clang::QualType qual_type(GetCanonicalQualType(type));
3142 if (qual_type->isFunctionPointerType())
3145 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3146 switch (type_class) {
3149 case clang::Type::Typedef:
3150 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3152 ->getUnderlyingType()
3154 case clang::Type::Auto:
3155 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3158 case clang::Type::Elaborated:
3159 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3162 case clang::Type::Paren:
3163 return IsFunctionPointerType(
3164 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3166 case clang::Type::LValueReference:
3167 case clang::Type::RValueReference: {
3168 const clang::ReferenceType *reference_type =
3169 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3171 return IsFunctionPointerType(
3172 reference_type->getPointeeType().getAsOpaquePtr());
3179 bool ClangASTContext::IsBlockPointerType(
3180 lldb::opaque_compiler_type_t type,
3181 CompilerType *function_pointer_type_ptr) {
3183 clang::QualType qual_type(GetCanonicalQualType(type));
3185 if (qual_type->isBlockPointerType()) {
3186 if (function_pointer_type_ptr) {
3187 const clang::BlockPointerType *block_pointer_type =
3188 qual_type->getAs<clang::BlockPointerType>();
3189 QualType pointee_type = block_pointer_type->getPointeeType();
3190 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3191 *function_pointer_type_ptr =
3192 CompilerType(getASTContext(), function_pointer_type);
3197 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3198 switch (type_class) {
3201 case clang::Type::Typedef:
3202 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3204 ->getUnderlyingType()
3206 function_pointer_type_ptr);
3207 case clang::Type::Auto:
3208 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3211 function_pointer_type_ptr);
3212 case clang::Type::Elaborated:
3213 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3216 function_pointer_type_ptr);
3217 case clang::Type::Paren:
3218 return IsBlockPointerType(
3219 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3220 function_pointer_type_ptr);
3222 case clang::Type::LValueReference:
3223 case clang::Type::RValueReference: {
3224 const clang::ReferenceType *reference_type =
3225 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3227 return IsBlockPointerType(
3228 reference_type->getPointeeType().getAsOpaquePtr(),
3229 function_pointer_type_ptr);
3236 bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3241 clang::QualType qual_type(GetCanonicalQualType(type));
3242 const clang::BuiltinType *builtin_type =
3243 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3246 if (builtin_type->isInteger()) {
3247 is_signed = builtin_type->isSignedInteger();
3255 bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3258 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3259 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3262 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3271 bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3272 CompilerType *pointee_type) {
3274 clang::QualType qual_type(GetCanonicalQualType(type));
3275 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3276 switch (type_class) {
3277 case clang::Type::Builtin:
3278 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3281 case clang::BuiltinType::ObjCId:
3282 case clang::BuiltinType::ObjCClass:
3286 case clang::Type::ObjCObjectPointer:
3288 pointee_type->SetCompilerType(
3289 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3290 ->getPointeeType());
3292 case clang::Type::BlockPointer:
3294 pointee_type->SetCompilerType(
3296 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3298 case clang::Type::Pointer:
3300 pointee_type->SetCompilerType(
3302 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3304 case clang::Type::MemberPointer:
3306 pointee_type->SetCompilerType(
3308 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3310 case clang::Type::Typedef:
3311 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3313 ->getUnderlyingType()
3316 case clang::Type::Auto:
3317 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3321 case clang::Type::Elaborated:
3322 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3326 case clang::Type::Paren:
3327 return IsPointerType(
3328 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3335 pointee_type->Clear();
3339 bool ClangASTContext::IsPointerOrReferenceType(
3340 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3342 clang::QualType qual_type(GetCanonicalQualType(type));
3343 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3344 switch (type_class) {
3345 case clang::Type::Builtin:
3346 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3349 case clang::BuiltinType::ObjCId:
3350 case clang::BuiltinType::ObjCClass:
3354 case clang::Type::ObjCObjectPointer:
3356 pointee_type->SetCompilerType(
3357 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3358 ->getPointeeType());
3360 case clang::Type::BlockPointer:
3362 pointee_type->SetCompilerType(
3364 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3366 case clang::Type::Pointer:
3368 pointee_type->SetCompilerType(
3370 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3372 case clang::Type::MemberPointer:
3374 pointee_type->SetCompilerType(
3376 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3378 case clang::Type::LValueReference:
3380 pointee_type->SetCompilerType(
3382 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3384 case clang::Type::RValueReference:
3386 pointee_type->SetCompilerType(
3388 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3390 case clang::Type::Typedef:
3391 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3393 ->getUnderlyingType()
3396 case clang::Type::Auto:
3397 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3401 case clang::Type::Elaborated:
3402 return IsPointerOrReferenceType(
3403 llvm::cast<clang::ElaboratedType>(qual_type)
3407 case clang::Type::Paren:
3408 return IsPointerOrReferenceType(
3409 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3416 pointee_type->Clear();
3420 bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3421 CompilerType *pointee_type,
3424 clang::QualType qual_type(GetCanonicalQualType(type));
3425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3427 switch (type_class) {
3428 case clang::Type::LValueReference:
3430 pointee_type->SetCompilerType(
3432 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3436 case clang::Type::RValueReference:
3438 pointee_type->SetCompilerType(
3440 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3444 case clang::Type::Typedef:
3445 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3447 ->getUnderlyingType()
3449 pointee_type, is_rvalue);
3450 case clang::Type::Auto:
3451 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3454 pointee_type, is_rvalue);
3455 case clang::Type::Elaborated:
3456 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3459 pointee_type, is_rvalue);
3460 case clang::Type::Paren:
3461 return IsReferenceType(
3462 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3463 pointee_type, is_rvalue);
3470 pointee_type->Clear();
3474 bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3475 uint32_t &count, bool &is_complex) {
3477 clang::QualType qual_type(GetCanonicalQualType(type));
3479 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3480 qual_type->getCanonicalTypeInternal())) {
3481 clang::BuiltinType::Kind kind = BT->getKind();
3482 if (kind >= clang::BuiltinType::Float &&
3483 kind <= clang::BuiltinType::LongDouble) {
3488 } else if (const clang::ComplexType *CT =
3489 llvm::dyn_cast<clang::ComplexType>(
3490 qual_type->getCanonicalTypeInternal())) {
3491 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3497 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3498 qual_type->getCanonicalTypeInternal())) {
3499 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3501 count = VT->getNumElements();
3512 bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3516 clang::QualType qual_type(GetQualType(type));
3517 const clang::TagType *tag_type =
3518 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3520 clang::TagDecl *tag_decl = tag_type->getDecl();
3522 return tag_decl->isCompleteDefinition();
3525 const clang::ObjCObjectType *objc_class_type =
3526 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3527 if (objc_class_type) {
3528 clang::ObjCInterfaceDecl *class_interface_decl =
3529 objc_class_type->getInterface();
3530 if (class_interface_decl)
3531 return class_interface_decl->getDefinition() != nullptr;
3538 bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3540 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3542 const clang::ObjCObjectPointerType *obj_pointer_type =
3543 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3545 if (obj_pointer_type)
3546 return obj_pointer_type->isObjCClassType();
3551 bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3552 if (ClangUtil::IsClangType(type))
3553 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3557 bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3560 clang::QualType qual_type(GetCanonicalQualType(type));
3561 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3562 return (type_class == clang::Type::Record);
3565 bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3568 clang::QualType qual_type(GetCanonicalQualType(type));
3569 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3570 return (type_class == clang::Type::Enum);
3573 bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3575 clang::QualType qual_type(GetCanonicalQualType(type));
3576 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3577 switch (type_class) {
3578 case clang::Type::Record:
3579 if (GetCompleteType(type)) {
3580 const clang::RecordType *record_type =
3581 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3582 const clang::RecordDecl *record_decl = record_type->getDecl();
3584 const clang::CXXRecordDecl *cxx_record_decl =
3585 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3586 if (cxx_record_decl)
3587 return cxx_record_decl->isPolymorphic();
3599 bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3600 CompilerType *dynamic_pointee_type,
3601 bool check_cplusplus,
3603 clang::QualType pointee_qual_type;
3605 clang::QualType qual_type(GetCanonicalQualType(type));
3606 bool success = false;
3607 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3608 switch (type_class) {
3609 case clang::Type::Builtin:
3611 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3612 clang::BuiltinType::ObjCId) {
3613 if (dynamic_pointee_type)
3614 dynamic_pointee_type->SetCompilerType(this, type);
3619 case clang::Type::ObjCObjectPointer:
3621 if (auto objc_pointee_type =
3622 qual_type->getPointeeType().getTypePtrOrNull()) {
3623 if (auto objc_object_type =
3624 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3625 objc_pointee_type)) {
3626 if (objc_object_type->isObjCClass())
3630 if (dynamic_pointee_type)
3631 dynamic_pointee_type->SetCompilerType(
3633 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3634 ->getPointeeType());
3639 case clang::Type::Pointer:
3641 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3645 case clang::Type::LValueReference:
3646 case clang::Type::RValueReference:
3648 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3652 case clang::Type::Typedef:
3653 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3655 ->getUnderlyingType()
3657 dynamic_pointee_type, check_cplusplus,
3660 case clang::Type::Auto:
3661 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3664 dynamic_pointee_type, check_cplusplus,
3667 case clang::Type::Elaborated:
3668 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3671 dynamic_pointee_type, check_cplusplus,
3674 case clang::Type::Paren:
3675 return IsPossibleDynamicType(
3676 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3677 dynamic_pointee_type, check_cplusplus, check_objc);
3683 // Check to make sure what we are pointing too is a possible dynamic C++
3685 // We currently accept any "void *" (in case we have a class that has been
3686 // watered down to an opaque pointer) and virtual C++ classes.
3687 const clang::Type::TypeClass pointee_type_class =
3688 pointee_qual_type.getCanonicalType()->getTypeClass();
3689 switch (pointee_type_class) {
3690 case clang::Type::Builtin:
3691 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3692 case clang::BuiltinType::UnknownAny:
3693 case clang::BuiltinType::Void:
3694 if (dynamic_pointee_type)
3695 dynamic_pointee_type->SetCompilerType(getASTContext(),
3703 case clang::Type::Record:
3704 if (check_cplusplus) {
3705 clang::CXXRecordDecl *cxx_record_decl =
3706 pointee_qual_type->getAsCXXRecordDecl();
3707 if (cxx_record_decl) {
3708 bool is_complete = cxx_record_decl->isCompleteDefinition();
3711 success = cxx_record_decl->isDynamicClass();
3713 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3714 getASTContext(), cxx_record_decl);
3716 success = metadata->GetIsDynamicCXXType();
3718 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3721 success = cxx_record_decl->isDynamicClass();
3728 if (dynamic_pointee_type)
3729 dynamic_pointee_type->SetCompilerType(getASTContext(),
3737 case clang::Type::ObjCObject:
3738 case clang::Type::ObjCInterface:
3740 if (dynamic_pointee_type)
3741 dynamic_pointee_type->SetCompilerType(getASTContext(),
3752 if (dynamic_pointee_type)
3753 dynamic_pointee_type->Clear();
3757 bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3761 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3764 bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3767 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3770 bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3773 return GetCanonicalQualType(type)->isVoidType();
3776 bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3777 return ClangASTContextSupportsLanguage(language);
3780 bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3781 std::string &class_name) {
3783 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3784 if (!qual_type.isNull()) {
3785 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3786 if (cxx_record_decl) {
3787 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3796 bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3800 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3801 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3806 bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3809 clang::QualType qual_type(GetCanonicalQualType(type));
3810 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3812 return tag_type->isBeingDefined();
3816 bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3817 CompilerType *class_type_ptr) {
3821 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3823 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3824 if (class_type_ptr) {
3825 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3826 const clang::ObjCObjectPointerType *obj_pointer_type =
3827 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3828 if (obj_pointer_type == nullptr)
3829 class_type_ptr->Clear();
3831 class_type_ptr->SetCompilerType(
3832 type.GetTypeSystem(),
3833 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3840 class_type_ptr->Clear();
3844 bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3845 std::string &class_name) {
3849 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3851 const clang::ObjCObjectType *object_type =
3852 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3854 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3856 class_name = interface->getNameAsString();
3863 //----------------------------------------------------------------------
3865 //----------------------------------------------------------------------
3867 bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3870 const bool allow_completion = true;
3871 return GetCompleteQualType(getASTContext(), GetQualType(type),
3875 ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3876 std::string type_name;
3878 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3879 clang::QualType qual_type(GetQualType(type));
3880 printing_policy.SuppressTagKeyword = true;
3881 const clang::TypedefType *typedef_type =
3882 qual_type->getAs<clang::TypedefType>();
3884 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3885 type_name = typedef_decl->getQualifiedNameAsString();
3887 type_name = qual_type.getAsString(printing_policy);
3890 return ConstString(type_name);
3894 ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3895 CompilerType *pointee_or_element_clang_type) {
3899 if (pointee_or_element_clang_type)
3900 pointee_or_element_clang_type->Clear();
3902 clang::QualType qual_type(GetQualType(type));
3904 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3905 switch (type_class) {
3906 case clang::Type::Builtin: {
3907 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3908 qual_type->getCanonicalTypeInternal());
3910 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3911 switch (builtin_type->getKind()) {
3912 case clang::BuiltinType::ObjCId:
3913 case clang::BuiltinType::ObjCClass:
3914 if (pointee_or_element_clang_type)
3915 pointee_or_element_clang_type->SetCompilerType(
3916 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3917 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3920 case clang::BuiltinType::ObjCSel:
3921 if (pointee_or_element_clang_type)
3922 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3923 getASTContext()->CharTy);
3924 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3927 case clang::BuiltinType::Bool:
3928 case clang::BuiltinType::Char_U:
3929 case clang::BuiltinType::UChar:
3930 case clang::BuiltinType::WChar_U:
3931 case clang::BuiltinType::Char16:
3932 case clang::BuiltinType::Char32:
3933 case clang::BuiltinType::UShort:
3934 case clang::BuiltinType::UInt:
3935 case clang::BuiltinType::ULong:
3936 case clang::BuiltinType::ULongLong:
3937 case clang::BuiltinType::UInt128:
3938 case clang::BuiltinType::Char_S:
3939 case clang::BuiltinType::SChar:
3940 case clang::BuiltinType::WChar_S:
3941 case clang::BuiltinType::Short:
3942 case clang::BuiltinType::Int:
3943 case clang::BuiltinType::Long:
3944 case clang::BuiltinType::LongLong:
3945 case clang::BuiltinType::Int128:
3946 case clang::BuiltinType::Float:
3947 case clang::BuiltinType::Double:
3948 case clang::BuiltinType::LongDouble:
3949 builtin_type_flags |= eTypeIsScalar;
3950 if (builtin_type->isInteger()) {
3951 builtin_type_flags |= eTypeIsInteger;
3952 if (builtin_type->isSignedInteger())
3953 builtin_type_flags |= eTypeIsSigned;
3954 } else if (builtin_type->isFloatingPoint())
3955 builtin_type_flags |= eTypeIsFloat;
3960 return builtin_type_flags;
3963 case clang::Type::BlockPointer:
3964 if (pointee_or_element_clang_type)
3965 pointee_or_element_clang_type->SetCompilerType(
3966 getASTContext(), qual_type->getPointeeType());
3967 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3969 case clang::Type::Complex: {
3970 uint32_t complex_type_flags =
3971 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3972 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3973 qual_type->getCanonicalTypeInternal());
3975 clang::QualType complex_element_type(complex_type->getElementType());
3976 if (complex_element_type->isIntegerType())
3977 complex_type_flags |= eTypeIsFloat;
3978 else if (complex_element_type->isFloatingType())
3979 complex_type_flags |= eTypeIsInteger;
3981 return complex_type_flags;
3984 case clang::Type::ConstantArray:
3985 case clang::Type::DependentSizedArray:
3986 case clang::Type::IncompleteArray:
3987 case clang::Type::VariableArray:
3988 if (pointee_or_element_clang_type)
3989 pointee_or_element_clang_type->SetCompilerType(
3990 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3991 ->getElementType());
3992 return eTypeHasChildren | eTypeIsArray;
3994 case clang::Type::DependentName:
3996 case clang::Type::DependentSizedExtVector:
3997 return eTypeHasChildren | eTypeIsVector;
3998 case clang::Type::DependentTemplateSpecialization:
3999 return eTypeIsTemplate;
4000 case clang::Type::Decltype:
4003 case clang::Type::Enum:
4004 if (pointee_or_element_clang_type)
4005 pointee_or_element_clang_type->SetCompilerType(
4007 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4008 return eTypeIsEnumeration | eTypeHasValue;
4010 case clang::Type::Auto:
4011 return CompilerType(
4013 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4014 .GetTypeInfo(pointee_or_element_clang_type);
4015 case clang::Type::Elaborated:
4016 return CompilerType(
4018 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4019 .GetTypeInfo(pointee_or_element_clang_type);
4020 case clang::Type::Paren:
4021 return CompilerType(getASTContext(),
4022 llvm::cast<clang::ParenType>(qual_type)->desugar())
4023 .GetTypeInfo(pointee_or_element_clang_type);
4025 case clang::Type::FunctionProto:
4026 return eTypeIsFuncPrototype | eTypeHasValue;
4027 case clang::Type::FunctionNoProto:
4028 return eTypeIsFuncPrototype | eTypeHasValue;
4029 case clang::Type::InjectedClassName:
4032 case clang::Type::LValueReference:
4033 case clang::Type::RValueReference:
4034 if (pointee_or_element_clang_type)
4035 pointee_or_element_clang_type->SetCompilerType(
4037 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4038 ->getPointeeType());
4039 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4041 case clang::Type::MemberPointer:
4042 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4044 case clang::Type::ObjCObjectPointer:
4045 if (pointee_or_element_clang_type)
4046 pointee_or_element_clang_type->SetCompilerType(
4047 getASTContext(), qual_type->getPointeeType());
4048 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4051 case clang::Type::ObjCObject:
4052 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4053 case clang::Type::ObjCInterface:
4054 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4056 case clang::Type::Pointer:
4057 if (pointee_or_element_clang_type)
4058 pointee_or_element_clang_type->SetCompilerType(
4059 getASTContext(), qual_type->getPointeeType());
4060 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4062 case clang::Type::Record:
4063 if (qual_type->getAsCXXRecordDecl())
4064 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4066 return eTypeHasChildren | eTypeIsStructUnion;
4068 case clang::Type::SubstTemplateTypeParm:
4069 return eTypeIsTemplate;
4070 case clang::Type::TemplateTypeParm:
4071 return eTypeIsTemplate;
4072 case clang::Type::TemplateSpecialization:
4073 return eTypeIsTemplate;
4075 case clang::Type::Typedef:
4076 return eTypeIsTypedef |
4077 CompilerType(getASTContext(),
4078 llvm::cast<clang::TypedefType>(qual_type)
4080 ->getUnderlyingType())
4081 .GetTypeInfo(pointee_or_element_clang_type);
4082 case clang::Type::TypeOfExpr:
4084 case clang::Type::TypeOf:
4086 case clang::Type::UnresolvedUsing:
4089 case clang::Type::ExtVector:
4090 case clang::Type::Vector: {
4091 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4092 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4093 qual_type->getCanonicalTypeInternal());
4095 if (vector_type->isIntegerType())
4096 vector_type_flags |= eTypeIsFloat;
4097 else if (vector_type->isFloatingType())
4098 vector_type_flags |= eTypeIsInteger;
4100 return vector_type_flags;
4109 ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4111 return lldb::eLanguageTypeC;
4113 // If the type is a reference, then resolve it to what it refers to first:
4114 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4115 if (qual_type->isAnyPointerType()) {
4116 if (qual_type->isObjCObjectPointerType())
4117 return lldb::eLanguageTypeObjC;
4119 clang::QualType pointee_type(qual_type->getPointeeType());
4120 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4121 return lldb::eLanguageTypeC_plus_plus;
4122 if (pointee_type->isObjCObjectOrInterfaceType())
4123 return lldb::eLanguageTypeObjC;
4124 if (pointee_type->isObjCClassType())
4125 return lldb::eLanguageTypeObjC;
4126 if (pointee_type.getTypePtr() ==
4127 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4128 return lldb::eLanguageTypeObjC;
4130 if (qual_type->isObjCObjectOrInterfaceType())
4131 return lldb::eLanguageTypeObjC;
4132 if (qual_type->getAsCXXRecordDecl())
4133 return lldb::eLanguageTypeC_plus_plus;
4134 switch (qual_type->getTypeClass()) {
4137 case clang::Type::Builtin:
4138 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4140 case clang::BuiltinType::Void:
4141 case clang::BuiltinType::Bool:
4142 case clang::BuiltinType::Char_U:
4143 case clang::BuiltinType::UChar:
4144 case clang::BuiltinType::WChar_U:
4145 case clang::BuiltinType::Char16:
4146 case clang::BuiltinType::Char32:
4147 case clang::BuiltinType::UShort:
4148 case clang::BuiltinType::UInt:
4149 case clang::BuiltinType::ULong:
4150 case clang::BuiltinType::ULongLong:
4151 case clang::BuiltinType::UInt128:
4152 case clang::BuiltinType::Char_S:
4153 case clang::BuiltinType::SChar:
4154 case clang::BuiltinType::WChar_S:
4155 case clang::BuiltinType::Short:
4156 case clang::BuiltinType::Int:
4157 case clang::BuiltinType::Long:
4158 case clang::BuiltinType::LongLong:
4159 case clang::BuiltinType::Int128:
4160 case clang::BuiltinType::Float:
4161 case clang::BuiltinType::Double:
4162 case clang::BuiltinType::LongDouble:
4165 case clang::BuiltinType::NullPtr:
4166 return eLanguageTypeC_plus_plus;
4168 case clang::BuiltinType::ObjCId:
4169 case clang::BuiltinType::ObjCClass:
4170 case clang::BuiltinType::ObjCSel:
4171 return eLanguageTypeObjC;
4173 case clang::BuiltinType::Dependent:
4174 case clang::BuiltinType::Overload:
4175 case clang::BuiltinType::BoundMember:
4176 case clang::BuiltinType::UnknownAny:
4180 case clang::Type::Typedef:
4181 return CompilerType(getASTContext(),
4182 llvm::cast<clang::TypedefType>(qual_type)
4184 ->getUnderlyingType())
4185 .GetMinimumLanguage();
4188 return lldb::eLanguageTypeC;
4192 ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4194 return lldb::eTypeClassInvalid;
4196 clang::QualType qual_type(GetQualType(type));
4198 switch (qual_type->getTypeClass()) {
4199 case clang::Type::UnaryTransform:
4201 case clang::Type::FunctionNoProto:
4202 return lldb::eTypeClassFunction;
4203 case clang::Type::FunctionProto:
4204 return lldb::eTypeClassFunction;
4205 case clang::Type::IncompleteArray:
4206 return lldb::eTypeClassArray;
4207 case clang::Type::VariableArray:
4208 return lldb::eTypeClassArray;
4209 case clang::Type::ConstantArray:
4210 return lldb::eTypeClassArray;
4211 case clang::Type::DependentSizedArray:
4212 return lldb::eTypeClassArray;
4213 case clang::Type::DependentSizedExtVector:
4214 return lldb::eTypeClassVector;
4215 case clang::Type::ExtVector:
4216 return lldb::eTypeClassVector;
4217 case clang::Type::Vector:
4218 return lldb::eTypeClassVector;
4219 case clang::Type::Builtin:
4220 return lldb::eTypeClassBuiltin;
4221 case clang::Type::ObjCObjectPointer:
4222 return lldb::eTypeClassObjCObjectPointer;
4223 case clang::Type::BlockPointer:
4224 return lldb::eTypeClassBlockPointer;
4225 case clang::Type::Pointer:
4226 return lldb::eTypeClassPointer;
4227 case clang::Type::LValueReference:
4228 return lldb::eTypeClassReference;
4229 case clang::Type::RValueReference:
4230 return lldb::eTypeClassReference;
4231 case clang::Type::MemberPointer:
4232 return lldb::eTypeClassMemberPointer;
4233 case clang::Type::Complex:
4234 if (qual_type->isComplexType())
4235 return lldb::eTypeClassComplexFloat;
4237 return lldb::eTypeClassComplexInteger;
4238 case clang::Type::ObjCObject:
4239 return lldb::eTypeClassObjCObject;
4240 case clang::Type::ObjCInterface:
4241 return lldb::eTypeClassObjCInterface;
4242 case clang::Type::Record: {
4243 const clang::RecordType *record_type =
4244 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4245 const clang::RecordDecl *record_decl = record_type->getDecl();
4246 if (record_decl->isUnion())
4247 return lldb::eTypeClassUnion;
4248 else if (record_decl->isStruct())
4249 return lldb::eTypeClassStruct;
4251 return lldb::eTypeClassClass;
4253 case clang::Type::Enum:
4254 return lldb::eTypeClassEnumeration;
4255 case clang::Type::Typedef:
4256 return lldb::eTypeClassTypedef;
4257 case clang::Type::UnresolvedUsing:
4259 case clang::Type::Paren:
4260 return CompilerType(getASTContext(),
4261 llvm::cast<clang::ParenType>(qual_type)->desugar())
4263 case clang::Type::Auto:
4264 return CompilerType(
4266 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4268 case clang::Type::Elaborated:
4269 return CompilerType(
4271 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4274 case clang::Type::Attributed:
4276 case clang::Type::TemplateTypeParm:
4278 case clang::Type::SubstTemplateTypeParm:
4280 case clang::Type::SubstTemplateTypeParmPack:
4282 case clang::Type::InjectedClassName:
4284 case clang::Type::DependentName:
4286 case clang::Type::DependentTemplateSpecialization:
4288 case clang::Type::PackExpansion:
4291 case clang::Type::TypeOfExpr:
4293 case clang::Type::TypeOf:
4295 case clang::Type::Decltype:
4297 case clang::Type::TemplateSpecialization:
4299 case clang::Type::DeducedTemplateSpecialization:
4301 case clang::Type::Atomic:
4303 case clang::Type::Pipe:
4306 // pointer type decayed from an array or function type.
4307 case clang::Type::Decayed:
4309 case clang::Type::Adjusted:
4311 case clang::Type::ObjCTypeParam:
4314 // We don't know hot to display this type...
4315 return lldb::eTypeClassOther;
4318 unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4320 return GetQualType(type).getQualifiers().getCVRQualifiers();
4324 //----------------------------------------------------------------------
4325 // Creating related types
4326 //----------------------------------------------------------------------
4329 ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4332 clang::QualType qual_type(GetCanonicalQualType(type));
4334 const clang::Type *array_eletype =
4335 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4338 return CompilerType();
4340 CompilerType element_type(getASTContext(),
4341 array_eletype->getCanonicalTypeUnqualified());
4343 // TODO: the real stride will be >= this value.. find the real one!
4345 *stride = element_type.GetByteSize(nullptr);
4347 return element_type;
4349 return CompilerType();
4352 CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4355 clang::QualType qual_type(GetCanonicalQualType(type));
4356 if (clang::ASTContext *ast_ctx = getASTContext()) {
4358 return CompilerType(
4359 ast_ctx, ast_ctx->getConstantArrayType(
4360 qual_type, llvm::APInt(64, size),
4361 clang::ArrayType::ArraySizeModifier::Normal, 0));
4363 return CompilerType(
4365 ast_ctx->getIncompleteArrayType(
4366 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4370 return CompilerType();
4374 ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4376 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4377 return CompilerType();
4380 static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4381 clang::QualType qual_type) {
4382 if (qual_type->isPointerType())
4383 qual_type = ast->getPointerType(
4384 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4386 qual_type = qual_type.getUnqualifiedType();
4387 qual_type.removeLocalConst();
4388 qual_type.removeLocalRestrict();
4389 qual_type.removeLocalVolatile();
4394 ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4396 return CompilerType(
4398 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4399 return CompilerType();
4402 int ClangASTContext::GetFunctionArgumentCount(
4403 lldb::opaque_compiler_type_t type) {
4405 const clang::FunctionProtoType *func =
4406 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4408 return func->getNumParams();
4413 CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4414 lldb::opaque_compiler_type_t type, size_t idx) {
4416 const clang::FunctionProtoType *func =
4417 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4419 const uint32_t num_args = func->getNumParams();
4421 return CompilerType(getASTContext(), func->getParamType(idx));
4424 return CompilerType();
4428 ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4430 clang::QualType qual_type(GetQualType(type));
4431 const clang::FunctionProtoType *func =
4432 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4434 return CompilerType(getASTContext(), func->getReturnType());
4436 return CompilerType();
4440 ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4441 size_t num_functions = 0;
4443 clang::QualType qual_type(GetCanonicalQualType(type));
4444 switch (qual_type->getTypeClass()) {
4445 case clang::Type::Record:
4446 if (GetCompleteQualType(getASTContext(), qual_type)) {
4447 const clang::RecordType *record_type =
4448 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4449 const clang::RecordDecl *record_decl = record_type->getDecl();
4450 assert(record_decl);
4451 const clang::CXXRecordDecl *cxx_record_decl =
4452 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4453 if (cxx_record_decl)
4454 num_functions = std::distance(cxx_record_decl->method_begin(),
4455 cxx_record_decl->method_end());
4459 case clang::Type::ObjCObjectPointer: {
4460 const clang::ObjCObjectPointerType *objc_class_type =
4461 qual_type->getAsObjCInterfacePointerType();
4462 const clang::ObjCInterfaceType *objc_interface_type =
4463 objc_class_type->getInterfaceType();
4464 if (objc_interface_type &&
4465 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4466 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4467 clang::ObjCInterfaceDecl *class_interface_decl =
4468 objc_interface_type->getDecl();
4469 if (class_interface_decl) {
4470 num_functions = std::distance(class_interface_decl->meth_begin(),
4471 class_interface_decl->meth_end());
4477 case clang::Type::ObjCObject:
4478 case clang::Type::ObjCInterface:
4479 if (GetCompleteType(type)) {
4480 const clang::ObjCObjectType *objc_class_type =
4481 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4482 if (objc_class_type) {
4483 clang::ObjCInterfaceDecl *class_interface_decl =
4484 objc_class_type->getInterface();
4485 if (class_interface_decl)
4486 num_functions = std::distance(class_interface_decl->meth_begin(),
4487 class_interface_decl->meth_end());
4492 case clang::Type::Typedef:
4493 return CompilerType(getASTContext(),
4494 llvm::cast<clang::TypedefType>(qual_type)
4496 ->getUnderlyingType())
4497 .GetNumMemberFunctions();
4499 case clang::Type::Auto:
4500 return CompilerType(
4502 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4503 .GetNumMemberFunctions();
4505 case clang::Type::Elaborated:
4506 return CompilerType(
4508 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4509 .GetNumMemberFunctions();
4511 case clang::Type::Paren:
4512 return CompilerType(getASTContext(),
4513 llvm::cast<clang::ParenType>(qual_type)->desugar())
4514 .GetNumMemberFunctions();
4520 return num_functions;
4523 TypeMemberFunctionImpl
4524 ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4527 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4528 CompilerType clang_type;
4529 CompilerDecl clang_decl;
4531 clang::QualType qual_type(GetCanonicalQualType(type));
4532 switch (qual_type->getTypeClass()) {
4533 case clang::Type::Record:
4534 if (GetCompleteQualType(getASTContext(), qual_type)) {
4535 const clang::RecordType *record_type =
4536 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4537 const clang::RecordDecl *record_decl = record_type->getDecl();
4538 assert(record_decl);
4539 const clang::CXXRecordDecl *cxx_record_decl =
4540 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4541 if (cxx_record_decl) {
4542 auto method_iter = cxx_record_decl->method_begin();
4543 auto method_end = cxx_record_decl->method_end();
4545 static_cast<size_t>(std::distance(method_iter, method_end))) {
4546 std::advance(method_iter, idx);
4547 clang::CXXMethodDecl *cxx_method_decl =
4548 method_iter->getCanonicalDecl();
4549 if (cxx_method_decl) {
4550 name = cxx_method_decl->getDeclName().getAsString();
4551 if (cxx_method_decl->isStatic())
4552 kind = lldb::eMemberFunctionKindStaticMethod;
4553 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4554 kind = lldb::eMemberFunctionKindConstructor;
4555 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4556 kind = lldb::eMemberFunctionKindDestructor;
4558 kind = lldb::eMemberFunctionKindInstanceMethod;
4559 clang_type = CompilerType(
4560 this, cxx_method_decl->getType().getAsOpaquePtr());
4561 clang_decl = CompilerDecl(this, cxx_method_decl);
4568 case clang::Type::ObjCObjectPointer: {
4569 const clang::ObjCObjectPointerType *objc_class_type =
4570 qual_type->getAsObjCInterfacePointerType();
4571 const clang::ObjCInterfaceType *objc_interface_type =
4572 objc_class_type->getInterfaceType();
4573 if (objc_interface_type &&
4574 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4575 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4576 clang::ObjCInterfaceDecl *class_interface_decl =
4577 objc_interface_type->getDecl();
4578 if (class_interface_decl) {
4579 auto method_iter = class_interface_decl->meth_begin();
4580 auto method_end = class_interface_decl->meth_end();
4582 static_cast<size_t>(std::distance(method_iter, method_end))) {
4583 std::advance(method_iter, idx);
4584 clang::ObjCMethodDecl *objc_method_decl =
4585 method_iter->getCanonicalDecl();
4586 if (objc_method_decl) {
4587 clang_decl = CompilerDecl(this, objc_method_decl);
4588 name = objc_method_decl->getSelector().getAsString();
4589 if (objc_method_decl->isClassMethod())
4590 kind = lldb::eMemberFunctionKindStaticMethod;
4592 kind = lldb::eMemberFunctionKindInstanceMethod;
4600 case clang::Type::ObjCObject:
4601 case clang::Type::ObjCInterface:
4602 if (GetCompleteType(type)) {
4603 const clang::ObjCObjectType *objc_class_type =
4604 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4605 if (objc_class_type) {
4606 clang::ObjCInterfaceDecl *class_interface_decl =
4607 objc_class_type->getInterface();
4608 if (class_interface_decl) {
4609 auto method_iter = class_interface_decl->meth_begin();
4610 auto method_end = class_interface_decl->meth_end();
4612 static_cast<size_t>(std::distance(method_iter, method_end))) {
4613 std::advance(method_iter, idx);
4614 clang::ObjCMethodDecl *objc_method_decl =
4615 method_iter->getCanonicalDecl();
4616 if (objc_method_decl) {
4617 clang_decl = CompilerDecl(this, objc_method_decl);
4618 name = objc_method_decl->getSelector().getAsString();
4619 if (objc_method_decl->isClassMethod())
4620 kind = lldb::eMemberFunctionKindStaticMethod;
4622 kind = lldb::eMemberFunctionKindInstanceMethod;
4630 case clang::Type::Typedef:
4631 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4633 ->getUnderlyingType()
4637 case clang::Type::Auto:
4638 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4643 case clang::Type::Elaborated:
4644 return GetMemberFunctionAtIndex(
4645 llvm::cast<clang::ElaboratedType>(qual_type)
4650 case clang::Type::Paren:
4651 return GetMemberFunctionAtIndex(
4652 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4660 if (kind == eMemberFunctionKindUnknown)
4661 return TypeMemberFunctionImpl();
4663 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4667 ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4669 return CompilerType(getASTContext(),
4670 GetQualType(type).getNonReferenceType());
4671 return CompilerType();
4674 CompilerType ClangASTContext::CreateTypedefType(
4675 const CompilerType &type, const char *typedef_name,
4676 const CompilerDeclContext &compiler_decl_ctx) {
4677 if (type && typedef_name && typedef_name[0]) {
4678 ClangASTContext *ast =
4679 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4681 return CompilerType();
4682 clang::ASTContext *clang_ast = ast->getASTContext();
4683 clang::QualType qual_type(ClangUtil::GetQualType(type));
4685 clang::DeclContext *decl_ctx =
4686 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4687 if (decl_ctx == nullptr)
4688 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4690 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4691 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4692 &clang_ast->Idents.get(typedef_name),
4693 clang_ast->getTrivialTypeSourceInfo(qual_type));
4695 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4697 // Get a uniqued clang::QualType for the typedef decl type
4698 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4700 return CompilerType();
4704 ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4706 clang::QualType qual_type(GetQualType(type));
4707 return CompilerType(getASTContext(),
4708 qual_type.getTypePtr()->getPointeeType());
4710 return CompilerType();
4714 ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4716 clang::QualType qual_type(GetQualType(type));
4718 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4719 switch (type_class) {
4720 case clang::Type::ObjCObject:
4721 case clang::Type::ObjCInterface:
4722 return CompilerType(getASTContext(),
4723 getASTContext()->getObjCObjectPointerType(qual_type));
4726 return CompilerType(getASTContext(),
4727 getASTContext()->getPointerType(qual_type));
4730 return CompilerType();
4734 ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4736 return CompilerType(this, getASTContext()
4737 ->getLValueReferenceType(GetQualType(type))
4740 return CompilerType();
4744 ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4746 return CompilerType(this, getASTContext()
4747 ->getRValueReferenceType(GetQualType(type))
4750 return CompilerType();
4754 ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4756 clang::QualType result(GetQualType(type));
4758 return CompilerType(this, result.getAsOpaquePtr());
4760 return CompilerType();
4764 ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4766 clang::QualType result(GetQualType(type));
4767 result.addVolatile();
4768 return CompilerType(this, result.getAsOpaquePtr());
4770 return CompilerType();
4774 ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4776 clang::QualType result(GetQualType(type));
4777 result.addRestrict();
4778 return CompilerType(this, result.getAsOpaquePtr());
4780 return CompilerType();
4784 ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4785 const char *typedef_name,
4786 const CompilerDeclContext &compiler_decl_ctx) {
4788 clang::ASTContext *clang_ast = getASTContext();
4789 clang::QualType qual_type(GetQualType(type));
4791 clang::DeclContext *decl_ctx =
4792 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4793 if (decl_ctx == nullptr)
4794 decl_ctx = getASTContext()->getTranslationUnitDecl();
4796 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4797 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4798 &clang_ast->Idents.get(typedef_name),
4799 clang_ast->getTrivialTypeSourceInfo(qual_type));
4801 clang::TagDecl *tdecl = nullptr;
4802 if (!qual_type.isNull()) {
4803 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4804 tdecl = rt->getDecl();
4805 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4806 tdecl = et->getDecl();
4809 // Check whether this declaration is an anonymous struct, union, or enum,
4810 // hidden behind a typedef. If so, we
4811 // try to check whether we have a typedef tag to attach to the original
4812 // record declaration
4813 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4814 tdecl->setTypedefNameForAnonDecl(decl);
4816 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4818 // Get a uniqued clang::QualType for the typedef decl type
4819 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4821 return CompilerType();
4825 ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4827 const clang::TypedefType *typedef_type =
4828 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4830 return CompilerType(getASTContext(),
4831 typedef_type->getDecl()->getUnderlyingType());
4833 return CompilerType();
4836 //----------------------------------------------------------------------
4837 // Create related types using the current type's AST
4838 //----------------------------------------------------------------------
4840 CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4841 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
4843 //----------------------------------------------------------------------
4844 // Exploring the type
4845 //----------------------------------------------------------------------
4847 uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4848 ExecutionContextScope *exe_scope) {
4849 if (GetCompleteType(type)) {
4850 clang::QualType qual_type(GetCanonicalQualType(type));
4851 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4852 switch (type_class) {
4853 case clang::Type::Record:
4854 if (GetCompleteType(type))
4855 return getASTContext()->getTypeSize(qual_type);
4860 case clang::Type::ObjCInterface:
4861 case clang::Type::ObjCObject: {
4862 ExecutionContext exe_ctx(exe_scope);
4863 Process *process = exe_ctx.GetProcessPtr();
4865 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4867 uint64_t bit_size = 0;
4868 if (objc_runtime->GetTypeBitSize(
4869 CompilerType(getASTContext(), qual_type), bit_size))
4873 static bool g_printed = false;
4876 DumpTypeDescription(type, &s);
4878 llvm::outs() << "warning: trying to determine the size of type ";
4879 llvm::outs() << s.GetString() << "\n";
4880 llvm::outs() << "without a valid ExecutionContext. this is not "
4881 "reliable. please file a bug against LLDB.\n";
4882 llvm::outs() << "backtrace:\n";
4883 llvm::sys::PrintStackTrace(llvm::outs());
4884 llvm::outs() << "\n";
4891 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4892 if (bit_size == 0) {
4893 if (qual_type->isIncompleteArrayType())
4894 return getASTContext()->getTypeSize(
4895 qual_type->getArrayElementTypeNoTypeQual()
4896 ->getCanonicalTypeUnqualified());
4898 if (qual_type->isObjCObjectOrInterfaceType())
4900 getASTContext()->getTypeSize(
4901 getASTContext()->ObjCBuiltinClassTy);
4908 size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4909 if (GetCompleteType(type))
4910 return getASTContext()->getTypeAlign(GetQualType(type));
4914 lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4917 return lldb::eEncodingInvalid;
4920 clang::QualType qual_type(GetCanonicalQualType(type));
4922 switch (qual_type->getTypeClass()) {
4923 case clang::Type::UnaryTransform:
4926 case clang::Type::FunctionNoProto:
4927 case clang::Type::FunctionProto:
4930 case clang::Type::IncompleteArray:
4931 case clang::Type::VariableArray:
4934 case clang::Type::ConstantArray:
4937 case clang::Type::ExtVector:
4938 case clang::Type::Vector:
4939 // TODO: Set this to more than one???
4942 case clang::Type::Builtin:
4943 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4944 case clang::BuiltinType::Void:
4947 case clang::BuiltinType::Bool:
4948 case clang::BuiltinType::Char_S:
4949 case clang::BuiltinType::SChar:
4950 case clang::BuiltinType::WChar_S:
4951 case clang::BuiltinType::Char16:
4952 case clang::BuiltinType::Char32:
4953 case clang::BuiltinType::Short:
4954 case clang::BuiltinType::Int:
4955 case clang::BuiltinType::Long:
4956 case clang::BuiltinType::LongLong:
4957 case clang::BuiltinType::Int128:
4958 return lldb::eEncodingSint;
4960 case clang::BuiltinType::Char_U:
4961 case clang::BuiltinType::UChar:
4962 case clang::BuiltinType::WChar_U:
4963 case clang::BuiltinType::UShort:
4964 case clang::BuiltinType::UInt:
4965 case clang::BuiltinType::ULong:
4966 case clang::BuiltinType::ULongLong:
4967 case clang::BuiltinType::UInt128:
4968 return lldb::eEncodingUint;
4970 case clang::BuiltinType::Half:
4971 case clang::BuiltinType::Float:
4972 case clang::BuiltinType::Float128:
4973 case clang::BuiltinType::Double:
4974 case clang::BuiltinType::LongDouble:
4975 return lldb::eEncodingIEEE754;
4977 case clang::BuiltinType::ObjCClass:
4978 case clang::BuiltinType::ObjCId:
4979 case clang::BuiltinType::ObjCSel:
4980 return lldb::eEncodingUint;
4982 case clang::BuiltinType::NullPtr:
4983 return lldb::eEncodingUint;
4985 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4986 case clang::BuiltinType::Kind::BoundMember:
4987 case clang::BuiltinType::Kind::BuiltinFn:
4988 case clang::BuiltinType::Kind::Dependent:
4989 case clang::BuiltinType::Kind::OCLClkEvent:
4990 case clang::BuiltinType::Kind::OCLEvent:
4991 case clang::BuiltinType::Kind::OCLImage1dRO:
4992 case clang::BuiltinType::Kind::OCLImage1dWO:
4993 case clang::BuiltinType::Kind::OCLImage1dRW:
4994 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4995 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4996 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4997 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4998 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4999 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5000 case clang::BuiltinType::Kind::OCLImage2dRO:
5001 case clang::BuiltinType::Kind::OCLImage2dWO:
5002 case clang::BuiltinType::Kind::OCLImage2dRW:
5003 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5004 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5005 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5006 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5007 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5008 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5009 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5010 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5011 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5012 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5013 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5014 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5015 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5016 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5017 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5018 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5019 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5020 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5021 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5022 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5023 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5024 case clang::BuiltinType::Kind::OCLImage3dRO:
5025 case clang::BuiltinType::Kind::OCLImage3dWO:
5026 case clang::BuiltinType::Kind::OCLImage3dRW:
5027 case clang::BuiltinType::Kind::OCLQueue:
5028 case clang::BuiltinType::Kind::OCLReserveID:
5029 case clang::BuiltinType::Kind::OCLSampler:
5030 case clang::BuiltinType::Kind::OMPArraySection:
5031 case clang::BuiltinType::Kind::Overload:
5032 case clang::BuiltinType::Kind::PseudoObject:
5033 case clang::BuiltinType::Kind::UnknownAny:
5037 // All pointer types are represented as unsigned integer encodings.
5038 // We may nee to add a eEncodingPointer if we ever need to know the
5040 case clang::Type::ObjCObjectPointer:
5041 case clang::Type::BlockPointer:
5042 case clang::Type::Pointer:
5043 case clang::Type::LValueReference:
5044 case clang::Type::RValueReference:
5045 case clang::Type::MemberPointer:
5046 return lldb::eEncodingUint;
5047 case clang::Type::Complex: {
5048 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5049 if (qual_type->isComplexType())
5050 encoding = lldb::eEncodingIEEE754;
5052 const clang::ComplexType *complex_type =
5053 qual_type->getAsComplexIntegerType();
5055 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5056 .GetEncoding(count);
5058 encoding = lldb::eEncodingSint;
5064 case clang::Type::ObjCInterface:
5066 case clang::Type::Record:
5068 case clang::Type::Enum:
5069 return lldb::eEncodingSint;
5070 case clang::Type::Typedef:
5071 return CompilerType(getASTContext(),
5072 llvm::cast<clang::TypedefType>(qual_type)
5074 ->getUnderlyingType())
5075 .GetEncoding(count);
5077 case clang::Type::Auto:
5078 return CompilerType(
5080 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5081 .GetEncoding(count);
5083 case clang::Type::Elaborated:
5084 return CompilerType(
5086 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5087 .GetEncoding(count);
5089 case clang::Type::Paren:
5090 return CompilerType(getASTContext(),
5091 llvm::cast<clang::ParenType>(qual_type)->desugar())
5092 .GetEncoding(count);
5094 case clang::Type::DependentSizedArray:
5095 case clang::Type::DependentSizedExtVector:
5096 case clang::Type::UnresolvedUsing:
5097 case clang::Type::Attributed:
5098 case clang::Type::TemplateTypeParm:
5099 case clang::Type::SubstTemplateTypeParm:
5100 case clang::Type::SubstTemplateTypeParmPack:
5101 case clang::Type::InjectedClassName:
5102 case clang::Type::DependentName:
5103 case clang::Type::DependentTemplateSpecialization:
5104 case clang::Type::PackExpansion:
5105 case clang::Type::ObjCObject:
5107 case clang::Type::TypeOfExpr:
5108 case clang::Type::TypeOf:
5109 case clang::Type::Decltype:
5110 case clang::Type::TemplateSpecialization:
5111 case clang::Type::DeducedTemplateSpecialization:
5112 case clang::Type::Atomic:
5113 case clang::Type::Adjusted:
5114 case clang::Type::Pipe:
5117 // pointer type decayed from an array or function type.
5118 case clang::Type::Decayed:
5120 case clang::Type::ObjCTypeParam:
5124 return lldb::eEncodingInvalid;
5127 lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5129 return lldb::eFormatDefault;
5131 clang::QualType qual_type(GetCanonicalQualType(type));
5133 switch (qual_type->getTypeClass()) {
5134 case clang::Type::UnaryTransform:
5137 case clang::Type::FunctionNoProto:
5138 case clang::Type::FunctionProto:
5141 case clang::Type::IncompleteArray:
5142 case clang::Type::VariableArray:
5145 case clang::Type::ConstantArray:
5146 return lldb::eFormatVoid; // no value
5148 case clang::Type::ExtVector:
5149 case clang::Type::Vector:
5152 case clang::Type::Builtin:
5153 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5154 // default: assert(0 && "Unknown builtin type!");
5155 case clang::BuiltinType::UnknownAny:
5156 case clang::BuiltinType::Void:
5157 case clang::BuiltinType::BoundMember:
5160 case clang::BuiltinType::Bool:
5161 return lldb::eFormatBoolean;
5162 case clang::BuiltinType::Char_S:
5163 case clang::BuiltinType::SChar:
5164 case clang::BuiltinType::WChar_S:
5165 case clang::BuiltinType::Char_U:
5166 case clang::BuiltinType::UChar:
5167 case clang::BuiltinType::WChar_U:
5168 return lldb::eFormatChar;
5169 case clang::BuiltinType::Char16:
5170 return lldb::eFormatUnicode16;
5171 case clang::BuiltinType::Char32:
5172 return lldb::eFormatUnicode32;
5173 case clang::BuiltinType::UShort:
5174 return lldb::eFormatUnsigned;
5175 case clang::BuiltinType::Short:
5176 return lldb::eFormatDecimal;
5177 case clang::BuiltinType::UInt:
5178 return lldb::eFormatUnsigned;
5179 case clang::BuiltinType::Int:
5180 return lldb::eFormatDecimal;
5181 case clang::BuiltinType::ULong:
5182 return lldb::eFormatUnsigned;
5183 case clang::BuiltinType::Long:
5184 return lldb::eFormatDecimal;
5185 case clang::BuiltinType::ULongLong:
5186 return lldb::eFormatUnsigned;
5187 case clang::BuiltinType::LongLong:
5188 return lldb::eFormatDecimal;
5189 case clang::BuiltinType::UInt128:
5190 return lldb::eFormatUnsigned;
5191 case clang::BuiltinType::Int128:
5192 return lldb::eFormatDecimal;
5193 case clang::BuiltinType::Half:
5194 case clang::BuiltinType::Float:
5195 case clang::BuiltinType::Double:
5196 case clang::BuiltinType::LongDouble:
5197 return lldb::eFormatFloat;
5199 return lldb::eFormatHex;
5202 case clang::Type::ObjCObjectPointer:
5203 return lldb::eFormatHex;
5204 case clang::Type::BlockPointer:
5205 return lldb::eFormatHex;
5206 case clang::Type::Pointer:
5207 return lldb::eFormatHex;
5208 case clang::Type::LValueReference:
5209 case clang::Type::RValueReference:
5210 return lldb::eFormatHex;
5211 case clang::Type::MemberPointer:
5213 case clang::Type::Complex: {
5214 if (qual_type->isComplexType())
5215 return lldb::eFormatComplex;
5217 return lldb::eFormatComplexInteger;
5219 case clang::Type::ObjCInterface:
5221 case clang::Type::Record:
5223 case clang::Type::Enum:
5224 return lldb::eFormatEnum;
5225 case clang::Type::Typedef:
5226 return CompilerType(getASTContext(),
5227 llvm::cast<clang::TypedefType>(qual_type)
5229 ->getUnderlyingType())
5231 case clang::Type::Auto:
5232 return CompilerType(getASTContext(),
5233 llvm::cast<clang::AutoType>(qual_type)->desugar())
5235 case clang::Type::Paren:
5236 return CompilerType(getASTContext(),
5237 llvm::cast<clang::ParenType>(qual_type)->desugar())
5239 case clang::Type::Elaborated:
5240 return CompilerType(
5242 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5244 case clang::Type::DependentSizedArray:
5245 case clang::Type::DependentSizedExtVector:
5246 case clang::Type::UnresolvedUsing:
5247 case clang::Type::Attributed:
5248 case clang::Type::TemplateTypeParm:
5249 case clang::Type::SubstTemplateTypeParm:
5250 case clang::Type::SubstTemplateTypeParmPack:
5251 case clang::Type::InjectedClassName:
5252 case clang::Type::DependentName:
5253 case clang::Type::DependentTemplateSpecialization:
5254 case clang::Type::PackExpansion:
5255 case clang::Type::ObjCObject:
5257 case clang::Type::TypeOfExpr:
5258 case clang::Type::TypeOf:
5259 case clang::Type::Decltype:
5260 case clang::Type::TemplateSpecialization:
5261 case clang::Type::DeducedTemplateSpecialization:
5262 case clang::Type::Atomic:
5263 case clang::Type::Adjusted:
5264 case clang::Type::Pipe:
5267 // pointer type decayed from an array or function type.
5268 case clang::Type::Decayed:
5270 case clang::Type::ObjCTypeParam:
5273 // We don't know hot to display this type...
5274 return lldb::eFormatBytes;
5277 static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5278 bool check_superclass) {
5279 while (class_interface_decl) {
5280 if (class_interface_decl->ivar_size() > 0)
5283 if (check_superclass)
5284 class_interface_decl = class_interface_decl->getSuperClass();
5291 uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5292 bool omit_empty_base_classes) {
5296 uint32_t num_children = 0;
5297 clang::QualType qual_type(GetQualType(type));
5298 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5299 switch (type_class) {
5300 case clang::Type::Builtin:
5301 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5302 case clang::BuiltinType::ObjCId: // child is Class
5303 case clang::BuiltinType::ObjCClass: // child is Class
5312 case clang::Type::Complex:
5315 case clang::Type::Record:
5316 if (GetCompleteQualType(getASTContext(), qual_type)) {
5317 const clang::RecordType *record_type =
5318 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5319 const clang::RecordDecl *record_decl = record_type->getDecl();
5320 assert(record_decl);
5321 const clang::CXXRecordDecl *cxx_record_decl =
5322 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5323 if (cxx_record_decl) {
5324 if (omit_empty_base_classes) {
5325 // Check each base classes to see if it or any of its
5326 // base classes contain any fields. This can help
5327 // limit the noise in variable views by not having to
5328 // show base classes that contain no members.
5329 clang::CXXRecordDecl::base_class_const_iterator base_class,
5331 for (base_class = cxx_record_decl->bases_begin(),
5332 base_class_end = cxx_record_decl->bases_end();
5333 base_class != base_class_end; ++base_class) {
5334 const clang::CXXRecordDecl *base_class_decl =
5335 llvm::cast<clang::CXXRecordDecl>(
5336 base_class->getType()
5337 ->getAs<clang::RecordType>()
5340 // Skip empty base classes
5341 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5347 // Include all base classes
5348 num_children += cxx_record_decl->getNumBases();
5351 clang::RecordDecl::field_iterator field, field_end;
5352 for (field = record_decl->field_begin(),
5353 field_end = record_decl->field_end();
5354 field != field_end; ++field)
5359 case clang::Type::ObjCObject:
5360 case clang::Type::ObjCInterface:
5361 if (GetCompleteQualType(getASTContext(), qual_type)) {
5362 const clang::ObjCObjectType *objc_class_type =
5363 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5364 assert(objc_class_type);
5365 if (objc_class_type) {
5366 clang::ObjCInterfaceDecl *class_interface_decl =
5367 objc_class_type->getInterface();
5369 if (class_interface_decl) {
5371 clang::ObjCInterfaceDecl *superclass_interface_decl =
5372 class_interface_decl->getSuperClass();
5373 if (superclass_interface_decl) {
5374 if (omit_empty_base_classes) {
5375 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5381 num_children += class_interface_decl->ivar_size();
5387 case clang::Type::ObjCObjectPointer: {
5388 const clang::ObjCObjectPointerType *pointer_type =
5389 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5390 clang::QualType pointee_type = pointer_type->getPointeeType();
5391 uint32_t num_pointee_children =
5392 CompilerType(getASTContext(), pointee_type)
5393 .GetNumChildren(omit_empty_base_classes);
5394 // If this type points to a simple type, then it has 1 child
5395 if (num_pointee_children == 0)
5398 num_children = num_pointee_children;
5401 case clang::Type::Vector:
5402 case clang::Type::ExtVector:
5404 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5407 case clang::Type::ConstantArray:
5408 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5413 case clang::Type::Pointer: {
5414 const clang::PointerType *pointer_type =
5415 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5416 clang::QualType pointee_type(pointer_type->getPointeeType());
5417 uint32_t num_pointee_children =
5418 CompilerType(getASTContext(), pointee_type)
5419 .GetNumChildren(omit_empty_base_classes);
5420 if (num_pointee_children == 0) {
5421 // We have a pointer to a pointee type that claims it has no children.
5422 // We will want to look at
5423 num_children = GetNumPointeeChildren(pointee_type);
5425 num_children = num_pointee_children;
5428 case clang::Type::LValueReference:
5429 case clang::Type::RValueReference: {
5430 const clang::ReferenceType *reference_type =
5431 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5432 clang::QualType pointee_type = reference_type->getPointeeType();
5433 uint32_t num_pointee_children =
5434 CompilerType(getASTContext(), pointee_type)
5435 .GetNumChildren(omit_empty_base_classes);
5436 // If this type points to a simple type, then it has 1 child
5437 if (num_pointee_children == 0)
5440 num_children = num_pointee_children;
5443 case clang::Type::Typedef:
5445 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5447 ->getUnderlyingType())
5448 .GetNumChildren(omit_empty_base_classes);
5451 case clang::Type::Auto:
5453 CompilerType(getASTContext(),
5454 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5455 .GetNumChildren(omit_empty_base_classes);
5458 case clang::Type::Elaborated:
5462 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5463 .GetNumChildren(omit_empty_base_classes);
5466 case clang::Type::Paren:
5468 CompilerType(getASTContext(),
5469 llvm::cast<clang::ParenType>(qual_type)->desugar())
5470 .GetNumChildren(omit_empty_base_classes);
5475 return num_children;
5478 CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5479 return GetBasicType(GetBasicTypeEnumeration(name));
5483 ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5485 clang::QualType qual_type(GetQualType(type));
5486 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5487 if (type_class == clang::Type::Builtin) {
5488 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5489 case clang::BuiltinType::Void:
5490 return eBasicTypeVoid;
5491 case clang::BuiltinType::Bool:
5492 return eBasicTypeBool;
5493 case clang::BuiltinType::Char_S:
5494 return eBasicTypeSignedChar;
5495 case clang::BuiltinType::Char_U:
5496 return eBasicTypeUnsignedChar;
5497 case clang::BuiltinType::Char16:
5498 return eBasicTypeChar16;
5499 case clang::BuiltinType::Char32:
5500 return eBasicTypeChar32;
5501 case clang::BuiltinType::UChar:
5502 return eBasicTypeUnsignedChar;
5503 case clang::BuiltinType::SChar:
5504 return eBasicTypeSignedChar;
5505 case clang::BuiltinType::WChar_S:
5506 return eBasicTypeSignedWChar;
5507 case clang::BuiltinType::WChar_U:
5508 return eBasicTypeUnsignedWChar;
5509 case clang::BuiltinType::Short:
5510 return eBasicTypeShort;
5511 case clang::BuiltinType::UShort:
5512 return eBasicTypeUnsignedShort;
5513 case clang::BuiltinType::Int:
5514 return eBasicTypeInt;
5515 case clang::BuiltinType::UInt:
5516 return eBasicTypeUnsignedInt;
5517 case clang::BuiltinType::Long:
5518 return eBasicTypeLong;
5519 case clang::BuiltinType::ULong:
5520 return eBasicTypeUnsignedLong;
5521 case clang::BuiltinType::LongLong:
5522 return eBasicTypeLongLong;
5523 case clang::BuiltinType::ULongLong:
5524 return eBasicTypeUnsignedLongLong;
5525 case clang::BuiltinType::Int128:
5526 return eBasicTypeInt128;
5527 case clang::BuiltinType::UInt128:
5528 return eBasicTypeUnsignedInt128;
5530 case clang::BuiltinType::Half:
5531 return eBasicTypeHalf;
5532 case clang::BuiltinType::Float:
5533 return eBasicTypeFloat;
5534 case clang::BuiltinType::Double:
5535 return eBasicTypeDouble;
5536 case clang::BuiltinType::LongDouble:
5537 return eBasicTypeLongDouble;
5539 case clang::BuiltinType::NullPtr:
5540 return eBasicTypeNullPtr;
5541 case clang::BuiltinType::ObjCId:
5542 return eBasicTypeObjCID;
5543 case clang::BuiltinType::ObjCClass:
5544 return eBasicTypeObjCClass;
5545 case clang::BuiltinType::ObjCSel:
5546 return eBasicTypeObjCSel;
5548 return eBasicTypeOther;
5552 return eBasicTypeInvalid;
5555 void ClangASTContext::ForEachEnumerator(
5556 lldb::opaque_compiler_type_t type,
5557 std::function<bool(const CompilerType &integer_type,
5558 const ConstString &name,
5559 const llvm::APSInt &value)> const &callback) {
5560 const clang::EnumType *enum_type =
5561 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5563 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5565 CompilerType integer_type(this,
5566 enum_decl->getIntegerType().getAsOpaquePtr());
5568 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5569 for (enum_pos = enum_decl->enumerator_begin(),
5570 enum_end_pos = enum_decl->enumerator_end();
5571 enum_pos != enum_end_pos; ++enum_pos) {
5572 ConstString name(enum_pos->getNameAsString().c_str());
5573 if (!callback(integer_type, name, enum_pos->getInitVal()))
5580 #pragma mark Aggregate Types
5582 uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5587 clang::QualType qual_type(GetCanonicalQualType(type));
5588 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5589 switch (type_class) {
5590 case clang::Type::Record:
5591 if (GetCompleteType(type)) {
5592 const clang::RecordType *record_type =
5593 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5595 clang::RecordDecl *record_decl = record_type->getDecl();
5597 uint32_t field_idx = 0;
5598 clang::RecordDecl::field_iterator field, field_end;
5599 for (field = record_decl->field_begin(),
5600 field_end = record_decl->field_end();
5601 field != field_end; ++field)
5609 case clang::Type::Typedef:
5611 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5613 ->getUnderlyingType())
5617 case clang::Type::Auto:
5619 CompilerType(getASTContext(),
5620 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5624 case clang::Type::Elaborated:
5625 count = CompilerType(
5627 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5631 case clang::Type::Paren:
5632 count = CompilerType(getASTContext(),
5633 llvm::cast<clang::ParenType>(qual_type)->desugar())
5637 case clang::Type::ObjCObjectPointer: {
5638 const clang::ObjCObjectPointerType *objc_class_type =
5639 qual_type->getAsObjCInterfacePointerType();
5640 const clang::ObjCInterfaceType *objc_interface_type =
5641 objc_class_type->getInterfaceType();
5642 if (objc_interface_type &&
5643 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5644 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5645 clang::ObjCInterfaceDecl *class_interface_decl =
5646 objc_interface_type->getDecl();
5647 if (class_interface_decl) {
5648 count = class_interface_decl->ivar_size();
5654 case clang::Type::ObjCObject:
5655 case clang::Type::ObjCInterface:
5656 if (GetCompleteType(type)) {
5657 const clang::ObjCObjectType *objc_class_type =
5658 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5659 if (objc_class_type) {
5660 clang::ObjCInterfaceDecl *class_interface_decl =
5661 objc_class_type->getInterface();
5663 if (class_interface_decl)
5664 count = class_interface_decl->ivar_size();
5675 static lldb::opaque_compiler_type_t
5676 GetObjCFieldAtIndex(clang::ASTContext *ast,
5677 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5678 std::string &name, uint64_t *bit_offset_ptr,
5679 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5680 if (class_interface_decl) {
5681 if (idx < (class_interface_decl->ivar_size())) {
5682 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5683 ivar_end = class_interface_decl->ivar_end();
5684 uint32_t ivar_idx = 0;
5686 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5687 ++ivar_pos, ++ivar_idx) {
5688 if (ivar_idx == idx) {
5689 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5691 clang::QualType ivar_qual_type(ivar_decl->getType());
5693 name.assign(ivar_decl->getNameAsString());
5695 if (bit_offset_ptr) {
5696 const clang::ASTRecordLayout &interface_layout =
5697 ast->getASTObjCInterfaceLayout(class_interface_decl);
5698 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5701 const bool is_bitfield = ivar_pos->isBitField();
5703 if (bitfield_bit_size_ptr) {
5704 *bitfield_bit_size_ptr = 0;
5706 if (is_bitfield && ast) {
5707 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5708 llvm::APSInt bitfield_apsint;
5709 if (bitfield_bit_size_expr &&
5710 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5712 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5716 if (is_bitfield_ptr)
5717 *is_bitfield_ptr = is_bitfield;
5719 return ivar_qual_type.getAsOpaquePtr();
5727 CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5728 size_t idx, std::string &name,
5729 uint64_t *bit_offset_ptr,
5730 uint32_t *bitfield_bit_size_ptr,
5731 bool *is_bitfield_ptr) {
5733 return CompilerType();
5735 clang::QualType qual_type(GetCanonicalQualType(type));
5736 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5737 switch (type_class) {
5738 case clang::Type::Record:
5739 if (GetCompleteType(type)) {
5740 const clang::RecordType *record_type =
5741 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5742 const clang::RecordDecl *record_decl = record_type->getDecl();
5743 uint32_t field_idx = 0;
5744 clang::RecordDecl::field_iterator field, field_end;
5745 for (field = record_decl->field_begin(),
5746 field_end = record_decl->field_end();
5747 field != field_end; ++field, ++field_idx) {
5748 if (idx == field_idx) {
5749 // Print the member type if requested
5750 // Print the member name and equal sign
5751 name.assign(field->getNameAsString());
5753 // Figure out the type byte size (field_type_info.first) and
5754 // alignment (field_type_info.second) from the AST context.
5755 if (bit_offset_ptr) {
5756 const clang::ASTRecordLayout &record_layout =
5757 getASTContext()->getASTRecordLayout(record_decl);
5758 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5761 const bool is_bitfield = field->isBitField();
5763 if (bitfield_bit_size_ptr) {
5764 *bitfield_bit_size_ptr = 0;
5767 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5768 llvm::APSInt bitfield_apsint;
5769 if (bitfield_bit_size_expr &&
5770 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5771 *getASTContext())) {
5772 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5776 if (is_bitfield_ptr)
5777 *is_bitfield_ptr = is_bitfield;
5779 return CompilerType(getASTContext(), field->getType());
5785 case clang::Type::ObjCObjectPointer: {
5786 const clang::ObjCObjectPointerType *objc_class_type =
5787 qual_type->getAsObjCInterfacePointerType();
5788 const clang::ObjCInterfaceType *objc_interface_type =
5789 objc_class_type->getInterfaceType();
5790 if (objc_interface_type &&
5791 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5792 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5793 clang::ObjCInterfaceDecl *class_interface_decl =
5794 objc_interface_type->getDecl();
5795 if (class_interface_decl) {
5796 return CompilerType(
5797 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5798 idx, name, bit_offset_ptr,
5799 bitfield_bit_size_ptr, is_bitfield_ptr));
5805 case clang::Type::ObjCObject:
5806 case clang::Type::ObjCInterface:
5807 if (GetCompleteType(type)) {
5808 const clang::ObjCObjectType *objc_class_type =
5809 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5810 assert(objc_class_type);
5811 if (objc_class_type) {
5812 clang::ObjCInterfaceDecl *class_interface_decl =
5813 objc_class_type->getInterface();
5814 return CompilerType(
5815 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5816 idx, name, bit_offset_ptr,
5817 bitfield_bit_size_ptr, is_bitfield_ptr));
5822 case clang::Type::Typedef:
5823 return CompilerType(getASTContext(),
5824 llvm::cast<clang::TypedefType>(qual_type)
5826 ->getUnderlyingType())
5827 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5830 case clang::Type::Auto:
5831 return CompilerType(
5833 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5834 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5837 case clang::Type::Elaborated:
5838 return CompilerType(
5840 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5841 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5844 case clang::Type::Paren:
5845 return CompilerType(getASTContext(),
5846 llvm::cast<clang::ParenType>(qual_type)->desugar())
5847 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5853 return CompilerType();
5857 ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5859 clang::QualType qual_type(GetCanonicalQualType(type));
5860 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5861 switch (type_class) {
5862 case clang::Type::Record:
5863 if (GetCompleteType(type)) {
5864 const clang::CXXRecordDecl *cxx_record_decl =
5865 qual_type->getAsCXXRecordDecl();
5866 if (cxx_record_decl)
5867 count = cxx_record_decl->getNumBases();
5871 case clang::Type::ObjCObjectPointer:
5872 count = GetPointeeType(type).GetNumDirectBaseClasses();
5875 case clang::Type::ObjCObject:
5876 if (GetCompleteType(type)) {
5877 const clang::ObjCObjectType *objc_class_type =
5878 qual_type->getAsObjCQualifiedInterfaceType();
5879 if (objc_class_type) {
5880 clang::ObjCInterfaceDecl *class_interface_decl =
5881 objc_class_type->getInterface();
5883 if (class_interface_decl && class_interface_decl->getSuperClass())
5888 case clang::Type::ObjCInterface:
5889 if (GetCompleteType(type)) {
5890 const clang::ObjCInterfaceType *objc_interface_type =
5891 qual_type->getAs<clang::ObjCInterfaceType>();
5892 if (objc_interface_type) {
5893 clang::ObjCInterfaceDecl *class_interface_decl =
5894 objc_interface_type->getInterface();
5896 if (class_interface_decl && class_interface_decl->getSuperClass())
5902 case clang::Type::Typedef:
5903 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5905 ->getUnderlyingType()
5909 case clang::Type::Auto:
5910 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5915 case clang::Type::Elaborated:
5916 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5921 case clang::Type::Paren:
5922 return GetNumDirectBaseClasses(
5923 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5932 ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5934 clang::QualType qual_type(GetCanonicalQualType(type));
5935 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5936 switch (type_class) {
5937 case clang::Type::Record:
5938 if (GetCompleteType(type)) {
5939 const clang::CXXRecordDecl *cxx_record_decl =
5940 qual_type->getAsCXXRecordDecl();
5941 if (cxx_record_decl)
5942 count = cxx_record_decl->getNumVBases();
5946 case clang::Type::Typedef:
5947 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5949 ->getUnderlyingType()
5953 case clang::Type::Auto:
5954 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5959 case clang::Type::Elaborated:
5961 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5966 case clang::Type::Paren:
5967 count = GetNumVirtualBaseClasses(
5968 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5977 CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5978 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5979 clang::QualType qual_type(GetCanonicalQualType(type));
5980 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5981 switch (type_class) {
5982 case clang::Type::Record:
5983 if (GetCompleteType(type)) {
5984 const clang::CXXRecordDecl *cxx_record_decl =
5985 qual_type->getAsCXXRecordDecl();
5986 if (cxx_record_decl) {
5987 uint32_t curr_idx = 0;
5988 clang::CXXRecordDecl::base_class_const_iterator base_class,
5990 for (base_class = cxx_record_decl->bases_begin(),
5991 base_class_end = cxx_record_decl->bases_end();
5992 base_class != base_class_end; ++base_class, ++curr_idx) {
5993 if (curr_idx == idx) {
5994 if (bit_offset_ptr) {
5995 const clang::ASTRecordLayout &record_layout =
5996 getASTContext()->getASTRecordLayout(cxx_record_decl);
5997 const clang::CXXRecordDecl *base_class_decl =
5998 llvm::cast<clang::CXXRecordDecl>(
5999 base_class->getType()
6000 ->getAs<clang::RecordType>()
6002 if (base_class->isVirtual())
6004 record_layout.getVBaseClassOffset(base_class_decl)
6009 record_layout.getBaseClassOffset(base_class_decl)
6013 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6020 case clang::Type::ObjCObjectPointer:
6021 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6023 case clang::Type::ObjCObject:
6024 if (idx == 0 && GetCompleteType(type)) {
6025 const clang::ObjCObjectType *objc_class_type =
6026 qual_type->getAsObjCQualifiedInterfaceType();
6027 if (objc_class_type) {
6028 clang::ObjCInterfaceDecl *class_interface_decl =
6029 objc_class_type->getInterface();
6031 if (class_interface_decl) {
6032 clang::ObjCInterfaceDecl *superclass_interface_decl =
6033 class_interface_decl->getSuperClass();
6034 if (superclass_interface_decl) {
6036 *bit_offset_ptr = 0;
6037 return CompilerType(getASTContext(),
6038 getASTContext()->getObjCInterfaceType(
6039 superclass_interface_decl));
6045 case clang::Type::ObjCInterface:
6046 if (idx == 0 && GetCompleteType(type)) {
6047 const clang::ObjCObjectType *objc_interface_type =
6048 qual_type->getAs<clang::ObjCInterfaceType>();
6049 if (objc_interface_type) {
6050 clang::ObjCInterfaceDecl *class_interface_decl =
6051 objc_interface_type->getInterface();
6053 if (class_interface_decl) {
6054 clang::ObjCInterfaceDecl *superclass_interface_decl =
6055 class_interface_decl->getSuperClass();
6056 if (superclass_interface_decl) {
6058 *bit_offset_ptr = 0;
6059 return CompilerType(getASTContext(),
6060 getASTContext()->getObjCInterfaceType(
6061 superclass_interface_decl));
6068 case clang::Type::Typedef:
6069 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6071 ->getUnderlyingType()
6073 idx, bit_offset_ptr);
6075 case clang::Type::Auto:
6076 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6079 idx, bit_offset_ptr);
6081 case clang::Type::Elaborated:
6082 return GetDirectBaseClassAtIndex(
6083 llvm::cast<clang::ElaboratedType>(qual_type)
6086 idx, bit_offset_ptr);
6088 case clang::Type::Paren:
6089 return GetDirectBaseClassAtIndex(
6090 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6091 idx, bit_offset_ptr);
6096 return CompilerType();
6099 CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6100 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6101 clang::QualType qual_type(GetCanonicalQualType(type));
6102 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6103 switch (type_class) {
6104 case clang::Type::Record:
6105 if (GetCompleteType(type)) {
6106 const clang::CXXRecordDecl *cxx_record_decl =
6107 qual_type->getAsCXXRecordDecl();
6108 if (cxx_record_decl) {
6109 uint32_t curr_idx = 0;
6110 clang::CXXRecordDecl::base_class_const_iterator base_class,
6112 for (base_class = cxx_record_decl->vbases_begin(),
6113 base_class_end = cxx_record_decl->vbases_end();
6114 base_class != base_class_end; ++base_class, ++curr_idx) {
6115 if (curr_idx == idx) {
6116 if (bit_offset_ptr) {
6117 const clang::ASTRecordLayout &record_layout =
6118 getASTContext()->getASTRecordLayout(cxx_record_decl);
6119 const clang::CXXRecordDecl *base_class_decl =
6120 llvm::cast<clang::CXXRecordDecl>(
6121 base_class->getType()
6122 ->getAs<clang::RecordType>()
6125 record_layout.getVBaseClassOffset(base_class_decl)
6129 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6136 case clang::Type::Typedef:
6137 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6139 ->getUnderlyingType()
6141 idx, bit_offset_ptr);
6143 case clang::Type::Auto:
6144 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6147 idx, bit_offset_ptr);
6149 case clang::Type::Elaborated:
6150 return GetVirtualBaseClassAtIndex(
6151 llvm::cast<clang::ElaboratedType>(qual_type)
6154 idx, bit_offset_ptr);
6156 case clang::Type::Paren:
6157 return GetVirtualBaseClassAtIndex(
6158 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6159 idx, bit_offset_ptr);
6164 return CompilerType();
6167 // If a pointer to a pointee type (the clang_type arg) says that it has no
6168 // children, then we either need to trust it, or override it and return a
6169 // different result. For example, an "int *" has one child that is an integer,
6170 // but a function pointer doesn't have any children. Likewise if a Record type
6171 // claims it has no children, then there really is nothing to show.
6172 uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6176 clang::QualType qual_type(type.getCanonicalType());
6177 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6178 switch (type_class) {
6179 case clang::Type::Builtin:
6180 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6181 case clang::BuiltinType::UnknownAny:
6182 case clang::BuiltinType::Void:
6183 case clang::BuiltinType::NullPtr:
6184 case clang::BuiltinType::OCLEvent:
6185 case clang::BuiltinType::OCLImage1dRO:
6186 case clang::BuiltinType::OCLImage1dWO:
6187 case clang::BuiltinType::OCLImage1dRW:
6188 case clang::BuiltinType::OCLImage1dArrayRO:
6189 case clang::BuiltinType::OCLImage1dArrayWO:
6190 case clang::BuiltinType::OCLImage1dArrayRW:
6191 case clang::BuiltinType::OCLImage1dBufferRO:
6192 case clang::BuiltinType::OCLImage1dBufferWO:
6193 case clang::BuiltinType::OCLImage1dBufferRW:
6194 case clang::BuiltinType::OCLImage2dRO:
6195 case clang::BuiltinType::OCLImage2dWO:
6196 case clang::BuiltinType::OCLImage2dRW:
6197 case clang::BuiltinType::OCLImage2dArrayRO:
6198 case clang::BuiltinType::OCLImage2dArrayWO:
6199 case clang::BuiltinType::OCLImage2dArrayRW:
6200 case clang::BuiltinType::OCLImage3dRO:
6201 case clang::BuiltinType::OCLImage3dWO:
6202 case clang::BuiltinType::OCLImage3dRW:
6203 case clang::BuiltinType::OCLSampler:
6205 case clang::BuiltinType::Bool:
6206 case clang::BuiltinType::Char_U:
6207 case clang::BuiltinType::UChar:
6208 case clang::BuiltinType::WChar_U:
6209 case clang::BuiltinType::Char16:
6210 case clang::BuiltinType::Char32:
6211 case clang::BuiltinType::UShort:
6212 case clang::BuiltinType::UInt:
6213 case clang::BuiltinType::ULong:
6214 case clang::BuiltinType::ULongLong:
6215 case clang::BuiltinType::UInt128:
6216 case clang::BuiltinType::Char_S:
6217 case clang::BuiltinType::SChar:
6218 case clang::BuiltinType::WChar_S:
6219 case clang::BuiltinType::Short:
6220 case clang::BuiltinType::Int:
6221 case clang::BuiltinType::Long:
6222 case clang::BuiltinType::LongLong:
6223 case clang::BuiltinType::Int128:
6224 case clang::BuiltinType::Float:
6225 case clang::BuiltinType::Double:
6226 case clang::BuiltinType::LongDouble:
6227 case clang::BuiltinType::Dependent:
6228 case clang::BuiltinType::Overload:
6229 case clang::BuiltinType::ObjCId:
6230 case clang::BuiltinType::ObjCClass:
6231 case clang::BuiltinType::ObjCSel:
6232 case clang::BuiltinType::BoundMember:
6233 case clang::BuiltinType::Half:
6234 case clang::BuiltinType::ARCUnbridgedCast:
6235 case clang::BuiltinType::PseudoObject:
6236 case clang::BuiltinType::BuiltinFn:
6237 case clang::BuiltinType::OMPArraySection:
6244 case clang::Type::Complex:
6246 case clang::Type::Pointer:
6248 case clang::Type::BlockPointer:
6249 return 0; // If block pointers don't have debug info, then no children for
6251 case clang::Type::LValueReference:
6253 case clang::Type::RValueReference:
6255 case clang::Type::MemberPointer:
6257 case clang::Type::ConstantArray:
6259 case clang::Type::IncompleteArray:
6261 case clang::Type::VariableArray:
6263 case clang::Type::DependentSizedArray:
6265 case clang::Type::DependentSizedExtVector:
6267 case clang::Type::Vector:
6269 case clang::Type::ExtVector:
6271 case clang::Type::FunctionProto:
6272 return 0; // When we function pointers, they have no children...
6273 case clang::Type::FunctionNoProto:
6274 return 0; // When we function pointers, they have no children...
6275 case clang::Type::UnresolvedUsing:
6277 case clang::Type::Paren:
6278 return GetNumPointeeChildren(
6279 llvm::cast<clang::ParenType>(qual_type)->desugar());
6280 case clang::Type::Typedef:
6281 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6283 ->getUnderlyingType());
6284 case clang::Type::Auto:
6285 return GetNumPointeeChildren(
6286 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6287 case clang::Type::Elaborated:
6288 return GetNumPointeeChildren(
6289 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6290 case clang::Type::TypeOfExpr:
6292 case clang::Type::TypeOf:
6294 case clang::Type::Decltype:
6296 case clang::Type::Record:
6298 case clang::Type::Enum:
6300 case clang::Type::TemplateTypeParm:
6302 case clang::Type::SubstTemplateTypeParm:
6304 case clang::Type::TemplateSpecialization:
6306 case clang::Type::InjectedClassName:
6308 case clang::Type::DependentName:
6310 case clang::Type::DependentTemplateSpecialization:
6312 case clang::Type::ObjCObject:
6314 case clang::Type::ObjCInterface:
6316 case clang::Type::ObjCObjectPointer:
6324 CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6325 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6326 bool transparent_pointers, bool omit_empty_base_classes,
6327 bool ignore_array_bounds, std::string &child_name,
6328 uint32_t &child_byte_size, int32_t &child_byte_offset,
6329 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6330 bool &child_is_base_class, bool &child_is_deref_of_parent,
6331 ValueObject *valobj, uint64_t &language_flags) {
6333 return CompilerType();
6335 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6336 const clang::Type::TypeClass parent_type_class =
6337 parent_qual_type->getTypeClass();
6338 child_bitfield_bit_size = 0;
6339 child_bitfield_bit_offset = 0;
6340 child_is_base_class = false;
6343 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6344 uint32_t bit_offset;
6345 switch (parent_type_class) {
6346 case clang::Type::Builtin:
6348 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6349 case clang::BuiltinType::ObjCId:
6350 case clang::BuiltinType::ObjCClass:
6353 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6355 return CompilerType(getASTContext(),
6356 getASTContext()->ObjCBuiltinClassTy);
6364 case clang::Type::Record:
6365 if (idx_is_valid && GetCompleteType(type)) {
6366 const clang::RecordType *record_type =
6367 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6368 const clang::RecordDecl *record_decl = record_type->getDecl();
6369 assert(record_decl);
6370 const clang::ASTRecordLayout &record_layout =
6371 getASTContext()->getASTRecordLayout(record_decl);
6372 uint32_t child_idx = 0;
6374 const clang::CXXRecordDecl *cxx_record_decl =
6375 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6376 if (cxx_record_decl) {
6377 // We might have base classes to print out first
6378 clang::CXXRecordDecl::base_class_const_iterator base_class,
6380 for (base_class = cxx_record_decl->bases_begin(),
6381 base_class_end = cxx_record_decl->bases_end();
6382 base_class != base_class_end; ++base_class) {
6383 const clang::CXXRecordDecl *base_class_decl = nullptr;
6385 // Skip empty base classes
6386 if (omit_empty_base_classes) {
6387 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6388 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6389 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6393 if (idx == child_idx) {
6394 if (base_class_decl == nullptr)
6395 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6396 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6398 if (base_class->isVirtual()) {
6399 bool handled = false;
6402 AddressType addr_type = eAddressTypeInvalid;
6403 lldb::addr_t vtable_ptr_addr =
6404 valobj->GetCPPVTableAddress(addr_type);
6406 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6407 addr_type == eAddressTypeLoad) {
6409 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6410 Process *process = exe_ctx.GetProcessPtr();
6412 clang::VTableContextBase *vtable_ctx =
6413 getASTContext()->getVTableContext();
6415 if (vtable_ctx->isMicrosoft()) {
6416 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6417 static_cast<clang::MicrosoftVTableContext *>(
6420 if (vtable_ptr_addr) {
6421 const lldb::addr_t vbtable_ptr_addr =
6423 record_layout.getVBPtrOffset().getQuantity();
6425 const lldb::addr_t vbtable_ptr =
6426 process->ReadPointerFromMemory(vbtable_ptr_addr,
6428 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6429 // Get the index into the virtual base table. The
6430 // index is the index in uint32_t from vbtable_ptr
6431 const unsigned vbtable_index =
6432 msoft_vtable_ctx->getVBTableIndex(
6433 cxx_record_decl, base_class_decl);
6434 const lldb::addr_t base_offset_addr =
6435 vbtable_ptr + vbtable_index * 4;
6436 const uint32_t base_offset =
6437 process->ReadUnsignedIntegerFromMemory(
6438 base_offset_addr, 4, UINT32_MAX, err);
6439 if (base_offset != UINT32_MAX) {
6441 bit_offset = base_offset * 8;
6446 clang::ItaniumVTableContext *itanium_vtable_ctx =
6447 static_cast<clang::ItaniumVTableContext *>(
6449 if (vtable_ptr_addr) {
6450 const lldb::addr_t vtable_ptr =
6451 process->ReadPointerFromMemory(vtable_ptr_addr,
6453 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6454 clang::CharUnits base_offset_offset =
6455 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6456 cxx_record_decl, base_class_decl);
6457 const lldb::addr_t base_offset_addr =
6458 vtable_ptr + base_offset_offset.getQuantity();
6459 const uint32_t base_offset_size =
6460 process->GetAddressByteSize();
6461 const uint64_t base_offset =
6462 process->ReadUnsignedIntegerFromMemory(
6463 base_offset_addr, base_offset_size,
6465 if (base_offset < UINT32_MAX) {
6467 bit_offset = base_offset * 8;
6477 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6481 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6485 // Base classes should be a multiple of 8 bits in size
6486 child_byte_offset = bit_offset / 8;
6487 CompilerType base_class_clang_type(getASTContext(),
6488 base_class->getType());
6489 child_name = base_class_clang_type.GetTypeName().AsCString("");
6490 uint64_t base_class_clang_type_bit_size =
6491 base_class_clang_type.GetBitSize(
6492 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6494 // Base classes bit sizes should be a multiple of 8 bits in size
6495 assert(base_class_clang_type_bit_size % 8 == 0);
6496 child_byte_size = base_class_clang_type_bit_size / 8;
6497 child_is_base_class = true;
6498 return base_class_clang_type;
6500 // We don't increment the child index in the for loop since we might
6501 // be skipping empty base classes
6505 // Make sure index is in range...
6506 uint32_t field_idx = 0;
6507 clang::RecordDecl::field_iterator field, field_end;
6508 for (field = record_decl->field_begin(),
6509 field_end = record_decl->field_end();
6510 field != field_end; ++field, ++field_idx, ++child_idx) {
6511 if (idx == child_idx) {
6512 // Print the member type if requested
6513 // Print the member name and equal sign
6514 child_name.assign(field->getNameAsString());
6516 // Figure out the type byte size (field_type_info.first) and
6517 // alignment (field_type_info.second) from the AST context.
6518 CompilerType field_clang_type(getASTContext(), field->getType());
6519 assert(field_idx < record_layout.getFieldCount());
6520 child_byte_size = field_clang_type.GetByteSize(
6521 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6522 const uint32_t child_bit_size = child_byte_size * 8;
6524 // Figure out the field offset within the current struct/union/class
6526 bit_offset = record_layout.getFieldOffset(field_idx);
6527 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6528 child_bitfield_bit_size)) {
6529 child_bitfield_bit_offset = bit_offset % child_bit_size;
6530 const uint32_t child_bit_offset =
6531 bit_offset - child_bitfield_bit_offset;
6532 child_byte_offset = child_bit_offset / 8;
6534 child_byte_offset = bit_offset / 8;
6537 return field_clang_type;
6543 case clang::Type::ObjCObject:
6544 case clang::Type::ObjCInterface:
6545 if (idx_is_valid && GetCompleteType(type)) {
6546 const clang::ObjCObjectType *objc_class_type =
6547 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6548 assert(objc_class_type);
6549 if (objc_class_type) {
6550 uint32_t child_idx = 0;
6551 clang::ObjCInterfaceDecl *class_interface_decl =
6552 objc_class_type->getInterface();
6554 if (class_interface_decl) {
6556 const clang::ASTRecordLayout &interface_layout =
6557 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6558 clang::ObjCInterfaceDecl *superclass_interface_decl =
6559 class_interface_decl->getSuperClass();
6560 if (superclass_interface_decl) {
6561 if (omit_empty_base_classes) {
6562 CompilerType base_class_clang_type(
6563 getASTContext(), getASTContext()->getObjCInterfaceType(
6564 superclass_interface_decl));
6565 if (base_class_clang_type.GetNumChildren(
6566 omit_empty_base_classes) > 0) {
6568 clang::QualType ivar_qual_type(
6569 getASTContext()->getObjCInterfaceType(
6570 superclass_interface_decl));
6573 superclass_interface_decl->getNameAsString());
6575 clang::TypeInfo ivar_type_info =
6576 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6578 child_byte_size = ivar_type_info.Width / 8;
6579 child_byte_offset = 0;
6580 child_is_base_class = true;
6582 return CompilerType(getASTContext(), ivar_qual_type);
6591 const uint32_t superclass_idx = child_idx;
6593 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6594 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6595 ivar_end = class_interface_decl->ivar_end();
6597 for (ivar_pos = class_interface_decl->ivar_begin();
6598 ivar_pos != ivar_end; ++ivar_pos) {
6599 if (child_idx == idx) {
6600 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6602 clang::QualType ivar_qual_type(ivar_decl->getType());
6604 child_name.assign(ivar_decl->getNameAsString());
6606 clang::TypeInfo ivar_type_info =
6607 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6609 child_byte_size = ivar_type_info.Width / 8;
6611 // Figure out the field offset within the current
6612 // struct/union/class type
6613 // For ObjC objects, we can't trust the bit offset we get from
6614 // the Clang AST, since
6615 // that doesn't account for the space taken up by unbacked
6616 // properties, or from
6617 // the changing size of base classes that are newer than this
6619 // So if we have a process around that we can ask about this
6621 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6622 Process *process = nullptr;
6624 process = exe_ctx->GetProcessPtr();
6626 ObjCLanguageRuntime *objc_runtime =
6627 process->GetObjCLanguageRuntime();
6628 if (objc_runtime != nullptr) {
6629 CompilerType parent_ast_type(getASTContext(),
6631 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6632 parent_ast_type, ivar_decl->getNameAsString().c_str());
6636 // Setting this to UINT32_MAX to make sure we don't compute it
6638 bit_offset = UINT32_MAX;
6640 if (child_byte_offset ==
6641 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6642 bit_offset = interface_layout.getFieldOffset(child_idx -
6644 child_byte_offset = bit_offset / 8;
6647 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6648 // account for the bit offset
6649 // of a bitfield within its containing object. So regardless of
6650 // where we get the byte
6651 // offset from, we still need to get the bit offset for
6652 // bitfields from the layout.
6654 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6655 child_bitfield_bit_size)) {
6656 if (bit_offset == UINT32_MAX)
6657 bit_offset = interface_layout.getFieldOffset(
6658 child_idx - superclass_idx);
6660 child_bitfield_bit_offset = bit_offset % 8;
6662 return CompilerType(getASTContext(), ivar_qual_type);
6672 case clang::Type::ObjCObjectPointer:
6674 CompilerType pointee_clang_type(GetPointeeType(type));
6676 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6677 child_is_deref_of_parent = false;
6678 bool tmp_child_is_deref_of_parent = false;
6679 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6680 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6681 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6682 child_bitfield_bit_size, child_bitfield_bit_offset,
6683 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6686 child_is_deref_of_parent = true;
6687 const char *parent_name =
6688 valobj ? valobj->GetName().GetCString() : NULL;
6690 child_name.assign(1, '*');
6691 child_name += parent_name;
6694 // We have a pointer to an simple type
6695 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6696 child_byte_size = pointee_clang_type.GetByteSize(
6697 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6698 child_byte_offset = 0;
6699 return pointee_clang_type;
6705 case clang::Type::Vector:
6706 case clang::Type::ExtVector:
6708 const clang::VectorType *array =
6709 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6711 CompilerType element_type(getASTContext(), array->getElementType());
6712 if (element_type.GetCompleteType()) {
6713 char element_name[64];
6714 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6715 static_cast<uint64_t>(idx));
6716 child_name.assign(element_name);
6717 child_byte_size = element_type.GetByteSize(
6718 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6719 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6720 return element_type;
6726 case clang::Type::ConstantArray:
6727 case clang::Type::IncompleteArray:
6728 if (ignore_array_bounds || idx_is_valid) {
6729 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6731 CompilerType element_type(getASTContext(), array->getElementType());
6732 if (element_type.GetCompleteType()) {
6733 child_name = llvm::formatv("[{0}]", idx);
6734 child_byte_size = element_type.GetByteSize(
6735 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6736 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6737 return element_type;
6743 case clang::Type::Pointer: {
6744 CompilerType pointee_clang_type(GetPointeeType(type));
6746 // Don't dereference "void *" pointers
6747 if (pointee_clang_type.IsVoidType())
6748 return CompilerType();
6750 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6751 child_is_deref_of_parent = false;
6752 bool tmp_child_is_deref_of_parent = false;
6753 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6754 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6755 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6756 child_bitfield_bit_size, child_bitfield_bit_offset,
6757 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6760 child_is_deref_of_parent = true;
6762 const char *parent_name =
6763 valobj ? valobj->GetName().GetCString() : NULL;
6765 child_name.assign(1, '*');
6766 child_name += parent_name;
6769 // We have a pointer to an simple type
6771 child_byte_size = pointee_clang_type.GetByteSize(
6772 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6773 child_byte_offset = 0;
6774 return pointee_clang_type;
6780 case clang::Type::LValueReference:
6781 case clang::Type::RValueReference:
6783 const clang::ReferenceType *reference_type =
6784 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6785 CompilerType pointee_clang_type(getASTContext(),
6786 reference_type->getPointeeType());
6787 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6788 child_is_deref_of_parent = false;
6789 bool tmp_child_is_deref_of_parent = false;
6790 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6791 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6792 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6793 child_bitfield_bit_size, child_bitfield_bit_offset,
6794 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6797 const char *parent_name =
6798 valobj ? valobj->GetName().GetCString() : NULL;
6800 child_name.assign(1, '&');
6801 child_name += parent_name;
6804 // We have a pointer to an simple type
6806 child_byte_size = pointee_clang_type.GetByteSize(
6807 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6808 child_byte_offset = 0;
6809 return pointee_clang_type;
6815 case clang::Type::Typedef: {
6816 CompilerType typedefed_clang_type(
6817 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6819 ->getUnderlyingType());
6820 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6821 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6822 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6823 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6824 child_is_deref_of_parent, valobj, language_flags);
6827 case clang::Type::Auto: {
6828 CompilerType elaborated_clang_type(
6830 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6831 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6832 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6833 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6834 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6835 child_is_deref_of_parent, valobj, language_flags);
6838 case clang::Type::Elaborated: {
6839 CompilerType elaborated_clang_type(
6841 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6842 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6843 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6844 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6845 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6846 child_is_deref_of_parent, valobj, language_flags);
6849 case clang::Type::Paren: {
6850 CompilerType paren_clang_type(
6852 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6853 return paren_clang_type.GetChildCompilerTypeAtIndex(
6854 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6855 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6856 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6857 child_is_deref_of_parent, valobj, language_flags);
6863 return CompilerType();
6866 static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6867 const clang::CXXBaseSpecifier *base_spec,
6868 bool omit_empty_base_classes) {
6869 uint32_t child_idx = 0;
6871 const clang::CXXRecordDecl *cxx_record_decl =
6872 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6874 // const char *super_name = record_decl->getNameAsCString();
6875 // const char *base_name =
6876 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6877 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6879 if (cxx_record_decl) {
6880 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6881 for (base_class = cxx_record_decl->bases_begin(),
6882 base_class_end = cxx_record_decl->bases_end();
6883 base_class != base_class_end; ++base_class) {
6884 if (omit_empty_base_classes) {
6885 if (BaseSpecifierIsEmpty(base_class))
6889 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6890 // super_name, base_name,
6892 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6895 if (base_class == base_spec)
6904 static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6905 clang::NamedDecl *canonical_decl,
6906 bool omit_empty_base_classes) {
6907 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6908 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6909 omit_empty_base_classes);
6911 clang::RecordDecl::field_iterator field, field_end;
6912 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6913 field != field_end; ++field, ++child_idx) {
6914 if (field->getCanonicalDecl() == canonical_decl)
6921 // Look for a child member (doesn't include base classes, but it does include
6922 // their members) in the type hierarchy. Returns an index path into "clang_type"
6923 // on how to reach the appropriate member.
6942 // If we have a clang type that describes "class C", and we wanted to looked
6945 // With omit_empty_base_classes == false we would get an integer array back
6948 // The first index 1 is the child index for "class A" within class C
6949 // The second index 1 is the child index for "m_b" within class A
6951 // With omit_empty_base_classes == true we would get an integer array back with:
6953 // The first index 0 is the child index for "class A" within class C (since
6954 // class B doesn't have any members it doesn't count)
6955 // The second index 1 is the child index for "m_b" within class A
6957 size_t ClangASTContext::GetIndexOfChildMemberWithName(
6958 lldb::opaque_compiler_type_t type, const char *name,
6959 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6960 if (type && name && name[0]) {
6961 clang::QualType qual_type(GetCanonicalQualType(type));
6962 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6963 switch (type_class) {
6964 case clang::Type::Record:
6965 if (GetCompleteType(type)) {
6966 const clang::RecordType *record_type =
6967 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6968 const clang::RecordDecl *record_decl = record_type->getDecl();
6970 assert(record_decl);
6971 uint32_t child_idx = 0;
6973 const clang::CXXRecordDecl *cxx_record_decl =
6974 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6976 // Try and find a field that matches NAME
6977 clang::RecordDecl::field_iterator field, field_end;
6978 llvm::StringRef name_sref(name);
6979 for (field = record_decl->field_begin(),
6980 field_end = record_decl->field_end();
6981 field != field_end; ++field, ++child_idx) {
6982 llvm::StringRef field_name = field->getName();
6983 if (field_name.empty()) {
6984 CompilerType field_type(getASTContext(), field->getType());
6985 child_indexes.push_back(child_idx);
6986 if (field_type.GetIndexOfChildMemberWithName(
6987 name, omit_empty_base_classes, child_indexes))
6988 return child_indexes.size();
6989 child_indexes.pop_back();
6991 } else if (field_name.equals(name_sref)) {
6992 // We have to add on the number of base classes to this index!
6993 child_indexes.push_back(
6994 child_idx + ClangASTContext::GetNumBaseClasses(
6995 cxx_record_decl, omit_empty_base_classes));
6996 return child_indexes.size();
7000 if (cxx_record_decl) {
7001 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7003 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7005 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7006 // Didn't find things easily, lets let clang do its thang...
7007 clang::IdentifierInfo &ident_ref =
7008 getASTContext()->Idents.get(name_sref);
7009 clang::DeclarationName decl_name(&ident_ref);
7011 clang::CXXBasePaths paths;
7012 if (cxx_record_decl->lookupInBases(
7013 [decl_name](const clang::CXXBaseSpecifier *specifier,
7014 clang::CXXBasePath &path) {
7015 return clang::CXXRecordDecl::FindOrdinaryMember(
7016 specifier, path, decl_name);
7019 clang::CXXBasePaths::const_paths_iterator path,
7020 path_end = paths.end();
7021 for (path = paths.begin(); path != path_end; ++path) {
7022 const size_t num_path_elements = path->size();
7023 for (size_t e = 0; e < num_path_elements; ++e) {
7024 clang::CXXBasePathElement elem = (*path)[e];
7026 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7027 omit_empty_base_classes);
7028 if (child_idx == UINT32_MAX) {
7029 child_indexes.clear();
7032 child_indexes.push_back(child_idx);
7033 parent_record_decl = llvm::cast<clang::RecordDecl>(
7034 elem.Base->getType()
7035 ->getAs<clang::RecordType>()
7039 for (clang::NamedDecl *path_decl : path->Decls) {
7040 child_idx = GetIndexForRecordChild(
7041 parent_record_decl, path_decl, omit_empty_base_classes);
7042 if (child_idx == UINT32_MAX) {
7043 child_indexes.clear();
7046 child_indexes.push_back(child_idx);
7050 return child_indexes.size();
7056 case clang::Type::ObjCObject:
7057 case clang::Type::ObjCInterface:
7058 if (GetCompleteType(type)) {
7059 llvm::StringRef name_sref(name);
7060 const clang::ObjCObjectType *objc_class_type =
7061 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7062 assert(objc_class_type);
7063 if (objc_class_type) {
7064 uint32_t child_idx = 0;
7065 clang::ObjCInterfaceDecl *class_interface_decl =
7066 objc_class_type->getInterface();
7068 if (class_interface_decl) {
7069 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7070 ivar_end = class_interface_decl->ivar_end();
7071 clang::ObjCInterfaceDecl *superclass_interface_decl =
7072 class_interface_decl->getSuperClass();
7074 for (ivar_pos = class_interface_decl->ivar_begin();
7075 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7076 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7078 if (ivar_decl->getName().equals(name_sref)) {
7079 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7080 (omit_empty_base_classes &&
7081 ObjCDeclHasIVars(superclass_interface_decl, true)))
7084 child_indexes.push_back(child_idx);
7085 return child_indexes.size();
7089 if (superclass_interface_decl) {
7090 // The super class index is always zero for ObjC classes,
7091 // so we push it onto the child indexes in case we find
7092 // an ivar in our superclass...
7093 child_indexes.push_back(0);
7095 CompilerType superclass_clang_type(
7096 getASTContext(), getASTContext()->getObjCInterfaceType(
7097 superclass_interface_decl));
7098 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7099 name, omit_empty_base_classes, child_indexes)) {
7100 // We did find an ivar in a superclass so just
7101 // return the results!
7102 return child_indexes.size();
7105 // We didn't find an ivar matching "name" in our
7106 // superclass, pop the superclass zero index that
7107 // we pushed on above.
7108 child_indexes.pop_back();
7115 case clang::Type::ObjCObjectPointer: {
7116 CompilerType objc_object_clang_type(
7118 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7119 ->getPointeeType());
7120 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7121 name, omit_empty_base_classes, child_indexes);
7124 case clang::Type::ConstantArray: {
7125 // const clang::ConstantArrayType *array =
7126 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7127 // const uint64_t element_count =
7128 // array->getSize().getLimitedValue();
7130 // if (idx < element_count)
7132 // std::pair<uint64_t, unsigned> field_type_info =
7133 // ast->getTypeInfo(array->getElementType());
7135 // char element_name[32];
7136 // ::snprintf (element_name, sizeof (element_name),
7137 // "%s[%u]", parent_name ? parent_name : "", idx);
7139 // child_name.assign(element_name);
7140 // assert(field_type_info.first % 8 == 0);
7141 // child_byte_size = field_type_info.first / 8;
7142 // child_byte_offset = idx * child_byte_size;
7143 // return array->getElementType().getAsOpaquePtr();
7147 // case clang::Type::MemberPointerType:
7149 // MemberPointerType *mem_ptr_type =
7150 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7151 // clang::QualType pointee_type =
7152 // mem_ptr_type->getPointeeType();
7154 // if (ClangASTContext::IsAggregateType
7155 // (pointee_type.getAsOpaquePtr()))
7157 // return GetIndexOfChildWithName (ast,
7158 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7164 case clang::Type::LValueReference:
7165 case clang::Type::RValueReference: {
7166 const clang::ReferenceType *reference_type =
7167 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7168 clang::QualType pointee_type(reference_type->getPointeeType());
7169 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7171 if (pointee_clang_type.IsAggregateType()) {
7172 return pointee_clang_type.GetIndexOfChildMemberWithName(
7173 name, omit_empty_base_classes, child_indexes);
7177 case clang::Type::Pointer: {
7178 CompilerType pointee_clang_type(GetPointeeType(type));
7180 if (pointee_clang_type.IsAggregateType()) {
7181 return pointee_clang_type.GetIndexOfChildMemberWithName(
7182 name, omit_empty_base_classes, child_indexes);
7186 case clang::Type::Typedef:
7187 return CompilerType(getASTContext(),
7188 llvm::cast<clang::TypedefType>(qual_type)
7190 ->getUnderlyingType())
7191 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7194 case clang::Type::Auto:
7195 return CompilerType(
7197 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7198 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7201 case clang::Type::Elaborated:
7202 return CompilerType(
7204 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7205 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7208 case clang::Type::Paren:
7209 return CompilerType(getASTContext(),
7210 llvm::cast<clang::ParenType>(qual_type)->desugar())
7211 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7221 // Get the index of the child of "clang_type" whose name matches. This function
7222 // doesn't descend into the children, but only looks one level deep and name
7223 // matches can include base class names.
7226 ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7228 bool omit_empty_base_classes) {
7229 if (type && name && name[0]) {
7230 clang::QualType qual_type(GetCanonicalQualType(type));
7232 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7234 switch (type_class) {
7235 case clang::Type::Record:
7236 if (GetCompleteType(type)) {
7237 const clang::RecordType *record_type =
7238 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7239 const clang::RecordDecl *record_decl = record_type->getDecl();
7241 assert(record_decl);
7242 uint32_t child_idx = 0;
7244 const clang::CXXRecordDecl *cxx_record_decl =
7245 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7247 if (cxx_record_decl) {
7248 clang::CXXRecordDecl::base_class_const_iterator base_class,
7250 for (base_class = cxx_record_decl->bases_begin(),
7251 base_class_end = cxx_record_decl->bases_end();
7252 base_class != base_class_end; ++base_class) {
7253 // Skip empty base classes
7254 clang::CXXRecordDecl *base_class_decl =
7255 llvm::cast<clang::CXXRecordDecl>(
7256 base_class->getType()
7257 ->getAs<clang::RecordType>()
7259 if (omit_empty_base_classes &&
7260 ClangASTContext::RecordHasFields(base_class_decl) == false)
7263 CompilerType base_class_clang_type(getASTContext(),
7264 base_class->getType());
7265 std::string base_class_type_name(
7266 base_class_clang_type.GetTypeName().AsCString(""));
7267 if (base_class_type_name.compare(name) == 0)
7273 // Try and find a field that matches NAME
7274 clang::RecordDecl::field_iterator field, field_end;
7275 llvm::StringRef name_sref(name);
7276 for (field = record_decl->field_begin(),
7277 field_end = record_decl->field_end();
7278 field != field_end; ++field, ++child_idx) {
7279 if (field->getName().equals(name_sref))
7285 case clang::Type::ObjCObject:
7286 case clang::Type::ObjCInterface:
7287 if (GetCompleteType(type)) {
7288 llvm::StringRef name_sref(name);
7289 const clang::ObjCObjectType *objc_class_type =
7290 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7291 assert(objc_class_type);
7292 if (objc_class_type) {
7293 uint32_t child_idx = 0;
7294 clang::ObjCInterfaceDecl *class_interface_decl =
7295 objc_class_type->getInterface();
7297 if (class_interface_decl) {
7298 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7299 ivar_end = class_interface_decl->ivar_end();
7300 clang::ObjCInterfaceDecl *superclass_interface_decl =
7301 class_interface_decl->getSuperClass();
7303 for (ivar_pos = class_interface_decl->ivar_begin();
7304 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7305 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7307 if (ivar_decl->getName().equals(name_sref)) {
7308 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7309 (omit_empty_base_classes &&
7310 ObjCDeclHasIVars(superclass_interface_decl, true)))
7317 if (superclass_interface_decl) {
7318 if (superclass_interface_decl->getName().equals(name_sref))
7326 case clang::Type::ObjCObjectPointer: {
7327 CompilerType pointee_clang_type(
7329 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7330 ->getPointeeType());
7331 return pointee_clang_type.GetIndexOfChildWithName(
7332 name, omit_empty_base_classes);
7335 case clang::Type::ConstantArray: {
7336 // const clang::ConstantArrayType *array =
7337 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7338 // const uint64_t element_count =
7339 // array->getSize().getLimitedValue();
7341 // if (idx < element_count)
7343 // std::pair<uint64_t, unsigned> field_type_info =
7344 // ast->getTypeInfo(array->getElementType());
7346 // char element_name[32];
7347 // ::snprintf (element_name, sizeof (element_name),
7348 // "%s[%u]", parent_name ? parent_name : "", idx);
7350 // child_name.assign(element_name);
7351 // assert(field_type_info.first % 8 == 0);
7352 // child_byte_size = field_type_info.first / 8;
7353 // child_byte_offset = idx * child_byte_size;
7354 // return array->getElementType().getAsOpaquePtr();
7358 // case clang::Type::MemberPointerType:
7360 // MemberPointerType *mem_ptr_type =
7361 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7362 // clang::QualType pointee_type =
7363 // mem_ptr_type->getPointeeType();
7365 // if (ClangASTContext::IsAggregateType
7366 // (pointee_type.getAsOpaquePtr()))
7368 // return GetIndexOfChildWithName (ast,
7369 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7375 case clang::Type::LValueReference:
7376 case clang::Type::RValueReference: {
7377 const clang::ReferenceType *reference_type =
7378 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7379 CompilerType pointee_type(getASTContext(),
7380 reference_type->getPointeeType());
7382 if (pointee_type.IsAggregateType()) {
7383 return pointee_type.GetIndexOfChildWithName(name,
7384 omit_empty_base_classes);
7388 case clang::Type::Pointer: {
7389 const clang::PointerType *pointer_type =
7390 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7391 CompilerType pointee_type(getASTContext(),
7392 pointer_type->getPointeeType());
7394 if (pointee_type.IsAggregateType()) {
7395 return pointee_type.GetIndexOfChildWithName(name,
7396 omit_empty_base_classes);
7400 // child_name.assign(1, '*');
7401 // child_name += parent_name;
7404 // // We have a pointer to an simple type
7407 // std::pair<uint64_t, unsigned> clang_type_info
7408 // = ast->getTypeInfo(pointee_type);
7409 // assert(clang_type_info.first % 8 == 0);
7410 // child_byte_size = clang_type_info.first / 8;
7411 // child_byte_offset = 0;
7412 // return pointee_type.getAsOpaquePtr();
7417 case clang::Type::Auto:
7418 return CompilerType(
7420 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7421 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7423 case clang::Type::Elaborated:
7424 return CompilerType(
7426 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7427 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7429 case clang::Type::Paren:
7430 return CompilerType(getASTContext(),
7431 llvm::cast<clang::ParenType>(qual_type)->desugar())
7432 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7434 case clang::Type::Typedef:
7435 return CompilerType(getASTContext(),
7436 llvm::cast<clang::TypedefType>(qual_type)
7438 ->getUnderlyingType())
7439 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7449 ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7453 clang::QualType qual_type(GetCanonicalQualType(type));
7454 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7455 switch (type_class) {
7456 case clang::Type::Record:
7457 if (GetCompleteType(type)) {
7458 const clang::CXXRecordDecl *cxx_record_decl =
7459 qual_type->getAsCXXRecordDecl();
7460 if (cxx_record_decl) {
7461 const clang::ClassTemplateSpecializationDecl *template_decl =
7462 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7465 return template_decl->getTemplateArgs().size();
7470 case clang::Type::Typedef:
7471 return (CompilerType(getASTContext(),
7472 llvm::cast<clang::TypedefType>(qual_type)
7474 ->getUnderlyingType()))
7475 .GetNumTemplateArguments();
7477 case clang::Type::Auto:
7478 return (CompilerType(
7480 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7481 .GetNumTemplateArguments();
7483 case clang::Type::Elaborated:
7484 return (CompilerType(
7486 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7487 .GetNumTemplateArguments();
7489 case clang::Type::Paren:
7490 return (CompilerType(getASTContext(),
7491 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7492 .GetNumTemplateArguments();
7502 ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7504 lldb::TemplateArgumentKind &kind) {
7506 return CompilerType();
7508 clang::QualType qual_type(GetCanonicalQualType(type));
7509 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7510 switch (type_class) {
7511 case clang::Type::Record:
7512 if (GetCompleteType(type)) {
7513 const clang::CXXRecordDecl *cxx_record_decl =
7514 qual_type->getAsCXXRecordDecl();
7515 if (cxx_record_decl) {
7516 const clang::ClassTemplateSpecializationDecl *template_decl =
7517 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7519 if (template_decl &&
7520 arg_idx < template_decl->getTemplateArgs().size()) {
7521 const clang::TemplateArgument &template_arg =
7522 template_decl->getTemplateArgs()[arg_idx];
7523 switch (template_arg.getKind()) {
7524 case clang::TemplateArgument::Null:
7525 kind = eTemplateArgumentKindNull;
7526 return CompilerType();
7528 case clang::TemplateArgument::Type:
7529 kind = eTemplateArgumentKindType;
7530 return CompilerType(getASTContext(), template_arg.getAsType());
7532 case clang::TemplateArgument::Declaration:
7533 kind = eTemplateArgumentKindDeclaration;
7534 return CompilerType();
7536 case clang::TemplateArgument::Integral:
7537 kind = eTemplateArgumentKindIntegral;
7538 return CompilerType(getASTContext(),
7539 template_arg.getIntegralType());
7541 case clang::TemplateArgument::Template:
7542 kind = eTemplateArgumentKindTemplate;
7543 return CompilerType();
7545 case clang::TemplateArgument::TemplateExpansion:
7546 kind = eTemplateArgumentKindTemplateExpansion;
7547 return CompilerType();
7549 case clang::TemplateArgument::Expression:
7550 kind = eTemplateArgumentKindExpression;
7551 return CompilerType();
7553 case clang::TemplateArgument::Pack:
7554 kind = eTemplateArgumentKindPack;
7555 return CompilerType();
7558 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7565 case clang::Type::Typedef:
7566 return (CompilerType(getASTContext(),
7567 llvm::cast<clang::TypedefType>(qual_type)
7569 ->getUnderlyingType()))
7570 .GetTemplateArgument(arg_idx, kind);
7572 case clang::Type::Auto:
7573 return (CompilerType(
7575 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7576 .GetTemplateArgument(arg_idx, kind);
7578 case clang::Type::Elaborated:
7579 return (CompilerType(
7581 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7582 .GetTemplateArgument(arg_idx, kind);
7584 case clang::Type::Paren:
7585 return (CompilerType(getASTContext(),
7586 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7587 .GetTemplateArgument(arg_idx, kind);
7592 kind = eTemplateArgumentKindNull;
7593 return CompilerType();
7596 CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7598 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7599 return CompilerType();
7602 clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7603 const clang::EnumType *enutype =
7604 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7606 return enutype->getDecl();
7610 clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7611 const clang::RecordType *record_type =
7612 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7614 return record_type->getDecl();
7618 clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7619 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7620 if (qual_type.isNull())
7623 return qual_type->getAsTagDecl();
7626 clang::CXXRecordDecl *
7627 ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7628 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7631 clang::ObjCInterfaceDecl *
7632 ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7633 const clang::ObjCObjectType *objc_class_type =
7634 llvm::dyn_cast<clang::ObjCObjectType>(
7635 ClangUtil::GetCanonicalQualType(type));
7636 if (objc_class_type)
7637 return objc_class_type->getInterface();
7641 clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7642 const CompilerType &type, const char *name,
7643 const CompilerType &field_clang_type, AccessType access,
7644 uint32_t bitfield_bit_size) {
7645 if (!type.IsValid() || !field_clang_type.IsValid())
7647 ClangASTContext *ast =
7648 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7651 clang::ASTContext *clang_ast = ast->getASTContext();
7653 clang::FieldDecl *field = nullptr;
7655 clang::Expr *bit_width = nullptr;
7656 if (bitfield_bit_size != 0) {
7657 llvm::APInt bitfield_bit_size_apint(
7658 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7659 bit_width = new (*clang_ast)
7660 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7661 clang_ast->IntTy, clang::SourceLocation());
7664 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7666 field = clang::FieldDecl::Create(
7667 *clang_ast, record_decl, clang::SourceLocation(),
7668 clang::SourceLocation(),
7669 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7670 ClangUtil::GetQualType(field_clang_type), // Field type
7672 bit_width, // BitWidth
7674 clang::ICIS_NoInit); // HasInit
7677 // Determine whether this field corresponds to an anonymous
7679 if (const clang::TagType *TagT =
7680 field->getType()->getAs<clang::TagType>()) {
7681 if (clang::RecordDecl *Rec =
7682 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7683 if (!Rec->getDeclName()) {
7684 Rec->setAnonymousStructOrUnion(true);
7685 field->setImplicit();
7692 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7694 record_decl->addDecl(field);
7696 #ifdef LLDB_CONFIGURATION_DEBUG
7701 clang::ObjCInterfaceDecl *class_interface_decl =
7702 ast->GetAsObjCInterfaceDecl(type);
7704 if (class_interface_decl) {
7705 const bool is_synthesized = false;
7707 field_clang_type.GetCompleteType();
7709 field = clang::ObjCIvarDecl::Create(
7710 *clang_ast, class_interface_decl, clang::SourceLocation(),
7711 clang::SourceLocation(),
7712 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7713 ClangUtil::GetQualType(field_clang_type), // Field type
7714 nullptr, // TypeSourceInfo *
7715 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7719 class_interface_decl->addDecl(field);
7721 #ifdef LLDB_CONFIGURATION_DEBUG
7730 void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7734 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7738 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7743 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7745 IndirectFieldVector indirect_fields;
7746 clang::RecordDecl::field_iterator field_pos;
7747 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7748 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7749 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7750 last_field_pos = field_pos++) {
7751 if (field_pos->isAnonymousStructOrUnion()) {
7752 clang::QualType field_qual_type = field_pos->getType();
7754 const clang::RecordType *field_record_type =
7755 field_qual_type->getAs<clang::RecordType>();
7757 if (!field_record_type)
7760 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7762 if (!field_record_decl)
7765 for (clang::RecordDecl::decl_iterator
7766 di = field_record_decl->decls_begin(),
7767 de = field_record_decl->decls_end();
7769 if (clang::FieldDecl *nested_field_decl =
7770 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7771 clang::NamedDecl **chain =
7772 new (*ast->getASTContext()) clang::NamedDecl *[2];
7773 chain[0] = *field_pos;
7774 chain[1] = nested_field_decl;
7775 clang::IndirectFieldDecl *indirect_field =
7776 clang::IndirectFieldDecl::Create(
7777 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7778 nested_field_decl->getIdentifier(),
7779 nested_field_decl->getType(), {chain, 2});
7781 indirect_field->setImplicit();
7783 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7784 field_pos->getAccess(), nested_field_decl->getAccess()));
7786 indirect_fields.push_back(indirect_field);
7787 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7788 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7789 size_t nested_chain_size =
7790 nested_indirect_field_decl->getChainingSize();
7791 clang::NamedDecl **chain = new (*ast->getASTContext())
7792 clang::NamedDecl *[nested_chain_size + 1];
7793 chain[0] = *field_pos;
7795 int chain_index = 1;
7796 for (clang::IndirectFieldDecl::chain_iterator
7797 nci = nested_indirect_field_decl->chain_begin(),
7798 nce = nested_indirect_field_decl->chain_end();
7800 chain[chain_index] = *nci;
7804 clang::IndirectFieldDecl *indirect_field =
7805 clang::IndirectFieldDecl::Create(
7806 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7807 nested_indirect_field_decl->getIdentifier(),
7808 nested_indirect_field_decl->getType(),
7809 {chain, nested_chain_size + 1});
7811 indirect_field->setImplicit();
7813 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7814 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7816 indirect_fields.push_back(indirect_field);
7822 // Check the last field to see if it has an incomplete array type as its
7823 // last member and if it does, the tell the record decl about it
7824 if (last_field_pos != field_end_pos) {
7825 if (last_field_pos->getType()->isIncompleteArrayType())
7826 record_decl->hasFlexibleArrayMember();
7829 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7830 ife = indirect_fields.end();
7832 record_decl->addDecl(*ifi);
7836 void ClangASTContext::SetIsPacked(const CompilerType &type) {
7838 ClangASTContext *ast =
7839 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7841 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7846 record_decl->addAttr(
7847 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
7852 clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7853 const CompilerType &type, const char *name, const CompilerType &var_type,
7854 AccessType access) {
7855 clang::VarDecl *var_decl = nullptr;
7857 if (!type.IsValid() || !var_type.IsValid())
7859 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7863 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7865 var_decl = clang::VarDecl::Create(
7866 *ast->getASTContext(), // ASTContext &
7867 record_decl, // DeclContext *
7868 clang::SourceLocation(), // clang::SourceLocation StartLoc
7869 clang::SourceLocation(), // clang::SourceLocation IdLoc
7870 name ? &ast->getASTContext()->Idents.get(name)
7871 : nullptr, // clang::IdentifierInfo *
7872 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7873 nullptr, // TypeSourceInfo *
7874 clang::SC_Static); // StorageClass
7876 var_decl->setAccess(
7877 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7878 record_decl->addDecl(var_decl);
7880 #ifdef LLDB_CONFIGURATION_DEBUG
7881 VerifyDecl(var_decl);
7888 clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7889 lldb::opaque_compiler_type_t type, const char *name,
7890 const CompilerType &method_clang_type, lldb::AccessType access,
7891 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7892 bool is_attr_used, bool is_artificial) {
7893 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7897 clang::QualType record_qual_type(GetCanonicalQualType(type));
7899 clang::CXXRecordDecl *cxx_record_decl =
7900 record_qual_type->getAsCXXRecordDecl();
7902 if (cxx_record_decl == nullptr)
7905 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7907 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7909 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7911 const clang::FunctionType *function_type =
7912 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7914 if (function_type == nullptr)
7917 const clang::FunctionProtoType *method_function_prototype(
7918 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7920 if (!method_function_prototype)
7923 unsigned int num_params = method_function_prototype->getNumParams();
7925 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7926 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7929 return nullptr; // skip everything artificial
7931 if (name[0] == '~') {
7932 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7933 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7934 clang::DeclarationNameInfo(
7935 getASTContext()->DeclarationNames.getCXXDestructorName(
7936 getASTContext()->getCanonicalType(record_qual_type)),
7937 clang::SourceLocation()),
7938 method_qual_type, nullptr, is_inline, is_artificial);
7939 cxx_method_decl = cxx_dtor_decl;
7940 } else if (decl_name == cxx_record_decl->getDeclName()) {
7941 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7942 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7943 clang::DeclarationNameInfo(
7944 getASTContext()->DeclarationNames.getCXXConstructorName(
7945 getASTContext()->getCanonicalType(record_qual_type)),
7946 clang::SourceLocation()),
7948 nullptr, // TypeSourceInfo *
7949 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7950 cxx_method_decl = cxx_ctor_decl;
7952 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7953 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7955 if (IsOperator(name, op_kind)) {
7956 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7957 // Check the number of operator parameters. Sometimes we have
7958 // seen bad DWARF that doesn't correctly describe operators and
7959 // if we try to create a method and add it to the class, clang
7960 // will assert and crash, so we need to make sure things are
7962 const bool is_method = true;
7963 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7964 is_method, op_kind, num_params))
7966 cxx_method_decl = clang::CXXMethodDecl::Create(
7967 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7968 clang::DeclarationNameInfo(
7969 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7970 clang::SourceLocation()),
7972 nullptr, // TypeSourceInfo *
7973 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7974 } else if (num_params == 0) {
7975 // Conversion operators don't take params...
7976 cxx_method_decl = clang::CXXConversionDecl::Create(
7977 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7978 clang::DeclarationNameInfo(
7979 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7980 getASTContext()->getCanonicalType(
7981 function_type->getReturnType())),
7982 clang::SourceLocation()),
7984 nullptr, // TypeSourceInfo *
7985 is_inline, is_explicit, false /*is_constexpr*/,
7986 clang::SourceLocation());
7990 if (cxx_method_decl == nullptr) {
7991 cxx_method_decl = clang::CXXMethodDecl::Create(
7992 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7993 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
7995 nullptr, // TypeSourceInfo *
7996 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8000 clang::AccessSpecifier access_specifier =
8001 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8003 cxx_method_decl->setAccess(access_specifier);
8004 cxx_method_decl->setVirtualAsWritten(is_virtual);
8007 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8009 // Populate the method decl with parameter decls
8011 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8013 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8014 params.push_back(clang::ParmVarDecl::Create(
8015 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8016 clang::SourceLocation(),
8017 nullptr, // anonymous
8018 method_function_prototype->getParamType(param_index), nullptr,
8019 clang::SC_None, nullptr));
8022 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8024 cxx_record_decl->addDecl(cxx_method_decl);
8026 // Sometimes the debug info will mention a constructor (default/copy/move),
8027 // destructor, or assignment operator (copy/move) but there won't be any
8028 // version of this in the code. So we check if the function was artificially
8029 // generated and if it is trivial and this lets the compiler/backend know
8030 // that it can inline the IR for these when it needs to and we can avoid a
8031 // "missing function" error when running expressions.
8033 if (is_artificial) {
8034 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8035 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8036 (cxx_ctor_decl->isCopyConstructor() &&
8037 cxx_record_decl->hasTrivialCopyConstructor()) ||
8038 (cxx_ctor_decl->isMoveConstructor() &&
8039 cxx_record_decl->hasTrivialMoveConstructor()))) {
8040 cxx_ctor_decl->setDefaulted();
8041 cxx_ctor_decl->setTrivial(true);
8042 } else if (cxx_dtor_decl) {
8043 if (cxx_record_decl->hasTrivialDestructor()) {
8044 cxx_dtor_decl->setDefaulted();
8045 cxx_dtor_decl->setTrivial(true);
8047 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8048 cxx_record_decl->hasTrivialCopyAssignment()) ||
8049 (cxx_method_decl->isMoveAssignmentOperator() &&
8050 cxx_record_decl->hasTrivialMoveAssignment())) {
8051 cxx_method_decl->setDefaulted();
8052 cxx_method_decl->setTrivial(true);
8056 #ifdef LLDB_CONFIGURATION_DEBUG
8057 VerifyDecl(cxx_method_decl);
8060 // printf ("decl->isPolymorphic() = %i\n",
8061 // cxx_record_decl->isPolymorphic());
8062 // printf ("decl->isAggregate() = %i\n",
8063 // cxx_record_decl->isAggregate());
8064 // printf ("decl->isPOD() = %i\n",
8065 // cxx_record_decl->isPOD());
8066 // printf ("decl->isEmpty() = %i\n",
8067 // cxx_record_decl->isEmpty());
8068 // printf ("decl->isAbstract() = %i\n",
8069 // cxx_record_decl->isAbstract());
8070 // printf ("decl->hasTrivialConstructor() = %i\n",
8071 // cxx_record_decl->hasTrivialConstructor());
8072 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8073 // cxx_record_decl->hasTrivialCopyConstructor());
8074 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8075 // cxx_record_decl->hasTrivialCopyAssignment());
8076 // printf ("decl->hasTrivialDestructor() = %i\n",
8077 // cxx_record_decl->hasTrivialDestructor());
8078 return cxx_method_decl;
8081 #pragma mark C++ Base Classes
8083 clang::CXXBaseSpecifier *
8084 ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8085 AccessType access, bool is_virtual,
8086 bool base_of_class) {
8088 return new clang::CXXBaseSpecifier(
8089 clang::SourceRange(), is_virtual, base_of_class,
8090 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8091 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8092 clang::SourceLocation());
8096 void ClangASTContext::DeleteBaseClassSpecifiers(
8097 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8098 for (unsigned i = 0; i < num_base_classes; ++i) {
8099 delete base_classes[i];
8100 base_classes[i] = nullptr;
8104 bool ClangASTContext::SetBaseClassesForClassType(
8105 lldb::opaque_compiler_type_t type,
8106 clang::CXXBaseSpecifier const *const *base_classes,
8107 unsigned num_base_classes) {
8109 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8110 if (cxx_record_decl) {
8111 cxx_record_decl->setBases(base_classes, num_base_classes);
8118 bool ClangASTContext::SetObjCSuperClass(
8119 const CompilerType &type, const CompilerType &superclass_clang_type) {
8120 ClangASTContext *ast =
8121 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8124 clang::ASTContext *clang_ast = ast->getASTContext();
8126 if (type && superclass_clang_type.IsValid() &&
8127 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8128 clang::ObjCInterfaceDecl *class_interface_decl =
8129 GetAsObjCInterfaceDecl(type);
8130 clang::ObjCInterfaceDecl *super_interface_decl =
8131 GetAsObjCInterfaceDecl(superclass_clang_type);
8132 if (class_interface_decl && super_interface_decl) {
8133 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8134 clang_ast->getObjCInterfaceType(super_interface_decl)));
8141 bool ClangASTContext::AddObjCClassProperty(
8142 const CompilerType &type, const char *property_name,
8143 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8144 const char *property_setter_name, const char *property_getter_name,
8145 uint32_t property_attributes, ClangASTMetadata *metadata) {
8146 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8147 property_name[0] == '\0')
8149 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8152 clang::ASTContext *clang_ast = ast->getASTContext();
8154 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8156 if (class_interface_decl) {
8157 CompilerType property_clang_type_to_access;
8159 if (property_clang_type.IsValid())
8160 property_clang_type_to_access = property_clang_type;
8162 property_clang_type_to_access =
8163 CompilerType(clang_ast, ivar_decl->getType());
8165 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8166 clang::TypeSourceInfo *prop_type_source;
8169 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8171 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8172 ClangUtil::GetQualType(property_clang_type));
8174 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8175 *clang_ast, class_interface_decl,
8176 clang::SourceLocation(), // Source Location
8177 &clang_ast->Idents.get(property_name),
8178 clang::SourceLocation(), // Source Location for AT
8179 clang::SourceLocation(), // Source location for (
8180 ivar_decl ? ivar_decl->getType()
8181 : ClangUtil::GetQualType(property_clang_type),
8184 if (property_decl) {
8186 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8188 class_interface_decl->addDecl(property_decl);
8190 clang::Selector setter_sel, getter_sel;
8192 if (property_setter_name != nullptr) {
8193 std::string property_setter_no_colon(
8194 property_setter_name, strlen(property_setter_name) - 1);
8195 clang::IdentifierInfo *setter_ident =
8196 &clang_ast->Idents.get(property_setter_no_colon);
8197 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8198 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8199 std::string setter_sel_string("set");
8200 setter_sel_string.push_back(::toupper(property_name[0]));
8201 setter_sel_string.append(&property_name[1]);
8202 clang::IdentifierInfo *setter_ident =
8203 &clang_ast->Idents.get(setter_sel_string);
8204 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8206 property_decl->setSetterName(setter_sel);
8207 property_decl->setPropertyAttributes(
8208 clang::ObjCPropertyDecl::OBJC_PR_setter);
8210 if (property_getter_name != nullptr) {
8211 clang::IdentifierInfo *getter_ident =
8212 &clang_ast->Idents.get(property_getter_name);
8213 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8215 clang::IdentifierInfo *getter_ident =
8216 &clang_ast->Idents.get(property_name);
8217 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8219 property_decl->setGetterName(getter_sel);
8220 property_decl->setPropertyAttributes(
8221 clang::ObjCPropertyDecl::OBJC_PR_getter);
8224 property_decl->setPropertyIvarDecl(ivar_decl);
8226 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8227 property_decl->setPropertyAttributes(
8228 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8229 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8230 property_decl->setPropertyAttributes(
8231 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8232 if (property_attributes & DW_APPLE_PROPERTY_assign)
8233 property_decl->setPropertyAttributes(
8234 clang::ObjCPropertyDecl::OBJC_PR_assign);
8235 if (property_attributes & DW_APPLE_PROPERTY_retain)
8236 property_decl->setPropertyAttributes(
8237 clang::ObjCPropertyDecl::OBJC_PR_retain);
8238 if (property_attributes & DW_APPLE_PROPERTY_copy)
8239 property_decl->setPropertyAttributes(
8240 clang::ObjCPropertyDecl::OBJC_PR_copy);
8241 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8242 property_decl->setPropertyAttributes(
8243 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8244 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8245 property_decl->setPropertyAttributes(
8246 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8247 if (property_attributes &
8248 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8249 property_decl->setPropertyAttributes(
8250 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8251 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8252 property_decl->setPropertyAttributes(
8253 clang::ObjCPropertyDecl::OBJC_PR_class);
8255 const bool isInstance =
8256 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8258 if (!getter_sel.isNull() &&
8260 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8261 : class_interface_decl->lookupClassMethod(getter_sel))) {
8262 const bool isVariadic = false;
8263 const bool isSynthesized = false;
8264 const bool isImplicitlyDeclared = true;
8265 const bool isDefined = false;
8266 const clang::ObjCMethodDecl::ImplementationControl impControl =
8267 clang::ObjCMethodDecl::None;
8268 const bool HasRelatedResultType = false;
8270 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8271 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8272 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8273 nullptr, class_interface_decl, isInstance, isVariadic,
8274 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8275 HasRelatedResultType);
8277 if (getter && metadata)
8278 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8281 getter->setMethodParams(*clang_ast,
8282 llvm::ArrayRef<clang::ParmVarDecl *>(),
8283 llvm::ArrayRef<clang::SourceLocation>());
8285 class_interface_decl->addDecl(getter);
8289 if (!setter_sel.isNull() &&
8291 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8292 : class_interface_decl->lookupClassMethod(setter_sel))) {
8293 clang::QualType result_type = clang_ast->VoidTy;
8294 const bool isVariadic = false;
8295 const bool isSynthesized = false;
8296 const bool isImplicitlyDeclared = true;
8297 const bool isDefined = false;
8298 const clang::ObjCMethodDecl::ImplementationControl impControl =
8299 clang::ObjCMethodDecl::None;
8300 const bool HasRelatedResultType = false;
8302 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8303 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8304 setter_sel, result_type, nullptr, class_interface_decl,
8305 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8306 isDefined, impControl, HasRelatedResultType);
8308 if (setter && metadata)
8309 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8311 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8313 params.push_back(clang::ParmVarDecl::Create(
8314 *clang_ast, setter, clang::SourceLocation(),
8315 clang::SourceLocation(),
8316 nullptr, // anonymous
8317 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8318 clang::SC_Auto, nullptr));
8321 setter->setMethodParams(
8322 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8323 llvm::ArrayRef<clang::SourceLocation>());
8325 class_interface_decl->addDecl(setter);
8336 bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8337 bool check_superclass) {
8338 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8339 if (class_interface_decl)
8340 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8344 clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8345 const CompilerType &type,
8346 const char *name, // the full symbol name as seen in the symbol table
8347 // (lldb::opaque_compiler_type_t type, "-[NString
8348 // stringWithCString:]")
8349 const CompilerType &method_clang_type, lldb::AccessType access,
8350 bool is_artificial, bool is_variadic) {
8351 if (!type || !method_clang_type.IsValid())
8354 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8356 if (class_interface_decl == nullptr)
8358 ClangASTContext *lldb_ast =
8359 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8360 if (lldb_ast == nullptr)
8362 clang::ASTContext *ast = lldb_ast->getASTContext();
8364 const char *selector_start = ::strchr(name, ' ');
8365 if (selector_start == nullptr)
8369 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8373 // printf ("name = '%s'\n", name);
8375 unsigned num_selectors_with_args = 0;
8376 for (start = selector_start; start && *start != '\0' && *start != ']';
8378 len = ::strcspn(start, ":]");
8379 bool has_arg = (start[len] == ':');
8381 ++num_selectors_with_args;
8382 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8387 if (selector_idents.size() == 0)
8390 clang::Selector method_selector = ast->Selectors.getSelector(
8391 num_selectors_with_args ? selector_idents.size() : 0,
8392 selector_idents.data());
8394 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8396 // Populate the method decl with parameter decls
8397 const clang::Type *method_type(method_qual_type.getTypePtr());
8399 if (method_type == nullptr)
8402 const clang::FunctionProtoType *method_function_prototype(
8403 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8405 if (!method_function_prototype)
8408 bool is_synthesized = false;
8409 bool is_defined = false;
8410 clang::ObjCMethodDecl::ImplementationControl imp_control =
8411 clang::ObjCMethodDecl::None;
8413 const unsigned num_args = method_function_prototype->getNumParams();
8415 if (num_args != num_selectors_with_args)
8416 return nullptr; // some debug information is corrupt. We are not going to
8419 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8421 clang::SourceLocation(), // beginLoc,
8422 clang::SourceLocation(), // endLoc,
8423 method_selector, method_function_prototype->getReturnType(),
8424 nullptr, // TypeSourceInfo *ResultTInfo,
8425 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8426 ClangUtil::GetQualType(type)),
8427 name[0] == '-', is_variadic, is_synthesized,
8428 true, // is_implicitly_declared; we force this to true because we don't
8429 // have source locations
8430 is_defined, imp_control, false /*has_related_result_type*/);
8432 if (objc_method_decl == nullptr)
8436 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8438 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8439 params.push_back(clang::ParmVarDecl::Create(
8440 *ast, objc_method_decl, clang::SourceLocation(),
8441 clang::SourceLocation(),
8442 nullptr, // anonymous
8443 method_function_prototype->getParamType(param_index), nullptr,
8444 clang::SC_Auto, nullptr));
8447 objc_method_decl->setMethodParams(
8448 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8449 llvm::ArrayRef<clang::SourceLocation>());
8452 class_interface_decl->addDecl(objc_method_decl);
8454 #ifdef LLDB_CONFIGURATION_DEBUG
8455 VerifyDecl(objc_method_decl);
8458 return objc_method_decl;
8461 bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8462 if (ClangUtil::IsClangType(type))
8465 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
8467 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8468 switch (type_class) {
8469 case clang::Type::Record: {
8470 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8471 if (cxx_record_decl)
8472 return cxx_record_decl->hasExternalLexicalStorage() ||
8473 cxx_record_decl->hasExternalVisibleStorage();
8476 case clang::Type::Enum: {
8477 clang::EnumDecl *enum_decl =
8478 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8480 return enum_decl->hasExternalLexicalStorage() ||
8481 enum_decl->hasExternalVisibleStorage();
8484 case clang::Type::ObjCObject:
8485 case clang::Type::ObjCInterface: {
8486 const clang::ObjCObjectType *objc_class_type =
8487 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8488 assert(objc_class_type);
8489 if (objc_class_type) {
8490 clang::ObjCInterfaceDecl *class_interface_decl =
8491 objc_class_type->getInterface();
8493 if (class_interface_decl)
8494 return class_interface_decl->hasExternalLexicalStorage() ||
8495 class_interface_decl->hasExternalVisibleStorage();
8499 case clang::Type::Typedef:
8500 return GetHasExternalStorage(CompilerType(
8501 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8503 ->getUnderlyingType()
8504 .getAsOpaquePtr()));
8506 case clang::Type::Auto:
8507 return GetHasExternalStorage(CompilerType(
8508 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8510 .getAsOpaquePtr()));
8512 case clang::Type::Elaborated:
8513 return GetHasExternalStorage(CompilerType(
8514 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8516 .getAsOpaquePtr()));
8518 case clang::Type::Paren:
8519 return GetHasExternalStorage(CompilerType(
8520 type.GetTypeSystem(),
8521 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8529 bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8534 clang::QualType qual_type(GetCanonicalQualType(type));
8536 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8537 switch (type_class) {
8538 case clang::Type::Record: {
8539 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8540 if (cxx_record_decl) {
8541 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8542 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8547 case clang::Type::Enum: {
8548 clang::EnumDecl *enum_decl =
8549 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8551 enum_decl->setHasExternalLexicalStorage(has_extern);
8552 enum_decl->setHasExternalVisibleStorage(has_extern);
8557 case clang::Type::ObjCObject:
8558 case clang::Type::ObjCInterface: {
8559 const clang::ObjCObjectType *objc_class_type =
8560 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8561 assert(objc_class_type);
8562 if (objc_class_type) {
8563 clang::ObjCInterfaceDecl *class_interface_decl =
8564 objc_class_type->getInterface();
8566 if (class_interface_decl) {
8567 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8568 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8574 case clang::Type::Typedef:
8575 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8577 ->getUnderlyingType()
8581 case clang::Type::Auto:
8582 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8587 case clang::Type::Elaborated:
8588 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8593 case clang::Type::Paren:
8594 return SetHasExternalStorage(
8595 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8604 #pragma mark TagDecl
8606 bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8607 clang::QualType qual_type(ClangUtil::GetQualType(type));
8608 if (!qual_type.isNull()) {
8609 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8611 clang::TagDecl *tag_decl = tag_type->getDecl();
8613 tag_decl->startDefinition();
8618 const clang::ObjCObjectType *object_type =
8619 qual_type->getAs<clang::ObjCObjectType>();
8621 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8622 if (interface_decl) {
8623 interface_decl->startDefinition();
8631 bool ClangASTContext::CompleteTagDeclarationDefinition(
8632 const CompilerType &type) {
8633 clang::QualType qual_type(ClangUtil::GetQualType(type));
8634 if (!qual_type.isNull()) {
8635 // Make sure we use the same methodology as
8636 // ClangASTContext::StartTagDeclarationDefinition()
8637 // as to how we start/end the definition. Previously we were calling
8638 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8640 clang::TagDecl *tag_decl = tag_type->getDecl();
8642 clang::CXXRecordDecl *cxx_record_decl =
8643 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8645 if (cxx_record_decl) {
8646 if (!cxx_record_decl->isCompleteDefinition())
8647 cxx_record_decl->completeDefinition();
8648 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8649 cxx_record_decl->setHasExternalLexicalStorage(false);
8650 cxx_record_decl->setHasExternalVisibleStorage(false);
8656 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8659 clang::EnumDecl *enum_decl = enutype->getDecl();
8662 if (!enum_decl->isCompleteDefinition()) {
8663 ClangASTContext *lldb_ast =
8664 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8665 if (lldb_ast == nullptr)
8667 clang::ASTContext *ast = lldb_ast->getASTContext();
8669 /// TODO This really needs to be fixed.
8671 QualType integer_type(enum_decl->getIntegerType());
8672 if (!integer_type.isNull()) {
8673 unsigned NumPositiveBits = 1;
8674 unsigned NumNegativeBits = 0;
8676 clang::QualType promotion_qual_type;
8677 // If the enum integer type is less than an integer in bit width,
8678 // then we must promote it to an integer size.
8679 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8680 ast->getTypeSize(ast->IntTy)) {
8681 if (enum_decl->getIntegerType()->isSignedIntegerType())
8682 promotion_qual_type = ast->IntTy;
8684 promotion_qual_type = ast->UnsignedIntTy;
8686 promotion_qual_type = enum_decl->getIntegerType();
8688 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8689 promotion_qual_type, NumPositiveBits,
8700 bool ClangASTContext::AddEnumerationValueToEnumerationType(
8701 lldb::opaque_compiler_type_t type,
8702 const CompilerType &enumerator_clang_type, const Declaration &decl,
8703 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8704 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8705 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8707 bool is_signed = false;
8708 enumerator_clang_type.IsIntegerType(is_signed);
8709 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8711 const clang::EnumType *enutype =
8712 llvm::dyn_cast<clang::EnumType>(clang_type);
8715 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8716 enum_llvm_apsint = enum_value;
8717 clang::EnumConstantDecl *enumerator_decl =
8718 clang::EnumConstantDecl::Create(
8719 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8720 name ? &getASTContext()->Idents.get(name)
8721 : nullptr, // Identifier
8722 ClangUtil::GetQualType(enumerator_clang_type),
8723 nullptr, enum_llvm_apsint);
8725 if (enumerator_decl) {
8726 enutype->getDecl()->addDecl(enumerator_decl);
8728 #ifdef LLDB_CONFIGURATION_DEBUG
8729 VerifyDecl(enumerator_decl);
8741 ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8742 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8743 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8745 const clang::EnumType *enutype =
8746 llvm::dyn_cast<clang::EnumType>(clang_type);
8748 clang::EnumDecl *enum_decl = enutype->getDecl();
8750 return CompilerType(getASTContext(), enum_decl->getIntegerType());
8753 return CompilerType();
8757 ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8758 const CompilerType &pointee_type) {
8759 if (type && pointee_type.IsValid() &&
8760 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8761 ClangASTContext *ast =
8762 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8764 return CompilerType();
8765 return CompilerType(ast->getASTContext(),
8766 ast->getASTContext()->getMemberPointerType(
8767 ClangUtil::GetQualType(pointee_type),
8768 ClangUtil::GetQualType(type).getTypePtr()));
8770 return CompilerType();
8774 ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8775 const char *s, uint8_t *dst,
8778 clang::QualType qual_type(GetCanonicalQualType(type));
8780 bool is_complex = false;
8781 if (IsFloatingPointType(type, count, is_complex)) {
8782 // TODO: handle complex and vector types
8786 llvm::StringRef s_sref(s);
8787 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8790 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8791 const uint64_t byte_size = bit_size / 8;
8792 if (dst_size >= byte_size) {
8793 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8794 llvm::NextPowerOf2(byte_size) * 8);
8795 lldb_private::Error get_data_error;
8796 if (scalar.GetAsMemoryData(dst, byte_size,
8797 lldb_private::endian::InlHostByteOrder(),
8806 //----------------------------------------------------------------------
8808 //----------------------------------------------------------------------
8809 #define DEPTH_INCREMENT 2
8811 void ClangASTContext::DumpValue(
8812 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
8813 lldb::Format format, const DataExtractor &data,
8814 lldb::offset_t data_byte_offset, size_t data_byte_size,
8815 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8816 bool show_summary, bool verbose, uint32_t depth) {
8820 clang::QualType qual_type(GetQualType(type));
8821 switch (qual_type->getTypeClass()) {
8822 case clang::Type::Record:
8823 if (GetCompleteType(type)) {
8824 const clang::RecordType *record_type =
8825 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8826 const clang::RecordDecl *record_decl = record_type->getDecl();
8827 assert(record_decl);
8828 uint32_t field_bit_offset = 0;
8829 uint32_t field_byte_offset = 0;
8830 const clang::ASTRecordLayout &record_layout =
8831 getASTContext()->getASTRecordLayout(record_decl);
8832 uint32_t child_idx = 0;
8834 const clang::CXXRecordDecl *cxx_record_decl =
8835 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8836 if (cxx_record_decl) {
8837 // We might have base classes to print out first
8838 clang::CXXRecordDecl::base_class_const_iterator base_class,
8840 for (base_class = cxx_record_decl->bases_begin(),
8841 base_class_end = cxx_record_decl->bases_end();
8842 base_class != base_class_end; ++base_class) {
8843 const clang::CXXRecordDecl *base_class_decl =
8844 llvm::cast<clang::CXXRecordDecl>(
8845 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8847 // Skip empty base classes
8848 if (verbose == false &&
8849 ClangASTContext::RecordHasFields(base_class_decl) == false)
8852 if (base_class->isVirtual())
8854 record_layout.getVBaseClassOffset(base_class_decl)
8858 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8861 field_byte_offset = field_bit_offset / 8;
8862 assert(field_bit_offset % 8 == 0);
8868 clang::QualType base_class_qual_type = base_class->getType();
8869 std::string base_class_type_name(base_class_qual_type.getAsString());
8871 // Indent and print the base class type name
8872 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8873 base_class_type_name);
8875 clang::TypeInfo base_class_type_info =
8876 getASTContext()->getTypeInfo(base_class_qual_type);
8878 // Dump the value of the member
8879 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8880 base_clang_type.DumpValue(
8882 s, // Stream to dump to
8884 .GetFormat(), // The format with which to display the member
8885 data, // Data buffer containing all bytes for this type
8886 data_byte_offset + field_byte_offset, // Offset into "data" where
8887 // to grab value from
8888 base_class_type_info.Width / 8, // Size of this type in bytes
8889 0, // Bitfield bit size
8890 0, // Bitfield bit offset
8891 show_types, // Boolean indicating if we should show the variable
8893 show_summary, // Boolean indicating if we should show a summary
8894 // for the current type
8895 verbose, // Verbose output?
8896 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8902 uint32_t field_idx = 0;
8903 clang::RecordDecl::field_iterator field, field_end;
8904 for (field = record_decl->field_begin(),
8905 field_end = record_decl->field_end();
8906 field != field_end; ++field, ++field_idx, ++child_idx) {
8907 // Print the starting squiggly bracket (if this is the
8908 // first member) or comma (for member 2 and beyond) for
8909 // the struct/union/class member.
8916 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8918 clang::QualType field_type = field->getType();
8919 // Print the member type if requested
8920 // Figure out the type byte size (field_type_info.first) and
8921 // alignment (field_type_info.second) from the AST context.
8922 clang::TypeInfo field_type_info =
8923 getASTContext()->getTypeInfo(field_type);
8924 assert(field_idx < record_layout.getFieldCount());
8925 // Figure out the field offset within the current struct/union/class
8927 field_bit_offset = record_layout.getFieldOffset(field_idx);
8928 field_byte_offset = field_bit_offset / 8;
8929 uint32_t field_bitfield_bit_size = 0;
8930 uint32_t field_bitfield_bit_offset = 0;
8931 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8932 field_bitfield_bit_size))
8933 field_bitfield_bit_offset = field_bit_offset % 8;
8936 std::string field_type_name(field_type.getAsString());
8937 if (field_bitfield_bit_size > 0)
8938 s->Printf("(%s:%u) ", field_type_name.c_str(),
8939 field_bitfield_bit_size);
8941 s->Printf("(%s) ", field_type_name.c_str());
8943 // Print the member name and equal sign
8944 s->Printf("%s = ", field->getNameAsString().c_str());
8946 // Dump the value of the member
8947 CompilerType field_clang_type(getASTContext(), field_type);
8948 field_clang_type.DumpValue(
8950 s, // Stream to dump to
8952 .GetFormat(), // The format with which to display the member
8953 data, // Data buffer containing all bytes for this type
8954 data_byte_offset + field_byte_offset, // Offset into "data" where to
8956 field_type_info.Width / 8, // Size of this type in bytes
8957 field_bitfield_bit_size, // Bitfield bit size
8958 field_bitfield_bit_offset, // Bitfield bit offset
8959 show_types, // Boolean indicating if we should show the variable
8961 show_summary, // Boolean indicating if we should show a summary for
8963 verbose, // Verbose output?
8964 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8968 // Indent the trailing squiggly bracket
8970 s->Printf("\n%*s}", depth, "");
8974 case clang::Type::Enum:
8975 if (GetCompleteType(type)) {
8976 const clang::EnumType *enutype =
8977 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8978 const clang::EnumDecl *enum_decl = enutype->getDecl();
8980 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8981 lldb::offset_t offset = data_byte_offset;
8982 const int64_t enum_value = data.GetMaxU64Bitfield(
8983 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8984 for (enum_pos = enum_decl->enumerator_begin(),
8985 enum_end_pos = enum_decl->enumerator_end();
8986 enum_pos != enum_end_pos; ++enum_pos) {
8987 if (enum_pos->getInitVal() == enum_value) {
8988 s->Printf("%s", enum_pos->getNameAsString().c_str());
8992 // If we have gotten here we didn't get find the enumerator in the
8993 // enum decl, so just print the integer.
8994 s->Printf("%" PRIi64, enum_value);
8998 case clang::Type::ConstantArray: {
8999 const clang::ConstantArrayType *array =
9000 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9001 bool is_array_of_characters = false;
9002 clang::QualType element_qual_type = array->getElementType();
9004 const clang::Type *canonical_type =
9005 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9007 is_array_of_characters = canonical_type->isCharType();
9009 const uint64_t element_count = array->getSize().getLimitedValue();
9011 clang::TypeInfo field_type_info =
9012 getASTContext()->getTypeInfo(element_qual_type);
9014 uint32_t element_idx = 0;
9015 uint32_t element_offset = 0;
9016 uint64_t element_byte_size = field_type_info.Width / 8;
9017 uint32_t element_stride = element_byte_size;
9019 if (is_array_of_characters) {
9021 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9022 element_byte_size, element_count, UINT32_MAX,
9023 LLDB_INVALID_ADDRESS, 0, 0);
9027 CompilerType element_clang_type(getASTContext(), element_qual_type);
9028 lldb::Format element_format = element_clang_type.GetFormat();
9030 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9031 // Print the starting squiggly bracket (if this is the
9032 // first member) or comman (for member 2 and beyong) for
9033 // the struct/union/class member.
9034 if (element_idx == 0)
9039 // Indent and print the index
9040 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9042 // Figure out the field offset within the current struct/union/class
9044 element_offset = element_idx * element_stride;
9046 // Dump the value of the member
9047 element_clang_type.DumpValue(
9049 s, // Stream to dump to
9050 element_format, // The format with which to display the element
9051 data, // Data buffer containing all bytes for this type
9053 element_offset, // Offset into "data" where to grab value from
9054 element_byte_size, // Size of this type in bytes
9055 0, // Bitfield bit size
9056 0, // Bitfield bit offset
9057 show_types, // Boolean indicating if we should show the variable
9059 show_summary, // Boolean indicating if we should show a summary for
9061 verbose, // Verbose output?
9062 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9066 // Indent the trailing squiggly bracket
9067 if (element_idx > 0)
9068 s->Printf("\n%*s}", depth, "");
9073 case clang::Type::Typedef: {
9074 clang::QualType typedef_qual_type =
9075 llvm::cast<clang::TypedefType>(qual_type)
9077 ->getUnderlyingType();
9079 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9080 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9081 clang::TypeInfo typedef_type_info =
9082 getASTContext()->getTypeInfo(typedef_qual_type);
9083 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9085 return typedef_clang_type.DumpValue(
9087 s, // Stream to dump to
9088 typedef_format, // The format with which to display the element
9089 data, // Data buffer containing all bytes for this type
9090 data_byte_offset, // Offset into "data" where to grab value from
9091 typedef_byte_size, // Size of this type in bytes
9092 bitfield_bit_size, // Bitfield bit size
9093 bitfield_bit_offset, // Bitfield bit offset
9094 show_types, // Boolean indicating if we should show the variable types
9095 show_summary, // Boolean indicating if we should show a summary for the
9097 verbose, // Verbose output?
9098 depth); // Scope depth for any types that have children
9101 case clang::Type::Auto: {
9102 clang::QualType elaborated_qual_type =
9103 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9104 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9105 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9106 clang::TypeInfo elaborated_type_info =
9107 getASTContext()->getTypeInfo(elaborated_qual_type);
9108 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9110 return elaborated_clang_type.DumpValue(
9112 s, // Stream to dump to
9113 elaborated_format, // The format with which to display the element
9114 data, // Data buffer containing all bytes for this type
9115 data_byte_offset, // Offset into "data" where to grab value from
9116 elaborated_byte_size, // Size of this type in bytes
9117 bitfield_bit_size, // Bitfield bit size
9118 bitfield_bit_offset, // Bitfield bit offset
9119 show_types, // Boolean indicating if we should show the variable types
9120 show_summary, // Boolean indicating if we should show a summary for the
9122 verbose, // Verbose output?
9123 depth); // Scope depth for any types that have children
9126 case clang::Type::Elaborated: {
9127 clang::QualType elaborated_qual_type =
9128 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9129 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9130 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9131 clang::TypeInfo elaborated_type_info =
9132 getASTContext()->getTypeInfo(elaborated_qual_type);
9133 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9135 return elaborated_clang_type.DumpValue(
9137 s, // Stream to dump to
9138 elaborated_format, // The format with which to display the element
9139 data, // Data buffer containing all bytes for this type
9140 data_byte_offset, // Offset into "data" where to grab value from
9141 elaborated_byte_size, // Size of this type in bytes
9142 bitfield_bit_size, // Bitfield bit size
9143 bitfield_bit_offset, // Bitfield bit offset
9144 show_types, // Boolean indicating if we should show the variable types
9145 show_summary, // Boolean indicating if we should show a summary for the
9147 verbose, // Verbose output?
9148 depth); // Scope depth for any types that have children
9151 case clang::Type::Paren: {
9152 clang::QualType desugar_qual_type =
9153 llvm::cast<clang::ParenType>(qual_type)->desugar();
9154 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9156 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9157 clang::TypeInfo desugar_type_info =
9158 getASTContext()->getTypeInfo(desugar_qual_type);
9159 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9161 return desugar_clang_type.DumpValue(
9163 s, // Stream to dump to
9164 desugar_format, // The format with which to display the element
9165 data, // Data buffer containing all bytes for this type
9166 data_byte_offset, // Offset into "data" where to grab value from
9167 desugar_byte_size, // Size of this type in bytes
9168 bitfield_bit_size, // Bitfield bit size
9169 bitfield_bit_offset, // Bitfield bit offset
9170 show_types, // Boolean indicating if we should show the variable types
9171 show_summary, // Boolean indicating if we should show a summary for the
9173 verbose, // Verbose output?
9174 depth); // Scope depth for any types that have children
9178 // We are down to a scalar type that we just need to display.
9179 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9180 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9181 bitfield_bit_offset);
9184 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9189 bool ClangASTContext::DumpTypeValue(
9190 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9191 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9192 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9193 ExecutionContextScope *exe_scope) {
9196 if (IsAggregateType(type)) {
9199 clang::QualType qual_type(GetQualType(type));
9201 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9202 switch (type_class) {
9203 case clang::Type::Typedef: {
9204 clang::QualType typedef_qual_type =
9205 llvm::cast<clang::TypedefType>(qual_type)
9207 ->getUnderlyingType();
9208 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9209 if (format == eFormatDefault)
9210 format = typedef_clang_type.GetFormat();
9211 clang::TypeInfo typedef_type_info =
9212 getASTContext()->getTypeInfo(typedef_qual_type);
9213 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9215 return typedef_clang_type.DumpTypeValue(
9217 format, // The format with which to display the element
9218 data, // Data buffer containing all bytes for this type
9219 byte_offset, // Offset into "data" where to grab value from
9220 typedef_byte_size, // Size of this type in bytes
9221 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9222 // treat as a bitfield
9223 bitfield_bit_offset, // Offset in bits of a bitfield value if
9224 // bitfield_bit_size != 0
9228 case clang::Type::Enum:
9229 // If our format is enum or default, show the enumeration value as
9230 // its enumeration string value, else just display it as requested.
9231 if ((format == eFormatEnum || format == eFormatDefault) &&
9232 GetCompleteType(type)) {
9233 const clang::EnumType *enutype =
9234 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9235 const clang::EnumDecl *enum_decl = enutype->getDecl();
9237 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9238 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9239 lldb::offset_t offset = byte_offset;
9241 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9242 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9243 for (enum_pos = enum_decl->enumerator_begin(),
9244 enum_end_pos = enum_decl->enumerator_end();
9245 enum_pos != enum_end_pos; ++enum_pos) {
9246 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9247 s->PutCString(enum_pos->getNameAsString());
9251 // If we have gotten here we didn't get find the enumerator in the
9252 // enum decl, so just print the integer.
9253 s->Printf("%" PRIi64, enum_svalue);
9255 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9256 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9257 for (enum_pos = enum_decl->enumerator_begin(),
9258 enum_end_pos = enum_decl->enumerator_end();
9259 enum_pos != enum_end_pos; ++enum_pos) {
9260 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9261 s->PutCString(enum_pos->getNameAsString());
9265 // If we have gotten here we didn't get find the enumerator in the
9266 // enum decl, so just print the integer.
9267 s->Printf("%" PRIu64, enum_uvalue);
9271 // format was not enum, just fall through and dump the value as
9276 // We are down to a scalar type that we just need to display.
9278 uint32_t item_count = 1;
9279 // A few formats, we might need to modify our size and count for
9281 // on how we are trying to display the value...
9284 case eFormatBoolean:
9286 case eFormatComplex:
9287 case eFormatCString: // NULL terminated C strings
9288 case eFormatDecimal:
9291 case eFormatHexUppercase:
9295 case eFormatUnsigned:
9296 case eFormatPointer:
9297 case eFormatVectorOfChar:
9298 case eFormatVectorOfSInt8:
9299 case eFormatVectorOfUInt8:
9300 case eFormatVectorOfSInt16:
9301 case eFormatVectorOfUInt16:
9302 case eFormatVectorOfSInt32:
9303 case eFormatVectorOfUInt32:
9304 case eFormatVectorOfSInt64:
9305 case eFormatVectorOfUInt64:
9306 case eFormatVectorOfFloat32:
9307 case eFormatVectorOfFloat64:
9308 case eFormatVectorOfUInt128:
9312 case eFormatCharPrintable:
9313 case eFormatCharArray:
9315 case eFormatBytesWithASCII:
9316 item_count = byte_size;
9320 case eFormatUnicode16:
9321 item_count = byte_size / 2;
9325 case eFormatUnicode32:
9326 item_count = byte_size / 4;
9330 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9331 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9332 bitfield_bit_size, bitfield_bit_offset,
9341 void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9342 ExecutionContext *exe_ctx, Stream *s,
9343 const lldb_private::DataExtractor &data,
9344 lldb::offset_t data_byte_offset,
9345 size_t data_byte_size) {
9346 uint32_t length = 0;
9347 if (IsCStringType(type, length)) {
9349 Process *process = exe_ctx->GetProcessPtr();
9351 lldb::offset_t offset = data_byte_offset;
9352 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9353 std::vector<uint8_t> buf;
9359 DataExtractor cstr_data(&buf.front(), buf.size(),
9360 process->GetByteOrder(), 4);
9363 size_t total_cstr_len = 0;
9365 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9366 buf.size(), error)) > 0) {
9367 const size_t len = strlen((const char *)&buf.front());
9370 if (total_cstr_len == 0)
9371 s->PutCString(" \"");
9372 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9373 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9374 total_cstr_len += len;
9375 if (len < buf.size())
9377 pointer_address += total_cstr_len;
9379 if (total_cstr_len > 0)
9386 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9387 StreamFile s(stdout, false);
9388 DumpTypeDescription(type, &s);
9389 ClangASTMetadata *metadata =
9390 ClangASTContext::GetMetadata(getASTContext(), type);
9396 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9399 clang::QualType qual_type(GetQualType(type));
9401 llvm::SmallVector<char, 1024> buf;
9402 llvm::raw_svector_ostream llvm_ostrm(buf);
9404 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9405 switch (type_class) {
9406 case clang::Type::ObjCObject:
9407 case clang::Type::ObjCInterface: {
9408 GetCompleteType(type);
9410 const clang::ObjCObjectType *objc_class_type =
9411 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9412 assert(objc_class_type);
9413 if (objc_class_type) {
9414 clang::ObjCInterfaceDecl *class_interface_decl =
9415 objc_class_type->getInterface();
9416 if (class_interface_decl) {
9417 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9418 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9423 case clang::Type::Typedef: {
9424 const clang::TypedefType *typedef_type =
9425 qual_type->getAs<clang::TypedefType>();
9427 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9428 std::string clang_typedef_name(
9429 typedef_decl->getQualifiedNameAsString());
9430 if (!clang_typedef_name.empty()) {
9431 s->PutCString("typedef ");
9432 s->PutCString(clang_typedef_name);
9437 case clang::Type::Auto:
9438 CompilerType(getASTContext(),
9439 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9440 .DumpTypeDescription(s);
9443 case clang::Type::Elaborated:
9444 CompilerType(getASTContext(),
9445 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9446 .DumpTypeDescription(s);
9449 case clang::Type::Paren:
9450 CompilerType(getASTContext(),
9451 llvm::cast<clang::ParenType>(qual_type)->desugar())
9452 .DumpTypeDescription(s);
9455 case clang::Type::Record: {
9456 GetCompleteType(type);
9458 const clang::RecordType *record_type =
9459 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9460 const clang::RecordDecl *record_decl = record_type->getDecl();
9461 const clang::CXXRecordDecl *cxx_record_decl =
9462 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9464 if (cxx_record_decl)
9465 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9466 s->GetIndentLevel());
9468 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9469 s->GetIndentLevel());
9473 const clang::TagType *tag_type =
9474 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9476 clang::TagDecl *tag_decl = tag_type->getDecl();
9478 tag_decl->print(llvm_ostrm, 0);
9480 std::string clang_type_name(qual_type.getAsString());
9481 if (!clang_type_name.empty())
9482 s->PutCString(clang_type_name);
9487 if (buf.size() > 0) {
9488 s->Write(buf.data(), buf.size());
9493 void ClangASTContext::DumpTypeName(const CompilerType &type) {
9494 if (ClangUtil::IsClangType(type)) {
9495 clang::QualType qual_type(
9496 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9498 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9499 switch (type_class) {
9500 case clang::Type::Record: {
9501 const clang::CXXRecordDecl *cxx_record_decl =
9502 qual_type->getAsCXXRecordDecl();
9503 if (cxx_record_decl)
9504 printf("class %s", cxx_record_decl->getName().str().c_str());
9507 case clang::Type::Enum: {
9508 clang::EnumDecl *enum_decl =
9509 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9511 printf("enum %s", enum_decl->getName().str().c_str());
9515 case clang::Type::ObjCObject:
9516 case clang::Type::ObjCInterface: {
9517 const clang::ObjCObjectType *objc_class_type =
9518 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9519 if (objc_class_type) {
9520 clang::ObjCInterfaceDecl *class_interface_decl =
9521 objc_class_type->getInterface();
9522 // We currently can't complete objective C types through the newly added
9524 // because it only supports TagDecl objects right now...
9525 if (class_interface_decl)
9526 printf("@class %s", class_interface_decl->getName().str().c_str());
9530 case clang::Type::Typedef:
9531 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9538 case clang::Type::Auto:
9540 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9541 llvm::cast<clang::AutoType>(qual_type)
9543 .getAsOpaquePtr()));
9545 case clang::Type::Elaborated:
9546 printf("elaborated ");
9547 return DumpTypeName(CompilerType(
9548 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9550 .getAsOpaquePtr()));
9552 case clang::Type::Paren:
9554 return DumpTypeName(CompilerType(
9555 type.GetTypeSystem(),
9556 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9559 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9565 clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9566 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9567 const char *parent_name, int tag_decl_kind,
9568 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9569 if (template_param_infos.IsValid()) {
9570 std::string template_basename(parent_name);
9571 template_basename.erase(template_basename.find('<'));
9573 return CreateClassTemplateDecl(decl_ctx, access_type,
9574 template_basename.c_str(), tag_decl_kind,
9575 template_param_infos);
9580 void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9581 ClangASTContext *ast = (ClangASTContext *)baton;
9582 SymbolFile *sym_file = ast->GetSymbolFile();
9584 CompilerType clang_type = GetTypeForDecl(decl);
9586 sym_file->CompleteType(clang_type);
9590 void ClangASTContext::CompleteObjCInterfaceDecl(
9591 void *baton, clang::ObjCInterfaceDecl *decl) {
9592 ClangASTContext *ast = (ClangASTContext *)baton;
9593 SymbolFile *sym_file = ast->GetSymbolFile();
9595 CompilerType clang_type = GetTypeForDecl(decl);
9597 sym_file->CompleteType(clang_type);
9601 DWARFASTParser *ClangASTContext::GetDWARFParser() {
9602 if (!m_dwarf_ast_parser_ap)
9603 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9604 return m_dwarf_ast_parser_ap.get();
9608 PDBASTParser *ClangASTContext::GetPDBParser() {
9609 if (!m_pdb_ast_parser_ap)
9610 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9611 return m_pdb_ast_parser_ap.get();
9615 bool ClangASTContext::LayoutRecordType(
9616 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9617 uint64_t &alignment,
9618 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9619 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9621 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9623 ClangASTContext *ast = (ClangASTContext *)baton;
9624 DWARFASTParserClang *dwarf_ast_parser =
9625 (DWARFASTParserClang *)ast->GetDWARFParser();
9626 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9627 record_decl, bit_size, alignment, field_offsets, base_offsets,
9631 //----------------------------------------------------------------------
9632 // CompilerDecl override functions
9633 //----------------------------------------------------------------------
9635 ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9637 clang::NamedDecl *nd =
9638 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9640 return ConstString(nd->getDeclName().getAsString());
9642 return ConstString();
9645 ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9647 clang::NamedDecl *nd =
9648 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9649 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9650 clang::MangleContext *mc = getMangleContext();
9651 if (mc && mc->shouldMangleCXXName(nd)) {
9652 llvm::SmallVector<char, 1024> buf;
9653 llvm::raw_svector_ostream llvm_ostrm(buf);
9654 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9655 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9656 Ctor_Complete, llvm_ostrm);
9657 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9658 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9659 Dtor_Complete, llvm_ostrm);
9661 mc->mangleName(nd, llvm_ostrm);
9664 return ConstString(buf.data(), buf.size());
9668 return ConstString();
9671 CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9673 return CompilerDeclContext(this,
9674 ((clang::Decl *)opaque_decl)->getDeclContext());
9676 return CompilerDeclContext();
9679 CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9680 if (clang::FunctionDecl *func_decl =
9681 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9682 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9683 if (clang::ObjCMethodDecl *objc_method =
9684 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9685 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9687 return CompilerType();
9690 size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9691 if (clang::FunctionDecl *func_decl =
9692 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9693 return func_decl->param_size();
9694 if (clang::ObjCMethodDecl *objc_method =
9695 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9696 return objc_method->param_size();
9701 CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9703 if (clang::FunctionDecl *func_decl =
9704 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9705 if (idx < func_decl->param_size()) {
9706 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9708 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9710 } else if (clang::ObjCMethodDecl *objc_method =
9711 llvm::dyn_cast<clang::ObjCMethodDecl>(
9712 (clang::Decl *)opaque_decl)) {
9713 if (idx < objc_method->param_size())
9714 return CompilerType(
9716 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9718 return CompilerType();
9721 //----------------------------------------------------------------------
9722 // CompilerDeclContext functions
9723 //----------------------------------------------------------------------
9725 std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9726 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9727 std::vector<CompilerDecl> found_decls;
9728 if (opaque_decl_ctx) {
9729 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9730 std::set<DeclContext *> searched;
9731 std::multimap<DeclContext *, DeclContext *> search_queue;
9732 SymbolFile *symbol_file = GetSymbolFile();
9734 for (clang::DeclContext *decl_context = root_decl_ctx;
9735 decl_context != nullptr && found_decls.empty();
9736 decl_context = decl_context->getParent()) {
9737 search_queue.insert(std::make_pair(decl_context, decl_context));
9739 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9741 if (!searched.insert(it->second).second)
9743 symbol_file->ParseDeclsForContext(
9744 CompilerDeclContext(this, it->second));
9746 for (clang::Decl *child : it->second->decls()) {
9747 if (clang::UsingDirectiveDecl *ud =
9748 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9749 if (ignore_using_decls)
9751 clang::DeclContext *from = ud->getCommonAncestor();
9752 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9753 search_queue.insert(
9754 std::make_pair(from, ud->getNominatedNamespace()));
9755 } else if (clang::UsingDecl *ud =
9756 llvm::dyn_cast<clang::UsingDecl>(child)) {
9757 if (ignore_using_decls)
9759 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9760 clang::Decl *target = usd->getTargetDecl();
9761 if (clang::NamedDecl *nd =
9762 llvm::dyn_cast<clang::NamedDecl>(target)) {
9763 IdentifierInfo *ii = nd->getIdentifier();
9764 if (ii != nullptr &&
9765 ii->getName().equals(name.AsCString(nullptr)))
9766 found_decls.push_back(CompilerDecl(this, nd));
9769 } else if (clang::NamedDecl *nd =
9770 llvm::dyn_cast<clang::NamedDecl>(child)) {
9771 IdentifierInfo *ii = nd->getIdentifier();
9772 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9773 found_decls.push_back(CompilerDecl(this, nd));
9782 // Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9783 // and return the number of levels it took to find it, or
9784 // LLDB_INVALID_DECL_LEVEL
9785 // if not found. If the decl was imported via a using declaration, its name
9787 // type, if set, will be used to check that the decl found in the scope is a
9790 // The optional name is required by languages (like C++) to handle using
9801 // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9802 // // LLDB_INVALID_DECL_LEVEL for 'goo'.
9805 // The optional type is useful in the case that there's a specific overload
9806 // that we're looking for that might otherwise be shadowed, like:
9814 // // CountDeclLevels returns 0 for { 'foo', void() },
9815 // // 1 for { 'foo', void(int) }, and
9816 // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9819 // NOTE: Because file statics are at the TranslationUnit along with globals, a
9820 // function at file scope will return the same level as a function at global
9822 // Ideally we'd like to treat the file scope as an additional scope just below
9824 // global scope. More work needs to be done to recognise that, if the decl
9826 // trying to look up is static, we should compare its source file with that of
9828 // current scope and return a lower number for it.
9829 uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9830 clang::DeclContext *child_decl_ctx,
9831 ConstString *child_name,
9832 CompilerType *child_type) {
9833 if (frame_decl_ctx) {
9834 std::set<DeclContext *> searched;
9835 std::multimap<DeclContext *, DeclContext *> search_queue;
9836 SymbolFile *symbol_file = GetSymbolFile();
9838 // Get the lookup scope for the decl we're trying to find.
9839 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9841 // Look for it in our scope's decl context and its parents.
9843 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9844 decl_ctx = decl_ctx->getParent()) {
9845 if (!decl_ctx->isLookupContext())
9847 if (decl_ctx == parent_decl_ctx)
9850 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9851 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9853 if (searched.find(it->second) != searched.end())
9856 // Currently DWARF has one shared translation unit for all Decls at top
9858 // would erroneously find using statements anywhere. So don't look at
9860 // translation unit.
9861 // TODO fix this and add a testcase that depends on it.
9863 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9866 searched.insert(it->second);
9867 symbol_file->ParseDeclsForContext(
9868 CompilerDeclContext(this, it->second));
9870 for (clang::Decl *child : it->second->decls()) {
9871 if (clang::UsingDirectiveDecl *ud =
9872 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9873 clang::DeclContext *ns = ud->getNominatedNamespace();
9874 if (ns == parent_decl_ctx)
9877 clang::DeclContext *from = ud->getCommonAncestor();
9878 if (searched.find(ns) == searched.end())
9879 search_queue.insert(std::make_pair(from, ns));
9880 } else if (child_name) {
9881 if (clang::UsingDecl *ud =
9882 llvm::dyn_cast<clang::UsingDecl>(child)) {
9883 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9884 clang::Decl *target = usd->getTargetDecl();
9885 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9889 IdentifierInfo *ii = nd->getIdentifier();
9890 if (ii == nullptr ||
9891 !ii->getName().equals(child_name->AsCString(nullptr)))
9893 // Check types, if one was provided.
9895 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9896 if (!AreTypesSame(clang_type, *child_type,
9897 /*ignore_qualifiers=*/true))
9910 return LLDB_INVALID_DECL_LEVEL;
9913 bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9914 if (opaque_decl_ctx)
9915 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9920 ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9921 if (opaque_decl_ctx) {
9922 clang::NamedDecl *named_decl =
9923 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9925 return ConstString(named_decl->getName());
9927 return ConstString();
9931 ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9932 if (opaque_decl_ctx) {
9933 clang::NamedDecl *named_decl =
9934 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9937 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9939 return ConstString();
9942 bool ClangASTContext::DeclContextIsClassMethod(
9943 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9944 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9945 if (opaque_decl_ctx) {
9946 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9947 if (ObjCMethodDecl *objc_method =
9948 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9949 if (is_instance_method_ptr)
9950 *is_instance_method_ptr = objc_method->isInstanceMethod();
9952 *language_ptr = eLanguageTypeObjC;
9953 if (language_object_name_ptr)
9954 language_object_name_ptr->SetCString("self");
9956 } else if (CXXMethodDecl *cxx_method =
9957 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9958 if (is_instance_method_ptr)
9959 *is_instance_method_ptr = cxx_method->isInstance();
9961 *language_ptr = eLanguageTypeC_plus_plus;
9962 if (language_object_name_ptr)
9963 language_object_name_ptr->SetCString("this");
9965 } else if (clang::FunctionDecl *function_decl =
9966 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9967 ClangASTMetadata *metadata =
9968 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9969 if (metadata && metadata->HasObjectPtr()) {
9970 if (is_instance_method_ptr)
9971 *is_instance_method_ptr = true;
9973 *language_ptr = eLanguageTypeObjC;
9974 if (language_object_name_ptr)
9975 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9983 clang::DeclContext *
9984 ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9986 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9991 ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9993 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9994 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9999 ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10001 return llvm::dyn_cast<clang::CXXMethodDecl>(
10002 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10006 clang::FunctionDecl *
10007 ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10009 return llvm::dyn_cast<clang::FunctionDecl>(
10010 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10014 clang::NamespaceDecl *
10015 ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10017 return llvm::dyn_cast<clang::NamespaceDecl>(
10018 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10023 ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10024 const void *object) {
10025 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10027 return ClangASTContext::GetMetadata(ast, object);
10031 clang::ASTContext *
10032 ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10033 ClangASTContext *ast =
10034 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10036 return ast->getASTContext();
10040 ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10041 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10042 m_target_wp(target.shared_from_this()),
10043 m_persistent_variables(new ClangPersistentVariables) {}
10045 UserExpression *ClangASTContextForExpressions::GetUserExpression(
10046 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
10047 Expression::ResultType desired_type,
10048 const EvaluateExpressionOptions &options) {
10049 TargetSP target_sp = m_target_wp.lock();
10053 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
10054 desired_type, options);
10057 FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10058 const CompilerType &return_type, const Address &function_address,
10059 const ValueList &arg_value_list, const char *name) {
10060 TargetSP target_sp = m_target_wp.lock();
10064 Process *process = target_sp->GetProcessSP().get();
10068 return new ClangFunctionCaller(*process, return_type, function_address,
10069 arg_value_list, name);
10073 ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10074 const char *name) {
10075 TargetSP target_sp = m_target_wp.lock();
10079 return new ClangUtilityFunction(*target_sp.get(), text, name);
10082 PersistentExpressionState *
10083 ClangASTContextForExpressions::GetPersistentExpressionState() {
10084 return m_persistent_variables.get();