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