Coverage Report

Created: 2020-07-11 14:00

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- TypeSystemClang.cpp -----------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "TypeSystemClang.h"
10
11
#include "llvm/Support/FormatAdapters.h"
12
#include "llvm/Support/FormatVariadic.h"
13
14
#include <mutex>
15
#include <string>
16
#include <vector>
17
18
#include "clang/AST/ASTContext.h"
19
#include "clang/AST/ASTImporter.h"
20
#include "clang/AST/Attr.h"
21
#include "clang/AST/CXXInheritance.h"
22
#include "clang/AST/DeclObjC.h"
23
#include "clang/AST/DeclTemplate.h"
24
#include "clang/AST/Mangle.h"
25
#include "clang/AST/RecordLayout.h"
26
#include "clang/AST/Type.h"
27
#include "clang/AST/VTableBuilder.h"
28
#include "clang/Basic/Builtins.h"
29
#include "clang/Basic/Diagnostic.h"
30
#include "clang/Basic/FileManager.h"
31
#include "clang/Basic/FileSystemOptions.h"
32
#include "clang/Basic/LangStandard.h"
33
#include "clang/Basic/SourceManager.h"
34
#include "clang/Basic/TargetInfo.h"
35
#include "clang/Basic/TargetOptions.h"
36
#include "clang/Frontend/FrontendOptions.h"
37
#include "clang/Lex/HeaderSearch.h"
38
#include "clang/Lex/HeaderSearchOptions.h"
39
#include "clang/Lex/ModuleMap.h"
40
#include "clang/Sema/Sema.h"
41
42
#include "llvm/Support/Signals.h"
43
#include "llvm/Support/Threading.h"
44
45
#include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
46
#include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
47
#include "Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h"
48
#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
49
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
50
#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
51
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
52
#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
53
#include "lldb/Utility/ArchSpec.h"
54
#include "lldb/Utility/Flags.h"
55
56
#include "lldb/Core/DumpDataExtractor.h"
57
#include "lldb/Core/Module.h"
58
#include "lldb/Core/PluginManager.h"
59
#include "lldb/Core/StreamFile.h"
60
#include "lldb/Core/ThreadSafeDenseMap.h"
61
#include "lldb/Core/UniqueCStringMap.h"
62
#include "lldb/Symbol/ObjectFile.h"
63
#include "lldb/Symbol/SymbolFile.h"
64
#include "lldb/Target/ExecutionContext.h"
65
#include "lldb/Target/Language.h"
66
#include "lldb/Target/Process.h"
67
#include "lldb/Target/Target.h"
68
#include "lldb/Utility/DataExtractor.h"
69
#include "lldb/Utility/LLDBAssert.h"
70
#include "lldb/Utility/Log.h"
71
#include "lldb/Utility/RegularExpression.h"
72
#include "lldb/Utility/Scalar.h"
73
74
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
75
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
76
#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
77
78
#include <stdio.h>
79
80
#include <mutex>
81
82
using namespace lldb;
83
using namespace lldb_private;
84
using namespace clang;
85
using llvm::StringSwitch;
86
87
LLDB_PLUGIN_DEFINE(TypeSystemClang)
88
89
namespace {
90
82.9k
static void VerifyDecl(clang::Decl *decl) {
91
82.9k
  assert(decl && "VerifyDecl called with nullptr?");
92
82.9k
#ifndef NDEBUG
93
82.9k
  // We don't care about the actual access value here but only want to trigger
94
82.9k
  // that Clang calls its internal Decl::AccessDeclContextSanity check.
95
82.9k
  decl->getAccess();
96
82.9k
#endif
97
82.9k
}
98
99
static inline bool
100
9.01k
TypeSystemClangSupportsLanguage(lldb::LanguageType language) {
101
9.01k
  return language == eLanguageTypeUnknown || // Clang is the default type system
102
9.01k
         
lldb_private::Language::LanguageIsC(language)9.00k
||
103
9.01k
         
lldb_private::Language::LanguageIsCPlusPlus(language)5.42k
||
104
9.01k
         
lldb_private::Language::LanguageIsObjC(language)1.58k
||
105
9.01k
         
lldb_private::Language::LanguageIsPascal(language)2
||
106
9.01k
         // Use Clang for Rust until there is a proper language plugin for it
107
9.01k
         
language == eLanguageTypeRust2
||
108
9.01k
         
language == eLanguageTypeExtRenderScript2
||
109
9.01k
         // Use Clang for D until there is a proper language plugin for it
110
9.01k
         
language == eLanguageTypeD2
||
111
9.01k
         // Open Dylan compiler debug info is designed to be Clang-compatible
112
9.01k
         
language == eLanguageTypeDylan1
;
113
9.01k
}
114
115
// Checks whether m1 is an overload of m2 (as opposed to an override). This is
116
// called by addOverridesForMethod to distinguish overrides (which share a
117
// vtable entry) from overloads (which require distinct entries).
118
50
bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) {
119
50
  // FIXME: This should detect covariant return types, but currently doesn't.
120
50
  lldbassert(&m1->getASTContext() == &m2->getASTContext() &&
121
50
             "Methods should have the same AST context");
122
50
  clang::ASTContext &context = m1->getASTContext();
123
50
124
50
  const auto *m1Type = llvm::cast<clang::FunctionProtoType>(
125
50
      context.getCanonicalType(m1->getType()));
126
50
127
50
  const auto *m2Type = llvm::cast<clang::FunctionProtoType>(
128
50
      context.getCanonicalType(m2->getType()));
129
50
130
50
  auto compareArgTypes = [&context](const clang::QualType &m1p,
131
50
                                    const clang::QualType &m2p) {
132
3
    return context.hasSameType(m1p.getUnqualifiedType(),
133
3
                               m2p.getUnqualifiedType());
134
3
  };
135
50
136
50
  // FIXME: In C++14 and later, we can just pass m2Type->param_type_end()
137
50
  //        as a fourth parameter to std::equal().
138
50
  return (m1->getNumParams() != m2->getNumParams()) ||
139
50
         !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(),
140
41
                     m2Type->param_type_begin(), compareArgTypes);
141
50
}
142
143
// If decl is a virtual method, walk the base classes looking for methods that
144
// decl overrides. This table of overridden methods is used by IRGen to
145
// determine the vtable layout for decl's parent class.
146
50.1k
void addOverridesForMethod(clang::CXXMethodDecl *decl) {
147
50.1k
  if (!decl->isVirtual())
148
49.9k
    return;
149
223
150
223
  clang::CXXBasePaths paths;
151
223
152
223
  auto find_overridden_methods =
153
223
      [decl](const clang::CXXBaseSpecifier *specifier,
154
223
             clang::CXXBasePath &path) {
155
114
        if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>(
156
114
                specifier->getType()->getAs<clang::RecordType>()->getDecl())) {
157
114
158
114
          clang::DeclarationName name = decl->getDeclName();
159
114
160
114
          // If this is a destructor, check whether the base class destructor is
161
114
          // virtual.
162
114
          if (name.getNameKind() == clang::DeclarationName::CXXDestructorName)
163
38
            if (auto *baseDtorDecl = base_record->getDestructor()) {
164
28
              if (baseDtorDecl->isVirtual()) {
165
28
                path.Decls = baseDtorDecl;
166
28
                return true;
167
28
              } else
168
0
                return false;
169
86
            }
170
86
171
86
          // Otherwise, search for name in the base class.
172
95
          
for (path.Decls = base_record->lookup(name); 86
!path.Decls.empty();
173
86
               
path.Decls = path.Decls.slice(1)9
) {
174
50
            if (auto *method_decl =
175
50
                    llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front()))
176
50
              if (method_decl->isVirtual() && !isOverload(decl, method_decl)) {
177
41
                path.Decls = method_decl;
178
41
                return true;
179
41
              }
180
50
          }
181
86
        }
182
114
183
114
        
return false45
;
184
114
      };
185
223
186
223
  if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) {
187
66
    for (auto *overridden_decl : paths.found_decls())
188
66
      decl->addOverriddenMethod(
189
66
          llvm::cast<clang::CXXMethodDecl>(overridden_decl));
190
66
  }
191
223
}
192
}
193
194
static lldb::addr_t GetVTableAddress(Process &process,
195
                                     VTableContextBase &vtable_ctx,
196
                                     ValueObject &valobj,
197
24
                                     const ASTRecordLayout &record_layout) {
198
24
  // Retrieve type info
199
24
  CompilerType pointee_type;
200
24
  CompilerType this_type(valobj.GetCompilerType());
201
24
  uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
202
24
  if (!type_info)
203
0
    return LLDB_INVALID_ADDRESS;
204
24
205
24
  // Check if it's a pointer or reference
206
24
  bool ptr_or_ref = false;
207
24
  if (type_info & (eTypeIsPointer | eTypeIsReference)) {
208
18
    ptr_or_ref = true;
209
18
    type_info = pointee_type.GetTypeInfo();
210
18
  }
211
24
212
24
  // We process only C++ classes
213
24
  const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
214
24
  if ((type_info & cpp_class) != cpp_class)
215
0
    return LLDB_INVALID_ADDRESS;
216
24
217
24
  // Calculate offset to VTable pointer
218
24
  lldb::offset_t vbtable_ptr_offset =
219
24
      vtable_ctx.isMicrosoft() ? 
record_layout.getVBPtrOffset().getQuantity()0
220
24
                               : 0;
221
24
222
24
  if (ptr_or_ref) {
223
18
    // We have a pointer / ref to object, so read
224
18
    // VTable pointer from process memory
225
18
226
18
    if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad)
227
0
      return LLDB_INVALID_ADDRESS;
228
18
229
18
    auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
230
18
    if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS)
231
18
      
return 0
LLDB_INVALID_ADDRESS0
;
232
18
233
18
    vbtable_ptr_addr += vbtable_ptr_offset;
234
18
235
18
    Status err;
236
18
    return process.ReadPointerFromMemory(vbtable_ptr_addr, err);
237
18
  }
238
6
239
6
  // We have an object already read from process memory,
240
6
  // so just extract VTable pointer from it
241
6
242
6
  DataExtractor data;
243
6
  Status err;
244
6
  auto size = valobj.GetData(data, err);
245
6
  if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size)
246
0
    return LLDB_INVALID_ADDRESS;
247
6
248
6
  return data.GetAddress(&vbtable_ptr_offset);
249
6
}
250
251
static int64_t ReadVBaseOffsetFromVTable(Process &process,
252
                                         VTableContextBase &vtable_ctx,
253
                                         lldb::addr_t vtable_ptr,
254
                                         const CXXRecordDecl *cxx_record_decl,
255
24
                                         const CXXRecordDecl *base_class_decl) {
256
24
  if (vtable_ctx.isMicrosoft()) {
257
0
    clang::MicrosoftVTableContext &msoft_vtable_ctx =
258
0
        static_cast<clang::MicrosoftVTableContext &>(vtable_ctx);
259
0
260
0
    // Get the index into the virtual base table. The
261
0
    // index is the index in uint32_t from vbtable_ptr
262
0
    const unsigned vbtable_index =
263
0
        msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl);
264
0
    const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4;
265
0
    Status err;
266
0
    return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX,
267
0
                                               err);
268
0
  }
269
24
270
24
  clang::ItaniumVTableContext &itanium_vtable_ctx =
271
24
      static_cast<clang::ItaniumVTableContext &>(vtable_ctx);
272
24
273
24
  clang::CharUnits base_offset_offset =
274
24
      itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl,
275
24
                                                    base_class_decl);
276
24
  const lldb::addr_t base_offset_addr =
277
24
      vtable_ptr + base_offset_offset.getQuantity();
278
24
  const uint32_t base_offset_size = process.GetAddressByteSize();
279
24
  Status err;
280
24
  return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size,
281
24
                                             INT64_MAX, err);
282
24
}
283
284
static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx,
285
                              ValueObject &valobj,
286
                              const ASTRecordLayout &record_layout,
287
                              const CXXRecordDecl *cxx_record_decl,
288
                              const CXXRecordDecl *base_class_decl,
289
24
                              int32_t &bit_offset) {
290
24
  ExecutionContext exe_ctx(valobj.GetExecutionContextRef());
291
24
  Process *process = exe_ctx.GetProcessPtr();
292
24
  if (!process)
293
0
    return false;
294
24
295
24
  lldb::addr_t vtable_ptr =
296
24
      GetVTableAddress(*process, vtable_ctx, valobj, record_layout);
297
24
  if (vtable_ptr == LLDB_INVALID_ADDRESS)
298
24
    
return false0
;
299
24
300
24
  auto base_offset = ReadVBaseOffsetFromVTable(
301
24
      *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl);
302
24
  if (base_offset == INT64_MAX)
303
24
    
return false0
;
304
24
305
24
  bit_offset = base_offset * 8;
306
24
307
24
  return true;
308
24
}
309
310
typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, TypeSystemClang *>
311
    ClangASTMap;
312
313
476k
static ClangASTMap &GetASTMap() {
314
476k
  static ClangASTMap *g_map_ptr = nullptr;
315
476k
  static llvm::once_flag g_once_flag;
316
476k
  llvm::call_once(g_once_flag, []() {
317
835
    g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
318
835
  });
319
476k
  return *g_map_ptr;
320
476k
}
321
322
TypePayloadClang::TypePayloadClang(OptionalClangModuleID owning_module,
323
                                   bool is_complete_objc_class)
324
63.3k
    : m_payload(owning_module.GetValue()) {
325
63.3k
  SetIsCompleteObjCClass(is_complete_objc_class);
326
63.3k
}
327
328
0
void TypePayloadClang::SetOwningModule(OptionalClangModuleID id) {
329
0
  assert(id.GetValue() < ObjCClassBit);
330
0
  bool is_complete = IsCompleteObjCClass();
331
0
  m_payload = id.GetValue();
332
0
  SetIsCompleteObjCClass(is_complete);
333
0
}
334
335
static void SetMemberOwningModule(clang::Decl *member,
336
66.5k
                                  const clang::Decl *parent) {
337
66.5k
  if (!member || !parent)
338
0
    return;
339
66.5k
340
66.5k
  OptionalClangModuleID id(parent->getOwningModuleID());
341
66.5k
  if (!id.HasValue())
342
45.2k
    return;
343
21.3k
344
21.3k
  member->setFromASTFile();
345
21.3k
  member->setOwningModuleID(id.GetValue());
346
21.3k
  member->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
347
21.3k
  if (llvm::isa<clang::NamedDecl>(member))
348
21.3k
    if (auto *dc = llvm::dyn_cast<clang::DeclContext>(parent)) {
349
21.3k
      dc->setHasExternalVisibleStorage(true);
350
21.3k
      // This triggers ExternalASTSource::FindExternalVisibleDeclsByName() to be
351
21.3k
      // called when searching for members.
352
21.3k
      dc->setHasExternalLexicalStorage(true);
353
21.3k
    }
354
21.3k
}
355
356
char TypeSystemClang::ID;
357
358
bool TypeSystemClang::IsOperator(llvm::StringRef name,
359
48.4k
                                 clang::OverloadedOperatorKind &op_kind) {
360
48.4k
  // All operators have to start with "operator".
361
48.4k
  if (!name.consume_front("operator"))
362
44.3k
    return false;
363
4.14k
364
4.14k
  // Remember if there was a space after "operator". This is necessary to
365
4.14k
  // check for collisions with strangely named functions like "operatorint()".
366
4.14k
  bool space_after_operator = name.consume_front(" ");
367
4.14k
368
4.14k
  op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
369
4.14k
                .Case("+", clang::OO_Plus)
370
4.14k
                .Case("+=", clang::OO_PlusEqual)
371
4.14k
                .Case("++", clang::OO_PlusPlus)
372
4.14k
                .Case("-", clang::OO_Minus)
373
4.14k
                .Case("-=", clang::OO_MinusEqual)
374
4.14k
                .Case("--", clang::OO_MinusMinus)
375
4.14k
                .Case("->", clang::OO_Arrow)
376
4.14k
                .Case("->*", clang::OO_ArrowStar)
377
4.14k
                .Case("*", clang::OO_Star)
378
4.14k
                .Case("*=", clang::OO_StarEqual)
379
4.14k
                .Case("/", clang::OO_Slash)
380
4.14k
                .Case("/=", clang::OO_SlashEqual)
381
4.14k
                .Case("%", clang::OO_Percent)
382
4.14k
                .Case("%=", clang::OO_PercentEqual)
383
4.14k
                .Case("^", clang::OO_Caret)
384
4.14k
                .Case("^=", clang::OO_CaretEqual)
385
4.14k
                .Case("&", clang::OO_Amp)
386
4.14k
                .Case("&=", clang::OO_AmpEqual)
387
4.14k
                .Case("&&", clang::OO_AmpAmp)
388
4.14k
                .Case("|", clang::OO_Pipe)
389
4.14k
                .Case("|=", clang::OO_PipeEqual)
390
4.14k
                .Case("||", clang::OO_PipePipe)
391
4.14k
                .Case("~", clang::OO_Tilde)
392
4.14k
                .Case("!", clang::OO_Exclaim)
393
4.14k
                .Case("!=", clang::OO_ExclaimEqual)
394
4.14k
                .Case("=", clang::OO_Equal)
395
4.14k
                .Case("==", clang::OO_EqualEqual)
396
4.14k
                .Case("<", clang::OO_Less)
397
4.14k
                .Case("<<", clang::OO_LessLess)
398
4.14k
                .Case("<<=", clang::OO_LessLessEqual)
399
4.14k
                .Case("<=", clang::OO_LessEqual)
400
4.14k
                .Case(">", clang::OO_Greater)
401
4.14k
                .Case(">>", clang::OO_GreaterGreater)
402
4.14k
                .Case(">>=", clang::OO_GreaterGreaterEqual)
403
4.14k
                .Case(">=", clang::OO_GreaterEqual)
404
4.14k
                .Case("()", clang::OO_Call)
405
4.14k
                .Case("[]", clang::OO_Subscript)
406
4.14k
                .Case(",", clang::OO_Comma)
407
4.14k
                .Default(clang::NUM_OVERLOADED_OPERATORS);
408
4.14k
409
4.14k
  // We found a fitting operator, so we can exit now.
410
4.14k
  if (op_kind != clang::NUM_OVERLOADED_OPERATORS)
411
3.82k
    return true;
412
317
413
317
  // After the "operator " or "operator" part is something unknown. This means
414
317
  // it's either one of the named operators (new/delete), a conversion operator
415
317
  // (e.g. operator bool) or a function which name starts with "operator"
416
317
  // (e.g. void operatorbool).
417
317
418
317
  // If it's a function that starts with operator it can't have a space after
419
317
  // "operator" because identifiers can't contain spaces.
420
317
  // E.g. "operator int" (conversion operator)
421
317
  //  vs. "operatorint" (function with colliding name).
422
317
  if (!space_after_operator)
423
22
    return false; // not an operator.
424
295
425
295
  // Now the operator is either one of the named operators or a conversion
426
295
  // operator.
427
295
  op_kind = StringSwitch<clang::OverloadedOperatorKind>(name)
428
295
                .Case("new", clang::OO_New)
429
295
                .Case("new[]", clang::OO_Array_New)
430
295
                .Case("delete", clang::OO_Delete)
431
295
                .Case("delete[]", clang::OO_Array_Delete)
432
295
                // conversion operators hit this case.
433
295
                .Default(clang::NUM_OVERLOADED_OPERATORS);
434
295
435
295
  return true;
436
295
}
437
438
clang::AccessSpecifier
439
68.5k
TypeSystemClang::ConvertAccessTypeToAccessSpecifier(AccessType access) {
440
68.5k
  switch (access) {
441
1
  default:
442
1
    break;
443
1
  case eAccessNone:
444
1
    return AS_none;
445
62.1k
  case eAccessPublic:
446
62.1k
    return AS_public;
447
3.69k
  case eAccessPrivate:
448
3.69k
    return AS_private;
449
2.76k
  case eAccessProtected:
450
2.76k
    return AS_protected;
451
1
  }
452
1
  return AS_none;
453
1
}
454
455
7.10k
static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
456
7.10k
  // FIXME: Cleanup per-file based stuff.
457
7.10k
458
7.10k
  // Set some properties which depend solely on the input kind; it would be
459
7.10k
  // nice to move these to the language standard, and have the driver resolve
460
7.10k
  // the input kind + language standard.
461
7.10k
  if (IK.getLanguage() == clang::Language::Asm) {
462
0
    Opts.AsmPreprocessor = 1;
463
7.10k
  } else if (IK.isObjectiveC()) {
464
7.10k
    Opts.ObjC = 1;
465
7.10k
  }
466
7.10k
467
7.10k
  LangStandard::Kind LangStd = LangStandard::lang_unspecified;
468
7.10k
469
7.10k
  if (LangStd == LangStandard::lang_unspecified) {
470
7.10k
    // Based on the base language, pick one.
471
7.10k
    switch (IK.getLanguage()) {
472
0
    case clang::Language::Unknown:
473
0
    case clang::Language::LLVM_IR:
474
0
    case clang::Language::RenderScript:
475
0
      llvm_unreachable("Invalid input kind!");
476
0
    case clang::Language::OpenCL:
477
0
      LangStd = LangStandard::lang_opencl10;
478
0
      break;
479
0
    case clang::Language::CUDA:
480
0
      LangStd = LangStandard::lang_cuda;
481
0
      break;
482
0
    case clang::Language::Asm:
483
0
    case clang::Language::C:
484
0
    case clang::Language::ObjC:
485
0
      LangStd = LangStandard::lang_gnu99;
486
0
      break;
487
7.10k
    case clang::Language::CXX:
488
7.10k
    case clang::Language::ObjCXX:
489
7.10k
      LangStd = LangStandard::lang_gnucxx98;
490
7.10k
      break;
491
7.10k
    case clang::Language::HIP:
492
0
      LangStd = LangStandard::lang_hip;
493
0
      break;
494
7.10k
    }
495
7.10k
  }
496
7.10k
497
7.10k
  const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
498
7.10k
  Opts.LineComment = Std.hasLineComments();
499
7.10k
  Opts.C99 = Std.isC99();
500
7.10k
  Opts.CPlusPlus = Std.isCPlusPlus();
501
7.10k
  Opts.CPlusPlus11 = Std.isCPlusPlus11();
502
7.10k
  Opts.Digraphs = Std.hasDigraphs();
503
7.10k
  Opts.GNUMode = Std.isGNUMode();
504
7.10k
  Opts.GNUInline = !Std.isC99();
505
7.10k
  Opts.HexFloats = Std.hasHexFloats();
506
7.10k
  Opts.ImplicitInt = Std.hasImplicitInt();
507
7.10k
508
7.10k
  Opts.WChar = true;
509
7.10k
510
7.10k
  // OpenCL has some additional defaults.
511
7.10k
  if (LangStd == LangStandard::lang_opencl10) {
512
0
    Opts.OpenCL = 1;
513
0
    Opts.AltiVec = 1;
514
0
    Opts.CXXOperatorNames = 1;
515
0
    Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::All);
516
0
  }
517
7.10k
518
7.10k
  // OpenCL and C++ both have bool, true, false keywords.
519
7.10k
  Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
520
7.10k
521
7.10k
  Opts.setValueVisibilityMode(DefaultVisibility);
522
7.10k
523
7.10k
  // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is
524
7.10k
  // specified, or -std is set to a conforming mode.
525
7.10k
  Opts.Trigraphs = !Opts.GNUMode;
526
7.10k
  Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
527
7.10k
  Opts.OptimizeSize = 0;
528
7.10k
529
7.10k
  // FIXME: Eliminate this dependency.
530
7.10k
  //    unsigned Opt =
531
7.10k
  //    Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
532
7.10k
  //    Opts.Optimize = Opt != 0;
533
7.10k
  unsigned Opt = 0;
534
7.10k
535
7.10k
  // This is the __NO_INLINE__ define, which just depends on things like the
536
7.10k
  // optimization level and -fno-inline, not actually whether the backend has
537
7.10k
  // inlining enabled.
538
7.10k
  //
539
7.10k
  // FIXME: This is affected by other options (-fno-inline).
540
7.10k
  Opts.NoInlineDefine = !Opt;
541
7.10k
542
7.10k
  // This is needed to allocate the extra space for the owning module
543
7.10k
  // on each decl.
544
7.10k
  Opts.ModulesLocalVisibility = 1;
545
7.10k
}
546
547
TypeSystemClang::TypeSystemClang(llvm::StringRef name,
548
7.10k
                                 llvm::Triple target_triple) {
549
7.10k
  m_display_name = name.str();
550
7.10k
  if (!target_triple.str().empty())
551
7.10k
    SetTargetTriple(target_triple.str());
552
7.10k
  // The caller didn't pass an ASTContext so create a new one for this
553
7.10k
  // TypeSystemClang.
554
7.10k
  CreateASTContext();
555
7.10k
}
556
557
TypeSystemClang::TypeSystemClang(llvm::StringRef name,
558
13.0k
                                 ASTContext &existing_ctxt) {
559
13.0k
  m_display_name = name.str();
560
13.0k
  SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str());
561
13.0k
562
13.0k
  m_ast_up.reset(&existing_ctxt);
563
13.0k
  GetASTMap().Insert(&existing_ctxt, this);
564
13.0k
}
565
566
// Destructor
567
19.7k
TypeSystemClang::~TypeSystemClang() { Finalize(); }
568
569
2.97k
ConstString TypeSystemClang::GetPluginNameStatic() {
570
2.97k
  return ConstString("clang");
571
2.97k
}
572
573
0
ConstString TypeSystemClang::GetPluginName() {
574
0
  return TypeSystemClang::GetPluginNameStatic();
575
0
}
576
577
0
uint32_t TypeSystemClang::GetPluginVersion() { return 1; }
578
579
lldb::TypeSystemSP TypeSystemClang::CreateInstance(lldb::LanguageType language,
580
                                                   lldb_private::Module *module,
581
5.02k
                                                   Target *target) {
582
5.02k
  if (!TypeSystemClangSupportsLanguage(language))
583
1
    return lldb::TypeSystemSP();
584
5.02k
  ArchSpec arch;
585
5.02k
  if (module)
586
2.74k
    arch = module->GetArchitecture();
587
2.27k
  else if (target)
588
2.27k
    arch = target->GetArchitecture();
589
5.02k
590
5.02k
  if (!arch.IsValid())
591
0
    return lldb::TypeSystemSP();
592
5.02k
593
5.02k
  llvm::Triple triple = arch.GetTriple();
594
5.02k
  // LLVM wants this to be set to iOS or MacOSX; if we're working on
595
5.02k
  // a bare-boards type image, change the triple for llvm's benefit.
596
5.02k
  if (triple.getVendor() == llvm::Triple::Apple &&
597
5.02k
      
triple.getOS() == llvm::Triple::UnknownOS4.94k
) {
598
0
    if (triple.getArch() == llvm::Triple::arm ||
599
0
        triple.getArch() == llvm::Triple::aarch64 ||
600
0
        triple.getArch() == llvm::Triple::aarch64_32 ||
601
0
        triple.getArch() == llvm::Triple::thumb) {
602
0
      triple.setOS(llvm::Triple::IOS);
603
0
    } else {
604
0
      triple.setOS(llvm::Triple::MacOSX);
605
0
    }
606
0
  }
607
5.02k
608
5.02k
  if (module) {
609
2.74k
    std::string ast_name =
610
2.74k
        "ASTContext for '" + module->GetFileSpec().GetPath() + "'";
611
2.74k
    return std::make_shared<TypeSystemClang>(ast_name, triple);
612
2.74k
  } else 
if (2.27k
target2.27k
&&
target->IsValid()2.27k
)
613
2.27k
    return std::make_shared<TypeSystemClangForExpressions>(*target, triple);
614
0
  return lldb::TypeSystemSP();
615
0
}
616
617
2.97k
LanguageSet TypeSystemClang::GetSupportedLanguagesForTypes() {
618
2.97k
  LanguageSet languages;
619
2.97k
  languages.Insert(lldb::eLanguageTypeC89);
620
2.97k
  languages.Insert(lldb::eLanguageTypeC);
621
2.97k
  languages.Insert(lldb::eLanguageTypeC11);
622
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus);
623
2.97k
  languages.Insert(lldb::eLanguageTypeC99);
