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