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