Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
Line
Count
Source (jump to first uncovered line)
1
//===-- TypeSystemClang.h ---------------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H
10
#define LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H
11
12
#include <cstdint>
13
14
#include <functional>
15
#include <initializer_list>
16
#include <map>
17
#include <memory>
18
#include <set>
19
#include <string>
20
#include <utility>
21
#include <vector>
22
23
#include "clang/AST/ASTContext.h"
24
#include "clang/AST/ASTFwd.h"
25
#include "clang/AST/TemplateBase.h"
26
#include "clang/Basic/TargetInfo.h"
27
#include "llvm/ADT/APSInt.h"
28
#include "llvm/ADT/SmallVector.h"
29
30
#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
31
#include "lldb/Expression/ExpressionVariable.h"
32
#include "lldb/Symbol/CompilerType.h"
33
#include "lldb/Symbol/TypeSystem.h"
34
#include "lldb/Target/Target.h"
35
#include "lldb/Utility/ConstString.h"
36
#include "lldb/Utility/Flags.h"
37
#include "lldb/Utility/Log.h"
38
#include "lldb/Utility/Logging.h"
39
#include "lldb/lldb-enumerations.h"
40
41
class DWARFASTParserClang;
42
class PDBASTParser;
43
44
namespace clang {
45
class FileManager;
46
class HeaderSearch;
47
class ModuleMap;
48
} // namespace clang
49
50
namespace lldb_private {
51
52
class ClangASTMetadata;
53
class ClangASTSource;
54
class Declaration;
55
56
/// A Clang module ID.
57
class OptionalClangModuleID {
58
  unsigned m_id = 0;
59
60
public:
61
157k
  OptionalClangModuleID() = default;
62
142k
  explicit OptionalClangModuleID(unsigned id) : m_id(id) {}
63
251k
  bool HasValue() const { return m_id != 0; }
64
187k
  unsigned GetValue() const { return m_id; }
65
};
66
67
/// The implementation of lldb::Type's m_payload field for TypeSystemClang.
68
class TypePayloadClang {
69
  /// The Layout is as follows:
70
  /// \verbatim
71
  /// bit 0..30 ... Owning Module ID.
72
  /// bit 31 ...... IsCompleteObjCClass.
73
  /// \endverbatim
74
  Type::Payload m_payload = 0;
75
76
public:
77
  TypePayloadClang() = default;
78
  explicit TypePayloadClang(OptionalClangModuleID owning_module,
79
                            bool is_complete_objc_class = false);
80
15.3k
  explicit TypePayloadClang(uint32_t opaque_payload) : m_payload(opaque_payload) {}
81
70.7k
  operator Type::Payload() { return m_payload; }
82
83
  static constexpr unsigned ObjCClassBit = 1 << 31;
84
236
  bool IsCompleteObjCClass() { return Flags(m_payload).Test(ObjCClassBit); }
85
70.7k
  void SetIsCompleteObjCClass(bool is_complete_objc_class) {
86
70.7k
    m_payload = is_complete_objc_class ? 
Flags(m_payload).Set(ObjCClassBit)292
87
70.7k
                                       : 
Flags(m_payload).Clear(ObjCClassBit)70.5k
;
88
70.7k
  }
89
15.1k
  OptionalClangModuleID GetOwningModule() {
90
15.1k
    return OptionalClangModuleID(Flags(m_payload).Clear(ObjCClassBit));
91
15.1k
  }
92
  void SetOwningModule(OptionalClangModuleID id);
93
  /// \}
94
};
95
  
96
/// A TypeSystem implementation based on Clang.
97
///
98
/// This class uses a single clang::ASTContext as the backend for storing
99
/// its types and declarations. Every clang::ASTContext should also just have
100
/// a single associated TypeSystemClang instance that manages it.
101
///
102
/// The clang::ASTContext instance can either be created by TypeSystemClang
103
/// itself or it can adopt an existing clang::ASTContext (for example, when
104
/// it is necessary to provide a TypeSystem interface for an existing
105
/// clang::ASTContext that was created by clang::CompilerInstance).
106
class TypeSystemClang : public TypeSystem {
107
  // LLVM RTTI support
108
  static char ID;
109
110
public:
111
  typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
112
  typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton,
113
                                                    clang::ObjCInterfaceDecl *);
114
115
  // llvm casting support
116
3.24M
  bool isA(const void *ClassID) const override { return ClassID == &ID; }
117
3.24M
  static bool classof(const TypeSystem *ts) { return ts->isA(&ID); }
118
119
  /// Constructs a TypeSystemClang with an ASTContext using the given triple.
120
  ///
121
  /// \param name The name for the TypeSystemClang (for logging purposes)
122
  /// \param triple The llvm::Triple used for the ASTContext. The triple defines
123
  ///               certain characteristics of the ASTContext and its types
124
  ///               (e.g., whether certain primitive types exist or what their
125
  ///               signedness is).
126
  explicit TypeSystemClang(llvm::StringRef name, llvm::Triple triple);
127
128
  /// Constructs a TypeSystemClang that uses an existing ASTContext internally.
129
  /// Useful when having an existing ASTContext created by Clang.
130
  ///
131
  /// \param name The name for the TypeSystemClang (for logging purposes)
132
  /// \param existing_ctxt An existing ASTContext.
133
  explicit TypeSystemClang(llvm::StringRef name,
134
                           clang::ASTContext &existing_ctxt);
135
136
  ~TypeSystemClang() override;
137
138
  void Finalize() override;
139
140
  // PluginInterface functions
141
0
  llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); }
142
143
3.47k
  static llvm::StringRef GetPluginNameStatic() { return "clang"; }
144
145
  static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
146
                                           Module *module, Target *target);
147
148
  static LanguageSet GetSupportedLanguagesForTypes();
149
  static LanguageSet GetSupportedLanguagesForExpressions();
150
151
  static void Initialize();
152
153
  static void Terminate();
154
155
  static TypeSystemClang *GetASTContext(clang::ASTContext *ast_ctx);
156
157
  /// Returns the display name of this TypeSystemClang that indicates what
158
  /// purpose it serves in LLDB. Used for example in logs.
159
116
  llvm::StringRef getDisplayName() const { return m_display_name; }
