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