624
2.97k
  languages.Insert(lldb::eLanguageTypeObjC);
625
2.97k
  languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
626
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
627
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
628
2.97k
  languages.Insert(lldb::eLanguageTypeC11);
629
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
630
2.97k
  return languages;
631
2.97k
}
632
633
2.97k
LanguageSet TypeSystemClang::GetSupportedLanguagesForExpressions() {
634
2.97k
  LanguageSet languages;
635
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus);
636
2.97k
  languages.Insert(lldb::eLanguageTypeObjC_plus_plus);
637
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_03);
638
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_11);
639
2.97k
  languages.Insert(lldb::eLanguageTypeC_plus_plus_14);
640
2.97k
  return languages;
641
2.97k
}
642
643
2.97k
void TypeSystemClang::Initialize() {
644
2.97k
  PluginManager::RegisterPlugin(
645
2.97k
      GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance,
646
2.97k
      GetSupportedLanguagesForTypes(), GetSupportedLanguagesForExpressions());
647
2.97k
}
648
649
2.94k
void TypeSystemClang::Terminate() {
650
2.94k
  PluginManager::UnregisterPlugin(CreateInstance);
651
2.94k
}
652
653
22.0k
void TypeSystemClang::Finalize() {
654
22.0k
  assert(m_ast_up);
655
22.0k
  GetASTMap().Erase(m_ast_up.get());
656
22.0k
  if (!m_ast_owned)
657
12.9k
    m_ast_up.release();
658
22.0k
659
22.0k
  m_builtins_up.reset();
660
22.0k
  m_selector_table_up.reset();
661
22.0k
  m_identifier_table_up.reset();
662
22.0k
  m_target_info_up.reset();
663
22.0k
  m_target_options_rp.reset();
664
22.0k
  m_diagnostics_engine_up.reset();
665
22.0k
  m_source_manager_up.reset();
666
22.0k
  m_language_options_up.reset();
667
22.0k
}
668
669
1.06k
void TypeSystemClang::setSema(Sema *s) {
670
1.06k
  // Ensure that the new sema actually belongs to our ASTContext.
671
1.06k
  assert(s == nullptr || &s->getASTContext() == m_ast_up.get());
672
1.06k
  m_sema = s;
673
1.06k
}
674
675
7.10k
const char *TypeSystemClang::GetTargetTriple() {
676
7.10k
  return m_target_triple.c_str();
677
7.10k
}
678
679
20.1k
void TypeSystemClang::SetTargetTriple(llvm::StringRef target_triple) {
680
20.1k
  m_target_triple = target_triple.str();
681
20.1k
}
682
683
void TypeSystemClang::SetExternalSource(
684
9.37k
    llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) {
685
9.37k
  ASTContext &ast = getASTContext();
686
9.37k
  ast.setExternalSource(ast_source_up);
687
9.37k
  ast.getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
688
9.37k
}
689
690
3.18M
ASTContext &TypeSystemClang::getASTContext() {
691
3.18M
  assert(m_ast_up);
692
3.18M
  return *m_ast_up;
693
3.18M
}
694
695
class NullDiagnosticConsumer : public DiagnosticConsumer {
696
public:
697
7.10k
  NullDiagnosticConsumer() {
698
7.10k
    m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
699
7.10k
  }
700
701
  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
702
0
                        const clang::Diagnostic &info) override {
703
0
    if (m_log) {
704
0
      llvm::SmallVector<char, 32> diag_str(10);
705
0
      info.FormatDiagnostic(diag_str);
706
0
      diag_str.push_back('\0');
707
0
      LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data());
708
0
    }
709
0
  }
710
711
0
  DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
712
0
    return new NullDiagnosticConsumer();
713
0
  }
714
715
private:
716
  Log *m_log;
717
};
718
719
7.10k
void TypeSystemClang::CreateASTContext() {
720
7.10k
  assert(!m_ast_up);
721
7.10k
  m_ast_owned = true;
722
7.10k
723
7.10k
  m_language_options_up = std::make_unique<LangOptions>();
724
7.10k
  ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX,
725
7.10k
                GetTargetTriple());
726
7.10k
727
7.10k
  m_identifier_table_up =
728
7.10k
      std::make_unique<IdentifierTable>(*m_language_options_up, nullptr);
729
7.10k
  m_builtins_up = std::make_unique<Builtin::Context>();
730
7.10k
731
7.10k
  m_selector_table_up = std::make_unique<SelectorTable>();
732
7.10k
733
7.10k
  clang::FileSystemOptions file_system_options;
734
7.10k
  m_file_manager_up = std::make_unique<clang::FileManager>(
735
7.10k
      file_system_options, FileSystem::Instance().GetVirtualFileSystem());
736
7.10k
737
7.10k
  llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
738
7.10k
  m_diagnostics_engine_up =
739
7.10k
      std::make_unique<DiagnosticsEngine>(diag_id_sp, new DiagnosticOptions());
740
7.10k
741
7.10k
  m_source_manager_up = std::make_unique<clang::SourceManager>(
742
7.10k
      *m_diagnostics_engine_up, *m_file_manager_up);
743
7.10k
  m_ast_up = std::make_unique<ASTContext>(
744
7.10k
      *m_language_options_up, *m_source_manager_up, *m_identifier_table_up,
745
7.10k
      *m_selector_table_up, *m_builtins_up);
746
7.10k
747
7.10k
  m_diagnostic_consumer_up = std::make_unique<NullDiagnosticConsumer>();
748
7.10k
  m_ast_up->getDiagnostics().setClient(m_diagnostic_consumer_up.get(), false);
749
7.10k
750
7.10k
  // This can be NULL if we don't know anything about the architecture or if
751
7.10k
  // the target for an architecture isn't enabled in the llvm/clang that we
752
7.10k
  // built
753
7.10k
  TargetInfo *target_info = getTargetInfo();
754
7.10k
  if (target_info)
755
7.10k
    m_ast_up->InitBuiltinTypes(*target_info);
756
7.10k
757
7.10k
  GetASTMap().Insert(m_ast_up.get(), this);
758
7.10k
759
7.10k
  llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up(
760
7.10k
      new ClangExternalASTSourceCallbacks(*this));
761
7.10k
  SetExternalSource(ast_source_up);
762
7.10k
}
763
764
434k
TypeSystemClang *TypeSystemClang::GetASTContext(clang::ASTContext *ast) {
765
434k
  TypeSystemClang *clang_ast = GetASTMap().Lookup(ast);
766
434k
  return clang_ast;
767
434k
}
768
769
36
clang::MangleContext *TypeSystemClang::getMangleContext() {
770
36
  if (m_mangle_ctx_up == nullptr)
771
3
    m_mangle_ctx_up.reset(getASTContext().createMangleContext());
772
36
  return m_mangle_ctx_up.get();
773
36
}
774
775
7.10k
std::shared_ptr<clang::TargetOptions> &TypeSystemClang::getTargetOptions() {
776
7.10k
  if (m_target_options_rp == nullptr && !m_target_triple.empty()) {
777
7.10k
    m_target_options_rp = std::make_shared<clang::TargetOptions>();
778
7.10k
    if (m_target_options_rp != nullptr)
779
7.10k
      m_target_options_rp->Triple = m_target_triple;
780
7.10k
  }
781
7.10k
  return m_target_options_rp;
782
7.10k
}
783
784
7.12k
TargetInfo *TypeSystemClang::getTargetInfo() {
785
7.12k
  // target_triple should be something like "x86_64-apple-macosx"
786
7.12k
  if (m_target_info_up == nullptr && 
!m_target_triple.empty()7.10k
)
787
7.10k
    m_target_info_up.reset(TargetInfo::CreateTargetInfo(
788
7.10k
        getASTContext().getDiagnostics(), getTargetOptions()));
789
7.12k
  return m_target_info_up.get();
790
7.12k
}
791
792
#pragma mark Basic Types
793
794
static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
795
29.9k
                                          ASTContext &ast, QualType qual_type) {
796
29.9k
  uint64_t qual_type_bit_size = ast.getTypeSize(qual_type);
797
29.9k
  return qual_type_bit_size == bit_size;
798
29.9k
}
799
800
CompilerType
801
TypeSystemClang::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
802
9.04k
                                                     size_t bit_size) {
803
9.04k
  ASTContext &ast = getASTContext();
804
9.04k
  switch (encoding) {
805
0
  case eEncodingInvalid:
806
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
807
0
      return GetType(ast.VoidPtrTy);
808
0
    break;
809
0
810
8.54k
  case eEncodingUint:
811
8.54k
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
812
184
      return GetType(ast.UnsignedCharTy);
813
8.35k
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
814
1.39k
      return GetType(ast.UnsignedShortTy);
815
6.96k
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
816
6.62k
      return GetType(ast.UnsignedIntTy);
817
346
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
818
338
      return GetType(ast.UnsignedLongTy);
819
8
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
820
0
      return GetType(ast.UnsignedLongLongTy);
821
8
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
822
7
      return GetType(ast.UnsignedInt128Ty);
823
1
    break;
824
1
825
120
  case eEncodingSint:
826
120
    if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
827
1
      return GetType(ast.SignedCharTy);
828
119
    if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
829
7
      return GetType(ast.ShortTy);
830
112
    if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
831
89
      return GetType(ast.IntTy);
832
23
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
833
22
      return GetType(ast.LongTy);
834
1
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
835
0
      return GetType(ast.LongLongTy);
836
1
    if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
837
1
      return GetType(ast.Int128Ty);
838
0
    break;
839
0
840
20
  case eEncodingIEEE754:
841
20
    if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
842
19
      return GetType(ast.FloatTy);
843
1
    if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
844
1
      return GetType(ast.DoubleTy);
845
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
846
0
      return GetType(ast.LongDoubleTy);
847
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
848
0
      return GetType(ast.HalfTy);
849
0
    break;
850
0
851
362
  case eEncodingVector:
852
362
    // Sanity check that bit_size is a multiple of 8's.
853
362
    if (bit_size && !(bit_size & 0x7u))
854
362
      return GetType(ast.getExtVectorType(ast.UnsignedCharTy, bit_size / 8));
855
0
    break;
856
1
  }
857
1
858
1
  return CompilerType();
859
1
}
860
861
lldb::BasicType
862
32
TypeSystemClang::GetBasicTypeEnumeration(ConstString name) {
863
32
  if (name) {
864
32
    typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
865
32
    static TypeNameToBasicTypeMap g_type_map;
866
32
    static llvm::once_flag g_once_flag;
867
32
    llvm::call_once(g_once_flag, []() {
868
1
      // "void"
869
1
      g_type_map.Append(ConstString("void"), eBasicTypeVoid);
870
1
871
1
      // "char"
872
1
      g_type_map.Append(ConstString("char"), eBasicTypeChar);
873
1
      g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
874
1
      g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
875
1
      g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
876
1
      g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
877
1
      g_type_map.Append(ConstString("unsigned wchar_t"),
878
1
                        eBasicTypeUnsignedWChar);
879
1
      // "short"
880
1
      g_type_map.Append(ConstString("short"), eBasicTypeShort);
881
1
      g_type_map.Append(ConstString("short int"), eBasicTypeShort);
882
1
      g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
883
1
      g_type_map.Append(ConstString("unsigned short int"),
884
1
                        eBasicTypeUnsignedShort);
885
1
886
1
      // "int"
887
1
      g_type_map.Append(ConstString("int"), eBasicTypeInt);
888
1
      g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
889
1
      g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
890
1
      g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
891
1
892
1
      // "long"
893
1
      g_type_map.Append(ConstString("long"), eBasicTypeLong);
894
1
      g_type_map.Append(ConstString("long int"), eBasicTypeLong);
895
1
      g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
896
1
      g_type_map.Append(ConstString("unsigned long int"),
897
1
                        eBasicTypeUnsignedLong);
898
1
899
1
      // "long long"
900
1
      g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
901
1
      g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
902
1
      g_type_map.Append(ConstString("unsigned long long"),
903
1
                        eBasicTypeUnsignedLongLong);
904
1
      g_type_map.Append(ConstString("unsigned long long int"),
905
1
                        eBasicTypeUnsignedLongLong);
906
1
907
1
      // "int128"
908
1
      g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
909
1
      g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
910
1
911
1
      // Miscellaneous
912
1
      g_type_map.Append(ConstString("bool"), eBasicTypeBool);
913
1
      g_type_map.Append(ConstString("float"), eBasicTypeFloat);
914
1
      g_type_map.Append(ConstString("double"), eBasicTypeDouble);
915
1
      g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
916
1
      g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
917
1
      g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
918
1
      g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
919
1
      g_type_map.Sort();
920
1
    });
921
32
922
32
    return g_type_map.Find(name, eBasicTypeInvalid);
923
32
  }
924
0
  return eBasicTypeInvalid;
925
0
}
926
927
2.33k
uint32_t TypeSystemClang::GetPointerByteSize() {
928
2.33k
  if (m_pointer_byte_size == 0)
929
179
    if (auto size = GetBasicType(lldb::eBasicTypeVoid)
930
179
                        .GetPointerType()
931
179
                        .GetByteSize(nullptr))
932
179
      m_pointer_byte_size = *size;
933
2.33k
  return m_pointer_byte_size;
934
2.33k
}
935
936
29.7k
CompilerType TypeSystemClang::GetBasicType(lldb::BasicType basic_type) {
937
29.7k
  clang::ASTContext &ast = getASTContext();
938
29.7k
939
29.7k
  lldb::opaque_compiler_type_t clang_type =
940
29.7k
      GetOpaqueCompilerType(&ast, basic_type);
941
29.7k
942
29.7k
  if (clang_type)
943
29.7k
    return CompilerType(this, clang_type);
944
1
  return CompilerType();
945
1
}
946
947
CompilerType TypeSystemClang::GetBuiltinTypeForDWARFEncodingAndBitSize(
948
5.30k
    llvm::StringRef type_name, uint32_t dw_ate, uint32_t bit_size) {
949
5.30k
  ASTContext &ast = getASTContext();
950
5.30k
951
5.30k
  switch (dw_ate) {
952
0
  default:
953
0
    break;
954
0
955
0
  case DW_ATE_address:
956
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.VoidPtrTy))
957
0
      return GetType(ast.VoidPtrTy);
958
0
    break;
959
0
960
274
  case DW_ATE_boolean:
961
274
    if (QualTypeMatchesBitSize(bit_size, ast, ast.BoolTy))
962
274
      return GetType(ast.BoolTy);
963
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
964
0
      return GetType(ast.UnsignedCharTy);
965
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
966
0
      return GetType(ast.UnsignedShortTy);
967
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
968
0
      return GetType(ast.UnsignedIntTy);
969
0
    break;
970
0
971
4
  case DW_ATE_lo_user:
972
4
    // This has been seen to mean DW_AT_complex_integer
973
4
    if (type_name.contains("complex")) {
974
4
      CompilerType complex_int_clang_type =
975
4
          GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
976
4
                                                   bit_size / 2);
977
4
      return GetType(
978
4
          ast.getComplexType(ClangUtil::GetQualType(complex_int_clang_type)));
979
4
    }
980
0
    break;
981
0
982
6
  case DW_ATE_complex_float:
983
6
    if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatComplexTy))
984
2
      return GetType(ast.FloatComplexTy);
985
4
    else if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleComplexTy))
986
2
      return GetType(ast.DoubleComplexTy);
987
2
    else if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleComplexTy))
988
2
      return GetType(ast.LongDoubleComplexTy);
989
0
    else {
990
0
      CompilerType complex_float_clang_type =
991
0
          GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
992
0
                                                   bit_size / 2);
993
0
      return GetType(
994
0
          ast.getComplexType(ClangUtil::GetQualType(complex_float_clang_type)));
995
0
    }
996
0
    break;
997
0
998
347
  case DW_ATE_float:
999
347
    if (type_name == "float" &&
1000
347
        
QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy)104
)
1001
104
      return GetType(ast.FloatTy);
1002
243
    if (type_name == "double" &&
1003
243
        
QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy)240
)
1004
240
      return GetType(ast.DoubleTy);
1005
3
    if (type_name == "long double" &&
1006
3
        QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
1007
3
      return GetType(ast.LongDoubleTy);
1008
0
    // Fall back to not requiring a name match
1009
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.FloatTy))
1010
0
      return GetType(ast.FloatTy);
1011
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.DoubleTy))
1012
0
      return GetType(ast.DoubleTy);
1013
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongDoubleTy))
1014
0
      return GetType(ast.LongDoubleTy);
1015
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.HalfTy))
1016
0
      return GetType(ast.HalfTy);
1017
0
    break;
1018
0
1019
2.59k
  case DW_ATE_signed:
1020
2.59k
    if (!type_name.empty()) {
1021
2.59k
      if (type_name == "wchar_t" &&
1022
2.59k
          
QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy)10
&&
1023
2.59k
          
(10
getTargetInfo()10
&&
1024
10
           TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1025
10
        return GetType(ast.WCharTy);
1026
2.58k
      if (type_name == "void" &&
1027
2.58k
          
QualTypeMatchesBitSize(bit_size, ast, ast.VoidTy)0
)
1028
0
        return GetType(ast.VoidTy);
1029
2.58k
      if (type_name.contains("long long") &&
1030
2.58k
          
QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy)60
)
1031
60
        return GetType(ast.LongLongTy);
1032
2.52k
      if (type_name.contains("long") &&
1033
2.52k
          
QualTypeMatchesBitSize(bit_size, ast, ast.LongTy)156
)
1034
156
        return GetType(ast.LongTy);
1035
2.36k
      if (type_name.contains("short") &&
1036
2.36k
          
QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy)80
)
1037
80
        return GetType(ast.ShortTy);
1038
2.28k
      if (type_name.contains("char")) {
1039
0
        if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1040
0
          return GetType(ast.CharTy);
1041
0
        if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
1042
0
          return GetType(ast.SignedCharTy);
1043
2.28k
      }
1044
2.28k
      if (type_name.contains("int")) {
1045
2.28k
        if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
1046
2.27k
          return GetType(ast.IntTy);
1047
7
        if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
1048
5
          return GetType(ast.Int128Ty);
1049
3
      }
1050
2.28k
    }
1051
3
    // We weren't able to match up a type name, just search by size
1052
3
    if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1053
0
      return GetType(ast.CharTy);
1054
3
    if (QualTypeMatchesBitSize(bit_size, ast, ast.ShortTy))
1055
0
      return GetType(ast.ShortTy);
1056
3
    if (QualTypeMatchesBitSize(bit_size, ast, ast.IntTy))
1057
1
      return GetType(ast.IntTy);
1058
2
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongTy))
1059
2
      return GetType(ast.LongTy);
1060
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.LongLongTy))
1061
0
      return GetType(ast.LongLongTy);
1062
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.Int128Ty))
1063
0
      return GetType(ast.Int128Ty);
1064
0
    break;
1065
0
1066
1.22k
  case DW_ATE_signed_char:
1067
1.22k
    if (ast.getLangOpts().CharIsSigned && type_name == "char") {
1068
1.15k
      if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1069
1.15k
        return GetType(ast.CharTy);
1070
71
    }
1071
71
    if (QualTypeMatchesBitSize(bit_size, ast, ast.SignedCharTy))
1072
71
      return GetType(ast.SignedCharTy);
1073
0
    break;
1074
0
1075
665
  case DW_ATE_unsigned:
1076
665
    if (!type_name.empty()) {
1077
665
      if (type_name == "wchar_t") {
1078
0
        if (QualTypeMatchesBitSize(bit_size, ast, ast.WCharTy)) {
1079
0
          if (!(getTargetInfo() &&
1080
0
                TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1081
0
            return GetType(ast.WCharTy);
1082
665
        }
1083
0
      }
1084
665
      if (type_name.contains("long long")) {
1085
52
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1086
52
          return GetType(ast.UnsignedLongLongTy);
1087
613
      } else if (type_name.contains("long")) {
1088
335
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1089
335
          return GetType(ast.UnsignedLongTy);
1090
278
      } else if (type_name.contains("short")) {
1091
72
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1092
72
          return GetType(ast.UnsignedShortTy);
1093
206
      } else if (type_name.contains("char")) {
1094
0
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1095
0
          return GetType(ast.UnsignedCharTy);
1096
206
      } else if (type_name.contains("int")) {
1097
206
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1098
204
          return GetType(ast.UnsignedIntTy);
1099
2
        if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1100
2
          return GetType(ast.UnsignedInt128Ty);
1101
0
      }
1102
665
    }
1103
0
    // We weren't able to match up a type name, just search by size
1104
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1105
0
      return GetType(ast.UnsignedCharTy);
1106
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1107
0
      return GetType(ast.UnsignedShortTy);
1108
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedIntTy))
1109
0
      return GetType(ast.UnsignedIntTy);
1110
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongTy))
1111
0
      return GetType(ast.UnsignedLongTy);
1112
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedLongLongTy))
1113
0
      return GetType(ast.UnsignedLongLongTy);
1114
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedInt128Ty))
1115
0
      return GetType(ast.UnsignedInt128Ty);
1116
0
    break;
1117
0
1118
168
  case DW_ATE_unsigned_char:
1119
168
    if (!ast.getLangOpts().CharIsSigned && 
type_name == "char"6
) {
1120
6
      if (QualTypeMatchesBitSize(bit_size, ast, ast.CharTy))
1121
6
        return GetType(ast.CharTy);
1122
162
    }
1123
162
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedCharTy))
1124
162
      return GetType(ast.UnsignedCharTy);
1125
0
    if (QualTypeMatchesBitSize(bit_size, ast, ast.UnsignedShortTy))
1126
0
      return GetType(ast.UnsignedShortTy);
1127
0
    break;
1128
0
1129
0
  case DW_ATE_imaginary_float:
1130
0
    break;
1131
0
1132
20
  case DW_ATE_UTF:
1133
20
    switch (bit_size) {
1134
4
    case 8:
1135
4
      return GetType(ast.Char8Ty);
1136
8
    case 16:
1137
8
      return GetType(ast.Char16Ty);
1138
8
    case 32:
1139
8
      return GetType(ast.Char32Ty);
1140
0
    default:
1141
0
      if (!type_name.empty()) {
1142
0
        if (type_name == "char16_t")
1143
0
          return GetType(ast.Char16Ty);
1144
0
        if (type_name == "char32_t")
1145
0
          return GetType(ast.Char32Ty);
1146
0
        if (type_name == "char8_t")
1147
0
          return GetType(ast.Char8Ty);
1148
0
      }
1149
0
    }
1150
0
    break;
1151
0
  }
1152
0
  // This assert should fire for anything that we don't catch above so we know
1153
0
  // to fix any issues we run into.
1154
0
  if (!type_name.empty()) {
1155
0
    std::string type_name_str = type_name.str();
1156
0
    Host::SystemLog(Host::eSystemLogError,
1157
0
                    "error: need to add support for DW_TAG_base_type '%s' "
1158
0
                    "encoded with DW_ATE = 0x%x, bit_size = %u\n",
1159
0
                    type_name_str.c_str(), dw_ate, bit_size);
1160
0
  } else {
1161
0
    Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1162
0
                                           "DW_TAG_base_type encoded with "
1163
0
                                           "DW_ATE = 0x%x, bit_size = %u\n",
1164
0
                    dw_ate, bit_size);
1165
0
  }
1166
0
  return CompilerType();
1167
0
}
1168
1169
196
CompilerType TypeSystemClang::GetCStringType(bool is_const) {
1170
196
  ASTContext &ast = getASTContext();
1171
196
  QualType char_type(ast.CharTy);
1172
196
1173
196
  if (is_const)
1174
196
    char_type.addConst();
1175
196
1176
196
  return GetType(ast.getPointerType(char_type));
1177
196
}
1178
1179
bool TypeSystemClang::AreTypesSame(CompilerType type1, CompilerType type2,
1180
40
                                   bool ignore_qualifiers) {
1181
40
  TypeSystemClang *ast =
1182
40
      llvm::dyn_cast_or_null<TypeSystemClang>(type1.GetTypeSystem());
1183
40
  if (!ast || ast != type2.GetTypeSystem())
1184
12
    return false;
1185
28
1186
28
  if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1187
0
    return true;
1188
28
1189
28
  QualType type1_qual = ClangUtil::GetQualType(type1);
1190
28
  QualType type2_qual = ClangUtil::GetQualType(type2);
1191
28
1192
28
  if (ignore_qualifiers) {
1193
0
    type1_qual = type1_qual.getUnqualifiedType();
1194
0
    type2_qual = type2_qual.getUnqualifiedType();
1195
0
  }
1196
28
1197
28
  return ast->getASTContext().hasSameType(type1_qual, type2_qual);
1198
28
}
1199
1200
643
CompilerType TypeSystemClang::GetTypeForDecl(void *opaque_decl) {
1201
643
  if (!opaque_decl)
1202
0
    return CompilerType();
1203
643
1204
643
  clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl);
1205
643
  if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl))
1206
643
    return GetTypeForDecl(named_decl);
1207
0
  return CompilerType();
