1 //===-- Type.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Type_h_
11 #define liblldb_Type_h_
13 #include "lldb/Core/ClangForward.h"
14 #include "lldb/Core/ConstString.h"
15 #include "lldb/Core/UserID.h"
16 #include "lldb/Symbol/CompilerDecl.h"
17 #include "lldb/Symbol/CompilerType.h"
18 #include "lldb/Symbol/Declaration.h"
19 #include "lldb/lldb-private.h"
21 #include "llvm/ADT/APSInt.h"
25 namespace lldb_private {
27 //----------------------------------------------------------------------
28 // CompilerContext allows an array of these items to be passed to
29 // perform detailed lookups in SymbolVendor and SymbolFile functions.
30 //----------------------------------------------------------------------
31 struct CompilerContext {
32 CompilerContext(CompilerContextKind t, const ConstString &n)
35 bool operator==(const CompilerContext &rhs) const {
36 return type == rhs.type && name == rhs.name;
41 CompilerContextKind type;
45 class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>,
48 SymbolFileType(SymbolFile &symbol_file, lldb::user_id_t uid)
49 : UserID(uid), m_symbol_file(symbol_file) {}
51 SymbolFileType(SymbolFile &symbol_file, const lldb::TypeSP &type_sp);
55 Type *operator->() { return GetType(); }
60 SymbolFile &m_symbol_file;
61 lldb::TypeSP m_type_sp;
64 class Type : public std::enable_shared_from_this<Type>, public UserID {
66 typedef enum EncodingDataTypeTag {
68 eEncodingIsUID, ///< This type is the type whose UID is m_encoding_uid
69 eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid
70 ///with the const qualifier added
71 eEncodingIsRestrictUID, ///< This type is the type whose UID is
72 ///m_encoding_uid with the restrict qualifier added
73 eEncodingIsVolatileUID, ///< This type is the type whose UID is
74 ///m_encoding_uid with the volatile qualifier added
75 eEncodingIsTypedefUID, ///< This type is pointer to a type whose UID is
77 eEncodingIsPointerUID, ///< This type is pointer to a type whose UID is
79 eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type
80 ///whose UID is m_encoding_uid
81 eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type
82 ///whose UID is m_encoding_uid
83 eEncodingIsSyntheticUID
86 // We must force the underlying type of the enum to be unsigned here. Not all
88 // behave the same with regards to the default underlying type of an enum, but
90 // this enum is used in an enum bitfield and integer comparisons are done with
92 // we need to guarantee that it's always unsigned so that, for example,
94 // doesn't compare less than eResolveStateUnresolved when used in a 2-bit
96 typedef enum ResolveStateTag : unsigned {
97 eResolveStateUnresolved = 0,
98 eResolveStateForward = 1,
99 eResolveStateLayout = 2,
100 eResolveStateFull = 3
103 Type(lldb::user_id_t uid, SymbolFile *symbol_file, const ConstString &name,
104 uint64_t byte_size, SymbolContextScope *context,
105 lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
106 const Declaration &decl, const CompilerType &compiler_qual_type,
107 ResolveState compiler_type_resolve_state);
109 // This makes an invalid type. Used for functions that return a Type when
114 Type(const Type &rhs);
116 const Type &operator=(const Type &rhs);
118 void Dump(Stream *s, bool show_context);
120 void DumpTypeName(Stream *s);
122 // Since Type instances only keep a "SymbolFile *" internally, other classes
123 // like TypeImpl need make sure the module is still around before playing with
124 // Type instances. They can store a weak pointer to the Module;
125 lldb::ModuleSP GetModule();
127 void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name);
129 SymbolFile *GetSymbolFile() { return m_symbol_file; }
130 const SymbolFile *GetSymbolFile() const { return m_symbol_file; }
132 TypeList *GetTypeList();
134 const ConstString &GetName();
136 uint64_t GetByteSize();
138 uint32_t GetNumChildren(bool omit_empty_base_classes);
140 bool IsAggregateType();
142 bool IsValidType() { return m_encoding_uid_type != eEncodingInvalid; }
144 bool IsTypedef() { return m_encoding_uid_type == eEncodingIsTypedefUID; }
146 lldb::TypeSP GetTypedefType();
148 const ConstString &GetName() const { return m_name; }
150 ConstString GetQualifiedName();
152 void DumpValue(ExecutionContext *exe_ctx, Stream *s,
153 const DataExtractor &data, uint32_t data_offset,
154 bool show_type, bool show_summary, bool verbose,
155 lldb::Format format = lldb::eFormatDefault);
157 bool DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s,
158 lldb::addr_t address, AddressType address_type,
159 bool show_types, bool show_summary, bool verbose);
161 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
162 AddressType address_type, DataExtractor &data);
164 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
165 AddressType address_type, DataExtractor &data);
167 bool GetIsDeclaration() const;
169 void SetIsDeclaration(bool b);
171 bool GetIsExternal() const;
173 void SetIsExternal(bool b);
175 lldb::Format GetFormat();
177 lldb::Encoding GetEncoding(uint64_t &count);
179 SymbolContextScope *GetSymbolContextScope() { return m_context; }
180 const SymbolContextScope *GetSymbolContextScope() const { return m_context; }
181 void SetSymbolContextScope(SymbolContextScope *context) {
185 const lldb_private::Declaration &GetDeclaration() const;
187 // Get the clang type, and resolve definitions for any
188 // class/struct/union/enum types completely.
189 CompilerType GetFullCompilerType();
191 // Get the clang type, and resolve definitions enough so that the type could
192 // have layout performed. This allows ptrs and refs to class/struct/union/enum
193 // types remain forward declarations.
194 CompilerType GetLayoutCompilerType();
196 // Get the clang type and leave class/struct/union/enum types as forward
197 // declarations if they haven't already been fully defined.
198 CompilerType GetForwardCompilerType();
200 static int Compare(const Type &a, const Type &b);
202 // From a fully qualified typename, split the type into the type basename
203 // and the remaining type scope (namespaces/classes).
204 static bool GetTypeScopeAndBasename(const llvm::StringRef& name,
205 llvm::StringRef &scope,
206 llvm::StringRef &basename,
207 lldb::TypeClass &type_class);
208 void SetEncodingType(Type *encoding_type) { m_encoding_type = encoding_type; }
210 uint32_t GetEncodingMask();
212 bool IsCompleteObjCClass() { return m_flags.is_complete_objc_class; }
214 void SetIsCompleteObjCClass(bool is_complete_objc_class) {
215 m_flags.is_complete_objc_class = is_complete_objc_class;
220 SymbolFile *m_symbol_file;
222 *m_context; // The symbol context in which this type is defined
223 Type *m_encoding_type;
224 lldb::user_id_t m_encoding_uid;
225 EncodingDataType m_encoding_uid_type;
226 uint64_t m_byte_size;
228 CompilerType m_compiler_type;
232 // using unsigned type here to work around a very noisy gcc warning
233 unsigned compiler_type_resolve_state : 2;
235 ResolveState compiler_type_resolve_state : 2;
237 bool is_complete_objc_class : 1;
240 Type *GetEncodingType();
242 bool ResolveClangType(ResolveState compiler_type_resolve_state);
245 // these classes are used to back the SBType* objects
249 TypePair() : compiler_type(), type_sp() {}
251 TypePair(CompilerType type) : compiler_type(type), type_sp() {}
253 TypePair(lldb::TypeSP type) : compiler_type(), type_sp(type) {
254 compiler_type = type_sp->GetForwardCompilerType();
257 bool IsValid() const {
258 return compiler_type.IsValid() || (type_sp.get() != nullptr);
261 explicit operator bool() const { return IsValid(); }
263 bool operator==(const TypePair &rhs) const {
264 return compiler_type == rhs.compiler_type &&
265 type_sp.get() == rhs.type_sp.get();
268 bool operator!=(const TypePair &rhs) const {
269 return compiler_type != rhs.compiler_type ||
270 type_sp.get() != rhs.type_sp.get();
274 compiler_type.Clear();
278 ConstString GetName() const {
280 return type_sp->GetName();
282 return compiler_type.GetTypeName();
283 return ConstString();
286 ConstString GetDisplayTypeName() const {
288 return type_sp->GetForwardCompilerType().GetDisplayTypeName();
290 return compiler_type.GetDisplayTypeName();
291 return ConstString();
294 void SetType(CompilerType type) {
296 compiler_type = type;
299 void SetType(lldb::TypeSP type) {
302 compiler_type = type_sp->GetForwardCompilerType();
304 compiler_type.Clear();
307 lldb::TypeSP GetTypeSP() const { return type_sp; }
309 CompilerType GetCompilerType() const { return compiler_type; }
311 CompilerType GetPointerType() const {
313 return type_sp->GetForwardCompilerType().GetPointerType();
314 return compiler_type.GetPointerType();
317 CompilerType GetPointeeType() const {
319 return type_sp->GetForwardCompilerType().GetPointeeType();
320 return compiler_type.GetPointeeType();
323 CompilerType GetReferenceType() const {
325 return type_sp->GetForwardCompilerType().GetLValueReferenceType();
327 return compiler_type.GetLValueReferenceType();
330 CompilerType GetTypedefedType() const {
332 return type_sp->GetForwardCompilerType().GetTypedefedType();
334 return compiler_type.GetTypedefedType();
337 CompilerType GetDereferencedType() const {
339 return type_sp->GetForwardCompilerType().GetNonReferenceType();
341 return compiler_type.GetNonReferenceType();
344 CompilerType GetUnqualifiedType() const {
346 return type_sp->GetForwardCompilerType().GetFullyUnqualifiedType();
348 return compiler_type.GetFullyUnqualifiedType();
351 CompilerType GetCanonicalType() const {
353 return type_sp->GetForwardCompilerType().GetCanonicalType();
354 return compiler_type.GetCanonicalType();
357 TypeSystem *GetTypeSystem() const { return compiler_type.GetTypeSystem(); }
359 lldb::ModuleSP GetModule() const {
361 return type_sp->GetModule();
362 return lldb::ModuleSP();
366 CompilerType compiler_type;
367 lldb::TypeSP type_sp;
370 // the two classes here are used by the public API as a backend to
371 // the SBType and SBTypeList classes
379 TypeImpl(const TypeImpl &rhs);
381 TypeImpl(const lldb::TypeSP &type_sp);
383 TypeImpl(const CompilerType &compiler_type);
385 TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
387 TypeImpl(const CompilerType &compiler_type, const CompilerType &dynamic);
389 TypeImpl(const TypePair &pair, const CompilerType &dynamic);
391 void SetType(const lldb::TypeSP &type_sp);
393 void SetType(const CompilerType &compiler_type);
395 void SetType(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
397 void SetType(const CompilerType &compiler_type, const CompilerType &dynamic);
399 void SetType(const TypePair &pair, const CompilerType &dynamic);
401 TypeImpl &operator=(const TypeImpl &rhs);
403 bool operator==(const TypeImpl &rhs) const;
405 bool operator!=(const TypeImpl &rhs) const;
407 bool IsValid() const;
409 explicit operator bool() const;
413 ConstString GetName() const;
415 ConstString GetDisplayTypeName() const;
417 TypeImpl GetPointerType() const;
419 TypeImpl GetPointeeType() const;
421 TypeImpl GetReferenceType() const;
423 TypeImpl GetTypedefedType() const;
425 TypeImpl GetDereferencedType() const;
427 TypeImpl GetUnqualifiedType() const;
429 TypeImpl GetCanonicalType() const;
431 CompilerType GetCompilerType(bool prefer_dynamic);
433 TypeSystem *GetTypeSystem(bool prefer_dynamic);
435 bool GetDescription(lldb_private::Stream &strm,
436 lldb::DescriptionLevel description_level);
439 bool CheckModule(lldb::ModuleSP &module_sp) const;
441 lldb::ModuleWP m_module_wp;
442 TypePair m_static_type;
443 CompilerType m_dynamic_type;
448 TypeListImpl() : m_content() {}
450 void Append(const lldb::TypeImplSP &type) { m_content.push_back(type); }
452 class AppendVisitor {
454 AppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {}
456 void operator()(const lldb::TypeImplSP &type) { m_type_list.Append(type); }
459 TypeListImpl &m_type_list;
462 void Append(const lldb_private::TypeList &type_list);
464 lldb::TypeImplSP GetTypeAtIndex(size_t idx) {
465 lldb::TypeImplSP type_sp;
467 type_sp = m_content[idx];
471 size_t GetSize() { return m_content.size(); }
474 std::vector<lldb::TypeImplSP> m_content;
477 class TypeMemberImpl {
480 : m_type_impl_sp(), m_bit_offset(0), m_name(), m_bitfield_bit_size(0),
485 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset,
486 const ConstString &name, uint32_t bitfield_bit_size = 0,
487 bool is_bitfield = false)
488 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(name),
489 m_bitfield_bit_size(bitfield_bit_size), m_is_bitfield(is_bitfield) {}
491 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset)
492 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(),
493 m_bitfield_bit_size(0), m_is_bitfield(false) {
495 m_name = m_type_impl_sp->GetName();
498 const lldb::TypeImplSP &GetTypeImpl() { return m_type_impl_sp; }
500 const ConstString &GetName() const { return m_name; }
502 uint64_t GetBitOffset() const { return m_bit_offset; }
504 uint32_t GetBitfieldBitSize() const { return m_bitfield_bit_size; }
506 void SetBitfieldBitSize(uint32_t bitfield_bit_size) {
507 m_bitfield_bit_size = bitfield_bit_size;
510 bool GetIsBitfield() const { return m_is_bitfield; }
512 void SetIsBitfield(bool is_bitfield) { m_is_bitfield = is_bitfield; }
515 lldb::TypeImplSP m_type_impl_sp;
516 uint64_t m_bit_offset;
518 uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
523 /// Sometimes you can find the name of the type corresponding to an object, but
524 /// we don't have debug
525 /// information for it. If that is the case, you can return one of these
526 /// objects, and then if it
527 /// has a full type, you can use that, but if not at least you can print the
528 /// name for informational
532 class TypeAndOrName {
535 TypeAndOrName(lldb::TypeSP &type_sp);
536 TypeAndOrName(const CompilerType &compiler_type);
537 TypeAndOrName(const char *type_str);
538 TypeAndOrName(const TypeAndOrName &rhs);
539 TypeAndOrName(ConstString &type_const_string);
541 TypeAndOrName &operator=(const TypeAndOrName &rhs);
543 bool operator==(const TypeAndOrName &other) const;
545 bool operator!=(const TypeAndOrName &other) const;
547 ConstString GetName() const;
549 lldb::TypeSP GetTypeSP() const { return m_type_pair.GetTypeSP(); }
551 CompilerType GetCompilerType() const { return m_type_pair.GetCompilerType(); }
553 void SetName(const ConstString &type_name);
555 void SetName(const char *type_name_cstr);
557 void SetTypeSP(lldb::TypeSP type_sp);
559 void SetCompilerType(CompilerType compiler_type);
561 bool IsEmpty() const;
563 bool HasName() const;
565 bool HasTypeSP() const;
567 bool HasCompilerType() const;
569 bool HasType() const { return HasTypeSP() || HasCompilerType(); }
573 explicit operator bool() { return !IsEmpty(); }
576 TypePair m_type_pair;
577 ConstString m_type_name;
580 class TypeMemberFunctionImpl {
582 TypeMemberFunctionImpl()
583 : m_type(), m_decl(), m_name(), m_kind(lldb::eMemberFunctionKindUnknown) {
586 TypeMemberFunctionImpl(const CompilerType &type, const CompilerDecl &decl,
587 const std::string &name,
588 const lldb::MemberFunctionKind &kind)
589 : m_type(type), m_decl(decl), m_name(name), m_kind(kind) {}
593 ConstString GetName() const;
595 ConstString GetMangledName() const;
597 CompilerType GetType() const;
599 CompilerType GetReturnType() const;
601 size_t GetNumArguments() const;
603 CompilerType GetArgumentAtIndex(size_t idx) const;
605 lldb::MemberFunctionKind GetKind() const;
607 bool GetDescription(Stream &stream);
610 std::string GetPrintableTypeName();
616 lldb::MemberFunctionKind m_kind;
619 class TypeEnumMemberImpl {
622 : m_integer_type_sp(), m_name("<invalid>"), m_value(), m_valid(false) {}
624 TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
625 const ConstString &name, const llvm::APSInt &value);
627 TypeEnumMemberImpl(const TypeEnumMemberImpl &rhs)
628 : m_integer_type_sp(rhs.m_integer_type_sp), m_name(rhs.m_name),
629 m_value(rhs.m_value), m_valid(rhs.m_valid) {}
631 TypeEnumMemberImpl &operator=(const TypeEnumMemberImpl &rhs);
633 bool IsValid() { return m_valid; }
635 const ConstString &GetName() const { return m_name; }
637 const lldb::TypeImplSP &GetIntegerType() const { return m_integer_type_sp; }
639 uint64_t GetValueAsUnsigned() const { return m_value.getZExtValue(); }
641 int64_t GetValueAsSigned() const { return m_value.getSExtValue(); }
644 lldb::TypeImplSP m_integer_type_sp;
646 llvm::APSInt m_value;
650 class TypeEnumMemberListImpl {
652 TypeEnumMemberListImpl() : m_content() {}
654 void Append(const lldb::TypeEnumMemberImplSP &type) {
655 m_content.push_back(type);
658 void Append(const lldb_private::TypeEnumMemberListImpl &type_list);
660 lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx) {
661 lldb::TypeEnumMemberImplSP enum_member;
663 enum_member = m_content[idx];
667 size_t GetSize() { return m_content.size(); }
670 std::vector<lldb::TypeEnumMemberImplSP> m_content;
673 } // namespace lldb_private
675 #endif // liblldb_Type_h_