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/Symbol/CompilerDecl.h"
15 #include "lldb/Symbol/CompilerType.h"
16 #include "lldb/Symbol/Declaration.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/UserID.h"
19 #include "lldb/lldb-private.h"
21 #include "llvm/ADT/APSInt.h"
25 namespace lldb_private {
26 //----------------------------------------------------------------------
27 // CompilerContext allows an array of these items to be passed to
28 // perform detailed lookups in SymbolVendor and SymbolFile functions.
29 //----------------------------------------------------------------------
30 struct CompilerContext {
31 CompilerContext(CompilerContextKind t, const ConstString &n)
34 bool operator==(const CompilerContext &rhs) const {
35 return type == rhs.type && name == rhs.name;
40 CompilerContextKind type;
44 class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>,
47 SymbolFileType(SymbolFile &symbol_file, lldb::user_id_t uid)
48 : UserID(uid), m_symbol_file(symbol_file) {}
50 SymbolFileType(SymbolFile &symbol_file, const lldb::TypeSP &type_sp);
54 Type *operator->() { return GetType(); }
59 SymbolFile &m_symbol_file;
60 lldb::TypeSP m_type_sp;
63 class Type : public std::enable_shared_from_this<Type>, public UserID {
65 typedef enum EncodingDataTypeTag {
67 eEncodingIsUID, ///< This type is the type whose UID is m_encoding_uid
68 eEncodingIsConstUID, ///< This type is the type whose UID is m_encoding_uid
69 ///with the const qualifier added
70 eEncodingIsRestrictUID, ///< This type is the type whose UID is
71 ///m_encoding_uid with the restrict qualifier added
72 eEncodingIsVolatileUID, ///< This type is the type whose UID is
73 ///m_encoding_uid with the volatile qualifier added
74 eEncodingIsTypedefUID, ///< This type is pointer to a type whose UID is
76 eEncodingIsPointerUID, ///< This type is pointer to a type whose UID is
78 eEncodingIsLValueReferenceUID, ///< This type is L value reference to a type
79 ///whose UID is m_encoding_uid
80 eEncodingIsRValueReferenceUID, ///< This type is R value reference to a type
81 ///whose UID is m_encoding_uid
82 eEncodingIsSyntheticUID
85 // We must force the underlying type of the enum to be unsigned here. Not all
87 // behave the same with regards to the default underlying type of an enum, but
89 // this enum is used in an enum bitfield and integer comparisons are done with
91 // we need to guarantee that it's always unsigned so that, for example,
93 // doesn't compare less than eResolveStateUnresolved when used in a 2-bit
95 typedef enum ResolveStateTag : unsigned {
96 eResolveStateUnresolved = 0,
97 eResolveStateForward = 1,
98 eResolveStateLayout = 2,
102 Type(lldb::user_id_t uid, SymbolFile *symbol_file, const ConstString &name,
103 uint64_t byte_size, SymbolContextScope *context,
104 lldb::user_id_t encoding_uid, EncodingDataType encoding_uid_type,
105 const Declaration &decl, const CompilerType &compiler_qual_type,
106 ResolveState compiler_type_resolve_state);
108 // This makes an invalid type. Used for functions that return a Type when
113 Type(const Type &rhs);
115 const Type &operator=(const Type &rhs);
117 void Dump(Stream *s, bool show_context);
119 void DumpTypeName(Stream *s);
121 // Since Type instances only keep a "SymbolFile *" internally, other classes
122 // like TypeImpl need make sure the module is still around before playing with
123 // Type instances. They can store a weak pointer to the Module;
124 lldb::ModuleSP GetModule();
126 void GetDescription(Stream *s, lldb::DescriptionLevel level, bool show_name);
128 SymbolFile *GetSymbolFile() { return m_symbol_file; }
129 const SymbolFile *GetSymbolFile() const { return m_symbol_file; }
131 TypeList *GetTypeList();
133 const ConstString &GetName();
135 uint64_t GetByteSize();
137 uint32_t GetNumChildren(bool omit_empty_base_classes);
139 bool IsAggregateType();
141 bool IsValidType() { return m_encoding_uid_type != eEncodingInvalid; }
143 bool IsTypedef() { return m_encoding_uid_type == eEncodingIsTypedefUID; }
145 lldb::TypeSP GetTypedefType();
147 const ConstString &GetName() const { return m_name; }
149 ConstString GetQualifiedName();
151 void DumpValue(ExecutionContext *exe_ctx, Stream *s,
152 const DataExtractor &data, uint32_t data_offset,
153 bool show_type, bool show_summary, bool verbose,
154 lldb::Format format = lldb::eFormatDefault);
156 bool DumpValueInMemory(ExecutionContext *exe_ctx, Stream *s,
157 lldb::addr_t address, AddressType address_type,
158 bool show_types, bool show_summary, bool verbose);
160 bool ReadFromMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
161 AddressType address_type, DataExtractor &data);
163 bool WriteToMemory(ExecutionContext *exe_ctx, lldb::addr_t address,
164 AddressType address_type, DataExtractor &data);
166 bool GetIsDeclaration() const;
168 void SetIsDeclaration(bool b);
170 bool GetIsExternal() const;
172 void SetIsExternal(bool b);
174 lldb::Format GetFormat();
176 lldb::Encoding GetEncoding(uint64_t &count);
178 SymbolContextScope *GetSymbolContextScope() { return m_context; }
179 const SymbolContextScope *GetSymbolContextScope() const { return m_context; }
180 void SetSymbolContextScope(SymbolContextScope *context) {
184 const lldb_private::Declaration &GetDeclaration() const;
186 // Get the clang type, and resolve definitions for any
187 // class/struct/union/enum types completely.
188 CompilerType GetFullCompilerType();
190 // Get the clang type, and resolve definitions enough so that the type could
191 // have layout performed. This allows ptrs and refs to class/struct/union/enum
192 // types remain forward declarations.
193 CompilerType GetLayoutCompilerType();
195 // Get the clang type and leave class/struct/union/enum types as forward
196 // declarations if they haven't already been fully defined.
197 CompilerType GetForwardCompilerType();
199 static int Compare(const Type &a, const Type &b);
201 // From a fully qualified typename, split the type into the type basename
202 // and the remaining type scope (namespaces/classes).
203 static bool GetTypeScopeAndBasename(const llvm::StringRef& name,
204 llvm::StringRef &scope,
205 llvm::StringRef &basename,
206 lldb::TypeClass &type_class);
207 void SetEncodingType(Type *encoding_type) { m_encoding_type = encoding_type; }
209 uint32_t GetEncodingMask();
211 bool IsCompleteObjCClass() { return m_flags.is_complete_objc_class; }
213 void SetIsCompleteObjCClass(bool is_complete_objc_class) {
214 m_flags.is_complete_objc_class = is_complete_objc_class;
219 SymbolFile *m_symbol_file;
221 *m_context; // The symbol context in which this type is defined
222 Type *m_encoding_type;
223 lldb::user_id_t m_encoding_uid;
224 EncodingDataType m_encoding_uid_type;
225 uint64_t m_byte_size;
227 CompilerType m_compiler_type;
231 // using unsigned type here to work around a very noisy gcc warning
232 unsigned compiler_type_resolve_state : 2;
234 ResolveState compiler_type_resolve_state : 2;
236 bool is_complete_objc_class : 1;
239 Type *GetEncodingType();
241 bool ResolveClangType(ResolveState compiler_type_resolve_state);
244 // these classes are used to back the SBType* objects
248 TypePair() : compiler_type(), type_sp() {}
250 TypePair(CompilerType type) : compiler_type(type), type_sp() {}
252 TypePair(lldb::TypeSP type) : compiler_type(), type_sp(type) {
253 compiler_type = type_sp->GetForwardCompilerType();
256 bool IsValid() const {
257 return compiler_type.IsValid() || (type_sp.get() != nullptr);
260 explicit operator bool() const { return IsValid(); }
262 bool operator==(const TypePair &rhs) const {
263 return compiler_type == rhs.compiler_type &&
264 type_sp.get() == rhs.type_sp.get();
267 bool operator!=(const TypePair &rhs) const {
268 return compiler_type != rhs.compiler_type ||
269 type_sp.get() != rhs.type_sp.get();
273 compiler_type.Clear();
277 ConstString GetName() const {
279 return type_sp->GetName();
281 return compiler_type.GetTypeName();
282 return ConstString();
285 ConstString GetDisplayTypeName() const {
287 return type_sp->GetForwardCompilerType().GetDisplayTypeName();
289 return compiler_type.GetDisplayTypeName();
290 return ConstString();
293 void SetType(CompilerType type) {
295 compiler_type = type;
298 void SetType(lldb::TypeSP type) {
301 compiler_type = type_sp->GetForwardCompilerType();
303 compiler_type.Clear();
306 lldb::TypeSP GetTypeSP() const { return type_sp; }
308 CompilerType GetCompilerType() const { return compiler_type; }
310 CompilerType GetPointerType() const {
312 return type_sp->GetForwardCompilerType().GetPointerType();
313 return compiler_type.GetPointerType();
316 CompilerType GetPointeeType() const {
318 return type_sp->GetForwardCompilerType().GetPointeeType();
319 return compiler_type.GetPointeeType();
322 CompilerType GetReferenceType() const {
324 return type_sp->GetForwardCompilerType().GetLValueReferenceType();
326 return compiler_type.GetLValueReferenceType();
329 CompilerType GetTypedefedType() const {
331 return type_sp->GetForwardCompilerType().GetTypedefedType();
333 return compiler_type.GetTypedefedType();
336 CompilerType GetDereferencedType() const {
338 return type_sp->GetForwardCompilerType().GetNonReferenceType();
340 return compiler_type.GetNonReferenceType();
343 CompilerType GetUnqualifiedType() const {
345 return type_sp->GetForwardCompilerType().GetFullyUnqualifiedType();
347 return compiler_type.GetFullyUnqualifiedType();
350 CompilerType GetCanonicalType() const {
352 return type_sp->GetForwardCompilerType().GetCanonicalType();
353 return compiler_type.GetCanonicalType();
356 TypeSystem *GetTypeSystem() const { return compiler_type.GetTypeSystem(); }
358 lldb::ModuleSP GetModule() const {
360 return type_sp->GetModule();
361 return lldb::ModuleSP();
365 CompilerType compiler_type;
366 lldb::TypeSP type_sp;
369 // the two classes here are used by the public API as a backend to
370 // the SBType and SBTypeList classes
378 TypeImpl(const TypeImpl &rhs);
380 TypeImpl(const lldb::TypeSP &type_sp);
382 TypeImpl(const CompilerType &compiler_type);
384 TypeImpl(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
386 TypeImpl(const CompilerType &compiler_type, const CompilerType &dynamic);
388 TypeImpl(const TypePair &pair, const CompilerType &dynamic);
390 void SetType(const lldb::TypeSP &type_sp);
392 void SetType(const CompilerType &compiler_type);
394 void SetType(const lldb::TypeSP &type_sp, const CompilerType &dynamic);
396 void SetType(const CompilerType &compiler_type, const CompilerType &dynamic);
398 void SetType(const TypePair &pair, const CompilerType &dynamic);
400 TypeImpl &operator=(const TypeImpl &rhs);
402 bool operator==(const TypeImpl &rhs) const;
404 bool operator!=(const TypeImpl &rhs) const;
406 bool IsValid() const;
408 explicit operator bool() const;
412 ConstString GetName() const;
414 ConstString GetDisplayTypeName() const;
416 TypeImpl GetPointerType() const;
418 TypeImpl GetPointeeType() const;
420 TypeImpl GetReferenceType() const;
422 TypeImpl GetTypedefedType() const;
424 TypeImpl GetDereferencedType() const;
426 TypeImpl GetUnqualifiedType() const;
428 TypeImpl GetCanonicalType() const;
430 CompilerType GetCompilerType(bool prefer_dynamic);
432 TypeSystem *GetTypeSystem(bool prefer_dynamic);
434 bool GetDescription(lldb_private::Stream &strm,
435 lldb::DescriptionLevel description_level);
438 bool CheckModule(lldb::ModuleSP &module_sp) const;
440 lldb::ModuleWP m_module_wp;
441 TypePair m_static_type;
442 CompilerType m_dynamic_type;
447 TypeListImpl() : m_content() {}
449 void Append(const lldb::TypeImplSP &type) { m_content.push_back(type); }
451 class AppendVisitor {
453 AppendVisitor(TypeListImpl &type_list) : m_type_list(type_list) {}
455 void operator()(const lldb::TypeImplSP &type) { m_type_list.Append(type); }
458 TypeListImpl &m_type_list;
461 void Append(const lldb_private::TypeList &type_list);
463 lldb::TypeImplSP GetTypeAtIndex(size_t idx) {
464 lldb::TypeImplSP type_sp;
466 type_sp = m_content[idx];
470 size_t GetSize() { return m_content.size(); }
473 std::vector<lldb::TypeImplSP> m_content;
476 class TypeMemberImpl {
479 : m_type_impl_sp(), m_bit_offset(0), m_name(), m_bitfield_bit_size(0),
484 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset,
485 const ConstString &name, uint32_t bitfield_bit_size = 0,
486 bool is_bitfield = false)
487 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(name),
488 m_bitfield_bit_size(bitfield_bit_size), m_is_bitfield(is_bitfield) {}
490 TypeMemberImpl(const lldb::TypeImplSP &type_impl_sp, uint64_t bit_offset)
491 : m_type_impl_sp(type_impl_sp), m_bit_offset(bit_offset), m_name(),
492 m_bitfield_bit_size(0), m_is_bitfield(false) {
494 m_name = m_type_impl_sp->GetName();
497 const lldb::TypeImplSP &GetTypeImpl() { return m_type_impl_sp; }
499 const ConstString &GetName() const { return m_name; }
501 uint64_t GetBitOffset() const { return m_bit_offset; }
503 uint32_t GetBitfieldBitSize() const { return m_bitfield_bit_size; }
505 void SetBitfieldBitSize(uint32_t bitfield_bit_size) {
506 m_bitfield_bit_size = bitfield_bit_size;
509 bool GetIsBitfield() const { return m_is_bitfield; }
511 void SetIsBitfield(bool is_bitfield) { m_is_bitfield = is_bitfield; }
514 lldb::TypeImplSP m_type_impl_sp;
515 uint64_t m_bit_offset;
517 uint32_t m_bitfield_bit_size; // Bit size for bitfield members only
522 /// Sometimes you can find the name of the type corresponding to an object, but
523 /// we don't have debug
524 /// information for it. If that is the case, you can return one of these
525 /// objects, and then if it
526 /// has a full type, you can use that, but if not at least you can print the
527 /// name for informational
531 class TypeAndOrName {
534 TypeAndOrName(lldb::TypeSP &type_sp);
535 TypeAndOrName(const CompilerType &compiler_type);
536 TypeAndOrName(const char *type_str);
537 TypeAndOrName(const TypeAndOrName &rhs);
538 TypeAndOrName(ConstString &type_const_string);
540 TypeAndOrName &operator=(const TypeAndOrName &rhs);
542 bool operator==(const TypeAndOrName &other) const;
544 bool operator!=(const TypeAndOrName &other) const;
546 ConstString GetName() const;
548 lldb::TypeSP GetTypeSP() const { return m_type_pair.GetTypeSP(); }
550 CompilerType GetCompilerType() const { return m_type_pair.GetCompilerType(); }
552 void SetName(const ConstString &type_name);
554 void SetName(const char *type_name_cstr);
556 void SetTypeSP(lldb::TypeSP type_sp);
558 void SetCompilerType(CompilerType compiler_type);
560 bool IsEmpty() const;
562 bool HasName() const;
564 bool HasTypeSP() const;
566 bool HasCompilerType() const;
568 bool HasType() const { return HasTypeSP() || HasCompilerType(); }
572 explicit operator bool() { return !IsEmpty(); }
575 TypePair m_type_pair;
576 ConstString m_type_name;
579 class TypeMemberFunctionImpl {
581 TypeMemberFunctionImpl()
582 : m_type(), m_decl(), m_name(), m_kind(lldb::eMemberFunctionKindUnknown) {
585 TypeMemberFunctionImpl(const CompilerType &type, const CompilerDecl &decl,
586 const std::string &name,
587 const lldb::MemberFunctionKind &kind)
588 : m_type(type), m_decl(decl), m_name(name), m_kind(kind) {}
592 ConstString GetName() const;
594 ConstString GetMangledName() const;
596 CompilerType GetType() const;
598 CompilerType GetReturnType() const;
600 size_t GetNumArguments() const;
602 CompilerType GetArgumentAtIndex(size_t idx) const;
604 lldb::MemberFunctionKind GetKind() const;
606 bool GetDescription(Stream &stream);
609 std::string GetPrintableTypeName();
615 lldb::MemberFunctionKind m_kind;
618 class TypeEnumMemberImpl {
621 : m_integer_type_sp(), m_name("<invalid>"), m_value(), m_valid(false) {}
623 TypeEnumMemberImpl(const lldb::TypeImplSP &integer_type_sp,
624 const ConstString &name, const llvm::APSInt &value);
626 TypeEnumMemberImpl(const TypeEnumMemberImpl &rhs)
627 : m_integer_type_sp(rhs.m_integer_type_sp), m_name(rhs.m_name),
628 m_value(rhs.m_value), m_valid(rhs.m_valid) {}
630 TypeEnumMemberImpl &operator=(const TypeEnumMemberImpl &rhs);
632 bool IsValid() { return m_valid; }
634 const ConstString &GetName() const { return m_name; }
636 const lldb::TypeImplSP &GetIntegerType() const { return m_integer_type_sp; }
638 uint64_t GetValueAsUnsigned() const { return m_value.getZExtValue(); }
640 int64_t GetValueAsSigned() const { return m_value.getSExtValue(); }
643 lldb::TypeImplSP m_integer_type_sp;
645 llvm::APSInt m_value;
649 class TypeEnumMemberListImpl {
651 TypeEnumMemberListImpl() : m_content() {}
653 void Append(const lldb::TypeEnumMemberImplSP &type) {
654 m_content.push_back(type);
657 void Append(const lldb_private::TypeEnumMemberListImpl &type_list);
659 lldb::TypeEnumMemberImplSP GetTypeEnumMemberAtIndex(size_t idx) {
660 lldb::TypeEnumMemberImplSP enum_member;
662 enum_member = m_content[idx];
666 size_t GetSize() { return m_content.size(); }
669 std::vector<lldb::TypeEnumMemberImplSP> m_content;
672 } // namespace lldb_private
674 #endif // liblldb_Type_h_