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