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