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