LLDB mainline
TypeSystemClang.cpp
Go to the documentation of this file.
1//===-- TypeSystemClang.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "TypeSystemClang.h"
10
11#include "clang/AST/DeclBase.h"
12#include "clang/AST/ExprCXX.h"
13#include "llvm/Support/Casting.h"
14#include "llvm/Support/FormatAdapters.h"
15#include "llvm/Support/FormatVariadic.h"
16
17#include <mutex>
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "clang/AST/ASTContext.h"
23#include "clang/AST/ASTImporter.h"
24#include "clang/AST/Attr.h"
25#include "clang/AST/CXXInheritance.h"
26#include "clang/AST/DeclObjC.h"
27#include "clang/AST/DeclTemplate.h"
28#include "clang/AST/Mangle.h"
29#include "clang/AST/RecordLayout.h"
30#include "clang/AST/Type.h"
31#include "clang/AST/VTableBuilder.h"
32#include "clang/Basic/Builtins.h"
33#include "clang/Basic/Diagnostic.h"
34#include "clang/Basic/FileManager.h"
35#include "clang/Basic/FileSystemOptions.h"
36#include "clang/Basic/LangStandard.h"
37#include "clang/Basic/SourceManager.h"
38#include "clang/Basic/TargetInfo.h"
39#include "clang/Basic/TargetOptions.h"
40#include "clang/Frontend/FrontendOptions.h"
41#include "clang/Lex/HeaderSearch.h"
42#include "clang/Lex/HeaderSearchOptions.h"
43#include "clang/Lex/ModuleMap.h"
44#include "clang/Sema/Sema.h"
45
46#include "llvm/Support/Signals.h"
47#include "llvm/Support/Threading.h"
48
58#include "lldb/Core/Module.h"
66#include "lldb/Target/Process.h"
67#include "lldb/Target/Target.h"
70#include "lldb/Utility/Flags.h"
74#include "lldb/Utility/Scalar.h"
76
81
82#include <cstdio>
83
84#include <mutex>
85#include <optional>
86
87using namespace lldb;
88using namespace lldb_private;
89using namespace lldb_private::dwarf;
90using namespace lldb_private::plugin::dwarf;
91using namespace clang;
92using llvm::StringSwitch;
93
95
96namespace {
97static void VerifyDecl(clang::Decl *decl) {
98 assert(decl && "VerifyDecl called with nullptr?");
99#ifndef NDEBUG
100 // We don't care about the actual access value here but only want to trigger
101 // that Clang calls its internal Decl::AccessDeclContextCheck validation.
102 decl->getAccess();
103#endif
104}
105
106static inline bool
107TypeSystemClangSupportsLanguage(lldb::LanguageType language) {
108 return language == eLanguageTypeUnknown || // Clang is the default type system
113 // Use Clang for Rust until there is a proper language plugin for it
114 language == eLanguageTypeRust ||
115 // Use Clang for D until there is a proper language plugin for it
116 language == eLanguageTypeD ||
117 // Open Dylan compiler debug info is designed to be Clang-compatible
118 language == eLanguageTypeDylan;
119}
120
121// Checks whether m1 is an overload of m2 (as opposed to an override). This is
122// called by addOverridesForMethod to distinguish overrides (which share a
123// vtable entry) from overloads (which require distinct entries).
124bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
125 // FIXME: This should detect covariant return types, but currently doesn't.
126 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
127 "Methods should have the same AST context");
128 clang::ASTContext &context = m1->getASTContext();
129
130 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
131 context.getCanonicalType(m1->getType()));
132
133 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
134 context.getCanonicalType(m2->getType()));
135
136 auto compareArgTypes = [&context](const clang::QualType &m1p,
137 const clang::QualType &m2p) {
138 return context.hasSameType(m1p.getUnqualifiedType(),
139 m2p.getUnqualifiedType());
140 };
141
142 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
143 // as a fourth parameter to std::equal().
144 return (m1->getNumParams() != m2->getNumParams()) ||
145 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
146 m2Type->param_type_begin(), compareArgTypes);
147}
148
149// If decl is a virtual method, walk the base classes looking for methods that
150// decl overrides. This table of overridden methods is used by IRGen to
151// determine the vtable layout for decl's parent class.
152void addOverridesForMethod(clang::CXXMethodDecl *decl) {
153 if (!decl->isVirtual())
154 return;
155
156 clang::CXXBasePaths paths;
157 llvm::SmallVector<clang::NamedDecl *, 4> decls;
158
159 auto find_overridden_methods =
160 [&decls, decl](const clang::CXXBaseSpecifier *specifier,
161 clang::CXXBasePath &path) {
162 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
163 specifier->getType()->castAs<clang::RecordType>()->getDecl())) {
164
165 clang::DeclarationName name = decl->getDeclName();
166
167 // If this is a destructor, check whether the base class destructor is
168 // virtual.
169 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
170 if (auto *baseDtorDecl = base_record->getDestructor()) {
171 if (baseDtorDecl->isVirtual()) {
172 decls.push_back(baseDtorDecl);
173 return true;
174 } else
175 return false;
176 }
177
178 // Otherwise, search for name in the base class.
179 for (path.Decls = base_record->lookup(name).begin();
180 path.Decls != path.Decls.end(); ++path.Decls) {
181 if (auto *method_decl =
182 llvm::dyn_cast<clang::CXXMethodDecl>(*path.Decls))
183 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
184 decls.push_back(method_decl);
185 return true;
186 }
187 }
188 }
189
190 return false;
191 };
192
193 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
194 for (auto *overridden_decl : decls)
195 decl->addOverriddenMethod(
196 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
197 }
198}
199}
200
202 VTableContextBase &vtable_ctx,
203 ValueObject &valobj,
204 const ASTRecordLayout &record_layout) {
205 // Retrieve type info
206 CompilerType pointee_type;
207 CompilerType this_type(valobj.GetCompilerType());
208 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
209 if (!type_info)
211
212 // Check if it's a pointer or reference
213 bool ptr_or_ref = false;
214 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
215 ptr_or_ref = true;
216 type_info = pointee_type.GetTypeInfo();
217 }
218
219 // We process only C++ classes
220 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
221 if ((type_info & cpp_class) != cpp_class)
223
224 // Calculate offset to VTable pointer
225 lldb::offset_t vbtable_ptr_offset =
226 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
227 : 0;
228
229 if (ptr_or_ref) {
230 // We have a pointer / ref to object, so read
231 // VTable pointer from process memory
232
235
236 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
237 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
239
240 vbtable_ptr_addr += vbtable_ptr_offset;
241
242 Status err;
243 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
244 }
245
246 // We have an object already read from process memory,
247 // so just extract VTable pointer from it
248
249 DataExtractor data;
250 Status err;
251 auto size = valobj.GetData(data, err);
252 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
254
255 return data.GetAddress(&vbtable_ptr_offset);
256}
257
258static int64_t ReadVBaseOffsetFromVTable(Process &process,
259 VTableContextBase &vtable_ctx,
260 lldb::addr_t vtable_ptr,
261 const CXXRecordDecl *cxx_record_decl,
262 const CXXRecordDecl *base_class_decl) {
263 if (vtable_ctx.isMicrosoft()) {
264 clang::MicrosoftVTableContext &msoft_vtable_ctx =
265 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
266
267 // Get the index into the virtual base table. The
268 // index is the index in uint32_t from vbtable_ptr
269 const unsigned vbtable_index =
270 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
271 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
272 Status err;
273 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
274 err);
275 }
276
277 clang::ItaniumVTableContext &itanium_vtable_ctx =
278 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
279
280 clang::CharUnits base_offset_offset =
281 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
282 base_class_decl);
283 const lldb::addr_t base_offset_addr =
284 vtable_ptr + base_offset_offset.getQuantity();
285 const uint32_t base_offset_size = process.GetAddressByteSize();
286 Status err;
287 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
288 INT64_MAX, err);
289}
290
291static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
292 ValueObject &valobj,
293 const ASTRecordLayout &record_layout,
294 const CXXRecordDecl *cxx_record_decl,
295 const CXXRecordDecl *base_class_decl,
296 int32_t &bit_offset) {
298 Process *process = exe_ctx.GetProcessPtr();
299 if (!process)
300 return false;
301
302 lldb::addr_t vtable_ptr =
303 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
304 if (vtable_ptr == LLDB_INVALID_ADDRESS)
305 return false;
306
307 auto base_offset = ReadVBaseOffsetFromVTable(
308 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
309 if (base_offset == INT64_MAX)
310 return false;
311
312 bit_offset = base_offset * 8;
313
314 return true;
315}
316
319
321 static ClangASTMap *g_map_ptr = nullptr;
322 static llvm::once_flag g_once_flag;
323 llvm::call_once(g_once_flag, []() {
324 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
325 });
326 return *g_map_ptr;
327}
328
330 bool is_complete_objc_class)
331 : m_payload(owning_module.GetValue()) {
332 SetIsCompleteObjCClass(is_complete_objc_class);
333}
334
336 assert(id.GetValue() < ObjCClassBit);
337 bool is_complete = IsCompleteObjCClass();
338 m_payload = id.GetValue();
339 SetIsCompleteObjCClass(is_complete);
340}
341
342static void SetMemberOwningModule(clang::Decl *member,
343 const clang::Decl *parent) {
344 if (!member || !parent)
345 return;
346
347 OptionalClangModuleID id(parent->getOwningModuleID());
348 if (!id.HasValue())
349 return;
350
351 member->setFromASTFile();
352 member->setOwningModuleID(id.GetValue());
353 member->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
354 if (llvm::isa<clang::NamedDecl>(member))
355 if (auto *dc = llvm::dyn_cast<clang::DeclContext>(parent)) {
356 dc->setHasExternalVisibleStorage(true);
357 // This triggers ExternalASTSource::FindExternalVisibleDeclsByName() to be
358 // called when searching for members.
359 dc->setHasExternalLexicalStorage(true);
360 }
361}
362
364
365bool TypeSystemClang::IsOperator(llvm::StringRef name,
366 clang::OverloadedOperatorKind &op_kind) {
367 // All operators have to start with "operator".
368 if (!name.consume_front("operator"))
369 return false;
370
371 // Remember if there was a space after "operator". This is necessary to
372 // check for collisions with strangely named functions like "operatorint()".
373 bool space_after_operator = name.consume_front(" ");
374
375 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
376 .Case("+", clang::OO_Plus)
377 .Case("+=", clang::OO_PlusEqual)
378 .Case("++", clang::OO_PlusPlus)
379 .Case("-", clang::OO_Minus)
380 .Case("-=", clang::OO_MinusEqual)
381 .Case("--", clang::OO_MinusMinus)
382 .Case("->", clang::OO_Arrow)
383 .Case("->*", clang::OO_ArrowStar)
384 .Case("*", clang::OO_Star)
385 .Case("*=", clang::OO_StarEqual)
386 .Case("/", clang::OO_Slash)
387 .Case("/=", clang::OO_SlashEqual)
388 .Case("%", clang::OO_Percent)
389 .Case("%=", clang::OO_PercentEqual)
390 .Case("^", clang::OO_Caret)
391 .Case("^=", clang::OO_CaretEqual)
392 .Case("&", clang::OO_Amp)
393 .Case("&=", clang::OO_AmpEqual)
394 .Case("&&", clang::OO_AmpAmp)
395 .Case("|", clang::OO_Pipe)
396 .Case("|=", clang::OO_PipeEqual)
397 .Case("||", clang::OO_PipePipe)
398 .Case("~", clang::OO_Tilde)
399 .Case("!", clang::OO_Exclaim)
400 .Case("!=", clang::OO_ExclaimEqual)
401 .Case("=", clang::OO_Equal)
402 .Case("==", clang::OO_EqualEqual)
403 .Case("<", clang::OO_Less)
404 .Case("<=>", clang::OO_Spaceship)
405 .Case("<<", clang::OO_LessLess)
406 .Case("<<=", clang::OO_LessLessEqual)
407 .Case("<=", clang::OO_LessEqual)
408 .Case(">", clang::OO_Greater)
409 .Case(">>", clang::OO_GreaterGreater)
410 .Case(">>=", clang::OO_GreaterGreaterEqual)
411 .Case(">=", clang::OO_GreaterEqual)
412 .Case("()", clang::OO_Call)
413 .Case("[]", clang::OO_Subscript)
414 .Case(",", clang::OO_Comma)
415 .Default(clang::NUM_OVERLOADED_OPERATORS);
416
417 // We found a fitting operator, so we can exit now.
418 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
419 return true;
420
421 // After the "operator " or "operator" part is something unknown. This means
422 // it's either one of the named operators (new/delete), a conversion operator
423 // (e.g. operator bool) or a function which name starts with "operator"
424 // (e.g. void operatorbool).
425
426 // If it's a function that starts with operator it can't have a space after
427 // "operator" because identifiers can't contain spaces.
428 // E.g. "operator int" (conversion operator)
429 // vs. "operatorint" (function with colliding name).
430 if (!space_after_operator)
431 return false; // not an operator.
432
433 // Now the operator is either one of the named operators or a conversion
434 // operator.
435 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
436 .Case("new", clang::OO_New)
437 .Case("new[]", clang::OO_Array_New)
438 .Case("delete", clang::OO_Delete)
439 .Case("delete[]", clang::OO_Array_Delete)
440 // conversion operators hit this case.
441 .Default(clang::NUM_OVERLOADED_OPERATORS);
442
443 return true;
444}
445
446clang::AccessSpecifier
448 switch (access) {
449 default:
450 break;
451 case eAccessNone:
452 return AS_none;
453 case eAccessPublic:
454 return AS_public;
455 case eAccessPrivate:
456 return AS_private;
457 case eAccessProtected:
458 return AS_protected;
459 }
460 return AS_none;
461}
462
463static void ParseLangArgs(LangOptions &Opts, ArchSpec arch) {
464 // FIXME: Cleanup per-file based stuff.
465
466 std::vector<std::string> Includes;
467 LangOptions::setLangDefaults(Opts, clang::Language::ObjCXX, arch.GetTriple(),
468 Includes, clang::LangStandard::lang_gnucxx98);
469
470 Opts.setValueVisibilityMode(DefaultVisibility);
471
472 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
473 // specified, or -std is set to a conforming mode.
474 Opts.Trigraphs = !Opts.GNUMode;
475 Opts.CharIsSigned = arch.CharIsSignedByDefault();
476 Opts.OptimizeSize = 0;
477
478 // FIXME: Eliminate this dependency.
479 // unsigned Opt =
480 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
481 // Opts.Optimize = Opt != 0;
482 unsigned Opt = 0;
483
484 // This is the __NO_INLINE__ define, which just depends on things like the
485 // optimization level and -fno-inline, not actually whether the backend has
486 // inlining enabled.
487 //
488 // FIXME: This is affected by other options (-fno-inline).
489 Opts.NoInlineDefine = !Opt;
490
491 // This is needed to allocate the extra space for the owning module
492 // on each decl.
493 Opts.ModulesLocalVisibility = 1;
494}
495
497 llvm::Triple target_triple) {
498 m_display_name = name.str();
499 if (!target_triple.str().empty())
500 SetTargetTriple(target_triple.str());
501 // The caller didn't pass an ASTContext so create a new one for this
502 // TypeSystemClang.
504
505 LogCreation();
506}
507
508TypeSystemClang::TypeSystemClang(llvm::StringRef name,
509 ASTContext &existing_ctxt) {
510 m_display_name = name.str();
511 SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
512
513 m_ast_up.reset(&existing_ctxt);
514 GetASTMap().Insert(&existing_ctxt, this);
515
516 LogCreation();
517}
518
519// Destructor
521
523 lldb_private::Module *module,
524 Target *target) {
525 if (!TypeSystemClangSupportsLanguage(language))
526 return lldb::TypeSystemSP();
527 ArchSpec arch;
528 if (module)
529 arch = module->GetArchitecture();
530 else if (target)
531 arch = target->GetArchitecture();
532
533 if (!arch.IsValid())
534 return lldb::TypeSystemSP();
535
536 llvm::Triple triple = arch.GetTriple();
537 // LLVM wants this to be set to iOS or MacOSX; if we're working on
538 // a bare-boards type image, change the triple for llvm's benefit.
539 if (triple.getVendor() == llvm::Triple::Apple &&
540 triple.getOS() == llvm::Triple::UnknownOS) {
541 if (triple.getArch() == llvm::Triple::arm ||
542 triple.getArch() == llvm::Triple::aarch64 ||
543 triple.getArch() == llvm::Triple::aarch64_32 ||
544 triple.getArch() == llvm::Triple::thumb) {
545 triple.setOS(llvm::Triple::IOS);
546 } else {
547 triple.setOS(llvm::Triple::MacOSX);
548 }
549 }
550
551 if (module) {
552 std::string ast_name =
553 "ASTContext for '" + module->GetFileSpec().GetPath() + "'";
554 return std::make_shared<TypeSystemClang>(ast_name, triple);
555 } else if (target && target->IsValid())
556 return std::make_shared<ScratchTypeSystemClang>(*target, triple);
557 return lldb::TypeSystemSP();
558}
559
561 LanguageSet languages;
563 languages.Insert(lldb::eLanguageTypeC);
575 return languages;
576}
577
579 LanguageSet languages;
587 return languages;
588}
589
592 GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
594}
595
598}
599
601 assert(m_ast_up);
602 GetASTMap().Erase(m_ast_up.get());
603 if (!m_ast_owned)
604 m_ast_up.release();
605
606 m_builtins_up.reset();
607 m_selector_table_up.reset();
608 m_identifier_table_up.reset();
609 m_target_info_up.reset();
610 m_target_options_rp.reset();
612 m_source_manager_up.reset();
613 m_language_options_up.reset();
614}
615
617 // Ensure that the new sema actually belongs to our ASTContext.
618 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
619 m_sema = s;
620}
621
623 return m_target_triple.c_str();
624}
625
626void TypeSystemClang::SetTargetTriple(llvm::StringRef target_triple) {
627 m_target_triple = target_triple.str();
628}
629
631 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
632 ASTContext &ast = getASTContext();
633 ast.getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
634 ast.setExternalSource(ast_source_up);
635}
636
638 assert(m_ast_up);
639 return *m_ast_up;
640}
641
642class NullDiagnosticConsumer : public DiagnosticConsumer {
643public:
644 NullDiagnosticConsumer() { m_log = GetLog(LLDBLog::Expressions); }
645
646 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
647 const clang::Diagnostic &info) override {
648 if (m_log) {
649 llvm::SmallVector<char, 32> diag_str(10);
650 info.FormatDiagnostic(diag_str);
651 diag_str.push_back('\0');
652 LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
653 }
654 }
655
656 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
657 return new NullDiagnosticConsumer();
658 }
659
660private:
662};
663
665 assert(!m_ast_up);
666 m_ast_owned = true;
667
668 m_language_options_up = std::make_unique<LangOptions>();
670
672 std::make_unique<IdentifierTable>(*m_language_options_up, nullptr);
673 m_builtins_up = std::make_unique<Builtin::Context>();
674
675 m_selector_table_up = std::make_unique<SelectorTable>();
676
677 clang::FileSystemOptions file_system_options;
678 m_file_manager_up = std::make_unique<clang::FileManager>(
679 file_system_options, FileSystem::Instance().GetVirtualFileSystem());
680
681 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
683 std::make_unique<DiagnosticsEngine>(diag_id_sp, new DiagnosticOptions());
684
685 m_source_manager_up = std::make_unique<clang::SourceManager>(
687 m_ast_up = std::make_unique<ASTContext>(
689 *m_selector_table_up, *m_builtins_up, TU_Complete);
690
691 m_diagnostic_consumer_up = std::make_unique<NullDiagnosticConsumer>();
692 m_ast_up->getDiagnostics().setClient(m_diagnostic_consumer_up.get(), false);
693
694 // This can be NULL if we don't know anything about the architecture or if
695 // the target for an architecture isn't enabled in the llvm/clang that we
696 // built
697 TargetInfo *target_info = getTargetInfo();
698 if (target_info)
699 m_ast_up->InitBuiltinTypes(*target_info);
700
701 GetASTMap().Insert(m_ast_up.get(), this);
702
703 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
705 SetExternalSource(ast_source_up);
706}
707
709 TypeSystemClang *clang_ast = GetASTMap().Lookup(ast);
710 return clang_ast;
711}
712
713clang::MangleContext *TypeSystemClang::getMangleContext() {
714 if (m_mangle_ctx_up == nullptr)
715 m_mangle_ctx_up.reset(getASTContext().createMangleContext());
716 return m_mangle_ctx_up.get();
717}
718
719std::shared_ptr<clang::TargetOptions> &TypeSystemClang::getTargetOptions() {
720 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
721 m_target_options_rp = std::make_shared<clang::TargetOptions>();
722 if (m_target_options_rp != nullptr)
724 }
725 return m_target_options_rp;
726}
727
729 // target_triple should be something like "x86_64-apple-macosx"
730 if (m_target_info_up == nullptr && !m_target_triple.empty())
731 m_target_info_up.reset(TargetInfo::CreateTargetInfo(
732 getASTContext().getDiagnostics(), getTargetOptions()));
733 return m_target_info_up.get();
734}
735
736#pragma mark Basic Types
737
738static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
739 ASTContext &ast, QualType qual_type) {
740 uint64_t qual_type_bit_size = ast.getTypeSize(qual_type);
741 return qual_type_bit_size == bit_size;
742}
743
746 size_t bit_size) {
747 ASTContext &ast = getASTContext();
748 switch (encoding) {
749 case eEncodingInvalid:
750 if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
751 return GetType(ast.VoidPtrTy);
752 break;
753
754 case eEncodingUint:
755 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
756 return GetType(ast.UnsignedCharTy);
757 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
758 return GetType(ast.UnsignedShortTy);
759 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
760 return GetType(ast.UnsignedIntTy);
761 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
762 return GetType(ast.UnsignedLongTy);
763 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
764 return GetType(ast.UnsignedLongLongTy);
765 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
766 return GetType(ast.UnsignedInt128Ty);
767 break;
768
769 case eEncodingSint:
770 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
771 return GetType(ast.SignedCharTy);
772 if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
773 return GetType(ast.ShortTy);
774 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
775 return GetType(ast.IntTy);
776 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
777 return GetType(ast.LongTy);
778 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
779 return GetType(ast.LongLongTy);
780 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
781 return GetType(ast.Int128Ty);
782 break;
783
784 case eEncodingIEEE754:
785 if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
786 return GetType(ast.FloatTy);
787 if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
788 return GetType(ast.DoubleTy);
789 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
790 return GetType(ast.LongDoubleTy);
791 if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
792 return GetType(ast.HalfTy);
793 break;
794
795 case eEncodingVector:
796 // Sanity check that bit_size is a multiple of 8's.
797 if (bit_size && !(bit_size & 0x7u))
798 return GetType(ast.getExtVectorType(ast.UnsignedCharTy, bit_size / 8));
799 break;
800 }
801
802 return CompilerType();
803}
804
806 static const llvm::StringMap<lldb::BasicType> g_type_map = {
807 // "void"
808 {"void", eBasicTypeVoid},
809
810 // "char"
811 {"char", eBasicTypeChar},
812 {"signed char", eBasicTypeSignedChar},
813 {"unsigned char", eBasicTypeUnsignedChar},
814 {"wchar_t", eBasicTypeWChar},
815 {"signed wchar_t", eBasicTypeSignedWChar},
816 {"unsigned wchar_t", eBasicTypeUnsignedWChar},
817
818 // "short"
819 {"short", eBasicTypeShort},
820 {"short int", eBasicTypeShort},
821 {"unsigned short", eBasicTypeUnsignedShort},
822 {"unsigned short int", eBasicTypeUnsignedShort},
823
824 // "int"
825 {"int", eBasicTypeInt},
826 {"signed int", eBasicTypeInt},
827 {"unsigned int", eBasicTypeUnsignedInt},
828 {"unsigned", eBasicTypeUnsignedInt},
829
830 // "long"
831 {"long", eBasicTypeLong},
832 {"long int", eBasicTypeLong},
833 {"unsigned long", eBasicTypeUnsignedLong},
834 {"unsigned long int", eBasicTypeUnsignedLong},
835
836 // "long long"
837 {"long long", eBasicTypeLongLong},
838 {"long long int", eBasicTypeLongLong},
839 {"unsigned long long", eBasicTypeUnsignedLongLong},
840 {"unsigned long long int", eBasicTypeUnsignedLongLong},
841
842 // "int128"
843 {"__int128_t", eBasicTypeInt128},
844 {"__uint128_t", eBasicTypeUnsignedInt128},
845
846 // "bool"
847 {"bool", eBasicTypeBool},
848 {"_Bool", eBasicTypeBool},
849
850 // Miscellaneous
851 {"float", eBasicTypeFloat},
852 {"double", eBasicTypeDouble},
853 {"long double", eBasicTypeLongDouble},
854 {"id", eBasicTypeObjCID},
855 {"SEL", eBasicTypeObjCSel},
856 {"nullptr", eBasicTypeNullPtr},
857 };
858
859 auto iter = g_type_map.find(name);
860 if (iter == g_type_map.end())
861 return eBasicTypeInvalid;
862
863 return iter->second;
864}
865
867 if (m_pointer_byte_size == 0)
868 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
870 .GetByteSize(nullptr))
871 m_pointer_byte_size = *size;
872 return m_pointer_byte_size;
873}
874
876 clang::ASTContext &ast = getASTContext();
877
879 GetOpaqueCompilerType(&ast, basic_type);
880
881 if (clang_type)
882 return CompilerType(weak_from_this(), clang_type);
883 return CompilerType();
884}
885
887 llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
888 ASTContext &ast = getASTContext();
889
890 switch (dw_ate) {
891 default:
892 break;
893
894 case DW_ATE_address:
895 if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
896 return GetType(ast.VoidPtrTy);
897 break;
898
899 case DW_ATE_boolean:
900 if (QualTypeMatchesBitSize(bit_size, ast, ast.BoolTy))
901 return GetType(ast.BoolTy);
902 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
903 return GetType(ast.UnsignedCharTy);
904 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
905 return GetType(ast.UnsignedShortTy);
906 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
907 return GetType(ast.UnsignedIntTy);
908 break;
909
910 case DW_ATE_lo_user:
911 // This has been seen to mean DW_AT_complex_integer
912 if (type_name.contains("complex")) {
913 CompilerType complex_int_clang_type =
914 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
915 bit_size / 2);
916 return GetType(
917 ast.getComplexType(ClangUtil::GetQualType(complex_int_clang_type)));
918 }
919 break;
920
921 case DW_ATE_complex_float: {
922 CanQualType FloatComplexTy = ast.getComplexType(ast.FloatTy);
923 if (QualTypeMatchesBitSize(bit_size, ast, FloatComplexTy))
924 return GetType(FloatComplexTy);
925
926 CanQualType DoubleComplexTy = ast.getComplexType(ast.DoubleTy);
927 if (QualTypeMatchesBitSize(bit_size, ast, DoubleComplexTy))
928 return GetType(DoubleComplexTy);
929
930 CanQualType LongDoubleComplexTy = ast.getComplexType(ast.LongDoubleTy);
931 if (QualTypeMatchesBitSize(bit_size, ast, LongDoubleComplexTy))
932 return GetType(LongDoubleComplexTy);
933
934 CompilerType complex_float_clang_type =
935 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
936 bit_size / 2);
937 return GetType(
938 ast.getComplexType(ClangUtil::GetQualType(complex_float_clang_type)));
939 }
940
941 case DW_ATE_float:
942 if (type_name == "float" &&
943 QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
944 return GetType(ast.FloatTy);
945 if (type_name == "double" &&
946 QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
947 return GetType(ast.DoubleTy);
948 if (type_name == "long double" &&
949 QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
950 return GetType(ast.LongDoubleTy);
951 // Fall back to not requiring a name match
952 if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
953 return GetType(ast.FloatTy);
954 if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
955 return GetType(ast.DoubleTy);
956 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
957 return GetType(ast.LongDoubleTy);
958 if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
959 return GetType(ast.HalfTy);
960 break;
961
962 case DW_ATE_signed:
963 if (!type_name.empty()) {
964 if (type_name == "wchar_t" &&
965 QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy) &&
966 (getTargetInfo() &&
967 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
968 return GetType(ast.WCharTy);
969 if (type_name == "void" &&
970 QualTypeMatchesBitSize(bit_size, ast, ast.VoidTy))
971 return GetType(ast.VoidTy);
972 if (type_name.contains("long long") &&
973 QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
974 return GetType(ast.LongLongTy);
975 if (type_name.contains("long") &&
976 QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
977 return GetType(ast.LongTy);
978 if (type_name.contains("short") &&
979 QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
980 return GetType(ast.ShortTy);
981 if (type_name.contains("char")) {
982 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
983 return GetType(ast.CharTy);
984 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
985 return GetType(ast.SignedCharTy);
986 }
987 if (type_name.contains("int")) {
988 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
989 return GetType(ast.IntTy);
990 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
991 return GetType(ast.Int128Ty);
992 }
993 }
994 // We weren't able to match up a type name, just search by size
995 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
996 return GetType(ast.CharTy);
997 if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
998 return GetType(ast.ShortTy);
999 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
1000 return GetType(ast.IntTy);
1001 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
1002 return GetType(ast.LongTy);
1003 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
1004 return GetType(ast.LongLongTy);
1005 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
1006 return GetType(ast.Int128Ty);
1007 break;
1008
1009 case DW_ATE_signed_char:
1010 if (type_name == "char") {
1011 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1012 return GetType(ast.CharTy);
1013 }
1014 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
1015 return GetType(ast.SignedCharTy);
1016 break;
1017
1018 case DW_ATE_unsigned:
1019 if (!type_name.empty()) {
1020 if (type_name == "wchar_t") {
1021 if (QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy)) {
1022 if (!(getTargetInfo() &&
1023 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1024 return GetType(ast.WCharTy);
1025 }
1026 }
1027 if (type_name.contains("long long")) {
1028 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1029 return GetType(ast.UnsignedLongLongTy);
1030 } else if (type_name.contains("long")) {
1031 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1032 return GetType(ast.UnsignedLongTy);
1033 } else if (type_name.contains("short")) {
1034 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1035 return GetType(ast.UnsignedShortTy);
1036 } else if (type_name.contains("char")) {
1037 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1038 return GetType(ast.UnsignedCharTy);
1039 } else if (type_name.contains("int")) {
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1041 return GetType(ast.UnsignedIntTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1043 return GetType(ast.UnsignedInt128Ty);
1044 }
1045 }
1046 // We weren't able to match up a type name, just search by size
1047 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1048 return GetType(ast.UnsignedCharTy);
1049 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1050 return GetType(ast.UnsignedShortTy);
1051 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1052 return GetType(ast.UnsignedIntTy);
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1054 return GetType(ast.UnsignedLongTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1056 return GetType(ast.UnsignedLongLongTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1058 return GetType(ast.UnsignedInt128Ty);
1059 break;
1060
1061 case DW_ATE_unsigned_char:
1062 if (type_name == "char") {
1063 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1064 return GetType(ast.CharTy);
1065 }
1066 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1067 return GetType(ast.UnsignedCharTy);
1068 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1069 return GetType(ast.UnsignedShortTy);
1070 break;
1071
1072 case DW_ATE_imaginary_float:
1073 break;
1074
1075 case DW_ATE_UTF:
1076 switch (bit_size) {
1077 case 8:
1078 return GetType(ast.Char8Ty);
1079 case 16:
1080 return GetType(ast.Char16Ty);
1081 case 32:
1082 return GetType(ast.Char32Ty);
1083 default:
1084 if (!type_name.empty()) {
1085 if (type_name == "char16_t")
1086 return GetType(ast.Char16Ty);
1087 if (type_name == "char32_t")
1088 return GetType(ast.Char32Ty);
1089 if (type_name == "char8_t")
1090 return GetType(ast.Char8Ty);
1091 }
1092 }
1093 break;
1094 }
1095
1096 Log *log = GetLog(LLDBLog::Types);
1097 LLDB_LOG(log,
1098 "error: need to add support for DW_TAG_base_type '{0}' "
1099 "encoded with DW_ATE = {1:x}, bit_size = {2}",
1100 type_name, dw_ate, bit_size);
1101 return CompilerType();
1102}
1103
1105 ASTContext &ast = getASTContext();
1106 QualType char_type(ast.CharTy);
1107
1108 if (is_const)
1109 char_type.addConst();
1110
1111 return GetType(ast.getPointerType(char_type));
1112}
1113
1115 bool ignore_qualifiers) {
1116 auto ast = type1.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
1117 if (!ast || type1.GetTypeSystem() != type2.GetTypeSystem())
1118 return false;
1119
1120 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1121 return true;
1122
1123 QualType type1_qual = ClangUtil::GetQualType(type1);
1124 QualType type2_qual = ClangUtil::GetQualType(type2);
1125
1126 if (ignore_qualifiers) {
1127 type1_qual = type1_qual.getUnqualifiedType();
1128 type2_qual = type2_qual.getUnqualifiedType();
1129 }
1130
1131 return ast->getASTContext().hasSameType(type1_qual, type2_qual);
1132}
1133
1135 if (!opaque_decl)
1136 return CompilerType();
1137
1138 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1139 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1140 return GetTypeForDecl(named_decl);
1141 return CompilerType();
1142}
1143
1145 // Check that the DeclContext actually belongs to this ASTContext.
1146 assert(&ctx->getParentASTContext() == &getASTContext());
1147 return CompilerDeclContext(this, ctx);
1148}
1149
1151 if (clang::ObjCInterfaceDecl *interface_decl =
1152 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1153 return GetTypeForDecl(interface_decl);
1154 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1155 return GetTypeForDecl(tag_decl);
1156 if (clang::ValueDecl *value_decl = llvm::dyn_cast<clang::ValueDecl>(decl))
1157 return GetTypeForDecl(value_decl);
1158 return CompilerType();
1159}
1160
1162 return GetType(getASTContext().getTagDeclType(decl));
1163}
1164
1165CompilerType TypeSystemClang::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1166 return GetType(getASTContext().getObjCInterfaceType(decl));
1167}
1168
1169CompilerType TypeSystemClang::GetTypeForDecl(clang::ValueDecl *value_decl) {
1170 return GetType(value_decl->getType());
1171}
1172
1173#pragma mark Structure, Unions, Classes
1174
1176 OptionalClangModuleID owning_module) {
1177 if (!decl || !owning_module.HasValue())
1178 return;
1179
1180 decl->setFromASTFile();
1181 decl->setOwningModuleID(owning_module.GetValue());
1182 decl->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
1183}
1184
1187 OptionalClangModuleID parent,
1188 bool is_framework, bool is_explicit) {
1189 // Get the external AST source which holds the modules.
1190 auto *ast_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1191 getASTContext().getExternalSource());
1192 assert(ast_source && "external ast source was lost");
1193 if (!ast_source)
1194 return {};
1195
1196 // Lazily initialize the module map.
1197 if (!m_header_search_up) {
1198 auto HSOpts = std::make_shared<clang::HeaderSearchOptions>();
1199 m_header_search_up = std::make_unique<clang::HeaderSearch>(
1202 m_module_map_up = std::make_unique<clang::ModuleMap>(
1205 }
1206
1207 // Get or create the module context.
1208 bool created;
1209 clang::Module *module;
1210 auto parent_desc = ast_source->getSourceDescriptor(parent.GetValue());
1211 std::tie(module, created) = m_module_map_up->findOrCreateModule(
1212 name, parent_desc ? parent_desc->getModuleOrNull() : nullptr,
1213 is_framework, is_explicit);
1214 if (!created)
1215 return ast_source->GetIDForModule(module);
1216
1217 return ast_source->RegisterModule(module);
1218}
1219
1221 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1222 AccessType access_type, llvm::StringRef name, int kind,
1223 LanguageType language, std::optional<ClangASTMetadata> metadata,
1224 bool exports_symbols) {
1225 ASTContext &ast = getASTContext();
1226
1227 if (decl_ctx == nullptr)
1228 decl_ctx = ast.getTranslationUnitDecl();
1229
1230 if (language == eLanguageTypeObjC ||
1231 language == eLanguageTypeObjC_plus_plus) {
1232 bool isInternal = false;
1233 return CreateObjCClass(name, decl_ctx, owning_module, isInternal, metadata);
1234 }
1235
1236 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1237 // we will need to update this code. I was told to currently always use the
1238 // CXXRecordDecl class since we often don't know from debug information if
1239 // something is struct or a class, so we default to always use the more
1240 // complete definition just in case.
1241
1242 bool has_name = !name.empty();
1243 CXXRecordDecl *decl = CXXRecordDecl::CreateDeserialized(ast, GlobalDeclID());
1244 decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1245 decl->setDeclContext(decl_ctx);
1246 if (has_name)
1247 decl->setDeclName(&ast.Idents.get(name));
1248 SetOwningModule(decl, owning_module);
1249
1250 if (!has_name) {
1251 // In C++ a lambda is also represented as an unnamed class. This is
1252 // different from an *anonymous class* that the user wrote:
1253 //
1254 // struct A {
1255 // // anonymous class (GNU/MSVC extension)
1256 // struct {
1257 // int x;
1258 // };
1259 // // unnamed class within a class
1260 // struct {
1261 // int y;
1262 // } B;
1263 // };
1264 //
1265 // void f() {
1266 // // unammed class outside of a class
1267 // struct {
1268 // int z;
1269 // } C;
1270 // }
1271 //
1272 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1273 // requires the anonymous class be embedded within a class. So the new
1274 // heuristic verifies this condition.
1275 if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols)
1276 decl->setAnonymousStructOrUnion(true);
1277 }
1278
1279 if (metadata)
1280 SetMetadata(decl, *metadata);
1281
1282 if (access_type != eAccessNone)
1283 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1284
1285 if (decl_ctx)
1286 decl_ctx->addDecl(decl);
1287
1288 return GetType(ast.getTagDeclType(decl));
1289}
1290
1291namespace {
1292/// Returns true iff the given TemplateArgument should be represented as an
1293/// NonTypeTemplateParmDecl in the AST.
1294bool IsValueParam(const clang::TemplateArgument &argument) {
1295 return argument.getKind() == TemplateArgument::Integral;
1296}
1297
1298void AddAccessSpecifierDecl(clang::CXXRecordDecl *cxx_record_decl,
1299 ASTContext &ct,
1300 clang::AccessSpecifier previous_access,
1301 clang::AccessSpecifier access_specifier) {
1302 if (!cxx_record_decl->isClass() && !cxx_record_decl->isStruct())
1303 return;
1304 if (previous_access != access_specifier) {
1305 // For struct, don't add AS_public if it's the first AccessSpecDecl.
1306 // For class, don't add AS_private if it's the first AccessSpecDecl.
1307 if ((cxx_record_decl->isStruct() &&
1308 previous_access == clang::AccessSpecifier::AS_none &&
1309 access_specifier == clang::AccessSpecifier::AS_public) ||
1310 (cxx_record_decl->isClass() &&
1311 previous_access == clang::AccessSpecifier::AS_none &&
1312 access_specifier == clang::AccessSpecifier::AS_private)) {
1313 return;
1314 }
1315 cxx_record_decl->addDecl(
1316 AccessSpecDecl::Create(ct, access_specifier, cxx_record_decl,
1317 SourceLocation(), SourceLocation()));
1318 }
1319}
1320} // namespace
1321
1322static TemplateParameterList *CreateTemplateParameterList(
1323 ASTContext &ast,
1324 const TypeSystemClang::TemplateParameterInfos &template_param_infos,
1325 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1326 const bool parameter_pack = false;
1327 const bool is_typename = false;
1328 const unsigned depth = 0;
1329 const size_t num_template_params = template_param_infos.Size();
1330 DeclContext *const decl_context =
1331 ast.getTranslationUnitDecl(); // Is this the right decl context?,
1332
1333 auto const &args = template_param_infos.GetArgs();
1334 auto const &names = template_param_infos.GetNames();
1335 for (size_t i = 0; i < num_template_params; ++i) {
1336 const char *name = names[i];
1337
1338 IdentifierInfo *identifier_info = nullptr;
1339 if (name && name[0])
1340 identifier_info = &ast.Idents.get(name);
1341 TemplateArgument const &targ = args[i];
1342 if (IsValueParam(targ)) {
1343 QualType template_param_type = targ.getIntegralType();
1344 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1345 ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1346 identifier_info, template_param_type, parameter_pack,
1347 ast.getTrivialTypeSourceInfo(template_param_type)));
1348 } else {
1349 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1350 ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1351 identifier_info, is_typename, parameter_pack));
1352 }
1353 }
1354
1355 if (template_param_infos.hasParameterPack()) {
1356 IdentifierInfo *identifier_info = nullptr;
1357 if (template_param_infos.HasPackName())
1358 identifier_info = &ast.Idents.get(template_param_infos.GetPackName());
1359 const bool parameter_pack_true = true;
1360
1361 if (!template_param_infos.GetParameterPack().IsEmpty() &&
1362 IsValueParam(template_param_infos.GetParameterPack().Front())) {
1363 QualType template_param_type =
1364 template_param_infos.GetParameterPack().Front().getIntegralType();
1365 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1366 ast, decl_context, SourceLocation(), SourceLocation(), depth,
1367 num_template_params, identifier_info, template_param_type,
1368 parameter_pack_true,
1369 ast.getTrivialTypeSourceInfo(template_param_type)));
1370 } else {
1371 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1372 ast, decl_context, SourceLocation(), SourceLocation(), depth,
1373 num_template_params, identifier_info, is_typename,
1374 parameter_pack_true));
1375 }
1376 }
1377 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1378 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1379 ast, SourceLocation(), SourceLocation(), template_param_decls,
1380 SourceLocation(), requires_clause);
1381 return template_param_list;
1382}
1383
1385 const TemplateParameterInfos &template_param_infos) {
1386 llvm::SmallVector<NamedDecl *, 8> ignore;
1387 clang::TemplateParameterList *template_param_list =
1388 CreateTemplateParameterList(getASTContext(), template_param_infos,
1389 ignore);
1390 llvm::SmallVector<clang::TemplateArgument, 2> args(
1391 template_param_infos.GetArgs());
1392 if (template_param_infos.hasParameterPack()) {
1393 llvm::ArrayRef<TemplateArgument> pack_args =
1394 template_param_infos.GetParameterPackArgs();
1395 args.append(pack_args.begin(), pack_args.end());
1396 }
1397 std::string str;
1398 llvm::raw_string_ostream os(str);
1399 clang::printTemplateArgumentList(os, args, GetTypePrintingPolicy(),
1400 template_param_list);
1401 return str;
1402}
1403
1405 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1406 clang::FunctionDecl *func_decl,
1407 const TemplateParameterInfos &template_param_infos) {
1408 // /// Create a function template node.
1409 ASTContext &ast = getASTContext();
1410
1411 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1412 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1413 ast, template_param_infos, template_param_decls);
1414 FunctionTemplateDecl *func_tmpl_decl =
1415 FunctionTemplateDecl::CreateDeserialized(ast, GlobalDeclID());
1416 func_tmpl_decl->setDeclContext(decl_ctx);
1417 func_tmpl_decl->setLocation(func_decl->getLocation());
1418 func_tmpl_decl->setDeclName(func_decl->getDeclName());
1419 func_tmpl_decl->setTemplateParameters(template_param_list);
1420 func_tmpl_decl->init(func_decl);
1421 SetOwningModule(func_tmpl_decl, owning_module);
1422
1423 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1424 i < template_param_decl_count; ++i) {
1425 // TODO: verify which decl context we should put template_param_decls into..
1426 template_param_decls[i]->setDeclContext(func_decl);
1427 }
1428 // Function templates inside a record need to have an access specifier.
1429 // It doesn't matter what access specifier we give the template as LLDB
1430 // anyway allows accessing everything inside a record.
1431 if (decl_ctx->isRecord())
1432 func_tmpl_decl->setAccess(clang::AccessSpecifier::AS_public);
1433
1434 return func_tmpl_decl;
1435}
1436
1438 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1439 const TemplateParameterInfos &infos) {
1440 TemplateArgumentList *template_args_ptr = TemplateArgumentList::CreateCopy(
1441 func_decl->getASTContext(), infos.GetArgs());
1442
1443 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1444 template_args_ptr, nullptr);
1445}
1446
1447/// Returns true if the given template parameter can represent the given value.
1448/// For example, `typename T` can represent `int` but not integral values such
1449/// as `int I = 3`.
1450static bool TemplateParameterAllowsValue(NamedDecl *param,
1451 const TemplateArgument &value) {
1452 if (llvm::isa<TemplateTypeParmDecl>(param)) {
1453 // Compare the argument kind, i.e. ensure that <typename> != <int>.
1454 if (value.getKind() != TemplateArgument::Type)
1455 return false;
1456 } else if (auto *type_param =
1457 llvm::dyn_cast<NonTypeTemplateParmDecl>(param)) {
1458 // Compare the argument kind, i.e. ensure that <typename> != <int>.
1459 if (!IsValueParam(value))
1460 return false;
1461 // Compare the integral type, i.e. ensure that <int> != <char>.
1462 if (type_param->getType() != value.getIntegralType())
1463 return false;
1464 } else {
1465 // There is no way to create other parameter decls at the moment, so we
1466 // can't reach this case during normal LLDB usage. Log that this happened
1467 // and assert.
1469 LLDB_LOG(log,
1470 "Don't know how to compare template parameter to passed"
1471 " value. Decl kind of parameter is: {0}",
1472 param->getDeclKindName());
1473 lldbassert(false && "Can't compare this TemplateParmDecl subclass");
1474 // In release builds just fall back to marking the parameter as not
1475 // accepting the value so that we don't try to fit an instantiation to a
1476 // template that doesn't fit. E.g., avoid that `S<1>` is being connected to
1477 // `template<typename T> struct S;`.
1478 return false;
1479 }
1480 return true;
1481}
1482
1483/// Returns true if the given class template declaration could produce an
1484/// instantiation with the specified values.
1485/// For example, `<typename T>` allows the arguments `float`, but not for
1486/// example `bool, float` or `3` (as an integer parameter value).
1488 ClassTemplateDecl *class_template_decl,
1489 const TypeSystemClang::TemplateParameterInfos &instantiation_values) {
1490
1491 TemplateParameterList &params = *class_template_decl->getTemplateParameters();
1492
1493 // Save some work by iterating only once over the found parameters and
1494 // calculate the information related to parameter packs.
1495
1496 // Contains the first pack parameter (or non if there are none).
1497 std::optional<NamedDecl *> pack_parameter;
1498 // Contains the number of non-pack parameters.
1499 size_t non_pack_params = params.size();
1500 for (size_t i = 0; i < params.size(); ++i) {
1501 NamedDecl *param = params.getParam(i);
1502 if (param->isParameterPack()) {
1503 pack_parameter = param;
1504 non_pack_params = i;
1505 break;
1506 }
1507 }
1508
1509 // The found template needs to have compatible non-pack template arguments.
1510 // E.g., ensure that <typename, typename> != <typename>.
1511 // The pack parameters are compared later.
1512 if (non_pack_params != instantiation_values.Size())
1513 return false;
1514
1515 // Ensure that <typename...> != <typename>.
1516 if (pack_parameter.has_value() != instantiation_values.hasParameterPack())
1517 return false;
1518
1519 // Compare the first pack parameter that was found with the first pack
1520 // parameter value. The special case of having an empty parameter pack value
1521 // always fits to a pack parameter.
1522 // E.g., ensure that <int...> != <typename...>.
1523 if (pack_parameter && !instantiation_values.GetParameterPack().IsEmpty() &&
1525 *pack_parameter, instantiation_values.GetParameterPack().Front()))
1526 return false;
1527
1528 // Compare all the non-pack parameters now.
1529 // E.g., ensure that <int> != <long>.
1530 for (const auto pair :
1531 llvm::zip_first(instantiation_values.GetArgs(), params)) {
1532 const TemplateArgument &passed_arg = std::get<0>(pair);
1533 NamedDecl *found_param = std::get<1>(pair);
1534 if (!TemplateParameterAllowsValue(found_param, passed_arg))
1535 return false;
1536 }
1537
1538 return class_template_decl;
1539}
1540
1542 DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1543 lldb::AccessType access_type, llvm::StringRef class_name, int kind,
1544 const TemplateParameterInfos &template_param_infos) {
1545 ASTContext &ast = getASTContext();
1546
1547 ClassTemplateDecl *class_template_decl = nullptr;
1548 if (decl_ctx == nullptr)
1549 decl_ctx = ast.getTranslationUnitDecl();
1550
1551 IdentifierInfo &identifier_info = ast.Idents.get(class_name);
1552 DeclarationName decl_name(&identifier_info);
1553
1554 // Search the AST for an existing ClassTemplateDecl that could be reused.
1555 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1556 for (NamedDecl *decl : result) {
1557 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1558 if (!class_template_decl)
1559 continue;
1560 // The class template has to be able to represents the instantiation
1561 // values we received. Without this we might end up putting an instantiation
1562 // with arguments such as <int, int> to a template such as:
1563 // template<typename T> struct S;
1564 // Connecting the instantiation to an incompatible template could cause
1565 // problems later on.
1566 if (!ClassTemplateAllowsToInstantiationArgs(class_template_decl,
1567 template_param_infos))
1568 continue;
1569 return class_template_decl;
1570 }
1571
1572 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1573
1574 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1575 ast, template_param_infos, template_param_decls);
1576
1577 CXXRecordDecl *template_cxx_decl =
1578 CXXRecordDecl::CreateDeserialized(ast, GlobalDeclID());
1579 template_cxx_decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1580 // What decl context do we use here? TU? The actual decl context?
1581 template_cxx_decl->setDeclContext(decl_ctx);
1582 template_cxx_decl->setDeclName(decl_name);
1583 SetOwningModule(template_cxx_decl, owning_module);
1584
1585 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1586 i < template_param_decl_count; ++i) {
1587 template_param_decls[i]->setDeclContext(template_cxx_decl);
1588 }
1589
1590 // With templated classes, we say that a class is templated with
1591 // specializations, but that the bare class has no functions.
1592 // template_cxx_decl->startDefinition();
1593 // template_cxx_decl->completeDefinition();
1594
1595 class_template_decl =
1596 ClassTemplateDecl::CreateDeserialized(ast, GlobalDeclID());
1597 // What decl context do we use here? TU? The actual decl context?
1598 class_template_decl->setDeclContext(decl_ctx);
1599 class_template_decl->setDeclName(decl_name);
1600 class_template_decl->setTemplateParameters(template_param_list);
1601 class_template_decl->init(template_cxx_decl);
1602 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
1603 SetOwningModule(class_template_decl, owning_module);
1604
1605 if (access_type != eAccessNone)
1606 class_template_decl->setAccess(
1608
1609 decl_ctx->addDecl(class_template_decl);
1610
1611 VerifyDecl(class_template_decl);
1612
1613 return class_template_decl;
1614}
1615
1616TemplateTemplateParmDecl *
1618 ASTContext &ast = getASTContext();
1619
1620 auto *decl_ctx = ast.getTranslationUnitDecl();
1621
1622 IdentifierInfo &identifier_info = ast.Idents.get(template_name);
1623 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1624
1625 TypeSystemClang::TemplateParameterInfos template_param_infos;
1626 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1627 ast, template_param_infos, template_param_decls);
1628
1629 // LLDB needs to create those decls only to be able to display a
1630 // type that includes a template template argument. Only the name matters for
1631 // this purpose, so we use dummy values for the other characteristics of the
1632 // type.
1633 return TemplateTemplateParmDecl::Create(ast, decl_ctx, SourceLocation(),
1634 /*Depth=*/0, /*Position=*/0,
1635 /*IsParameterPack=*/false,
1636 &identifier_info, /*Typename=*/false,
1637 template_param_list);
1638}
1639
1640ClassTemplateSpecializationDecl *
1642 DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1643 ClassTemplateDecl *class_template_decl, int kind,
1644 const TemplateParameterInfos &template_param_infos) {
1645 ASTContext &ast = getASTContext();
1646 llvm::SmallVector<clang::TemplateArgument, 2> args(
1647 template_param_infos.Size() +
1648 (template_param_infos.hasParameterPack() ? 1 : 0));
1649
1650 auto const &orig_args = template_param_infos.GetArgs();
1651 std::copy(orig_args.begin(), orig_args.end(), args.begin());
1652 if (template_param_infos.hasParameterPack()) {
1653 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1654 ast, template_param_infos.GetParameterPackArgs());
1655 }
1656 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1657 ClassTemplateSpecializationDecl::CreateDeserialized(ast, GlobalDeclID());
1658 class_template_specialization_decl->setTagKind(
1659 static_cast<TagDecl::TagKind>(kind));
1660 class_template_specialization_decl->setDeclContext(decl_ctx);
1661 class_template_specialization_decl->setInstantiationOf(class_template_decl);
1662 class_template_specialization_decl->setTemplateArgs(
1663 TemplateArgumentList::CreateCopy(ast, args));
1664 ast.getTypeDeclType(class_template_specialization_decl, nullptr);
1665 class_template_specialization_decl->setDeclName(
1666 class_template_decl->getDeclName());
1667 SetOwningModule(class_template_specialization_decl, owning_module);
1668 decl_ctx->addDecl(class_template_specialization_decl);
1669
1670 class_template_specialization_decl->setSpecializationKind(
1671 TSK_ExplicitSpecialization);
1672
1673 return class_template_specialization_decl;
1674}
1675
1677 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1678 if (class_template_specialization_decl) {
1679 ASTContext &ast = getASTContext();
1680 return GetType(ast.getTagDeclType(class_template_specialization_decl));
1681 }
1682 return CompilerType();
1683}
1684
1685static inline bool check_op_param(bool is_method,
1686 clang::OverloadedOperatorKind op_kind,
1687 bool unary, bool binary,
1688 uint32_t num_params) {
1689 // Special-case call since it can take any number of operands
1690 if (op_kind == OO_Call)
1691 return true;
1692
1693 // The parameter count doesn't include "this"
1694 if (is_method)
1695 ++num_params;
1696 if (num_params == 1)
1697 return unary;
1698 if (num_params == 2)
1699 return binary;
1700 else
1701 return false;
1702}
1703
1705 bool is_method, clang::OverloadedOperatorKind op_kind,
1706 uint32_t num_params) {
1707 switch (op_kind) {
1708 default:
1709 break;
1710 // C++ standard allows any number of arguments to new/delete
1711 case OO_New:
1712 case OO_Array_New:
1713 case OO_Delete:
1714 case OO_Array_Delete:
1715 return true;
1716 }
1717
1718#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1719 case OO_##Name: \
1720 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1721 switch (op_kind) {
1722#include "clang/Basic/OperatorKinds.def"
1723 default:
1724 break;
1725 }
1726 return false;
1727}
1728
1729clang::AccessSpecifier
1731 clang::AccessSpecifier rhs) {
1732 // Make the access equal to the stricter of the field and the nested field's
1733 // access
1734 if (lhs == AS_none || rhs == AS_none)
1735 return AS_none;
1736 if (lhs == AS_private || rhs == AS_private)
1737 return AS_private;
1738 if (lhs == AS_protected || rhs == AS_protected)
1739 return AS_protected;
1740 return AS_public;
1741}
1742
1744 uint32_t &bitfield_bit_size) {
1745 ASTContext &ast = getASTContext();
1746 if (field == nullptr)
1747 return false;
1748
1749 if (field->isBitField()) {
1750 Expr *bit_width_expr = field->getBitWidth();
1751 if (bit_width_expr) {
1752 if (std::optional<llvm::APSInt> bit_width_apsint =
1753 bit_width_expr->getIntegerConstantExpr(ast)) {
1754 bitfield_bit_size = bit_width_apsint->getLimitedValue(UINT32_MAX);
1755 return true;
1756 }
1757 }
1758 }
1759 return false;
1760}
1761
1762bool TypeSystemClang::RecordHasFields(const RecordDecl *record_decl) {
1763 if (record_decl == nullptr)
1764 return false;
1765
1766 if (!record_decl->field_empty())
1767 return true;
1768
1769 // No fields, lets check this is a CXX record and check the base classes
1770 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1771 if (cxx_record_decl) {
1772 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1773 for (base_class = cxx_record_decl->bases_begin(),
1774 base_class_end = cxx_record_decl->bases_end();
1775 base_class != base_class_end; ++base_class) {
1776 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1777 base_class->getType()->getAs<RecordType>()->getDecl());
1778 if (RecordHasFields(base_class_decl))
1779 return true;
1780 }
1781 }
1782
1783 // We always want forcefully completed types to show up so we can print a
1784 // message in the summary that indicates that the type is incomplete.
1785 // This will help users know when they are running into issues with
1786 // -flimit-debug-info instead of just seeing nothing if this is a base class
1787 // (since we were hiding empty base classes), or nothing when you turn open
1788 // an valiable whose type was incomplete.
1789 if (std::optional<ClangASTMetadata> meta_data = GetMetadata(record_decl);
1790 meta_data && meta_data->IsForcefullyCompleted())
1791 return true;
1792
1793 return false;
1794}
1795
1796#pragma mark Objective-C Classes
1797
1799 llvm::StringRef name, clang::DeclContext *decl_ctx,
1800 OptionalClangModuleID owning_module, bool isInternal,
1801 std::optional<ClangASTMetadata> metadata) {
1802 ASTContext &ast = getASTContext();
1803 assert(!name.empty());
1804 if (!decl_ctx)
1805 decl_ctx = ast.getTranslationUnitDecl();
1806
1807 ObjCInterfaceDecl *decl =
1808 ObjCInterfaceDecl::CreateDeserialized(ast, GlobalDeclID());
1809 decl->setDeclContext(decl_ctx);
1810 decl->setDeclName(&ast.Idents.get(name));
1811 decl->setImplicit(isInternal);
1812 SetOwningModule(decl, owning_module);
1813
1814 if (metadata)
1815 SetMetadata(decl, *metadata);
1816
1817 return GetType(ast.getObjCInterfaceType(decl));
1818}
1819
1820bool TypeSystemClang::BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1821 return !TypeSystemClang::RecordHasFields(b->getType()->getAsCXXRecordDecl());
1822}
1823
1824uint32_t
1825TypeSystemClang::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1826 bool omit_empty_base_classes) {
1827 uint32_t num_bases = 0;
1828 if (cxx_record_decl) {
1829 if (omit_empty_base_classes) {
1830 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1831 for (base_class = cxx_record_decl->bases_begin(),
1832 base_class_end = cxx_record_decl->bases_end();
1833 base_class != base_class_end; ++base_class) {
1834 // Skip empty base classes
1835 if (BaseSpecifierIsEmpty(base_class))
1836 continue;
1837 ++num_bases;
1838 }
1839 } else
1840 num_bases = cxx_record_decl->getNumBases();
1841 }
1842 return num_bases;
1843}
1844
1845#pragma mark Namespace Declarations
1846
1848 const char *name, clang::DeclContext *decl_ctx,
1849 OptionalClangModuleID owning_module, bool is_inline) {
1850 NamespaceDecl *namespace_decl = nullptr;
1851 ASTContext &ast = getASTContext();
1852 TranslationUnitDecl *translation_unit_decl = ast.getTranslationUnitDecl();
1853 if (!decl_ctx)
1854 decl_ctx = translation_unit_decl;
1855
1856 if (name) {
1857 IdentifierInfo &identifier_info = ast.Idents.get(name);
1858 DeclarationName decl_name(&identifier_info);
1859 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1860 for (NamedDecl *decl : result) {
1861 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1862 if (namespace_decl)
1863 return namespace_decl;
1864 }
1865
1866 namespace_decl = NamespaceDecl::Create(ast, decl_ctx, is_inline,
1867 SourceLocation(), SourceLocation(),
1868 &identifier_info, nullptr, false);
1869
1870 decl_ctx->addDecl(namespace_decl);
1871 } else {
1872 if (decl_ctx == translation_unit_decl) {
1873 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1874 if (namespace_decl)
1875 return namespace_decl;
1876
1877 namespace_decl =
1878 NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1879 SourceLocation(), nullptr, nullptr, false);
1880 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1881 translation_unit_decl->addDecl(namespace_decl);
1882 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1883 } else {
1884 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1885 if (parent_namespace_decl) {
1886 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1887 if (namespace_decl)
1888 return namespace_decl;
1889 namespace_decl =
1890 NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1891 SourceLocation(), nullptr, nullptr, false);
1892 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1893 parent_namespace_decl->addDecl(namespace_decl);
1894 assert(namespace_decl ==
1895 parent_namespace_decl->getAnonymousNamespace());
1896 } else {
1897 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1898 "no namespace as decl_ctx");
1899 }
1900 }
1901 }
1902 // Note: namespaces can span multiple modules, so perhaps this isn't a good
1903 // idea.
1904 SetOwningModule(namespace_decl, owning_module);
1905
1906 VerifyDecl(namespace_decl);
1907 return namespace_decl;
1908}
1909
1910clang::BlockDecl *
1912 OptionalClangModuleID owning_module) {
1913 if (ctx) {
1914 clang::BlockDecl *decl =
1915 clang::BlockDecl::CreateDeserialized(getASTContext(), GlobalDeclID());
1916 decl->setDeclContext(ctx);
1917 ctx->addDecl(decl);
1918 SetOwningModule(decl, owning_module);
1919 return decl;
1920 }
1921 return nullptr;
1922}
1923
1924clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1925 clang::DeclContext *right,
1926 clang::DeclContext *root) {
1927 if (root == nullptr)
1928 return nullptr;
1929
1930 std::set<clang::DeclContext *> path_left;
1931 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1932 path_left.insert(d);
1933
1934 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1935 if (path_left.find(d) != path_left.end())
1936 return d;
1937
1938 return nullptr;
1939}
1940
1942 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1943 clang::NamespaceDecl *ns_decl) {
1944 if (decl_ctx && ns_decl) {
1945 auto *translation_unit = getASTContext().getTranslationUnitDecl();
1946 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1947 getASTContext(), decl_ctx, clang::SourceLocation(),
1948 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1949 clang::SourceLocation(), ns_decl,
1950 FindLCABetweenDecls(decl_ctx, ns_decl,
1951 translation_unit));
1952 decl_ctx->addDecl(using_decl);
1953 SetOwningModule(using_decl, owning_module);
1954 return using_decl;
1955 }
1956 return nullptr;
1957}
1958
1959clang::UsingDecl *
1960TypeSystemClang::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1961 OptionalClangModuleID owning_module,
1962 clang::NamedDecl *target) {
1963 if (current_decl_ctx && target) {
1964 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1965 getASTContext(), current_decl_ctx, clang::SourceLocation(),
1966 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1967 SetOwningModule(using_decl, owning_module);
1968 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1969 getASTContext(), current_decl_ctx, clang::SourceLocation(),
1970 target->getDeclName(), using_decl, target);
1971 SetOwningModule(shadow_decl, owning_module);
1972 using_decl->addShadowDecl(shadow_decl);
1973 current_decl_ctx->addDecl(using_decl);
1974 return using_decl;
1975 }
1976 return nullptr;
1977}
1978
1980 clang::DeclContext *decl_context, OptionalClangModuleID owning_module,
1981 const char *name, clang::QualType type) {
1982 if (decl_context) {
1983 clang::VarDecl *var_decl =
1984 clang::VarDecl::CreateDeserialized(getASTContext(), GlobalDeclID());
1985 var_decl->setDeclContext(decl_context);
1986 if (name && name[0])
1987 var_decl->setDeclName(&getASTContext().Idents.getOwn(name));
1988 var_decl->setType(type);
1989 SetOwningModule(var_decl, owning_module);
1990 var_decl->setAccess(clang::AS_public);
1991 decl_context->addDecl(var_decl);
1992 return var_decl;
1993 }
1994 return nullptr;
1995}
1996
1999 lldb::BasicType basic_type) {
2000 switch (basic_type) {
2001 case eBasicTypeVoid:
2002 return ast->VoidTy.getAsOpaquePtr();
2003 case eBasicTypeChar:
2004 return ast->CharTy.getAsOpaquePtr();
2006 return ast->SignedCharTy.getAsOpaquePtr();
2008 return ast->UnsignedCharTy.getAsOpaquePtr();
2009 case eBasicTypeWChar:
2010 return ast->getWCharType().getAsOpaquePtr();
2012 return ast->getSignedWCharType().getAsOpaquePtr();
2014 return ast->getUnsignedWCharType().getAsOpaquePtr();
2015 case eBasicTypeChar8:
2016 return ast->Char8Ty.getAsOpaquePtr();
2017 case eBasicTypeChar16:
2018 return ast->Char16Ty.getAsOpaquePtr();
2019 case eBasicTypeChar32:
2020 return ast->Char32Ty.getAsOpaquePtr();
2021 case eBasicTypeShort:
2022 return ast->ShortTy.getAsOpaquePtr();
2024 return ast->UnsignedShortTy.getAsOpaquePtr();
2025 case eBasicTypeInt:
2026 return ast->IntTy.getAsOpaquePtr();
2028 return ast->UnsignedIntTy.getAsOpaquePtr();
2029 case eBasicTypeLong:
2030 return ast->LongTy.getAsOpaquePtr();
2032 return ast->UnsignedLongTy.getAsOpaquePtr();
2033 case eBasicTypeLongLong:
2034 return ast->LongLongTy.getAsOpaquePtr();
2036 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2037 case eBasicTypeInt128:
2038 return ast->Int128Ty.getAsOpaquePtr();
2040 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2041 case eBasicTypeBool:
2042 return ast->BoolTy.getAsOpaquePtr();
2043 case eBasicTypeHalf:
2044 return ast->HalfTy.getAsOpaquePtr();
2045 case eBasicTypeFloat:
2046 return ast->FloatTy.getAsOpaquePtr();
2047 case eBasicTypeDouble:
2048 return ast->DoubleTy.getAsOpaquePtr();
2050 return ast->LongDoubleTy.getAsOpaquePtr();
2052 return ast->getComplexType(ast->FloatTy).getAsOpaquePtr();
2054 return ast->getComplexType(ast->DoubleTy).getAsOpaquePtr();
2056 return ast->getComplexType(ast->LongDoubleTy).getAsOpaquePtr();
2057 case eBasicTypeObjCID:
2058 return ast->getObjCIdType().getAsOpaquePtr();
2060 return ast->getObjCClassType().getAsOpaquePtr();
2061 case eBasicTypeObjCSel:
2062 return ast->getObjCSelType().getAsOpaquePtr();
2063 case eBasicTypeNullPtr:
2064 return ast->NullPtrTy.getAsOpaquePtr();
2065 default:
2066 return nullptr;
2067 }
2068}
2069
2070#pragma mark Function Types
2071
2072clang::DeclarationName
2074 const CompilerType &function_clang_type) {
2075 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2076 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2077 return DeclarationName(&getASTContext().Idents.get(
2078 name)); // Not operator, but a regular function.
2079
2080 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2081 // that doesn't correctly describe operators and if we try to create a method
2082 // and add it to the class, clang will assert and crash, so we need to make
2083 // sure things are acceptable.
2084 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2085 const clang::FunctionProtoType *function_type =
2086 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2087 if (function_type == nullptr)
2088 return clang::DeclarationName();
2089
2090 const bool is_method = false;
2091 const unsigned int num_params = function_type->getNumParams();
2093 is_method, op_kind, num_params))
2094 return clang::DeclarationName();
2095
2096 return getASTContext().DeclarationNames.getCXXOperatorName(op_kind);
2097}
2098
2100 clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
2101 printing_policy.SuppressTagKeyword = true;
2102 // Inline namespaces are important for some type formatters (e.g., libc++
2103 // and libstdc++ are differentiated by their inline namespaces).
2104 printing_policy.SuppressInlineNamespace = false;
2105 printing_policy.SuppressUnwrittenScope = false;
2106 // Default arguments are also always important for type formatters. Otherwise
2107 // we would need to always specify two type names for the setups where we do
2108 // know the default arguments and where we don't know default arguments.
2109 //
2110 // For example, without this we would need to have formatters for both:
2111 // std::basic_string<char>
2112 // and
2113 // std::basic_string<char, std::char_traits<char>, std::allocator<char> >
2114 // to support setups where LLDB was able to reconstruct default arguments
2115 // (and we then would have suppressed them from the type name) and also setups
2116 // where LLDB wasn't able to reconstruct the default arguments.
2117 printing_policy.SuppressDefaultTemplateArgs = false;
2118 return printing_policy;
2119}
2120
2121std::string TypeSystemClang::GetTypeNameForDecl(const NamedDecl *named_decl,
2122 bool qualified) {
2123 clang::PrintingPolicy printing_policy = GetTypePrintingPolicy();
2124 std::string result;
2125 llvm::raw_string_ostream os(result);
2126 named_decl->getNameForDiagnostic(os, printing_policy, qualified);
2127 return result;
2128}
2129
2131 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
2132 llvm::StringRef name, const CompilerType &function_clang_type,
2133 clang::StorageClass storage, bool is_inline) {
2134 FunctionDecl *func_decl = nullptr;
2135 ASTContext &ast = getASTContext();
2136 if (!decl_ctx)
2137 decl_ctx = ast.getTranslationUnitDecl();
2138
2139 const bool hasWrittenPrototype = true;
2140 const bool isConstexprSpecified = false;
2141
2142 clang::DeclarationName declarationName =
2143 GetDeclarationName(name, function_clang_type);
2144 func_decl = FunctionDecl::CreateDeserialized(ast, GlobalDeclID());
2145 func_decl->setDeclContext(decl_ctx);
2146 func_decl->setDeclName(declarationName);
2147 func_decl->setType(ClangUtil::GetQualType(function_clang_type));
2148 func_decl->setStorageClass(storage);
2149 func_decl->setInlineSpecified(is_inline);
2150 func_decl->setHasWrittenPrototype(hasWrittenPrototype);
2151 func_decl->setConstexprKind(isConstexprSpecified
2152 ? ConstexprSpecKind::Constexpr
2153 : ConstexprSpecKind::Unspecified);
2154 SetOwningModule(func_decl, owning_module);
2155 decl_ctx->addDecl(func_decl);
2156
2157 VerifyDecl(func_decl);
2158
2159 return func_decl;
2160}
2161
2163 const CompilerType &result_type, const CompilerType *args,
2164 unsigned num_args, bool is_variadic, unsigned type_quals,
2165 clang::CallingConv cc, clang::RefQualifierKind ref_qual) {
2166 if (!result_type || !ClangUtil::IsClangType(result_type))
2167 return CompilerType(); // invalid return type
2168
2169 std::vector<QualType> qual_type_args;
2170 if (num_args > 0 && args == nullptr)
2171 return CompilerType(); // invalid argument array passed in
2172
2173 // Verify that all arguments are valid and the right type
2174 for (unsigned i = 0; i < num_args; ++i) {
2175 if (args[i]) {
2176 // Make sure we have a clang type in args[i] and not a type from another
2177 // language whose name might match
2178 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2179 lldbassert(is_clang_type);
2180 if (is_clang_type)
2181 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2182 else
2183 return CompilerType(); // invalid argument type (must be a clang type)
2184 } else
2185 return CompilerType(); // invalid argument type (empty)
2186 }
2187
2188 // TODO: Detect calling convention in DWARF?
2189 FunctionProtoType::ExtProtoInfo proto_info;
2190 proto_info.ExtInfo = cc;
2191 proto_info.Variadic = is_variadic;
2192 proto_info.ExceptionSpec = EST_None;
2193 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
2194 proto_info.RefQualifier = ref_qual;
2195
2196 return GetType(getASTContext().getFunctionType(
2197 ClangUtil::GetQualType(result_type), qual_type_args, proto_info));
2198}
2199
2201 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
2202 const char *name, const CompilerType &param_type, int storage,
2203 bool add_decl) {
2204 ASTContext &ast = getASTContext();
2205 auto *decl = ParmVarDecl::CreateDeserialized(ast, GlobalDeclID());
2206 decl->setDeclContext(decl_ctx);
2207 if (name && name[0])
2208 decl->setDeclName(&ast.Idents.get(name));
2209 decl->setType(ClangUtil::GetQualType(param_type));
2210 decl->setStorageClass(static_cast<clang::StorageClass>(storage));
2211 SetOwningModule(decl, owning_module);
2212 if (add_decl)
2213 decl_ctx->addDecl(decl);
2214
2215 return decl;
2216}
2217
2219 FunctionDecl *function_decl, llvm::ArrayRef<ParmVarDecl *> params) {
2220 if (function_decl)
2221 function_decl->setParams(params);
2222}
2223
2226 QualType block_type = m_ast_up->getBlockPointerType(
2227 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2228
2229 return GetType(block_type);
2230}
2231
2232#pragma mark Array Types
2233
2236 std::optional<size_t> element_count,
2237 bool is_vector) {
2238 if (!element_type.IsValid())
2239 return {};
2240
2241 ASTContext &ast = getASTContext();
2242
2243 // Unknown number of elements; this is an incomplete array
2244 // (e.g., variable length array with non-constant bounds, or
2245 // a flexible array member).
2246 if (!element_count)
2247 return GetType(
2248 ast.getIncompleteArrayType(ClangUtil::GetQualType(element_type),
2249 clang::ArraySizeModifier::Normal, 0));
2250
2251 if (is_vector)
2252 return GetType(ast.getExtVectorType(ClangUtil::GetQualType(element_type),
2253 *element_count));
2254
2255 llvm::APInt ap_element_count(64, *element_count);
2256 return GetType(ast.getConstantArrayType(ClangUtil::GetQualType(element_type),
2257 ap_element_count, nullptr,
2258 clang::ArraySizeModifier::Normal, 0));
2259}
2260
2262 llvm::StringRef type_name,
2263 const std::initializer_list<std::pair<const char *, CompilerType>>
2264 &type_fields,
2265 bool packed) {
2266 CompilerType type;
2267 if (!type_name.empty() &&
2268 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2269 .IsValid()) {
2270 lldbassert(0 && "Trying to create a type for an existing name");
2271 return type;
2272 }
2273
2274 type = CreateRecordType(
2275 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, type_name,
2276 llvm::to_underlying(clang::TagTypeKind::Struct), lldb::eLanguageTypeC);
2278 for (const auto &field : type_fields)
2279 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2280 0);
2281 if (packed)
2282 SetIsPacked(type);
2284 return type;
2285}
2286
2288 llvm::StringRef type_name,
2289 const std::initializer_list<std::pair<const char *, CompilerType>>
2290 &type_fields,
2291 bool packed) {
2292 CompilerType type;
2293 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2294 return type;
2295
2296 return CreateStructForIdentifier(type_name, type_fields, packed);
2297}
2298
2299#pragma mark Enumeration Types
2300
2302 llvm::StringRef name, clang::DeclContext *decl_ctx,
2303 OptionalClangModuleID owning_module, const Declaration &decl,
2304 const CompilerType &integer_clang_type, bool is_scoped) {
2305 // TODO: Do something intelligent with the Declaration object passed in
2306 // like maybe filling in the SourceLocation with it...
2307 ASTContext &ast = getASTContext();
2308
2309 // TODO: ask about these...
2310 // const bool IsFixed = false;
2311 EnumDecl *enum_decl = EnumDecl::CreateDeserialized(ast, GlobalDeclID());
2312 enum_decl->setDeclContext(decl_ctx);
2313 if (!name.empty())
2314 enum_decl->setDeclName(&ast.Idents.get(name));
2315 enum_decl->setScoped(is_scoped);
2316 enum_decl->setScopedUsingClassTag(is_scoped);
2317 enum_decl->setFixed(false);
2318 SetOwningModule(enum_decl, owning_module);
2319 if (decl_ctx)
2320 decl_ctx->addDecl(enum_decl);
2321
2322 // TODO: check if we should be setting the promotion type too?
2323 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2324
2325 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2326
2327 return GetType(ast.getTagDeclType(enum_decl));
2328}
2329
2331 bool is_signed) {
2332 clang::ASTContext &ast = getASTContext();
2333
2334 if (is_signed) {
2335 if (bit_size == ast.getTypeSize(ast.SignedCharTy))
2336 return GetType(ast.SignedCharTy);
2337
2338 if (bit_size == ast.getTypeSize(ast.ShortTy))
2339 return GetType(ast.ShortTy);
2340
2341 if (bit_size == ast.getTypeSize(ast.IntTy))
2342 return GetType(ast.IntTy);
2343
2344 if (bit_size == ast.getTypeSize(ast.LongTy))
2345 return GetType(ast.LongTy);
2346
2347 if (bit_size == ast.getTypeSize(ast.LongLongTy))
2348 return GetType(ast.LongLongTy);
2349
2350 if (bit_size == ast.getTypeSize(ast.Int128Ty))
2351 return GetType(ast.Int128Ty);
2352 } else {
2353 if (bit_size == ast.getTypeSize(ast.UnsignedCharTy))
2354 return GetType(ast.UnsignedCharTy);
2355
2356 if (bit_size == ast.getTypeSize(ast.UnsignedShortTy))
2357 return GetType(ast.UnsignedShortTy);
2358
2359 if (bit_size == ast.getTypeSize(ast.UnsignedIntTy))
2360 return GetType(ast.UnsignedIntTy);
2361
2362 if (bit_size == ast.getTypeSize(ast.UnsignedLongTy))
2363 return GetType(ast.UnsignedLongTy);
2364
2365 if (bit_size == ast.getTypeSize(ast.UnsignedLongLongTy))
2366 return GetType(ast.UnsignedLongLongTy);
2367
2368 if (bit_size == ast.getTypeSize(ast.UnsignedInt128Ty))
2369 return GetType(ast.UnsignedInt128Ty);
2370 }
2371 return CompilerType();
2372}
2373
2375 return GetIntTypeFromBitSize(
2376 getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
2377}
2378
2379void TypeSystemClang::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2380 if (decl_ctx) {
2381 DumpDeclContextHiearchy(decl_ctx->getParent());
2382
2383 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2384 if (named_decl) {
2385 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2386 named_decl->getDeclName().getAsString().c_str());
2387 } else {
2388 printf("%20s\n", decl_ctx->getDeclKindName());
2389 }
2390 }
2391}
2392
2393void TypeSystemClang::DumpDeclHiearchy(clang::Decl *decl) {
2394 if (decl == nullptr)
2395 return;
2396 DumpDeclContextHiearchy(decl->getDeclContext());
2397
2398 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2399 if (record_decl) {
2400 printf("%20s: %s%s\n", decl->getDeclKindName(),
2401 record_decl->getDeclName().getAsString().c_str(),
2402 record_decl->isInjectedClassName() ? " (injected class name)" : "");
2403
2404 } else {
2405 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2406 if (named_decl) {
2407 printf("%20s: %s\n", decl->getDeclKindName(),
2408 named_decl->getDeclName().getAsString().c_str());
2409 } else {
2410 printf("%20s\n", decl->getDeclKindName());
2411 }
2412 }
2413}
2414
2415bool TypeSystemClang::GetCompleteDecl(clang::ASTContext *ast,
2416 clang::Decl *decl) {
2417 if (!decl)
2418 return false;
2419
2420 ExternalASTSource *ast_source = ast->getExternalSource();
2421
2422 if (!ast_source)
2423 return false;
2424
2425 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2426 if (tag_decl->isCompleteDefinition())
2427 return true;
2428
2429 if (!tag_decl->hasExternalLexicalStorage())
2430 return false;
2431
2432 ast_source->CompleteType(tag_decl);
2433
2434 return !tag_decl->getTypeForDecl()->isIncompleteType();
2435 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2436 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2437 if (objc_interface_decl->getDefinition())
2438 return true;
2439
2440 if (!objc_interface_decl->hasExternalLexicalStorage())
2441 return false;
2442
2443 ast_source->CompleteType(objc_interface_decl);
2444
2445 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2446 } else {
2447 return false;
2448 }
2449}
2450
2451void TypeSystemClang::SetMetadataAsUserID(const clang::Decl *decl,
2452 user_id_t user_id) {
2453 ClangASTMetadata meta_data;
2454 meta_data.SetUserID(user_id);
2455 SetMetadata(decl, meta_data);
2456}
2457
2458void TypeSystemClang::SetMetadataAsUserID(const clang::Type *type,
2459 user_id_t user_id) {
2460 ClangASTMetadata meta_data;
2461 meta_data.SetUserID(user_id);
2462 SetMetadata(type, meta_data);
2463}
2464
2465void TypeSystemClang::SetMetadata(const clang::Decl *object,
2466 ClangASTMetadata metadata) {
2467 m_decl_metadata[object] = metadata;
2468}
2469
2470void TypeSystemClang::SetMetadata(const clang::Type *object,
2471 ClangASTMetadata metadata) {
2472 m_type_metadata[object] = metadata;
2473}
2474
2475std::optional<ClangASTMetadata>
2476TypeSystemClang::GetMetadata(const clang::Decl *object) {
2477 auto It = m_decl_metadata.find(object);
2478 if (It != m_decl_metadata.end())
2479 return It->second;
2480
2481 return std::nullopt;
2482}
2483
2484std::optional<ClangASTMetadata>
2485TypeSystemClang::GetMetadata(const clang::Type *object) {
2486 auto It = m_type_metadata.find(object);
2487 if (It != m_type_metadata.end())
2488 return It->second;
2489
2490 return std::nullopt;
2491}
2492
2493void TypeSystemClang::SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object,
2494 clang::AccessSpecifier access) {
2495 if (access == clang::AccessSpecifier::AS_none)
2496 m_cxx_record_decl_access.erase(object);
2497 else
2498 m_cxx_record_decl_access[object] = access;
2499}
2500
2501clang::AccessSpecifier
2502TypeSystemClang::GetCXXRecordDeclAccess(const clang::CXXRecordDecl *object) {
2503 auto It = m_cxx_record_decl_access.find(object);
2504 if (It != m_cxx_record_decl_access.end())
2505 return It->second;
2506 return clang::AccessSpecifier::AS_none;
2507}
2508
2509clang::DeclContext *
2512}
2513
2516 if (auto *decl_context = GetDeclContextForType(type))
2517 return CreateDeclContext(decl_context);
2518 return CompilerDeclContext();
2519}
2520
2521/// Aggressively desugar the provided type, skipping past various kinds of
2522/// syntactic sugar and other constructs one typically wants to ignore.
2523/// The \p mask argument allows one to skip certain kinds of simplifications,
2524/// when one wishes to handle a certain kind of type directly.
2525static QualType
2526RemoveWrappingTypes(QualType type, ArrayRef<clang::Type::TypeClass> mask = {}) {
2527 while (true) {
2528 if (find(mask, type->getTypeClass()) != mask.end())
2529 return type;
2530 switch (type->getTypeClass()) {
2531 // This is not fully correct as _Atomic is more than sugar, but it is
2532 // sufficient for the purposes we care about.
2533 case clang::Type::Atomic:
2534 type = cast<clang::AtomicType>(type)->getValueType();
2535 break;
2536 case clang::Type::Auto:
2537 case clang::Type::Decltype:
2538 case clang::Type::Elaborated:
2539 case clang::Type::Paren:
2540 case clang::Type::SubstTemplateTypeParm:
2541 case clang::Type::TemplateSpecialization:
2542 case clang::Type::Typedef:
2543 case clang::Type::TypeOf:
2544 case clang::Type::TypeOfExpr:
2545 case clang::Type::Using:
2546 type = type->getLocallyUnqualifiedSingleStepDesugaredType();
2547 break;
2548 default:
2549 return type;
2550 }
2551 }
2552}
2553
2554clang::DeclContext *
2556 if (type.isNull())
2557 return nullptr;
2558
2559 clang::QualType qual_type = RemoveWrappingTypes(type.getCanonicalType());
2560 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2561 switch (type_class) {
2562 case clang::Type::ObjCInterface:
2563 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2564 ->getInterface();
2565 case clang::Type::ObjCObjectPointer:
2566 return GetDeclContextForType(
2567 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2568 ->getPointeeType());
2569 case clang::Type::Record:
2570 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2571 case clang::Type::Enum:
2572 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2573 default:
2574 break;
2575 }
2576 // No DeclContext in this type...
2577 return nullptr;
2578}
2579
2580/// Returns the clang::RecordType of the specified \ref qual_type. This
2581/// function will try to complete the type if necessary (and allowed
2582/// by the specified \ref allow_completion). If we fail to return a *complete*
2583/// type, returns nullptr.
2584static const clang::RecordType *GetCompleteRecordType(clang::ASTContext *ast,
2585 clang::QualType qual_type,
2586 bool allow_completion) {
2587 assert(qual_type->isRecordType());
2588
2589 const auto *tag_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2590
2591 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2592
2593 // RecordType with no way of completing it, return the plain
2594 // TagType.
2595 if (!cxx_record_decl || !cxx_record_decl->hasExternalLexicalStorage())
2596 return tag_type;
2597
2598 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2599 const bool fields_loaded =
2600 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2601
2602 // Already completed this type, nothing to be done.
2603 if (is_complete && fields_loaded)
2604 return tag_type;
2605
2606 if (!allow_completion)
2607 return nullptr;
2608
2609 // Call the field_begin() accessor to for it to use the external source
2610 // to load the fields...
2611 //
2612 // TODO: if we need to complete the type but have no external source,
2613 // shouldn't we error out instead?
2614 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2615 if (external_ast_source) {
2616 external_ast_source->CompleteType(cxx_record_decl);
2617 if (cxx_record_decl->isCompleteDefinition()) {
2618 cxx_record_decl->field_begin();
2619 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2620 }
2621 }
2622
2623 return tag_type;
2624}
2625
2626/// Returns the clang::EnumType of the specified \ref qual_type. This
2627/// function will try to complete the type if necessary (and allowed
2628/// by the specified \ref allow_completion). If we fail to return a *complete*
2629/// type, returns nullptr.
2630static const clang::EnumType *GetCompleteEnumType(clang::ASTContext *ast,
2631 clang::QualType qual_type,
2632 bool allow_completion) {
2633 assert(qual_type->isEnumeralType());
2634 assert(ast);
2635
2636 const clang::EnumType *enum_type =
2637 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
2638
2639 auto *tag_decl = enum_type->getAsTagDecl();
2640 assert(tag_decl);
2641
2642 // Already completed, nothing to be done.
2643 if (tag_decl->getDefinition())
2644 return enum_type;
2645
2646 if (!allow_completion)
2647 return nullptr;
2648
2649 // No definition but can't complete it, error out.
2650 if (!tag_decl->hasExternalLexicalStorage())
2651 return nullptr;
2652
2653 // We can't complete the type without an external source.
2654 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2655 if (!external_ast_source)
2656 return nullptr;
2657
2658 external_ast_source->CompleteType(tag_decl);
2659 return enum_type;
2660}
2661
2662/// Returns the clang::ObjCObjectType of the specified \ref qual_type. This
2663/// function will try to complete the type if necessary (and allowed
2664/// by the specified \ref allow_completion). If we fail to return a *complete*
2665/// type, returns nullptr.
2666static const clang::ObjCObjectType *
2667GetCompleteObjCObjectType(clang::ASTContext *ast, QualType qual_type,
2668 bool allow_completion) {
2669 assert(qual_type->isObjCObjectType());
2670 assert(ast);
2671
2672 const clang::ObjCObjectType *objc_class_type =
2673 llvm::cast<clang::ObjCObjectType>(qual_type);
2674
2675 clang::ObjCInterfaceDecl *class_interface_decl =
2676 objc_class_type->getInterface();
2677 // We currently can't complete objective C types through the newly added
2678 // ASTContext because it only supports TagDecl objects right now...
2679 if (!class_interface_decl)
2680 return objc_class_type;
2681
2682 // Already complete, nothing to be done.
2683 if (class_interface_decl->getDefinition())
2684 return objc_class_type;
2685
2686 if (!allow_completion)
2687 return nullptr;
2688
2689 // No definition but can't complete it, error out.
2690 if (!class_interface_decl->hasExternalLexicalStorage())
2691 return nullptr;
2692
2693 // We can't complete the type without an external source.
2694 clang::ExternalASTSource *external_ast_source = ast->getExternalSource();
2695 if (!external_ast_source)
2696 return nullptr;
2697
2698 external_ast_source->CompleteType(class_interface_decl);
2699 return objc_class_type;
2700}
2701
2702static bool GetCompleteQualType(clang::ASTContext *ast,
2703 clang::QualType qual_type,
2704 bool allow_completion = true) {
2705 qual_type = RemoveWrappingTypes(qual_type);
2706 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2707 switch (type_class) {
2708 case clang::Type::ConstantArray:
2709 case clang::Type::IncompleteArray:
2710 case clang::Type::VariableArray: {
2711 const clang::ArrayType *array_type =
2712 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2713
2714 if (array_type)
2715 return GetCompleteQualType(ast, array_type->getElementType(),
2716 allow_completion);
2717 } break;
2718 case clang::Type::Record: {
2719 if (const auto *RT =
2720 GetCompleteRecordType(ast, qual_type, allow_completion))
2721 return !RT->isIncompleteType();
2722
2723 return false;
2724 } break;
2725
2726 case clang::Type::Enum: {
2727 if (const auto *ET = GetCompleteEnumType(ast, qual_type, allow_completion))
2728 return !ET->isIncompleteType();
2729
2730 return false;
2731 } break;
2732 case clang::Type::ObjCObject:
2733 case clang::Type::ObjCInterface: {
2734 if (const auto *OT =
2735 GetCompleteObjCObjectType(ast, qual_type, allow_completion))
2736 return !OT->isIncompleteType();
2737
2738 return false;
2739 } break;
2740
2741 case clang::Type::Attributed:
2742 return GetCompleteQualType(
2743 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2744 allow_completion);
2745
2746 default:
2747 break;
2748 }
2749
2750 return true;
2751}
2752
2753static clang::ObjCIvarDecl::AccessControl
2755 switch (access) {
2756 case eAccessNone:
2757 return clang::ObjCIvarDecl::None;
2758 case eAccessPublic:
2759 return clang::ObjCIvarDecl::Public;
2760 case eAccessPrivate:
2761 return clang::ObjCIvarDecl::Private;
2762 case eAccessProtected:
2763 return clang::ObjCIvarDecl::Protected;
2764 case eAccessPackage:
2765 return clang::ObjCIvarDecl::Package;
2766 }
2767 return clang::ObjCIvarDecl::None;
2768}
2769
2770// Tests
2771
2772#ifndef NDEBUG
2774 return !type || llvm::isa<clang::Type>(GetQualType(type).getTypePtr());
2775}
2776#endif
2777
2779 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2780
2781 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2782 switch (type_class) {
2783 case clang::Type::IncompleteArray:
2784 case clang::Type::VariableArray:
2785 case clang::Type::ConstantArray:
2786 case clang::Type::ExtVector:
2787 case clang::Type::Vector:
2788 case clang::Type::Record:
2789 case clang::Type::ObjCObject:
2790 case clang::Type::ObjCInterface:
2791 return true;
2792 default:
2793 break;
2794 }
2795 // The clang type does have a value
2796 return false;
2797}
2798
2800 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2801
2802 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2803 switch (type_class) {
2804 case clang::Type::Record: {
2805 if (const clang::RecordType *record_type =
2806 llvm::dyn_cast_or_null<clang::RecordType>(
2807 qual_type.getTypePtrOrNull())) {
2808 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2809 return record_decl->isAnonymousStructOrUnion();
2810 }
2811 }
2812 break;
2813 }
2814 default:
2815 break;
2816 }
2817 // The clang type does have a value
2818 return false;
2819}
2820
2822 CompilerType *element_type_ptr,
2823 uint64_t *size, bool *is_incomplete) {
2824 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2825
2826 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2827 switch (type_class) {
2828 default:
2829 break;
2830
2831 case clang::Type::ConstantArray:
2832 if (element_type_ptr)
2833 element_type_ptr->SetCompilerType(
2834 weak_from_this(), llvm::cast<clang::ConstantArrayType>(qual_type)
2835 ->getElementType()
2836 .getAsOpaquePtr());
2837 if (size)
2838 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2839 ->getSize()
2840 .getLimitedValue(ULLONG_MAX);
2841 if (is_incomplete)
2842 *is_incomplete = false;
2843 return true;
2844
2845 case clang::Type::IncompleteArray:
2846 if (element_type_ptr)
2847 element_type_ptr->SetCompilerType(
2848 weak_from_this(), llvm::cast<clang::IncompleteArrayType>(qual_type)
2849 ->getElementType()
2850 .getAsOpaquePtr());
2851 if (size)
2852 *size = 0;
2853 if (is_incomplete)
2854 *is_incomplete = true;
2855 return true;
2856
2857 case clang::Type::VariableArray:
2858 if (element_type_ptr)
2859 element_type_ptr->SetCompilerType(
2860 weak_from_this(), llvm::cast<clang::VariableArrayType>(qual_type)
2861 ->getElementType()
2862 .getAsOpaquePtr());
2863 if (size)
2864 *size = 0;
2865 if (is_incomplete)
2866 *is_incomplete = false;
2867 return true;
2868
2869 case clang::Type::DependentSizedArray:
2870 if (element_type_ptr)
2871 element_type_ptr->SetCompilerType(
2872 weak_from_this(),
2873 llvm::cast<clang::DependentSizedArrayType>(qual_type)
2874 ->getElementType()
2875 .getAsOpaquePtr());
2876 if (size)
2877 *size = 0;
2878 if (is_incomplete)
2879 *is_incomplete = false;
2880 return true;
2881 }
2882 if (element_type_ptr)
2883 element_type_ptr->Clear();
2884 if (size)
2885 *size = 0;
2886 if (is_incomplete)
2887 *is_incomplete = false;
2888 return false;
2889}
2890
2892 CompilerType *element_type, uint64_t *size) {
2893 clang::QualType qual_type(GetCanonicalQualType(type));
2894
2895 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2896 switch (type_class) {
2897 case clang::Type::Vector: {
2898 const clang::VectorType *vector_type =
2899 qual_type->getAs<clang::VectorType>();
2900 if (vector_type) {
2901 if (size)
2902 *size = vector_type->getNumElements();
2903 if (element_type)
2904 *element_type = GetType(vector_type->getElementType());
2905 }
2906 return true;
2907 } break;
2908 case clang::Type::ExtVector: {
2909 const clang::ExtVectorType *ext_vector_type =
2910 qual_type->getAs<clang::ExtVectorType>();
2911 if (ext_vector_type) {
2912 if (size)
2913 *size = ext_vector_type->getNumElements();
2914 if (element_type)
2915 *element_type =
2916 CompilerType(weak_from_this(),
2917 ext_vector_type->getElementType().getAsOpaquePtr());
2918 }
2919 return true;
2920 }
2921 default:
2922 break;
2923 }
2924 return false;
2925}
2926
2929 clang::DeclContext *decl_ctx = GetDeclContextForType(GetQualType(type));
2930 if (!decl_ctx)
2931 return false;
2932
2933 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2934 return false;
2935
2936 clang::ObjCInterfaceDecl *result_iface_decl =
2937 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2938
2939 std::optional<ClangASTMetadata> ast_metadata = GetMetadata(result_iface_decl);
2940 if (!ast_metadata)
2941 return false;
2942
2943 return (ast_metadata->GetISAPtr() != 0);
2944}
2945
2947 return GetQualType(type).getUnqualifiedType()->isCharType();
2948}
2949
2951 // If the type hasn't been lazily completed yet, complete it now so that we
2952 // can give the caller an accurate answer whether the type actually has a
2953 // definition. Without completing the type now we would just tell the user
2954 // the current (internal) completeness state of the type and most users don't
2955 // care (or even know) about this behavior.
2956 const bool allow_completion = true;
2958 allow_completion);
2959}
2960
2962 return GetQualType(type).isConstQualified();
2963}
2964
2966 uint32_t &length) {
2967 CompilerType pointee_or_element_clang_type;
2968 length = 0;
2969 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2970
2971 if (!pointee_or_element_clang_type.IsValid())
2972 return false;
2973
2974 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2975 if (pointee_or_element_clang_type.IsCharType()) {
2976 if (type_flags.Test(eTypeIsArray)) {
2977 // We know the size of the array and it could be a C string since it is
2978 // an array of characters
2979 length = llvm::cast<clang::ConstantArrayType>(
2980 GetCanonicalQualType(type).getTypePtr())
2981 ->getSize()
2982 .getLimitedValue();
2983 }
2984 return true;
2985 }
2986 }
2987 return false;
2988}
2989
2991 if (type) {
2992 clang::QualType qual_type(GetCanonicalQualType(type));
2993 if (auto pointer_auth = qual_type.getPointerAuth())
2994 return pointer_auth.getKey();
2995 }
2996 return 0;
2997}
2998
2999unsigned
3001 if (type) {
3002 clang::QualType qual_type(GetCanonicalQualType(type));
3003 if (auto pointer_auth = qual_type.getPointerAuth())
3004 return pointer_auth.getExtraDiscriminator();
3005 }
3006 return 0;
3007}
3008
3011 if (type) {
3012 clang::QualType qual_type(GetCanonicalQualType(type));
3013 if (auto pointer_auth = qual_type.getPointerAuth())
3014 return pointer_auth.isAddressDiscriminated();
3015 }
3016 return false;
3017}
3018
3020 auto isFunctionType = [&](clang::QualType qual_type) {
3021 return qual_type->isFunctionType();
3022 };
3023
3024 return IsTypeImpl(type, isFunctionType);
3025}
3026
3027// Used to detect "Homogeneous Floating-point Aggregates"
3028uint32_t
3030 CompilerType *base_type_ptr) {
3031 if (!type)
3032 return 0;
3033
3034 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
3035 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3036 switch (type_class) {
3037 case clang::Type::Record:
3038 if (GetCompleteType(type)) {
3039 const clang::CXXRecordDecl *cxx_record_decl =
3040 qual_type->getAsCXXRecordDecl();
3041 if (cxx_record_decl) {
3042 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3043 return 0;
3044 }
3045 const clang::RecordType *record_type =
3046 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3047 if (record_type) {
3048 const clang::RecordDecl *record_decl = record_type->getDecl();
3049 if (record_decl) {
3050 // We are looking for a structure that contains only floating point
3051 // types
3052 clang::RecordDecl::field_iterator field_pos,
3053 field_end = record_decl->field_end();
3054 uint32_t num_fields = 0;
3055 bool is_hva = false;
3056 bool is_hfa = false;
3057 clang::QualType base_qual_type;
3058 uint64_t base_bitwidth = 0;
3059 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3060 ++field_pos) {
3061 clang::QualType field_qual_type = field_pos->getType();
3062 uint64_t field_bitwidth = getASTContext().getTypeSize(qual_type);
3063 if (field_qual_type->isFloatingType()) {
3064 if (field_qual_type->isComplexType())
3065 return 0;
3066 else {
3067 if (num_fields == 0)
3068 base_qual_type = field_qual_type;
3069 else {
3070 if (is_hva)
3071 return 0;
3072 is_hfa = true;
3073 if (field_qual_type.getTypePtr() !=
3074 base_qual_type.getTypePtr())
3075 return 0;
3076 }
3077 }
3078 } else if (field_qual_type->isVectorType() ||
3079 field_qual_type->isExtVectorType()) {
3080 if (num_fields == 0) {
3081 base_qual_type = field_qual_type;
3082 base_bitwidth = field_bitwidth;
3083 } else {
3084 if (is_hfa)
3085 return 0;
3086 is_hva = true;
3087 if (base_bitwidth != field_bitwidth)
3088 return 0;
3089 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3090 return 0;
3091 }
3092 } else
3093 return 0;
3094 ++num_fields;
3095 }
3096 if (base_type_ptr)
3097 *base_type_ptr =
3098 CompilerType(weak_from_this(), base_qual_type.getAsOpaquePtr());
3099 return num_fields;
3100 }
3101 }
3102 }
3103 break;
3104
3105 default:
3106 break;
3107 }
3108 return 0;
3109}
3110
3113 if (type) {
3114 clang::QualType qual_type(GetCanonicalQualType(type));
3115 const clang::FunctionProtoType *func =
3116 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3117 if (func)
3118 return func->getNumParams();
3119 }
3120 return 0;
3121}
3122
3125 const size_t index) {
3126 if (type) {
3127 clang::QualType qual_type(GetQualType(type));
3128 const clang::FunctionProtoType *func =
3129 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3130 if (func) {
3131 if (index < func->getNumParams())
3132 return CompilerType(weak_from_this(), func->getParamType(index).getAsOpaquePtr());
3133 }
3134 }
3135 return CompilerType();
3136}
3137
3140 llvm::function_ref<bool(clang::QualType)> predicate) const {
3141 if (type) {
3142 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3143
3144 if (predicate(qual_type))
3145 return true;
3146
3147 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3148 switch (type_class) {
3149 default:
3150 break;
3151
3152 case clang::Type::LValueReference:
3153 case clang::Type::RValueReference: {
3154 const clang::ReferenceType *reference_type =
3155 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3156 if (reference_type)
3157 return IsTypeImpl(reference_type->getPointeeType().getAsOpaquePtr(), predicate);
3158 } break;
3159 }
3160 }
3161 return false;
3162}
3163
3166 auto isMemberFunctionPointerType = [](clang::QualType qual_type) {
3167 return qual_type->isMemberFunctionPointerType();
3168 };
3169
3170 return IsTypeImpl(type, isMemberFunctionPointerType);
3171}
3172
3174 auto isFunctionPointerType = [](clang::QualType qual_type) {
3175 return qual_type->isFunctionPointerType();
3176 };
3177
3178 return IsTypeImpl(type, isFunctionPointerType);
3179}
3180
3183 CompilerType *function_pointer_type_ptr) {
3184 auto isBlockPointerType = [&](clang::QualType qual_type) {
3185 if (qual_type->isBlockPointerType()) {
3186 if (function_pointer_type_ptr) {
3187 const clang::BlockPointerType *block_pointer_type =
3188 qual_type->castAs<clang::BlockPointerType>();
3189 QualType pointee_type = block_pointer_type->getPointeeType();
3190 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
3191 *function_pointer_type_ptr = CompilerType(
3192 weak_from_this(), function_pointer_type.getAsOpaquePtr());
3193 }
3194 return true;
3195 }
3196
3197 return false;
3198 };
3199
3200 return IsTypeImpl(type, isBlockPointerType);
3201}
3202
3204 bool &is_signed) {
3205 if (!type)
3206 return false;
3207
3208 clang::QualType qual_type(GetCanonicalQualType(type));
3209 const clang::BuiltinType *builtin_type =
3210 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3211
3212 if (builtin_type) {
3213 if (builtin_type->isInteger()) {
3214 is_signed = builtin_type->isSignedInteger();
3215 return true;
3216 }
3217 }
3218
3219 return false;
3220}
3221
3223 bool &is_signed) {
3224 if (type) {
3225 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3226 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3227
3228 if (enum_type) {
3229 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3230 is_signed);
3231 return true;
3232 }
3233 }
3234
3235 return false;
3236}
3237
3240 if (type) {
3241 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3242 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3243
3244 if (enum_type) {
3245 return enum_type->isScopedEnumeralType();
3246 }
3247 }
3248
3249 return false;
3250}
3251
3253 CompilerType *pointee_type) {
3254 if (type) {
3255 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3256 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3257 switch (type_class) {
3258 case clang::Type::Builtin:
3259 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3260 default:
3261 break;
3262 case clang::BuiltinType::ObjCId:
3263 case clang::BuiltinType::ObjCClass:
3264 return true;
3265 }
3266 return false;
3267 case clang::Type::ObjCObjectPointer:
3268 if (pointee_type)
3269 pointee_type->SetCompilerType(
3270 weak_from_this(),
3271 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3272 ->getPointeeType()
3273 .getAsOpaquePtr());
3274 return true;
3275 case clang::Type::BlockPointer:
3276 if (pointee_type)
3277 pointee_type->SetCompilerType(
3278 weak_from_this(), llvm::cast<clang::BlockPointerType>(qual_type)
3279 ->getPointeeType()
3280 .getAsOpaquePtr());
3281 return true;
3282 case clang::Type::Pointer:
3283 if (pointee_type)
3284 pointee_type->SetCompilerType(weak_from_this(),
3285 llvm::cast<clang::PointerType>(qual_type)
3286 ->getPointeeType()
3287 .getAsOpaquePtr());
3288 return true;
3289 case clang::Type::MemberPointer:
3290 if (pointee_type)
3291 pointee_type->SetCompilerType(
3292 weak_from_this(), llvm::cast<clang::MemberPointerType>(qual_type)
3293 ->getPointeeType()
3294 .getAsOpaquePtr());
3295 return true;
3296 default:
3297 break;
3298 }
3299 }
3300 if (pointee_type)
3301 pointee_type->Clear();
3302 return false;
3303}
3304
3306 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3307 if (type) {
3308 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3309 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3310 switch (type_class) {
3311 case clang::Type::Builtin:
3312 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3313 default:
3314 break;
3315 case clang::BuiltinType::ObjCId:
3316 case clang::BuiltinType::ObjCClass:
3317 return true;
3318 }
3319 return false;
3320 case clang::Type::ObjCObjectPointer:
3321 if (pointee_type)
3322 pointee_type->SetCompilerType(
3323 weak_from_this(),
3324 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3325 ->getPointeeType()
3326 .getAsOpaquePtr());
3327 return true;
3328 case clang::Type::BlockPointer:
3329 if (pointee_type)
3330 pointee_type->SetCompilerType(
3331 weak_from_this(), llvm::cast<clang::BlockPointerType>(qual_type)
3332 ->getPointeeType()
3333 .getAsOpaquePtr());
3334 return true;
3335 case clang::Type::Pointer:
3336 if (pointee_type)
3337 pointee_type->SetCompilerType(weak_from_this(),
3338 llvm::cast<clang::PointerType>(qual_type)
3339 ->getPointeeType()
3340 .getAsOpaquePtr());
3341 return true;
3342 case clang::Type::MemberPointer:
3343 if (pointee_type)
3344 pointee_type->SetCompilerType(
3345 weak_from_this(), llvm::cast<clang::MemberPointerType>(qual_type)
3346 ->getPointeeType()
3347 .getAsOpaquePtr());
3348 return true;
3349 case clang::Type::LValueReference:
3350 if (pointee_type)
3351 pointee_type->SetCompilerType(
3352 weak_from_this(), llvm::cast<clang::LValueReferenceType>(qual_type)
3353 ->desugar()
3354 .getAsOpaquePtr());
3355 return true;
3356 case clang::Type::RValueReference:
3357 if (pointee_type)
3358 pointee_type->SetCompilerType(
3359 weak_from_this(), llvm::cast<clang::RValueReferenceType>(qual_type)
3360 ->desugar()
3361 .getAsOpaquePtr());
3362 return true;
3363 default:
3364 break;
3365 }
3366 }
3367 if (pointee_type)
3368 pointee_type->Clear();
3369 return false;
3370}
3371
3373 CompilerType *pointee_type,
3374 bool *is_rvalue) {
3375 if (type) {
3376 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3377 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3378
3379 switch (type_class) {
3380 case clang::Type::LValueReference:
3381 if (pointee_type)
3382 pointee_type->SetCompilerType(
3383 weak_from_this(), llvm::cast<clang::LValueReferenceType>(qual_type)
3384 ->desugar()
3385 .getAsOpaquePtr());
3386 if (is_rvalue)
3387 *is_rvalue = false;
3388 return true;
3389 case clang::Type::RValueReference:
3390 if (pointee_type)
3391 pointee_type->SetCompilerType(
3392 weak_from_this(), llvm::cast<clang::RValueReferenceType>(qual_type)
3393 ->desugar()
3394 .getAsOpaquePtr());
3395 if (is_rvalue)
3396 *is_rvalue = true;
3397 return true;
3398
3399 default:
3400 break;
3401 }
3402 }
3403 if (pointee_type)
3404 pointee_type->Clear();
3405 return false;
3406}
3407
3409 uint32_t &count, bool &is_complex) {
3410 if (type) {
3411 clang::QualType qual_type(GetCanonicalQualType(type));
3412
3413 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3414 qual_type->getCanonicalTypeInternal())) {
3415 clang::BuiltinType::Kind kind = BT->getKind();
3416 if (kind >= clang::BuiltinType::Float &&
3417 kind <= clang::BuiltinType::LongDouble) {
3418 count = 1;
3419 is_complex = false;
3420 return true;
3421 }
3422 } else if (const clang::ComplexType *CT =
3423 llvm::dyn_cast<clang::ComplexType>(
3424 qual_type->getCanonicalTypeInternal())) {
3425 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3426 is_complex)) {
3427 count = 2;
3428 is_complex = true;
3429 return true;
3430 }
3431 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3432 qual_type->getCanonicalTypeInternal())) {
3433 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3434 is_complex)) {
3435 count = VT->getNumElements();
3436 is_complex = false;
3437 return true;
3438 }
3439 }
3440 }
3441 count = 0;
3442 is_complex = false;
3443 return false;
3444}
3445
3447 if (!type)
3448 return false;
3449
3450 clang::QualType qual_type(GetQualType(type));
3451 const clang::TagType *tag_type =
3452 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3453 if (tag_type) {
3454 clang::TagDecl *tag_decl = tag_type->getDecl();
3455 if (tag_decl)
3456 return tag_decl->isCompleteDefinition();
3457 return false;
3458 } else {
3459 const clang::ObjCObjectType *objc_class_type =
3460 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3461 if (objc_class_type) {
3462 clang::ObjCInterfaceDecl *class_interface_decl =
3463 objc_class_type->getInterface();
3464 if (class_interface_decl)
3465 return class_interface_decl->getDefinition() != nullptr;
3466 return false;
3467 }
3468 }
3469 return true;
3470}
3471
3473 if (ClangUtil::IsClangType(type)) {
3474 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3475
3476 const clang::ObjCObjectPointerType *obj_pointer_type =
3477 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3478
3479 if (obj_pointer_type)
3480 return obj_pointer_type->isObjCClassType();
3481 }
3482 return false;
3483}
3484
3486 if (ClangUtil::IsClangType(type))
3487 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3488 return false;
3489}
3490
3492 if (!type)
3493 return false;
3494 clang::QualType qual_type(GetCanonicalQualType(type));
3495 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3496 return (type_class == clang::Type::Record);
3497}
3498
3500 if (!type)
3501 return false;
3502 clang::QualType qual_type(GetCanonicalQualType(type));
3503 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3504 return (type_class == clang::Type::Enum);
3505}
3506
3508 if (type) {
3509 clang::QualType qual_type(GetCanonicalQualType(type));
3510 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3511 switch (type_class) {
3512 case clang::Type::Record:
3513 if (GetCompleteType(type)) {
3514 const clang::RecordType *record_type =
3515 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3516 const clang::RecordDecl *record_decl = record_type->getDecl();
3517 if (record_decl) {
3518 const clang::CXXRecordDecl *cxx_record_decl =
3519 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3520 if (cxx_record_decl) {
3521 // We can't just call is isPolymorphic() here because that just
3522 // means the current class has virtual functions, it doesn't check
3523 // if any inherited classes have virtual functions. The doc string
3524 // in SBType::IsPolymorphicClass() says it is looking for both
3525 // if the class has virtual methods or if any bases do, so this
3526 // should be more correct.
3527 return cxx_record_decl->isDynamicClass();
3528 }
3529 }
3530 }
3531 break;
3532
3533 default:
3534 break;
3535 }
3536 }
3537 return false;
3538}
3539
3541 CompilerType *dynamic_pointee_type,
3542 bool check_cplusplus,
3543 bool check_objc) {
3544 clang::QualType pointee_qual_type;
3545 if (type) {
3546 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3547 bool success = false;
3548 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3549 switch (type_class) {
3550 case clang::Type::Builtin:
3551 if (check_objc &&
3552 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3553 clang::BuiltinType::ObjCId) {
3554 if (dynamic_pointee_type)
3555 dynamic_pointee_type->SetCompilerType(weak_from_this(), type);
3556 return true;
3557 }
3558 break;
3559
3560 case clang::Type::ObjCObjectPointer:
3561 if (check_objc) {
3562 if (const auto *objc_pointee_type =
3563 qual_type->getPointeeType().getTypePtrOrNull()) {
3564 if (const auto *objc_object_type =
3565 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3566 objc_pointee_type)) {
3567 if (objc_object_type->isObjCClass())
3568 return false;
3569 }
3570 }
3571 if (dynamic_pointee_type)
3572 dynamic_pointee_type->SetCompilerType(
3573 weak_from_this(),
3574 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3575 ->getPointeeType()
3576 .getAsOpaquePtr());
3577 return true;
3578 }
3579 break;
3580
3581 case clang::Type::Pointer:
3582 pointee_qual_type =
3583 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3584 success = true;
3585 break;
3586
3587 case clang::Type::LValueReference:
3588 case clang::Type::RValueReference:
3589 pointee_qual_type =
3590 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3591 success = true;
3592 break;
3593
3594 default:
3595 break;
3596 }
3597
3598 if (success) {
3599 // Check to make sure what we are pointing too is a possible dynamic C++
3600 // type We currently accept any "void *" (in case we have a class that
3601 // has been watered down to an opaque pointer) and virtual C++ classes.
3602 const clang::Type::TypeClass pointee_type_class =
3603 pointee_qual_type.getCanonicalType()->getTypeClass();
3604 switch (pointee_type_class) {
3605 case clang::Type::Builtin:
3606 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3607 case clang::BuiltinType::UnknownAny:
3608 case clang::BuiltinType::Void:
3609 if (dynamic_pointee_type)
3610 dynamic_pointee_type->SetCompilerType(
3611 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3612 return true;
3613 default:
3614 break;
3615 }
3616 break;
3617
3618 case clang::Type::Record:
3619 if (check_cplusplus) {
3620 clang::CXXRecordDecl *cxx_record_decl =
3621 pointee_qual_type->getAsCXXRecordDecl();
3622 if (cxx_record_decl) {
3623 bool is_complete = cxx_record_decl->isCompleteDefinition();
3624
3625 if (is_complete)
3626 success = cxx_record_decl->isDynamicClass();
3627 else {
3628 if (std::optional<ClangASTMetadata> metadata =
3629 GetMetadata(cxx_record_decl))
3630 success = metadata->GetIsDynamicCXXType();
3631 else {
3632 is_complete = GetType(pointee_qual_type).GetCompleteType();
3633 if (is_complete)
3634 success = cxx_record_decl->isDynamicClass();
3635 else
3636 success = false;
3637 }
3638 }
3639
3640 if (success) {
3641 if (dynamic_pointee_type)
3642 dynamic_pointee_type->SetCompilerType(
3643 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3644 return true;
3645 }
3646 }
3647 }
3648 break;
3649
3650 case clang::Type::ObjCObject:
3651 case clang::Type::ObjCInterface:
3652 if (check_objc) {
3653 if (dynamic_pointee_type)
3654 dynamic_pointee_type->SetCompilerType(
3655 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3656 return true;
3657 }
3658 break;
3659
3660 default:
3661 break;
3662 }
3663 }
3664 }
3665 if (dynamic_pointee_type)
3666 dynamic_pointee_type->Clear();
3667 return false;
3668}
3669
3671 if (!type)
3672 return false;
3673
3674 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3675}
3676
3678 if (!type)
3679 return false;
3680 return RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef})
3681 ->getTypeClass() == clang::Type::Typedef;
3682}
3683
3685 if (!type)
3686 return false;
3687 return GetCanonicalQualType(type)->isVoidType();
3688}
3689
3691 if (auto *record_decl =
3693 return record_decl->canPassInRegisters();
3694 }
3695 return false;
3696}
3697
3699 return TypeSystemClangSupportsLanguage(language);
3700}
3701
3702std::optional<std::string>
3704 if (!type)
3705 return std::nullopt;
3706
3707 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3708 if (qual_type.isNull())
3709 return std::nullopt;
3710
3711 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3712 if (!cxx_record_decl)
3713 return std::nullopt;
3714
3715 return std::string(cxx_record_decl->getIdentifier()->getNameStart());
3716}
3717
3719 if (!type)
3720 return false;
3721
3722 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3723 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
3724}
3725
3727 if (!type)
3728 return false;
3729 clang::QualType qual_type(GetCanonicalQualType(type));
3730 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3731 if (tag_type)
3732 return tag_type->isBeingDefined();
3733 return false;
3734}
3735
3737 CompilerType *class_type_ptr) {
3738 if (!ClangUtil::IsClangType(type))
3739 return false;
3740
3741 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3742
3743 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3744 if (class_type_ptr) {
3745 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3746 const clang::ObjCObjectPointerType *obj_pointer_type =
3747 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3748 if (obj_pointer_type == nullptr)
3749 class_type_ptr->Clear();
3750 else
3751 class_type_ptr->SetCompilerType(
3752 type.GetTypeSystem(),
3753 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3754 .getAsOpaquePtr());
3755 }
3756 }
3757 return true;
3758 }
3759 if (class_type_ptr)
3760 class_type_ptr->Clear();
3761 return false;
3762}
3763
3764// Type Completion
3765
3767 if (!type)
3768 return false;
3769 const bool allow_completion = true;
3771 allow_completion);
3772}
3773
3775 bool base_only) {
3776 if (!type)
3777 return ConstString();
3778
3779 clang::QualType qual_type(GetQualType(type));
3780
3781 // Remove certain type sugar from the name. Sugar such as elaborated types
3782 // or template types which only serve to improve diagnostics shouldn't
3783 // act as their own types from the user's perspective (e.g., formatter
3784 // shouldn't format a variable differently depending on how the ser has
3785 // specified the type. '::Type' and 'Type' should behave the same).
3786 // Typedefs and atomic derived types are not removed as they are actually
3787 // useful for identifiying specific types.
3788 qual_type = RemoveWrappingTypes(qual_type,
3789 {clang::Type::Typedef, clang::Type::Atomic});
3790
3791 // For a typedef just return the qualified name.
3792 if (const auto *typedef_type = qual_type->getAs<clang::TypedefType>()) {
3793 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3794 return ConstString(GetTypeNameForDecl(typedef_decl));
3795 }
3796
3797 // For consistency, this follows the same code path that clang uses to emit
3798 // debug info. This also handles when we don't want any scopes preceding the
3799 // name.
3800 if (auto *named_decl = qual_type->getAsTagDecl())
3801 return ConstString(GetTypeNameForDecl(named_decl, !base_only));
3802
3803 return ConstString(qual_type.getAsString(GetTypePrintingPolicy()));
3804}
3805
3808 if (!type)
3809 return ConstString();
3810
3811 clang::QualType qual_type(GetQualType(type));
3812 clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
3813 printing_policy.SuppressTagKeyword = true;
3814 printing_policy.SuppressScope = false;
3815 printing_policy.SuppressUnwrittenScope = true;
3816 printing_policy.SuppressInlineNamespace = true;
3817 return ConstString(qual_type.getAsString(printing_policy));
3818}
3819
3820uint32_t
3822 CompilerType *pointee_or_element_clang_type) {
3823 if (!type)
3824 return 0;
3825
3826 if (pointee_or_element_clang_type)
3827 pointee_or_element_clang_type->Clear();
3828
3829 clang::QualType qual_type =
3830 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
3831
3832 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3833 switch (type_class) {
3834 case clang::Type::Attributed:
3835 return GetTypeInfo(qual_type->castAs<clang::AttributedType>()
3836 ->getModifiedType()
3837 .getAsOpaquePtr(),
3838 pointee_or_element_clang_type);
3839 case clang::Type::Builtin: {
3840 const clang::BuiltinType *builtin_type =
3841 llvm::cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3842
3843 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3844 switch (builtin_type->getKind()) {
3845 case clang::BuiltinType::ObjCId:
3846 case clang::BuiltinType::ObjCClass:
3847 if (pointee_or_element_clang_type)
3848 pointee_or_element_clang_type->SetCompilerType(
3849 weak_from_this(),
3850 getASTContext().ObjCBuiltinClassTy.getAsOpaquePtr());
3851 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3852 break;
3853
3854 case clang::BuiltinType::ObjCSel:
3855 if (pointee_or_element_clang_type)
3856 pointee_or_element_clang_type->SetCompilerType(
3857 weak_from_this(), getASTContext().CharTy.getAsOpaquePtr());
3858 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3859 break;
3860
3861 case clang::BuiltinType::Bool:
3862 case clang::BuiltinType::Char_U:
3863 case clang::BuiltinType::UChar:
3864 case clang::BuiltinType::WChar_U:
3865 case clang::BuiltinType::Char16:
3866 case clang::BuiltinType::Char32:
3867 case clang::BuiltinType::UShort:
3868 case clang::BuiltinType::UInt:
3869 case clang::BuiltinType::ULong:
3870 case clang::BuiltinType::ULongLong:
3871 case clang::BuiltinType::UInt128:
3872 case clang::BuiltinType::Char_S:
3873 case clang::BuiltinType::SChar:
3874 case clang::BuiltinType::WChar_S:
3875 case clang::BuiltinType::Short:
3876 case clang::BuiltinType::Int:
3877 case clang::BuiltinType::Long:
3878 case clang::BuiltinType::LongLong:
3879 case clang::BuiltinType::Int128:
3880 case clang::BuiltinType::Float:
3881 case clang::BuiltinType::Double:
3882 case clang::BuiltinType::LongDouble:
3883 builtin_type_flags |= eTypeIsScalar;
3884 if (builtin_type->isInteger()) {
3885 builtin_type_flags |= eTypeIsInteger;
3886 if (builtin_type->isSignedInteger())
3887 builtin_type_flags |= eTypeIsSigned;
3888 } else if (builtin_type->isFloatingPoint())
3889 builtin_type_flags |= eTypeIsFloat;
3890 break;
3891 default:
3892 break;
3893 }
3894 return builtin_type_flags;
3895 }
3896
3897 case clang::Type::BlockPointer:
3898 if (pointee_or_element_clang_type)
3899 pointee_or_element_clang_type->SetCompilerType(
3900 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3901 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3902
3903 case clang::Type::Complex: {
3904 uint32_t complex_type_flags =
3905 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3906 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3907 qual_type->getCanonicalTypeInternal());
3908 if (complex_type) {
3909 clang::QualType complex_element_type(complex_type->getElementType());
3910 if (complex_element_type->isIntegerType())
3911 complex_type_flags |= eTypeIsFloat;
3912 else if (complex_element_type->isFloatingType())
3913 complex_type_flags |= eTypeIsInteger;
3914 }
3915 return complex_type_flags;
3916 } break;
3917
3918 case clang::Type::ConstantArray:
3919 case clang::Type::DependentSizedArray:
3920 case clang::Type::IncompleteArray:
3921 case clang::Type::VariableArray:
3922 if (pointee_or_element_clang_type)
3923 pointee_or_element_clang_type->SetCompilerType(
3924 weak_from_this(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3925 ->getElementType()
3926 .getAsOpaquePtr());
3927 return eTypeHasChildren | eTypeIsArray;
3928
3929 case clang::Type::DependentName:
3930 return 0;
3931 case clang::Type::DependentSizedExtVector:
3932 return eTypeHasChildren | eTypeIsVector;
3933 case clang::Type::DependentTemplateSpecialization:
3934 return eTypeIsTemplate;
3935
3936 case clang::Type::Enum:
3937 if (pointee_or_element_clang_type)
3938 pointee_or_element_clang_type->SetCompilerType(
3939 weak_from_this(), llvm::cast<clang::EnumType>(qual_type)
3940 ->getDecl()
3941 ->getIntegerType()
3942 .getAsOpaquePtr());
3943 return eTypeIsEnumeration | eTypeHasValue;
3944
3945 case clang::Type::FunctionProto:
3946 return eTypeIsFuncPrototype | eTypeHasValue;
3947 case clang::Type::FunctionNoProto:
3948 return eTypeIsFuncPrototype | eTypeHasValue;
3949 case clang::Type::InjectedClassName:
3950 return 0;
3951
3952 case clang::Type::LValueReference:
3953 case clang::Type::RValueReference:
3954 if (pointee_or_element_clang_type)
3955 pointee_or_element_clang_type->SetCompilerType(
3956 weak_from_this(),
3957 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3958 ->getPointeeType()
3959 .getAsOpaquePtr());
3960 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3961
3962 case clang::Type::MemberPointer:
3963 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3964
3965 case clang::Type::ObjCObjectPointer:
3966 if (pointee_or_element_clang_type)
3967 pointee_or_element_clang_type->SetCompilerType(
3968 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3969 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3970 eTypeHasValue;
3971
3972 case clang::Type::ObjCObject:
3973 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3974 case clang::Type::ObjCInterface:
3975 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3976
3977 case clang::Type::Pointer:
3978 if (pointee_or_element_clang_type)
3979 pointee_or_element_clang_type->SetCompilerType(
3980 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3981 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3982
3983 case clang::Type::Record:
3984 if (qual_type->getAsCXXRecordDecl())
3985 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3986 else
3987 return eTypeHasChildren | eTypeIsStructUnion;
3988 break;
3989 case clang::Type::SubstTemplateTypeParm:
3990 return eTypeIsTemplate;
3991 case clang::Type::TemplateTypeParm:
3992 return eTypeIsTemplate;
3993 case clang::Type::TemplateSpecialization:
3994 return eTypeIsTemplate;
3995
3996 case clang::Type::Typedef:
3997 return eTypeIsTypedef | GetType(llvm::cast<clang::TypedefType>(qual_type)
3998 ->getDecl()
3999 ->getUnderlyingType())
4000 .GetTypeInfo(pointee_or_element_clang_type);
4001 case clang::Type::UnresolvedUsing:
4002 return 0;
4003
4004 case clang::Type::ExtVector:
4005 case clang::Type::Vector: {
4006 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4007 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4008 qual_type->getCanonicalTypeInternal());
4009 if (vector_type) {
4010 if (vector_type->isIntegerType())
4011 vector_type_flags |= eTypeIsFloat;
4012 else if (vector_type->isFloatingType())
4013 vector_type_flags |= eTypeIsInteger;
4014 }
4015 return vector_type_flags;
4016 }
4017 default:
4018 return 0;
4019 }
4020 return 0;
4021}
4022
4025 if (!type)
4026 return lldb::eLanguageTypeC;
4027
4028 // If the type is a reference, then resolve it to what it refers to first:
4029 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4030 if (qual_type->isAnyPointerType()) {
4031 if (qual_type->isObjCObjectPointerType())
4033 if (qual_type->getPointeeCXXRecordDecl())
4035
4036 clang::QualType pointee_type(qual_type->getPointeeType());
4037 if (pointee_type->getPointeeCXXRecordDecl())
4039 if (pointee_type->isObjCObjectOrInterfaceType())
4041 if (pointee_type->isObjCClassType())
4043 if (pointee_type.getTypePtr() ==
4044 getASTContext().ObjCBuiltinIdTy.getTypePtr())
4046 } else {
4047 if (qual_type->isObjCObjectOrInterfaceType())
4049 if (qual_type->getAsCXXRecordDecl())
4051 switch (qual_type->getTypeClass()) {
4052 default:
4053 break;
4054 case clang::Type::Builtin:
4055 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4056 default:
4057 case clang::BuiltinType::Void:
4058 case clang::BuiltinType::Bool:
4059 case clang::BuiltinType::Char_U:
4060 case clang::BuiltinType::UChar:
4061 case clang::BuiltinType::WChar_U:
4062 case clang::BuiltinType::Char16:
4063 case clang::BuiltinType::Char32:
4064 case clang::BuiltinType::UShort:
4065 case clang::BuiltinType::UInt:
4066 case clang::BuiltinType::ULong:
4067 case clang::BuiltinType::ULongLong:
4068 case clang::BuiltinType::UInt128:
4069 case clang::BuiltinType::Char_S:
4070 case clang::BuiltinType::SChar:
4071 case clang::BuiltinType::WChar_S:
4072 case clang::BuiltinType::Short:
4073 case clang::BuiltinType::Int:
4074 case clang::BuiltinType::Long:
4075 case clang::BuiltinType::LongLong:
4076 case clang::BuiltinType::Int128:
4077 case clang::BuiltinType::Float:
4078 case clang::BuiltinType::Double:
4079 case clang::BuiltinType::LongDouble:
4080 break;
4081
4082 case clang::BuiltinType::NullPtr:
4084
4085 case clang::BuiltinType::ObjCId:
4086 case clang::BuiltinType::ObjCClass:
4087 case clang::BuiltinType::ObjCSel:
4088 return eLanguageTypeObjC;
4089
4090 case clang::BuiltinType::Dependent:
4091 case clang::BuiltinType::Overload:
4092 case clang::BuiltinType::BoundMember:
4093 case clang::BuiltinType::UnknownAny:
4094 break;
4095 }
4096 break;
4097 case clang::Type::Typedef:
4098 return GetType(llvm::cast<clang::TypedefType>(qual_type)
4099 ->getDecl()
4100 ->getUnderlyingType())
4102 }
4103 }
4104 return lldb::eLanguageTypeC;
4105}
4106
4107lldb::TypeClass
4109 if (!type)
4110 return lldb::eTypeClassInvalid;
4111
4112 clang::QualType qual_type =
4113 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
4114
4115 switch (qual_type->getTypeClass()) {
4116 case clang::Type::Atomic:
4117 case clang::Type::Auto:
4118 case clang::Type::CountAttributed:
4119 case clang::Type::Decltype:
4120 case clang::Type::Elaborated:
4121 case clang::Type::Paren:
4122 case clang::Type::TypeOf:
4123 case clang::Type::TypeOfExpr:
4124 case clang::Type::Using:
4125 llvm_unreachable("Handled in RemoveWrappingTypes!");
4126 case clang::Type::UnaryTransform:
4127 break;
4128 case clang::Type::FunctionNoProto:
4129 return lldb::eTypeClassFunction;
4130 case clang::Type::FunctionProto:
4131 return lldb::eTypeClassFunction;
4132 case clang::Type::IncompleteArray:
4133 return lldb::eTypeClassArray;
4134 case clang::Type::VariableArray:
4135 return lldb::eTypeClassArray;
4136 case clang::Type::ConstantArray:
4137 return lldb::eTypeClassArray;
4138 case clang::Type::DependentSizedArray:
4139 return lldb::eTypeClassArray;
4140 case clang::Type::ArrayParameter:
4141 return lldb::eTypeClassArray;
4142 case clang::Type::DependentSizedExtVector:
4143 return lldb::eTypeClassVector;
4144 case clang::Type::DependentVector:
4145 return lldb::eTypeClassVector;
4146 case clang::Type::ExtVector:
4147 return lldb::eTypeClassVector;
4148 case clang::Type::Vector:
4149 return lldb::eTypeClassVector;
4150 case clang::Type::Builtin:
4151 // Ext-Int is just an integer type.
4152 case clang::Type::BitInt:
4153 case clang::Type::DependentBitInt:
4154 return lldb::eTypeClassBuiltin;
4155 case clang::Type::ObjCObjectPointer:
4156 return lldb::eTypeClassObjCObjectPointer;
4157 case clang::Type::BlockPointer:
4158 return lldb::eTypeClassBlockPointer;
4159 case clang::Type::Pointer:
4160 return lldb::eTypeClassPointer;
4161 case clang::Type::LValueReference:
4162 return lldb::eTypeClassReference;
4163 case clang::Type::RValueReference:
4164 return lldb::eTypeClassReference;
4165 case clang::Type::MemberPointer:
4166 return lldb::eTypeClassMemberPointer;
4167 case clang::Type::Complex:
4168 if (qual_type->isComplexType())
4169 return lldb::eTypeClassComplexFloat;
4170 else
4171 return lldb::eTypeClassComplexInteger;
4172 case clang::Type::ObjCObject:
4173 return lldb::eTypeClassObjCObject;
4174 case clang::Type::ObjCInterface:
4175 return lldb::eTypeClassObjCInterface;
4176 case clang::Type::Record: {
4177 const clang::RecordType *record_type =
4178 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4179 const clang::RecordDecl *record_decl = record_type->getDecl();
4180 if (record_decl->isUnion())
4181 return lldb::eTypeClassUnion;
4182 else if (record_decl->isStruct())
4183 return lldb::eTypeClassStruct;
4184 else
4185 return lldb::eTypeClassClass;
4186 } break;
4187 case clang::Type::Enum:
4188 return lldb::eTypeClassEnumeration;
4189 case clang::Type::Typedef:
4190 return lldb::eTypeClassTypedef;
4191 case clang::Type::UnresolvedUsing:
4192 break;
4193
4194 case clang::Type::Attributed:
4195 case clang::Type::BTFTagAttributed:
4196 break;
4197 case clang::Type::TemplateTypeParm:
4198 break;
4199 case clang::Type::SubstTemplateTypeParm:
4200 break;
4201 case clang::Type::SubstTemplateTypeParmPack:
4202 break;
4203 case clang::Type::InjectedClassName:
4204 break;
4205 case clang::Type::DependentName:
4206 break;
4207 case clang::Type::DependentTemplateSpecialization:
4208 break;
4209 case clang::Type::PackExpansion:
4210 break;
4211
4212 case clang::Type::TemplateSpecialization:
4213 break;
4214 case clang::Type::DeducedTemplateSpecialization:
4215 break;
4216 case clang::Type::Pipe:
4217 break;
4218
4219 // pointer type decayed from an array or function type.
4220 case clang::Type::Decayed:
4221 break;
4222 case clang::Type::Adjusted:
4223 break;
4224 case clang::Type::ObjCTypeParam:
4225 break;
4226
4227 case clang::Type::DependentAddressSpace:
4228 break;
4229 case clang::Type::MacroQualified:
4230 break;
4231
4232 // Matrix types that we're not sure how to display at the moment.
4233 case clang::Type::ConstantMatrix:
4234 case clang::Type::DependentSizedMatrix:
4235 break;
4236
4237 // We don't handle pack indexing yet
4238 case clang::Type::PackIndexing:
4239 break;
4240 }
4241 // We don't know hot to display this type...
4242 return lldb::eTypeClassOther;
4243}
4244
4246 if (type)
4247 return GetQualType(type).getQualifiers().getCVRQualifiers();
4248 return 0;
4249}
4250
4251// Creating related types
4252
4255 ExecutionContextScope *exe_scope) {
4256 if (type) {
4257 clang::QualType qual_type(GetQualType(type));
4258
4259 const clang::Type *array_eletype =
4260 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4261
4262 if (!array_eletype)
4263 return CompilerType();
4264
4265 return GetType(clang::QualType(array_eletype, 0));
4266 }
4267 return CompilerType();
4268}
4269
4271 uint64_t size) {
4272 if (type) {
4273 clang::QualType qual_type(GetCanonicalQualType(type));
4274 clang::ASTContext &ast_ctx = getASTContext();
4275 if (size != 0)
4276 return GetType(ast_ctx.getConstantArrayType(
4277 qual_type, llvm::APInt(64, size), nullptr,
4278 clang::ArraySizeModifier::Normal, 0));
4279 else
4280 return GetType(ast_ctx.getIncompleteArrayType(
4281 qual_type, clang::ArraySizeModifier::Normal, 0));
4282 }
4283
4284 return CompilerType();
4285}
4286
4289 if (type)
4290 return GetType(GetCanonicalQualType(type));
4291 return CompilerType();
4292}
4293
4294static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4295 clang::QualType qual_type) {
4296 if (qual_type->isPointerType())
4297 qual_type = ast->getPointerType(
4298 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4299 else if (const ConstantArrayType *arr =
4300 ast->getAsConstantArrayType(qual_type)) {
4301 qual_type = ast->getConstantArrayType(
4302 GetFullyUnqualifiedType_Impl(ast, arr->getElementType()),
4303 arr->getSize(), arr->getSizeExpr(), arr->getSizeModifier(),
4304 arr->getIndexTypeQualifiers().getAsOpaqueValue());
4305 } else
4306 qual_type = qual_type.getUnqualifiedType();
4307 qual_type.removeLocalConst();
4308 qual_type.removeLocalRestrict();
4309 qual_type.removeLocalVolatile();
4310 return qual_type;
4311}
4312
4315 if (type)
4316 return GetType(
4318 return CompilerType();
4319}
4320
4323 if (type)
4325 return CompilerType();
4326}
4327
4330 if (type) {
4331 const clang::FunctionProtoType *func =
4332 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4333 if (func)
4334 return func->getNumParams();
4335 }
4336 return -1;
4337}
4338
4340 lldb::opaque_compiler_type_t type, size_t idx) {
4341 if (type) {
4342 const clang::FunctionProtoType *func =
4343 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4344 if (func) {
4345 const uint32_t num_args = func->getNumParams();
4346 if (idx < num_args)
4347 return GetType(func->getParamType(idx));
4348 }
4349 }
4350 return CompilerType();
4351}
4352
4355 if (type) {
4356 clang::QualType qual_type(GetQualType(type));
4357 const clang::FunctionProtoType *func =
4358 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4359 if (func)
4360 return GetType(func->getReturnType());
4361 }
4362 return CompilerType();
4363}
4364
4365size_t
4367 size_t num_functions = 0;
4368 if (type) {
4369 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4370 switch (qual_type->getTypeClass()) {
4371 case clang::Type::Record:
4372 if (GetCompleteQualType(&getASTContext(), qual_type)) {
4373 const clang::RecordType *record_type =
4374 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4375 const clang::RecordDecl *record_decl = record_type->getDecl();
4376 assert(record_decl);
4377 const clang::CXXRecordDecl *cxx_record_decl =
4378 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4379 if (cxx_record_decl)
4380 num_functions = std::distance(cxx_record_decl->method_begin(),
4381 cxx_record_decl->method_end());
4382 }
4383 break;
4384
4385 case clang::Type::ObjCObjectPointer: {
4386 const clang::ObjCObjectPointerType *objc_class_type =
4387 qual_type->castAs<clang::ObjCObjectPointerType>();
4388 const clang::ObjCInterfaceType *objc_interface_type =
4389 objc_class_type->getInterfaceType();
4390 if (objc_interface_type &&
4392 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4393 clang::ObjCInterfaceDecl *class_interface_decl =
4394 objc_interface_type->getDecl();
4395 if (class_interface_decl) {
4396 num_functions = std::distance(class_interface_decl->meth_begin(),
4397 class_interface_decl->meth_end());
4398 }
4399 }
4400 break;
4401 }
4402
4403 case clang::Type::ObjCObject:
4404 case clang::Type::ObjCInterface:
4405 if (GetCompleteType(type)) {
4406 const clang::ObjCObjectType *objc_class_type =
4407 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4408 if (objc_class_type) {
4409 clang::ObjCInterfaceDecl *class_interface_decl =
4410 objc_class_type->getInterface();
4411 if (class_interface_decl)
4412 num_functions = std::distance(class_interface_decl->meth_begin(),
4413 class_interface_decl->meth_end());
4414 }
4415 }
4416 break;
4417
4418 default:
4419 break;
4420 }
4421 }
4422 return num_functions;
4423}
4424
4427 size_t idx) {
4428 std::string name;
4429 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4430 CompilerType clang_type;
4431 CompilerDecl clang_decl;
4432 if (type) {
4433 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4434 switch (qual_type->getTypeClass()) {
4435 case clang::Type::Record:
4436 if (GetCompleteQualType(&getASTContext(), qual_type)) {
4437 const clang::RecordType *record_type =
4438 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4439 const clang::RecordDecl *record_decl = record_type->getDecl();
4440 assert(record_decl);
4441 const clang::CXXRecordDecl *cxx_record_decl =
4442 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4443 if (cxx_record_decl) {
4444 auto method_iter = cxx_record_decl->method_begin();
4445 auto method_end = cxx_record_decl->method_end();
4446 if (idx <
4447 static_cast<size_t>(std::distance(method_iter, method_end))) {
4448 std::advance(method_iter, idx);
4449 clang::CXXMethodDecl *cxx_method_decl =
4450 method_iter->getCanonicalDecl();
4451 if (cxx_method_decl) {
4452 name = cxx_method_decl->getDeclName().getAsString();
4453 if (cxx_method_decl->isStatic())
4455 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4457 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4459 else
4461 clang_type = GetType(cxx_method_decl->getType());
4462 clang_decl = GetCompilerDecl(cxx_method_decl);
4463 }
4464 }
4465 }
4466 }
4467 break;
4468
4469 case clang::Type::ObjCObjectPointer: {
4470 const clang::ObjCObjectPointerType *objc_class_type =
4471 qual_type->castAs<clang::ObjCObjectPointerType>();
4472 const clang::ObjCInterfaceType *objc_interface_type =
4473 objc_class_type->getInterfaceType();
4474 if (objc_interface_type &&
4476 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4477 clang::ObjCInterfaceDecl *class_interface_decl =
4478 objc_interface_type->getDecl();
4479 if (class_interface_decl) {
4480 auto method_iter = class_interface_decl->meth_begin();
4481 auto method_end = class_interface_decl->meth_end();
4482 if (idx <
4483 static_cast<size_t>(std::distance(method_iter, method_end))) {
4484 std::advance(method_iter, idx);
4485 clang::ObjCMethodDecl *objc_method_decl =
4486 method_iter->getCanonicalDecl();
4487 if (objc_method_decl) {
4488 clang_decl = GetCompilerDecl(objc_method_decl);
4489 name = objc_method_decl->getSelector().getAsString();
4490 if (objc_method_decl->isClassMethod())
4492 else
4494 }
4495 }
4496 }
4497 }
4498 break;
4499 }
4500
4501 case clang::Type::ObjCObject:
4502 case clang::Type::ObjCInterface:
4503 if (GetCompleteType(type)) {
4504 const clang::ObjCObjectType *objc_class_type =
4505 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4506 if (objc_class_type) {
4507 clang::ObjCInterfaceDecl *class_interface_decl =
4508 objc_class_type->getInterface();
4509 if (class_interface_decl) {
4510 auto method_iter = class_interface_decl->meth_begin();
4511 auto method_end = class_interface_decl->meth_end();
4512 if (idx <
4513 static_cast<size_t>(std::distance(method_iter, method_end))) {
4514 std::advance(method_iter, idx);
4515 clang::ObjCMethodDecl *objc_method_decl =
4516 method_iter->getCanonicalDecl();
4517 if (objc_method_decl) {
4518 clang_decl = GetCompilerDecl(objc_method_decl);
4519 name = objc_method_decl->getSelector().getAsString();
4520 if (objc_method_decl->isClassMethod())
4522 else
4524 }
4525 }
4526 }
4527 }
4528 }
4529 break;
4530
4531 default:
4532 break;
4533 }
4534 }
4535
4536 if (kind == eMemberFunctionKindUnknown)
4537 return TypeMemberFunctionImpl();
4538 else
4539 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4540}
4541
4544 if (type)
4545 return GetType(GetQualType(type).getNonReferenceType());
4546 return CompilerType();
4547}
4548
4551 if (type) {
4552 clang::QualType qual_type(GetQualType(type));
4553 return GetType(qual_type.getTypePtr()->getPointeeType());
4554 }
4555 return CompilerType();
4556}
4557
4560 if (type) {
4561 clang::QualType qual_type(GetQualType(type));
4562
4563 switch (qual_type.getDesugaredType(getASTContext())->getTypeClass()) {
4564 case clang::Type::ObjCObject:
4565 case clang::Type::ObjCInterface:
4566 return GetType(getASTContext().getObjCObjectPointerType(qual_type));
4567
4568 default:
4569 return GetType(getASTContext().getPointerType(qual_type));
4570 }
4571 }
4572 return CompilerType();
4573}
4574
4577 if (type)
4578 return GetType(getASTContext().getLValueReferenceType(GetQualType(type)));
4579 else
4580 return CompilerType();
4581}
4582
4585 if (type)
4586 return GetType(getASTContext().getRValueReferenceType(GetQualType(type)));
4587 else
4588 return CompilerType();
4589}
4590
4592 if (!type)
4593 return CompilerType();
4594 return GetType(getASTContext().getAtomicType(GetQualType(type)));
4595}
4596
4599 if (type) {
4600 clang::QualType result(GetQualType(type));
4601 result.addConst();
4602 return GetType(result);
4603 }
4604 return CompilerType();
4605}
4606
4609 uint32_t payload) {
4610 if (type) {
4611 clang::ASTContext &clang_ast = getASTContext();
4612 auto pauth = PointerAuthQualifier::fromOpaqueValue(payload);
4613 clang::QualType result =
4614 clang_ast.getPointerAuthType(GetQualType(type), pauth);
4615 return GetType(result);
4616 }
4617 return CompilerType();
4618}
4619
4622 if (type) {
4623 clang::QualType result(GetQualType(type));
4624 result.addVolatile();
4625 return GetType(result);
4626 }
4627 return CompilerType();
4628}
4629
4632 if (type) {
4633 clang::QualType result(GetQualType(type));
4634 result.addRestrict();
4635 return GetType(result);
4636 }
4637 return CompilerType();
4638}
4639
4641 lldb::opaque_compiler_type_t type, const char *typedef_name,
4642 const CompilerDeclContext &compiler_decl_ctx, uint32_t payload) {
4643 if (type && typedef_name && typedef_name[0]) {
4644 clang::ASTContext &clang_ast = getASTContext();
4645 clang::QualType qual_type(GetQualType(type));
4646
4647 clang::DeclContext *decl_ctx =
4649 if (!decl_ctx)
4650 decl_ctx = getASTContext().getTranslationUnitDecl();
4651
4652 clang::TypedefDecl *decl =
4653 clang::TypedefDecl::CreateDeserialized(clang_ast, GlobalDeclID());
4654 decl->setDeclContext(decl_ctx);
4655 decl->setDeclName(&clang_ast.Idents.get(typedef_name));
4656 decl->setTypeSourceInfo(clang_ast.getTrivialTypeSourceInfo(qual_type));
4657 decl_ctx->addDecl(decl);
4658 SetOwningModule(decl, TypePayloadClang(payload).GetOwningModule());
4659
4660 clang::TagDecl *tdecl = nullptr;
4661 if (!qual_type.isNull()) {
4662 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4663 tdecl = rt->getDecl();
4664 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4665 tdecl = et->getDecl();
4666 }
4667
4668 // Check whether this declaration is an anonymous struct, union, or enum,
4669 // hidden behind a typedef. If so, we try to check whether we have a
4670 // typedef tag to attach to the original record declaration
4671 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4672 tdecl->setTypedefNameForAnonDecl(decl);
4673
4674 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4675
4676 // Get a uniqued clang::QualType for the typedef decl type
4677 return GetType(clang_ast.getTypedefType(decl));
4678 }
4679 return CompilerType();
4680}
4681
4684 if (type) {
4685 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(
4686 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef}));
4687 if (typedef_type)
4688 return GetType(typedef_type->getDecl()->getUnderlyingType());
4689 }
4690 return CompilerType();
4691}
4692
4693// Create related types using the current type's AST
4694
4696 return TypeSystemClang::GetBasicType(basic_type);
4697}
4698
4700 clang::ASTContext &ast = getASTContext();
4701 const FunctionType::ExtInfo generic_ext_info(
4702 /*noReturn=*/false,
4703 /*hasRegParm=*/false,
4704 /*regParm=*/0,
4705 CallingConv::CC_C,
4706 /*producesResult=*/false,
4707 /*noCallerSavedRegs=*/false,
4708 /*NoCfCheck=*/false,
4709 /*cmseNSCall=*/false);
4710 QualType func_type = ast.getFunctionNoProtoType(ast.VoidTy, generic_ext_info);
4711 return GetType(func_type);
4712}
4713// Exploring the type
4714
4715const llvm::fltSemantics &
4717 clang::ASTContext &ast = getASTContext();
4718 const size_t bit_size = byte_size * 8;
4719 if (bit_size == ast.getTypeSize(ast.FloatTy))
4720 return ast.getFloatTypeSemantics(ast.FloatTy);
4721 else if (bit_size == ast.getTypeSize(ast.DoubleTy))
4722 return ast.getFloatTypeSemantics(ast.DoubleTy);
4723 else if (bit_size == ast.getTypeSize(ast.LongDoubleTy) ||
4724 bit_size == llvm::APFloat::semanticsSizeInBits(
4725 ast.getFloatTypeSemantics(ast.LongDoubleTy)))
4726 return ast.getFloatTypeSemantics(ast.LongDoubleTy);
4727 else if (bit_size == ast.getTypeSize(ast.HalfTy))
4728 return ast.getFloatTypeSemantics(ast.HalfTy);
4729 return llvm::APFloatBase::Bogus();
4730}
4731
4732std::optional<uint64_t>
4734 ExecutionContextScope *exe_scope) {
4735 assert(qual_type->isObjCObjectOrInterfaceType());
4736 ExecutionContext exe_ctx(exe_scope);
4737 if (Process *process = exe_ctx.GetProcessPtr()) {
4738 if (ObjCLanguageRuntime *objc_runtime =
4739 ObjCLanguageRuntime::Get(*process)) {
4740 if (std::optional<uint64_t> bit_size =
4741 objc_runtime->GetTypeBitSize(GetType(qual_type)))
4742 return *bit_size;
4743 }
4744 } else {
4745 static bool g_printed = false;
4746 if (!g_printed) {
4747 StreamString s;
4748 DumpTypeDescription(qual_type.getAsOpaquePtr(), s);
4749
4750 llvm::outs() << "warning: trying to determine the size of type ";
4751 llvm::outs() << s.GetString() << "\n";
4752 llvm::outs() << "without a valid ExecutionContext. this is not "
4753 "reliable. please file a bug against LLDB.\n";
4754 llvm::outs() << "backtrace:\n";
4755 llvm::sys::PrintStackTrace(llvm::outs());
4756 llvm::outs() << "\n";
4757 g_printed = true;
4758 }
4759 }
4760
4761 return getASTContext().getTypeSize(qual_type) +
4762 getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4763}
4764
4765std::optional<uint64_t>
4767 ExecutionContextScope *exe_scope) {
4768 if (!GetCompleteType(type))
4769 return std::nullopt;
4770
4771 clang::QualType qual_type(GetCanonicalQualType(type));
4772 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4773 switch (type_class) {
4774 case clang::Type::ConstantArray:
4775 case clang::Type::FunctionProto:
4776 case clang::Type::Record:
4777 return getASTContext().getTypeSize(qual_type);
4778 case clang::Type::ObjCInterface:
4779 case clang::Type::ObjCObject:
4780 return GetObjCBitSize(qual_type, exe_scope);
4781 case clang::Type::IncompleteArray: {
4782 const uint64_t bit_size = getASTContext().getTypeSize(qual_type);
4783 if (bit_size == 0)
4784 return getASTContext().getTypeSize(
4785 qual_type->getArrayElementTypeNoTypeQual()
4786 ->getCanonicalTypeUnqualified());
4787
4788 return bit_size;
4789 }
4790 default:
4791 if (const uint64_t bit_size = getASTContext().getTypeSize(qual_type))
4792 return bit_size;
4793 }
4794
4795 return std::nullopt;
4796}
4797
4798std::optional<size_t>
4800 ExecutionContextScope *exe_scope) {
4801 if (GetCompleteType(type))
4802 return getASTContext().getTypeAlign(GetQualType(type));
4803 return {};
4804}
4805
4807 uint64_t &count) {
4808 if (!type)
4810
4811 count = 1;
4812 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4813
4814 switch (qual_type->getTypeClass()) {
4815 case clang::Type::Atomic:
4816 case clang::Type::Auto:
4817 case clang::Type::CountAttributed:
4818 case clang::Type::Decltype:
4819 case clang::Type::Elaborated:
4820 case clang::Type::Paren:
4821 case clang::Type::Typedef:
4822 case clang::Type::TypeOf:
4823 case clang::Type::TypeOfExpr:
4824 case clang::Type::Using:
4825 llvm_unreachable("Handled in RemoveWrappingTypes!");
4826
4827 case clang::Type::UnaryTransform:
4828 break;
4829
4830 case clang::Type::FunctionNoProto:
4831 case clang::Type::FunctionProto:
4832 return lldb::eEncodingUint;
4833
4834 case clang::Type::IncompleteArray:
4835 case clang::Type::VariableArray:
4836 case clang::Type::ArrayParameter:
4837 break;
4838
4839 case clang::Type::ConstantArray:
4840 break;
4841
4842 case clang::Type::DependentVector:
4843 case clang::Type::ExtVector:
4844 case clang::Type::Vector:
4845 // TODO: Set this to more than one???
4846 break;
4847
4848 case clang::Type::BitInt:
4849 case clang::Type::DependentBitInt:
4850 return qual_type->isUnsignedIntegerType() ? lldb::eEncodingUint
4852
4853 case clang::Type::Builtin:
4854 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4855 case clang::BuiltinType::Void:
4856 break;
4857
4858 case clang::BuiltinType::Char_S:
4859 case clang::BuiltinType::SChar:
4860 case clang::BuiltinType::WChar_S:
4861 case clang::BuiltinType::Short:
4862 case clang::BuiltinType::Int:
4863 case clang::BuiltinType::Long:
4864 case clang::BuiltinType::LongLong:
4865 case clang::BuiltinType::Int128:
4866 return lldb::eEncodingSint;
4867
4868 case clang::BuiltinType::Bool:
4869 case clang::BuiltinType::Char_U:
4870 case clang::BuiltinType::UChar:
4871 case clang::BuiltinType::WChar_U:
4872 case clang::BuiltinType::Char8:
4873 case clang::BuiltinType::Char16:
4874 case clang::BuiltinType::Char32:
4875 case clang::BuiltinType::UShort:
4876 case clang::BuiltinType::UInt:
4877 case clang::BuiltinType::ULong:
4878 case clang::BuiltinType::ULongLong:
4879 case clang::BuiltinType::UInt128:
4880 return lldb::eEncodingUint;
4881
4882 // Fixed point types. Note that they are currently ignored.
4883 case clang::BuiltinType::ShortAccum:
4884 case clang::BuiltinType::Accum:
4885 case clang::BuiltinType::LongAccum:
4886 case clang::BuiltinType::UShortAccum:
4887 case clang::BuiltinType::UAccum:
4888 case clang::BuiltinType::ULongAccum:
4889 case clang::BuiltinType::ShortFract:
4890 case clang::BuiltinType::Fract:
4891 case clang::BuiltinType::LongFract:
4892 case clang::BuiltinType::UShortFract:
4893 case clang::BuiltinType::UFract:
4894 case clang::BuiltinType::ULongFract:
4895 case clang::BuiltinType::SatShortAccum:
4896 case clang::BuiltinType::SatAccum:
4897 case clang::BuiltinType::SatLongAccum:
4898 case clang::BuiltinType::SatUShortAccum:
4899 case clang::BuiltinType::SatUAccum:
4900 case clang::BuiltinType::SatULongAccum:
4901 case clang::BuiltinType::SatShortFract:
4902 case clang::BuiltinType::SatFract:
4903 case clang::BuiltinType::SatLongFract:
4904 case clang::BuiltinType::SatUShortFract:
4905 case clang::BuiltinType::SatUFract:
4906 case clang::BuiltinType::SatULongFract:
4907 break;
4908
4909 case clang::BuiltinType::Half:
4910 case clang::BuiltinType::Float:
4911 case clang::BuiltinType::Float16:
4912 case clang::BuiltinType::Float128:
4913 case clang::BuiltinType::Double:
4914 case clang::BuiltinType::LongDouble:
4915 case clang::BuiltinType::BFloat16:
4916 case clang::BuiltinType::Ibm128:
4918
4919 case clang::BuiltinType::ObjCClass:
4920 case clang::BuiltinType::ObjCId:
4921 case clang::BuiltinType::ObjCSel:
4922 return lldb::eEncodingUint;
4923
4924 case clang::BuiltinType::NullPtr:
4925 return lldb::eEncodingUint;
4926
4927 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4928 case clang::BuiltinType::Kind::BoundMember:
4929 case clang::BuiltinType::Kind::BuiltinFn:
4930 case clang::BuiltinType::Kind::Dependent:
4931 case clang::BuiltinType::Kind::OCLClkEvent:
4932 case clang::BuiltinType::Kind::OCLEvent:
4933 case clang::BuiltinType::Kind::OCLImage1dRO:
4934 case clang::BuiltinType::Kind::OCLImage1dWO:
4935 case clang::BuiltinType::Kind::OCLImage1dRW:
4936 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4937 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4938 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4939 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4940 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4941 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4942 case clang::BuiltinType::Kind::OCLImage2dRO:
4943 case clang::BuiltinType::Kind::OCLImage2dWO:
4944 case clang::BuiltinType::Kind::OCLImage2dRW:
4945 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4946 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4947 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4948 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4949 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4950 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4951 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4952 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4953 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4954 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4955 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4956 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4957 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4958 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4959 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4960 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4961 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4962 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4963 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4964 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4965 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4966 case clang::BuiltinType::Kind::OCLImage3dRO:
4967 case clang::BuiltinType::Kind::OCLImage3dWO:
4968 case clang::BuiltinType::Kind::OCLImage3dRW:
4969 case clang::BuiltinType::Kind::OCLQueue:
4970 case clang::BuiltinType::Kind::OCLReserveID:
4971 case clang::BuiltinType::Kind::OCLSampler:
4972 case clang::BuiltinType::Kind::HLSLResource:
4973 case clang::BuiltinType::Kind::ArraySection:
4974 case clang::BuiltinType::Kind::OMPArrayShaping:
4975 case clang::BuiltinType::Kind::OMPIterator:
4976 case clang::BuiltinType::Kind::Overload:
4977 case clang::BuiltinType::Kind::PseudoObject:
4978 case clang::BuiltinType::Kind::UnknownAny:
4979 break;
4980
4981 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
4982 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
4983 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
4984 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
4985 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
4986 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
4987 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
4988 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
4989 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleReferenceStreamout:
4990 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualReferenceStreamout:
4991 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleReferenceStreamin:
4992 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualReferenceStreamin:
4993 break;
4994
4995 // PowerPC -- Matrix Multiply Assist
4996 case clang::BuiltinType::VectorPair:
4997 case clang::BuiltinType::VectorQuad:
4998 break;
4999
5000 // ARM -- Scalable Vector Extension
5001 case clang::BuiltinType::SveBool:
5002 case clang::BuiltinType::SveBoolx2:
5003 case clang::BuiltinType::SveBoolx4:
5004 case clang::BuiltinType::SveCount:
5005 case clang::BuiltinType::SveInt8:
5006 case clang::BuiltinType::SveInt8x2:
5007 case clang::BuiltinType::SveInt8x3:
5008 case clang::BuiltinType::SveInt8x4:
5009 case clang::BuiltinType::SveInt16:
5010 case clang::BuiltinType::SveInt16x2:
5011 case clang::BuiltinType::SveInt16x3:
5012 case clang::BuiltinType::SveInt16x4:
5013 case clang::BuiltinType::SveInt32:
5014 case clang::BuiltinType::SveInt32x2:
5015 case clang::BuiltinType::SveInt32x3:
5016 case clang::BuiltinType::SveInt32x4:
5017 case clang::BuiltinType::SveInt64:
5018 case clang::BuiltinType::SveInt64x2:
5019 case clang::BuiltinType::SveInt64x3:
5020 case clang::BuiltinType::SveInt64x4:
5021 case clang::BuiltinType::SveUint8:
5022 case clang::BuiltinType::SveUint8x2:
5023 case clang::BuiltinType::SveUint8x3:
5024 case clang::BuiltinType::SveUint8x4:
5025 case clang::BuiltinType::SveUint16:
5026 case clang::BuiltinType::SveUint16x2:
5027 case clang::BuiltinType::SveUint16x3:
5028 case clang::BuiltinType::SveUint16x4:
5029 case clang::BuiltinType::SveUint32:
5030 case clang::BuiltinType::SveUint32x2:
5031 case clang::BuiltinType::SveUint32x3:
5032 case clang::BuiltinType::SveUint32x4:
5033 case clang::BuiltinType::SveUint64:
5034 case clang::BuiltinType::SveUint64x2:
5035 case clang::BuiltinType::SveUint64x3:
5036 case clang::BuiltinType::SveUint64x4:
5037 case clang::BuiltinType::SveFloat16:
5038 case clang::BuiltinType::SveBFloat16:
5039 case clang::BuiltinType::SveBFloat16x2:
5040 case clang::BuiltinType::SveBFloat16x3:
5041 case clang::BuiltinType::SveBFloat16x4:
5042 case clang::BuiltinType::SveFloat16x2:
5043 case clang::BuiltinType::SveFloat16x3:
5044 case clang::BuiltinType::SveFloat16x4:
5045 case clang::BuiltinType::SveFloat32:
5046 case clang::BuiltinType::SveFloat32x2:
5047 case clang::BuiltinType::SveFloat32x3:
5048 case clang::BuiltinType::SveFloat32x4:
5049 case clang::BuiltinType::SveFloat64:
5050 case clang::BuiltinType::SveFloat64x2:
5051 case clang::BuiltinType::SveFloat64x3:
5052 case clang::BuiltinType::SveFloat64x4:
5053 break;
5054
5055 // RISC-V V builtin types.
5056#define RVV_TYPE(Name, Id, SingletonId) case clang::BuiltinType::Id:
5057#include "clang/Basic/RISCVVTypes.def"
5058 break;
5059
5060 // WebAssembly builtin types.
5061 case clang::BuiltinType::WasmExternRef:
5062 break;
5063
5064 case clang::BuiltinType::IncompleteMatrixIdx:
5065 break;
5066
5067 case clang::BuiltinType::UnresolvedTemplate:
5068 break;
5069
5070 // AMD GPU builtin types.
5071#define AMDGPU_TYPE(Name, Id, SingletonId) case clang::BuiltinType::Id:
5072#include "clang/Basic/AMDGPUTypes.def"
5073 break;
5074 }
5075 break;
5076 // All pointer types are represented as unsigned integer encodings. We may
5077 // nee to add a eEncodingPointer if we ever need to know the difference
5078 case clang::Type::ObjCObjectPointer:
5079 case clang::Type::BlockPointer:
5080 case clang::Type::Pointer:
5081 case clang::Type::LValueReference:
5082 case clang::Type::RValueReference:
5083 case clang::Type::MemberPointer:
5084 return lldb::eEncodingUint;
5085 case clang::Type::Complex: {
5087 if (qual_type->isComplexType())
5088 encoding = lldb::eEncodingIEEE754;
5089 else {
5090 const clang::ComplexType *complex_type =
5091 qual_type->getAsComplexIntegerType();
5092 if (complex_type)
5093 encoding = GetType(complex_type->getElementType()).GetEncoding(count);
5094 else
5095 encoding = lldb::eEncodingSint;
5096 }
5097 count = 2;
5098 return encoding;
5099 }
5100
5101 case clang::Type::ObjCInterface:
5102 break;
5103 case clang::Type::Record:
5104 break;
5105 case clang::Type::Enum:
5106 return qual_type->isUnsignedIntegerOrEnumerationType()
5109 case clang::Type::DependentSizedArray:
5110 case clang::Type::DependentSizedExtVector:
5111 case clang::Type::UnresolvedUsing:
5112 case clang::Type::Attributed:
5113 case clang::Type::BTFTagAttributed:
5114 case clang::Type::TemplateTypeParm:
5115 case clang::Type::SubstTemplateTypeParm:
5116 case clang::Type::SubstTemplateTypeParmPack:
5117 case clang::Type::InjectedClassName:
5118 case clang::Type::DependentName:
5119 case clang::Type::DependentTemplateSpecialization:
5120 case clang::Type::PackExpansion:
5121 case clang::Type::ObjCObject:
5122
5123 case clang::Type::TemplateSpecialization:
5124 case clang::Type::DeducedTemplateSpecialization:
5125 case clang::Type::Adjusted:
5126 case clang::Type::Pipe:
5127 break;
5128
5129 // pointer type decayed from an array or function type.
5130 case clang::Type::Decayed:
5131 break;
5132 case clang::Type::ObjCTypeParam:
5133 break;
5134
5135 case clang::Type::DependentAddressSpace:
5136 break;
5137 case clang::Type::MacroQualified:
5138 break;
5139
5140 case clang::Type::ConstantMatrix:
5141 case clang::Type::DependentSizedMatrix:
5142 break;
5143
5144 // We don't handle pack indexing yet
5145 case clang::Type::PackIndexing:
5146 break;
5147 }
5148 count = 0;
5150}
5151
5153 if (!type)
5154 return lldb::eFormatDefault;
5155
5156 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5157
5158 switch (qual_type->getTypeClass()) {
5159 case clang::Type::Atomic:
5160 case clang::Type::Auto:
5161 case clang::Type::CountAttributed:
5162 case clang::Type::Decltype:
5163 case clang::Type::Elaborated:
5164 case clang::Type::Paren:
5165 case clang::Type::Typedef:
5166 case clang::Type::TypeOf:
5167 case clang::Type::TypeOfExpr:
5168 case clang::Type::Using:
5169 llvm_unreachable("Handled in RemoveWrappingTypes!");
5170 case clang::Type::UnaryTransform:
5171 break;
5172
5173 case clang::Type::FunctionNoProto:
5174 case clang::Type::FunctionProto:
5175 break;
5176
5177 case clang::Type::IncompleteArray:
5178 case clang::Type::VariableArray:
5179 case clang::Type::ArrayParameter:
5180 break;
5181
5182 case clang::Type::ConstantArray:
5183 return lldb::eFormatVoid; // no value
5184
5185 case clang::Type::DependentVector:
5186 case clang::Type::ExtVector:
5187 case clang::Type::Vector:
5188 break;
5189
5190 case clang::Type::BitInt:
5191 case clang::Type::DependentBitInt:
5192 return qual_type->isUnsignedIntegerType() ? lldb::eFormatUnsigned
5194
5195 case clang::Type::Builtin:
5196 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5197 case clang::BuiltinType::UnknownAny:
5198 case clang::BuiltinType::Void:
5199 case clang::BuiltinType::BoundMember:
5200 break;
5201
5202 case clang::BuiltinType::Bool:
5203 return lldb::eFormatBoolean;
5204 case clang::BuiltinType::Char_S:
5205 case clang::BuiltinType::SChar:
5206 case clang::BuiltinType::WChar_S:
5207 case clang::BuiltinType::Char_U:
5208 case clang::BuiltinType::UChar:
5209 case clang::BuiltinType::WChar_U:
5210 return lldb::eFormatChar;
5211 case clang::BuiltinType::Char8:
5212 return lldb::eFormatUnicode8;
5213 case clang::BuiltinType::Char16:
5215 case clang::BuiltinType::Char32:
5217 case clang::BuiltinType::UShort:
5218 return lldb::eFormatUnsigned;
5219 case clang::BuiltinType::Short:
5220 return lldb::eFormatDecimal;
5221 case clang::BuiltinType::UInt:
5222 return lldb::eFormatUnsigned;
5223 case clang::BuiltinType::Int:
5224 return lldb::eFormatDecimal;
5225 case clang::BuiltinType::ULong:
5226 return lldb::eFormatUnsigned;
5227 case clang::BuiltinType::Long:
5228 return lldb::eFormatDecimal;
5229 case clang::BuiltinType::ULongLong:
5230 return lldb::eFormatUnsigned;
5231 case clang::BuiltinType::LongLong:
5232 return lldb::eFormatDecimal;
5233 case clang::BuiltinType::UInt128:
5234 return lldb::eFormatUnsigned;
5235 case clang::BuiltinType::Int128:
5236 return lldb::eFormatDecimal;
5237 case clang::BuiltinType::Half:
5238 case clang::BuiltinType::Float:
5239 case clang::BuiltinType::Double:
5240 case clang::BuiltinType::LongDouble:
5241 return lldb::eFormatFloat;
5242 default:
5243 return lldb::eFormatHex;
5244 }
5245 break;
5246 case clang::Type::ObjCObjectPointer:
5247 return lldb::eFormatHex;
5248 case clang::Type::BlockPointer:
5249 return lldb::eFormatHex;
5250 case clang::Type::Pointer:
5251 return lldb::eFormatHex;
5252 case clang::Type::LValueReference:
5253 case clang::Type::RValueReference:
5254 return lldb::eFormatHex;
5255 case clang::Type::MemberPointer:
5256 return lldb::eFormatHex;
5257 case clang::Type::Complex: {
5258 if (qual_type->isComplexType())
5259 return lldb::eFormatComplex;
5260 else
5262 }
5263 case clang::Type::ObjCInterface:
5264 break;
5265 case clang::Type::Record:
5266 break;
5267 case clang::Type::Enum:
5268 return lldb::eFormatEnum;
5269 case clang::Type::DependentSizedArray:
5270 case clang::Type::DependentSizedExtVector:
5271 case clang::Type::UnresolvedUsing:
5272 case clang::Type::Attributed:
5273 case clang::Type::BTFTagAttributed:
5274 case clang::Type::TemplateTypeParm:
5275 case clang::Type::SubstTemplateTypeParm:
5276 case clang::Type::SubstTemplateTypeParmPack:
5277 case clang::Type::InjectedClassName:
5278 case clang::Type::DependentName:
5279 case clang::Type::DependentTemplateSpecialization:
5280 case clang::Type::PackExpansion:
5281 case clang::Type::ObjCObject:
5282
5283 case clang::Type::TemplateSpecialization:
5284 case clang::Type::DeducedTemplateSpecialization:
5285 case clang::Type::Adjusted:
5286 case clang::Type::Pipe:
5287 break;
5288
5289 // pointer type decayed from an array or function type.
5290 case clang::Type::Decayed:
5291 break;
5292 case clang::Type::ObjCTypeParam:
5293 break;
5294
5295 case clang::Type::DependentAddressSpace:
5296 break;
5297 case clang::Type::MacroQualified:
5298 break;
5299
5300 // Matrix types we're not sure how to display yet.
5301 case clang::Type::ConstantMatrix:
5302 case clang::Type::DependentSizedMatrix:
5303 break;
5304
5305 // We don't handle pack indexing yet
5306 case clang::Type::PackIndexing:
5307 break;
5308 }
5309 // We don't know hot to display this type...
5310 return lldb::eFormatBytes;
5311}
5312
5313static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5314 bool check_superclass) {
5315 while (class_interface_decl) {
5316 if (class_interface_decl->ivar_size() > 0)
5317 return true;
5318
5319 if (check_superclass)
5320 class_interface_decl = class_interface_decl->getSuperClass();
5321 else
5322 break;
5323 }
5324 return false;
5325}
5326
5327static std::optional<SymbolFile::ArrayInfo>
5329 clang::QualType qual_type,
5330 const ExecutionContext *exe_ctx) {
5331 if (qual_type->isIncompleteArrayType())
5332 if (std::optional<ClangASTMetadata> metadata =
5333 ast.GetMetadata(qual_type.getTypePtr()))
5334 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5335 exe_ctx);
5336 return std::nullopt;
5337}
5338
5339llvm::Expected<uint32_t>
5341 bool omit_empty_base_classes,
5342 const ExecutionContext *exe_ctx) {
5343 if (!type)
5344 return llvm::createStringError("invalid clang type");
5345
5346 uint32_t num_children = 0;
5347 clang::QualType qual_type(RemoveWrappingTypes(GetQualType(type)));
5348 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5349 switch (type_class) {
5350 case clang::Type::Builtin:
5351 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5352 case clang::BuiltinType::ObjCId: // child is Class
5353 case clang::BuiltinType::ObjCClass: // child is Class
5354 num_children = 1;
5355 break;
5356
5357 default:
5358 break;
5359 }
5360 break;
5361
5362 case clang::Type::Complex:
5363 return 0;
5364 case clang::Type::Record:
5365 if (GetCompleteQualType(&getASTContext(), qual_type)) {
5366 const clang::RecordType *record_type =
5367 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5368 const clang::RecordDecl *record_decl = record_type->getDecl();
5369 assert(record_decl);
5370 const clang::CXXRecordDecl *cxx_record_decl =
5371 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5372 if (cxx_record_decl) {
5373 if (omit_empty_base_classes) {
5374 // Check each base classes to see if it or any of its base classes
5375 // contain any fields. This can help limit the noise in variable
5376 // views by not having to show base classes that contain no members.
5377 clang::CXXRecordDecl::base_class_const_iterator base_class,
5378 base_class_end;
5379 for (base_class = cxx_record_decl->bases_begin(),
5380 base_class_end = cxx_record_decl->bases_end();
5381 base_class != base_class_end; ++base_class) {
5382 const clang::CXXRecordDecl *base_class_decl =
5383 llvm::cast<clang::CXXRecordDecl>(
5384 base_class->getType()
5385 ->getAs<clang::RecordType>()
5386 ->getDecl());
5387
5388 // Skip empty base classes
5389 if (!TypeSystemClang::RecordHasFields(base_class_decl))
5390 continue;
5391
5392 num_children++;
5393 }
5394 } else {
5395 // Include all base classes
5396 num_children += cxx_record_decl->getNumBases();
5397 }
5398 }
5399 num_children += std::distance(record_decl->field_begin(),
5400 record_decl->field_end());
5401 } else
5402 return llvm::createStringError(
5403 "incomplete type \"" + GetDisplayTypeName(type).GetString() + "\"");
5404 break;
5405 case clang::Type::ObjCObject:
5406 case clang::Type::ObjCInterface:
5407 if (GetCompleteQualType(&getASTContext(), qual_type)) {
5408 const clang::ObjCObjectType *objc_class_type =
5409 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5410 assert(objc_class_type);
5411 if (objc_class_type) {
5412 clang::ObjCInterfaceDecl *class_interface_decl =
5413 objc_class_type->getInterface();
5414
5415 if (class_interface_decl) {
5416
5417 clang::ObjCInterfaceDecl *superclass_interface_decl =
5418 class_interface_decl->getSuperClass();
5419 if (superclass_interface_decl) {
5420 if (omit_empty_base_classes) {
5421 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5422 ++num_children;
5423 } else
5424 ++num_children;
5425 }
5426
5427 num_children += class_interface_decl->ivar_size();
5428 }
5429 }
5430 }
5431 break;
5432
5433 case clang::Type::LValueReference:
5434 case clang::Type::RValueReference:
5435 case clang::Type::ObjCObjectPointer: {
5436 CompilerType pointee_clang_type(GetPointeeType(type));
5437
5438 uint32_t num_pointee_children = 0;
5439 if (pointee_clang_type.IsAggregateType()) {
5440 auto num_children_or_err =
5441 pointee_clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
5442 if (!num_children_or_err)
5443 return num_children_or_err;
5444 num_pointee_children = *num_children_or_err;
5445 }
5446 // If this type points to a simple type, then it has 1 child
5447 if (num_pointee_children == 0)
5448 num_children = 1;
5449 else
5450 num_children = num_pointee_children;
5451 } break;
5452
5453 case clang::Type::Vector:
5454 case clang::Type::ExtVector:
5455 num_children =
5456 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5457 break;
5458
5459 case clang::Type::ConstantArray:
5460 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5461 ->getSize()
5462 .getLimitedValue();
5463 break;
5464 case clang::Type::IncompleteArray:
5465 if (auto array_info =
5466 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5467 // FIXME: Only 1-dimensional arrays are supported.
5468 num_children = array_info->element_orders.size()
5469 ? array_info->element_orders.back().value_or(0)
5470 : 0;
5471 break;
5472
5473 case clang::Type::Pointer: {
5474 const clang::PointerType *pointer_type =
5475 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5476 clang::QualType pointee_type(pointer_type->getPointeeType());
5477 CompilerType pointee_clang_type(GetType(pointee_type));
5478 uint32_t num_pointee_children = 0;
5479 if (pointee_clang_type.IsAggregateType()) {
5480 auto num_children_or_err =
5481 pointee_clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
5482 if (!num_children_or_err)
5483 return num_children_or_err;
5484 num_pointee_children = *num_children_or_err;
5485 }
5486 if (num_pointee_children == 0) {
5487 // We have a pointer to a pointee type that claims it has no children. We
5488 // will want to look at
5489 num_children = GetNumPointeeChildren(pointee_type);
5490 } else
5491 num_children = num_pointee_children;
5492 } break;
5493
5494 default:
5495 break;
5496 }
5497 return num_children;
5498}
5499
5502}
5503
5506 if (type) {
5507 clang::QualType qual_type(GetQualType(type));
5508 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5509 if (type_class == clang::Type::Builtin) {
5510 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5511 case clang::BuiltinType::Void:
5512 return eBasicTypeVoid;
5513 case clang::BuiltinType::Bool:
5514 return eBasicTypeBool;
5515 case clang::BuiltinType::Char_S:
5516 return eBasicTypeSignedChar;
5517 case clang::BuiltinType::Char_U:
5519 case clang::BuiltinType::Char8:
5520 return eBasicTypeChar8;
5521 case clang::BuiltinType::Char16:
5522 return eBasicTypeChar16;
5523 case clang::BuiltinType::Char32:
5524 return eBasicTypeChar32;
5525 case clang::BuiltinType::UChar:
5527 case clang::BuiltinType::SChar:
5528 return eBasicTypeSignedChar;
5529 case clang::BuiltinType::WChar_S:
5530 return eBasicTypeSignedWChar;
5531 case clang::BuiltinType::WChar_U:
5533 case clang::BuiltinType::Short:
5534 return eBasicTypeShort;
5535 case clang::BuiltinType::UShort:
5537 case clang::BuiltinType::Int:
5538 return eBasicTypeInt;
5539 case clang::BuiltinType::UInt:
5540 return eBasicTypeUnsignedInt;
5541 case clang::BuiltinType::Long:
5542 return eBasicTypeLong;
5543 case clang::BuiltinType::ULong:
5545 case clang::BuiltinType::LongLong:
5546 return eBasicTypeLongLong;
5547 case clang::BuiltinType::ULongLong:
5549 case clang::BuiltinType::Int128:
5550 return eBasicTypeInt128;
5551 case clang::BuiltinType::UInt128:
5553
5554 case clang::BuiltinType::Half:
5555 return eBasicTypeHalf;
5556 case clang::BuiltinType::Float:
5557 return eBasicTypeFloat;
5558 case clang::BuiltinType::Double:
5559 return eBasicTypeDouble;
5560 case clang::BuiltinType::LongDouble:
5561 return eBasicTypeLongDouble;
5562
5563 case clang::BuiltinType::NullPtr:
5564 return eBasicTypeNullPtr;
5565 case clang::BuiltinType::ObjCId:
5566 return eBasicTypeObjCID;
5567 case clang::BuiltinType::ObjCClass:
5568 return eBasicTypeObjCClass;
5569 case clang::BuiltinType::ObjCSel:
5570 return eBasicTypeObjCSel;
5571 default:
5572 return eBasicTypeOther;
5573 }
5574 }
5575 }
5576 return eBasicTypeInvalid;
5577}
5578
5581 std::function<bool(const CompilerType &integer_type,
5582 ConstString name,
5583 const llvm::APSInt &value)> const &callback) {
5584 const clang::EnumType *enum_type =
5585 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5586 if (enum_type) {
5587 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5588 if (enum_decl) {
5589 CompilerType integer_type = GetType(enum_decl->getIntegerType());
5590
5591 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5592 for (enum_pos = enum_decl->enumerator_begin(),
5593 enum_end_pos = enum_decl->enumerator_end();
5594 enum_pos != enum_end_pos; ++enum_pos) {
5595 ConstString name(enum_pos->getNameAsString().c_str());
5596 if (!callback(integer_type, name, enum_pos->getInitVal()))
5597 break;
5598 }
5599 }
5600 }
5601}
5602
5603#pragma mark Aggregate Types
5604
5606 if (!type)
5607 return 0;
5608
5609 uint32_t count = 0;
5610 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
5611 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5612 switch (type_class) {
5613 case clang::Type::Record:
5614 if (GetCompleteType(type)) {
5615 const clang::RecordType *record_type =
5616 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5617 if (record_type) {
5618 clang::RecordDecl *record_decl = record_type->getDecl();
5619 if (record_decl) {
5620 count = std::distance(record_decl->field_begin(),
5621 record_decl->field_end());
5622 }
5623 }
5624 }
5625 break;
5626
5627 case clang::Type::ObjCObjectPointer: {
5628 const clang::ObjCObjectPointerType *objc_class_type =
5629 qual_type->castAs<clang::ObjCObjectPointerType>();
5630 const clang::ObjCInterfaceType *objc_interface_type =
5631 objc_class_type->getInterfaceType();
5632 if (objc_interface_type &&
5634 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5635 clang::ObjCInterfaceDecl *class_interface_decl =
5636 objc_interface_type->getDecl();
5637 if (class_interface_decl) {
5638 count = class_interface_decl->ivar_size();
5639 }
5640 }
5641 break;
5642 }
5643
5644 case clang::Type::ObjCObject:
5645 case clang::Type::ObjCInterface:
5646 if (GetCompleteType(type)) {
5647 const clang::ObjCObjectType *objc_class_type =
5648 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5649 if (objc_class_type) {
5650 clang::ObjCInterfaceDecl *class_interface_decl =
5651 objc_class_type->getInterface();
5652
5653 if (class_interface_decl)
5654 count = class_interface_decl->ivar_size();
5655 }
5656 }
5657 break;
5658
5659 default:
5660 break;
5661 }
5662 return count;
5663}
5664
5666GetObjCFieldAtIndex(clang::ASTContext *ast,
5667 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5668 std::string &name, uint64_t *bit_offset_ptr,
5669 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5670 if (class_interface_decl) {
5671 if (idx < (class_interface_decl->ivar_size())) {
5672 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5673 ivar_end = class_interface_decl->ivar_end();
5674 uint32_t ivar_idx = 0;
5675
5676 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5677 ++ivar_pos, ++ivar_idx) {
5678 if (ivar_idx == idx) {
5679 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5680
5681 clang::QualType ivar_qual_type(ivar_decl->getType());
5682
5683 name.assign(ivar_decl->getNameAsString());
5684
5685 if (bit_offset_ptr) {
5686 const clang::ASTRecordLayout &interface_layout =
5687 ast->getASTObjCInterfaceLayout(class_interface_decl);
5688 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5689 }
5690
5691 const bool is_bitfield = ivar_pos->isBitField();
5692
5693 if (bitfield_bit_size_ptr) {
5694 *bitfield_bit_size_ptr = 0;
5695
5696 if (is_bitfield && ast) {
5697 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5698 clang::Expr::EvalResult result;
5699 if (bitfield_bit_size_expr &&
5700 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5701 llvm::APSInt bitfield_apsint = result.Val.getInt();
5702 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5703 }
5704 }
5705 }
5706 if (is_bitfield_ptr)
5707 *is_bitfield_ptr = is_bitfield;
5708
5709 return ivar_qual_type.getAsOpaquePtr();
5710 }
5711 }
5712 }
5713 }
5714 return nullptr;
5715}
5716
5718 size_t idx, std::string &name,
5719 uint64_t *bit_offset_ptr,
5720 uint32_t *bitfield_bit_size_ptr,
5721 bool *is_bitfield_ptr) {
5722 if (!type)
5723 return CompilerType();
5724
5725 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
5726 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5727 switch (type_class) {
5728 case clang::Type::Record:
5729 if (GetCompleteType(type)) {
5730 const clang::RecordType *record_type =
5731 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5732 const clang::RecordDecl *record_decl = record_type->getDecl();
5733 uint32_t field_idx = 0;
5734 clang::RecordDecl::field_iterator field, field_end;
5735 for (field = record_decl->field_begin(),
5736 field_end = record_decl->field_end();
5737 field != field_end; ++field, ++field_idx) {
5738 if (idx == field_idx) {
5739 // Print the member type if requested
5740 // Print the member name and equal sign
5741 name.assign(field->getNameAsString());
5742
5743 // Figure out the type byte size (field_type_info.first) and
5744 // alignment (field_type_info.second) from the AST context.
5745 if (bit_offset_ptr) {
5746 const clang::ASTRecordLayout &record_layout =
5747 getASTContext().getASTRecordLayout(record_decl);
5748 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5749 }
5750
5751 const bool is_bitfield = field->isBitField();
5752
5753 if (bitfield_bit_size_ptr) {
5754 *bitfield_bit_size_ptr = 0;
5755
5756 if (is_bitfield) {
5757 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5758 clang::Expr::EvalResult result;
5759 if (bitfield_bit_size_expr &&
5760 bitfield_bit_size_expr->EvaluateAsInt(result,
5761 getASTContext())) {
5762 llvm::APSInt bitfield_apsint = result.Val.getInt();
5763 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5764 }
5765 }
5766 }
5767 if (is_bitfield_ptr)
5768 *is_bitfield_ptr = is_bitfield;
5769
5770 return GetType(field->getType());
5771 }
5772 }
5773 }
5774 break;
5775
5776 case clang::Type::ObjCObjectPointer: {
5777 const clang::ObjCObjectPointerType *objc_class_type =
5778 qual_type->castAs<clang::ObjCObjectPointerType>();
5779 const clang::ObjCInterfaceType *objc_interface_type =
5780 objc_class_type->getInterfaceType();
5781 if (objc_interface_type &&
5783 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5784 clang::ObjCInterfaceDecl *class_interface_decl =
5785 objc_interface_type->getDecl();
5786 if (class_interface_decl) {
5787 return CompilerType(
5788 weak_from_this(),
5789 GetObjCFieldAtIndex(&getASTContext(), class_interface_decl, idx,
5790 name, bit_offset_ptr, bitfield_bit_size_ptr,
5791 is_bitfield_ptr));
5792 }
5793 }
5794 break;
5795 }
5796
5797 case clang::Type::ObjCObject:
5798 case clang::Type::ObjCInterface:
5799 if (GetCompleteType(type)) {
5800 const clang::ObjCObjectType *objc_class_type =
5801 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5802 assert(objc_class_type);
5803 if (objc_class_type) {
5804 clang::ObjCInterfaceDecl *class_interface_decl =
5805 objc_class_type->getInterface();
5806 return CompilerType(
5807 weak_from_this(),
5808 GetObjCFieldAtIndex(&getASTContext(), class_interface_decl, idx,
5809 name, bit_offset_ptr, bitfield_bit_size_ptr,
5810 is_bitfield_ptr));
5811 }
5812 }
5813 break;
5814
5815 default:
5816 break;
5817 }
5818 return CompilerType();
5819}
5820
5821uint32_t
5823 uint32_t count = 0;
5824 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5825 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5826 switch (type_class) {
5827 case clang::Type::Record:
5828 if (GetCompleteType(type)) {
5829 const clang::CXXRecordDecl *cxx_record_decl =
5830 qual_type->getAsCXXRecordDecl();
5831 if (cxx_record_decl)
5832 count = cxx_record_decl->getNumBases();
5833 }
5834 break;
5835
5836 case clang::Type::ObjCObjectPointer:
5838 break;
5839
5840 case clang::Type::ObjCObject:
5841 if (GetCompleteType(type)) {
5842 const clang::ObjCObjectType *objc_class_type =
5843 qual_type->getAsObjCQualifiedInterfaceType();
5844 if (objc_class_type) {
5845 clang::ObjCInterfaceDecl *class_interface_decl =
5846 objc_class_type->getInterface();
5847
5848 if (class_interface_decl && class_interface_decl->getSuperClass())
5849 count = 1;
5850 }
5851 }
5852 break;
5853 case clang::Type::ObjCInterface:
5854 if (GetCompleteType(type)) {
5855 const clang::ObjCInterfaceType *objc_interface_type =
5856 qual_type->getAs<clang::ObjCInterfaceType>();
5857 if (objc_interface_type) {
5858 clang::ObjCInterfaceDecl *class_interface_decl =
5859 objc_interface_type->getInterface();
5860
5861 if (class_interface_decl && class_interface_decl->getSuperClass())
5862 count = 1;
5863 }
5864 }
5865 break;
5866
5867 default:
5868 break;
5869 }
5870 return count;
5871}
5872
5873uint32_t
5875 uint32_t count = 0;
5876 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5877 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5878 switch (type_class) {
5879 case clang::Type::Record:
5880 if (GetCompleteType(type)) {
5881 const clang::CXXRecordDecl *cxx_record_decl =
5882 qual_type->getAsCXXRecordDecl();
5883 if (cxx_record_decl)
5884 count = cxx_record_decl->getNumVBases();
5885 }
5886 break;
5887
5888 default:
5889 break;
5890 }
5891 return count;
5892}
5893
5895 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5896 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5897 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5898 switch (type_class) {
5899 case clang::Type::Record:
5900 if (GetCompleteType(type)) {
5901 const clang::CXXRecordDecl *cxx_record_decl =
5902 qual_type->getAsCXXRecordDecl();
5903 if (cxx_record_decl) {
5904 uint32_t curr_idx = 0;
5905 clang::CXXRecordDecl::base_class_const_iterator base_class,
5906 base_class_end;
5907 for (base_class = cxx_record_decl->bases_begin(),
5908 base_class_end = cxx_record_decl->bases_end();
5909 base_class != base_class_end; ++base_class, ++curr_idx) {
5910 if (curr_idx == idx) {
5911 if (bit_offset_ptr) {
5912 const clang::ASTRecordLayout &record_layout =
5913 getASTContext().getASTRecordLayout(cxx_record_decl);
5914 const clang::CXXRecordDecl *base_class_decl =
5915 llvm::cast<clang::CXXRecordDecl>(
5916 base_class->getType()
5917 ->castAs<clang::RecordType>()
5918 ->getDecl());
5919 if (base_class->isVirtual())
5920 *bit_offset_ptr =
5921 record_layout.getVBaseClassOffset(base_class_decl)
5922 .getQuantity() *
5923 8;
5924 else
5925 *bit_offset_ptr =
5926 record_layout.getBaseClassOffset(base_class_decl)
5927 .getQuantity() *
5928 8;
5929 }
5930 return GetType(base_class->getType());
5931 }
5932 }
5933 }
5934 }
5935 break;
5936
5937 case clang::Type::ObjCObjectPointer:
5938 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
5939
5940 case clang::Type::ObjCObject:
5941 if (idx == 0 && GetCompleteType(type)) {
5942 const clang::ObjCObjectType *objc_class_type =
5943 qual_type->getAsObjCQualifiedInterfaceType();
5944 if (objc_class_type) {
5945 clang::ObjCInterfaceDecl *class_interface_decl =
5946 objc_class_type->getInterface();
5947
5948 if (class_interface_decl) {
5949 clang::ObjCInterfaceDecl *superclass_interface_decl =
5950 class_interface_decl->getSuperClass();
5951 if (superclass_interface_decl) {
5952 if (bit_offset_ptr)
5953 *bit_offset_ptr = 0;
5954 return GetType(getASTContext().getObjCInterfaceType(
5955 superclass_interface_decl));
5956 }
5957 }
5958 }
5959 }
5960 break;
5961 case clang::Type::ObjCInterface:
5962 if (idx == 0 && GetCompleteType(type)) {
5963 const clang::ObjCObjectType *objc_interface_type =
5964 qual_type->getAs<clang::ObjCInterfaceType>();
5965 if (objc_interface_type) {
5966 clang::ObjCInterfaceDecl *class_interface_decl =
5967 objc_interface_type->getInterface();
5968
5969 if (class_interface_decl) {
5970 clang::ObjCInterfaceDecl *superclass_interface_decl =
5971 class_interface_decl->getSuperClass();
5972 if (superclass_interface_decl) {
5973 if (bit_offset_ptr)
5974 *bit_offset_ptr = 0;
5975 return GetType(getASTContext().getObjCInterfaceType(
5976 superclass_interface_decl));
5977 }
5978 }
5979 }
5980 }
5981 break;
5982
5983 default:
5984 break;
5985 }
5986 return CompilerType();
5987}
5988
5990 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5991 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5992 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5993 switch (type_class) {
5994 case clang::Type::Record:
5995 if (GetCompleteType(type)) {
5996 const clang::CXXRecordDecl *cxx_record_decl =
5997 qual_type->getAsCXXRecordDecl();
5998 if (cxx_record_decl) {
5999 uint32_t curr_idx = 0;
6000 clang::CXXRecordDecl::base_class_const_iterator base_class,
6001 base_class_end;
6002 for (base_class = cxx_record_decl->vbases_begin(),
6003 base_class_end = cxx_record_decl->vbases_end();
6004 base_class != base_class_end; ++base_class, ++curr_idx) {
6005 if (curr_idx == idx) {
6006 if (bit_offset_ptr) {
6007 const clang::ASTRecordLayout &record_layout =
6008 getASTContext().getASTRecordLayout(cxx_record_decl);
6009 const clang::CXXRecordDecl *base_class_decl =
6010 llvm::cast<clang::CXXRecordDecl>(
6011 base_class->getType()
6012 ->castAs<clang::RecordType>()
6013 ->getDecl());
6014 *bit_offset_ptr =
6015 record_layout.getVBaseClassOffset(base_class_decl)
6016 .getQuantity() *
6017 8;
6018 }
6019 return GetType(base_class->getType());
6020 }
6021 }
6022 }
6023 }
6024 break;
6025
6026 default:
6027 break;
6028 }
6029 return CompilerType();
6030}
6031
6034 llvm::StringRef name) {
6035 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
6036 switch (qual_type->getTypeClass()) {
6037 case clang::Type::Record: {
6038 if (!GetCompleteType(type))
6039 return CompilerDecl();
6040
6041 const clang::RecordType *record_type =
6042 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6043 const clang::RecordDecl *record_decl = record_type->getDecl();
6044
6045 clang::DeclarationName decl_name(&getASTContext().Idents.get(name));
6046 for (NamedDecl *decl : record_decl->lookup(decl_name)) {
6047 auto *var_decl = dyn_cast<clang::VarDecl>(decl);
6048 if (!var_decl || var_decl->getStorageClass() != clang::SC_Static)
6049 continue;
6050
6051 return CompilerDecl(this, var_decl);
6052 }
6053 break;
6054 }
6055
6056 default:
6057 break;
6058 }
6059 return CompilerDecl();
6060}
6061
6062// If a pointer to a pointee type (the clang_type arg) says that it has no
6063// children, then we either need to trust it, or override it and return a
6064// different result. For example, an "int *" has one child that is an integer,
6065// but a function pointer doesn't have any children. Likewise if a Record type
6066// claims it has no children, then there really is nothing to show.
6067uint32_t TypeSystemClang::GetNumPointeeChildren(clang::QualType type) {
6068 if (type.isNull())
6069 return 0;
6070
6071 clang::QualType qual_type = RemoveWrappingTypes(type.getCanonicalType());
6072 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6073 switch (type_class) {
6074 case clang::Type::Builtin:
6075 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6076 case clang::BuiltinType::UnknownAny:
6077 case clang::BuiltinType::Void:
6078 case clang::BuiltinType::NullPtr:
6079 case clang::BuiltinType::OCLEvent:
6080 case clang::BuiltinType::OCLImage1dRO:
6081 case clang::BuiltinType::OCLImage1dWO:
6082 case clang::BuiltinType::OCLImage1dRW:
6083 case clang::BuiltinType::OCLImage1dArrayRO:
6084 case clang::BuiltinType::OCLImage1dArrayWO:
6085 case clang::BuiltinType::OCLImage1dArrayRW:
6086 case clang::BuiltinType::OCLImage1dBufferRO:
6087 case clang::BuiltinType::OCLImage1dBufferWO:
6088 case clang::BuiltinType::OCLImage1dBufferRW:
6089 case clang::BuiltinType::OCLImage2dRO:
6090 case clang::BuiltinType::OCLImage2dWO:
6091 case clang::BuiltinType::OCLImage2dRW:
6092 case clang::BuiltinType::OCLImage2dArrayRO:
6093 case clang::BuiltinType::OCLImage2dArrayWO:
6094 case clang::BuiltinType::OCLImage2dArrayRW:
6095 case clang::BuiltinType::OCLImage3dRO:
6096 case clang::BuiltinType::OCLImage3dWO:
6097 case clang::BuiltinType::OCLImage3dRW:
6098 case clang::BuiltinType::OCLSampler:
6099 case clang::BuiltinType::HLSLResource:
6100 return 0;
6101 case clang::BuiltinType::Bool:
6102 case clang::BuiltinType::Char_U:
6103 case clang::BuiltinType::UChar:
6104 case clang::BuiltinType::WChar_U:
6105 case clang::BuiltinType::Char16:
6106 case clang::BuiltinType::Char32:
6107 case clang::BuiltinType::UShort:
6108 case clang::BuiltinType::UInt:
6109 case clang::BuiltinType::ULong:
6110 case clang::BuiltinType::ULongLong:
6111 case clang::BuiltinType::UInt128:
6112 case clang::BuiltinType::Char_S:
6113 case clang::BuiltinType::SChar:
6114 case clang::BuiltinType::WChar_S:
6115 case clang::BuiltinType::Short:
6116 case clang::BuiltinType::Int:
6117 case clang::BuiltinType::Long:
6118 case clang::BuiltinType::LongLong:
6119 case clang::BuiltinType::Int128:
6120 case clang::BuiltinType::Float:
6121 case clang::BuiltinType::Double:
6122 case clang::BuiltinType::LongDouble:
6123 case clang::BuiltinType::Dependent:
6124 case clang::BuiltinType::Overload:
6125 case clang::BuiltinType::ObjCId:
6126 case clang::BuiltinType::ObjCClass:
6127 case clang::BuiltinType::ObjCSel:
6128 case clang::BuiltinType::BoundMember:
6129 case clang::BuiltinType::Half:
6130 case clang::BuiltinType::ARCUnbridgedCast:
6131 case clang::BuiltinType::PseudoObject:
6132 case clang::BuiltinType::BuiltinFn:
6133 case clang::BuiltinType::ArraySection:
6134 return 1;
6135 default:
6136 return 0;
6137 }
6138 break;
6139
6140 case clang::Type::Complex:
6141 return 1;
6142 case clang::Type::Pointer:
6143 return 1;
6144 case clang::Type::BlockPointer:
6145 return 0; // If block pointers don't have debug info, then no children for
6146 // them
6147 case clang::Type::LValueReference:
6148 return 1;
6149 case clang::Type::RValueReference:
6150 return 1;
6151 case clang::Type::MemberPointer:
6152 return 0;
6153 case clang::Type::ConstantArray:
6154 return 0;
6155 case clang::Type::IncompleteArray:
6156 return 0;
6157 case clang::Type::VariableArray:
6158 return 0;
6159 case clang::Type::DependentSizedArray:
6160 return 0;
6161 case clang::Type::DependentSizedExtVector:
6162 return 0;
6163 case clang::Type::Vector:
6164 return 0;
6165 case clang::Type::ExtVector:
6166 return 0;
6167 case clang::Type::FunctionProto:
6168 return 0; // When we function pointers, they have no children...
6169 case clang::Type::FunctionNoProto:
6170 return 0; // When we function pointers, they have no children...
6171 case clang::Type::UnresolvedUsing:
6172 return 0;
6173 case clang::Type::Record:
6174 return 0;
6175 case clang::Type::Enum:
6176 return 1;
6177 case clang::Type::TemplateTypeParm:
6178 return 1;
6179 case clang::Type::SubstTemplateTypeParm:
6180 return 1;
6181 case clang::Type::TemplateSpecialization:
6182 return 1;
6183 case clang::Type::InjectedClassName:
6184 return 0;
6185 case clang::Type::DependentName:
6186 return 1;
6187 case clang::Type::DependentTemplateSpecialization:
6188 return 1;
6189 case clang::Type::ObjCObject:
6190 return 0;
6191 case clang::Type::ObjCInterface:
6192 return 0;
6193 case clang::Type::ObjCObjectPointer:
6194 return 1;
6195 default:
6196 break;
6197 }
6198 return 0;
6199}
6200
6202 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6203 bool transparent_pointers, bool omit_empty_base_classes,
6204 bool ignore_array_bounds, std::string &child_name,
6205 uint32_t &child_byte_size, int32_t &child_byte_offset,
6206 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6207 bool &child_is_base_class, bool &child_is_deref_of_parent,
6208 ValueObject *valobj, uint64_t &language_flags) {
6209 if (!type)
6210 return CompilerType();
6211
6212 auto get_exe_scope = [&exe_ctx]() {
6213 return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr;
6214 };
6215
6216 clang::QualType parent_qual_type(
6218 const clang::Type::TypeClass parent_type_class =
6219 parent_qual_type->getTypeClass();
6220 child_bitfield_bit_size = 0;
6221 child_bitfield_bit_offset = 0;
6222 child_is_base_class = false;
6223 language_flags = 0;
6224
6225 auto num_children_or_err =
6226 GetNumChildren(type, omit_empty_base_classes, exe_ctx);
6227 if (!num_children_or_err)
6228 return num_children_or_err.takeError();
6229
6230 const bool idx_is_valid = idx < *num_children_or_err;
6231 int32_t bit_offset;
6232 switch (parent_type_class) {
6233 case clang::Type::Builtin:
6234 if (idx_is_valid) {
6235 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6236 case clang::BuiltinType::ObjCId:
6237 case clang::BuiltinType::ObjCClass:
6238 child_name = "isa";
6239 child_byte_size =
6240 getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy) /
6241 CHAR_BIT;
6242 return GetType(getASTContext().ObjCBuiltinClassTy);
6243
6244 default:
6245 break;
6246 }
6247 }
6248 break;
6249
6250 case clang::Type::Record:
6251 if (idx_is_valid && GetCompleteType(type)) {
6252 const clang::RecordType *record_type =
6253 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6254 const clang::RecordDecl *record_decl = record_type->getDecl();
6255 assert(record_decl);
6256 const clang::ASTRecordLayout &record_layout =
6257 getASTContext().getASTRecordLayout(record_decl);
6258 uint32_t child_idx = 0;
6259
6260 const clang::CXXRecordDecl *cxx_record_decl =
6261 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6262 if (cxx_record_decl) {
6263 // We might have base classes to print out first
6264 clang::CXXRecordDecl::base_class_const_iterator base_class,
6265 base_class_end;
6266 for (base_class = cxx_record_decl->bases_begin(),
6267 base_class_end = cxx_record_decl->bases_end();
6268 base_class != base_class_end; ++base_class) {
6269 const clang::CXXRecordDecl *base_class_decl = nullptr;
6270
6271 // Skip empty base classes
6272 if (omit_empty_base_classes) {
6273 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6274 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6275 if (!TypeSystemClang::RecordHasFields(base_class_decl))
6276 continue;
6277 }
6278
6279 if (idx == child_idx) {
6280 if (base_class_decl == nullptr)
6281 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6282 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6283
6284 if (base_class->isVirtual()) {
6285 bool handled = false;
6286 if (valobj) {
6287 clang::VTableContextBase *vtable_ctx =
6288 getASTContext().getVTableContext();
6289 if (vtable_ctx)
6290 handled = GetVBaseBitOffset(*vtable_ctx, *valobj,
6291 record_layout, cxx_record_decl,
6292 base_class_decl, bit_offset);
6293 }
6294 if (!handled)
6295 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6296 .getQuantity() *
6297 8;
6298 } else
6299 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6300 .getQuantity() *
6301 8;
6302
6303 // Base classes should be a multiple of 8 bits in size
6304 child_byte_offset = bit_offset / 8;
6305 CompilerType base_class_clang_type = GetType(base_class->getType());
6306 child_name = base_class_clang_type.GetTypeName().AsCString("");
6307 std::optional<uint64_t> size =
6308 base_class_clang_type.GetBitSize(get_exe_scope());
6309 if (!size)
6310 return llvm::createStringError("no size info for base class");
6311
6312 uint64_t base_class_clang_type_bit_size = *size;
6313
6314 // Base classes bit sizes should be a multiple of 8 bits in size
6315 assert(base_class_clang_type_bit_size % 8 == 0);
6316 child_byte_size = base_class_clang_type_bit_size / 8;
6317 child_is_base_class = true;
6318 return base_class_clang_type;
6319 }
6320 // We don't increment the child index in the for loop since we might
6321 // be skipping empty base classes
6322 ++child_idx;
6323 }
6324 }
6325 // Make sure index is in range...
6326 uint32_t field_idx = 0;
6327 clang::RecordDecl::field_iterator field, field_end;
6328 for (field = record_decl->field_begin(),
6329 field_end = record_decl->field_end();
6330 field != field_end; ++field, ++field_idx, ++child_idx) {
6331 if (idx == child_idx) {
6332 // Print the member type if requested
6333 // Print the member name and equal sign
6334 child_name.assign(field->getNameAsString());
6335
6336 // Figure out the type byte size (field_type_info.first) and
6337 // alignment (field_type_info.second) from the AST context.
6338 CompilerType field_clang_type = GetType(field->getType());
6339 assert(field_idx < record_layout.getFieldCount());
6340 std::optional<uint64_t> size =
6341 field_clang_type.GetByteSize(get_exe_scope());
6342 if (!size)
6343 return llvm::createStringError("no size info for field");
6344
6345 child_byte_size = *size;
6346 const uint32_t child_bit_size = child_byte_size * 8;
6347
6348 // Figure out the field offset within the current struct/union/class
6349 // type
6350 bit_offset = record_layout.getFieldOffset(field_idx);
6351 if (FieldIsBitfield(*field, child_bitfield_bit_size)) {
6352 child_bitfield_bit_offset = bit_offset % child_bit_size;
6353 const uint32_t child_bit_offset =
6354 bit_offset - child_bitfield_bit_offset;
6355 child_byte_offset = child_bit_offset / 8;
6356 } else {
6357 child_byte_offset = bit_offset / 8;
6358 }
6359
6360 return field_clang_type;
6361 }
6362 }
6363 }
6364 break;
6365
6366 case clang::Type::ObjCObject:
6367 case clang::Type::ObjCInterface:
6368 if (idx_is_valid && GetCompleteType(type)) {
6369 const clang::ObjCObjectType *objc_class_type =
6370 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6371 assert(objc_class_type);
6372 if (objc_class_type) {
6373 uint32_t child_idx = 0;
6374 clang::ObjCInterfaceDecl *class_interface_decl =
6375 objc_class_type->getInterface();
6376
6377 if (class_interface_decl) {
6378
6379 const clang::ASTRecordLayout &interface_layout =
6380 getASTContext().getASTObjCInterfaceLayout(class_interface_decl);
6381 clang::ObjCInterfaceDecl *superclass_interface_decl =
6382 class_interface_decl->getSuperClass();
6383 if (superclass_interface_decl) {
6384 if (omit_empty_base_classes) {
6385 CompilerType base_class_clang_type =
6386 GetType(getASTContext().getObjCInterfaceType(
6387 superclass_interface_decl));
6388 if (llvm::expectedToStdOptional(
6389 base_class_clang_type.GetNumChildren(
6390 omit_empty_base_classes, exe_ctx))
6391 .value_or(0) > 0) {
6392 if (idx == 0) {
6393 clang::QualType ivar_qual_type(
6394 getASTContext().getObjCInterfaceType(
6395 superclass_interface_decl));
6396
6397 child_name.assign(
6398 superclass_interface_decl->getNameAsString());
6399
6400 clang::TypeInfo ivar_type_info =
6401 getASTContext().getTypeInfo(ivar_qual_type.getTypePtr());
6402
6403 child_byte_size = ivar_type_info.Width / 8;
6404 child_byte_offset = 0;
6405 child_is_base_class = true;
6406
6407 return GetType(ivar_qual_type);
6408 }
6409
6410 ++child_idx;
6411 }
6412 } else
6413 ++child_idx;
6414 }
6415
6416 const uint32_t superclass_idx = child_idx;
6417
6418 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6419 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6420 ivar_end = class_interface_decl->ivar_end();
6421
6422 for (ivar_pos = class_interface_decl->ivar_begin();
6423 ivar_pos != ivar_end; ++ivar_pos) {
6424 if (child_idx == idx) {
6425 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6426
6427 clang::QualType ivar_qual_type(ivar_decl->getType());
6428
6429 child_name.assign(ivar_decl->getNameAsString());
6430
6431 clang::TypeInfo ivar_type_info =
6432 getASTContext().getTypeInfo(ivar_qual_type.getTypePtr());
6433
6434 child_byte_size = ivar_type_info.Width / 8;
6435
6436 // Figure out the field offset within the current
6437 // struct/union/class type For ObjC objects, we can't trust the
6438 // bit offset we get from the Clang AST, since that doesn't
6439 // account for the space taken up by unbacked properties, or
6440 // from the changing size of base classes that are newer than
6441 // this class. So if we have a process around that we can ask
6442 // about this object, do so.
6443 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6444 Process *process = nullptr;
6445 if (exe_ctx)
6446 process = exe_ctx->GetProcessPtr();
6447 if (process) {
6448 ObjCLanguageRuntime *objc_runtime =
6449 ObjCLanguageRuntime::Get(*process);
6450 if (objc_runtime != nullptr) {
6451 CompilerType parent_ast_type = GetType(parent_qual_type);
6452 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6453 parent_ast_type, ivar_decl->getNameAsString().c_str());
6454 }
6455 }
6456
6457 // Setting this to INT32_MAX to make sure we don't compute it
6458 // twice...
6459 bit_offset = INT32_MAX;
6460
6461 if (child_byte_offset ==
6462 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6463 bit_offset = interface_layout.getFieldOffset(child_idx -
6464 superclass_idx);
6465 child_byte_offset = bit_offset / 8;
6466 }
6467
6468 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6469 // account for the bit offset of a bitfield within its
6470 // containing object. So regardless of where we get the byte
6471 // offset from, we still need to get the bit offset for
6472 // bitfields from the layout.
6473
6474 if (FieldIsBitfield(ivar_decl, child_bitfield_bit_size)) {
6475 if (bit_offset == INT32_MAX)
6476 bit_offset = interface_layout.getFieldOffset(
6477 child_idx - superclass_idx);
6478
6479 child_bitfield_bit_offset = bit_offset % 8;
6480 }
6481 return GetType(ivar_qual_type);
6482 }
6483 ++child_idx;
6484 }
6485 }
6486 }
6487 }
6488 }
6489 break;
6490
6491 case clang::Type::ObjCObjectPointer:
6492 if (idx_is_valid) {
6493 CompilerType pointee_clang_type(GetPointeeType(type));
6494
6495 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6496 child_is_deref_of_parent = false;
6497 bool tmp_child_is_deref_of_parent = false;
6498 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6499 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6500 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6501 child_bitfield_bit_size, child_bitfield_bit_offset,
6502 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6503 language_flags);
6504 } else {
6505 child_is_deref_of_parent = true;
6506 const char *parent_name =
6507 valobj ? valobj->GetName().GetCString() : nullptr;
6508 if (parent_name) {
6509 child_name.assign(1, '*');
6510 child_name += parent_name;
6511 }
6512
6513 // We have a pointer to an simple type
6514 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6515 if (std::optional<uint64_t> size =
6516 pointee_clang_type.GetByteSize(get_exe_scope())) {
6517 child_byte_size = *size;
6518 child_byte_offset = 0;
6519 return pointee_clang_type;
6520 }
6521 }
6522 }
6523 }
6524 break;
6525
6526 case clang::Type::Vector:
6527 case clang::Type::ExtVector:
6528 if (idx_is_valid) {
6529 const clang::VectorType *array =
6530 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6531 if (array) {
6532 CompilerType element_type = GetType(array->getElementType());
6533 if (element_type.GetCompleteType()) {
6534 char element_name[64];
6535 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6536 static_cast<uint64_t>(idx));
6537 child_name.assign(element_name);
6538 if (std::optional<uint64_t> size =
6539 element_type.GetByteSize(get_exe_scope())) {
6540 child_byte_size = *size;
6541 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6542 return element_type;
6543 }
6544 }
6545 }
6546 }
6547 break;
6548
6549 case clang::Type::ConstantArray:
6550 case clang::Type::IncompleteArray:
6551 if (ignore_array_bounds || idx_is_valid) {
6552 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6553 if (array) {
6554 CompilerType element_type = GetType(array->getElementType());
6555 if (element_type.GetCompleteType()) {
6556 child_name = std::string(llvm::formatv("[{0}]", idx));
6557 if (std::optional<uint64_t> size =
6558 element_type.GetByteSize(get_exe_scope())) {
6559 child_byte_size = *size;
6560 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6561 return element_type;
6562 }
6563 }
6564 }
6565 }
6566 break;
6567
6568 case clang::Type::Pointer: {
6569 CompilerType pointee_clang_type(GetPointeeType(type));
6570
6571 // Don't dereference "void *" pointers
6572 if (pointee_clang_type.IsVoidType())
6573 return CompilerType();
6574
6575 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6576 child_is_deref_of_parent = false;
6577 bool tmp_child_is_deref_of_parent = false;
6578 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6579 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6580 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6581 child_bitfield_bit_size, child_bitfield_bit_offset,
6582 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6583 language_flags);
6584 } else {
6585 child_is_deref_of_parent = true;
6586
6587 const char *parent_name =
6588 valobj ? valobj->GetName().GetCString() : nullptr;
6589 if (parent_name) {
6590 child_name.assign(1, '*');
6591 child_name += parent_name;
6592 }
6593
6594 // We have a pointer to an simple type
6595 if (idx == 0) {
6596 if (std::optional<uint64_t> size =
6597 pointee_clang_type.GetByteSize(get_exe_scope())) {
6598 child_byte_size = *size;
6599 child_byte_offset = 0;
6600 return pointee_clang_type;
6601 }
6602 }
6603 }
6604 break;
6605 }
6606
6607 case clang::Type::LValueReference:
6608 case clang::Type::RValueReference:
6609 if (idx_is_valid) {
6610 const clang::ReferenceType *reference_type =
6611 llvm::cast<clang::ReferenceType>(
6612 RemoveWrappingTypes(GetQualType(type)).getTypePtr());
6613 CompilerType pointee_clang_type =
6614 GetType(reference_type->getPointeeType());
6615 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6616 child_is_deref_of_parent = false;
6617 bool tmp_child_is_deref_of_parent = false;
6618 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6619 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6620 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6621 child_bitfield_bit_size, child_bitfield_bit_offset,
6622 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6623 language_flags);
6624 } else {
6625 const char *parent_name =
6626 valobj ? valobj->GetName().GetCString() : nullptr;
6627 if (parent_name) {
6628 child_name.assign(1, '&');
6629 child_name += parent_name;
6630 }
6631
6632 // We have a pointer to an simple type
6633 if (idx == 0) {
6634 if (std::optional<uint64_t> size =
6635 pointee_clang_type.GetByteSize(get_exe_scope())) {
6636 child_byte_size = *size;
6637 child_byte_offset = 0;
6638 return pointee_clang_type;
6639 }
6640 }
6641 }
6642 }
6643 break;
6644
6645 default:
6646 break;
6647 }
6648 return CompilerType();
6649}
6650
6652 const clang::RecordDecl *record_decl,
6653 const clang::CXXBaseSpecifier *base_spec,
6654 bool omit_empty_base_classes) {
6655 uint32_t child_idx = 0;
6656
6657 const clang::CXXRecordDecl *cxx_record_decl =
6658 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6659
6660 if (cxx_record_decl) {
6661 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6662 for (base_class = cxx_record_decl->bases_begin(),
6663 base_class_end = cxx_record_decl->bases_end();
6664 base_class != base_class_end; ++base_class) {
6665 if (omit_empty_base_classes) {
6666 if (BaseSpecifierIsEmpty(base_class))
6667 continue;
6668 }
6669
6670 if (base_class == base_spec)
6671 return child_idx;
6672 ++child_idx;
6673 }
6674 }
6675
6676 return UINT32_MAX;
6677}
6678
6680 const clang::RecordDecl *record_decl, clang::NamedDecl *canonical_decl,
6681 bool omit_empty_base_classes) {
6682 uint32_t child_idx = TypeSystemClang::GetNumBaseClasses(
6683 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6684 omit_empty_base_classes);
6685
6686 clang::RecordDecl::field_iterator field, field_end;
6687 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6688 field != field_end; ++field, ++child_idx) {
6689 if (field->getCanonicalDecl() == canonical_decl)
6690 return child_idx;
6691 }
6692
6693 return UINT32_MAX;
6694}
6695
6696// Look for a child member (doesn't include base classes, but it does include
6697// their members) in the type hierarchy. Returns an index path into
6698// "clang_type" on how to reach the appropriate member.
6699//
6700// class A
6701// {
6702// public:
6703// int m_a;
6704// int m_b;
6705// };
6706//
6707// class B
6708// {
6709// };
6710//
6711// class C :
6712// public B,
6713// public A
6714// {
6715// };
6716//
6717// If we have a clang type that describes "class C", and we wanted to looked
6718// "m_b" in it:
6719//
6720// With omit_empty_base_classes == false we would get an integer array back
6721// with: { 1, 1 } The first index 1 is the child index for "class A" within
6722// class C The second index 1 is the child index for "m_b" within class A
6723//
6724// With omit_empty_base_classes == true we would get an integer array back
6725// with: { 0, 1 } The first index 0 is the child index for "class A" within
6726// class C (since class B doesn't have any members it doesn't count) The second
6727// index 1 is the child index for "m_b" within class A
6728
6730 lldb::opaque_compiler_type_t type, llvm::StringRef name,
6731 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6732 if (type && !name.empty()) {
6733 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
6734 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6735 switch (type_class) {
6736 case clang::Type::Record:
6737 if (GetCompleteType(type)) {
6738 const clang::RecordType *record_type =
6739 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6740 const clang::RecordDecl *record_decl = record_type->getDecl();
6741
6742 assert(record_decl);
6743 uint32_t child_idx = 0;
6744
6745 const clang::CXXRecordDecl *cxx_record_decl =
6746 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6747
6748 // Try and find a field that matches NAME
6749 clang::RecordDecl::field_iterator field, field_end;
6750 for (field = record_decl->field_begin(),
6751 field_end = record_decl->field_end();
6752 field != field_end; ++field, ++child_idx) {
6753 llvm::StringRef field_name = field->getName();
6754 if (field_name.empty()) {
6755 CompilerType field_type = GetType(field->getType());
6756 child_indexes.push_back(child_idx);
6757 if (field_type.GetIndexOfChildMemberWithName(
6758 name, omit_empty_base_classes, child_indexes))
6759 return child_indexes.size();
6760 child_indexes.pop_back();
6761
6762 } else if (field_name == name) {
6763 // We have to add on the number of base classes to this index!
6764 child_indexes.push_back(
6766 cxx_record_decl, omit_empty_base_classes));
6767 return child_indexes.size();
6768 }
6769 }
6770
6771 if (cxx_record_decl) {
6772 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
6773
6774 // Didn't find things easily, lets let clang do its thang...
6775 clang::IdentifierInfo &ident_ref = getASTContext().Idents.get(name);
6776 clang::DeclarationName decl_name(&ident_ref);
6777
6778 clang::CXXBasePaths paths;
6779 if (cxx_record_decl->lookupInBases(
6780 [decl_name](const clang::CXXBaseSpecifier *specifier,
6781 clang::CXXBasePath &path) {
6782 CXXRecordDecl *record =
6783 specifier->getType()->getAsCXXRecordDecl();
6784 auto r = record->lookup(decl_name);
6785 path.Decls = r.begin();
6786 return !r.empty();
6787 },
6788 paths)) {
6789 clang::CXXBasePaths::const_paths_iterator path,
6790 path_end = paths.end();
6791 for (path = paths.begin(); path != path_end; ++path) {
6792 const size_t num_path_elements = path->size();
6793 for (size_t e = 0; e < num_path_elements; ++e) {
6794 clang::CXXBasePathElement elem = (*path)[e];
6795
6796 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
6797 omit_empty_base_classes);
6798 if (child_idx == UINT32_MAX) {
6799 child_indexes.clear();
6800 return 0;
6801 } else {
6802 child_indexes.push_back(child_idx);
6803 parent_record_decl = llvm::cast<clang::RecordDecl>(
6804 elem.Base->getType()
6805 ->castAs<clang::RecordType>()
6806 ->getDecl());
6807 }
6808 }
6809 for (clang::DeclContext::lookup_iterator I = path->Decls, E;
6810 I != E; ++I) {
6811 child_idx = GetIndexForRecordChild(
6812 parent_record_decl, *I, omit_empty_base_classes);
6813 if (child_idx == UINT32_MAX) {
6814 child_indexes.clear();
6815 return 0;
6816 } else {
6817 child_indexes.push_back(child_idx);
6818 }
6819 }
6820 }
6821 return child_indexes.size();
6822 }
6823 }
6824 }
6825 break;
6826
6827 case clang::Type::ObjCObject:
6828 case clang::Type::ObjCInterface:
6829 if (GetCompleteType(type)) {
6830 llvm::StringRef name_sref(name);
6831 const clang::ObjCObjectType *objc_class_type =
6832 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
6833 assert(objc_class_type);
6834 if (objc_class_type) {
6835 uint32_t child_idx = 0;
6836 clang::ObjCInterfaceDecl *class_interface_decl =
6837 objc_class_type->getInterface();
6838
6839 if (class_interface_decl) {
6840 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6841 ivar_end = class_interface_decl->ivar_end();
6842 clang::ObjCInterfaceDecl *superclass_interface_decl =
6843 class_interface_decl->getSuperClass();
6844
6845 for (ivar_pos = class_interface_decl->ivar_begin();
6846 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
6847 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6848
6849 if (ivar_decl->getName() == name_sref) {
6850 if ((!omit_empty_base_classes && superclass_interface_decl) ||
6851 (omit_empty_base_classes &&
6852 ObjCDeclHasIVars(superclass_interface_decl, true)))
6853 ++child_idx;
6854
6855 child_indexes.push_back(child_idx);
6856 return child_indexes.size();
6857 }
6858 }
6859
6860 if (superclass_interface_decl) {
6861 // The super class index is always zero for ObjC classes, so we
6862 // push it onto the child indexes in case we find an ivar in our
6863 // superclass...
6864 child_indexes.push_back(0);
6865
6866 CompilerType superclass_clang_type =
6867 GetType(getASTContext().getObjCInterfaceType(
6868 superclass_interface_decl));
6869 if (superclass_clang_type.GetIndexOfChildMemberWithName(
6870 name, omit_empty_base_classes, child_indexes)) {
6871 // We did find an ivar in a superclass so just return the
6872 // results!
6873 return child_indexes.size();
6874 }
6875
6876 // We didn't find an ivar matching "name" in our superclass, pop
6877 // the superclass zero index that we pushed on above.
6878 child_indexes.pop_back();
6879 }
6880 }
6881 }
6882 }
6883 break;
6884
6885 case clang::Type::ObjCObjectPointer: {
6886 CompilerType objc_object_clang_type = GetType(
6887 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
6888 ->getPointeeType());
6889 return objc_object_clang_type.GetIndexOfChildMemberWithName(
6890 name, omit_empty_base_classes, child_indexes);
6891 } break;
6892
6893 case clang::Type::ConstantArray: {
6894 // const clang::ConstantArrayType *array =
6895 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
6896 // const uint64_t element_count =
6897 // array->getSize().getLimitedValue();
6898 //
6899 // if (idx < element_count)
6900 // {
6901 // std::pair<uint64_t, unsigned> field_type_info =
6902 // ast->getTypeInfo(array->getElementType());
6903 //
6904 // char element_name[32];
6905 // ::snprintf (element_name, sizeof (element_name),
6906 // "%s[%u]", parent_name ? parent_name : "", idx);
6907 //
6908 // child_name.assign(element_name);
6909 // assert(field_type_info.first % 8 == 0);
6910 // child_byte_size = field_type_info.first / 8;
6911 // child_byte_offset = idx * child_byte_size;
6912 // return array->getElementType().getAsOpaquePtr();
6913 // }
6914 } break;
6915
6916 // case clang::Type::MemberPointerType:
6917 // {
6918 // MemberPointerType *mem_ptr_type =
6919 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
6920 // clang::QualType pointee_type =
6921 // mem_ptr_type->getPointeeType();
6922 //
6923 // if (TypeSystemClang::IsAggregateType
6924 // (pointee_type.getAsOpaquePtr()))
6925 // {
6926 // return GetIndexOfChildWithName (ast,
6927 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
6928 // name);
6929 // }
6930 // }
6931 // break;
6932 //
6933 case clang::Type::LValueReference:
6934 case clang::Type::RValueReference: {
6935 const clang::ReferenceType *reference_type =
6936 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
6937 clang::QualType pointee_type(reference_type->getPointeeType());
6938 CompilerType pointee_clang_type = GetType(pointee_type);
6939
6940 if (pointee_clang_type.IsAggregateType()) {
6941 return pointee_clang_type.GetIndexOfChildMemberWithName(
6942 name, omit_empty_base_classes, child_indexes);
6943 }
6944 } break;
6945
6946 case clang::Type::Pointer: {
6947 CompilerType pointee_clang_type(GetPointeeType(type));
6948
6949 if (pointee_clang_type.IsAggregateType()) {
6950 return pointee_clang_type.GetIndexOfChildMemberWithName(
6951 name, omit_empty_base_classes, child_indexes);
6952 }
6953 } break;
6954
6955 default:
6956 break;
6957 }
6958 }
6959 return 0;
6960}
6961
6962// Get the index of the child of "clang_type" whose name matches. This function
6963// doesn't descend into the children, but only looks one level deep and name
6964// matches can include base class names.
6965
6966uint32_t
6968 llvm::StringRef name,
6969 bool omit_empty_base_classes) {
6970 if (type && !name.empty()) {
6971 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
6972
6973 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6974
6975 switch (type_class) {
6976 case clang::Type::Record:
6977 if (GetCompleteType(type)) {
6978 const clang::RecordType *record_type =
6979 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6980 const clang::RecordDecl *record_decl = record_type->getDecl();
6981
6982 assert(record_decl);
6983 uint32_t child_idx = 0;
6984
6985 const clang::CXXRecordDecl *cxx_record_decl =
6986 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6987
6988 if (cxx_record_decl) {
6989 clang::CXXRecordDecl::base_class_const_iterator base_class,
6990 base_class_end;
6991 for (base_class = cxx_record_decl->bases_begin(),
6992 base_class_end = cxx_record_decl->bases_end();
6993 base_class != base_class_end; ++base_class) {
6994 // Skip empty base classes
6995 clang::CXXRecordDecl *base_class_decl =
6996 llvm::cast<clang::CXXRecordDecl>(
6997 base_class->getType()
6998 ->castAs<clang::RecordType>()
6999 ->getDecl());
7000 if (omit_empty_base_classes &&
7001 !TypeSystemClang::RecordHasFields(base_class_decl))
7002 continue;
7003
7004 CompilerType base_class_clang_type = GetType(base_class->getType());
7005 std::string base_class_type_name(
7006 base_class_clang_type.GetTypeName().AsCString(""));
7007 if (base_class_type_name == name)
7008 return child_idx;
7009 ++child_idx;
7010 }
7011 }
7012
7013 // Try and find a field that matches NAME
7014 clang::RecordDecl::field_iterator field, field_end;
7015 for (field = record_decl->field_begin(),
7016 field_end = record_decl->field_end();
7017 field != field_end; ++field, ++child_idx) {
7018 if (field->getName() == name)
7019 return child_idx;
7020 }
7021 }
7022 break;
7023
7024 case clang::Type::ObjCObject:
7025 case clang::Type::ObjCInterface:
7026 if (GetCompleteType(type)) {
7027 const clang::ObjCObjectType *objc_class_type =
7028 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7029 assert(objc_class_type);
7030 if (objc_class_type) {
7031 uint32_t child_idx = 0;
7032 clang::ObjCInterfaceDecl *class_interface_decl =
7033 objc_class_type->getInterface();
7034
7035 if (class_interface_decl) {
7036 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7037 ivar_end = class_interface_decl->ivar_end();
7038 clang::ObjCInterfaceDecl *superclass_interface_decl =
7039 class_interface_decl->getSuperClass();
7040
7041 for (ivar_pos = class_interface_decl->ivar_begin();
7042 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7043 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7044
7045 if (ivar_decl->getName() == name) {
7046 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7047 (omit_empty_base_classes &&
7048 ObjCDeclHasIVars(superclass_interface_decl, true)))
7049 ++child_idx;
7050
7051 return child_idx;
7052 }
7053 }
7054
7055 if (superclass_interface_decl) {
7056 if (superclass_interface_decl->getName() == name)
7057 return 0;
7058 }
7059 }
7060 }
7061 }
7062 break;
7063
7064 case clang::Type::ObjCObjectPointer: {
7065 CompilerType pointee_clang_type = GetType(
7066 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7067 ->getPointeeType());
7068 return pointee_clang_type.GetIndexOfChildWithName(
7069 name, omit_empty_base_classes);
7070 } break;
7071
7072 case clang::Type::ConstantArray: {
7073 // const clang::ConstantArrayType *array =
7074 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7075 // const uint64_t element_count =
7076 // array->getSize().getLimitedValue();
7077 //
7078 // if (idx < element_count)
7079 // {
7080 // std::pair<uint64_t, unsigned> field_type_info =
7081 // ast->getTypeInfo(array->getElementType());
7082 //
7083 // char element_name[32];
7084 // ::snprintf (element_name, sizeof (element_name),
7085 // "%s[%u]", parent_name ? parent_name : "", idx);
7086 //
7087 // child_name.assign(element_name);
7088 // assert(field_type_info.first % 8 == 0);
7089 // child_byte_size = field_type_info.first / 8;
7090 // child_byte_offset = idx * child_byte_size;
7091 // return array->getElementType().getAsOpaquePtr();
7092 // }
7093 } break;
7094
7095 // case clang::Type::MemberPointerType:
7096 // {
7097 // MemberPointerType *mem_ptr_type =
7098 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7099 // clang::QualType pointee_type =
7100 // mem_ptr_type->getPointeeType();
7101 //
7102 // if (TypeSystemClang::IsAggregateType
7103 // (pointee_type.getAsOpaquePtr()))
7104 // {
7105 // return GetIndexOfChildWithName (ast,
7106 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7107 // name);
7108 // }
7109 // }
7110 // break;
7111 //
7112 case clang::Type::LValueReference:
7113 case clang::Type::RValueReference: {
7114 const clang::ReferenceType *reference_type =
7115 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7116 CompilerType pointee_type = GetType(reference_type->getPointeeType());
7117
7118 if (pointee_type.IsAggregateType()) {
7119 return pointee_type.GetIndexOfChildWithName(name,
7120 omit_empty_base_classes);
7121 }
7122 } break;
7123
7124 case clang::Type::Pointer: {
7125 const clang::PointerType *pointer_type =
7126 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7127 CompilerType pointee_type = GetType(pointer_type->getPointeeType());
7128
7129 if (pointee_type.IsAggregateType()) {
7130 return pointee_type.GetIndexOfChildWithName(name,
7131 omit_empty_base_classes);
7132 } else {
7133 // if (parent_name)
7134 // {
7135 // child_name.assign(1, '*');
7136 // child_name += parent_name;
7137 // }
7138 //
7139 // // We have a pointer to an simple type
7140 // if (idx == 0)
7141 // {
7142 // std::pair<uint64_t, unsigned> clang_type_info
7143 // = ast->getTypeInfo(pointee_type);
7144 // assert(clang_type_info.first % 8 == 0);
7145 // child_byte_size = clang_type_info.first / 8;
7146 // child_byte_offset = 0;
7147 // return pointee_type.getAsOpaquePtr();
7148 // }
7149 }
7150 } break;
7151
7152 default:
7153 break;
7154 }
7155 }
7156 return UINT32_MAX;
7157}
7158
7161 llvm::StringRef name) {
7162 if (!type || name.empty())
7163 return CompilerType();
7164
7165 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
7166 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7167
7168 switch (type_class) {
7169 case clang::Type::Record: {
7170 if (!GetCompleteType(type))
7171 return CompilerType();
7172 const clang::RecordType *record_type =
7173 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7174 const clang::RecordDecl *record_decl = record_type->getDecl();
7175
7176 clang::DeclarationName decl_name(&getASTContext().Idents.get(name));
7177 for (NamedDecl *decl : record_decl->lookup(decl_name)) {
7178 if (auto *tag_decl = dyn_cast<clang::TagDecl>(decl))
7179 return GetType(getASTContext().getTagDeclType(tag_decl));
7180 if (auto *typedef_decl = dyn_cast<clang::TypedefNameDecl>(decl))
7181 return GetType(getASTContext().getTypedefType(typedef_decl));
7182 }
7183 break;
7184 }
7185 default:
7186 break;
7187 }
7188 return CompilerType();
7189}
7190
7192 if (!type)
7193 return false;
7194 CompilerType ct(weak_from_this(), type);
7195 const clang::Type *clang_type = ClangUtil::GetQualType(ct).getTypePtr();
7196 if (auto *cxx_record_decl = dyn_cast<clang::TagType>(clang_type))
7197 return isa<clang::ClassTemplateSpecializationDecl>(
7198 cxx_record_decl->getDecl());
7199 return false;
7200}
7201
7202size_t
7204 bool expand_pack) {
7205 if (!type)
7206 return 0;
7207
7208 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
7209 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7210 switch (type_class) {
7211 case clang::Type::Record:
7212 if (GetCompleteType(type)) {
7213 const clang::CXXRecordDecl *cxx_record_decl =
7214 qual_type->getAsCXXRecordDecl();
7215 if (cxx_record_decl) {
7216 const clang::ClassTemplateSpecializationDecl *template_decl =
7217 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7218 cxx_record_decl);
7219 if (template_decl) {
7220 const auto &template_arg_list = template_decl->getTemplateArgs();
7221 size_t num_args = template_arg_list.size();
7222 assert(num_args && "template specialization without any args");
7223 if (expand_pack && num_args) {
7224 const auto &pack = template_arg_list[num_args - 1];
7225 if (pack.getKind() == clang::TemplateArgument::Pack)
7226 num_args += pack.pack_size() - 1;
7227 }
7228 return num_args;
7229 }
7230 }
7231 }
7232 break;
7233
7234 default:
7235 break;
7236 }
7237
7238 return 0;
7239}
7240
7241const clang::ClassTemplateSpecializationDecl *
7244 if (!type)
7245 return nullptr;
7246
7247 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
7248 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7249 switch (type_class) {
7250 case clang::Type::Record: {
7251 if (! GetCompleteType(type))
7252 return nullptr;
7253 const clang::CXXRecordDecl *cxx_record_decl =
7254 qual_type->getAsCXXRecordDecl();
7255 if (!cxx_record_decl)
7256 return nullptr;
7257 return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7258 cxx_record_decl);
7259 }
7260
7261 default:
7262 return nullptr;
7263 }
7264}
7265
7266const TemplateArgument *
7267GetNthTemplateArgument(const clang::ClassTemplateSpecializationDecl *decl,
7268 size_t idx, bool expand_pack) {
7269 const auto &args = decl->getTemplateArgs();
7270 const size_t args_size = args.size();
7271
7272 assert(args_size && "template specialization without any args");
7273 if (!args_size)
7274 return nullptr;
7275
7276 const size_t last_idx = args_size - 1;
7277
7278 // We're asked for a template argument that can't be a parameter pack, so
7279 // return it without worrying about 'expand_pack'.
7280 if (idx < last_idx)
7281 return &args[idx];
7282
7283 // We're asked for the last template argument but we don't want/need to
7284 // expand it.
7285 if (!expand_pack || args[last_idx].getKind() != clang::TemplateArgument::Pack)
7286 return idx >= args.size() ? nullptr : &args[idx];
7287
7288 // Index into the expanded pack.
7289 // Note that 'idx' counts from the beginning of all template arguments
7290 // (including the ones preceding the parameter pack).
7291 const auto &pack = args[last_idx];
7292 const size_t pack_idx = idx - last_idx;
7293 if (pack_idx >= pack.pack_size())
7294 return nullptr;
7295 return &pack.pack_elements()[pack_idx];
7296}
7297
7300 size_t arg_idx, bool expand_pack) {
7301 const clang::ClassTemplateSpecializationDecl *template_decl =
7303 if (!template_decl)
7305
7306 const auto *arg = GetNthTemplateArgument(template_decl, arg_idx, expand_pack);
7307 if (!arg)
7309
7310 switch (arg->getKind()) {
7311 case clang::TemplateArgument::Null:
7313
7314 case clang::TemplateArgument::NullPtr:
7316
7317 case clang::TemplateArgument::Type:
7319
7320 case clang::TemplateArgument::Declaration:
7322
7323 case clang::TemplateArgument::Integral:
7325
7326 case clang::TemplateArgument::Template:
7328
7329 case clang::TemplateArgument::TemplateExpansion:
7331
7332 case clang::TemplateArgument::Expression:
7334
7335 case clang::TemplateArgument::Pack:
7337
7338 case clang::TemplateArgument::StructuralValue:
7340 }
7341 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
7342}
7343
7346 size_t idx, bool expand_pack) {
7347 const clang::ClassTemplateSpecializationDecl *template_decl =
7349 if (!template_decl)
7350 return CompilerType();
7351
7352 const auto *arg = GetNthTemplateArgument(template_decl, idx, expand_pack);
7353 if (!arg || arg->getKind() != clang::TemplateArgument::Type)
7354 return CompilerType();
7355
7356 return GetType(arg->getAsType());
7357}
7358
7359std::optional<CompilerType::IntegralTemplateArgument>
7361 size_t idx, bool expand_pack) {
7362 const clang::ClassTemplateSpecializationDecl *template_decl =
7364 if (!template_decl)
7365 return std::nullopt;
7366
7367 const auto *arg = GetNthTemplateArgument(template_decl, idx, expand_pack);
7368 if (!arg || arg->getKind() != clang::TemplateArgument::Integral)
7369 return std::nullopt;
7370
7371 return {{arg->getAsIntegral(), GetType(arg->getIntegralType())}};
7372}
7373
7375 if (type)
7376 return ClangUtil::RemoveFastQualifiers(CompilerType(weak_from_this(), type));
7377 return CompilerType();
7378}
7379
7380clang::EnumDecl *TypeSystemClang::GetAsEnumDecl(const CompilerType &type) {
7381 const clang::EnumType *enutype =
7382 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7383 if (enutype)
7384 return enutype->getDecl();
7385 return nullptr;
7386}
7387
7388clang::RecordDecl *TypeSystemClang::GetAsRecordDecl(const CompilerType &type) {
7389 const clang::RecordType *record_type =
7390 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7391 if (record_type)
7392 return record_type->getDecl();
7393 return nullptr;
7394}
7395
7396clang::TagDecl *TypeSystemClang::GetAsTagDecl(const CompilerType &type) {
7397 return ClangUtil::GetAsTagDecl(type);
7398}
7399
7400clang::TypedefNameDecl *
7402 const clang::TypedefType *typedef_type =
7403 llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type));
7404 if (typedef_type)
7405 return typedef_type->getDecl();
7406 return nullptr;
7407}
7408
7409clang::CXXRecordDecl *
7411 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
7412}
7413
7414clang::ObjCInterfaceDecl *
7416 const clang::ObjCObjectType *objc_class_type =
7417 llvm::dyn_cast<clang::ObjCObjectType>(
7419 if (objc_class_type)
7420 return objc_class_type->getInterface();
7421 return nullptr;
7422}
7423
7425 const CompilerType &type, llvm::StringRef name,
7426 const CompilerType &field_clang_type, AccessType access,
7427 uint32_t bitfield_bit_size) {
7428 if (!type.IsValid() || !field_clang_type.IsValid())
7429 return nullptr;
7430 auto ts = type.GetTypeSystem();
7431 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7432 if (!ast)
7433 return nullptr;
7434 clang::ASTContext &clang_ast = ast->getASTContext();
7435 clang::IdentifierInfo *ident = nullptr;
7436 if (!name.empty())
7437 ident = &clang_ast.Idents.get(name);
7438
7439 clang::FieldDecl *field = nullptr;
7440
7441 clang::Expr *bit_width = nullptr;
7442 if (bitfield_bit_size != 0) {
7443 llvm::APInt bitfield_bit_size_apint(clang_ast.getTypeSize(clang_ast.IntTy),
7444 bitfield_bit_size);
7445 bit_width = new (clang_ast)
7446 clang::IntegerLiteral(clang_ast, bitfield_bit_size_apint,
7447 clang_ast.IntTy, clang::SourceLocation());
7448 }
7449
7450 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7451 if (record_decl) {
7452 field = clang::FieldDecl::CreateDeserialized(clang_ast, GlobalDeclID());
7453 field->setDeclContext(record_decl);
7454 field->setDeclName(ident);
7455 field->setType(ClangUtil::GetQualType(field_clang_type));
7456 if (bit_width)
7457 field->setBitWidth(bit_width);
7458 SetMemberOwningModule(field, record_decl);
7459
7460 if (name.empty()) {
7461 // Determine whether this field corresponds to an anonymous struct or
7462 // union.
7463 if (const clang::TagType *TagT =
7464 field->getType()->getAs<clang::TagType>()) {
7465 if (clang::RecordDecl *Rec =
7466 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7467 if (!Rec->getDeclName()) {
7468 Rec->setAnonymousStructOrUnion(true);
7469 field->setImplicit();
7470 }
7471 }
7472 }
7473
7474 if (field) {
7475 clang::AccessSpecifier access_specifier =
7477 field->setAccess(access_specifier);
7478
7479 if (clang::CXXRecordDecl *cxx_record_decl =
7480 llvm::dyn_cast<CXXRecordDecl>(record_decl)) {
7481 AddAccessSpecifierDecl(cxx_record_decl, ast->getASTContext(),
7482 ast->GetCXXRecordDeclAccess(cxx_record_decl),
7483 access_specifier);
7484 ast->SetCXXRecordDeclAccess(cxx_record_decl, access_specifier);
7485 }
7486 record_decl->addDecl(field);
7487
7488 VerifyDecl(field);
7489 }
7490 } else {
7491 clang::ObjCInterfaceDecl *class_interface_decl =
7492 ast->GetAsObjCInterfaceDecl(type);
7493
7494 if (class_interface_decl) {
7495 const bool is_synthesized = false;
7496
7497 field_clang_type.GetCompleteType();
7498
7499 auto *ivar =
7500 clang::ObjCIvarDecl::CreateDeserialized(clang_ast, GlobalDeclID());
7501 ivar->setDeclContext(class_interface_decl);
7502 ivar->setDeclName(ident);
7503 ivar->setType(ClangUtil::GetQualType(field_clang_type));
7504 ivar->setAccessControl(ConvertAccessTypeToObjCIvarAccessControl(access));
7505 if (bit_width)
7506 ivar->setBitWidth(bit_width);
7507 ivar->setSynthesize(is_synthesized);
7508 field = ivar;
7509 SetMemberOwningModule(field, class_interface_decl);
7510
7511 if (field) {
7512 class_interface_decl->addDecl(field);
7513
7514 VerifyDecl(field);
7515 }
7516 }
7517 }
7518 return field;
7519}
7520
7522 if (!type)
7523 return;
7524
7525 auto ts = type.GetTypeSystem();
7526 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7527 if (!ast)
7528 return;
7529
7530 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7531
7532 if (!record_decl)
7533 return;
7534
7535 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7536
7537 IndirectFieldVector indirect_fields;
7538 clang::RecordDecl::field_iterator field_pos;
7539 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7540 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7541 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7542 last_field_pos = field_pos++) {
7543 if (field_pos->isAnonymousStructOrUnion()) {
7544 clang::QualType field_qual_type = field_pos->getType();
7545
7546 const clang::RecordType *field_record_type =
7547 field_qual_type->getAs<clang::RecordType>();
7548
7549 if (!field_record_type)
7550 continue;
7551
7552 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7553
7554 if (!field_record_decl)
7555 continue;
7556
7557 for (clang::RecordDecl::decl_iterator
7558 di = field_record_decl->decls_begin(),
7559 de = field_record_decl->decls_end();
7560 di != de; ++di) {
7561 if (clang::FieldDecl *nested_field_decl =
7562 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7563 clang::NamedDecl **chain =
7564 new (ast->getASTContext()) clang::NamedDecl *[2];
7565 chain[0] = *field_pos;
7566 chain[1] = nested_field_decl;
7567 clang::IndirectFieldDecl *indirect_field =
7568 clang::IndirectFieldDecl::Create(
7569 ast->getASTContext(), record_decl, clang::SourceLocation(),
7570 nested_field_decl->getIdentifier(),
7571 nested_field_decl->getType(), {chain, 2});
7572 SetMemberOwningModule(indirect_field, record_decl);
7573
7574 indirect_field->setImplicit();
7575
7576 indirect_field->setAccess(TypeSystemClang::UnifyAccessSpecifiers(
7577 field_pos->getAccess(), nested_field_decl->getAccess()));
7578
7579 indirect_fields.push_back(indirect_field);
7580 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7581 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7582 size_t nested_chain_size =
7583 nested_indirect_field_decl->getChainingSize();
7584 clang::NamedDecl **chain = new (ast->getASTContext())
7585 clang::NamedDecl *[nested_chain_size + 1];
7586 chain[0] = *field_pos;
7587
7588 int chain_index = 1;
7589 for (clang::IndirectFieldDecl::chain_iterator
7590 nci = nested_indirect_field_decl->chain_begin(),
7591 nce = nested_indirect_field_decl->chain_end();
7592 nci < nce; ++nci) {
7593 chain[chain_index] = *nci;
7594 chain_index++;
7595 }
7596
7597 clang::IndirectFieldDecl *indirect_field =
7598 clang::IndirectFieldDecl::Create(
7599 ast->getASTContext(), record_decl, clang::SourceLocation(),
7600 nested_indirect_field_decl->getIdentifier(),
7601 nested_indirect_field_decl->getType(),
7602 {chain, nested_chain_size + 1});
7603 SetMemberOwningModule(indirect_field, record_decl);
7604
7605 indirect_field->setImplicit();
7606
7607 indirect_field->setAccess(TypeSystemClang::UnifyAccessSpecifiers(
7608 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7609
7610 indirect_fields.push_back(indirect_field);
7611 }
7612 }
7613 }
7614 }
7615
7616 // Check the last field to see if it has an incomplete array type as its last
7617 // member and if it does, the tell the record decl about it
7618 if (last_field_pos != field_end_pos) {
7619 if (last_field_pos->getType()->isIncompleteArrayType())
7620 record_decl->hasFlexibleArrayMember();
7621 }
7622
7623 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7624 ife = indirect_fields.end();
7625 ifi < ife; ++ifi) {
7626 record_decl->addDecl(*ifi);
7627 }
7628}
7629
7631 if (type) {
7632 auto ts = type.GetTypeSystem();
7633 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7634 if (ast) {
7635 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7636
7637 if (!record_decl)
7638 return;
7639
7640 record_decl->addAttr(
7641 clang::PackedAttr::CreateImplicit(ast->getASTContext()));
7642 }
7643 }
7644}
7645
7647 const CompilerType &type, llvm::StringRef name,
7648 const CompilerType &var_type, AccessType access) {
7649 if (!type.IsValid() || !var_type.IsValid())
7650 return nullptr;
7651
7652 auto ts = type.GetTypeSystem();
7653 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7654 if (!ast)
7655 return nullptr;
7656
7657 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7658 if (!record_decl)
7659 return nullptr;
7660
7661 clang::VarDecl *var_decl = nullptr;
7662 clang::IdentifierInfo *ident = nullptr;
7663 if (!name.empty())
7664 ident = &ast->getASTContext().Idents.get(name);
7665
7666 var_decl =
7667 clang::VarDecl::CreateDeserialized(ast->getASTContext(), GlobalDeclID());
7668 var_decl->setDeclContext(record_decl);
7669 var_decl->setDeclName(ident);
7670 var_decl->setType(ClangUtil::GetQualType(var_type));
7671 var_decl->setStorageClass(clang::SC_Static);
7672 SetMemberOwningModule(var_decl, record_decl);
7673 if (!var_decl)
7674 return nullptr;
7675
7676 var_decl->setAccess(
7678 record_decl->addDecl(var_decl);
7679
7680 VerifyDecl(var_decl);
7681
7682 return var_decl;
7683}
7684
7686 VarDecl *var, const llvm::APInt &init_value) {
7687 assert(!var->hasInit() && "variable already initialized");
7688
7689 clang::ASTContext &ast = var->getASTContext();
7690 QualType qt = var->getType();
7691 assert(qt->isIntegralOrEnumerationType() &&
7692 "only integer or enum types supported");
7693 // If the variable is an enum type, take the underlying integer type as
7694 // the type of the integer literal.
7695 if (const EnumType *enum_type = qt->getAs<EnumType>()) {
7696 const EnumDecl *enum_decl = enum_type->getDecl();
7697 qt = enum_decl->getIntegerType();
7698 }
7699 // Bools are handled separately because the clang AST printer handles bools
7700 // separately from other integral types.
7701 if (qt->isSpecificBuiltinType(BuiltinType::Bool)) {
7702 var->setInit(CXXBoolLiteralExpr::Create(
7703 ast, !init_value.isZero(), qt.getUnqualifiedType(), SourceLocation()));
7704 } else {
7705 var->setInit(IntegerLiteral::Create(
7706 ast, init_value, qt.getUnqualifiedType(), SourceLocation()));
7707 }
7708}
7709
7711 clang::VarDecl *var, const llvm::APFloat &init_value) {
7712 assert(!var->hasInit() && "variable already initialized");
7713
7714 clang::ASTContext &ast = var->getASTContext();
7715 QualType qt = var->getType();
7716 assert(qt->isFloatingType() && "only floating point types supported");
7717 var->setInit(FloatingLiteral::Create(
7718 ast, init_value, true, qt.getUnqualifiedType(), SourceLocation()));
7719}
7720
7722 lldb::opaque_compiler_type_t type, llvm::StringRef name,
7723 const char *mangled_name, const CompilerType &method_clang_type,
7724 lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline,
7725 bool is_explicit, bool is_attr_used, bool is_artificial) {
7726 if (!type || !method_clang_type.IsValid() || name.empty())
7727 return nullptr;
7728
7729 clang::QualType record_qual_type(GetCanonicalQualType(type));
7730
7731 clang::CXXRecordDecl *cxx_record_decl =
7732 record_qual_type->getAsCXXRecordDecl();
7733
7734 if (cxx_record_decl == nullptr)
7735 return nullptr;
7736
7737 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7738
7739 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7740
7741 clang::DeclarationName decl_name(&getASTContext().Idents.get(name));
7742
7743 const clang::FunctionType *function_type =
7744 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7745
7746 if (function_type == nullptr)
7747 return nullptr;
7748
7749 const clang::FunctionProtoType *method_function_prototype(
7750 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7751
7752 if (!method_function_prototype)
7753 return nullptr;
7754
7755 unsigned int num_params = method_function_prototype->getNumParams();
7756
7757 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7758 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7759
7760 if (is_artificial)
7761 return nullptr; // skip everything artificial
7762
7763 const clang::ExplicitSpecifier explicit_spec(
7764 nullptr /*expr*/, is_explicit ? clang::ExplicitSpecKind::ResolvedTrue
7765 : clang::ExplicitSpecKind::ResolvedFalse);
7766
7767 if (name.starts_with("~")) {
7768 cxx_dtor_decl = clang::CXXDestructorDecl::CreateDeserialized(
7769 getASTContext(), GlobalDeclID());
7770 cxx_dtor_decl->setDeclContext(cxx_record_decl);
7771 cxx_dtor_decl->setDeclName(
7772 getASTContext().DeclarationNames.getCXXDestructorName(
7773 getASTContext().getCanonicalType(record_qual_type)));
7774 cxx_dtor_decl->setType(method_qual_type);
7775 cxx_dtor_decl->setImplicit(is_artificial);
7776 cxx_dtor_decl->setInlineSpecified(is_inline);
7777 cxx_dtor_decl->setConstexprKind(ConstexprSpecKind::Unspecified);
7778 cxx_method_decl = cxx_dtor_decl;
7779 } else if (decl_name == cxx_record_decl->getDeclName()) {
7780 cxx_ctor_decl = clang::CXXConstructorDecl::CreateDeserialized(
7781 getASTContext(), GlobalDeclID(), 0);
7782 cxx_ctor_decl->setDeclContext(cxx_record_decl);
7783 cxx_ctor_decl->setDeclName(
7784 getASTContext().DeclarationNames.getCXXConstructorName(
7785 getASTContext().getCanonicalType(record_qual_type)));
7786 cxx_ctor_decl->setType(method_qual_type);
7787 cxx_ctor_decl->setImplicit(is_artificial);
7788 cxx_ctor_decl->setInlineSpecified(is_inline);
7789 cxx_ctor_decl->setConstexprKind(ConstexprSpecKind::Unspecified);
7790 cxx_ctor_decl->setNumCtorInitializers(0);
7791 cxx_ctor_decl->setExplicitSpecifier(explicit_spec);
7792 cxx_method_decl = cxx_ctor_decl;
7793 } else {
7794 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7795 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7796
7797 if (IsOperator(name, op_kind)) {
7798 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7799 // Check the number of operator parameters. Sometimes we have seen bad
7800 // DWARF that doesn't correctly describe operators and if we try to
7801 // create a method and add it to the class, clang will assert and
7802 // crash, so we need to make sure things are acceptable.
7803 const bool is_method = true;
7805 is_method, op_kind, num_params))
7806 return nullptr;
7807 cxx_method_decl = clang::CXXMethodDecl::CreateDeserialized(
7808 getASTContext(), GlobalDeclID());
7809 cxx_method_decl->setDeclContext(cxx_record_decl);
7810 cxx_method_decl->setDeclName(
7811 getASTContext().DeclarationNames.getCXXOperatorName(op_kind));
7812 cxx_method_decl->setType(method_qual_type);
7813 cxx_method_decl->setStorageClass(SC);
7814 cxx_method_decl->setInlineSpecified(is_inline);
7815 cxx_method_decl->setConstexprKind(ConstexprSpecKind::Unspecified);
7816 } else if (num_params == 0) {
7817 // Conversion operators don't take params...
7818 auto *cxx_conversion_decl =
7819 clang::CXXConversionDecl::CreateDeserialized(getASTContext(),
7820 GlobalDeclID());
7821 cxx_conversion_decl->setDeclContext(cxx_record_decl);
7822 cxx_conversion_decl->setDeclName(
7823 getASTContext().DeclarationNames.getCXXConversionFunctionName(
7824 getASTContext().getCanonicalType(
7825 function_type->getReturnType())));
7826 cxx_conversion_decl->setType(method_qual_type);
7827 cxx_conversion_decl->setInlineSpecified(is_inline);
7828 cxx_conversion_decl->setExplicitSpecifier(explicit_spec);
7829 cxx_conversion_decl->setConstexprKind(ConstexprSpecKind::Unspecified);
7830 cxx_method_decl = cxx_conversion_decl;
7831 }
7832 }
7833
7834 if (cxx_method_decl == nullptr) {
7835 cxx_method_decl = clang::CXXMethodDecl::CreateDeserialized(
7836 getASTContext(), GlobalDeclID());
7837 cxx_method_decl->setDeclContext(cxx_record_decl);
7838 cxx_method_decl->setDeclName(decl_name);
7839 cxx_method_decl->setType(method_qual_type);
7840 cxx_method_decl->setInlineSpecified(is_inline);
7841 cxx_method_decl->setStorageClass(SC);
7842 cxx_method_decl->setConstexprKind(ConstexprSpecKind::Unspecified);
7843 }
7844 }
7845 SetMemberOwningModule(cxx_method_decl, cxx_record_decl);
7846
7847 clang::AccessSpecifier access_specifier =
7849
7850 cxx_method_decl->setAccess(access_specifier);
7851 cxx_method_decl->setVirtualAsWritten(is_virtual);
7852
7853 if (is_attr_used)
7854 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(getASTContext()));
7855
7856 if (mangled_name != nullptr) {
7857 cxx_method_decl->addAttr(clang::AsmLabelAttr::CreateImplicit(
7858 getASTContext(), mangled_name, /*literal=*/false));
7859 }
7860
7861 // Populate the method decl with parameter decls
7862
7863 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
7864
7865 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
7866 params.push_back(clang::ParmVarDecl::Create(
7867 getASTContext(), cxx_method_decl, clang::SourceLocation(),
7868 clang::SourceLocation(),
7869 nullptr, // anonymous
7870 method_function_prototype->getParamType(param_index), nullptr,
7871 clang::SC_None, nullptr));
7872 }
7873
7874 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
7875
7876 AddAccessSpecifierDecl(cxx_record_decl, getASTContext(),
7877 GetCXXRecordDeclAccess(cxx_record_decl),
7878 access_specifier);
7879 SetCXXRecordDeclAccess(cxx_record_decl, access_specifier);
7880
7881 cxx_record_decl->addDecl(cxx_method_decl);
7882
7883 // Sometimes the debug info will mention a constructor (default/copy/move),
7884 // destructor, or assignment operator (copy/move) but there won't be any
7885 // version of this in the code. So we check if the function was artificially
7886 // generated and if it is trivial and this lets the compiler/backend know
7887 // that it can inline the IR for these when it needs to and we can avoid a
7888 // "missing function" error when running expressions.
7889
7890 if (is_artificial) {
7891 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
7892 cxx_record_decl->hasTrivialDefaultConstructor()) ||
7893 (cxx_ctor_decl->isCopyConstructor() &&
7894 cxx_record_decl->hasTrivialCopyConstructor()) ||
7895 (cxx_ctor_decl->isMoveConstructor() &&
7896 cxx_record_decl->hasTrivialMoveConstructor()))) {
7897 cxx_ctor_decl->setDefaulted();
7898 cxx_ctor_decl->setTrivial(true);
7899 } else if (cxx_dtor_decl) {
7900 if (cxx_record_decl->hasTrivialDestructor()) {
7901 cxx_dtor_decl->setDefaulted();
7902 cxx_dtor_decl->setTrivial(true);
7903 }
7904 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
7905 cxx_record_decl->hasTrivialCopyAssignment()) ||
7906 (cxx_method_decl->isMoveAssignmentOperator() &&
7907 cxx_record_decl->hasTrivialMoveAssignment())) {
7908 cxx_method_decl->setDefaulted();
7909 cxx_method_decl->setTrivial(true);
7910 }
7911 }
7912
7913 VerifyDecl(cxx_method_decl);
7914
7915 return cxx_method_decl;
7916}
7917
7920 if (auto *record = GetAsCXXRecordDecl(type))
7921 for (auto *method : record->methods())
7922 addOverridesForMethod(method);
7923}
7924
7925#pragma mark C++ Base Classes
7926
7927std::unique_ptr<clang::CXXBaseSpecifier>
7929 AccessType access, bool is_virtual,
7930 bool base_of_class) {
7931 if (!type)
7932 return nullptr;
7933
7934 return std::make_unique<clang::CXXBaseSpecifier>(
7935 clang::SourceRange(), is_virtual, base_of_class,
7937 getASTContext().getTrivialTypeSourceInfo(GetQualType(type)),
7938 clang::SourceLocation());
7939}
7940
7943 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) {
7944 if (!type)
7945 return false;
7946 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
7947 if (!cxx_record_decl)
7948 return false;
7949 std::vector<clang::CXXBaseSpecifier *> raw_bases;
7950 raw_bases.reserve(bases.size());
7951
7952 // Clang will make a copy of them, so it's ok that we pass pointers that we're
7953 // about to destroy.
7954 for (auto &b : bases)
7955 raw_bases.push_back(b.get());
7956 cxx_record_decl->setBases(raw_bases.data(), raw_bases.size());
7957 return true;
7958}
7959
7961 const CompilerType &type, const CompilerType &superclass_clang_type) {
7962 auto ts = type.GetTypeSystem();
7963 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7964 if (!ast)
7965 return false;
7966 clang::ASTContext &clang_ast = ast->getASTContext();
7967
7968 if (type && superclass_clang_type.IsValid() &&
7969 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
7970 clang::ObjCInterfaceDecl *class_interface_decl =
7972 clang::ObjCInterfaceDecl *super_interface_decl =
7973 GetAsObjCInterfaceDecl(superclass_clang_type);
7974 if (class_interface_decl && super_interface_decl) {
7975 class_interface_decl->setSuperClass(clang_ast.getTrivialTypeSourceInfo(
7976 clang_ast.getObjCInterfaceType(super_interface_decl)));
7977 return true;
7978 }
7979 }
7980 return false;
7981}
7982
7984 const CompilerType &type, const char *property_name,
7985 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
7986 const char *property_setter_name, const char *property_getter_name,
7987 uint32_t property_attributes, ClangASTMetadata metadata) {
7988 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
7989 property_name[0] == '\0')
7990 return false;
7991 auto ts = type.GetTypeSystem();
7992 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
7993 if (!ast)
7994 return false;
7995 clang::ASTContext &clang_ast = ast->getASTContext();
7996
7997 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
7998 if (!class_interface_decl)
7999 return false;
8000
8001 CompilerType property_clang_type_to_access;
8002
8003 if (property_clang_type.IsValid())
8004 property_clang_type_to_access = property_clang_type;
8005 else if (ivar_decl)
8006 property_clang_type_to_access = ast->GetType(ivar_decl->getType());
8007
8008 if (!class_interface_decl || !property_clang_type_to_access.IsValid())
8009 return false;
8010
8011 clang::TypeSourceInfo *prop_type_source;
8012 if (ivar_decl)
8013 prop_type_source = clang_ast.getTrivialTypeSourceInfo(ivar_decl->getType());
8014 else
8015 prop_type_source = clang_ast.getTrivialTypeSourceInfo(
8016 ClangUtil::GetQualType(property_clang_type));
8017
8018 clang::ObjCPropertyDecl *property_decl =
8019 clang::ObjCPropertyDecl::CreateDeserialized(clang_ast, GlobalDeclID());
8020 property_decl->setDeclContext(class_interface_decl);
8021 property_decl->setDeclName(&clang_ast.Idents.get(property_name));
8022 property_decl->setType(ivar_decl
8023 ? ivar_decl->getType()
8024 : ClangUtil::GetQualType(property_clang_type),
8025 prop_type_source);
8026 SetMemberOwningModule(property_decl, class_interface_decl);
8027
8028 if (!property_decl)
8029 return false;
8030
8031 ast->SetMetadata(property_decl, metadata);
8032
8033 class_interface_decl->addDecl(property_decl);
8034
8035 clang::Selector setter_sel, getter_sel;
8036
8037 if (property_setter_name) {
8038 std::string property_setter_no_colon(property_setter_name,
8039 strlen(property_setter_name) - 1);
8040 const clang::IdentifierInfo *setter_ident =
8041 &clang_ast.Idents.get(property_setter_no_colon);
8042 setter_sel = clang_ast.Selectors.getSelector(1, &setter_ident);
8043 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8044 std::string setter_sel_string("set");
8045 setter_sel_string.push_back(::toupper(property_name[0]));
8046 setter_sel_string.append(&property_name[1]);
8047 const clang::IdentifierInfo *setter_ident =
8048 &clang_ast.Idents.get(setter_sel_string);
8049 setter_sel = clang_ast.Selectors.getSelector(1, &setter_ident);
8050 }
8051 property_decl->setSetterName(setter_sel);
8052 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_setter);
8053
8054 if (property_getter_name != nullptr) {
8055 const clang::IdentifierInfo *getter_ident =
8056 &clang_ast.Idents.get(property_getter_name);
8057 getter_sel = clang_ast.Selectors.getSelector(0, &getter_ident);
8058 } else {
8059 const clang::IdentifierInfo *getter_ident =
8060 &clang_ast.Idents.get(property_name);
8061 getter_sel = clang_ast.Selectors.getSelector(0, &getter_ident);
8062 }
8063 property_decl->setGetterName(getter_sel);
8064 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_getter);
8065
8066 if (ivar_decl)
8067 property_decl->setPropertyIvarDecl(ivar_decl);
8068
8069 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8070 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_readonly);
8071 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8072 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_readwrite);
8073 if (property_attributes & DW_APPLE_PROPERTY_assign)
8074 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_assign);
8075 if (property_attributes & DW_APPLE_PROPERTY_retain)
8076 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_retain);
8077 if (property_attributes & DW_APPLE_PROPERTY_copy)
8078 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_copy);
8079 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8080 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_nonatomic);
8081 if (property_attributes & ObjCPropertyAttribute::kind_nullability)
8082 property_decl->setPropertyAttributes(
8083 ObjCPropertyAttribute::kind_nullability);
8084 if (property_attributes & ObjCPropertyAttribute::kind_null_resettable)
8085 property_decl->setPropertyAttributes(
8086 ObjCPropertyAttribute::kind_null_resettable);
8087 if (property_attributes & ObjCPropertyAttribute::kind_class)
8088 property_decl->setPropertyAttributes(ObjCPropertyAttribute::kind_class);
8089
8090 const bool isInstance =
8091 (property_attributes & ObjCPropertyAttribute::kind_class) == 0;
8092
8093 clang::ObjCMethodDecl *getter = nullptr;
8094 if (!getter_sel.isNull())
8095 getter = isInstance ? class_interface_decl->lookupInstanceMethod(getter_sel)
8096 : class_interface_decl->lookupClassMethod(getter_sel);
8097 if (!getter_sel.isNull() && !getter) {
8098 const bool isVariadic = false;
8099 const bool isPropertyAccessor = true;
8100 const bool isSynthesizedAccessorStub = false;
8101 const bool isImplicitlyDeclared = true;
8102 const bool isDefined = false;
8103 const clang::ObjCImplementationControl impControl =
8104 clang::ObjCImplementationControl::None;
8105 const bool HasRelatedResultType = false;
8106
8107 getter =
8108 clang::ObjCMethodDecl::CreateDeserialized(clang_ast, GlobalDeclID());
8109 getter->setDeclName(getter_sel);
8110 getter->setReturnType(ClangUtil::GetQualType(property_clang_type_to_access));
8111 getter->setDeclContext(class_interface_decl);
8112 getter->setInstanceMethod(isInstance);
8113 getter->setVariadic(isVariadic);
8114 getter->setPropertyAccessor(isPropertyAccessor);
8115 getter->setSynthesizedAccessorStub(isSynthesizedAccessorStub);
8116 getter->setImplicit(isImplicitlyDeclared);
8117 getter->setDefined(isDefined);
8118 getter->setDeclImplementation(impControl);
8119 getter->setRelatedResultType(HasRelatedResultType);
8120 SetMemberOwningModule(getter, class_interface_decl);
8121
8122 if (getter) {
8123 ast->SetMetadata(getter, metadata);
8124
8125 getter->setMethodParams(clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(),
8126 llvm::ArrayRef<clang::SourceLocation>());
8127 class_interface_decl->addDecl(getter);
8128 }
8129 }
8130 if (getter) {
8131 getter->setPropertyAccessor(true);
8132 property_decl->setGetterMethodDecl(getter);
8133 }
8134
8135 clang::ObjCMethodDecl *setter = nullptr;
8136 setter = isInstance ? class_interface_decl->lookupInstanceMethod(setter_sel)
8137 : class_interface_decl->lookupClassMethod(setter_sel);
8138 if (!setter_sel.isNull() && !setter) {
8139 clang::QualType result_type = clang_ast.VoidTy;
8140 const bool isVariadic = false;
8141 const bool isPropertyAccessor = true;
8142 const bool isSynthesizedAccessorStub = false;
8143 const bool isImplicitlyDeclared = true;
8144 const bool isDefined = false;
8145 const clang::ObjCImplementationControl impControl =
8146 clang::ObjCImplementationControl::None;
8147 const bool HasRelatedResultType = false;
8148
8149 setter =
8150 clang::ObjCMethodDecl::CreateDeserialized(clang_ast, GlobalDeclID());
8151 setter->setDeclName(setter_sel);
8152 setter->setReturnType(result_type);
8153 setter->setDeclContext(class_interface_decl);
8154 setter->setInstanceMethod(isInstance);
8155 setter->setVariadic(isVariadic);
8156 setter->setPropertyAccessor(isPropertyAccessor);
8157 setter->setSynthesizedAccessorStub(isSynthesizedAccessorStub);
8158 setter->setImplicit(isImplicitlyDeclared);
8159 setter->setDefined(isDefined);
8160 setter->setDeclImplementation(impControl);
8161 setter->setRelatedResultType(HasRelatedResultType);
8162 SetMemberOwningModule(setter, class_interface_decl);
8163
8164 if (setter) {
8165 ast->SetMetadata(setter, metadata);
8166
8167 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8168 params.push_back(clang::ParmVarDecl::Create(
8169 clang_ast, setter, clang::SourceLocation(), clang::SourceLocation(),
8170 nullptr, // anonymous
8171 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8172 clang::SC_Auto, nullptr));
8173
8174 setter->setMethodParams(clang_ast,
8175 llvm::ArrayRef<clang::ParmVarDecl *>(params),
8176 llvm::ArrayRef<clang::SourceLocation>());
8177
8178 class_interface_decl->addDecl(setter);
8179 }
8180 }
8181 if (setter) {
8182 setter->setPropertyAccessor(true);
8183 property_decl->setSetterMethodDecl(setter);
8184 }
8185
8186 return true;
8187}
8188
8190 bool check_superclass) {
8191 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8192 if (class_interface_decl)
8193 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8194 return false;
8195}
8196
8198 const CompilerType &type,
8199 const char *name, // the full symbol name as seen in the symbol table
8200 // (lldb::opaque_compiler_type_t type, "-[NString
8201 // stringWithCString:]")
8202 const CompilerType &method_clang_type, bool is_artificial, bool is_variadic,
8203 bool is_objc_direct_call) {
8204 if (!type || !method_clang_type.IsValid())
8205 return nullptr;
8206
8207 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8208
8209 if (class_interface_decl == nullptr)
8210 return nullptr;
8211 auto ts = type.GetTypeSystem();
8212 auto lldb_ast = ts.dyn_cast_or_null<TypeSystemClang>();
8213 if (lldb_ast == nullptr)
8214 return nullptr;
8215 clang::ASTContext &ast = lldb_ast->getASTContext();
8216
8217 const char *selector_start = ::strchr(name, ' ');
8218 if (selector_start == nullptr)
8219 return nullptr;
8220
8221 selector_start++;
8222 llvm::SmallVector<const clang::IdentifierInfo *, 12> selector_idents;
8223
8224 size_t len = 0;
8225 const char *start;
8226
8227 unsigned num_selectors_with_args = 0;
8228 for (start = selector_start; start && *start != '\0' && *start != ']';
8229 start += len) {
8230 len = ::strcspn(start, ":]");
8231 bool has_arg = (start[len] == ':');
8232 if (has_arg)
8233 ++num_selectors_with_args;
8234 selector_idents.push_back(&ast.Idents.get(llvm::StringRef(start, len)));
8235 if (has_arg)
8236 len += 1;
8237 }
8238
8239 if (selector_idents.size() == 0)
8240 return nullptr;
8241
8242 clang::Selector method_selector = ast.Selectors.getSelector(
8243 num_selectors_with_args ? selector_idents.size() : 0,
8244 selector_idents.data());
8245
8246 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8247
8248 // Populate the method decl with parameter decls
8249 const clang::Type *method_type(method_qual_type.getTypePtr());
8250
8251 if (method_type == nullptr)
8252 return nullptr;
8253
8254 const clang::FunctionProtoType *method_function_prototype(
8255 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8256
8257 if (!method_function_prototype)
8258 return nullptr;
8259
8260 const bool isInstance = (name[0] == '-');
8261 const bool isVariadic = is_variadic;
8262 const bool isPropertyAccessor = false;
8263 const bool isSynthesizedAccessorStub = false;
8264 /// Force this to true because we don't have source locations.
8265 const bool isImplicitlyDeclared = true;
8266 const bool isDefined = false;
8267 const clang::ObjCImplementationControl impControl =
8268 clang::ObjCImplementationControl::None;
8269 const bool HasRelatedResultType = false;
8270
8271 const unsigned num_args = method_function_prototype->getNumParams();
8272
8273 if (num_args != num_selectors_with_args)
8274 return nullptr; // some debug information is corrupt. We are not going to
8275 // deal with it.
8276
8277 auto *objc_method_decl =
8278 clang::ObjCMethodDecl::CreateDeserialized(ast, GlobalDeclID());
8279 objc_method_decl->setDeclName(method_selector);
8280 objc_method_decl->setReturnType(method_function_prototype->getReturnType());
8281 objc_method_decl->setDeclContext(
8282 lldb_ast->GetDeclContextForType(ClangUtil::GetQualType(type)));
8283 objc_method_decl->setInstanceMethod(isInstance);
8284 objc_method_decl->setVariadic(isVariadic);
8285 objc_method_decl->setPropertyAccessor(isPropertyAccessor);
8286 objc_method_decl->setSynthesizedAccessorStub(isSynthesizedAccessorStub);
8287 objc_method_decl->setImplicit(isImplicitlyDeclared);
8288 objc_method_decl->setDefined(isDefined);
8289 objc_method_decl->setDeclImplementation(impControl);
8290 objc_method_decl->setRelatedResultType(HasRelatedResultType);
8291 SetMemberOwningModule(objc_method_decl, class_interface_decl);
8292
8293 if (objc_method_decl == nullptr)
8294 return nullptr;
8295
8296 if (num_args > 0) {
8297 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8298
8299 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8300 params.push_back(clang::ParmVarDecl::Create(
8301 ast, objc_method_decl, clang::SourceLocation(),
8302 clang::SourceLocation(),
8303 nullptr, // anonymous
8304 method_function_prototype->getParamType(param_index), nullptr,
8305 clang::SC_Auto, nullptr));
8306 }
8307
8308 objc_method_decl->setMethodParams(
8309 ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8310 llvm::ArrayRef<clang::SourceLocation>());
8311 }
8312
8313 if (is_objc_direct_call) {
8314 // Add a the objc_direct attribute to the declaration we generate that
8315 // we generate a direct method call for this ObjCMethodDecl.
8316 objc_method_decl->addAttr(
8317 clang::ObjCDirectAttr::CreateImplicit(ast, SourceLocation()));
8318 // Usually Sema is creating implicit parameters (e.g., self) when it
8319 // parses the method. We don't have a parsing Sema when we build our own
8320 // AST here so we manually need to create these implicit parameters to
8321 // make the direct call code generation happy.
8322 objc_method_decl->createImplicitParams(ast, class_interface_decl);
8323 }
8324
8325 class_interface_decl->addDecl(objc_method_decl);
8326
8327 VerifyDecl(objc_method_decl);
8328
8329 return objc_method_decl;
8330}
8331
8333 bool has_extern) {
8334 if (!type)
8335 return false;
8336
8337 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
8338
8339 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8340 switch (type_class) {
8341 case clang::Type::Record: {
8342 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8343 if (cxx_record_decl) {
8344 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8345 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8346 return true;
8347 }
8348 } break;
8349
8350 case clang::Type::Enum: {
8351 clang::EnumDecl *enum_decl =
8352 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8353 if (enum_decl) {
8354 enum_decl->setHasExternalLexicalStorage(has_extern);
8355 enum_decl->setHasExternalVisibleStorage(has_extern);
8356 return true;
8357 }
8358 } break;
8359
8360 case clang::Type::ObjCObject:
8361 case clang::Type::ObjCInterface: {
8362 const clang::ObjCObjectType *objc_class_type =
8363 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8364 assert(objc_class_type);
8365 if (objc_class_type) {
8366 clang::ObjCInterfaceDecl *class_interface_decl =
8367 objc_class_type->getInterface();
8368
8369 if (class_interface_decl) {
8370 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8371 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8372 return true;
8373 }
8374 }
8375 } break;
8376
8377 default:
8378 break;
8379 }
8380 return false;
8381}
8382
8383#pragma mark TagDecl
8384
8386 clang::QualType qual_type(ClangUtil::GetQualType(type));
8387 if (!qual_type.isNull()) {
8388 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8389 if (tag_type) {
8390 clang::TagDecl *tag_decl = tag_type->getDecl();
8391 if (tag_decl) {
8392 tag_decl->startDefinition();
8393 return true;
8394 }
8395 }
8396
8397 const clang::ObjCObjectType *object_type =
8398 qual_type->getAs<clang::ObjCObjectType>();
8399 if (object_type) {
8400 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8401 if (interface_decl) {
8402 interface_decl->startDefinition();
8403 return true;
8404 }
8405 }
8406 }
8407 return false;
8408}
8409
8411 const CompilerType &type) {
8412 clang::QualType qual_type(ClangUtil::GetQualType(type));
8413 if (qual_type.isNull())
8414 return false;
8415
8416 auto ts = type.GetTypeSystem();
8417 auto lldb_ast = ts.dyn_cast_or_null<TypeSystemClang>();
8418 if (lldb_ast == nullptr)
8419 return false;
8420
8421 // Make sure we use the same methodology as
8422 // TypeSystemClang::StartTagDeclarationDefinition() as to how we start/end
8423 // the definition.
8424 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8425 if (tag_type) {
8426 clang::TagDecl *tag_decl = tag_type->getDecl();
8427
8428 if (auto *cxx_record_decl = llvm::dyn_cast<CXXRecordDecl>(tag_decl)) {
8429 // If we have a move constructor declared but no copy constructor we
8430 // need to explicitly mark it as deleted. Usually Sema would do this for
8431 // us in Sema::DeclareImplicitCopyConstructor but we don't have a Sema
8432 // when building an AST from debug information.
8433 // See also:
8434 // C++11 [class.copy]p7, p18:
8435 // If the class definition declares a move constructor or move assignment
8436 // operator, an implicitly declared copy constructor or copy assignment
8437 // operator is defined as deleted.
8438 if (cxx_record_decl->hasUserDeclaredMoveConstructor() ||
8439 cxx_record_decl->hasUserDeclaredMoveAssignment()) {
8440 if (cxx_record_decl->needsImplicitCopyConstructor())
8441 cxx_record_decl->setImplicitCopyConstructorIsDeleted();
8442 if (cxx_record_decl->needsImplicitCopyAssignment())
8443 cxx_record_decl->setImplicitCopyAssignmentIsDeleted();
8444 }
8445
8446 if (!cxx_record_decl->isCompleteDefinition())
8447 cxx_record_decl->completeDefinition();
8448 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8449 cxx_record_decl->setHasExternalLexicalStorage(false);
8450 cxx_record_decl->setHasExternalVisibleStorage(false);
8451 lldb_ast->SetCXXRecordDeclAccess(cxx_record_decl,
8452 clang::AccessSpecifier::AS_none);
8453 return true;
8454 }
8455 }
8456
8457 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8458
8459 if (!enutype)
8460 return false;
8461 clang::EnumDecl *enum_decl = enutype->getDecl();
8462
8463 if (enum_decl->isCompleteDefinition())
8464 return true;
8465
8466 clang::ASTContext &ast = lldb_ast->getASTContext();
8467
8468 /// TODO This really needs to be fixed.
8469
8470 QualType integer_type(enum_decl->getIntegerType());
8471 if (!integer_type.isNull()) {
8472 unsigned NumPositiveBits = 1;
8473 unsigned NumNegativeBits = 0;
8474
8475 clang::QualType promotion_qual_type;
8476 // If the enum integer type is less than an integer in bit width,
8477 // then we must promote it to an integer size.
8478 if (ast.getTypeSize(enum_decl->getIntegerType()) <
8479 ast.getTypeSize(ast.IntTy)) {
8480 if (enum_decl->getIntegerType()->isSignedIntegerType())
8481 promotion_qual_type = ast.IntTy;
8482 else
8483 promotion_qual_type = ast.UnsignedIntTy;
8484 } else
8485 promotion_qual_type = enum_decl->getIntegerType();
8486
8487 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8488 promotion_qual_type, NumPositiveBits,
8489 NumNegativeBits);
8490 }
8491 return true;
8492}
8493
8495 const CompilerType &enum_type, const Declaration &decl, const char *name,
8496 const llvm::APSInt &value) {
8497
8498 if (!enum_type || ConstString(name).IsEmpty())
8499 return nullptr;
8500
8501 lldbassert(enum_type.GetTypeSystem().GetSharedPointer().get() ==
8502 static_cast<TypeSystem *>(this));
8503
8504 lldb::opaque_compiler_type_t enum_opaque_compiler_type =
8505 enum_type.GetOpaqueQualType();
8506
8507 if (!enum_opaque_compiler_type)
8508 return nullptr;
8509
8510 clang::QualType enum_qual_type(
8511 GetCanonicalQualType(enum_opaque_compiler_type));
8512
8513 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8514
8515 if (!clang_type)
8516 return nullptr;
8517
8518 const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type);
8519
8520 if (!enutype)
8521 return nullptr;
8522
8523 clang::EnumConstantDecl *enumerator_decl =
8524 clang::EnumConstantDecl::CreateDeserialized(getASTContext(),
8525 GlobalDeclID());
8526 enumerator_decl->setDeclContext(enutype->getDecl());
8527 if (name && name[0])
8528 enumerator_decl->setDeclName(&getASTContext().Idents.get(name));
8529 enumerator_decl->setType(clang::QualType(enutype, 0));
8530 enumerator_decl->setInitVal(getASTContext(), value);
8531 SetMemberOwningModule(enumerator_decl, enutype->getDecl());
8532
8533 if (!enumerator_decl)
8534 return nullptr;
8535
8536 enutype->getDecl()->addDecl(enumerator_decl);
8537
8538 VerifyDecl(enumerator_decl);
8539 return enumerator_decl;
8540}
8541
8543 const CompilerType &enum_type, const Declaration &decl, const char *name,
8544 int64_t enum_value, uint32_t enum_value_bit_size) {
8545 CompilerType underlying_type = GetEnumerationIntegerType(enum_type);
8546 bool is_signed = false;
8547 underlying_type.IsIntegerType(is_signed);
8548
8549 llvm::APSInt value(enum_value_bit_size, is_signed);
8550 value = enum_value;
8551
8552 return AddEnumerationValueToEnumerationType(enum_type, decl, name, value);
8553}
8554
8556 clang::QualType qt(ClangUtil::GetQualType(type));
8557 const clang::Type *clang_type = qt.getTypePtrOrNull();
8558 const auto *enum_type = llvm::dyn_cast_or_null<clang::EnumType>(clang_type);
8559 if (!enum_type)
8560 return CompilerType();
8561
8562 return GetType(enum_type->getDecl()->getIntegerType());
8563}
8564
8567 const CompilerType &pointee_type) {
8568 if (type && pointee_type.IsValid() &&
8569 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8570 auto ts = type.GetTypeSystem();
8571 auto ast = ts.dyn_cast_or_null<TypeSystemClang>();
8572 if (!ast)
8573 return CompilerType();
8574 return ast->GetType(ast->getASTContext().getMemberPointerType(
8575 ClangUtil::GetQualType(pointee_type),
8576 ClangUtil::GetQualType(type).getTypePtr()));
8577 }
8578 return CompilerType();
8579}
8580
8581// Dumping types
8582#define DEPTH_INCREMENT 2
8583
8584#ifndef NDEBUG
8585LLVM_DUMP_METHOD void
8587 if (!type)
8588 return;
8589 clang::QualType qual_type(GetQualType(type));
8590 qual_type.dump();
8591}
8592#endif
8593
8594void TypeSystemClang::Dump(llvm::raw_ostream &output) {
8595 GetTranslationUnitDecl()->dump(output);
8596}
8597
8599 llvm::StringRef symbol_name) {
8600 SymbolFile *symfile = GetSymbolFile();
8601
8602 if (!symfile)
8603 return;
8604
8605 lldb_private::TypeList type_list;
8606 symfile->GetTypes(nullptr, eTypeClassAny, type_list);
8607 size_t ntypes = type_list.GetSize();
8608
8609 for (size_t i = 0; i < ntypes; ++i) {
8610 TypeSP type = type_list.GetTypeAtIndex(i);
8611
8612 if (!symbol_name.empty())
8613 if (symbol_name != type->GetName().GetStringRef())
8614 continue;
8615
8616 s << type->GetName().AsCString() << "\n";
8617
8618 CompilerType full_type = type->GetFullCompilerType();
8619 if (clang::TagDecl *tag_decl = GetAsTagDecl(full_type)) {
8620 tag_decl->dump(s.AsRawOstream());
8621 continue;
8622 }
8623 if (clang::TypedefNameDecl *typedef_decl = GetAsTypedefDecl(full_type)) {
8624 typedef_decl->dump(s.AsRawOstream());
8625 continue;
8626 }
8627 if (auto *objc_obj = llvm::dyn_cast<clang::ObjCObjectType>(
8628 ClangUtil::GetQualType(full_type).getTypePtr())) {
8629 if (clang::ObjCInterfaceDecl *interface_decl = objc_obj->getInterface()) {
8630 interface_decl->dump(s.AsRawOstream());
8631 continue;
8632 }
8633 }
8635 .dump(s.AsRawOstream(), getASTContext());
8636 }
8637}
8638
8639static bool DumpEnumValue(const clang::QualType &qual_type, Stream &s,
8640 const DataExtractor &data, lldb::offset_t byte_offset,
8641 size_t byte_size, uint32_t bitfield_bit_offset,
8642 uint32_t bitfield_bit_size) {
8643 const clang::EnumType *enutype =
8644 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8645 const clang::EnumDecl *enum_decl = enutype->getDecl();
8646 assert(enum_decl);
8647 lldb::offset_t offset = byte_offset;
8648 bool qual_type_is_signed = qual_type->isSignedIntegerOrEnumerationType();
8649 const uint64_t enum_svalue =
8650 qual_type_is_signed
8651 ? data.GetMaxS64Bitfield(&offset, byte_size, bitfield_bit_size,
8652 bitfield_bit_offset)
8653 : data.GetMaxU64Bitfield(&offset, byte_size, bitfield_bit_size,
8654 bitfield_bit_offset);
8655 bool can_be_bitfield = true;
8656 uint64_t covered_bits = 0;
8657 int num_enumerators = 0;
8658
8659 // Try to find an exact match for the value.
8660 // At the same time, we're applying a heuristic to determine whether we want
8661 // to print this enum as a bitfield. We're likely dealing with a bitfield if
8662 // every enumerator is either a one bit value or a superset of the previous
8663 // enumerators. Also 0 doesn't make sense when the enumerators are used as
8664 // flags.
8665 clang::EnumDecl::enumerator_range enumerators = enum_decl->enumerators();
8666 if (enumerators.empty())
8667 can_be_bitfield = false;
8668 else {
8669 for (auto *enumerator : enumerators) {
8670 llvm::APSInt init_val = enumerator->getInitVal();
8671 uint64_t val = qual_type_is_signed ? init_val.getSExtValue()
8672 : init_val.getZExtValue();
8673 if (qual_type_is_signed)
8674 val = llvm::SignExtend64(val, 8 * byte_size);
8675 if (llvm::popcount(val) != 1 && (val & ~covered_bits) != 0)
8676 can_be_bitfield = false;
8677 covered_bits |= val;
8678 ++num_enumerators;
8679 if (val == enum_svalue) {
8680 // Found an exact match, that's all we need to do.
8681 s.PutCString(enumerator->getNameAsString());
8682 return true;
8683 }
8684 }
8685 }
8686
8687 // Unsigned values make more sense for flags.
8688 offset = byte_offset;
8689 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
8690 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
8691
8692 // No exact match, but we don't think this is a bitfield. Print the value as
8693 // decimal.
8694 if (!can_be_bitfield) {
8695 if (qual_type_is_signed)
8696 s.Printf("%" PRIi64, enum_svalue);
8697 else
8698 s.Printf("%" PRIu64, enum_uvalue);
8699 return true;
8700 }
8701
8702 if (!enum_uvalue) {
8703 // This is a bitfield enum, but the value is 0 so we know it won't match
8704 // with any of the enumerators.
8705 s.Printf("0x%" PRIx64, enum_uvalue);
8706 return true;
8707 }
8708
8709 uint64_t remaining_value = enum_uvalue;
8710 std::vector<std::pair<uint64_t, llvm::StringRef>> values;
8711 values.reserve(num_enumerators);
8712 for (auto *enumerator : enum_decl->enumerators())
8713 if (auto val = enumerator->getInitVal().getZExtValue())
8714 values.emplace_back(val, enumerator->getName());
8715
8716 // Sort in reverse order of the number of the population count, so that in
8717 // `enum {A, B, ALL = A|B }` we visit ALL first. Use a stable sort so that
8718 // A | C where A is declared before C is displayed in this order.
8719 std::stable_sort(values.begin(), values.end(),
8720 [](const auto &a, const auto &b) {
8721 return llvm::popcount(a.first) > llvm::popcount(b.first);
8722 });
8723
8724 for (const auto &val : values) {
8725 if ((remaining_value & val.first) != val.first)
8726 continue;
8727 remaining_value &= ~val.first;
8728 s.PutCString(val.second);
8729 if (remaining_value)
8730 s.PutCString(" | ");
8731 }
8732
8733 // If there is a remainder that is not covered by the value, print it as
8734 // hex.
8735 if (remaining_value)
8736 s.Printf("0x%" PRIx64, remaining_value);
8737
8738 return true;
8739}
8740
8743 const lldb_private::DataExtractor &data, lldb::offset_t byte_offset,
8744 size_t byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
8745 ExecutionContextScope *exe_scope) {
8746 if (!type)
8747 return false;
8748 if (IsAggregateType(type)) {
8749 return false;
8750 } else {
8751 clang::QualType qual_type(GetQualType(type));
8752
8753 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8754
8755 if (type_class == clang::Type::Elaborated) {
8756 qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
8757 return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size,
8758 bitfield_bit_size, bitfield_bit_offset, exe_scope);
8759 }
8760
8761 switch (type_class) {
8762 case clang::Type::Typedef: {
8763 clang::QualType typedef_qual_type =
8764 llvm::cast<clang::TypedefType>(qual_type)
8765 ->getDecl()
8766 ->getUnderlyingType();
8767 CompilerType typedef_clang_type = GetType(typedef_qual_type);
8768 if (format == eFormatDefault)
8769 format = typedef_clang_type.GetFormat();
8770 clang::TypeInfo typedef_type_info =
8771 getASTContext().getTypeInfo(typedef_qual_type);
8772 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
8773
8774 return typedef_clang_type.DumpTypeValue(
8775 &s,
8776 format, // The format with which to display the element
8777 data, // Data buffer containing all bytes for this type
8778 byte_offset, // Offset into "data" where to grab value from
8779 typedef_byte_size, // Size of this type in bytes
8780 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
8781 // treat as a bitfield
8782 bitfield_bit_offset, // Offset in bits of a bitfield value if
8783 // bitfield_bit_size != 0
8784 exe_scope);
8785 } break;
8786
8787 case clang::Type::Enum:
8788 // If our format is enum or default, show the enumeration value as its
8789 // enumeration string value, else just display it as requested.
8790 if ((format == eFormatEnum || format == eFormatDefault) &&
8791 GetCompleteType(type))
8792 return DumpEnumValue(qual_type, s, data, byte_offset, byte_size,
8793 bitfield_bit_offset, bitfield_bit_size);
8794 // format was not enum, just fall through and dump the value as
8795 // requested....
8796 [[fallthrough]];
8797
8798 default:
8799 // We are down to a scalar type that we just need to display.
8800 {
8801 uint32_t item_count = 1;
8802 // A few formats, we might need to modify our size and count for
8803 // depending
8804 // on how we are trying to display the value...
8805 switch (format) {
8806 default:
8807 case eFormatBoolean:
8808 case eFormatBinary:
8809 case eFormatComplex:
8810 case eFormatCString: // NULL terminated C strings
8811 case eFormatDecimal:
8812 case eFormatEnum:
8813 case eFormatHex:
8815 case eFormatFloat:
8816 case eFormatOctal:
8817 case eFormatOSType:
8818 case eFormatUnsigned:
8819 case eFormatPointer:
8832 break;
8833
8834 case eFormatChar:
8836 case eFormatCharArray:
8837 case eFormatBytes:
8838 case eFormatUnicode8:
8840 item_count = byte_size;
8841 byte_size = 1;
8842 break;
8843
8844 case eFormatUnicode16:
8845 item_count = byte_size / 2;
8846 byte_size = 2;
8847 break;
8848
8849 case eFormatUnicode32:
8850 item_count = byte_size / 4;
8851 byte_size = 4;
8852 break;
8853 }
8854 return DumpDataExtractor(data, &s, byte_offset, format, byte_size,
8855 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
8856 bitfield_bit_size, bitfield_bit_offset,
8857 exe_scope);
8858 }
8859 break;
8860 }
8861 }
8862 return false;
8863}
8864
8866 lldb::DescriptionLevel level) {
8867 StreamFile s(stdout, false);
8868 DumpTypeDescription(type, s, level);
8869
8870 CompilerType ct(weak_from_this(), type);
8871 const clang::Type *clang_type = ClangUtil::GetQualType(ct).getTypePtr();
8872 if (std::optional<ClangASTMetadata> metadata = GetMetadata(clang_type)) {
8873 metadata->Dump(&s);
8874 }
8875}
8876
8878 Stream &s,
8879 lldb::DescriptionLevel level) {
8880 if (type) {
8881 clang::QualType qual_type =
8882 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
8883
8884 llvm::SmallVector<char, 1024> buf;
8885 llvm::raw_svector_ostream llvm_ostrm(buf);
8886
8887 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8888 switch (type_class) {
8889 case clang::Type::ObjCObject:
8890 case clang::Type::ObjCInterface: {
8891 GetCompleteType(type);
8892
8893 auto *objc_class_type =
8894 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8895 assert(objc_class_type);
8896 if (!objc_class_type)
8897 break;
8898 clang::ObjCInterfaceDecl *class_interface_decl =
8899 objc_class_type->getInterface();
8900 if (!class_interface_decl)
8901 break;
8902 if (level == eDescriptionLevelVerbose)
8903 class_interface_decl->dump(llvm_ostrm);
8904 else
8905 class_interface_decl->print(llvm_ostrm,
8906 getASTContext().getPrintingPolicy(),
8907 s.GetIndentLevel());
8908 } break;
8909
8910 case clang::Type::Typedef: {
8911 auto *typedef_type = qual_type->getAs<clang::TypedefType>();
8912 if (!typedef_type)
8913 break;
8914 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
8915 if (level == eDescriptionLevelVerbose)
8916 typedef_decl->dump(llvm_ostrm);
8917 else {
8918 std::string clang_typedef_name(GetTypeNameForDecl(typedef_decl));
8919 if (!clang_typedef_name.empty()) {
8920 s.PutCString("typedef ");
8921 s.PutCString(clang_typedef_name);
8922 }
8923 }
8924 } break;
8925
8926 case clang::Type::Record: {
8927 GetCompleteType(type);
8928
8929 auto *record_type = llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8930 const clang::RecordDecl *record_decl = record_type->getDecl();
8931 if (level == eDescriptionLevelVerbose)
8932 record_decl->dump(llvm_ostrm);
8933 else {
8934 record_decl->print(llvm_ostrm, getASTContext().getPrintingPolicy(),
8935 s.GetIndentLevel());
8936 }
8937 } break;
8938
8939 default: {
8940 if (auto *tag_type =
8941 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr())) {
8942 if (clang::TagDecl *tag_decl = tag_type->getDecl()) {
8943 if (level == eDescriptionLevelVerbose)
8944 tag_decl->dump(llvm_ostrm);
8945 else
8946 tag_decl->print(llvm_ostrm, 0);
8947 }
8948 } else {
8949 if (level == eDescriptionLevelVerbose)
8950 qual_type->dump(llvm_ostrm, getASTContext());
8951 else {
8952 std::string clang_type_name(qual_type.getAsString());
8953 if (!clang_type_name.empty())
8954 s.PutCString(clang_type_name);
8955 }
8956 }
8957 }
8958 }
8959
8960 if (buf.size() > 0) {
8961 s.Write(buf.data(), buf.size());
8962 }
8963}
8964}
8965
8967 if (ClangUtil::IsClangType(type)) {
8968 clang::QualType qual_type(
8970
8971 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8972 switch (type_class) {
8973 case clang::Type::Record: {
8974 const clang::CXXRecordDecl *cxx_record_decl =
8975 qual_type->getAsCXXRecordDecl();
8976 if (cxx_record_decl)
8977 printf("class %s", cxx_record_decl->getName().str().c_str());
8978 } break;
8979
8980 case clang::Type::Enum: {
8981 clang::EnumDecl *enum_decl =
8982 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8983 if (enum_decl) {
8984 printf("enum %s", enum_decl->getName().str().c_str());
8985 }
8986 } break;
8987
8988 case clang::Type::ObjCObject:
8989 case clang::Type::ObjCInterface: {
8990 const clang::ObjCObjectType *objc_class_type =
8991 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
8992 if (objc_class_type) {
8993 clang::ObjCInterfaceDecl *class_interface_decl =
8994 objc_class_type->getInterface();
8995 // We currently can't complete objective C types through the newly
8996 // added ASTContext because it only supports TagDecl objects right
8997 // now...
8998 if (class_interface_decl)
8999 printf("@class %s", class_interface_decl->getName().str().c_str());
9000 }
9001 } break;
9002
9003 case clang::Type::Typedef:
9004 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9005 ->getDecl()
9006 ->getName()
9007 .str()
9008 .c_str());
9009 break;
9010
9011 case clang::Type::Auto:
9012 printf("auto ");
9014 llvm::cast<clang::AutoType>(qual_type)
9015 ->getDeducedType()
9016 .getAsOpaquePtr()));
9017
9018 case clang::Type::Elaborated:
9019 printf("elaborated ");
9021 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9022 ->getNamedType()
9023 .getAsOpaquePtr()));
9024
9025 case clang::Type::Paren:
9026 printf("paren ");
9028 type.GetTypeSystem(),
9029 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9030
9031 default:
9032 printf("TypeSystemClang::DumpTypeName() type_class = %u", type_class);
9033 break;
9034 }
9035 }
9036}
9037
9039 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
9040 lldb::AccessType access_type, const char *parent_name, int tag_decl_kind,
9041 const TypeSystemClang::TemplateParameterInfos &template_param_infos) {
9042 if (template_param_infos.IsValid()) {
9043 std::string template_basename(parent_name);
9044 // With -gsimple-template-names we may omit template parameters in the name.
9045 if (auto i = template_basename.find('<'); i != std::string::npos)
9046 template_basename.erase(i);
9047
9048 return CreateClassTemplateDecl(decl_ctx, owning_module, access_type,
9049 template_basename.c_str(), tag_decl_kind,
9050 template_param_infos);
9051 }
9052 return nullptr;
9053}
9054
9055void TypeSystemClang::CompleteTagDecl(clang::TagDecl *decl) {
9056 SymbolFile *sym_file = GetSymbolFile();
9057 if (sym_file) {
9058 CompilerType clang_type = GetTypeForDecl(decl);
9059 if (clang_type)
9060 sym_file->CompleteType(clang_type);
9061 }
9062}
9063
9065 clang::ObjCInterfaceDecl *decl) {
9066 SymbolFile *sym_file = GetSymbolFile();
9067 if (sym_file) {
9068 CompilerType clang_type = GetTypeForDecl(decl);
9069 if (clang_type)
9070 sym_file->CompleteType(clang_type);
9071 }
9072}
9073
9076 m_dwarf_ast_parser_up = std::make_unique<DWARFASTParserClang>(*this);
9077 return m_dwarf_ast_parser_up.get();
9078}
9079
9082 m_pdb_ast_parser_up = std::make_unique<PDBASTParser>(*this);
9083 return m_pdb_ast_parser_up.get();
9084}
9085
9088 m_native_pdb_ast_parser_up = std::make_unique<npdb::PdbAstBuilder>(*this);
9089 return m_native_pdb_ast_parser_up.get();
9090}
9091
9093 const clang::RecordDecl *record_decl, uint64_t &bit_size,
9094 uint64_t &alignment,
9095 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9096 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9097 &base_offsets,
9098 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9099 &vbase_offsets) {
9100 lldb_private::ClangASTImporter *importer = nullptr;
9102 importer = &m_dwarf_ast_parser_up->GetClangASTImporter();
9103 if (!importer && m_pdb_ast_parser_up)
9104 importer = &m_pdb_ast_parser_up->GetClangASTImporter();
9105 if (!importer && m_native_pdb_ast_parser_up)
9106 importer = &m_native_pdb_ast_parser_up->GetClangASTImporter();
9107 if (!importer)
9108 return false;
9109
9110 return importer->LayoutRecordType(record_decl, bit_size, alignment,
9111 field_offsets, base_offsets, vbase_offsets);
9112}
9113
9114// CompilerDecl override functions
9115
9117 if (opaque_decl) {
9118 clang::NamedDecl *nd =
9119 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9120 if (nd != nullptr)
9121 return ConstString(nd->getDeclName().getAsString());
9122 }
9123 return ConstString();
9124}
9125
9127 if (opaque_decl) {
9128 clang::NamedDecl *nd =
9129 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9130 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9131 clang::MangleContext *mc = getMangleContext();
9132 if (mc && mc->shouldMangleCXXName(nd)) {
9133 llvm::SmallVector<char, 1024> buf;
9134 llvm::raw_svector_ostream llvm_ostrm(buf);
9135 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9136 mc->mangleName(
9137 clang::GlobalDecl(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9138 Ctor_Complete),
9139 llvm_ostrm);
9140 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9141 mc->mangleName(
9142 clang::GlobalDecl(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9143 Dtor_Complete),
9144 llvm_ostrm);
9145 } else {
9146 mc->mangleName(nd, llvm_ostrm);
9147 }
9148 if (buf.size() > 0)
9149 return ConstString(buf.data(), buf.size());
9150 }
9151 }
9152 }
9153 return ConstString();
9154}
9155
9157 if (opaque_decl)
9158 return CreateDeclContext(((clang::Decl *)opaque_decl)->getDeclContext());
9159 return CompilerDeclContext();
9160}
9161
9163 if (clang::FunctionDecl *func_decl =
9164 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9165 return GetType(func_decl->getReturnType());
9166 if (clang::ObjCMethodDecl *objc_method =
9167 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9168 return GetType(objc_method->getReturnType());
9169 else
9170 return CompilerType();
9171}
9172
9174 if (clang::FunctionDecl *func_decl =
9175 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9176 return func_decl->param_size();
9177 if (clang::ObjCMethodDecl *objc_method =
9178 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9179 return objc_method->param_size();
9180 else
9181 return 0;
9182}
9183
9184static CompilerContextKind GetCompilerKind(clang::Decl::Kind clang_kind,
9185 clang::DeclContext const *decl_ctx) {
9186 switch (clang_kind) {
9187 case Decl::TranslationUnit:
9189 case Decl::Namespace:
9191 case Decl::Var:
9193 case Decl::Enum:
9195 case Decl::Typedef:
9197 default:
9198 // Many other kinds have multiple values
9199 if (decl_ctx) {
9200 if (decl_ctx->isFunctionOrMethod())
9202 if (decl_ctx->isRecord())
9204 }
9205 break;
9206 }
9208}
9209
9210static void
9211InsertCompilerContext(TypeSystemClang *ts, clang::DeclContext *decl_ctx,
9212 std::vector<lldb_private::CompilerContext> &context) {
9213 if (decl_ctx == nullptr)
9214 return;
9215 InsertCompilerContext(ts, decl_ctx->getParent(), context);
9216 clang::Decl::Kind clang_kind = decl_ctx->getDeclKind();
9217 if (clang_kind == Decl::TranslationUnit)
9218 return; // Stop at the translation unit.
9219 const CompilerContextKind compiler_kind =
9220 GetCompilerKind(clang_kind, decl_ctx);
9221 ConstString decl_ctx_name = ts->DeclContextGetName(decl_ctx);
9222 context.push_back({compiler_kind, decl_ctx_name});
9223}
9224
9225std::vector<lldb_private::CompilerContext>
9227 std::vector<lldb_private::CompilerContext> context;
9228 ConstString decl_name = DeclGetName(opaque_decl);
9229 if (decl_name) {
9230 clang::Decl *decl = (clang::Decl *)opaque_decl;
9231 // Add the entire decl context first
9232 clang::DeclContext *decl_ctx = decl->getDeclContext();
9233 InsertCompilerContext(this, decl_ctx, context);
9234 // Now add the decl information
9235 auto compiler_kind =
9236 GetCompilerKind(decl->getKind(), dyn_cast<DeclContext>(decl));
9237 context.push_back({compiler_kind, decl_name});
9238 }
9239 return context;
9240}
9241
9243 size_t idx) {
9244 if (clang::FunctionDecl *func_decl =
9245 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9246 if (idx < func_decl->param_size()) {
9247 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9248 if (var_decl)
9249 return GetType(var_decl->getOriginalType());
9250 }
9251 } else if (clang::ObjCMethodDecl *objc_method =
9252 llvm::dyn_cast<clang::ObjCMethodDecl>(
9253 (clang::Decl *)opaque_decl)) {
9254 if (idx < objc_method->param_size())
9255 return GetType(objc_method->parameters()[idx]->getOriginalType());
9256 }
9257 return CompilerType();
9258}
9259
9261 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
9262 clang::VarDecl *var_decl = llvm::dyn_cast<clang::VarDecl>(decl);
9263 if (!var_decl)
9264 return Scalar();
9265 clang::Expr *init_expr = var_decl->getInit();
9266 if (!init_expr)
9267 return Scalar();
9268 std::optional<llvm::APSInt> value =
9269 init_expr->getIntegerConstantExpr(getASTContext());
9270 if (!value)
9271 return Scalar();
9272 return Scalar(*value);
9273}
9274
9275// CompilerDeclContext functions
9276
9278 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9279 std::vector<CompilerDecl> found_decls;
9280 SymbolFile *symbol_file = GetSymbolFile();
9281 if (opaque_decl_ctx && symbol_file) {
9282 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9283 std::set<DeclContext *> searched;
9284 std::multimap<DeclContext *, DeclContext *> search_queue;
9285
9286 for (clang::DeclContext *decl_context = root_decl_ctx;
9287 decl_context != nullptr && found_decls.empty();
9288 decl_context = decl_context->getParent()) {
9289 search_queue.insert(std::make_pair(decl_context, decl_context));
9290
9291 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9292 it++) {
9293 if (!searched.insert(it->second).second)
9294 continue;
9295 symbol_file->ParseDeclsForContext(
9296 CreateDeclContext(it->second));
9297
9298 for (clang::Decl *child : it->second->decls()) {
9299 if (clang::UsingDirectiveDecl *ud =
9300 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9301 if (ignore_using_decls)
9302 continue;
9303 clang::DeclContext *from = ud->getCommonAncestor();
9304 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9305 search_queue.insert(
9306 std::make_pair(from, ud->getNominatedNamespace()));
9307 } else if (clang::UsingDecl *ud =
9308 llvm::dyn_cast<clang::UsingDecl>(child)) {
9309 if (ignore_using_decls)
9310 continue;
9311 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9312 clang::Decl *target = usd->getTargetDecl();
9313 if (clang::NamedDecl *nd =
9314 llvm::dyn_cast<clang::NamedDecl>(target)) {
9315 IdentifierInfo *ii = nd->getIdentifier();
9316 if (ii != nullptr && ii->getName() == name.AsCString(nullptr))
9317 found_decls.push_back(GetCompilerDecl(nd));
9318 }
9319 }
9320 } else if (clang::NamedDecl *nd =
9321 llvm::dyn_cast<clang::NamedDecl>(child)) {
9322 IdentifierInfo *ii = nd->getIdentifier();
9323 if (ii != nullptr && ii->getName() == name.AsCString(nullptr))
9324 found_decls.push_back(GetCompilerDecl(nd));
9325 }
9326 }
9327 }
9328 }
9329 }
9330 return found_decls;
9331}
9332
9333// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
9334// and return the number of levels it took to find it, or
9335// LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using
9336// declaration, its name and/or type, if set, will be used to check that the
9337// decl found in the scope is a match.
9338//
9339// The optional name is required by languages (like C++) to handle using
9340// declarations like:
9341//
9342// void poo();
9343// namespace ns {
9344// void foo();
9345// void goo();
9346// }
9347// void bar() {
9348// using ns::foo;
9349// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9350// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9351// }
9352//
9353// The optional type is useful in the case that there's a specific overload
9354// that we're looking for that might otherwise be shadowed, like:
9355//
9356// void foo(int);
9357// namespace ns {
9358// void foo();
9359// }
9360// void bar() {
9361// using ns::foo;
9362// // CountDeclLevels returns 0 for { 'foo', void() },
9363// // 1 for { 'foo', void(int) }, and
9364// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9365// }
9366//
9367// NOTE: Because file statics are at the TranslationUnit along with globals, a
9368// function at file scope will return the same level as a function at global
9369// scope. Ideally we'd like to treat the file scope as an additional scope just
9370// below the global scope. More work needs to be done to recognise that, if
9371// the decl we're trying to look up is static, we should compare its source
9372// file with that of the current scope and return a lower number for it.
9373uint32_t TypeSystemClang::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9374 clang::DeclContext *child_decl_ctx,
9375 ConstString *child_name,
9376 CompilerType *child_type) {
9377 SymbolFile *symbol_file = GetSymbolFile();
9378 if (frame_decl_ctx && symbol_file) {
9379 std::set<DeclContext *> searched;
9380 std::multimap<DeclContext *, DeclContext *> search_queue;
9381
9382 // Get the lookup scope for the decl we're trying to find.
9383 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
9384
9385 // Look for it in our scope's decl context and its parents.
9386 uint32_t level = 0;
9387 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9388 decl_ctx = decl_ctx->getParent()) {
9389 if (!decl_ctx->isLookupContext())
9390 continue;
9391 if (decl_ctx == parent_decl_ctx)
9392 // Found it!
9393 return level;
9394 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9395 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9396 it++) {
9397 if (searched.find(it->second) != searched.end())
9398 continue;
9399
9400 // Currently DWARF has one shared translation unit for all Decls at top
9401 // level, so this would erroneously find using statements anywhere. So
9402 // don't look at the top-level translation unit.
9403 // TODO fix this and add a testcase that depends on it.
9404
9405 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9406 continue;
9407
9408 searched.insert(it->second);
9409 symbol_file->ParseDeclsForContext(
9410 CreateDeclContext(it->second));
9411
9412 for (clang::Decl *child : it->second->decls()) {
9413 if (clang::UsingDirectiveDecl *ud =
9414 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9415 clang::DeclContext *ns = ud->getNominatedNamespace();
9416 if (ns == parent_decl_ctx)
9417 // Found it!
9418 return level;
9419 clang::DeclContext *from = ud->getCommonAncestor();
9420 if (searched.find(ns) == searched.end())
9421 search_queue.insert(std::make_pair(from, ns));
9422 } else if (child_name) {
9423 if (clang::UsingDecl *ud =
9424 llvm::dyn_cast<clang::UsingDecl>(child)) {
9425 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9426 clang::Decl *target = usd->getTargetDecl();
9427 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9428 if (!nd)
9429 continue;
9430 // Check names.
9431 IdentifierInfo *ii = nd->getIdentifier();
9432 if (ii == nullptr ||
9433 ii->getName() != child_name->AsCString(nullptr))
9434 continue;
9435 // Check types, if one was provided.
9436 if (child_type) {
9437 CompilerType clang_type = GetTypeForDecl(nd);
9438 if (!AreTypesSame(clang_type, *child_type,
9439 /*ignore_qualifiers=*/true))
9440 continue;
9441 }
9442 // Found it!
9443 return level;
9444 }
9445 }
9446 }
9447 }
9448 }
9449 ++level;
9450 }
9451 }
9453}
9454
9456 if (opaque_decl_ctx) {
9457 clang::NamedDecl *named_decl =
9458 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9459 if (named_decl) {
9460 std::string name;
9461 llvm::raw_string_ostream stream{name};
9462 auto policy = GetTypePrintingPolicy();
9463 policy.AlwaysIncludeTypeForTemplateArgument = true;
9464 named_decl->getNameForDiagnostic(stream, policy, /*qualified=*/false);
9465 return ConstString(name);
9466 }
9467 }
9468 return ConstString();
9469}
9470
9473 if (opaque_decl_ctx) {
9474 clang::NamedDecl *named_decl =
9475 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9476 if (named_decl)
9477 return ConstString(GetTypeNameForDecl(named_decl));
9478 }
9479 return ConstString();
9480}
9481
9483 if (!opaque_decl_ctx)
9484 return false;
9485
9486 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9487 if (llvm::isa<clang::ObjCMethodDecl>(decl_ctx)) {
9488 return true;
9489 } else if (llvm::isa<clang::CXXMethodDecl>(decl_ctx)) {
9490 return true;
9491 } else if (clang::FunctionDecl *fun_decl =
9492 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9493 if (std::optional<ClangASTMetadata> metadata = GetMetadata(fun_decl))
9494 return metadata->HasObjectPtr();
9495 }
9496
9497 return false;
9498}
9499
9500std::vector<lldb_private::CompilerContext>
9502 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9503 std::vector<lldb_private::CompilerContext> context;
9504 InsertCompilerContext(this, decl_ctx, context);
9505 return context;
9506}
9507
9509 void *opaque_decl_ctx, void *other_opaque_decl_ctx) {
9510 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9511 auto *other = (clang::DeclContext *)other_opaque_decl_ctx;
9512
9513 // If we have an inline or anonymous namespace, then the lookup of the
9514 // parent context also includes those namespace contents.
9515 auto is_transparent_lookup_allowed = [](clang::DeclContext *DC) {
9516 if (DC->isInlineNamespace())
9517 return true;
9518
9519 if (auto const *NS = dyn_cast<NamespaceDecl>(DC))
9520 return NS->isAnonymousNamespace();
9521
9522 return false;
9523 };
9524
9525 do {
9526 // A decl context always includes its own contents in its lookup.
9527 if (decl_ctx == other)
9528 return true;
9529 } while (is_transparent_lookup_allowed(other) &&
9530 (other = other->getParent()));
9531
9532 return false;
9533}
9534
9537 if (!opaque_decl_ctx)
9538 return eLanguageTypeUnknown;
9539
9540 auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9541 if (llvm::isa<clang::ObjCMethodDecl>(decl_ctx)) {
9542 return eLanguageTypeObjC;
9543 } else if (llvm::isa<clang::CXXMethodDecl>(decl_ctx)) {
9545 } else if (auto *fun_decl = llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9546 if (std::optional<ClangASTMetadata> metadata = GetMetadata(fun_decl))
9547 return metadata->GetObjectPtrLanguage();
9548 }
9549
9550 return eLanguageTypeUnknown;
9551}
9552
9554 return dc.IsValid() && isa<TypeSystemClang>(dc.GetTypeSystem());
9555}
9556
9557clang::DeclContext *
9559 if (IsClangDeclContext(dc))
9560 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9561 return nullptr;
9562}
9563
9564ObjCMethodDecl *
9566 if (IsClangDeclContext(dc))
9567 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9568 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9569 return nullptr;
9570}
9571
9572CXXMethodDecl *
9574 if (IsClangDeclContext(dc))
9575 return llvm::dyn_cast<clang::CXXMethodDecl>(
9576 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9577 return nullptr;
9578}
9579
9580clang::FunctionDecl *
9582 if (IsClangDeclContext(dc))
9583 return llvm::dyn_cast<clang::FunctionDecl>(
9584 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9585 return nullptr;
9586}
9587
9588clang::NamespaceDecl *
9590 if (IsClangDeclContext(dc))
9591 return llvm::dyn_cast<clang::NamespaceDecl>(
9592 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9593 return nullptr;
9594}
9595
9596std::optional<ClangASTMetadata>
9598 const Decl *object) {
9599 TypeSystemClang *ast = llvm::cast<TypeSystemClang>(dc.GetTypeSystem());
9600 return ast->GetMetadata(object);
9601}
9602
9603clang::ASTContext *
9605 TypeSystemClang *ast =
9606 llvm::dyn_cast_or_null<TypeSystemClang>(dc.GetTypeSystem());
9607 if (ast)
9608 return &ast->getASTContext();
9609 return nullptr;
9610}
9611
9613 // Technically, enums can be incomplete too, but we don't handle those as they
9614 // are emitted even under -flimit-debug-info.
9616 return;
9617
9618 if (type.GetCompleteType())
9619 return;
9620
9621 // No complete definition in this module. Mark the class as complete to
9622 // satisfy local ast invariants, but make a note of the fact that
9623 // it is not _really_ complete so we can later search for a definition in a
9624 // different module.
9625 // Since we provide layout assistance, layouts of types containing this class
9626 // will be correct even if we are not able to find the definition elsewhere.
9628 lldbassert(started && "Unable to start a class type definition.");
9630 const clang::TagDecl *td = ClangUtil::GetAsTagDecl(type);
9632 if (ts)
9634}
9635
9636namespace {
9637/// A specialized scratch AST used within ScratchTypeSystemClang.
9638/// These are the ASTs backing the different IsolatedASTKinds. They behave
9639/// like a normal ScratchTypeSystemClang but they don't own their own
9640/// persistent storage or target reference.
9641class SpecializedScratchAST : public TypeSystemClang {
9642public:
9643 /// \param name The display name of the TypeSystemClang instance.
9644 /// \param triple The triple used for the TypeSystemClang instance.
9645 /// \param ast_source The ClangASTSource that should be used to complete
9646 /// type information.
9647 SpecializedScratchAST(llvm::StringRef name, llvm::Triple triple,
9648 std::unique_ptr<ClangASTSource> ast_source)
9649 : TypeSystemClang(name, triple),
9650 m_scratch_ast_source_up(std::move(ast_source)) {
9651 // Setup the ClangASTSource to complete this AST.
9652 m_scratch_ast_source_up->InstallASTContext(*this);
9653 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
9654 m_scratch_ast_source_up->CreateProxy());
9655 SetExternalSource(proxy_ast_source);
9656 }
9657
9658 /// The ExternalASTSource that performs lookups and completes types.
9659 std::unique_ptr<ClangASTSource> m_scratch_ast_source_up;
9660};
9661} // namespace
9662
9664const std::nullopt_t ScratchTypeSystemClang::DefaultAST = std::nullopt;
9665
9667 llvm::Triple triple)
9668 : TypeSystemClang("scratch ASTContext", triple), m_triple(triple),
9669 m_target_wp(target.shared_from_this()),
9670 m_persistent_variables(
9671 new ClangPersistentVariables(target.shared_from_this())) {
9673 m_scratch_ast_source_up->InstallASTContext(*this);
9674 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
9675 m_scratch_ast_source_up->CreateProxy());
9676 SetExternalSource(proxy_ast_source);
9677}
9678
9682}
9683
9686 std::optional<IsolatedASTKind> ast_kind,
9687 bool create_on_demand) {
9688 auto type_system_or_err = target.GetScratchTypeSystemForLanguage(
9689 lldb::eLanguageTypeC, create_on_demand);
9690 if (auto err = type_system_or_err.takeError()) {
9691 LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err),
9692 "Couldn't get scratch TypeSystemClang");
9693 return nullptr;
9694 }
9695 auto ts_sp = *type_system_or_err;
9696 ScratchTypeSystemClang *scratch_ast =
9697 llvm::dyn_cast_or_null<ScratchTypeSystemClang>(ts_sp.get());
9698 if (!scratch_ast)
9699 return nullptr;
9700 // If no dedicated sub-AST was requested, just return the main AST.
9701 if (ast_kind == DefaultAST)
9702 return std::static_pointer_cast<TypeSystemClang>(ts_sp);
9703 // Search the sub-ASTs.
9704 return std::static_pointer_cast<TypeSystemClang>(
9705 scratch_ast->GetIsolatedAST(*ast_kind).shared_from_this());
9706}
9707
9708/// Returns a human-readable name that uniquely identifiers the sub-AST kind.
9709static llvm::StringRef
9711 switch (kind) {
9713 return "C++ modules";
9714 }
9715 llvm_unreachable("Unimplemented IsolatedASTKind?");
9716}
9717
9718void ScratchTypeSystemClang::Dump(llvm::raw_ostream &output) {
9719 // First dump the main scratch AST.
9720 output << "State of scratch Clang type system:\n";
9721 TypeSystemClang::Dump(output);
9722
9723 // Now sort the isolated sub-ASTs.
9724 typedef std::pair<IsolatedASTKey, TypeSystem *> KeyAndTS;
9725 std::vector<KeyAndTS> sorted_typesystems;
9726 for (const auto &a : m_isolated_asts)
9727 sorted_typesystems.emplace_back(a.first, a.second.get());
9728 llvm::stable_sort(sorted_typesystems, llvm::less_first());
9729
9730 // Dump each sub-AST too.
9731 for (const auto &a : sorted_typesystems) {
9732 IsolatedASTKind kind =
9733 static_cast<ScratchTypeSystemClang::IsolatedASTKind>(a.first);
9734 output << "State of scratch Clang type subsystem "
9735 << GetNameForIsolatedASTKind(kind) << ":\n";
9736 a.second->Dump(output);
9737 }
9738}
9739
9741 llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language,
9742 Expression::ResultType desired_type,
9743 const EvaluateExpressionOptions &options, ValueObject *ctx_obj) {
9744 TargetSP target_sp = m_target_wp.lock();
9745 if (!target_sp)
9746 return nullptr;
9747
9748 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
9749 desired_type, options, ctx_obj);
9750}
9751
9753 const CompilerType &return_type, const Address &function_address,
9754 const ValueList &arg_value_list, const char *name) {
9755 TargetSP target_sp = m_target_wp.lock();
9756 if (!target_sp)
9757 return nullptr;
9758
9759 Process *process = target_sp->GetProcessSP().get();
9760 if (!process)
9761 return nullptr;
9762
9763 return new ClangFunctionCaller(*process, return_type, function_address,
9764 arg_value_list, name);
9765}
9766
9767std::unique_ptr<UtilityFunction>
9769 std::string name) {
9770 TargetSP target_sp = m_target_wp.lock();
9771 if (!target_sp)
9772 return {};
9773
9774 return std::make_unique<ClangUtilityFunction>(
9775 *target_sp.get(), std::move(text), std::move(name),
9776 target_sp->GetDebugUtilityExpression());
9777}
9778
9781 return m_persistent_variables.get();
9782}
9783
9785 ClangASTImporter &importer) {
9786 // Remove it as a source from the main AST.
9787 importer.ForgetSource(&getASTContext(), src_ctx);
9788 // Remove it as a source from all created sub-ASTs.
9789 for (const auto &a : m_isolated_asts)
9790 importer.ForgetSource(&a.second->getASTContext(), src_ctx);
9791}
9792
9793std::unique_ptr<ClangASTSource> ScratchTypeSystemClang::CreateASTSource() {
9794 return std::make_unique<ClangASTSource>(
9795 m_target_wp.lock()->shared_from_this(),
9796 m_persistent_variables->GetClangASTImporter());
9797}
9798
9799static llvm::StringRef
9801 switch (feature) {
9803 return "scratch ASTContext for C++ module types";
9804 }
9805 llvm_unreachable("Unimplemented ASTFeature kind?");
9806}
9807
9810 auto found_ast = m_isolated_asts.find(feature);
9811 if (found_ast != m_isolated_asts.end())
9812 return *found_ast->second;
9813
9814 // Couldn't find the requested sub-AST, so create it now.
9815 std::shared_ptr<TypeSystemClang> new_ast_sp =
9816 std::make_shared<SpecializedScratchAST>(GetSpecializedASTName(feature),
9818 m_isolated_asts.insert({feature, new_ast_sp});
9819 return *new_ast_sp;
9820}
9821
9823 if (type) {
9824 clang::QualType qual_type(GetQualType(type));
9825 const clang::RecordType *record_type =
9826 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
9827 if (record_type) {
9828 const clang::RecordDecl *record_decl = record_type->getDecl();
9829 assert(record_decl);
9830 if (std::optional<ClangASTMetadata> metadata = GetMetadata(record_decl))
9831 return metadata->IsForcefullyCompleted();
9832 }
9833 }
9834 return false;
9835}
9836
9838 if (td == nullptr)
9839 return false;
9840 std::optional<ClangASTMetadata> metadata = GetMetadata(td);
9841 if (!metadata)
9842 return false;
9844 metadata->SetIsForcefullyCompleted();
9845 SetMetadata(td, *metadata);
9846
9847 return true;
9848}
9849
9851 if (auto *log = GetLog(LLDBLog::Expressions))
9852 LLDB_LOG(log, "Created new TypeSystem for (ASTContext*){0:x} '{1}'",
9854}
#define lldbassert(x)
Definition: LLDBAssert.h:15
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:359
#define LLDB_LOGF(log,...)
Definition: Log.h:366
#define LLDB_LOG_ERROR(log, error,...)
Definition: Log.h:382
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
static bool DumpEnumValue(const clang::QualType &qual_type, Stream &s, const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size, uint32_t bitfield_bit_offset, uint32_t bitfield_bit_size)
static lldb::opaque_compiler_type_t GetObjCFieldAtIndex(clang::ASTContext *ast, clang::ObjCInterfaceDecl *class_interface_decl, size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr)
static void ParseLangArgs(LangOptions &Opts, ArchSpec arch)
static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast, clang::QualType qual_type)
const TemplateArgument * GetNthTemplateArgument(const clang::ClassTemplateSpecializationDecl *decl, size_t idx, bool expand_pack)
static int64_t ReadVBaseOffsetFromVTable(Process &process, VTableContextBase &vtable_ctx, lldb::addr_t vtable_ptr, const CXXRecordDecl *cxx_record_decl, const CXXRecordDecl *base_class_decl)
static const clang::RecordType * GetCompleteRecordType(clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion)
Returns the clang::RecordType of the specified qual_type.
lldb_private::ThreadSafeDenseMap< clang::ASTContext *, TypeSystemClang * > ClangASTMap
static bool IsClangDeclContext(const CompilerDeclContext &dc)
static bool TemplateParameterAllowsValue(NamedDecl *param, const TemplateArgument &value)
Returns true if the given template parameter can represent the given value.
static CompilerContextKind GetCompilerKind(clang::Decl::Kind clang_kind, clang::DeclContext const *decl_ctx)
static QualType RemoveWrappingTypes(QualType type, ArrayRef< clang::Type::TypeClass > mask={})
Aggressively desugar the provided type, skipping past various kinds of syntactic sugar and other cons...
static TemplateParameterList * CreateTemplateParameterList(ASTContext &ast, const TypeSystemClang::TemplateParameterInfos &template_param_infos, llvm::SmallVector< NamedDecl *, 8 > &template_param_decls)
clang::DeclContext * FindLCABetweenDecls(clang::DeclContext *left, clang::DeclContext *right, clang::DeclContext *root)
static bool check_op_param(bool is_method, clang::OverloadedOperatorKind op_kind, bool unary, bool binary, uint32_t num_params)
static llvm::StringRef GetSpecializedASTName(ScratchTypeSystemClang::IsolatedASTKind feature)
static const clang::ObjCObjectType * GetCompleteObjCObjectType(clang::ASTContext *ast, QualType qual_type, bool allow_completion)
Returns the clang::ObjCObjectType of the specified qual_type.
static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
static lldb::addr_t GetVTableAddress(Process &process, VTableContextBase &vtable_ctx, ValueObject &valobj, const ASTRecordLayout &record_layout)
static clang::ObjCIvarDecl::AccessControl ConvertAccessTypeToObjCIvarAccessControl(AccessType access)
static std::optional< SymbolFile::ArrayInfo > GetDynamicArrayInfo(TypeSystemClang &ast, SymbolFile *sym_file, clang::QualType qual_type, const ExecutionContext *exe_ctx)
static llvm::StringRef GetNameForIsolatedASTKind(ScratchTypeSystemClang::IsolatedASTKind kind)
Returns a human-readable name that uniquely identifiers the sub-AST kind.
static void InsertCompilerContext(TypeSystemClang *ts, clang::DeclContext *decl_ctx, std::vector< lldb_private::CompilerContext > &context)
static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx, ValueObject &valobj, const ASTRecordLayout &record_layout, const CXXRecordDecl *cxx_record_decl, const CXXRecordDecl *base_class_decl, int32_t &bit_offset)
static const clang::EnumType * GetCompleteEnumType(clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion)
Returns the clang::EnumType of the specified qual_type.
static bool QualTypeMatchesBitSize(const uint64_t bit_size, ASTContext &ast, QualType qual_type)
static ClangASTMap & GetASTMap()
static bool GetCompleteQualType(clang::ASTContext *ast, clang::QualType qual_type, bool allow_completion=true)
static void SetMemberOwningModule(clang::Decl *member, const clang::Decl *parent)
static bool ClassTemplateAllowsToInstantiationArgs(ClassTemplateDecl *class_template_decl, const TypeSystemClang::TemplateParameterInfos &instantiation_values)
Returns true if the given class template declaration could produce an instantiation with the specifie...
#define LLDB_INVALID_DECL_LEVEL
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &info) override
DiagnosticConsumer * clone(DiagnosticsEngine &Diags) const
A section + offset based address class.
Definition: Address.h:62
An architecture specification class.
Definition: ArchSpec.h:31
bool IsValid() const
Tests if this ArchSpec is valid.
Definition: ArchSpec.h:348
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:450
bool CharIsSignedByDefault() const
Returns true if 'char' is a signed type by default in the architecture false otherwise.
Definition: ArchSpec.cpp:712
Manages and observes all Clang AST node importing in LLDB.
bool LayoutRecordType(const clang::RecordDecl *record_decl, uint64_t &bit_size, uint64_t &alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &field_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &vbase_offsets)
void ForgetSource(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx)
void SetUserID(lldb::user_id_t user_id)
"lldb/Expression/ClangFunctionCaller.h" Encapsulates a function that can be called.
"lldb/Expression/ClangPersistentVariables.h" Manages persistent values that need to be preserved betw...
"lldb/Expression/ClangUserExpression.h" Encapsulates a single expression for use with Clang
Represents a generic declaration context in a program.
Represents a generic declaration such as a function declaration.
Definition: CompilerDecl.h:28
lldb::TypeSystemSP GetSharedPointer() const
Definition: CompilerType.h:85
std::shared_ptr< TypeSystemType > dyn_cast_or_null()
Return a shared_ptr<TypeSystemType> if dyn_cast succeeds.
Definition: CompilerType.h:65
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
lldb::LanguageType GetMinimumLanguage()
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
void SetCompilerType(lldb::TypeSystemWP type_system, lldb::opaque_compiler_type_t type)
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
lldb::Encoding GetEncoding(uint64_t &count) const
size_t GetIndexOfChildMemberWithName(llvm::StringRef name, bool omit_empty_base_classes, std::vector< uint32_t > &child_indexes) const
Lookup a child member given a name.
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:287
LLVM_DUMP_METHOD void dump() const
Dumping types.
uint32_t GetNumDirectBaseClasses() const
ConstString GetTypeName(bool BaseOnly=false) const
uint32_t GetIndexOfChildWithName(llvm::StringRef name, bool omit_empty_base_classes) const
Lookup a child given a name.
bool DumpTypeValue(Stream *s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope)
lldb::Format GetFormat() const
llvm::Expected< CompilerType > GetChildCompilerTypeAtIndex(ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name, uint32_t &child_byte_size, int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags) const
CompilerType GetDirectBaseClassAtIndex(size_t idx, uint32_t *bit_offset_ptr) const
bool IsIntegerType(bool &is_signed) const
bool GetCompleteType() const
Type Completion.
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
std::optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
An data extractor class.
Definition: DataExtractor.h:48
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
uint64_t GetMaxU64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an unsigned integer of size byte_size from *offset_ptr, then extract the bitfield from this v...
uint32_t GetAddressByteSize() const
Get the current address size.
int64_t GetMaxS64Bitfield(lldb::offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
Extract an signed integer of size size from *offset_ptr, then extract and sign-extend the bitfield fr...
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
Process * GetProcessPtr() const
Returns a pointer to the process object.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
static FileSystem & Instance()
A class to manage flags.
Definition: Flags.h:22
bool Test(ValueType bit) const
Test a single flag bit.
Definition: Flags.h:96
bool AnySet(ValueType mask) const
Test one or more flags.
Definition: Flags.h:90
Encapsulates a function that can be called.
static bool LanguageIsC(lldb::LanguageType language)
Definition: Language.cpp:324
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition: Language.cpp:299
static bool LanguageIsPascal(lldb::LanguageType language)
Definition: Language.cpp:356
static bool LanguageIsObjC(lldb::LanguageType language)
Definition: Language.cpp:314
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
Definition: Module.cpp:1040
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:452
virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type, const char *ivar_name)
static ObjCLanguageRuntime * Get(Process &process)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A plug-in interface definition class for debugging a process.
Definition: Process.h:341
int64_t ReadSignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, int64_t fail_value, Status &error)
Definition: Process.cpp:2248
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition: Process.cpp:2259
uint32_t GetAddressByteSize() const
Definition: Process.cpp:3600
The TypeSystemClang instance used for the scratch ASTContext in a lldb::Target.
void Finalize() override
Free up any resources associated with this TypeSystem.
static lldb::TypeSystemClangSP GetForTarget(Target &target, std::optional< IsolatedASTKind > ast_kind=DefaultAST, bool create_on_demand=true)
Returns the scratch TypeSystemClang for the given target.
llvm::Triple m_triple
The target triple.
std::unique_ptr< ClangASTSource > CreateASTSource()
TypeSystemClang & GetIsolatedAST(IsolatedASTKind feature)
Returns the requested sub-AST.
UserExpression * GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language, Expression::ResultType desired_type, const EvaluateExpressionOptions &options, ValueObject *ctx_obj) override
std::unique_ptr< ClangASTSource > m_scratch_ast_source_up
The ExternalASTSource that performs lookups and completes minimally imported types.
IsolatedASTKind
The different kinds of isolated ASTs within the scratch TypeSystem.
@ CppModules
The isolated AST for declarations/types from expressions that imported type information from a C++ mo...
std::unique_ptr< ClangPersistentVariables > m_persistent_variables
The persistent variables associated with this process for the expression parser.
static char ID
LLVM RTTI support.
PersistentExpressionState * GetPersistentExpressionState() override
FunctionCaller * GetFunctionCaller(const CompilerType &return_type, const Address &function_address, const ValueList &arg_value_list, const char *name) override
std::unique_ptr< UtilityFunction > CreateUtilityFunction(std::string text, std::string name) override
void ForgetSource(clang::ASTContext *src_ctx, ClangASTImporter &importer)
Unregisters the given ASTContext as a source from the scratch AST (and all sub-ASTs).
void Dump(llvm::raw_ostream &output) override
static const std::nullopt_t DefaultAST
Alias for requesting the default scratch TypeSystemClang in GetForTarget.
ScratchTypeSystemClang(Target &target, llvm::Triple triple)
llvm::DenseMap< IsolatedASTKey, std::shared_ptr< TypeSystemClang > > m_isolated_asts
Map from IsolatedASTKind to their actual TypeSystemClang instance.
An error handling class.
Definition: Status.h:44
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
llvm::StringRef GetString() const
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
Definition: Stream.h:112
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
unsigned GetIndentLevel() const
Get the current indentation level.
Definition: Stream.cpp:187
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:50
virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx)
Definition: SymbolFile.h:235
virtual bool CompleteType(CompilerType &compiler_type)=0
virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope, lldb::TypeClass type_mask, lldb_private::TypeList &type_list)=0
virtual std::optional< ArrayInfo > GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx)=0
If type_uid points to an array type, return its characteristics.
llvm::Expected< lldb::TypeSystemSP > GetScratchTypeSystemForLanguage(lldb::LanguageType language, bool create_on_demand=true)
Definition: Target.cpp:2424
const ArchSpec & GetArchitecture() const
Definition: Target.h:1028
void Insert(_KeyType k, _ValueType v)
uint32_t GetSize() const
Definition: TypeList.cpp:60
lldb::TypeSP GetTypeAtIndex(uint32_t idx)
Definition: TypeList.cpp:66
The implementation of lldb::Type's m_payload field for TypeSystemClang.
void SetIsCompleteObjCClass(bool is_complete_objc_class)
Type::Payload m_payload
The payload is used for typedefs and ptrauth types.
void SetOwningModule(OptionalClangModuleID id)
static constexpr unsigned ObjCClassBit
llvm::ArrayRef< clang::TemplateArgument > GetParameterPackArgs() const
clang::TemplateArgument const & Front() const
TemplateParameterInfos const & GetParameterPack() const
llvm::ArrayRef< const char * > GetNames() const
llvm::ArrayRef< clang::TemplateArgument > GetArgs() const
A TypeSystem implementation based on Clang.
bool IsMemberFunctionPointerType(lldb::opaque_compiler_type_t type) override
clang::TranslationUnitDecl * GetTranslationUnitDecl()
size_t GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type, llvm::StringRef name, bool omit_empty_base_classes, std::vector< uint32_t > &child_indexes) override
static clang::TypedefNameDecl * GetAsTypedefDecl(const CompilerType &type)
std::string GetTypeNameForDecl(const clang::NamedDecl *named_decl, bool qualified=true)
Returns the internal type name for the given NamedDecl using the type printing policy.
static clang::ObjCInterfaceDecl * GetAsObjCInterfaceDecl(const CompilerType &type)
bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream &s, lldb::Format format, const DataExtractor &data, lldb::offset_t data_offset, size_t data_byte_size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, ExecutionContextScope *exe_scope) override
std::string m_display_name
A string describing what this TypeSystemClang represents (e.g., AST for debug information,...
ConstString GetTypeName(lldb::opaque_compiler_type_t type, bool base_only) override
static void SetOwningModule(clang::Decl *decl, OptionalClangModuleID owning_module)
Set the owning module for decl.
std::unique_ptr< clang::TargetInfo > m_target_info_up
std::unique_ptr< clang::LangOptions > m_language_options_up
Scalar DeclGetConstantValue(void *opaque_decl) override
bool BaseSpecifierIsEmpty(const clang::CXXBaseSpecifier *b)
static uint32_t GetNumPointeeChildren(clang::QualType type)
ConstString DeclGetMangledName(void *opaque_decl) override
CompilerType GetBasicType(lldb::BasicType type)
clang::ClassTemplateDecl * CreateClassTemplateDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef class_name, int kind, const TemplateParameterInfos &infos)
void SetExternalSource(llvm::IntrusiveRefCntPtr< clang::ExternalASTSource > &ast_source_up)
clang::UsingDecl * CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, OptionalClangModuleID owning_module, clang::NamedDecl *target)
static clang::AccessSpecifier ConvertAccessTypeToAccessSpecifier(lldb::AccessType access)
CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override
std::optional< uint64_t > GetByteSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope)
bool IsForcefullyCompleted(lldb::opaque_compiler_type_t type) override
bool SupportsLanguage(lldb::LanguageType language) override
uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override
OptionalClangModuleID GetOrCreateClangModule(llvm::StringRef name, OptionalClangModuleID parent, bool is_framework=false, bool is_explicit=false)
Synthesize a clang::Module and return its ID or a default-constructed ID.
void CompleteTagDecl(clang::TagDecl *)
static clang::FieldDecl * AddFieldToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &field_type, lldb::AccessType access, uint32_t bitfield_bit_size)
std::shared_ptr< clang::TargetOptions > & getTargetOptions()
static TypeSystemClang * GetASTContext(clang::ASTContext *ast_ctx)
bool IsReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type, bool *is_rvalue) override
CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size) override
TypeSystemClang(llvm::StringRef name, llvm::Triple triple)
Constructs a TypeSystemClang with an ASTContext using the given triple.
static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language, Module *module, Target *target)
clang::TargetInfo * getTargetInfo()
clang::FunctionTemplateDecl * CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::FunctionDecl *func_decl, const TemplateParameterInfos &infos)
CompilerType CreateArrayType(const CompilerType &element_type, std::optional< size_t > element_count, bool is_vector)
static bool AreTypesSame(CompilerType type1, CompilerType type2, bool ignore_qualifiers=false)
std::optional< uint64_t > GetBitSize(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override
CompilerType GetArrayType(lldb::opaque_compiler_type_t type, uint64_t size) override
bool IsFunctionType(lldb::opaque_compiler_type_t type) override
CompilerType GetFunctionReturnType(lldb::opaque_compiler_type_t type) override
std::optional< ClangASTMetadata > GetMetadata(const clang::Decl *object)
static bool IsObjCClassTypeAndHasIVars(const CompilerType &type, bool check_superclass)
CompilerType GetLValueReferenceType(lldb::opaque_compiler_type_t type) override
bool SetDeclIsForcefullyCompleted(const clang::TagDecl *td)
lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override
bool CanPassInRegisters(const CompilerType &type) override
CompilerDecl GetStaticFieldWithName(lldb::opaque_compiler_type_t type, llvm::StringRef name) override
static clang::DeclContext * GetDeclContextForType(clang::QualType type)
bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count, bool &is_complex) override
bool IsEnumerationType(lldb::opaque_compiler_type_t type, bool &is_signed) override
bool IsTemplateType(lldb::opaque_compiler_type_t type) override
CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack) override
static bool IsCXXClassType(const CompilerType &type)
bool IsIntegerType(lldb::opaque_compiler_type_t type, bool &is_signed) override
void SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object, clang::AccessSpecifier access)
uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override
static bool IsOperator(llvm::StringRef name, clang::OverloadedOperatorKind &op_kind)
bool IsCharType(lldb::opaque_compiler_type_t type) override
CompilerType CreateStructForIdentifier(llvm::StringRef type_name, const std::initializer_list< std::pair< const char *, CompilerType > > &type_fields, bool packed=false)
static void SetFloatingInitializerForVariable(clang::VarDecl *var, const llvm::APFloat &init_value)
Initializes a variable with a floating point value.
uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type, CompilerType *pointee_or_element_compiler_type) override
llvm::Expected< CompilerType > GetChildCompilerTypeAtIndex(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, bool transparent_pointers, bool omit_empty_base_classes, bool ignore_array_bounds, std::string &child_name, uint32_t &child_byte_size, int32_t &child_byte_offset, uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, bool &child_is_base_class, bool &child_is_deref_of_parent, ValueObject *valobj, uint64_t &language_flags) override
CompilerType GetType(clang::QualType qt)
Creates a CompilerType from the given QualType with the current TypeSystemClang instance as the Compi...
static clang::TagDecl * GetAsTagDecl(const CompilerType &type)
ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) override
bool TransferBaseClasses(lldb::opaque_compiler_type_t type, std::vector< std::unique_ptr< clang::CXXBaseSpecifier > > bases)
bool IsBeingDefined(lldb::opaque_compiler_type_t type) override
ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override
std::unique_ptr< clang::IdentifierTable > m_identifier_table_up
std::optional< uint64_t > GetObjCBitSize(clang::QualType qual_type, ExecutionContextScope *exe_scope)
static lldb::BasicType GetBasicTypeEnumeration(llvm::StringRef name)
static void SetIntegerInitializerForVariable(clang::VarDecl *var, const llvm::APInt &init_value)
Initializes a variable with an integer value.
bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override
CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) override
bool LayoutRecordType(const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment, llvm::DenseMap< const clang::FieldDecl *, uint64_t > &field_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &base_offsets, llvm::DenseMap< const clang::CXXRecordDecl *, clang::CharUnits > &vbase_offsets)
bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override
std::unique_ptr< clang::SourceManager > m_source_manager_up
bool IsVoidType(lldb::opaque_compiler_type_t type) override
static void SetIsPacked(const CompilerType &type)
void ForEachEnumerator(lldb::opaque_compiler_type_t type, std::function< bool(const CompilerType &integer_type, ConstString name, const llvm::APSInt &value)> const &callback) override
clang::AccessSpecifier GetCXXRecordDeclAccess(const clang::CXXRecordDecl *object)
CompilerType CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *class_template_specialization_decl)
bool IsPointerType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type) override
void CreateFunctionTemplateSpecializationInfo(clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template, const TemplateParameterInfos &infos)
llvm::StringRef getDisplayName() const
Returns the display name of this TypeSystemClang that indicates what purpose it serves in LLDB.
bool IsCStringType(lldb::opaque_compiler_type_t type, uint32_t &length)
CompilerType GetRValueReferenceType(lldb::opaque_compiler_type_t type) override
CompilerDecl GetCompilerDecl(clang::Decl *decl)
Creates a CompilerDecl from the given Decl with the current TypeSystemClang instance as its typesyste...
unsigned GetPtrAuthDiscriminator(lldb::opaque_compiler_type_t type) override
CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override
bool GetCompleteType(lldb::opaque_compiler_type_t type) override
bool IsBlockPointerType(lldb::opaque_compiler_type_t type, CompilerType *function_pointer_type_ptr) override
bool IsConst(lldb::opaque_compiler_type_t type) override
static clang::AccessSpecifier UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs)
std::unique_ptr< clang::CXXBaseSpecifier > CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, lldb::AccessType access, bool is_virtual, bool base_of_class)
CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override
std::vector< CompilerDecl > DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) override
uint32_t GetPointerByteSize() override
bool IsCompleteType(lldb::opaque_compiler_type_t type) override
void Dump(llvm::raw_ostream &output) override
CompilerType GetIntTypeFromBitSize(size_t bit_size, bool is_signed)
clang::MangleContext * getMangleContext()
void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *)
std::string PrintTemplateParams(const TemplateParameterInfos &template_param_infos)
Return the template parameters (including surrounding <>) in string form.
unsigned GetPtrAuthKey(lldb::opaque_compiler_type_t type) override
static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx)
CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx)
Creates a CompilerDeclContext from the given DeclContext with the current TypeSystemClang instance as...
CompilerType GetTypeForFormatters(void *type) override
void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id)
bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override
This is used by swift.
static LanguageSet GetSupportedLanguagesForExpressions()
CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override
CompilerDeclContext GetCompilerDeclContextForType(const CompilerType &type) override
Returns the direct parent context of specified type.
std::unique_ptr< clang::SelectorTable > m_selector_table_up
PDBASTParser * GetPDBParser() override
std::optional< CompilerType::IntegralTemplateArgument > GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack) override
bool DeclContextIsClassMethod(void *opaque_decl_ctx) override
void SetTargetTriple(llvm::StringRef target_triple)
CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) override
static bool CheckOverloadedOperatorKindParameterCount(bool is_method, clang::OverloadedOperatorKind op_kind, uint32_t num_params)
clang::DeclarationName GetDeclarationName(llvm::StringRef name, const CompilerType &function_clang_type)
DeclMetadataMap m_decl_metadata
Maps Decls to their associated ClangASTMetadata.
static clang::CXXMethodDecl * DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc)
CompilerType GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override
uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx, clang::DeclContext *child_decl_ctx, ConstString *child_name=nullptr, CompilerType *child_type=nullptr)
static clang::QualType GetQualType(lldb::opaque_compiler_type_t type)
clang::PrintingPolicy GetTypePrintingPolicy()
Returns the PrintingPolicy used when generating the internal type names.
uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override
static clang::RecordDecl * GetAsRecordDecl(const CompilerType &type)
CompilerType GetPointerSizedIntType(bool is_signed)
clang::FunctionDecl * CreateFunctionDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, llvm::StringRef name, const CompilerType &function_Type, clang::StorageClass storage, bool is_inline)
uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl, bool omit_empty_base_classes)
lldb::LanguageType DeclContextGetLanguage(void *opaque_decl_ctx) override
std::unique_ptr< DWARFASTParserClang > m_dwarf_ast_parser_up
CompilerType GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size)
bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override
int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override
static void BuildIndirectFields(const CompilerType &type)
std::unique_ptr< clang::FileManager > m_file_manager_up
uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl, const clang::CXXBaseSpecifier *base_spec, bool omit_empty_base_classes)
bool IsAnonymousType(lldb::opaque_compiler_type_t type) override
bool Verify(lldb::opaque_compiler_type_t type) override
Verify the integrity of the type to catch CompilerTypes that mix and match invalid TypeSystem/Opaque ...
size_t GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override
void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type)
CompilerType CreateBlockPointerType(const CompilerType &function_type)
lldb::LanguageType GetMinimumLanguage(lldb::opaque_compiler_type_t type) override
bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size)
clang::ClassTemplateSpecializationDecl * CreateClassTemplateSpecializationDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::ClassTemplateDecl *class_template_decl, int kind, const TemplateParameterInfos &infos)
std::unique_ptr< clang::HeaderSearch > m_header_search_up
void Finalize() override
Free up any resources associated with this TypeSystem.
clang::CXXMethodDecl * AddMethodToCXXRecordType(lldb::opaque_compiler_type_t type, llvm::StringRef name, const char *mangled_name, const CompilerType &method_type, lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline, bool is_explicit, bool is_attr_used, bool is_artificial)
static clang::ASTContext * DeclContextGetTypeSystemClang(const CompilerDeclContext &dc)
uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, CompilerType *base_type_ptr) override
clang::EnumConstantDecl * AddEnumerationValueToEnumerationType(const CompilerType &enum_type, const Declaration &decl, const char *name, int64_t enum_value, uint32_t enum_value_bit_size)
LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override
Convenience LLVM-style dump method for use in the debugger only.
CXXRecordDeclAccessMap m_cxx_record_decl_access
Maps CXXRecordDecl to their most recent added method/field's AccessSpecifier.
clang::NamespaceDecl * GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, bool is_inline=false)
std::unique_ptr< clang::ASTContext > m_ast_up
CompilerType CreateGenericFunctionPrototype() override
static clang::QualType GetCanonicalQualType(lldb::opaque_compiler_type_t type)
CompilerType DeclGetFunctionReturnType(void *opaque_decl) override
static bool IsEnumType(lldb::opaque_compiler_type_t type)
std::unique_ptr< npdb::PdbAstBuilder > m_native_pdb_ast_parser_up
static clang::CXXRecordDecl * GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type)
CompilerType GetDirectNestedTypeWithName(lldb::opaque_compiler_type_t type, llvm::StringRef name) override
static bool SetObjCSuperClass(const CompilerType &type, const CompilerType &superclass_compiler_type)
clang::UsingDirectiveDecl * CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, clang::NamespaceDecl *ns_decl)
static lldb::opaque_compiler_type_t GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type)
bool IsArrayType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size, bool *is_incomplete) override
void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name)
Dump clang AST types from the symbol file.
CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override
static void DumpDeclHiearchy(clang::Decl *decl)
static clang::ObjCMethodDecl * DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc)
static clang::FunctionDecl * DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc)
bool IsScalarType(lldb::opaque_compiler_type_t type) override
bool GetPtrAuthAddressDiversity(lldb::opaque_compiler_type_t type) override
std::shared_ptr< clang::TargetOptions > m_target_options_rp
lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override
static bool IsClassType(lldb::opaque_compiler_type_t type)
bool IsDefined(lldb::opaque_compiler_type_t type) override
static bool IsObjCClassType(const CompilerType &type)
TypeMetadataMap m_type_metadata
Maps Types to their associated ClangASTMetadata.
const llvm::fltSemantics & GetFloatTypeSemantics(size_t byte_size) override
CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override
bool RecordHasFields(const clang::RecordDecl *record_decl)
CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, const size_t index) override
static std::optional< ClangASTMetadata > DeclContextGetMetaData(const CompilerDeclContext &dc, const clang::Decl *object)
static bool CompleteTagDeclarationDefinition(const CompilerType &type)
unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override
static clang::ObjCMethodDecl * AddMethodToObjCObjectType(const CompilerType &type, const char *name, const CompilerType &method_compiler_type, bool is_artificial, bool is_variadic, bool is_objc_direct_call)
CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override
CompilerType CreateRecordType(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, llvm::StringRef name, int kind, lldb::LanguageType language, std::optional< ClangASTMetadata > metadata=std::nullopt, bool exports_symbols=false)
bool DeclContextIsContainedInLookup(void *opaque_decl_ctx, void *other_opaque_decl_ctx) override
CompilerType AddPtrAuthModifier(lldb::opaque_compiler_type_t type, uint32_t payload) override
static bool AddObjCClassProperty(const CompilerType &type, const char *property_name, const CompilerType &property_compiler_type, clang::ObjCIvarDecl *ivar_decl, const char *property_setter_name, const char *property_getter_name, uint32_t property_attributes, ClangASTMetadata metadata)
static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type, bool has_extern)
void SetMetadata(const clang::Decl *object, ClangASTMetadata meta_data)
CompilerType CreateEnumerationType(llvm::StringRef name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const Declaration &decl, const CompilerType &integer_qual_type, bool is_scoped)
clang::ParmVarDecl * CreateParameterDeclaration(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, const char *name, const CompilerType &param_type, int storage, bool add_decl=false)
void DumpTypeDescription(lldb::opaque_compiler_type_t type, lldb::DescriptionLevel level=lldb::eDescriptionLevelFull) override
Dump the type to stdout.
CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type, size_t idx) override
static clang::NamespaceDecl * DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc)
npdb::PdbAstBuilder * GetNativePDBParser() override
std::unique_ptr< clang::DiagnosticConsumer > m_diagnostic_consumer_up
CompilerType CreateObjCClass(llvm::StringRef name, clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, bool isInternal, std::optional< ClangASTMetadata > metadata=std::nullopt)
CompilerType GetTypeForDecl(clang::NamedDecl *decl)
CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) override
CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override
static clang::DeclContext * DeclContextGetAsDeclContext(const CompilerDeclContext &dc)
lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type, uint64_t &count) override
bool IsTypedefType(lldb::opaque_compiler_type_t type) override
CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override
std::optional< size_t > GetTypeBitAlign(lldb::opaque_compiler_type_t type, ExecutionContextScope *exe_scope) override
std::unique_ptr< clang::Builtin::Context > m_builtins_up
CompilerType GetBuiltinTypeByName(ConstString name) override
bool GetCompleteDecl(clang::Decl *decl)
static bool StartTagDeclarationDefinition(const CompilerType &type)
uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl, clang::NamedDecl *canonical_decl, bool omit_empty_base_classes)
bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type, CompilerType *target_type, bool check_cplusplus, bool check_objc) override
CompilerType GetOrCreateStructForIdentifier(llvm::StringRef type_name, const std::initializer_list< std::pair< const char *, CompilerType > > &type_fields, bool packed=false)
void LogCreation() const
Emits information about this TypeSystem into the expression log.
static llvm::StringRef GetPluginNameStatic()
clang::Sema * m_sema
The sema associated that is currently used to build this ASTContext.
size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override
static clang::VarDecl * AddVariableToRecordType(const CompilerType &type, llvm::StringRef name, const CompilerType &var_type, lldb::AccessType access)
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override
const clang::ClassTemplateSpecializationDecl * GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type)
std::unique_ptr< clang::MangleContext > m_mangle_ctx_up
TypeMemberFunctionImpl GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) override
bool IsTypeImpl(lldb::opaque_compiler_type_t type, llvm::function_ref< bool(clang::QualType)> predicate) const
size_t DeclGetFunctionNumArguments(void *opaque_decl) override
CompilerType CreateFunctionType(const CompilerType &result_type, const CompilerType *args, unsigned num_args, bool is_variadic, unsigned type_quals, clang::CallingConv cc=clang::CC_C, clang::RefQualifierKind ref_qual=clang::RQ_None)
CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override
std::unique_ptr< PDBASTParser > m_pdb_ast_parser_up
std::unique_ptr< clang::DiagnosticsEngine > m_diagnostics_engine_up
static std::optional< std::string > GetCXXClassName(const CompilerType &type)
static void DumpTypeName(const CompilerType &type)
plugin::dwarf::DWARFASTParser * GetDWARFParser() override
CompilerType DeclGetFunctionArgumentType(void *opaque_decl, size_t arg_idx) override
bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type, CompilerType *pointee_type) override
static clang::EnumDecl * GetAsEnumDecl(const CompilerType &type)
CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override
std::unique_ptr< clang::ModuleMap > m_module_map_up
void SetFunctionParameters(clang::FunctionDecl *function_decl, llvm::ArrayRef< clang::ParmVarDecl * > params)
static bool IsObjCObjectOrInterfaceType(const CompilerType &type)
static void RequireCompleteType(CompilerType type)
Complete a type from debug info, or mark it as forcefully completed if there is no definition of the ...
CompilerType CreateTypedef(lldb::opaque_compiler_type_t type, const char *name, const CompilerDeclContext &decl_ctx, uint32_t opaque_payload) override
Using the current type, create a new typedef to that type using "typedef_name" as the name and "decl_...
llvm::Expected< uint32_t > GetNumChildren(lldb::opaque_compiler_type_t type, bool omit_empty_base_classes, const ExecutionContext *exe_ctx) override
CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override
clang::TemplateTemplateParmDecl * CreateTemplateTemplateParmDecl(const char *template_name)
lldb::TemplateArgumentKind GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx, bool expand_pack) override
clang::ClassTemplateDecl * ParseClassTemplateDecl(clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module, lldb::AccessType access_type, const char *parent_name, int tag_decl_kind, const TypeSystemClang::TemplateParameterInfos &template_param_infos)
clang::ASTContext & getASTContext() const
Returns the clang::ASTContext instance managed by this TypeSystemClang.
std::vector< lldb_private::CompilerContext > DeclGetCompilerContext(void *opaque_decl) override
static CompilerType CreateMemberPointerType(const CompilerType &type, const CompilerType &pointee_type)
std::vector< lldb_private::CompilerContext > DeclContextGetCompilerContext(void *opaque_decl_ctx) override
void CreateASTContext()
Creates the internal ASTContext.
CompilerType GetCStringType(bool is_const)
bool IsAggregateType(lldb::opaque_compiler_type_t type) override
uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, llvm::StringRef name, bool omit_empty_base_classes) override
static bool IsObjCObjectPointerType(const CompilerType &type, CompilerType *target_type=nullptr)
bool IsVectorType(lldb::opaque_compiler_type_t type, CompilerType *element_type, uint64_t *size) override
static LanguageSet GetSupportedLanguagesForTypes()
clang::VarDecl * CreateVariableDeclaration(clang::DeclContext *decl_context, OptionalClangModuleID owning_module, const char *name, clang::QualType type)
clang::BlockDecl * CreateBlockDeclaration(clang::DeclContext *ctx, OptionalClangModuleID owning_module)
ConstString DeclContextGetName(void *opaque_decl_ctx) override
size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type, bool expand_pack) override
ConstString DeclGetName(void *opaque_decl) override
Interface for representing a type system.
Definition: TypeSystem.h:70
virtual SymbolFile * GetSymbolFile() const
Definition: TypeSystem.h:94
bool m_has_forcefully_completed_types
Used for reporting statistics.
Definition: TypeSystem.h:545
Encapsulates a one-time expression for use in lldb.
virtual uint64_t GetData(DataExtractor &data, Status &error)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
AddressType GetAddressTypeOfChildren()
ConstString GetName() const
Definition: ValueObject.h:487
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
#define INT32_MAX
Definition: lldb-defines.h:15
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_IVAR_OFFSET
Definition: lldb-defines.h:84
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
@ eAddressTypeLoad
Address is an address as in the current target inferior process.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::TypeSystem > TypeSystemSP
Definition: lldb-forward.h:465
void * opaque_compiler_type_t
Definition: lldb-types.h:89
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelVerbose
BasicType
Basic types enumeration for the public API SBType::GetBasicType().
@ eBasicTypeUnsignedShort
@ eBasicTypeSignedChar
@ eBasicTypeUnsignedInt128
@ eBasicTypeFloatComplex
@ eBasicTypeNullPtr
@ eBasicTypeObjCSel
@ eBasicTypeUnsignedWChar
@ eBasicTypeInvalid
@ eBasicTypeUnsignedLong
@ eBasicTypeDouble
@ eBasicTypeInt128
@ eBasicTypeLongDoubleComplex
@ eBasicTypeSignedWChar
@ eBasicTypeChar16
@ eBasicTypeUnsignedChar
@ eBasicTypeUnsignedLongLong
@ eBasicTypeDoubleComplex
@ eBasicTypeLongDouble
@ eBasicTypeChar32
@ eBasicTypeObjCID
@ eBasicTypeUnsignedInt
@ eBasicTypeLongLong
@ eBasicTypeObjCClass
Format
Display format definitions.
@ eFormatCString
NULL terminated C strings.
@ eFormatCharArray
Print characters with no single quotes, used for character arrays that can contain non printable char...
@ eFormatVectorOfChar
@ eFormatVectorOfUInt64
@ eFormatVoid
Do not print this.
@ eFormatVectorOfSInt64
@ eFormatComplex
Floating point complex type.
@ eFormatBytesWithASCII
@ eFormatOSType
OS character codes encoded into an integer 'PICT' 'text' etc...
@ eFormatUnicode16
@ eFormatVectorOfUInt128
@ eFormatVectorOfUInt8
@ eFormatVectorOfFloat32
@ eFormatVectorOfSInt32
@ eFormatUnicode32
@ eFormatVectorOfSInt8
@ eFormatVectorOfUInt16
@ eFormatHexUppercase
@ eFormatVectorOfFloat64
@ eFormatCharPrintable
Only printable characters, '.' if not printable.
@ eFormatComplexInteger
Integer complex type.
@ eFormatVectorOfSInt16
@ eFormatVectorOfUInt32
uint64_t offset_t
Definition: lldb-types.h:85
LanguageType
Programming language type.
@ eLanguageTypeC_plus_plus_20
ISO C++:2020.
@ eLanguageTypeC_plus_plus_14
ISO C++:2014.
@ eLanguageTypeC11
ISO C:2011.
@ eLanguageTypeC99
ISO C:1999.
@ eLanguageTypeC_plus_plus_03
ISO C++:2003.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeRust
Rust.
@ eLanguageTypeC_plus_plus_17
ISO C++:2017.
@ eLanguageTypeObjC_plus_plus
Objective-C++.
@ eLanguageTypeC_plus_plus_11
ISO C++:2011.
@ eLanguageTypeC89
ISO C:1989.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eLanguageTypeD
D.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
@ eLanguageTypeDylan
Dylan.
@ eAccessProtected
std::shared_ptr< lldb_private::Type > TypeSP
Definition: lldb-forward.h:457
@ eTemplateArgumentKindTemplate
@ eTemplateArgumentKindTemplateExpansion
@ eTemplateArgumentKindNull
@ eTemplateArgumentKindNullPtr
@ eTemplateArgumentKindDeclaration
@ eTemplateArgumentKindIntegral
@ eTemplateArgumentKindPack
@ eTemplateArgumentKindType
@ eTemplateArgumentKindStructuralValue
@ eTemplateArgumentKindExpression
Encoding
Register encoding definitions.
@ eEncodingIEEE754
float
@ eEncodingVector
vector registers
@ eEncodingUint
unsigned integer
@ eEncodingInvalid
@ eEncodingSint
signed integer
MemberFunctionKind
Kind of member function.
@ eMemberFunctionKindInstanceMethod
A function that applies to a specific instance.
@ eMemberFunctionKindConstructor
A function used to create instances.
@ eMemberFunctionKindUnknown
Not sure what the type of this is.
@ eMemberFunctionKindDestructor
A function used to tear down existing instances.
@ eMemberFunctionKindStaticMethod
A function that applies to a type rather than any instance.
std::shared_ptr< lldb_private::TypeSystemClang > TypeSystemClangSP
Definition: lldb-forward.h:466
uint64_t user_id_t
Definition: lldb-types.h:82
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444
static clang::QualType GetQualType(const CompilerType &ct)
Definition: ClangUtil.cpp:36
static clang::QualType GetCanonicalQualType(const CompilerType &ct)
Definition: ClangUtil.cpp:44
static bool IsClangType(const CompilerType &ct)
Definition: ClangUtil.cpp:17
static CompilerType RemoveFastQualifiers(const CompilerType &ct)
Definition: ClangUtil.cpp:51
static clang::TagDecl * GetAsTagDecl(const CompilerType &type)
Definition: ClangUtil.cpp:60
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: Type.h:38
void Insert(lldb::LanguageType language)
Definition: TypeSystem.cpp:34
A type-erased pair of llvm::dwarf::SourceLanguageName and version.