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