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