1 //===-- TypeList.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 //===----------------------------------------------------------------------===//
14 // Other libraries and framework includes
15 #include "llvm/Support/FormattedStream.h"
16 #include "llvm/Support/raw_ostream.h"
19 #include "lldb/Symbol/SymbolFile.h"
20 #include "lldb/Symbol/SymbolVendor.h"
21 #include "lldb/Symbol/Type.h"
22 #include "lldb/Symbol/TypeList.h"
25 using namespace lldb_private;
27 TypeList::TypeList() : m_types() {}
29 //----------------------------------------------------------------------
31 //----------------------------------------------------------------------
32 TypeList::~TypeList() {}
34 void TypeList::Insert(const TypeSP &type_sp) {
35 // Just push each type on the back for now. We will worry about uniquing later
37 m_types.push_back(type_sp);
40 //----------------------------------------------------------------------
41 // Find a base type by its unique ID.
42 //----------------------------------------------------------------------
44 // TypeList::FindType(lldb::user_id_t uid)
46 // iterator pos = m_types.find(uid);
47 // if (pos != m_types.end())
48 // return pos->second;
52 //----------------------------------------------------------------------
53 // Find a type by name.
54 //----------------------------------------------------------------------
56 // TypeList::FindTypes (const ConstString &name)
58 // // Do we ever need to make a lookup by name map? Here we are doing
59 // // a linear search which isn't going to be fast.
60 // TypeList types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
62 // for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
63 // if (pos->second->GetName() == name)
64 // types.Insert (pos->second);
68 void TypeList::Clear() { m_types.clear(); }
70 uint32_t TypeList::GetSize() const { return m_types.size(); }
72 // GetTypeAtIndex isn't used a lot for large type lists, currently only for
73 // type lists that are returned for "image dump -t TYPENAME" commands and other
74 // simple symbol queries that grab the first result...
76 TypeSP TypeList::GetTypeAtIndex(uint32_t idx) {
79 for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
87 void TypeList::ForEach(
88 std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
89 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
95 void TypeList::ForEach(
96 std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
97 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
103 void TypeList::Dump(Stream *s, bool show_context) {
104 for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
105 pos->get()->Dump(s, show_context);
109 void TypeList::RemoveMismatchedTypes(const char *qualified_typename,
111 llvm::StringRef type_scope;
112 llvm::StringRef type_basename;
113 TypeClass type_class = eTypeClassAny;
114 if (!Type::GetTypeScopeAndBasename(qualified_typename, type_scope,
115 type_basename, type_class)) {
116 type_basename = qualified_typename;
119 return RemoveMismatchedTypes(type_scope, type_basename, type_class,
123 void TypeList::RemoveMismatchedTypes(const std::string &type_scope,
124 const std::string &type_basename,
125 TypeClass type_class, bool exact_match) {
126 // Our "collection" type currently is a std::map which doesn't
127 // have any good way to iterate and remove items from the map
128 // so we currently just make a new list and add all of the matching
129 // types to it, and then swap it into m_types at the end
130 collection matching_types;
132 iterator pos, end = m_types.end();
134 for (pos = m_types.begin(); pos != end; ++pos) {
135 Type *the_type = pos->get();
136 bool keep_match = false;
137 TypeClass match_type_class = eTypeClassAny;
139 if (type_class != eTypeClassAny) {
140 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
141 if ((match_type_class & type_class) == 0)
145 ConstString match_type_name_const_str(the_type->GetQualifiedName());
146 if (match_type_name_const_str) {
147 const char *match_type_name = match_type_name_const_str.GetCString();
148 llvm::StringRef match_type_scope;
149 llvm::StringRef match_type_basename;
150 if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
153 if (match_type_basename == type_basename) {
154 const size_t type_scope_size = type_scope.size();
155 const size_t match_type_scope_size = match_type_scope.size();
156 if (exact_match || (type_scope_size == match_type_scope_size)) {
157 keep_match = match_type_scope == type_scope;
159 if (match_type_scope_size > type_scope_size) {
160 const size_t type_scope_pos = match_type_scope.rfind(type_scope);
161 if (type_scope_pos == match_type_scope_size - type_scope_size) {
162 if (type_scope_pos >= 2) {
163 // Our match scope ends with the type scope we were looking
165 // but we need to make sure what comes before the matching
166 // type scope is a namespace boundary in case we are trying to
168 // type_basename = "d"
169 // type_scope = "b::c::"
171 // match_type_scope "a::b::c::"
173 // match_type_scope "a::bb::c::"
174 // So below we make sure what comes before "b::c::" in
176 // is "::", or the namespace boundary
177 if (match_type_scope[type_scope_pos - 1] == ':' &&
178 match_type_scope[type_scope_pos - 2] == ':') {
187 // The type we are currently looking at doesn't exists
188 // in a namespace or class, so it only matches if there
189 // is no type scope...
191 type_scope.empty() && type_basename.compare(match_type_name) == 0;
196 matching_types.push_back(*pos);
199 m_types.swap(matching_types);
202 void TypeList::RemoveMismatchedTypes(TypeClass type_class) {
203 if (type_class == eTypeClassAny)
206 // Our "collection" type currently is a std::map which doesn't
207 // have any good way to iterate and remove items from the map
208 // so we currently just make a new list and add all of the matching
209 // types to it, and then swap it into m_types at the end
210 collection matching_types;
212 iterator pos, end = m_types.end();
214 for (pos = m_types.begin(); pos != end; ++pos) {
215 Type *the_type = pos->get();
216 TypeClass match_type_class =
217 the_type->GetForwardCompilerType().GetTypeClass();
218 if (match_type_class & type_class)
219 matching_types.push_back(*pos);
221 m_types.swap(matching_types);