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