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"
20 // Clang headers like to use NDEBUG inside of them to enable/disable debug
21 // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
22 // or another. This is bad because it means that if clang was built in release
23 // mode, it assumes that you are building in release mode which is not always
24 // the case. You can end up with functions that are defined as empty in header
25 // files when NDEBUG is not defined, and this can cause link errors with the
26 // clang .a files that you have since you might be missing functions in the .a
27 // file. So we have to define NDEBUG when including clang headers to avoid any
28 // mismatches. This is covered by rdar://problem/8691220
30 #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
31 #define LLDB_DEFINED_NDEBUG_FOR_CLANG
33 // Need to include assert.h so it is as clang would expect it to be (disabled)
37 #include "clang/AST/ASTContext.h"
38 #include "clang/AST/ASTImporter.h"
39 #include "clang/AST/Attr.h"
40 #include "clang/AST/CXXInheritance.h"
41 #include "clang/AST/DeclObjC.h"
42 #include "clang/AST/DeclTemplate.h"
43 #include "clang/AST/Mangle.h"
44 #include "clang/AST/RecordLayout.h"
45 #include "clang/AST/Type.h"
46 #include "clang/AST/VTableBuilder.h"
47 #include "clang/Basic/Builtins.h"
48 #include "clang/Basic/Diagnostic.h"
49 #include "clang/Basic/FileManager.h"
50 #include "clang/Basic/FileSystemOptions.h"
51 #include "clang/Basic/SourceManager.h"
52 #include "clang/Basic/TargetInfo.h"
53 #include "clang/Basic/TargetOptions.h"
54 #include "clang/Frontend/FrontendOptions.h"
55 #include "clang/Frontend/LangStandard.h"
57 #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
59 #undef LLDB_DEFINED_NDEBUG_FOR_CLANG
60 // Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
64 #include "llvm/Support/Signals.h"
65 #include "llvm/Support/Threading.h"
67 #include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
68 #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
69 #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
70 #include "lldb/Utility/ArchSpec.h"
71 #include "lldb/Utility/Flags.h"
73 #include "lldb/Core/DumpDataExtractor.h"
74 #include "lldb/Core/Module.h"
75 #include "lldb/Core/PluginManager.h"
76 #include "lldb/Core/StreamFile.h"
77 #include "lldb/Core/ThreadSafeDenseMap.h"
78 #include "lldb/Core/UniqueCStringMap.h"
79 #include "lldb/Symbol/ClangASTContext.h"
80 #include "lldb/Symbol/ClangASTImporter.h"
81 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
82 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
83 #include "lldb/Symbol/ClangUtil.h"
84 #include "lldb/Symbol/ObjectFile.h"
85 #include "lldb/Symbol/SymbolFile.h"
86 #include "lldb/Symbol/VerifyDecl.h"
87 #include "lldb/Target/ExecutionContext.h"
88 #include "lldb/Target/Language.h"
89 #include "lldb/Target/ObjCLanguageRuntime.h"
90 #include "lldb/Target/Process.h"
91 #include "lldb/Target/Target.h"
92 #include "lldb/Utility/DataExtractor.h"
93 #include "lldb/Utility/LLDBAssert.h"
94 #include "lldb/Utility/Log.h"
95 #include "lldb/Utility/RegularExpression.h"
96 #include "lldb/Utility/Scalar.h"
98 #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
99 #ifdef LLDB_ENABLE_ALL
100 #include "Plugins/SymbolFile/PDB/PDBASTParser.h"
101 #endif // LLDB_ENABLE_ALL
107 using namespace lldb;
108 using namespace lldb_private;
109 using namespace llvm;
110 using namespace clang;
114 ClangASTContextSupportsLanguage(lldb::LanguageType language) {
115 return language == eLanguageTypeUnknown || // Clang is the default type system
116 Language::LanguageIsC(language) ||
117 Language::LanguageIsCPlusPlus(language) ||
118 Language::LanguageIsObjC(language) ||
119 Language::LanguageIsPascal(language) ||
120 // Use Clang for Rust until there is a proper language plugin for it
121 language == eLanguageTypeRust ||
122 language == eLanguageTypeExtRenderScript ||
123 // Use Clang for D until there is a proper language plugin for it
124 language == eLanguageTypeD ||
125 // Open Dylan compiler debug info is designed to be Clang-compatible
126 language == eLanguageTypeDylan;
129 // Checks whether m1 is an overload of m2 (as opposed to an override). This is
130 // called by addOverridesForMethod to distinguish overrides (which share a
131 // vtable entry) from overloads (which require distinct entries).
132 bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
133 // FIXME: This should detect covariant return types, but currently doesn't.
134 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
135 "Methods should have the same AST context");
136 clang::ASTContext &context = m1->getASTContext();
138 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
139 context.getCanonicalType(m1->getType()));
141 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
142 context.getCanonicalType(m2->getType()));
144 auto compareArgTypes = [&context](const clang::QualType &m1p,
145 const clang::QualType &m2p) {
146 return context.hasSameType(m1p.getUnqualifiedType(),
147 m2p.getUnqualifiedType());
150 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
151 // as a fourth parameter to std::equal().
152 return (m1->getNumParams() != m2->getNumParams()) ||
153 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
154 m2Type->param_type_begin(), compareArgTypes);
157 // If decl is a virtual method, walk the base classes looking for methods that
158 // decl overrides. This table of overridden methods is used by IRGen to
159 // determine the vtable layout for decl's parent class.
160 void addOverridesForMethod(clang::CXXMethodDecl *decl) {
161 if (!decl->isVirtual())
164 clang::CXXBasePaths paths;
166 auto find_overridden_methods =
167 [decl](const clang::CXXBaseSpecifier *specifier,
168 clang::CXXBasePath &path) {
169 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
170 specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
172 clang::DeclarationName name = decl->getDeclName();
174 // If this is a destructor, check whether the base class destructor is
176 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
177 if (auto *baseDtorDecl = base_record->getDestructor()) {
178 if (baseDtorDecl->isVirtual()) {
179 path.Decls = baseDtorDecl;
185 // Otherwise, search for name in the base class.
186 for (path.Decls = base_record->lookup(name); !path.Decls.empty();
187 path.Decls = path.Decls.slice(1)) {
188 if (auto *method_decl =
189 llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
190 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
191 path.Decls = method_decl;
200 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
201 for (auto *overridden_decl : paths.found_decls())
202 decl->addOverriddenMethod(
203 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
208 static lldb::addr_t GetVTableAddress(Process &process,
209 VTableContextBase &vtable_ctx,
211 const ASTRecordLayout &record_layout) {
212 // Retrieve type info
213 CompilerType pointee_type;
214 CompilerType this_type(valobj.GetCompilerType());
215 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
217 return LLDB_INVALID_ADDRESS;
219 // Check if it's a pointer or reference
220 bool ptr_or_ref = false;
221 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
223 type_info = pointee_type.GetTypeInfo();
226 // We process only C++ classes
227 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
228 if ((type_info & cpp_class) != cpp_class)
229 return LLDB_INVALID_ADDRESS;
231 // Calculate offset to VTable pointer
232 lldb::offset_t vbtable_ptr_offset =
233 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
237 // We have a pointer / ref to object, so read
238 // VTable pointer from process memory
240 if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
241 return LLDB_INVALID_ADDRESS;
243 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
244 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
245 return LLDB_INVALID_ADDRESS;
247 vbtable_ptr_addr += vbtable_ptr_offset;
250 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
253 // We have an object already read from process memory,
254 // so just extract VTable pointer from it
258 auto size = valobj.GetData(data, err);
259 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
260 return LLDB_INVALID_ADDRESS;
262 return data.GetPointer(&vbtable_ptr_offset);
265 static int64_t ReadVBaseOffsetFromVTable(Process &process,
266 VTableContextBase &vtable_ctx,
267 lldb::addr_t vtable_ptr,
268 const CXXRecordDecl *cxx_record_decl,
269 const CXXRecordDecl *base_class_decl) {
270 if (vtable_ctx.isMicrosoft()) {
271 clang::MicrosoftVTableContext &msoft_vtable_ctx =
272 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
274 // Get the index into the virtual base table. The
275 // index is the index in uint32_t from vbtable_ptr
276 const unsigned vbtable_index =
277 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
278 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
280 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
284 clang::ItaniumVTableContext &itanium_vtable_ctx =
285 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
287 clang::CharUnits base_offset_offset =
288 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
290 const lldb::addr_t base_offset_addr =
291 vtable_ptr + base_offset_offset.getQuantity();
292 const uint32_t base_offset_size = process.GetAddressByteSize();
294 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
298 static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
300 const ASTRecordLayout &record_layout,
301 const CXXRecordDecl *cxx_record_decl,
302 const CXXRecordDecl *base_class_decl,
303 int32_t &bit_offset) {
304 ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
305 Process *process = exe_ctx.GetProcessPtr();
309 lldb::addr_t vtable_ptr =
310 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
311 if (vtable_ptr == LLDB_INVALID_ADDRESS)
314 auto base_offset = ReadVBaseOffsetFromVTable(
315 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
316 if (base_offset == INT64_MAX)
319 bit_offset = base_offset * 8;
324 typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
327 static ClangASTMap &GetASTMap() {
328 static ClangASTMap *g_map_ptr = nullptr;
329 static llvm::once_flag g_once_flag;
330 llvm::call_once(g_once_flag, []() {
331 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
336 bool ClangASTContext::IsOperator(const char *name,
337 clang::OverloadedOperatorKind &op_kind) {
338 if (name == nullptr || name[0] == '\0')
341 #define OPERATOR_PREFIX "operator"
342 #define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
344 const char *post_op_name = nullptr;
346 bool no_space = true;
348 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
351 post_op_name = name + OPERATOR_PREFIX_LENGTH;
353 if (post_op_name[0] == ' ') {
358 #undef OPERATOR_PREFIX
359 #undef OPERATOR_PREFIX_LENGTH
361 // This is an operator, set the overloaded operator kind to invalid in case
362 // this is a conversion operator...
363 op_kind = clang::NUM_OVERLOADED_OPERATORS;
365 switch (post_op_name[0]) {
373 if (strcmp(post_op_name, "new") == 0)
374 op_kind = clang::OO_New;
375 else if (strcmp(post_op_name, "new[]") == 0)
376 op_kind = clang::OO_Array_New;
382 if (strcmp(post_op_name, "delete") == 0)
383 op_kind = clang::OO_Delete;
384 else if (strcmp(post_op_name, "delete[]") == 0)
385 op_kind = clang::OO_Array_Delete;
389 if (post_op_name[1] == '\0')
390 op_kind = clang::OO_Plus;
391 else if (post_op_name[2] == '\0') {
392 if (post_op_name[1] == '=')
393 op_kind = clang::OO_PlusEqual;
394 else if (post_op_name[1] == '+')
395 op_kind = clang::OO_PlusPlus;
400 if (post_op_name[1] == '\0')
401 op_kind = clang::OO_Minus;
402 else if (post_op_name[2] == '\0') {
403 switch (post_op_name[1]) {
405 op_kind = clang::OO_MinusEqual;
408 op_kind = clang::OO_MinusMinus;
411 op_kind = clang::OO_Arrow;
414 } else if (post_op_name[3] == '\0') {
415 if (post_op_name[2] == '*')
416 op_kind = clang::OO_ArrowStar;
422 if (post_op_name[1] == '\0')
423 op_kind = clang::OO_Star;
424 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
425 op_kind = clang::OO_StarEqual;
429 if (post_op_name[1] == '\0')
430 op_kind = clang::OO_Slash;
431 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
432 op_kind = clang::OO_SlashEqual;
436 if (post_op_name[1] == '\0')
437 op_kind = clang::OO_Percent;
438 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
439 op_kind = clang::OO_PercentEqual;
443 if (post_op_name[1] == '\0')
444 op_kind = clang::OO_Caret;
445 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
446 op_kind = clang::OO_CaretEqual;
450 if (post_op_name[1] == '\0')
451 op_kind = clang::OO_Amp;
452 else if (post_op_name[2] == '\0') {
453 switch (post_op_name[1]) {
455 op_kind = clang::OO_AmpEqual;
458 op_kind = clang::OO_AmpAmp;
465 if (post_op_name[1] == '\0')
466 op_kind = clang::OO_Pipe;
467 else if (post_op_name[2] == '\0') {
468 switch (post_op_name[1]) {
470 op_kind = clang::OO_PipeEqual;
473 op_kind = clang::OO_PipePipe;
480 if (post_op_name[1] == '\0')
481 op_kind = clang::OO_Tilde;
485 if (post_op_name[1] == '\0')
486 op_kind = clang::OO_Exclaim;
487 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
488 op_kind = clang::OO_ExclaimEqual;
492 if (post_op_name[1] == '\0')
493 op_kind = clang::OO_Equal;
494 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
495 op_kind = clang::OO_EqualEqual;
499 if (post_op_name[1] == '\0')
500 op_kind = clang::OO_Less;
501 else if (post_op_name[2] == '\0') {
502 switch (post_op_name[1]) {
504 op_kind = clang::OO_LessLess;
507 op_kind = clang::OO_LessEqual;
510 } else if (post_op_name[3] == '\0') {
511 if (post_op_name[2] == '=')
512 op_kind = clang::OO_LessLessEqual;
517 if (post_op_name[1] == '\0')
518 op_kind = clang::OO_Greater;
519 else if (post_op_name[2] == '\0') {
520 switch (post_op_name[1]) {
522 op_kind = clang::OO_GreaterGreater;
525 op_kind = clang::OO_GreaterEqual;
528 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
529 post_op_name[3] == '\0') {
530 op_kind = clang::OO_GreaterGreaterEqual;
535 if (post_op_name[1] == '\0')
536 op_kind = clang::OO_Comma;
540 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
541 op_kind = clang::OO_Call;
545 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
546 op_kind = clang::OO_Subscript;
553 clang::AccessSpecifier
554 ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
564 case eAccessProtected:
570 static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
571 // FIXME: Cleanup per-file based stuff.
573 // Set some properties which depend solely on the input kind; it would be
574 // nice to move these to the language standard, and have the driver resolve
575 // the input kind + language standard.
576 if (IK.getLanguage() == InputKind::Asm) {
577 Opts.AsmPreprocessor = 1;
578 } else if (IK.isObjectiveC()) {
582 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
584 if (LangStd == LangStandard::lang_unspecified) {
585 // Based on the base language, pick one.
586 switch (IK.getLanguage()) {
587 case InputKind::Unknown:
588 case InputKind::LLVM_IR:
589 case InputKind::RenderScript:
590 llvm_unreachable("Invalid input kind!");
591 case InputKind::OpenCL:
592 LangStd = LangStandard::lang_opencl10;
594 case InputKind::CUDA:
595 LangStd = LangStandard::lang_cuda;
599 case InputKind::ObjC:
600 LangStd = LangStandard::lang_gnu99;
603 case InputKind::ObjCXX:
604 LangStd = LangStandard::lang_gnucxx98;
607 LangStd = LangStandard::lang_hip;
612 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
613 Opts.LineComment = Std.hasLineComments();
614 Opts.C99 = Std.isC99();
615 Opts.CPlusPlus = Std.isCPlusPlus();
616 Opts.CPlusPlus11 = Std.isCPlusPlus11();
617 Opts.Digraphs = Std.hasDigraphs();
618 Opts.GNUMode = Std.isGNUMode();
619 Opts.GNUInline = !Std.isC99();
620 Opts.HexFloats = Std.hasHexFloats();
621 Opts.ImplicitInt = Std.hasImplicitInt();
625 // OpenCL has some additional defaults.
626 if (LangStd == LangStandard::lang_opencl10) {
629 Opts.CXXOperatorNames = 1;
630 Opts.LaxVectorConversions = 1;
633 // OpenCL and C++ both have bool, true, false keywords.
634 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
636 Opts.setValueVisibilityMode(DefaultVisibility);
638 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
639 // specified, or -std is set to a conforming mode.
640 Opts.Trigraphs = !Opts.GNUMode;
641 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
642 Opts.OptimizeSize = 0;
644 // FIXME: Eliminate this dependency.
646 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
647 // Opts.Optimize = Opt != 0;
650 // This is the __NO_INLINE__ define, which just depends on things like the
651 // optimization level and -fno-inline, not actually whether the backend has
654 // FIXME: This is affected by other options (-fno-inline).
655 Opts.NoInlineDefine = !Opt;
658 ClangASTContext::ClangASTContext(const char *target_triple)
659 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
660 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
661 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
662 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
663 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
664 m_pointer_byte_size(0), m_ast_owned(false) {
665 if (target_triple && target_triple[0])
666 SetTargetTriple(target_triple);
669 //----------------------------------------------------------------------
671 //----------------------------------------------------------------------
672 ClangASTContext::~ClangASTContext() { Finalize(); }
674 ConstString ClangASTContext::GetPluginNameStatic() {
675 return ConstString("clang");
678 ConstString ClangASTContext::GetPluginName() {
679 return ClangASTContext::GetPluginNameStatic();
682 uint32_t ClangASTContext::GetPluginVersion() { return 1; }
684 lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
685 lldb_private::Module *module,
687 if (ClangASTContextSupportsLanguage(language)) {
690 arch = module->GetArchitecture();
692 arch = target->GetArchitecture();
694 if (arch.IsValid()) {
695 ArchSpec fixed_arch = arch;
696 // LLVM wants this to be set to iOS or MacOSX; if we're working on
697 // a bare-boards type image, change the triple for llvm's benefit.
698 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
699 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
700 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
701 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
702 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
703 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
705 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
710 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
712 ast_sp->SetArchitecture(fixed_arch);
715 } else if (target && target->IsValid()) {
716 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
717 new ClangASTContextForExpressions(*target));
719 ast_sp->SetArchitecture(fixed_arch);
720 ast_sp->m_scratch_ast_source_ap.reset(
721 new ClangASTSource(target->shared_from_this()));
722 lldbassert(ast_sp->getFileManager());
723 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
724 *ast_sp->getASTContext(), *ast_sp->getFileManager(), true);
725 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
726 ast_sp->m_scratch_ast_source_ap->CreateProxy());
727 ast_sp->SetExternalSource(proxy_ast_source);
733 return lldb::TypeSystemSP();
736 void ClangASTContext::EnumerateSupportedLanguages(
737 std::set<lldb::LanguageType> &languages_for_types,
738 std::set<lldb::LanguageType> &languages_for_expressions) {
739 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
740 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
741 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
742 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
743 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
744 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
746 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
747 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
748 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
749 lldb::eLanguageTypeC_plus_plus_14});
751 languages_for_types.insert(s_supported_languages_for_types.begin(),
752 s_supported_languages_for_types.end());
753 languages_for_expressions.insert(
754 s_supported_languages_for_expressions.begin(),
755 s_supported_languages_for_expressions.end());
758 void ClangASTContext::Initialize() {
759 PluginManager::RegisterPlugin(GetPluginNameStatic(),
760 "clang base AST context plug-in",
761 CreateInstance, EnumerateSupportedLanguages);
764 void ClangASTContext::Terminate() {
765 PluginManager::UnregisterPlugin(CreateInstance);
768 void ClangASTContext::Finalize() {
769 if (m_ast_ap.get()) {
770 GetASTMap().Erase(m_ast_ap.get());
775 m_builtins_ap.reset();
776 m_selector_table_ap.reset();
777 m_identifier_table_ap.reset();
778 m_target_info_ap.reset();
779 m_target_options_rp.reset();
780 m_diagnostics_engine_ap.reset();
781 m_source_manager_ap.reset();
782 m_language_options_ap.reset();
784 m_scratch_ast_source_ap.reset();
787 void ClangASTContext::Clear() {
789 m_language_options_ap.reset();
790 m_source_manager_ap.reset();
791 m_diagnostics_engine_ap.reset();
792 m_target_options_rp.reset();
793 m_target_info_ap.reset();
794 m_identifier_table_ap.reset();
795 m_selector_table_ap.reset();
796 m_builtins_ap.reset();
797 m_pointer_byte_size = 0;
800 const char *ClangASTContext::GetTargetTriple() {
801 return m_target_triple.c_str();
804 void ClangASTContext::SetTargetTriple(const char *target_triple) {
806 m_target_triple.assign(target_triple);
809 void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
810 SetTargetTriple(arch.GetTriple().str().c_str());
813 bool ClangASTContext::HasExternalSource() {
814 ASTContext *ast = getASTContext();
816 return ast->getExternalSource() != nullptr;
820 void ClangASTContext::SetExternalSource(
821 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
822 ASTContext *ast = getASTContext();
824 ast->setExternalSource(ast_source_ap);
825 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
829 void ClangASTContext::RemoveExternalSource() {
830 ASTContext *ast = getASTContext();
833 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
834 ast->setExternalSource(empty_ast_source_ap);
835 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
839 void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
844 m_ast_ap.reset(ast_ctx);
845 GetASTMap().Insert(ast_ctx, this);
848 ASTContext *ClangASTContext::getASTContext() {
849 if (m_ast_ap.get() == nullptr) {
851 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
852 *getIdentifierTable(), *getSelectorTable(),
853 *getBuiltinContext()));
855 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
857 // This can be NULL if we don't know anything about the architecture or if
858 // the target for an architecture isn't enabled in the llvm/clang that we
860 TargetInfo *target_info = getTargetInfo();
862 m_ast_ap->InitBuiltinTypes(*target_info);
864 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
865 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
866 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
869 GetASTMap().Insert(m_ast_ap.get(), this);
871 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
872 new ClangExternalASTSourceCallbacks(
873 ClangASTContext::CompleteTagDecl,
874 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
875 ClangASTContext::LayoutRecordType, this));
876 SetExternalSource(ast_source_ap);
878 return m_ast_ap.get();
881 ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
882 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
886 Builtin::Context *ClangASTContext::getBuiltinContext() {
887 if (m_builtins_ap.get() == nullptr)
888 m_builtins_ap.reset(new Builtin::Context());
889 return m_builtins_ap.get();
892 IdentifierTable *ClangASTContext::getIdentifierTable() {
893 if (m_identifier_table_ap.get() == nullptr)
894 m_identifier_table_ap.reset(
895 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
896 return m_identifier_table_ap.get();
899 LangOptions *ClangASTContext::getLanguageOptions() {
900 if (m_language_options_ap.get() == nullptr) {
901 m_language_options_ap.reset(new LangOptions());
902 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
903 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
905 return m_language_options_ap.get();
908 SelectorTable *ClangASTContext::getSelectorTable() {
909 if (m_selector_table_ap.get() == nullptr)
910 m_selector_table_ap.reset(new SelectorTable());
911 return m_selector_table_ap.get();
914 clang::FileManager *ClangASTContext::getFileManager() {
915 if (m_file_manager_ap.get() == nullptr) {
916 clang::FileSystemOptions file_system_options;
917 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
919 return m_file_manager_ap.get();
922 clang::SourceManager *ClangASTContext::getSourceManager() {
923 if (m_source_manager_ap.get() == nullptr)
924 m_source_manager_ap.reset(
925 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
926 return m_source_manager_ap.get();
929 clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
930 if (m_diagnostics_engine_ap.get() == nullptr) {
931 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
932 m_diagnostics_engine_ap.reset(
933 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
935 return m_diagnostics_engine_ap.get();
938 clang::MangleContext *ClangASTContext::getMangleContext() {
939 if (m_mangle_ctx_ap.get() == nullptr)
940 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
941 return m_mangle_ctx_ap.get();
944 class NullDiagnosticConsumer : public DiagnosticConsumer {
946 NullDiagnosticConsumer() {
947 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
950 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
951 const clang::Diagnostic &info) {
953 llvm::SmallVector<char, 32> diag_str(10);
954 info.FormatDiagnostic(diag_str);
955 diag_str.push_back('\0');
956 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
960 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
961 return new NullDiagnosticConsumer();
968 DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
969 if (m_diagnostic_consumer_ap.get() == nullptr)
970 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
972 return m_diagnostic_consumer_ap.get();
975 std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
976 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
977 m_target_options_rp = std::make_shared<clang::TargetOptions>();
978 if (m_target_options_rp.get() != nullptr)
979 m_target_options_rp->Triple = m_target_triple;
981 return m_target_options_rp;
984 TargetInfo *ClangASTContext::getTargetInfo() {
985 // target_triple should be something like "x86_64-apple-macosx"
986 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
987 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
988 getTargetOptions()));
989 return m_target_info_ap.get();
992 #pragma mark Basic Types
994 static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
995 ASTContext *ast, QualType qual_type) {
996 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
997 return qual_type_bit_size == bit_size;
1001 ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
1003 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1004 getASTContext(), encoding, bit_size);
1007 CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
1008 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
1010 return CompilerType();
1012 case eEncodingInvalid:
1013 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1014 return CompilerType(ast, ast->VoidPtrTy);
1018 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1019 return CompilerType(ast, ast->UnsignedCharTy);
1020 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1021 return CompilerType(ast, ast->UnsignedShortTy);
1022 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1023 return CompilerType(ast, ast->UnsignedIntTy);
1024 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1025 return CompilerType(ast, ast->UnsignedLongTy);
1026 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1027 return CompilerType(ast, ast->UnsignedLongLongTy);
1028 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1029 return CompilerType(ast, ast->UnsignedInt128Ty);
1033 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1034 return CompilerType(ast, ast->SignedCharTy);
1035 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1036 return CompilerType(ast, ast->ShortTy);
1037 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1038 return CompilerType(ast, ast->IntTy);
1039 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1040 return CompilerType(ast, ast->LongTy);
1041 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1042 return CompilerType(ast, ast->LongLongTy);
1043 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1044 return CompilerType(ast, ast->Int128Ty);
1047 case eEncodingIEEE754:
1048 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1049 return CompilerType(ast, ast->FloatTy);
1050 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1051 return CompilerType(ast, ast->DoubleTy);
1052 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1053 return CompilerType(ast, ast->LongDoubleTy);
1054 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1055 return CompilerType(ast, ast->HalfTy);
1058 case eEncodingVector:
1059 // Sanity check that bit_size is a multiple of 8's.
1060 if (bit_size && !(bit_size & 0x7u))
1061 return CompilerType(
1062 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
1066 return CompilerType();
1070 ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
1072 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
1073 static TypeNameToBasicTypeMap g_type_map;
1074 static llvm::once_flag g_once_flag;
1075 llvm::call_once(g_once_flag, []() {
1077 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
1080 g_type_map.Append(ConstString("char"), eBasicTypeChar);
1081 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
1082 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
1083 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
1084 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
1085 g_type_map.Append(ConstString("unsigned wchar_t"),
1086 eBasicTypeUnsignedWChar);
1088 g_type_map.Append(ConstString("short"), eBasicTypeShort);
1089 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
1090 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
1091 g_type_map.Append(ConstString("unsigned short int"),
1092 eBasicTypeUnsignedShort);
1095 g_type_map.Append(ConstString("int"), eBasicTypeInt);
1096 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
1097 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
1098 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
1101 g_type_map.Append(ConstString("long"), eBasicTypeLong);
1102 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
1103 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
1104 g_type_map.Append(ConstString("unsigned long int"),
1105 eBasicTypeUnsignedLong);
1108 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
1109 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
1110 g_type_map.Append(ConstString("unsigned long long"),
1111 eBasicTypeUnsignedLongLong);
1112 g_type_map.Append(ConstString("unsigned long long int"),
1113 eBasicTypeUnsignedLongLong);
1116 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
1117 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
1120 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1121 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1122 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1123 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1124 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1125 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1126 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
1130 return g_type_map.Find(name, eBasicTypeInvalid);
1132 return eBasicTypeInvalid;
1135 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1136 const ConstString &name) {
1138 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1139 return ClangASTContext::GetBasicType(ast, basic_type);
1141 return CompilerType();
1144 uint32_t ClangASTContext::GetPointerByteSize() {
1145 if (m_pointer_byte_size == 0)
1146 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
1148 .GetByteSize(nullptr))
1149 m_pointer_byte_size = *size;
1150 return m_pointer_byte_size;
1153 CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1154 return GetBasicType(getASTContext(), basic_type);
1157 CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1158 lldb::BasicType basic_type) {
1160 return CompilerType();
1161 lldb::opaque_compiler_type_t clang_type =
1162 GetOpaqueCompilerType(ast, basic_type);
1165 return CompilerType(GetASTContext(ast), clang_type);
1166 return CompilerType();
1169 CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1170 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1171 ASTContext *ast = getASTContext();
1173 #define streq(a, b) strcmp(a, b) == 0
1174 assert(ast != nullptr);
1180 case DW_ATE_address:
1181 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1182 return CompilerType(ast, ast->VoidPtrTy);
1185 case DW_ATE_boolean:
1186 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1187 return CompilerType(ast, ast->BoolTy);
1188 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1189 return CompilerType(ast, ast->UnsignedCharTy);
1190 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1191 return CompilerType(ast, ast->UnsignedShortTy);
1192 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1193 return CompilerType(ast, ast->UnsignedIntTy);
1196 case DW_ATE_lo_user:
1197 // This has been seen to mean DW_AT_complex_integer
1199 if (::strstr(type_name, "complex")) {
1200 CompilerType complex_int_clang_type =
1201 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1203 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1204 complex_int_clang_type)));
1209 case DW_ATE_complex_float:
1210 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1211 return CompilerType(ast, ast->FloatComplexTy);
1212 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1213 return CompilerType(ast, ast->DoubleComplexTy);
1214 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1215 return CompilerType(ast, ast->LongDoubleComplexTy);
1217 CompilerType complex_float_clang_type =
1218 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1220 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1221 complex_float_clang_type)));
1226 if (streq(type_name, "float") &&
1227 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1228 return CompilerType(ast, ast->FloatTy);
1229 if (streq(type_name, "double") &&
1230 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1231 return CompilerType(ast, ast->DoubleTy);
1232 if (streq(type_name, "long double") &&
1233 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1234 return CompilerType(ast, ast->LongDoubleTy);
1235 // Fall back to not requiring a name match
1236 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1237 return CompilerType(ast, ast->FloatTy);
1238 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1239 return CompilerType(ast, ast->DoubleTy);
1240 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1241 return CompilerType(ast, ast->LongDoubleTy);
1242 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1243 return CompilerType(ast, ast->HalfTy);
1248 if (streq(type_name, "wchar_t") &&
1249 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1251 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1252 return CompilerType(ast, ast->WCharTy);
1253 if (streq(type_name, "void") &&
1254 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1255 return CompilerType(ast, ast->VoidTy);
1256 if (strstr(type_name, "long long") &&
1257 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1258 return CompilerType(ast, ast->LongLongTy);
1259 if (strstr(type_name, "long") &&
1260 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1261 return CompilerType(ast, ast->LongTy);
1262 if (strstr(type_name, "short") &&
1263 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1264 return CompilerType(ast, ast->ShortTy);
1265 if (strstr(type_name, "char")) {
1266 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1267 return CompilerType(ast, ast->CharTy);
1268 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1269 return CompilerType(ast, ast->SignedCharTy);
1271 if (strstr(type_name, "int")) {
1272 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1273 return CompilerType(ast, ast->IntTy);
1274 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1275 return CompilerType(ast, ast->Int128Ty);
1278 // We weren't able to match up a type name, just search by size
1279 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1280 return CompilerType(ast, ast->CharTy);
1281 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1282 return CompilerType(ast, ast->ShortTy);
1283 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1284 return CompilerType(ast, ast->IntTy);
1285 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1286 return CompilerType(ast, ast->LongTy);
1287 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1288 return CompilerType(ast, ast->LongLongTy);
1289 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1290 return CompilerType(ast, ast->Int128Ty);
1293 case DW_ATE_signed_char:
1294 if (ast->getLangOpts().CharIsSigned && type_name &&
1295 streq(type_name, "char")) {
1296 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1297 return CompilerType(ast, ast->CharTy);
1299 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1300 return CompilerType(ast, ast->SignedCharTy);
1303 case DW_ATE_unsigned:
1305 if (streq(type_name, "wchar_t")) {
1306 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1307 if (!(getTargetInfo() &&
1308 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1309 return CompilerType(ast, ast->WCharTy);
1312 if (strstr(type_name, "long long")) {
1313 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1314 return CompilerType(ast, ast->UnsignedLongLongTy);
1315 } else if (strstr(type_name, "long")) {
1316 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1317 return CompilerType(ast, ast->UnsignedLongTy);
1318 } else if (strstr(type_name, "short")) {
1319 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1320 return CompilerType(ast, ast->UnsignedShortTy);
1321 } else if (strstr(type_name, "char")) {
1322 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1323 return CompilerType(ast, ast->UnsignedCharTy);
1324 } else if (strstr(type_name, "int")) {
1325 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1326 return CompilerType(ast, ast->UnsignedIntTy);
1327 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1328 return CompilerType(ast, ast->UnsignedInt128Ty);
1331 // We weren't able to match up a type name, just search by size
1332 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1333 return CompilerType(ast, ast->UnsignedCharTy);
1334 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1335 return CompilerType(ast, ast->UnsignedShortTy);
1336 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1337 return CompilerType(ast, ast->UnsignedIntTy);
1338 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1339 return CompilerType(ast, ast->UnsignedLongTy);
1340 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1341 return CompilerType(ast, ast->UnsignedLongLongTy);
1342 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1343 return CompilerType(ast, ast->UnsignedInt128Ty);
1346 case DW_ATE_unsigned_char:
1347 if (!ast->getLangOpts().CharIsSigned && type_name &&
1348 streq(type_name, "char")) {
1349 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1350 return CompilerType(ast, ast->CharTy);
1352 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1353 return CompilerType(ast, ast->UnsignedCharTy);
1354 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1355 return CompilerType(ast, ast->UnsignedShortTy);
1358 case DW_ATE_imaginary_float:
1363 if (streq(type_name, "char16_t")) {
1364 return CompilerType(ast, ast->Char16Ty);
1365 } else if (streq(type_name, "char32_t")) {
1366 return CompilerType(ast, ast->Char32Ty);
1372 // This assert should fire for anything that we don't catch above so we know
1373 // to fix any issues we run into.
1375 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1376 "DW_TAG_base_type '%s' encoded with "
1377 "DW_ATE = 0x%x, bit_size = %u\n",
1378 type_name, dw_ate, bit_size);
1380 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1381 "DW_TAG_base_type encoded with "
1382 "DW_ATE = 0x%x, bit_size = %u\n",
1385 return CompilerType();
1388 CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1390 return CompilerType(ast, ast->UnknownAnyTy);
1391 return CompilerType();
1394 CompilerType ClangASTContext::GetCStringType(bool is_const) {
1395 ASTContext *ast = getASTContext();
1396 QualType char_type(ast->CharTy);
1399 char_type.addConst();
1401 return CompilerType(ast, ast->getPointerType(char_type));
1404 clang::DeclContext *
1405 ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1406 return ast->getTranslationUnitDecl();
1409 clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1410 clang::Decl *source_decl) {
1411 FileSystemOptions file_system_options;
1412 FileManager file_manager(file_system_options);
1413 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1415 return importer.Import(source_decl);
1418 bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1419 bool ignore_qualifiers) {
1420 ClangASTContext *ast =
1421 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1422 if (!ast || ast != type2.GetTypeSystem())
1425 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1428 QualType type1_qual = ClangUtil::GetQualType(type1);
1429 QualType type2_qual = ClangUtil::GetQualType(type2);
1431 if (ignore_qualifiers) {
1432 type1_qual = type1_qual.getUnqualifiedType();
1433 type2_qual = type2_qual.getUnqualifiedType();
1436 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
1439 CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1440 if (clang::ObjCInterfaceDecl *interface_decl =
1441 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1442 return GetTypeForDecl(interface_decl);
1443 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1444 return GetTypeForDecl(tag_decl);
1445 return CompilerType();
1448 CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1449 // No need to call the getASTContext() accessor (which can create the AST if
1450 // it isn't created yet, because we can't have created a decl in this
1451 // AST if our AST didn't already exist...
1452 ASTContext *ast = &decl->getASTContext();
1454 return CompilerType(ast, ast->getTagDeclType(decl));
1455 return CompilerType();
1458 CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1459 // No need to call the getASTContext() accessor (which can create the AST if
1460 // it isn't created yet, because we can't have created a decl in this
1461 // AST if our AST didn't already exist...
1462 ASTContext *ast = &decl->getASTContext();
1464 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1465 return CompilerType();
1468 #pragma mark Structure, Unions, Classes
1470 CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1471 AccessType access_type,
1472 const char *name, int kind,
1473 LanguageType language,
1474 ClangASTMetadata *metadata) {
1475 ASTContext *ast = getASTContext();
1476 assert(ast != nullptr);
1478 if (decl_ctx == nullptr)
1479 decl_ctx = ast->getTranslationUnitDecl();
1481 if (language == eLanguageTypeObjC ||
1482 language == eLanguageTypeObjC_plus_plus) {
1483 bool isForwardDecl = true;
1484 bool isInternal = false;
1485 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1488 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1489 // we will need to update this code. I was told to currently always use the
1490 // CXXRecordDecl class since we often don't know from debug information if
1491 // something is struct or a class, so we default to always use the more
1492 // complete definition just in case.
1494 bool is_anonymous = (!name) || (!name[0]);
1496 CXXRecordDecl *decl = CXXRecordDecl::Create(
1497 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1498 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1501 decl->setAnonymousStructOrUnion(true);
1505 SetMetadata(ast, decl, *metadata);
1507 if (access_type != eAccessNone)
1508 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1511 decl_ctx->addDecl(decl);
1513 return CompilerType(ast, ast->getTagDeclType(decl));
1515 return CompilerType();
1519 bool IsValueParam(const clang::TemplateArgument &argument) {
1520 return argument.getKind() == TemplateArgument::Integral;
1524 static TemplateParameterList *CreateTemplateParameterList(
1526 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1527 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1528 const bool parameter_pack = false;
1529 const bool is_typename = false;
1530 const unsigned depth = 0;
1531 const size_t num_template_params = template_param_infos.args.size();
1532 DeclContext *const decl_context =
1533 ast->getTranslationUnitDecl(); // Is this the right decl context?,
1534 for (size_t i = 0; i < num_template_params; ++i) {
1535 const char *name = template_param_infos.names[i];
1537 IdentifierInfo *identifier_info = nullptr;
1538 if (name && name[0])
1539 identifier_info = &ast->Idents.get(name);
1540 if (IsValueParam(template_param_infos.args[i])) {
1541 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1543 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1544 template_param_infos.args[i].getIntegralType(), parameter_pack,
1548 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1550 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1551 is_typename, parameter_pack));
1555 if (template_param_infos.packed_args &&
1556 template_param_infos.packed_args->args.size()) {
1557 IdentifierInfo *identifier_info = nullptr;
1558 if (template_param_infos.pack_name && template_param_infos.pack_name[0])
1559 identifier_info = &ast->Idents.get(template_param_infos.pack_name);
1560 const bool parameter_pack_true = true;
1561 if (IsValueParam(template_param_infos.packed_args->args[0])) {
1562 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1564 SourceLocation(), SourceLocation(), depth, num_template_params,
1566 template_param_infos.packed_args->args[0].getIntegralType(),
1567 parameter_pack_true, nullptr));
1569 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1571 SourceLocation(), SourceLocation(), depth, num_template_params,
1573 is_typename, parameter_pack_true));
1576 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1577 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1578 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1579 SourceLocation(), requires_clause);
1580 return template_param_list;
1583 clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1584 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1585 const char *name, const TemplateParameterInfos &template_param_infos) {
1586 // /// Create a function template node.
1587 ASTContext *ast = getASTContext();
1589 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1591 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1592 ast, template_param_infos, template_param_decls);
1593 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1594 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1595 template_param_list, func_decl);
1597 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1598 i < template_param_decl_count; ++i) {
1599 // TODO: verify which decl context we should put template_param_decls into..
1600 template_param_decls[i]->setDeclContext(func_decl);
1603 return func_tmpl_decl;
1606 void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1607 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1608 const TemplateParameterInfos &infos) {
1609 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
1611 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1615 ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1616 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1617 int kind, const TemplateParameterInfos &template_param_infos) {
1618 ASTContext *ast = getASTContext();
1620 ClassTemplateDecl *class_template_decl = nullptr;
1621 if (decl_ctx == nullptr)
1622 decl_ctx = ast->getTranslationUnitDecl();
1624 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1625 DeclarationName decl_name(&identifier_info);
1627 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1629 for (NamedDecl *decl : result) {
1630 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1631 if (class_template_decl)
1632 return class_template_decl;
1635 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1637 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1638 ast, template_param_infos, template_param_decls);
1640 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1641 *ast, (TagDecl::TagKind)kind,
1642 decl_ctx, // What decl context do we use here? TU? The actual decl
1644 SourceLocation(), SourceLocation(), &identifier_info);
1646 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1647 i < template_param_decl_count; ++i) {
1648 template_param_decls[i]->setDeclContext(template_cxx_decl);
1651 // With templated classes, we say that a class is templated with
1652 // specializations, but that the bare class has no functions.
1653 // template_cxx_decl->startDefinition();
1654 // template_cxx_decl->completeDefinition();
1656 class_template_decl = ClassTemplateDecl::Create(
1658 decl_ctx, // What decl context do we use here? TU? The actual decl
1660 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
1662 if (class_template_decl) {
1663 if (access_type != eAccessNone)
1664 class_template_decl->setAccess(
1665 ConvertAccessTypeToAccessSpecifier(access_type));
1667 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1668 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1670 decl_ctx->addDecl(class_template_decl);
1672 #ifdef LLDB_CONFIGURATION_DEBUG
1673 VerifyDecl(class_template_decl);
1677 return class_template_decl;
1680 TemplateTemplateParmDecl *
1681 ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) {
1682 ASTContext *ast = getASTContext();
1684 auto *decl_ctx = ast->getTranslationUnitDecl();
1686 IdentifierInfo &identifier_info = ast->Idents.get(template_name);
1687 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1689 ClangASTContext::TemplateParameterInfos template_param_infos;
1690 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1691 ast, template_param_infos, template_param_decls);
1693 // LLDB needs to create those decls only to be able to display a
1694 // type that includes a template template argument. Only the name matters for
1695 // this purpose, so we use dummy values for the other characterisitcs of the
1697 return TemplateTemplateParmDecl::Create(
1698 *ast, decl_ctx, SourceLocation(),
1699 /*Depth*/ 0, /*Position*/ 0,
1700 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1703 ClassTemplateSpecializationDecl *
1704 ClangASTContext::CreateClassTemplateSpecializationDecl(
1705 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1706 const TemplateParameterInfos &template_param_infos) {
1707 ASTContext *ast = getASTContext();
1708 llvm::SmallVector<clang::TemplateArgument, 2> args(
1709 template_param_infos.args.size() +
1710 (template_param_infos.packed_args ? 1 : 0));
1711 std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1713 if (template_param_infos.packed_args) {
1714 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1715 *ast, template_param_infos.packed_args->args);
1717 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1718 ClassTemplateSpecializationDecl::Create(
1719 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1720 SourceLocation(), class_template_decl, args,
1723 class_template_specialization_decl->setSpecializationKind(
1724 TSK_ExplicitSpecialization);
1726 return class_template_specialization_decl;
1729 CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1730 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1731 if (class_template_specialization_decl) {
1732 ASTContext *ast = getASTContext();
1734 return CompilerType(
1735 ast, ast->getTagDeclType(class_template_specialization_decl));
1737 return CompilerType();
1740 static inline bool check_op_param(bool is_method,
1741 clang::OverloadedOperatorKind op_kind,
1742 bool unary, bool binary,
1743 uint32_t num_params) {
1744 // Special-case call since it can take any number of operands
1745 if (op_kind == OO_Call)
1748 // The parameter count doesn't include "this"
1751 if (num_params == 1)
1753 if (num_params == 2)
1759 bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1760 bool is_method, clang::OverloadedOperatorKind op_kind,
1761 uint32_t num_params) {
1765 // C++ standard allows any number of arguments to new/delete
1769 case OO_Array_Delete:
1773 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1775 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1777 #include "clang/Basic/OperatorKinds.def"
1784 clang::AccessSpecifier
1785 ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1786 clang::AccessSpecifier rhs) {
1787 // Make the access equal to the stricter of the field and the nested field's
1789 if (lhs == AS_none || rhs == AS_none)
1791 if (lhs == AS_private || rhs == AS_private)
1793 if (lhs == AS_protected || rhs == AS_protected)
1794 return AS_protected;
1798 bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1799 uint32_t &bitfield_bit_size) {
1800 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
1803 bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1804 uint32_t &bitfield_bit_size) {
1805 if (ast == nullptr || field == nullptr)
1808 if (field->isBitField()) {
1809 Expr *bit_width_expr = field->getBitWidth();
1810 if (bit_width_expr) {
1811 llvm::APSInt bit_width_apsint;
1812 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1813 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1821 bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1822 if (record_decl == nullptr)
1825 if (!record_decl->field_empty())
1828 // No fields, lets check this is a CXX record and check the base classes
1829 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1830 if (cxx_record_decl) {
1831 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1832 for (base_class = cxx_record_decl->bases_begin(),
1833 base_class_end = cxx_record_decl->bases_end();
1834 base_class != base_class_end; ++base_class) {
1835 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1836 base_class->getType()->getAs<RecordType>()->getDecl());
1837 if (RecordHasFields(base_class_decl))
1844 #pragma mark Objective-C Classes
1846 CompilerType ClangASTContext::CreateObjCClass(const char *name,
1847 DeclContext *decl_ctx,
1850 ClangASTMetadata *metadata) {
1851 ASTContext *ast = getASTContext();
1852 assert(ast != nullptr);
1853 assert(name && name[0]);
1854 if (decl_ctx == nullptr)
1855 decl_ctx = ast->getTranslationUnitDecl();
1857 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1858 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1859 nullptr, SourceLocation(),
1863 if (decl && metadata)
1864 SetMetadata(ast, decl, *metadata);
1866 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1869 static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1870 return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl());
1874 ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1875 bool omit_empty_base_classes) {
1876 uint32_t num_bases = 0;
1877 if (cxx_record_decl) {
1878 if (omit_empty_base_classes) {
1879 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1880 for (base_class = cxx_record_decl->bases_begin(),
1881 base_class_end = cxx_record_decl->bases_end();
1882 base_class != base_class_end; ++base_class) {
1883 // Skip empty base classes
1884 if (omit_empty_base_classes) {
1885 if (BaseSpecifierIsEmpty(base_class))
1891 num_bases = cxx_record_decl->getNumBases();
1896 #pragma mark Namespace Declarations
1899 ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1900 DeclContext *decl_ctx) {
1901 NamespaceDecl *namespace_decl = nullptr;
1902 ASTContext *ast = getASTContext();
1903 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1904 if (decl_ctx == nullptr)
1905 decl_ctx = translation_unit_decl;
1908 IdentifierInfo &identifier_info = ast->Idents.get(name);
1909 DeclarationName decl_name(&identifier_info);
1910 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1911 for (NamedDecl *decl : result) {
1912 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1914 return namespace_decl;
1918 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1919 SourceLocation(), &identifier_info, nullptr);
1921 decl_ctx->addDecl(namespace_decl);
1923 if (decl_ctx == translation_unit_decl) {
1924 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1926 return namespace_decl;
1929 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1930 SourceLocation(), nullptr, nullptr);
1931 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1932 translation_unit_decl->addDecl(namespace_decl);
1933 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1935 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1936 if (parent_namespace_decl) {
1937 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1939 return namespace_decl;
1941 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1942 SourceLocation(), nullptr, nullptr);
1943 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1944 parent_namespace_decl->addDecl(namespace_decl);
1945 assert(namespace_decl ==
1946 parent_namespace_decl->getAnonymousNamespace());
1952 #ifdef LLDB_CONFIGURATION_DEBUG
1953 VerifyDecl(namespace_decl);
1955 return namespace_decl;
1958 NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1959 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1960 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1961 if (ast_ctx == nullptr)
1964 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
1968 ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1969 if (ctx != nullptr) {
1970 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1971 clang::SourceLocation());
1978 clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1979 clang::DeclContext *right,
1980 clang::DeclContext *root) {
1981 if (root == nullptr)
1984 std::set<clang::DeclContext *> path_left;
1985 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1986 path_left.insert(d);
1988 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1989 if (path_left.find(d) != path_left.end())
1995 clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1996 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1997 if (decl_ctx != nullptr && ns_decl != nullptr) {
1998 clang::TranslationUnitDecl *translation_unit =
1999 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
2000 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
2001 *getASTContext(), decl_ctx, clang::SourceLocation(),
2002 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
2003 clang::SourceLocation(), ns_decl,
2004 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
2005 decl_ctx->addDecl(using_decl);
2012 ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2013 clang::NamedDecl *target) {
2014 if (current_decl_ctx != nullptr && target != nullptr) {
2015 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2016 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
2017 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2018 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2019 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
2021 using_decl->addShadowDecl(shadow_decl);
2022 current_decl_ctx->addDecl(using_decl);
2028 clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
2029 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
2030 if (decl_context != nullptr) {
2031 clang::VarDecl *var_decl = clang::VarDecl::Create(
2032 *getASTContext(), decl_context, clang::SourceLocation(),
2033 clang::SourceLocation(),
2034 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
2035 nullptr, clang::SC_None);
2036 var_decl->setAccess(clang::AS_public);
2037 decl_context->addDecl(var_decl);
2043 lldb::opaque_compiler_type_t
2044 ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
2045 lldb::BasicType basic_type) {
2046 switch (basic_type) {
2047 case eBasicTypeVoid:
2048 return ast->VoidTy.getAsOpaquePtr();
2049 case eBasicTypeChar:
2050 return ast->CharTy.getAsOpaquePtr();
2051 case eBasicTypeSignedChar:
2052 return ast->SignedCharTy.getAsOpaquePtr();
2053 case eBasicTypeUnsignedChar:
2054 return ast->UnsignedCharTy.getAsOpaquePtr();
2055 case eBasicTypeWChar:
2056 return ast->getWCharType().getAsOpaquePtr();
2057 case eBasicTypeSignedWChar:
2058 return ast->getSignedWCharType().getAsOpaquePtr();
2059 case eBasicTypeUnsignedWChar:
2060 return ast->getUnsignedWCharType().getAsOpaquePtr();
2061 case eBasicTypeChar16:
2062 return ast->Char16Ty.getAsOpaquePtr();
2063 case eBasicTypeChar32:
2064 return ast->Char32Ty.getAsOpaquePtr();
2065 case eBasicTypeShort:
2066 return ast->ShortTy.getAsOpaquePtr();
2067 case eBasicTypeUnsignedShort:
2068 return ast->UnsignedShortTy.getAsOpaquePtr();
2070 return ast->IntTy.getAsOpaquePtr();
2071 case eBasicTypeUnsignedInt:
2072 return ast->UnsignedIntTy.getAsOpaquePtr();
2073 case eBasicTypeLong:
2074 return ast->LongTy.getAsOpaquePtr();
2075 case eBasicTypeUnsignedLong:
2076 return ast->UnsignedLongTy.getAsOpaquePtr();
2077 case eBasicTypeLongLong:
2078 return ast->LongLongTy.getAsOpaquePtr();
2079 case eBasicTypeUnsignedLongLong:
2080 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2081 case eBasicTypeInt128:
2082 return ast->Int128Ty.getAsOpaquePtr();
2083 case eBasicTypeUnsignedInt128:
2084 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2085 case eBasicTypeBool:
2086 return ast->BoolTy.getAsOpaquePtr();
2087 case eBasicTypeHalf:
2088 return ast->HalfTy.getAsOpaquePtr();
2089 case eBasicTypeFloat:
2090 return ast->FloatTy.getAsOpaquePtr();
2091 case eBasicTypeDouble:
2092 return ast->DoubleTy.getAsOpaquePtr();
2093 case eBasicTypeLongDouble:
2094 return ast->LongDoubleTy.getAsOpaquePtr();
2095 case eBasicTypeFloatComplex:
2096 return ast->FloatComplexTy.getAsOpaquePtr();
2097 case eBasicTypeDoubleComplex:
2098 return ast->DoubleComplexTy.getAsOpaquePtr();
2099 case eBasicTypeLongDoubleComplex:
2100 return ast->LongDoubleComplexTy.getAsOpaquePtr();
2101 case eBasicTypeObjCID:
2102 return ast->getObjCIdType().getAsOpaquePtr();
2103 case eBasicTypeObjCClass:
2104 return ast->getObjCClassType().getAsOpaquePtr();
2105 case eBasicTypeObjCSel:
2106 return ast->getObjCSelType().getAsOpaquePtr();
2107 case eBasicTypeNullPtr:
2108 return ast->NullPtrTy.getAsOpaquePtr();
2114 #pragma mark Function Types
2116 clang::DeclarationName
2117 ClangASTContext::GetDeclarationName(const char *name,
2118 const CompilerType &function_clang_type) {
2119 if (!name || !name[0])
2120 return clang::DeclarationName();
2122 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2123 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2124 return DeclarationName(&getASTContext()->Idents.get(
2125 name)); // Not operator, but a regular function.
2127 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2128 // that doesn't correctly describe operators and if we try to create a method
2129 // and add it to the class, clang will assert and crash, so we need to make
2130 // sure things are acceptable.
2131 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2132 const clang::FunctionProtoType *function_type =
2133 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2134 if (function_type == nullptr)
2135 return clang::DeclarationName();
2137 const bool is_method = false;
2138 const unsigned int num_params = function_type->getNumParams();
2139 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
2140 is_method, op_kind, num_params))
2141 return clang::DeclarationName();
2143 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
2146 FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
2147 DeclContext *decl_ctx, const char *name,
2148 const CompilerType &function_clang_type, int storage, bool is_inline) {
2149 FunctionDecl *func_decl = nullptr;
2150 ASTContext *ast = getASTContext();
2151 if (decl_ctx == nullptr)
2152 decl_ctx = ast->getTranslationUnitDecl();
2154 const bool hasWrittenPrototype = true;
2155 const bool isConstexprSpecified = false;
2157 clang::DeclarationName declarationName =
2158 GetDeclarationName(name, function_clang_type);
2159 func_decl = FunctionDecl::Create(
2160 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
2161 ClangUtil::GetQualType(function_clang_type), nullptr,
2162 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
2163 isConstexprSpecified);
2165 decl_ctx->addDecl(func_decl);
2167 #ifdef LLDB_CONFIGURATION_DEBUG
2168 VerifyDecl(func_decl);
2174 CompilerType ClangASTContext::CreateFunctionType(
2175 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
2176 unsigned num_args, bool is_variadic, unsigned type_quals,
2177 clang::CallingConv cc) {
2179 return CompilerType(); // invalid AST
2181 if (!result_type || !ClangUtil::IsClangType(result_type))
2182 return CompilerType(); // invalid return type
2184 std::vector<QualType> qual_type_args;
2185 if (num_args > 0 && args == nullptr)
2186 return CompilerType(); // invalid argument array passed in
2188 // Verify that all arguments are valid and the right type
2189 for (unsigned i = 0; i < num_args; ++i) {
2191 // Make sure we have a clang type in args[i] and not a type from another
2192 // language whose name might match
2193 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2194 lldbassert(is_clang_type);
2196 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2198 return CompilerType(); // invalid argument type (must be a clang type)
2200 return CompilerType(); // invalid argument type (empty)
2203 // TODO: Detect calling convention in DWARF?
2204 FunctionProtoType::ExtProtoInfo proto_info;
2205 proto_info.ExtInfo = cc;
2206 proto_info.Variadic = is_variadic;
2207 proto_info.ExceptionSpec = EST_None;
2208 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
2209 proto_info.RefQualifier = RQ_None;
2211 return CompilerType(ast,
2212 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2213 qual_type_args, proto_info));
2216 ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2217 clang::DeclContext *decl_ctx, const char *name,
2218 const CompilerType ¶m_type, int storage) {
2219 ASTContext *ast = getASTContext();
2220 assert(ast != nullptr);
2222 ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(),
2223 name && name[0] ? &ast->Idents.get(name) : nullptr,
2224 ClangUtil::GetQualType(param_type), nullptr,
2225 (clang::StorageClass)storage, nullptr);
2226 decl_ctx->addDecl(decl);
2230 void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2231 ParmVarDecl **params,
2232 unsigned num_params) {
2234 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2238 ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2239 QualType block_type = m_ast_ap->getBlockPointerType(
2240 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2242 return CompilerType(this, block_type.getAsOpaquePtr());
2245 #pragma mark Array Types
2247 CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2248 size_t element_count,
2250 if (element_type.IsValid()) {
2251 ASTContext *ast = getASTContext();
2252 assert(ast != nullptr);
2255 return CompilerType(
2256 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2260 llvm::APInt ap_element_count(64, element_count);
2261 if (element_count == 0) {
2262 return CompilerType(ast, ast->getIncompleteArrayType(
2263 ClangUtil::GetQualType(element_type),
2264 clang::ArrayType::Normal, 0));
2266 return CompilerType(
2267 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2269 clang::ArrayType::Normal, 0));
2273 return CompilerType();
2276 CompilerType ClangASTContext::CreateStructForIdentifier(
2277 const ConstString &type_name,
2278 const std::initializer_list<std::pair<const char *, CompilerType>>
2282 if (!type_name.IsEmpty() &&
2283 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2285 lldbassert(0 && "Trying to create a type for an existing name");
2289 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2290 clang::TTK_Struct, lldb::eLanguageTypeC);
2291 StartTagDeclarationDefinition(type);
2292 for (const auto &field : type_fields)
2293 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2297 CompleteTagDeclarationDefinition(type);
2301 CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2302 const ConstString &type_name,
2303 const std::initializer_list<std::pair<const char *, CompilerType>>
2307 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2310 return CreateStructForIdentifier(type_name, type_fields, packed);
2313 #pragma mark Enumeration Types
2316 ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2317 const Declaration &decl,
2318 const CompilerType &integer_clang_type,
2320 // TODO: Do something intelligent with the Declaration object passed in
2321 // like maybe filling in the SourceLocation with it...
2322 ASTContext *ast = getASTContext();
2324 // TODO: ask about these...
2325 // const bool IsFixed = false;
2327 EnumDecl *enum_decl = EnumDecl::Create(
2328 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2329 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2330 is_scoped, // IsScoped
2331 is_scoped, // IsScopedUsingClassTag
2336 decl_ctx->addDecl(enum_decl);
2338 // TODO: check if we should be setting the promotion type too?
2339 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2341 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2343 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2345 return CompilerType();
2348 CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2353 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2354 return CompilerType(ast, ast->SignedCharTy);
2356 if (bit_size == ast->getTypeSize(ast->ShortTy))
2357 return CompilerType(ast, ast->ShortTy);
2359 if (bit_size == ast->getTypeSize(ast->IntTy))
2360 return CompilerType(ast, ast->IntTy);
2362 if (bit_size == ast->getTypeSize(ast->LongTy))
2363 return CompilerType(ast, ast->LongTy);
2365 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2366 return CompilerType(ast, ast->LongLongTy);
2368 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2369 return CompilerType(ast, ast->Int128Ty);
2371 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2372 return CompilerType(ast, ast->UnsignedCharTy);
2374 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2375 return CompilerType(ast, ast->UnsignedShortTy);
2377 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2378 return CompilerType(ast, ast->UnsignedIntTy);
2380 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2381 return CompilerType(ast, ast->UnsignedLongTy);
2383 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2384 return CompilerType(ast, ast->UnsignedLongLongTy);
2386 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2387 return CompilerType(ast, ast->UnsignedInt128Ty);
2390 return CompilerType();
2393 CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2396 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2398 return CompilerType();
2401 void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2403 DumpDeclContextHiearchy(decl_ctx->getParent());
2405 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2407 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2408 named_decl->getDeclName().getAsString().c_str());
2410 printf("%20s\n", decl_ctx->getDeclKindName());
2415 void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2416 if (decl == nullptr)
2418 DumpDeclContextHiearchy(decl->getDeclContext());
2420 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2422 printf("%20s: %s%s\n", decl->getDeclKindName(),
2423 record_decl->getDeclName().getAsString().c_str(),
2424 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2427 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2429 printf("%20s: %s\n", decl->getDeclKindName(),
2430 named_decl->getDeclName().getAsString().c_str());
2432 printf("%20s\n", decl->getDeclKindName());
2437 bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2438 clang::Decl *rhs_decl) {
2439 if (lhs_decl && rhs_decl) {
2440 //----------------------------------------------------------------------
2441 // Make sure the decl kinds match first
2442 //----------------------------------------------------------------------
2443 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2444 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2446 if (lhs_decl_kind == rhs_decl_kind) {
2447 //------------------------------------------------------------------
2448 // Now check that the decl contexts kinds are all equivalent before we
2449 // have to check any names of the decl contexts...
2450 //------------------------------------------------------------------
2451 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2452 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2453 if (lhs_decl_ctx && rhs_decl_ctx) {
2455 if (lhs_decl_ctx && rhs_decl_ctx) {
2456 const clang::Decl::Kind lhs_decl_ctx_kind =
2457 lhs_decl_ctx->getDeclKind();
2458 const clang::Decl::Kind rhs_decl_ctx_kind =
2459 rhs_decl_ctx->getDeclKind();
2460 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2461 lhs_decl_ctx = lhs_decl_ctx->getParent();
2462 rhs_decl_ctx = rhs_decl_ctx->getParent();
2464 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2472 //--------------------------------------------------------------
2473 // Now make sure the name of the decls match
2474 //--------------------------------------------------------------
2475 clang::NamedDecl *lhs_named_decl =
2476 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2477 clang::NamedDecl *rhs_named_decl =
2478 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2479 if (lhs_named_decl && rhs_named_decl) {
2480 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2481 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2482 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2483 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2490 //--------------------------------------------------------------
2491 // We know that the decl context kinds all match, so now we need to
2492 // make sure the names match as well
2493 //--------------------------------------------------------------
2494 lhs_decl_ctx = lhs_decl->getDeclContext();
2495 rhs_decl_ctx = rhs_decl->getDeclContext();
2497 switch (lhs_decl_ctx->getDeclKind()) {
2498 case clang::Decl::TranslationUnit:
2499 // We don't care about the translation unit names
2502 clang::NamedDecl *lhs_named_decl =
2503 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2504 clang::NamedDecl *rhs_named_decl =
2505 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2506 if (lhs_named_decl && rhs_named_decl) {
2507 clang::DeclarationName lhs_decl_name =
2508 lhs_named_decl->getDeclName();
2509 clang::DeclarationName rhs_decl_name =
2510 rhs_named_decl->getDeclName();
2511 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2512 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2520 lhs_decl_ctx = lhs_decl_ctx->getParent();
2521 rhs_decl_ctx = rhs_decl_ctx->getParent();
2528 bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2529 clang::Decl *decl) {
2533 ExternalASTSource *ast_source = ast->getExternalSource();
2538 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2539 if (tag_decl->isCompleteDefinition())
2542 if (!tag_decl->hasExternalLexicalStorage())
2545 ast_source->CompleteType(tag_decl);
2547 return !tag_decl->getTypeForDecl()->isIncompleteType();
2548 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2549 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2550 if (objc_interface_decl->getDefinition())
2553 if (!objc_interface_decl->hasExternalLexicalStorage())
2556 ast_source->CompleteType(objc_interface_decl);
2558 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2564 void ClangASTContext::SetMetadataAsUserID(const void *object,
2565 user_id_t user_id) {
2566 ClangASTMetadata meta_data;
2567 meta_data.SetUserID(user_id);
2568 SetMetadata(object, meta_data);
2571 void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2572 ClangASTMetadata &metadata) {
2573 ClangExternalASTSourceCommon *external_source =
2574 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2576 if (external_source)
2577 external_source->SetMetadata(object, metadata);
2580 ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2581 const void *object) {
2582 ClangExternalASTSourceCommon *external_source =
2583 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2585 if (external_source && external_source->HasMetadata(object))
2586 return external_source->GetMetadata(object);
2591 clang::DeclContext *
2592 ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2593 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2596 clang::DeclContext *
2597 ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2598 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2601 bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2603 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2605 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2607 clang::TagDecl *tag_decl =
2608 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2610 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2618 bool ClangASTContext::SetDefaultAccessForRecordFields(
2619 clang::RecordDecl *record_decl, int default_accessibility,
2620 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2623 clang::RecordDecl::field_iterator field, field_end;
2624 for (field = record_decl->field_begin(),
2625 field_end = record_decl->field_end(), field_idx = 0;
2626 field != field_end; ++field, ++field_idx) {
2627 // If no accessibility was assigned, assign the correct one
2628 if (field_idx < num_assigned_accessibilities &&
2629 assigned_accessibilities[field_idx] == clang::AS_none)
2630 field->setAccess((clang::AccessSpecifier)default_accessibility);
2637 clang::DeclContext *
2638 ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2639 return GetDeclContextForType(ClangUtil::GetQualType(type));
2642 clang::DeclContext *
2643 ClangASTContext::GetDeclContextForType(clang::QualType type) {
2647 clang::QualType qual_type = type.getCanonicalType();
2648 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2649 switch (type_class) {
2650 case clang::Type::ObjCInterface:
2651 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2653 case clang::Type::ObjCObjectPointer:
2654 return GetDeclContextForType(
2655 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2656 ->getPointeeType());
2657 case clang::Type::Record:
2658 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2659 case clang::Type::Enum:
2660 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2661 case clang::Type::Typedef:
2662 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2664 ->getUnderlyingType());
2665 case clang::Type::Auto:
2666 return GetDeclContextForType(
2667 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2668 case clang::Type::Elaborated:
2669 return GetDeclContextForType(
2670 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2671 case clang::Type::Paren:
2672 return GetDeclContextForType(
2673 llvm::cast<clang::ParenType>(qual_type)->desugar());
2677 // No DeclContext in this type...
2681 static bool GetCompleteQualType(clang::ASTContext *ast,
2682 clang::QualType qual_type,
2683 bool allow_completion = true) {
2684 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2685 switch (type_class) {
2686 case clang::Type::ConstantArray:
2687 case clang::Type::IncompleteArray:
2688 case clang::Type::VariableArray: {
2689 const clang::ArrayType *array_type =
2690 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2693 return GetCompleteQualType(ast, array_type->getElementType(),
2696 case clang::Type::Record: {
2697 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2698 if (cxx_record_decl) {
2699 if (cxx_record_decl->hasExternalLexicalStorage()) {
2700 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2701 const bool fields_loaded =
2702 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2703 if (is_complete && fields_loaded)
2706 if (!allow_completion)
2709 // Call the field_begin() accessor to for it to use the external source
2710 // to load the fields...
2711 clang::ExternalASTSource *external_ast_source =
2712 ast->getExternalSource();
2713 if (external_ast_source) {
2714 external_ast_source->CompleteType(cxx_record_decl);
2715 if (cxx_record_decl->isCompleteDefinition()) {
2716 cxx_record_decl->field_begin();
2717 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2722 const clang::TagType *tag_type =
2723 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2724 return !tag_type->isIncompleteType();
2727 case clang::Type::Enum: {
2728 const clang::TagType *tag_type =
2729 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2731 clang::TagDecl *tag_decl = tag_type->getDecl();
2733 if (tag_decl->getDefinition())
2736 if (!allow_completion)
2739 if (tag_decl->hasExternalLexicalStorage()) {
2741 clang::ExternalASTSource *external_ast_source =
2742 ast->getExternalSource();
2743 if (external_ast_source) {
2744 external_ast_source->CompleteType(tag_decl);
2745 return !tag_type->isIncompleteType();
2754 case clang::Type::ObjCObject:
2755 case clang::Type::ObjCInterface: {
2756 const clang::ObjCObjectType *objc_class_type =
2757 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2758 if (objc_class_type) {
2759 clang::ObjCInterfaceDecl *class_interface_decl =
2760 objc_class_type->getInterface();
2761 // We currently can't complete objective C types through the newly added
2762 // ASTContext because it only supports TagDecl objects right now...
2763 if (class_interface_decl) {
2764 if (class_interface_decl->getDefinition())
2767 if (!allow_completion)
2770 if (class_interface_decl->hasExternalLexicalStorage()) {
2772 clang::ExternalASTSource *external_ast_source =
2773 ast->getExternalSource();
2774 if (external_ast_source) {
2775 external_ast_source->CompleteType(class_interface_decl);
2776 return !objc_class_type->isIncompleteType();
2785 case clang::Type::Typedef:
2786 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2788 ->getUnderlyingType(),
2791 case clang::Type::Auto:
2792 return GetCompleteQualType(
2793 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2796 case clang::Type::Elaborated:
2797 return GetCompleteQualType(
2798 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2801 case clang::Type::Paren:
2802 return GetCompleteQualType(
2803 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2806 case clang::Type::Attributed:
2807 return GetCompleteQualType(
2808 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2818 static clang::ObjCIvarDecl::AccessControl
2819 ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2822 return clang::ObjCIvarDecl::None;
2824 return clang::ObjCIvarDecl::Public;
2825 case eAccessPrivate:
2826 return clang::ObjCIvarDecl::Private;
2827 case eAccessProtected:
2828 return clang::ObjCIvarDecl::Protected;
2829 case eAccessPackage:
2830 return clang::ObjCIvarDecl::Package;
2832 return clang::ObjCIvarDecl::None;
2835 //----------------------------------------------------------------------
2837 //----------------------------------------------------------------------
2839 bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2840 clang::QualType qual_type(GetCanonicalQualType(type));
2842 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2843 switch (type_class) {
2844 case clang::Type::IncompleteArray:
2845 case clang::Type::VariableArray:
2846 case clang::Type::ConstantArray:
2847 case clang::Type::ExtVector:
2848 case clang::Type::Vector:
2849 case clang::Type::Record:
2850 case clang::Type::ObjCObject:
2851 case clang::Type::ObjCInterface:
2853 case clang::Type::Auto:
2854 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2857 case clang::Type::Elaborated:
2858 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2861 case clang::Type::Typedef:
2862 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2864 ->getUnderlyingType()
2866 case clang::Type::Paren:
2867 return IsAggregateType(
2868 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2872 // The clang type does have a value
2876 bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2877 clang::QualType qual_type(GetCanonicalQualType(type));
2879 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2880 switch (type_class) {
2881 case clang::Type::Record: {
2882 if (const clang::RecordType *record_type =
2883 llvm::dyn_cast_or_null<clang::RecordType>(
2884 qual_type.getTypePtrOrNull())) {
2885 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2886 return record_decl->isAnonymousStructOrUnion();
2891 case clang::Type::Auto:
2892 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2895 case clang::Type::Elaborated:
2896 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2899 case clang::Type::Typedef:
2900 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2902 ->getUnderlyingType()
2904 case clang::Type::Paren:
2905 return IsAnonymousType(
2906 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2910 // The clang type does have a value
2914 bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2915 CompilerType *element_type_ptr,
2916 uint64_t *size, bool *is_incomplete) {
2917 clang::QualType qual_type(GetCanonicalQualType(type));
2919 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2920 switch (type_class) {
2924 case clang::Type::ConstantArray:
2925 if (element_type_ptr)
2926 element_type_ptr->SetCompilerType(
2928 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
2930 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2932 .getLimitedValue(ULLONG_MAX);
2934 *is_incomplete = false;
2937 case clang::Type::IncompleteArray:
2938 if (element_type_ptr)
2939 element_type_ptr->SetCompilerType(
2941 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2945 *is_incomplete = true;
2948 case clang::Type::VariableArray:
2949 if (element_type_ptr)
2950 element_type_ptr->SetCompilerType(
2952 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2956 *is_incomplete = false;
2959 case clang::Type::DependentSizedArray:
2960 if (element_type_ptr)
2961 element_type_ptr->SetCompilerType(
2962 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2963 ->getElementType());
2967 *is_incomplete = false;
2970 case clang::Type::Typedef:
2971 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2973 ->getUnderlyingType()
2975 element_type_ptr, size, is_incomplete);
2976 case clang::Type::Auto:
2977 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2980 element_type_ptr, size, is_incomplete);
2981 case clang::Type::Elaborated:
2982 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2985 element_type_ptr, size, is_incomplete);
2986 case clang::Type::Paren:
2988 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2989 element_type_ptr, size, is_incomplete);
2991 if (element_type_ptr)
2992 element_type_ptr->Clear();
2996 *is_incomplete = false;
3000 bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
3001 CompilerType *element_type, uint64_t *size) {
3002 clang::QualType qual_type(GetCanonicalQualType(type));
3004 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3005 switch (type_class) {
3006 case clang::Type::Vector: {
3007 const clang::VectorType *vector_type =
3008 qual_type->getAs<clang::VectorType>();
3011 *size = vector_type->getNumElements();
3014 CompilerType(getASTContext(), vector_type->getElementType());
3018 case clang::Type::ExtVector: {
3019 const clang::ExtVectorType *ext_vector_type =
3020 qual_type->getAs<clang::ExtVectorType>();
3021 if (ext_vector_type) {
3023 *size = ext_vector_type->getNumElements();
3026 CompilerType(getASTContext(), ext_vector_type->getElementType());
3036 bool ClangASTContext::IsRuntimeGeneratedType(
3037 lldb::opaque_compiler_type_t type) {
3038 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
3039 ->GetDeclContextForType(GetQualType(type));
3043 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
3046 clang::ObjCInterfaceDecl *result_iface_decl =
3047 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
3049 ClangASTMetadata *ast_metadata =
3050 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
3053 return (ast_metadata->GetISAPtr() != 0);
3056 bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
3057 return GetQualType(type).getUnqualifiedType()->isCharType();
3060 bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
3061 const bool allow_completion = false;
3062 return GetCompleteQualType(getASTContext(), GetQualType(type),
3066 bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
3067 return GetQualType(type).isConstQualified();
3070 bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
3072 CompilerType pointee_or_element_clang_type;
3074 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
3076 if (!pointee_or_element_clang_type.IsValid())
3079 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
3080 if (pointee_or_element_clang_type.IsCharType()) {
3081 if (type_flags.Test(eTypeIsArray)) {
3082 // We know the size of the array and it could be a C string since it is
3083 // an array of characters
3084 length = llvm::cast<clang::ConstantArrayType>(
3085 GetCanonicalQualType(type).getTypePtr())
3095 bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
3096 bool *is_variadic_ptr) {
3098 clang::QualType qual_type(GetCanonicalQualType(type));
3100 if (qual_type->isFunctionType()) {
3101 if (is_variadic_ptr) {
3102 const clang::FunctionProtoType *function_proto_type =
3103 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3104 if (function_proto_type)
3105 *is_variadic_ptr = function_proto_type->isVariadic();
3107 *is_variadic_ptr = false;
3112 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3113 switch (type_class) {
3116 case clang::Type::Typedef:
3117 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
3119 ->getUnderlyingType()
3122 case clang::Type::Auto:
3123 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
3127 case clang::Type::Elaborated:
3128 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
3132 case clang::Type::Paren:
3133 return IsFunctionType(
3134 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3136 case clang::Type::LValueReference:
3137 case clang::Type::RValueReference: {
3138 const clang::ReferenceType *reference_type =
3139 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3141 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3149 // Used to detect "Homogeneous Floating-point Aggregates"
3151 ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3152 CompilerType *base_type_ptr) {
3156 clang::QualType qual_type(GetCanonicalQualType(type));
3157 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3158 switch (type_class) {
3159 case clang::Type::Record:
3160 if (GetCompleteType(type)) {
3161 const clang::CXXRecordDecl *cxx_record_decl =
3162 qual_type->getAsCXXRecordDecl();
3163 if (cxx_record_decl) {
3164 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3167 const clang::RecordType *record_type =
3168 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3170 const clang::RecordDecl *record_decl = record_type->getDecl();
3172 // We are looking for a structure that contains only floating point
3174 clang::RecordDecl::field_iterator field_pos,
3175 field_end = record_decl->field_end();
3176 uint32_t num_fields = 0;
3177 bool is_hva = false;
3178 bool is_hfa = false;
3179 clang::QualType base_qual_type;
3180 uint64_t base_bitwidth = 0;
3181 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3183 clang::QualType field_qual_type = field_pos->getType();
3184 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3185 if (field_qual_type->isFloatingType()) {
3186 if (field_qual_type->isComplexType())
3189 if (num_fields == 0)
3190 base_qual_type = field_qual_type;
3195 if (field_qual_type.getTypePtr() !=
3196 base_qual_type.getTypePtr())
3200 } else if (field_qual_type->isVectorType() ||
3201 field_qual_type->isExtVectorType()) {
3202 if (num_fields == 0) {
3203 base_qual_type = field_qual_type;
3204 base_bitwidth = field_bitwidth;
3209 if (base_bitwidth != field_bitwidth)
3211 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3219 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3226 case clang::Type::Typedef:
3227 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3229 ->getUnderlyingType()
3233 case clang::Type::Auto:
3234 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3239 case clang::Type::Elaborated:
3240 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3250 size_t ClangASTContext::GetNumberOfFunctionArguments(
3251 lldb::opaque_compiler_type_t type) {
3253 clang::QualType qual_type(GetCanonicalQualType(type));
3254 const clang::FunctionProtoType *func =
3255 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3257 return func->getNumParams();
3263 ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3264 const size_t index) {
3266 clang::QualType qual_type(GetQualType(type));
3267 const clang::FunctionProtoType *func =
3268 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3270 if (index < func->getNumParams())
3271 return CompilerType(getASTContext(), func->getParamType(index));
3274 return CompilerType();
3277 bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3279 clang::QualType qual_type(GetCanonicalQualType(type));
3281 if (qual_type->isFunctionPointerType())
3284 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3285 switch (type_class) {
3288 case clang::Type::Typedef:
3289 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3291 ->getUnderlyingType()
3293 case clang::Type::Auto:
3294 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3297 case clang::Type::Elaborated:
3298 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3301 case clang::Type::Paren:
3302 return IsFunctionPointerType(
3303 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3305 case clang::Type::LValueReference:
3306 case clang::Type::RValueReference: {
3307 const clang::ReferenceType *reference_type =
3308 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3310 return IsFunctionPointerType(
3311 reference_type->getPointeeType().getAsOpaquePtr());
3318 bool ClangASTContext::IsBlockPointerType(
3319 lldb::opaque_compiler_type_t type,
3320 CompilerType *function_pointer_type_ptr) {
3322 clang::QualType qual_type(GetCanonicalQualType(type));
3324 if (qual_type->isBlockPointerType()) {
3325 if (function_pointer_type_ptr) {
3326 const clang::BlockPointerType *block_pointer_type =
3327 qual_type->getAs<clang::BlockPointerType>();
3328 QualType pointee_type = block_pointer_type->getPointeeType();
3329 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3330 *function_pointer_type_ptr =
3331 CompilerType(getASTContext(), function_pointer_type);
3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3337 switch (type_class) {
3340 case clang::Type::Typedef:
3341 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3343 ->getUnderlyingType()
3345 function_pointer_type_ptr);
3346 case clang::Type::Auto:
3347 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3350 function_pointer_type_ptr);
3351 case clang::Type::Elaborated:
3352 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3355 function_pointer_type_ptr);
3356 case clang::Type::Paren:
3357 return IsBlockPointerType(
3358 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3359 function_pointer_type_ptr);
3361 case clang::Type::LValueReference:
3362 case clang::Type::RValueReference: {
3363 const clang::ReferenceType *reference_type =
3364 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3366 return IsBlockPointerType(
3367 reference_type->getPointeeType().getAsOpaquePtr(),
3368 function_pointer_type_ptr);
3375 bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3380 clang::QualType qual_type(GetCanonicalQualType(type));
3381 const clang::BuiltinType *builtin_type =
3382 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3385 if (builtin_type->isInteger()) {
3386 is_signed = builtin_type->isSignedInteger();
3394 bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3397 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3398 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3401 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3410 bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3411 CompilerType *pointee_type) {
3413 clang::QualType qual_type(GetCanonicalQualType(type));
3414 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3415 switch (type_class) {
3416 case clang::Type::Builtin:
3417 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3420 case clang::BuiltinType::ObjCId:
3421 case clang::BuiltinType::ObjCClass:
3425 case clang::Type::ObjCObjectPointer:
3427 pointee_type->SetCompilerType(
3428 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3429 ->getPointeeType());
3431 case clang::Type::BlockPointer:
3433 pointee_type->SetCompilerType(
3435 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3437 case clang::Type::Pointer:
3439 pointee_type->SetCompilerType(
3441 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3443 case clang::Type::MemberPointer:
3445 pointee_type->SetCompilerType(
3447 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3449 case clang::Type::Typedef:
3450 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3452 ->getUnderlyingType()
3455 case clang::Type::Auto:
3456 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3460 case clang::Type::Elaborated:
3461 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3465 case clang::Type::Paren:
3466 return IsPointerType(
3467 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3474 pointee_type->Clear();
3478 bool ClangASTContext::IsPointerOrReferenceType(
3479 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3481 clang::QualType qual_type(GetCanonicalQualType(type));
3482 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3483 switch (type_class) {
3484 case clang::Type::Builtin:
3485 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3488 case clang::BuiltinType::ObjCId:
3489 case clang::BuiltinType::ObjCClass:
3493 case clang::Type::ObjCObjectPointer:
3495 pointee_type->SetCompilerType(
3496 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3497 ->getPointeeType());
3499 case clang::Type::BlockPointer:
3501 pointee_type->SetCompilerType(
3503 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3505 case clang::Type::Pointer:
3507 pointee_type->SetCompilerType(
3509 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3511 case clang::Type::MemberPointer:
3513 pointee_type->SetCompilerType(
3515 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3517 case clang::Type::LValueReference:
3519 pointee_type->SetCompilerType(
3521 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3523 case clang::Type::RValueReference:
3525 pointee_type->SetCompilerType(
3527 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3529 case clang::Type::Typedef:
3530 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3532 ->getUnderlyingType()
3535 case clang::Type::Auto:
3536 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3540 case clang::Type::Elaborated:
3541 return IsPointerOrReferenceType(
3542 llvm::cast<clang::ElaboratedType>(qual_type)
3546 case clang::Type::Paren:
3547 return IsPointerOrReferenceType(
3548 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3555 pointee_type->Clear();
3559 bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3560 CompilerType *pointee_type,
3563 clang::QualType qual_type(GetCanonicalQualType(type));
3564 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3566 switch (type_class) {
3567 case clang::Type::LValueReference:
3569 pointee_type->SetCompilerType(
3571 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3575 case clang::Type::RValueReference:
3577 pointee_type->SetCompilerType(
3579 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3583 case clang::Type::Typedef:
3584 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3586 ->getUnderlyingType()
3588 pointee_type, is_rvalue);
3589 case clang::Type::Auto:
3590 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3593 pointee_type, is_rvalue);
3594 case clang::Type::Elaborated:
3595 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3598 pointee_type, is_rvalue);
3599 case clang::Type::Paren:
3600 return IsReferenceType(
3601 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3602 pointee_type, is_rvalue);
3609 pointee_type->Clear();
3613 bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3614 uint32_t &count, bool &is_complex) {
3616 clang::QualType qual_type(GetCanonicalQualType(type));
3618 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3619 qual_type->getCanonicalTypeInternal())) {
3620 clang::BuiltinType::Kind kind = BT->getKind();
3621 if (kind >= clang::BuiltinType::Float &&
3622 kind <= clang::BuiltinType::LongDouble) {
3627 } else if (const clang::ComplexType *CT =
3628 llvm::dyn_cast<clang::ComplexType>(
3629 qual_type->getCanonicalTypeInternal())) {
3630 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3636 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3637 qual_type->getCanonicalTypeInternal())) {
3638 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3640 count = VT->getNumElements();
3651 bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3655 clang::QualType qual_type(GetQualType(type));
3656 const clang::TagType *tag_type =
3657 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3659 clang::TagDecl *tag_decl = tag_type->getDecl();
3661 return tag_decl->isCompleteDefinition();
3664 const clang::ObjCObjectType *objc_class_type =
3665 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3666 if (objc_class_type) {
3667 clang::ObjCInterfaceDecl *class_interface_decl =
3668 objc_class_type->getInterface();
3669 if (class_interface_decl)
3670 return class_interface_decl->getDefinition() != nullptr;
3677 bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3679 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3681 const clang::ObjCObjectPointerType *obj_pointer_type =
3682 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3684 if (obj_pointer_type)
3685 return obj_pointer_type->isObjCClassType();
3690 bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3691 if (ClangUtil::IsClangType(type))
3692 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3696 bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3699 clang::QualType qual_type(GetCanonicalQualType(type));
3700 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3701 return (type_class == clang::Type::Record);
3704 bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3707 clang::QualType qual_type(GetCanonicalQualType(type));
3708 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3709 return (type_class == clang::Type::Enum);
3712 bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3714 clang::QualType qual_type(GetCanonicalQualType(type));
3715 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3716 switch (type_class) {
3717 case clang::Type::Record:
3718 if (GetCompleteType(type)) {
3719 const clang::RecordType *record_type =
3720 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3721 const clang::RecordDecl *record_decl = record_type->getDecl();
3723 const clang::CXXRecordDecl *cxx_record_decl =
3724 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3725 if (cxx_record_decl)
3726 return cxx_record_decl->isPolymorphic();
3738 bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3739 CompilerType *dynamic_pointee_type,
3740 bool check_cplusplus,
3742 clang::QualType pointee_qual_type;
3744 clang::QualType qual_type(GetCanonicalQualType(type));
3745 bool success = false;
3746 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3747 switch (type_class) {
3748 case clang::Type::Builtin:
3750 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3751 clang::BuiltinType::ObjCId) {
3752 if (dynamic_pointee_type)
3753 dynamic_pointee_type->SetCompilerType(this, type);
3758 case clang::Type::ObjCObjectPointer:
3760 if (auto objc_pointee_type =
3761 qual_type->getPointeeType().getTypePtrOrNull()) {
3762 if (auto objc_object_type =
3763 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3764 objc_pointee_type)) {
3765 if (objc_object_type->isObjCClass())
3769 if (dynamic_pointee_type)
3770 dynamic_pointee_type->SetCompilerType(
3772 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3773 ->getPointeeType());
3778 case clang::Type::Pointer:
3780 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3784 case clang::Type::LValueReference:
3785 case clang::Type::RValueReference:
3787 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3791 case clang::Type::Typedef:
3792 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3794 ->getUnderlyingType()
3796 dynamic_pointee_type, check_cplusplus,
3799 case clang::Type::Auto:
3800 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3803 dynamic_pointee_type, check_cplusplus,
3806 case clang::Type::Elaborated:
3807 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3810 dynamic_pointee_type, check_cplusplus,
3813 case clang::Type::Paren:
3814 return IsPossibleDynamicType(
3815 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3816 dynamic_pointee_type, check_cplusplus, check_objc);
3822 // Check to make sure what we are pointing too is a possible dynamic C++
3823 // type We currently accept any "void *" (in case we have a class that
3824 // has been watered down to an opaque pointer) and virtual C++ classes.
3825 const clang::Type::TypeClass pointee_type_class =
3826 pointee_qual_type.getCanonicalType()->getTypeClass();
3827 switch (pointee_type_class) {
3828 case clang::Type::Builtin:
3829 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3830 case clang::BuiltinType::UnknownAny:
3831 case clang::BuiltinType::Void:
3832 if (dynamic_pointee_type)
3833 dynamic_pointee_type->SetCompilerType(getASTContext(),
3841 case clang::Type::Record:
3842 if (check_cplusplus) {
3843 clang::CXXRecordDecl *cxx_record_decl =
3844 pointee_qual_type->getAsCXXRecordDecl();
3845 if (cxx_record_decl) {
3846 bool is_complete = cxx_record_decl->isCompleteDefinition();
3849 success = cxx_record_decl->isDynamicClass();
3851 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3852 getASTContext(), cxx_record_decl);
3854 success = metadata->GetIsDynamicCXXType();
3856 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3859 success = cxx_record_decl->isDynamicClass();
3866 if (dynamic_pointee_type)
3867 dynamic_pointee_type->SetCompilerType(getASTContext(),
3875 case clang::Type::ObjCObject:
3876 case clang::Type::ObjCInterface:
3878 if (dynamic_pointee_type)
3879 dynamic_pointee_type->SetCompilerType(getASTContext(),
3890 if (dynamic_pointee_type)
3891 dynamic_pointee_type->Clear();
3895 bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3899 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3902 bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3905 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3908 bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3911 return GetCanonicalQualType(type)->isVoidType();
3914 bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3915 return ClangASTContextSupportsLanguage(language);
3918 bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3919 std::string &class_name) {
3921 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3922 if (!qual_type.isNull()) {
3923 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3924 if (cxx_record_decl) {
3925 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3934 bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3938 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3939 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
3942 bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3945 clang::QualType qual_type(GetCanonicalQualType(type));
3946 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3948 return tag_type->isBeingDefined();
3952 bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3953 CompilerType *class_type_ptr) {
3957 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3959 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3960 if (class_type_ptr) {
3961 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3962 const clang::ObjCObjectPointerType *obj_pointer_type =
3963 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3964 if (obj_pointer_type == nullptr)
3965 class_type_ptr->Clear();
3967 class_type_ptr->SetCompilerType(
3968 type.GetTypeSystem(),
3969 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3976 class_type_ptr->Clear();
3980 bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3981 std::string &class_name) {
3985 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3987 const clang::ObjCObjectType *object_type =
3988 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3990 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3992 class_name = interface->getNameAsString();
3999 //----------------------------------------------------------------------
4001 //----------------------------------------------------------------------
4003 bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
4006 const bool allow_completion = true;
4007 return GetCompleteQualType(getASTContext(), GetQualType(type),
4011 ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
4012 std::string type_name;
4014 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
4015 clang::QualType qual_type(GetQualType(type));
4016 printing_policy.SuppressTagKeyword = true;
4017 const clang::TypedefType *typedef_type =
4018 qual_type->getAs<clang::TypedefType>();
4020 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4021 type_name = typedef_decl->getQualifiedNameAsString();
4023 type_name = qual_type.getAsString(printing_policy);
4026 return ConstString(type_name);
4030 ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
4031 CompilerType *pointee_or_element_clang_type) {
4035 if (pointee_or_element_clang_type)
4036 pointee_or_element_clang_type->Clear();
4038 clang::QualType qual_type(GetQualType(type));
4040 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4041 switch (type_class) {
4042 case clang::Type::Attributed:
4044 qual_type->getAs<clang::AttributedType>()
4045 ->getModifiedType().getAsOpaquePtr(),
4046 pointee_or_element_clang_type);
4047 case clang::Type::Builtin: {
4048 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
4049 qual_type->getCanonicalTypeInternal());
4051 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
4052 switch (builtin_type->getKind()) {
4053 case clang::BuiltinType::ObjCId:
4054 case clang::BuiltinType::ObjCClass:
4055 if (pointee_or_element_clang_type)
4056 pointee_or_element_clang_type->SetCompilerType(
4057 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
4058 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4061 case clang::BuiltinType::ObjCSel:
4062 if (pointee_or_element_clang_type)
4063 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
4064 getASTContext()->CharTy);
4065 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
4068 case clang::BuiltinType::Bool:
4069 case clang::BuiltinType::Char_U:
4070 case clang::BuiltinType::UChar:
4071 case clang::BuiltinType::WChar_U:
4072 case clang::BuiltinType::Char16:
4073 case clang::BuiltinType::Char32:
4074 case clang::BuiltinType::UShort:
4075 case clang::BuiltinType::UInt:
4076 case clang::BuiltinType::ULong:
4077 case clang::BuiltinType::ULongLong:
4078 case clang::BuiltinType::UInt128:
4079 case clang::BuiltinType::Char_S:
4080 case clang::BuiltinType::SChar:
4081 case clang::BuiltinType::WChar_S:
4082 case clang::BuiltinType::Short:
4083 case clang::BuiltinType::Int:
4084 case clang::BuiltinType::Long:
4085 case clang::BuiltinType::LongLong:
4086 case clang::BuiltinType::Int128:
4087 case clang::BuiltinType::Float:
4088 case clang::BuiltinType::Double:
4089 case clang::BuiltinType::LongDouble:
4090 builtin_type_flags |= eTypeIsScalar;
4091 if (builtin_type->isInteger()) {
4092 builtin_type_flags |= eTypeIsInteger;
4093 if (builtin_type->isSignedInteger())
4094 builtin_type_flags |= eTypeIsSigned;
4095 } else if (builtin_type->isFloatingPoint())
4096 builtin_type_flags |= eTypeIsFloat;
4101 return builtin_type_flags;
4104 case clang::Type::BlockPointer:
4105 if (pointee_or_element_clang_type)
4106 pointee_or_element_clang_type->SetCompilerType(
4107 getASTContext(), qual_type->getPointeeType());
4108 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
4110 case clang::Type::Complex: {
4111 uint32_t complex_type_flags =
4112 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
4113 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
4114 qual_type->getCanonicalTypeInternal());
4116 clang::QualType complex_element_type(complex_type->getElementType());
4117 if (complex_element_type->isIntegerType())
4118 complex_type_flags |= eTypeIsFloat;
4119 else if (complex_element_type->isFloatingType())
4120 complex_type_flags |= eTypeIsInteger;
4122 return complex_type_flags;
4125 case clang::Type::ConstantArray:
4126 case clang::Type::DependentSizedArray:
4127 case clang::Type::IncompleteArray:
4128 case clang::Type::VariableArray:
4129 if (pointee_or_element_clang_type)
4130 pointee_or_element_clang_type->SetCompilerType(
4131 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
4132 ->getElementType());
4133 return eTypeHasChildren | eTypeIsArray;
4135 case clang::Type::DependentName:
4137 case clang::Type::DependentSizedExtVector:
4138 return eTypeHasChildren | eTypeIsVector;
4139 case clang::Type::DependentTemplateSpecialization:
4140 return eTypeIsTemplate;
4141 case clang::Type::Decltype:
4142 return CompilerType(
4144 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
4145 .GetTypeInfo(pointee_or_element_clang_type);
4147 case clang::Type::Enum:
4148 if (pointee_or_element_clang_type)
4149 pointee_or_element_clang_type->SetCompilerType(
4151 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4152 return eTypeIsEnumeration | eTypeHasValue;
4154 case clang::Type::Auto:
4155 return CompilerType(
4157 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4158 .GetTypeInfo(pointee_or_element_clang_type);
4159 case clang::Type::Elaborated:
4160 return CompilerType(
4162 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4163 .GetTypeInfo(pointee_or_element_clang_type);
4164 case clang::Type::Paren:
4165 return CompilerType(getASTContext(),
4166 llvm::cast<clang::ParenType>(qual_type)->desugar())
4167 .GetTypeInfo(pointee_or_element_clang_type);
4169 case clang::Type::FunctionProto:
4170 return eTypeIsFuncPrototype | eTypeHasValue;
4171 case clang::Type::FunctionNoProto:
4172 return eTypeIsFuncPrototype | eTypeHasValue;
4173 case clang::Type::InjectedClassName:
4176 case clang::Type::LValueReference:
4177 case clang::Type::RValueReference:
4178 if (pointee_or_element_clang_type)
4179 pointee_or_element_clang_type->SetCompilerType(
4181 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4182 ->getPointeeType());
4183 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4185 case clang::Type::MemberPointer:
4186 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4188 case clang::Type::ObjCObjectPointer:
4189 if (pointee_or_element_clang_type)
4190 pointee_or_element_clang_type->SetCompilerType(
4191 getASTContext(), qual_type->getPointeeType());
4192 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4195 case clang::Type::ObjCObject:
4196 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4197 case clang::Type::ObjCInterface:
4198 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4200 case clang::Type::Pointer:
4201 if (pointee_or_element_clang_type)
4202 pointee_or_element_clang_type->SetCompilerType(
4203 getASTContext(), qual_type->getPointeeType());
4204 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4206 case clang::Type::Record:
4207 if (qual_type->getAsCXXRecordDecl())
4208 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4210 return eTypeHasChildren | eTypeIsStructUnion;
4212 case clang::Type::SubstTemplateTypeParm:
4213 return eTypeIsTemplate;
4214 case clang::Type::TemplateTypeParm:
4215 return eTypeIsTemplate;
4216 case clang::Type::TemplateSpecialization:
4217 return eTypeIsTemplate;
4219 case clang::Type::Typedef:
4220 return eTypeIsTypedef |
4221 CompilerType(getASTContext(),
4222 llvm::cast<clang::TypedefType>(qual_type)
4224 ->getUnderlyingType())
4225 .GetTypeInfo(pointee_or_element_clang_type);
4226 case clang::Type::TypeOfExpr:
4227 return CompilerType(getASTContext(),
4228 llvm::cast<clang::TypeOfExprType>(qual_type)
4229 ->getUnderlyingExpr()
4231 .GetTypeInfo(pointee_or_element_clang_type);
4232 case clang::Type::TypeOf:
4233 return CompilerType(
4235 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4236 .GetTypeInfo(pointee_or_element_clang_type);
4237 case clang::Type::UnresolvedUsing:
4240 case clang::Type::ExtVector:
4241 case clang::Type::Vector: {
4242 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4243 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4244 qual_type->getCanonicalTypeInternal());
4246 if (vector_type->isIntegerType())
4247 vector_type_flags |= eTypeIsFloat;
4248 else if (vector_type->isFloatingType())
4249 vector_type_flags |= eTypeIsInteger;
4251 return vector_type_flags;
4260 ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4262 return lldb::eLanguageTypeC;
4264 // If the type is a reference, then resolve it to what it refers to first:
4265 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4266 if (qual_type->isAnyPointerType()) {
4267 if (qual_type->isObjCObjectPointerType())
4268 return lldb::eLanguageTypeObjC;
4270 clang::QualType pointee_type(qual_type->getPointeeType());
4271 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4272 return lldb::eLanguageTypeC_plus_plus;
4273 if (pointee_type->isObjCObjectOrInterfaceType())
4274 return lldb::eLanguageTypeObjC;
4275 if (pointee_type->isObjCClassType())
4276 return lldb::eLanguageTypeObjC;
4277 if (pointee_type.getTypePtr() ==
4278 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4279 return lldb::eLanguageTypeObjC;
4281 if (qual_type->isObjCObjectOrInterfaceType())
4282 return lldb::eLanguageTypeObjC;
4283 if (qual_type->getAsCXXRecordDecl())
4284 return lldb::eLanguageTypeC_plus_plus;
4285 switch (qual_type->getTypeClass()) {
4288 case clang::Type::Builtin:
4289 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4291 case clang::BuiltinType::Void:
4292 case clang::BuiltinType::Bool:
4293 case clang::BuiltinType::Char_U:
4294 case clang::BuiltinType::UChar:
4295 case clang::BuiltinType::WChar_U:
4296 case clang::BuiltinType::Char16:
4297 case clang::BuiltinType::Char32:
4298 case clang::BuiltinType::UShort:
4299 case clang::BuiltinType::UInt:
4300 case clang::BuiltinType::ULong:
4301 case clang::BuiltinType::ULongLong:
4302 case clang::BuiltinType::UInt128:
4303 case clang::BuiltinType::Char_S:
4304 case clang::BuiltinType::SChar:
4305 case clang::BuiltinType::WChar_S:
4306 case clang::BuiltinType::Short:
4307 case clang::BuiltinType::Int:
4308 case clang::BuiltinType::Long:
4309 case clang::BuiltinType::LongLong:
4310 case clang::BuiltinType::Int128:
4311 case clang::BuiltinType::Float:
4312 case clang::BuiltinType::Double:
4313 case clang::BuiltinType::LongDouble:
4316 case clang::BuiltinType::NullPtr:
4317 return eLanguageTypeC_plus_plus;
4319 case clang::BuiltinType::ObjCId:
4320 case clang::BuiltinType::ObjCClass:
4321 case clang::BuiltinType::ObjCSel:
4322 return eLanguageTypeObjC;
4324 case clang::BuiltinType::Dependent:
4325 case clang::BuiltinType::Overload:
4326 case clang::BuiltinType::BoundMember:
4327 case clang::BuiltinType::UnknownAny:
4331 case clang::Type::Typedef:
4332 return CompilerType(getASTContext(),
4333 llvm::cast<clang::TypedefType>(qual_type)
4335 ->getUnderlyingType())
4336 .GetMinimumLanguage();
4339 return lldb::eLanguageTypeC;
4343 ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4345 return lldb::eTypeClassInvalid;
4347 clang::QualType qual_type(GetQualType(type));
4349 switch (qual_type->getTypeClass()) {
4350 case clang::Type::UnaryTransform:
4352 case clang::Type::FunctionNoProto:
4353 return lldb::eTypeClassFunction;
4354 case clang::Type::FunctionProto:
4355 return lldb::eTypeClassFunction;
4356 case clang::Type::IncompleteArray:
4357 return lldb::eTypeClassArray;
4358 case clang::Type::VariableArray:
4359 return lldb::eTypeClassArray;
4360 case clang::Type::ConstantArray:
4361 return lldb::eTypeClassArray;
4362 case clang::Type::DependentSizedArray:
4363 return lldb::eTypeClassArray;
4364 case clang::Type::DependentSizedExtVector:
4365 return lldb::eTypeClassVector;
4366 case clang::Type::DependentVector:
4367 return lldb::eTypeClassVector;
4368 case clang::Type::ExtVector:
4369 return lldb::eTypeClassVector;
4370 case clang::Type::Vector:
4371 return lldb::eTypeClassVector;
4372 case clang::Type::Builtin:
4373 return lldb::eTypeClassBuiltin;
4374 case clang::Type::ObjCObjectPointer:
4375 return lldb::eTypeClassObjCObjectPointer;
4376 case clang::Type::BlockPointer:
4377 return lldb::eTypeClassBlockPointer;
4378 case clang::Type::Pointer:
4379 return lldb::eTypeClassPointer;
4380 case clang::Type::LValueReference:
4381 return lldb::eTypeClassReference;
4382 case clang::Type::RValueReference:
4383 return lldb::eTypeClassReference;
4384 case clang::Type::MemberPointer:
4385 return lldb::eTypeClassMemberPointer;
4386 case clang::Type::Complex:
4387 if (qual_type->isComplexType())
4388 return lldb::eTypeClassComplexFloat;
4390 return lldb::eTypeClassComplexInteger;
4391 case clang::Type::ObjCObject:
4392 return lldb::eTypeClassObjCObject;
4393 case clang::Type::ObjCInterface:
4394 return lldb::eTypeClassObjCInterface;
4395 case clang::Type::Record: {
4396 const clang::RecordType *record_type =
4397 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4398 const clang::RecordDecl *record_decl = record_type->getDecl();
4399 if (record_decl->isUnion())
4400 return lldb::eTypeClassUnion;
4401 else if (record_decl->isStruct())
4402 return lldb::eTypeClassStruct;
4404 return lldb::eTypeClassClass;
4406 case clang::Type::Enum:
4407 return lldb::eTypeClassEnumeration;
4408 case clang::Type::Typedef:
4409 return lldb::eTypeClassTypedef;
4410 case clang::Type::UnresolvedUsing:
4412 case clang::Type::Paren:
4413 return CompilerType(getASTContext(),
4414 llvm::cast<clang::ParenType>(qual_type)->desugar())
4416 case clang::Type::Auto:
4417 return CompilerType(
4419 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4421 case clang::Type::Elaborated:
4422 return CompilerType(
4424 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4427 case clang::Type::Attributed:
4429 case clang::Type::TemplateTypeParm:
4431 case clang::Type::SubstTemplateTypeParm:
4433 case clang::Type::SubstTemplateTypeParmPack:
4435 case clang::Type::InjectedClassName:
4437 case clang::Type::DependentName:
4439 case clang::Type::DependentTemplateSpecialization:
4441 case clang::Type::PackExpansion:
4444 case clang::Type::TypeOfExpr:
4445 return CompilerType(getASTContext(),
4446 llvm::cast<clang::TypeOfExprType>(qual_type)
4447 ->getUnderlyingExpr()
4450 case clang::Type::TypeOf:
4451 return CompilerType(
4453 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4455 case clang::Type::Decltype:
4456 return CompilerType(
4458 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
4460 case clang::Type::TemplateSpecialization:
4462 case clang::Type::DeducedTemplateSpecialization:
4464 case clang::Type::Atomic:
4466 case clang::Type::Pipe:
4469 // pointer type decayed from an array or function type.
4470 case clang::Type::Decayed:
4472 case clang::Type::Adjusted:
4474 case clang::Type::ObjCTypeParam:
4477 case clang::Type::DependentAddressSpace:
4480 // We don't know hot to display this type...
4481 return lldb::eTypeClassOther;
4484 unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4486 return GetQualType(type).getQualifiers().getCVRQualifiers();
4490 //----------------------------------------------------------------------
4491 // Creating related types
4492 //----------------------------------------------------------------------
4495 ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4498 clang::QualType qual_type(GetCanonicalQualType(type));
4500 const clang::Type *array_eletype =
4501 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4504 return CompilerType();
4506 CompilerType element_type(getASTContext(),
4507 array_eletype->getCanonicalTypeUnqualified());
4509 // TODO: the real stride will be >= this value.. find the real one!
4511 if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
4514 return element_type;
4516 return CompilerType();
4519 CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4522 clang::QualType qual_type(GetCanonicalQualType(type));
4523 if (clang::ASTContext *ast_ctx = getASTContext()) {
4525 return CompilerType(
4526 ast_ctx, ast_ctx->getConstantArrayType(
4527 qual_type, llvm::APInt(64, size),
4528 clang::ArrayType::ArraySizeModifier::Normal, 0));
4530 return CompilerType(
4532 ast_ctx->getIncompleteArrayType(
4533 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4537 return CompilerType();
4541 ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4543 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4544 return CompilerType();
4547 static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4548 clang::QualType qual_type) {
4549 if (qual_type->isPointerType())
4550 qual_type = ast->getPointerType(
4551 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4553 qual_type = qual_type.getUnqualifiedType();
4554 qual_type.removeLocalConst();
4555 qual_type.removeLocalRestrict();
4556 qual_type.removeLocalVolatile();
4561 ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4563 return CompilerType(
4565 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4566 return CompilerType();
4569 int ClangASTContext::GetFunctionArgumentCount(
4570 lldb::opaque_compiler_type_t type) {
4572 const clang::FunctionProtoType *func =
4573 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4575 return func->getNumParams();
4580 CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4581 lldb::opaque_compiler_type_t type, size_t idx) {
4583 const clang::FunctionProtoType *func =
4584 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4586 const uint32_t num_args = func->getNumParams();
4588 return CompilerType(getASTContext(), func->getParamType(idx));
4591 return CompilerType();
4595 ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4597 clang::QualType qual_type(GetQualType(type));
4598 const clang::FunctionProtoType *func =
4599 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4601 return CompilerType(getASTContext(), func->getReturnType());
4603 return CompilerType();
4607 ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4608 size_t num_functions = 0;
4610 clang::QualType qual_type(GetCanonicalQualType(type));
4611 switch (qual_type->getTypeClass()) {
4612 case clang::Type::Record:
4613 if (GetCompleteQualType(getASTContext(), qual_type)) {
4614 const clang::RecordType *record_type =
4615 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4616 const clang::RecordDecl *record_decl = record_type->getDecl();
4617 assert(record_decl);
4618 const clang::CXXRecordDecl *cxx_record_decl =
4619 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4620 if (cxx_record_decl)
4621 num_functions = std::distance(cxx_record_decl->method_begin(),
4622 cxx_record_decl->method_end());
4626 case clang::Type::ObjCObjectPointer: {
4627 const clang::ObjCObjectPointerType *objc_class_type =
4628 qual_type->getAs<clang::ObjCObjectPointerType>();
4629 const clang::ObjCInterfaceType *objc_interface_type =
4630 objc_class_type->getInterfaceType();
4631 if (objc_interface_type &&
4632 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4633 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4634 clang::ObjCInterfaceDecl *class_interface_decl =
4635 objc_interface_type->getDecl();
4636 if (class_interface_decl) {
4637 num_functions = std::distance(class_interface_decl->meth_begin(),
4638 class_interface_decl->meth_end());
4644 case clang::Type::ObjCObject:
4645 case clang::Type::ObjCInterface:
4646 if (GetCompleteType(type)) {
4647 const clang::ObjCObjectType *objc_class_type =
4648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4649 if (objc_class_type) {
4650 clang::ObjCInterfaceDecl *class_interface_decl =
4651 objc_class_type->getInterface();
4652 if (class_interface_decl)
4653 num_functions = std::distance(class_interface_decl->meth_begin(),
4654 class_interface_decl->meth_end());
4659 case clang::Type::Typedef:
4660 return CompilerType(getASTContext(),
4661 llvm::cast<clang::TypedefType>(qual_type)
4663 ->getUnderlyingType())
4664 .GetNumMemberFunctions();
4666 case clang::Type::Auto:
4667 return CompilerType(
4669 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4670 .GetNumMemberFunctions();
4672 case clang::Type::Elaborated:
4673 return CompilerType(
4675 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4676 .GetNumMemberFunctions();
4678 case clang::Type::Paren:
4679 return CompilerType(getASTContext(),
4680 llvm::cast<clang::ParenType>(qual_type)->desugar())
4681 .GetNumMemberFunctions();
4687 return num_functions;
4690 TypeMemberFunctionImpl
4691 ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4694 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4695 CompilerType clang_type;
4696 CompilerDecl clang_decl;
4698 clang::QualType qual_type(GetCanonicalQualType(type));
4699 switch (qual_type->getTypeClass()) {
4700 case clang::Type::Record:
4701 if (GetCompleteQualType(getASTContext(), qual_type)) {
4702 const clang::RecordType *record_type =
4703 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4704 const clang::RecordDecl *record_decl = record_type->getDecl();
4705 assert(record_decl);
4706 const clang::CXXRecordDecl *cxx_record_decl =
4707 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4708 if (cxx_record_decl) {
4709 auto method_iter = cxx_record_decl->method_begin();
4710 auto method_end = cxx_record_decl->method_end();
4712 static_cast<size_t>(std::distance(method_iter, method_end))) {
4713 std::advance(method_iter, idx);
4714 clang::CXXMethodDecl *cxx_method_decl =
4715 method_iter->getCanonicalDecl();
4716 if (cxx_method_decl) {
4717 name = cxx_method_decl->getDeclName().getAsString();
4718 if (cxx_method_decl->isStatic())
4719 kind = lldb::eMemberFunctionKindStaticMethod;
4720 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4721 kind = lldb::eMemberFunctionKindConstructor;
4722 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4723 kind = lldb::eMemberFunctionKindDestructor;
4725 kind = lldb::eMemberFunctionKindInstanceMethod;
4726 clang_type = CompilerType(
4727 this, cxx_method_decl->getType().getAsOpaquePtr());
4728 clang_decl = CompilerDecl(this, cxx_method_decl);
4735 case clang::Type::ObjCObjectPointer: {
4736 const clang::ObjCObjectPointerType *objc_class_type =
4737 qual_type->getAs<clang::ObjCObjectPointerType>();
4738 const clang::ObjCInterfaceType *objc_interface_type =
4739 objc_class_type->getInterfaceType();
4740 if (objc_interface_type &&
4741 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4742 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4743 clang::ObjCInterfaceDecl *class_interface_decl =
4744 objc_interface_type->getDecl();
4745 if (class_interface_decl) {
4746 auto method_iter = class_interface_decl->meth_begin();
4747 auto method_end = class_interface_decl->meth_end();
4749 static_cast<size_t>(std::distance(method_iter, method_end))) {
4750 std::advance(method_iter, idx);
4751 clang::ObjCMethodDecl *objc_method_decl =
4752 method_iter->getCanonicalDecl();
4753 if (objc_method_decl) {
4754 clang_decl = CompilerDecl(this, objc_method_decl);
4755 name = objc_method_decl->getSelector().getAsString();
4756 if (objc_method_decl->isClassMethod())
4757 kind = lldb::eMemberFunctionKindStaticMethod;
4759 kind = lldb::eMemberFunctionKindInstanceMethod;
4767 case clang::Type::ObjCObject:
4768 case clang::Type::ObjCInterface:
4769 if (GetCompleteType(type)) {
4770 const clang::ObjCObjectType *objc_class_type =
4771 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4772 if (objc_class_type) {
4773 clang::ObjCInterfaceDecl *class_interface_decl =
4774 objc_class_type->getInterface();
4775 if (class_interface_decl) {
4776 auto method_iter = class_interface_decl->meth_begin();
4777 auto method_end = class_interface_decl->meth_end();
4779 static_cast<size_t>(std::distance(method_iter, method_end))) {
4780 std::advance(method_iter, idx);
4781 clang::ObjCMethodDecl *objc_method_decl =
4782 method_iter->getCanonicalDecl();
4783 if (objc_method_decl) {
4784 clang_decl = CompilerDecl(this, objc_method_decl);
4785 name = objc_method_decl->getSelector().getAsString();
4786 if (objc_method_decl->isClassMethod())
4787 kind = lldb::eMemberFunctionKindStaticMethod;
4789 kind = lldb::eMemberFunctionKindInstanceMethod;
4797 case clang::Type::Typedef:
4798 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4800 ->getUnderlyingType()
4804 case clang::Type::Auto:
4805 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4810 case clang::Type::Elaborated:
4811 return GetMemberFunctionAtIndex(
4812 llvm::cast<clang::ElaboratedType>(qual_type)
4817 case clang::Type::Paren:
4818 return GetMemberFunctionAtIndex(
4819 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4827 if (kind == eMemberFunctionKindUnknown)
4828 return TypeMemberFunctionImpl();
4830 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4834 ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4836 return CompilerType(getASTContext(),
4837 GetQualType(type).getNonReferenceType());
4838 return CompilerType();
4841 CompilerType ClangASTContext::CreateTypedefType(
4842 const CompilerType &type, const char *typedef_name,
4843 const CompilerDeclContext &compiler_decl_ctx) {
4844 if (type && typedef_name && typedef_name[0]) {
4845 ClangASTContext *ast =
4846 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4848 return CompilerType();
4849 clang::ASTContext *clang_ast = ast->getASTContext();
4850 clang::QualType qual_type(ClangUtil::GetQualType(type));
4852 clang::DeclContext *decl_ctx =
4853 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4854 if (decl_ctx == nullptr)
4855 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4857 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4858 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4859 &clang_ast->Idents.get(typedef_name),
4860 clang_ast->getTrivialTypeSourceInfo(qual_type));
4862 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4864 decl_ctx->addDecl(decl);
4866 // Get a uniqued clang::QualType for the typedef decl type
4867 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4869 return CompilerType();
4873 ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4875 clang::QualType qual_type(GetQualType(type));
4876 return CompilerType(getASTContext(),
4877 qual_type.getTypePtr()->getPointeeType());
4879 return CompilerType();
4883 ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4885 clang::QualType qual_type(GetQualType(type));
4887 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4888 switch (type_class) {
4889 case clang::Type::ObjCObject:
4890 case clang::Type::ObjCInterface:
4891 return CompilerType(getASTContext(),
4892 getASTContext()->getObjCObjectPointerType(qual_type));
4895 return CompilerType(getASTContext(),
4896 getASTContext()->getPointerType(qual_type));
4899 return CompilerType();
4903 ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4905 return CompilerType(this, getASTContext()
4906 ->getLValueReferenceType(GetQualType(type))
4909 return CompilerType();
4913 ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4915 return CompilerType(this, getASTContext()
4916 ->getRValueReferenceType(GetQualType(type))
4919 return CompilerType();
4923 ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4925 clang::QualType result(GetQualType(type));
4927 return CompilerType(this, result.getAsOpaquePtr());
4929 return CompilerType();
4933 ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4935 clang::QualType result(GetQualType(type));
4936 result.addVolatile();
4937 return CompilerType(this, result.getAsOpaquePtr());
4939 return CompilerType();
4943 ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4945 clang::QualType result(GetQualType(type));
4946 result.addRestrict();
4947 return CompilerType(this, result.getAsOpaquePtr());
4949 return CompilerType();
4953 ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4954 const char *typedef_name,
4955 const CompilerDeclContext &compiler_decl_ctx) {
4957 clang::ASTContext *clang_ast = getASTContext();
4958 clang::QualType qual_type(GetQualType(type));
4960 clang::DeclContext *decl_ctx =
4961 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4962 if (decl_ctx == nullptr)
4963 decl_ctx = getASTContext()->getTranslationUnitDecl();
4965 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4966 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4967 &clang_ast->Idents.get(typedef_name),
4968 clang_ast->getTrivialTypeSourceInfo(qual_type));
4970 clang::TagDecl *tdecl = nullptr;
4971 if (!qual_type.isNull()) {
4972 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4973 tdecl = rt->getDecl();
4974 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4975 tdecl = et->getDecl();
4978 // Check whether this declaration is an anonymous struct, union, or enum,
4979 // hidden behind a typedef. If so, we try to check whether we have a
4980 // typedef tag to attach to the original record declaration
4981 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4982 tdecl->setTypedefNameForAnonDecl(decl);
4984 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4986 // Get a uniqued clang::QualType for the typedef decl type
4987 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4989 return CompilerType();
4993 ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4995 const clang::TypedefType *typedef_type =
4996 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4998 return CompilerType(getASTContext(),
4999 typedef_type->getDecl()->getUnderlyingType());
5001 return CompilerType();
5004 //----------------------------------------------------------------------
5005 // Create related types using the current type's AST
5006 //----------------------------------------------------------------------
5008 CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
5009 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
5011 //----------------------------------------------------------------------
5012 // Exploring the type
5013 //----------------------------------------------------------------------
5015 uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
5016 ExecutionContextScope *exe_scope) {
5017 if (GetCompleteType(type)) {
5018 clang::QualType qual_type(GetCanonicalQualType(type));
5019 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5020 switch (type_class) {
5021 case clang::Type::Record:
5022 if (GetCompleteType(type))
5023 return getASTContext()->getTypeSize(qual_type);
5028 case clang::Type::ObjCInterface:
5029 case clang::Type::ObjCObject: {
5030 ExecutionContext exe_ctx(exe_scope);
5031 Process *process = exe_ctx.GetProcessPtr();
5033 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
5035 uint64_t bit_size = 0;
5036 if (objc_runtime->GetTypeBitSize(
5037 CompilerType(getASTContext(), qual_type), bit_size))
5041 static bool g_printed = false;
5044 DumpTypeDescription(type, &s);
5046 llvm::outs() << "warning: trying to determine the size of type ";
5047 llvm::outs() << s.GetString() << "\n";
5048 llvm::outs() << "without a valid ExecutionContext. this is not "
5049 "reliable. please file a bug against LLDB.\n";
5050 llvm::outs() << "backtrace:\n";
5051 llvm::sys::PrintStackTrace(llvm::outs());
5052 llvm::outs() << "\n";
5059 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
5060 if (bit_size == 0) {
5061 if (qual_type->isIncompleteArrayType())
5062 return getASTContext()->getTypeSize(
5063 qual_type->getArrayElementTypeNoTypeQual()
5064 ->getCanonicalTypeUnqualified());
5066 if (qual_type->isObjCObjectOrInterfaceType())
5068 getASTContext()->getTypeSize(
5069 getASTContext()->ObjCBuiltinClassTy);
5076 size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
5077 if (GetCompleteType(type))
5078 return getASTContext()->getTypeAlign(GetQualType(type));
5082 lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
5085 return lldb::eEncodingInvalid;
5088 clang::QualType qual_type(GetCanonicalQualType(type));
5090 switch (qual_type->getTypeClass()) {
5091 case clang::Type::UnaryTransform:
5094 case clang::Type::FunctionNoProto:
5095 case clang::Type::FunctionProto:
5098 case clang::Type::IncompleteArray:
5099 case clang::Type::VariableArray:
5102 case clang::Type::ConstantArray:
5105 case clang::Type::DependentVector:
5106 case clang::Type::ExtVector:
5107 case clang::Type::Vector:
5108 // TODO: Set this to more than one???
5111 case clang::Type::Builtin:
5112 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5113 case clang::BuiltinType::Void:
5116 case clang::BuiltinType::Bool:
5117 case clang::BuiltinType::Char_S:
5118 case clang::BuiltinType::SChar:
5119 case clang::BuiltinType::WChar_S:
5120 case clang::BuiltinType::Short:
5121 case clang::BuiltinType::Int:
5122 case clang::BuiltinType::Long:
5123 case clang::BuiltinType::LongLong:
5124 case clang::BuiltinType::Int128:
5125 return lldb::eEncodingSint;
5127 case clang::BuiltinType::Char_U:
5128 case clang::BuiltinType::UChar:
5129 case clang::BuiltinType::WChar_U:
5130 case clang::BuiltinType::Char8:
5131 case clang::BuiltinType::Char16:
5132 case clang::BuiltinType::Char32:
5133 case clang::BuiltinType::UShort:
5134 case clang::BuiltinType::UInt:
5135 case clang::BuiltinType::ULong:
5136 case clang::BuiltinType::ULongLong:
5137 case clang::BuiltinType::UInt128:
5138 return lldb::eEncodingUint;
5140 // Fixed point types. Note that they are currently ignored.
5141 case clang::BuiltinType::ShortAccum:
5142 case clang::BuiltinType::Accum:
5143 case clang::BuiltinType::LongAccum:
5144 case clang::BuiltinType::UShortAccum:
5145 case clang::BuiltinType::UAccum:
5146 case clang::BuiltinType::ULongAccum:
5147 case clang::BuiltinType::ShortFract:
5148 case clang::BuiltinType::Fract:
5149 case clang::BuiltinType::LongFract:
5150 case clang::BuiltinType::UShortFract:
5151 case clang::BuiltinType::UFract:
5152 case clang::BuiltinType::ULongFract:
5153 case clang::BuiltinType::SatShortAccum:
5154 case clang::BuiltinType::SatAccum:
5155 case clang::BuiltinType::SatLongAccum:
5156 case clang::BuiltinType::SatUShortAccum:
5157 case clang::BuiltinType::SatUAccum:
5158 case clang::BuiltinType::SatULongAccum:
5159 case clang::BuiltinType::SatShortFract:
5160 case clang::BuiltinType::SatFract:
5161 case clang::BuiltinType::SatLongFract:
5162 case clang::BuiltinType::SatUShortFract:
5163 case clang::BuiltinType::SatUFract:
5164 case clang::BuiltinType::SatULongFract:
5167 case clang::BuiltinType::Half:
5168 case clang::BuiltinType::Float:
5169 case clang::BuiltinType::Float16:
5170 case clang::BuiltinType::Float128:
5171 case clang::BuiltinType::Double:
5172 case clang::BuiltinType::LongDouble:
5173 return lldb::eEncodingIEEE754;
5175 case clang::BuiltinType::ObjCClass:
5176 case clang::BuiltinType::ObjCId:
5177 case clang::BuiltinType::ObjCSel:
5178 return lldb::eEncodingUint;
5180 case clang::BuiltinType::NullPtr:
5181 return lldb::eEncodingUint;
5183 case clang::BuiltinType::Kind::ARCUnbridgedCast:
5184 case clang::BuiltinType::Kind::BoundMember:
5185 case clang::BuiltinType::Kind::BuiltinFn:
5186 case clang::BuiltinType::Kind::Dependent:
5187 case clang::BuiltinType::Kind::OCLClkEvent:
5188 case clang::BuiltinType::Kind::OCLEvent:
5189 case clang::BuiltinType::Kind::OCLImage1dRO:
5190 case clang::BuiltinType::Kind::OCLImage1dWO:
5191 case clang::BuiltinType::Kind::OCLImage1dRW:
5192 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
5193 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
5194 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
5195 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
5196 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
5197 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5198 case clang::BuiltinType::Kind::OCLImage2dRO:
5199 case clang::BuiltinType::Kind::OCLImage2dWO:
5200 case clang::BuiltinType::Kind::OCLImage2dRW:
5201 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5202 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5203 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5204 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5205 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5206 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5207 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5208 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5209 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5210 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5211 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5212 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5213 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5214 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5215 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5216 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5217 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5218 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5219 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5220 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5221 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5222 case clang::BuiltinType::Kind::OCLImage3dRO:
5223 case clang::BuiltinType::Kind::OCLImage3dWO:
5224 case clang::BuiltinType::Kind::OCLImage3dRW:
5225 case clang::BuiltinType::Kind::OCLQueue:
5226 case clang::BuiltinType::Kind::OCLReserveID:
5227 case clang::BuiltinType::Kind::OCLSampler:
5228 case clang::BuiltinType::Kind::OMPArraySection:
5229 case clang::BuiltinType::Kind::Overload:
5230 case clang::BuiltinType::Kind::PseudoObject:
5231 case clang::BuiltinType::Kind::UnknownAny:
5234 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
5235 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
5236 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
5237 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
5238 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
5239 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
5240 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
5241 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
5242 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
5243 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
5244 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
5245 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
5249 // All pointer types are represented as unsigned integer encodings. We may
5250 // nee to add a eEncodingPointer if we ever need to know the difference
5251 case clang::Type::ObjCObjectPointer:
5252 case clang::Type::BlockPointer:
5253 case clang::Type::Pointer:
5254 case clang::Type::LValueReference:
5255 case clang::Type::RValueReference:
5256 case clang::Type::MemberPointer:
5257 return lldb::eEncodingUint;
5258 case clang::Type::Complex: {
5259 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5260 if (qual_type->isComplexType())
5261 encoding = lldb::eEncodingIEEE754;
5263 const clang::ComplexType *complex_type =
5264 qual_type->getAsComplexIntegerType();
5266 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5267 .GetEncoding(count);
5269 encoding = lldb::eEncodingSint;
5275 case clang::Type::ObjCInterface:
5277 case clang::Type::Record:
5279 case clang::Type::Enum:
5280 return lldb::eEncodingSint;
5281 case clang::Type::Typedef:
5282 return CompilerType(getASTContext(),
5283 llvm::cast<clang::TypedefType>(qual_type)
5285 ->getUnderlyingType())
5286 .GetEncoding(count);
5288 case clang::Type::Auto:
5289 return CompilerType(
5291 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5292 .GetEncoding(count);
5294 case clang::Type::Elaborated:
5295 return CompilerType(
5297 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5298 .GetEncoding(count);
5300 case clang::Type::Paren:
5301 return CompilerType(getASTContext(),
5302 llvm::cast<clang::ParenType>(qual_type)->desugar())
5303 .GetEncoding(count);
5304 case clang::Type::TypeOfExpr:
5305 return CompilerType(getASTContext(),
5306 llvm::cast<clang::TypeOfExprType>(qual_type)
5307 ->getUnderlyingExpr()
5309 .GetEncoding(count);
5310 case clang::Type::TypeOf:
5311 return CompilerType(
5313 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5314 .GetEncoding(count);
5315 case clang::Type::Decltype:
5316 return CompilerType(
5318 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5319 .GetEncoding(count);
5320 case clang::Type::DependentSizedArray:
5321 case clang::Type::DependentSizedExtVector:
5322 case clang::Type::UnresolvedUsing:
5323 case clang::Type::Attributed:
5324 case clang::Type::TemplateTypeParm:
5325 case clang::Type::SubstTemplateTypeParm:
5326 case clang::Type::SubstTemplateTypeParmPack:
5327 case clang::Type::InjectedClassName:
5328 case clang::Type::DependentName:
5329 case clang::Type::DependentTemplateSpecialization:
5330 case clang::Type::PackExpansion:
5331 case clang::Type::ObjCObject:
5333 case clang::Type::TemplateSpecialization:
5334 case clang::Type::DeducedTemplateSpecialization:
5335 case clang::Type::Atomic:
5336 case clang::Type::Adjusted:
5337 case clang::Type::Pipe:
5340 // pointer type decayed from an array or function type.
5341 case clang::Type::Decayed:
5343 case clang::Type::ObjCTypeParam:
5346 case clang::Type::DependentAddressSpace:
5350 return lldb::eEncodingInvalid;
5353 lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5355 return lldb::eFormatDefault;
5357 clang::QualType qual_type(GetCanonicalQualType(type));
5359 switch (qual_type->getTypeClass()) {
5360 case clang::Type::UnaryTransform:
5363 case clang::Type::FunctionNoProto:
5364 case clang::Type::FunctionProto:
5367 case clang::Type::IncompleteArray:
5368 case clang::Type::VariableArray:
5371 case clang::Type::ConstantArray:
5372 return lldb::eFormatVoid; // no value
5374 case clang::Type::DependentVector:
5375 case clang::Type::ExtVector:
5376 case clang::Type::Vector:
5379 case clang::Type::Builtin:
5380 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5381 // default: assert(0 && "Unknown builtin type!");
5382 case clang::BuiltinType::UnknownAny:
5383 case clang::BuiltinType::Void:
5384 case clang::BuiltinType::BoundMember:
5387 case clang::BuiltinType::Bool:
5388 return lldb::eFormatBoolean;
5389 case clang::BuiltinType::Char_S:
5390 case clang::BuiltinType::SChar:
5391 case clang::BuiltinType::WChar_S:
5392 case clang::BuiltinType::Char_U:
5393 case clang::BuiltinType::UChar:
5394 case clang::BuiltinType::WChar_U:
5395 return lldb::eFormatChar;
5396 case clang::BuiltinType::Char16:
5397 return lldb::eFormatUnicode16;
5398 case clang::BuiltinType::Char32:
5399 return lldb::eFormatUnicode32;
5400 case clang::BuiltinType::UShort:
5401 return lldb::eFormatUnsigned;
5402 case clang::BuiltinType::Short:
5403 return lldb::eFormatDecimal;
5404 case clang::BuiltinType::UInt:
5405 return lldb::eFormatUnsigned;
5406 case clang::BuiltinType::Int:
5407 return lldb::eFormatDecimal;
5408 case clang::BuiltinType::ULong:
5409 return lldb::eFormatUnsigned;
5410 case clang::BuiltinType::Long:
5411 return lldb::eFormatDecimal;
5412 case clang::BuiltinType::ULongLong:
5413 return lldb::eFormatUnsigned;
5414 case clang::BuiltinType::LongLong:
5415 return lldb::eFormatDecimal;
5416 case clang::BuiltinType::UInt128:
5417 return lldb::eFormatUnsigned;
5418 case clang::BuiltinType::Int128:
5419 return lldb::eFormatDecimal;
5420 case clang::BuiltinType::Half:
5421 case clang::BuiltinType::Float:
5422 case clang::BuiltinType::Double:
5423 case clang::BuiltinType::LongDouble:
5424 return lldb::eFormatFloat;
5426 return lldb::eFormatHex;
5429 case clang::Type::ObjCObjectPointer:
5430 return lldb::eFormatHex;
5431 case clang::Type::BlockPointer:
5432 return lldb::eFormatHex;
5433 case clang::Type::Pointer:
5434 return lldb::eFormatHex;
5435 case clang::Type::LValueReference:
5436 case clang::Type::RValueReference:
5437 return lldb::eFormatHex;
5438 case clang::Type::MemberPointer:
5440 case clang::Type::Complex: {
5441 if (qual_type->isComplexType())
5442 return lldb::eFormatComplex;
5444 return lldb::eFormatComplexInteger;
5446 case clang::Type::ObjCInterface:
5448 case clang::Type::Record:
5450 case clang::Type::Enum:
5451 return lldb::eFormatEnum;
5452 case clang::Type::Typedef:
5453 return CompilerType(getASTContext(),
5454 llvm::cast<clang::TypedefType>(qual_type)
5456 ->getUnderlyingType())
5458 case clang::Type::Auto:
5459 return CompilerType(getASTContext(),
5460 llvm::cast<clang::AutoType>(qual_type)->desugar())
5462 case clang::Type::Paren:
5463 return CompilerType(getASTContext(),
5464 llvm::cast<clang::ParenType>(qual_type)->desugar())
5466 case clang::Type::Elaborated:
5467 return CompilerType(
5469 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5471 case clang::Type::TypeOfExpr:
5472 return CompilerType(getASTContext(),
5473 llvm::cast<clang::TypeOfExprType>(qual_type)
5474 ->getUnderlyingExpr()
5477 case clang::Type::TypeOf:
5478 return CompilerType(
5480 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType())
5482 case clang::Type::Decltype:
5483 return CompilerType(
5485 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType())
5487 case clang::Type::DependentSizedArray:
5488 case clang::Type::DependentSizedExtVector:
5489 case clang::Type::UnresolvedUsing:
5490 case clang::Type::Attributed:
5491 case clang::Type::TemplateTypeParm:
5492 case clang::Type::SubstTemplateTypeParm:
5493 case clang::Type::SubstTemplateTypeParmPack:
5494 case clang::Type::InjectedClassName:
5495 case clang::Type::DependentName:
5496 case clang::Type::DependentTemplateSpecialization:
5497 case clang::Type::PackExpansion:
5498 case clang::Type::ObjCObject:
5500 case clang::Type::TemplateSpecialization:
5501 case clang::Type::DeducedTemplateSpecialization:
5502 case clang::Type::Atomic:
5503 case clang::Type::Adjusted:
5504 case clang::Type::Pipe:
5507 // pointer type decayed from an array or function type.
5508 case clang::Type::Decayed:
5510 case clang::Type::ObjCTypeParam:
5513 case clang::Type::DependentAddressSpace:
5516 // We don't know hot to display this type...
5517 return lldb::eFormatBytes;
5520 static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5521 bool check_superclass) {
5522 while (class_interface_decl) {
5523 if (class_interface_decl->ivar_size() > 0)
5526 if (check_superclass)
5527 class_interface_decl = class_interface_decl->getSuperClass();
5534 static Optional<SymbolFile::ArrayInfo>
5535 GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file,
5536 clang::QualType qual_type,
5537 const ExecutionContext *exe_ctx) {
5538 if (qual_type->isIncompleteArrayType())
5539 if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr()))
5540 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5545 uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5546 bool omit_empty_base_classes,
5547 const ExecutionContext *exe_ctx) {
5551 uint32_t num_children = 0;
5552 clang::QualType qual_type(GetQualType(type));
5553 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5554 switch (type_class) {
5555 case clang::Type::Builtin:
5556 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5557 case clang::BuiltinType::ObjCId: // child is Class
5558 case clang::BuiltinType::ObjCClass: // child is Class
5567 case clang::Type::Complex:
5569 case clang::Type::Record:
5570 if (GetCompleteQualType(getASTContext(), qual_type)) {
5571 const clang::RecordType *record_type =
5572 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5573 const clang::RecordDecl *record_decl = record_type->getDecl();
5574 assert(record_decl);
5575 const clang::CXXRecordDecl *cxx_record_decl =
5576 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5577 if (cxx_record_decl) {
5578 if (omit_empty_base_classes) {
5579 // Check each base classes to see if it or any of its base classes
5580 // contain any fields. This can help limit the noise in variable
5581 // views by not having to show base classes that contain no members.
5582 clang::CXXRecordDecl::base_class_const_iterator base_class,
5584 for (base_class = cxx_record_decl->bases_begin(),
5585 base_class_end = cxx_record_decl->bases_end();
5586 base_class != base_class_end; ++base_class) {
5587 const clang::CXXRecordDecl *base_class_decl =
5588 llvm::cast<clang::CXXRecordDecl>(
5589 base_class->getType()
5590 ->getAs<clang::RecordType>()
5593 // Skip empty base classes
5594 if (!ClangASTContext::RecordHasFields(base_class_decl))
5600 // Include all base classes
5601 num_children += cxx_record_decl->getNumBases();
5604 clang::RecordDecl::field_iterator field, field_end;
5605 for (field = record_decl->field_begin(),
5606 field_end = record_decl->field_end();
5607 field != field_end; ++field)
5612 case clang::Type::ObjCObject:
5613 case clang::Type::ObjCInterface:
5614 if (GetCompleteQualType(getASTContext(), qual_type)) {
5615 const clang::ObjCObjectType *objc_class_type =
5616 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5617 assert(objc_class_type);
5618 if (objc_class_type) {
5619 clang::ObjCInterfaceDecl *class_interface_decl =
5620 objc_class_type->getInterface();
5622 if (class_interface_decl) {
5624 clang::ObjCInterfaceDecl *superclass_interface_decl =
5625 class_interface_decl->getSuperClass();
5626 if (superclass_interface_decl) {
5627 if (omit_empty_base_classes) {
5628 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5634 num_children += class_interface_decl->ivar_size();
5640 case clang::Type::ObjCObjectPointer: {
5641 const clang::ObjCObjectPointerType *pointer_type =
5642 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5643 clang::QualType pointee_type = pointer_type->getPointeeType();
5644 uint32_t num_pointee_children =
5645 CompilerType(getASTContext(), pointee_type)
5646 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5647 // If this type points to a simple type, then it has 1 child
5648 if (num_pointee_children == 0)
5651 num_children = num_pointee_children;
5654 case clang::Type::Vector:
5655 case clang::Type::ExtVector:
5657 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5660 case clang::Type::ConstantArray:
5661 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5665 case clang::Type::IncompleteArray:
5666 if (auto array_info =
5667 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5668 // Only 1-dimensional arrays are supported.
5669 num_children = array_info->element_orders.size()
5670 ? array_info->element_orders.back()
5674 case clang::Type::Pointer: {
5675 const clang::PointerType *pointer_type =
5676 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5677 clang::QualType pointee_type(pointer_type->getPointeeType());
5678 uint32_t num_pointee_children =
5679 CompilerType(getASTContext(), pointee_type)
5680 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5681 if (num_pointee_children == 0) {
5682 // We have a pointer to a pointee type that claims it has no children. We
5683 // will want to look at
5684 num_children = GetNumPointeeChildren(pointee_type);
5686 num_children = num_pointee_children;
5689 case clang::Type::LValueReference:
5690 case clang::Type::RValueReference: {
5691 const clang::ReferenceType *reference_type =
5692 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5693 clang::QualType pointee_type = reference_type->getPointeeType();
5694 uint32_t num_pointee_children =
5695 CompilerType(getASTContext(), pointee_type)
5696 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5697 // If this type points to a simple type, then it has 1 child
5698 if (num_pointee_children == 0)
5701 num_children = num_pointee_children;
5704 case clang::Type::Typedef:
5706 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5708 ->getUnderlyingType())
5709 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5712 case clang::Type::Auto:
5714 CompilerType(getASTContext(),
5715 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5716 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5719 case clang::Type::Elaborated:
5723 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5724 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5727 case clang::Type::Paren:
5729 CompilerType(getASTContext(),
5730 llvm::cast<clang::ParenType>(qual_type)->desugar())
5731 .GetNumChildren(omit_empty_base_classes, exe_ctx);
5736 return num_children;
5739 CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5740 return GetBasicType(GetBasicTypeEnumeration(name));
5744 ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5746 clang::QualType qual_type(GetQualType(type));
5747 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5748 if (type_class == clang::Type::Builtin) {
5749 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5750 case clang::BuiltinType::Void:
5751 return eBasicTypeVoid;
5752 case clang::BuiltinType::Bool:
5753 return eBasicTypeBool;
5754 case clang::BuiltinType::Char_S:
5755 return eBasicTypeSignedChar;
5756 case clang::BuiltinType::Char_U:
5757 return eBasicTypeUnsignedChar;
5758 case clang::BuiltinType::Char16:
5759 return eBasicTypeChar16;
5760 case clang::BuiltinType::Char32:
5761 return eBasicTypeChar32;
5762 case clang::BuiltinType::UChar:
5763 return eBasicTypeUnsignedChar;
5764 case clang::BuiltinType::SChar:
5765 return eBasicTypeSignedChar;
5766 case clang::BuiltinType::WChar_S:
5767 return eBasicTypeSignedWChar;
5768 case clang::BuiltinType::WChar_U:
5769 return eBasicTypeUnsignedWChar;
5770 case clang::BuiltinType::Short:
5771 return eBasicTypeShort;
5772 case clang::BuiltinType::UShort:
5773 return eBasicTypeUnsignedShort;
5774 case clang::BuiltinType::Int:
5775 return eBasicTypeInt;
5776 case clang::BuiltinType::UInt:
5777 return eBasicTypeUnsignedInt;
5778 case clang::BuiltinType::Long:
5779 return eBasicTypeLong;
5780 case clang::BuiltinType::ULong:
5781 return eBasicTypeUnsignedLong;
5782 case clang::BuiltinType::LongLong:
5783 return eBasicTypeLongLong;
5784 case clang::BuiltinType::ULongLong:
5785 return eBasicTypeUnsignedLongLong;
5786 case clang::BuiltinType::Int128:
5787 return eBasicTypeInt128;
5788 case clang::BuiltinType::UInt128:
5789 return eBasicTypeUnsignedInt128;
5791 case clang::BuiltinType::Half:
5792 return eBasicTypeHalf;
5793 case clang::BuiltinType::Float:
5794 return eBasicTypeFloat;
5795 case clang::BuiltinType::Double:
5796 return eBasicTypeDouble;
5797 case clang::BuiltinType::LongDouble:
5798 return eBasicTypeLongDouble;
5800 case clang::BuiltinType::NullPtr:
5801 return eBasicTypeNullPtr;
5802 case clang::BuiltinType::ObjCId:
5803 return eBasicTypeObjCID;
5804 case clang::BuiltinType::ObjCClass:
5805 return eBasicTypeObjCClass;
5806 case clang::BuiltinType::ObjCSel:
5807 return eBasicTypeObjCSel;
5809 return eBasicTypeOther;
5813 return eBasicTypeInvalid;
5816 void ClangASTContext::ForEachEnumerator(
5817 lldb::opaque_compiler_type_t type,
5818 std::function<bool(const CompilerType &integer_type,
5819 const ConstString &name,
5820 const llvm::APSInt &value)> const &callback) {
5821 const clang::EnumType *enum_type =
5822 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5824 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5826 CompilerType integer_type(this,
5827 enum_decl->getIntegerType().getAsOpaquePtr());
5829 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5830 for (enum_pos = enum_decl->enumerator_begin(),
5831 enum_end_pos = enum_decl->enumerator_end();
5832 enum_pos != enum_end_pos; ++enum_pos) {
5833 ConstString name(enum_pos->getNameAsString().c_str());
5834 if (!callback(integer_type, name, enum_pos->getInitVal()))
5841 #pragma mark Aggregate Types
5843 uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5848 clang::QualType qual_type(GetCanonicalQualType(type));
5849 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5850 switch (type_class) {
5851 case clang::Type::Record:
5852 if (GetCompleteType(type)) {
5853 const clang::RecordType *record_type =
5854 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5856 clang::RecordDecl *record_decl = record_type->getDecl();
5858 uint32_t field_idx = 0;
5859 clang::RecordDecl::field_iterator field, field_end;
5860 for (field = record_decl->field_begin(),
5861 field_end = record_decl->field_end();
5862 field != field_end; ++field)
5870 case clang::Type::Typedef:
5872 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5874 ->getUnderlyingType())
5878 case clang::Type::Auto:
5880 CompilerType(getASTContext(),
5881 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5885 case clang::Type::Elaborated:
5886 count = CompilerType(
5888 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5892 case clang::Type::Paren:
5893 count = CompilerType(getASTContext(),
5894 llvm::cast<clang::ParenType>(qual_type)->desugar())
5898 case clang::Type::ObjCObjectPointer: {
5899 const clang::ObjCObjectPointerType *objc_class_type =
5900 qual_type->getAs<clang::ObjCObjectPointerType>();
5901 const clang::ObjCInterfaceType *objc_interface_type =
5902 objc_class_type->getInterfaceType();
5903 if (objc_interface_type &&
5904 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5905 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5906 clang::ObjCInterfaceDecl *class_interface_decl =
5907 objc_interface_type->getDecl();
5908 if (class_interface_decl) {
5909 count = class_interface_decl->ivar_size();
5915 case clang::Type::ObjCObject:
5916 case clang::Type::ObjCInterface:
5917 if (GetCompleteType(type)) {
5918 const clang::ObjCObjectType *objc_class_type =
5919 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5920 if (objc_class_type) {
5921 clang::ObjCInterfaceDecl *class_interface_decl =
5922 objc_class_type->getInterface();
5924 if (class_interface_decl)
5925 count = class_interface_decl->ivar_size();
5936 static lldb::opaque_compiler_type_t
5937 GetObjCFieldAtIndex(clang::ASTContext *ast,
5938 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5939 std::string &name, uint64_t *bit_offset_ptr,
5940 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5941 if (class_interface_decl) {
5942 if (idx < (class_interface_decl->ivar_size())) {
5943 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5944 ivar_end = class_interface_decl->ivar_end();
5945 uint32_t ivar_idx = 0;
5947 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5948 ++ivar_pos, ++ivar_idx) {
5949 if (ivar_idx == idx) {
5950 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5952 clang::QualType ivar_qual_type(ivar_decl->getType());
5954 name.assign(ivar_decl->getNameAsString());
5956 if (bit_offset_ptr) {
5957 const clang::ASTRecordLayout &interface_layout =
5958 ast->getASTObjCInterfaceLayout(class_interface_decl);
5959 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5962 const bool is_bitfield = ivar_pos->isBitField();
5964 if (bitfield_bit_size_ptr) {
5965 *bitfield_bit_size_ptr = 0;
5967 if (is_bitfield && ast) {
5968 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5969 clang::Expr::EvalResult result;
5970 if (bitfield_bit_size_expr &&
5971 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5972 llvm::APSInt bitfield_apsint = result.Val.getInt();
5973 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5977 if (is_bitfield_ptr)
5978 *is_bitfield_ptr = is_bitfield;
5980 return ivar_qual_type.getAsOpaquePtr();
5988 CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5989 size_t idx, std::string &name,
5990 uint64_t *bit_offset_ptr,
5991 uint32_t *bitfield_bit_size_ptr,
5992 bool *is_bitfield_ptr) {
5994 return CompilerType();
5996 clang::QualType qual_type(GetCanonicalQualType(type));
5997 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5998 switch (type_class) {
5999 case clang::Type::Record:
6000 if (GetCompleteType(type)) {
6001 const clang::RecordType *record_type =
6002 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6003 const clang::RecordDecl *record_decl = record_type->getDecl();
6004 uint32_t field_idx = 0;
6005 clang::RecordDecl::field_iterator field, field_end;
6006 for (field = record_decl->field_begin(),
6007 field_end = record_decl->field_end();
6008 field != field_end; ++field, ++field_idx) {
6009 if (idx == field_idx) {
6010 // Print the member type if requested
6011 // Print the member name and equal sign
6012 name.assign(field->getNameAsString());
6014 // Figure out the type byte size (field_type_info.first) and
6015 // alignment (field_type_info.second) from the AST context.
6016 if (bit_offset_ptr) {
6017 const clang::ASTRecordLayout &record_layout =
6018 getASTContext()->getASTRecordLayout(record_decl);
6019 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
6022 const bool is_bitfield = field->isBitField();
6024 if (bitfield_bit_size_ptr) {
6025 *bitfield_bit_size_ptr = 0;
6028 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
6029 clang::Expr::EvalResult result;
6030 if (bitfield_bit_size_expr &&
6031 bitfield_bit_size_expr->EvaluateAsInt(result,
6032 *getASTContext())) {
6033 llvm::APSInt bitfield_apsint = result.Val.getInt();
6034 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
6038 if (is_bitfield_ptr)
6039 *is_bitfield_ptr = is_bitfield;
6041 return CompilerType(getASTContext(), field->getType());
6047 case clang::Type::ObjCObjectPointer: {
6048 const clang::ObjCObjectPointerType *objc_class_type =
6049 qual_type->getAs<clang::ObjCObjectPointerType>();
6050 const clang::ObjCInterfaceType *objc_interface_type =
6051 objc_class_type->getInterfaceType();
6052 if (objc_interface_type &&
6053 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
6054 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
6055 clang::ObjCInterfaceDecl *class_interface_decl =
6056 objc_interface_type->getDecl();
6057 if (class_interface_decl) {
6058 return CompilerType(
6059 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6060 idx, name, bit_offset_ptr,
6061 bitfield_bit_size_ptr, is_bitfield_ptr));
6067 case clang::Type::ObjCObject:
6068 case clang::Type::ObjCInterface:
6069 if (GetCompleteType(type)) {
6070 const clang::ObjCObjectType *objc_class_type =
6071 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6072 assert(objc_class_type);
6073 if (objc_class_type) {
6074 clang::ObjCInterfaceDecl *class_interface_decl =
6075 objc_class_type->getInterface();
6076 return CompilerType(
6077 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
6078 idx, name, bit_offset_ptr,
6079 bitfield_bit_size_ptr, is_bitfield_ptr));
6084 case clang::Type::Typedef:
6085 return CompilerType(getASTContext(),
6086 llvm::cast<clang::TypedefType>(qual_type)
6088 ->getUnderlyingType())
6089 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6092 case clang::Type::Auto:
6093 return CompilerType(
6095 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
6096 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6099 case clang::Type::Elaborated:
6100 return CompilerType(
6102 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
6103 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6106 case clang::Type::Paren:
6107 return CompilerType(getASTContext(),
6108 llvm::cast<clang::ParenType>(qual_type)->desugar())
6109 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
6115 return CompilerType();
6119 ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
6121 clang::QualType qual_type(GetCanonicalQualType(type));
6122 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6123 switch (type_class) {
6124 case clang::Type::Record:
6125 if (GetCompleteType(type)) {
6126 const clang::CXXRecordDecl *cxx_record_decl =
6127 qual_type->getAsCXXRecordDecl();
6128 if (cxx_record_decl)
6129 count = cxx_record_decl->getNumBases();
6133 case clang::Type::ObjCObjectPointer:
6134 count = GetPointeeType(type).GetNumDirectBaseClasses();
6137 case clang::Type::ObjCObject:
6138 if (GetCompleteType(type)) {
6139 const clang::ObjCObjectType *objc_class_type =
6140 qual_type->getAsObjCQualifiedInterfaceType();
6141 if (objc_class_type) {
6142 clang::ObjCInterfaceDecl *class_interface_decl =
6143 objc_class_type->getInterface();
6145 if (class_interface_decl && class_interface_decl->getSuperClass())
6150 case clang::Type::ObjCInterface:
6151 if (GetCompleteType(type)) {
6152 const clang::ObjCInterfaceType *objc_interface_type =
6153 qual_type->getAs<clang::ObjCInterfaceType>();
6154 if (objc_interface_type) {
6155 clang::ObjCInterfaceDecl *class_interface_decl =
6156 objc_interface_type->getInterface();
6158 if (class_interface_decl && class_interface_decl->getSuperClass())
6164 case clang::Type::Typedef:
6165 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6167 ->getUnderlyingType()
6171 case clang::Type::Auto:
6172 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6177 case clang::Type::Elaborated:
6178 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6183 case clang::Type::Paren:
6184 return GetNumDirectBaseClasses(
6185 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6194 ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
6196 clang::QualType qual_type(GetCanonicalQualType(type));
6197 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6198 switch (type_class) {
6199 case clang::Type::Record:
6200 if (GetCompleteType(type)) {
6201 const clang::CXXRecordDecl *cxx_record_decl =
6202 qual_type->getAsCXXRecordDecl();
6203 if (cxx_record_decl)
6204 count = cxx_record_decl->getNumVBases();
6208 case clang::Type::Typedef:
6209 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
6211 ->getUnderlyingType()
6215 case clang::Type::Auto:
6216 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
6221 case clang::Type::Elaborated:
6223 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
6228 case clang::Type::Paren:
6229 count = GetNumVirtualBaseClasses(
6230 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
6239 CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
6240 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6241 clang::QualType qual_type(GetCanonicalQualType(type));
6242 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6243 switch (type_class) {
6244 case clang::Type::Record:
6245 if (GetCompleteType(type)) {
6246 const clang::CXXRecordDecl *cxx_record_decl =
6247 qual_type->getAsCXXRecordDecl();
6248 if (cxx_record_decl) {
6249 uint32_t curr_idx = 0;
6250 clang::CXXRecordDecl::base_class_const_iterator base_class,
6252 for (base_class = cxx_record_decl->bases_begin(),
6253 base_class_end = cxx_record_decl->bases_end();
6254 base_class != base_class_end; ++base_class, ++curr_idx) {
6255 if (curr_idx == idx) {
6256 if (bit_offset_ptr) {
6257 const clang::ASTRecordLayout &record_layout =
6258 getASTContext()->getASTRecordLayout(cxx_record_decl);
6259 const clang::CXXRecordDecl *base_class_decl =
6260 llvm::cast<clang::CXXRecordDecl>(
6261 base_class->getType()
6262 ->getAs<clang::RecordType>()
6264 if (base_class->isVirtual())
6266 record_layout.getVBaseClassOffset(base_class_decl)
6271 record_layout.getBaseClassOffset(base_class_decl)
6275 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6282 case clang::Type::ObjCObjectPointer:
6283 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6285 case clang::Type::ObjCObject:
6286 if (idx == 0 && GetCompleteType(type)) {
6287 const clang::ObjCObjectType *objc_class_type =
6288 qual_type->getAsObjCQualifiedInterfaceType();
6289 if (objc_class_type) {
6290 clang::ObjCInterfaceDecl *class_interface_decl =
6291 objc_class_type->getInterface();
6293 if (class_interface_decl) {
6294 clang::ObjCInterfaceDecl *superclass_interface_decl =
6295 class_interface_decl->getSuperClass();
6296 if (superclass_interface_decl) {
6298 *bit_offset_ptr = 0;
6299 return CompilerType(getASTContext(),
6300 getASTContext()->getObjCInterfaceType(
6301 superclass_interface_decl));
6307 case clang::Type::ObjCInterface:
6308 if (idx == 0 && GetCompleteType(type)) {
6309 const clang::ObjCObjectType *objc_interface_type =
6310 qual_type->getAs<clang::ObjCInterfaceType>();
6311 if (objc_interface_type) {
6312 clang::ObjCInterfaceDecl *class_interface_decl =
6313 objc_interface_type->getInterface();
6315 if (class_interface_decl) {
6316 clang::ObjCInterfaceDecl *superclass_interface_decl =
6317 class_interface_decl->getSuperClass();
6318 if (superclass_interface_decl) {
6320 *bit_offset_ptr = 0;
6321 return CompilerType(getASTContext(),
6322 getASTContext()->getObjCInterfaceType(
6323 superclass_interface_decl));
6330 case clang::Type::Typedef:
6331 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6333 ->getUnderlyingType()
6335 idx, bit_offset_ptr);
6337 case clang::Type::Auto:
6338 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6341 idx, bit_offset_ptr);
6343 case clang::Type::Elaborated:
6344 return GetDirectBaseClassAtIndex(
6345 llvm::cast<clang::ElaboratedType>(qual_type)
6348 idx, bit_offset_ptr);
6350 case clang::Type::Paren:
6351 return GetDirectBaseClassAtIndex(
6352 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6353 idx, bit_offset_ptr);
6358 return CompilerType();
6361 CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6362 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6363 clang::QualType qual_type(GetCanonicalQualType(type));
6364 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6365 switch (type_class) {
6366 case clang::Type::Record:
6367 if (GetCompleteType(type)) {
6368 const clang::CXXRecordDecl *cxx_record_decl =
6369 qual_type->getAsCXXRecordDecl();
6370 if (cxx_record_decl) {
6371 uint32_t curr_idx = 0;
6372 clang::CXXRecordDecl::base_class_const_iterator base_class,
6374 for (base_class = cxx_record_decl->vbases_begin(),
6375 base_class_end = cxx_record_decl->vbases_end();
6376 base_class != base_class_end; ++base_class, ++curr_idx) {
6377 if (curr_idx == idx) {
6378 if (bit_offset_ptr) {
6379 const clang::ASTRecordLayout &record_layout =
6380 getASTContext()->getASTRecordLayout(cxx_record_decl);
6381 const clang::CXXRecordDecl *base_class_decl =
6382 llvm::cast<clang::CXXRecordDecl>(
6383 base_class->getType()
6384 ->getAs<clang::RecordType>()
6387 record_layout.getVBaseClassOffset(base_class_decl)
6391 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6398 case clang::Type::Typedef:
6399 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6401 ->getUnderlyingType()
6403 idx, bit_offset_ptr);
6405 case clang::Type::Auto:
6406 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6409 idx, bit_offset_ptr);
6411 case clang::Type::Elaborated:
6412 return GetVirtualBaseClassAtIndex(
6413 llvm::cast<clang::ElaboratedType>(qual_type)
6416 idx, bit_offset_ptr);
6418 case clang::Type::Paren:
6419 return GetVirtualBaseClassAtIndex(
6420 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6421 idx, bit_offset_ptr);
6426 return CompilerType();
6429 // If a pointer to a pointee type (the clang_type arg) says that it has no
6430 // children, then we either need to trust it, or override it and return a
6431 // different result. For example, an "int *" has one child that is an integer,
6432 // but a function pointer doesn't have any children. Likewise if a Record type
6433 // claims it has no children, then there really is nothing to show.
6434 uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6438 clang::QualType qual_type(type.getCanonicalType());
6439 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6440 switch (type_class) {
6441 case clang::Type::Builtin:
6442 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6443 case clang::BuiltinType::UnknownAny:
6444 case clang::BuiltinType::Void:
6445 case clang::BuiltinType::NullPtr:
6446 case clang::BuiltinType::OCLEvent:
6447 case clang::BuiltinType::OCLImage1dRO:
6448 case clang::BuiltinType::OCLImage1dWO:
6449 case clang::BuiltinType::OCLImage1dRW:
6450 case clang::BuiltinType::OCLImage1dArrayRO:
6451 case clang::BuiltinType::OCLImage1dArrayWO:
6452 case clang::BuiltinType::OCLImage1dArrayRW:
6453 case clang::BuiltinType::OCLImage1dBufferRO:
6454 case clang::BuiltinType::OCLImage1dBufferWO:
6455 case clang::BuiltinType::OCLImage1dBufferRW:
6456 case clang::BuiltinType::OCLImage2dRO:
6457 case clang::BuiltinType::OCLImage2dWO:
6458 case clang::BuiltinType::OCLImage2dRW:
6459 case clang::BuiltinType::OCLImage2dArrayRO:
6460 case clang::BuiltinType::OCLImage2dArrayWO:
6461 case clang::BuiltinType::OCLImage2dArrayRW:
6462 case clang::BuiltinType::OCLImage3dRO:
6463 case clang::BuiltinType::OCLImage3dWO:
6464 case clang::BuiltinType::OCLImage3dRW:
6465 case clang::BuiltinType::OCLSampler:
6467 case clang::BuiltinType::Bool:
6468 case clang::BuiltinType::Char_U:
6469 case clang::BuiltinType::UChar:
6470 case clang::BuiltinType::WChar_U:
6471 case clang::BuiltinType::Char16:
6472 case clang::BuiltinType::Char32:
6473 case clang::BuiltinType::UShort:
6474 case clang::BuiltinType::UInt:
6475 case clang::BuiltinType::ULong:
6476 case clang::BuiltinType::ULongLong:
6477 case clang::BuiltinType::UInt128:
6478 case clang::BuiltinType::Char_S:
6479 case clang::BuiltinType::SChar:
6480 case clang::BuiltinType::WChar_S:
6481 case clang::BuiltinType::Short:
6482 case clang::BuiltinType::Int:
6483 case clang::BuiltinType::Long:
6484 case clang::BuiltinType::LongLong:
6485 case clang::BuiltinType::Int128:
6486 case clang::BuiltinType::Float:
6487 case clang::BuiltinType::Double:
6488 case clang::BuiltinType::LongDouble:
6489 case clang::BuiltinType::Dependent:
6490 case clang::BuiltinType::Overload:
6491 case clang::BuiltinType::ObjCId:
6492 case clang::BuiltinType::ObjCClass:
6493 case clang::BuiltinType::ObjCSel:
6494 case clang::BuiltinType::BoundMember:
6495 case clang::BuiltinType::Half:
6496 case clang::BuiltinType::ARCUnbridgedCast:
6497 case clang::BuiltinType::PseudoObject:
6498 case clang::BuiltinType::BuiltinFn:
6499 case clang::BuiltinType::OMPArraySection:
6506 case clang::Type::Complex:
6508 case clang::Type::Pointer:
6510 case clang::Type::BlockPointer:
6511 return 0; // If block pointers don't have debug info, then no children for
6513 case clang::Type::LValueReference:
6515 case clang::Type::RValueReference:
6517 case clang::Type::MemberPointer:
6519 case clang::Type::ConstantArray:
6521 case clang::Type::IncompleteArray:
6523 case clang::Type::VariableArray:
6525 case clang::Type::DependentSizedArray:
6527 case clang::Type::DependentSizedExtVector:
6529 case clang::Type::Vector:
6531 case clang::Type::ExtVector:
6533 case clang::Type::FunctionProto:
6534 return 0; // When we function pointers, they have no children...
6535 case clang::Type::FunctionNoProto:
6536 return 0; // When we function pointers, they have no children...
6537 case clang::Type::UnresolvedUsing:
6539 case clang::Type::Paren:
6540 return GetNumPointeeChildren(
6541 llvm::cast<clang::ParenType>(qual_type)->desugar());
6542 case clang::Type::Typedef:
6543 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6545 ->getUnderlyingType());
6546 case clang::Type::Auto:
6547 return GetNumPointeeChildren(
6548 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6549 case clang::Type::Elaborated:
6550 return GetNumPointeeChildren(
6551 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6552 case clang::Type::TypeOfExpr:
6553 return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type)
6554 ->getUnderlyingExpr()
6556 case clang::Type::TypeOf:
6557 return GetNumPointeeChildren(
6558 llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType());
6559 case clang::Type::Decltype:
6560 return GetNumPointeeChildren(
6561 llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType());
6562 case clang::Type::Record:
6564 case clang::Type::Enum:
6566 case clang::Type::TemplateTypeParm:
6568 case clang::Type::SubstTemplateTypeParm:
6570 case clang::Type::TemplateSpecialization:
6572 case clang::Type::InjectedClassName:
6574 case clang::Type::DependentName:
6576 case clang::Type::DependentTemplateSpecialization:
6578 case clang::Type::ObjCObject:
6580 case clang::Type::ObjCInterface:
6582 case clang::Type::ObjCObjectPointer:
6590 CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6591 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6592 bool transparent_pointers, bool omit_empty_base_classes,
6593 bool ignore_array_bounds, std::string &child_name,
6594 uint32_t &child_byte_size, int32_t &child_byte_offset,
6595 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6596 bool &child_is_base_class, bool &child_is_deref_of_parent,
6597 ValueObject *valobj, uint64_t &language_flags) {
6599 return CompilerType();
6601 auto get_exe_scope = [&exe_ctx]() {
6602 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6605 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6606 const clang::Type::TypeClass parent_type_class =
6607 parent_qual_type->getTypeClass();
6608 child_bitfield_bit_size = 0;
6609 child_bitfield_bit_offset = 0;
6610 child_is_base_class = false;
6613 const bool idx_is_valid =
6614 idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx);
6616 switch (parent_type_class) {
6617 case clang::Type::Builtin:
6619 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6620 case clang::BuiltinType::ObjCId:
6621 case clang::BuiltinType::ObjCClass:
6624 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6626 return CompilerType(getASTContext(),
6627 getASTContext()->ObjCBuiltinClassTy);
6635 case clang::Type::Record:
6636 if (idx_is_valid && GetCompleteType(type)) {
6637 const clang::RecordType *record_type =
6638 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6639 const clang::RecordDecl *record_decl = record_type->getDecl();
6640 assert(record_decl);
6641 const clang::ASTRecordLayout &record_layout =
6642 getASTContext()->getASTRecordLayout(record_decl);
6643 uint32_t child_idx = 0;
6645 const clang::CXXRecordDecl *cxx_record_decl =
6646 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6647 if (cxx_record_decl) {
6648 // We might have base classes to print out first
6649 clang::CXXRecordDecl::base_class_const_iterator base_class,
6651 for (base_class = cxx_record_decl->bases_begin(),
6652 base_class_end = cxx_record_decl->bases_end();
6653 base_class != base_class_end; ++base_class) {
6654 const clang::CXXRecordDecl *base_class_decl = nullptr;
6656 // Skip empty base classes
6657 if (omit_empty_base_classes) {
6658 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6659 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6660 if (!ClangASTContext::RecordHasFields(base_class_decl))
6664 if (idx == child_idx) {
6665 if (base_class_decl == nullptr)
6666 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6667 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6669 if (base_class->isVirtual()) {
6670 bool handled = false;
6672 clang::VTableContextBase *vtable_ctx =
6673 getASTContext()->getVTableContext();
6675 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6676 record_layout, cxx_record_decl,
6677 base_class_decl, bit_offset);
6680 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6684 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6688 // Base classes should be a multiple of 8 bits in size
6689 child_byte_offset = bit_offset / 8;
6690 CompilerType base_class_clang_type(getASTContext(),
6691 base_class->getType());
6692 child_name = base_class_clang_type.GetTypeName().AsCString("");
6693 Optional<uint64_t> size =
6694 base_class_clang_type.GetBitSize(get_exe_scope());
6697 uint64_t base_class_clang_type_bit_size = *size;
6699 // Base classes bit sizes should be a multiple of 8 bits in size
6700 assert(base_class_clang_type_bit_size % 8 == 0);
6701 child_byte_size = base_class_clang_type_bit_size / 8;
6702 child_is_base_class = true;
6703 return base_class_clang_type;
6705 // We don't increment the child index in the for loop since we might
6706 // be skipping empty base classes
6710 // Make sure index is in range...
6711 uint32_t field_idx = 0;
6712 clang::RecordDecl::field_iterator field, field_end;
6713 for (field = record_decl->field_begin(),
6714 field_end = record_decl->field_end();
6715 field != field_end; ++field, ++field_idx, ++child_idx) {
6716 if (idx == child_idx) {
6717 // Print the member type if requested
6718 // Print the member name and equal sign
6719 child_name.assign(field->getNameAsString());
6721 // Figure out the type byte size (field_type_info.first) and
6722 // alignment (field_type_info.second) from the AST context.
6723 CompilerType field_clang_type(getASTContext(), field->getType());
6724 assert(field_idx < record_layout.getFieldCount());
6725 Optional<uint64_t> size =
6726 field_clang_type.GetByteSize(get_exe_scope());
6729 child_byte_size = *size;
6730 const uint32_t child_bit_size = child_byte_size * 8;
6732 // Figure out the field offset within the current struct/union/class
6734 bit_offset = record_layout.getFieldOffset(field_idx);
6735 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6736 child_bitfield_bit_size)) {
6737 child_bitfield_bit_offset = bit_offset % child_bit_size;
6738 const uint32_t child_bit_offset =
6739 bit_offset - child_bitfield_bit_offset;
6740 child_byte_offset = child_bit_offset / 8;
6742 child_byte_offset = bit_offset / 8;
6745 return field_clang_type;
6751 case clang::Type::ObjCObject:
6752 case clang::Type::ObjCInterface:
6753 if (idx_is_valid && GetCompleteType(type)) {
6754 const clang::ObjCObjectType *objc_class_type =
6755 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6756 assert(objc_class_type);
6757 if (objc_class_type) {
6758 uint32_t child_idx = 0;
6759 clang::ObjCInterfaceDecl *class_interface_decl =
6760 objc_class_type->getInterface();
6762 if (class_interface_decl) {
6764 const clang::ASTRecordLayout &interface_layout =
6765 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6766 clang::ObjCInterfaceDecl *superclass_interface_decl =
6767 class_interface_decl->getSuperClass();
6768 if (superclass_interface_decl) {
6769 if (omit_empty_base_classes) {
6770 CompilerType base_class_clang_type(
6771 getASTContext(), getASTContext()->getObjCInterfaceType(
6772 superclass_interface_decl));
6773 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes,
6776 clang::QualType ivar_qual_type(
6777 getASTContext()->getObjCInterfaceType(
6778 superclass_interface_decl));
6781 superclass_interface_decl->getNameAsString());
6783 clang::TypeInfo ivar_type_info =
6784 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6786 child_byte_size = ivar_type_info.Width / 8;
6787 child_byte_offset = 0;
6788 child_is_base_class = true;
6790 return CompilerType(getASTContext(), ivar_qual_type);
6799 const uint32_t superclass_idx = child_idx;
6801 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6802 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6803 ivar_end = class_interface_decl->ivar_end();
6805 for (ivar_pos = class_interface_decl->ivar_begin();
6806 ivar_pos != ivar_end; ++ivar_pos) {
6807 if (child_idx == idx) {
6808 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6810 clang::QualType ivar_qual_type(ivar_decl->getType());
6812 child_name.assign(ivar_decl->getNameAsString());
6814 clang::TypeInfo ivar_type_info =
6815 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6817 child_byte_size = ivar_type_info.Width / 8;
6819 // Figure out the field offset within the current
6820 // struct/union/class type For ObjC objects, we can't trust the
6821 // bit offset we get from the Clang AST, since that doesn't
6822 // account for the space taken up by unbacked properties, or
6823 // from the changing size of base classes that are newer than
6824 // this class. So if we have a process around that we can ask
6825 // about this object, do so.
6826 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6827 Process *process = nullptr;
6829 process = exe_ctx->GetProcessPtr();
6831 ObjCLanguageRuntime *objc_runtime =
6832 process->GetObjCLanguageRuntime();
6833 if (objc_runtime != nullptr) {
6834 CompilerType parent_ast_type(getASTContext(),
6836 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6837 parent_ast_type, ivar_decl->getNameAsString().c_str());
6841 // Setting this to INT32_MAX to make sure we don't compute it
6843 bit_offset = INT32_MAX;
6845 if (child_byte_offset ==
6846 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6847 bit_offset = interface_layout.getFieldOffset(child_idx -
6849 child_byte_offset = bit_offset / 8;
6852 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6853 // account for the bit offset of a bitfield within its
6854 // containing object. So regardless of where we get the byte
6855 // offset from, we still need to get the bit offset for
6856 // bitfields from the layout.
6858 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6859 child_bitfield_bit_size)) {
6860 if (bit_offset == INT32_MAX)
6861 bit_offset = interface_layout.getFieldOffset(
6862 child_idx - superclass_idx);
6864 child_bitfield_bit_offset = bit_offset % 8;
6866 return CompilerType(getASTContext(), ivar_qual_type);
6876 case clang::Type::ObjCObjectPointer:
6878 CompilerType pointee_clang_type(GetPointeeType(type));
6880 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6881 child_is_deref_of_parent = false;
6882 bool tmp_child_is_deref_of_parent = false;
6883 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6884 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6885 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6886 child_bitfield_bit_size, child_bitfield_bit_offset,
6887 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6890 child_is_deref_of_parent = true;
6891 const char *parent_name =
6892 valobj ? valobj->GetName().GetCString() : NULL;
6894 child_name.assign(1, '*');
6895 child_name += parent_name;
6898 // We have a pointer to an simple type
6899 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6900 if (Optional<uint64_t> size =
6901 pointee_clang_type.GetByteSize(get_exe_scope())) {
6902 child_byte_size = *size;
6903 child_byte_offset = 0;
6904 return pointee_clang_type;
6911 case clang::Type::Vector:
6912 case clang::Type::ExtVector:
6914 const clang::VectorType *array =
6915 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6917 CompilerType element_type(getASTContext(), array->getElementType());
6918 if (element_type.GetCompleteType()) {
6919 char element_name[64];
6920 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6921 static_cast<uint64_t>(idx));
6922 child_name.assign(element_name);
6923 if (Optional<uint64_t> size =
6924 element_type.GetByteSize(get_exe_scope())) {
6925 child_byte_size = *size;
6926 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6927 return element_type;
6934 case clang::Type::ConstantArray:
6935 case clang::Type::IncompleteArray:
6936 if (ignore_array_bounds || idx_is_valid) {
6937 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6939 CompilerType element_type(getASTContext(), array->getElementType());
6940 if (element_type.GetCompleteType()) {
6941 child_name = llvm::formatv("[{0}]", idx);
6942 if (Optional<uint64_t> size =
6943 element_type.GetByteSize(get_exe_scope())) {
6944 child_byte_size = *size;
6945 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6946 return element_type;
6953 case clang::Type::Pointer: {
6954 CompilerType pointee_clang_type(GetPointeeType(type));
6956 // Don't dereference "void *" pointers
6957 if (pointee_clang_type.IsVoidType())
6958 return CompilerType();
6960 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6961 child_is_deref_of_parent = false;
6962 bool tmp_child_is_deref_of_parent = false;
6963 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6964 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6965 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6966 child_bitfield_bit_size, child_bitfield_bit_offset,
6967 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6970 child_is_deref_of_parent = true;
6972 const char *parent_name =
6973 valobj ? valobj->GetName().GetCString() : NULL;
6975 child_name.assign(1, '*');
6976 child_name += parent_name;
6979 // We have a pointer to an simple type
6981 if (Optional<uint64_t> size =
6982 pointee_clang_type.GetByteSize(get_exe_scope())) {
6983 child_byte_size = *size;
6984 child_byte_offset = 0;
6985 return pointee_clang_type;
6992 case clang::Type::LValueReference:
6993 case clang::Type::RValueReference:
6995 const clang::ReferenceType *reference_type =
6996 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6997 CompilerType pointee_clang_type(getASTContext(),
6998 reference_type->getPointeeType());
6999 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
7000 child_is_deref_of_parent = false;
7001 bool tmp_child_is_deref_of_parent = false;
7002 return pointee_clang_type.GetChildCompilerTypeAtIndex(
7003 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7004 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7005 child_bitfield_bit_size, child_bitfield_bit_offset,
7006 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
7009 const char *parent_name =
7010 valobj ? valobj->GetName().GetCString() : NULL;
7012 child_name.assign(1, '&');
7013 child_name += parent_name;
7016 // We have a pointer to an simple type
7018 if (Optional<uint64_t> size =
7019 pointee_clang_type.GetByteSize(get_exe_scope())) {
7020 child_byte_size = *size;
7021 child_byte_offset = 0;
7022 return pointee_clang_type;
7029 case clang::Type::Typedef: {
7030 CompilerType typedefed_clang_type(
7031 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
7033 ->getUnderlyingType());
7034 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
7035 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7036 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7037 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7038 child_is_deref_of_parent, valobj, language_flags);
7041 case clang::Type::Auto: {
7042 CompilerType elaborated_clang_type(
7044 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
7045 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7046 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7047 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7048 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7049 child_is_deref_of_parent, valobj, language_flags);
7052 case clang::Type::Elaborated: {
7053 CompilerType elaborated_clang_type(
7055 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
7056 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
7057 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7058 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7059 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7060 child_is_deref_of_parent, valobj, language_flags);
7063 case clang::Type::Paren: {
7064 CompilerType paren_clang_type(
7066 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
7067 return paren_clang_type.GetChildCompilerTypeAtIndex(
7068 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
7069 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
7070 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
7071 child_is_deref_of_parent, valobj, language_flags);
7077 return CompilerType();
7080 static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
7081 const clang::CXXBaseSpecifier *base_spec,
7082 bool omit_empty_base_classes) {
7083 uint32_t child_idx = 0;
7085 const clang::CXXRecordDecl *cxx_record_decl =
7086 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7088 // const char *super_name = record_decl->getNameAsCString();
7089 // const char *base_name =
7090 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
7091 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
7093 if (cxx_record_decl) {
7094 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
7095 for (base_class = cxx_record_decl->bases_begin(),
7096 base_class_end = cxx_record_decl->bases_end();
7097 base_class != base_class_end; ++base_class) {
7098 if (omit_empty_base_classes) {
7099 if (BaseSpecifierIsEmpty(base_class))
7103 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
7104 // super_name, base_name,
7106 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
7109 if (base_class == base_spec)
7118 static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
7119 clang::NamedDecl *canonical_decl,
7120 bool omit_empty_base_classes) {
7121 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
7122 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
7123 omit_empty_base_classes);
7125 clang::RecordDecl::field_iterator field, field_end;
7126 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
7127 field != field_end; ++field, ++child_idx) {
7128 if (field->getCanonicalDecl() == canonical_decl)
7135 // Look for a child member (doesn't include base classes, but it does include
7136 // their members) in the type hierarchy. Returns an index path into
7137 // "clang_type" on how to reach the appropriate member.
7156 // If we have a clang type that describes "class C", and we wanted to looked
7159 // With omit_empty_base_classes == false we would get an integer array back
7160 // with: { 1, 1 } The first index 1 is the child index for "class A" within
7161 // class C The second index 1 is the child index for "m_b" within class A
7163 // With omit_empty_base_classes == true we would get an integer array back
7164 // with: { 0, 1 } The first index 0 is the child index for "class A" within
7165 // class C (since class B doesn't have any members it doesn't count) The second
7166 // index 1 is the child index for "m_b" within class A
7168 size_t ClangASTContext::GetIndexOfChildMemberWithName(
7169 lldb::opaque_compiler_type_t type, const char *name,
7170 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
7171 if (type && name && name[0]) {
7172 clang::QualType qual_type(GetCanonicalQualType(type));
7173 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7174 switch (type_class) {
7175 case clang::Type::Record:
7176 if (GetCompleteType(type)) {
7177 const clang::RecordType *record_type =
7178 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7179 const clang::RecordDecl *record_decl = record_type->getDecl();
7181 assert(record_decl);
7182 uint32_t child_idx = 0;
7184 const clang::CXXRecordDecl *cxx_record_decl =
7185 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7187 // Try and find a field that matches NAME
7188 clang::RecordDecl::field_iterator field, field_end;
7189 llvm::StringRef name_sref(name);
7190 for (field = record_decl->field_begin(),
7191 field_end = record_decl->field_end();
7192 field != field_end; ++field, ++child_idx) {
7193 llvm::StringRef field_name = field->getName();
7194 if (field_name.empty()) {
7195 CompilerType field_type(getASTContext(), field->getType());
7196 child_indexes.push_back(child_idx);
7197 if (field_type.GetIndexOfChildMemberWithName(
7198 name, omit_empty_base_classes, child_indexes))
7199 return child_indexes.size();
7200 child_indexes.pop_back();
7202 } else if (field_name.equals(name_sref)) {
7203 // We have to add on the number of base classes to this index!
7204 child_indexes.push_back(
7205 child_idx + ClangASTContext::GetNumBaseClasses(
7206 cxx_record_decl, omit_empty_base_classes));
7207 return child_indexes.size();
7211 if (cxx_record_decl) {
7212 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7214 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7216 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7217 // Didn't find things easily, lets let clang do its thang...
7218 clang::IdentifierInfo &ident_ref =
7219 getASTContext()->Idents.get(name_sref);
7220 clang::DeclarationName decl_name(&ident_ref);
7222 clang::CXXBasePaths paths;
7223 if (cxx_record_decl->lookupInBases(
7224 [decl_name](const clang::CXXBaseSpecifier *specifier,
7225 clang::CXXBasePath &path) {
7226 return clang::CXXRecordDecl::FindOrdinaryMember(
7227 specifier, path, decl_name);
7230 clang::CXXBasePaths::const_paths_iterator path,
7231 path_end = paths.end();
7232 for (path = paths.begin(); path != path_end; ++path) {
7233 const size_t num_path_elements = path->size();
7234 for (size_t e = 0; e < num_path_elements; ++e) {
7235 clang::CXXBasePathElement elem = (*path)[e];
7237 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7238 omit_empty_base_classes);
7239 if (child_idx == UINT32_MAX) {
7240 child_indexes.clear();
7243 child_indexes.push_back(child_idx);
7244 parent_record_decl = llvm::cast<clang::RecordDecl>(
7245 elem.Base->getType()
7246 ->getAs<clang::RecordType>()
7250 for (clang::NamedDecl *path_decl : path->Decls) {
7251 child_idx = GetIndexForRecordChild(
7252 parent_record_decl, path_decl, omit_empty_base_classes);
7253 if (child_idx == UINT32_MAX) {
7254 child_indexes.clear();
7257 child_indexes.push_back(child_idx);
7261 return child_indexes.size();
7267 case clang::Type::ObjCObject:
7268 case clang::Type::ObjCInterface:
7269 if (GetCompleteType(type)) {
7270 llvm::StringRef name_sref(name);
7271 const clang::ObjCObjectType *objc_class_type =
7272 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7273 assert(objc_class_type);
7274 if (objc_class_type) {
7275 uint32_t child_idx = 0;
7276 clang::ObjCInterfaceDecl *class_interface_decl =
7277 objc_class_type->getInterface();
7279 if (class_interface_decl) {
7280 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7281 ivar_end = class_interface_decl->ivar_end();
7282 clang::ObjCInterfaceDecl *superclass_interface_decl =
7283 class_interface_decl->getSuperClass();
7285 for (ivar_pos = class_interface_decl->ivar_begin();
7286 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7287 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7289 if (ivar_decl->getName().equals(name_sref)) {
7290 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7291 (omit_empty_base_classes &&
7292 ObjCDeclHasIVars(superclass_interface_decl, true)))
7295 child_indexes.push_back(child_idx);
7296 return child_indexes.size();
7300 if (superclass_interface_decl) {
7301 // The super class index is always zero for ObjC classes, so we
7302 // push it onto the child indexes in case we find an ivar in our
7304 child_indexes.push_back(0);
7306 CompilerType superclass_clang_type(
7307 getASTContext(), getASTContext()->getObjCInterfaceType(
7308 superclass_interface_decl));
7309 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7310 name, omit_empty_base_classes, child_indexes)) {
7311 // We did find an ivar in a superclass so just return the
7313 return child_indexes.size();
7316 // We didn't find an ivar matching "name" in our superclass, pop
7317 // the superclass zero index that we pushed on above.
7318 child_indexes.pop_back();
7325 case clang::Type::ObjCObjectPointer: {
7326 CompilerType objc_object_clang_type(
7328 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7329 ->getPointeeType());
7330 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7331 name, omit_empty_base_classes, child_indexes);
7334 case clang::Type::ConstantArray: {
7335 // const clang::ConstantArrayType *array =
7336 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7337 // const uint64_t element_count =
7338 // array->getSize().getLimitedValue();
7340 // if (idx < element_count)
7342 // std::pair<uint64_t, unsigned> field_type_info =
7343 // ast->getTypeInfo(array->getElementType());
7345 // char element_name[32];
7346 // ::snprintf (element_name, sizeof (element_name),
7347 // "%s[%u]", parent_name ? parent_name : "", idx);
7349 // child_name.assign(element_name);
7350 // assert(field_type_info.first % 8 == 0);
7351 // child_byte_size = field_type_info.first / 8;
7352 // child_byte_offset = idx * child_byte_size;
7353 // return array->getElementType().getAsOpaquePtr();
7357 // case clang::Type::MemberPointerType:
7359 // MemberPointerType *mem_ptr_type =
7360 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7361 // clang::QualType pointee_type =
7362 // mem_ptr_type->getPointeeType();
7364 // if (ClangASTContext::IsAggregateType
7365 // (pointee_type.getAsOpaquePtr()))
7367 // return GetIndexOfChildWithName (ast,
7368 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7374 case clang::Type::LValueReference:
7375 case clang::Type::RValueReference: {
7376 const clang::ReferenceType *reference_type =
7377 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7378 clang::QualType pointee_type(reference_type->getPointeeType());
7379 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7381 if (pointee_clang_type.IsAggregateType()) {
7382 return pointee_clang_type.GetIndexOfChildMemberWithName(
7383 name, omit_empty_base_classes, child_indexes);
7387 case clang::Type::Pointer: {
7388 CompilerType pointee_clang_type(GetPointeeType(type));
7390 if (pointee_clang_type.IsAggregateType()) {
7391 return pointee_clang_type.GetIndexOfChildMemberWithName(
7392 name, omit_empty_base_classes, child_indexes);
7396 case clang::Type::Typedef:
7397 return CompilerType(getASTContext(),
7398 llvm::cast<clang::TypedefType>(qual_type)
7400 ->getUnderlyingType())
7401 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7404 case clang::Type::Auto:
7405 return CompilerType(
7407 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7408 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7411 case clang::Type::Elaborated:
7412 return CompilerType(
7414 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7415 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7418 case clang::Type::Paren:
7419 return CompilerType(getASTContext(),
7420 llvm::cast<clang::ParenType>(qual_type)->desugar())
7421 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7431 // Get the index of the child of "clang_type" whose name matches. This function
7432 // doesn't descend into the children, but only looks one level deep and name
7433 // matches can include base class names.
7436 ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7438 bool omit_empty_base_classes) {
7439 if (type && name && name[0]) {
7440 clang::QualType qual_type(GetCanonicalQualType(type));
7442 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7444 switch (type_class) {
7445 case clang::Type::Record:
7446 if (GetCompleteType(type)) {
7447 const clang::RecordType *record_type =
7448 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7449 const clang::RecordDecl *record_decl = record_type->getDecl();
7451 assert(record_decl);
7452 uint32_t child_idx = 0;
7454 const clang::CXXRecordDecl *cxx_record_decl =
7455 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7457 if (cxx_record_decl) {
7458 clang::CXXRecordDecl::base_class_const_iterator base_class,
7460 for (base_class = cxx_record_decl->bases_begin(),
7461 base_class_end = cxx_record_decl->bases_end();
7462 base_class != base_class_end; ++base_class) {
7463 // Skip empty base classes
7464 clang::CXXRecordDecl *base_class_decl =
7465 llvm::cast<clang::CXXRecordDecl>(
7466 base_class->getType()
7467 ->getAs<clang::RecordType>()
7469 if (omit_empty_base_classes &&
7470 !ClangASTContext::RecordHasFields(base_class_decl))
7473 CompilerType base_class_clang_type(getASTContext(),
7474 base_class->getType());
7475 std::string base_class_type_name(
7476 base_class_clang_type.GetTypeName().AsCString(""));
7477 if (base_class_type_name == name)
7483 // Try and find a field that matches NAME
7484 clang::RecordDecl::field_iterator field, field_end;
7485 llvm::StringRef name_sref(name);
7486 for (field = record_decl->field_begin(),
7487 field_end = record_decl->field_end();
7488 field != field_end; ++field, ++child_idx) {
7489 if (field->getName().equals(name_sref))
7495 case clang::Type::ObjCObject:
7496 case clang::Type::ObjCInterface:
7497 if (GetCompleteType(type)) {
7498 llvm::StringRef name_sref(name);
7499 const clang::ObjCObjectType *objc_class_type =
7500 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7501 assert(objc_class_type);
7502 if (objc_class_type) {
7503 uint32_t child_idx = 0;
7504 clang::ObjCInterfaceDecl *class_interface_decl =
7505 objc_class_type->getInterface();
7507 if (class_interface_decl) {
7508 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7509 ivar_end = class_interface_decl->ivar_end();
7510 clang::ObjCInterfaceDecl *superclass_interface_decl =
7511 class_interface_decl->getSuperClass();
7513 for (ivar_pos = class_interface_decl->ivar_begin();
7514 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7515 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7517 if (ivar_decl->getName().equals(name_sref)) {
7518 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7519 (omit_empty_base_classes &&
7520 ObjCDeclHasIVars(superclass_interface_decl, true)))
7527 if (superclass_interface_decl) {
7528 if (superclass_interface_decl->getName().equals(name_sref))
7536 case clang::Type::ObjCObjectPointer: {
7537 CompilerType pointee_clang_type(
7539 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7540 ->getPointeeType());
7541 return pointee_clang_type.GetIndexOfChildWithName(
7542 name, omit_empty_base_classes);
7545 case clang::Type::ConstantArray: {
7546 // const clang::ConstantArrayType *array =
7547 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7548 // const uint64_t element_count =
7549 // array->getSize().getLimitedValue();
7551 // if (idx < element_count)
7553 // std::pair<uint64_t, unsigned> field_type_info =
7554 // ast->getTypeInfo(array->getElementType());
7556 // char element_name[32];
7557 // ::snprintf (element_name, sizeof (element_name),
7558 // "%s[%u]", parent_name ? parent_name : "", idx);
7560 // child_name.assign(element_name);
7561 // assert(field_type_info.first % 8 == 0);
7562 // child_byte_size = field_type_info.first / 8;
7563 // child_byte_offset = idx * child_byte_size;
7564 // return array->getElementType().getAsOpaquePtr();
7568 // case clang::Type::MemberPointerType:
7570 // MemberPointerType *mem_ptr_type =
7571 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7572 // clang::QualType pointee_type =
7573 // mem_ptr_type->getPointeeType();
7575 // if (ClangASTContext::IsAggregateType
7576 // (pointee_type.getAsOpaquePtr()))
7578 // return GetIndexOfChildWithName (ast,
7579 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7585 case clang::Type::LValueReference:
7586 case clang::Type::RValueReference: {
7587 const clang::ReferenceType *reference_type =
7588 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7589 CompilerType pointee_type(getASTContext(),
7590 reference_type->getPointeeType());
7592 if (pointee_type.IsAggregateType()) {
7593 return pointee_type.GetIndexOfChildWithName(name,
7594 omit_empty_base_classes);
7598 case clang::Type::Pointer: {
7599 const clang::PointerType *pointer_type =
7600 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7601 CompilerType pointee_type(getASTContext(),
7602 pointer_type->getPointeeType());
7604 if (pointee_type.IsAggregateType()) {
7605 return pointee_type.GetIndexOfChildWithName(name,
7606 omit_empty_base_classes);
7610 // child_name.assign(1, '*');
7611 // child_name += parent_name;
7614 // // We have a pointer to an simple type
7617 // std::pair<uint64_t, unsigned> clang_type_info
7618 // = ast->getTypeInfo(pointee_type);
7619 // assert(clang_type_info.first % 8 == 0);
7620 // child_byte_size = clang_type_info.first / 8;
7621 // child_byte_offset = 0;
7622 // return pointee_type.getAsOpaquePtr();
7627 case clang::Type::Auto:
7628 return CompilerType(
7630 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7631 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7633 case clang::Type::Elaborated:
7634 return CompilerType(
7636 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7637 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7639 case clang::Type::Paren:
7640 return CompilerType(getASTContext(),
7641 llvm::cast<clang::ParenType>(qual_type)->desugar())
7642 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7644 case clang::Type::Typedef:
7645 return CompilerType(getASTContext(),
7646 llvm::cast<clang::TypedefType>(qual_type)
7648 ->getUnderlyingType())
7649 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7659 ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7663 clang::QualType qual_type(GetCanonicalQualType(type));
7664 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7665 switch (type_class) {
7666 case clang::Type::Record:
7667 if (GetCompleteType(type)) {
7668 const clang::CXXRecordDecl *cxx_record_decl =
7669 qual_type->getAsCXXRecordDecl();
7670 if (cxx_record_decl) {
7671 const clang::ClassTemplateSpecializationDecl *template_decl =
7672 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7675 return template_decl->getTemplateArgs().size();
7680 case clang::Type::Typedef:
7681 return (CompilerType(getASTContext(),
7682 llvm::cast<clang::TypedefType>(qual_type)
7684 ->getUnderlyingType()))
7685 .GetNumTemplateArguments();
7687 case clang::Type::Auto:
7688 return (CompilerType(
7690 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7691 .GetNumTemplateArguments();
7693 case clang::Type::Elaborated:
7694 return (CompilerType(
7696 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7697 .GetNumTemplateArguments();
7699 case clang::Type::Paren:
7700 return (CompilerType(getASTContext(),
7701 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7702 .GetNumTemplateArguments();
7711 const clang::ClassTemplateSpecializationDecl *
7712 ClangASTContext::GetAsTemplateSpecialization(
7713 lldb::opaque_compiler_type_t type) {
7717 clang::QualType qual_type(GetCanonicalQualType(type));
7718 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7719 switch (type_class) {
7720 case clang::Type::Record: {
7721 if (! GetCompleteType(type))
7723 const clang::CXXRecordDecl *cxx_record_decl =
7724 qual_type->getAsCXXRecordDecl();
7725 if (!cxx_record_decl)
7727 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7731 case clang::Type::Typedef:
7732 return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type)
7734 ->getUnderlyingType()
7737 case clang::Type::Auto:
7738 return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type)
7742 case clang::Type::Elaborated:
7743 return GetAsTemplateSpecialization(
7744 llvm::cast<clang::ElaboratedType>(qual_type)
7748 case clang::Type::Paren:
7749 return GetAsTemplateSpecialization(
7750 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
7757 lldb::TemplateArgumentKind
7758 ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
7760 const clang::ClassTemplateSpecializationDecl *template_decl =
7761 GetAsTemplateSpecialization(type);
7762 if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size())
7763 return eTemplateArgumentKindNull;
7765 switch (template_decl->getTemplateArgs()[arg_idx].getKind()) {
7766 case clang::TemplateArgument::Null:
7767 return eTemplateArgumentKindNull;
7769 case clang::TemplateArgument::NullPtr:
7770 return eTemplateArgumentKindNullPtr;
7772 case clang::TemplateArgument::Type:
7773 return eTemplateArgumentKindType;
7775 case clang::TemplateArgument::Declaration:
7776 return eTemplateArgumentKindDeclaration;
7778 case clang::TemplateArgument::Integral:
7779 return eTemplateArgumentKindIntegral;
7781 case clang::TemplateArgument::Template:
7782 return eTemplateArgumentKindTemplate;
7784 case clang::TemplateArgument::TemplateExpansion:
7785 return eTemplateArgumentKindTemplateExpansion;
7787 case clang::TemplateArgument::Expression:
7788 return eTemplateArgumentKindExpression;
7790 case clang::TemplateArgument::Pack:
7791 return eTemplateArgumentKindPack;
7793 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7797 ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
7799 const clang::ClassTemplateSpecializationDecl *template_decl =
7800 GetAsTemplateSpecialization(type);
7801 if (!template_decl || idx >= template_decl->getTemplateArgs().size())
7802 return CompilerType();
7804 const clang::TemplateArgument &template_arg =
7805 template_decl->getTemplateArgs()[idx];
7806 if (template_arg.getKind() != clang::TemplateArgument::Type)
7807 return CompilerType();
7809 return CompilerType(getASTContext(), template_arg.getAsType());
7812 Optional<CompilerType::IntegralTemplateArgument>
7813 ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
7815 const clang::ClassTemplateSpecializationDecl *template_decl =
7816 GetAsTemplateSpecialization(type);
7817 if (! template_decl || idx >= template_decl->getTemplateArgs().size())
7820 const clang::TemplateArgument &template_arg =
7821 template_decl->getTemplateArgs()[idx];
7822 if (template_arg.getKind() != clang::TemplateArgument::Integral)
7825 return {{template_arg.getAsIntegral(),
7826 CompilerType(getASTContext(), template_arg.getIntegralType())}};
7829 CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7831 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7832 return CompilerType();
7835 clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7836 const clang::EnumType *enutype =
7837 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7839 return enutype->getDecl();
7843 clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7844 const clang::RecordType *record_type =
7845 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7847 return record_type->getDecl();
7851 clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7852 return ClangUtil::GetAsTagDecl(type);
7855 clang::TypedefNameDecl *
7856 ClangASTContext::GetAsTypedefDecl(const CompilerType &type) {
7857 const clang::TypedefType *typedef_type =
7858 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7860 return typedef_type->getDecl();
7864 clang::CXXRecordDecl *
7865 ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7866 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7869 clang::ObjCInterfaceDecl *
7870 ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7871 const clang::ObjCObjectType *objc_class_type =
7872 llvm::dyn_cast<clang::ObjCObjectType>(
7873 ClangUtil::GetCanonicalQualType(type));
7874 if (objc_class_type)
7875 return objc_class_type->getInterface();
7879 clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7880 const CompilerType &type, llvm::StringRef name,
7881 const CompilerType &field_clang_type, AccessType access,
7882 uint32_t bitfield_bit_size) {
7883 if (!type.IsValid() || !field_clang_type.IsValid())
7885 ClangASTContext *ast =
7886 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7889 clang::ASTContext *clang_ast = ast->getASTContext();
7890 clang::IdentifierInfo *ident = nullptr;
7892 ident = &clang_ast->Idents.get(name);
7894 clang::FieldDecl *field = nullptr;
7896 clang::Expr *bit_width = nullptr;
7897 if (bitfield_bit_size != 0) {
7898 llvm::APInt bitfield_bit_size_apint(
7899 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7900 bit_width = new (*clang_ast)
7901 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7902 clang_ast->IntTy, clang::SourceLocation());
7905 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7907 field = clang::FieldDecl::Create(
7908 *clang_ast, record_decl, clang::SourceLocation(),
7909 clang::SourceLocation(),
7910 ident, // Identifier
7911 ClangUtil::GetQualType(field_clang_type), // Field type
7913 bit_width, // BitWidth
7915 clang::ICIS_NoInit); // HasInit
7918 // Determine whether this field corresponds to an anonymous struct or
7920 if (const clang::TagType *TagT =
7921 field->getType()->getAs<clang::TagType>()) {
7922 if (clang::RecordDecl *Rec =
7923 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7924 if (!Rec->getDeclName()) {
7925 Rec->setAnonymousStructOrUnion(true);
7926 field->setImplicit();
7933 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7935 record_decl->addDecl(field);
7937 #ifdef LLDB_CONFIGURATION_DEBUG
7942 clang::ObjCInterfaceDecl *class_interface_decl =
7943 ast->GetAsObjCInterfaceDecl(type);
7945 if (class_interface_decl) {
7946 const bool is_synthesized = false;
7948 field_clang_type.GetCompleteType();
7950 field = clang::ObjCIvarDecl::Create(
7951 *clang_ast, class_interface_decl, clang::SourceLocation(),
7952 clang::SourceLocation(),
7953 ident, // Identifier
7954 ClangUtil::GetQualType(field_clang_type), // Field type
7955 nullptr, // TypeSourceInfo *
7956 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7960 class_interface_decl->addDecl(field);
7962 #ifdef LLDB_CONFIGURATION_DEBUG
7971 void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7975 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7979 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7984 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7986 IndirectFieldVector indirect_fields;
7987 clang::RecordDecl::field_iterator field_pos;
7988 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7989 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7990 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7991 last_field_pos = field_pos++) {
7992 if (field_pos->isAnonymousStructOrUnion()) {
7993 clang::QualType field_qual_type = field_pos->getType();
7995 const clang::RecordType *field_record_type =
7996 field_qual_type->getAs<clang::RecordType>();
7998 if (!field_record_type)
8001 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
8003 if (!field_record_decl)
8006 for (clang::RecordDecl::decl_iterator
8007 di = field_record_decl->decls_begin(),
8008 de = field_record_decl->decls_end();
8010 if (clang::FieldDecl *nested_field_decl =
8011 llvm::dyn_cast<clang::FieldDecl>(*di)) {
8012 clang::NamedDecl **chain =
8013 new (*ast->getASTContext()) clang::NamedDecl *[2];
8014 chain[0] = *field_pos;
8015 chain[1] = nested_field_decl;
8016 clang::IndirectFieldDecl *indirect_field =
8017 clang::IndirectFieldDecl::Create(
8018 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8019 nested_field_decl->getIdentifier(),
8020 nested_field_decl->getType(), {chain, 2});
8022 indirect_field->setImplicit();
8024 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8025 field_pos->getAccess(), nested_field_decl->getAccess()));
8027 indirect_fields.push_back(indirect_field);
8028 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
8029 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
8030 size_t nested_chain_size =
8031 nested_indirect_field_decl->getChainingSize();
8032 clang::NamedDecl **chain = new (*ast->getASTContext())
8033 clang::NamedDecl *[nested_chain_size + 1];
8034 chain[0] = *field_pos;
8036 int chain_index = 1;
8037 for (clang::IndirectFieldDecl::chain_iterator
8038 nci = nested_indirect_field_decl->chain_begin(),
8039 nce = nested_indirect_field_decl->chain_end();
8041 chain[chain_index] = *nci;
8045 clang::IndirectFieldDecl *indirect_field =
8046 clang::IndirectFieldDecl::Create(
8047 *ast->getASTContext(), record_decl, clang::SourceLocation(),
8048 nested_indirect_field_decl->getIdentifier(),
8049 nested_indirect_field_decl->getType(),
8050 {chain, nested_chain_size + 1});
8052 indirect_field->setImplicit();
8054 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
8055 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
8057 indirect_fields.push_back(indirect_field);
8063 // Check the last field to see if it has an incomplete array type as its last
8064 // member and if it does, the tell the record decl about it
8065 if (last_field_pos != field_end_pos) {
8066 if (last_field_pos->getType()->isIncompleteArrayType())
8067 record_decl->hasFlexibleArrayMember();
8070 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
8071 ife = indirect_fields.end();
8073 record_decl->addDecl(*ifi);
8077 void ClangASTContext::SetIsPacked(const CompilerType &type) {
8079 ClangASTContext *ast =
8080 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8082 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
8087 record_decl->addAttr(
8088 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
8093 clang::VarDecl *ClangASTContext::AddVariableToRecordType(
8094 const CompilerType &type, llvm::StringRef name,
8095 const CompilerType &var_type, AccessType access) {
8096 if (!type.IsValid() || !var_type.IsValid())
8099 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8103 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
8107 clang::VarDecl *var_decl = nullptr;
8108 clang::IdentifierInfo *ident = nullptr;
8110 ident = &ast->getASTContext()->Idents.get(name);
8112 var_decl = clang::VarDecl::Create(
8113 *ast->getASTContext(), // ASTContext &
8114 record_decl, // DeclContext *
8115 clang::SourceLocation(), // clang::SourceLocation StartLoc
8116 clang::SourceLocation(), // clang::SourceLocation IdLoc
8117 ident, // clang::IdentifierInfo *
8118 ClangUtil::GetQualType(var_type), // Variable clang::QualType
8119 nullptr, // TypeSourceInfo *
8120 clang::SC_Static); // StorageClass
8124 var_decl->setAccess(
8125 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
8126 record_decl->addDecl(var_decl);
8128 #ifdef LLDB_CONFIGURATION_DEBUG
8129 VerifyDecl(var_decl);
8135 clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
8136 lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name,
8137 const CompilerType &method_clang_type, lldb::AccessType access,
8138 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
8139 bool is_attr_used, bool is_artificial) {
8140 if (!type || !method_clang_type.IsValid() || name == nullptr ||
8144 clang::QualType record_qual_type(GetCanonicalQualType(type));
8146 clang::CXXRecordDecl *cxx_record_decl =
8147 record_qual_type->getAsCXXRecordDecl();
8149 if (cxx_record_decl == nullptr)
8152 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8154 clang::CXXMethodDecl *cxx_method_decl = nullptr;
8156 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
8158 const clang::FunctionType *function_type =
8159 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
8161 if (function_type == nullptr)
8164 const clang::FunctionProtoType *method_function_prototype(
8165 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
8167 if (!method_function_prototype)
8170 unsigned int num_params = method_function_prototype->getNumParams();
8172 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
8173 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
8176 return nullptr; // skip everything artificial
8178 if (name[0] == '~') {
8179 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
8180 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8181 clang::DeclarationNameInfo(
8182 getASTContext()->DeclarationNames.getCXXDestructorName(
8183 getASTContext()->getCanonicalType(record_qual_type)),
8184 clang::SourceLocation()),
8185 method_qual_type, nullptr, is_inline, is_artificial);
8186 cxx_method_decl = cxx_dtor_decl;
8187 } else if (decl_name == cxx_record_decl->getDeclName()) {
8188 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
8189 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8190 clang::DeclarationNameInfo(
8191 getASTContext()->DeclarationNames.getCXXConstructorName(
8192 getASTContext()->getCanonicalType(record_qual_type)),
8193 clang::SourceLocation()),
8195 nullptr, // TypeSourceInfo *
8196 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
8197 cxx_method_decl = cxx_ctor_decl;
8199 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
8200 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
8202 if (IsOperator(name, op_kind)) {
8203 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
8204 // Check the number of operator parameters. Sometimes we have seen bad
8205 // DWARF that doesn't correctly describe operators and if we try to
8206 // create a method and add it to the class, clang will assert and
8207 // crash, so we need to make sure things are acceptable.
8208 const bool is_method = true;
8209 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
8210 is_method, op_kind, num_params))
8212 cxx_method_decl = clang::CXXMethodDecl::Create(
8213 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8214 clang::DeclarationNameInfo(
8215 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
8216 clang::SourceLocation()),
8218 nullptr, // TypeSourceInfo *
8219 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8220 } else if (num_params == 0) {
8221 // Conversion operators don't take params...
8222 cxx_method_decl = clang::CXXConversionDecl::Create(
8223 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8224 clang::DeclarationNameInfo(
8225 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
8226 getASTContext()->getCanonicalType(
8227 function_type->getReturnType())),
8228 clang::SourceLocation()),
8230 nullptr, // TypeSourceInfo *
8231 is_inline, is_explicit, false /*is_constexpr*/,
8232 clang::SourceLocation());
8236 if (cxx_method_decl == nullptr) {
8237 cxx_method_decl = clang::CXXMethodDecl::Create(
8238 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
8239 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
8241 nullptr, // TypeSourceInfo *
8242 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
8246 clang::AccessSpecifier access_specifier =
8247 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8249 cxx_method_decl->setAccess(access_specifier);
8250 cxx_method_decl->setVirtualAsWritten(is_virtual);
8253 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8255 if (mangled_name != NULL) {
8256 cxx_method_decl->addAttr(
8257 clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name));
8260 // Populate the method decl with parameter decls
8262 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8264 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8265 params.push_back(clang::ParmVarDecl::Create(
8266 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8267 clang::SourceLocation(),
8268 nullptr, // anonymous
8269 method_function_prototype->getParamType(param_index), nullptr,
8270 clang::SC_None, nullptr));
8273 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8275 cxx_record_decl->addDecl(cxx_method_decl);
8277 // Sometimes the debug info will mention a constructor (default/copy/move),
8278 // destructor, or assignment operator (copy/move) but there won't be any
8279 // version of this in the code. So we check if the function was artificially
8280 // generated and if it is trivial and this lets the compiler/backend know
8281 // that it can inline the IR for these when it needs to and we can avoid a
8282 // "missing function" error when running expressions.
8284 if (is_artificial) {
8285 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8286 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8287 (cxx_ctor_decl->isCopyConstructor() &&
8288 cxx_record_decl->hasTrivialCopyConstructor()) ||
8289 (cxx_ctor_decl->isMoveConstructor() &&
8290 cxx_record_decl->hasTrivialMoveConstructor()))) {
8291 cxx_ctor_decl->setDefaulted();
8292 cxx_ctor_decl->setTrivial(true);
8293 } else if (cxx_dtor_decl) {
8294 if (cxx_record_decl->hasTrivialDestructor()) {
8295 cxx_dtor_decl->setDefaulted();
8296 cxx_dtor_decl->setTrivial(true);
8298 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8299 cxx_record_decl->hasTrivialCopyAssignment()) ||
8300 (cxx_method_decl->isMoveAssignmentOperator() &&
8301 cxx_record_decl->hasTrivialMoveAssignment())) {
8302 cxx_method_decl->setDefaulted();
8303 cxx_method_decl->setTrivial(true);
8307 #ifdef LLDB_CONFIGURATION_DEBUG
8308 VerifyDecl(cxx_method_decl);
8311 // printf ("decl->isPolymorphic() = %i\n",
8312 // cxx_record_decl->isPolymorphic());
8313 // printf ("decl->isAggregate() = %i\n",
8314 // cxx_record_decl->isAggregate());
8315 // printf ("decl->isPOD() = %i\n",
8316 // cxx_record_decl->isPOD());
8317 // printf ("decl->isEmpty() = %i\n",
8318 // cxx_record_decl->isEmpty());
8319 // printf ("decl->isAbstract() = %i\n",
8320 // cxx_record_decl->isAbstract());
8321 // printf ("decl->hasTrivialConstructor() = %i\n",
8322 // cxx_record_decl->hasTrivialConstructor());
8323 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8324 // cxx_record_decl->hasTrivialCopyConstructor());
8325 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8326 // cxx_record_decl->hasTrivialCopyAssignment());
8327 // printf ("decl->hasTrivialDestructor() = %i\n",
8328 // cxx_record_decl->hasTrivialDestructor());
8329 return cxx_method_decl;
8332 void ClangASTContext::AddMethodOverridesForCXXRecordType(
8333 lldb::opaque_compiler_type_t type) {
8334 if (auto *record = GetAsCXXRecordDecl(type))
8335 for (auto *method : record->methods())
8336 addOverridesForMethod(method);
8339 #pragma mark C++ Base Classes
8341 std::unique_ptr<clang::CXXBaseSpecifier>
8342 ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8343 AccessType access, bool is_virtual,
8344 bool base_of_class) {
8348 return llvm::make_unique<clang::CXXBaseSpecifier>(
8349 clang::SourceRange(), is_virtual, base_of_class,
8350 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8351 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8352 clang::SourceLocation());
8355 bool ClangASTContext::TransferBaseClasses(
8356 lldb::opaque_compiler_type_t type,
8357 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
8360 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8361 if (!cxx_record_decl)
8363 std::vector<clang::CXXBaseSpecifier *> raw_bases;
8364 raw_bases.reserve(bases.size());
8366 // Clang will make a copy of them, so it's ok that we pass pointers that we're
8367 // about to destroy.
8368 for (auto &b : bases)
8369 raw_bases.push_back(b.get());
8370 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
8374 bool ClangASTContext::SetObjCSuperClass(
8375 const CompilerType &type, const CompilerType &superclass_clang_type) {
8376 ClangASTContext *ast =
8377 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8380 clang::ASTContext *clang_ast = ast->getASTContext();
8382 if (type && superclass_clang_type.IsValid() &&
8383 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8384 clang::ObjCInterfaceDecl *class_interface_decl =
8385 GetAsObjCInterfaceDecl(type);
8386 clang::ObjCInterfaceDecl *super_interface_decl =
8387 GetAsObjCInterfaceDecl(superclass_clang_type);
8388 if (class_interface_decl && super_interface_decl) {
8389 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8390 clang_ast->getObjCInterfaceType(super_interface_decl)));
8397 bool ClangASTContext::AddObjCClassProperty(
8398 const CompilerType &type, const char *property_name,
8399 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8400 const char *property_setter_name, const char *property_getter_name,
8401 uint32_t property_attributes, ClangASTMetadata *metadata) {
8402 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8403 property_name[0] == '\0')
8405 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8408 clang::ASTContext *clang_ast = ast->getASTContext();
8410 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8412 if (class_interface_decl) {
8413 CompilerType property_clang_type_to_access;
8415 if (property_clang_type.IsValid())
8416 property_clang_type_to_access = property_clang_type;
8418 property_clang_type_to_access =
8419 CompilerType(clang_ast, ivar_decl->getType());
8421 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8422 clang::TypeSourceInfo *prop_type_source;
8425 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8427 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8428 ClangUtil::GetQualType(property_clang_type));
8430 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8431 *clang_ast, class_interface_decl,
8432 clang::SourceLocation(), // Source Location
8433 &clang_ast->Idents.get(property_name),
8434 clang::SourceLocation(), // Source Location for AT
8435 clang::SourceLocation(), // Source location for (
8436 ivar_decl ? ivar_decl->getType()
8437 : ClangUtil::GetQualType(property_clang_type),
8440 if (property_decl) {
8442 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8444 class_interface_decl->addDecl(property_decl);
8446 clang::Selector setter_sel, getter_sel;
8448 if (property_setter_name != nullptr) {
8449 std::string property_setter_no_colon(
8450 property_setter_name, strlen(property_setter_name) - 1);
8451 clang::IdentifierInfo *setter_ident =
8452 &clang_ast->Idents.get(property_setter_no_colon);
8453 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8454 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8455 std::string setter_sel_string("set");
8456 setter_sel_string.push_back(::toupper(property_name[0]));
8457 setter_sel_string.append(&property_name[1]);
8458 clang::IdentifierInfo *setter_ident =
8459 &clang_ast->Idents.get(setter_sel_string);
8460 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8462 property_decl->setSetterName(setter_sel);
8463 property_decl->setPropertyAttributes(
8464 clang::ObjCPropertyDecl::OBJC_PR_setter);
8466 if (property_getter_name != nullptr) {
8467 clang::IdentifierInfo *getter_ident =
8468 &clang_ast->Idents.get(property_getter_name);
8469 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8471 clang::IdentifierInfo *getter_ident =
8472 &clang_ast->Idents.get(property_name);
8473 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8475 property_decl->setGetterName(getter_sel);
8476 property_decl->setPropertyAttributes(
8477 clang::ObjCPropertyDecl::OBJC_PR_getter);
8480 property_decl->setPropertyIvarDecl(ivar_decl);
8482 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8483 property_decl->setPropertyAttributes(
8484 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8485 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8486 property_decl->setPropertyAttributes(
8487 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8488 if (property_attributes & DW_APPLE_PROPERTY_assign)
8489 property_decl->setPropertyAttributes(
8490 clang::ObjCPropertyDecl::OBJC_PR_assign);
8491 if (property_attributes & DW_APPLE_PROPERTY_retain)
8492 property_decl->setPropertyAttributes(
8493 clang::ObjCPropertyDecl::OBJC_PR_retain);
8494 if (property_attributes & DW_APPLE_PROPERTY_copy)
8495 property_decl->setPropertyAttributes(
8496 clang::ObjCPropertyDecl::OBJC_PR_copy);
8497 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8498 property_decl->setPropertyAttributes(
8499 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8500 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8501 property_decl->setPropertyAttributes(
8502 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8503 if (property_attributes &
8504 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8505 property_decl->setPropertyAttributes(
8506 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8507 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8508 property_decl->setPropertyAttributes(
8509 clang::ObjCPropertyDecl::OBJC_PR_class);
8511 const bool isInstance =
8512 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8514 if (!getter_sel.isNull() &&
8516 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8517 : class_interface_decl->lookupClassMethod(getter_sel))) {
8518 const bool isVariadic = false;
8519 const bool isSynthesized = false;
8520 const bool isImplicitlyDeclared = true;
8521 const bool isDefined = false;
8522 const clang::ObjCMethodDecl::ImplementationControl impControl =
8523 clang::ObjCMethodDecl::None;
8524 const bool HasRelatedResultType = false;
8526 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8527 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8528 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8529 nullptr, class_interface_decl, isInstance, isVariadic,
8530 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8531 HasRelatedResultType);
8533 if (getter && metadata)
8534 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8537 getter->setMethodParams(*clang_ast,
8538 llvm::ArrayRef<clang::ParmVarDecl *>(),
8539 llvm::ArrayRef<clang::SourceLocation>());
8541 class_interface_decl->addDecl(getter);
8545 if (!setter_sel.isNull() &&
8547 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8548 : class_interface_decl->lookupClassMethod(setter_sel))) {
8549 clang::QualType result_type = clang_ast->VoidTy;
8550 const bool isVariadic = false;
8551 const bool isSynthesized = false;
8552 const bool isImplicitlyDeclared = true;
8553 const bool isDefined = false;
8554 const clang::ObjCMethodDecl::ImplementationControl impControl =
8555 clang::ObjCMethodDecl::None;
8556 const bool HasRelatedResultType = false;
8558 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8559 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8560 setter_sel, result_type, nullptr, class_interface_decl,
8561 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8562 isDefined, impControl, HasRelatedResultType);
8564 if (setter && metadata)
8565 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8567 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8569 params.push_back(clang::ParmVarDecl::Create(
8570 *clang_ast, setter, clang::SourceLocation(),
8571 clang::SourceLocation(),
8572 nullptr, // anonymous
8573 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8574 clang::SC_Auto, nullptr));
8577 setter->setMethodParams(
8578 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8579 llvm::ArrayRef<clang::SourceLocation>());
8581 class_interface_decl->addDecl(setter);
8592 bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8593 bool check_superclass) {
8594 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8595 if (class_interface_decl)
8596 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8600 clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8601 const CompilerType &type,
8602 const char *name, // the full symbol name as seen in the symbol table
8603 // (lldb::opaque_compiler_type_t type, "-[NString
8604 // stringWithCString:]")
8605 const CompilerType &method_clang_type, lldb::AccessType access,
8606 bool is_artificial, bool is_variadic) {
8607 if (!type || !method_clang_type.IsValid())
8610 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8612 if (class_interface_decl == nullptr)
8614 ClangASTContext *lldb_ast =
8615 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8616 if (lldb_ast == nullptr)
8618 clang::ASTContext *ast = lldb_ast->getASTContext();
8620 const char *selector_start = ::strchr(name, ' ');
8621 if (selector_start == nullptr)
8625 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8629 // printf ("name = '%s'\n", name);
8631 unsigned num_selectors_with_args = 0;
8632 for (start = selector_start; start && *start != '\0' && *start != ']';
8634 len = ::strcspn(start, ":]");
8635 bool has_arg = (start[len] == ':');
8637 ++num_selectors_with_args;
8638 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8643 if (selector_idents.size() == 0)
8646 clang::Selector method_selector = ast->Selectors.getSelector(
8647 num_selectors_with_args ? selector_idents.size() : 0,
8648 selector_idents.data());
8650 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8652 // Populate the method decl with parameter decls
8653 const clang::Type *method_type(method_qual_type.getTypePtr());
8655 if (method_type == nullptr)
8658 const clang::FunctionProtoType *method_function_prototype(
8659 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8661 if (!method_function_prototype)
8664 bool is_synthesized = false;
8665 bool is_defined = false;
8666 clang::ObjCMethodDecl::ImplementationControl imp_control =
8667 clang::ObjCMethodDecl::None;
8669 const unsigned num_args = method_function_prototype->getNumParams();
8671 if (num_args != num_selectors_with_args)
8672 return nullptr; // some debug information is corrupt. We are not going to
8675 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8677 clang::SourceLocation(), // beginLoc,
8678 clang::SourceLocation(), // endLoc,
8679 method_selector, method_function_prototype->getReturnType(),
8680 nullptr, // TypeSourceInfo *ResultTInfo,
8681 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8682 ClangUtil::GetQualType(type)),
8683 name[0] == '-', is_variadic, is_synthesized,
8684 true, // is_implicitly_declared; we force this to true because we don't
8685 // have source locations
8686 is_defined, imp_control, false /*has_related_result_type*/);
8688 if (objc_method_decl == nullptr)
8692 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8694 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8695 params.push_back(clang::ParmVarDecl::Create(
8696 *ast, objc_method_decl, clang::SourceLocation(),
8697 clang::SourceLocation(),
8698 nullptr, // anonymous
8699 method_function_prototype->getParamType(param_index), nullptr,
8700 clang::SC_Auto, nullptr));
8703 objc_method_decl->setMethodParams(
8704 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8705 llvm::ArrayRef<clang::SourceLocation>());
8708 class_interface_decl->addDecl(objc_method_decl);
8710 #ifdef LLDB_CONFIGURATION_DEBUG
8711 VerifyDecl(objc_method_decl);
8714 return objc_method_decl;
8717 bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8718 if (ClangUtil::IsClangType(type))
8721 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
8723 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8724 switch (type_class) {
8725 case clang::Type::Record: {
8726 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8727 if (cxx_record_decl)
8728 return cxx_record_decl->hasExternalLexicalStorage() ||
8729 cxx_record_decl->hasExternalVisibleStorage();
8732 case clang::Type::Enum: {
8733 clang::EnumDecl *enum_decl =
8734 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8736 return enum_decl->hasExternalLexicalStorage() ||
8737 enum_decl->hasExternalVisibleStorage();
8740 case clang::Type::ObjCObject:
8741 case clang::Type::ObjCInterface: {
8742 const clang::ObjCObjectType *objc_class_type =
8743 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8744 assert(objc_class_type);
8745 if (objc_class_type) {
8746 clang::ObjCInterfaceDecl *class_interface_decl =
8747 objc_class_type->getInterface();
8749 if (class_interface_decl)
8750 return class_interface_decl->hasExternalLexicalStorage() ||
8751 class_interface_decl->hasExternalVisibleStorage();
8755 case clang::Type::Typedef:
8756 return GetHasExternalStorage(CompilerType(
8757 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8759 ->getUnderlyingType()
8760 .getAsOpaquePtr()));
8762 case clang::Type::Auto:
8763 return GetHasExternalStorage(CompilerType(
8764 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8766 .getAsOpaquePtr()));
8768 case clang::Type::Elaborated:
8769 return GetHasExternalStorage(CompilerType(
8770 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8772 .getAsOpaquePtr()));
8774 case clang::Type::Paren:
8775 return GetHasExternalStorage(CompilerType(
8776 type.GetTypeSystem(),
8777 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8785 bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8790 clang::QualType qual_type(GetCanonicalQualType(type));
8792 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8793 switch (type_class) {
8794 case clang::Type::Record: {
8795 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8796 if (cxx_record_decl) {
8797 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8798 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8803 case clang::Type::Enum: {
8804 clang::EnumDecl *enum_decl =
8805 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8807 enum_decl->setHasExternalLexicalStorage(has_extern);
8808 enum_decl->setHasExternalVisibleStorage(has_extern);
8813 case clang::Type::ObjCObject:
8814 case clang::Type::ObjCInterface: {
8815 const clang::ObjCObjectType *objc_class_type =
8816 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8817 assert(objc_class_type);
8818 if (objc_class_type) {
8819 clang::ObjCInterfaceDecl *class_interface_decl =
8820 objc_class_type->getInterface();
8822 if (class_interface_decl) {
8823 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8824 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8830 case clang::Type::Typedef:
8831 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8833 ->getUnderlyingType()
8837 case clang::Type::Auto:
8838 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8843 case clang::Type::Elaborated:
8844 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8849 case clang::Type::Paren:
8850 return SetHasExternalStorage(
8851 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8860 #pragma mark TagDecl
8862 bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8863 clang::QualType qual_type(ClangUtil::GetQualType(type));
8864 if (!qual_type.isNull()) {
8865 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8867 clang::TagDecl *tag_decl = tag_type->getDecl();
8869 tag_decl->startDefinition();
8874 const clang::ObjCObjectType *object_type =
8875 qual_type->getAs<clang::ObjCObjectType>();
8877 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8878 if (interface_decl) {
8879 interface_decl->startDefinition();
8887 bool ClangASTContext::CompleteTagDeclarationDefinition(
8888 const CompilerType &type) {
8889 clang::QualType qual_type(ClangUtil::GetQualType(type));
8890 if (!qual_type.isNull()) {
8891 // Make sure we use the same methodology as
8892 // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end
8893 // the definition. Previously we were calling
8894 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8896 clang::TagDecl *tag_decl = tag_type->getDecl();
8898 clang::CXXRecordDecl *cxx_record_decl =
8899 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8901 if (cxx_record_decl) {
8902 if (!cxx_record_decl->isCompleteDefinition())
8903 cxx_record_decl->completeDefinition();
8904 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8905 cxx_record_decl->setHasExternalLexicalStorage(false);
8906 cxx_record_decl->setHasExternalVisibleStorage(false);
8912 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8915 clang::EnumDecl *enum_decl = enutype->getDecl();
8918 if (!enum_decl->isCompleteDefinition()) {
8919 ClangASTContext *lldb_ast =
8920 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8921 if (lldb_ast == nullptr)
8923 clang::ASTContext *ast = lldb_ast->getASTContext();
8925 /// TODO This really needs to be fixed.
8927 QualType integer_type(enum_decl->getIntegerType());
8928 if (!integer_type.isNull()) {
8929 unsigned NumPositiveBits = 1;
8930 unsigned NumNegativeBits = 0;
8932 clang::QualType promotion_qual_type;
8933 // If the enum integer type is less than an integer in bit width,
8934 // then we must promote it to an integer size.
8935 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8936 ast->getTypeSize(ast->IntTy)) {
8937 if (enum_decl->getIntegerType()->isSignedIntegerType())
8938 promotion_qual_type = ast->IntTy;
8940 promotion_qual_type = ast->UnsignedIntTy;
8942 promotion_qual_type = enum_decl->getIntegerType();
8944 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8945 promotion_qual_type, NumPositiveBits,
8956 clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
8957 const CompilerType &enum_type, const Declaration &decl, const char *name,
8958 const llvm::APSInt &value) {
8960 if (!enum_type || ConstString(name).IsEmpty())
8963 lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this));
8965 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8966 enum_type.GetOpaqueQualType();
8968 if (!enum_opaque_compiler_type)
8971 clang::QualType enum_qual_type(
8972 GetCanonicalQualType(enum_opaque_compiler_type));
8974 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8979 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8984 clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create(
8985 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8986 name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier
8987 clang::QualType(enutype, 0), nullptr, value);
8989 if (!enumerator_decl)
8992 enutype->getDecl()->addDecl(enumerator_decl);
8994 #ifdef LLDB_CONFIGURATION_DEBUG
8995 VerifyDecl(enumerator_decl);
8998 return enumerator_decl;
9001 clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType(
9002 const CompilerType &enum_type, const Declaration &decl, const char *name,
9003 int64_t enum_value, uint32_t enum_value_bit_size) {
9004 CompilerType underlying_type =
9005 GetEnumerationIntegerType(enum_type.GetOpaqueQualType());
9006 bool is_signed = false;
9007 underlying_type.IsIntegerType(is_signed);
9009 llvm::APSInt value(enum_value_bit_size, is_signed);
9012 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
9016 ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
9017 clang::QualType enum_qual_type(GetCanonicalQualType(type));
9018 const clang::Type *clang_type = enum_qual_type.getTypePtr();
9020 const clang::EnumType *enutype =
9021 llvm::dyn_cast<clang::EnumType>(clang_type);
9023 clang::EnumDecl *enum_decl = enutype->getDecl();
9025 return CompilerType(getASTContext(), enum_decl->getIntegerType());
9028 return CompilerType();
9032 ClangASTContext::CreateMemberPointerType(const CompilerType &type,
9033 const CompilerType &pointee_type) {
9034 if (type && pointee_type.IsValid() &&
9035 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
9036 ClangASTContext *ast =
9037 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
9039 return CompilerType();
9040 return CompilerType(ast->getASTContext(),
9041 ast->getASTContext()->getMemberPointerType(
9042 ClangUtil::GetQualType(pointee_type),
9043 ClangUtil::GetQualType(type).getTypePtr()));
9045 return CompilerType();
9049 ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
9050 const char *s, uint8_t *dst,
9053 clang::QualType qual_type(GetCanonicalQualType(type));
9055 bool is_complex = false;
9056 if (IsFloatingPointType(type, count, is_complex)) {
9057 // TODO: handle complex and vector types
9061 llvm::StringRef s_sref(s);
9062 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
9065 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
9066 const uint64_t byte_size = bit_size / 8;
9067 if (dst_size >= byte_size) {
9068 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
9069 llvm::NextPowerOf2(byte_size) * 8);
9070 lldb_private::Status get_data_error;
9071 if (scalar.GetAsMemoryData(dst, byte_size,
9072 lldb_private::endian::InlHostByteOrder(),
9081 //----------------------------------------------------------------------
9083 //----------------------------------------------------------------------
9084 #define DEPTH_INCREMENT 2
9086 void ClangASTContext::Dump(Stream &s) {
9087 Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl());
9088 tu->dump(s.AsRawOstream());
9091 void ClangASTContext::DumpValue(
9092 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
9093 lldb::Format format, const DataExtractor &data,
9094 lldb::offset_t data_byte_offset, size_t data_byte_size,
9095 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
9096 bool show_summary, bool verbose, uint32_t depth) {
9100 clang::QualType qual_type(GetQualType(type));
9101 switch (qual_type->getTypeClass()) {
9102 case clang::Type::Record:
9103 if (GetCompleteType(type)) {
9104 const clang::RecordType *record_type =
9105 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9106 const clang::RecordDecl *record_decl = record_type->getDecl();
9107 assert(record_decl);
9108 uint32_t field_bit_offset = 0;
9109 uint32_t field_byte_offset = 0;
9110 const clang::ASTRecordLayout &record_layout =
9111 getASTContext()->getASTRecordLayout(record_decl);
9112 uint32_t child_idx = 0;
9114 const clang::CXXRecordDecl *cxx_record_decl =
9115 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9116 if (cxx_record_decl) {
9117 // We might have base classes to print out first
9118 clang::CXXRecordDecl::base_class_const_iterator base_class,
9120 for (base_class = cxx_record_decl->bases_begin(),
9121 base_class_end = cxx_record_decl->bases_end();
9122 base_class != base_class_end; ++base_class) {
9123 const clang::CXXRecordDecl *base_class_decl =
9124 llvm::cast<clang::CXXRecordDecl>(
9125 base_class->getType()->getAs<clang::RecordType>()->getDecl());
9127 // Skip empty base classes
9128 if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl))
9131 if (base_class->isVirtual())
9133 record_layout.getVBaseClassOffset(base_class_decl)
9137 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
9140 field_byte_offset = field_bit_offset / 8;
9141 assert(field_bit_offset % 8 == 0);
9147 clang::QualType base_class_qual_type = base_class->getType();
9148 std::string base_class_type_name(base_class_qual_type.getAsString());
9150 // Indent and print the base class type name
9151 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
9152 base_class_type_name);
9154 clang::TypeInfo base_class_type_info =
9155 getASTContext()->getTypeInfo(base_class_qual_type);
9157 // Dump the value of the member
9158 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
9159 base_clang_type.DumpValue(
9161 s, // Stream to dump to
9163 .GetFormat(), // The format with which to display the member
9164 data, // Data buffer containing all bytes for this type
9165 data_byte_offset + field_byte_offset, // Offset into "data" where
9166 // to grab value from
9167 base_class_type_info.Width / 8, // Size of this type in bytes
9168 0, // Bitfield bit size
9169 0, // Bitfield bit offset
9170 show_types, // Boolean indicating if we should show the variable
9172 show_summary, // Boolean indicating if we should show a summary
9173 // for the current type
9174 verbose, // Verbose output?
9175 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9181 uint32_t field_idx = 0;
9182 clang::RecordDecl::field_iterator field, field_end;
9183 for (field = record_decl->field_begin(),
9184 field_end = record_decl->field_end();
9185 field != field_end; ++field, ++field_idx, ++child_idx) {
9186 // Print the starting squiggly bracket (if this is the first member) or
9187 // comma (for member 2 and beyond) for the struct/union/class member.
9194 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
9196 clang::QualType field_type = field->getType();
9197 // Print the member type if requested
9198 // Figure out the type byte size (field_type_info.first) and alignment
9199 // (field_type_info.second) from the AST context.
9200 clang::TypeInfo field_type_info =
9201 getASTContext()->getTypeInfo(field_type);
9202 assert(field_idx < record_layout.getFieldCount());
9203 // Figure out the field offset within the current struct/union/class
9205 field_bit_offset = record_layout.getFieldOffset(field_idx);
9206 field_byte_offset = field_bit_offset / 8;
9207 uint32_t field_bitfield_bit_size = 0;
9208 uint32_t field_bitfield_bit_offset = 0;
9209 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
9210 field_bitfield_bit_size))
9211 field_bitfield_bit_offset = field_bit_offset % 8;
9214 std::string field_type_name(field_type.getAsString());
9215 if (field_bitfield_bit_size > 0)
9216 s->Printf("(%s:%u) ", field_type_name.c_str(),
9217 field_bitfield_bit_size);
9219 s->Printf("(%s) ", field_type_name.c_str());
9221 // Print the member name and equal sign
9222 s->Printf("%s = ", field->getNameAsString().c_str());
9224 // Dump the value of the member
9225 CompilerType field_clang_type(getASTContext(), field_type);
9226 field_clang_type.DumpValue(
9228 s, // Stream to dump to
9230 .GetFormat(), // The format with which to display the member
9231 data, // Data buffer containing all bytes for this type
9232 data_byte_offset + field_byte_offset, // Offset into "data" where to
9234 field_type_info.Width / 8, // Size of this type in bytes
9235 field_bitfield_bit_size, // Bitfield bit size
9236 field_bitfield_bit_offset, // Bitfield bit offset
9237 show_types, // Boolean indicating if we should show the variable
9239 show_summary, // Boolean indicating if we should show a summary for
9241 verbose, // Verbose output?
9242 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9246 // Indent the trailing squiggly bracket
9248 s->Printf("\n%*s}", depth, "");
9252 case clang::Type::Enum:
9253 if (GetCompleteType(type)) {
9254 const clang::EnumType *enutype =
9255 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9256 const clang::EnumDecl *enum_decl = enutype->getDecl();
9258 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9259 lldb::offset_t offset = data_byte_offset;
9260 const int64_t enum_value = data.GetMaxU64Bitfield(
9261 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
9262 for (enum_pos = enum_decl->enumerator_begin(),
9263 enum_end_pos = enum_decl->enumerator_end();
9264 enum_pos != enum_end_pos; ++enum_pos) {
9265 if (enum_pos->getInitVal() == enum_value) {
9266 s->Printf("%s", enum_pos->getNameAsString().c_str());
9270 // If we have gotten here we didn't get find the enumerator in the enum
9271 // decl, so just print the integer.
9272 s->Printf("%" PRIi64, enum_value);
9276 case clang::Type::ConstantArray: {
9277 const clang::ConstantArrayType *array =
9278 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9279 bool is_array_of_characters = false;
9280 clang::QualType element_qual_type = array->getElementType();
9282 const clang::Type *canonical_type =
9283 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9285 is_array_of_characters = canonical_type->isCharType();
9287 const uint64_t element_count = array->getSize().getLimitedValue();
9289 clang::TypeInfo field_type_info =
9290 getASTContext()->getTypeInfo(element_qual_type);
9292 uint32_t element_idx = 0;
9293 uint32_t element_offset = 0;
9294 uint64_t element_byte_size = field_type_info.Width / 8;
9295 uint32_t element_stride = element_byte_size;
9297 if (is_array_of_characters) {
9299 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9300 element_byte_size, element_count, UINT32_MAX,
9301 LLDB_INVALID_ADDRESS, 0, 0);
9305 CompilerType element_clang_type(getASTContext(), element_qual_type);
9306 lldb::Format element_format = element_clang_type.GetFormat();
9308 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9309 // Print the starting squiggly bracket (if this is the first member) or
9310 // comman (for member 2 and beyong) for the struct/union/class member.
9311 if (element_idx == 0)
9316 // Indent and print the index
9317 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9319 // Figure out the field offset within the current struct/union/class
9321 element_offset = element_idx * element_stride;
9323 // Dump the value of the member
9324 element_clang_type.DumpValue(
9326 s, // Stream to dump to
9327 element_format, // The format with which to display the element
9328 data, // Data buffer containing all bytes for this type
9330 element_offset, // Offset into "data" where to grab value from
9331 element_byte_size, // Size of this type in bytes
9332 0, // Bitfield bit size
9333 0, // Bitfield bit offset
9334 show_types, // Boolean indicating if we should show the variable
9336 show_summary, // Boolean indicating if we should show a summary for
9338 verbose, // Verbose output?
9339 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9343 // Indent the trailing squiggly bracket
9344 if (element_idx > 0)
9345 s->Printf("\n%*s}", depth, "");
9350 case clang::Type::Typedef: {
9351 clang::QualType typedef_qual_type =
9352 llvm::cast<clang::TypedefType>(qual_type)
9354 ->getUnderlyingType();
9356 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9357 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9358 clang::TypeInfo typedef_type_info =
9359 getASTContext()->getTypeInfo(typedef_qual_type);
9360 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9362 return typedef_clang_type.DumpValue(
9364 s, // Stream to dump to
9365 typedef_format, // The format with which to display the element
9366 data, // Data buffer containing all bytes for this type
9367 data_byte_offset, // Offset into "data" where to grab value from
9368 typedef_byte_size, // Size of this type in bytes
9369 bitfield_bit_size, // Bitfield bit size
9370 bitfield_bit_offset, // Bitfield bit offset
9371 show_types, // Boolean indicating if we should show the variable types
9372 show_summary, // Boolean indicating if we should show a summary for the
9374 verbose, // Verbose output?
9375 depth); // Scope depth for any types that have children
9378 case clang::Type::Auto: {
9379 clang::QualType elaborated_qual_type =
9380 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9381 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9382 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9383 clang::TypeInfo elaborated_type_info =
9384 getASTContext()->getTypeInfo(elaborated_qual_type);
9385 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9387 return elaborated_clang_type.DumpValue(
9389 s, // Stream to dump to
9390 elaborated_format, // The format with which to display the element
9391 data, // Data buffer containing all bytes for this type
9392 data_byte_offset, // Offset into "data" where to grab value from
9393 elaborated_byte_size, // Size of this type in bytes
9394 bitfield_bit_size, // Bitfield bit size
9395 bitfield_bit_offset, // Bitfield bit offset
9396 show_types, // Boolean indicating if we should show the variable types
9397 show_summary, // Boolean indicating if we should show a summary for the
9399 verbose, // Verbose output?
9400 depth); // Scope depth for any types that have children
9403 case clang::Type::Elaborated: {
9404 clang::QualType elaborated_qual_type =
9405 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9406 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9407 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9408 clang::TypeInfo elaborated_type_info =
9409 getASTContext()->getTypeInfo(elaborated_qual_type);
9410 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9412 return elaborated_clang_type.DumpValue(
9414 s, // Stream to dump to
9415 elaborated_format, // The format with which to display the element
9416 data, // Data buffer containing all bytes for this type
9417 data_byte_offset, // Offset into "data" where to grab value from
9418 elaborated_byte_size, // Size of this type in bytes
9419 bitfield_bit_size, // Bitfield bit size
9420 bitfield_bit_offset, // Bitfield bit offset
9421 show_types, // Boolean indicating if we should show the variable types
9422 show_summary, // Boolean indicating if we should show a summary for the
9424 verbose, // Verbose output?
9425 depth); // Scope depth for any types that have children
9428 case clang::Type::Paren: {
9429 clang::QualType desugar_qual_type =
9430 llvm::cast<clang::ParenType>(qual_type)->desugar();
9431 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9433 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9434 clang::TypeInfo desugar_type_info =
9435 getASTContext()->getTypeInfo(desugar_qual_type);
9436 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9438 return desugar_clang_type.DumpValue(
9440 s, // Stream to dump to
9441 desugar_format, // The format with which to display the element
9442 data, // Data buffer containing all bytes for this type
9443 data_byte_offset, // Offset into "data" where to grab value from
9444 desugar_byte_size, // Size of this type in bytes
9445 bitfield_bit_size, // Bitfield bit size
9446 bitfield_bit_offset, // Bitfield bit offset
9447 show_types, // Boolean indicating if we should show the variable types
9448 show_summary, // Boolean indicating if we should show a summary for the
9450 verbose, // Verbose output?
9451 depth); // Scope depth for any types that have children
9455 // We are down to a scalar type that we just need to display.
9456 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9457 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9458 bitfield_bit_offset);
9461 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9466 bool ClangASTContext::DumpTypeValue(
9467 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
9468 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9469 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
9470 ExecutionContextScope *exe_scope) {
9473 if (IsAggregateType(type)) {
9476 clang::QualType qual_type(GetQualType(type));
9478 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9479 switch (type_class) {
9480 case clang::Type::Typedef: {
9481 clang::QualType typedef_qual_type =
9482 llvm::cast<clang::TypedefType>(qual_type)
9484 ->getUnderlyingType();
9485 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9486 if (format == eFormatDefault)
9487 format = typedef_clang_type.GetFormat();
9488 clang::TypeInfo typedef_type_info =
9489 getASTContext()->getTypeInfo(typedef_qual_type);
9490 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9492 return typedef_clang_type.DumpTypeValue(
9494 format, // The format with which to display the element
9495 data, // Data buffer containing all bytes for this type
9496 byte_offset, // Offset into "data" where to grab value from
9497 typedef_byte_size, // Size of this type in bytes
9498 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9499 // treat as a bitfield
9500 bitfield_bit_offset, // Offset in bits of a bitfield value if
9501 // bitfield_bit_size != 0
9505 case clang::Type::Enum:
9506 // If our format is enum or default, show the enumeration value as its
9507 // enumeration string value, else just display it as requested.
9508 if ((format == eFormatEnum || format == eFormatDefault) &&
9509 GetCompleteType(type)) {
9510 const clang::EnumType *enutype =
9511 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9512 const clang::EnumDecl *enum_decl = enutype->getDecl();
9514 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9515 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9516 lldb::offset_t offset = byte_offset;
9518 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9519 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9520 for (enum_pos = enum_decl->enumerator_begin(),
9521 enum_end_pos = enum_decl->enumerator_end();
9522 enum_pos != enum_end_pos; ++enum_pos) {
9523 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
9524 s->PutCString(enum_pos->getNameAsString());
9528 // If we have gotten here we didn't get find the enumerator in the
9529 // enum decl, so just print the integer.
9530 s->Printf("%" PRIi64, enum_svalue);
9532 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9533 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9534 for (enum_pos = enum_decl->enumerator_begin(),
9535 enum_end_pos = enum_decl->enumerator_end();
9536 enum_pos != enum_end_pos; ++enum_pos) {
9537 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
9538 s->PutCString(enum_pos->getNameAsString());
9542 // If we have gotten here we didn't get find the enumerator in the
9543 // enum decl, so just print the integer.
9544 s->Printf("%" PRIu64, enum_uvalue);
9548 // format was not enum, just fall through and dump the value as
9553 // We are down to a scalar type that we just need to display.
9555 uint32_t item_count = 1;
9556 // A few formats, we might need to modify our size and count for
9558 // on how we are trying to display the value...
9561 case eFormatBoolean:
9563 case eFormatComplex:
9564 case eFormatCString: // NULL terminated C strings
9565 case eFormatDecimal:
9568 case eFormatHexUppercase:
9572 case eFormatUnsigned:
9573 case eFormatPointer:
9574 case eFormatVectorOfChar:
9575 case eFormatVectorOfSInt8:
9576 case eFormatVectorOfUInt8:
9577 case eFormatVectorOfSInt16:
9578 case eFormatVectorOfUInt16:
9579 case eFormatVectorOfSInt32:
9580 case eFormatVectorOfUInt32:
9581 case eFormatVectorOfSInt64:
9582 case eFormatVectorOfUInt64:
9583 case eFormatVectorOfFloat32:
9584 case eFormatVectorOfFloat64:
9585 case eFormatVectorOfUInt128:
9589 case eFormatCharPrintable:
9590 case eFormatCharArray:
9592 case eFormatBytesWithASCII:
9593 item_count = byte_size;
9597 case eFormatUnicode16:
9598 item_count = byte_size / 2;
9602 case eFormatUnicode32:
9603 item_count = byte_size / 4;
9607 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9608 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9609 bitfield_bit_size, bitfield_bit_offset,
9618 void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9619 ExecutionContext *exe_ctx, Stream *s,
9620 const lldb_private::DataExtractor &data,
9621 lldb::offset_t data_byte_offset,
9622 size_t data_byte_size) {
9623 uint32_t length = 0;
9624 if (IsCStringType(type, length)) {
9626 Process *process = exe_ctx->GetProcessPtr();
9628 lldb::offset_t offset = data_byte_offset;
9629 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9630 std::vector<uint8_t> buf;
9636 DataExtractor cstr_data(&buf.front(), buf.size(),
9637 process->GetByteOrder(), 4);
9640 size_t total_cstr_len = 0;
9642 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9643 buf.size(), error)) > 0) {
9644 const size_t len = strlen((const char *)&buf.front());
9647 if (total_cstr_len == 0)
9648 s->PutCString(" \"");
9649 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9650 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
9651 total_cstr_len += len;
9652 if (len < buf.size())
9654 pointer_address += total_cstr_len;
9656 if (total_cstr_len > 0)
9663 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9664 StreamFile s(stdout, false);
9665 DumpTypeDescription(type, &s);
9666 ClangASTMetadata *metadata =
9667 ClangASTContext::GetMetadata(getASTContext(), type);
9673 void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9676 clang::QualType qual_type(GetQualType(type));
9678 llvm::SmallVector<char, 1024> buf;
9679 llvm::raw_svector_ostream llvm_ostrm(buf);
9681 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9682 switch (type_class) {
9683 case clang::Type::ObjCObject:
9684 case clang::Type::ObjCInterface: {
9685 GetCompleteType(type);
9687 const clang::ObjCObjectType *objc_class_type =
9688 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9689 assert(objc_class_type);
9690 if (objc_class_type) {
9691 clang::ObjCInterfaceDecl *class_interface_decl =
9692 objc_class_type->getInterface();
9693 if (class_interface_decl) {
9694 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9695 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
9700 case clang::Type::Typedef: {
9701 const clang::TypedefType *typedef_type =
9702 qual_type->getAs<clang::TypedefType>();
9704 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9705 std::string clang_typedef_name(
9706 typedef_decl->getQualifiedNameAsString());
9707 if (!clang_typedef_name.empty()) {
9708 s->PutCString("typedef ");
9709 s->PutCString(clang_typedef_name);
9714 case clang::Type::Auto:
9715 CompilerType(getASTContext(),
9716 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9717 .DumpTypeDescription(s);
9720 case clang::Type::Elaborated:
9721 CompilerType(getASTContext(),
9722 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9723 .DumpTypeDescription(s);
9726 case clang::Type::Paren:
9727 CompilerType(getASTContext(),
9728 llvm::cast<clang::ParenType>(qual_type)->desugar())
9729 .DumpTypeDescription(s);
9732 case clang::Type::Record: {
9733 GetCompleteType(type);
9735 const clang::RecordType *record_type =
9736 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9737 const clang::RecordDecl *record_decl = record_type->getDecl();
9738 const clang::CXXRecordDecl *cxx_record_decl =
9739 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9741 if (cxx_record_decl)
9742 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9743 s->GetIndentLevel());
9745 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9746 s->GetIndentLevel());
9750 const clang::TagType *tag_type =
9751 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9753 clang::TagDecl *tag_decl = tag_type->getDecl();
9755 tag_decl->print(llvm_ostrm, 0);
9757 std::string clang_type_name(qual_type.getAsString());
9758 if (!clang_type_name.empty())
9759 s->PutCString(clang_type_name);
9764 if (buf.size() > 0) {
9765 s->Write(buf.data(), buf.size());
9770 void ClangASTContext::DumpTypeName(const CompilerType &type) {
9771 if (ClangUtil::IsClangType(type)) {
9772 clang::QualType qual_type(
9773 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9775 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9776 switch (type_class) {
9777 case clang::Type::Record: {
9778 const clang::CXXRecordDecl *cxx_record_decl =
9779 qual_type->getAsCXXRecordDecl();
9780 if (cxx_record_decl)
9781 printf("class %s", cxx_record_decl->getName().str().c_str());
9784 case clang::Type::Enum: {
9785 clang::EnumDecl *enum_decl =
9786 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9788 printf("enum %s", enum_decl->getName().str().c_str());
9792 case clang::Type::ObjCObject:
9793 case clang::Type::ObjCInterface: {
9794 const clang::ObjCObjectType *objc_class_type =
9795 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9796 if (objc_class_type) {
9797 clang::ObjCInterfaceDecl *class_interface_decl =
9798 objc_class_type->getInterface();
9799 // We currently can't complete objective C types through the newly
9800 // added ASTContext because it only supports TagDecl objects right
9802 if (class_interface_decl)
9803 printf("@class %s", class_interface_decl->getName().str().c_str());
9807 case clang::Type::Typedef:
9808 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9815 case clang::Type::Auto:
9817 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9818 llvm::cast<clang::AutoType>(qual_type)
9820 .getAsOpaquePtr()));
9822 case clang::Type::Elaborated:
9823 printf("elaborated ");
9824 return DumpTypeName(CompilerType(
9825 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9827 .getAsOpaquePtr()));
9829 case clang::Type::Paren:
9831 return DumpTypeName(CompilerType(
9832 type.GetTypeSystem(),
9833 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9836 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9842 clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9843 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9844 const char *parent_name, int tag_decl_kind,
9845 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9846 if (template_param_infos.IsValid()) {
9847 std::string template_basename(parent_name);
9848 template_basename.erase(template_basename.find('<'));
9850 return CreateClassTemplateDecl(decl_ctx, access_type,
9851 template_basename.c_str(), tag_decl_kind,
9852 template_param_infos);
9857 void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9858 ClangASTContext *ast = (ClangASTContext *)baton;
9859 SymbolFile *sym_file = ast->GetSymbolFile();
9861 CompilerType clang_type = GetTypeForDecl(decl);
9863 sym_file->CompleteType(clang_type);
9867 void ClangASTContext::CompleteObjCInterfaceDecl(
9868 void *baton, clang::ObjCInterfaceDecl *decl) {
9869 ClangASTContext *ast = (ClangASTContext *)baton;
9870 SymbolFile *sym_file = ast->GetSymbolFile();
9872 CompilerType clang_type = GetTypeForDecl(decl);
9874 sym_file->CompleteType(clang_type);
9878 DWARFASTParser *ClangASTContext::GetDWARFParser() {
9879 if (!m_dwarf_ast_parser_ap)
9880 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9881 return m_dwarf_ast_parser_ap.get();
9884 #ifdef LLDB_ENABLE_ALL
9885 PDBASTParser *ClangASTContext::GetPDBParser() {
9886 if (!m_pdb_ast_parser_ap)
9887 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9888 return m_pdb_ast_parser_ap.get();
9890 #endif // LLDB_ENABLE_ALL
9892 bool ClangASTContext::LayoutRecordType(
9893 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9894 uint64_t &alignment,
9895 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9896 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9898 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9900 ClangASTContext *ast = (ClangASTContext *)baton;
9901 lldb_private::ClangASTImporter *importer = nullptr;
9902 if (ast->m_dwarf_ast_parser_ap)
9903 importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter();
9904 #ifdef LLDB_ENABLE_ALL
9905 if (!importer && ast->m_pdb_ast_parser_ap)
9906 importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter();
9907 #endif // LLDB_ENABLE_ALL
9911 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9912 field_offsets, base_offsets, vbase_offsets);
9915 //----------------------------------------------------------------------
9916 // CompilerDecl override functions
9917 //----------------------------------------------------------------------
9919 ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9921 clang::NamedDecl *nd =
9922 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9924 return ConstString(nd->getDeclName().getAsString());
9926 return ConstString();
9929 ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9931 clang::NamedDecl *nd =
9932 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9933 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9934 clang::MangleContext *mc = getMangleContext();
9935 if (mc && mc->shouldMangleCXXName(nd)) {
9936 llvm::SmallVector<char, 1024> buf;
9937 llvm::raw_svector_ostream llvm_ostrm(buf);
9938 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9939 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9940 Ctor_Complete, llvm_ostrm);
9941 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9942 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9943 Dtor_Complete, llvm_ostrm);
9945 mc->mangleName(nd, llvm_ostrm);
9948 return ConstString(buf.data(), buf.size());
9952 return ConstString();
9955 CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9957 return CompilerDeclContext(this,
9958 ((clang::Decl *)opaque_decl)->getDeclContext());
9960 return CompilerDeclContext();
9963 CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9964 if (clang::FunctionDecl *func_decl =
9965 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9966 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9967 if (clang::ObjCMethodDecl *objc_method =
9968 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9969 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9971 return CompilerType();
9974 size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9975 if (clang::FunctionDecl *func_decl =
9976 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9977 return func_decl->param_size();
9978 if (clang::ObjCMethodDecl *objc_method =
9979 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9980 return objc_method->param_size();
9985 CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9987 if (clang::FunctionDecl *func_decl =
9988 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9989 if (idx < func_decl->param_size()) {
9990 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9992 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9994 } else if (clang::ObjCMethodDecl *objc_method =
9995 llvm::dyn_cast<clang::ObjCMethodDecl>(
9996 (clang::Decl *)opaque_decl)) {
9997 if (idx < objc_method->param_size())
9998 return CompilerType(
10000 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
10002 return CompilerType();
10005 //----------------------------------------------------------------------
10006 // CompilerDeclContext functions
10007 //----------------------------------------------------------------------
10009 std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
10010 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
10011 std::vector<CompilerDecl> found_decls;
10012 if (opaque_decl_ctx) {
10013 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
10014 std::set<DeclContext *> searched;
10015 std::multimap<DeclContext *, DeclContext *> search_queue;
10016 SymbolFile *symbol_file = GetSymbolFile();
10018 for (clang::DeclContext *decl_context = root_decl_ctx;
10019 decl_context != nullptr && found_decls.empty();
10020 decl_context = decl_context->getParent()) {
10021 search_queue.insert(std::make_pair(decl_context, decl_context));
10023 for (auto it = search_queue.find(decl_context); it != search_queue.end();
10025 if (!searched.insert(it->second).second)
10027 symbol_file->ParseDeclsForContext(
10028 CompilerDeclContext(this, it->second));
10030 for (clang::Decl *child : it->second->decls()) {
10031 if (clang::UsingDirectiveDecl *ud =
10032 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10033 if (ignore_using_decls)
10035 clang::DeclContext *from = ud->getCommonAncestor();
10036 if (searched.find(ud->getNominatedNamespace()) == searched.end())
10037 search_queue.insert(
10038 std::make_pair(from, ud->getNominatedNamespace()));
10039 } else if (clang::UsingDecl *ud =
10040 llvm::dyn_cast<clang::UsingDecl>(child)) {
10041 if (ignore_using_decls)
10043 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10044 clang::Decl *target = usd->getTargetDecl();
10045 if (clang::NamedDecl *nd =
10046 llvm::dyn_cast<clang::NamedDecl>(target)) {
10047 IdentifierInfo *ii = nd->getIdentifier();
10048 if (ii != nullptr &&
10049 ii->getName().equals(name.AsCString(nullptr)))
10050 found_decls.push_back(CompilerDecl(this, nd));
10053 } else if (clang::NamedDecl *nd =
10054 llvm::dyn_cast<clang::NamedDecl>(child)) {
10055 IdentifierInfo *ii = nd->getIdentifier();
10056 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
10057 found_decls.push_back(CompilerDecl(this, nd));
10063 return found_decls;
10066 // Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
10067 // and return the number of levels it took to find it, or
10068 // LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
10069 // declaration, its name and/or type, if set, will be used to check that the
10070 // decl found in the scope is a match.
10072 // The optional name is required by languages (like C++) to handle using
10073 // declarations like:
10082 // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
10083 // // LLDB_INVALID_DECL_LEVEL for 'goo'.
10086 // The optional type is useful in the case that there's a specific overload
10087 // that we're looking for that might otherwise be shadowed, like:
10095 // // CountDeclLevels returns 0 for { 'foo', void() },
10096 // // 1 for { 'foo', void(int) }, and
10097 // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
10100 // NOTE: Because file statics are at the TranslationUnit along with globals, a
10101 // function at file scope will return the same level as a function at global
10102 // scope. Ideally we'd like to treat the file scope as an additional scope just
10103 // below the global scope. More work needs to be done to recognise that, if
10104 // the decl we're trying to look up is static, we should compare its source
10105 // file with that of the current scope and return a lower number for it.
10106 uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
10107 clang::DeclContext *child_decl_ctx,
10108 ConstString *child_name,
10109 CompilerType *child_type) {
10110 if (frame_decl_ctx) {
10111 std::set<DeclContext *> searched;
10112 std::multimap<DeclContext *, DeclContext *> search_queue;
10113 SymbolFile *symbol_file = GetSymbolFile();
10115 // Get the lookup scope for the decl we're trying to find.
10116 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
10118 // Look for it in our scope's decl context and its parents.
10119 uint32_t level = 0;
10120 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
10121 decl_ctx = decl_ctx->getParent()) {
10122 if (!decl_ctx->isLookupContext())
10124 if (decl_ctx == parent_decl_ctx)
10127 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
10128 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
10130 if (searched.find(it->second) != searched.end())
10133 // Currently DWARF has one shared translation unit for all Decls at top
10134 // level, so this would erroneously find using statements anywhere. So
10135 // don't look at the top-level translation unit.
10136 // TODO fix this and add a testcase that depends on it.
10138 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
10141 searched.insert(it->second);
10142 symbol_file->ParseDeclsForContext(
10143 CompilerDeclContext(this, it->second));
10145 for (clang::Decl *child : it->second->decls()) {
10146 if (clang::UsingDirectiveDecl *ud =
10147 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
10148 clang::DeclContext *ns = ud->getNominatedNamespace();
10149 if (ns == parent_decl_ctx)
10152 clang::DeclContext *from = ud->getCommonAncestor();
10153 if (searched.find(ns) == searched.end())
10154 search_queue.insert(std::make_pair(from, ns));
10155 } else if (child_name) {
10156 if (clang::UsingDecl *ud =
10157 llvm::dyn_cast<clang::UsingDecl>(child)) {
10158 for (clang::UsingShadowDecl *usd : ud->shadows()) {
10159 clang::Decl *target = usd->getTargetDecl();
10160 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
10164 IdentifierInfo *ii = nd->getIdentifier();
10165 if (ii == nullptr ||
10166 !ii->getName().equals(child_name->AsCString(nullptr)))
10168 // Check types, if one was provided.
10170 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
10171 if (!AreTypesSame(clang_type, *child_type,
10172 /*ignore_qualifiers=*/true))
10185 return LLDB_INVALID_DECL_LEVEL;
10188 bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
10189 if (opaque_decl_ctx)
10190 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
10195 ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
10196 if (opaque_decl_ctx) {
10197 clang::NamedDecl *named_decl =
10198 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10200 return ConstString(named_decl->getName());
10202 return ConstString();
10206 ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
10207 if (opaque_decl_ctx) {
10208 clang::NamedDecl *named_decl =
10209 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
10211 return ConstString(
10212 llvm::StringRef(named_decl->getQualifiedNameAsString()));
10214 return ConstString();
10217 bool ClangASTContext::DeclContextIsClassMethod(
10218 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
10219 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
10220 if (opaque_decl_ctx) {
10221 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
10222 if (ObjCMethodDecl *objc_method =
10223 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
10224 if (is_instance_method_ptr)
10225 *is_instance_method_ptr = objc_method->isInstanceMethod();
10227 *language_ptr = eLanguageTypeObjC;
10228 if (language_object_name_ptr)
10229 language_object_name_ptr->SetCString("self");
10231 } else if (CXXMethodDecl *cxx_method =
10232 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
10233 if (is_instance_method_ptr)
10234 *is_instance_method_ptr = cxx_method->isInstance();
10236 *language_ptr = eLanguageTypeC_plus_plus;
10237 if (language_object_name_ptr)
10238 language_object_name_ptr->SetCString("this");
10240 } else if (clang::FunctionDecl *function_decl =
10241 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
10242 ClangASTMetadata *metadata =
10243 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
10244 if (metadata && metadata->HasObjectPtr()) {
10245 if (is_instance_method_ptr)
10246 *is_instance_method_ptr = true;
10248 *language_ptr = eLanguageTypeObjC;
10249 if (language_object_name_ptr)
10250 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
10258 clang::DeclContext *
10259 ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
10261 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
10266 ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
10268 return llvm::dyn_cast<clang::ObjCMethodDecl>(
10269 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10274 ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
10276 return llvm::dyn_cast<clang::CXXMethodDecl>(
10277 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10281 clang::FunctionDecl *
10282 ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10284 return llvm::dyn_cast<clang::FunctionDecl>(
10285 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10289 clang::NamespaceDecl *
10290 ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10292 return llvm::dyn_cast<clang::NamespaceDecl>(
10293 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10298 ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10299 const void *object) {
10300 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10302 return ClangASTContext::GetMetadata(ast, object);
10306 clang::ASTContext *
10307 ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10308 ClangASTContext *ast =
10309 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10311 return ast->getASTContext();
10315 ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10316 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10317 m_target_wp(target.shared_from_this()),
10318 m_persistent_variables(new ClangPersistentVariables) {}
10320 UserExpression *ClangASTContextForExpressions::GetUserExpression(
10321 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
10322 Expression::ResultType desired_type,
10323 const EvaluateExpressionOptions &options) {
10324 TargetSP target_sp = m_target_wp.lock();
10328 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
10329 desired_type, options);
10332 FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10333 const CompilerType &return_type, const Address &function_address,
10334 const ValueList &arg_value_list, const char *name) {
10335 TargetSP target_sp = m_target_wp.lock();
10339 Process *process = target_sp->GetProcessSP().get();
10343 return new ClangFunctionCaller(*process, return_type, function_address,
10344 arg_value_list, name);
10348 ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10349 const char *name) {
10350 TargetSP target_sp = m_target_wp.lock();
10354 return new ClangUtilityFunction(*target_sp.get(), text, name);
10357 PersistentExpressionState *
10358 ClangASTContextForExpressions::GetPersistentExpressionState() {
10359 return m_persistent_variables.get();
10362 clang::ExternalASTMerger &
10363 ClangASTContextForExpressions::GetMergerUnchecked() {
10364 lldbassert(m_scratch_ast_source_ap != nullptr);
10365 return m_scratch_ast_source_ap->GetMergerUnchecked();