160
161
  /// Returns the clang::ASTContext instance managed by this TypeSystemClang.
162
  clang::ASTContext &getASTContext();
163
164
  clang::MangleContext *getMangleContext();
165
166
  std::shared_ptr<clang::TargetOptions> &getTargetOptions();
167
168
  clang::TargetInfo *getTargetInfo();
169
170
  void setSema(clang::Sema *s);
171
351k
  clang::Sema *getSema() { return m_sema; }
172
173
  const char *GetTargetTriple();
174
175
  void SetExternalSource(
176
      llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_up);
177
178
0
  bool GetCompleteDecl(clang::Decl *decl) {
179
0
    return TypeSystemClang::GetCompleteDecl(&getASTContext(), decl);
180
0
  }
181
182
  static void DumpDeclHiearchy(clang::Decl *decl);
183
184
  static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx);
185
186
  static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl);
187
188
  static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl);
189
190
  void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id);
191
  void SetMetadataAsUserID(const clang::Type *type, lldb::user_id_t user_id);
192
193
  void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data);
194
195
  void SetMetadata(const clang::Type *object, ClangASTMetadata &meta_data);
196
  ClangASTMetadata *GetMetadata(const clang::Decl *object);
197
  ClangASTMetadata *GetMetadata(const clang::Type *object);
198
199
  void SetCXXRecordDeclAccess(const clang::CXXRecordDecl *object,
200
                              clang::AccessSpecifier access);
201
  clang::AccessSpecifier
202
  GetCXXRecordDeclAccess(const clang::CXXRecordDecl *object);
203
204
  // Basic Types
205
  CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
206
                                                   size_t bit_size) override;
207
208
  CompilerType GetBasicType(lldb::BasicType type);
209
210
  static lldb::BasicType GetBasicTypeEnumeration(ConstString name);
211
212
  CompilerType
213
  GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name,
214
                                           uint32_t dw_ate, uint32_t bit_size);
215
216
  CompilerType GetCStringType(bool is_const);
217
218
  static clang::DeclContext *GetDeclContextForType(clang::QualType type);
219
220
  static clang::DeclContext *GetDeclContextForType(const CompilerType &type);
221
222
  uint32_t GetPointerByteSize() override;
223
224
7.25k
  clang::TranslationUnitDecl *GetTranslationUnitDecl() {
225
7.25k
    return getASTContext().getTranslationUnitDecl();
226
7.25k
  }
227
228
  static bool AreTypesSame(CompilerType type1, CompilerType type2,
229
                           bool ignore_qualifiers = false);
230
231
  /// Creates a CompilerType form the given QualType with the current
232
  /// TypeSystemClang instance as the CompilerType's typesystem.
233
  /// \param qt The QualType for a type that belongs to the ASTContext of this
234
  ///           TypeSystemClang.
235
  /// \return The CompilerType representing the given QualType. If the
236
  ///         QualType's type pointer is a nullptr then the function returns an
237
  ///         invalid CompilerType.
238
663k
  CompilerType GetType(clang::QualType qt) {
239
663k
    if (qt.getTypePtrOrNull() == nullptr)
240
2.11k
      return CompilerType();
241
    // Check that the type actually belongs to this TypeSystemClang.
242
661k
    assert(qt->getAsTagDecl() == nullptr ||
243
661k
           &qt->getAsTagDecl()->getASTContext() == &getASTContext());
244
0
    return CompilerType(this, qt.getAsOpaquePtr());
245
663k
  }
246
247
  CompilerType GetTypeForDecl(clang::NamedDecl *decl);
248
249
  CompilerType GetTypeForDecl(clang::TagDecl *decl);
250
251
  CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl);
252
253
  template <typename RecordDeclType>
254
  CompilerType
255
  GetTypeForIdentifier(ConstString type_name,
256
102
                       clang::DeclContext *decl_context = nullptr) {
257
102
    CompilerType compiler_type;
258
259
102
    if (type_name.GetLength()) {
260
102
      clang::ASTContext &ast = getASTContext();
261
102
      if (!decl_context)
262
102
        decl_context = ast.getTranslationUnitDecl();
263
264
102
      clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString());
265
102
      clang::DeclarationName myName =
266
102
          ast.DeclarationNames.getIdentifier(&myIdent);
267
268
102
      clang::DeclContext::lookup_result result = decl_context->lookup(myName);
269
270
102
      if (!result.empty()) {
271
66
        clang::NamedDecl *named_decl = *result.begin();
272
66
        if (const RecordDeclType *record_decl =
273
66
                llvm::dyn_cast<RecordDeclType>(named_decl))
274
66
          compiler_type.SetCompilerType(
275
66
              this, clang::QualType(record_decl->getTypeForDecl(), 0)
276
66
                        .getAsOpaquePtr());
277
66
      }
278
102
    }
279
280
102
    return compiler_type;
281
102
  }
lldb_private::CompilerType lldb_private::TypeSystemClang::GetTypeForIdentifier<clang::CXXRecordDecl>(lldb_private::ConstString, clang::DeclContext*)
Line
Count
Source
256
102
                       clang::DeclContext *decl_context = nullptr) {
257
102
    CompilerType compiler_type;
258
259
102
    if (type_name.GetLength()) {
260
102
      clang::ASTContext &ast = getASTContext();
261
102
      if (!decl_context)
262
102
        decl_context = ast.getTranslationUnitDecl();
263
264
102
      clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString());
265
102
      clang::DeclarationName myName =
266
102
          ast.DeclarationNames.getIdentifier(&myIdent);
267
268
102
      clang::DeclContext::lookup_result result = decl_context->lookup(myName);
269
270
102
      if (!result.empty()) {
271
66
        clang::NamedDecl *named_decl = *result.begin();
272
66
        if (const RecordDeclType *record_decl =
273
66
                llvm::dyn_cast<RecordDeclType>(named_decl))
274
66
          compiler_type.SetCompilerType(
275
66
              this, clang::QualType(record_decl->getTypeForDecl(), 0)
276
66
                        .getAsOpaquePtr());
277
66
      }
278
102
    }
279
280
102
    return compiler_type;
281
102
  }