1208
0
}
1209
1210
145k
CompilerDeclContext TypeSystemClang::CreateDeclContext(DeclContext *ctx) {
1211
145k
  // Check that the DeclContext actually belongs to this ASTContext.
1212
145k
  assert(&ctx->getParentASTContext() == &getASTContext());
1213
145k
  return CompilerDeclContext(this, ctx);
1214
145k
}
1215
1216
643
CompilerType TypeSystemClang::GetTypeForDecl(clang::NamedDecl *decl) {
1217
643
  if (clang::ObjCInterfaceDecl *interface_decl =
1218
643
      llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1219
643
    return GetTypeForDecl(interface_decl);
1220
0
  if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1221
0
    return GetTypeForDecl(tag_decl);
1222
0
  return CompilerType();
1223
0
}
1224
1225
14.2k
CompilerType TypeSystemClang::GetTypeForDecl(TagDecl *decl) {
1226
14.2k
  return GetType(getASTContext().getTagDeclType(decl));
1227
14.2k
}
1228
1229
772
CompilerType TypeSystemClang::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1230
772
  return GetType(getASTContext().getObjCInterfaceType(decl));
1231
772
}
1232
1233
#pragma mark Structure, Unions, Classes
1234
1235
void TypeSystemClang::SetOwningModule(clang::Decl *decl,
1236
129k
                                      OptionalClangModuleID owning_module) {
1237
129k
  if (!decl || !owning_module.HasValue())
1238
86.4k
    return;
1239
42.7k
1240
42.7k
  decl->setFromASTFile();
1241
42.7k
  decl->setOwningModuleID(owning_module.GetValue());
1242
42.7k
  decl->setModuleOwnershipKind(clang::Decl::ModuleOwnershipKind::Visible);
1243
42.7k
}
1244
1245
OptionalClangModuleID
1246
TypeSystemClang::GetOrCreateClangModule(llvm::StringRef name,
1247
                                        OptionalClangModuleID parent,
1248
16.0k
                                        bool is_framework, bool is_explicit) {
1249
16.0k
  // Get the external AST source which holds the modules.
1250
16.0k
  auto *ast_source = llvm::dyn_cast_or_null<ClangExternalASTSourceCallbacks>(
1251
16.0k
      getASTContext().getExternalSource());
1252
16.0k
  assert(ast_source && "external ast source was lost");
1253
16.0k
  if (!ast_source)
1254
0
    return {};
1255
16.0k
1256
16.0k
  // Lazily initialize the module map.
1257
16.0k
  if (!m_header_search_up) {
1258
509
    auto HSOpts = std::make_shared<clang::HeaderSearchOptions>();
1259
509
    m_header_search_up = std::make_unique<clang::HeaderSearch>(
1260
509
        HSOpts, *m_source_manager_up, *m_diagnostics_engine_up,
1261
509
        *m_language_options_up, m_target_info_up.get());
1262
509
    m_module_map_up = std::make_unique<clang::ModuleMap>(
1263
509
        *m_source_manager_up, *m_diagnostics_engine_up, *m_language_options_up,
1264
509
        m_target_info_up.get(), *m_header_search_up);
1265
509
  }
1266
16.0k
1267
16.0k
  // Get or create the module context.
1268
16.0k
  bool created;
1269
16.0k
  clang::Module *module;
1270
16.0k
  auto parent_desc = ast_source->getSourceDescriptor(parent.GetValue());
1271
16.0k
  std::tie(module, created) = m_module_map_up->findOrCreateModule(
1272
16.0k
      name, parent_desc ? 
parent_desc->getModuleOrNull()9.02k
:
nullptr7.05k
,
1273
16.0k
      is_framework, is_explicit);
1274
16.0k
  if (!created)
1275
13.1k
    return ast_source->GetIDForModule(module);
1276
2.88k
1277
2.88k
  return ast_source->RegisterModule(module);
1278
2.88k
}
1279
1280
CompilerType TypeSystemClang::CreateRecordType(
1281
    clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1282
    AccessType access_type, llvm::StringRef name, int kind,
1283
8.80k
    LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) {
1284
8.80k
  ASTContext &ast = getASTContext();
1285
8.80k
1286
8.80k
  if (decl_ctx == nullptr)
1287
2.39k
    decl_ctx = ast.getTranslationUnitDecl();
1288
8.80k
1289
8.80k
  if (language == eLanguageTypeObjC ||
1290
8.80k
      
language == eLanguageTypeObjC_plus_plus7.60k
) {
1291
1.29k
    bool isForwardDecl = true;
1292
1.29k
    bool isInternal = false;
1293
1.29k
    return CreateObjCClass(name, decl_ctx, owning_module, isForwardDecl,
1294
1.29k
                           isInternal, metadata);
1295
1.29k
  }
1296
7.51k
1297
7.51k
  // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1298
7.51k
  // we will need to update this code. I was told to currently always use the
1299
7.51k
  // CXXRecordDecl class since we often don't know from debug information if
1300
7.51k
  // something is struct or a class, so we default to always use the more
1301
7.51k
  // complete definition just in case.
1302
7.51k
1303
7.51k
  bool has_name = !name.empty();
1304
7.51k
  CXXRecordDecl *decl = CXXRecordDecl::CreateDeserialized(ast, 0);
1305
7.51k
  decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1306
7.51k
  decl->setDeclContext(decl_ctx);
1307
7.51k
  if (has_name)
1308
6.98k
    decl->setDeclName(&ast.Idents.get(name));
1309
7.51k
  SetOwningModule(decl, owning_module);
1310
7.51k
1311
7.51k
  if (!has_name) {
1312
534
    // In C++ a lambda is also represented as an unnamed class. This is
1313
534
    // different from an *anonymous class* that the user wrote:
1314
534
    //
1315
534
    // struct A {
1316
534
    //  // anonymous class (GNU/MSVC extension)
1317
534
    //  struct {
1318
534
    //    int x;
1319
534
    //  };
1320
534
    //  // unnamed class within a class
1321
534
    //  struct {
1322
534
    //    int y;
1323
534
    //  } B;
1324
534
    // };
1325
534
    //
1326
534
    // void f() {
1327
534
    //    // unammed class outside of a class
1328
534
    //    struct {
1329
534
    //      int z;
1330
534
    //    } C;
1331
534
    // }
1332
534
    //
1333
534
    // Anonymous classes is a GNU/MSVC extension that clang supports. It
1334
534
    // requires the anonymous class be embedded within a class. So the new
1335
534
    // heuristic verifies this condition.
1336
534
    if (isa<CXXRecordDecl>(decl_ctx) && 
exports_symbols239
)
1337
184
      decl->setAnonymousStructOrUnion(true);
1338
534
  }
1339
7.51k
1340
7.51k
  if (decl) {
1341
7.51k
    if (metadata)
1342
5.11k
      SetMetadata(decl, *metadata);
1343
7.51k
1344
7.51k
    if (access_type != eAccessNone)
1345
3.20k
      decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1346
7.51k
1347
7.51k
    if (decl_ctx)
1348
7.51k
      decl_ctx->addDecl(decl);
1349
7.51k
1350
7.51k
    return GetType(ast.getTagDeclType(decl));
1351
7.51k
  }
1352
0
  return CompilerType();
1353
0
}
1354
1355
namespace {
1356
10.3k
  bool IsValueParam(const clang::TemplateArgument &argument) {
1357
10.3k
    return argument.getKind() == TemplateArgument::Integral;
1358
10.3k
  }
1359
}
1360
1361
static TemplateParameterList *CreateTemplateParameterList(
1362
    ASTContext &ast,
1363
    const TypeSystemClang::TemplateParameterInfos &template_param_infos,
1364
6.12k
    llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1365
6.12k
  const bool parameter_pack = false;
1366
6.12k
  const bool is_typename = false;
1367
6.12k
  const unsigned depth = 0;
1368
6.12k
  const size_t num_template_params = template_param_infos.args.size();
1369
6.12k
  DeclContext *const decl_context =
1370
6.12k
      ast.getTranslationUnitDecl(); // Is this the right decl context?,
1371
15.7k
  for (size_t i = 0; i < num_template_params; 
++i9.59k
) {
1372
9.59k
    const char *name = template_param_infos.names[i];
1373
9.59k
1374
9.59k
    IdentifierInfo *identifier_info = nullptr;
1375
9.59k
    if (name && 
name[0]9.04k
)
1376
9.04k
      identifier_info = &ast.Idents.get(name);
1377
9.59k
    if (IsValueParam(template_param_infos.args[i])) {
1378
1.91k
      QualType template_param_type =
1379
1.91k
          template_param_infos.args[i].getIntegralType();
1380
1.91k
      template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1381
1.91k
          ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1382
1.91k
          identifier_info, template_param_type, parameter_pack,
1383
1.91k
          ast.getTrivialTypeSourceInfo(template_param_type)));
1384
7.68k
    } else {
1385
7.68k
      template_param_decls.push_back(TemplateTypeParmDecl::Create(
1386
7.68k
          ast, decl_context, SourceLocation(), SourceLocation(), depth, i,
1387
7.68k
          identifier_info, is_typename, parameter_pack));
1388
7.68k
    }
1389
9.59k
  }
1390
6.12k
1391
6.12k
  if (template_param_infos.packed_args) {
1392
783
    IdentifierInfo *identifier_info = nullptr;
1393
783
    if (template_param_infos.pack_name && 
template_param_infos.pack_name[0]780
)
1394
780
      identifier_info = &ast.Idents.get(template_param_infos.pack_name);
1395
783
    const bool parameter_pack_true = true;
1396
783
1397
783
    if (!template_param_infos.packed_args->args.empty() &&
1398
783
        
IsValueParam(template_param_infos.packed_args->args[0])714
) {
1399
3
      QualType template_param_type =
1400
3
          template_param_infos.packed_args->args[0].getIntegralType();
1401
3
      template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1402
3
          ast, decl_context, SourceLocation(), SourceLocation(), depth,
1403
3
          num_template_params, identifier_info, template_param_type,
1404
3
          parameter_pack_true,
1405
3
          ast.getTrivialTypeSourceInfo(template_param_type)));
1406
780
    } else {
1407
780
      template_param_decls.push_back(TemplateTypeParmDecl::Create(
1408
780
          ast, decl_context, SourceLocation(), SourceLocation(), depth,
1409
780
          num_template_params, identifier_info, is_typename,
1410
780
          parameter_pack_true));
1411
780
    }
1412
783
  }
1413
6.12k
  clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1414
6.12k
  TemplateParameterList *template_param_list = TemplateParameterList::Create(
1415
6.12k
      ast, SourceLocation(), SourceLocation(), template_param_decls,
1416
6.12k
      SourceLocation(), requires_clause);
1417
6.12k
  return template_param_list;
1418
6.12k
}
1419
1420
clang::FunctionTemplateDecl *TypeSystemClang::CreateFunctionTemplateDecl(
1421
    clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1422
    clang::FunctionDecl *func_decl, const char *name,
1423
2.09k
    const TemplateParameterInfos &template_param_infos) {
1424
2.09k
  //    /// Create a function template node.
1425
2.09k
  ASTContext &ast = getASTContext();
1426
2.09k
1427
2.09k
  llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1428
2.09k
  TemplateParameterList *template_param_list = CreateTemplateParameterList(
1429
2.09k
      ast, template_param_infos, template_param_decls);
1430
2.09k
  FunctionTemplateDecl *func_tmpl_decl =
1431
2.09k
      FunctionTemplateDecl::CreateDeserialized(ast, 0);
1432
2.09k
  func_tmpl_decl->setDeclContext(decl_ctx);
1433
2.09k
  func_tmpl_decl->setLocation(func_decl->getLocation());
1434
2.09k
  func_tmpl_decl->setDeclName(func_decl->getDeclName());
1435
2.09k
  func_tmpl_decl->init(func_decl, template_param_list);
1436
2.09k
  SetOwningModule(func_tmpl_decl, owning_module);
1437
2.09k
1438
2.09k
  for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1439
5.18k
       i < template_param_decl_count; 
++i3.09k
) {
1440
3.09k
    // TODO: verify which decl context we should put template_param_decls into..
1441
3.09k
    template_param_decls[i]->setDeclContext(func_decl);
1442
3.09k
  }
1443
2.09k
  // Function templates inside a record need to have an access specifier.
1444
2.09k
  // It doesn't matter what access specifier we give the template as LLDB
1445
2.09k
  // anyway allows accessing everything inside a record.
1446
2.09k
  if (decl_ctx->isRecord())
1447
2.05k
    func_tmpl_decl->setAccess(clang::AccessSpecifier::AS_public);
1448
2.09k
1449
2.09k
  return func_tmpl_decl;
1450
2.09k
}
1451
1452
void TypeSystemClang::CreateFunctionTemplateSpecializationInfo(
1453
    FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1454
2.09k
    const TemplateParameterInfos &infos) {
1455
2.09k
  TemplateArgumentList *template_args_ptr =
1456
2.09k
      TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args);
1457
2.09k
1458
2.09k
  func_decl->setFunctionTemplateSpecialization(func_tmpl_decl,
1459
2.09k
                                               template_args_ptr, nullptr);
1460
2.09k
}
1461
1462
ClassTemplateDecl *TypeSystemClang::CreateClassTemplateDecl(
1463
    DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1464
    lldb::AccessType access_type, const char *class_name, int kind,
1465
5.99k
    const TemplateParameterInfos &template_param_infos) {
1466
5.99k
  ASTContext &ast = getASTContext();
1467
5.99k
1468
5.99k
  ClassTemplateDecl *class_template_decl = nullptr;
1469
5.99k
  if (decl_ctx == nullptr)
1470
0
    decl_ctx = ast.getTranslationUnitDecl();
1471
5.99k
1472
5.99k
  IdentifierInfo &identifier_info = ast.Idents.get(class_name);
1473
5.99k
  DeclarationName decl_name(&identifier_info);
1474
5.99k
1475
5.99k
  clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1476
5.99k
1477
5.99k
  for (NamedDecl *decl : result) {
1478
1.98k
    class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
1479
1.98k
    if (class_template_decl)
1480
1.98k
      return class_template_decl;
1481
1.98k
  }
1482
5.99k
1483
5.99k
  llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1484
4.00k
1485
4.00k
  TemplateParameterList *template_param_list = CreateTemplateParameterList(
1486
4.00k
      ast, template_param_infos, template_param_decls);
1487
4.00k
1488
4.00k
  CXXRecordDecl *template_cxx_decl = CXXRecordDecl::CreateDeserialized(ast, 0);
1489
4.00k
  template_cxx_decl->setTagKind(static_cast<TagDecl::TagKind>(kind));
1490
4.00k
  // What decl context do we use here? TU? The actual decl context?
1491
4.00k
  template_cxx_decl->setDeclContext(decl_ctx);
1492
4.00k
  template_cxx_decl->setDeclName(decl_name);
1493
4.00k
  SetOwningModule(template_cxx_decl, owning_module);
1494
4.00k
1495
4.00k
  for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1496
11.2k
       i < template_param_decl_count; 
++i7.28k
) {
1497
7.28k
    template_param_decls[i]->setDeclContext(template_cxx_decl);
1498
7.28k
  }
1499
4.00k
1500
4.00k
  // With templated classes, we say that a class is templated with
1501
4.00k
  // specializations, but that the bare class has no functions.
1502
4.00k
  // template_cxx_decl->startDefinition();
1503
4.00k
  // template_cxx_decl->completeDefinition();
1504
4.00k
1505
4.00k
  class_template_decl = ClassTemplateDecl::CreateDeserialized(ast, 0);
1506
4.00k
  // What decl context do we use here? TU? The actual decl context?
1507
4.00k
  class_template_decl->setDeclContext(decl_ctx);
1508
4.00k
  class_template_decl->setDeclName(decl_name);
1509
4.00k
  class_template_decl->init(template_cxx_decl, template_param_list);
1510
4.00k
  template_cxx_decl->setDescribedClassTemplate(class_template_decl);
1511
4.00k
  SetOwningModule(class_template_decl, owning_module);
1512
4.00k
1513
4.00k
  if (class_template_decl) {
1514
4.00k
    if (access_type != eAccessNone)
1515
101
      class_template_decl->setAccess(
1516
101
          ConvertAccessTypeToAccessSpecifier(access_type));
1517
4.00k
1518
4.00k
    decl_ctx->addDecl(class_template_decl);
1519
4.00k
1520
4.00k
    VerifyDecl(class_template_decl);
1521
4.00k
  }
1522
4.00k
1523
4.00k
  return class_template_decl;
1524
5.99k
}
1525
1526
TemplateTemplateParmDecl *
1527
27
TypeSystemClang::CreateTemplateTemplateParmDecl(const char *template_name) {
1528
27
  ASTContext &ast = getASTContext();
1529
27
1530
27
  auto *decl_ctx = ast.getTranslationUnitDecl();
1531
27
1532
27
  IdentifierInfo &identifier_info = ast.Idents.get(template_name);
1533
27
  llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1534
27
1535
27
  TypeSystemClang::TemplateParameterInfos template_param_infos;
1536
27
  TemplateParameterList *template_param_list = CreateTemplateParameterList(
1537
27
      ast, template_param_infos, template_param_decls);
1538
27
1539
27
  // LLDB needs to create those decls only to be able to display a
1540
27
  // type that includes a template template argument. Only the name matters for
1541
27
  // this purpose, so we use dummy values for the other characteristics of the
1542
27
  // type.
1543
27
  return TemplateTemplateParmDecl::Create(
1544
27
      ast, decl_ctx, SourceLocation(),
1545
27
      /*Depth*/ 0, /*Position*/ 0,
1546
27
      /*IsParameterPack*/ false, &identifier_info, template_param_list);
1547
27
}
1548
1549
ClassTemplateSpecializationDecl *
1550
TypeSystemClang::CreateClassTemplateSpecializationDecl(
1551
    DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1552
    ClassTemplateDecl *class_template_decl, int kind,
1553
5.99k
    const TemplateParameterInfos &template_param_infos) {
1554
5.99k
  ASTContext &ast = getASTContext();
1555
5.99k
  llvm::SmallVector<clang::TemplateArgument, 2> args(
1556
5.99k
      template_param_infos.args.size() +
1557
5.99k
      (template_param_infos.packed_args ? 
195
:
05.89k
));
1558
5.99k
  std::copy(template_param_infos.args.begin(), template_param_infos.args.end(),
1559
5.99k
            args.begin());
1560
5.99k
  if (template_param_infos.packed_args) {
1561
95
    args[args.size() - 1] = TemplateArgument::CreatePackCopy(
1562
95
        ast, template_param_infos.packed_args->args);
1563
95
  }
1564
5.99k
  ClassTemplateSpecializationDecl *class_template_specialization_decl =
1565
5.99k
      ClassTemplateSpecializationDecl::CreateDeserialized(ast, 0);
1566
5.99k
  class_template_specialization_decl->setTagKind(
1567
5.99k
      static_cast<TagDecl::TagKind>(kind));
1568
5.99k
  class_template_specialization_decl->setDeclContext(decl_ctx);
1569
5.99k
  class_template_specialization_decl->setInstantiationOf(class_template_decl);
1570
5.99k
  class_template_specialization_decl->setTemplateArgs(
1571
5.99k
      TemplateArgumentList::CreateCopy(ast, args));
1572
5.99k
  ast.getTypeDeclType(class_template_specialization_decl, nullptr);
1573
5.99k
  class_template_specialization_decl->setDeclName(
1574
5.99k
      class_template_decl->getDeclName());
1575
5.99k
  SetOwningModule(class_template_specialization_decl, owning_module);
1576
5.99k
  decl_ctx->addDecl(class_template_specialization_decl);
1577
5.99k
1578
5.99k
  class_template_specialization_decl->setSpecializationKind(
1579
5.99k
      TSK_ExplicitSpecialization);
1580
5.99k
1581
5.99k
  return class_template_specialization_decl;
1582
5.99k
}
1583
1584
CompilerType TypeSystemClang::CreateClassTemplateSpecializationType(
1585
5.99k
    ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1586
5.99k
  if (class_template_specialization_decl) {
1587
5.99k
    ASTContext &ast = getASTContext();
1588
5.99k
    return GetType(ast.getTagDeclType(class_template_specialization_decl));
1589
5.99k
  }
1590
0
  return CompilerType();
1591
0
}
1592
1593
static inline bool check_op_param(bool is_method,
1594
                                  clang::OverloadedOperatorKind op_kind,
1595
                                  bool unary, bool binary,
1596
3.82k
                                  uint32_t num_params) {
1597
3.82k
  // Special-case call since it can take any number of operands
1598
3.82k
  if (op_kind == OO_Call)
1599
240
    return true;
1600
3.58k
1601
3.58k
  // The parameter count doesn't include "this"
1602
3.58k
  if (is_method)
1603
3.45k
    ++num_params;
1604
3.58k
  if (num_params == 1)
1605
352
    return unary;
1606
3.23k
  if (num_params == 2)
1607
3.13k
    return binary;
1608
98
  else
1609
98
    return false;
1610
3.23k
}
1611
1612
bool TypeSystemClang::CheckOverloadedOperatorKindParameterCount(
1613
    bool is_method, clang::OverloadedOperatorKind op_kind,
1614
3.92k
    uint32_t num_params) {
1615
3.92k
  switch (op_kind) {
1616
3.82k
  default:
1617
3.82k
    break;
1618
0
  // C++ standard allows any number of arguments to new/delete
1619
96
  case OO_New:
1620
96
  case OO_Array_New:
1621
96
  case OO_Delete:
1622
96
  case OO_Array_Delete:
1623
96
    return true;
1624
3.82k
  }
1625
3.82k
1626
3.82k
#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly)  \
1627
3.82k
  case OO_##Name:                                                              \
1628
3.82k
    return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1629
3.82k
  switch (op_kind) {
1630
0
#include "clang/Basic/OperatorKinds.def"
1631
0
  default:
1632
0
    break;
1633
0
  }
1634
0
  return false;
1635
0
}
1636
1637
clang::AccessSpecifier
1638
TypeSystemClang::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1639
521
                                       clang::AccessSpecifier rhs) {
1640
521
  // Make the access equal to the stricter of the field and the nested field's
1641
521
  // access
1642
521
  if (lhs == AS_none || 
rhs == AS_none518
)
1643
6
    return AS_none;
1644
515
  if (lhs == AS_private || 
rhs == AS_private512
)
1645
5
    return AS_private;
1646
510
  if (lhs == AS_protected || 
rhs == AS_protected508
)
1647
3
    return AS_protected;
1648
507
  return AS_public;
1649
507
}
1650
1651
bool TypeSystemClang::FieldIsBitfield(FieldDecl *field,
1652
59.5k
                                      uint32_t &bitfield_bit_size) {
1653
59.5k
  ASTContext &ast = getASTContext();
1654
59.5k
  if (field == nullptr)
1655
0
    return false;
1656
59.5k
1657
59.5k
  if (field->isBitField()) {
1658
270
    Expr *bit_width_expr = field->getBitWidth();
1659
270
    if (bit_width_expr) {
1660
270
      llvm::APSInt bit_width_apsint;
1661
270
      if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, ast)) {
1662
270
        bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
1663
270
        return true;
1664
270
      }
1665
59.3k
    }
1666
270
  }
1667
59.3k
  return false;
1668
59.3k
}
1669
1670
25.2k
bool TypeSystemClang::RecordHasFields(const RecordDecl *record_decl) {
1671
25.2k
  if (record_decl == nullptr)
1672
0
    return false;
1673
25.2k
1674
25.2k
  if (!record_decl->field_empty())
1675
8.81k
    return true;
1676
16.4k
1677
16.4k
  // No fields, lets check this is a CXX record and check the base classes
1678
16.4k
  const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1679
16.4k
  if (cxx_record_decl) {
1680
16.4k
    CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1681
16.4k
    for (base_class = cxx_record_decl->bases_begin(),
1682
16.4k
        base_class_end = cxx_record_decl->bases_end();
1683
19.2k
         base_class != base_class_end; 
++base_class2.76k
) {
1684
10.4k
      const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1685
10.4k
          base_class->getType()->getAs<RecordType>()->getDecl());
1686
10.4k
      if (RecordHasFields(base_class_decl))
1687
7.64k
        return true;
1688
10.4k
    }
1689
16.4k
  }
1690
16.4k
  
return false8.82k
;
1691
16.4k
}
1692
1693
#pragma mark Objective-C Classes
1694
1695
CompilerType TypeSystemClang::CreateObjCClass(
1696
    llvm::StringRef name, clang::DeclContext *decl_ctx,
1697
    OptionalClangModuleID owning_module, bool isForwardDecl, bool isInternal,
1698
1.29k
    ClangASTMetadata *metadata) {
1699
1.29k
  ASTContext &ast = getASTContext();
1700
1.29k
  assert(!name.empty());
1701
1.29k
  if (!decl_ctx)
1702
0
    decl_ctx = ast.getTranslationUnitDecl();
1703
1.29k
1704
1.29k
  ObjCInterfaceDecl *decl = ObjCInterfaceDecl::CreateDeserialized(ast, 0);
1705
1.29k
  decl->setDeclContext(decl_ctx);
1706
1.29k
  decl->setDeclName(&ast.Idents.get(name));
1707
1.29k
  /*isForwardDecl,*/
1708
1.29k
  decl->setImplicit(isInternal);
1709
1.29k
  SetOwningModule(decl, owning_module);
1710
1.29k
1711
1.29k
  if (decl && metadata)
1712
1.29k
    SetMetadata(decl, *metadata);
1713
1.29k
1714
1.29k
  return GetType(ast.getObjCInterfaceType(decl));
1715
1.29k
}
1716
1717
4.06k
static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1718
4.06k
  return !TypeSystemClang::RecordHasFields(b->getType()->getAsCXXRecordDecl());
1719
4.06k
}
1720
1721
uint32_t
1722
TypeSystemClang::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1723
9.44k
                                   bool omit_empty_base_classes) {
1724
9.44k
  uint32_t num_bases = 0;
1725
9.44k
  if (cxx_record_decl) {
1726
9.44k
    if (omit_empty_base_classes) {
1727
9.44k
      CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1728
9.44k
      for (base_class = cxx_record_decl->bases_begin(),
1729
9.44k
          base_class_end = cxx_record_decl->bases_end();
1730
10.7k
           base_class != base_class_end; 
++base_class1.35k
) {
1731
1.35k
        // Skip empty base classes
1732
1.35k
        if (BaseSpecifierIsEmpty(base_class))
1733
951
          continue;
1734
403
        ++num_bases;
1735
403
      }
1736
9.44k
    } else
1737
2
      num_bases = cxx_record_decl->getNumBases();
1738
9.44k
  }
1739
9.44k
  return num_bases;
1740
9.44k
}
1741
1742
#pragma mark Namespace Declarations
1743
1744
NamespaceDecl *TypeSystemClang::GetUniqueNamespaceDeclaration(
1745
    const char *name, clang::DeclContext *decl_ctx,
1746
7.65k
    OptionalClangModuleID owning_module, bool is_inline) {
1747
7.65k
  NamespaceDecl *namespace_decl = nullptr;
1748
7.65k
  ASTContext &ast = getASTContext();
1749
7.65k
  TranslationUnitDecl *translation_unit_decl = ast.getTranslationUnitDecl();
1750
7.65k
  if (!decl_ctx)
1751
5.84k
    decl_ctx = translation_unit_decl;
1752
7.65k
1753
7.65k
  if (name) {
1754
7.64k
    IdentifierInfo &identifier_info = ast.Idents.get(name);
1755
7.64k
    DeclarationName decl_name(&identifier_info);
1756
7.64k
    clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1757
7.64k
    for (NamedDecl *decl : result) {
1758
265
      namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1759
265
      if (namespace_decl)
1760
265
        return namespace_decl;
1761
265
    }
1762
7.64k
1763
7.64k
    namespace_decl =
1764
7.38k
        NamespaceDecl::Create(ast, decl_ctx, is_inline, SourceLocation(),
1765
7.38k
                              SourceLocation(), &identifier_info, nullptr);
1766
7.38k
1767
7.38k
    decl_ctx->addDecl(namespace_decl);
1768
7.38k
  } else {
1769
9
    if (decl_ctx == translation_unit_decl) {
1770
6
      namespace_decl = translation_unit_decl->getAnonymousNamespace();
1771
6
      if (namespace_decl)
1772
0
        return namespace_decl;
1773
6
1774
6
      namespace_decl =
1775
6
          NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1776
6
                                SourceLocation(), nullptr, nullptr);
1777
6
      translation_unit_decl->setAnonymousNamespace(namespace_decl);
1778
6
      translation_unit_decl->addDecl(namespace_decl);
1779
6
      assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1780
6
    } else {
1781
3
      NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1782
3
      if (parent_namespace_decl) {
1783
3
        namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1784
3
        if (namespace_decl)
1785
0
          return namespace_decl;
1786
3
        namespace_decl =
1787
3
            NamespaceDecl::Create(ast, decl_ctx, false, SourceLocation(),
1788
3
                                  SourceLocation(), nullptr, nullptr);
1789
3
        parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1790
3
        parent_namespace_decl->addDecl(namespace_decl);
1791
3
        assert(namespace_decl ==
1792
3
               parent_namespace_decl->getAnonymousNamespace());
1793
3
      } else {
1794
0
        assert(false && "GetUniqueNamespaceDeclaration called with no name and "
1795
0
                        "no namespace as decl_ctx");
1796
0
      }
1797
3
    }
1798
9
  }
1799
7.65k
  // Note: namespaces can span multiple modules, so perhaps this isn't a good
1800
7.65k
  // idea.
1801
7.65k
  SetOwningModule(namespace_decl, owning_module);
1802
7.39k
1803
7.39k
  VerifyDecl(namespace_decl);
1804
7.39k
  return namespace_decl;
1805
7.65k
}
1806
1807
clang::BlockDecl *
1808
TypeSystemClang::CreateBlockDeclaration(clang::DeclContext *ctx,
1809
97
                                        OptionalClangModuleID owning_module) {
1810
97
  if (ctx) {
1811
97
    clang::BlockDecl *decl =
1812
97
        clang::BlockDecl::CreateDeserialized(getASTContext(), 0);
1813
97
    decl->setDeclContext(ctx);
1814
97
    ctx->addDecl(decl);
1815
97
    SetOwningModule(decl, owning_module);
1816
97
    return decl;
1817
97
  }
1818
0
  return nullptr;
1819
0
}
1820
1821
clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1822
                                        clang::DeclContext *right,
