#include <vector>
#include "llvm/Support/FormattedStream.h"
#include "llvm/Support/raw_ostream.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/TypeMap.h"
using namespace lldb;
using namespace lldb_private;
TypeMap::TypeMap() : m_types() {}
TypeMap::~TypeMap() = default;
void TypeMap::Insert(const TypeSP &type_sp) {
if (type_sp)
m_types.insert(std::make_pair(type_sp->GetID(), type_sp));
}
bool TypeMap::InsertUnique(const TypeSP &type_sp) {
if (type_sp) {
user_id_t type_uid = type_sp->GetID();
iterator pos, end = m_types.end();
for (pos = m_types.find(type_uid);
pos != end && pos->second->GetID() == type_uid; ++pos) {
if (pos->second.get() == type_sp.get())
return false;
}
Insert(type_sp);
}
return true;
}
void TypeMap::Clear() { m_types.clear(); }
uint32_t TypeMap::GetSize() const { return m_types.size(); }
bool TypeMap::Empty() const { return m_types.empty(); }
TypeSP TypeMap::GetTypeAtIndex(uint32_t idx) {
iterator pos, end;
uint32_t i = idx;
for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
if (i == 0)
return pos->second;
--i;
}
return TypeSP();
}
void TypeMap::ForEach(
std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
if (!callback(pos->second))
break;
}
}
void TypeMap::ForEach(
std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
if (!callback(pos->second))
break;
}
}
bool TypeMap::Remove(const lldb::TypeSP &type_sp) {
if (type_sp) {
lldb::user_id_t uid = type_sp->GetID();
for (iterator pos = m_types.find(uid), end = m_types.end();
pos != end && pos->first == uid; ++pos) {
if (pos->second == type_sp) {
m_types.erase(pos);
return true;
}
}
}
return false;
}
void TypeMap::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
pos->second->Dump(s, show_context, level);
}
}
void TypeMap::RemoveMismatchedTypes(llvm::StringRef type_scope,
llvm::StringRef type_basename,
TypeClass type_class, bool exact_match) {
collection matching_types;
iterator pos, end = m_types.end();
for (pos = m_types.begin(); pos != end; ++pos) {
Type *the_type = pos->second.get();
bool keep_match = false;
TypeClass match_type_class = eTypeClassAny;
if (type_class != eTypeClassAny) {
match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
if ((match_type_class & type_class) == 0)
continue;
}
ConstString match_type_name_const_str(the_type->GetQualifiedName());
if (match_type_name_const_str) {
const char *match_type_name = match_type_name_const_str.GetCString();
llvm::StringRef match_type_scope;
llvm::StringRef match_type_basename;
if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
match_type_basename,
match_type_class)) {
if (match_type_basename == type_basename) {
const size_t type_scope_size = type_scope.size();
const size_t match_type_scope_size = match_type_scope.size();
if (exact_match || (type_scope_size == match_type_scope_size)) {
keep_match = match_type_scope == type_scope;
} else {
if (match_type_scope_size > type_scope_size) {
const size_t type_scope_pos = match_type_scope.rfind(type_scope);
if (type_scope_pos == match_type_scope_size - type_scope_size) {
if (type_scope_pos >= 2) {
if (match_type_scope[type_scope_pos - 1] == ':' &&
match_type_scope[type_scope_pos - 2] == ':') {
keep_match = true;
}
}
}
}
}
}
} else {
keep_match = type_scope.empty() && type_basename == match_type_name;
}
}
if (keep_match) {
matching_types.insert(*pos);
}
}
m_types.swap(matching_types);
}