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