LLDB  mainline
Type.h
Go to the documentation of this file.
1 //===-- Type.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_Type_h_
10 #define liblldb_Type_h_
11 
12 #include "lldb/Core/ClangForward.h"
17 #include "lldb/Utility/UserID.h"
18 #include "lldb/lldb-private.h"
19 
20 #include "llvm/ADT/APSInt.h"
21 
22 #include <set>
23 
24 namespace lldb_private {
25 // CompilerContext allows an array of these items to be passed to perform
26 // detailed lookups in SymbolVendor and SymbolFile functions.
29  : type(t), name(n) {}
30 
31  bool operator==(const CompilerContext &rhs) const {
32  return type == rhs.type && name == rhs.name;
33  }
34 
35  void Dump() const;
36 
39 };
40 
41 class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>,
42  public UserID {
43 public:
45  : UserID(uid), m_symbol_file(symbol_file) {}
46 
47  SymbolFileType(SymbolFile &symbol_file, const lldb::TypeSP &type_sp);
48 
50 
51  Type *operator->() { return GetType(); }
52 
53  Type *GetType();
54 
55 protected:
57  lldb::TypeSP m_type_sp;
58 };
59 
60 class Type : public std::enable_shared_from_this<Type>, public UserID {
61 public:
62  typedef enum EncodingDataTypeTag {
64  eEncodingIsUID, ///< This type is the type whose UID is m_encoding_uid
65  eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid
66  ///with the const qualifier added
67  eEncodingIsRestrictUID, ///< This type is the type whose UID is
68  ///m_encoding_uid with the restrict qualifier added
69  eEncodingIsVolatileUID, ///< This type is the type whose UID is
70  ///m_encoding_uid with the volatile qualifier added
71  eEncodingIsTypedefUID, ///< This type is pointer to a type whose UID is
72  ///m_encoding_uid
73  eEncodingIsPointerUID, ///< This type is pointer to a type whose UID is
74  ///m_encoding_uid
75  eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type
76  ///whose UID is m_encoding_uid
77  eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type
78  ///whose UID is m_encoding_uid
79  eEncodingIsSyntheticUID
81 
82  // We must force the underlying type of the enum to be unsigned here. Not
83  // all compilers behave the same with regards to the default underlying type
84  // of an enum, but because this enum is used in an enum bitfield and integer
85  // comparisons are done with the value we need to guarantee that it's always
86  // unsigned so that, for example, eResolveStateFull doesn't compare less than
87  // eResolveStateUnresolved when used in a 2-bit bitfield.
88  typedef enum ResolveStateTag : unsigned {
89  eResolveStateUnresolved = 0,
90  eResolveStateForward = 1,
91  eResolveStateLayout = 2,
92  eResolveStateFull = 3
93  } ResolveState;
94 
95  Type(lldb::user_id_t uid, SymbolFile *symbol_file, ConstString name,
96  llvm::Optional<uint64_t> byte_size, SymbolContextScope *context,
97  lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
98  const Declaration &decl, const CompilerType &compiler_qual_type,
99  ResolveState compiler_type_resolve_state);
100 
101  // This makes an invalid type. Used for functions that return a Type when
102  // they get an error.
103  Type();
104 
105  Type(const Type &rhs);
106 
107  const Type &operator=(const Type &rhs);
108 
109  void Dump(Stream *s, bool show_context);
110 
111  void DumpTypeName(Stream *s);
112 
113  // Since Type instances only keep a "SymbolFile *" internally, other classes
114  // like TypeImpl need make sure the module is still around before playing
115  // with
116  // Type instances. They can store a weak pointer to the Module;
117  lldb::ModuleSP GetModule();
118 
119  void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name);
120 
121  SymbolFile *GetSymbolFile() { return m_symbol_file; }
122  const SymbolFile *GetSymbolFile() const { return m_symbol_file; }
123 
124  TypeList *GetTypeList();
125 
126  ConstString GetName();
127 
128  llvm::Optional<uint64_t> GetByteSize();
129 
130  uint32_t GetNumChildren(bool omit_empty_base_classes);
131 
132  bool IsAggregateType();
133 
134  bool IsValidType() { return m_encoding_uid_type != eEncodingInvalid; }
135 
136  bool IsTypedef() { return m_encoding_uid_type == eEncodingIsTypedefUID; }
137 
138  lldb::TypeSP GetTypedefType();
139 
140  ConstString GetName() const { return m_name; }
141 
142  ConstString GetQualifiedName();
143 
144  void DumpValue(ExecutionContext *exe_ctx, Stream *s,
145  const DataExtractor &data, uint32_t data_offset,
146  bool show_type, bool show_summary, bool verbose,
148 
149  bool DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s,
150  lldb::addr_t address, AddressType address_type,
151  bool show_types, bool show_summary, bool verbose);
152 
153  bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
154  AddressType address_type, DataExtractor &data);
155 
156  bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
157  AddressType address_type, DataExtractor &data);
158 
159  bool GetIsDeclaration() const;
160 
161  void SetIsDeclaration(bool b);
162 
163  bool GetIsExternal() const;
164 
165  void SetIsExternal(bool b);
166 
167  lldb::Format GetFormat();
168 
169  lldb::Encoding GetEncoding(uint64_t &count);
170 
171  SymbolContextScope *GetSymbolContextScope() { return m_context; }
172  const SymbolContextScope *GetSymbolContextScope() const { return m_context; }
174  m_context = context;
175  }
176 
177  const lldb_private::Declaration &GetDeclaration() const;
178 
179  // Get the clang type, and resolve definitions for any
180  // class/struct/union/enum types completely.
181  CompilerType GetFullCompilerType();
182 
183  // Get the clang type, and resolve definitions enough so that the type could
184  // have layout performed. This allows ptrs and refs to
185  // class/struct/union/enum types remain forward declarations.
186  CompilerType GetLayoutCompilerType();
187 
188  // Get the clang type and leave class/struct/union/enum types as forward
189  // declarations if they haven't already been fully defined.
190  CompilerType GetForwardCompilerType();
191 
192  static int Compare(const Type &a, const Type &b);
193 
194  // From a fully qualified typename, split the type into the type basename and
195  // the remaining type scope (namespaces/classes).
196  static bool GetTypeScopeAndBasename(const llvm::StringRef& name,
197  llvm::StringRef &scope,
198  llvm::StringRef &basename,
199  lldb::TypeClass &type_class);
200  void SetEncodingType(Type *encoding_type) { m_encoding_type = encoding_type; }
201 
202  uint32_t GetEncodingMask();
203 
204  bool IsCompleteObjCClass() { return m_flags.is_complete_objc_class; }
205 
206  void SetIsCompleteObjCClass(bool is_complete_objc_class) {
207  m_flags.is_complete_objc_class = is_complete_objc_class;
208  }
209 
210 protected:
214  *m_context; // The symbol context in which this type is defined
217  EncodingDataType m_encoding_uid_type;
218  uint64_t m_byte_size : 63;
219  uint64_t m_byte_size_has_value : 1;
222 
223  struct Flags {
224 #ifdef __GNUC__
225  // using unsigned type here to work around a very noisy gcc warning
226  unsigned compiler_type_resolve_state : 2;
227 #else
229 #endif
231  } m_flags;
232 
233  Type *GetEncodingType();
234 
235  bool ResolveClangType(ResolveState compiler_type_resolve_state);
236 };
237 
238 // the two classes here are used by the public API as a backend to the SBType
239 // and SBTypeList classes
240 
241 class TypeImpl {
242 public:
243  TypeImpl();
244 
246 
247  TypeImpl(const TypeImpl &rhs);
248 
249  TypeImpl(const lldb::TypeSP &type_sp);
250 
251  TypeImpl(const CompilerType &compiler_type);
252 
253  TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
254 
255  TypeImpl(const CompilerType &compiler_type, const CompilerType &dynamic);
256 
257  void SetType(const lldb::TypeSP &type_sp);
258 
259  void SetType(const CompilerType &compiler_type);
260 
261  void SetType(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
262 
263  void SetType(const CompilerType &compiler_type, const CompilerType &dynamic);
264 
265  TypeImpl &operator=(const TypeImpl &rhs);
266 
267  bool operator==(const TypeImpl &rhs) const;
268 
269  bool operator!=(const TypeImpl &rhs) const;
270 
271  bool IsValid() const;
272 
273  explicit operator bool() const;
274 
275  void Clear();
276 
277  ConstString GetName() const;
278 
279  ConstString GetDisplayTypeName() const;
280 
281  TypeImpl GetPointerType() const;
282 
283  TypeImpl GetPointeeType() const;
284 
285  TypeImpl GetReferenceType() const;
286 
287  TypeImpl GetTypedefedType() const;
288 
289  TypeImpl GetDereferencedType() const;
290 
291  TypeImpl GetUnqualifiedType() const;
292 
293  TypeImpl GetCanonicalType() const;
294 
295  CompilerType GetCompilerType(bool prefer_dynamic);
296 
297  TypeSystem *GetTypeSystem(bool prefer_dynamic);
298 
299  bool GetDescription(lldb_private::Stream &strm,
300  lldb::DescriptionLevel description_level);
301 
302 private:
303  bool CheckModule(lldb::ModuleSP &module_sp) const;
304 
305  lldb::ModuleWP m_module_wp;
306  CompilerType m_static_type;
307  CompilerType m_dynamic_type;
308 };
309 
311 public:
312  TypeListImpl() : m_content() {}
313 
314  void Append(const lldb::TypeImplSP &type) { m_content.push_back(type); }
315 
317  public:
318  AppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {}
319 
320  void operator()(const lldb::TypeImplSP &type) { m_type_list.Append(type); }
321 
322  private:
323  TypeListImpl &m_type_list;
324  };
325 
326  void Append(const lldb_private::TypeList &type_list);
327 
328  lldb::TypeImplSP GetTypeAtIndex(size_t idx) {
329  lldb::TypeImplSP type_sp;
330  if (idx < GetSize())
331  type_sp = m_content[idx];
332  return type_sp;
333  }
334 
335  size_t GetSize() { return m_content.size(); }
336 
337 private:
338  std::vector<lldb::TypeImplSP> m_content;
339 };
340 
342 public:
344  : m_type_impl_sp(), m_bit_offset(0), m_name(), m_bitfield_bit_size(0),
345  m_is_bitfield(false)
346 
347  {}
348 
349  TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset,
350  ConstString name, uint32_t bitfield_bit_size = 0,
351  bool is_bitfield = false)
352  : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(name),
353  m_bitfield_bit_size(bitfield_bit_size), m_is_bitfield(is_bitfield) {}
354 
355  TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset)
356  : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(),
357  m_bitfield_bit_size(0), m_is_bitfield(false) {
358  if (m_type_impl_sp)
359  m_name = m_type_impl_sp->GetName();
360  }
361 
362  const lldb::TypeImplSP &GetTypeImpl() { return m_type_impl_sp; }
363 
364  ConstString GetName() const { return m_name; }
365 
366  uint64_t GetBitOffset() const { return m_bit_offset; }
367 
368  uint32_t GetBitfieldBitSize() const { return m_bitfield_bit_size; }
369 
370  void SetBitfieldBitSize(uint32_t bitfield_bit_size) {
371  m_bitfield_bit_size = bitfield_bit_size;
372  }
373 
374  bool GetIsBitfield() const { return m_is_bitfield; }
375 
376  void SetIsBitfield(bool is_bitfield) { m_is_bitfield = is_bitfield; }
377 
378 protected:
379  lldb::TypeImplSP m_type_impl_sp;
380  uint64_t m_bit_offset;
382  uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
384 };
385 
386 ///
387 /// Sometimes you can find the name of the type corresponding to an object, but
388 /// we don't have debug
389 /// information for it. If that is the case, you can return one of these
390 /// objects, and then if it
391 /// has a full type, you can use that, but if not at least you can print the
392 /// name for informational
393 /// purposes.
394 ///
395 
397 public:
398  TypeAndOrName() = default;
399  TypeAndOrName(lldb::TypeSP &type_sp);
400  TypeAndOrName(const CompilerType &compiler_type);
401  TypeAndOrName(const char *type_str);
402  TypeAndOrName(ConstString &type_const_string);
403 
404  bool operator==(const TypeAndOrName &other) const;
405 
406  bool operator!=(const TypeAndOrName &other) const;
407 
408  ConstString GetName() const;
409 
410  CompilerType GetCompilerType() const { return m_compiler_type; }
411 
412  void SetName(ConstString type_name);
413 
414  void SetName(const char *type_name_cstr);
415 
416  void SetTypeSP(lldb::TypeSP type_sp);
417 
418  void SetCompilerType(CompilerType compiler_type);
419 
420  bool IsEmpty() const;
421 
422  bool HasName() const;
423 
424  bool HasCompilerType() const;
425 
426  bool HasType() const { return HasCompilerType(); }
427 
428  void Clear();
429 
430  explicit operator bool() { return !IsEmpty(); }
431 
432 private:
433  CompilerType m_compiler_type;
434  ConstString m_type_name;
435 };
436 
438 public:
440  : m_type(), m_decl(), m_name(), m_kind(lldb::eMemberFunctionKindUnknown) {
441  }
442 
444  const std::string &name,
445  const lldb::MemberFunctionKind &kind)
446  : m_type(type), m_decl(decl), m_name(name), m_kind(kind) {}
447 
448  bool IsValid();
449 
450  ConstString GetName() const;
451 
452  ConstString GetMangledName() const;
453 
454  CompilerType GetType() const;
455 
456  CompilerType GetReturnType() const;
457 
458  size_t GetNumArguments() const;
459 
460  CompilerType GetArgumentAtIndex(size_t idx) const;
461 
462  lldb::MemberFunctionKind GetKind() const;
463 
464  bool GetDescription(Stream &stream);
465 
466 protected:
467  std::string GetPrintableTypeName();
468 
469 private:
470  CompilerType m_type;
471  CompilerDecl m_decl;
472  ConstString m_name;
474 };
475 
477 public:
479  : m_integer_type_sp(), m_name("<invalid>"), m_value(), m_valid(false) {}
480 
481  TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
482  ConstString name, const llvm::APSInt &value);
483 
485  : m_integer_type_sp(rhs.m_integer_type_sp), m_name(rhs.m_name),
486  m_value(rhs.m_value), m_valid(rhs.m_valid) {}
487 
488  TypeEnumMemberImpl &operator=(const TypeEnumMemberImpl &rhs);
489 
490  bool IsValid() { return m_valid; }
491 
492  ConstString GetName() const { return m_name; }
493 
494  const lldb::TypeImplSP &GetIntegerType() const { return m_integer_type_sp; }
495 
496  uint64_t GetValueAsUnsigned() const { return m_value.getZExtValue(); }
497 
498  int64_t GetValueAsSigned() const { return m_value.getSExtValue(); }
499 
500 protected:
501  lldb::TypeImplSP m_integer_type_sp;
503  llvm::APSInt m_value;
504  bool m_valid;
505 };
506 
508 public:
509  TypeEnumMemberListImpl() : m_content() {}
510 
511  void Append(const lldb::TypeEnumMemberImplSP &type) {
512  m_content.push_back(type);
513  }
514 
515  void Append(const lldb_private::TypeEnumMemberListImpl &type_list);
516 
517  lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx) {
518  lldb::TypeEnumMemberImplSP enum_member;
519  if (idx < GetSize())
520  enum_member = m_content[idx];
521  return enum_member;
522  }
523 
524  size_t GetSize() { return m_content.size(); }
525 
526 private:
527  std::vector<lldb::TypeEnumMemberImplSP> m_content;
528 };
529 
530 } // namespace lldb_private
531 
532 #endif // liblldb_Type_h_
void SetIsBitfield(bool is_bitfield)
Definition: Type.h:376
lldb::user_id_t m_encoding_uid
Definition: Type.h:216
Declaration m_decl
Definition: Type.h:220
An data extractor class.
Definition: DataExtractor.h:47
CompilerContextKind type
Definition: Type.h:37
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Sometimes you can find the name of the type corresponding to an object, but we don&#39;t have debug infor...
Definition: Type.h:396
CompilerType GetCompilerType() const
Definition: Type.h:410
Type * m_encoding_type
Definition: Type.h:215
SymbolFileType(SymbolFile &symbol_file, lldb::user_id_t uid)
Definition: Type.h:44
TypeMemberFunctionImpl(const CompilerType &type, const CompilerDecl &decl, const std::string &name, const lldb::MemberFunctionKind &kind)
Definition: Type.h:443
void Append(const lldb::TypeEnumMemberImplSP &type)
Definition: Type.h:511
TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset)
Definition: Type.h:355
ConstString GetName() const
Definition: Type.h:492
A mix in class that contains a generic user ID.
Definition: UserID.h:33
lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx)
Definition: Type.h:517
uint64_t GetValueAsUnsigned() const
Definition: Type.h:496
SymbolFile * m_symbol_file
Definition: Type.h:212
TypeEnumMemberImpl(const TypeEnumMemberImpl &rhs)
Definition: Type.h:484
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
uint32_t m_bitfield_bit_size
Definition: Type.h:382
SymbolFile & m_symbol_file
Definition: Type.h:56
const lldb::TypeImplSP & GetIntegerType() const
Definition: Type.h:494
EncodingDataType m_encoding_uid_type
Definition: Type.h:217
ConstString GetName() const
Definition: Type.h:140
uint64_t m_byte_size
Definition: Type.h:218
bool IsCompleteObjCClass()
Definition: Type.h:204
TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset, ConstString name, uint32_t bitfield_bit_size=0, bool is_bitfield=false)
Definition: Type.h:349
ConstString m_name
Definition: Type.h:211
const lldb::TypeImplSP & GetTypeImpl()
Definition: Type.h:362
CompilerType m_compiler_type
Definition: Type.h:221
uint64_t user_id_t
Definition: lldb-types.h:84
const SymbolFile * GetSymbolFile() const
Definition: Type.h:122
uint32_t GetBitfieldBitSize() const
Definition: Type.h:368
enum lldb_private::Type::ResolveStateTag ResolveState
bool IsTypedef()
Definition: Type.h:136
lldb::TypeImplSP m_type_impl_sp
Definition: Type.h:379
const SymbolContextScope * GetSymbolContextScope() const
Definition: Type.h:172
lldb::TypeImplSP GetTypeAtIndex(size_t idx)
Definition: Type.h:328
void SetEncodingType(Type *encoding_type)
Definition: Type.h:200
SymbolFile * GetSymbolFile()
Definition: Type.h:121
void SetIsCompleteObjCClass(bool is_complete_objc_class)
Definition: Type.h:206
bool IsValidType()
Definition: Type.h:134
ConstString GetName() const
Definition: Type.h:364
This type is the type whose UID is m_encoding_uid.
Definition: Type.h:64
bool operator==(const CompilerContext &rhs) const
Definition: Type.h:31
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:979
void SetSymbolContextScope(SymbolContextScope *context)
Definition: Type.h:173
uint64_t GetBitOffset() const
Definition: Type.h:366
lldb::TypeSP m_type_sp
Definition: Type.h:57
AppendVisitor(TypeListImpl &type_list)
Definition: Type.h:318
uint64_t addr_t
Definition: lldb-types.h:83
A uniqued constant string class.
Definition: ConstString.h:38
void SetBitfieldBitSize(uint32_t bitfield_bit_size)
Definition: Type.h:370
char * basename(char *path)
static bool Compare(BreakpointLocationSP lhs, lldb::break_id_t val)
Definition: SBAddress.h:15
uint64_t m_byte_size_has_value
Definition: Type.h:219
bool GetIsBitfield() const
Definition: Type.h:374
void operator()(const lldb::TypeImplSP &type)
Definition: Type.h:320
lldb::TypeImplSP m_integer_type_sp
Definition: Type.h:501
enum lldb_private::Type::EncodingDataTypeTag EncodingDataType
ResolveState compiler_type_resolve_state
Definition: Type.h:228
SymbolContextScope * GetSymbolContextScope()
Definition: Type.h:171
bool HasType() const
Definition: Type.h:426
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
void Append(const lldb::TypeImplSP &type)
Definition: Type.h:314
SymbolContextScope * m_context
Definition: Type.h:214
CompilerContext(CompilerContextKind t, ConstString n)
Definition: Type.h:28
static bool DumpValue(Stream &s, const SymbolContext *sc, const ExecutionContext *exe_ctx, const FormatEntity::Entry &entry, ValueObject *valobj)
int64_t GetValueAsSigned() const
Definition: Type.h:498