1823
48
                                        clang::DeclContext *root) {
1824
48
  if (root == nullptr)
1825
0
    return nullptr;
1826
48
1827
48
  std::set<clang::DeclContext *> path_left;
1828
144
  for (clang::DeclContext *d = left; d != nullptr; 
d = d->getParent()96
)
1829
96
    path_left.insert(d);
1830
48
1831
102
  for (clang::DeclContext *d = right; d != nullptr; 
d = d->getParent()54
)
1832
102
    if (path_left.find(d) != path_left.end())
1833
48
      return d;
1834
48
1835
48
  
return nullptr0
;
1836
48
}
1837
1838
clang::UsingDirectiveDecl *TypeSystemClang::CreateUsingDirectiveDeclaration(
1839
    clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1840
48
    clang::NamespaceDecl *ns_decl) {
1841
48
  if (decl_ctx && ns_decl) {
1842
48
    auto *translation_unit = getASTContext().getTranslationUnitDecl();
1843
48
    clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1844
48
          getASTContext(), decl_ctx, clang::SourceLocation(),
1845
48
          clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1846
48
          clang::SourceLocation(), ns_decl,
1847
48
          FindLCABetweenDecls(decl_ctx, ns_decl,
1848
48
                              translation_unit));
1849
48
      decl_ctx->addDecl(using_decl);
1850
48
      SetOwningModule(using_decl, owning_module);
1851
48
      return using_decl;
1852
48
  }
1853
0
  return nullptr;
1854
0
}
1855
1856
clang::UsingDecl *
1857
TypeSystemClang::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1858
                                        OptionalClangModuleID owning_module,
1859
15
                                        clang::NamedDecl *target) {
1860
15
  if (current_decl_ctx && target) {
1861
15
    clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1862
15
        getASTContext(), current_decl_ctx, clang::SourceLocation(),
1863
15
        clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1864
15
    SetOwningModule(using_decl, owning_module);
1865
15
    clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1866
15
        getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1867
15
        target);
1868
15
    SetOwningModule(shadow_decl, owning_module);
1869
15
    using_decl->addShadowDecl(shadow_decl);
1870
15
    current_decl_ctx->addDecl(using_decl);
1871
15
    return using_decl;
1872
15
  }
1873
0
  return nullptr;
1874
0
}
1875
1876
clang::VarDecl *TypeSystemClang::CreateVariableDeclaration(
1877
    clang::DeclContext *decl_context, OptionalClangModuleID owning_module,
1878
8.39k
    const char *name, clang::QualType type) {
1879
8.39k
  if (decl_context) {
1880
8.39k
    clang::VarDecl *var_decl =
1881
8.39k
        clang::VarDecl::CreateDeserialized(getASTContext(), 0);
1882
8.39k
    var_decl->setDeclContext(decl_context);
1883
8.39k
    if (name && 
name[0]8.35k
)
1884
8.35k
      var_decl->setDeclName(&getASTContext().Idents.getOwn(name));
1885
8.39k
    var_decl->setType(type);
1886
8.39k
    SetOwningModule(var_decl, owning_module);
1887
8.39k
    var_decl->setAccess(clang::AS_public);
1888
8.39k
    decl_context->addDecl(var_decl);
1889
8.39k
    return var_decl;
1890
8.39k
  }
1891
0
  return nullptr;
1892
0
}
1893
1894
lldb::opaque_compiler_type_t
1895
TypeSystemClang::GetOpaqueCompilerType(clang::ASTContext *ast,
1896
29.7k
                                       lldb::BasicType basic_type) {
1897
29.7k
  switch (basic_type) {
1898
28.2k
  case eBasicTypeVoid:
1899
28.2k
    return ast->VoidTy.getAsOpaquePtr();
1900
115
  case eBasicTypeChar:
1901
115
    return ast->CharTy.getAsOpaquePtr();
1902
3
  case eBasicTypeSignedChar:
1903
3
    return ast->SignedCharTy.getAsOpaquePtr();
1904
3
  case eBasicTypeUnsignedChar:
1905
3
    return ast->UnsignedCharTy.getAsOpaquePtr();
1906
63
  case eBasicTypeWChar:
1907
63
    return ast->getWCharType().getAsOpaquePtr();
1908
2
  case eBasicTypeSignedWChar:
1909
2
    return ast->getSignedWCharType().getAsOpaquePtr();
1910
2
  case eBasicTypeUnsignedWChar:
1911
2
    return ast->getUnsignedWCharType().getAsOpaquePtr();
1912
1
  case eBasicTypeChar16:
1913
1
    return ast->Char16Ty.getAsOpaquePtr();
1914
1
  case eBasicTypeChar32:
1915
1
    return ast->Char32Ty.getAsOpaquePtr();
1916
5
  case eBasicTypeShort:
1917
5
    return ast->ShortTy.getAsOpaquePtr();
1918
5
  case eBasicTypeUnsignedShort:
1919
5
    return ast->UnsignedShortTy.getAsOpaquePtr();
1920
55
  case eBasicTypeInt:
1921
55
    return ast->IntTy.getAsOpaquePtr();
1922
7
  case eBasicTypeUnsignedInt:
1923
7
    return ast->UnsignedIntTy.getAsOpaquePtr();
1924
9
  case eBasicTypeLong:
1925
9
    return ast->LongTy.getAsOpaquePtr();
1926
7
  case eBasicTypeUnsignedLong:
1927
7
    return ast->UnsignedLongTy.getAsOpaquePtr();
1928
7
  case eBasicTypeLongLong:
1929
7
    return ast->LongLongTy.getAsOpaquePtr();
1930
7
  case eBasicTypeUnsignedLongLong:
1931
7
    return ast->UnsignedLongLongTy.getAsOpaquePtr();
1932
3
  case eBasicTypeInt128:
1933
3
    return ast->Int128Ty.getAsOpaquePtr();
1934
3
  case eBasicTypeUnsignedInt128:
1935
3
    return ast->UnsignedInt128Ty.getAsOpaquePtr();
1936
181
  case eBasicTypeBool:
1937
181
    return ast->BoolTy.getAsOpaquePtr();
1938
1
  case eBasicTypeHalf:
1939
1
    return ast->HalfTy.getAsOpaquePtr();
1940
3
  case eBasicTypeFloat:
1941
3
    return ast->FloatTy.getAsOpaquePtr();
1942
5
  case eBasicTypeDouble:
1943
5
    return ast->DoubleTy.getAsOpaquePtr();
1944
3
  case eBasicTypeLongDouble:
1945
3
    return ast->LongDoubleTy.getAsOpaquePtr();
1946
1
  case eBasicTypeFloatComplex:
1947
1
    return ast->FloatComplexTy.getAsOpaquePtr();
1948
1
  case eBasicTypeDoubleComplex:
1949
1
    return ast->DoubleComplexTy.getAsOpaquePtr();
1950
1
  case eBasicTypeLongDoubleComplex:
1951
1
    return ast->LongDoubleComplexTy.getAsOpaquePtr();
1952
519
  case eBasicTypeObjCID:
1953
519
    return ast->getObjCIdType().getAsOpaquePtr();
1954
247
  case eBasicTypeObjCClass:
1955
247
    return ast->getObjCClassType().getAsOpaquePtr();
1956
77
  case eBasicTypeObjCSel:
1957
77
    return ast->getObjCSelType().getAsOpaquePtr();
1958
136
  case eBasicTypeNullPtr:
1959
136
    return ast->NullPtrTy.getAsOpaquePtr();
1960
1
  default:
1961
1
    return nullptr;
1962
29.7k
  }
1963
29.7k
}
1964
1965
#pragma mark Function Types
1966
1967
clang::DeclarationName
1968
TypeSystemClang::GetDeclarationName(const char *name,
1969
6.49k
                                    const CompilerType &function_clang_type) {
1970
6.49k
  if (!name || !name[0])
1971
0
    return clang::DeclarationName();
1972
6.49k
1973
6.49k
  clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1974
6.49k
  if (!IsOperator(name, op_kind) || 
op_kind == clang::NUM_OVERLOADED_OPERATORS41
)
1975
6.45k
    return DeclarationName(&getASTContext().Idents.get(
1976
6.45k
        name)); // Not operator, but a regular function.
1977
41
1978
41
  // Check the number of operator parameters. Sometimes we have seen bad DWARF
1979
41
  // that doesn't correctly describe operators and if we try to create a method
1980
41
  // and add it to the class, clang will assert and crash, so we need to make
1981
41
  // sure things are acceptable.
1982
41
  clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1983
41
  const clang::FunctionProtoType *function_type =
1984
41
      llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1985
41
  if (function_type == nullptr)
1986
0
    return clang::DeclarationName();
1987
41
1988
41
  const bool is_method = false;
1989
41
  const unsigned int num_params = function_type->getNumParams();
1990
41
  if (!TypeSystemClang::CheckOverloadedOperatorKindParameterCount(
1991
41
          is_method, op_kind, num_params))
1992
12
    return clang::DeclarationName();
1993
29
1994
29
  return getASTContext().DeclarationNames.getCXXOperatorName(op_kind);
1995
29
}
1996
1997
FunctionDecl *TypeSystemClang::CreateFunctionDeclaration(
1998
    clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
1999
    const char *name, const CompilerType &function_clang_type, int storage,
2000
6.49k
    bool is_inline) {
2001
6.49k
  FunctionDecl *func_decl = nullptr;
2002
6.49k
  ASTContext &ast = getASTContext();
2003
6.49k
  if (!decl_ctx)
2004
0
    decl_ctx = ast.getTranslationUnitDecl();
2005
6.49k
2006
6.49k
  const bool hasWrittenPrototype = true;
2007
6.49k
  const bool isConstexprSpecified = false;
2008
6.49k
2009
6.49k
  clang::DeclarationName declarationName =
2010
6.49k
      GetDeclarationName(name, function_clang_type);
2011
6.49k
  func_decl = FunctionDecl::CreateDeserialized(ast, 0);
2012
6.49k
  func_decl->setDeclContext(decl_ctx);
2013
6.49k
  func_decl->setDeclName(declarationName);
2014
6.49k
  func_decl->setType(ClangUtil::GetQualType(function_clang_type));
2015
6.49k
  func_decl->setStorageClass(static_cast<clang::StorageClass>(storage));
2016
6.49k
  func_decl->setInlineSpecified(is_inline);
2017
6.49k
  func_decl->setHasWrittenPrototype(hasWrittenPrototype);
2018
6.49k
  func_decl->setConstexprKind(isConstexprSpecified ? 
CSK_constexpr0
2019
6.49k
                                                   : CSK_unspecified);
2020
6.49k
  SetOwningModule(func_decl, owning_module);
2021
6.49k
  if (func_decl)
2022
6.49k
    decl_ctx->addDecl(func_decl);
2023
6.49k
2024
6.49k
  VerifyDecl(func_decl);
2025
6.49k
2026
6.49k
  return func_decl;
2027
6.49k
}
2028
2029
CompilerType
2030
TypeSystemClang::CreateFunctionType(const CompilerType &result_type,
2031
                                    const CompilerType *args, unsigned num_args,
2032
                                    bool is_variadic, unsigned type_quals,
2033
56.7k
                                    clang::CallingConv cc) {
2034
56.7k
  if (!result_type || !ClangUtil::IsClangType(result_type))
2035
0
    return CompilerType(); // invalid return type
2036
56.7k
2037
56.7k
  std::vector<QualType> qual_type_args;
2038
56.7k
  if (num_args > 0 && 
args == nullptr35.5k
)
2039
0
    return CompilerType(); // invalid argument array passed in
2040
56.7k
2041
56.7k
  // Verify that all arguments are valid and the right type
2042
115k
  
for (unsigned i = 0; 56.7k
i < num_args;
++i58.3k
) {
2043
58.3k
    if (args[i]) {
2044
58.3k
      // Make sure we have a clang type in args[i] and not a type from another
2045
58.3k
      // language whose name might match
2046
58.3k
      const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2047
58.3k
      lldbassert(is_clang_type);
2048
58.3k
      if (is_clang_type)
2049
58.3k
        qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2050
0
      else
2051
0
        return CompilerType(); //  invalid argument type (must be a clang type)
2052
0
    } else
2053
0
      return CompilerType(); // invalid argument type (empty)
2054
58.3k
  }
2055
56.7k
2056
56.7k
  // TODO: Detect calling convention in DWARF?
2057
56.7k
  FunctionProtoType::ExtProtoInfo proto_info;
2058
56.7k
  proto_info.ExtInfo = cc;
2059
56.7k
  proto_info.Variadic = is_variadic;
2060
56.7k
  proto_info.ExceptionSpec = EST_None;
2061
56.7k
  proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals);
2062
56.7k
  proto_info.RefQualifier = RQ_None;
2063
56.7k
2064
56.7k
  return GetType(getASTContext().getFunctionType(
2065
56.7k
      ClangUtil::GetQualType(result_type), qual_type_args, proto_info));
2066
56.7k
}
2067
2068
ParmVarDecl *TypeSystemClang::CreateParameterDeclaration(
2069
    clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
2070
    const char *name, const CompilerType &param_type, int storage,
2071
58.1k
    bool add_decl) {
2072
58.1k
  ASTContext &ast = getASTContext();
2073
58.1k
  auto *decl = ParmVarDecl::CreateDeserialized(ast, 0);
2074
58.1k
  decl->setDeclContext(decl_ctx);
2075
58.1k
  if (name && 
name[0]2.65k
)
2076
2.65k
    decl->setDeclName(&ast.Idents.get(name));
2077
58.1k
  decl->setType(ClangUtil::GetQualType(param_type));
2078
58.1k
  decl->setStorageClass(static_cast<clang::StorageClass>(storage));
2079
58.1k
  SetOwningModule(decl, owning_module);
2080
58.1k
  if (add_decl)
2081
0
    decl_ctx->addDecl(decl);
2082
58.1k
2083
58.1k
  return decl;
2084
58.1k
}
2085
2086
void TypeSystemClang::SetFunctionParameters(FunctionDecl *function_decl,
2087
                                            ParmVarDecl **params,
2088
5.29k
                                            unsigned num_params) {
2089
5.29k
  if (function_decl)
2090
5.29k
    function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
2091
5.29k
}
2092
2093
CompilerType
2094
9
TypeSystemClang::CreateBlockPointerType(const CompilerType &function_type) {
2095
9
  QualType block_type = m_ast_up->getBlockPointerType(
2096
9
      clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
2097
9
2098
9
  return GetType(block_type);
2099
9
}
2100
2101
#pragma mark Array Types
2102
2103
CompilerType TypeSystemClang::CreateArrayType(const CompilerType &element_type,
2104
                                              size_t element_count,
2105
869
                                              bool is_vector) {
2106
869
  if (element_type.IsValid()) {
2107
869
    ASTContext &ast = getASTContext();
2108
869
2109
869
    if (is_vector) {
2110
43
      return GetType(ast.getExtVectorType(ClangUtil::GetQualType(element_type),
2111
43
                                          element_count));
2112
826
    } else {
2113
826
2114
826
      llvm::APInt ap_element_count(64, element_count);
2115
826
      if (element_count == 0) {
2116
34
        return GetType(ast.getIncompleteArrayType(
2117
34
            ClangUtil::GetQualType(element_type), clang::ArrayType::Normal, 0));
2118
792
      } else {
2119
792
        return GetType(ast.getConstantArrayType(
2120
792
            ClangUtil::GetQualType(element_type), ap_element_count, nullptr,
2121
792
            clang::ArrayType::Normal, 0));
2122
792
      }
2123
0
    }
2124
869
  }
2125
0
  return CompilerType();
2126
0
}
2127
2128
CompilerType TypeSystemClang::CreateStructForIdentifier(
2129
    ConstString type_name,
2130
    const std::initializer_list<std::pair<const char *, CompilerType>>
2131
        &type_fields,
2132
157
    bool packed) {
2133
157
  CompilerType type;
2134
157
  if (!type_name.IsEmpty() &&
2135
157
      (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2136
7
          .IsValid()) {
2137
0
    lldbassert(0 && "Trying to create a type for an existing name");
2138
0
    return type;
2139
0
  }
2140
157
2141
157
  type = CreateRecordType(nullptr, OptionalClangModuleID(), lldb::eAccessPublic,
2142
157
                          type_name.GetCString(), clang::TTK_Struct,
2143
157
                          lldb::eLanguageTypeC);
2144
157
  StartTagDeclarationDefinition(type);
2145
157
  for (const auto &field : type_fields)
2146
771
    AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2147
771
                         0);
2148
157
  if (packed)
2149
0
    SetIsPacked(type);
2150
157
  CompleteTagDeclarationDefinition(type);
2151
157
  return type;
2152
157
}
2153
2154
CompilerType TypeSystemClang::GetOrCreateStructForIdentifier(
2155
    ConstString type_name,
2156
    const std::initializer_list<std::pair<const char *, CompilerType>>
2157
        &type_fields,
2158
69
    bool packed) {
2159
69
  CompilerType type;
2160
69
  if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2161
62
    return type;
2162
7
2163
7
  return CreateStructForIdentifier(type_name, type_fields, packed);
2164
7
}
2165
2166
#pragma mark Enumeration Types
2167
2168
CompilerType TypeSystemClang::CreateEnumerationType(
2169
    const char *name, clang::DeclContext *decl_ctx,
2170
    OptionalClangModuleID owning_module, const Declaration &decl,
2171
103
    const CompilerType &integer_clang_type, bool is_scoped) {
2172
103
  // TODO: Do something intelligent with the Declaration object passed in
2173
103
  // like maybe filling in the SourceLocation with it...
2174
103
  ASTContext &ast = getASTContext();
2175
103
2176
103
  // TODO: ask about these...
2177
103
  //    const bool IsFixed = false;
2178
103
  EnumDecl *enum_decl = EnumDecl::CreateDeserialized(ast, 0);
2179
103
  enum_decl->setDeclContext(decl_ctx);
2180
103
  if (name && 
name[0]98
)
2181
98
    enum_decl->setDeclName(&ast.Idents.get(name));
2182
103
  enum_decl->setScoped(is_scoped);
2183
103
  enum_decl->setScopedUsingClassTag(is_scoped);
2184
103
  enum_decl->setFixed(false);
2185
103
  SetOwningModule(enum_decl, owning_module);
2186
103
  if (enum_decl) {
2187
103
    if (decl_ctx)
2188
103
      decl_ctx->addDecl(enum_decl);
2189
103
2190
103
    // TODO: check if we should be setting the promotion type too?
2191
103
    enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2192
103
2193
103
    enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2194
103
2195
103
    return GetType(ast.getTagDeclType(enum_decl));
2196
103
  }
2197
0
  return CompilerType();
2198
0
}
2199
2200
CompilerType TypeSystemClang::GetIntTypeFromBitSize(size_t bit_size,
2201
30
                                                    bool is_signed) {
2202
30
  clang::ASTContext &ast = getASTContext();
2203
30
2204
30
  if (is_signed) {
2205
0
    if (bit_size == ast.getTypeSize(ast.SignedCharTy))
2206
0
      return GetType(ast.SignedCharTy);
2207
0
2208
0
    if (bit_size == ast.getTypeSize(ast.ShortTy))
2209
0
      return GetType(ast.ShortTy);
2210
0
2211
0
    if (bit_size == ast.getTypeSize(ast.IntTy))
2212
0
      return GetType(ast.IntTy);
2213
0
2214
0
    if (bit_size == ast.getTypeSize(ast.LongTy))
2215
0
      return GetType(ast.LongTy);
2216
0
2217
0
    if (bit_size == ast.getTypeSize(ast.LongLongTy))
2218
0
      return GetType(ast.LongLongTy);
2219
0
2220
0
    if (bit_size == ast.getTypeSize(ast.Int128Ty))
2221
0
      return GetType(ast.Int128Ty);
2222
30
  } else {
2223
30
    if (bit_size == ast.getTypeSize(ast.UnsignedCharTy))
2224
0
      return GetType(ast.UnsignedCharTy);
2225
30
2226
30
    if (bit_size == ast.getTypeSize(ast.UnsignedShortTy))
2227
0
      return GetType(ast.UnsignedShortTy);
2228
30
2229
30
    if (bit_size == ast.getTypeSize(ast.UnsignedIntTy))
2230
0
      return GetType(ast.UnsignedIntTy);
2231
30
2232
30
    if (bit_size == ast.getTypeSize(ast.UnsignedLongTy))
2233
30
      return GetType(ast.UnsignedLongTy);
2234
0
2235
0
    if (bit_size == ast.getTypeSize(ast.UnsignedLongLongTy))
2236
0
      return GetType(ast.UnsignedLongLongTy);
2237
0
2238
0
    if (bit_size == ast.getTypeSize(ast.UnsignedInt128Ty))
2239
0
      return GetType(ast.UnsignedInt128Ty);
2240
0
  }
2241
0
  return CompilerType();
2242
0
}
2243
2244
30
CompilerType TypeSystemClang::GetPointerSizedIntType(bool is_signed) {
2245
30
  return GetIntTypeFromBitSize(
2246
30
      getASTContext().getTypeSize(getASTContext().VoidPtrTy), is_signed);
2247
30
}
2248
2249
0
void TypeSystemClang::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2250
0
  if (decl_ctx) {
2251
0
    DumpDeclContextHiearchy(decl_ctx->getParent());
2252
0
2253
0
    clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2254
0
    if (named_decl) {
2255
0
      printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2256
0
             named_decl->getDeclName().getAsString().c_str());
2257
0
    } else {
2258
0
      printf("%20s\n", decl_ctx->getDeclKindName());
2259
0
    }
2260
0
  }
2261
0
}
2262
2263
0
void TypeSystemClang::DumpDeclHiearchy(clang::Decl *decl) {
2264
0
  if (decl == nullptr)
2265
0
    return;
2266
0
  DumpDeclContextHiearchy(decl->getDeclContext());
2267
0
2268
0
  clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2269
0
  if (record_decl) {
2270
0
    printf("%20s: %s%s\n", decl->getDeclKindName(),
2271
0
           record_decl->getDeclName().getAsString().c_str(),
2272
0
           record_decl->isInjectedClassName() ? " (injected class name)" : "");
2273
0
2274
0
  } else {
2275
0
    clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2276
0
    if (named_decl) {
2277
0
      printf("%20s: %s\n", decl->getDeclKindName(),
2278
0
             named_decl->getDeclName().getAsString().c_str());
2279
0
    } else {
2280
0
      printf("%20s\n", decl->getDeclKindName());
2281
0
    }
2282
0
  }
2283
0
}
2284
2285
bool TypeSystemClang::DeclsAreEquivalent(clang::Decl *lhs_decl,
2286
9
                                         clang::Decl *rhs_decl) {
2287
9
  if (lhs_decl && rhs_decl) {
2288
9
    // Make sure the decl kinds match first
2289
9
    const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2290
9
    const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
2291
9
2292
9
    if (lhs_decl_kind == rhs_decl_kind) {
2293
9
      // Now check that the decl contexts kinds are all equivalent before we
2294
9
      // have to check any names of the decl contexts...
2295
9
      clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2296
9
      clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2297
9
      if (lhs_decl_ctx && rhs_decl_ctx) {
2298
9
        while (true) {
2299
9
          if (lhs_decl_ctx && rhs_decl_ctx) {
2300
9
            const clang::Decl::Kind lhs_decl_ctx_kind =
2301
9
                lhs_decl_ctx->getDeclKind();
2302
9
            const clang::Decl::Kind rhs_decl_ctx_kind =
2303
9
                rhs_decl_ctx->getDeclKind();
2304
9
            if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2305
9
              lhs_decl_ctx = lhs_decl_ctx->getParent();
2306
9
              rhs_decl_ctx = rhs_decl_ctx->getParent();
2307
9
2308
9
              if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2309
9
                break;
2310
0
            } else
2311
0
              return false;
2312
0
          } else
2313
0
            return false;
2314
9
        }
2315
9
2316
9
        // Now make sure the name of the decls match
2317
9
        clang::NamedDecl *lhs_named_decl =
2318
9
            llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2319
9
        clang::NamedDecl *rhs_named_decl =
2320
9
            llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2321
9
        if (lhs_named_decl && rhs_named_decl) {
2322
9
          clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2323
9
          clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2324
9
          if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2325
9
            if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2326
0
              return false;
2327
0
          } else
2328
0
            return false;
2329
0
        } else
2330
0
          return false;
2331
9
2332
9
        // We know that the decl context kinds all match, so now we need to
2333
9
        // make sure the names match as well
2334
9
        lhs_decl_ctx = lhs_decl->getDeclContext();
2335
9
        rhs_decl_ctx = rhs_decl->getDeclContext();
2336
9
        while (true) {
2337
9
          switch (lhs_decl_ctx->getDeclKind()) {
2338
9
          case clang::Decl::TranslationUnit:
2339
9
            // We don't care about the translation unit names
2340
9
            return true;
2341
0
          default: {
2342
0
            clang::NamedDecl *lhs_named_decl =
2343
0
                llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2344
0
            clang::NamedDecl *rhs_named_decl =
2345
0
                llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2346
0
            if (lhs_named_decl && rhs_named_decl) {
2347
0
              clang::DeclarationName lhs_decl_name =
2348
0
                  lhs_named_decl->getDeclName();
2349
0
              clang::DeclarationName rhs_decl_name =
2350
0
                  rhs_named_decl->getDeclName();
2351
0
              if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2352
0
                if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2353
0
                  return false;
2354
0
              } else
2355
0
                return false;
2356
0
            } else
2357
0
              return false;
2358
0
          } break;
2359
0
          }
2360
0
          lhs_decl_ctx = lhs_decl_ctx->getParent();
2361
0
          rhs_decl_ctx = rhs_decl_ctx->getParent();
2362
0
        }
2363
9
      }
2364
9
    }
2365
9
  }
2366
9
  
return false0
;
2367
9
}
2368
bool TypeSystemClang::GetCompleteDecl(clang::ASTContext *ast,
2369
13.8k
                                      clang::Decl *decl) {
2370
13.8k
  if (!decl)
2371
0
    return false;
2372
13.8k
2373
13.8k
  ExternalASTSource *ast_source = ast->getExternalSource();
2374
13.8k
2375
13.8k
  if (!ast_source)
2376
0
    return false;
2377
13.8k
2378
13.8k
  if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2379
10.8k
    if (tag_decl->isCompleteDefinition())
2380
10.5k
      return true;
2381
297
2382
297
    if (!tag_decl->hasExternalLexicalStorage())
2383
79
      return false;
2384
218
2385
218
    ast_source->CompleteType(tag_decl);
2386
218
2387
218
    return !tag_decl->getTypeForDecl()->isIncompleteType();
2388
3.06k
  } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2389
3.06k
                 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2390
3.06k
    if (objc_interface_decl->getDefinition())
2391
2.90k
      return true;
2392
160
2393
160
    if (!objc_interface_decl->hasExternalLexicalStorage())
2394
0
      return false;
2395
160
2396
160
    ast_source->CompleteType(objc_interface_decl);
2397
160
2398
160
    return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2399
160
  } else {
2400
0
    return false;
2401
0
  }
