1 //===-- TypeMap.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <vector>
10 
11 #include "llvm/Support/FormattedStream.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 #include "lldb/Symbol/SymbolFile.h"
15 #include "lldb/Symbol/SymbolVendor.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Symbol/TypeMap.h"
18 
19 using namespace lldb;
20 using namespace lldb_private;
21 
TypeMap()22 TypeMap::TypeMap() : m_types() {}
23 
24 // Destructor
~TypeMap()25 TypeMap::~TypeMap() {}
26 
Insert(const TypeSP & type_sp)27 void TypeMap::Insert(const TypeSP &type_sp) {
28   // Just push each type on the back for now. We will worry about uniquing
29   // later
30   if (type_sp)
31     m_types.insert(std::make_pair(type_sp->GetID(), type_sp));
32 }
33 
InsertUnique(const TypeSP & type_sp)34 bool TypeMap::InsertUnique(const TypeSP &type_sp) {
35   if (type_sp) {
36     user_id_t type_uid = type_sp->GetID();
37     iterator pos, end = m_types.end();
38 
39     for (pos = m_types.find(type_uid);
40          pos != end && pos->second->GetID() == type_uid; ++pos) {
41       if (pos->second.get() == type_sp.get())
42         return false;
43     }
44     Insert(type_sp);
45   }
46   return true;
47 }
48 
49 // Find a base type by its unique ID.
50 // TypeSP
51 // TypeMap::FindType(lldb::user_id_t uid)
52 //{
53 //    iterator pos = m_types.find(uid);
54 //    if (pos != m_types.end())
55 //        return pos->second;
56 //    return TypeSP();
57 //}
58 
59 // Find a type by name.
60 // TypeMap
61 // TypeMap::FindTypes (ConstString name)
62 //{
63 //    // Do we ever need to make a lookup by name map? Here we are doing
64 //    // a linear search which isn't going to be fast.
65 //    TypeMap types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
66 //    iterator pos, end;
67 //    for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
68 //        if (pos->second->GetName() == name)
69 //            types.Insert (pos->second);
70 //    return types;
71 //}
72 
Clear()73 void TypeMap::Clear() { m_types.clear(); }
74 
GetSize() const75 uint32_t TypeMap::GetSize() const { return m_types.size(); }
76 
Empty() const77 bool TypeMap::Empty() const { return m_types.empty(); }
78 
79 // GetTypeAtIndex isn't used a lot for large type lists, currently only for
80 // type lists that are returned for "image dump -t TYPENAME" commands and other
81 // simple symbol queries that grab the first result...
82 
GetTypeAtIndex(uint32_t idx)83 TypeSP TypeMap::GetTypeAtIndex(uint32_t idx) {
84   iterator pos, end;
85   uint32_t i = idx;
86   for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
87     if (i == 0)
88       return pos->second;
89     --i;
90   }
91   return TypeSP();
92 }
93 
ForEach(std::function<bool (const lldb::TypeSP & type_sp)> const & callback) const94 void TypeMap::ForEach(
95     std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
96   for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
97     if (!callback(pos->second))
98       break;
99   }
100 }
101 
ForEach(std::function<bool (lldb::TypeSP & type_sp)> const & callback)102 void TypeMap::ForEach(
103     std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
104   for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
105     if (!callback(pos->second))
106       break;
107   }
108 }
109 
Remove(const lldb::TypeSP & type_sp)110 bool TypeMap::Remove(const lldb::TypeSP &type_sp) {
111   if (type_sp) {
112     lldb::user_id_t uid = type_sp->GetID();
113     for (iterator pos = m_types.find(uid), end = m_types.end();
114          pos != end && pos->first == uid; ++pos) {
115       if (pos->second == type_sp) {
116         m_types.erase(pos);
117         return true;
118       }
119     }
120   }
121   return false;
122 }
123 
Dump(Stream * s,bool show_context,lldb::DescriptionLevel level)124 void TypeMap::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
125   for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
126     pos->second->Dump(s, show_context, level);
127   }
128 }
129 
RemoveMismatchedTypes(const char * qualified_typename,bool exact_match)130 void TypeMap::RemoveMismatchedTypes(const char *qualified_typename,
131                                     bool exact_match) {
132   llvm::StringRef type_scope;
133   llvm::StringRef type_basename;
134   TypeClass type_class = eTypeClassAny;
135   if (!Type::GetTypeScopeAndBasename(qualified_typename, type_scope,
136                                      type_basename, type_class)) {
137     type_basename = qualified_typename;
138     type_scope = "";
139   }
140   return RemoveMismatchedTypes(std::string(type_scope),
141                                std::string(type_basename), type_class,
142                                exact_match);
143 }
144 
RemoveMismatchedTypes(const std::string & type_scope,const std::string & type_basename,TypeClass type_class,bool exact_match)145 void TypeMap::RemoveMismatchedTypes(const std::string &type_scope,
146                                     const std::string &type_basename,
147                                     TypeClass type_class, bool exact_match) {
148   // Our "collection" type currently is a std::map which doesn't have any good
149   // way to iterate and remove items from the map so we currently just make a
150   // new list and add all of the matching types to it, and then swap it into
151   // m_types at the end
152   collection matching_types;
153 
154   iterator pos, end = m_types.end();
155 
156   for (pos = m_types.begin(); pos != end; ++pos) {
157     Type *the_type = pos->second.get();
158     bool keep_match = false;
159     TypeClass match_type_class = eTypeClassAny;
160 
161     if (type_class != eTypeClassAny) {
162       match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
163       if ((match_type_class & type_class) == 0)
164         continue;
165     }
166 
167     ConstString match_type_name_const_str(the_type->GetQualifiedName());
168     if (match_type_name_const_str) {
169       const char *match_type_name = match_type_name_const_str.GetCString();
170       llvm::StringRef match_type_scope;
171       llvm::StringRef match_type_basename;
172       if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
173                                         match_type_basename,
174                                         match_type_class)) {
175         if (match_type_basename == type_basename) {
176           const size_t type_scope_size = type_scope.size();
177           const size_t match_type_scope_size = match_type_scope.size();
178           if (exact_match || (type_scope_size == match_type_scope_size)) {
179             keep_match = match_type_scope == type_scope;
180           } else {
181             if (match_type_scope_size > type_scope_size) {
182               const size_t type_scope_pos = match_type_scope.rfind(type_scope);
183               if (type_scope_pos == match_type_scope_size - type_scope_size) {
184                 if (type_scope_pos >= 2) {
185                   // Our match scope ends with the type scope we were looking
186                   // for, but we need to make sure what comes before the
187                   // matching type scope is a namespace boundary in case we are
188                   // trying to match: type_basename = "d" type_scope = "b::c::"
189                   // We want to match:
190                   //  match_type_scope "a::b::c::"
191                   // But not:
192                   //  match_type_scope "a::bb::c::"
193                   // So below we make sure what comes before "b::c::" in
194                   // match_type_scope is "::", or the namespace boundary
195                   if (match_type_scope[type_scope_pos - 1] == ':' &&
196                       match_type_scope[type_scope_pos - 2] == ':') {
197                     keep_match = true;
198                   }
199                 }
200               }
201             }
202           }
203         }
204       } else {
205         // The type we are currently looking at doesn't exists in a namespace
206         // or class, so it only matches if there is no type scope...
207         keep_match = type_scope.empty() && type_basename == match_type_name;
208       }
209     }
210 
211     if (keep_match) {
212       matching_types.insert(*pos);
213     }
214   }
215   m_types.swap(matching_types);
216 }
217 
RemoveMismatchedTypes(TypeClass type_class)218 void TypeMap::RemoveMismatchedTypes(TypeClass type_class) {
219   if (type_class == eTypeClassAny)
220     return;
221 
222   // Our "collection" type currently is a std::map which doesn't have any good
223   // way to iterate and remove items from the map so we currently just make a
224   // new list and add all of the matching types to it, and then swap it into
225   // m_types at the end
226   collection matching_types;
227 
228   iterator pos, end = m_types.end();
229 
230   for (pos = m_types.begin(); pos != end; ++pos) {
231     Type *the_type = pos->second.get();
232     TypeClass match_type_class =
233         the_type->GetForwardCompilerType().GetTypeClass();
234     if (match_type_class & type_class)
235       matching_types.insert(*pos);
236   }
237   m_types.swap(matching_types);
238 }
239