1 //===-- TypeMap.cpp --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
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"
24 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Support/raw_ostream.h"
27 #include "lldb/Symbol/SymbolFile.h"
28 #include "lldb/Symbol/SymbolVendor.h"
29 #include "lldb/Symbol/Type.h"
30 #include "lldb/Symbol/TypeMap.h"
33 using namespace lldb_private;
34 using namespace clang;
36 TypeMap::TypeMap() : m_types() {}
38 //----------------------------------------------------------------------
40 //----------------------------------------------------------------------
41 TypeMap::~TypeMap() {}
43 void TypeMap::Insert(const TypeSP &type_sp) {
44 // Just push each type on the back for now. We will worry about uniquing
47 m_types.insert(std::make_pair(type_sp->GetID(), type_sp));
50 bool TypeMap::InsertUnique(const TypeSP &type_sp) {
52 user_id_t type_uid = type_sp->GetID();
53 iterator pos, end = m_types.end();
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())
65 //----------------------------------------------------------------------
66 // Find a base type by its unique ID.
67 //----------------------------------------------------------------------
69 // TypeMap::FindType(lldb::user_id_t uid)
71 // iterator pos = m_types.find(uid);
72 // if (pos != m_types.end())
73 // return pos->second;
77 //----------------------------------------------------------------------
78 // Find a type by name.
79 //----------------------------------------------------------------------
81 // TypeMap::FindTypes (const ConstString &name)
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());
87 // for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
88 // if (pos->second->GetName() == name)
89 // types.Insert (pos->second);
93 void TypeMap::Clear() { m_types.clear(); }
95 uint32_t TypeMap::GetSize() const { return m_types.size(); }
97 bool TypeMap::Empty() const { return m_types.empty(); }
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...
103 TypeSP TypeMap::GetTypeAtIndex(uint32_t idx) {
106 for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
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))
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))
130 bool TypeMap::Remove(const lldb::TypeSP &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) {
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);
150 void TypeMap::RemoveMismatchedTypes(const char *qualified_typename,
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;
160 return RemoveMismatchedTypes(type_scope, type_basename, type_class,
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;
173 iterator pos, end = m_types.end();
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;
180 if (type_class != eTypeClassAny) {
181 match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
182 if ((match_type_class & type_class) == 0)
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,
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;
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::"
209 // match_type_scope "a::b::c::"
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] == ':') {
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;
231 matching_types.insert(*pos);
234 m_types.swap(matching_types);
237 void TypeMap::RemoveMismatchedTypes(TypeClass type_class) {
238 if (type_class == eTypeClassAny)
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;
247 iterator pos, end = m_types.end();
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);
256 m_types.swap(matching_types);