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