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