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