1 //===-- TypeList.cpp --------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <vector>
11
12 #include "llvm/Support/FormattedStream.h"
13 #include "llvm/Support/raw_ostream.h"
14
15 #include "lldb/Symbol/SymbolFile.h"
16 #include "lldb/Symbol/SymbolVendor.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/TypeList.h"
19
20 using namespace lldb;
21 using namespace lldb_private;
22
TypeList()23 TypeList::TypeList() : m_types() {}
24
25 //----------------------------------------------------------------------
26 // Destructor
27 //----------------------------------------------------------------------
~TypeList()28 TypeList::~TypeList() {}
29
Insert(const TypeSP & type_sp)30 void TypeList::Insert(const TypeSP &type_sp) {
31 // Just push each type on the back for now. We will worry about uniquing
32 // later
33 if (type_sp)
34 m_types.push_back(type_sp);
35 }
36
37 //----------------------------------------------------------------------
38 // Find a base type by its unique ID.
39 //----------------------------------------------------------------------
40 // TypeSP
41 // TypeList::FindType(lldb::user_id_t uid)
42 //{
43 // iterator pos = m_types.find(uid);
44 // if (pos != m_types.end())
45 // return pos->second;
46 // return TypeSP();
47 //}
48
49 //----------------------------------------------------------------------
50 // Find a type by name.
51 //----------------------------------------------------------------------
52 // TypeList
53 // TypeList::FindTypes (const ConstString &name)
54 //{
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());
58 // iterator pos, end;
59 // for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
60 // if (pos->second->GetName() == name)
61 // types.Insert (pos->second);
62 // return types;
63 //}
64
Clear()65 void TypeList::Clear() { m_types.clear(); }
66
GetSize() const67 uint32_t TypeList::GetSize() const { return m_types.size(); }
68
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...
72
GetTypeAtIndex(uint32_t idx)73 TypeSP TypeList::GetTypeAtIndex(uint32_t idx) {
74 iterator pos, end;
75 uint32_t i = 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) {
78 if (i == 0)
79 return *pos;
80 --i;
81 }
82 return TypeSP();
83 }
84
ForEach(std::function<bool (const lldb::TypeSP & type_sp)> const & callback) const85 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) {
88 if (!callback(*pos))
89 break;
90 }
91 }
92
ForEach(std::function<bool (lldb::TypeSP & type_sp)> const & callback)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) {
96 if (!callback(*pos))
97 break;
98 }
99 }
100
Dump(Stream * s,bool show_context)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);
104 }
105 }
106
RemoveMismatchedTypes(const char * qualified_typename,bool exact_match)107 void TypeList::RemoveMismatchedTypes(const char *qualified_typename,
108 bool exact_match) {
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;
115 type_scope = "";
116 }
117 return RemoveMismatchedTypes(type_scope, type_basename, type_class,
118 exact_match);
119 }
120
RemoveMismatchedTypes(const std::string & type_scope,const std::string & type_basename,TypeClass type_class,bool exact_match)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;
129
130 iterator pos, end = m_types.end();
131
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;
136
137 if (type_class != eTypeClassAny) {
138 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
139 if ((match_type_class & type_class) == 0)
140 continue;
141 }
142
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,
149 match_type_basename,
150 match_type_class)) {
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;
156 } else {
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::"
165 // We want to match:
166 // match_type_scope "a::b::c::"
167 // But not:
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] == ':') {
173 keep_match = true;
174 }
175 }
176 }
177 }
178 }
179 }
180 } else {
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;
184 }
185 }
186
187 if (keep_match) {
188 matching_types.push_back(*pos);
189 }
190 }
191 m_types.swap(matching_types);
192 }
193
RemoveMismatchedTypes(TypeClass type_class)194 void TypeList::RemoveMismatchedTypes(TypeClass type_class) {
195 if (type_class == eTypeClassAny)
196 return;
197
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;
203
204 iterator pos, end = m_types.end();
205
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);
212 }
213 m_types.swap(matching_types);
214 }
215