Unexecuted instantiation: lldb_private::CompilerType lldb_private::TypeSystemClang::GetTypeForIdentifier<clang::EnumDecl>(lldb_private::ConstString, clang::DeclContext*)
Unexecuted instantiation: lldb_private::CompilerType lldb_private::TypeSystemClang::GetTypeForIdentifier<clang::TypedefNameDecl>(lldb_private::ConstString, clang::DeclContext*)
282
283
  CompilerType CreateStructForIdentifier(
284
      ConstString type_name,
285
      const std::initializer_list<std::pair<const char *, CompilerType>>
286
          &type_fields,
287
      bool packed = false);
288
289
  CompilerType GetOrCreateStructForIdentifier(
290
      ConstString type_name,
291
      const std::initializer_list<std::pair<const char *, CompilerType>>
292
          &type_fields,
293
      bool packed = false);
294
295
  static bool IsOperator(llvm::StringRef name,
296
                         clang::OverloadedOperatorKind &op_kind);
297
298
  // Structure, Unions, Classes
299
300
  static clang::AccessSpecifier
301
  ConvertAccessTypeToAccessSpecifier(lldb::AccessType access);
302
303
  static clang::AccessSpecifier
304
  UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs);
305
306
  static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl,
307
                                    bool omit_empty_base_classes);
308
309
  /// Synthesize a clang::Module and return its ID or a default-constructed ID.
310
  OptionalClangModuleID GetOrCreateClangModule(llvm::StringRef name,
311
                                               OptionalClangModuleID parent,
312
                                               bool is_framework = false,
313
                                               bool is_explicit = false);
314
315
  CompilerType CreateRecordType(clang::DeclContext *decl_ctx,
316
                                OptionalClangModuleID owning_module,
317
                                lldb::AccessType access_type,
318
                                llvm::StringRef name, int kind,
319
                                lldb::LanguageType language,
320
                                ClangASTMetadata *metadata = nullptr,
321
                                bool exports_symbols = false);
322
323
  class TemplateParameterInfos {
324
  public:
325
8.49k
    bool IsValid() const {
326
      // Having a pack name but no packed args doesn't make sense, so mark
327
      // these template parameters as invalid.
328
8.49k
      if (pack_name && 
!packed_args228
)
329
1
        return false;
330
8.48k
      return args.size() == names.size() &&
331
8.48k
        (!packed_args || 
!packed_args->packed_args308
);
332
8.49k
    }
333
334
3.02k
    bool hasParameterPack() const { return static_cast<bool>(packed_args); }
335
336
    llvm::SmallVector<const char *, 2> names;
337
    llvm::SmallVector<clang::TemplateArgument, 2> args;
338
    
339
    const char * pack_name = nullptr;
340
    std::unique_ptr<TemplateParameterInfos> packed_args;
341
  };
342
343
  clang::FunctionTemplateDecl *CreateFunctionTemplateDecl(
344
      clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
345
      clang::FunctionDecl *func_decl, const TemplateParameterInfos &infos);
346
347
  void CreateFunctionTemplateSpecializationInfo(
348
      clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template,
349
      const TemplateParameterInfos &infos);
350
351
  clang::ClassTemplateDecl *CreateClassTemplateDecl(
352
      clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
353
      lldb::AccessType access_type, llvm::StringRef class_name, int kind,
354
      const TemplateParameterInfos &infos);
355
356
  clang::TemplateTemplateParmDecl *
357
  CreateTemplateTemplateParmDecl(const char *template_name);
358
359
  clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl(
360
      clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
361
      clang::ClassTemplateDecl *class_template_decl, int kind,
362
      const TemplateParameterInfos &infos);
363
364
  CompilerType
365
  CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *
366
                                            class_template_specialization_decl);
367
368
  static clang::DeclContext *
369
  GetAsDeclContext(clang::FunctionDecl *function_decl);
370
371
  static bool CheckOverloadedOperatorKindParameterCount(
372
      bool is_method, clang::OverloadedOperatorKind op_kind,
373
      uint32_t num_params);
374
375
  bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size);
376
377
  static bool RecordHasFields(const clang::RecordDecl *record_decl);
378
379
  CompilerType CreateObjCClass(llvm::StringRef name,
380
                               clang::DeclContext *decl_ctx,
381
                               OptionalClangModuleID owning_module,
382
                               bool isForwardDecl, bool isInternal,
383
                               ClangASTMetadata *metadata = nullptr);
384
385
  // Returns a mask containing bits from the TypeSystemClang::eTypeXXX
386
  // enumerations
387
388
  // Namespace Declarations
389
390
  clang::NamespaceDecl *
391
  GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx,
392
                                OptionalClangModuleID owning_module,
393
                                bool is_inline = false);
394
395
  // Function Types
396
397
  clang::FunctionDecl *CreateFunctionDeclaration(
398
      clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
399
      llvm::StringRef name, const CompilerType &function_Type,
400
      clang::StorageClass storage, bool is_inline);
401
402
  CompilerType CreateFunctionType(const CompilerType &result_type,
403
                                  const CompilerType *args, unsigned num_args,
404
                                  bool is_variadic, unsigned type_quals,
405
                                  clang::CallingConv cc = clang::CC_C);
406
407
  clang::ParmVarDecl *
408
  CreateParameterDeclaration(clang::DeclContext *decl_ctx,
409
                             OptionalClangModuleID owning_module,
410
                             const char *name, const CompilerType &param_type,
411
                             int storage, bool add_decl = false);
412
413
  void SetFunctionParameters(clang::FunctionDecl *function_decl,
414
                             llvm::ArrayRef<clang::ParmVarDecl *> params);
415
416
  CompilerType CreateBlockPointerType(const CompilerType &function_type);
417
418
  // Array Types
419
420
  CompilerType CreateArrayType(const CompilerType &element_type,
421
                               size_t element_count, bool is_vector);
422
423
  // Enumeration Types
424
  CompilerType CreateEnumerationType(llvm::StringRef name,
425
                                     clang::DeclContext *decl_ctx,
426
                                     OptionalClangModuleID owning_module,
427
                                     const Declaration &decl,
428
                                     const CompilerType &integer_qual_type,
429
                                     bool is_scoped);
430
431
  // Integer type functions
432
433
  CompilerType GetIntTypeFromBitSize(size_t bit_size, bool is_signed);
