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