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
38 m_types.push_back(type_sp);
41 //----------------------------------------------------------------------
42 // Find a base type by its unique ID.
43 //----------------------------------------------------------------------
45 // TypeList::FindType(lldb::user_id_t uid)
47 // iterator pos = m_types.find(uid);
48 // if (pos != m_types.end())
49 // return pos->second;
53 //----------------------------------------------------------------------
54 // Find a type by name.
55 //----------------------------------------------------------------------
57 // TypeList::FindTypes (const ConstString &name)
59 // // Do we ever need to make a lookup by name map? Here we are doing
60 // // a linear search which isn't going to be fast.
61 // TypeList types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
63 // for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
64 // if (pos->second->GetName() == name)
65 // types.Insert (pos->second);
69 void TypeList::Clear() { m_types.clear(); }
71 uint32_t TypeList::GetSize() const { return m_types.size(); }
73 // GetTypeAtIndex isn't used a lot for large type lists, currently only for
74 // type lists that are returned for "image dump -t TYPENAME" commands and other
75 // simple symbol queries that grab the first result...
77 TypeSP TypeList::GetTypeAtIndex(uint32_t idx) {
80 assert(i < GetSize() && "Accessing past the end of a TypeList");
81 for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
89 void TypeList::ForEach(
90 std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
91 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
97 void TypeList::ForEach(
98 std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
99 for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
105 void TypeList::Dump(Stream *s, bool show_context) {
106 for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
107 pos->get()->Dump(s, show_context);
111 void TypeList::RemoveMismatchedTypes(const char *qualified_typename,
113 llvm::StringRef type_scope;
114 llvm::StringRef type_basename;
115 TypeClass type_class = eTypeClassAny;
116 if (!Type::GetTypeScopeAndBasename(qualified_typename, type_scope,
117 type_basename, type_class)) {
118 type_basename = qualified_typename;
121 return RemoveMismatchedTypes(type_scope, type_basename, type_class,
125 void TypeList::RemoveMismatchedTypes(const std::string &type_scope,
126 const std::string &type_basename,
127 TypeClass type_class, bool exact_match) {
128 // Our "collection" type currently is a std::map which doesn't have any good
129 // way to iterate and remove items from the map so we currently just make a
130 // new list and add all of the matching types to it, and then swap it into
131 // m_types at the end
132 collection matching_types;
134 iterator pos, end = m_types.end();
136 for (pos = m_types.begin(); pos != end; ++pos) {
137 Type *the_type = pos->get();
138 bool keep_match = false;
139 TypeClass match_type_class = eTypeClassAny;
141 if (type_class != eTypeClassAny) {
142 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
143 if ((match_type_class & type_class) == 0)
147 ConstString match_type_name_const_str(the_type->GetQualifiedName());
148 if (match_type_name_const_str) {
149 const char *match_type_name = match_type_name_const_str.GetCString();
150 llvm::StringRef match_type_scope;
151 llvm::StringRef match_type_basename;
152 if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
155 if (match_type_basename == type_basename) {
156 const size_t type_scope_size = type_scope.size();
157 const size_t match_type_scope_size = match_type_scope.size();
158 if (exact_match || (type_scope_size == match_type_scope_size)) {
159 keep_match = match_type_scope == type_scope;
161 if (match_type_scope_size > type_scope_size) {
162 const size_t type_scope_pos = match_type_scope.rfind(type_scope);
163 if (type_scope_pos == match_type_scope_size - type_scope_size) {
164 if (type_scope_pos >= 2) {
165 // Our match scope ends with the type scope we were looking
166 // for, but we need to make sure what comes before the
167 // matching type scope is a namespace boundary in case we are
168 // trying to match: type_basename = "d" type_scope = "b::c::"
170 // match_type_scope "a::b::c::"
172 // match_type_scope "a::bb::c::"
173 // So below we make sure what comes before "b::c::" in
174 // match_type_scope is "::", or the namespace boundary
175 if (match_type_scope[type_scope_pos - 1] == ':' &&
176 match_type_scope[type_scope_pos - 2] == ':') {
185 // The type we are currently looking at doesn't exists in a namespace
186 // or class, so it only matches if there is no type scope...
188 type_scope.empty() && type_basename.compare(match_type_name) == 0;
193 matching_types.push_back(*pos);
196 m_types.swap(matching_types);
199 void TypeList::RemoveMismatchedTypes(TypeClass type_class) {
200 if (type_class == eTypeClassAny)
203 // Our "collection" type currently is a std::map which doesn't have any good
204 // way to iterate and remove items from the map so we currently just make a
205 // new list and add all of the matching types to it, and then swap it into
206 // m_types at the end
207 collection matching_types;
209 iterator pos, end = m_types.end();
211 for (pos = m_types.begin(); pos != end; ++pos) {
212 Type *the_type = pos->get();
213 TypeClass match_type_class =
214 the_type->GetForwardCompilerType().GetTypeClass();
215 if (match_type_class & type_class)
216 matching_types.push_back(*pos);
218 m_types.swap(matching_types);