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