434
435
  CompilerType GetPointerSizedIntType(bool is_signed);
436
437
  // Floating point functions
438
439
  static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast,
440
                                              size_t bit_size);
441
442
  // TypeSystem methods
443
  DWARFASTParser *GetDWARFParser() override;
444
  PDBASTParser *GetPDBParser() override;
445
446
  // TypeSystemClang callbacks for external source lookups.
447
  void CompleteTagDecl(clang::TagDecl *);
448
449
  void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *);
450
451
  bool LayoutRecordType(
452
      const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment,
453
      llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
454
      llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
455
          &base_offsets,
456
      llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
457
          &vbase_offsets);
458
459
  /// Creates a CompilerDecl from the given Decl with the current
460
  /// TypeSystemClang instance as its typesystem.
461
  /// The Decl has to come from the ASTContext of this
462
  /// TypeSystemClang.
463
422k
  CompilerDecl GetCompilerDecl(clang::Decl *decl) {
464
422k
    assert(&decl->getASTContext() == &getASTContext() &&
465
422k
           "CreateCompilerDecl for Decl from wrong ASTContext?");
466
0
    return CompilerDecl(this, decl);
467
422k
  }
468
469
  // CompilerDecl override functions
470
  ConstString DeclGetName(void *opaque_decl) override;
471
472
  ConstString DeclGetMangledName(void *opaque_decl) override;
473
474
  CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override;
475
476
  CompilerType DeclGetFunctionReturnType(void *opaque_decl) override;
477
478
  size_t DeclGetFunctionNumArguments(void *opaque_decl) override;
479
480
  CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
481
                                           size_t arg_idx) override;
482
483
  CompilerType GetTypeForDecl(void *opaque_decl) override;
484
485
  // CompilerDeclContext override functions
486
487
  /// Creates a CompilerDeclContext from the given DeclContext
488
  /// with the current TypeSystemClang instance as its typesystem.
489
  /// The DeclContext has to come from the ASTContext of this
490
  /// TypeSystemClang.
491
  CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx);
492
493
  /// Set the owning module for \p decl.
494
  static void SetOwningModule(clang::Decl *decl,
495
                              OptionalClangModuleID owning_module);
496
497
  std::vector<CompilerDecl>
498
  DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
499
                            const bool ignore_using_decls) override;
500
501
  ConstString DeclContextGetName(void *opaque_decl_ctx) override;
502
503
  ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override;
504
505
  bool DeclContextIsClassMethod(void *opaque_decl_ctx,
506
                                lldb::LanguageType *language_ptr,
507
                                bool *is_instance_method_ptr,
508
                                ConstString *language_object_name_ptr) override;
509
510
  bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
511
                                      void *other_opaque_decl_ctx) override;
512
513
  // Clang specific clang::DeclContext functions
514
515
  static clang::DeclContext *
516
  DeclContextGetAsDeclContext(const CompilerDeclContext &dc);
517
518
  static clang::ObjCMethodDecl *
519
  DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc);
520
521
  static clang::CXXMethodDecl *
522
  DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc);
523
524
  static clang::FunctionDecl *
525
  DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc);
526
527
  static clang::NamespaceDecl *
528
  DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc);
529
530
  static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc,
531
                                                  const clang::Decl *object);
532
533
  static clang::ASTContext *
534
  DeclContextGetTypeSystemClang(const CompilerDeclContext &dc);
535
536
  // Tests
537
538
#ifndef NDEBUG
539
  bool Verify(lldb::opaque_compiler_type_t type) override;
540
#endif
541
  
542
  bool IsArrayType(lldb::opaque_compiler_type_t type,
543
                   CompilerType *element_type, uint64_t *size,
544
                   bool *is_incomplete) override;
545
546
  bool IsVectorType(lldb::opaque_compiler_type_t type,
547
                    CompilerType *element_type, uint64_t *size) override;
548
549
  bool IsAggregateType(lldb::opaque_compiler_type_t type) override;
550
551
  bool IsAnonymousType(lldb::opaque_compiler_type_t type) override;
552
553
  bool IsBeingDefined(lldb::opaque_compiler_type_t type) override;
554
555
  bool IsCharType(lldb::opaque_compiler_type_t type) override;
556
557
  bool IsCompleteType(lldb::opaque_compiler_type_t type) override;
558
559
  bool IsConst(lldb::opaque_compiler_type_t type) override;
560
561
  bool IsCStringType(lldb::opaque_compiler_type_t type,
562
                     uint32_t &length) override;
563
564
  static bool IsCXXClassType(const CompilerType &type);
565
566
  bool IsDefined(lldb::opaque_compiler_type_t type) override;
567
568
  bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
569
                           bool &is_complex) override;
570
571
  bool IsFunctionType(lldb::opaque_compiler_type_t type) override;
572
573
  uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
574
                                  CompilerType *base_type_ptr) override;
575
576
  size_t
577
  GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override;
578
579
  CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
580
                                          const size_t index) override;
581
582
  bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;
583
584
  bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
585
                          CompilerType *function_pointer_type_ptr) override;
586
587
  bool IsIntegerType(lldb::opaque_compiler_type_t type,
588
                     bool &is_signed) override;
589
590
  bool IsEnumerationType(lldb::opaque_compiler_type_t type,
591
                         bool &is_signed) override;
592
593
  bool IsScopedEnumerationType(lldb::opaque_compiler_type_t type) override;
594
595
  static bool IsObjCClassType(const CompilerType &type);
596
597
  static bool IsObjCClassTypeAndHasIVars(const CompilerType &type,
598
                                         bool check_superclass);
599
600
  static bool IsObjCObjectOrInterfaceType(const CompilerType &type);
601
602
  static bool IsObjCObjectPointerType(const CompilerType &type,
603
                                      CompilerType *target_type = nullptr);
604
605
  bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override;
606
607
  static bool IsClassType(lldb::opaque_compiler_type_t type);
608
609
  static bool IsEnumType(lldb::opaque_compiler_type_t type);
610
611
  bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
612
                             CompilerType *target_type, // Can pass nullptr
613
                             bool check_cplusplus, bool check_objc) override;
614
615
  bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override;
