]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Symbol/TypeSystem.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / Symbol / TypeSystem.h
1 //===-- TypeSystem.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_TypeSystem_h_
11 #define liblldb_TypeSystem_h_
12
13 // C Includes
14 // C++ Includes
15 #include <functional>
16 #include <map>
17 #include <mutex>
18 #include <string>
19
20 // Other libraries and framework includes
21 #include "llvm/ADT/APSInt.h"
22 #include "llvm/Support/Casting.h"
23
24 // Project includes
25 #include "lldb/Core/PluginInterface.h"
26 #include "lldb/Expression/Expression.h"
27 #include "lldb/Symbol/CompilerDecl.h"
28 #include "lldb/Symbol/CompilerDeclContext.h"
29 #include "lldb/lldb-private.h"
30
31 class DWARFDIE;
32 class DWARFASTParser;
33
34 namespace lldb_private {
35
36 //----------------------------------------------------------------------
37 // Interface for representing the Type Systems in different languages.
38 //----------------------------------------------------------------------
39 class TypeSystem : public PluginInterface {
40 public:
41   //----------------------------------------------------------------------
42   // Intrusive type system that allows us to use llvm casting.
43   //
44   // To add a new type system:
45   //
46   // 1 - Add a new enumeration for llvm casting below for your TypeSystem
47   //     subclass, here we will use eKindFoo
48   //
49   // 2 - Your TypeSystem subclass will inherit from TypeSystem and needs
50   //     to implement a static classof() function that returns your
51   //     enumeration:
52   //
53   //    class Foo : public lldb_private::TypeSystem
54   //    {
55   //        static bool classof(const TypeSystem *ts)
56   //        {
57   //            return ts->getKind() == TypeSystem::eKindFoo;
58   //        }
59   //    };
60   //
61   // 3 - Contruct your TypeSystem subclass with the enumeration from below
62   //
63   //    Foo() :
64   //        TypeSystem(TypeSystem::eKindFoo),
65   //        ...
66   //    {
67   //    }
68   //
69   // Then you can use the llvm casting on any "TypeSystem *" to get an
70   // instance of your subclass.
71   //----------------------------------------------------------------------
72   enum LLVMCastKind {
73     eKindClang,
74     eKindSwift,
75     eKindGo,
76     eKindJava,
77     eKindOCaml,
78     kNumKinds
79   };
80
81   //----------------------------------------------------------------------
82   // Constructors and Destructors
83   //----------------------------------------------------------------------
84   TypeSystem(LLVMCastKind kind);
85
86   ~TypeSystem() override;
87
88   LLVMCastKind getKind() const { return m_kind; }
89
90   static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
91                                            Module *module);
92
93   static lldb::TypeSystemSP CreateInstance(lldb::LanguageType language,
94                                            Target *target);
95
96   // Free up any resources associated with this TypeSystem.  Done before
97   // removing
98   // all the TypeSystems from the TypeSystemMap.
99   virtual void Finalize() {}
100
101   virtual DWARFASTParser *GetDWARFParser() { return nullptr; }
102
103   virtual SymbolFile *GetSymbolFile() const { return m_sym_file; }
104
105   // Returns true if the symbol file changed during the set accessor.
106   virtual void SetSymbolFile(SymbolFile *sym_file) { m_sym_file = sym_file; }
107
108   //----------------------------------------------------------------------
109   // CompilerDecl functions
110   //----------------------------------------------------------------------
111   virtual ConstString DeclGetName(void *opaque_decl) = 0;
112
113   virtual ConstString DeclGetMangledName(void *opaque_decl);
114
115   virtual CompilerDeclContext DeclGetDeclContext(void *opaque_decl);
116
117   virtual CompilerType DeclGetFunctionReturnType(void *opaque_decl);
118
119   virtual size_t DeclGetFunctionNumArguments(void *opaque_decl);
120
121   virtual CompilerType DeclGetFunctionArgumentType(void *opaque_decl,
122                                                    size_t arg_idx);
123
124   //----------------------------------------------------------------------
125   // CompilerDeclContext functions
126   //----------------------------------------------------------------------
127
128   virtual std::vector<CompilerDecl>
129   DeclContextFindDeclByName(void *opaque_decl_ctx, ConstString name,
130                             const bool ignore_imported_decls);
131
132   virtual bool DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) = 0;
133
134   virtual ConstString DeclContextGetName(void *opaque_decl_ctx) = 0;
135
136   virtual ConstString
137   DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) = 0;
138
139   virtual bool DeclContextIsClassMethod(
140       void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
141       bool *is_instance_method_ptr, ConstString *language_object_name_ptr) = 0;
142
143   //----------------------------------------------------------------------
144   // Tests
145   //----------------------------------------------------------------------
146
147   virtual bool IsArrayType(lldb::opaque_compiler_type_t type,
148                            CompilerType *element_type, uint64_t *size,
149                            bool *is_incomplete) = 0;
150
151   virtual bool IsAggregateType(lldb::opaque_compiler_type_t type) = 0;
152
153   virtual bool IsAnonymousType(lldb::opaque_compiler_type_t type);
154
155   virtual bool IsCharType(lldb::opaque_compiler_type_t type) = 0;
156
157   virtual bool IsCompleteType(lldb::opaque_compiler_type_t type) = 0;
158
159   virtual bool IsDefined(lldb::opaque_compiler_type_t type) = 0;
160
161   virtual bool IsFloatingPointType(lldb::opaque_compiler_type_t type,
162                                    uint32_t &count, bool &is_complex) = 0;
163
164   virtual bool IsFunctionType(lldb::opaque_compiler_type_t type,
165                               bool *is_variadic_ptr) = 0;
166
167   virtual size_t
168   GetNumberOfFunctionArguments(lldb::opaque_compiler_type_t type) = 0;
169
170   virtual CompilerType
171   GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
172                              const size_t index) = 0;
173
174   virtual bool IsFunctionPointerType(lldb::opaque_compiler_type_t type) = 0;
175
176   virtual bool IsBlockPointerType(lldb::opaque_compiler_type_t type,
177                                   CompilerType *function_pointer_type_ptr) = 0;
178
179   virtual bool IsIntegerType(lldb::opaque_compiler_type_t type,
180                              bool &is_signed) = 0;
181
182   virtual bool IsEnumerationType(lldb::opaque_compiler_type_t type,
183                                  bool &is_signed) {
184     is_signed = false;
185     return false;
186   }
187
188   virtual bool IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
189                                      CompilerType *target_type, // Can pass NULL
190                                      bool check_cplusplus, bool check_objc) = 0;
191
192   virtual bool IsPointerType(lldb::opaque_compiler_type_t type,
193                              CompilerType *pointee_type) = 0;
194
195   virtual bool IsScalarType(lldb::opaque_compiler_type_t type) = 0;
196
197   virtual bool IsVoidType(lldb::opaque_compiler_type_t type) = 0;
198
199   // TypeSystems can support more than one language
200   virtual bool SupportsLanguage(lldb::LanguageType language) = 0;
201
202   //----------------------------------------------------------------------
203   // Type Completion
204   //----------------------------------------------------------------------
205
206   virtual bool GetCompleteType(lldb::opaque_compiler_type_t type) = 0;
207
208   //----------------------------------------------------------------------
209   // AST related queries
210   //----------------------------------------------------------------------
211
212   virtual uint32_t GetPointerByteSize() = 0;
213
214   //----------------------------------------------------------------------
215   // Accessors
216   //----------------------------------------------------------------------
217
218   virtual ConstString GetTypeName(lldb::opaque_compiler_type_t type) = 0;
219
220   virtual uint32_t
221   GetTypeInfo(lldb::opaque_compiler_type_t type,
222               CompilerType *pointee_or_element_compiler_type) = 0;
223
224   virtual lldb::LanguageType
225   GetMinimumLanguage(lldb::opaque_compiler_type_t type) = 0;
226
227   virtual lldb::TypeClass GetTypeClass(lldb::opaque_compiler_type_t type) = 0;
228
229   //----------------------------------------------------------------------
230   // Creating related types
231   //----------------------------------------------------------------------
232
233   virtual CompilerType GetArrayElementType(lldb::opaque_compiler_type_t type,
234                                            uint64_t *stride) = 0;
235
236   virtual CompilerType GetArrayType(lldb::opaque_compiler_type_t type,
237                                     uint64_t size);
238
239   virtual CompilerType GetCanonicalType(lldb::opaque_compiler_type_t type) = 0;
240
241   // Returns -1 if this isn't a function of if the function doesn't have a
242   // prototype
243   // Returns a value >= 0 if there is a prototype.
244   virtual int GetFunctionArgumentCount(lldb::opaque_compiler_type_t type) = 0;
245
246   virtual CompilerType
247   GetFunctionArgumentTypeAtIndex(lldb::opaque_compiler_type_t type,
248                                  size_t idx) = 0;
249
250   virtual CompilerType
251   GetFunctionReturnType(lldb::opaque_compiler_type_t type) = 0;
252
253   virtual size_t GetNumMemberFunctions(lldb::opaque_compiler_type_t type) = 0;
254
255   virtual TypeMemberFunctionImpl
256   GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, size_t idx) = 0;
257
258   virtual CompilerType GetPointeeType(lldb::opaque_compiler_type_t type) = 0;
259
260   virtual CompilerType GetPointerType(lldb::opaque_compiler_type_t type) = 0;
261
262   virtual CompilerType
263   GetLValueReferenceType(lldb::opaque_compiler_type_t type);
264
265   virtual CompilerType
266   GetRValueReferenceType(lldb::opaque_compiler_type_t type);
267
268   virtual CompilerType AddConstModifier(lldb::opaque_compiler_type_t type);
269
270   virtual CompilerType AddVolatileModifier(lldb::opaque_compiler_type_t type);
271
272   virtual CompilerType AddRestrictModifier(lldb::opaque_compiler_type_t type);
273
274   virtual CompilerType CreateTypedef(lldb::opaque_compiler_type_t type,
275                                      const char *name,
276                                      const CompilerDeclContext &decl_ctx);
277
278   //----------------------------------------------------------------------
279   // Exploring the type
280   //----------------------------------------------------------------------
281
282   virtual uint64_t GetBitSize(lldb::opaque_compiler_type_t type,
283                               ExecutionContextScope *exe_scope) = 0;
284
285   virtual lldb::Encoding GetEncoding(lldb::opaque_compiler_type_t type,
286                                      uint64_t &count) = 0;
287
288   virtual lldb::Format GetFormat(lldb::opaque_compiler_type_t type) = 0;
289
290   virtual uint32_t GetNumChildren(lldb::opaque_compiler_type_t type,
291                                   bool omit_empty_base_classes) = 0;
292
293   virtual CompilerType GetBuiltinTypeByName(const ConstString &name);
294
295   virtual lldb::BasicType
296   GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) = 0;
297
298   virtual void ForEachEnumerator(
299       lldb::opaque_compiler_type_t type,
300       std::function<bool(const CompilerType &integer_type,
301                          const ConstString &name,
302                          const llvm::APSInt &value)> const &callback) {}
303
304   virtual uint32_t GetNumFields(lldb::opaque_compiler_type_t type) = 0;
305
306   virtual CompilerType GetFieldAtIndex(lldb::opaque_compiler_type_t type,
307                                        size_t idx, std::string &name,
308                                        uint64_t *bit_offset_ptr,
309                                        uint32_t *bitfield_bit_size_ptr,
310                                        bool *is_bitfield_ptr) = 0;
311
312   virtual uint32_t
313   GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) = 0;
314
315   virtual uint32_t
316   GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) = 0;
317
318   virtual CompilerType
319   GetDirectBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
320                             uint32_t *bit_offset_ptr) = 0;
321
322   virtual CompilerType
323   GetVirtualBaseClassAtIndex(lldb::opaque_compiler_type_t type, size_t idx,
324                              uint32_t *bit_offset_ptr) = 0;
325
326   virtual CompilerType GetChildCompilerTypeAtIndex(
327       lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
328       bool transparent_pointers, bool omit_empty_base_classes,
329       bool ignore_array_bounds, std::string &child_name,
330       uint32_t &child_byte_size, int32_t &child_byte_offset,
331       uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
332       bool &child_is_base_class, bool &child_is_deref_of_parent,
333       ValueObject *valobj, uint64_t &language_flags) = 0;
334
335   // Lookup a child given a name. This function will match base class names
336   // and member member names in "clang_type" only, not descendants.
337   virtual uint32_t GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
338                                            const char *name,
339                                            bool omit_empty_base_classes) = 0;
340
341   // Lookup a child member given a name. This function will match member names
342   // only and will descend into "clang_type" children in search for the first
343   // member in this class, or any base class that matches "name".
344   // TODO: Return all matches for a given name by returning a
345   // vector<vector<uint32_t>>
346   // so we catch all names that match a given child name, not just the first.
347   virtual size_t
348   GetIndexOfChildMemberWithName(lldb::opaque_compiler_type_t type,
349                                 const char *name, bool omit_empty_base_classes,
350                                 std::vector<uint32_t> &child_indexes) = 0;
351
352   virtual size_t GetNumTemplateArguments(lldb::opaque_compiler_type_t type) = 0;
353
354   virtual CompilerType
355   GetTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx,
356                       lldb::TemplateArgumentKind &kind) = 0;
357
358   //----------------------------------------------------------------------
359   // Dumping types
360   //----------------------------------------------------------------------
361
362   virtual void DumpValue(lldb::opaque_compiler_type_t type,
363                          ExecutionContext *exe_ctx, Stream *s,
364                          lldb::Format format, const DataExtractor &data,
365                          lldb::offset_t data_offset, size_t data_byte_size,
366                          uint32_t bitfield_bit_size,
367                          uint32_t bitfield_bit_offset, bool show_types,
368                          bool show_summary, bool verbose, uint32_t depth) = 0;
369
370   virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
371                              lldb::Format format, const DataExtractor &data,
372                              lldb::offset_t data_offset, size_t data_byte_size,
373                              uint32_t bitfield_bit_size,
374                              uint32_t bitfield_bit_offset,
375                              ExecutionContextScope *exe_scope) = 0;
376
377   virtual void
378   DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
379
380   virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
381                                    Stream *s) = 0;
382
383   //----------------------------------------------------------------------
384   // TODO: These methods appear unused. Should they be removed?
385   //----------------------------------------------------------------------
386
387   virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
388
389   virtual void DumpSummary(lldb::opaque_compiler_type_t type,
390                            ExecutionContext *exe_ctx, Stream *s,
391                            const DataExtractor &data,
392                            lldb::offset_t data_offset,
393                            size_t data_byte_size) = 0;
394
395   // Converts "s" to a floating point value and place resulting floating
396   // point bytes in the "dst" buffer.
397   virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
398                                            const char *s, uint8_t *dst,
399                                            size_t dst_size) = 0;
400
401   //----------------------------------------------------------------------
402   // TODO: Determine if these methods should move to ClangASTContext.
403   //----------------------------------------------------------------------
404
405   virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
406                                         CompilerType *pointee_type) = 0;
407
408   virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
409
410   virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
411                              uint32_t &length) = 0;
412
413   virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
414
415   virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
416
417   virtual CompilerType
418   GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
419                                       size_t bit_size) = 0;
420
421   virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
422
423   virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
424
425   virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
426                                           CompilerType *base_type_ptr) = 0;
427
428   virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
429
430   virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
431
432   // If the current object represents a typedef type, get the underlying type
433   virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
434
435   virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
436                             CompilerType *element_type, uint64_t *size) = 0;
437
438   virtual CompilerType
439   GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
440
441   virtual CompilerType
442   GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
443
444   virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
445                                CompilerType *pointee_type, bool *is_rvalue) = 0;
446
447   virtual bool
448   ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
449     return IsPointerOrReferenceType(type, nullptr);
450   }
451
452   virtual UserExpression *
453   GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
454                     lldb::LanguageType language,
455                     Expression::ResultType desired_type,
456                     const EvaluateExpressionOptions &options) {
457     return nullptr;
458   }
459
460   virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
461                                             const Address &function_address,
462                                             const ValueList &arg_value_list,
463                                             const char *name) {
464     return nullptr;
465   }
466
467   virtual UtilityFunction *GetUtilityFunction(const char *text,
468                                               const char *name) {
469     return nullptr;
470   }
471
472   virtual PersistentExpressionState *GetPersistentExpressionState() {
473     return nullptr;
474   }
475
476   virtual CompilerType GetTypeForFormatters(void *type);
477
478   virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
479
480   // Type systems can have types that are placeholder types, which are meant to
481   // indicate
482   // the presence of a type, but offer no actual information about said types,
483   // and leave
484   // the burden of actually figuring type information out to dynamic type
485   // resolution. For instance
486   // a language with a generics system, can use placeholder types to indicate
487   // "type argument goes here",
488   // without promising uniqueness of the placeholder, nor attaching any actually
489   // idenfiable information
490   // to said placeholder. This API allows type systems to tell LLDB when such a
491   // type has been encountered
492   // In response, the debugger can react by not using this type as a cache entry
493   // in any type-specific way
494   // For instance, LLDB will currently not cache any formatters that are
495   // discovered on such a type as
496   // attributable to the meaningless type itself, instead preferring to use the
497   // dynamic type
498   virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
499
500 protected:
501   const LLVMCastKind m_kind; // Support for llvm casting
502   SymbolFile *m_sym_file;
503 };
504
505 class TypeSystemMap {
506 public:
507   TypeSystemMap();
508   ~TypeSystemMap();
509
510   // Clear calls Finalize on all the TypeSystems managed by this map, and then
511   // empties the map.
512   void Clear();
513
514   // Iterate through all of the type systems that are created. Return true
515   // from callback to keep iterating, false to stop iterating.
516   void ForEach(std::function<bool(TypeSystem *)> const &callback);
517
518   TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
519                                        Module *module, bool can_create);
520
521   TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
522                                        Target *target, bool can_create);
523
524 protected:
525   // This function does not take the map mutex, and should only be called from
526   // functions that do take the mutex.
527   void AddToMap(lldb::LanguageType language,
528                 lldb::TypeSystemSP const &type_system_sp);
529
530   typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
531   mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
532                               ///multi-threaded environments.
533   collection m_map;
534   bool m_clear_in_progress;
535 };
536
537 } // namespace lldb_private
538
539 #endif // liblldb_TypeSystem_h_