2402
13.8k
}
2403
2404
void TypeSystemClang::SetMetadataAsUserID(const clang::Decl *decl,
2405
66.3k
                                          user_id_t user_id) {
2406
66.3k
  ClangASTMetadata meta_data;
2407
66.3k
  meta_data.SetUserID(user_id);
2408
66.3k
  SetMetadata(decl, meta_data);
2409
66.3k
}
2410
2411
void TypeSystemClang::SetMetadataAsUserID(const clang::Type *type,
2412
824
                                          user_id_t user_id) {
2413
824
  ClangASTMetadata meta_data;
2414
824
  meta_data.SetUserID(user_id);
2415
824
  SetMetadata(type, meta_data);
2416
824
}
2417
2418
void TypeSystemClang::SetMetadata(const clang::Decl *object,
2419
140k
                                  ClangASTMetadata &metadata) {
2420
140k
  m_decl_metadata[object] = metadata;
2421
140k
}
2422
2423
void TypeSystemClang::SetMetadata(const clang::Type *object,
2424
824
                                  ClangASTMetadata &metadata) {
2425
824
  m_type_metadata[object] = metadata;
2426
824
}
2427
2428
382k
ClangASTMetadata *TypeSystemClang::GetMetadata(const clang::Decl *object) {
2429
382k
  auto It = m_decl_metadata.find(object);
2430
382k
  if (It != m_decl_metadata.end())
2431
196k
    return &It->second;
2432
185k
  return nullptr;
2433
185k
}
2434
2435
39
ClangASTMetadata *TypeSystemClang::GetMetadata(const clang::Type *object) {
2436
39
  auto It = m_type_metadata.find(object);
2437
39
  if (It != m_type_metadata.end())
2438
39
    return &It->second;
2439
0
  return nullptr;
2440
0
}
2441
2442
bool TypeSystemClang::SetTagTypeKind(clang::QualType tag_qual_type,
2443
675
                                     int kind) const {
2444
675
  const clang::Type *clang_type = tag_qual_type.getTypePtr();
2445
675
  if (clang_type) {
2446
675
    const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2447
675
    if (tag_type) {
2448
675
      clang::TagDecl *tag_decl =
2449
675
          llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2450
675
      if (tag_decl) {
2451
675
        tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2452
675
        return true;
2453
675
      }
2454
0
    }
2455
675
  }
2456
0
  return false;
2457
0
}
2458
2459
bool TypeSystemClang::SetDefaultAccessForRecordFields(
2460
    clang::RecordDecl *record_decl, int default_accessibility,
2461
511
    int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2462
511
  if (record_decl) {
2463
286
    uint32_t field_idx;
2464
286
    clang::RecordDecl::field_iterator field, field_end;
2465
286
    for (field = record_decl->field_begin(),
2466
286
        field_end = record_decl->field_end(), field_idx = 0;
2467
800
         field != field_end; 
++field, ++field_idx514
) {
2468
514
      // If no accessibility was assigned, assign the correct one
2469
514
      if (field_idx < num_assigned_accessibilities &&
2470
514
          assigned_accessibilities[field_idx] == clang::AS_none)
2471
249
        field->setAccess((clang::AccessSpecifier)default_accessibility);
2472
514
    }
2473
286
    return true;
2474
286
  }
2475
225
  return false;
2476
225
}
2477
2478
clang::DeclContext *
2479
12.4k
TypeSystemClang::GetDeclContextForType(const CompilerType &type) {
2480
12.4k
  return GetDeclContextForType(ClangUtil::GetQualType(type));
2481
12.4k
}
2482
2483
/// Aggressively desugar the provided type, skipping past various kinds of
2484
/// syntactic sugar and other constructs one typically wants to ignore.
2485
/// The \p mask argument allows one to skip certain kinds of simplifications,
2486
/// when one wishes to handle a certain kind of type directly.
2487
static QualType
2488
2.39M
RemoveWrappingTypes(QualType type, ArrayRef<clang::Type::TypeClass> mask = {}) {
2489
2.53M
  while (true) {
2490
2.53M
    if (find(mask, type->getTypeClass()) != mask.end())
2491
67.8k
      return type;
2492
2.46M
    switch (type->getTypeClass()) {
2493
0
    // This is not fully correct as _Atomic is more than sugar, but it is
2494
0
    // sufficient for the purposes we care about.
2495
64
    case clang::Type::Atomic:
2496
64
      type = cast<clang::AtomicType>(type)->getValueType();
2497
64
      break;
2498
134k
    case clang::Type::Auto:
2499
134k
    case clang::Type::Decltype:
2500
134k
    case clang::Type::Elaborated:
2501
134k
    case clang::Type::Paren:
2502
134k
    case clang::Type::Typedef:
2503
134k
    case clang::Type::TypeOf:
2504
134k
    case clang::Type::TypeOfExpr:
2505
134k
      type = type->getLocallyUnqualifiedSingleStepDesugaredType();
2506
134k
      break;
2507
2.33M
    default:
2508
2.33M
      return type;
2509
2.46M
    }
2510
2.46M
  }
2511
2.39M
}
2512
2513
clang::DeclContext *
2514
13.2k
TypeSystemClang::GetDeclContextForType(clang::QualType type) {
2515
13.2k
  if (type.isNull())
2516
0
    return nullptr;
2517
13.2k
2518
13.2k
  clang::QualType qual_type = RemoveWrappingTypes(type.getCanonicalType());
2519
13.2k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2520
13.2k
  switch (type_class) {
2521
2.03k
  case clang::Type::ObjCInterface:
2522
2.03k
    return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2523
2.03k
        ->getInterface();
2524
0
  case clang::Type::ObjCObjectPointer:
2525
0
    return GetDeclContextForType(
2526
0
        llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2527
0
            ->getPointeeType());
2528
11.1k
  case clang::Type::Record:
2529
11.1k
    return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2530
90
  case clang::Type::Enum:
2531
90
    return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2532
0
  default:
2533
0
    break;
2534
0
  }
2535
0
  // No DeclContext in this type...
2536
0
  return nullptr;
2537
0
}
2538
2539
static bool GetCompleteQualType(clang::ASTContext *ast,
2540
                                clang::QualType qual_type,
2541
709k
                                bool allow_completion = true) {
2542
709k
  qual_type = RemoveWrappingTypes(qual_type);
2543
709k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2544
709k
  switch (type_class) {
2545
2.05k
  case clang::Type::ConstantArray:
2546
2.05k
  case clang::Type::IncompleteArray:
2547
2.05k
  case clang::Type::VariableArray: {
2548
2.05k
    const clang::ArrayType *array_type =
2549
2.05k
        llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2550
2.05k
2551
2.05k
    if (array_type)
2552
2.05k
      return GetCompleteQualType(ast, array_type->getElementType(),
2553
2.05k
                                 allow_completion);
2554
0
  } break;
2555
287k
  case clang::Type::Record: {
2556
287k
    clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2557
287k
    if (cxx_record_decl) {
2558
287k
      if (cxx_record_decl->hasExternalLexicalStorage()) {
2559
2.80k
        const bool is_complete = cxx_record_decl->isCompleteDefinition();
2560
2.80k
        const bool fields_loaded =
2561
2.80k
            cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2562
2.80k
        if (is_complete && 
fields_loaded1.40k
)
2563
961
          return true;
2564
1.83k
2565
1.83k
        if (!allow_completion)
2566
218
          return false;
2567
1.62k
2568
1.62k
        // Call the field_begin() accessor to for it to use the external source
2569
1.62k
        // to load the fields...
2570
1.62k
        clang::ExternalASTSource *external_ast_source =
2571
1.62k
            ast->getExternalSource();
2572
1.62k
        if (external_ast_source) {
2573
1.62k
          external_ast_source->CompleteType(cxx_record_decl);
2574
1.62k
          if (cxx_record_decl->isCompleteDefinition()) {
2575
1.61k
            cxx_record_decl->field_begin();
2576
1.61k
            cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2577
1.61k
          }
2578
1.62k
        }
2579
1.62k
      }
2580
287k
    }
2581
287k
    const clang::TagType *tag_type =
2582
286k
        llvm::cast<clang::TagType>(qual_type.getTypePtr());
2583
286k
    return !tag_type->isIncompleteType();
2584
287k
  } 
break0
;
2585
287k
2586
287k
  case clang::Type::Enum: {
2587
2.29k
    const clang::TagType *tag_type =
2588
2.29k
        llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2589
2.29k
    if (tag_type) {
2590
2.29k
      clang::TagDecl *tag_decl = tag_type->getDecl();
2591
2.29k
      if (tag_decl) {
2592
2.29k
        if (tag_decl->getDefinition())
2593
2.29k
          return true;
2594
0
2595
0
        if (!allow_completion)
2596
0
          return false;
2597
0
2598
0
        if (tag_decl->hasExternalLexicalStorage()) {
2599
0
          if (ast) {
2600
0
            clang::ExternalASTSource *external_ast_source =
2601
0
                ast->getExternalSource();
2602
0
            if (external_ast_source) {
2603
0
              external_ast_source->CompleteType(tag_decl);
2604
0
              return !tag_type->isIncompleteType();
2605
0
            }
2606
0
          }
2607
0
        }
2608
0
        return false;
2609
0
      }
2610
2.29k
    }
2611
0
2612
0
  } break;
2613
5.07k
  case clang::Type::ObjCObject:
2614
5.07k
  case clang::Type::ObjCInterface: {
2615
5.07k
    const clang::ObjCObjectType *objc_class_type =
2616
5.07k
        llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2617
5.07k
    if (objc_class_type) {
2618
5.07k
      clang::ObjCInterfaceDecl *class_interface_decl =
2619
5.07k
          objc_class_type->getInterface();
2620
5.07k
      // We currently can't complete objective C types through the newly added
2621
5.07k
      // ASTContext because it only supports TagDecl objects right now...
2622
5.07k
      if (class_interface_decl) {
2623
4.41k
        if (class_interface_decl->getDefinition())
2624
4.31k
          return true;
2625
94
2626
94
        if (!allow_completion)
2627
0
          return false;
2628
94
2629
94
        if (class_interface_decl->hasExternalLexicalStorage()) {
2630
94
          if (ast) {
2631
94
            clang::ExternalASTSource *external_ast_source =
2632
94
                ast->getExternalSource();
2633
94
            if (external_ast_source) {
2634
94
              external_ast_source->CompleteType(class_interface_decl);
2635
94
              return !objc_class_type->isIncompleteType();
2636
94
            }
2637
0
          }
2638
94
        }
2639
0
        return false;
2640
0
      }
2641
5.07k
    }
2642
667
  } break;
2643
667
2644
667
  case clang::Type::Attributed:
2645
175
    return GetCompleteQualType(
2646
175
        ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2647
175
        allow_completion);
2648
667
2649
412k
  default:
2650
412k
    break;
2651
413k
  }
2652
413k
2653
413k
  return true;
2654
413k
}
2655
2656
static clang::ObjCIvarDecl::AccessControl
2657
669
ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2658
669
  switch (access) {
2659
0
  case eAccessNone:
2660
0
    return clang::ObjCIvarDecl::None;
2661
669
  case eAccessPublic:
2662
669
    return clang::ObjCIvarDecl::Public;
2663
0
  case eAccessPrivate:
2664
0
    return clang::ObjCIvarDecl::Private;
2665
0
  case eAccessProtected:
2666
0
    return clang::ObjCIvarDecl::Protected;
2667
0
  case eAccessPackage:
2668
0
    return clang::ObjCIvarDecl::Package;
2669
0
  }
2670
0
  return clang::ObjCIvarDecl::None;
2671
0
}
2672
2673
// Tests
2674
2675
#ifndef NDEBUG
2676
740k
bool TypeSystemClang::Verify(lldb::opaque_compiler_type_t type) {
2677
740k
  return !type || llvm::isa<clang::Type>(GetQualType(type).getTypePtr());
2678
740k
}
2679
#endif
2680
2681
196k
bool TypeSystemClang::IsAggregateType(lldb::opaque_compiler_type_t type) {
2682
196k
  clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2683
196k
2684
196k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2685
196k
  switch (type_class) {
2686
145k
  case clang::Type::IncompleteArray:
2687
145k
  case clang::Type::VariableArray:
2688
145k
  case clang::Type::ConstantArray:
2689
145k
  case clang::Type::ExtVector:
2690
145k
  case clang::Type::Vector:
2691
145k
  case clang::Type::Record:
2692
145k
  case clang::Type::ObjCObject:
2693
145k
  case clang::Type::ObjCInterface:
2694
145k
    return true;
2695
145k
  default:
2696
51.5k
    break;
2697
51.5k
  }
2698
51.5k
  // The clang type does have a value
2699
51.5k
  return false;
2700
51.5k
}
2701
2702
21
bool TypeSystemClang::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2703
21
  clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2704
21
2705
21
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2706
21
  switch (type_class) {
2707
21
  case clang::Type::Record: {
2708
21
    if (const clang::RecordType *record_type =
2709
21
            llvm::dyn_cast_or_null<clang::RecordType>(
2710
21
                qual_type.getTypePtrOrNull())) {
2711
21
      if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2712
21
        return record_decl->isAnonymousStructOrUnion();
2713
21
      }
2714
0
    }
2715
0
    break;
2716
0
  }
2717
0
  default:
2718
0
    break;
2719
0
  }
2720
0
  // The clang type does have a value
2721
0
  return false;
2722
0
}
2723
2724
bool TypeSystemClang::IsArrayType(lldb::opaque_compiler_type_t type,
2725
                                  CompilerType *element_type_ptr,
2726
41.5k
                                  uint64_t *size, bool *is_incomplete) {
2727
41.5k
  clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2728
41.5k
2729
41.5k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2730
41.5k
  switch (type_class) {
2731
37.2k
  default:
2732
37.2k
    break;
2733
0
2734
4.24k
  case clang::Type::ConstantArray:
2735
4.24k
    if (element_type_ptr)
2736
2.71k
      element_type_ptr->SetCompilerType(
2737
2.71k
          this, llvm::cast<clang::ConstantArrayType>(qual_type)
2738
2.71k
                    ->getElementType()
2739
2.71k
                    .getAsOpaquePtr());
2740
4.24k
    if (size)
2741
1.09k
      *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2742
1.09k
                  ->getSize()
2743
1.09k
                  .getLimitedValue(ULLONG_MAX);
2744
4.24k
    if (is_incomplete)
2745
918
      *is_incomplete = false;
2746
4.24k
    return true;
2747
0
2748
64
  case clang::Type::IncompleteArray:
2749
64
    if (element_type_ptr)
2750
27
      element_type_ptr->SetCompilerType(
2751
27
          this, llvm::cast<clang::IncompleteArrayType>(qual_type)
2752
27
                    ->getElementType()
2753
27
                    .getAsOpaquePtr());
2754
64
    if (size)
2755
10
      *size = 0;
2756
64
    if (is_incomplete)
2757
27
      *is_incomplete = true;
2758
64
    return true;
2759
0
2760
0
  case clang::Type::VariableArray:
2761
0
    if (element_type_ptr)
2762
0
      element_type_ptr->SetCompilerType(
2763
0
          this, llvm::cast<clang::VariableArrayType>(qual_type)
2764
0
                    ->getElementType()
2765
0
                    .getAsOpaquePtr());
2766
0
    if (size)
2767
0
      *size = 0;
2768
0
    if (is_incomplete)
2769
0
      *is_incomplete = false;
2770
0
    return true;
2771
0
2772
0
  case clang::Type::DependentSizedArray:
2773
0
    if (element_type_ptr)
2774
0
      element_type_ptr->SetCompilerType(
2775
0
          this, llvm::cast<clang::DependentSizedArrayType>(qual_type)
2776
0
                    ->getElementType()
2777
0
                    .getAsOpaquePtr());
2778
0
    if (size)
2779
0
      *size = 0;
2780
0
    if (is_incomplete)
2781
0
      *is_incomplete = false;
2782
0
    return true;
2783
37.2k
  }
2784
37.2k
  if (element_type_ptr)
2785
7.39k
    element_type_ptr->Clear();
2786
37.2k
  if (size)
2787
36.8k
    *size = 0;
2788
37.2k
  if (is_incomplete)
2789
7.23k
    *is_incomplete = false;
2790
37.2k
  return false;
2791
37.2k
}
2792
2793
bool TypeSystemClang::IsVectorType(lldb::opaque_compiler_type_t type,
2794
166k
                                   CompilerType *element_type, uint64_t *size) {
2795
166k
  clang::QualType qual_type(GetCanonicalQualType(type));
2796
166k
2797
166k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2798
166k
  switch (type_class) {
2799
0
  case clang::Type::Vector: {
2800
0
    const clang::VectorType *vector_type =
2801
0
        qual_type->getAs<clang::VectorType>();
2802
0
    if (vector_type) {
2803
0
      if (size)
2804
0
        *size = vector_type->getNumElements();
2805
0
      if (element_type)
2806
0
        *element_type = GetType(vector_type->getElementType());
2807
0
    }
2808
0
    return true;
2809
0
  } break;
2810
2.18k
  case clang::Type::ExtVector: {
2811
2.18k
    const clang::ExtVectorType *ext_vector_type =
2812
2.18k
        qual_type->getAs<clang::ExtVectorType>();
2813
2.18k
    if (ext_vector_type) {
2814
2.18k
      if (size)
2815
0
        *size = ext_vector_type->getNumElements();
2816
2.18k
      if (element_type)
2817
466
        *element_type =
2818
466
            CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr());
2819
2.18k
    }
2820
2.18k
    return true;
2821
0
  }
2822
164k
  default:
2823
164k
    break;
2824
164k
  }
2825
164k
  return false;
2826
164k
}
2827
2828
bool TypeSystemClang::IsRuntimeGeneratedType(
2829
0
    lldb::opaque_compiler_type_t type) {
2830
0
  clang::DeclContext *decl_ctx = GetDeclContextForType(GetQualType(type));
2831
0
  if (!decl_ctx)
2832
0
    return false;
2833
0
2834
0
  if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2835
0
    return false;
2836
0
2837
0
  clang::ObjCInterfaceDecl *result_iface_decl =
2838
0
      llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2839
0
2840
0
  ClangASTMetadata *ast_metadata = GetMetadata(result_iface_decl);
2841
0
  if (!ast_metadata)
2842
0
    return false;
2843
0
  return (ast_metadata->GetISAPtr() != 0);
2844
0
}
2845
2846
2.43k
bool TypeSystemClang::IsCharType(lldb::opaque_compiler_type_t type) {
2847
2.43k
  return GetQualType(type).getUnqualifiedType()->isCharType();
2848
2.43k
}
2849
2850
11.4k
bool TypeSystemClang::IsCompleteType(lldb::opaque_compiler_type_t type) {
2851
11.4k
  const bool allow_completion = false;
2852
11.4k
  return GetCompleteQualType(&getASTContext(), GetQualType(type),
2853
11.4k
                             allow_completion);
2854
11.4k
}
2855
2856
0
bool TypeSystemClang::IsConst(lldb::opaque_compiler_type_t type) {
2857
0
  return GetQualType(type).isConstQualified();
2858
0
}
2859
2860
bool TypeSystemClang::IsCStringType(lldb::opaque_compiler_type_t type,
2861
0
                                    uint32_t &length) {
2862
0
  CompilerType pointee_or_element_clang_type;
2863
0
  length = 0;
2864
0
  Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2865
0
2866
0
  if (!pointee_or_element_clang_type.IsValid())
2867
0
    return false;
2868
0
2869
0
  if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2870
0
    if (pointee_or_element_clang_type.IsCharType()) {
2871
0
      if (type_flags.Test(eTypeIsArray)) {
2872
0
        // We know the size of the array and it could be a C string since it is
2873
0
        // an array of characters
2874
0
        length = llvm::cast<clang::ConstantArrayType>(
2875
0
                     GetCanonicalQualType(type).getTypePtr())
2876
0
                     ->getSize()
2877
0
                     .getLimitedValue();
2878
0
      }
2879
0
      return true;
2880
0
    }
2881
0
  }
2882
0
  return false;
2883
0
}
2884
2885
bool TypeSystemClang::IsFunctionType(lldb::opaque_compiler_type_t type,
2886
6
                                     bool *is_variadic_ptr) {
2887
6
  if (type) {
2888
6
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
2889
6
2890
6
    if (qual_type->isFunctionType()) {
2891
6
      if (is_variadic_ptr) {
2892
0
        const clang::FunctionProtoType *function_proto_type =
2893
0
            llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2894
0
        if (function_proto_type)
2895
0
          *is_variadic_ptr = function_proto_type->isVariadic();
2896
0
        else
2897
0
          *is_variadic_ptr = false;
2898
0
      }
2899
6
      return true;
2900
6
    }
2901
0
2902
0
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2903
0
    switch (type_class) {
2904
0
    default:
2905
0
      break;
2906
0
    case clang::Type::LValueReference:
2907
0
    case clang::Type::RValueReference: {
2908
0
      const clang::ReferenceType *reference_type =
2909
0
          llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
2910
0
      if (reference_type)
2911
0
        return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
2912
0
                              nullptr);
2913
0
    } break;
2914
0
    }
2915
0
  }
2916
0
  return false;
2917
0
}
2918
2919
// Used to detect "Homogeneous Floating-point Aggregates"
2920
uint32_t
2921
TypeSystemClang::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
2922
0
                                        CompilerType *base_type_ptr) {
2923
0
  if (!type)
2924
0
    return 0;
2925
0
2926
0
  clang::QualType qual_type(RemoveWrappingTypes(GetCanonicalQualType(type)));
2927
0
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2928
0
  switch (type_class) {
2929
0
  case clang::Type::Record:
2930
0
    if (GetCompleteType(type)) {
2931
0
      const clang::CXXRecordDecl *cxx_record_decl =
2932
0
          qual_type->getAsCXXRecordDecl();
2933
0
      if (cxx_record_decl) {
2934
0
        if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
2935
0
          return 0;
2936
0
      }
2937
0
      const clang::RecordType *record_type =
2938
0
          llvm::cast<clang::RecordType>(qual_type.getTypePtr());
2939
0
      if (record_type) {
2940
0
        const clang::RecordDecl *record_decl = record_type->getDecl();
2941
0
        if (record_decl) {
2942
0
          // We are looking for a structure that contains only floating point
2943
0
          // types
2944
0
          clang::RecordDecl::field_iterator field_pos,
2945
0
              field_end = record_decl->field_end();
2946
0
          uint32_t num_fields = 0;
2947
0
          bool is_hva = false;
2948
0
          bool is_hfa = false;
2949
0
          clang::QualType base_qual_type;
2950
0
          uint64_t base_bitwidth = 0;
2951
0
          for (field_pos = record_decl->field_begin(); field_pos != field_end;
2952
0
               ++field_pos) {
2953
0
            clang::QualType field_qual_type = field_pos->getType();
2954
0
            uint64_t field_bitwidth = getASTContext().getTypeSize(qual_type);
2955
0
            if (field_qual_type->isFloatingType()) {
2956
0
              if (field_qual_type->isComplexType())
2957
0
                return 0;
2958
0
              else {
2959
0
                if (num_fields == 0)
2960
0
                  base_qual_type = field_qual_type;
2961
0
                else {
2962
0
                  if (is_hva)
2963
0
                    return 0;
2964
0
                  is_hfa = true;
2965
0
                  if (field_qual_type.getTypePtr() !=
2966
0
                      base_qual_type.getTypePtr())
2967
0
                    return 0;
2968
0
                }
2969
0
              }
2970
0
            } else if (field_qual_type->isVectorType() ||
2971
0
                       field_qual_type->isExtVectorType()) {
2972
0
              if (num_fields == 0) {
2973
0
                base_qual_type = field_qual_type;
2974
0
                base_bitwidth = field_bitwidth;
2975
0
              } else {
2976
0
                if (is_hfa)
2977
0
                  return 0;
2978
0
                is_hva = true;
2979
0
                if (base_bitwidth != field_bitwidth)
2980
0
                  return 0;
2981
0
                if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
2982
0
                  return 0;
2983
0
              }
2984
0
            } else
2985
0
              return 0;
2986
0
            ++num_fields;
2987
0
          }
2988
0
          if (base_type_ptr)
2989
0
            *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr());
2990
0
          return num_fields;
2991
0
        }
2992
0
      }
2993
0
    }
2994
0
    break;
2995
0
2996
0
  default:
2997
0
    break;
2998
0
  }
2999
0
  return 0;
3000
0
}
3001
3002
size_t TypeSystemClang::GetNumberOfFunctionArguments(
3003
9
    lldb::opaque_compiler_type_t type) {
3004
9
  if (type) {
3005
9
    clang::QualType qual_type(GetCanonicalQualType(type));
3006
9
    const clang::FunctionProtoType *func =
3007
9
        llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3008
9
    if (func)
3009
9
      return func->getNumParams();
3010
0
  }
3011
0
  return 0;
3012
0
}
3013
3014
CompilerType
3015
TypeSystemClang::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3016
15
                                            const size_t index) {
3017
15
  if (type) {
3018
15
    clang::QualType qual_type(GetQualType(type));
3019
15
    const clang::FunctionProtoType *func =
3020
15
        llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3021
15
    if (func) {
3022
15
      if (index < func->getNumParams())
3023
15
        return CompilerType(this, func->getParamType(index).getAsOpaquePtr());
3024
0
    }
3025
15
  }
3026
0
  return CompilerType();
3027
0
}
3028
3029
82.2k
bool TypeSystemClang::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3030
82.2k
  if (type) {
3031
82.2k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3032
82.2k
3033
82.2k
    if (qual_type->isFunctionPointerType())
3034
141
      return true;
3035
82.1k
3036
82.1k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3037
82.1k
    switch (type_class) {
3038
81.1k
    default:
3039
81.1k
      break;
3040
0
3041
982
    case clang::Type::LValueReference:
3042
982
    case clang::Type::RValueReference: {
3043
982
      const clang::ReferenceType *reference_type =
3044
982
          llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3045
982
      if (reference_type)
3046
982
        return IsFunctionPointerType(
3047
982
            reference_type->getPointeeType().getAsOpaquePtr());
3048
0
    } break;
3049
82.1k
    }
3050
82.1k
  }
3051
81.1k
  return false;
3052
81.1k
}
3053
3054
bool TypeSystemClang::IsBlockPointerType(
3055
    lldb::opaque_compiler_type_t type,
3056
165k
    CompilerType *function_pointer_type_ptr) {
3057
165k
  if (type) {
3058
165k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3059
165k
3060
165k
    if (qual_type->isBlockPointerType()) {
3061
0
      if (function_pointer_type_ptr) {
3062
0
        const clang::BlockPointerType *block_pointer_type =
3063
0
            qual_type->getAs<clang::BlockPointerType>();
3064
0
        QualType pointee_type = block_pointer_type->getPointeeType();
3065
0
        QualType function_pointer_type = m_ast_up->getPointerType(pointee_type);
3066
0
        *function_pointer_type_ptr =
3067
0
            CompilerType(this, function_pointer_type.getAsOpaquePtr());
3068
0
      }
3069
0
      return true;
3070
0
    }
3071
165k
3072
165k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3073
165k
    switch (type_class) {
3074
164k
    default:
3075
164k
      break;
3076
0
3077
1.91k
    case clang::Type::LValueReference:
3078
1.91k
    case clang::Type::RValueReference: {
3079
1.91k
      const clang::ReferenceType *reference_type =
3080
1.91k
          llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3081
1.91k
      if (reference_type)
3082
1.91k
        return IsBlockPointerType(
3083
1.91k
            reference_type->getPointeeType().getAsOpaquePtr(),
3084
1.91k
            function_pointer_type_ptr);
3085
0
    } break;
3086
165k
    }
3087
165k
  }
3088
164k
  return false;
3089
164k
}
3090
3091
bool TypeSystemClang::IsIntegerType(lldb::opaque_compiler_type_t type,
3092
24.4k
                                    bool &is_signed) {
3093
24.4k
  if (!type)
3094
0
    return false;
3095
24.4k
3096
24.4k
  clang::QualType qual_type(GetCanonicalQualType(type));
3097
24.4k
  const clang::BuiltinType *builtin_type =
3098
24.4k
      llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3099
24.4k
3100
24.4k
  if (builtin_type) {
3101
12.9k
    if (builtin_type->isInteger()) {
3102
12.3k
      is_signed = builtin_type->isSignedInteger();
3103
12.3k
      return true;
3104
12.3k
    }
3105
12.0k
  }
3106
12.0k
3107
12.0k
  return false;
3108
12.0k
}
3109
3110
bool TypeSystemClang::IsEnumerationType(lldb::opaque_compiler_type_t type,
3111
11.8k
                                        bool &is_signed) {
3112
11.8k
  if (type) {
3113
11.8k
    const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3114
11.8k
        GetCanonicalQualType(type)->getCanonicalTypeInternal());
3115
11.8k
3116
11.8k
    if (enum_type) {
3117
99
      IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3118
99
                    is_signed);
3119
99
      return true;
3120
99
    }