616
617
  bool IsPointerType(lldb::opaque_compiler_type_t type,
618
                     CompilerType *pointee_type) override;
619
620
  bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
621
                                CompilerType *pointee_type) override;
622
623
  bool IsReferenceType(lldb::opaque_compiler_type_t type,
624
                       CompilerType *pointee_type, bool *is_rvalue) override;
625
626
  bool IsScalarType(lldb::opaque_compiler_type_t type) override;
627
628
  bool IsTypedefType(lldb::opaque_compiler_type_t type) override;
629
630
  bool IsVoidType(lldb::opaque_compiler_type_t type) override;
631
632
  bool CanPassInRegisters(const CompilerType &type) override;
633
634
  bool SupportsLanguage(lldb::LanguageType language) override;
635
636
  static llvm::Optional<std::string> GetCXXClassName(const CompilerType &type);
637
638
  // Type Completion
639
640
  bool GetCompleteType(lldb::opaque_compiler_type_t type) override;
641
642
  // Accessors
643
644
  ConstString GetTypeName(lldb::opaque_compiler_type_t type) override;
645
646
  ConstString GetDisplayTypeName(lldb::opaque_compiler_type_t type) override;
647
648
  uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
649
                       CompilerType *pointee_or_element_compiler_type) override;
650
651
  lldb::LanguageType
652
  GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;
653
654
  lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;
655
656
  unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override;
657
658
  // Creating related types
659
660
  CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
661
                                   ExecutionContextScope *exe_scope) override;
662
663
  CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
664
                            uint64_t size) override;
665
666
  CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override;
667
668
  CompilerType
669
  GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override;
670
671
  CompilerType
672
  GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) override;
673
674
  // Returns -1 if this isn't a function of if the function doesn't have a
675
  // prototype Returns a value >= 0 if there is a prototype.
676
  int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override;
677
678
  CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
679
                                              size_t idx) override;
680
681
  CompilerType
682
  GetFunctionReturnType(lldb::opaque_compiler_type_t type) override;
683
684
  size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override;
685
686
  TypeMemberFunctionImpl
687
  GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
688
                           size_t idx) override;
689
690
  CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override;
691
692
  CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
693
694
  CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
695
696
  CompilerType
697
  GetLValueReferenceType(lldb::opaque_compiler_type_t type) override;
698
699
  CompilerType
700
  GetRValueReferenceType(lldb::opaque_compiler_type_t type) override;
701
702
  CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override;
703
704
  CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override;
705
706
  CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override;
707
708
  CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override;
709
710
  /// Using the current type, create a new typedef to that type using
711
  /// "typedef_name" as the name and "decl_ctx" as the decl context.
712
  /// \param opaque_payload is an opaque TypePayloadClang.
713
  CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
714
                             const char *name,
715
                             const CompilerDeclContext &decl_ctx,
716
                             uint32_t opaque_payload) override;
717
718
  // If the current object represents a typedef type, get the underlying type
719
  CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override;
720
721
  // Create related types using the current type's AST
722
  CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override;
723
724
  // Exploring the type
725
726
  const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override;
727
728
  llvm::Optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type,
729
0
                       ExecutionContextScope *exe_scope) {
730
0
    if (llvm::Optional<uint64_t> bit_size = GetBitSize(type, exe_scope))
731
0
      return (*bit_size + 7) / 8;
732
0
    return llvm::None;
733
0
  }
734
735
  llvm::Optional<uint64_t>
736
  GetBitSize(lldb::opaque_compiler_type_t type,
737
             ExecutionContextScope *exe_scope) override;
738
739
  lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
740
                             uint64_t &count) override;
741
742
  lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override;
743
744
  llvm::Optional<size_t>
745
  GetTypeBitAlign(lldb::opaque_compiler_type_t type,
746
                  ExecutionContextScope *exe_scope) override;
747
748
  uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
749
                          bool omit_empty_base_classes,
750
                          const ExecutionContext *exe_ctx) override;
751
752
  CompilerType GetBuiltinTypeByName(ConstString name) override;
753
754
  lldb::BasicType
755
  GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override;
756
757
  static lldb::BasicType
758
  GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type,
759
                          ConstString name);
760
761
  void ForEachEnumerator(
762
      lldb::opaque_compiler_type_t type,
763
      std::function<bool(const CompilerType &integer_type,
764
                         ConstString name,
765
                         const llvm::APSInt &value)> const &callback) override;
766
767
  uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
768
769
  CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
770
                               std::string &name, uint64_t *bit_offset_ptr,
771
                               uint32_t *bitfield_bit_size_ptr,
772
                               bool *is_bitfield_ptr) override;
773
774
  uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override;
775
776
  uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override;
777
778
  CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type,
779
                                         size_t idx,
780
                                         uint32_t *bit_offset_ptr) override;
781
782
  CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type,
783
                                          size_t idx,
784
                                          uint32_t *bit_offset_ptr) override;
785
786
  static uint32_t GetNumPointeeChildren(clang::QualType type);
787
788
  CompilerType GetChildCompilerTypeAtIndex(
789
      lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
790
      bool transparent_pointers, bool omit_empty_base_classes,
791
      bool ignore_array_bounds, std::string &child_name,
792
      uint32_t &child_byte_size, int32_t &child_byte_offset,
793
      uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
794
      bool &child_is_base_class, bool &child_is_deref_of_parent,
795
      ValueObject *valobj, uint64_t &language_flags) override;
796
797
  // Lookup a child given a name. This function will match base class names and
798
  // member member names in "clang_type" only, not descendants.
799
  uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
800
                                   const char *name,
801
                                   bool omit_empty_base_classes) override;
802
803
  // Lookup a child member given a name. This function will match member names
804
  // only and will descend into "clang_type" children in search for the first
805
  // member in this class, or any base class that matches "name".
806
  // TODO: Return all matches for a given name by returning a
807
  // vector<vector<uint32_t>>
808
  // so we catch all names that match a given child name, not just the first.
809
  size_t
810
  GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
811
                                const char *name, bool omit_empty_base_classes,
812
                                std::vector<uint32_t> &child_indexes) override;
813
814
  size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override;
815
816
  lldb::TemplateArgumentKind
817
  GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
818
                          size_t idx) override;
819
  CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
820
                                       size_t idx) override;
