TypeSystem.h revision 327952
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
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 {
40public:
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 lldb::TemplateArgumentKind
355  GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, size_t idx);
356  virtual CompilerType GetTypeTemplateArgument(lldb::opaque_compiler_type_t type,
357                                           size_t idx);
358  virtual llvm::Optional<CompilerType::IntegralTemplateArgument>
359  GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, size_t idx);
360
361  //----------------------------------------------------------------------
362  // Dumping types
363  //----------------------------------------------------------------------
364
365  virtual void DumpValue(lldb::opaque_compiler_type_t type,
366                         ExecutionContext *exe_ctx, Stream *s,
367                         lldb::Format format, const DataExtractor &data,
368                         lldb::offset_t data_offset, size_t data_byte_size,
369                         uint32_t bitfield_bit_size,
370                         uint32_t bitfield_bit_offset, bool show_types,
371                         bool show_summary, bool verbose, uint32_t depth) = 0;
372
373  virtual bool DumpTypeValue(lldb::opaque_compiler_type_t type, Stream *s,
374                             lldb::Format format, const DataExtractor &data,
375                             lldb::offset_t data_offset, size_t data_byte_size,
376                             uint32_t bitfield_bit_size,
377                             uint32_t bitfield_bit_offset,
378                             ExecutionContextScope *exe_scope) = 0;
379
380  virtual void
381  DumpTypeDescription(lldb::opaque_compiler_type_t type) = 0; // Dump to stdout
382
383  virtual void DumpTypeDescription(lldb::opaque_compiler_type_t type,
384                                   Stream *s) = 0;
385
386  //----------------------------------------------------------------------
387  // TODO: These methods appear unused. Should they be removed?
388  //----------------------------------------------------------------------
389
390  virtual bool IsRuntimeGeneratedType(lldb::opaque_compiler_type_t type) = 0;
391
392  virtual void DumpSummary(lldb::opaque_compiler_type_t type,
393                           ExecutionContext *exe_ctx, Stream *s,
394                           const DataExtractor &data,
395                           lldb::offset_t data_offset,
396                           size_t data_byte_size) = 0;
397
398  // Converts "s" to a floating point value and place resulting floating
399  // point bytes in the "dst" buffer.
400  virtual size_t ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
401                                           const char *s, uint8_t *dst,
402                                           size_t dst_size) = 0;
403
404  //----------------------------------------------------------------------
405  // TODO: Determine if these methods should move to ClangASTContext.
406  //----------------------------------------------------------------------
407
408  virtual bool IsPointerOrReferenceType(lldb::opaque_compiler_type_t type,
409                                        CompilerType *pointee_type) = 0;
410
411  virtual unsigned GetTypeQualifiers(lldb::opaque_compiler_type_t type) = 0;
412
413  virtual bool IsCStringType(lldb::opaque_compiler_type_t type,
414                             uint32_t &length) = 0;
415
416  virtual size_t GetTypeBitAlign(lldb::opaque_compiler_type_t type) = 0;
417
418  virtual CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) = 0;
419
420  virtual CompilerType
421  GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding,
422                                      size_t bit_size) = 0;
423
424  virtual bool IsBeingDefined(lldb::opaque_compiler_type_t type) = 0;
425
426  virtual bool IsConst(lldb::opaque_compiler_type_t type) = 0;
427
428  virtual uint32_t IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
429                                          CompilerType *base_type_ptr) = 0;
430
431  virtual bool IsPolymorphicClass(lldb::opaque_compiler_type_t type) = 0;
432
433  virtual bool IsTypedefType(lldb::opaque_compiler_type_t type) = 0;
434
435  // If the current object represents a typedef type, get the underlying type
436  virtual CompilerType GetTypedefedType(lldb::opaque_compiler_type_t type) = 0;
437
438  virtual bool IsVectorType(lldb::opaque_compiler_type_t type,
439                            CompilerType *element_type, uint64_t *size) = 0;
440
441  virtual CompilerType
442  GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) = 0;
443
444  virtual CompilerType
445  GetNonReferenceType(lldb::opaque_compiler_type_t type) = 0;
446
447  virtual bool IsReferenceType(lldb::opaque_compiler_type_t type,
448                               CompilerType *pointee_type, bool *is_rvalue) = 0;
449
450  virtual bool
451  ShouldTreatScalarValueAsAddress(lldb::opaque_compiler_type_t type) {
452    return IsPointerOrReferenceType(type, nullptr);
453  }
454
455  virtual UserExpression *
456  GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
457                    lldb::LanguageType language,
458                    Expression::ResultType desired_type,
459                    const EvaluateExpressionOptions &options) {
460    return nullptr;
461  }
462
463  virtual FunctionCaller *GetFunctionCaller(const CompilerType &return_type,
464                                            const Address &function_address,
465                                            const ValueList &arg_value_list,
466                                            const char *name) {
467    return nullptr;
468  }
469
470  virtual UtilityFunction *GetUtilityFunction(const char *text,
471                                              const char *name) {
472    return nullptr;
473  }
474
475  virtual PersistentExpressionState *GetPersistentExpressionState() {
476    return nullptr;
477  }
478
479  virtual CompilerType GetTypeForFormatters(void *type);
480
481  virtual LazyBool ShouldPrintAsOneLiner(void *type, ValueObject *valobj);
482
483  // Type systems can have types that are placeholder types, which are meant to
484  // indicate
485  // the presence of a type, but offer no actual information about said types,
486  // and leave
487  // the burden of actually figuring type information out to dynamic type
488  // resolution. For instance
489  // a language with a generics system, can use placeholder types to indicate
490  // "type argument goes here",
491  // without promising uniqueness of the placeholder, nor attaching any actually
492  // idenfiable information
493  // to said placeholder. This API allows type systems to tell LLDB when such a
494  // type has been encountered
495  // In response, the debugger can react by not using this type as a cache entry
496  // in any type-specific way
497  // For instance, LLDB will currently not cache any formatters that are
498  // discovered on such a type as
499  // attributable to the meaningless type itself, instead preferring to use the
500  // dynamic type
501  virtual bool IsMeaninglessWithoutDynamicResolution(void *type);
502
503protected:
504  const LLVMCastKind m_kind; // Support for llvm casting
505  SymbolFile *m_sym_file;
506};
507
508class TypeSystemMap {
509public:
510  TypeSystemMap();
511  ~TypeSystemMap();
512
513  // Clear calls Finalize on all the TypeSystems managed by this map, and then
514  // empties the map.
515  void Clear();
516
517  // Iterate through all of the type systems that are created. Return true
518  // from callback to keep iterating, false to stop iterating.
519  void ForEach(std::function<bool(TypeSystem *)> const &callback);
520
521  TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
522                                       Module *module, bool can_create);
523
524  TypeSystem *GetTypeSystemForLanguage(lldb::LanguageType language,
525                                       Target *target, bool can_create);
526
527protected:
528  // This function does not take the map mutex, and should only be called from
529  // functions that do take the mutex.
530  void AddToMap(lldb::LanguageType language,
531                lldb::TypeSystemSP const &type_system_sp);
532
533  typedef std::map<lldb::LanguageType, lldb::TypeSystemSP> collection;
534  mutable std::mutex m_mutex; ///< A mutex to keep this object happy in
535                              ///multi-threaded environments.
536  collection m_map;
537  bool m_clear_in_progress;
538};
539
540} // namespace lldb_private
541
542#endif // liblldb_TypeSystem_h_
543