1 //===-- Type.cpp ------------------------------------------------*- 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 // Other libraries and framework includes
12 #include "lldb/Core/DataExtractor.h"
13 #include "lldb/Core/DataBufferHeap.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Scalar.h"
16 #include "lldb/Core/StreamString.h"
18 #include "lldb/Symbol/ClangASTType.h"
19 #include "lldb/Symbol/ClangASTContext.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContextScope.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Symbol/SymbolVendor.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Symbol/TypeList.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
31 #include "llvm/ADT/StringRef.h"
34 using namespace lldb_private;
36 class TypeAppendVisitor
39 TypeAppendVisitor(TypeListImpl &type_list) :
40 m_type_list(type_list)
45 operator() (const lldb::TypeSP& type)
47 m_type_list.Append(TypeImplSP(new TypeImpl(type)));
52 TypeListImpl &m_type_list;
56 TypeListImpl::Append (const lldb_private::TypeList &type_list)
58 TypeAppendVisitor cb(*this);
59 type_list.ForEach(cb);
64 SymbolFileType::GetType ()
68 Type *resolved_type = m_symbol_file.ResolveTypeUID (GetID());
70 m_type_sp = resolved_type->shared_from_this();
72 return m_type_sp.get();
79 SymbolFile* symbol_file,
80 const ConstString &name,
82 SymbolContextScope *context,
83 user_id_t encoding_uid,
84 EncodingDataType encoding_uid_type,
85 const Declaration& decl,
86 const ClangASTType &clang_type,
87 ResolveState clang_type_resolve_state
89 std::enable_shared_from_this<Type> (),
92 m_symbol_file (symbol_file),
94 m_encoding_type (NULL),
95 m_encoding_uid (encoding_uid),
96 m_encoding_uid_type (encoding_uid_type),
97 m_byte_size (byte_size),
99 m_clang_type (clang_type)
101 m_flags.clang_type_resolve_state = (clang_type ? clang_type_resolve_state : eResolveStateUnresolved);
102 m_flags.is_complete_objc_class = false;
106 std::enable_shared_from_this<Type> (),
108 m_name ("<INVALID TYPE>"),
109 m_symbol_file (NULL),
111 m_encoding_type (NULL),
112 m_encoding_uid (LLDB_INVALID_UID),
113 m_encoding_uid_type (eEncodingInvalid),
118 m_flags.clang_type_resolve_state = eResolveStateUnresolved;
119 m_flags.is_complete_objc_class = false;
123 Type::Type (const Type &rhs) :
124 std::enable_shared_from_this<Type> (rhs),
127 m_symbol_file (rhs.m_symbol_file),
128 m_context (rhs.m_context),
129 m_encoding_type (rhs.m_encoding_type),
130 m_encoding_uid (rhs.m_encoding_uid),
131 m_encoding_uid_type (rhs.m_encoding_uid_type),
132 m_byte_size (rhs.m_byte_size),
134 m_clang_type (rhs.m_clang_type),
135 m_flags (rhs.m_flags)
140 Type::operator= (const Type& rhs)
150 Type::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_name)
152 *s << "id = " << (const UserID&)*this;
154 // Call the name accessor to make sure we resolve the type name
157 const ConstString &type_name = GetName();
160 *s << ", name = \"" << type_name << '"';
161 ConstString qualified_type_name (GetQualifiedName());
162 if (qualified_type_name != type_name)
164 *s << ", qualified = \"" << qualified_type_name << '"';
169 // Call the get byte size accesor so we resolve our byte size
171 s->Printf(", byte-size = %" PRIu64, m_byte_size);
172 bool show_fullpaths = (level == lldb::eDescriptionLevelVerbose);
173 m_decl.Dump(s, show_fullpaths);
175 if (m_clang_type.IsValid())
177 *s << ", clang_type = \"";
178 GetClangForwardType().DumpTypeDescription(s);
181 else if (m_encoding_uid != LLDB_INVALID_UID)
183 s->Printf(", type_uid = 0x%8.8" PRIx64, m_encoding_uid);
184 switch (m_encoding_uid_type)
186 case eEncodingInvalid: break;
187 case eEncodingIsUID: s->PutCString(" (unresolved type)"); break;
188 case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break;
189 case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break;
190 case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break;
191 case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break;
192 case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break;
193 case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break;
194 case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break;
195 case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break;
202 Type::Dump (Stream *s, bool show_context)
204 s->Printf("%p: ", this);
206 *s << "Type" << (const UserID&)*this << ' ';
208 *s << ", name = \"" << m_name << "\"";
210 if (m_byte_size != 0)
211 s->Printf(", size = %" PRIu64, m_byte_size);
213 if (show_context && m_context != NULL)
215 s->PutCString(", context = ( ");
216 m_context->DumpSymbolContext(s);
220 bool show_fullpaths = false;
221 m_decl.Dump (s,show_fullpaths);
223 if (m_clang_type.IsValid())
225 *s << ", clang_type = " << m_clang_type.GetOpaqueQualType() << ' ';
226 GetClangForwardType().DumpTypeDescription (s);
228 else if (m_encoding_uid != LLDB_INVALID_UID)
230 *s << ", type_data = " << (uint64_t)m_encoding_uid;
231 switch (m_encoding_uid_type)
233 case eEncodingInvalid: break;
234 case eEncodingIsUID: s->PutCString(" (unresolved type)"); break;
235 case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break;
236 case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break;
237 case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break;
238 case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break;
239 case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break;
240 case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break;
241 case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break;
242 case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break;
248 // s->Printf(", access = %u", m_access);
256 m_name = GetClangForwardType().GetConstTypeName();
261 Type::DumpTypeName(Stream *s)
263 GetName().Dump(s, "<invalid-type-name>");
270 ExecutionContext *exe_ctx,
272 const DataExtractor &data,
273 uint32_t data_byte_offset,
280 if (ResolveClangType(eResolveStateForward))
286 s->Printf("Type{0x%8.8" PRIx64 "} ", GetID());
291 GetClangForwardType().DumpValue (exe_ctx,
293 format == lldb::eFormatDefault ? GetFormat() : format,
297 0, // Bitfield bit size
298 0, // Bitfield bit offset
307 Type::GetEncodingType ()
309 if (m_encoding_type == NULL && m_encoding_uid != LLDB_INVALID_UID)
310 m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
311 return m_encoding_type;
319 if (m_byte_size == 0)
321 switch (m_encoding_uid_type)
323 case eEncodingInvalid:
324 case eEncodingIsSyntheticUID:
327 case eEncodingIsConstUID:
328 case eEncodingIsRestrictUID:
329 case eEncodingIsVolatileUID:
330 case eEncodingIsTypedefUID:
332 Type *encoding_type = GetEncodingType ();
334 m_byte_size = encoding_type->GetByteSize();
335 if (m_byte_size == 0)
336 m_byte_size = GetClangLayoutType().GetByteSize();
340 // If we are a pointer or reference, then this is just a pointer size;
341 case eEncodingIsPointerUID:
342 case eEncodingIsLValueReferenceUID:
343 case eEncodingIsRValueReferenceUID:
344 m_byte_size = m_symbol_file->GetClangASTContext().GetPointerByteSize();
353 Type::GetNumChildren (bool omit_empty_base_classes)
355 return GetClangForwardType().GetNumChildren(omit_empty_base_classes);
359 Type::IsAggregateType ()
361 return GetClangForwardType().IsAggregateType();
365 Type::GetTypedefType()
367 lldb::TypeSP type_sp;
370 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
372 type_sp = typedef_type->shared_from_this();
382 return GetClangForwardType().GetFormat();
388 Type::GetEncoding (uint64_t &count)
390 // Make sure we resolve our type if it already hasn't been.
391 return GetClangForwardType().GetEncoding(count);
395 Type::DumpValueInMemory
397 ExecutionContext *exe_ctx,
399 lldb::addr_t address,
400 AddressType address_type,
406 if (address != LLDB_INVALID_ADDRESS)
409 Target *target = NULL;
411 target = exe_ctx->GetTargetPtr();
413 data.SetByteOrder (target->GetArchitecture().GetByteOrder());
414 if (ReadFromMemory (exe_ctx, address, address_type, data))
416 DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose);
425 Type::ReadFromMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
427 if (address_type == eAddressTypeFile)
429 // Can't convert a file address to anything valid without more
430 // context (which Module it came from)
434 const uint64_t byte_size = GetByteSize();
435 if (data.GetByteSize() < byte_size)
437 lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
438 data.SetData(data_sp);
441 uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
444 if (address_type == eAddressTypeHost)
446 // The address is an address in this process, so just copy it
449 memcpy (dst, (uint8_t*)NULL + addr, byte_size);
456 Process *process = exe_ctx->GetProcessPtr();
460 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, error) == byte_size;
470 Type::WriteToMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data)
479 return GetSymbolFile()->GetTypeList();
483 Type::GetDeclaration () const
489 Type::ResolveClangType (ResolveState clang_type_resolve_state)
491 Type *encoding_type = NULL;
492 if (!m_clang_type.IsValid())
494 encoding_type = GetEncodingType();
497 switch (m_encoding_uid_type)
501 ClangASTType encoding_clang_type = encoding_type->GetClangForwardType();
502 if (encoding_clang_type.IsValid())
504 m_clang_type = encoding_clang_type;
505 m_flags.clang_type_resolve_state = encoding_type->m_flags.clang_type_resolve_state;
510 case eEncodingIsConstUID:
511 m_clang_type = encoding_type->GetClangForwardType().AddConstModifier();
514 case eEncodingIsRestrictUID:
515 m_clang_type = encoding_type->GetClangForwardType().AddRestrictModifier();
518 case eEncodingIsVolatileUID:
519 m_clang_type = encoding_type->GetClangForwardType().AddVolatileModifier();
522 case eEncodingIsTypedefUID:
523 m_clang_type = encoding_type->GetClangForwardType().CreateTypedefType (GetName().AsCString(),
524 GetSymbolFile()->GetClangDeclContextContainingTypeUID(GetID()));
528 case eEncodingIsPointerUID:
529 m_clang_type = encoding_type->GetClangForwardType().GetPointerType();
532 case eEncodingIsLValueReferenceUID:
533 m_clang_type = encoding_type->GetClangForwardType().GetLValueReferenceType();
536 case eEncodingIsRValueReferenceUID:
537 m_clang_type = encoding_type->GetClangForwardType().GetRValueReferenceType();
541 assert(!"Unhandled encoding_data_type.");
547 // We have no encoding type, return void?
548 ClangASTType void_clang_type (ClangASTContext::GetBasicType(GetClangASTContext().getASTContext(), eBasicTypeVoid));
549 switch (m_encoding_uid_type)
552 m_clang_type = void_clang_type;
555 case eEncodingIsConstUID:
556 m_clang_type = void_clang_type.AddConstModifier ();
559 case eEncodingIsRestrictUID:
560 m_clang_type = void_clang_type.AddRestrictModifier ();
563 case eEncodingIsVolatileUID:
564 m_clang_type = void_clang_type.AddVolatileModifier ();
567 case eEncodingIsTypedefUID:
568 m_clang_type = void_clang_type.CreateTypedefType (GetName().AsCString(),
569 GetSymbolFile()->GetClangDeclContextContainingTypeUID(GetID()));
572 case eEncodingIsPointerUID:
573 m_clang_type = void_clang_type.GetPointerType ();
576 case eEncodingIsLValueReferenceUID:
577 m_clang_type = void_clang_type.GetLValueReferenceType ();
580 case eEncodingIsRValueReferenceUID:
581 m_clang_type = void_clang_type.GetRValueReferenceType ();
585 assert(!"Unhandled encoding_data_type.");
591 // Check if we have a forward reference to a class/struct/union/enum?
592 if (m_clang_type.IsValid() && m_flags.clang_type_resolve_state < clang_type_resolve_state)
594 m_flags.clang_type_resolve_state = eResolveStateFull;
595 if (!m_clang_type.IsDefined ())
597 // We have a forward declaration, we need to resolve it to a complete definition.
598 m_symbol_file->ResolveClangOpaqueTypeDefinition (m_clang_type);
602 // If we have an encoding type, then we need to make sure it is
603 // resolved appropriately.
604 if (m_encoding_uid != LLDB_INVALID_UID)
606 if (encoding_type == NULL)
607 encoding_type = GetEncodingType();
610 ResolveState encoding_clang_type_resolve_state = clang_type_resolve_state;
612 if (clang_type_resolve_state == eResolveStateLayout)
614 switch (m_encoding_uid_type)
616 case eEncodingIsPointerUID:
617 case eEncodingIsLValueReferenceUID:
618 case eEncodingIsRValueReferenceUID:
619 encoding_clang_type_resolve_state = eResolveStateForward;
625 encoding_type->ResolveClangType (encoding_clang_type_resolve_state);
628 return m_clang_type.IsValid();
631 Type::GetEncodingMask ()
633 uint32_t encoding_mask = 1u << m_encoding_uid_type;
634 Type *encoding_type = GetEncodingType();
635 assert (encoding_type != this);
637 encoding_mask |= encoding_type->GetEncodingMask ();
638 return encoding_mask;
642 Type::GetClangFullType ()
644 ResolveClangType(eResolveStateFull);
649 Type::GetClangLayoutType ()
651 ResolveClangType(eResolveStateLayout);
656 Type::GetClangForwardType ()
658 ResolveClangType (eResolveStateForward);
663 Type::GetClangASTContext ()
665 return m_symbol_file->GetClangASTContext();
669 Type::Compare(const Type &a, const Type &b)
671 // Just compare the UID values for now...
672 lldb::user_id_t a_uid = a.GetID();
673 lldb::user_id_t b_uid = b.GetID();
679 // if (a.getQualType() == b.getQualType())
684 #if 0 // START REMOVE
685 // Move this into ClangASTType
687 Type::CreateClangPointerType (Type *type)
690 return GetClangASTContext().CreatePointerType(type->GetClangForwardType());
694 Type::CreateClangTypedefType (Type *typedef_type, Type *base_type)
696 assert(typedef_type && base_type);
697 return GetClangASTContext().CreateTypedefType (typedef_type->GetName().AsCString(),
698 base_type->GetClangForwardType(),
699 typedef_type->GetSymbolFile()->GetClangDeclContextContainingTypeUID(typedef_type->GetID()));
703 Type::CreateClangLValueReferenceType (Type *type)
706 return GetClangASTContext().CreateLValueReferenceType(type->GetClangForwardType());
710 Type::CreateClangRValueReferenceType (Type *type)
713 return GetClangASTContext().CreateRValueReferenceType (type->GetClangForwardType());
718 Type::IsRealObjCClass()
720 // For now we are just skipping ObjC classes that get made by hand from the runtime, because
721 // those don't have any information. We could extend this to only return true for "full
722 // definitions" if we can figure that out.
724 if (m_clang_type.IsObjCObjectOrInterfaceType() && GetByteSize() != 0)
731 Type::GetQualifiedName ()
733 return GetClangForwardType().GetConstTypeName();
738 Type::GetTypeScopeAndBasename (const char* &name_cstr,
740 std::string &basename,
741 TypeClass &type_class)
743 // Protect against null c string.
745 type_class = eTypeClassAny;
747 if (name_cstr && name_cstr[0])
749 llvm::StringRef name_strref(name_cstr);
750 if (name_strref.startswith("struct "))
753 type_class = eTypeClassStruct;
755 else if (name_strref.startswith("class "))
758 type_class = eTypeClassClass;
760 else if (name_strref.startswith("union "))
763 type_class = eTypeClassUnion;
765 else if (name_strref.startswith("enum "))
768 type_class = eTypeClassEnumeration;
770 else if (name_strref.startswith("typedef "))
773 type_class = eTypeClassTypedef;
775 const char *basename_cstr = name_cstr;
776 const char* namespace_separator = ::strstr (basename_cstr, "::");
777 if (namespace_separator)
779 const char* template_arg_char = ::strchr (basename_cstr, '<');
780 while (namespace_separator != NULL)
782 if (template_arg_char && namespace_separator > template_arg_char) // but namespace'd template arguments are still good to go
784 basename_cstr = namespace_separator + 2;
785 namespace_separator = strstr(basename_cstr, "::");
787 if (basename_cstr > name_cstr)
789 scope.assign (name_cstr, basename_cstr - name_cstr);
790 basename.assign (basename_cstr);
801 TypeAndOrName::TypeAndOrName () : m_type_sp(), m_type_name()
806 TypeAndOrName::TypeAndOrName (TypeSP &in_type_sp) : m_type_sp(in_type_sp)
809 m_type_name = in_type_sp->GetName();
812 TypeAndOrName::TypeAndOrName (const char *in_type_str) : m_type_name(in_type_str)
816 TypeAndOrName::TypeAndOrName (const TypeAndOrName &rhs) : m_type_sp (rhs.m_type_sp), m_type_name (rhs.m_type_name)
821 TypeAndOrName::TypeAndOrName (ConstString &in_type_const_string) : m_type_name (in_type_const_string)
826 TypeAndOrName::operator= (const TypeAndOrName &rhs)
830 m_type_name = rhs.m_type_name;
831 m_type_sp = rhs.m_type_sp;
837 TypeAndOrName::operator==(const TypeAndOrName &other) const
839 if (m_type_sp != other.m_type_sp)
841 if (m_type_name != other.m_type_name)
847 TypeAndOrName::operator!=(const TypeAndOrName &other) const
849 if (m_type_sp != other.m_type_sp)
851 if (m_type_name != other.m_type_name)
857 TypeAndOrName::GetName () const
860 return m_type_sp->GetName();
866 TypeAndOrName::SetName (const ConstString &type_name)
868 m_type_name = type_name;
872 TypeAndOrName::SetName (const char *type_name_cstr)
874 m_type_name.SetCString (type_name_cstr);
878 TypeAndOrName::SetTypeSP (lldb::TypeSP type_sp)
882 m_type_name = type_sp->GetName();
886 TypeAndOrName::IsEmpty()
888 if (m_type_name || m_type_sp)
895 TypeAndOrName::Clear ()
902 TypeAndOrName::HasName ()
904 return (bool)m_type_name;
908 TypeAndOrName::HasTypeSP ()
910 return m_type_sp.get() != NULL;
913 TypeImpl::TypeImpl(const lldb_private::ClangASTType& clang_ast_type) :
914 m_clang_ast_type(clang_ast_type),
919 TypeImpl::TypeImpl(const lldb::TypeSP& type) :
920 m_clang_ast_type(type->GetClangForwardType()),
926 TypeImpl::SetType (const lldb::TypeSP &type_sp)
930 m_clang_ast_type = type_sp->GetClangForwardType();
935 m_clang_ast_type.Clear();
941 TypeImpl::operator = (const TypeImpl& rhs)
945 m_clang_ast_type = rhs.m_clang_ast_type;
946 m_type_sp = rhs.m_type_sp;
952 TypeImpl::GetASTContext()
957 return m_clang_ast_type.GetASTContext();
961 TypeImpl::GetOpaqueQualType()
966 return m_clang_ast_type.GetOpaqueQualType();
970 TypeImpl::GetDescription (lldb_private::Stream &strm,
971 lldb::DescriptionLevel description_level)
973 if (m_clang_ast_type.IsValid())
975 m_clang_ast_type.DumpTypeDescription (&strm);
979 strm.PutCString ("No value");
987 if (m_clang_ast_type.IsValid())
988 return m_clang_ast_type.GetConstTypeName();
989 return ConstString();