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