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 "llvm/Support/Casting.h"
13#include "llvm/Support/FormatAdapters.h"
14#include "llvm/Support/FormatVariadic.h"
15
16#include <mutex>
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/ASTImporter.h"
23#include "clang/AST/Attr.h"
24#include "clang/AST/CXXInheritance.h"
25#include "clang/AST/DeclObjC.h"
26#include "clang/AST/DeclTemplate.h"
27#include "clang/AST/Mangle.h"
28#include "clang/AST/RecordLayout.h"
29#include "clang/AST/Type.h"
30#include "clang/AST/VTableBuilder.h"
31#include "clang/Basic/Builtins.h"
32#include "clang/Basic/Diagnostic.h"
33#include "clang/Basic/FileManager.h"
34#include "clang/Basic/FileSystemOptions.h"
35#include "clang/Basic/LangStandard.h"
36#include "clang/Basic/SourceManager.h"
37#include "clang/Basic/TargetInfo.h"
38#include "clang/Basic/TargetOptions.h"
39#include "clang/Frontend/FrontendOptions.h"
40#include "clang/Lex/HeaderSearch.h"
41#include "clang/Lex/HeaderSearchOptions.h"
42#include "clang/Lex/ModuleMap.h"
43#include "clang/Sema/Sema.h"
44
45#include "llvm/Support/Signals.h"
46#include "llvm/Support/Threading.h"
47
57#include "lldb/Core/Module.h"
65#include "lldb/Target/Process.h"
66#include "lldb/Target/Target.h"
69#include "lldb/Utility/Flags.h"
73#include "lldb/Utility/Scalar.h"
75
80
81#include <cstdio>
82
83#include <mutex>
84#include <optional>
85
86using namespace lldb;
87using namespace lldb_private;
88using namespace lldb_private::dwarf;
89using namespace lldb_private::plugin::dwarf;
90using namespace clang;
91using llvm::StringSwitch;
92
94
95namespace {
96static void VerifyDecl(clang::Decl *decl) {
97 assert(decl && "VerifyDecl called with nullptr?");
98#ifndef NDEBUG
99 // We don't care about the actual access value here but only want to trigger
100 // that Clang calls its internal Decl::AccessDeclContextCheck validation.
101 decl->getAccess();
102#endif
103}
104
105static inline bool
106TypeSystemClangSupportsLanguage(lldb::LanguageType language) {
107 return language == eLanguageTypeUnknown || // Clang is the default type system
112 // Use Clang for Rust until there is a proper language plugin for it
113 language == eLanguageTypeRust ||
114 // Use Clang for D until there is a proper language plugin for it
115 language == eLanguageTypeD ||
116 // Open Dylan compiler debug info is designed to be Clang-compatible
117 language == eLanguageTypeDylan;
118}
119
120// Checks whether m1 is an overload of m2 (as opposed to an override). This is
121// called by addOverridesForMethod to distinguish overrides (which share a
122// vtable entry) from overloads (which require distinct entries).
123bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
124 // FIXME: This should detect covariant return types, but currently doesn't.
125 lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
126 "Methods should have the same AST context");
127 clang::ASTContext &context = m1->getASTContext();
128
129 const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
130 context.getCanonicalType(m1->getType()));
131
132 const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
133 context.getCanonicalType(m2->getType()));
134
135 auto compareArgTypes = [&context](const clang::QualType &m1p,
136 const clang::QualType &m2p) {
137 return context.hasSameType(m1p.getUnqualifiedType(),
138 m2p.getUnqualifiedType());
139 };
140
141 // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
142 // as a fourth parameter to std::equal().
143 return (m1->getNumParams() != m2->getNumParams()) ||
144 !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
145 m2Type->param_type_begin(), compareArgTypes);
146}
147
148// If decl is a virtual method, walk the base classes looking for methods that
149// decl overrides. This table of overridden methods is used by IRGen to
150// determine the vtable layout for decl's parent class.
151void addOverridesForMethod(clang::CXXMethodDecl *decl) {
152 if (!decl->isVirtual())
153 return;
154
155 clang::CXXBasePaths paths;
156 llvm::SmallVector<clang::NamedDecl *, 4> decls;
157
158 auto find_overridden_methods =
159 [&decls, decl](const clang::CXXBaseSpecifier *specifier,
160 clang::CXXBasePath &path) {
161 if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
162 specifier->getType()->castAs<clang::RecordType>()->getDecl())) {
163
164 clang::DeclarationName name = decl->getDeclName();
165
166 // If this is a destructor, check whether the base class destructor is
167 // virtual.
168 if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
169 if (auto *baseDtorDecl = base_record->getDestructor()) {
170 if (baseDtorDecl->isVirtual()) {
171 decls.push_back(baseDtorDecl);
172 return true;
173 } else
174 return false;
175 }
176
177 // Otherwise, search for name in the base class.
178 for (path.Decls = base_record->lookup(name).begin();
179 path.Decls != path.Decls.end(); ++path.Decls) {
180 if (auto *method_decl =
181 llvm::dyn_cast<clang::CXXMethodDecl>(*path.Decls))
182 if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
183 decls.push_back(method_decl);
184 return true;
185 }
186 }
187 }
188
189 return false;
190 };
191
192 if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
193 for (auto *overridden_decl : decls)
194 decl->addOverriddenMethod(
195 llvm::cast<clang::CXXMethodDecl>(overridden_decl));
196 }
197}
198}
199
201 VTableContextBase &vtable_ctx,
202 ValueObject &valobj,
203 const ASTRecordLayout &record_layout) {
204 // Retrieve type info
205 CompilerType pointee_type;
206 CompilerType this_type(valobj.GetCompilerType());
207 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
208 if (!type_info)
210
211 // Check if it's a pointer or reference
212 bool ptr_or_ref = false;
213 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
214 ptr_or_ref = true;
215 type_info = pointee_type.GetTypeInfo();
216 }
217
218 // We process only C++ classes
219 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
220 if ((type_info & cpp_class) != cpp_class)
222
223 // Calculate offset to VTable pointer
224 lldb::offset_t vbtable_ptr_offset =
225 vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity()
226 : 0;
227
228 if (ptr_or_ref) {
229 // We have a pointer / ref to object, so read
230 // VTable pointer from process memory
231
234
235 auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
236 if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
238
239 vbtable_ptr_addr += vbtable_ptr_offset;
240
241 Status err;
242 return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
243 }
244
245 // We have an object already read from process memory,
246 // so just extract VTable pointer from it
247
248 DataExtractor data;
249 Status err;
250 auto size = valobj.GetData(data, err);
251 if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
253
254 return data.GetAddress(&vbtable_ptr_offset);
255}
256
257static int64_t ReadVBaseOffsetFromVTable(Process &process,
258 VTableContextBase &vtable_ctx,
259 lldb::addr_t vtable_ptr,
260 const CXXRecordDecl *cxx_record_decl,
261 const CXXRecordDecl *base_class_decl) {
262 if (vtable_ctx.isMicrosoft()) {
263 clang::MicrosoftVTableContext &msoft_vtable_ctx =
264 static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
265
266 // Get the index into the virtual base table. The
267 // index is the index in uint32_t from vbtable_ptr
268 const unsigned vbtable_index =
269 msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
270 const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
271 Status err;
272 return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
273 err);
274 }
275
276 clang::ItaniumVTableContext &itanium_vtable_ctx =
277 static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
278
279 clang::CharUnits base_offset_offset =
280 itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
281 base_class_decl);
282 const lldb::addr_t base_offset_addr =
283 vtable_ptr + base_offset_offset.getQuantity();
284 const uint32_t base_offset_size = process.GetAddressByteSize();
285 Status err;
286 return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
287 INT64_MAX, err);
288}
289
290static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
291 ValueObject &valobj,
292 const ASTRecordLayout &record_layout,
293 const CXXRecordDecl *cxx_record_decl,
294 const CXXRecordDecl *base_class_decl,
295 int32_t &bit_offset) {
297 Process *process = exe_ctx.GetProcessPtr();
298 if (!process)
299 return false;
300
301 lldb::addr_t vtable_ptr =
302 GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
303 if (vtable_ptr == LLDB_INVALID_ADDRESS)
304 return false;
305
306 auto base_offset = ReadVBaseOffsetFromVTable(
307 *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
308 if (base_offset == INT64_MAX)
309 return false;
310
311 bit_offset = base_offset * 8;
312
313 return true;
314}
315
318
320 static ClangASTMap *g_map_ptr = nullptr;
321 static llvm::once_flag g_once_flag;
322 llvm::call_once(g_once_flag, []() {
323 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
324 });
325 return *g_map_ptr;
326}
327
329 bool is_complete_objc_class)
330 : m_payload(owning_module.GetValue()) {
331 SetIsCompleteObjCClass(is_complete_objc_class);
332}
333
335 assert(id.GetValue() < ObjCClassBit);
336 bool is_complete = IsCompleteObjCClass();
337 m_payload = id.GetValue();
338 SetIsCompleteObjCClass(is_complete);
339}
340
341static void SetMemberOwningModule(clang::Decl *member,
342 const clang::Decl *parent) {
343 if (!member || !parent)
344 return;
345
346 OptionalClangModuleID id(parent->getOwningModuleID());
347 if (!id.HasValue())
348 return;
349
350 member->setFromASTFile();
351 member->setOwningModuleID(id.GetValue());
352 member->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
353 if (llvm::isa<clang::NamedDecl>(member))
354 if (auto *dc = llvm::dyn_cast<clang::DeclContext>(parent)) {
355 dc->setHasExternalVisibleStorage(true);
356 // This triggers ExternalASTSource::FindExternalVisibleDeclsByName() to be
357 // called when searching for members.
358 dc->setHasExternalLexicalStorage(true);
359 }
360}
361
363
364bool TypeSystemClang::IsOperator(llvm::StringRef name,
365 clang::OverloadedOperatorKind &op_kind) {
366 // All operators have to start with "operator".
367 if (!name.consume_front("operator"))
368 return false;
369
370 // Remember if there was a space after "operator". This is necessary to
371 // check for collisions with strangely named functions like "operatorint()".
372 bool space_after_operator = name.consume_front(" ");
373
374 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
375 .Case("+", clang::OO_Plus)
376 .Case("+=", clang::OO_PlusEqual)
377 .Case("++", clang::OO_PlusPlus)
378 .Case("-", clang::OO_Minus)
379 .Case("-=", clang::OO_MinusEqual)
380 .Case("--", clang::OO_MinusMinus)
381 .Case("->", clang::OO_Arrow)
382 .Case("->*", clang::OO_ArrowStar)
383 .Case("*", clang::OO_Star)
384 .Case("*=", clang::OO_StarEqual)
385 .Case("/", clang::OO_Slash)
386 .Case("/=", clang::OO_SlashEqual)
387 .Case("%", clang::OO_Percent)
388 .Case("%=", clang::OO_PercentEqual)
389 .Case("^", clang::OO_Caret)
390 .Case("^=", clang::OO_CaretEqual)
391 .Case("&", clang::OO_Amp)
392 .Case("&=", clang::OO_AmpEqual)
393 .Case("&&", clang::OO_AmpAmp)
394 .Case("|", clang::OO_Pipe)
395 .Case("|=", clang::OO_PipeEqual)
396 .Case("||", clang::OO_PipePipe)
397 .Case("~", clang::OO_Tilde)
398 .Case("!", clang::OO_Exclaim)
399 .Case("!=", clang::OO_ExclaimEqual)
400 .Case("=", clang::OO_Equal)
401 .Case("==", clang::OO_EqualEqual)
402 .Case("<", clang::OO_Less)
403 .Case("<=>", clang::OO_Spaceship)
404 .Case("<<", clang::OO_LessLess)
405 .Case("<<=", clang::OO_LessLessEqual)
406 .Case("<=", clang::OO_LessEqual)
407 .Case(">", clang::OO_Greater)
408 .Case(">>", clang::OO_GreaterGreater)
409 .Case(">>=", clang::OO_GreaterGreaterEqual)
410 .Case(">=", clang::OO_GreaterEqual)
411 .Case("()", clang::OO_Call)
412 .Case("[]", clang::OO_Subscript)
413 .Case(",", clang::OO_Comma)
414 .Default(clang::NUM_OVERLOADED_OPERATORS);
415
416 // We found a fitting operator, so we can exit now.
417 if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
418 return true;
419
420 // After the "operator " or "operator" part is something unknown. This means
421 // it's either one of the named operators (new/delete), a conversion operator
422 // (e.g. operator bool) or a function which name starts with "operator"
423 // (e.g. void operatorbool).
424
425 // If it's a function that starts with operator it can't have a space after
426 // "operator" because identifiers can't contain spaces.
427 // E.g. "operator int" (conversion operator)
428 // vs. "operatorint" (function with colliding name).
429 if (!space_after_operator)
430 return false; // not an operator.
431
432 // Now the operator is either one of the named operators or a conversion
433 // operator.
434 op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
435 .Case("new", clang::OO_New)
436 .Case("new[]", clang::OO_Array_New)
437 .Case("delete", clang::OO_Delete)
438 .Case("delete[]", clang::OO_Array_Delete)
439 // conversion operators hit this case.
440 .Default(clang::NUM_OVERLOADED_OPERATORS);
441
442 return true;
443}
444
445clang::AccessSpecifier
447 switch (access) {
448 default:
449 break;
450 case eAccessNone:
451 return AS_none;
452 case eAccessPublic:
453 return AS_public;
454 case eAccessPrivate:
455 return AS_private;
456 case eAccessProtected:
457 return AS_protected;
458 }
459 return AS_none;
460}
461
462static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
463 // FIXME: Cleanup per-file based stuff.
464
465 // Set some properties which depend solely on the input kind; it would be
466 // nice to move these to the language standard, and have the driver resolve
467 // the input kind + language standard.
468 if (IK.getLanguage() == clang::Language::Asm) {
469 Opts.AsmPreprocessor = 1;
470 } else if (IK.isObjectiveC()) {
471 Opts.ObjC = 1;
472 }
473
474 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
475
476 if (LangStd == LangStandard::lang_unspecified) {
477 // Based on the base language, pick one.
478 switch (IK.getLanguage()) {
479 case clang::Language::Unknown:
480 case clang::Language::LLVM_IR:
481 case clang::Language::RenderScript:
482 llvm_unreachable("Invalid input kind!");
483 case clang::Language::OpenCL:
484 LangStd = LangStandard::lang_opencl10;
485 break;
486 case clang::Language::OpenCLCXX:
487 LangStd = LangStandard::lang_openclcpp10;
488 break;
489 case clang::Language::Asm:
490 case clang::Language::C:
491 case clang::Language::ObjC:
492 LangStd = LangStandard::lang_gnu99;
493 break;
494 case clang::Language::CXX:
495 case clang::Language::ObjCXX:
496 LangStd = LangStandard::lang_gnucxx98;
497 break;
498 case clang::Language::CUDA:
499 case clang::Language::HIP:
500 LangStd = LangStandard::lang_gnucxx17;
501 break;
502 case clang::Language::HLSL:
503 LangStd = LangStandard::lang_hlsl;
504 break;
505 }
506 }
507
508 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
509 Opts.LineComment = Std.hasLineComments();
510 Opts.C99 = Std.isC99();
511 Opts.CPlusPlus = Std.isCPlusPlus();
512 Opts.CPlusPlus11 = Std.isCPlusPlus11();
513 Opts.CPlusPlus14 = Std.isCPlusPlus14();
514 Opts.CPlusPlus17 = Std.isCPlusPlus17();
515 Opts.CPlusPlus20 = Std.isCPlusPlus20();
516 Opts.Digraphs = Std.hasDigraphs();
517 Opts.GNUMode = Std.isGNUMode();
518 Opts.GNUInline = !Std.isC99();
519 Opts.HexFloats = Std.hasHexFloats();
520
521 Opts.WChar = true;
522
523 // OpenCL has some additional defaults.
524 if (LangStd == LangStandard::lang_opencl10) {
525 Opts.OpenCL = 1;
526 Opts.AltiVec = 1;
527 Opts.CXXOperatorNames = 1;
528 Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::All);
529 }
530
531 // OpenCL and C++ both have bool, true, false keywords.
532 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
533
534 Opts.setValueVisibilityMode(DefaultVisibility);
535
536 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
537 // specified, or -std is set to a conforming mode.
538 Opts.Trigraphs = !Opts.GNUMode;
539 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
540 Opts.OptimizeSize = 0;
541
542 // FIXME: Eliminate this dependency.
543 // unsigned Opt =
544 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
545 // Opts.Optimize = Opt != 0;
546 unsigned Opt = 0;
547
548 // This is the __NO_INLINE__ define, which just depends on things like the
549 // optimization level and -fno-inline, not actually whether the backend has
550 // inlining enabled.
551 //
552 // FIXME: This is affected by other options (-fno-inline).
553 Opts.NoInlineDefine = !Opt;
554
555 // This is needed to allocate the extra space for the owning module
556 // on each decl.
557 Opts.ModulesLocalVisibility = 1;
558}
559
561 llvm::Triple target_triple) {
562 m_display_name = name.str();
563 if (!target_triple.str().empty())
564 SetTargetTriple(target_triple.str());
565 // The caller didn't pass an ASTContext so create a new one for this
566 // TypeSystemClang.
568}
569
570TypeSystemClang::TypeSystemClang(llvm::StringRef name,
571 ASTContext &existing_ctxt) {
572 m_display_name = name.str();
573 SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
574
575 m_ast_up.reset(&existing_ctxt);
576 GetASTMap().Insert(&existing_ctxt, this);
577}
578
579// Destructor
581
583 lldb_private::Module *module,
584 Target *target) {
585 if (!TypeSystemClangSupportsLanguage(language))
586 return lldb::TypeSystemSP();
587 ArchSpec arch;
588 if (module)
589 arch = module->GetArchitecture();
590 else if (target)
591 arch = target->GetArchitecture();
592
593 if (!arch.IsValid())
594 return lldb::TypeSystemSP();
595
596 llvm::Triple triple = arch.GetTriple();
597 // LLVM wants this to be set to iOS or MacOSX; if we're working on
598 // a bare-boards type image, change the triple for llvm's benefit.
599 if (triple.getVendor() == llvm::Triple::Apple &&
600 triple.getOS() == llvm::Triple::UnknownOS) {
601 if (triple.getArch() == llvm::Triple::arm ||
602 triple.getArch() == llvm::Triple::aarch64 ||
603 triple.getArch() == llvm::Triple::aarch64_32 ||
604 triple.getArch() == llvm::Triple::thumb) {
605 triple.setOS(llvm::Triple::IOS);
606 } else {
607 triple.setOS(llvm::Triple::MacOSX);
608 }
609 }
610
611 if (module) {
612 std::string ast_name =
613 "ASTContext for '" + module->GetFileSpec().GetPath() + "'";
614 return std::make_shared<TypeSystemClang>(ast_name, triple);
615 } else if (target && target->IsValid())
616 return std::make_shared<ScratchTypeSystemClang>(*target, triple);
617 return lldb::TypeSystemSP();
618}
619
621 LanguageSet languages;
623 languages.Insert(lldb::eLanguageTypeC);
635 return languages;
636}
637
639 LanguageSet languages;
647 return languages;
648}
649
652 GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
654}
655
658}
659
661 assert(m_ast_up);
662 GetASTMap().Erase(m_ast_up.get());
663 if (!m_ast_owned)
664 m_ast_up.release();
665
666 m_builtins_up.reset();
667 m_selector_table_up.reset();
668 m_identifier_table_up.reset();
669 m_target_info_up.reset();
670 m_target_options_rp.reset();
672 m_source_manager_up.reset();
673 m_language_options_up.reset();
674}
675
677 // Ensure that the new sema actually belongs to our ASTContext.
678 assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
679 m_sema = s;
680}
681
683 return m_target_triple.c_str();
684}
685
686void TypeSystemClang::SetTargetTriple(llvm::StringRef target_triple) {
687 m_target_triple = target_triple.str();
688}
689
691 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
692 ASTContext &ast = getASTContext();
693 ast.getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
694 ast.setExternalSource(ast_source_up);
695}
696
698 assert(m_ast_up);
699 return *m_ast_up;
700}
701
702class NullDiagnosticConsumer : public DiagnosticConsumer {
703public:
704 NullDiagnosticConsumer() { m_log = GetLog(LLDBLog::Expressions); }
705
706 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
707 const clang::Diagnostic &info) override {
708 if (m_log) {
709 llvm::SmallVector<char, 32> diag_str(10);
710 info.FormatDiagnostic(diag_str);
711 diag_str.push_back('\0');
712 LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
713 }
714 }
715
716 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
717 return new NullDiagnosticConsumer();
718 }
719
720private:
722};
723
725 assert(!m_ast_up);
726 m_ast_owned = true;
727
728 m_language_options_up = std::make_unique<LangOptions>();
729 ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
731
733 std::make_unique<IdentifierTable>(*m_language_options_up, nullptr);
734 m_builtins_up = std::make_unique<Builtin::Context>();
735
736 m_selector_table_up = std::make_unique<SelectorTable>();
737
738 clang::FileSystemOptions file_system_options;
739 m_file_manager_up = std::make_unique<clang::FileManager>(
740 file_system_options, FileSystem::Instance().GetVirtualFileSystem());
741
742 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
744 std::make_unique<DiagnosticsEngine>(diag_id_sp, new DiagnosticOptions());
745
746 m_source_manager_up = std::make_unique<clang::SourceManager>(
748 m_ast_up = std::make_unique<ASTContext>(
750 *m_selector_table_up, *m_builtins_up, TU_Complete);
751
752 m_diagnostic_consumer_up = std::make_unique<NullDiagnosticConsumer>();
753 m_ast_up->getDiagnostics().setClient(m_diagnostic_consumer_up.get(), false);
754
755 // This can be NULL if we don't know anything about the architecture or if
756 // the target for an architecture isn't enabled in the llvm/clang that we
757 // built
758 TargetInfo *target_info = getTargetInfo();
759 if (target_info)
760 m_ast_up->InitBuiltinTypes(*target_info);
761
762 GetASTMap().Insert(m_ast_up.get(), this);
763
764 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
766 SetExternalSource(ast_source_up);
767}
768
770 TypeSystemClang *clang_ast = GetASTMap().Lookup(ast);
771 return clang_ast;
772}
773
774clang::MangleContext *TypeSystemClang::getMangleContext() {
775 if (m_mangle_ctx_up == nullptr)
776 m_mangle_ctx_up.reset(getASTContext().createMangleContext());
777 return m_mangle_ctx_up.get();
778}
779
780std::shared_ptr<clang::TargetOptions> &TypeSystemClang::getTargetOptions() {
781 if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
782 m_target_options_rp = std::make_shared<clang::TargetOptions>();
783 if (m_target_options_rp != nullptr)
785 }
786 return m_target_options_rp;
787}
788
790 // target_triple should be something like "x86_64-apple-macosx"
791 if (m_target_info_up == nullptr && !m_target_triple.empty())
792 m_target_info_up.reset(TargetInfo::CreateTargetInfo(
793 getASTContext().getDiagnostics(), getTargetOptions()));
794 return m_target_info_up.get();
795}
796
797#pragma mark Basic Types
798
799static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
800 ASTContext &ast, QualType qual_type) {
801 uint64_t qual_type_bit_size = ast.getTypeSize(qual_type);
802 return qual_type_bit_size == bit_size;
803}
804
807 size_t bit_size) {
808 ASTContext &ast = getASTContext();
809 switch (encoding) {
810 case eEncodingInvalid:
811 if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
812 return GetType(ast.VoidPtrTy);
813 break;
814
815 case eEncodingUint:
816 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
817 return GetType(ast.UnsignedCharTy);
818 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
819 return GetType(ast.UnsignedShortTy);
820 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
821 return GetType(ast.UnsignedIntTy);
822 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
823 return GetType(ast.UnsignedLongTy);
824 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
825 return GetType(ast.UnsignedLongLongTy);
826 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
827 return GetType(ast.UnsignedInt128Ty);
828 break;
829
830 case eEncodingSint:
831 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
832 return GetType(ast.SignedCharTy);
833 if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
834 return GetType(ast.ShortTy);
835 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
836 return GetType(ast.IntTy);
837 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
838 return GetType(ast.LongTy);
839 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
840 return GetType(ast.LongLongTy);
841 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
842 return GetType(ast.Int128Ty);
843 break;
844
845 case eEncodingIEEE754:
846 if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
847 return GetType(ast.FloatTy);
848 if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
849 return GetType(ast.DoubleTy);
850 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
851 return GetType(ast.LongDoubleTy);
852 if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
853 return GetType(ast.HalfTy);
854 break;
855
856 case eEncodingVector:
857 // Sanity check that bit_size is a multiple of 8's.
858 if (bit_size && !(bit_size & 0x7u))
859 return GetType(ast.getExtVectorType(ast.UnsignedCharTy, bit_size / 8));
860 break;
861 }
862
863 return CompilerType();
864}
865
867 static const llvm::StringMap<lldb::BasicType> g_type_map = {
868 // "void"
869 {"void", eBasicTypeVoid},
870
871 // "char"
872 {"char", eBasicTypeChar},
873 {"signed char", eBasicTypeSignedChar},
874 {"unsigned char", eBasicTypeUnsignedChar},
875 {"wchar_t", eBasicTypeWChar},
876 {"signed wchar_t", eBasicTypeSignedWChar},
877 {"unsigned wchar_t", eBasicTypeUnsignedWChar},
878
879 // "short"
880 {"short", eBasicTypeShort},
881 {"short int", eBasicTypeShort},
882 {"unsigned short", eBasicTypeUnsignedShort},
883 {"unsigned short int", eBasicTypeUnsignedShort},
884
885 // "int"
886 {"int", eBasicTypeInt},
887 {"signed int", eBasicTypeInt},
888 {"unsigned int", eBasicTypeUnsignedInt},
889 {"unsigned", eBasicTypeUnsignedInt},
890
891 // "long"
892 {"long", eBasicTypeLong},
893 {"long int", eBasicTypeLong},
894 {"unsigned long", eBasicTypeUnsignedLong},
895 {"unsigned long int", eBasicTypeUnsignedLong},
896
897 // "long long"
898 {"long long", eBasicTypeLongLong},
899 {"long long int", eBasicTypeLongLong},
900 {"unsigned long long", eBasicTypeUnsignedLongLong},
901 {"unsigned long long int", eBasicTypeUnsignedLongLong},
902
903 // "int128"
904 {"__int128_t", eBasicTypeInt128},
905 {"__uint128_t", eBasicTypeUnsignedInt128},
906
907 // Miscellaneous
908 {"bool", eBasicTypeBool},
909 {"float", eBasicTypeFloat},
910 {"double", eBasicTypeDouble},
911 {"long double", eBasicTypeLongDouble},
912 {"id", eBasicTypeObjCID},
913 {"SEL", eBasicTypeObjCSel},
914 {"nullptr", eBasicTypeNullPtr},
915 };
916
917 auto iter = g_type_map.find(name);
918 if (iter == g_type_map.end())
919 return eBasicTypeInvalid;
920
921 return iter->second;
922}
923
925 if (m_pointer_byte_size == 0)
926 if (auto size = GetBasicType(lldb::eBasicTypeVoid)
928 .GetByteSize(nullptr))
929 m_pointer_byte_size = *size;
930 return m_pointer_byte_size;
931}
932
934 clang::ASTContext &ast = getASTContext();
935
937 GetOpaqueCompilerType(&ast, basic_type);
938
939 if (clang_type)
940 return CompilerType(weak_from_this(), clang_type);
941 return CompilerType();
942}
943
945 llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
946 ASTContext &ast = getASTContext();
947
948 switch (dw_ate) {
949 default:
950 break;
951
952 case DW_ATE_address:
953 if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
954 return GetType(ast.VoidPtrTy);
955 break;
956
957 case DW_ATE_boolean:
958 if (QualTypeMatchesBitSize(bit_size, ast, ast.BoolTy))
959 return GetType(ast.BoolTy);
960 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
961 return GetType(ast.UnsignedCharTy);
962 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
963 return GetType(ast.UnsignedShortTy);
964 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
965 return GetType(ast.UnsignedIntTy);
966 break;
967
968 case DW_ATE_lo_user:
969 // This has been seen to mean DW_AT_complex_integer
970 if (type_name.contains("complex")) {
971 CompilerType complex_int_clang_type =
972 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
973 bit_size / 2);
974 return GetType(
975 ast.getComplexType(ClangUtil::GetQualType(complex_int_clang_type)));
976 }
977 break;
978
979 case DW_ATE_complex_float: {
980 CanQualType FloatComplexTy = ast.getComplexType(ast.FloatTy);
981 if (QualTypeMatchesBitSize(bit_size, ast, FloatComplexTy))
982 return GetType(FloatComplexTy);
983
984 CanQualType DoubleComplexTy = ast.getComplexType(ast.DoubleTy);
985 if (QualTypeMatchesBitSize(bit_size, ast, DoubleComplexTy))
986 return GetType(DoubleComplexTy);
987
988 CanQualType LongDoubleComplexTy = ast.getComplexType(ast.LongDoubleTy);
989 if (QualTypeMatchesBitSize(bit_size, ast, LongDoubleComplexTy))
990 return GetType(LongDoubleComplexTy);
991
992 CompilerType complex_float_clang_type =
993 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
994 bit_size / 2);
995 return GetType(
996 ast.getComplexType(ClangUtil::GetQualType(complex_float_clang_type)));
997 }
998
999 case DW_ATE_float:
1000 if (type_name == "float" &&
1001 QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
1002 return GetType(ast.FloatTy);
1003 if (type_name == "double" &&
1004 QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
1005 return GetType(ast.DoubleTy);
1006 if (type_name == "long double" &&
1007 QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
1008 return GetType(ast.LongDoubleTy);
1009 // Fall back to not requiring a name match
1010 if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
1011 return GetType(ast.FloatTy);
1012 if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
1013 return GetType(ast.DoubleTy);
1014 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
1015 return GetType(ast.LongDoubleTy);
1016 if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
1017 return GetType(ast.HalfTy);
1018 break;
1019
1020 case DW_ATE_signed:
1021 if (!type_name.empty()) {
1022 if (type_name == "wchar_t" &&
1023 QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy) &&
1024 (getTargetInfo() &&
1025 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1026 return GetType(ast.WCharTy);
1027 if (type_name == "void" &&
1028 QualTypeMatchesBitSize(bit_size, ast, ast.VoidTy))
1029 return GetType(ast.VoidTy);
1030 if (type_name.contains("long long") &&
1031 QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
1032 return GetType(ast.LongLongTy);
1033 if (type_name.contains("long") &&
1034 QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
1035 return GetType(ast.LongTy);
1036 if (type_name.contains("short") &&
1037 QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
1038 return GetType(ast.ShortTy);
1039 if (type_name.contains("char")) {
1040 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1041 return GetType(ast.CharTy);
1042 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
1043 return GetType(ast.SignedCharTy);
1044 }
1045 if (type_name.contains("int")) {
1046 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
1047 return GetType(ast.IntTy);
1048 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
1049 return GetType(ast.Int128Ty);
1050 }
1051 }
1052 // We weren't able to match up a type name, just search by size
1053 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1054 return GetType(ast.CharTy);
1055 if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
1056 return GetType(ast.ShortTy);
1057 if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
1058 return GetType(ast.IntTy);
1059 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
1060 return GetType(ast.LongTy);
1061 if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
1062 return GetType(ast.LongLongTy);
1063 if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
1064 return GetType(ast.Int128Ty);
1065 break;
1066
1067 case DW_ATE_signed_char:
1068 if (type_name == "char") {
1069 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1070 return GetType(ast.CharTy);
1071 }
1072 if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
1073 return GetType(ast.SignedCharTy);
1074 break;
1075
1076 case DW_ATE_unsigned:
1077 if (!type_name.empty()) {
1078 if (type_name == "wchar_t") {
1079 if (QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy)) {
1080 if (!(getTargetInfo() &&
1081 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1082 return GetType(ast.WCharTy);
1083 }
1084 }
1085 if (type_name.contains("long long")) {
1086 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1087 return GetType(ast.UnsignedLongLongTy);
1088 } else if (type_name.contains("long")) {
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1090 return GetType(ast.UnsignedLongTy);
1091 } else if (type_name.contains("short")) {
1092 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1093 return GetType(ast.UnsignedShortTy);
1094 } else if (type_name.contains("char")) {
1095 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1096 return GetType(ast.UnsignedCharTy);
1097 } else if (type_name.contains("int")) {
1098 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1099 return GetType(ast.UnsignedIntTy);
1100 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1101 return GetType(ast.UnsignedInt128Ty);
1102 }
1103 }
1104 // We weren't able to match up a type name, just search by size
1105 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1106 return GetType(ast.UnsignedCharTy);
1107 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1108 return GetType(ast.UnsignedShortTy);
1109 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1110 return GetType(ast.UnsignedIntTy);
1111 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1112 return GetType(ast.UnsignedLongTy);
1113 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1114 return GetType(ast.UnsignedLongLongTy);
1115 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1116 return GetType(ast.UnsignedInt128Ty);
1117 break;
1118
1119 case DW_ATE_unsigned_char:
1120 if (type_name == "char") {
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1122 return GetType(ast.CharTy);
1123 }
1124 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1125 return GetType(ast.UnsignedCharTy);
1126 if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1127 return GetType(ast.UnsignedShortTy);
1128 break;
1129
1130 case DW_ATE_imaginary_float:
1131 break;
1132
1133 case DW_ATE_UTF:
1134 switch (bit_size) {
1135 case 8:
1136 return GetType(ast.Char8Ty);
1137 case 16:
1138 return GetType(ast.Char16Ty);
1139 case 32:
1140 return GetType(ast.Char32Ty);
1141 default:
1142 if (!type_name.empty()) {
1143 if (type_name == "char16_t")
1144 return GetType(ast.Char16Ty);
1145 if (type_name == "char32_t")
1146 return GetType(ast.Char32Ty);
1147 if (type_name == "char8_t")
1148 return GetType(ast.Char8Ty);
1149 }
1150 }
1151 break;
1152 }
1153
1154 Log *log = GetLog(LLDBLog::Types);
1155 LLDB_LOG(log,
1156 "error: need to add support for DW_TAG_base_type '{0}' "
1157 "encoded with DW_ATE = {1:x}, bit_size = {2}",
1158 type_name, dw_ate, bit_size);
1159 return CompilerType();
1160}
1161
1163 ASTContext &ast = getASTContext();
1164 QualType char_type(ast.CharTy);
1165
1166 if (is_const)
1167 char_type.addConst();
1168
1169 return GetType(ast.getPointerType(char_type));
1170}
1171
1173 bool ignore_qualifiers) {
1174 auto ast = type1.GetTypeSystem().dyn_cast_or_null<TypeSystemClang>();
1175 if (!ast || type1.GetTypeSystem() != type2.GetTypeSystem())
1176 return false;
1177
1178 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1179 return true;
1180
1181 QualType type1_qual = ClangUtil::GetQualType(type1);
1182 QualType type2_qual = ClangUtil::GetQualType(type2);
1183
1184 if (ignore_qualifiers) {
1185 type1_qual = type1_qual.getUnqualifiedType();
1186 type2_qual = type2_qual.getUnqualifiedType();
1187 }
1188
1189 return ast->getASTContext().hasSameType(type1_qual, type2_qual);
1190}
1191
1193 if (!opaque_decl)
1194 return CompilerType();
1195
1196 clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1197 if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1198 return GetTypeForDecl(named_decl);
1199 return CompilerType();
1200}
1201
1203 // Check that the DeclContext actually belongs to this ASTContext.
1204 assert(&ctx->getParentASTContext() == &getASTContext());
1205 return CompilerDeclContext(this, ctx);
1206}
1207
1209 if (clang::ObjCInterfaceDecl *interface_decl =
1210 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1211 return GetTypeForDecl(interface_decl);
1212 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1213 return GetTypeForDecl(tag_decl);
1214 return CompilerType();
1215}
1216
1218 return GetType(getASTContext().getTagDeclType(decl));
1219}
1220
1221CompilerType TypeSystemClang::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1222 return GetType(getASTContext().getObjCInterfaceType(decl));
1223}
1224
1225#pragma mark Structure, Unions, Classes
1226
1228 OptionalClangModuleID owning_module) {
1229 if (!decl || !owning_module.HasValue())
1230 return;
1231
1232 decl->setFromASTFile();
1233 decl->setOwningModuleID(owning_module.GetValue());
1234 decl->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
1235}
1236
1239 OptionalClangModuleID parent,
1240 bool is_framework, bool is_explicit) {
1241 // Get the external AST source which holds the modules.
1242 auto *ast_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1243 getASTContext().getExternalSource());
1244 assert(ast_source && "external ast source was lost");
1245 if (!ast_source)
1246 return {};
1247
1248 // Lazily initialize the module map.
1249 if (!m_header_search_up) {
1250 auto HSOpts = std::make_shared<clang::HeaderSearchOptions>();
1251 m_header_search_up = std::make_unique<clang::HeaderSearch>(
1254 m_module_map_up = std::make_unique<clang::ModuleMap>(
1257 }
1258
1259 // Get or create the module context.
1260 bool created;
1261 clang::Module *module;
1262 auto parent_desc = ast_source->getSourceDescriptor(parent.GetValue());
1263 std::tie(module, created) = m_module_map_up->findOrCreateModule(
1264 name, parent_desc ? parent_desc->getModuleOrNull() : nullptr,
1265 is_framework, is_explicit);
1266 if (!created)
1267 return ast_source->GetIDForModule(module);
1268
1269 return ast_source->RegisterModule(module);
1270}
1271
1273 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1274 AccessType access_type, llvm::StringRef name, int kind,
1275 LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) {
1276 ASTContext &ast = getASTContext();
1277
1278 if (decl_ctx == nullptr)
1279 decl_ctx = ast.getTranslationUnitDecl();
1280
1281 if (language == eLanguageTypeObjC ||
1282 language == eLanguageTypeObjC_plus_plus) {
1283 bool isForwardDecl = true;
1284 bool isInternal = false;
1285 return CreateObjCClass(name, decl_ctx, owning_module, isForwardDecl,
1286 isInternal, metadata);
1287 }
1288
1289 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1290 // we will need to update this code. I was told to currently always use the
1291 // CXXRecordDecl class since we often don't know from debug information if
1292 // something is struct or a class, so we default to always use the more
1293 // complete definition just in case.
1294
1295 bool has_name = !name.empty();
1296 CXXRecordDecl *decl = CXXRecordDecl::CreateDeserialized(ast, 0);
1297 decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1298 decl->setDeclContext(decl_ctx);
1299 if (has_name)
1300 decl->setDeclName(&ast.Idents.get(name));
1301 SetOwningModule(decl, owning_module);
1302
1303 if (!has_name) {
1304 // In C++ a lambda is also represented as an unnamed class. This is
1305 // different from an *anonymous class* that the user wrote:
1306 //
1307 // struct A {
1308 // // anonymous class (GNU/MSVC extension)
1309 // struct {
1310 // int x;
1311 // };
1312 // // unnamed class within a class
1313 // struct {
1314 // int y;
1315 // } B;
1316 // };
1317 //
1318 // void f() {
1319 // // unammed class outside of a class
1320 // struct {
1321 // int z;
1322 // } C;
1323 // }
1324 //
1325 // Anonymous classes is a GNU/MSVC extension that clang supports. It
1326 // requires the anonymous class be embedded within a class. So the new
1327 // heuristic verifies this condition.
1328 if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols)
1329 decl->setAnonymousStructOrUnion(true);
1330 }
1331
1332 if (metadata)
1333 SetMetadata(decl, *metadata);
1334
1335 if (access_type != eAccessNone)
1336 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1337
1338 if (decl_ctx)
1339 decl_ctx->addDecl(decl);
1340
1341 return GetType(ast.getTagDeclType(decl));
1342}
1343
1344namespace {
1345/// Returns true iff the given TemplateArgument should be represented as an
1346/// NonTypeTemplateParmDecl in the AST.
1347bool IsValueParam(const clang::TemplateArgument &argument) {
1348 return argument.getKind() == TemplateArgument::Integral;
1349}
1350
1351void AddAccessSpecifierDecl(clang::CXXRecordDecl *cxx_record_decl,
1352 ASTContext &ct,
1353 clang::AccessSpecifier previous_access,
1354 clang::AccessSpecifier access_specifier) {
1355 if (!cxx_record_decl->isClass() && !cxx_record_decl->isStruct())
1356 return;
1357 if (previous_access != access_specifier) {
1358 // For struct, don't add AS_public if it's the first AccessSpecDecl.
1359 // For class, don't add AS_private if it's the first AccessSpecDecl.
1360 if ((cxx_record_decl->isStruct() &&
1361 previous_access == clang::AccessSpecifier::AS_none &&
1362 access_specifier == clang::AccessSpecifier::AS_public) ||
1363 (cxx_record_decl->isClass() &&
1364 previous_access == clang::AccessSpecifier::AS_none &&
1365 access_specifier == clang::AccessSpecifier::AS_private)) {
1366 return;
1367 }
1368 cxx_record_decl->addDecl(
1369 AccessSpecDecl::Create(ct, access_specifier, cxx_record_decl,
1370 SourceLocation(), SourceLocation()));
1371 }
1372}
1373} // namespace
1374
1375static TemplateParameterList *CreateTemplateParameterList(
1376 ASTContext &ast,
1377 const TypeSystemClang::TemplateParameterInfos &template_param_infos,
1378 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1379 const bool parameter_pack = false;
1380 const bool is_typename = false;
1381 const unsigned depth = 0;
1382 const size_t num_template_params = template_param_infos.Size();
1383 DeclContext *const decl_context =
1384 ast.getTranslationUnitDecl(); // Is this the right decl context?,
1385
1386 auto const &args = template_param_infos.GetArgs();
1387 auto const &names = template_param_infos.GetNames();
1388 for (size_t i = 0; i < num_template_params; ++i) {
1389 const char *name = names[i];
1390
1391 IdentifierInfo *identifier_info = nullptr;
1392 if (name && name[0])
1393 identifier_info = &ast.Idents.get(name);
1394 TemplateArgument const &targ = args[i];
1395 if (IsValueParam(targ)) {
1396 QualType template_param_type = targ.getIntegralType();
1397 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1398 ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1399 identifier_info, template_param_type, parameter_pack,
1400 ast.getTrivialTypeSourceInfo(template_param_type)));
1401 } else {
1402 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1403 ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1404 identifier_info, is_typename, parameter_pack));
1405 }
1406 }
1407
1408 if (template_param_infos.hasParameterPack()) {
1409 IdentifierInfo *identifier_info = nullptr;
1410 if (template_param_infos.HasPackName())
1411 identifier_info = &ast.Idents.get(template_param_infos.GetPackName());
1412 const bool parameter_pack_true = true;
1413
1414 if (!template_param_infos.GetParameterPack().IsEmpty() &&
1415 IsValueParam(template_param_infos.GetParameterPack().Front())) {
1416 QualType template_param_type =
1417 template_param_infos.GetParameterPack().Front().getIntegralType();
1418 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1419 ast, decl_context, SourceLocation(), SourceLocation(), depth,
1420 num_template_params, identifier_info, template_param_type,
1421 parameter_pack_true,
1422 ast.getTrivialTypeSourceInfo(template_param_type)));
1423 } else {
1424 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1425 ast, decl_context, SourceLocation(), SourceLocation(), depth,
1426 num_template_params, identifier_info, is_typename,
1427 parameter_pack_true));
1428 }
1429 }
1430 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1431 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1432 ast, SourceLocation(), SourceLocation(), template_param_decls,
1433 SourceLocation(), requires_clause);
1434 return template_param_list;
1435}
1436
1438 const TemplateParameterInfos &template_param_infos) {
1439 llvm::SmallVector<NamedDecl *, 8> ignore;
1440 clang::TemplateParameterList *template_param_list =
1441 CreateTemplateParameterList(getASTContext(), template_param_infos,
1442 ignore);
1443 llvm::SmallVector<clang::TemplateArgument, 2> args(
1444 template_param_infos.GetArgs());
1445 if (template_param_infos.hasParameterPack()) {
1446 llvm::ArrayRef<TemplateArgument> pack_args =
1447 template_param_infos.GetParameterPackArgs();
1448 args.append(pack_args.begin(), pack_args.end());
1449 }
1450 std::string str;
1451 llvm::raw_string_ostream os(str);
1452 clang::printTemplateArgumentList(os, args, GetTypePrintingPolicy(),
1453 template_param_list);
1454 return str;
1455}
1456
1458 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1459 clang::FunctionDecl *func_decl,
1460 const TemplateParameterInfos &template_param_infos) {
1461 // /// Create a function template node.
1462 ASTContext &ast = getASTContext();
1463
1464 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1465 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1466 ast, template_param_infos, template_param_decls);
1467 FunctionTemplateDecl *func_tmpl_decl =
1468 FunctionTemplateDecl::CreateDeserialized(ast, 0);
1469 func_tmpl_decl->setDeclContext(decl_ctx);
1470 func_tmpl_decl->setLocation(func_decl->getLocation());
1471 func_tmpl_decl->setDeclName(func_decl->getDeclName());
1472 func_tmpl_decl->setTemplateParameters(template_param_list);
1473 func_tmpl_decl->init(func_decl);
1474 SetOwningModule(func_tmpl_decl, owning_module);
1475
1476 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1477 i < template_param_decl_count; ++i) {
1478 // TODO: verify which decl context we should put template_param_decls into..
1479 template_param_decls[i]->setDeclContext(func_decl);
1480 }
1481 // Function templates inside a record need to have an access specifier.
1482 // It doesn't matter what access specifier we give the template as LLDB
1483 // anyway allows accessing everything inside a record.
1484 if (decl_ctx->isRecord())
1485 func_tmpl_decl->setAccess(clang::AccessSpecifier::AS_public);
1486
1487 return func_tmpl_decl;
1488}
1489
1491 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1492 const TemplateParameterInfos &infos) {
1493 TemplateArgumentList *template_args_ptr = TemplateArgumentList::CreateCopy(
1494 func_decl->getASTContext(), infos.GetArgs());
1495
1496 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1497 template_args_ptr, nullptr);
1498}
1499
1500/// Returns true if the given template parameter can represent the given value.
1501/// For example, `typename T` can represent `int` but not integral values such
1502/// as `int I = 3`.
1503static bool TemplateParameterAllowsValue(NamedDecl *param,
1504 const TemplateArgument &value) {
1505 if (llvm::isa<TemplateTypeParmDecl>(param)) {
1506 // Compare the argument kind, i.e. ensure that <typename> != <int>.
1507 if (value.getKind() != TemplateArgument::Type)
1508 return false;
1509 } else if (auto *type_param =
1510 llvm::dyn_cast<NonTypeTemplateParmDecl>(param)) {
1511 // Compare the argument kind, i.e. ensure that <typename> != <int>.
1512 if (!IsValueParam(value))
1513 return false;
1514 // Compare the integral type, i.e. ensure that <int> != <char>.
1515 if (type_param->getType() != value.getIntegralType())
1516 return false;
1517 } else {
1518 // There is no way to create other parameter decls at the moment, so we
1519 // can't reach this case during normal LLDB usage. Log that this happened
1520 // and assert.
1522 LLDB_LOG(log,
1523 "Don't know how to compare template parameter to passed"
1524 " value. Decl kind of parameter is: {0}",
1525 param->getDeclKindName());
1526 lldbassert(false && "Can't compare this TemplateParmDecl subclass");
1527 // In release builds just fall back to marking the parameter as not
1528 // accepting the value so that we don't try to fit an instantiation to a
1529 // template that doesn't fit. E.g., avoid that `S<1>` is being connected to
1530 // `template<typename T> struct S;`.
1531 return false;
1532 }
1533 return true;
1534}
1535
1536/// Returns true if the given class template declaration could produce an
1537/// instantiation with the specified values.
1538/// For example, `<typename T>` allows the arguments `float`, but not for
1539/// example `bool, float` or `3` (as an integer parameter value).
1541 ClassTemplateDecl *class_template_decl,
1542 const TypeSystemClang::TemplateParameterInfos &instantiation_values) {
1543
1544 TemplateParameterList &params = *class_template_decl->getTemplateParameters();
1545
1546 // Save some work by iterating only once over the found parameters and
1547 // calculate the information related to parameter packs.
1548
1549 // Contains the first pack parameter (or non if there are none).
1550 std::optional<NamedDecl *> pack_parameter;
1551 // Contains the number of non-pack parameters.
1552 size_t non_pack_params = params.size();
1553 for (size_t i = 0; i < params.size(); ++i) {
1554 NamedDecl *param = params.getParam(i);
1555 if (param->isParameterPack()) {
1556 pack_parameter = param;
1557 non_pack_params = i;
1558 break;
1559 }
1560 }
1561
1562 // The found template needs to have compatible non-pack template arguments.
1563 // E.g., ensure that <typename, typename> != <typename>.
1564 // The pack parameters are compared later.
1565 if (non_pack_params != instantiation_values.Size())
1566 return false;
1567
1568 // Ensure that <typename...> != <typename>.
1569 if (pack_parameter.has_value() != instantiation_values.hasParameterPack())
1570 return false;
1571
1572 // Compare the first pack parameter that was found with the first pack
1573 // parameter value. The special case of having an empty parameter pack value
1574 // always fits to a pack parameter.
1575 // E.g., ensure that <int...> != <typename...>.
1576 if (pack_parameter && !instantiation_values.GetParameterPack().IsEmpty() &&
1578 *pack_parameter, instantiation_values.GetParameterPack().Front()))
1579 return false;
1580
1581 // Compare all the non-pack parameters now.
1582 // E.g., ensure that <int> != <long>.
1583 for (const auto pair :
1584 llvm::zip_first(instantiation_values.GetArgs(), params)) {
1585 const TemplateArgument &passed_arg = std::get<0>(pair);
1586 NamedDecl *found_param = std::get<1>(pair);
1587 if (!TemplateParameterAllowsValue(found_param, passed_arg))
1588 return false;
1589 }
1590
1591 return class_template_decl;
1592}
1593
1595 DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1596 lldb::AccessType access_type, llvm::StringRef class_name, int kind,
1597 const TemplateParameterInfos &template_param_infos) {
1598 ASTContext &ast = getASTContext();
1599
1600 ClassTemplateDecl *class_template_decl = nullptr;
1601 if (decl_ctx == nullptr)
1602 decl_ctx = ast.getTranslationUnitDecl();
1603
1604 IdentifierInfo &identifier_info = ast.Idents.get(class_name);
1605 DeclarationName decl_name(&identifier_info);
1606
1607 // Search the AST for an existing ClassTemplateDecl that could be reused.
1608 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1609 for (NamedDecl *decl : result) {
1610 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1611 if (!class_template_decl)
1612 continue;
1613 // The class template has to be able to represents the instantiation
1614 // values we received. Without this we might end up putting an instantiation
1615 // with arguments such as <int, int> to a template such as:
1616 // template<typename T> struct S;
1617 // Connecting the instantiation to an incompatible template could cause
1618 // problems later on.
1619 if (!ClassTemplateAllowsToInstantiationArgs(class_template_decl,
1620 template_param_infos))
1621 continue;
1622 return class_template_decl;
1623 }
1624
1625 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1626
1627 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1628 ast, template_param_infos, template_param_decls);
1629
1630 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::CreateDeserialized(ast, 0);
1631 template_cxx_decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1632 // What decl context do we use here? TU? The actual decl context?
1633 template_cxx_decl->setDeclContext(decl_ctx);
1634 template_cxx_decl->setDeclName(decl_name);
1635 SetOwningModule(template_cxx_decl, owning_module);
1636
1637 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1638 i < template_param_decl_count; ++i) {
1639 template_param_decls[i]->setDeclContext(template_cxx_decl);
1640 }
1641
1642 // With templated classes, we say that a class is templated with
1643 // specializations, but that the bare class has no functions.
1644 // template_cxx_decl->startDefinition();
1645 // template_cxx_decl->completeDefinition();
1646
1647 class_template_decl = ClassTemplateDecl::CreateDeserialized(ast, 0);
1648 // What decl context do we use here? TU? The actual decl context?
1649 class_template_decl->setDeclContext(decl_ctx);
1650 class_template_decl->setDeclName(decl_name);
1651 class_template_decl->setTemplateParameters(template_param_list);
1652 class_template_decl->init(template_cxx_decl);
1653 template_cxx_decl->setDescribedClassTemplate(class_template_decl);
1654 SetOwningModule(class_template_decl, owning_module);
1655
1656 if (access_type != eAccessNone)
1657 class_template_decl->setAccess(
1659
1660 decl_ctx->addDecl(class_template_decl);
1661
1662 VerifyDecl(class_template_decl);
1663
1664 return class_template_decl;
1665}
1666
1667TemplateTemplateParmDecl *
1669 ASTContext &ast = getASTContext();
1670
1671 auto *decl_ctx = ast.getTranslationUnitDecl();
1672
1673 IdentifierInfo &identifier_info = ast.Idents.get(template_name);
1674 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1675
1676 TypeSystemClang::TemplateParameterInfos template_param_infos;
1677 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1678 ast, template_param_infos, template_param_decls);
1679
1680 // LLDB needs to create those decls only to be able to display a
1681 // type that includes a template template argument. Only the name matters for
1682 // this purpose, so we use dummy values for the other characteristics of the
1683 // type.
1684 return TemplateTemplateParmDecl::Create(
1685 ast, decl_ctx, SourceLocation(),
1686 /*Depth*/ 0, /*Position*/ 0,
1687 /*IsParameterPack*/ false, &identifier_info, template_param_list);
1688}
1689
1690ClassTemplateSpecializationDecl *
1692 DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1693 ClassTemplateDecl *class_template_decl, int kind,
1694 const TemplateParameterInfos &template_param_infos) {
1695 ASTContext &ast = getASTContext();
1696 llvm::SmallVector<clang::TemplateArgument, 2> args(
1697 template_param_infos.Size() +
1698 (template_param_infos.hasParameterPack() ? 1 : 0));
1699
1700 auto const &orig_args = template_param_infos.GetArgs();
1701 std::copy(orig_args.begin(), orig_args.end(), args.begin());
1702 if (template_param_infos.hasParameterPack()) {
1703 args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1704 ast, template_param_infos.GetParameterPackArgs());
1705 }
1706 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1707 ClassTemplateSpecializationDecl::CreateDeserialized(ast, 0);
1708 class_template_specialization_decl->setTagKind(
1709 static_cast<TagDecl::TagKind>(kind));
1710 class_template_specialization_decl->setDeclContext(decl_ctx);
1711 class_template_specialization_decl->setInstantiationOf(class_template_decl);
1712 class_template_specialization_decl->setTemplateArgs(
1713 TemplateArgumentList::CreateCopy(ast, args));
1714 ast.getTypeDeclType(class_template_specialization_decl, nullptr);
1715 class_template_specialization_decl->setDeclName(
1716 class_template_decl->getDeclName());
1717 SetOwningModule(class_template_specialization_decl, owning_module);
1718 decl_ctx->addDecl(class_template_specialization_decl);
1719
1720 class_template_specialization_decl->setSpecializationKind(
1721 TSK_ExplicitSpecialization);
1722
1723 return class_template_specialization_decl;
1724}
1725
1727 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1728 if (class_template_specialization_decl) {
1729 ASTContext &ast = getASTContext();
1730 return GetType(ast.getTagDeclType(class_template_specialization_decl));
1731 }
1732 return CompilerType();
1733}
1734
1735static inline bool check_op_param(bool is_method,
1736 clang::OverloadedOperatorKind op_kind,
1737 bool unary, bool binary,
1738 uint32_t num_params) {
1739 // Special-case call since it can take any number of operands
1740 if (op_kind == OO_Call)
1741 return true;
1742
1743 // The parameter count doesn't include "this"
1744 if (is_method)
1745 ++num_params;
1746 if (num_params == 1)
1747 return unary;
1748 if (num_params == 2)
1749 return binary;
1750 else
1751 return false;
1752}
1753
1755 bool is_method, clang::OverloadedOperatorKind op_kind,
1756 uint32_t num_params) {
1757 switch (op_kind) {
1758 default:
1759 break;
1760 // C++ standard allows any number of arguments to new/delete
1761 case OO_New:
1762 case OO_Array_New:
1763 case OO_Delete:
1764 case OO_Array_Delete:
1765 return true;
1766 }
1767
1768#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1769 case OO_##Name: \
1770 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1771 switch (op_kind) {
1772#include "clang/Basic/OperatorKinds.def"
1773 default:
1774 break;
1775 }
1776 return false;
1777}
1778
1779clang::AccessSpecifier
1781 clang::AccessSpecifier rhs) {
1782 // Make the access equal to the stricter of the field and the nested field's
1783 // access
1784 if (lhs == AS_none || rhs == AS_none)
1785 return AS_none;
1786 if (lhs == AS_private || rhs == AS_private)
1787 return AS_private;
1788 if (lhs == AS_protected || rhs == AS_protected)
1789 return AS_protected;
1790 return AS_public;
1791}
1792
1794 uint32_t &bitfield_bit_size) {
1795 ASTContext &ast = getASTContext();
1796 if (field == nullptr)
1797 return false;
1798
1799 if (field->isBitField()) {
1800 Expr *bit_width_expr = field->getBitWidth();
1801 if (bit_width_expr) {
1802 if (std::optional<llvm::APSInt> bit_width_apsint =
1803 bit_width_expr->getIntegerConstantExpr(ast)) {
1804 bitfield_bit_size = bit_width_apsint->getLimitedValue(UINT32_MAX);
1805 return true;
1806 }
1807 }
1808 }
1809 return false;
1810}
1811
1812bool TypeSystemClang::RecordHasFields(const RecordDecl *record_decl) {
1813 if (record_decl == nullptr)
1814 return false;
1815
1816 if (!record_decl->field_empty())
1817 return true;
1818
1819 // No fields, lets check this is a CXX record and check the base classes
1820 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1821 if (cxx_record_decl) {
1822 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1823 for (base_class = cxx_record_decl->bases_begin(),
1824 base_class_end = cxx_record_decl->bases_end();
1825 base_class != base_class_end; ++base_class) {
1826 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1827 base_class->getType()->getAs<RecordType>()->getDecl());
1828 if (RecordHasFields(base_class_decl))
1829 return true;
1830 }
1831 }
1832
1833 // We always want forcefully completed types to show up so we can print a
1834 // message in the summary that indicates that the type is incomplete.
1835 // This will help users know when they are running into issues with
1836 // -flimit-debug-info instead of just seeing nothing if this is a base class
1837 // (since we were hiding empty base classes), or nothing when you turn open
1838 // an valiable whose type was incomplete.
1839 ClangASTMetadata *meta_data = GetMetadata(record_decl);
1840 if (meta_data && meta_data->IsForcefullyCompleted())
1841 return true;
1842
1843 return false;
1844}
1845
1846#pragma mark Objective-C Classes
1847
1849 llvm::StringRef name, clang::DeclContext *decl_ctx,
1850 OptionalClangModuleID owning_module, bool isForwardDecl, bool isInternal,
1851 ClangASTMetadata *metadata) {
1852 ASTContext &ast = getASTContext();
1853 assert(!name.empty());
1854 if (!decl_ctx)
1855 decl_ctx = ast.getTranslationUnitDecl();
1856
1857 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::CreateDeserialized(ast, 0);
1858 decl->setDeclContext(decl_ctx);
1859 decl->setDeclName(&ast.Idents.get(name));
1860 /*isForwardDecl,*/
1861 decl->setImplicit(isInternal);
1862 SetOwningModule(decl, owning_module);
1863
1864 if (metadata)
1865 SetMetadata(decl, *metadata);
1866
1867 return GetType(ast.getObjCInterfaceType(decl));
1868}
1869
1870bool TypeSystemClang::BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1871 return !TypeSystemClang::RecordHasFields(b->getType()->getAsCXXRecordDecl());
1872}
1873
1874uint32_t
1875TypeSystemClang::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1876 bool omit_empty_base_classes) {
1877 uint32_t num_bases = 0;
1878 if (cxx_record_decl) {
1879 if (omit_empty_base_classes) {
1880 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1881 for (base_class = cxx_record_decl->bases_begin(),
1882 base_class_end = cxx_record_decl->bases_end();
1883 base_class != base_class_end; ++base_class) {
1884 // Skip empty base classes
1885 if (BaseSpecifierIsEmpty(base_class))
1886 continue;
1887 ++num_bases;
1888 }
1889 } else
1890 num_bases = cxx_record_decl->getNumBases();
1891 }
1892 return num_bases;
1893}
1894
1895#pragma mark Namespace Declarations
1896
1898 const char *name, clang::DeclContext *decl_ctx,
1899 OptionalClangModuleID owning_module, bool is_inline) {
1900 NamespaceDecl *namespace_decl = nullptr;
1901 ASTContext &ast = getASTContext();
1902 TranslationUnitDecl *translation_unit_decl = ast.getTranslationUnitDecl();
1903 if (!decl_ctx)
1904 decl_ctx = translation_unit_decl;
1905
1906 if (name) {
1907 IdentifierInfo &identifier_info = ast.Idents.get(name);
1908 DeclarationName decl_name(&identifier_info);
1909 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1910 for (NamedDecl *decl : result) {
1911 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1912 if (namespace_decl)
1913 return namespace_decl;
1914 }
1915
1916 namespace_decl = NamespaceDecl::Create(ast, decl_ctx, is_inline,
1917 SourceLocation(), SourceLocation(),
1918 &identifier_info, nullptr, false);
1919
1920 decl_ctx->addDecl(namespace_decl);
1921 } else {
1922 if (decl_ctx == translation_unit_decl) {
1923 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1924 if (namespace_decl)
1925 return namespace_decl;
1926
1927 namespace_decl =
1928 NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1929 SourceLocation(), nullptr, nullptr, false);
1930 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1931 translation_unit_decl->addDecl(namespace_decl);
1932 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1933 } else {
1934 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1935 if (parent_namespace_decl) {
1936 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1937 if (namespace_decl)
1938 return namespace_decl;
1939 namespace_decl =
1940 NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1941 SourceLocation(), nullptr, nullptr, false);
1942 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1943 parent_namespace_decl->addDecl(namespace_decl);
1944 assert(namespace_decl ==
1945 parent_namespace_decl->getAnonymousNamespace());
1946 } else {
1947 assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1948 "no namespace as decl_ctx");
1949 }
1950 }
1951 }
1952 // Note: namespaces can span multiple modules, so perhaps this isn't a good
1953 // idea.
1954 SetOwningModule(namespace_decl, owning_module);
1955
1956 VerifyDecl(namespace_decl);
1957 return namespace_decl;
1958}
1959
1960clang::BlockDecl *
1962 OptionalClangModuleID owning_module) {
1963 if (ctx) {
1964 clang::BlockDecl *decl =
1965 clang::BlockDecl::CreateDeserialized(getASTContext(), 0);
1966 decl->setDeclContext(ctx);
1967 ctx->addDecl(decl);
1968 SetOwningModule(decl, owning_module);
1969 return decl;
1970 }
1971 return nullptr;
1972}
1973
1974clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1975 clang::DeclContext *right,
1976 clang::DeclContext *root) {
1977 if (root == nullptr)
1978 return nullptr;
1979
1980 std::set<clang::DeclContext *> path_left;
1981 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1982 path_left.insert(d);
1983
1984 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1985 if (path_left.find(d) != path_left.end())
1986 return d;
1987
1988 return nullptr;
1989}
1990
1992 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1993 clang::NamespaceDecl *ns_decl) {
1994 if (decl_ctx && ns_decl) {
1995 auto *translation_unit = getASTContext().getTranslationUnitDecl();
1996 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1997 getASTContext(), decl_ctx, clang::SourceLocation(),
1998 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1999 clang::SourceLocation(), ns_decl,
2000 FindLCABetweenDecls(decl_ctx, ns_decl,
2001 translation_unit));
2002 decl_ctx->addDecl(using_decl);
2003 SetOwningModule(using_decl, owning_module);
2004 return using_decl;
2005 }
2006 return nullptr;
2007}
2008
2009clang::UsingDecl *
2010TypeSystemClang::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
2011 OptionalClangModuleID owning_module,
2012 clang::NamedDecl *target) {
2013 if (current_decl_ctx && target) {
2014 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
2015 getASTContext(), current_decl_ctx, clang::SourceLocation(),
2016 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
2017 SetOwningModule(using_decl, owning_module);
2018 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
2019 getASTContext(), current_decl_ctx, clang::SourceLocation(),
2020 target->getDeclName(), using_decl, target);
2021 SetOwningModule(shadow_decl, owning_module);
2022 using_decl->addShadowDecl(shadow_decl);
2023 current_decl_ctx->addDecl(using_decl);
2024 return using_decl;
2025 }
2026 return nullptr;
2027}
2028
2030 clang::DeclContext *decl_context, OptionalClangModuleID owning_module,
2031 const char *name, clang::QualType type) {
2032 if (decl_context) {
2033 clang::VarDecl *var_decl =
2034 clang::VarDecl::CreateDeserialized(getASTContext(), 0);
2035 var_decl->setDeclContext(decl_context);
2036 if (name && name[0])
2037 var_decl->setDeclName(&getASTContext().Idents.getOwn(name));
2038 var_decl->setType(type);
2039 SetOwningModule(var_decl, owning_module);
2040 var_decl->setAccess(clang::AS_public);
2041 decl_context->addDecl(var_decl);
2042 return var_decl;
2043 }
2044 return nullptr;
2045}
2046
2049 lldb::BasicType basic_type) {
2050 switch (basic_type) {
2051 case eBasicTypeVoid:
2052 return ast->VoidTy.getAsOpaquePtr();
2053 case eBasicTypeChar:
2054 return ast->CharTy.getAsOpaquePtr();
2056 return ast->SignedCharTy.getAsOpaquePtr();
2058 return ast->UnsignedCharTy.getAsOpaquePtr();
2059 case eBasicTypeWChar:
2060 return ast->getWCharType().getAsOpaquePtr();
2062 return ast->getSignedWCharType().getAsOpaquePtr();
2064 return ast->getUnsignedWCharType().getAsOpaquePtr();
2065 case eBasicTypeChar8:
2066 return ast->Char8Ty.getAsOpaquePtr();
2067 case eBasicTypeChar16:
2068 return ast->Char16Ty.getAsOpaquePtr();
2069 case eBasicTypeChar32:
2070 return ast->Char32Ty.getAsOpaquePtr();
2071 case eBasicTypeShort:
2072 return ast->ShortTy.getAsOpaquePtr();
2074 return ast->UnsignedShortTy.getAsOpaquePtr();
2075 case eBasicTypeInt:
2076 return ast->IntTy.getAsOpaquePtr();
2078 return ast->UnsignedIntTy.getAsOpaquePtr();
2079 case eBasicTypeLong:
2080 return ast->LongTy.getAsOpaquePtr();
2082 return ast->UnsignedLongTy.getAsOpaquePtr();
2083 case eBasicTypeLongLong:
2084 return ast->LongLongTy.getAsOpaquePtr();
2086 return ast->UnsignedLongLongTy.getAsOpaquePtr();
2087 case eBasicTypeInt128:
2088 return ast->Int128Ty.getAsOpaquePtr();
2090 return ast->UnsignedInt128Ty.getAsOpaquePtr();
2091 case eBasicTypeBool:
2092 return ast->BoolTy.getAsOpaquePtr();
2093 case eBasicTypeHalf:
2094 return ast->HalfTy.getAsOpaquePtr();
2095 case eBasicTypeFloat:
2096 return ast->FloatTy.getAsOpaquePtr();
2097 case eBasicTypeDouble:
2098 return ast->DoubleTy.getAsOpaquePtr();
2100 return ast->LongDoubleTy.getAsOpaquePtr();
2102 return ast->getComplexType(ast->FloatTy).getAsOpaquePtr();
2104 return ast->getComplexType(ast->DoubleTy).getAsOpaquePtr();
2106 return ast->getComplexType(ast->LongDoubleTy).getAsOpaquePtr();
2107 case eBasicTypeObjCID:
2108 return ast->getObjCIdType().getAsOpaquePtr();
2110 return ast->getObjCClassType().getAsOpaquePtr();
2111 case eBasicTypeObjCSel:
2112 return ast->getObjCSelType().getAsOpaquePtr();
2113 case eBasicTypeNullPtr:
2114 return ast->NullPtrTy.getAsOpaquePtr();
2115 default:
2116 return nullptr;
2117 }
2118}
2119
2120#pragma mark Function Types
2121
2122clang::DeclarationName
2124 const CompilerType &function_clang_type) {
2125 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
2126 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
2127 return DeclarationName(&getASTContext().Idents.get(
2128 name)); // Not operator, but a regular function.
2129
2130 // Check the number of operator parameters. Sometimes we have seen bad DWARF
2131 // that doesn't correctly describe operators and if we try to create a method
2132 // and add it to the class, clang will assert and crash, so we need to make
2133 // sure things are acceptable.
2134 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
2135 const clang::FunctionProtoType *function_type =
2136 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
2137 if (function_type == nullptr)
2138 return clang::DeclarationName();
2139
2140 const bool is_method = false;
2141 const unsigned int num_params = function_type->getNumParams();
2143 is_method, op_kind, num_params))
2144 return clang::DeclarationName();
2145
2146 return getASTContext().DeclarationNames.getCXXOperatorName(op_kind);
2147}
2148
2150 clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
2151 printing_policy.SuppressTagKeyword = true;
2152 // Inline namespaces are important for some type formatters (e.g., libc++
2153 // and libstdc++ are differentiated by their inline namespaces).
2154 printing_policy.SuppressInlineNamespace = false;
2155 printing_policy.SuppressUnwrittenScope = false;
2156 // Default arguments are also always important for type formatters. Otherwise
2157 // we would need to always specify two type names for the setups where we do
2158 // know the default arguments and where we don't know default arguments.
2159 //
2160 // For example, without this we would need to have formatters for both:
2161 // std::basic_string<char>
2162 // and
2163 // std::basic_string<char, std::char_traits<char>, std::allocator<char> >
2164 // to support setups where LLDB was able to reconstruct default arguments
2165 // (and we then would have suppressed them from the type name) and also setups
2166 // where LLDB wasn't able to reconstruct the default arguments.
2167 printing_policy.SuppressDefaultTemplateArgs = false;
2168 return printing_policy;
2169}
2170
2171std::string TypeSystemClang::GetTypeNameForDecl(const NamedDecl *named_decl,
2172 bool qualified) {
2173 clang::PrintingPolicy printing_policy = GetTypePrintingPolicy();
2174 std::string result;
2175 llvm::raw_string_ostream os(result);
2176 named_decl->getNameForDiagnostic(os, printing_policy, qualified);
2177 return result;
2178}
2179
2181 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
2182 llvm::StringRef name, const CompilerType &function_clang_type,
2183 clang::StorageClass storage, bool is_inline) {
2184 FunctionDecl *func_decl = nullptr;
2185 ASTContext &ast = getASTContext();
2186 if (!decl_ctx)
2187 decl_ctx = ast.getTranslationUnitDecl();
2188
2189 const bool hasWrittenPrototype = true;
2190 const bool isConstexprSpecified = false;
2191
2192 clang::DeclarationName declarationName =
2193 GetDeclarationName(name, function_clang_type);
2194 func_decl = FunctionDecl::CreateDeserialized(ast, 0);
2195 func_decl->setDeclContext(decl_ctx);
2196 func_decl->setDeclName(declarationName);
2197 func_decl->setType(ClangUtil::GetQualType(function_clang_type));
2198 func_decl->setStorageClass(storage);
2199 func_decl->setInlineSpecified(is_inline);
2200 func_decl->setHasWrittenPrototype(hasWrittenPrototype);
2201 func_decl->setConstexprKind(isConstexprSpecified
2202 ? ConstexprSpecKind::Constexpr
2203 : ConstexprSpecKind::Unspecified);
2204 SetOwningModule(func_decl, owning_module);
2205 decl_ctx->addDecl(func_decl);
2206
2207 VerifyDecl(func_decl);
2208
2209 return func_decl;
2210}
2211
2213 const CompilerType &result_type, const CompilerType *args,
2214 unsigned num_args, bool is_variadic, unsigned type_quals,
2215 clang::CallingConv cc, clang::RefQualifierKind ref_qual) {
2216 if (!result_type || !ClangUtil::IsClangType(result_type))
2217 return CompilerType(); // invalid return type
2218
2219 std::vector<QualType> qual_type_args;
2220 if (num_args > 0 && args == nullptr)
2221 return CompilerType(); // invalid argument array passed in
2222
2223 // Verify that all arguments are valid and the right type
2224 for (unsigned i = 0; i < num_args; ++i) {
2225 if (args[i]) {
2226 // Make sure we have a clang type in args[i] and not a type from another
2227 // language whose name might match
2228 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2229 lldbassert(is_clang_type);
2230 if (is_clang_type)
2231 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2232 else
2233 return CompilerType(); // invalid argument type (must be a clang type)
2234 } else
2235 return CompilerType(); // invalid argument type (empty)
2236 }
2237
2238 // TODO: Detect calling convention in DWARF?
2239 FunctionProtoType::ExtProtoInfo proto_info;
2240 proto_info.ExtInfo = cc;
2241 proto_info.Variadic = is_variadic;
2242 proto_info.ExceptionSpec = EST_None;
2243 proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
2244 proto_info.RefQualifier = ref_qual;
2245
2246 return GetType(getASTContext().getFunctionType(
2247 ClangUtil::GetQualType(result_type), qual_type_args, proto_info));
2248}
2249
2251 clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
2252 const char *name, const CompilerType &param_type, int storage,
2253 bool add_decl) {
2254 ASTContext &ast = getASTContext();
2255 auto *decl = ParmVarDecl::CreateDeserialized(ast, 0);
2256 decl->setDeclContext(decl_ctx);
2257 if (name && name[0])
2258 decl->setDeclName(&ast.Idents.get(name));
2259 decl->setType(ClangUtil::GetQualType(param_type));
2260 decl->setStorageClass(static_cast<clang::StorageClass>(storage));
2261 SetOwningModule(decl, owning_module);
2262 if (add_decl)
2263 decl_ctx->addDecl(decl);
2264
2265 return decl;
2266}
2267
2269 FunctionDecl *function_decl, llvm::ArrayRef<ParmVarDecl *> params) {
2270 if (function_decl)
2271 function_decl->setParams(params);
2272}
2273
2276 QualType block_type = m_ast_up->getBlockPointerType(
2277 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2278
2279 return GetType(block_type);
2280}
2281
2282#pragma mark Array Types
2283
2285 size_t element_count,
2286 bool is_vector) {
2287 if (element_type.IsValid()) {
2288 ASTContext &ast = getASTContext();
2289
2290 if (is_vector) {
2291 return GetType(ast.getExtVectorType(ClangUtil::GetQualType(element_type),
2292 element_count));
2293 } else {
2294
2295 llvm::APInt ap_element_count(64, element_count);
2296 if (element_count == 0) {
2297 return GetType(
2298 ast.getIncompleteArrayType(ClangUtil::GetQualType(element_type),
2299 clang::ArraySizeModifier::Normal, 0));
2300 } else {
2301 return GetType(ast.getConstantArrayType(
2302 ClangUtil::GetQualType(element_type), ap_element_count, nullptr,
2303 clang::ArraySizeModifier::Normal, 0));
2304 }
2305 }
2306 }
2307 return CompilerType();
2308}
2309
2311 llvm::StringRef type_name,
2312 const std::initializer_list<std::pair<const char *, CompilerType>>
2313 &type_fields,
2314 bool packed) {
2315 CompilerType type;
2316 if (!type_name.empty() &&
2317 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2318 .IsValid()) {
2319 lldbassert(0 && "Trying to create a type for an existing name");
2320 return type;
2321 }
2322
2323 type = CreateRecordType(
2324 nullptr, OptionalClangModuleID(), lldb::eAccessPublic, type_name,
2325 llvm::to_underlying(clang::TagTypeKind::Struct), lldb::eLanguageTypeC);
2327 for (const auto &field : type_fields)
2328 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2329 0);
2330 if (packed)
2331 SetIsPacked(type);
2333 return type;
2334}
2335
2337 llvm::StringRef type_name,
2338 const std::initializer_list<std::pair<const char *, CompilerType>>
2339 &type_fields,
2340 bool packed) {
2341 CompilerType type;
2342 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2343 return type;
2344
2345 return CreateStructForIdentifier(type_name, type_fields, packed);
2346}
2347
2348#pragma mark Enumeration Types
2349
2351 llvm::StringRef name, clang::DeclContext *decl_ctx,
2352 OptionalClangModuleID owning_module, const Declaration &decl,
2353 const CompilerType &integer_clang_type, bool is_scoped) {
2354 // TODO: Do something intelligent with the Declaration object passed in
2355 // like maybe filling in the SourceLocation with it...
2356 ASTContext &ast = getASTContext();
2357
2358 // TODO: ask about these...
2359 // const bool IsFixed = false;
2360 EnumDecl *enum_decl = EnumDecl::CreateDeserialized(ast, 0);
2361 enum_decl->setDeclContext(decl_ctx);
2362 if (!name.empty())
2363 enum_decl->setDeclName(&ast.Idents.get(name));
2364 enum_decl->setScoped(is_scoped);
2365 enum_decl->setScopedUsingClassTag(is_scoped);
2366 enum_decl->setFixed(false);
2367 SetOwningModule(enum_decl, owning_module);
2368 if (decl_ctx)
2369 decl_ctx->addDecl(enum_decl);
2370
2371 // TODO: check if we should be setting the promotion type too?
2372 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2373
2374 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2375
2376 return GetType(ast.getTagDeclType(enum_decl));
2377}
2378
2380 bool is_signed) {
2381 clang::ASTContext &ast = getASTContext();
2382
2383 if (is_signed) {
2384 if (bit_size == ast.getTypeSize(ast.SignedCharTy))
2385 return GetType(ast.SignedCharTy);
2386
2387 if (bit_size == ast.getTypeSize(ast.ShortTy))
2388 return GetType(ast.ShortTy);
2389
2390 if (bit_size == ast.getTypeSize(ast.IntTy))
2391 return GetType(ast.IntTy);
2392
2393 if (bit_size == ast.getTypeSize(ast.LongTy))
2394 return GetType(ast.LongTy);
2395
2396 if (bit_size == ast.getTypeSize(ast.LongLongTy))
2397 return GetType(ast.LongLongTy);
2398
2399 if (bit_size == ast.getTypeSize(ast.Int128Ty))
2400 return GetType(ast.Int128Ty);
2401 } else {
2402 if (bit_size == ast.getTypeSize(ast.UnsignedCharTy))
2403 return GetType(ast.UnsignedCharTy);
2404
2405 if (bit_size == ast.getTypeSize(ast.UnsignedShortTy))
2406 return GetType(ast.UnsignedShortTy);
2407
2408 if (bit_size == ast.getTypeSize(ast.UnsignedIntTy))
2409 return GetType(ast.UnsignedIntTy);
2410
2411 if (bit_size == ast.getTypeSize(ast.UnsignedLongTy))
2412 return GetType(ast.UnsignedLongTy);
2413
2414 if (bit_size == ast.getTypeSize(ast.UnsignedLongLongTy))
2415 return GetType(ast.UnsignedLongLongTy);
2416
2417 if (bit_size == ast.getTypeSize(ast.UnsignedInt128Ty))
2418 return GetType(ast.UnsignedInt128Ty);
2419 }
2420 return CompilerType();
2421}
2422
2424 return GetIntTypeFromBitSize(
2425 getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
2426}
2427
2428void TypeSystemClang::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2429 if (decl_ctx) {
2430 DumpDeclContextHiearchy(decl_ctx->getParent());
2431
2432 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2433 if (named_decl) {
2434 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2435 named_decl->getDeclName().getAsString().c_str());
2436 } else {
2437 printf("%20s\n", decl_ctx->getDeclKindName());
2438 }
2439 }
2440}
2441
2442void TypeSystemClang::DumpDeclHiearchy(clang::Decl *decl) {
2443 if (decl == nullptr)
2444 return;
2445 DumpDeclContextHiearchy(decl->getDeclContext());
2446
2447 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2448 if (record_decl) {
2449 printf("%20s: %s%s\n", decl->getDeclKindName(),
2450 record_decl->getDeclName().getAsString().c_str(),
2451 record_decl->isInjectedClassName() ? " (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 !tag_decl->getTypeForDecl()->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
2525 auto It = m_decl_metadata.find(object);
2526 if (It != m_decl_metadata.end())
2527 return &It->second;
2528 return nullptr;
2529}
2530
2532 auto It = m_type_metadata.find(object);
2533 if (It != m_type_metadata.end())
2534 return &It->second;
2535 return nullptr;
2536}
2537
2538void TypeSystemClang::SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object,
2539 clang::AccessSpecifier access) {
2540 if (access == clang::AccessSpecifier::AS_none)
2541 m_cxx_record_decl_access.erase(object);
2542 else
2543 m_cxx_record_decl_access[object] = access;
2544}
2545
2546clang::AccessSpecifier
2547TypeSystemClang::GetCXXRecordDeclAccess(const clang::CXXRecordDecl *object) {
2548 auto It = m_cxx_record_decl_access.find(object);
2549 if (It != m_cxx_record_decl_access.end())
2550 return It->second;
2551 return clang::AccessSpecifier::AS_none;
2552}
2553
2554clang::DeclContext *
2557}
2558
2561 if (auto *decl_context = GetDeclContextForType(type))
2562 return CreateDeclContext(decl_context);
2563 return CompilerDeclContext();
2564}
2565
2566/// Aggressively desugar the provided type, skipping past various kinds of
2567/// syntactic sugar and other constructs one typically wants to ignore.
2568/// The \p mask argument allows one to skip certain kinds of simplifications,
2569/// when one wishes to handle a certain kind of type directly.
2570static QualType
2571RemoveWrappingTypes(QualType type, ArrayRef<clang::Type::TypeClass> mask = {}) {
2572 while (true) {
2573 if (find(mask, type->getTypeClass()) != mask.end())
2574 return type;
2575 switch (type->getTypeClass()) {
2576 // This is not fully correct as _Atomic is more than sugar, but it is
2577 // sufficient for the purposes we care about.
2578 case clang::Type::Atomic:
2579 type = cast<clang::AtomicType>(type)->getValueType();
2580 break;
2581 case clang::Type::Auto:
2582 case clang::Type::Decltype:
2583 case clang::Type::Elaborated:
2584 case clang::Type::Paren:
2585 case clang::Type::SubstTemplateTypeParm:
2586 case clang::Type::TemplateSpecialization:
2587 case clang::Type::Typedef:
2588 case clang::Type::TypeOf:
2589 case clang::Type::TypeOfExpr:
2590 case clang::Type::Using:
2591 type = type->getLocallyUnqualifiedSingleStepDesugaredType();
2592 break;
2593 default:
2594 return type;
2595 }
2596 }
2597}
2598
2599clang::DeclContext *
2601 if (type.isNull())
2602 return nullptr;
2603
2604 clang::QualType qual_type = RemoveWrappingTypes(type.getCanonicalType());
2605 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2606 switch (type_class) {
2607 case clang::Type::ObjCInterface:
2608 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2609 ->getInterface();
2610 case clang::Type::ObjCObjectPointer:
2611 return GetDeclContextForType(
2612 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2613 ->getPointeeType());
2614 case clang::Type::Record:
2615 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2616 case clang::Type::Enum:
2617 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2618 default:
2619 break;
2620 }
2621 // No DeclContext in this type...
2622 return nullptr;
2623}
2624
2625static bool GetCompleteQualType(clang::ASTContext *ast,
2626 clang::QualType qual_type,
2627 bool allow_completion = true) {
2628 qual_type = RemoveWrappingTypes(qual_type);
2629 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2630 switch (type_class) {
2631 case clang::Type::ConstantArray:
2632 case clang::Type::IncompleteArray:
2633 case clang::Type::VariableArray: {
2634 const clang::ArrayType *array_type =
2635 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2636
2637 if (array_type)
2638 return GetCompleteQualType(ast, array_type->getElementType(),
2639 allow_completion);
2640 } break;
2641 case clang::Type::Record: {
2642 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2643 if (cxx_record_decl) {
2644 if (cxx_record_decl->hasExternalLexicalStorage()) {
2645 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2646 const bool fields_loaded =
2647 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2648 if (is_complete && fields_loaded)
2649 return true;
2650
2651 if (!allow_completion)
2652 return false;
2653
2654 // Call the field_begin() accessor to for it to use the external source
2655 // to load the fields...
2656 clang::ExternalASTSource *external_ast_source =
2657 ast->getExternalSource();
2658 if (external_ast_source) {
2659 external_ast_source->CompleteType(cxx_record_decl);
2660 if (cxx_record_decl->isCompleteDefinition()) {
2661 cxx_record_decl->field_begin();
2662 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2663 }
2664 }
2665 }
2666 }
2667 const clang::TagType *tag_type =
2668 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2669 return !tag_type->isIncompleteType();
2670 } break;
2671
2672 case clang::Type::Enum: {
2673 const clang::TagType *tag_type =
2674 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2675 if (tag_type) {
2676 clang::TagDecl *tag_decl = tag_type->getDecl();
2677 if (tag_decl) {
2678 if (tag_decl->getDefinition())
2679 return true;
2680
2681 if (!allow_completion)
2682 return false;
2683
2684 if (tag_decl->hasExternalLexicalStorage()) {
2685 if (ast) {
2686 clang::ExternalASTSource *external_ast_source =
2687 ast->getExternalSource();
2688 if (external_ast_source) {
2689 external_ast_source->CompleteType(tag_decl);
2690 return !tag_type->isIncompleteType();
2691 }
2692 }
2693 }
2694 return false;
2695 }
2696 }
2697
2698 } break;
2699 case clang::Type::ObjCObject:
2700 case clang::Type::ObjCInterface: {
2701 const clang::ObjCObjectType *objc_class_type =
2702 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2703 if (objc_class_type) {
2704 clang::ObjCInterfaceDecl *class_interface_decl =
2705 objc_class_type->getInterface();
2706 // We currently can't complete objective C types through the newly added
2707 // ASTContext because it only supports TagDecl objects right now...
2708 if (class_interface_decl) {
2709 if (class_interface_decl->getDefinition())
2710 return true;
2711
2712 if (!allow_completion)
2713 return false;
2714
2715 if (class_interface_decl->hasExternalLexicalStorage()) {
2716 if (ast) {
2717 clang::ExternalASTSource *external_ast_source =
2718 ast->getExternalSource();
2719 if (external_ast_source) {
2720 external_ast_source->CompleteType(class_interface_decl);
2721 return !objc_class_type->isIncompleteType();
2722 }
2723 }
2724 }
2725 return false;
2726 }
2727 }
2728 } break;
2729
2730 case clang::Type::Attributed:
2731 return GetCompleteQualType(
2732 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2733 allow_completion);
2734
2735 default:
2736 break;
2737 }
2738
2739 return true;
2740}
2741
2742static clang::ObjCIvarDecl::AccessControl
2744 switch (access) {
2745 case eAccessNone:
2746 return clang::ObjCIvarDecl::None;
2747 case eAccessPublic:
2748 return clang::ObjCIvarDecl::Public;
2749 case eAccessPrivate:
2750 return clang::ObjCIvarDecl::Private;
2751 case eAccessProtected:
2752 return clang::ObjCIvarDecl::Protected;
2753 case eAccessPackage:
2754 return clang::ObjCIvarDecl::Package;
2755 }
2756 return clang::ObjCIvarDecl::None;
2757}
2758
2759// Tests
2760
2761#ifndef NDEBUG
2763 return !type || llvm::isa<clang::Type>(GetQualType(type).getTypePtr());
2764}
2765#endif
2766
2768 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2769
2770 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2771 switch (type_class) {
2772 case clang::Type::IncompleteArray:
2773 case clang::Type::VariableArray:
2774 case clang::Type::ConstantArray:
2775 case clang::Type::ExtVector:
2776 case clang::Type::Vector:
2777 case clang::Type::Record:
2778 case clang::Type::ObjCObject:
2779 case clang::Type::ObjCInterface:
2780 return true;
2781 default:
2782 break;
2783 }
2784 // The clang type does have a value
2785 return false;
2786}
2787
2789 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2790
2791 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2792 switch (type_class) {
2793 case clang::Type::Record: {
2794 if (const clang::RecordType *record_type =
2795 llvm::dyn_cast_or_null<clang::RecordType>(
2796 qual_type.getTypePtrOrNull())) {
2797 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2798 return record_decl->isAnonymousStructOrUnion();
2799 }
2800 }
2801 break;
2802 }
2803 default:
2804 break;
2805 }
2806 // The clang type does have a value
2807 return false;
2808}
2809
2811 CompilerType *element_type_ptr,
2812 uint64_t *size, bool *is_incomplete) {
2813 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2814
2815 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2816 switch (type_class) {
2817 default:
2818 break;
2819
2820 case clang::Type::ConstantArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
2823 weak_from_this(), llvm::cast<clang::ConstantArrayType>(qual_type)
2824 ->getElementType()
2825 .getAsOpaquePtr());
2826 if (size)
2827 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2828 ->getSize()
2829 .getLimitedValue(ULLONG_MAX);
2830 if (is_incomplete)
2831 *is_incomplete = false;
2832 return true;
2833
2834 case clang::Type::IncompleteArray:
2835 if (element_type_ptr)
2836 element_type_ptr->SetCompilerType(
2837 weak_from_this(), llvm::cast<clang::IncompleteArrayType>(qual_type)
2838 ->getElementType()
2839 .getAsOpaquePtr());
2840 if (size)
2841 *size = 0;
2842 if (is_incomplete)
2843 *is_incomplete = true;
2844 return true;
2845
2846 case clang::Type::VariableArray:
2847 if (element_type_ptr)
2848 element_type_ptr->SetCompilerType(
2849 weak_from_this(), llvm::cast<clang::VariableArrayType>(qual_type)
2850 ->getElementType()
2851 .getAsOpaquePtr());
2852 if (size)
2853 *size = 0;
2854 if (is_incomplete)
2855 *is_incomplete = false;
2856 return true;
2857
2858 case clang::Type::DependentSizedArray:
2859 if (element_type_ptr)
2860 element_type_ptr->SetCompilerType(
2861 weak_from_this(),
2862 llvm::cast<clang::DependentSizedArrayType>(qual_type)
2863 ->getElementType()
2864 .getAsOpaquePtr());
2865 if (size)
2866 *size = 0;
2867 if (is_incomplete)
2868 *is_incomplete = false;
2869 return true;
2870 }
2871 if (element_type_ptr)
2872 element_type_ptr->Clear();
2873 if (size)
2874 *size = 0;
2875 if (is_incomplete)
2876 *is_incomplete = false;
2877 return false;
2878}
2879
2881 CompilerType *element_type, uint64_t *size) {
2882 clang::QualType qual_type(GetCanonicalQualType(type));
2883
2884 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2885 switch (type_class) {
2886 case clang::Type::Vector: {
2887 const clang::VectorType *vector_type =
2888 qual_type->getAs<clang::VectorType>();
2889 if (vector_type) {
2890 if (size)
2891 *size = vector_type->getNumElements();
2892 if (element_type)
2893 *element_type = GetType(vector_type->getElementType());
2894 }
2895 return true;
2896 } break;
2897 case clang::Type::ExtVector: {
2898 const clang::ExtVectorType *ext_vector_type =
2899 qual_type->getAs<clang::ExtVectorType>();
2900 if (ext_vector_type) {
2901 if (size)
2902 *size = ext_vector_type->getNumElements();
2903 if (element_type)
2904 *element_type =
2905 CompilerType(weak_from_this(),
2906 ext_vector_type->getElementType().getAsOpaquePtr());
2907 }
2908 return true;
2909 }
2910 default:
2911 break;
2912 }
2913 return false;
2914}
2915
2918 clang::DeclContext *decl_ctx = GetDeclContextForType(GetQualType(type));
2919 if (!decl_ctx)
2920 return false;
2921
2922 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2923 return false;
2924
2925 clang::ObjCInterfaceDecl *result_iface_decl =
2926 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2927
2928 ClangASTMetadata *ast_metadata = GetMetadata(result_iface_decl);
2929 if (!ast_metadata)
2930 return false;
2931 return (ast_metadata->GetISAPtr() != 0);
2932}
2933
2935 return GetQualType(type).getUnqualifiedType()->isCharType();
2936}
2937
2939 // If the type hasn't been lazily completed yet, complete it now so that we
2940 // can give the caller an accurate answer whether the type actually has a
2941 // definition. Without completing the type now we would just tell the user
2942 // the current (internal) completeness state of the type and most users don't
2943 // care (or even know) about this behavior.
2944 const bool allow_completion = true;
2946 allow_completion);
2947}
2948
2950 return GetQualType(type).isConstQualified();
2951}
2952
2954 uint32_t &length) {
2955 CompilerType pointee_or_element_clang_type;
2956 length = 0;
2957 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2958
2959 if (!pointee_or_element_clang_type.IsValid())
2960 return false;
2961
2962 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2963 if (pointee_or_element_clang_type.IsCharType()) {
2964 if (type_flags.Test(eTypeIsArray)) {
2965 // We know the size of the array and it could be a C string since it is
2966 // an array of characters
2967 length = llvm::cast<clang::ConstantArrayType>(
2968 GetCanonicalQualType(type).getTypePtr())
2969 ->getSize()
2970 .getLimitedValue();
2971 }
2972 return true;
2973 }
2974 }
2975 return false;
2976}
2977
2979 auto isFunctionType = [&](clang::QualType qual_type) {
2980 return qual_type->isFunctionType();
2981 };
2982
2983 return IsTypeImpl(type, isFunctionType);
2984}
2985
2986// Used to detect "Homogeneous Floating-point Aggregates"
2987uint32_t
2989 CompilerType *base_type_ptr) {
2990 if (!type)
2991 return 0;
2992
2993 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2994 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2995 switch (type_class) {
2996 case clang::Type::Record:
2997 if (GetCompleteType(type)) {
2998 const clang::CXXRecordDecl *cxx_record_decl =
2999 qual_type->getAsCXXRecordDecl();
3000 if (cxx_record_decl) {
3001 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3002 return 0;
3003 }
3004 const clang::RecordType *record_type =
3005 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3006 if (record_type) {
3007 const clang::RecordDecl *record_decl = record_type->getDecl();
3008 if (record_decl) {
3009 // We are looking for a structure that contains only floating point
3010 // types
3011 clang::RecordDecl::field_iterator field_pos,
3012 field_end = record_decl->field_end();
3013 uint32_t num_fields = 0;
3014 bool is_hva = false;
3015 bool is_hfa = false;
3016 clang::QualType base_qual_type;
3017 uint64_t base_bitwidth = 0;
3018 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3019 ++field_pos) {
3020 clang::QualType field_qual_type = field_pos->getType();
3021 uint64_t field_bitwidth = getASTContext().getTypeSize(qual_type);
3022 if (field_qual_type->isFloatingType()) {
3023 if (field_qual_type->isComplexType())
3024 return 0;
3025 else {
3026 if (num_fields == 0)
3027 base_qual_type = field_qual_type;
3028 else {
3029 if (is_hva)
3030 return 0;
3031 is_hfa = true;
3032 if (field_qual_type.getTypePtr() !=
3033 base_qual_type.getTypePtr())
3034 return 0;
3035 }
3036 }
3037 } else if (field_qual_type->isVectorType() ||
3038 field_qual_type->isExtVectorType()) {
3039 if (num_fields == 0) {
3040 base_qual_type = field_qual_type;
3041 base_bitwidth = field_bitwidth;
3042 } else {
3043 if (is_hfa)
3044 return 0;
3045 is_hva = true;
3046 if (base_bitwidth != field_bitwidth)
3047 return 0;
3048 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3049 return 0;
3050 }
3051 } else
3052 return 0;
3053 ++num_fields;
3054 }
3055 if (base_type_ptr)
3056 *base_type_ptr =
3057 CompilerType(weak_from_this(), base_qual_type.getAsOpaquePtr());
3058 return num_fields;
3059 }
3060 }
3061 }
3062 break;
3063
3064 default:
3065 break;
3066 }
3067 return 0;
3068}
3069
3072 if (type) {
3073 clang::QualType qual_type(GetCanonicalQualType(type));
3074 const clang::FunctionProtoType *func =
3075 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3076 if (func)
3077 return func->getNumParams();
3078 }
3079 return 0;
3080}
3081
3084 const size_t index) {
3085 if (type) {
3086 clang::QualType qual_type(GetQualType(type));
3087 const clang::FunctionProtoType *func =
3088 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3089 if (func) {
3090 if (index < func->getNumParams())
3091 return CompilerType(weak_from_this(), func->getParamType(index).getAsOpaquePtr());
3092 }
3093 }
3094 return CompilerType();
3095}
3096
3099 llvm::function_ref<bool(clang::QualType)> predicate) const {
3100 if (type) {
3101 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3102
3103 if (predicate(qual_type))
3104 return true;
3105
3106 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3107 switch (type_class) {
3108 default:
3109 break;
3110
3111 case clang::Type::LValueReference:
3112 case clang::Type::RValueReference: {
3113 const clang::ReferenceType *reference_type =
3114 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3115 if (reference_type)
3116 return IsTypeImpl(reference_type->getPointeeType().getAsOpaquePtr(), predicate);
3117 } break;
3118 }
3119 }
3120 return false;
3121}
3122
3125 auto isMemberFunctionPointerType = [](clang::QualType qual_type) {
3126 return qual_type->isMemberFunctionPointerType();
3127 };
3128
3129 return IsTypeImpl(type, isMemberFunctionPointerType);
3130}
3131
3133 auto isFunctionPointerType = [](clang::QualType qual_type) {
3134 return qual_type->isFunctionPointerType();
3135 };
3136
3137 return IsTypeImpl(type, isFunctionPointerType);
3138}
3139
3142 CompilerType *function_pointer_type_ptr) {
3143 auto isBlockPointerType = [&](clang::QualType qual_type) {
3144 if (qual_type->isBlockPointerType()) {
3145 if (function_pointer_type_ptr) {
3146 const clang::BlockPointerType *block_pointer_type =
3147 qual_type->castAs<clang::BlockPointerType>();
3148 QualType pointee_type = block_pointer_type->getPointeeType();
3149 QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
3150 *function_pointer_type_ptr = CompilerType(
3151 weak_from_this(), function_pointer_type.getAsOpaquePtr());
3152 }
3153 return true;
3154 }
3155
3156 return false;
3157 };
3158
3159 return IsTypeImpl(type, isBlockPointerType);
3160}
3161
3163 bool &is_signed) {
3164 if (!type)
3165 return false;
3166
3167 clang::QualType qual_type(GetCanonicalQualType(type));
3168 const clang::BuiltinType *builtin_type =
3169 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3170
3171 if (builtin_type) {
3172 if (builtin_type->isInteger()) {
3173 is_signed = builtin_type->isSignedInteger();
3174 return true;
3175 }
3176 }
3177
3178 return false;
3179}
3180
3182 bool &is_signed) {
3183 if (type) {
3184 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3185 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3186
3187 if (enum_type) {
3188 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3189 is_signed);
3190 return true;
3191 }
3192 }
3193
3194 return false;
3195}
3196
3199 if (type) {
3200 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3201 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3202
3203 if (enum_type) {
3204 return enum_type->isScopedEnumeralType();
3205 }
3206 }
3207
3208 return false;
3209}
3210
3212 CompilerType *pointee_type) {
3213 if (type) {
3214 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3215 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3216 switch (type_class) {
3217 case clang::Type::Builtin:
3218 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3219 default:
3220 break;
3221 case clang::BuiltinType::ObjCId:
3222 case clang::BuiltinType::ObjCClass:
3223 return true;
3224 }
3225 return false;
3226 case clang::Type::ObjCObjectPointer:
3227 if (pointee_type)
3228 pointee_type->SetCompilerType(
3229 weak_from_this(),
3230 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3231 ->getPointeeType()
3232 .getAsOpaquePtr());
3233 return true;
3234 case clang::Type::BlockPointer:
3235 if (pointee_type)
3236 pointee_type->SetCompilerType(
3237 weak_from_this(), llvm::cast<clang::BlockPointerType>(qual_type)
3238 ->getPointeeType()
3239 .getAsOpaquePtr());
3240 return true;
3241 case clang::Type::Pointer:
3242 if (pointee_type)
3243 pointee_type->SetCompilerType(weak_from_this(),
3244 llvm::cast<clang::PointerType>(qual_type)
3245 ->getPointeeType()
3246 .getAsOpaquePtr());
3247 return true;
3248 case clang::Type::MemberPointer:
3249 if (pointee_type)
3250 pointee_type->SetCompilerType(
3251 weak_from_this(), llvm::cast<clang::MemberPointerType>(qual_type)
3252 ->getPointeeType()
3253 .getAsOpaquePtr());
3254 return true;
3255 default:
3256 break;
3257 }
3258 }
3259 if (pointee_type)
3260 pointee_type->Clear();
3261 return false;
3262}
3263
3265 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3266 if (type) {
3267 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3268 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3269 switch (type_class) {
3270 case clang::Type::Builtin:
3271 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3272 default:
3273 break;
3274 case clang::BuiltinType::ObjCId:
3275 case clang::BuiltinType::ObjCClass:
3276 return true;
3277 }
3278 return false;
3279 case clang::Type::ObjCObjectPointer:
3280 if (pointee_type)
3281 pointee_type->SetCompilerType(
3282 weak_from_this(),
3283 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3284 ->getPointeeType()
3285 .getAsOpaquePtr());
3286 return true;
3287 case clang::Type::BlockPointer:
3288 if (pointee_type)
3289 pointee_type->SetCompilerType(
3290 weak_from_this(), llvm::cast<clang::BlockPointerType>(qual_type)
3291 ->getPointeeType()
3292 .getAsOpaquePtr());
3293 return true;
3294 case clang::Type::Pointer:
3295 if (pointee_type)
3296 pointee_type->SetCompilerType(weak_from_this(),
3297 llvm::cast<clang::PointerType>(qual_type)
3298 ->getPointeeType()
3299 .getAsOpaquePtr());
3300 return true;
3301 case clang::Type::MemberPointer:
3302 if (pointee_type)
3303 pointee_type->SetCompilerType(
3304 weak_from_this(), llvm::cast<clang::MemberPointerType>(qual_type)
3305 ->getPointeeType()
3306 .getAsOpaquePtr());
3307 return true;
3308 case clang::Type::LValueReference:
3309 if (pointee_type)
3310 pointee_type->SetCompilerType(
3311 weak_from_this(), llvm::cast<clang::LValueReferenceType>(qual_type)
3312 ->desugar()
3313 .getAsOpaquePtr());
3314 return true;
3315 case clang::Type::RValueReference:
3316 if (pointee_type)
3317 pointee_type->SetCompilerType(
3318 weak_from_this(), llvm::cast<clang::RValueReferenceType>(qual_type)
3319 ->desugar()
3320 .getAsOpaquePtr());
3321 return true;
3322 default:
3323 break;
3324 }
3325 }
3326 if (pointee_type)
3327 pointee_type->Clear();
3328 return false;
3329}
3330
3332 CompilerType *pointee_type,
3333 bool *is_rvalue) {
3334 if (type) {
3335 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3336 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3337
3338 switch (type_class) {
3339 case clang::Type::LValueReference:
3340 if (pointee_type)
3341 pointee_type->SetCompilerType(
3342 weak_from_this(), llvm::cast<clang::LValueReferenceType>(qual_type)
3343 ->desugar()
3344 .getAsOpaquePtr());
3345 if (is_rvalue)
3346 *is_rvalue = false;
3347 return true;
3348 case clang::Type::RValueReference:
3349 if (pointee_type)
3350 pointee_type->SetCompilerType(
3351 weak_from_this(), llvm::cast<clang::RValueReferenceType>(qual_type)
3352 ->desugar()
3353 .getAsOpaquePtr());
3354 if (is_rvalue)
3355 *is_rvalue = true;
3356 return true;
3357
3358 default:
3359 break;
3360 }
3361 }
3362 if (pointee_type)
3363 pointee_type->Clear();
3364 return false;
3365}
3366
3368 uint32_t &count, bool &is_complex) {
3369 if (type) {
3370 clang::QualType qual_type(GetCanonicalQualType(type));
3371
3372 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3373 qual_type->getCanonicalTypeInternal())) {
3374 clang::BuiltinType::Kind kind = BT->getKind();
3375 if (kind >= clang::BuiltinType::Float &&
3376 kind <= clang::BuiltinType::LongDouble) {
3377 count = 1;
3378 is_complex = false;
3379 return true;
3380 }
3381 } else if (const clang::ComplexType *CT =
3382 llvm::dyn_cast<clang::ComplexType>(
3383 qual_type->getCanonicalTypeInternal())) {
3384 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3385 is_complex)) {
3386 count = 2;
3387 is_complex = true;
3388 return true;
3389 }
3390 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3391 qual_type->getCanonicalTypeInternal())) {
3392 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3393 is_complex)) {
3394 count = VT->getNumElements();
3395 is_complex = false;
3396 return true;
3397 }
3398 }
3399 }
3400 count = 0;
3401 is_complex = false;
3402 return false;
3403}
3404
3406 if (!type)
3407 return false;
3408
3409 clang::QualType qual_type(GetQualType(type));
3410 const clang::TagType *tag_type =
3411 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3412 if (tag_type) {
3413 clang::TagDecl *tag_decl = tag_type->getDecl();
3414 if (tag_decl)
3415 return tag_decl->isCompleteDefinition();
3416 return false;
3417 } else {
3418 const clang::ObjCObjectType *objc_class_type =
3419 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3420 if (objc_class_type) {
3421 clang::ObjCInterfaceDecl *class_interface_decl =
3422 objc_class_type->getInterface();
3423 if (class_interface_decl)
3424 return class_interface_decl->getDefinition() != nullptr;
3425 return false;
3426 }
3427 }
3428 return true;
3429}
3430
3432 if (ClangUtil::IsClangType(type)) {
3433 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3434
3435 const clang::ObjCObjectPointerType *obj_pointer_type =
3436 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3437
3438 if (obj_pointer_type)
3439 return obj_pointer_type->isObjCClassType();
3440 }
3441 return false;
3442}
3443
3445 if (ClangUtil::IsClangType(type))
3446 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3447 return false;
3448}
3449
3451 if (!type)
3452 return false;
3453 clang::QualType qual_type(GetCanonicalQualType(type));
3454 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3455 return (type_class == clang::Type::Record);
3456}
3457
3459 if (!type)
3460 return false;
3461 clang::QualType qual_type(GetCanonicalQualType(type));
3462 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3463 return (type_class == clang::Type::Enum);
3464}
3465
3467 if (type) {
3468 clang::QualType qual_type(GetCanonicalQualType(type));
3469 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3470 switch (type_class) {
3471 case clang::Type::Record:
3472 if (GetCompleteType(type)) {
3473 const clang::RecordType *record_type =
3474 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3475 const clang::RecordDecl *record_decl = record_type->getDecl();
3476 if (record_decl) {
3477 const clang::CXXRecordDecl *cxx_record_decl =
3478 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3479 if (cxx_record_decl) {
3480 // We can't just call is isPolymorphic() here because that just
3481 // means the current class has virtual functions, it doesn't check
3482 // if any inherited classes have virtual functions. The doc string
3483 // in SBType::IsPolymorphicClass() says it is looking for both
3484 // if the class has virtual methods or if any bases do, so this
3485 // should be more correct.
3486 return cxx_record_decl->isDynamicClass();
3487 }
3488 }
3489 }
3490 break;
3491
3492 default:
3493 break;
3494 }
3495 }
3496 return false;
3497}
3498
3500 CompilerType *dynamic_pointee_type,
3501 bool check_cplusplus,
3502 bool check_objc) {
3503 clang::QualType pointee_qual_type;
3504 if (type) {
3505 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3506 bool success = false;
3507 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3508 switch (type_class) {
3509 case clang::Type::Builtin:
3510 if (check_objc &&
3511 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3512 clang::BuiltinType::ObjCId) {
3513 if (dynamic_pointee_type)
3514 dynamic_pointee_type->SetCompilerType(weak_from_this(), type);
3515 return true;
3516 }
3517 break;
3518
3519 case clang::Type::ObjCObjectPointer:
3520 if (check_objc) {
3521 if (const auto *objc_pointee_type =
3522 qual_type->getPointeeType().getTypePtrOrNull()) {
3523 if (const auto *objc_object_type =
3524 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3525 objc_pointee_type)) {
3526 if (objc_object_type->isObjCClass())
3527 return false;
3528 }
3529 }
3530 if (dynamic_pointee_type)
3531 dynamic_pointee_type->SetCompilerType(
3532 weak_from_this(),
3533 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3534 ->getPointeeType()
3535 .getAsOpaquePtr());
3536 return true;
3537 }
3538 break;
3539
3540 case clang::Type::Pointer:
3541 pointee_qual_type =
3542 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3543 success = true;
3544 break;
3545
3546 case clang::Type::LValueReference:
3547 case clang::Type::RValueReference:
3548 pointee_qual_type =
3549 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3550 success = true;
3551 break;
3552
3553 default:
3554 break;
3555 }
3556
3557 if (success) {
3558 // Check to make sure what we are pointing too is a possible dynamic C++
3559 // type We currently accept any "void *" (in case we have a class that
3560 // has been watered down to an opaque pointer) and virtual C++ classes.
3561 const clang::Type::TypeClass pointee_type_class =
3562 pointee_qual_type.getCanonicalType()->getTypeClass();
3563 switch (pointee_type_class) {
3564 case clang::Type::Builtin:
3565 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3566 case clang::BuiltinType::UnknownAny:
3567 case clang::BuiltinType::Void:
3568 if (dynamic_pointee_type)
3569 dynamic_pointee_type->SetCompilerType(
3570 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3571 return true;
3572 default:
3573 break;
3574 }
3575 break;
3576
3577 case clang::Type::Record:
3578 if (check_cplusplus) {
3579 clang::CXXRecordDecl *cxx_record_decl =
3580 pointee_qual_type->getAsCXXRecordDecl();
3581 if (cxx_record_decl) {
3582 bool is_complete = cxx_record_decl->isCompleteDefinition();
3583
3584 if (is_complete)
3585 success = cxx_record_decl->isDynamicClass();
3586 else {
3587 ClangASTMetadata *metadata = GetMetadata(cxx_record_decl);
3588 if (metadata)
3589 success = metadata->GetIsDynamicCXXType();
3590 else {
3591 is_complete = GetType(pointee_qual_type).GetCompleteType();
3592 if (is_complete)
3593 success = cxx_record_decl->isDynamicClass();
3594 else
3595 success = false;
3596 }
3597 }
3598
3599 if (success) {
3600 if (dynamic_pointee_type)
3601 dynamic_pointee_type->SetCompilerType(
3602 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3603 return true;
3604 }
3605 }
3606 }
3607 break;
3608
3609 case clang::Type::ObjCObject:
3610 case clang::Type::ObjCInterface:
3611 if (check_objc) {
3612 if (dynamic_pointee_type)
3613 dynamic_pointee_type->SetCompilerType(
3614 weak_from_this(), pointee_qual_type.getAsOpaquePtr());
3615 return true;
3616 }
3617 break;
3618
3619 default:
3620 break;
3621 }
3622 }
3623 }
3624 if (dynamic_pointee_type)
3625 dynamic_pointee_type->Clear();
3626 return false;
3627}
3628
3630 if (!type)
3631 return false;
3632
3633 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3634}
3635
3637 if (!type)
3638 return false;
3639 return RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef})
3640 ->getTypeClass() == clang::Type::Typedef;
3641}
3642
3644 if (!type)
3645 return false;
3646 return GetCanonicalQualType(type)->isVoidType();
3647}
3648
3650 if (auto *record_decl =
3652 return record_decl->canPassInRegisters();
3653 }
3654 return false;
3655}
3656
3658 return TypeSystemClangSupportsLanguage(language);
3659}
3660
3661std::optional<std::string>
3663 if (!type)
3664 return std::nullopt;
3665
3666 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3667 if (qual_type.isNull())
3668 return std::nullopt;
3669
3670 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3671 if (!cxx_record_decl)
3672 return std::nullopt;
3673
3674 return std::string(cxx_record_decl->getIdentifier()->getNameStart());
3675}
3676
3678 if (!type)
3679 return false;
3680
3681 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3682 return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
3683}
3684
3686 if (!type)
3687 return false;
3688 clang::QualType qual_type(GetCanonicalQualType(type));
3689 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3690 if (tag_type)
3691 return tag_type->isBeingDefined();
3692 return false;
3693}
3694
3696 CompilerType *class_type_ptr) {
3697 if (!ClangUtil::IsClangType(type))
3698 return false;
3699
3700 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3701
3702 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3703 if (class_type_ptr) {
3704 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3705 const clang::ObjCObjectPointerType *obj_pointer_type =
3706 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3707 if (obj_pointer_type == nullptr)
3708 class_type_ptr->Clear();
3709 else
3710 class_type_ptr->SetCompilerType(
3711 type.GetTypeSystem(),
3712 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3713 .getAsOpaquePtr());
3714 }
3715 }
3716 return true;
3717 }
3718 if (class_type_ptr)
3719 class_type_ptr->Clear();
3720 return false;
3721}
3722
3723// Type Completion
3724
3726 if (!type)
3727 return false;
3728 const bool allow_completion = true;
3730 allow_completion);
3731}
3732
3734 bool base_only) {
3735 if (!type)
3736 return ConstString();
3737
3738 clang::QualType qual_type(GetQualType(type));
3739
3740 // Remove certain type sugar from the name. Sugar such as elaborated types
3741 // or template types which only serve to improve diagnostics shouldn't
3742 // act as their own types from the user's perspective (e.g., formatter
3743 // shouldn't format a variable differently depending on how the ser has
3744 // specified the type. '::Type' and 'Type' should behave the same).
3745 // Typedefs and atomic derived types are not removed as they are actually
3746 // useful for identifiying specific types.
3747 qual_type = RemoveWrappingTypes(qual_type,
3748 {clang::Type::Typedef, clang::Type::Atomic});
3749
3750 // For a typedef just return the qualified name.
3751 if (const auto *typedef_type = qual_type->getAs<clang::TypedefType>()) {
3752 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3753 return ConstString(GetTypeNameForDecl(typedef_decl));
3754 }
3755
3756 // For consistency, this follows the same code path that clang uses to emit
3757 // debug info. This also handles when we don't want any scopes preceding the
3758 // name.
3759 if (auto *named_decl = qual_type->getAsTagDecl())
3760 return ConstString(GetTypeNameForDecl(named_decl, !base_only));
3761
3762 return ConstString(qual_type.getAsString(GetTypePrintingPolicy()));
3763}
3764
3767 if (!type)
3768 return ConstString();
3769
3770 clang::QualType qual_type(GetQualType(type));
3771 clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
3772 printing_policy.SuppressTagKeyword = true;
3773 printing_policy.SuppressScope = false;
3774 printing_policy.SuppressUnwrittenScope = true;
3775 printing_policy.SuppressInlineNamespace = true;
3776 return ConstString(qual_type.getAsString(printing_policy));
3777}
3778
3779uint32_t
3781 CompilerType *pointee_or_element_clang_type) {
3782 if (!type)
3783 return 0;
3784
3785 if (pointee_or_element_clang_type)
3786 pointee_or_element_clang_type->Clear();
3787
3788 clang::QualType qual_type =
3789 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
3790
3791 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3792 switch (type_class) {
3793 case clang::Type::Attributed:
3794 return GetTypeInfo(qual_type->castAs<clang::AttributedType>()
3795 ->getModifiedType()
3796 .getAsOpaquePtr(),
3797 pointee_or_element_clang_type);
3798 case clang::Type::Builtin: {
3799 const clang::BuiltinType *builtin_type =
3800 llvm::cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3801
3802 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3803 switch (builtin_type->getKind()) {
3804 case clang::BuiltinType::ObjCId:
3805 case clang::BuiltinType::ObjCClass:
3806 if (pointee_or_element_clang_type)
3807 pointee_or_element_clang_type->SetCompilerType(
3808 weak_from_this(),
3809 getASTContext().ObjCBuiltinClassTy.getAsOpaquePtr());
3810 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3811 break;
3812
3813 case clang::BuiltinType::ObjCSel:
3814 if (pointee_or_element_clang_type)
3815 pointee_or_element_clang_type->SetCompilerType(
3816 weak_from_this(), getASTContext().CharTy.getAsOpaquePtr());
3817 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3818 break;
3819
3820 case clang::BuiltinType::Bool:
3821 case clang::BuiltinType::Char_U:
3822 case clang::BuiltinType::UChar:
3823 case clang::BuiltinType::WChar_U:
3824 case clang::BuiltinType::Char16:
3825 case clang::BuiltinType::Char32:
3826 case clang::BuiltinType::UShort:
3827 case clang::BuiltinType::UInt:
3828 case clang::BuiltinType::ULong:
3829 case clang::BuiltinType::ULongLong:
3830 case clang::BuiltinType::UInt128:
3831 case clang::BuiltinType::Char_S:
3832 case clang::BuiltinType::SChar:
3833 case clang::BuiltinType::WChar_S:
3834 case clang::BuiltinType::Short:
3835 case clang::BuiltinType::Int:
3836 case clang::BuiltinType::Long:
3837 case clang::BuiltinType::LongLong:
3838 case clang::BuiltinType::Int128:
3839 case clang::BuiltinType::Float:
3840 case clang::BuiltinType::Double:
3841 case clang::BuiltinType::LongDouble:
3842 builtin_type_flags |= eTypeIsScalar;
3843 if (builtin_type->isInteger()) {
3844 builtin_type_flags |= eTypeIsInteger;
3845 if (builtin_type->isSignedInteger())
3846 builtin_type_flags |= eTypeIsSigned;
3847 } else if (builtin_type->isFloatingPoint())
3848 builtin_type_flags |= eTypeIsFloat;
3849 break;
3850 default:
3851 break;
3852 }
3853 return builtin_type_flags;
3854 }
3855
3856 case clang::Type::BlockPointer:
3857 if (pointee_or_element_clang_type)
3858 pointee_or_element_clang_type->SetCompilerType(
3859 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3860 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3861
3862 case clang::Type::Complex: {
3863 uint32_t complex_type_flags =
3864 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3865 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3866 qual_type->getCanonicalTypeInternal());
3867 if (complex_type) {
3868 clang::QualType complex_element_type(complex_type->getElementType());
3869 if (complex_element_type->isIntegerType())
3870 complex_type_flags |= eTypeIsFloat;
3871 else if (complex_element_type->isFloatingType())
3872 complex_type_flags |= eTypeIsInteger;
3873 }
3874 return complex_type_flags;
3875 } break;
3876
3877 case clang::Type::ConstantArray:
3878 case clang::Type::DependentSizedArray:
3879 case clang::Type::IncompleteArray:
3880 case clang::Type::VariableArray:
3881 if (pointee_or_element_clang_type)
3882 pointee_or_element_clang_type->SetCompilerType(
3883 weak_from_this(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3884 ->getElementType()
3885 .getAsOpaquePtr());
3886 return eTypeHasChildren | eTypeIsArray;
3887
3888 case clang::Type::DependentName:
3889 return 0;
3890 case clang::Type::DependentSizedExtVector:
3891 return eTypeHasChildren | eTypeIsVector;
3892 case clang::Type::DependentTemplateSpecialization:
3893 return eTypeIsTemplate;
3894
3895 case clang::Type::Enum:
3896 if (pointee_or_element_clang_type)
3897 pointee_or_element_clang_type->SetCompilerType(
3898 weak_from_this(), llvm::cast<clang::EnumType>(qual_type)
3899 ->getDecl()
3900 ->getIntegerType()
3901 .getAsOpaquePtr());
3902 return eTypeIsEnumeration | eTypeHasValue;
3903
3904 case clang::Type::FunctionProto:
3905 return eTypeIsFuncPrototype | eTypeHasValue;
3906 case clang::Type::FunctionNoProto:
3907 return eTypeIsFuncPrototype | eTypeHasValue;
3908 case clang::Type::InjectedClassName:
3909 return 0;
3910
3911 case clang::Type::LValueReference:
3912 case clang::Type::RValueReference:
3913 if (pointee_or_element_clang_type)
3914 pointee_or_element_clang_type->SetCompilerType(
3915 weak_from_this(),
3916 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3917 ->getPointeeType()
3918 .getAsOpaquePtr());
3919 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3920
3921 case clang::Type::MemberPointer:
3922 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3923
3924 case clang::Type::ObjCObjectPointer:
3925 if (pointee_or_element_clang_type)
3926 pointee_or_element_clang_type->SetCompilerType(
3927 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3928 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3929 eTypeHasValue;
3930
3931 case clang::Type::ObjCObject:
3932 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3933 case clang::Type::ObjCInterface:
3934 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3935
3936 case clang::Type::Pointer:
3937 if (pointee_or_element_clang_type)
3938 pointee_or_element_clang_type->SetCompilerType(
3939 weak_from_this(), qual_type->getPointeeType().getAsOpaquePtr());
3940 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3941
3942 case clang::Type::Record:
3943 if (qual_type->getAsCXXRecordDecl())
3944 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3945 else
3946 return eTypeHasChildren | eTypeIsStructUnion;
3947 break;
3948 case clang::Type::SubstTemplateTypeParm:
3949 return eTypeIsTemplate;
3950 case clang::Type::TemplateTypeParm:
3951 return eTypeIsTemplate;
3952 case clang::Type::TemplateSpecialization:
3953 return eTypeIsTemplate;
3954
3955 case clang::Type::Typedef:
3956 return eTypeIsTypedef | GetType(llvm::cast<clang::TypedefType>(qual_type)
3957 ->getDecl()
3958 ->getUnderlyingType())
3959 .GetTypeInfo(pointee_or_element_clang_type);
3960 case clang::Type::UnresolvedUsing:
3961 return 0;
3962
3963 case clang::Type::ExtVector:
3964 case clang::Type::Vector: {
3965 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3966 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
3967 qual_type->getCanonicalTypeInternal());
3968 if (vector_type) {
3969 if (vector_type->isIntegerType())
3970 vector_type_flags |= eTypeIsFloat;
3971 else if (vector_type->isFloatingType())
3972 vector_type_flags |= eTypeIsInteger;
3973 }
3974 return vector_type_flags;
3975 }
3976 default:
3977 return 0;
3978 }
3979 return 0;
3980}
3981
3984 if (!type)
3985 return lldb::eLanguageTypeC;
3986
3987 // If the type is a reference, then resolve it to what it refers to first:
3988 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
3989 if (qual_type->isAnyPointerType()) {
3990 if (qual_type->isObjCObjectPointerType())
3992 if (qual_type->getPointeeCXXRecordDecl())
3994
3995 clang::QualType pointee_type(qual_type->getPointeeType());
3996 if (pointee_type->getPointeeCXXRecordDecl())
3998 if (pointee_type->isObjCObjectOrInterfaceType())
4000 if (pointee_type->isObjCClassType())
4002 if (pointee_type.getTypePtr() ==
4003 getASTContext().ObjCBuiltinIdTy.getTypePtr())
4005 } else {
4006 if (qual_type->isObjCObjectOrInterfaceType())
4008 if (qual_type->getAsCXXRecordDecl())
4010 switch (qual_type->getTypeClass()) {
4011 default:
4012 break;
4013 case clang::Type::Builtin:
4014 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4015 default:
4016 case clang::BuiltinType::Void:
4017 case clang::BuiltinType::Bool:
4018 case clang::BuiltinType::Char_U:
4019 case clang::BuiltinType::UChar:
4020 case clang::BuiltinType::WChar_U:
4021 case clang::BuiltinType::Char16:
4022 case clang::BuiltinType::Char32:
4023 case clang::BuiltinType::UShort:
4024 case clang::BuiltinType::UInt:
4025 case clang::BuiltinType::ULong:
4026 case clang::BuiltinType::ULongLong:
4027 case clang::BuiltinType::UInt128:
4028 case clang::BuiltinType::Char_S:
4029 case clang::BuiltinType::SChar:
4030 case clang::BuiltinType::WChar_S:
4031 case clang::BuiltinType::Short:
4032 case clang::BuiltinType::Int:
4033 case clang::BuiltinType::Long:
4034 case clang::BuiltinType::LongLong:
4035 case clang::BuiltinType::Int128:
4036 case clang::BuiltinType::Float:
4037 case clang::BuiltinType::Double:
4038 case clang::BuiltinType::LongDouble:
4039 break;
4040
4041 case clang::BuiltinType::NullPtr:
4043
4044 case clang::BuiltinType::ObjCId:
4045 case clang::BuiltinType::ObjCClass:
4046 case clang::BuiltinType::ObjCSel:
4047 return eLanguageTypeObjC;
4048
4049 case clang::BuiltinType::Dependent:
4050 case clang::BuiltinType::Overload:
4051 case clang::BuiltinType::BoundMember:
4052 case clang::BuiltinType::UnknownAny:
4053 break;
4054 }
4055 break;
4056 case clang::Type::Typedef:
4057 return GetType(llvm::cast<clang::TypedefType>(qual_type)
4058 ->getDecl()
4059 ->getUnderlyingType())
4061 }
4062 }
4063 return lldb::eLanguageTypeC;
4064}
4065
4066lldb::TypeClass
4068 if (!type)
4069 return lldb::eTypeClassInvalid;
4070
4071 clang::QualType qual_type =
4072 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
4073
4074 switch (qual_type->getTypeClass()) {
4075 case clang::Type::Atomic:
4076 case clang::Type::Auto:
4077 case clang::Type::Decltype:
4078 case clang::Type::Elaborated:
4079 case clang::Type::Paren:
4080 case clang::Type::TypeOf:
4081 case clang::Type::TypeOfExpr:
4082 case clang::Type::Using:
4083 llvm_unreachable("Handled in RemoveWrappingTypes!");
4084 case clang::Type::UnaryTransform:
4085 break;
4086 case clang::Type::FunctionNoProto:
4087 return lldb::eTypeClassFunction;
4088 case clang::Type::FunctionProto:
4089 return lldb::eTypeClassFunction;
4090 case clang::Type::IncompleteArray:
4091 return lldb::eTypeClassArray;
4092 case clang::Type::VariableArray:
4093 return lldb::eTypeClassArray;
4094 case clang::Type::ConstantArray:
4095 return lldb::eTypeClassArray;
4096 case clang::Type::DependentSizedArray:
4097 return lldb::eTypeClassArray;
4098 case clang::Type::DependentSizedExtVector:
4099 return lldb::eTypeClassVector;
4100 case clang::Type::DependentVector:
4101 return lldb::eTypeClassVector;
4102 case clang::Type::ExtVector:
4103 return lldb::eTypeClassVector;
4104 case clang::Type::Vector:
4105 return lldb::eTypeClassVector;
4106 case clang::Type::Builtin:
4107 // Ext-Int is just an integer type.
4108 case clang::Type::BitInt:
4109 case clang::Type::DependentBitInt:
4110 return lldb::eTypeClassBuiltin;
4111 case clang::Type::ObjCObjectPointer:
4112 return lldb::eTypeClassObjCObjectPointer;
4113 case clang::Type::BlockPointer:
4114 return lldb::eTypeClassBlockPointer;
4115 case clang::Type::Pointer:
4116 return lldb::eTypeClassPointer;
4117 case clang::Type::LValueReference:
4118 return lldb::eTypeClassReference;
4119 case clang::Type::RValueReference:
4120 return lldb::eTypeClassReference;
4121 case clang::Type::MemberPointer:
4122 return lldb::eTypeClassMemberPointer;
4123 case clang::Type::Complex:
4124 if (qual_type->isComplexType())
4125 return lldb::eTypeClassComplexFloat;
4126 else
4127 return lldb::eTypeClassComplexInteger;
4128 case clang::Type::ObjCObject:
4129 return lldb::eTypeClassObjCObject;
4130 case clang::Type::ObjCInterface:
4131 return lldb::eTypeClassObjCInterface;
4132 case clang::Type::Record: {
4133 const clang::RecordType *record_type =
4134 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4135 const clang::RecordDecl *record_decl = record_type->getDecl();
4136 if (record_decl->isUnion())
4137 return lldb::eTypeClassUnion;
4138 else if (record_decl->isStruct())
4139 return lldb::eTypeClassStruct;
4140 else
4141 return lldb::eTypeClassClass;
4142 } break;
4143 case clang::Type::Enum:
4144 return lldb::eTypeClassEnumeration;
4145 case clang::Type::Typedef:
4146 return lldb::eTypeClassTypedef;
4147 case clang::Type::UnresolvedUsing:
4148 break;
4149
4150 case clang::Type::Attributed:
4151 case clang::Type::BTFTagAttributed:
4152 break;
4153 case clang::Type::TemplateTypeParm:
4154 break;
4155 case clang::Type::SubstTemplateTypeParm:
4156 break;
4157 case clang::Type::SubstTemplateTypeParmPack:
4158 break;
4159 case clang::Type::InjectedClassName:
4160 break;
4161 case clang::Type::DependentName:
4162 break;
4163 case clang::Type::DependentTemplateSpecialization:
4164 break;
4165 case clang::Type::PackExpansion:
4166 break;
4167
4168 case clang::Type::TemplateSpecialization:
4169 break;
4170 case clang::Type::DeducedTemplateSpecialization:
4171 break;
4172 case clang::Type::Pipe:
4173 break;
4174
4175 // pointer type decayed from an array or function type.
4176 case clang::Type::Decayed:
4177 break;
4178 case clang::Type::Adjusted:
4179 break;
4180 case clang::Type::ObjCTypeParam:
4181 break;
4182
4183 case clang::Type::DependentAddressSpace:
4184 break;
4185 case clang::Type::MacroQualified:
4186 break;
4187
4188 // Matrix types that we're not sure how to display at the moment.
4189 case clang::Type::ConstantMatrix:
4190 case clang::Type::DependentSizedMatrix:
4191 break;
4192
4193 // We don't handle pack indexing yet
4194 case clang::Type::PackIndexing:
4195 break;
4196 }
4197 // We don't know hot to display this type...
4198 return lldb::eTypeClassOther;
4199}
4200
4202 if (type)
4203 return GetQualType(type).getQualifiers().getCVRQualifiers();
4204 return 0;
4205}
4206
4207// Creating related types
4208
4211 ExecutionContextScope *exe_scope) {
4212 if (type) {
4213 clang::QualType qual_type(GetQualType(type));
4214
4215 const clang::Type *array_eletype =
4216 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4217
4218 if (!array_eletype)
4219 return CompilerType();
4220
4221 return GetType(clang::QualType(array_eletype, 0));
4222 }
4223 return CompilerType();
4224}
4225
4227 uint64_t size) {
4228 if (type) {
4229 clang::QualType qual_type(GetCanonicalQualType(type));
4230 clang::ASTContext &ast_ctx = getASTContext();
4231 if (size != 0)
4232 return GetType(ast_ctx.getConstantArrayType(
4233 qual_type, llvm::APInt(64, size), nullptr,
4234 clang::ArraySizeModifier::Normal, 0));
4235 else
4236 return GetType(ast_ctx.getIncompleteArrayType(
4237 qual_type, clang::ArraySizeModifier::Normal, 0));
4238 }
4239
4240 return CompilerType();
4241}
4242
4245 if (type)
4246 return GetType(GetCanonicalQualType(type));
4247 return CompilerType();
4248}
4249
4250static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4251 clang::QualType qual_type) {
4252 if (qual_type->isPointerType())
4253 qual_type = ast->getPointerType(
4254 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4255 else if (const ConstantArrayType *arr =
4256 ast->getAsConstantArrayType(qual_type)) {
4257 qual_type = ast->getConstantArrayType(
4258 GetFullyUnqualifiedType_Impl(ast, arr->getElementType()),
4259 arr->getSize(), arr->getSizeExpr(), arr->getSizeModifier(),
4260 arr->getIndexTypeQualifiers().getAsOpaqueValue());
4261 } else
4262 qual_type = qual_type.getUnqualifiedType();
4263 qual_type.removeLocalConst();
4264 qual_type.removeLocalRestrict();
4265 qual_type.removeLocalVolatile();
4266 return qual_type;
4267}
4268
4271 if (type)
4272 return GetType(
4274 return CompilerType();
4275}
4276
4279 if (type)
4281 return CompilerType();
4282}
4283
4286 if (type) {
4287 const clang::FunctionProtoType *func =
4288 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4289 if (func)
4290 return func->getNumParams();
4291 }
4292 return -1;
4293}
4294
4296 lldb::opaque_compiler_type_t type, size_t idx) {
4297 if (type) {
4298 const clang::FunctionProtoType *func =
4299 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4300 if (func) {
4301 const uint32_t num_args = func->getNumParams();
4302 if (idx < num_args)
4303 return GetType(func->getParamType(idx));
4304 }
4305 }
4306 return CompilerType();
4307}
4308
4311 if (type) {
4312 clang::QualType qual_type(GetQualType(type));
4313 const clang::FunctionProtoType *func =
4314 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4315 if (func)
4316 return GetType(func->getReturnType());
4317 }
4318 return CompilerType();
4319}
4320
4321size_t
4323 size_t num_functions = 0;
4324 if (type) {
4325 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4326 switch (qual_type->getTypeClass()) {
4327 case clang::Type::Record:
4328 if (GetCompleteQualType(&getASTContext(), qual_type)) {
4329 const clang::RecordType *record_type =
4330 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4331 const clang::RecordDecl *record_decl = record_type->getDecl();
4332 assert(record_decl);
4333 const clang::CXXRecordDecl *cxx_record_decl =
4334 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4335 if (cxx_record_decl)
4336 num_functions = std::distance(cxx_record_decl->method_begin(),
4337 cxx_record_decl->method_end());
4338 }
4339 break;
4340
4341 case clang::Type::ObjCObjectPointer: {
4342 const clang::ObjCObjectPointerType *objc_class_type =
4343 qual_type->castAs<clang::ObjCObjectPointerType>();
4344 const clang::ObjCInterfaceType *objc_interface_type =
4345 objc_class_type->getInterfaceType();
4346 if (objc_interface_type &&
4348 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4349 clang::ObjCInterfaceDecl *class_interface_decl =
4350 objc_interface_type->getDecl();
4351 if (class_interface_decl) {
4352 num_functions = std::distance(class_interface_decl->meth_begin(),
4353 class_interface_decl->meth_end());
4354 }
4355 }
4356 break;
4357 }
4358
4359 case clang::Type::ObjCObject:
4360 case clang::Type::ObjCInterface:
4361 if (GetCompleteType(type)) {
4362 const clang::ObjCObjectType *objc_class_type =
4363 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4364 if (objc_class_type) {
4365 clang::ObjCInterfaceDecl *class_interface_decl =
4366 objc_class_type->getInterface();
4367 if (class_interface_decl)
4368 num_functions = std::distance(class_interface_decl->meth_begin(),
4369 class_interface_decl->meth_end());
4370 }
4371 }
4372 break;
4373
4374 default:
4375 break;
4376 }
4377 }
4378 return num_functions;
4379}
4380
4383 size_t idx) {
4384 std::string name;
4385 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4386 CompilerType clang_type;
4387 CompilerDecl clang_decl;
4388 if (type) {
4389 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4390 switch (qual_type->getTypeClass()) {
4391 case clang::Type::Record:
4392 if (GetCompleteQualType(&getASTContext(), qual_type)) {
4393 const clang::RecordType *record_type =
4394 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4395 const clang::RecordDecl *record_decl = record_type->getDecl();
4396 assert(record_decl);
4397 const clang::CXXRecordDecl *cxx_record_decl =
4398 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4399 if (cxx_record_decl) {
4400 auto method_iter = cxx_record_decl->method_begin();
4401 auto method_end = cxx_record_decl->method_end();
4402 if (idx <
4403 static_cast<size_t>(std::distance(method_iter, method_end))) {
4404 std::advance(method_iter, idx);
4405 clang::CXXMethodDecl *cxx_method_decl =
4406 method_iter->getCanonicalDecl();
4407 if (cxx_method_decl) {
4408 name = cxx_method_decl->getDeclName().getAsString();
4409 if (cxx_method_decl->isStatic())
4411 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4413 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4415 else
4417 clang_type = GetType(cxx_method_decl->getType());
4418 clang_decl = GetCompilerDecl(cxx_method_decl);
4419 }
4420 }
4421 }
4422 }
4423 break;
4424
4425 case clang::Type::ObjCObjectPointer: {
4426 const clang::ObjCObjectPointerType *objc_class_type =
4427 qual_type->castAs<clang::ObjCObjectPointerType>();
4428 const clang::ObjCInterfaceType *objc_interface_type =
4429 objc_class_type->getInterfaceType();
4430 if (objc_interface_type &&
4432 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4433 clang::ObjCInterfaceDecl *class_interface_decl =
4434 objc_interface_type->getDecl();
4435 if (class_interface_decl) {
4436 auto method_iter = class_interface_decl->meth_begin();
4437 auto method_end = class_interface_decl->meth_end();
4438 if (idx <
4439 static_cast<size_t>(std::distance(method_iter, method_end))) {
4440 std::advance(method_iter, idx);
4441 clang::ObjCMethodDecl *objc_method_decl =
4442 method_iter->getCanonicalDecl();
4443 if (objc_method_decl) {
4444 clang_decl = GetCompilerDecl(objc_method_decl);
4445 name = objc_method_decl->getSelector().getAsString();
4446 if (objc_method_decl->isClassMethod())
4448 else
4450 }
4451 }
4452 }
4453 }
4454 break;
4455 }
4456
4457 case clang::Type::ObjCObject:
4458 case clang::Type::ObjCInterface:
4459 if (GetCompleteType(type)) {
4460 const clang::ObjCObjectType *objc_class_type =
4461 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4462 if (objc_class_type) {
4463 clang::ObjCInterfaceDecl *class_interface_decl =
4464 objc_class_type->getInterface();
4465 if (class_interface_decl) {
4466 auto method_iter = class_interface_decl->meth_begin();
4467 auto method_end = class_interface_decl->meth_end();
4468 if (idx <
4469 static_cast<size_t>(std::distance(method_iter, method_end))) {
4470 std::advance(method_iter, idx);
4471 clang::ObjCMethodDecl *objc_method_decl =
4472 method_iter->getCanonicalDecl();
4473 if (objc_method_decl) {
4474 clang_decl = GetCompilerDecl(objc_method_decl);
4475 name = objc_method_decl->getSelector().getAsString();
4476 if (objc_method_decl->isClassMethod())
4478 else
4480 }
4481 }
4482 }
4483 }
4484 }
4485 break;
4486
4487 default:
4488 break;
4489 }
4490 }
4491
4492 if (kind == eMemberFunctionKindUnknown)
4493 return TypeMemberFunctionImpl();
4494 else
4495 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4496}
4497
4500 if (type)
4501 return GetType(GetQualType(type).getNonReferenceType());
4502 return CompilerType();
4503}
4504
4507 if (type) {
4508 clang::QualType qual_type(GetQualType(type));
4509 return GetType(qual_type.getTypePtr()->getPointeeType());
4510 }
4511 return CompilerType();
4512}
4513
4516 if (type) {
4517 clang::QualType qual_type(GetQualType(type));
4518
4519 switch (qual_type.getDesugaredType(getASTContext())->getTypeClass()) {
4520 case clang::Type::ObjCObject:
4521 case clang::Type::ObjCInterface:
4522 return GetType(getASTContext().getObjCObjectPointerType(qual_type));
4523
4524 default:
4525 return GetType(getASTContext().getPointerType(qual_type));
4526 }
4527 }
4528 return CompilerType();
4529}
4530
4533 if (type)
4534 return GetType(getASTContext().getLValueReferenceType(GetQualType(type)));
4535 else
4536 return CompilerType();
4537}
4538
4541 if (type)
4542 return GetType(getASTContext().getRValueReferenceType(GetQualType(type)));
4543 else
4544 return CompilerType();
4545}
4546
4548 if (!type)
4549 return CompilerType();
4550 return GetType(getASTContext().getAtomicType(GetQualType(type)));
4551}
4552
4555 if (type) {
4556 clang::QualType result(GetQualType(type));
4557 result.addConst();
4558 return GetType(result);
4559 }
4560 return CompilerType();
4561}
4562
4565 if (type) {
4566 clang::QualType result(GetQualType(type));
4567 result.addVolatile();
4568 return GetType(result);
4569 }
4570 return CompilerType();
4571}
4572
4575 if (type) {
4576 clang::QualType result(GetQualType(type));
4577 result.addRestrict();
4578 return GetType(result);
4579 }
4580 return CompilerType();
4581}
4582
4584 lldb::opaque_compiler_type_t type, const char *typedef_name,
4585 const CompilerDeclContext &compiler_decl_ctx, uint32_t payload) {
4586 if (type && typedef_name && typedef_name[0]) {
4587 clang::ASTContext &clang_ast = getASTContext();
4588 clang::QualType qual_type(GetQualType(type));
4589
4590 clang::DeclContext *decl_ctx =
4592 if (!decl_ctx)
4593 decl_ctx = getASTContext().getTranslationUnitDecl();
4594
4595 clang::TypedefDecl *decl =
4596 clang::TypedefDecl::CreateDeserialized(clang_ast, 0);
4597 decl->setDeclContext(decl_ctx);
4598 decl->setDeclName(&clang_ast.Idents.get(typedef_name));
4599 decl->setTypeSourceInfo(clang_ast.getTrivialTypeSourceInfo(qual_type));
4600 decl_ctx->addDecl(decl);
4601 SetOwningModule(decl, TypePayloadClang(payload).GetOwningModule());
4602
4603 clang::TagDecl *tdecl = nullptr;
4604 if (!qual_type.isNull()) {
4605 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4606 tdecl = rt->getDecl();
4607 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4608 tdecl = et->getDecl();
4609 }
4610
4611 // Check whether this declaration is an anonymous struct, union, or enum,
4612 // hidden behind a typedef. If so, we try to check whether we have a
4613 // typedef tag to attach to the original record declaration
4614 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4615 tdecl->setTypedefNameForAnonDecl(decl);
4616
4617 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4618
4619 // Get a uniqued clang::QualType for the typedef decl type
4620 return GetType(clang_ast.getTypedefType(decl));
4621 }
4622 return CompilerType();
4623}
4624
4627 if (type) {
4628 const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(
4629 RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef}));
4630 if (typedef_type)
4631 return GetType(typedef_type->getDecl()->getUnderlyingType());
4632 }
4633 return CompilerType();
4634}
4635
4636// Create related types using the current type's AST
4637
4639 return TypeSystemClang::GetBasicType(basic_type);
4640}
4641
4643 clang::ASTContext &ast = getASTContext();
4644 const FunctionType::ExtInfo generic_ext_info(
4645 /*noReturn=*/false,
4646 /*hasRegParm=*/false,
4647 /*regParm=*/0,
4648 CallingConv::CC_C,
4649 /*producesResult=*/false,
4650 /*noCallerSavedRegs=*/false,
4651 /*NoCfCheck=*/false,
4652 /*cmseNSCall=*/false);
4653 QualType func_type = ast.getFunctionNoProtoType(ast.VoidTy, generic_ext_info);
4654 return GetType(func_type);
4655}
4656// Exploring the type
4657
4658const llvm::fltSemantics &
4660 clang::ASTContext &ast = getASTContext();
4661 const size_t bit_size = byte_size * 8;
4662 if (bit_size == ast.getTypeSize(ast.FloatTy))
4663 return ast.getFloatTypeSemantics(ast.FloatTy);
4664 else if (bit_size == ast.getTypeSize(ast.DoubleTy))
4665 return ast.getFloatTypeSemantics(ast.DoubleTy);
4666 else if (bit_size == ast.getTypeSize(ast.LongDoubleTy) ||
4667 bit_size == llvm::APFloat::semanticsSizeInBits(
4668 ast.getFloatTypeSemantics(ast.LongDoubleTy)))
4669 return ast.getFloatTypeSemantics(ast.LongDoubleTy);
4670 else if (bit_size == ast.getTypeSize(ast.HalfTy))
4671 return ast.getFloatTypeSemantics(ast.HalfTy);
4672 return llvm::APFloatBase::Bogus();
4673}
4674
4675std::optional<uint64_t>
4677 ExecutionContextScope *exe_scope) {
4678 if (GetCompleteType(type)) {
4679 clang::QualType qual_type(GetCanonicalQualType(type));
4680 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4681 switch (type_class) {
4682 case clang::Type::Record:
4683 if (GetCompleteType(type))
4684 return getASTContext().getTypeSize(qual_type);
4685 else
4686 return std::nullopt;
4687 break;
4688
4689 case clang::Type::ObjCInterface:
4690 case clang::Type::ObjCObject: {
4691 ExecutionContext exe_ctx(exe_scope);
4692 Process *process = exe_ctx.GetProcessPtr();
4693 if (process) {
4694 ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
4695 if (objc_runtime) {
4696 uint64_t bit_size = 0;
4697 if (objc_runtime->GetTypeBitSize(GetType(qual_type), bit_size))
4698 return bit_size;
4699 }
4700 } else {
4701 static bool g_printed = false;
4702 if (!g_printed) {
4703 StreamString s;
4704 DumpTypeDescription(type, s);
4705
4706 llvm::outs() << "warning: trying to determine the size of type ";
4707 llvm::outs() << s.GetString() << "\n";
4708 llvm::outs() << "without a valid ExecutionContext. this is not "
4709 "reliable. please file a bug against LLDB.\n";
4710 llvm::outs() << "backtrace:\n";
4711 llvm::sys::PrintStackTrace(llvm::outs());
4712 llvm::outs() << "\n";
4713 g_printed = true;
4714 }
4715 }
4716 }
4717 [[fallthrough]];
4718 default:
4719 const uint32_t bit_size = getASTContext().getTypeSize(qual_type);
4720 if (bit_size == 0) {
4721 if (qual_type->isIncompleteArrayType())
4722 return getASTContext().getTypeSize(
4723 qual_type->getArrayElementTypeNoTypeQual()
4724 ->getCanonicalTypeUnqualified());
4725 }
4726 if (qual_type->isObjCObjectOrInterfaceType())
4727 return bit_size +
4728 getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4729 // Function types actually have a size of 0, that's not an error.
4730 if (qual_type->isFunctionProtoType())
4731 return bit_size;
4732 if (bit_size)
4733 return bit_size;
4734 }
4735 }
4736 return std::nullopt;
4737}
4738
4739std::optional<size_t>
4741 ExecutionContextScope *exe_scope) {
4742 if (GetCompleteType(type))
4743 return getASTContext().getTypeAlign(GetQualType(type));
4744 return {};
4745}
4746
4748 uint64_t &count) {
4749 if (!type)
4751
4752 count = 1;
4753 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4754
4755 switch (qual_type->getTypeClass()) {
4756 case clang::Type::Atomic:
4757 case clang::Type::Auto:
4758 case clang::Type::Decltype:
4759 case clang::Type::Elaborated:
4760 case clang::Type::Paren:
4761 case clang::Type::Typedef:
4762 case clang::Type::TypeOf:
4763 case clang::Type::TypeOfExpr:
4764 case clang::Type::Using:
4765 llvm_unreachable("Handled in RemoveWrappingTypes!");
4766
4767 case clang::Type::UnaryTransform:
4768 break;
4769
4770 case clang::Type::FunctionNoProto:
4771 case clang::Type::FunctionProto:
4772 return lldb::eEncodingUint;
4773
4774 case clang::Type::IncompleteArray:
4775 case clang::Type::VariableArray:
4776 break;
4777
4778 case clang::Type::ConstantArray:
4779 break;
4780
4781 case clang::Type::DependentVector:
4782 case clang::Type::ExtVector:
4783 case clang::Type::Vector:
4784 // TODO: Set this to more than one???
4785 break;
4786
4787 case clang::Type::BitInt:
4788 case clang::Type::DependentBitInt:
4789 return qual_type->isUnsignedIntegerType() ? lldb::eEncodingUint
4791
4792 case clang::Type::Builtin:
4793 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4794 case clang::BuiltinType::Void:
4795 break;
4796
4797 case clang::BuiltinType::Char_S:
4798 case clang::BuiltinType::SChar:
4799 case clang::BuiltinType::WChar_S:
4800 case clang::BuiltinType::Short:
4801 case clang::BuiltinType::Int:
4802 case clang::BuiltinType::Long:
4803 case clang::BuiltinType::LongLong:
4804 case clang::BuiltinType::Int128:
4805 return lldb::eEncodingSint;
4806
4807 case clang::BuiltinType::Bool:
4808 case clang::BuiltinType::Char_U:
4809 case clang::BuiltinType::UChar:
4810 case clang::BuiltinType::WChar_U:
4811 case clang::BuiltinType::Char8:
4812 case clang::BuiltinType::Char16:
4813 case clang::BuiltinType::Char32:
4814 case clang::BuiltinType::UShort:
4815 case clang::BuiltinType::UInt:
4816 case clang::BuiltinType::ULong:
4817 case clang::BuiltinType::ULongLong:
4818 case clang::BuiltinType::UInt128:
4819 return lldb::eEncodingUint;
4820
4821 // Fixed point types. Note that they are currently ignored.
4822 case clang::BuiltinType::ShortAccum:
4823 case clang::BuiltinType::Accum:
4824 case clang::BuiltinType::LongAccum:
4825 case clang::BuiltinType::UShortAccum:
4826 case clang::BuiltinType::UAccum:
4827 case clang::BuiltinType::ULongAccum:
4828 case clang::BuiltinType::ShortFract:
4829 case clang::BuiltinType::Fract:
4830 case clang::BuiltinType::LongFract:
4831 case clang::BuiltinType::UShortFract:
4832 case clang::BuiltinType::UFract:
4833 case clang::BuiltinType::ULongFract:
4834 case clang::BuiltinType::SatShortAccum:
4835 case clang::BuiltinType::SatAccum:
4836 case clang::BuiltinType::SatLongAccum:
4837 case clang::BuiltinType::SatUShortAccum:
4838 case clang::BuiltinType::SatUAccum:
4839 case clang::BuiltinType::SatULongAccum:
4840 case clang::BuiltinType::SatShortFract:
4841 case clang::BuiltinType::SatFract:
4842 case clang::BuiltinType::SatLongFract:
4843 case clang::BuiltinType::SatUShortFract:
4844 case clang::BuiltinType::SatUFract:
4845 case clang::BuiltinType::SatULongFract:
4846 break;
4847
4848 case clang::BuiltinType::Half:
4849 case clang::BuiltinType::Float:
4850 case clang::BuiltinType::Float16:
4851 case clang::BuiltinType::Float128:
4852 case clang::BuiltinType::Double:
4853 case clang::BuiltinType::LongDouble:
4854 case clang::BuiltinType::BFloat16:
4855 case clang::BuiltinType::Ibm128:
4857
4858 case clang::BuiltinType::ObjCClass:
4859 case clang::BuiltinType::ObjCId:
4860 case clang::BuiltinType::ObjCSel:
4861 return lldb::eEncodingUint;
4862
4863 case clang::BuiltinType::NullPtr:
4864 return lldb::eEncodingUint;
4865
4866 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4867 case clang::BuiltinType::Kind::BoundMember:
4868 case clang::BuiltinType::Kind::BuiltinFn:
4869 case clang::BuiltinType::Kind::Dependent:
4870 case clang::BuiltinType::Kind::OCLClkEvent:
4871 case clang::BuiltinType::Kind::OCLEvent:
4872 case clang::BuiltinType::Kind::OCLImage1dRO:
4873 case clang::BuiltinType::Kind::OCLImage1dWO:
4874 case clang::BuiltinType::Kind::OCLImage1dRW:
4875 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4876 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4877 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4878 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4879 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4880 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4881 case clang::BuiltinType::Kind::OCLImage2dRO:
4882 case clang::BuiltinType::Kind::OCLImage2dWO:
4883 case clang::BuiltinType::Kind::OCLImage2dRW:
4884 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4885 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4886 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4887 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4888 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4889 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4890 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4891 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4892 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4893 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4894 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4895 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4896 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4897 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4898 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4899 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4900 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4901 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4902 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4903 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4904 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4905 case clang::BuiltinType::Kind::OCLImage3dRO:
4906 case clang::BuiltinType::Kind::OCLImage3dWO:
4907 case clang::BuiltinType::Kind::OCLImage3dRW:
4908 case clang::BuiltinType::Kind::OCLQueue:
4909 case clang::BuiltinType::Kind::OCLReserveID:
4910 case clang::BuiltinType::Kind::OCLSampler:
4911 case clang::BuiltinType::Kind::OMPArraySection:
4912 case clang::BuiltinType::Kind::OMPArrayShaping:
4913 case clang::BuiltinType::Kind::OMPIterator:
4914 case clang::BuiltinType::Kind::Overload:
4915 case clang::BuiltinType::Kind::PseudoObject:
4916 case clang::BuiltinType::Kind::UnknownAny:
4917 break;
4918
4919 case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
4920 case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
4921 case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
4922 case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
4923 case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
4924 case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
4925 case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
4926 case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
4927 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleReferenceStreamout:
4928 case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualReferenceStreamout:
4929 case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleReferenceStreamin:
4930 case clang::BuiltinType::OCLIntelSubgroupAVCImeDualReferenceStreamin:
4931 break;
4932
4933 // PowerPC -- Matrix Multiply Assist
4934 case clang::BuiltinType::VectorPair:
4935 case clang::BuiltinType::VectorQuad:
4936 break;
4937
4938 // ARM -- Scalable Vector Extension
4939 case clang::BuiltinType::SveBool:
4940 case clang::BuiltinType::SveBoolx2:
4941 case clang::BuiltinType::SveBoolx4:
4942 case clang::BuiltinType::SveCount:
4943 case clang::BuiltinType::SveInt8:
4944 case clang::BuiltinType::SveInt8x2:
4945 case clang::BuiltinType::SveInt8x3:
4946 case clang::BuiltinType::SveInt8x4:
4947 case clang::BuiltinType::SveInt16:
4948 case clang::BuiltinType::SveInt16x2:
4949 case clang::BuiltinType::SveInt16x3:
4950 case clang::BuiltinType::SveInt16x4:
4951 case clang::BuiltinType::SveInt32:
4952 case clang::BuiltinType::SveInt32x2:
4953 case clang::BuiltinType::SveInt32x3:
4954 case clang::BuiltinType::SveInt32x4:
4955 case clang::BuiltinType::SveInt64:
4956 case clang::BuiltinType::SveInt64x2:
4957 case clang::BuiltinType::SveInt64x3:
4958 case clang::BuiltinType::SveInt64x4:
4959 case clang::BuiltinType::SveUint8:
4960 case clang::BuiltinType::SveUint8x2:
4961 case clang::BuiltinType::SveUint8x3:
4962 case clang::BuiltinType::SveUint8x4:
4963 case clang::BuiltinType::SveUint16:
4964 case clang::BuiltinType::SveUint16x2:
4965 case clang::BuiltinType::SveUint16x3:
4966 case clang::BuiltinType::SveUint16x4:
4967 case clang::BuiltinType::SveUint32:
4968 case clang::BuiltinType::SveUint32x2:
4969 case clang::BuiltinType::SveUint32x3:
4970 case clang::BuiltinType::SveUint32x4:
4971 case clang::BuiltinType::SveUint64:
4972 case clang::BuiltinType::SveUint64x2:
4973 case clang::BuiltinType::SveUint64x3:
4974 case clang::BuiltinType::SveUint64x4:
4975 case clang::BuiltinType::SveFloat16:
4976 case clang::BuiltinType::SveBFloat16:
4977 case clang::BuiltinType::SveBFloat16x2:
4978 case clang::BuiltinType::SveBFloat16x3:
4979 case clang::BuiltinType::SveBFloat16x4:
4980 case clang::BuiltinType::SveFloat16x2:
4981 case clang::BuiltinType::SveFloat16x3:
4982 case clang::BuiltinType::SveFloat16x4:
4983 case clang::BuiltinType::SveFloat32:
4984 case clang::BuiltinType::SveFloat32x2:
4985 case clang::BuiltinType::SveFloat32x3:
4986 case clang::BuiltinType::SveFloat32x4:
4987 case clang::BuiltinType::SveFloat64:
4988 case clang::BuiltinType::SveFloat64x2:
4989 case clang::BuiltinType::SveFloat64x3:
4990 case clang::BuiltinType::SveFloat64x4:
4991 break;
4992
4993 // RISC-V V builtin types.
4994#define RVV_TYPE(Name, Id, SingletonId) case clang::BuiltinType::Id:
4995#include "clang/Basic/RISCVVTypes.def"
4996 break;
4997
4998 // WebAssembly builtin types.
4999 case clang::BuiltinType::WasmExternRef:
5000 break;
5001
5002 case clang::BuiltinType::IncompleteMatrixIdx:
5003 break;
5004 }
5005 break;
5006 // All pointer types are represented as unsigned integer encodings. We may
5007 // nee to add a eEncodingPointer if we ever need to know the difference
5008 case clang::Type::ObjCObjectPointer:
5009 case clang::Type::BlockPointer:
5010 case clang::Type::Pointer:
5011 case clang::Type::LValueReference:
5012 case clang::Type::RValueReference:
5013 case clang::Type::MemberPointer:
5014 return lldb::eEncodingUint;
5015 case clang::Type::Complex: {
5017 if (qual_type->isComplexType())
5018 encoding = lldb::eEncodingIEEE754;
5019 else {
5020 const clang::ComplexType *complex_type =
5021 qual_type->getAsComplexIntegerType();
5022 if (complex_type)
5023 encoding = GetType(complex_type->getElementType()).GetEncoding(count);
5024 else
5025 encoding = lldb::eEncodingSint;
5026 }
5027 count = 2;
5028 return encoding;
5029 }
5030
5031 case clang::Type::ObjCInterface:
5032 break;
5033 case clang::Type::Record:
5034 break;
5035 case clang::Type::Enum:
5036 return qual_type->isUnsignedIntegerOrEnumerationType()
5039 case clang::Type::DependentSizedArray:
5040 case clang::Type::DependentSizedExtVector:
5041 case clang::Type::UnresolvedUsing:
5042 case clang::Type::Attributed:
5043 case clang::Type::BTFTagAttributed:
5044 case clang::Type::TemplateTypeParm:
5045 case clang::Type::SubstTemplateTypeParm:
5046 case clang::Type::SubstTemplateTypeParmPack:
5047 case clang::Type::InjectedClassName:
5048 case clang::Type::DependentName:
5049 case clang::Type::DependentTemplateSpecialization:
5050 case clang::Type::PackExpansion:
5051 case clang::Type::ObjCObject:
5052
5053 case clang::Type::TemplateSpecialization:
5054 case clang::Type::DeducedTemplateSpecialization:
5055 case clang::Type::Adjusted:
5056 case clang::Type::Pipe:
5057 break;
5058
5059 // pointer type decayed from an array or function type.
5060 case clang::Type::Decayed:
5061 break;
5062 case clang::Type::ObjCTypeParam:
5063 break;
5064
5065 case clang::Type::DependentAddressSpace:
5066 break;
5067 case clang::Type::MacroQualified:
5068 break;
5069
5070 case clang::Type::ConstantMatrix:
5071 case clang::Type::DependentSizedMatrix:
5072 break;
5073
5074 // We don't handle pack indexing yet
5075 case clang::Type::PackIndexing:
5076 break;
5077 }
5078 count = 0;
5080}
5081
5083 if (!type)
5084 return lldb::eFormatDefault;
5085
5086 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5087
5088 switch (qual_type->getTypeClass()) {
5089 case clang::Type::Atomic:
5090 case clang::Type::Auto:
5091 case clang::Type::Decltype:
5092 case clang::Type::Elaborated:
5093 case clang::Type::Paren:
5094 case clang::Type::Typedef:
5095 case clang::Type::TypeOf:
5096 case clang::Type::TypeOfExpr:
5097 case clang::Type::Using:
5098 llvm_unreachable("Handled in RemoveWrappingTypes!");
5099 case clang::Type::UnaryTransform:
5100 break;
5101
5102 case clang::Type::FunctionNoProto:
5103 case clang::Type::FunctionProto:
5104 break;
5105
5106 case clang::Type::IncompleteArray:
5107 case clang::Type::VariableArray:
5108 break;
5109
5110 case clang::Type::ConstantArray:
5111 return lldb::eFormatVoid; // no value
5112
5113 case clang::Type::DependentVector:
5114 case clang::Type::ExtVector:
5115 case clang::Type::Vector:
5116 break;
5117
5118 case clang::Type::BitInt:
5119 case clang::Type::DependentBitInt:
5120 return qual_type->isUnsignedIntegerType() ? lldb::eFormatUnsigned
5122
5123 case clang::Type::Builtin:
5124 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5125 case clang::BuiltinType::UnknownAny:
5126 case clang::BuiltinType::Void:
5127 case clang::BuiltinType::BoundMember:
5128 break;
5129
5130 case clang::BuiltinType::Bool:
5131 return lldb::eFormatBoolean;
5132 case clang::BuiltinType::Char_S:
5133 case clang::BuiltinType::SChar:
5134 case clang::BuiltinType::WChar_S:
5135 case clang::BuiltinType::Char_U:
5136 case clang::BuiltinType::UChar:
5137 case clang::BuiltinType::WChar_U:
5138 return lldb::eFormatChar;
5139 case clang::BuiltinType::Char8:
5140 return lldb::eFormatUnicode8;
5141 case clang::BuiltinType::Char16:
5143 case clang::BuiltinType::Char32:
5145 case clang::BuiltinType::UShort:
5146 return lldb::eFormatUnsigned;
5147 case clang::BuiltinType::Short:
5148 return lldb::eFormatDecimal;
5149 case clang::BuiltinType::UInt:
5150 return lldb::eFormatUnsigned;
5151 case clang::BuiltinType::Int:
5152 return lldb::eFormatDecimal;
5153 case clang::BuiltinType::ULong:
5154 return lldb::eFormatUnsigned;
5155 case clang::BuiltinType::Long:
5156 return lldb::eFormatDecimal;
5157 case clang::BuiltinType::ULongLong:
5158 return lldb::eFormatUnsigned;
5159 case clang::BuiltinType::LongLong:
5160 return lldb::eFormatDecimal;
5161 case clang::BuiltinType::UInt128:
5162 return lldb::eFormatUnsigned;
5163 case clang::BuiltinType::Int128:
5164 return lldb::eFormatDecimal;
5165 case clang::BuiltinType::Half:
5166 case clang::BuiltinType::Float:
5167 case clang::BuiltinType::Double:
5168 case clang::BuiltinType::LongDouble:
5169 return lldb::eFormatFloat;
5170 default:
5171 return lldb::eFormatHex;
5172 }
5173 break;
5174 case clang::Type::ObjCObjectPointer:
5175 return lldb::eFormatHex;
5176 case clang::Type::BlockPointer:
5177 return lldb::eFormatHex;
5178 case clang::Type::Pointer:
5179 return lldb::eFormatHex;
5180 case clang::Type::LValueReference:
5181 case clang::Type::RValueReference:
5182 return lldb::eFormatHex;
5183 case clang::Type::MemberPointer:
5184 return lldb::eFormatHex;
5185 case clang::Type::Complex: {
5186 if (qual_type->isComplexType())
5187 return lldb::eFormatComplex;
5188 else
5190 }
5191 case clang::Type::ObjCInterface:
5192 break;
5193 case clang::Type::Record:
5194 break;
5195 case clang::Type::Enum:
5196 return lldb::eFormatEnum;
5197 case clang::Type::DependentSizedArray:
5198 case clang::Type::DependentSizedExtVector:
5199 case clang::Type::UnresolvedUsing:
5200 case clang::Type::Attributed:
5201 case clang::Type::BTFTagAttributed:
5202 case clang::Type::TemplateTypeParm:
5203 case clang::Type::SubstTemplateTypeParm:
5204 case clang::Type::SubstTemplateTypeParmPack:
5205 case clang::Type::InjectedClassName:
5206 case clang::Type::DependentName:
5207 case clang::Type::DependentTemplateSpecialization:
5208 case clang::Type::PackExpansion:
5209 case clang::Type::ObjCObject:
5210
5211 case clang::Type::TemplateSpecialization:
5212 case clang::Type::DeducedTemplateSpecialization:
5213 case clang::Type::Adjusted:
5214 case clang::Type::Pipe:
5215 break;
5216
5217 // pointer type decayed from an array or function type.
5218 case clang::Type::Decayed:
5219 break;
5220 case clang::Type::ObjCTypeParam:
5221 break;
5222
5223 case clang::Type::DependentAddressSpace:
5224 break;
5225 case clang::Type::MacroQualified:
5226 break;
5227
5228 // Matrix types we're not sure how to display yet.
5229 case clang::Type::ConstantMatrix:
5230 case clang::Type::DependentSizedMatrix:
5231 break;
5232
5233 // We don't handle pack indexing yet
5234 case clang::Type::PackIndexing:
5235 break;
5236 }
5237 // We don't know hot to display this type...
5238 return lldb::eFormatBytes;
5239}
5240
5241static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5242 bool check_superclass) {
5243 while (class_interface_decl) {
5244 if (class_interface_decl->ivar_size() > 0)
5245 return true;
5246
5247 if (check_superclass)
5248 class_interface_decl = class_interface_decl->getSuperClass();
5249 else
5250 break;
5251 }
5252 return false;
5253}
5254
5255static std::optional<SymbolFile::ArrayInfo>
5257 clang::QualType qual_type,
5258 const ExecutionContext *exe_ctx) {
5259 if (qual_type->isIncompleteArrayType())
5260 if (auto *metadata = ast.GetMetadata(qual_type.getTypePtr()))
5261 return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(),
5262 exe_ctx);
5263 return std::nullopt;
5264}
5265
5267 bool omit_empty_base_classes,
5268 const ExecutionContext *exe_ctx) {
5269 if (!type)
5270 return 0;
5271
5272 uint32_t num_children = 0;
5273 clang::QualType qual_type(RemoveWrappingTypes(GetQualType(type)));
5274 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5275 switch (type_class) {
5276 case clang::Type::Builtin:
5277 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5278 case clang::BuiltinType::ObjCId: // child is Class
5279 case clang::BuiltinType::ObjCClass: // child is Class
5280 num_children = 1;
5281 break;
5282
5283 default:
5284 break;
5285 }
5286 break;
5287
5288 case clang::Type::Complex:
5289 return 0;
5290 case clang::Type::Record:
5291 if (GetCompleteQualType(&getASTContext(), qual_type)) {
5292 const clang::RecordType *record_type =
5293 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5294 const clang::RecordDecl *record_decl = record_type->getDecl();
5295 assert(record_decl);
5296 const clang::CXXRecordDecl *cxx_record_decl =
5297 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5298 if (cxx_record_decl) {
5299 if (omit_empty_base_classes) {
5300 // Check each base classes to see if it or any of its base classes
5301 // contain any fields. This can help limit the noise in variable
5302 // views by not having to show base classes that contain no members.
5303 clang::CXXRecordDecl::base_class_const_iterator base_class,
5304 base_class_end;
5305 for (base_class = cxx_record_decl->bases_begin(),
5306 base_class_end = cxx_record_decl->bases_end();
5307 base_class != base_class_end; ++base_class) {
5308 const clang::CXXRecordDecl *base_class_decl =
5309 llvm::cast<clang::CXXRecordDecl>(
5310 base_class->getType()
5311 ->getAs<clang::RecordType>()
5312 ->getDecl());
5313
5314 // Skip empty base classes
5315 if (!TypeSystemClang::RecordHasFields(base_class_decl))
5316 continue;
5317
5318 num_children++;
5319 }
5320 } else {
5321 // Include all base classes
5322 num_children += cxx_record_decl->getNumBases();
5323 }
5324 }
5325 num_children += std::distance(record_decl->field_begin(),
5326 record_decl->field_end());
5327 }
5328 break;
5329
5330 case clang::Type::ObjCObject:
5331 case clang::Type::ObjCInterface:
5332 if (GetCompleteQualType(&getASTContext(), qual_type)) {
5333 const clang::ObjCObjectType *objc_class_type =
5334 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5335 assert(objc_class_type);
5336 if (objc_class_type) {
5337 clang::ObjCInterfaceDecl *class_interface_decl =
5338 objc_class_type->getInterface();
5339
5340 if (class_interface_decl) {
5341
5342 clang::ObjCInterfaceDecl *superclass_interface_decl =
5343 class_interface_decl->getSuperClass();
5344 if (superclass_interface_decl) {
5345 if (omit_empty_base_classes) {
5346 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5347 ++num_children;
5348 } else
5349 ++num_children;
5350 }
5351
5352 num_children += class_interface_decl->ivar_size();
5353 }
5354 }
5355 }
5356 break;
5357
5358 case clang::Type::LValueReference:
5359 case clang::Type::RValueReference:
5360 case clang::Type::ObjCObjectPointer: {
5361 CompilerType pointee_clang_type(GetPointeeType(type));
5362
5363 uint32_t num_pointee_children = 0;
5364 if (pointee_clang_type.IsAggregateType())
5365 num_pointee_children =
5366 pointee_clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
5367 // If this type points to a simple type, then it has 1 child
5368 if (num_pointee_children == 0)
5369 num_children = 1;
5370 else
5371 num_children = num_pointee_children;
5372 } break;
5373
5374 case clang::Type::Vector:
5375 case clang::Type::ExtVector:
5376 num_children =
5377 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5378 break;
5379
5380 case clang::Type::ConstantArray:
5381 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5382 ->getSize()
5383 .getLimitedValue();
5384 break;
5385 case clang::Type::IncompleteArray:
5386 if (auto array_info =
5387 GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx))
5388 // Only 1-dimensional arrays are supported.
5389 num_children = array_info->element_orders.size()
5390 ? array_info->element_orders.back()
5391 : 0;
5392 break;
5393
5394 case clang::Type::Pointer: {
5395 const clang::PointerType *pointer_type =
5396 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5397 clang::QualType pointee_type(pointer_type->getPointeeType());
5398 CompilerType pointee_clang_type(GetType(pointee_type));
5399 uint32_t num_pointee_children = 0;
5400 if (pointee_clang_type.IsAggregateType())
5401 num_pointee_children =
5402 pointee_clang_type.GetNumChildren(omit_empty_base_classes, exe_ctx);
5403 if (num_pointee_children == 0) {
5404 // We have a pointer to a pointee type that claims it has no children. We
5405 // will want to look at
5406 num_children = GetNumPointeeChildren(pointee_type);
5407 } else
5408 num_children = num_pointee_children;
5409 } break;
5410
5411 default:
5412 break;
5413 }
5414 return num_children;
5415}
5416
5419}
5420
5423 if (type) {
5424 clang::QualType qual_type(GetQualType(type));
5425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5426 if (type_class == clang::Type::Builtin) {
5427 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5428 case clang::BuiltinType::Void:
5429 return eBasicTypeVoid;
5430 case clang::BuiltinType::Bool:
5431 return eBasicTypeBool;
5432 case clang::BuiltinType::Char_S:
5433 return eBasicTypeSignedChar;
5434 case clang::BuiltinType::Char_U:
5436 case clang::BuiltinType::Char8:
5437 return eBasicTypeChar8;
5438 case clang::BuiltinType::Char16:
5439 return eBasicTypeChar16;
5440 case clang::BuiltinType::Char32:
5441 return eBasicTypeChar32;
5442 case clang::BuiltinType::UChar:
5444 case clang::BuiltinType::SChar:
5445 return eBasicTypeSignedChar;
5446 case clang::BuiltinType::WChar_S:
5447 return eBasicTypeSignedWChar;
5448 case clang::BuiltinType::WChar_U:
5450 case clang::BuiltinType::Short:
5451 return eBasicTypeShort;
5452 case clang::BuiltinType::UShort:
5454 case clang::BuiltinType::Int:
5455 return eBasicTypeInt;
5456 case clang::BuiltinType::UInt:
5457 return eBasicTypeUnsignedInt;
5458 case clang::BuiltinType::Long:
5459 return eBasicTypeLong;
5460 case clang::BuiltinType::ULong:
5462 case clang::BuiltinType::LongLong:
5463 return eBasicTypeLongLong;
5464 case clang::BuiltinType::ULongLong:
5466 case clang::BuiltinType::Int128:
5467 return eBasicTypeInt128;
5468 case clang::BuiltinType::UInt128:
5470
5471 case clang::BuiltinType::Half:
5472 return eBasicTypeHalf;
5473 case clang::BuiltinType::Float:
5474 return eBasicTypeFloat;
5475 case clang::BuiltinType::Double:
5476 return eBasicTypeDouble;
5477 case clang::BuiltinType::LongDouble:
5478 return eBasicTypeLongDouble;
5479
5480 case clang::BuiltinType::NullPtr:
5481 return eBasicTypeNullPtr;
5482 case clang::BuiltinType::ObjCId:
5483 return eBasicTypeObjCID;
5484 case clang::BuiltinType::ObjCClass:
5485 return eBasicTypeObjCClass;
5486 case clang::BuiltinType::ObjCSel:
5487 return eBasicTypeObjCSel;
5488 default:
5489 return eBasicTypeOther;
5490 }
5491 }
5492 }
5493 return eBasicTypeInvalid;
5494}
5495
5498 std::function<bool(const CompilerType &integer_type,
5499 ConstString name,
5500 const llvm::APSInt &value)> const &callback) {
5501 const clang::EnumType *enum_type =
5502 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5503 if (enum_type) {
5504 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5505 if (enum_decl) {
5506 CompilerType integer_type = GetType(enum_decl->getIntegerType());
5507
5508 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5509 for (enum_pos = enum_decl->enumerator_begin(),
5510 enum_end_pos = enum_decl->enumerator_end();
5511 enum_pos != enum_end_pos; ++enum_pos) {
5512 ConstString name(enum_pos->getNameAsString().c_str());
5513 if (!callback(integer_type, name, enum_pos->getInitVal()))
5514 break;
5515 }
5516 }
5517 }
5518}
5519
5520#pragma mark Aggregate Types
5521
5523 if (!type)
5524 return 0;
5525
5526 uint32_t count = 0;
5527 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
5528 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5529 switch (type_class) {
5530 case clang::Type::Record:
5531 if (GetCompleteType(type)) {
5532 const clang::RecordType *record_type =
5533 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5534 if (record_type) {
5535 clang::RecordDecl *record_decl = record_type->getDecl();
5536 if (record_decl) {
5537 count = std::distance(record_decl->field_begin(),
5538 record_decl->field_end());
5539 }
5540 }
5541 }
5542 break;
5543
5544 case clang::Type::ObjCObjectPointer: {
5545 const clang::ObjCObjectPointerType *objc_class_type =
5546 qual_type->castAs<clang::ObjCObjectPointerType>();
5547 const clang::ObjCInterfaceType *objc_interface_type =
5548 objc_class_type->getInterfaceType();
5549 if (objc_interface_type &&
5551 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5552 clang::ObjCInterfaceDecl *class_interface_decl =
5553 objc_interface_type->getDecl();
5554 if (class_interface_decl) {
5555 count = class_interface_decl->ivar_size();
5556 }
5557 }
5558 break;
5559 }
5560
5561 case clang::Type::ObjCObject:
5562 case clang::Type::ObjCInterface:
5563 if (GetCompleteType(type)) {
5564 const clang::ObjCObjectType *objc_class_type =
5565 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5566 if (objc_class_type) {
5567 clang::ObjCInterfaceDecl *class_interface_decl =
5568 objc_class_type->getInterface();
5569
5570 if (class_interface_decl)
5571 count = class_interface_decl->ivar_size();
5572 }
5573 }
5574 break;
5575
5576 default:
5577 break;
5578 }
5579 return count;
5580}
5581
5583GetObjCFieldAtIndex(clang::ASTContext *ast,
5584 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5585 std::string &name, uint64_t *bit_offset_ptr,
5586 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5587 if (class_interface_decl) {
5588 if (idx < (class_interface_decl->ivar_size())) {
5589 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5590 ivar_end = class_interface_decl->ivar_end();
5591 uint32_t ivar_idx = 0;
5592
5593 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5594 ++ivar_pos, ++ivar_idx) {
5595 if (ivar_idx == idx) {
5596 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5597
5598 clang::QualType ivar_qual_type(ivar_decl->getType());
5599
5600 name.assign(ivar_decl->getNameAsString());
5601
5602 if (bit_offset_ptr) {
5603 const clang::ASTRecordLayout &interface_layout =
5604 ast->getASTObjCInterfaceLayout(class_interface_decl);
5605 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5606 }
5607
5608 const bool is_bitfield = ivar_pos->isBitField();
5609
5610 if (bitfield_bit_size_ptr) {
5611 *bitfield_bit_size_ptr = 0;
5612
5613 if (is_bitfield && ast) {
5614 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5615 clang::Expr::EvalResult result;
5616 if (bitfield_bit_size_expr &&
5617 bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) {
5618 llvm::APSInt bitfield_apsint = result.Val.getInt();
5619 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5620 }
5621 }
5622 }
5623 if (is_bitfield_ptr)
5624 *is_bitfield_ptr = is_bitfield;
5625
5626 return ivar_qual_type.getAsOpaquePtr();
5627 }
5628 }
5629 }
5630 }
5631 return nullptr;
5632}
5633
5635 size_t idx, std::string &name,
5636 uint64_t *bit_offset_ptr,
5637 uint32_t *bitfield_bit_size_ptr,
5638 bool *is_bitfield_ptr) {
5639 if (!type)
5640 return CompilerType();
5641
5642 clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
5643 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5644 switch (type_class) {
5645 case clang::Type::Record:
5646 if (GetCompleteType(type)) {
5647 const clang::RecordType *record_type =
5648 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5649 const clang::RecordDecl *record_decl = record_type->getDecl();
5650 uint32_t field_idx = 0;
5651 clang::RecordDecl::field_iterator field, field_end;
5652 for (field = record_decl->field_begin(),
5653 field_end = record_decl->field_end();
5654 field != field_end; ++field, ++field_idx) {
5655 if (idx == field_idx) {
5656 // Print the member type if requested
5657 // Print the member name and equal sign
5658 name.assign(field->getNameAsString());
5659
5660 // Figure out the type byte size (field_type_info.first) and
5661 // alignment (field_type_info.second) from the AST context.
5662 if (bit_offset_ptr) {
5663 const clang::ASTRecordLayout &record_layout =
5664 getASTContext().getASTRecordLayout(record_decl);
5665 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5666 }
5667
5668 const bool is_bitfield = field->isBitField();
5669
5670 if (bitfield_bit_size_ptr) {
5671 *bitfield_bit_size_ptr = 0;
5672
5673 if (is_bitfield) {
5674 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5675 clang::Expr::EvalResult result;
5676 if (bitfield_bit_size_expr &&
5677 bitfield_bit_size_expr->EvaluateAsInt(result,
5678 getASTContext())) {
5679 llvm::APSInt bitfield_apsint = result.Val.getInt();
5680 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5681 }
5682 }
5683 }
5684 if (is_bitfield_ptr)
5685 *is_bitfield_ptr = is_bitfield;
5686
5687 return GetType(field->getType());
5688 }
5689 }
5690 }
5691 break;
5692
5693 case clang::Type::ObjCObjectPointer: {
5694 const clang::ObjCObjectPointerType *objc_class_type =
5695 qual_type->castAs<clang::ObjCObjectPointerType>();
5696 const clang::ObjCInterfaceType *objc_interface_type =
5697 objc_class_type->getInterfaceType();
5698 if (objc_interface_type &&
5700 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
5701 clang::ObjCInterfaceDecl *class_interface_decl =
5702 objc_interface_type->getDecl();
5703 if (class_interface_decl) {
5704 return CompilerType(
5705 weak_from_this(),
5706 GetObjCFieldAtIndex(&getASTContext(), class_interface_decl, idx,
5707 name, bit_offset_ptr, bitfield_bit_size_ptr,
5708 is_bitfield_ptr));
5709 }
5710 }
5711 break;
5712 }
5713
5714 case clang::Type::ObjCObject:
5715 case clang::Type::ObjCInterface:
5716 if (GetCompleteType(type)) {
5717 const clang::ObjCObjectType *objc_class_type =
5718 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5719 assert(objc_class_type);
5720 if (objc_class_type) {
5721 clang::ObjCInterfaceDecl *class_interface_decl =
5722 objc_class_type->getInterface();
5723 return CompilerType(
5724 weak_from_this(),
5725 GetObjCFieldAtIndex(&getASTContext(), class_interface_decl, idx,
5726 name, bit_offset_ptr, bitfield_bit_size_ptr,
5727 is_bitfield_ptr));
5728 }
5729 }
5730 break;
5731
5732 default:
5733 break;
5734 }
5735 return CompilerType();
5736}
5737
5738uint32_t
5740 uint32_t count = 0;
5741 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5742 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5743 switch (type_class) {
5744 case clang::Type::Record:
5745 if (GetCompleteType(type)) {
5746 const clang::CXXRecordDecl *cxx_record_decl =
5747 qual_type->getAsCXXRecordDecl();
5748 if (cxx_record_decl)
5749 count = cxx_record_decl->getNumBases();
5750 }
5751 break;
5752
5753 case clang::Type::ObjCObjectPointer:
5755 break;
5756
5757 case clang::Type::ObjCObject:
5758 if (GetCompleteType(type)) {
5759 const clang::ObjCObjectType *objc_class_type =
5760 qual_type->getAsObjCQualifiedInterfaceType();
5761 if (objc_class_type) {
5762 clang::ObjCInterfaceDecl *class_interface_decl =
5763 objc_class_type->getInterface();
5764
5765 if (class_interface_decl && class_interface_decl->getSuperClass())
5766 count = 1;
5767 }
5768 }
5769 break;
5770 case clang::Type::ObjCInterface:
5771 if (GetCompleteType(type)) {
5772 const clang::ObjCInterfaceType *objc_interface_type =
5773 qual_type->getAs<clang::ObjCInterfaceType>();
5774 if (objc_interface_type) {
5775 clang::ObjCInterfaceDecl *class_interface_decl =
5776 objc_interface_type->getInterface();
5777
5778 if (class_interface_decl && class_interface_decl->getSuperClass())
5779 count = 1;
5780 }
5781 }
5782 break;
5783
5784 default:
5785 break;
5786 }
5787 return count;
5788}
5789
5790uint32_t
5792 uint32_t count = 0;
5793 clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
5794 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5795 switch (type_class) {
5796 case clang::Type::Record:
5797 if (GetCompleteType(type)) {
5798 const clang::CXXRecordDecl *cxx_record_decl =
5799 qual_type->getAsCXXRecordDecl();
5800 if (cxx_record_decl)
5801 count = cxx_record_decl->getNumVBases();
5802 }
5803 break;
5804
5805 default:
5806 break;
5807 }
5808 return count;
5809}
5810
5812 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5813 clang::QualType qual_type = RemoveWrappingTypes(