3121
11.7k
  }
3122
11.7k
3123
11.7k
  return false;
3124
11.7k
}
3125
3126
bool TypeSystemClang::IsPointerType(lldb::opaque_compiler_type_t type,
3127
40.1k
                                    CompilerType *pointee_type) {
3128
40.1k
  if (type) {
3129
40.1k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3130
40.1k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3131
40.1k
    switch (type_class) {
3132
9.48k
    case clang::Type::Builtin:
3133
9.48k
      switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3134
9.42k
      default:
3135
9.42k
        break;
3136
63
      case clang::BuiltinType::ObjCId:
3137
63
      case clang::BuiltinType::ObjCClass:
3138
63
        return true;
3139
9.42k
      }
3140
9.42k
      return false;
3141
9.42k
    case clang::Type::ObjCObjectPointer:
3142
2.07k
      if (pointee_type)
3143
0
        pointee_type->SetCompilerType(
3144
0
            this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3145
0
                      ->getPointeeType()
3146
0
                      .getAsOpaquePtr());
3147
2.07k
      return true;
3148
9.42k
    case clang::Type::BlockPointer:
3149
0
      if (pointee_type)
3150
0
        pointee_type->SetCompilerType(
3151
0
            this, llvm::cast<clang::BlockPointerType>(qual_type)
3152
0
                      ->getPointeeType()
3153
0
                      .getAsOpaquePtr());
3154
0
      return true;
3155
10.9k
    case clang::Type::Pointer:
3156
10.9k
      if (pointee_type)
3157
45
        pointee_type->SetCompilerType(this,
3158
45
                                      llvm::cast<clang::PointerType>(qual_type)
3159
45
                                          ->getPointeeType()
3160
45
                                          .getAsOpaquePtr());
3161
10.9k
      return true;
3162
9.42k
    case clang::Type::MemberPointer:
3163
0
      if (pointee_type)
3164
0
        pointee_type->SetCompilerType(
3165
0
            this, llvm::cast<clang::MemberPointerType>(qual_type)
3166
0
                      ->getPointeeType()
3167
0
                      .getAsOpaquePtr());
3168
0
      return true;
3169
17.5k
    default:
3170
17.5k
      break;
3171
17.5k
    }
3172
17.5k
  }
3173
17.5k
  if (pointee_type)
3174
0
    pointee_type->Clear();
3175
17.5k
  return false;
3176
17.5k
}
3177
3178
bool TypeSystemClang::IsPointerOrReferenceType(
3179
68.7k
    lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3180
68.7k
  if (type) {
3181
68.7k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3182
68.7k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3183
68.7k
    switch (type_class) {
3184
230
    case clang::Type::Builtin:
3185
230
      switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3186
197
      default:
3187
197
        break;
3188
33
      case clang::BuiltinType::ObjCId:
3189
33
      case clang::BuiltinType::ObjCClass:
3190
33
        return true;
3191
197
      }
3192
197
      return false;
3193
739
    case clang::Type::ObjCObjectPointer:
3194
739
      if (pointee_type)
3195
0
        pointee_type->SetCompilerType(
3196
0
            this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3197
0
                                 ->getPointeeType().getAsOpaquePtr());
3198
739
      return true;
3199
197
    case clang::Type::BlockPointer:
3200
0
      if (pointee_type)
3201
0
        pointee_type->SetCompilerType(
3202
0
            this, llvm::cast<clang::BlockPointerType>(qual_type)
3203
0
                      ->getPointeeType()
3204
0
                      .getAsOpaquePtr());
3205
0
      return true;
3206
50.8k
    case clang::Type::Pointer:
3207
50.8k
      if (pointee_type)
3208
0
        pointee_type->SetCompilerType(this,
3209
0
                                      llvm::cast<clang::PointerType>(qual_type)
3210
0
                                          ->getPointeeType()
3211
0
                                          .getAsOpaquePtr());
3212
50.8k
      return true;
3213
197
    case clang::Type::MemberPointer:
3214
0
      if (pointee_type)
3215
0
        pointee_type->SetCompilerType(
3216
0
            this, llvm::cast<clang::MemberPointerType>(qual_type)
3217
0
                      ->getPointeeType()
3218
0
                      .getAsOpaquePtr());
3219
0
      return true;
3220
691
    case clang::Type::LValueReference:
3221
691
      if (pointee_type)
3222
0
        pointee_type->SetCompilerType(
3223
0
            this, llvm::cast<clang::LValueReferenceType>(qual_type)
3224
0
                      ->desugar()
3225
0
                      .getAsOpaquePtr());
3226
691
      return true;
3227
197
    case clang::Type::RValueReference:
3228
9
      if (pointee_type)
3229
0
        pointee_type->SetCompilerType(
3230
0
            this, llvm::cast<clang::RValueReferenceType>(qual_type)
3231
0
                      ->desugar()
3232
0
                      .getAsOpaquePtr());
3233
9
      return true;
3234
16.2k
    default:
3235
16.2k
      break;
3236
16.2k
    }
3237
16.2k
  }
3238
16.2k
  if (pointee_type)
3239
0
    pointee_type->Clear();
3240
16.2k
  return false;
3241
16.2k
}
3242
3243
bool TypeSystemClang::IsReferenceType(lldb::opaque_compiler_type_t type,
3244
                                      CompilerType *pointee_type,
3245
52.0k
                                      bool *is_rvalue) {
3246
52.0k
  if (type) {
3247
52.0k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3248
52.0k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3249
52.0k
3250
52.0k
    switch (type_class) {
3251
2.96k
    case clang::Type::LValueReference:
3252
2.96k
      if (pointee_type)
3253
0
        pointee_type->SetCompilerType(
3254
0
            this, llvm::cast<clang::LValueReferenceType>(qual_type)
3255
0
                      ->desugar()
3256
0
                      .getAsOpaquePtr());
3257
2.96k
      if (is_rvalue)
3258
472
        *is_rvalue = false;
3259
2.96k
      return true;
3260
74
    case clang::Type::RValueReference:
3261
74
      if (pointee_type)
3262
0
        pointee_type->SetCompilerType(
3263
0
            this, llvm::cast<clang::RValueReferenceType>(qual_type)
3264
0
                      ->desugar()
3265
0
                      .getAsOpaquePtr());
3266
74
      if (is_rvalue)
3267
65
        *is_rvalue = true;
3268
74
      return true;
3269
0
3270
49.0k
    default:
3271
49.0k
      break;
3272
49.0k
    }
3273
49.0k
  }
3274
49.0k
  if (pointee_type)
3275
0
    pointee_type->Clear();
3276
49.0k
  return false;
3277
49.0k
}
3278
3279
bool TypeSystemClang::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3280
165
                                          uint32_t &count, bool &is_complex) {
3281
165
  if (type) {
3282
165
    clang::QualType qual_type(GetCanonicalQualType(type));
3283
165
3284
165
    if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3285
132
            qual_type->getCanonicalTypeInternal())) {
3286
132
      clang::BuiltinType::Kind kind = BT->getKind();
3287
132
      if (kind >= clang::BuiltinType::Float &&
3288
132
          kind <= clang::BuiltinType::LongDouble) {
3289
132
        count = 1;
3290
132
        is_complex = false;
3291
132
        return true;
3292
132
      }
3293
33
    } else if (const clang::ComplexType *CT =
3294
0
                   llvm::dyn_cast<clang::ComplexType>(
3295
0
                       qual_type->getCanonicalTypeInternal())) {
3296
0
      if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3297
0
                              is_complex)) {
3298
0
        count = 2;
3299
0
        is_complex = true;
3300
0
        return true;
3301
0
      }
3302
33
    } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3303
0
                   qual_type->getCanonicalTypeInternal())) {
3304
0
      if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3305
0
                              is_complex)) {
3306
0
        count = VT->getNumElements();
3307
0
        is_complex = false;
3308
0
        return true;
3309
0
      }
3310
33
    }
3311
165
  }
3312
33
  count = 0;
3313
33
  is_complex = false;
3314
33
  return false;
3315
33
}
3316
3317
10.7k
bool TypeSystemClang::IsDefined(lldb::opaque_compiler_type_t type) {
3318
10.7k
  if (!type)
3319
0
    return false;
3320
10.7k
3321
10.7k
  clang::QualType qual_type(GetQualType(type));
3322
10.7k
  const clang::TagType *tag_type =
3323
10.7k
      llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3324
10.7k
  if (tag_type) {
3325
4.77k
    clang::TagDecl *tag_decl = tag_type->getDecl();
3326
4.77k
    if (tag_decl)
3327
4.77k
      return tag_decl->isCompleteDefinition();
3328
0
    return false;
3329
6.02k
  } else {
3330
6.02k
    const clang::ObjCObjectType *objc_class_type =
3331
6.02k
        llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3332
6.02k
    if (objc_class_type) {
3333
997
      clang::ObjCInterfaceDecl *class_interface_decl =
3334
997
          objc_class_type->getInterface();
3335
997
      if (class_interface_decl)
3336
997
        return class_interface_decl->getDefinition() != nullptr;
3337
0
      return false;
3338
0
    }
3339
6.02k
  }
3340
5.03k
  return true;
3341
5.03k
}
3342
3343
15
bool TypeSystemClang::IsObjCClassType(const CompilerType &type) {
3344
15
  if (ClangUtil::IsClangType(type)) {
3345
15
    clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3346
15
3347
15
    const clang::ObjCObjectPointerType *obj_pointer_type =
3348
15
        llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3349
15
3350
15
    if (obj_pointer_type)
3351
15
      return obj_pointer_type->isObjCClassType();
3352
0
  }
3353
0
  return false;
3354
0
}
3355
3356
8.53k
bool TypeSystemClang::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3357
8.53k
  if (ClangUtil::IsClangType(type))
3358
8.53k
    return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3359
0
  return false;
3360
0
}
3361
3362
0
bool TypeSystemClang::IsClassType(lldb::opaque_compiler_type_t type) {
3363
0
  if (!type)
3364
0
    return false;
3365
0
  clang::QualType qual_type(GetCanonicalQualType(type));
3366
0
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3367
0
  return (type_class == clang::Type::Record);
3368
0
}
3369
3370
0
bool TypeSystemClang::IsEnumType(lldb::opaque_compiler_type_t type) {
3371
0
  if (!type)
3372
0
    return false;
3373
0
  clang::QualType qual_type(GetCanonicalQualType(type));
3374
0
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3375
0
  return (type_class == clang::Type::Enum);
3376
0
}
3377
3378
0
bool TypeSystemClang::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3379
0
  if (type) {
3380
0
    clang::QualType qual_type(GetCanonicalQualType(type));
3381
0
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3382
0
    switch (type_class) {
3383
0
    case clang::Type::Record:
3384
0
      if (GetCompleteType(type)) {
3385
0
        const clang::RecordType *record_type =
3386
0
            llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3387
0
        const clang::RecordDecl *record_decl = record_type->getDecl();
3388
0
        if (record_decl) {
3389
0
          const clang::CXXRecordDecl *cxx_record_decl =
3390
0
              llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3391
0
          if (cxx_record_decl)
3392
0
            return cxx_record_decl->isPolymorphic();
3393
0
        }
3394
0
      }
3395
0
      break;
3396
0
3397
0
    default:
3398
0
      break;
3399
0
    }
3400
0
  }
3401
0
  return false;
3402
0
}
3403
3404
bool TypeSystemClang::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3405
                                            CompilerType *dynamic_pointee_type,
3406
                                            bool check_cplusplus,
3407
100k
                                            bool check_objc) {
3408
100k
  clang::QualType pointee_qual_type;
3409
100k
  if (type) {
3410
100k
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
3411
100k
    bool success = false;
3412
100k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3413
100k
    switch (type_class) {
3414
2.34k
    case clang::Type::Builtin:
3415
2.34k
      if (check_objc &&
3416
2.34k
          llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3417
1.32k
              clang::BuiltinType::ObjCId) {
3418
294
        if (dynamic_pointee_type)
3419
0
          dynamic_pointee_type->SetCompilerType(this, type);
3420
294
        return true;
3421
294
      }
3422
2.05k
      break;
3423
2.05k
3424
2.98k
    case clang::Type::ObjCObjectPointer:
3425
2.98k
      if (check_objc) {
3426
2.98k
        if (const auto *objc_pointee_type =
3427
2.98k
                qual_type->getPointeeType().getTypePtrOrNull()) {
3428
2.98k
          if (const auto *objc_object_type =
3429
2.98k
                  llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3430
2.98k
                      objc_pointee_type)) {
3431
2.98k
            if (objc_object_type->isObjCClass())
3432
323
              return false;
3433
2.66k
          }
3434
2.98k
        }
3435
2.66k
        if (dynamic_pointee_type)
3436
0
          dynamic_pointee_type->SetCompilerType(
3437
0
              this, llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3438
0
                        ->getPointeeType()
3439
0
                        .getAsOpaquePtr());
3440
2.66k
        return true;
3441
2.66k
      }
3442
0
      break;
3443
0
3444
48.9k
    case clang::Type::Pointer:
3445
48.9k
      pointee_qual_type =
3446
48.9k
          llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3447
48.9k
      success = true;
3448
48.9k
      break;
3449
0
3450
2.56k
    case clang::Type::LValueReference:
3451
2.56k
    case clang::Type::RValueReference:
3452
2.56k
      pointee_qual_type =
3453
2.56k
          llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3454
2.56k
      success = true;
3455
2.56k
      break;
3456
2.56k
3457
43.4k
    default:
3458
43.4k
      break;
3459
97.0k
    }
3460
97.0k
3461
97.0k
    if (success) {
3462
51.5k
      // Check to make sure what we are pointing too is a possible dynamic C++
3463
51.5k
      // type We currently accept any "void *" (in case we have a class that
3464
51.5k
      // has been watered down to an opaque pointer) and virtual C++ classes.
3465
51.5k
      const clang::Type::TypeClass pointee_type_class =
3466
51.5k
          pointee_qual_type.getCanonicalType()->getTypeClass();
3467
51.5k
      switch (pointee_type_class) {
3468
907
      case clang::Type::Builtin:
3469
907
        switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3470
358
        case clang::BuiltinType::UnknownAny:
3471
358
        case clang::BuiltinType::Void:
3472
358
          if (dynamic_pointee_type)
3473
0
            dynamic_pointee_type->SetCompilerType(
3474
0
                this, pointee_qual_type.getAsOpaquePtr());
3475
358
          return true;
3476
549
        default:
3477
549
          break;
3478
549
        }
3479
549
        break;
3480
549
3481
50.3k
      case clang::Type::Record:
3482
50.3k
        if (check_cplusplus) {
3483
50.2k
          clang::CXXRecordDecl *cxx_record_decl =
3484
50.2k
              pointee_qual_type->getAsCXXRecordDecl();
3485
50.2k
          if (cxx_record_decl) {
3486
50.2k
            bool is_complete = cxx_record_decl->isCompleteDefinition();
3487
50.2k
3488
50.2k
            if (is_complete)
3489
50.1k
              success = cxx_record_decl->isDynamicClass();
3490
92
            else {
3491
92
              ClangASTMetadata *metadata = GetMetadata(cxx_record_decl);
3492
92
              if (metadata)
3493
92
                success = metadata->GetIsDynamicCXXType();
3494
0
              else {
3495
0
                is_complete = GetType(pointee_qual_type).GetCompleteType();
3496
0
                if (is_complete)
3497
0
                  success = cxx_record_decl->isDynamicClass();
3498
0
                else
3499
0
                  success = false;
3500
0
              }
3501
92
            }
3502
50.2k
3503
50.2k
            if (success) {
3504
167
              if (dynamic_pointee_type)
3505
0
                dynamic_pointee_type->SetCompilerType(
3506
0
                    this, pointee_qual_type.getAsOpaquePtr());
3507
167
              return true;
3508
167
            }
3509
50.1k
          }
3510
50.2k
        }
3511
50.1k
        break;
3512
50.1k
3513
50.1k
      case clang::Type::ObjCObject:
3514
0
      case clang::Type::ObjCInterface:
3515
0
        if (check_objc) {
3516
0
          if (dynamic_pointee_type)
3517
0
            dynamic_pointee_type->SetCompilerType(
3518
0
                this, pointee_qual_type.getAsOpaquePtr());
3519
0
          return true;
3520
0
        }
3521
0
        break;
3522
0
3523
290
      default:
3524
290
        break;
3525
96.4k
      }
3526
96.4k
    }
3527
97.0k
  }
3528
96.4k
  if (dynamic_pointee_type)
3529
0
    dynamic_pointee_type->Clear();
3530
96.4k
  return false;
3531
96.4k
}
3532
3533
886
bool TypeSystemClang::IsScalarType(lldb::opaque_compiler_type_t type) {
3534
886
  if (!type)
3535
0
    return false;
3536
886
3537
886
  return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3538
886
}
3539
3540
39.9k
bool TypeSystemClang::IsTypedefType(lldb::opaque_compiler_type_t type) {
3541
39.9k
  if (!type)
3542
0
    return false;
3543
39.9k
  return RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef})
3544
39.9k
             ->getTypeClass() == clang::Type::Typedef;
3545
39.9k
}
3546
3547
47.7k
bool TypeSystemClang::IsVoidType(lldb::opaque_compiler_type_t type) {
3548
47.7k
  if (!type)
3549
0
    return false;
3550
47.7k
  return GetCanonicalQualType(type)->isVoidType();
3551
47.7k
}
3552
3553
83
bool TypeSystemClang::CanPassInRegisters(const CompilerType &type) {
3554
83
  if (auto *record_decl =
3555
83
      TypeSystemClang::GetAsRecordDecl(type)) {
3556
83
    return record_decl->canPassInRegisters();
3557
83
  }
3558
0
  return false;
3559
0
}
3560
3561
3.99k
bool TypeSystemClang::SupportsLanguage(lldb::LanguageType language) {
3562
3.99k
  return TypeSystemClangSupportsLanguage(language);
3563
3.99k
}
3564
3565
Optional<std::string>
3566
0
TypeSystemClang::GetCXXClassName(const CompilerType &type) {
3567
0
  if (!type)
3568
0
    return llvm::None;
3569
0
3570
0
  clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3571
0
  if (qual_type.isNull())
3572
0
    return llvm::None;
3573
0
3574
0
  clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3575
0
  if (!cxx_record_decl)
3576
0
    return llvm::None;
3577
0
3578
0
  return std::string(cxx_record_decl->getIdentifier()->getNameStart());
3579
0
}
3580
3581
61.0k
bool TypeSystemClang::IsCXXClassType(const CompilerType &type) {
3582
61.0k
  if (!type)
3583
0
    return false;
3584
61.0k
3585
61.0k
  clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3586
61.0k
  return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr;
3587
61.0k
}
3588
3589
50.7k
bool TypeSystemClang::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3590
50.7k
  if (!type)
3591
0
    return false;
3592
50.7k
  clang::QualType qual_type(GetCanonicalQualType(type));
3593
50.7k
  const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3594
50.7k
  if (tag_type)
3595
50.7k
    return tag_type->isBeingDefined();
3596
0
  return false;
3597
0
}
3598
3599
bool TypeSystemClang::IsObjCObjectPointerType(const CompilerType &type,
3600
5.43k
                                              CompilerType *class_type_ptr) {
3601
5.43k
  if (!ClangUtil::IsClangType(type))
3602
0
    return false;
3603
5.43k
3604
5.43k
  clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3605
5.43k
3606
5.43k
  if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3607
4.53k
    if (class_type_ptr) {
3608
4.51k
      if (!qual_type->isObjCClassType() && 
!qual_type->isObjCIdType()4.21k
) {
3609
3.27k
        const clang::ObjCObjectPointerType *obj_pointer_type =
3610
3.27k
            llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3611
3.27k
        if (obj_pointer_type == nullptr)
3612
0
          class_type_ptr->Clear();
3613
3.27k
        else
3614
3.27k
          class_type_ptr->SetCompilerType(
3615
3.27k
              type.GetTypeSystem(),
3616
3.27k
              clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3617
3.27k
                  .getAsOpaquePtr());
3618
3.27k
      }
3619
4.51k
    }
3620
4.53k
    return true;
3621
4.53k
  }
3622
905
  if (class_type_ptr)
3623
905
    class_type_ptr->Clear();
3624
905
  return false;
3625
905
}
3626
3627
// Type Completion
3628
3629
528k
bool TypeSystemClang::GetCompleteType(lldb::opaque_compiler_type_t type) {
3630
528k
  if (!type)
3631
0
    return false;
3632
528k
  const bool allow_completion = true;
3633
528k
  return GetCompleteQualType(&getASTContext(), GetQualType(type),
3634
528k
                             allow_completion);
3635
528k
}
3636
3637
351k
ConstString TypeSystemClang::GetTypeName(lldb::opaque_compiler_type_t type) {
3638
351k
  if (!type)
3639
0
    return ConstString();
3640
351k
3641
351k
  clang::QualType qual_type(GetQualType(type));
3642
351k
3643
351k
  // For a typedef just return the qualified name.
3644
351k
  if (const auto *typedef_type = qual_type->getAs<clang::TypedefType>()) {
3645
23.3k
    const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3646
23.3k
    return ConstString(typedef_decl->getQualifiedNameAsString());
3647
23.3k
  }
3648
327k
3649
327k
  clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
3650
327k
  printing_policy.SuppressTagKeyword = true;
3651
327k
  return ConstString(qual_type.getAsString(printing_policy));
3652
327k
}
3653
3654
ConstString
3655
15.2k
TypeSystemClang::GetDisplayTypeName(lldb::opaque_compiler_type_t type) {
3656
15.2k
  if (!type)
3657
0
    return ConstString();
3658
15.2k
3659
15.2k
  clang::QualType qual_type(GetQualType(type));
3660
15.2k
  clang::PrintingPolicy printing_policy(getASTContext().getPrintingPolicy());
3661
15.2k
  printing_policy.SuppressTagKeyword = true;
3662
15.2k
  printing_policy.SuppressScope = false;
3663
15.2k
  printing_policy.SuppressUnwrittenScope = true;
3664
15.2k
  return ConstString(qual_type.getAsString(printing_policy));
3665
15.2k
}
3666
3667
uint32_t
3668
TypeSystemClang::GetTypeInfo(lldb::opaque_compiler_type_t type,
3669
406k
                             CompilerType *pointee_or_element_clang_type) {
3670
406k
  if (!type)
3671
0
    return 0;
3672
406k
3673
406k
  if (pointee_or_element_clang_type)
3674
5.89k
    pointee_or_element_clang_type->Clear();
3675
406k
3676
406k
  clang::QualType qual_type =
3677
406k
      RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
3678
406k
3679
406k
  const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3680
406k
  switch (type_class) {
3681
71
  case clang::Type::Attributed:
3682
71
    return GetTypeInfo(
3683
71
        qual_type->getAs<clang::AttributedType>()
3684
71
            ->getModifiedType().getAsOpaquePtr(),
3685
71
        pointee_or_element_clang_type);
3686
84.9k
  case clang::Type::Builtin: {
3687
84.9k
    const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3688
84.9k
        qual_type->getCanonicalTypeInternal());
3689
84.9k
3690
84.9k
    uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3691
84.9k
    switch (builtin_type->getKind()) {
3692
522
    case clang::BuiltinType::ObjCId:
3693
522
    case clang::BuiltinType::ObjCClass:
3694
522
      if (pointee_or_element_clang_type)
3695
0
        pointee_or_element_clang_type->SetCompilerType(
3696
0
            this, getASTContext().ObjCBuiltinClassTy.getAsOpaquePtr());
3697
522
      builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3698
522
      break;
3699
522
3700
522
    case clang::BuiltinType::ObjCSel:
3701
18
      if (pointee_or_element_clang_type)
3702
0
        pointee_or_element_clang_type->SetCompilerType(
3703
0
            this, getASTContext().CharTy.getAsOpaquePtr());
3704
18
      builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3705
18
      break;
3706
522
3707
84.4k
    case clang::BuiltinType::Bool:
3708
84.4k
    case clang::BuiltinType::Char_U:
3709
84.4k
    case clang::BuiltinType::UChar:
3710
84.4k
    case clang::BuiltinType::WChar_U:
3711
84.4k
    case clang::BuiltinType::Char16:
3712
84.4k
    case clang::BuiltinType::Char32:
3713
84.4k
    case clang::BuiltinType::UShort:
3714
84.4k
    case clang::BuiltinType::UInt:
3715
84.4k
    case clang::BuiltinType::ULong:
3716
84.4k
    case clang::BuiltinType::ULongLong:
3717
84.4k
    case clang::BuiltinType::UInt128:
3718
84.4k
    case clang::BuiltinType::Char_S:
3719
84.4k
    case clang::BuiltinType::SChar:
3720
84.4k
    case clang::BuiltinType::WChar_S:
3721
84.4k
    case clang::BuiltinType::Short:
3722
84.4k
    case clang::BuiltinType::Int:
3723
84.4k
    case clang::BuiltinType::Long:
3724
84.4k
    case clang::BuiltinType::LongLong:
3725
84.4k
    case clang::BuiltinType::Int128:
3726
84.4k
    case clang::BuiltinType::Float:
3727
84.4k
    case clang::BuiltinType::Double:
3728
84.4k
    case clang::BuiltinType::LongDouble:
3729
84.4k
      builtin_type_flags |= eTypeIsScalar;
3730
84.4k
      if (builtin_type->isInteger()) {
3731
75.9k
        builtin_type_flags |= eTypeIsInteger;
3732
75.9k
        if (builtin_type->isSignedInteger())
3733
49.5k
          builtin_type_flags |= eTypeIsSigned;
3734
75.9k
      } else 
if (8.43k
builtin_type->isFloatingPoint()8.43k
)
3735
8.43k
        builtin_type_flags |= eTypeIsFloat;
3736
84.4k
      break;
3737
84.4k
    default:
3738
28
      break;
3739
84.9k
    }
3740
84.9k
    return builtin_type_flags;
3741
84.9k
  }
3742
84.9k
3743
84.9k
  case clang::Type::BlockPointer:
3744
6
    if (pointee_or_element_clang_type)
3745
0
      pointee_or_element_clang_type->SetCompilerType(
3746
0
          this, qual_type->getPointeeType().getAsOpaquePtr());
3747
6
    return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3748
84.9k
3749
84.9k
  case clang::Type::Complex: {
3750
31
    uint32_t complex_type_flags =
3751
31
        eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3752
31
    const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3753
31
        qual_type->getCanonicalTypeInternal());
3754
31
    if (complex_type) {
3755
31
      clang::QualType complex_element_type(complex_type->getElementType());
3756
31
      if (complex_element_type->isIntegerType())
3757
16
        complex_type_flags |= eTypeIsFloat;
3758
15
      else if (complex_element_type->isFloatingType())
3759
15
        complex_type_flags |= eTypeIsInteger;
3760
31
    }
3761
31
    return complex_type_flags;
3762
84.9k
  } 
break0
;
3763
84.9k
3764
84.9k
  case clang::Type::ConstantArray:
3765
7.21k
  case clang::Type::DependentSizedArray:
3766
7.21k
  case clang::Type::IncompleteArray:
3767
7.21k
  case clang::Type::VariableArray:
3768
7.21k
    if (pointee_or_element_clang_type)
3769
1.16k
      pointee_or_element_clang_type->SetCompilerType(
3770
1.16k
          this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3771
1.16k
                    ->getElementType()
3772
1.16k
                    .getAsOpaquePtr());
3773
7.21k
    return eTypeHasChildren | eTypeIsArray;
3774
7.21k
3775
7.21k
  case clang::Type::DependentName:
3776
0
    return 0;
3777
7.21k
  case clang::Type::DependentSizedExtVector:
3778
0
    return eTypeHasChildren | eTypeIsVector;
