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 //===----------------------------------------------------------------------===//
12 #include "llvm/Support/FormattedStream.h"
13 #include "llvm/Support/raw_ostream.h"
15 #include "lldb/Symbol/SymbolFile.h"
16 #include "lldb/Symbol/SymbolVendor.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/TypeList.h"
21 using namespace lldb_private;
23 TypeList::TypeList() : m_types() {}
25 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 TypeList::~TypeList() {}
30 void TypeList::Insert(const TypeSP &type_sp) {
31 // Just push each type on the back for now. We will worry about uniquing
34 m_types.push_back(type_sp);
37 //----------------------------------------------------------------------
38 // Find a base type by its unique ID.
39 //----------------------------------------------------------------------
41 // TypeList::FindType(lldb::user_id_t uid)
43 // iterator pos = m_types.find(uid);
44 // if (pos != m_types.end())
45 // return pos->second;
49 //----------------------------------------------------------------------
50 // Find a type by name.
51 //----------------------------------------------------------------------
53 // TypeList::FindTypes (const ConstString &name)
55 // // Do we ever need to make a lookup by name map? Here we are doing
56 // // a linear search which isn't going to be fast.
57 // TypeList types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
59 // for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
60 // if (pos->second->GetName() == name)
61 // types.Insert (pos->second);
65 void TypeList::Clear() { m_types.clear(); }
67 uint32_t TypeList::GetSize() const { return m_types.size(); }
69 // GetTypeAtIndex isn't used a lot for large type lists, currently only for
70 // type lists that are returned for "image dump -t TYPENAME" commands and other
71 // simple symbol queries that grab the first result...
73 TypeSP TypeList::GetTypeAtIndex(uint32_t idx) {
76 assert(i < GetSize() && "Accessing past the end of a TypeList");
77 for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
85 void TypeList::ForEach(
86 std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
87 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
93 void TypeList::ForEach(
94 std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
95 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
101 void TypeList::Dump(Stream *s, bool show_context) {
102 for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
103 pos->get()->Dump(s, show_context);
107 void TypeList::RemoveMismatchedTypes(const char *qualified_typename,
109 llvm::StringRef type_scope;
110 llvm::StringRef type_basename;
111 TypeClass type_class = eTypeClassAny;
112 if (!Type::GetTypeScopeAndBasename(qualified_typename, type_scope,
113 type_basename, type_class)) {
114 type_basename = qualified_typename;
117 return RemoveMismatchedTypes(type_scope, type_basename, type_class,
121 void TypeList::RemoveMismatchedTypes(const std::string &type_scope,
122 const std::string &type_basename,
123 TypeClass type_class, bool exact_match) {
124 // Our "collection" type currently is a std::map which doesn't have any good
125 // way to iterate and remove items from the map so we currently just make a
126 // new list and add all of the matching types to it, and then swap it into
127 // m_types at the end
128 collection matching_types;
130 iterator pos, end = m_types.end();
132 for (pos = m_types.begin(); pos != end; ++pos) {
133 Type *the_type = pos->get();
134 bool keep_match = false;
135 TypeClass match_type_class = eTypeClassAny;
137 if (type_class != eTypeClassAny) {
138 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
139 if ((match_type_class & type_class) == 0)
143 ConstString match_type_name_const_str(the_type->GetQualifiedName());
144 if (match_type_name_const_str) {
145 const char *match_type_name = match_type_name_const_str.GetCString();
146 llvm::StringRef match_type_scope;
147 llvm::StringRef match_type_basename;
148 if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
151 if (match_type_basename == type_basename) {
152 const size_t type_scope_size = type_scope.size();
153 const size_t match_type_scope_size = match_type_scope.size();
154 if (exact_match || (type_scope_size == match_type_scope_size)) {
155 keep_match = match_type_scope == type_scope;
157 if (match_type_scope_size > type_scope_size) {
158 const size_t type_scope_pos = match_type_scope.rfind(type_scope);
159 if (type_scope_pos == match_type_scope_size - type_scope_size) {
160 if (type_scope_pos >= 2) {
161 // Our match scope ends with the type scope we were looking
162 // for, but we need to make sure what comes before the
163 // matching type scope is a namespace boundary in case we are
164 // trying to match: type_basename = "d" type_scope = "b::c::"
166 // match_type_scope "a::b::c::"
168 // match_type_scope "a::bb::c::"
169 // So below we make sure what comes before "b::c::" in
170 // match_type_scope is "::", or the namespace boundary
171 if (match_type_scope[type_scope_pos - 1] == ':' &&
172 match_type_scope[type_scope_pos - 2] == ':') {
181 // The type we are currently looking at doesn't exists in a namespace
182 // or class, so it only matches if there is no type scope...
183 keep_match = type_scope.empty() && type_basename == match_type_name;
188 matching_types.push_back(*pos);
191 m_types.swap(matching_types);
194 void TypeList::RemoveMismatchedTypes(TypeClass type_class) {
195 if (type_class == eTypeClassAny)
198 // Our "collection" type currently is a std::map which doesn't have any good
199 // way to iterate and remove items from the map so we currently just make a
200 // new list and add all of the matching types to it, and then swap it into
201 // m_types at the end
202 collection matching_types;
204 iterator pos, end = m_types.end();
206 for (pos = m_types.begin(); pos != end; ++pos) {
207 Type *the_type = pos->get();
208 TypeClass match_type_class =
209 the_type->GetForwardCompilerType().GetTypeClass();
210 if (match_type_class & type_class)
211 matching_types.push_back(*pos);
213 m_types.swap(matching_types);