[Go to site: main page, start]

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