821
  llvm::Optional<CompilerType::IntegralTemplateArgument>
822
  GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
823
                              size_t idx) override;
824
825
  CompilerType GetTypeForFormatters(void *type) override;
826
827
585
#define LLDB_INVALID_DECL_LEVEL UINT32_MAX
828
  // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx
829
  // could not be found in decl_ctx.
830
  uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx,
831
                           clang::DeclContext *child_decl_ctx,
832
                           ConstString *child_name = nullptr,
833
                           CompilerType *child_type = nullptr);
834
835
  // Modifying RecordType
836
  static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type,
837
                                                llvm::StringRef name,
838
                                                const CompilerType &field_type,
839
                                                lldb::AccessType access,
840
                                                uint32_t bitfield_bit_size);
841
842
  static void BuildIndirectFields(const CompilerType &type);
843
844
  static void SetIsPacked(const CompilerType &type);
845
846
  static clang::VarDecl *AddVariableToRecordType(const CompilerType &type,
847
                                                 llvm::StringRef name,
848
                                                 const CompilerType &var_type,
849
                                                 lldb::AccessType access);
850
851
  /// Initializes a variable with an integer value.
852
  /// \param var The variable to initialize. Must not already have an
853
  ///            initializer and must have an integer or enum type.
854
  /// \param init_value The integer value that the variable should be
855
  ///                   initialized to. Has to match the bit width of the
856
  ///                   variable type.
857
  static void SetIntegerInitializerForVariable(clang::VarDecl *var,
858
                                               const llvm::APInt &init_value);
859
860
  /// Initializes a variable with a floating point value.
861
  /// \param var The variable to initialize. Must not already have an
862
  ///            initializer and must have a floating point type.
863
  /// \param init_value The float value that the variable should be
864
  ///                   initialized to.
865
  static void
866
  SetFloatingInitializerForVariable(clang::VarDecl *var,
867
                                    const llvm::APFloat &init_value);
868
869
  clang::CXXMethodDecl *AddMethodToCXXRecordType(
870
      lldb::opaque_compiler_type_t type, llvm::StringRef name,
871
      const char *mangled_name, const CompilerType &method_type,
872
      lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline,
873
      bool is_explicit, bool is_attr_used, bool is_artificial);
874
875
  void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type);
876
877
  // C++ Base Classes
878
  std::unique_ptr<clang::CXXBaseSpecifier>
879
  CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
880
                           lldb::AccessType access, bool is_virtual,
881
                           bool base_of_class);
882
883
  bool TransferBaseClasses(
884
      lldb::opaque_compiler_type_t type,
885
      std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases);
886
887
  static bool SetObjCSuperClass(const CompilerType &type,
888
                                const CompilerType &superclass_compiler_type);
889
890
  static bool AddObjCClassProperty(const CompilerType &type,
891
                                   const char *property_name,
892
                                   const CompilerType &property_compiler_type,
893
                                   clang::ObjCIvarDecl *ivar_decl,
894
                                   const char *property_setter_name,
895
                                   const char *property_getter_name,
896
                                   uint32_t property_attributes,
897
                                   ClangASTMetadata *metadata);
898
899
  static clang::ObjCMethodDecl *AddMethodToObjCObjectType(
900
      const CompilerType &type,
901
      const char *name, // the full symbol name as seen in the symbol table
902
                        // (lldb::opaque_compiler_type_t type, "-[NString
903
                        // stringWithCString:]")
904
      const CompilerType &method_compiler_type, lldb::AccessType access,
905
      bool is_artificial, bool is_variadic, bool is_objc_direct_call);
906
907
  static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type,
908
                                    bool has_extern);
909
910
  // Tag Declarations
911
  static bool StartTagDeclarationDefinition(const CompilerType &type);
912
913
  static bool CompleteTagDeclarationDefinition(const CompilerType &type);
914
915
  // Modifying Enumeration types
916
  clang::EnumConstantDecl *AddEnumerationValueToEnumerationType(
917
      const CompilerType &enum_type, const Declaration &decl, const char *name,
918
      int64_t enum_value, uint32_t enum_value_bit_size);
919
  clang::EnumConstantDecl *AddEnumerationValueToEnumerationType(
920
      const CompilerType &enum_type, const Declaration &decl, const char *name,
921
      const llvm::APSInt &value);
922
923
  /// Returns the underlying integer type for an enum type. If the given type
924
  /// is invalid or not an enum-type, the function returns an invalid
925
  /// CompilerType.
926
  CompilerType GetEnumerationIntegerType(CompilerType type);
927
928
  // Pointers & References
929
930
  // Call this function using the class type when you want to make a member
931
  // pointer type to pointee_type.
932
  static CompilerType CreateMemberPointerType(const CompilerType &type,
933
                                              const CompilerType &pointee_type);
934
935
  // Dumping types
936
#ifndef NDEBUG
937
  /// Convenience LLVM-style dump method for use in the debugger only.
938
  /// In contrast to the other \p Dump() methods this directly invokes
939
  /// \p clang::QualType::dump().
940
  LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override;
941
#endif
942
943
  /// \see lldb_private::TypeSystem::Dump
944
  void Dump(llvm::raw_ostream &output) override;
945
946
  /// Dump clang AST types from the symbol file.
947
  ///
948
  /// \param[in] s
949
  ///       A stream to send the dumped AST node(s) to
950
  /// \param[in] symbol_name
951
  ///       The name of the symbol to dump, if it is empty dump all the symbols
952
  void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name);
953
954
  void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
955
                 Stream *s, lldb::Format format, const DataExtractor &data,
956
                 lldb::offset_t data_offset, size_t data_byte_size,
957
                 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
958
                 bool show_types, bool show_summary, bool verbose,
959
                 uint32_t depth) override;
960
961
  bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
962
                     lldb::Format format, const DataExtractor &data,
963
                     lldb::offset_t data_offset, size_t data_byte_size,
964
                     uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
965
                     ExecutionContextScope *exe_scope) override;
966
967
  void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
968
                   Stream *s, const DataExtractor &data,
969
                   lldb::offset_t data_offset, size_t data_byte_size) override;