3779
7.21k
  case clang::Type::DependentTemplateSpecialization:
3780
0
    return eTypeIsTemplate;
3781
7.21k
3782
7.21k
  case clang::Type::Enum:
3783
1.77k
    if (pointee_or_element_clang_type)
3784
0
      pointee_or_element_clang_type->SetCompilerType(
3785
0
          this, llvm::cast<clang::EnumType>(qual_type)
3786
0
                    ->getDecl()
3787
0
                    ->getIntegerType()
3788
0
                    .getAsOpaquePtr());
3789
1.77k
    return eTypeIsEnumeration | eTypeHasValue;
3790
7.21k
3791
7.21k
  case clang::Type::FunctionProto:
3792
60
    return eTypeIsFuncPrototype | eTypeHasValue;
3793
7.21k
  case clang::Type::FunctionNoProto:
3794
0
    return eTypeIsFuncPrototype | eTypeHasValue;
3795
7.21k
  case clang::Type::InjectedClassName:
3796
0
    return 0;
3797
7.21k
3798
7.45k
  case clang::Type::LValueReference:
3799
7.45k
  case clang::Type::RValueReference:
3800
7.45k
    if (pointee_or_element_clang_type)
3801
3
      pointee_or_element_clang_type->SetCompilerType(
3802
3
          this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
3803
3
                    ->getPointeeType()
3804
3
                    .getAsOpaquePtr());
3805
7.45k
    return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
3806
7.45k
3807
7.45k
  case clang::Type::MemberPointer:
3808
0
    return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
3809
7.45k
3810
20.4k
  case clang::Type::ObjCObjectPointer:
3811
20.4k
    if (pointee_or_element_clang_type)
3812
0
      pointee_or_element_clang_type->SetCompilerType(
3813
0
          this, qual_type->getPointeeType().getAsOpaquePtr());
3814
20.4k
    return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
3815
20.4k
           eTypeHasValue;
3816
7.45k
3817
7.45k
  case clang::Type::ObjCObject:
3818
0
    return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3819
7.45k
  case clang::Type::ObjCInterface:
3820
2.71k
    return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
3821
7.45k
3822
174k
  case clang::Type::Pointer:
3823
174k
    if (pointee_or_element_clang_type)
3824
2.77k
      pointee_or_element_clang_type->SetCompilerType(
3825
2.77k
          this, qual_type->getPointeeType().getAsOpaquePtr());
3826
174k
    return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
3827
7.45k
3828
45.4k
  case clang::Type::Record:
3829
45.4k
    if (qual_type->getAsCXXRecordDecl())
3830
45.4k
      return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
3831
0
    else
3832
0
      return eTypeHasChildren | eTypeIsStructUnion;
3833
0
    break;
3834
0
  case clang::Type::SubstTemplateTypeParm:
3835
0
    return eTypeIsTemplate;
3836
0
  case clang::Type::TemplateTypeParm:
3837
0
    return eTypeIsTemplate;
3838
0
  case clang::Type::TemplateSpecialization:
3839
0
    return eTypeIsTemplate;
3840
0
3841
60.9k
  case clang::Type::Typedef:
3842
60.9k
    return eTypeIsTypedef | GetType(llvm::cast<clang::TypedefType>(qual_type)
3843
60.9k
                                        ->getDecl()
3844
60.9k
                                        ->getUnderlyingType())
3845
60.9k
                                .GetTypeInfo(pointee_or_element_clang_type);
3846
0
  case clang::Type::UnresolvedUsing:
3847
0
    return 0;
3848
0
3849
1.14k
  case clang::Type::ExtVector:
3850
1.14k
  case clang::Type::Vector: {
3851
1.14k
    uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
3852
1.14k
    const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
3853
1.14k
        qual_type->getCanonicalTypeInternal());
3854
1.14k
    if (vector_type) {
3855
1.14k
      if (vector_type->isIntegerType())
3856
0
        vector_type_flags |= eTypeIsFloat;
3857
1.14k
      else if (vector_type->isFloatingType())
3858
0
        vector_type_flags |= eTypeIsInteger;
3859
1.14k
    }
3860
1.14k
    return vector_type_flags;
3861
1.14k
  }
3862
1.14k
  default:
3863
0
    return 0;
3864
0
  }
3865
0
  return 0;
3866
0
}
3867
3868
lldb::LanguageType
3869
632k
TypeSystemClang::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
3870
632k
  if (!type)
3871
2.33k
    return lldb::eLanguageTypeC;
3872
630k
3873
630k
  // If the type is a reference, then resolve it to what it refers to first:
3874
630k
  clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
3875
630k
  if (qual_type->isAnyPointerType()) {
3876
314k
    if (qual_type->isObjCObjectPointerType())
3877
31.1k
      return lldb::eLanguageTypeObjC;
3878
283k
    if (qual_type->getPointeeCXXRecordDecl())
3879
249k
      return lldb::eLanguageTypeC_plus_plus;
3880
34.2k
3881
34.2k
    clang::QualType pointee_type(qual_type->getPointeeType());
3882
34.2k
    if (pointee_type->getPointeeCXXRecordDecl())
3883
435
      return lldb::eLanguageTypeC_plus_plus;
3884
33.7k
    if (pointee_type->isObjCObjectOrInterfaceType())
3885
0
      return lldb::eLanguageTypeObjC;
3886
33.7k
    if (pointee_type->isObjCClassType())
3887
0
      return lldb::eLanguageTypeObjC;
3888
33.7k
    if (pointee_type.getTypePtr() ==
3889
33.7k
        getASTContext().ObjCBuiltinIdTy.getTypePtr())
3890
141
      return lldb::eLanguageTypeObjC;
3891
315k
  } else {
3892
315k
    if (qual_type->isObjCObjectOrInterfaceType())
3893
5.43k
      return lldb::eLanguageTypeObjC;
3894
310k
    if (qual_type->getAsCXXRecordDecl())
3895
132k
      return lldb::eLanguageTypeC_plus_plus;
3896
177k
    switch (qual_type->getTypeClass()) {
3897
19.9k
    default:
3898
19.9k
      break;
3899
157k
    case clang::Type::Builtin:
3900
157k
      switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3901
46
      default:
3902
156k
      case clang::BuiltinType::Void:
3903
156k
      case clang::BuiltinType::Bool:
3904
156k
      case clang::BuiltinType::Char_U:
3905
156k
      case clang::BuiltinType::UChar:
3906
156k
      case clang::BuiltinType::WChar_U:
3907
156k
      case clang::BuiltinType::Char16:
3908
156k
      case clang::BuiltinType::Char32:
3909
156k
      case clang::BuiltinType::UShort:
3910
156k
      case clang::BuiltinType::UInt:
3911
156k
      case clang::BuiltinType::ULong:
3912
156k
      case clang::BuiltinType::ULongLong:
3913
156k
      case clang::BuiltinType::UInt128:
3914
156k
      case clang::BuiltinType::Char_S:
3915
156k
      case clang::BuiltinType::SChar:
3916
156k
      case clang::BuiltinType::WChar_S:
3917
156k
      case clang::BuiltinType::Short:
3918
156k
      case clang::BuiltinType::Int:
3919
156k
      case clang::BuiltinType::Long:
3920
156k
      case clang::BuiltinType::LongLong:
3921
156k
      case clang::BuiltinType::Int128:
3922
156k
      case clang::BuiltinType::Float:
3923
156k
      case clang::BuiltinType::Double:
3924
156k
      case clang::BuiltinType::LongDouble:
3925
156k
        break;
3926
156k
3927
156k
      case clang::BuiltinType::NullPtr:
3928
0
        return eLanguageTypeC_plus_plus;
3929
156k
3930
156k
      case clang::BuiltinType::ObjCId:
3931
1.44k
      case clang::BuiltinType::ObjCClass:
3932
1.44k
      case clang::BuiltinType::ObjCSel:
3933
1.44k
        return eLanguageTypeObjC;
3934
1.44k
3935
1.44k
      case clang::BuiltinType::Dependent:
3936
0
      case clang::BuiltinType::Overload:
3937
0
      case clang::BuiltinType::BoundMember:
3938
0
      case clang::BuiltinType::UnknownAny:
3939
0
        break;
3940
156k
      }
3941
156k
      break;
3942
156k
    case clang::Type::Typedef:
3943
0
      return GetType(llvm::cast<clang::TypedefType>(qual_type)
3944
0
                         ->getDecl()
3945
0
                         ->getUnderlyingType())
3946
0
          .GetMinimumLanguage();
3947
209k
    }
3948
209k
  }
3949
209k
  return lldb::eLanguageTypeC;
3950
209k
}
3951
3952
lldb::TypeClass
3953
22
TypeSystemClang::GetTypeClass(lldb::opaque_compiler_type_t type) {
3954
22
  if (!type)
3955
0
    return lldb::eTypeClassInvalid;
3956
22
3957
22
  clang::QualType qual_type =
3958
22
      RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef});
3959
22
3960
22
  switch (qual_type->getTypeClass()) {
3961
0
  case clang::Type::Atomic:
3962
0
  case clang::Type::Auto:
3963
0
  case clang::Type::Decltype:
3964
0
  case clang::Type::Elaborated:
3965
0
  case clang::Type::Paren:
3966
0
  case clang::Type::TypeOf:
3967
0
  case clang::Type::TypeOfExpr:
3968
0
    llvm_unreachable("Handled in RemoveWrappingTypes!");
3969
0
  case clang::Type::UnaryTransform:
3970
0
    break;
3971
0
  case clang::Type::FunctionNoProto:
3972
0
    return lldb::eTypeClassFunction;
3973
0
  case clang::Type::FunctionProto:
3974
0
    return lldb::eTypeClassFunction;
3975
0
  case clang::Type::IncompleteArray:
3976
0
    return lldb::eTypeClassArray;
3977
0
  case clang::Type::VariableArray:
3978
0
    return lldb::eTypeClassArray;
3979
0
  case clang::Type::ConstantArray:
3980
0
    return lldb::eTypeClassArray;
3981
0
  case clang::Type::DependentSizedArray:
3982
0
    return lldb::eTypeClassArray;
3983
0
  case clang::Type::DependentSizedExtVector:
3984
0
    return lldb::eTypeClassVector;
3985
0
  case clang::Type::DependentVector:
3986
0
    return lldb::eTypeClassVector;
3987
0
  case clang::Type::ExtVector:
3988
0
    return lldb::eTypeClassVector;
3989
0
  case clang::Type::Vector:
3990
0
    return lldb::eTypeClassVector;
3991
4
  case clang::Type::Builtin:
3992
4
  // Ext-Int is just an integer type.
3993
4
  case clang::Type::ExtInt:
3994
4
  case clang::Type::DependentExtInt:
3995
4
    return lldb::eTypeClassBuiltin;
3996
4
  case clang::Type::ObjCObjectPointer:
3997
3
    return lldb::eTypeClassObjCObjectPointer;
3998
4
  case clang::Type::BlockPointer:
3999
0
    return lldb::eTypeClassBlockPointer;
4000
4
  case clang::Type::Pointer:
4001
4
    return lldb::eTypeClassPointer;
4002
4
  case clang::Type::LValueReference:
4003
0
    return lldb::eTypeClassReference;
4004
4
  case clang::Type::RValueReference:
4005
0
    return lldb::eTypeClassReference;
4006
4
  case clang::Type::MemberPointer:
4007
0
    return lldb::eTypeClassMemberPointer;
4008
4
  case clang::Type::Complex:
4009
0
    if (qual_type->isComplexType())
4010
0
      return lldb::eTypeClassComplexFloat;
4011
0
    else
4012
0
      return lldb::eTypeClassComplexInteger;
4013
0
  case clang::Type::ObjCObject:
4014
0
    return lldb::eTypeClassObjCObject;
4015
0
  case clang::Type::ObjCInterface:
4016
0
    return lldb::eTypeClassObjCInterface;
4017
10
  case clang::Type::Record: {
4018
10
    const clang::RecordType *record_type =
4019
10
        llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4020
10
    const clang::RecordDecl *record_decl = record_type->getDecl();
4021
10
    if (record_decl->isUnion())
4022
1
      return lldb::eTypeClassUnion;
4023
9
    else if (record_decl->isStruct())
4024
5
      return lldb::eTypeClassStruct;
4025
4
    else
4026
4
      return lldb::eTypeClassClass;
4027
0
  } break;
4028
0
  case clang::Type::Enum:
4029
0
    return lldb::eTypeClassEnumeration;
4030
1
  case clang::Type::Typedef:
4031
1
    return lldb::eTypeClassTypedef;
4032
0
  case clang::Type::UnresolvedUsing:
4033
0
    break;
4034
0
4035
0
  case clang::Type::Attributed:
4036
0
    break;
4037
0
  case clang::Type::TemplateTypeParm:
4038
0
    break;
4039
0
  case clang::Type::SubstTemplateTypeParm:
4040
0
    break;
4041
0
  case clang::Type::SubstTemplateTypeParmPack:
4042
0
    break;
4043
0
  case clang::Type::InjectedClassName:
4044
0
    break;
4045
0
  case clang::Type::DependentName:
4046
0
    break;
4047
0
  case clang::Type::DependentTemplateSpecialization:
4048
0
    break;
4049
0
  case clang::Type::PackExpansion:
4050
0
    break;
4051
0
4052
0
  case clang::Type::TemplateSpecialization:
4053
0
    break;
4054
0
  case clang::Type::DeducedTemplateSpecialization:
4055
0
    break;
4056
0
  case clang::Type::Pipe:
4057
0
    break;
4058
0
4059
0
  // pointer type decayed from an array or function type.
4060
0
  case clang::Type::Decayed:
4061
0
    break;
4062
0
  case clang::Type::Adjusted:
4063
0
    break;
4064
0
  case clang::Type::ObjCTypeParam:
4065
0
    break;
4066
0
4067
0
  case clang::Type::DependentAddressSpace:
4068
0
    break;
4069
0
  case clang::Type::MacroQualified:
4070
0
    break;
4071
0
4072
0
  // Matrix types that we're not sure how to display at the moment.
4073
0
  case clang::Type::ConstantMatrix:
4074
0
  case clang::Type::DependentSizedMatrix:
4075
0
    break;
4076
0
  }
4077
0
  // We don't know hot to display this type...
4078
0
  return lldb::eTypeClassOther;
4079
0
}
4080
4081
0
unsigned TypeSystemClang::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4082
0
  if (type)
4083
0
    return GetQualType(type).getQualifiers().getCVRQualifiers();
4084
0
  return 0;
4085
0
}
4086
4087
// Creating related types
4088
4089
CompilerType
4090
TypeSystemClang::GetArrayElementType(lldb::opaque_compiler_type_t type,
4091
752
                                     uint64_t *stride) {
4092
752
  if (type) {
4093
752
    clang::QualType qual_type(GetQualType(type));
4094
752
4095
752
    const clang::Type *array_eletype =
4096
752
        qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4097
752
4098
752
    if (!array_eletype)
4099
0
      return CompilerType();
4100
752
4101
752
    CompilerType element_type = GetType(clang::QualType(array_eletype, 0));
4102
752
4103
752
    // TODO: the real stride will be >= this value.. find the real one!
4104
752
    if (stride)
4105
0
      if (Optional<uint64_t> size = element_type.GetByteSize(nullptr))
4106
0
        *stride = *size;
4107
752
4108
752
    return element_type;
4109
752
  }
4110
0
  return CompilerType();
4111
0
}
4112
4113
CompilerType TypeSystemClang::GetArrayType(lldb::opaque_compiler_type_t type,
4114
124
                                           uint64_t size) {
4115
124
  if (type) {
4116
124
    clang::QualType qual_type(GetCanonicalQualType(type));
4117
124
    clang::ASTContext &ast_ctx = getASTContext();
4118
124
    if (size != 0)
4119
124
      return GetType(ast_ctx.getConstantArrayType(
4120
124
          qual_type, llvm::APInt(64, size), nullptr,
4121
124
          clang::ArrayType::ArraySizeModifier::Normal, 0));
4122
0
    else
4123
0
      return GetType(ast_ctx.getIncompleteArrayType(
4124
0
          qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
4125
0
  }
4126
0
4127
0
  return CompilerType();
4128
0
}
4129
4130
CompilerType
4131
42
TypeSystemClang::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4132
42
  if (type)
4133
42
    return GetType(GetCanonicalQualType(type));
4134
0
  return CompilerType();
4135
0
}
4136
4137
static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4138
21.0k
                                                    clang::QualType qual_type) {
4139
21.0k
  if (qual_type->isPointerType())
4140
3.86k
    qual_type = ast->getPointerType(
4141
3.86k
        GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4142
17.1k
  else
4143
17.1k
    qual_type = qual_type.getUnqualifiedType();
4144
21.0k
  qual_type.removeLocalConst();
4145
21.0k
  qual_type.removeLocalRestrict();
4146
21.0k
  qual_type.removeLocalVolatile();
4147
21.0k
  return qual_type;
4148
21.0k
}
4149
4150
CompilerType
4151
17.1k
TypeSystemClang::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4152
17.1k
  if (type)
4153
17.1k
    return GetType(
4154
17.1k
        GetFullyUnqualifiedType_Impl(&getASTContext(), GetQualType(type)));
4155
0
  return CompilerType();
4156
0
}
4157
4158
int TypeSystemClang::GetFunctionArgumentCount(
4159
0
    lldb::opaque_compiler_type_t type) {
4160
0
  if (type) {
4161
0
    const clang::FunctionProtoType *func =
4162
0
        llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4163
0
    if (func)
4164
0
      return func->getNumParams();
4165
0
  }
4166
0
  return -1;
4167
0
}
4168
4169
CompilerType TypeSystemClang::GetFunctionArgumentTypeAtIndex(
4170
0
    lldb::opaque_compiler_type_t type, size_t idx) {
4171
0
  if (type) {
4172
0
    const clang::FunctionProtoType *func =
4173
0
        llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4174
0
    if (func) {
4175
0
      const uint32_t num_args = func->getNumParams();
4176
0
      if (idx < num_args)
4177
0
        return GetType(func->getParamType(idx));
4178
0
    }
4179
0
  }
4180
0
  return CompilerType();
4181
0
}
4182
4183
CompilerType
4184
172
TypeSystemClang::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4185
172
  if (type) {
4186
172
    clang::QualType qual_type(GetQualType(type));
4187
172
    const clang::FunctionProtoType *func =
4188
172
        llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4189
172
    if (func)
4190
172
      return GetType(func->getReturnType());
4191
0
  }
4192
0
  return CompilerType();
4193
0
}
4194
4195
size_t
4196
9
TypeSystemClang::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4197
9
  size_t num_functions = 0;
4198
9
  if (type) {
4199
9
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4200
9
    switch (qual_type->getTypeClass()) {
4201
6
    case clang::Type::Record:
4202
6
      if (GetCompleteQualType(&getASTContext(), qual_type)) {
4203
6
        const clang::RecordType *record_type =
4204
6
            llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4205
6
        const clang::RecordDecl *record_decl = record_type->getDecl();
4206
6
        assert(record_decl);
4207
6
        const clang::CXXRecordDecl *cxx_record_decl =
4208
6
            llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4209
6
        if (cxx_record_decl)
4210
6
          num_functions = std::distance(cxx_record_decl->method_begin(),
4211
6
                                        cxx_record_decl->method_end());
4212
6
      }
4213
6
      break;
4214
0
4215
3
    case clang::Type::ObjCObjectPointer: {
4216
3
      const clang::ObjCObjectPointerType *objc_class_type =
4217
3
          qual_type->getAs<clang::ObjCObjectPointerType>();
4218
3
      const clang::ObjCInterfaceType *objc_interface_type =
4219
3
          objc_class_type->getInterfaceType();
4220
3
      if (objc_interface_type &&
4221
3
          GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4222
3
              const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4223
3
        clang::ObjCInterfaceDecl *class_interface_decl =
4224
3
            objc_interface_type->getDecl();
4225
3
        if (class_interface_decl) {
4226
3
          num_functions = std::distance(class_interface_decl->meth_begin(),
4227
3
                                        class_interface_decl->meth_end());
4228
3
        }
4229
3
      }
4230
3
      break;
4231
0
    }
4232
0
4233
0
    case clang::Type::ObjCObject:
4234
0
    case clang::Type::ObjCInterface:
4235
0
      if (GetCompleteType(type)) {
4236
0
        const clang::ObjCObjectType *objc_class_type =
4237
0
            llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4238
0
        if (objc_class_type) {
4239
0
          clang::ObjCInterfaceDecl *class_interface_decl =
4240
0
              objc_class_type->getInterface();
4241
0
          if (class_interface_decl)
4242
0
            num_functions = std::distance(class_interface_decl->meth_begin(),
4243
0
                                          class_interface_decl->meth_end());
4244
0
        }
4245
0
      }
4246
0
      break;
4247
0
4248
0
    default:
4249
0
      break;
4250
9
    }
4251
9
  }
4252
9
  return num_functions;
4253
9
}
4254
4255
TypeMemberFunctionImpl
4256
TypeSystemClang::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4257
66
                                          size_t idx) {
4258
66
  std::string name;
4259
66
  MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4260
66
  CompilerType clang_type;
4261
66
  CompilerDecl clang_decl;
4262
66
  if (type) {
4263
66
    clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4264
66
    switch (qual_type->getTypeClass()) {
4265
57
    case clang::Type::Record:
4266
57
      if (GetCompleteQualType(&getASTContext(), qual_type)) {
4267
57
        const clang::RecordType *record_type =
4268
57
            llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4269
57
        const clang::RecordDecl *record_decl = record_type->getDecl();
4270
57
        assert(record_decl);
4271
57
        const clang::CXXRecordDecl *cxx_record_decl =
4272
57
            llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4273
57
        if (cxx_record_decl) {
4274
57
          auto method_iter = cxx_record_decl->method_begin();
4275
57
          auto method_end = cxx_record_decl->method_end();
4276
57
          if (idx <
4277
57
              static_cast<size_t>(std::distance(method_iter, method_end))) {
4278
57
            std::advance(method_iter, idx);
4279
57
            clang::CXXMethodDecl *cxx_method_decl =
4280
57
                method_iter->getCanonicalDecl();
4281
57
            if (cxx_method_decl) {
4282
57
              name = cxx_method_decl->getDeclName().getAsString();
4283
57
              if (cxx_method_decl->isStatic())
4284
15
                kind = lldb::eMemberFunctionKindStaticMethod;
4285
42
              else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4286
0
                kind = lldb::eMemberFunctionKindConstructor;
4287
42
              else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4288
0
                kind = lldb::eMemberFunctionKindDestructor;
4289
42
              else
4290
42
                kind = lldb::eMemberFunctionKindInstanceMethod;
4291
57
              clang_type = GetType(cxx_method_decl->getType());
4292
57
              clang_decl = GetCompilerDecl(cxx_method_decl);
4293
57
            }
4294
57
          }
4295
57
        }
4296
57
      }
4297
57
      break;
4298
0
4299
9
    case clang::Type::ObjCObjectPointer: {
4300
9
      const clang::ObjCObjectPointerType *objc_class_type =
4301
9
          qual_type->getAs<clang::ObjCObjectPointerType>();
4302
9
      const clang::ObjCInterfaceType *objc_interface_type =
4303
9
          objc_class_type->getInterfaceType();
4304
9
      if (objc_interface_type &&
4305
9
          GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4306
9
              const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
4307
9
        clang::ObjCInterfaceDecl *class_interface_decl =
4308
9
            objc_interface_type->getDecl();
4309
9
        if (class_interface_decl) {
4310
9
          auto method_iter = class_interface_decl->meth_begin();
4311
9
          auto method_end = class_interface_decl->meth_end();
4312
9
          if (idx <
4313
9
              static_cast<size_t>(std::distance(method_iter, method_end))) {
4314
9
            std::advance(method_iter, idx);
4315
9
            clang::ObjCMethodDecl *objc_method_decl =
4316
9
                method_iter->getCanonicalDecl();
4317
9
            if (objc_method_decl) {
4318
9
              clang_decl = GetCompilerDecl(objc_method_decl);
4319
9
              name = objc_method_decl->getSelector().getAsString();
4320
9
              if (objc_method_decl->isClassMethod())
4321
0
                kind = lldb::eMemberFunctionKindStaticMethod;
4322
9
              else
4323
9
                kind = lldb::eMemberFunctionKindInstanceMethod;
4324
9
            }
4325
9
          }
4326
9
        }
4327
9
      }
4328
9
      break;
4329
0
    }
4330
0
4331
0
    case clang::Type::ObjCObject:
4332
0
    case clang::Type::ObjCInterface:
4333
0
      if (GetCompleteType(type)) {
4334
0
        const clang::ObjCObjectType *objc_class_type =
4335
0
            llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4336
0
        if (objc_class_type) {
4337
0
          clang::ObjCInterfaceDecl *class_interface_decl =
4338
0
              objc_class_type->getInterface();
4339
0
          if (class_interface_decl) {
4340
0
            auto method_iter = class_interface_decl->meth_begin();
4341
0
            auto method_end = class_interface_decl->meth_end();
4342
0
            if (idx <
4343
0
                static_cast<size_t>(std::distance(method_iter, method_end))) {
4344
0
              std::advance(method_iter, idx);
4345
0
              clang::ObjCMethodDecl *objc_method_decl =
4346
0
                  method_iter->getCanonicalDecl();
4347
0
              if (objc_method_decl) {
4348
0
                clang_decl = GetCompilerDecl(objc_method_decl);
4349
0
                name = objc_method_decl->getSelector().getAsString();
4350
0
                if (objc_method_decl->isClassMethod())
4351
0
                  kind = lldb::eMemberFunctionKindStaticMethod;
4352
0
                else
4353
0
                  kind = lldb::eMemberFunctionKindInstanceMethod;
4354
0
              }
4355
0
            }
4356
0
          }
4357
0
        }
4358
0
      }
4359
0
      break;
4360
0
4361
0
    default:
4362
0
      break;
4363
66
    }
4364
66
  }
4365
66
4366
66
  if (kind == eMemberFunctionKindUnknown)
4367
0
    return TypeMemberFunctionImpl();
4368
66
  else
4369
66
    return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
4370
66
}
4371
4372
CompilerType
4373
537
TypeSystemClang::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4374
537
  if (type)
4375
537
    return GetType(GetQualType(type).getNonReferenceType());
4376
0
  return CompilerType();
4377
0
}
4378
4379
CompilerType TypeSystemClang::CreateTypedefType(
4380
    const CompilerType &type, const char *typedef_name,
4381
1
    const CompilerDeclContext &compiler_decl_ctx, uint32_t payload) {
4382
1
  if (type && typedef_name && typedef_name[0]) {
4383
1
    TypeSystemClang *ast =
4384
1
        llvm::dyn_cast<TypeSystemClang>(type.GetTypeSystem());
4385
1
    if (!ast)
4386
0
      return CompilerType();
4387
1
    clang::ASTContext &clang_ast = ast->getASTContext();
4388
1
    clang::QualType qual_type(ClangUtil::GetQualType(type));
4389
1
4390
1
    clang::DeclContext *decl_ctx =
4391
1
        TypeSystemClang::DeclContextGetAsDeclContext(compiler_decl_ctx);
4392
1
    if (!decl_ctx)
4393
0
      decl_ctx = ast->getASTContext().getTranslationUnitDecl();
4394
1
4395
1
    clang::TypedefDecl *decl =
4396
1
        clang::TypedefDecl::CreateDeserialized(clang_ast, 0);
4397
1
    decl->setDeclContext(decl_ctx);
4398
1
    decl->setDeclName(&clang_ast.Idents.get(typedef_name));
4399
1
    decl->setTypeSourceInfo(clang_ast.getTrivialTypeSourceInfo(qual_type));
4400
1
4401
1
    SetOwningModule(decl, TypePayloadClang(payload).GetOwningModule());
4402
1
    decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4403
1
4404
1
    decl_ctx->addDecl(decl);
4405
1
4406
1
    // Get a uniqued clang::QualType for the typedef decl type
4407
1
    return ast->GetType(clang_ast.getTypedefType(decl));
4408
1
  }
4409
0
  return CompilerType();
4410
0
}
4411
4412
CompilerType
4413
60.7k
TypeSystemClang::GetPointeeType(lldb::opaque_compiler_type_t type) {
4414
60.7k
  if (type) {
4415
60.7k
    clang::QualType qual_type(GetQualType(type));
4416
60.7k
    return GetType(qual_type.getTypePtr()->getPointeeType());
4417
60.7k
  }
4418
0
  return CompilerType();
4419
0
}
4420
4421
CompilerType
4422
22.2k
TypeSystemClang::GetPointerType(lldb::opaque_compiler_type_t type) {
4423
22.2k
  if (type) {
4424
22.2k
    clang::QualType qual_type(GetQualType(type));
4425
22.2k
4426
22.2k
    switch (qual_type.getDesugaredType(getASTContext())->getTypeClass()) {
4427
2.83k
    case clang::Type::ObjCObject:
4428
2.83k
    case clang::Type::ObjCInterface:
4429
2.83k
      return GetType(getASTContext().getObjCObjectPointerType(qual_type));
4430
2.83k
4431
19.4k
    default:
4432
19.4k
      return GetType(getASTContext().getPointerType(qual_type));
4433
0
    }
4434
0
  }
4435
0
  return CompilerType();
4436
0
}
4437
4438
CompilerType
4439
22.9k
TypeSystemClang::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4440
22.9k
  if (type)
