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