970
971
  void DumpTypeDescription(
972
      lldb::opaque_compiler_type_t type,
973
      lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override;
974
975
  void DumpTypeDescription(
976
      lldb::opaque_compiler_type_t type, Stream *s,
977
      lldb::DescriptionLevel level = lldb::eDescriptionLevelFull) override;
978
979
  static void DumpTypeName(const CompilerType &type);
980
981
  static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type);
982
983
  static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type);
984
985
  static clang::TagDecl *GetAsTagDecl(const CompilerType &type);
986
987
  static clang::TypedefNameDecl *GetAsTypedefDecl(const CompilerType &type);
988
989
  static clang::CXXRecordDecl *
990
  GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type);
991
992
  static clang::ObjCInterfaceDecl *
993
  GetAsObjCInterfaceDecl(const CompilerType &type);
994
995
  clang::ClassTemplateDecl *ParseClassTemplateDecl(
996
      clang::DeclContext *decl_ctx, OptionalClangModuleID owning_module,
997
      lldb::AccessType access_type, const char *parent_name, int tag_decl_kind,
998
      const TypeSystemClang::TemplateParameterInfos &template_param_infos);
999
1000
  clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx,
1001
                                           OptionalClangModuleID owning_module);
1002
1003
  clang::UsingDirectiveDecl *
1004
  CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx,
1005
                                  OptionalClangModuleID owning_module,
1006
                                  clang::NamespaceDecl *ns_decl);
1007
1008
  clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1009
                                           OptionalClangModuleID owning_module,
1010
                                           clang::NamedDecl *target);
1011
1012
  clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context,
1013
                                            OptionalClangModuleID owning_module,
1014
                                            const char *name,
1015
                                            clang::QualType type);
1016
1017
  static lldb::opaque_compiler_type_t
1018
  GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type);
1019
1020
3.19M
  static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) {
1021
3.19M
    if (type)
1022
3.19M
      return clang::QualType::getFromOpaquePtr(type);
1023
0
    return clang::QualType();
1024
3.19M
  }
1025
1026
  static clang::QualType
1027
2.88M
  GetCanonicalQualType(lldb::opaque_compiler_type_t type) {
1028
2.88M
    if (type)
1029
2.88M
      return clang::QualType::getFromOpaquePtr(type).getCanonicalType();
1030
0
    return clang::QualType();
1031
2.88M
  }
1032
1033
  clang::DeclarationName
1034
  GetDeclarationName(llvm::StringRef name,
1035
                     const CompilerType &function_clang_type);
1036
1037
0
  clang::LangOptions *GetLangOpts() const {
1038
0
    return m_language_options_up.get();
1039
0
  }
1040
0
  clang::SourceManager *GetSourceMgr() const {
1041
0
    return m_source_manager_up.get();
1042
0
  }
1043
1044
private:
1045
  /// Returns the PrintingPolicy used when generating the internal type names.
1046
  /// These type names are mostly used for the formatter selection.
1047
  clang::PrintingPolicy GetTypePrintingPolicy();
1048
  /// Returns the internal type name for the given NamedDecl using the
1049
  /// type printing policy.
1050
  std::string GetTypeNameForDecl(const clang::NamedDecl *named_decl);
1051
1052
  const clang::ClassTemplateSpecializationDecl *
1053
  GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type);
1054
1055
  // Classes that inherit from TypeSystemClang can see and modify these
1056
  std::string m_target_triple;
1057
  std::unique_ptr<clang::ASTContext> m_ast_up;
1058
  std::unique_ptr<clang::LangOptions> m_language_options_up;
1059
  std::unique_ptr<clang::FileManager> m_file_manager_up;
1060
  std::unique_ptr<clang::SourceManager> m_source_manager_up;
1061
  std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_up;
1062
  std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_up;
1063
  std::shared_ptr<clang::TargetOptions> m_target_options_rp;
1064
  std::unique_ptr<clang::TargetInfo> m_target_info_up;
1065
  std::unique_ptr<clang::IdentifierTable> m_identifier_table_up;
1066
  std::unique_ptr<clang::SelectorTable> m_selector_table_up;
1067
  std::unique_ptr<clang::Builtin::Context> m_builtins_up;
1068
  std::unique_ptr<clang::HeaderSearch> m_header_search_up;
1069
  std::unique_ptr<clang::ModuleMap> m_module_map_up;
1070
  std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_up;
1071
  std::unique_ptr<PDBASTParser> m_pdb_ast_parser_up;
1072
  std::unique_ptr<clang::MangleContext> m_mangle_ctx_up;
1073
  uint32_t m_pointer_byte_size = 0;
1074
  bool m_ast_owned = false;
1075
  /// A string describing what this TypeSystemClang represents (e.g.,
1076
  /// AST for debug information, an expression, some other utility ClangAST).
1077
  /// Useful for logging and debugging.
1078
  std::string m_display_name;
1079
1080
  typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap;
1081
  /// Maps Decls to their associated ClangASTMetadata.
1082
  DeclMetadataMap m_decl_metadata;
1083
1084
  typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap;
1085
  /// Maps Types to their associated ClangASTMetadata.
1086
  TypeMetadataMap m_type_metadata;
1087
1088
  typedef llvm::DenseMap<const clang::CXXRecordDecl *, clang::AccessSpecifier>
1089
      CXXRecordDeclAccessMap;
1090
  /// Maps CXXRecordDecl to their most recent added method/field's
1091
  /// AccessSpecifier.
1092
  CXXRecordDeclAccessMap m_cxx_record_decl_access;
1093
1094
  /// The sema associated that is currently used to build this ASTContext.
1095
  /// May be null if we are already done parsing this ASTContext or the
1096
  /// ASTContext wasn't created by parsing source code.
1097
  clang::Sema *m_sema = nullptr;
1098
1099
  // For TypeSystemClang only
1100
  TypeSystemClang(const TypeSystemClang &);
1101
  const TypeSystemClang &operator=(const TypeSystemClang &);
1102
  /// Creates the internal ASTContext.
1103
  void CreateASTContext();
1104
  void SetTargetTriple(llvm::StringRef target_triple);
