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