4441
22.9k
    return GetType(getASTContext().getLValueReferenceType(GetQualType(type)));
4442
0
  else
4443
0
    return CompilerType();
4444
22.9k
}
4445
4446
CompilerType
4447
1.78k
TypeSystemClang::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4448
1.78k
  if (type)
4449
1.78k
    return GetType(getASTContext().getRValueReferenceType(GetQualType(type)));
4450
0
  else
4451
0
    return CompilerType();
4452
1.78k
}
4453
4454
2
CompilerType TypeSystemClang::GetAtomicType(lldb::opaque_compiler_type_t type) {
4455
2
  if (!type)
4456
0
    return CompilerType();
4457
2
  return GetType(getASTContext().getAtomicType(GetQualType(type)));
4458
2
}
4459
4460
CompilerType
4461
5.41k
TypeSystemClang::AddConstModifier(lldb::opaque_compiler_type_t type) {
4462
5.41k
  if (type) {
4463
5.41k
    clang::QualType result(GetQualType(type));
4464
5.41k
    result.addConst();
4465
5.41k
    return GetType(result);
4466
5.41k
  }
4467
0
  return CompilerType();
4468
0
}
4469
4470
CompilerType
4471
26
TypeSystemClang::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4472
26
  if (type) {
4473
26
    clang::QualType result(GetQualType(type));
4474
26
    result.addVolatile();
4475
26
    return GetType(result);
4476
26
  }
4477
0
  return CompilerType();
4478
0
}
4479
4480
CompilerType
4481
1
TypeSystemClang::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4482
1
  if (type) {
4483
1
    clang::QualType result(GetQualType(type));
4484
1
    result.addRestrict();
4485
1
    return GetType(result);
4486
1
  }
4487
0
  return CompilerType();
4488
0
}
4489
4490
CompilerType TypeSystemClang::CreateTypedef(
4491
    lldb::opaque_compiler_type_t type, const char *typedef_name,
4492
13.8k
    const CompilerDeclContext &compiler_decl_ctx, uint32_t payload) {
4493
13.8k
  if (type) {
4494
13.8k
    clang::ASTContext &clang_ast = getASTContext();
4495
13.8k
    clang::QualType qual_type(GetQualType(type));
4496
13.8k
4497
13.8k
    clang::DeclContext *decl_ctx =
4498
13.8k
        TypeSystemClang::DeclContextGetAsDeclContext(compiler_decl_ctx);
4499
13.8k
    if (!decl_ctx)
4500
0
      decl_ctx = getASTContext().getTranslationUnitDecl();
4501
13.8k
4502
13.8k
    clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4503
13.8k
        clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4504
13.8k
        &clang_ast.Idents.get(typedef_name),
4505
13.8k
        clang_ast.getTrivialTypeSourceInfo(qual_type));
4506
13.8k
    SetOwningModule(decl, TypePayloadClang(payload).GetOwningModule());
4507
13.8k
4508
13.8k
    clang::TagDecl *tdecl = nullptr;
4509
13.8k
    if (!qual_type.isNull()) {
4510
13.8k
      if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4511
4.88k
        tdecl = rt->getDecl();
4512
13.8k
      if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4513
30
        tdecl = et->getDecl();
4514
13.8k
    }
4515
13.8k
4516
13.8k
    // Check whether this declaration is an anonymous struct, union, or enum,
4517
13.8k
    // hidden behind a typedef. If so, we try to check whether we have a
4518
13.8k
    // typedef tag to attach to the original record declaration
4519
13.8k
    if (tdecl && 
!tdecl->getIdentifier()4.91k
&&
!tdecl->getTypedefNameForAnonDecl()84
)
4520
84
      tdecl->setTypedefNameForAnonDecl(decl);
4521
13.8k
4522
13.8k
    decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4523
13.8k
4524
13.8k
    // Get a uniqued clang::QualType for the typedef decl type
4525
13.8k
    return GetType(clang_ast.getTypedefType(decl));
4526
13.8k
  }
4527
0
  return CompilerType();
4528
0
}
4529
4530
CompilerType
4531
3.51k
TypeSystemClang::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4532
3.51k
  if (type) {
4533
3.51k
    const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(
4534
3.51k
        RemoveWrappingTypes(GetQualType(type), {clang::Type::Typedef}));
4535
3.51k
    if (typedef_type)
4536
3.48k
      return GetType(typedef_type->getDecl()->getUnderlyingType());
4537
30
  }
4538
30
  return CompilerType();
4539
30
}
4540
4541
// Create related types using the current type's AST
4542
4543
906
CompilerType TypeSystemClang::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4544
906
  return TypeSystemClang::GetBasicType(basic_type);
4545
906
}
4546
// Exploring the type
4547
4548
const llvm::fltSemantics &
4549
1.92k
TypeSystemClang::GetFloatTypeSemantics(size_t byte_size) {
4550
1.92k
  clang::ASTContext &ast = getASTContext();
4551
1.92k
  const size_t bit_size = byte_size * 8;
4552
1.92k
  if (bit_size == ast.getTypeSize(ast.FloatTy))
4553
987
    return ast.getFloatTypeSemantics(ast.FloatTy);
4554
938
  else if (bit_size == ast.getTypeSize(ast.DoubleTy))
4555
924
    return ast.getFloatTypeSemantics(ast.DoubleTy);
4556
14
  else if (bit_size == ast.getTypeSize(ast.LongDoubleTy))
4557
9
    return ast.getFloatTypeSemantics(ast.LongDoubleTy);
4558
5
  else if (bit_size == ast.getTypeSize(ast.HalfTy))
4559
1
    return ast.getFloatTypeSemantics(ast.HalfTy);
4560
4
  return llvm::APFloatBase::Bogus();
4561
4
}
4562
4563
Optional<uint64_t>
4564
TypeSystemClang::GetBitSize(lldb::opaque_compiler_type_t type,
4565
403k
                            ExecutionContextScope *exe_scope) {
4566
403k
  if (GetCompleteType(type)) {
4567
403k
    clang::QualType qual_type(GetCanonicalQualType(type));
4568
403k
    const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4569
403k
    switch (type_class) {
4570
15.2k
    case clang::Type::Record:
4571
15.2k
      if (GetCompleteType(type))
4572
15.2k
        return getASTContext().getTypeSize(qual_type);
4573
0
      else
4574
0
        return None;
4575
0
      break;
4576
0
4577
203
    case clang::Type::ObjCInterface:
4578
203
    case clang::Type::ObjCObject: {
4579
203
      ExecutionContext exe_ctx(exe_scope);
4580
203
      Process *process = exe_ctx.GetProcessPtr();
4581
203
      if (process) {
4582
202
        ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process);
4583
202
        if (objc_runtime) {
4584
202
          uint64_t bit_size = 0;
4585
202
          if (objc_runtime->GetTypeBitSize(GetType(qual_type), bit_size))
4586
139
            return bit_size;
4587
1
        }
4588
1
      } else {
4589
1
        static bool g_printed = false;
4590
1
        if (!g_printed) {
4591
1
          StreamString s;
4592
1
          DumpTypeDescription(type, &s);
4593
1
4594
1
          llvm::outs() << "warning: trying to determine the size of type ";
4595
1
          llvm::outs() << s.GetString() << "\n";
4596
1
          llvm::outs() << "without a valid ExecutionContext. this is not "
4597
1
                          "reliable. please file a bug against LLDB.\n";
4598
1
          llvm::outs() << "backtrace:\n";
4599
1
          llvm::sys::PrintStackTrace(llvm::outs());
4600
1
          llvm::outs() << "\n";
4601
1
          g_printed = true;
4602
1
        }
4603
1
      }
4604
203
    }
4605
203
      
LLVM_FALLTHROUGH64
;
4606
387k
    default:
4607
387k
      const uint32_t bit_size = getASTContext().getTypeSize(qual_type);
4608
387k
      if (bit_size == 0) {
4609
100
        if (qual_type->isIncompleteArrayType())
4610
6
          return getASTContext().getTypeSize(
4611
6
              qual_type->getArrayElementTypeNoTypeQual()
4612
6
                  ->getCanonicalTypeUnqualified());
4613
387k
      }
4614
387k
      if (qual_type->isObjCObjectOrInterfaceType())
4615
64
        return bit_size +
4616
64
               getASTContext().getTypeSize(getASTContext().ObjCBuiltinClassTy);
4617
387k
      // Function types actually have a size of 0, that's not an error.
4618
387k
      if (qual_type->isFunctionProtoType())
4619
75
        return bit_size;
4620
387k
      if (bit_size)
4621
387k
        return bit_size;
4622
202
    }
4623
202
  }
4624
202
  return None;
4625
202
}
4626
4627
llvm::Optional<size_t>
4628
TypeSystemClang::GetTypeBitAlign(lldb::opaque_compiler_type_t type,
4629
16.6k
                                 ExecutionContextScope *exe_scope) {
4630
16.6k
  if (GetCompleteType(type))
4631
16.6k
    return getASTContext().getTypeAlign(GetQualType(type));
4632
0
  return {};
4633
0
}
4634
4635
lldb::Encoding TypeSystemClang::GetEncoding(lldb::opaque_compiler_type_t type,
4636
15.0k
                                            uint64_t &count) {
4637
15.0k
  if (!type)
4638
0
    return lldb::eEncodingInvalid;
4639
15.0k
4640
15.0k
  count = 1;
4641
15.0k
  clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4642
15.0k
4643
15.0k
  switch (qual_type->getTypeClass()) {
4644
0
  case clang::Type::Atomic:
4645
0
  case clang::Type::Auto:
4646
0
  case clang::Type::Decltype:
4647
0
  case clang::Type::Elaborated:
4648
0
  case clang::Type::Paren:
4649
0
  case clang::Type::Typedef:
4650
0
  case clang::Type::TypeOf:
4651
0
  case clang::Type::TypeOfExpr:
4652
0
    llvm_unreachable("Handled in RemoveWrappingTypes!");
4653
0
4654
0
  case clang::Type::UnaryTransform:
4655
0
    break;
4656
0
4657
0
  case clang::Type::FunctionNoProto:
4658
0
  case clang::Type::FunctionProto:
4659
0
    break;
4660
0
4661
0
  case clang::Type::IncompleteArray:
4662
0
  case clang::Type::VariableArray:
4663
0
    break;
4664
0
4665
0
  case clang::Type::ConstantArray:
4666
0
    break;
4667
0
4668
0
  case clang::Type::DependentVector:
4669
0
  case clang::Type::ExtVector:
4670
0
  case clang::Type::Vector:
4671
0
    // TODO: Set this to more than one???
4672
0
    break;
4673
0
4674
0
  case clang::Type::ExtInt:
4675
0
  case clang::Type::DependentExtInt:
4676
0
    return qual_type->isUnsignedIntegerType() ? lldb::eEncodingUint
4677
0
                                              : lldb::eEncodingSint;
4678
0
4679
6.48k
  case clang::Type::Builtin:
4680
6.48k
    switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4681
0
    case clang::BuiltinType::Void:
4682
0
      break;
4683
0
4684
2.90k
    case clang::BuiltinType::Bool:
4685
2.90k
    case clang::BuiltinType::Char_S:
4686
2.90k
    case clang::BuiltinType::SChar:
4687
2.90k
    case clang::BuiltinType::WChar_S:
4688
2.90k
    case clang::BuiltinType::Short:
4689
2.90k
    case clang::BuiltinType::Int:
4690
2.90k
    case clang::BuiltinType::Long:
4691
2.90k
    case clang::BuiltinType::LongLong:
4692
2.90k
    case clang::BuiltinType::Int128:
4693
2.90k
      return lldb::eEncodingSint;
4694
2.90k
4695
3.47k
    case clang::BuiltinType::Char_U:
4696
3.47k
    case clang::BuiltinType::UChar:
4697
3.47k
    case clang::BuiltinType::WChar_U:
4698
3.47k
    case clang::BuiltinType::Char8:
4699
3.47k
    case clang::BuiltinType::Char16:
4700
3.47k
    case clang::BuiltinType::Char32:
4701
3.47k
    case clang::BuiltinType::UShort:
4702
3.47k
    case clang::BuiltinType::UInt:
4703
3.47k
    case clang::BuiltinType::ULong:
4704
3.47k
    case clang::BuiltinType::ULongLong:
4705
3.47k
    case clang::BuiltinType::UInt128:
4706
3.47k
      return lldb::eEncodingUint;
4707
3.47k
4708
3.47k
    // Fixed point types. Note that they are currently ignored.
4709
3.47k
    case clang::BuiltinType::ShortAccum:
4710
0
    case clang::BuiltinType::Accum:
4711
0
    case clang::BuiltinType::LongAccum:
4712
0
    case clang::BuiltinType::UShortAccum:
4713
0
    case clang::BuiltinType::UAccum:
4714
0
    case clang::BuiltinType::ULongAccum:
4715
0
    case clang::BuiltinType::ShortFract:
4716
0
    case clang::BuiltinType::Fract:
4717
0
    case clang::BuiltinType::LongFract:
4718
0
    case clang::BuiltinType::UShortFract:
4719
0
    case clang::BuiltinType::UFract:
4720
0
    case clang::BuiltinType::ULongFract:
4721
0
    case clang::BuiltinType::SatShortAccum:
4722
0
    case clang::BuiltinType::SatAccum:
4723
0
    case clang::BuiltinType::SatLongAccum:
4724
0
    case clang::BuiltinType::SatUShortAccum:
4725
0
    case clang::BuiltinType::SatUAccum:
4726
0
    case clang::BuiltinType::SatULongAccum:
4727
0
    case clang::BuiltinType::SatShortFract:
4728
0
    case clang::BuiltinType::SatFract:
4729
0
    case clang::BuiltinType::SatLongFract:
4730
0
    case clang::BuiltinType::SatUShortFract:
4731
0
    case clang::BuiltinType::SatUFract:
4732
0
    case clang::BuiltinType::SatULongFract:
4733
0
      break;
4734
0
4735
48
    case clang::BuiltinType::Half:
4736
48
    case clang::BuiltinType::Float:
4737
48
    case clang::BuiltinType::Float16:
4738
48
    case clang::BuiltinType::Float128:
4739
48
    case clang::BuiltinType::Double:
4740
48
    case clang::BuiltinType::LongDouble:
4741
48
    case clang::BuiltinType::BFloat16:
4742
48
      return lldb::eEncodingIEEE754;
4743
48
4744
54
    case clang::BuiltinType::ObjCClass:
4745
54
    case clang::BuiltinType::ObjCId:
4746
54
    case clang::BuiltinType::ObjCSel:
4747
54
      return lldb::eEncodingUint;
4748
54
4749
54
    case clang::BuiltinType::NullPtr:
4750
0
      return lldb::eEncodingUint;
4751
54
4752
54
    case clang::BuiltinType::Kind::ARCUnbridgedCast:
4753
0
    case clang::BuiltinType::Kind::BoundMember:
4754
0
    case clang::BuiltinType::Kind::BuiltinFn:
4755
0
    case clang::BuiltinType::Kind::Dependent:
4756
0
    case clang::BuiltinType::Kind::OCLClkEvent:
4757
0
    case clang::BuiltinType::Kind::OCLEvent:
4758
0
    case clang::BuiltinType::Kind::OCLImage1dRO:
4759
0
    case clang::BuiltinType::Kind::OCLImage1dWO:
4760
0
    case clang::BuiltinType::Kind::OCLImage1dRW:
4761
0
    case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4762
0
    case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4763
0
    case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4764
0
    case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4765
0
    case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4766
0
    case clang::BuiltinType::Kind::OCLImage1dBufferRW:
4767
0
    case clang::BuiltinType::Kind::OCLImage2dRO:
4768
0
    case clang::BuiltinType::Kind::OCLImage2dWO:
4769
0
    case clang::BuiltinType::Kind::OCLImage2dRW:
4770
0
    case clang::BuiltinType::Kind::OCLImage2dArrayRO:
4771
0
    case clang::BuiltinType::Kind::OCLImage2dArrayWO:
4772
0
    case clang::BuiltinType::Kind::OCLImage2dArrayRW:
4773
0
    case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
4774
0
    case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
4775
0
    case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
4776
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
4777
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
4778
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
4779
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
4780
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
4781
0
    case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
4782
0
    case clang::BuiltinType::Kind::OCLImage2dDepthRO:
4783
0
    case clang::BuiltinType::Kind::OCLImage2dDepthWO:
4784
0
    case clang::BuiltinType::Kind::OCLImage2dDepthRW:
4785
0
    case clang::BuiltinType::Kind::OCLImage2dMSAARO:
4786
0
    case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
4787
0
    case clang::BuiltinType::Kind::OCLImage2dMSAARW:
4788
0
    case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
4789
0
    case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
4790
0
    case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
4791
0
    case clang::BuiltinType::Kind::OCLImage3dRO:
4792
0
    case clang::BuiltinType::Kind::OCLImage3dWO:
4793
0
    case clang::BuiltinType::Kind::OCLImage3dRW:
4794
0
    case clang::BuiltinType::Kind::OCLQueue:
4795
0
    case clang::BuiltinType::Kind::OCLReserveID:
4796
0
    case clang::BuiltinType::Kind::OCLSampler:
4797
0
    case clang::BuiltinType::Kind::OMPArraySection:
4798
0
    case clang::BuiltinType::Kind::OMPArrayShaping:
4799
0
    case clang::BuiltinType::Kind::OMPIterator:
4800
0
    case clang::BuiltinType::Kind::Overload:
4801
0
    case clang::BuiltinType::Kind::PseudoObject:
4802
0
    case clang::BuiltinType::Kind::UnknownAny:
4803
0
      break;
4804
0
4805
0
    case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload:
4806
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImePayload:
4807
0
    case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload:
4808
0
    case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload:
4809
0
    case clang::BuiltinType::OCLIntelSubgroupAVCMceResult:
4810
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImeResult:
4811
0
    case clang::BuiltinType::OCLIntelSubgroupAVCRefResult:
4812
0
    case clang::BuiltinType::OCLIntelSubgroupAVCSicResult:
4813
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout:
4814
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout:
4815
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin:
4816
0
    case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin:
4817
0
      break;
4818
0
4819
0
    case clang::BuiltinType::SveBool:
4820
0
    case clang::BuiltinType::SveInt8:
4821
0
    case clang::BuiltinType::SveInt8x2:
4822
0
    case clang::BuiltinType::SveInt8x3:
4823
0
    case clang::BuiltinType::SveInt8x4:
4824
0
    case clang::BuiltinType::SveInt16:
4825
0
    case clang::BuiltinType::SveInt16x2:
4826
0
    case clang::BuiltinType::SveInt16x3:
4827
0
    case clang::BuiltinType::SveInt16x4:
4828
0
    case clang::BuiltinType::SveInt32:
4829
0
    case clang::BuiltinType::SveInt32x2:
4830
0
    case clang::BuiltinType::SveInt32x3:
4831
0
    case clang::BuiltinType::SveInt32x4:
4832
0
    case clang::BuiltinType::SveInt64:
4833
0
    case clang::BuiltinType::SveInt64x2:
4834
0
    case clang::BuiltinType::SveInt64x3:
4835
0
    case clang::BuiltinType::SveInt64x4:
4836
0
    case clang::BuiltinType::SveUint8:
4837
0
    case clang::BuiltinType::SveUint8x2:
4838
0
    case clang::BuiltinType::SveUint8x3:
4839
0
    case clang::BuiltinType::SveUint8x4:
4840
0
    case clang::BuiltinType::SveUint16:
4841
0
    case clang::BuiltinType::SveUint16x2:
4842
0
    case clang::BuiltinType::SveUint16x3:
4843
0
    case clang::BuiltinType::SveUint16x4:
4844
0
    case clang::BuiltinType::SveUint32:
4845
0
    case clang::BuiltinType::SveUint32x2:
4846
0
    case clang::BuiltinType::SveUint32x3:
4847
0
    case clang::BuiltinType::SveUint32x4:
4848
0
    case clang::BuiltinType::SveUint64:
4849
0
    case clang::BuiltinType::SveUint64x2:
4850
0
    case clang::BuiltinType::SveUint64x3:
4851
0
    case clang::BuiltinType::SveUint64x4:
4852
0
    case clang::BuiltinType::SveFloat16:
4853
0
    case clang::BuiltinType::SveBFloat16:
4854
0
    case clang::BuiltinType::SveBFloat16x2:
4855
0
    case clang::BuiltinType::SveBFloat16x3:
4856
0
    case clang::BuiltinType::SveBFloat16x4:
4857
0
    case clang::BuiltinType::SveFloat16x2:
4858
0
    case clang::BuiltinType::SveFloat16x3:
4859
0
    case clang::BuiltinType::SveFloat16x4:
4860
0
    case clang::BuiltinType::SveFloat32:
4861
0
    case clang::BuiltinType::SveFloat32x2:
4862
0
    case clang::BuiltinType::SveFloat32x3:
4863
0
    case clang::BuiltinType::SveFloat32x4:
4864
0
    case clang::BuiltinType::SveFloat64:
4865
0
    case clang::BuiltinType::SveFloat64x2:
4866
0
    case clang::BuiltinType::SveFloat64x3:
4867
0
    case clang::BuiltinType::SveFloat64x4:
4868
0
      break;
4869
0
4870
0
    case clang::BuiltinType::IncompleteMatrixIdx:
4871
0
      break;
4872
0
    }
4873
0
    break;
4874
0
  // All pointer types are represented as unsigned integer encodings. We may
4875
0
  // nee to add a eEncodingPointer if we ever need to know the difference
4876
8.60k
  case clang::Type::ObjCObjectPointer:
4877
8.60k
  case clang::Type::BlockPointer:
4878
8.60k
  case clang::Type::Pointer:
4879
8.60k
  case clang::Type::LValueReference:
4880
8.60k
  case clang::Type::RValueReference:
4881
8.60k
  case clang::Type::MemberPointer:
4882
8.60k
    return lldb::eEncodingUint;
4883
8.60k
  case clang::Type::Complex: {
4884
0
    lldb::Encoding encoding = lldb::eEncodingIEEE754;
4885
0
    if (qual_type->isComplexType())
4886
0
      encoding = lldb::eEncodingIEEE754;
4887
0
    else {
4888
0
      const clang::ComplexType *complex_type =
4889
0
          qual_type->getAsComplexIntegerType();
4890
0
      if (complex_type)
4891
0
        encoding = GetType(complex_type->getElementType()).GetEncoding(count);
4892
0
      else
4893
0
        encoding = lldb::eEncodingSint;
4894
0
    }
4895
0
    count = 2;
4896
0
    return encoding;
4897
8.60k
  }
4898
8.60k
4899
8.60k
  case clang::Type::ObjCInterface:
4900
0
    break;
4901
8.60k
  case clang::Type::Record:
4902
0
    break;
4903
8.60k
  case clang::Type::Enum:
4904
0
    return lldb::eEncodingSint;
4905
8.60k
  case clang::Type::DependentSizedArray:
4906
0
  case clang::Type::DependentSizedExtVector:
4907
0
  case clang::Type::UnresolvedUsing:
4908
0
  case clang::Type::Attributed:
4909
0
  case clang::Type::TemplateTypeParm:
4910
0
  case clang::Type::SubstTemplateTypeParm:
4911
0
  case clang::Type::SubstTemplateTypeParmPack:
4912
0
  case clang::Type::InjectedClassName:
4913
0
  case clang::Type::DependentName:
4914
0
  case clang::Type::DependentTemplateSpecialization:
4915
0
  case clang::Type::PackExpansion:
4916
0
  case clang::Type::ObjCObject:
4917
0
4918
0
  case clang::Type::TemplateSpecialization:
4919
0
  case clang::Type::DeducedTemplateSpecialization:
4920
0
  case clang::Type::Adjusted:
4921
0
  case clang::Type::Pipe:
4922
0
    break;
4923
0
4924
0
  // pointer type decayed from an array or function type.
4925
0
  case clang::Type::Decayed:
4926
0
    break;
4927
0
  case clang::Type::ObjCTypeParam:
4928
0
    break;
4929
0
4930
0
  case clang::Type::DependentAddressSpace:
4931
0
    break;
4932
0
  case clang::Type::MacroQualified:
4933
0
    break;
4934
0
4935
0
  case clang::Type::ConstantMatrix:
4936
0
  case clang::Type::DependentSizedMatrix:
4937
0
    break;
4938
0
  }
4939
0
  count = 0;
4940
0
  return lldb::eEncodingInvalid;
4941
0
}
4942
4943
29.0k
lldb::Format TypeSystemClang::GetFormat(lldb::opaque_compiler_type_t type) {
4944
29.0k
  if (!type)
4945
0
    return lldb::eFormatDefault;
4946
29.0k
4947
29.0k
  clang::QualType qual_type = RemoveWrappingTypes(GetCanonicalQualType(type));
4948
29.0k
4949
29.0k
  switch (qual_type->getTypeClass()) {
4950
0
  case clang::Type::Atomic:
4951
0
  case clang::Type::Auto:
4952
0
  case clang::Type::Decltype:
4953
0
  case clang::Type::Elaborated:
4954
0
  case clang::Type::Paren:
4955
0
  case clang::Type::Typedef:
4956
0
  case clang::Type::TypeOf:
4957
0
  case clang::Type::TypeOfExpr:
4958
0
    llvm_unreachable("Handled in RemoveWrappingTypes!");
4959
0
  case clang::Type::UnaryTransform:
4960
0
    break;
4961
0
4962
12
  case clang::Type::FunctionNoProto:
4963
12
  case clang::Type::FunctionProto:
4964
12
    break;
4965
12
4966
12
  case clang::Type::IncompleteArray:
4967
6
  case clang::Type::VariableArray:
4968
6
    break;
4969
6
4970
374
  case clang::Type::ConstantArray:
4971
374
    return lldb::eFormatVoid; // no value
4972
6
4973
29
  case clang::Type::DependentVector:
4974
29
  case clang::Type::ExtVector:
4975
29
  case clang::Type::Vector:
4976
29
    break;
4977
29
4978
29
  case clang::Type::ExtInt:
4979
0
  case clang::Type::DependentExtInt:
4980
0
    return qual_type->isUnsignedIntegerType() ? lldb::eFormatUnsigned
4981
0
                                              : lldb::eFormatDecimal;
4982
0
4983
17.9k
  case clang::Type::Builtin:
4984
17.9k
    switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4985
0
    case clang::BuiltinType::UnknownAny:
4986
0