xref: /freebsd-11-stable/contrib/llvm-project/lldb/include/lldb/Symbol/ClangASTContext.h (revision 9d1b76e2786478cdef6a403311925c92899640b8)
1 //===-- ClangASTContext.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 liblldb_ClangASTContext_h_
10 #define liblldb_ClangASTContext_h_
11 
12 #include <stdint.h>
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/TemplateBase.h"
25 #include "llvm/ADT/APSInt.h"
26 #include "llvm/ADT/SmallVector.h"
27 
28 #include "lldb/Core/ClangForward.h"
29 #include "lldb/Expression/ExpressionVariable.h"
30 #include "lldb/Symbol/CompilerType.h"
31 #include "lldb/Symbol/TypeSystem.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Utility/ConstString.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Logging.h"
36 #include "lldb/lldb-enumerations.h"
37 
38 class DWARFASTParserClang;
39 #ifdef LLDB_ENABLE_ALL
40 class PDBASTParser;
41 #endif // LLDB_ENABLE_ALL
42 
43 namespace lldb_private {
44 
45 class Declaration;
46 
47 class ClangASTContext : public TypeSystem {
48   // LLVM RTTI support
49   static char ID;
50 
51 public:
52   typedef void (*CompleteTagDeclCallback)(void *baton, clang::TagDecl *);
53   typedef void (*CompleteObjCInterfaceDeclCallback)(void *baton,
54                                                     clang::ObjCInterfaceDecl *);
55 
56   // llvm casting support
isA(const void * ClassID)57   bool isA(const void *ClassID) const override { return ClassID == &ID; }
classof(const TypeSystem * ts)58   static bool classof(const TypeSystem *ts) { return ts->isA(&ID); }
59 
60   /// Constructs a ClangASTContext with an ASTContext using the given triple.
61   ///
62   /// \param triple The llvm::Triple used for the ASTContext. The triple defines
63   ///               certain characteristics of the ASTContext and its types
64   ///               (e.g., whether certain primitive types exist or what their
65   ///               signedness is).
66   explicit ClangASTContext(llvm::Triple triple);
67 
68   /// Constructs a ClangASTContext that uses an existing ASTContext internally.
69   /// Useful when having an existing ASTContext created by Clang.
70   ///
71   /// \param existing_ctxt An existing ASTContext.
72   explicit ClangASTContext(clang::ASTContext &existing_ctxt);
73 
74   ~ClangASTContext() override;
75 
76   void Finalize() override;
77 
78   // PluginInterface functions
79   ConstString GetPluginName() override;
80 
81   uint32_t GetPluginVersion() override;
82 
83   static ConstString GetPluginNameStatic();
84 
85   static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
86                                            Module *module, Target *target);
87 
88   static LanguageSet GetSupportedLanguagesForTypes();
89   static LanguageSet GetSupportedLanguagesForExpressions();
90 
91   static void Initialize();
92 
93   static void Terminate();
94 
95   static ClangASTContext *GetASTContext(clang::ASTContext *ast_ctx);
96 
97   static ClangASTContext *GetScratch(Target &target,
98                                      bool create_on_demand = true) {
99     auto type_system_or_err = target.GetScratchTypeSystemForLanguage(
100         lldb::eLanguageTypeC, create_on_demand);
101     if (auto err = type_system_or_err.takeError()) {
102       LLDB_LOG_ERROR(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET),
103                      std::move(err), "Couldn't get scratch ClangASTContext");
104       return nullptr;
105     }
106     return llvm::dyn_cast<ClangASTContext>(&type_system_or_err.get());
107   }
108 
109   clang::ASTContext &getASTContext();
110 
111   clang::MangleContext *getMangleContext();
112 
113   std::shared_ptr<clang::TargetOptions> &getTargetOptions();
114 
115   clang::TargetInfo *getTargetInfo();
116 
117   void setSema(clang::Sema *s);
getSema()118   clang::Sema *getSema() { return m_sema; }
119 
120   const char *GetTargetTriple();
121 
122   void SetExternalSource(
123       llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> &ast_source_up);
124 
GetCompleteDecl(clang::Decl * decl)125   bool GetCompleteDecl(clang::Decl *decl) {
126     return ClangASTContext::GetCompleteDecl(&getASTContext(), decl);
127   }
128 
129   static void DumpDeclHiearchy(clang::Decl *decl);
130 
131   static void DumpDeclContextHiearchy(clang::DeclContext *decl_ctx);
132 
133   static bool DeclsAreEquivalent(clang::Decl *lhs_decl, clang::Decl *rhs_decl);
134 
135   static bool GetCompleteDecl(clang::ASTContext *ast, clang::Decl *decl);
136 
137   void SetMetadataAsUserID(const clang::Decl *decl, lldb::user_id_t user_id);
138   void SetMetadataAsUserID(const clang::Type *type, lldb::user_id_t user_id);
139 
140   void SetMetadata(const clang::Decl *object, ClangASTMetadata &meta_data);
141 
142   void SetMetadata(const clang::Type *object, ClangASTMetadata &meta_data);
143   ClangASTMetadata *GetMetadata(const clang::Decl *object);
144   ClangASTMetadata *GetMetadata(const clang::Type *object);
145 
146   // Basic Types
147   CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
148                                                    size_t bit_size) override;
149 
150   CompilerType GetBasicType(lldb::BasicType type);
151 
152   static lldb::BasicType GetBasicTypeEnumeration(ConstString name);
153 
154   CompilerType
155   GetBuiltinTypeForDWARFEncodingAndBitSize(llvm::StringRef type_name,
156                                            uint32_t dw_ate, uint32_t bit_size);
157 
158   CompilerType GetCStringType(bool is_const);
159 
160   static clang::DeclContext *GetDeclContextForType(clang::QualType type);
161 
162   static clang::DeclContext *GetDeclContextForType(const CompilerType &type);
163 
164   uint32_t GetPointerByteSize() override;
165 
GetTranslationUnitDecl()166   clang::TranslationUnitDecl *GetTranslationUnitDecl() {
167     return getASTContext().getTranslationUnitDecl();
168   }
169 
170   static bool AreTypesSame(CompilerType type1, CompilerType type2,
171                            bool ignore_qualifiers = false);
172 
173   /// Creates a CompilerType form the given QualType with the current
174   /// ClangASTContext instance as the CompilerType's typesystem.
175   /// \param qt The QualType for a type that belongs to the ASTContext of this
176   ///           ClangASTContext.
177   /// \return The CompilerType representing the given QualType. If the
178   ///         QualType's type pointer is a nullptr then the function returns an
179   ///         invalid CompilerType.
GetType(clang::QualType qt)180   CompilerType GetType(clang::QualType qt) {
181     if (qt.getTypePtrOrNull() == nullptr)
182       return CompilerType();
183     // Check that the type actually belongs to this ClangASTContext.
184     assert(qt->getAsTagDecl() == nullptr ||
185            &qt->getAsTagDecl()->getASTContext() == &getASTContext());
186     return CompilerType(this, qt.getAsOpaquePtr());
187   }
188 
189   CompilerType GetTypeForDecl(clang::NamedDecl *decl);
190 
191   CompilerType GetTypeForDecl(clang::TagDecl *decl);
192 
193   CompilerType GetTypeForDecl(clang::ObjCInterfaceDecl *objc_decl);
194 
195   template <typename RecordDeclType>
196   CompilerType
197   GetTypeForIdentifier(ConstString type_name,
198                        clang::DeclContext *decl_context = nullptr) {
199     CompilerType compiler_type;
200 
201     if (type_name.GetLength()) {
202       clang::ASTContext &ast = getASTContext();
203       if (!decl_context)
204         decl_context = ast.getTranslationUnitDecl();
205 
206       clang::IdentifierInfo &myIdent = ast.Idents.get(type_name.GetCString());
207       clang::DeclarationName myName =
208           ast.DeclarationNames.getIdentifier(&myIdent);
209 
210       clang::DeclContext::lookup_result result = decl_context->lookup(myName);
211 
212       if (!result.empty()) {
213         clang::NamedDecl *named_decl = result[0];
214         if (const RecordDeclType *record_decl =
215                 llvm::dyn_cast<RecordDeclType>(named_decl))
216           compiler_type.SetCompilerType(
217               this, clang::QualType(record_decl->getTypeForDecl(), 0)
218                         .getAsOpaquePtr());
219       }
220     }
221 
222     return compiler_type;
223   }
224 
225   CompilerType CreateStructForIdentifier(
226       ConstString type_name,
227       const std::initializer_list<std::pair<const char *, CompilerType>>
228           &type_fields,
229       bool packed = false);
230 
231   CompilerType GetOrCreateStructForIdentifier(
232       ConstString type_name,
233       const std::initializer_list<std::pair<const char *, CompilerType>>
234           &type_fields,
235       bool packed = false);
236 
237   static bool IsOperator(llvm::StringRef name,
238                          clang::OverloadedOperatorKind &op_kind);
239 
240   // Structure, Unions, Classes
241 
242   static clang::AccessSpecifier
243   ConvertAccessTypeToAccessSpecifier(lldb::AccessType access);
244 
245   static clang::AccessSpecifier
246   UnifyAccessSpecifiers(clang::AccessSpecifier lhs, clang::AccessSpecifier rhs);
247 
248   static uint32_t GetNumBaseClasses(const clang::CXXRecordDecl *cxx_record_decl,
249                                     bool omit_empty_base_classes);
250 
251   CompilerType CreateRecordType(clang::DeclContext *decl_ctx,
252                                 lldb::AccessType access_type,
253                                 llvm::StringRef name, int kind,
254                                 lldb::LanguageType language,
255                                 ClangASTMetadata *metadata = nullptr,
256                                 bool exports_symbols = false);
257 
258   class TemplateParameterInfos {
259   public:
IsValid()260     bool IsValid() const {
261       if (args.empty())
262         return false;
263       return args.size() == names.size() &&
264         ((bool)pack_name == (bool)packed_args) &&
265         (!packed_args || !packed_args->packed_args);
266     }
267 
268     llvm::SmallVector<const char *, 2> names;
269     llvm::SmallVector<clang::TemplateArgument, 2> args;
270 
271     const char * pack_name = nullptr;
272     std::unique_ptr<TemplateParameterInfos> packed_args;
273   };
274 
275   clang::FunctionTemplateDecl *
276   CreateFunctionTemplateDecl(clang::DeclContext *decl_ctx,
277                              clang::FunctionDecl *func_decl, const char *name,
278                              const TemplateParameterInfos &infos);
279 
280   void CreateFunctionTemplateSpecializationInfo(
281       clang::FunctionDecl *func_decl, clang::FunctionTemplateDecl *Template,
282       const TemplateParameterInfos &infos);
283 
284   clang::ClassTemplateDecl *
285   CreateClassTemplateDecl(clang::DeclContext *decl_ctx,
286                           lldb::AccessType access_type, const char *class_name,
287                           int kind, const TemplateParameterInfos &infos);
288 
289   clang::TemplateTemplateParmDecl *
290   CreateTemplateTemplateParmDecl(const char *template_name);
291 
292   clang::ClassTemplateSpecializationDecl *CreateClassTemplateSpecializationDecl(
293       clang::DeclContext *decl_ctx,
294       clang::ClassTemplateDecl *class_template_decl, int kind,
295       const TemplateParameterInfos &infos);
296 
297   CompilerType
298   CreateClassTemplateSpecializationType(clang::ClassTemplateSpecializationDecl *
299                                             class_template_specialization_decl);
300 
301   static clang::DeclContext *
302   GetAsDeclContext(clang::FunctionDecl *function_decl);
303 
304   static bool CheckOverloadedOperatorKindParameterCount(
305       bool is_method, clang::OverloadedOperatorKind op_kind,
306       uint32_t num_params);
307 
308   bool FieldIsBitfield(clang::FieldDecl *field, uint32_t &bitfield_bit_size);
309 
310   static bool RecordHasFields(const clang::RecordDecl *record_decl);
311 
312   CompilerType CreateObjCClass(llvm::StringRef name,
313                                clang::DeclContext *decl_ctx, bool isForwardDecl,
314                                bool isInternal,
315                                ClangASTMetadata *metadata = nullptr);
316 
317   bool SetTagTypeKind(clang::QualType type, int kind) const;
318 
319   bool SetDefaultAccessForRecordFields(clang::RecordDecl *record_decl,
320                                        int default_accessibility,
321                                        int *assigned_accessibilities,
322                                        size_t num_assigned_accessibilities);
323 
324   // Returns a mask containing bits from the ClangASTContext::eTypeXXX
325   // enumerations
326 
327   // Namespace Declarations
328 
329   clang::NamespaceDecl *
330   GetUniqueNamespaceDeclaration(const char *name, clang::DeclContext *decl_ctx,
331                                 bool is_inline = false);
332 
333   // Function Types
334 
335   clang::FunctionDecl *
336   CreateFunctionDeclaration(clang::DeclContext *decl_ctx, const char *name,
337                             const CompilerType &function_Type, int storage,
338                             bool is_inline);
339 
340   CompilerType CreateFunctionType(const CompilerType &result_type,
341                                   const CompilerType *args, unsigned num_args,
342                                   bool is_variadic, unsigned type_quals,
343                                   clang::CallingConv cc);
344 
CreateFunctionType(const CompilerType & result_type,const CompilerType * args,unsigned num_args,bool is_variadic,unsigned type_quals)345   CompilerType CreateFunctionType(const CompilerType &result_type,
346                                   const CompilerType *args, unsigned num_args,
347                                   bool is_variadic, unsigned type_quals) {
348     return CreateFunctionType(result_type, args, num_args, is_variadic,
349                               type_quals, clang::CC_C);
350   }
351 
352   clang::ParmVarDecl *CreateParameterDeclaration(clang::DeclContext *decl_ctx,
353                                                  const char *name,
354                                                  const CompilerType &param_type,
355                                                  int storage,
356                                                  bool add_decl=false);
357 
358   void SetFunctionParameters(clang::FunctionDecl *function_decl,
359                              clang::ParmVarDecl **params, unsigned num_params);
360 
361   CompilerType CreateBlockPointerType(const CompilerType &function_type);
362 
363   // Array Types
364 
365   CompilerType CreateArrayType(const CompilerType &element_type,
366                                size_t element_count, bool is_vector);
367 
368   // Enumeration Types
369   CompilerType CreateEnumerationType(const char *name,
370                                      clang::DeclContext *decl_ctx,
371                                      const Declaration &decl,
372                                      const CompilerType &integer_qual_type,
373                                      bool is_scoped);
374 
375   // Integer type functions
376 
377   CompilerType GetIntTypeFromBitSize(size_t bit_size, bool is_signed);
378 
379   CompilerType GetPointerSizedIntType(bool is_signed);
380 
381   // Floating point functions
382 
383   static CompilerType GetFloatTypeFromBitSize(clang::ASTContext *ast,
384                                               size_t bit_size);
385 
386   // TypeSystem methods
387   DWARFASTParser *GetDWARFParser() override;
388 #ifdef LLDB_ENABLE_ALL
389   PDBASTParser *GetPDBParser() override;
390 #endif // LLDB_ENABLE_ALL
391 
392   // ClangASTContext callbacks for external source lookups.
393   void CompleteTagDecl(clang::TagDecl *);
394 
395   void CompleteObjCInterfaceDecl(clang::ObjCInterfaceDecl *);
396 
397   bool LayoutRecordType(
398       const clang::RecordDecl *record_decl, uint64_t &size, uint64_t &alignment,
399       llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
400       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
401           &base_offsets,
402       llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
403           &vbase_offsets);
404 
405   // CompilerDecl override functions
406   ConstString DeclGetName(void *opaque_decl) override;
407 
408   ConstString DeclGetMangledName(void *opaque_decl) override;
409 
410   CompilerDeclContext DeclGetDeclContext(void *opaque_decl) override;
411 
412   CompilerType DeclGetFunctionReturnType(void *opaque_decl) override;
413 
414   size_t DeclGetFunctionNumArguments(void *opaque_decl) override;
415 
416   CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
417                                            size_t arg_idx) override;
418 
419   CompilerType GetTypeForDecl(void *opaque_decl) override;
420 
421   // CompilerDeclContext override functions
422 
423   /// Creates a CompilerDeclContext from the given DeclContext
424   /// with the current ClangASTContext instance as its typesystem.
425   /// The DeclContext has to come from the ASTContext of this
426   /// ClangASTContext.
427   CompilerDeclContext CreateDeclContext(clang::DeclContext *ctx);
428 
429   std::vector<CompilerDecl>
430   DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
431                             const bool ignore_using_decls) override;
432 
433   ConstString DeclContextGetName(void *opaque_decl_ctx) override;
434 
435   ConstString DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) override;
436 
437   bool DeclContextIsClassMethod(void *opaque_decl_ctx,
438                                 lldb::LanguageType *language_ptr,
439                                 bool *is_instance_method_ptr,
440                                 ConstString *language_object_name_ptr) override;
441 
442   bool DeclContextIsContainedInLookup(void *opaque_decl_ctx,
443                                       void *other_opaque_decl_ctx) override;
444 
445   // Clang specific clang::DeclContext functions
446 
447   static clang::DeclContext *
448   DeclContextGetAsDeclContext(const CompilerDeclContext &dc);
449 
450   static clang::ObjCMethodDecl *
451   DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc);
452 
453   static clang::CXXMethodDecl *
454   DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc);
455 
456   static clang::FunctionDecl *
457   DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc);
458 
459   static clang::NamespaceDecl *
460   DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc);
461 
462   static ClangASTMetadata *DeclContextGetMetaData(const CompilerDeclContext &dc,
463                                                   const clang::Decl *object);
464 
465   static clang::ASTContext *
466   DeclContextGetClangASTContext(const CompilerDeclContext &dc);
467 
468   // Tests
469 
470   bool IsArrayType(lldb::opaque_compiler_type_t type,
471                    CompilerType *element_type, uint64_t *size,
472                    bool *is_incomplete) override;
473 
474   bool IsVectorType(lldb::opaque_compiler_type_t type,
475                     CompilerType *element_type, uint64_t *size) override;
476 
477   bool IsAggregateType(lldb::opaque_compiler_type_t type) override;
478 
479   bool IsAnonymousType(lldb::opaque_compiler_type_t type) override;
480 
481   bool IsBeingDefined(lldb::opaque_compiler_type_t type) override;
482 
483   bool IsCharType(lldb::opaque_compiler_type_t type) override;
484 
485   bool IsCompleteType(lldb::opaque_compiler_type_t type) override;
486 
487   bool IsConst(lldb::opaque_compiler_type_t type) override;
488 
489   bool IsCStringType(lldb::opaque_compiler_type_t type,
490                      uint32_t &length) override;
491 
492   static bool IsCXXClassType(const CompilerType &type);
493 
494   bool IsDefined(lldb::opaque_compiler_type_t type) override;
495 
496   bool IsFloatingPointType(lldb::opaque_compiler_type_t type, uint32_t &count,
497                            bool &is_complex) override;
498 
499   bool IsFunctionType(lldb::opaque_compiler_type_t type,
500                       bool *is_variadic_ptr) override;
501 
502   uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
503                                   CompilerType *base_type_ptr) override;
504 
505   size_t
506   GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) override;
507 
508   CompilerType GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
509                                           const size_t index) override;
510 
511   bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) override;
512 
513   bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
514                           CompilerType *function_pointer_type_ptr) override;
515 
516   bool IsIntegerType(lldb::opaque_compiler_type_t type,
517                      bool &is_signed) override;
518 
519   bool IsEnumerationType(lldb::opaque_compiler_type_t type,
520                          bool &is_signed) override;
521 
522   static bool IsObjCClassType(const CompilerType &type);
523 
524   static bool IsObjCClassTypeAndHasIVars(const CompilerType &type,
525                                          bool check_superclass);
526 
527   static bool IsObjCObjectOrInterfaceType(const CompilerType &type);
528 
529   static bool IsObjCObjectPointerType(const CompilerType &type,
530                                       CompilerType *target_type = nullptr);
531 
532   bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) override;
533 
534   static bool IsClassType(lldb::opaque_compiler_type_t type);
535 
536   static bool IsEnumType(lldb::opaque_compiler_type_t type);
537 
538   bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
539                              CompilerType *target_type, // Can pass nullptr
540                              bool check_cplusplus, bool check_objc) override;
541 
542   bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) override;
543 
544   bool IsPointerType(lldb::opaque_compiler_type_t type,
545                      CompilerType *pointee_type) override;
546 
547   bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
548                                 CompilerType *pointee_type) override;
549 
550   bool IsReferenceType(lldb::opaque_compiler_type_t type,
551                        CompilerType *pointee_type, bool *is_rvalue) override;
552 
553   bool IsScalarType(lldb::opaque_compiler_type_t type) override;
554 
555   bool IsTypedefType(lldb::opaque_compiler_type_t type) override;
556 
557   bool IsVoidType(lldb::opaque_compiler_type_t type) override;
558 
559   bool CanPassInRegisters(const CompilerType &type) override;
560 
561   bool SupportsLanguage(lldb::LanguageType language) override;
562 
563   static llvm::Optional<std::string> GetCXXClassName(const CompilerType &type);
564 
565   // Type Completion
566 
567   bool GetCompleteType(lldb::opaque_compiler_type_t type) override;
568 
569   // Accessors
570 
571   ConstString GetTypeName(lldb::opaque_compiler_type_t type) override;
572 
573   uint32_t GetTypeInfo(lldb::opaque_compiler_type_t type,
574                        CompilerType *pointee_or_element_compiler_type) override;
575 
576   lldb::LanguageType
577   GetMinimumLanguage(lldb::opaque_compiler_type_t type) override;
578 
579   lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) override;
580 
581   unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) override;
582 
583   // Creating related types
584 
585   // Using the current type, create a new typedef to that type using
586   // "typedef_name" as the name and "decl_ctx" as the decl context.
587   static CompilerType
588   CreateTypedefType(const CompilerType &type, const char *typedef_name,
589                     const CompilerDeclContext &compiler_decl_ctx);
590 
591   CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
592                                    uint64_t *stride) override;
593 
594   CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
595                             uint64_t size) override;
596 
597   CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) override;
598 
599   CompilerType
600   GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) override;
601 
602   // Returns -1 if this isn't a function of if the function doesn't have a
603   // prototype Returns a value >= 0 if there is a prototype.
604   int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) override;
605 
606   CompilerType GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
607                                               size_t idx) override;
608 
609   CompilerType
610   GetFunctionReturnType(lldb::opaque_compiler_type_t type) override;
611 
612   size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) override;
613 
614   TypeMemberFunctionImpl
615   GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
616                            size_t idx) override;
617 
618   CompilerType GetNonReferenceType(lldb::opaque_compiler_type_t type) override;
619 
620   CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) override;
621 
622   CompilerType GetPointerType(lldb::opaque_compiler_type_t type) override;
623 
624   CompilerType
625   GetLValueReferenceType(lldb::opaque_compiler_type_t type) override;
626 
627   CompilerType
628   GetRValueReferenceType(lldb::opaque_compiler_type_t type) override;
629 
630   CompilerType GetAtomicType(lldb::opaque_compiler_type_t type) override;
631 
632   CompilerType AddConstModifier(lldb::opaque_compiler_type_t type) override;
633 
634   CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type) override;
635 
636   CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type) override;
637 
638   CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
639                              const char *name,
640                              const CompilerDeclContext &decl_ctx) override;
641 
642   // If the current object represents a typedef type, get the underlying type
643   CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) override;
644 
645   // Create related types using the current type's AST
646   CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) override;
647 
648   // Exploring the type
649 
650   const llvm::fltSemantics &GetFloatTypeSemantics(size_t byte_size) override;
651 
GetByteSize(lldb::opaque_compiler_type_t type,ExecutionContextScope * exe_scope)652   llvm::Optional<uint64_t> GetByteSize(lldb::opaque_compiler_type_t type,
653                        ExecutionContextScope *exe_scope) {
654     if (llvm::Optional<uint64_t> bit_size = GetBitSize(type, exe_scope))
655       return (*bit_size + 7) / 8;
656     return llvm::None;
657   }
658 
659   llvm::Optional<uint64_t>
660   GetBitSize(lldb::opaque_compiler_type_t type,
661              ExecutionContextScope *exe_scope) override;
662 
663   lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
664                              uint64_t &count) override;
665 
666   lldb::Format GetFormat(lldb::opaque_compiler_type_t type) override;
667 
668   llvm::Optional<size_t>
669   GetTypeBitAlign(lldb::opaque_compiler_type_t type,
670                   ExecutionContextScope *exe_scope) override;
671 
672   uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
673                           bool omit_empty_base_classes,
674                           const ExecutionContext *exe_ctx) override;
675 
676   CompilerType GetBuiltinTypeByName(ConstString name) override;
677 
678   lldb::BasicType
679   GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) override;
680 
681   static lldb::BasicType
682   GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type,
683                           ConstString name);
684 
685   void ForEachEnumerator(
686       lldb::opaque_compiler_type_t type,
687       std::function<bool(const CompilerType &integer_type,
688                          ConstString name,
689                          const llvm::APSInt &value)> const &callback) override;
690 
691   uint32_t GetNumFields(lldb::opaque_compiler_type_t type) override;
692 
693   CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
694                                std::string &name, uint64_t *bit_offset_ptr,
695                                uint32_t *bitfield_bit_size_ptr,
696                                bool *is_bitfield_ptr) override;
697 
698   uint32_t GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) override;
699 
700   uint32_t GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) override;
701 
702   CompilerType GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type,
703                                          size_t idx,
704                                          uint32_t *bit_offset_ptr) override;
705 
706   CompilerType GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type,
707                                           size_t idx,
708                                           uint32_t *bit_offset_ptr) override;
709 
710   static uint32_t GetNumPointeeChildren(clang::QualType type);
711 
712   CompilerType GetChildCompilerTypeAtIndex(
713       lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
714       bool transparent_pointers, bool omit_empty_base_classes,
715       bool ignore_array_bounds, std::string &child_name,
716       uint32_t &child_byte_size, int32_t &child_byte_offset,
717       uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
718       bool &child_is_base_class, bool &child_is_deref_of_parent,
719       ValueObject *valobj, uint64_t &language_flags) override;
720 
721   // Lookup a child given a name. This function will match base class names and
722   // member member names in "clang_type" only, not descendants.
723   uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
724                                    const char *name,
725                                    bool omit_empty_base_classes) override;
726 
727   // Lookup a child member given a name. This function will match member names
728   // only and will descend into "clang_type" children in search for the first
729   // member in this class, or any base class that matches "name".
730   // TODO: Return all matches for a given name by returning a
731   // vector<vector<uint32_t>>
732   // so we catch all names that match a given child name, not just the first.
733   size_t
734   GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
735                                 const char *name, bool omit_empty_base_classes,
736                                 std::vector<uint32_t> &child_indexes) override;
737 
738   size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) override;
739 
740   lldb::TemplateArgumentKind
741   GetTemplateArgumentKind(lldb::opaque_compiler_type_t type,
742                           size_t idx) override;
743   CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
744                                        size_t idx) override;
745   llvm::Optional<CompilerType::IntegralTemplateArgument>
746   GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type,
747                               size_t idx) override;
748 
749   CompilerType GetTypeForFormatters(void *type) override;
750 
751 #define LLDB_INVALID_DECL_LEVEL UINT32_MAX
752   // LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if child_decl_ctx
753   // could not be found in decl_ctx.
754   uint32_t CountDeclLevels(clang::DeclContext *frame_decl_ctx,
755                            clang::DeclContext *child_decl_ctx,
756                            ConstString *child_name = nullptr,
757                            CompilerType *child_type = nullptr);
758 
759   // Modifying RecordType
760   static clang::FieldDecl *AddFieldToRecordType(const CompilerType &type,
761                                                 llvm::StringRef name,
762                                                 const CompilerType &field_type,
763                                                 lldb::AccessType access,
764                                                 uint32_t bitfield_bit_size);
765 
766   static void BuildIndirectFields(const CompilerType &type);
767 
768   static void SetIsPacked(const CompilerType &type);
769 
770   static clang::VarDecl *AddVariableToRecordType(const CompilerType &type,
771                                                  llvm::StringRef name,
772                                                  const CompilerType &var_type,
773                                                  lldb::AccessType access);
774 
775   clang::CXXMethodDecl *AddMethodToCXXRecordType(
776       lldb::opaque_compiler_type_t type, llvm::StringRef name,
777       const char *mangled_name, const CompilerType &method_type,
778       lldb::AccessType access, bool is_virtual, bool is_static, bool is_inline,
779       bool is_explicit, bool is_attr_used, bool is_artificial);
780 
781   void AddMethodOverridesForCXXRecordType(lldb::opaque_compiler_type_t type);
782 
783   // C++ Base Classes
784   std::unique_ptr<clang::CXXBaseSpecifier>
785   CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
786                            lldb::AccessType access, bool is_virtual,
787                            bool base_of_class);
788 
789   bool TransferBaseClasses(
790       lldb::opaque_compiler_type_t type,
791       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases);
792 
793   static bool SetObjCSuperClass(const CompilerType &type,
794                                 const CompilerType &superclass_compiler_type);
795 
796   static bool AddObjCClassProperty(const CompilerType &type,
797                                    const char *property_name,
798                                    const CompilerType &property_compiler_type,
799                                    clang::ObjCIvarDecl *ivar_decl,
800                                    const char *property_setter_name,
801                                    const char *property_getter_name,
802                                    uint32_t property_attributes,
803                                    ClangASTMetadata *metadata);
804 
805   static clang::ObjCMethodDecl *AddMethodToObjCObjectType(
806       const CompilerType &type,
807       const char *name, // the full symbol name as seen in the symbol table
808                         // (lldb::opaque_compiler_type_t type, "-[NString
809                         // stringWithCString:]")
810       const CompilerType &method_compiler_type, lldb::AccessType access,
811       bool is_artificial, bool is_variadic, bool is_objc_direct_call);
812 
813   static bool SetHasExternalStorage(lldb::opaque_compiler_type_t type,
814                                     bool has_extern);
815 
816   // Tag Declarations
817   static bool StartTagDeclarationDefinition(const CompilerType &type);
818 
819   static bool CompleteTagDeclarationDefinition(const CompilerType &type);
820 
821   // Modifying Enumeration types
822   clang::EnumConstantDecl *AddEnumerationValueToEnumerationType(
823       const CompilerType &enum_type, const Declaration &decl, const char *name,
824       int64_t enum_value, uint32_t enum_value_bit_size);
825   clang::EnumConstantDecl *AddEnumerationValueToEnumerationType(
826       const CompilerType &enum_type, const Declaration &decl, const char *name,
827       const llvm::APSInt &value);
828 
829   CompilerType GetEnumerationIntegerType(lldb::opaque_compiler_type_t type);
830 
831   // Pointers & References
832 
833   // Call this function using the class type when you want to make a member
834   // pointer type to pointee_type.
835   static CompilerType CreateMemberPointerType(const CompilerType &type,
836                                               const CompilerType &pointee_type);
837 
838   // Dumping types
839 #ifndef NDEBUG
840   /// Convenience LLVM-style dump method for use in the debugger only.
841   /// In contrast to the other \p Dump() methods this directly invokes
842   /// \p clang::QualType::dump().
843   LLVM_DUMP_METHOD void dump(lldb::opaque_compiler_type_t type) const override;
844 #endif
845 
846   void Dump(Stream &s);
847 
848   /// Dump clang AST types from the symbol file.
849   ///
850   /// \param[in] s
851   ///       A stream to send the dumped AST node(s) to
852   /// \param[in] symbol_name
853   ///       The name of the symbol to dump, if it is empty dump all the symbols
854   void DumpFromSymbolFile(Stream &s, llvm::StringRef symbol_name);
855 
856   void DumpValue(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
857                  Stream *s, lldb::Format format, const DataExtractor &data,
858                  lldb::offset_t data_offset, size_t data_byte_size,
859                  uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
860                  bool show_types, bool show_summary, bool verbose,
861                  uint32_t depth) override;
862 
863   bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
864                      lldb::Format format, const DataExtractor &data,
865                      lldb::offset_t data_offset, size_t data_byte_size,
866                      uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
867                      ExecutionContextScope *exe_scope) override;
868 
869   void DumpSummary(lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx,
870                    Stream *s, const DataExtractor &data,
871                    lldb::offset_t data_offset, size_t data_byte_size) override;
872 
873   void DumpTypeDescription(
874       lldb::opaque_compiler_type_t type) override; // Dump to stdout
875 
876   void DumpTypeDescription(lldb::opaque_compiler_type_t type,
877                            Stream *s) override;
878 
879   static void DumpTypeName(const CompilerType &type);
880 
881   static clang::EnumDecl *GetAsEnumDecl(const CompilerType &type);
882 
883   static clang::RecordDecl *GetAsRecordDecl(const CompilerType &type);
884 
885   static clang::TagDecl *GetAsTagDecl(const CompilerType &type);
886 
887   static clang::TypedefNameDecl *GetAsTypedefDecl(const CompilerType &type);
888 
889   static clang::CXXRecordDecl *
890   GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type);
891 
892   static clang::ObjCInterfaceDecl *
893   GetAsObjCInterfaceDecl(const CompilerType &type);
894 
895   clang::ClassTemplateDecl *ParseClassTemplateDecl(
896       clang::DeclContext *decl_ctx, lldb::AccessType access_type,
897       const char *parent_name, int tag_decl_kind,
898       const ClangASTContext::TemplateParameterInfos &template_param_infos);
899 
900   clang::BlockDecl *CreateBlockDeclaration(clang::DeclContext *ctx);
901 
902   clang::UsingDirectiveDecl *
903   CreateUsingDirectiveDeclaration(clang::DeclContext *decl_ctx,
904                                   clang::NamespaceDecl *ns_decl);
905 
906   clang::UsingDecl *CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
907                                            clang::NamedDecl *target);
908 
909   clang::VarDecl *CreateVariableDeclaration(clang::DeclContext *decl_context,
910                                             const char *name,
911                                             clang::QualType type);
912 
913   static lldb::opaque_compiler_type_t
914   GetOpaqueCompilerType(clang::ASTContext *ast, lldb::BasicType basic_type);
915 
GetQualType(lldb::opaque_compiler_type_t type)916   static clang::QualType GetQualType(lldb::opaque_compiler_type_t type) {
917     if (type)
918       return clang::QualType::getFromOpaquePtr(type);
919     return clang::QualType();
920   }
921 
922   static clang::QualType
GetCanonicalQualType(lldb::opaque_compiler_type_t type)923   GetCanonicalQualType(lldb::opaque_compiler_type_t type) {
924     if (type)
925       return clang::QualType::getFromOpaquePtr(type).getCanonicalType();
926     return clang::QualType();
927   }
928 
929   clang::DeclarationName
930   GetDeclarationName(const char *name, const CompilerType &function_clang_type);
931 
932 private:
933   const clang::ClassTemplateSpecializationDecl *
934   GetAsTemplateSpecialization(lldb::opaque_compiler_type_t type);
935 
936   // Classes that inherit from ClangASTContext can see and modify these
937   std::string m_target_triple;
938   std::unique_ptr<clang::ASTContext> m_ast_up;
939   std::unique_ptr<clang::LangOptions> m_language_options_up;
940   std::unique_ptr<clang::FileManager> m_file_manager_up;
941   std::unique_ptr<clang::SourceManager> m_source_manager_up;
942   std::unique_ptr<clang::DiagnosticsEngine> m_diagnostics_engine_up;
943   std::unique_ptr<clang::DiagnosticConsumer> m_diagnostic_consumer_up;
944   std::shared_ptr<clang::TargetOptions> m_target_options_rp;
945   std::unique_ptr<clang::TargetInfo> m_target_info_up;
946   std::unique_ptr<clang::IdentifierTable> m_identifier_table_up;
947   std::unique_ptr<clang::SelectorTable> m_selector_table_up;
948   std::unique_ptr<clang::Builtin::Context> m_builtins_up;
949   std::unique_ptr<DWARFASTParserClang> m_dwarf_ast_parser_up;
950 #ifdef LLDB_ENABLE_ALL
951   std::unique_ptr<PDBASTParser> m_pdb_ast_parser_up;
952 #endif // LLDB_ENABLE_ALL
953   std::unique_ptr<clang::MangleContext> m_mangle_ctx_up;
954   uint32_t m_pointer_byte_size = 0;
955   bool m_ast_owned = false;
956 
957   typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap;
958   /// Maps Decls to their associated ClangASTMetadata.
959   DeclMetadataMap m_decl_metadata;
960 
961   typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap;
962   /// Maps Types to their associated ClangASTMetadata.
963   TypeMetadataMap m_type_metadata;
964 
965   /// The sema associated that is currently used to build this ASTContext.
966   /// May be null if we are already done parsing this ASTContext or the
967   /// ASTContext wasn't created by parsing source code.
968   clang::Sema *m_sema = nullptr;
969 
970   // For ClangASTContext only
971   ClangASTContext(const ClangASTContext &);
972   const ClangASTContext &operator=(const ClangASTContext &);
973   /// Creates the internal ASTContext.
974   void CreateASTContext();
975   void SetTargetTriple(llvm::StringRef target_triple);
976 };
977 
978 class ClangASTContextForExpressions : public ClangASTContext {
979 public:
980   ClangASTContextForExpressions(Target &target, llvm::Triple triple);
981 
982   ~ClangASTContextForExpressions() override = default;
983 
984   void Finalize() override;
985 
986   UserExpression *
987   GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
988                     lldb::LanguageType language,
989                     Expression::ResultType desired_type,
990                     const EvaluateExpressionOptions &options,
991                     ValueObject *ctx_obj) override;
992 
993   FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
994                                     const Address &function_address,
995                                     const ValueList &arg_value_list,
996                                     const char *name) override;
997 
998   UtilityFunction *GetUtilityFunction(const char *text,
999                                       const char *name) override;
1000 
1001   PersistentExpressionState *GetPersistentExpressionState() override;
1002 private:
1003   lldb::TargetWP m_target_wp;
1004   std::unique_ptr<PersistentExpressionState>
1005       m_persistent_variables; // These are the persistent variables associated
1006                               // with this process for the expression parser
1007   std::unique_ptr<ClangASTSource> m_scratch_ast_source_up;
1008 };
1009 
1010 } // namespace lldb_private
1011 
1012 #endif // liblldb_ClangASTContext_h_
1013