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