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