1105
};
1106
1107
/// The TypeSystemClang instance used for the scratch ASTContext in a
1108
/// lldb::Target.
1109
class ScratchTypeSystemClang : public TypeSystemClang {
1110
  /// LLVM RTTI support
1111
  static char ID;
1112
1113
public:
1114
  ScratchTypeSystemClang(Target &target, llvm::Triple triple);
1115
1116
2.42k
  ~ScratchTypeSystemClang() override = default;
1117
1118
  void Finalize() override;
1119
1120
  /// The different kinds of isolated ASTs within the scratch TypeSystem.
1121
  ///
1122
  /// These ASTs are isolated from the main scratch AST and are each
1123
  /// dedicated to a special language option/feature that makes the contained
1124
  /// AST nodes incompatible with other AST nodes.
1125
  enum IsolatedASTKind {
1126
    /// The isolated AST for declarations/types from expressions that imported
1127
    /// type information from a C++ module. The templates from a C++ module
1128
    /// often conflict with the templates we generate from debug information,
1129
    /// so we put these types in their own AST.
1130
    CppModules
1131
  };
1132
1133
  /// Alias for requesting the default scratch TypeSystemClang in GetForTarget.
1134
  // This isn't constexpr as gtest/llvm::Optional comparison logic is trying
1135
  // to get the address of this for pretty-printing.
1136
  static const llvm::NoneType DefaultAST;
1137
1138
  /// Infers the appropriate sub-AST from Clang's LangOptions.
1139
  static llvm::Optional<IsolatedASTKind>
1140
26.7k
  InferIsolatedASTKindFromLangOpts(const clang::LangOptions &l) {
1141
    // If modules are activated we want the dedicated C++ module AST.
1142
    // See IsolatedASTKind::CppModules for more info.
1143
26.7k
    if (l.Modules)
1144
2.23k
      return IsolatedASTKind::CppModules;
1145
24.5k
    return DefaultAST;
1146
26.7k
  }
1147
1148
  /// Returns the scratch TypeSystemClang for the given target.
1149
  /// \param target The Target which scratch TypeSystemClang should be returned.
1150
  /// \param ast_kind Allows requesting a specific sub-AST instead of the
1151
  ///                 default scratch AST. See also `IsolatedASTKind`.
1152
  /// \param create_on_demand If the scratch TypeSystemClang instance can be
1153
  /// created by this call if it doesn't exist yet. If it doesn't exist yet and
1154
  /// this parameter is false, this function returns a nullptr.
1155
  /// \return The scratch type system of the target or a nullptr in case an
1156
  ///         error occurred.
1157
  static TypeSystemClang *
1158
  GetForTarget(Target &target,
1159
               llvm::Optional<IsolatedASTKind> ast_kind = DefaultAST,
1160
               bool create_on_demand = true);
1161
1162
  /// Returns the scratch TypeSystemClang for the given target. The returned
1163
  /// TypeSystemClang will be the scratch AST or a sub-AST, depending on which
1164
  /// fits best to the passed LangOptions.
1165
  /// \param target The Target which scratch TypeSystemClang should be returned.
1166
  /// \param lang_opts The LangOptions of a clang ASTContext that the caller
1167
  ///                  wants to export type information from. This is used to
1168
  ///                  find the best matching sub-AST that will be returned.
1169
  static TypeSystemClang *GetForTarget(Target &target,
1170
26.7k
                                       const clang::LangOptions &lang_opts) {
1171
26.7k
    return GetForTarget(target, InferIsolatedASTKindFromLangOpts(lang_opts));
1172
26.7k
  }
1173
1174
  /// \see lldb_private::TypeSystem::Dump
1175
  void Dump(llvm::raw_ostream &output) override;
1176
1177
  UserExpression *
1178
  GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
1179
                    lldb::LanguageType language,
1180
                    Expression::ResultType desired_type,
1181
                    const EvaluateExpressionOptions &options,
1182
                    ValueObject *ctx_obj) override;
1183
1184
  FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
1185
                                    const Address &function_address,
1186
                                    const ValueList &arg_value_list,
1187
                                    const char *name) override;
1188
1189
  std::unique_ptr<UtilityFunction>
1190
  CreateUtilityFunction(std::string text, std::string name) override;
1191
1192
  PersistentExpressionState *GetPersistentExpressionState() override;
1193
1194
  /// Unregisters the given ASTContext as a source from the scratch AST (and
1195
  /// all sub-ASTs).
1196
  /// \see ClangASTImporter::ForgetSource
1197
  void ForgetSource(clang::ASTContext *src_ctx, ClangASTImporter &importer);
1198
1199
  // llvm casting support
1200
202k
  bool isA(const void *ClassID) const override {
1201
202k
    return ClassID == &ID || 
TypeSystemClang::isA(ClassID)85.4k
;
1202
202k
  }
1203
116k
  static bool classof(const TypeSystem *ts) { return ts->isA(&ID); }
1204
1205
private:
1206
  std::unique_ptr<ClangASTSource> CreateASTSource();
1207
  /// Returns the requested sub-AST.
1208
  /// Will lazily create the sub-AST if it hasn't been created before.
1209
  TypeSystemClang &GetIsolatedAST(IsolatedASTKind feature);
1210
1211
  /// The target triple.
1212
  /// This was potentially adjusted and might not be identical to the triple
1213
  /// of `m_target_wp`.
1214
  llvm::Triple m_triple;
1215
  lldb::TargetWP m_target_wp;
1216
  /// The persistent variables associated with this process for the expression
1217
  /// parser.
1218
  std::unique_ptr<ClangPersistentVariables> m_persistent_variables;
1219
  /// The ExternalASTSource that performs lookups and completes minimally
1220
  /// imported types.
1221
  std::unique_ptr<ClangASTSource> m_scratch_ast_source_up;
1222
1223
  // FIXME: GCC 5.x doesn't support enum as map keys.
1224
  typedef int IsolatedASTKey;
1225
1226
  /// Map from IsolatedASTKind to their actual TypeSystemClang instance.
1227
  /// This map is lazily filled with sub-ASTs and should be accessed via
1228
  /// `GetSubAST` (which lazily fills this map).
1229
  std::unordered_map<IsolatedASTKey, std::unique_ptr<TypeSystemClang>>
1230
      m_isolated_asts;
1231
};
1232
1233
} // namespace lldb_private
1234
1235
#endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLANG_TYPESYSTEMCLANG_H