180814287SRaphael Isemann //===-- TypeMap.cpp -------------------------------------------------------===//
24069730cSRavitheja Addepally //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
64069730cSRavitheja Addepally //
74069730cSRavitheja Addepally //===----------------------------------------------------------------------===//
84069730cSRavitheja Addepally 
94069730cSRavitheja Addepally #include <vector>
104069730cSRavitheja Addepally 
114069730cSRavitheja Addepally #include "llvm/Support/FormattedStream.h"
124069730cSRavitheja Addepally #include "llvm/Support/raw_ostream.h"
134069730cSRavitheja Addepally 
144069730cSRavitheja Addepally #include "lldb/Symbol/SymbolFile.h"
154069730cSRavitheja Addepally #include "lldb/Symbol/SymbolVendor.h"
164069730cSRavitheja Addepally #include "lldb/Symbol/Type.h"
174069730cSRavitheja Addepally #include "lldb/Symbol/TypeMap.h"
184069730cSRavitheja Addepally 
194069730cSRavitheja Addepally using namespace lldb;
204069730cSRavitheja Addepally using namespace lldb_private;
214069730cSRavitheja Addepally 
TypeMap()22b9c1b51eSKate Stone TypeMap::TypeMap() : m_types() {}
234069730cSRavitheja Addepally 
244069730cSRavitheja Addepally // Destructor
25fd2433e1SJonas Devlieghere TypeMap::~TypeMap() = default;
264069730cSRavitheja Addepally 
Insert(const TypeSP & type_sp)27b9c1b51eSKate Stone void TypeMap::Insert(const TypeSP &type_sp) {
2805097246SAdrian Prantl   // Just push each type on the back for now. We will worry about uniquing
2905097246SAdrian Prantl   // later
304069730cSRavitheja Addepally   if (type_sp)
314069730cSRavitheja Addepally     m_types.insert(std::make_pair(type_sp->GetID(), type_sp));
324069730cSRavitheja Addepally }
334069730cSRavitheja Addepally 
InsertUnique(const TypeSP & type_sp)34b9c1b51eSKate Stone bool TypeMap::InsertUnique(const TypeSP &type_sp) {
35b9c1b51eSKate Stone   if (type_sp) {
364069730cSRavitheja Addepally     user_id_t type_uid = type_sp->GetID();
374069730cSRavitheja Addepally     iterator pos, end = m_types.end();
384069730cSRavitheja Addepally 
39b9c1b51eSKate Stone     for (pos = m_types.find(type_uid);
40b9c1b51eSKate Stone          pos != end && pos->second->GetID() == type_uid; ++pos) {
414069730cSRavitheja Addepally       if (pos->second.get() == type_sp.get())
424069730cSRavitheja Addepally         return false;
434069730cSRavitheja Addepally     }
444069730cSRavitheja Addepally     Insert(type_sp);
459d472121SGreg Clayton   }
464069730cSRavitheja Addepally   return true;
474069730cSRavitheja Addepally }
484069730cSRavitheja Addepally 
494069730cSRavitheja Addepally // Find a base type by its unique ID.
504069730cSRavitheja Addepally // TypeSP
514069730cSRavitheja Addepally // TypeMap::FindType(lldb::user_id_t uid)
524069730cSRavitheja Addepally //{
534069730cSRavitheja Addepally //    iterator pos = m_types.find(uid);
544069730cSRavitheja Addepally //    if (pos != m_types.end())
554069730cSRavitheja Addepally //        return pos->second;
564069730cSRavitheja Addepally //    return TypeSP();
574069730cSRavitheja Addepally //}
584069730cSRavitheja Addepally 
594069730cSRavitheja Addepally // Find a type by name.
604069730cSRavitheja Addepally // TypeMap
610e4c4821SAdrian Prantl // TypeMap::FindTypes (ConstString name)
624069730cSRavitheja Addepally //{
634069730cSRavitheja Addepally //    // Do we ever need to make a lookup by name map? Here we are doing
644069730cSRavitheja Addepally //    // a linear search which isn't going to be fast.
654069730cSRavitheja Addepally //    TypeMap types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
664069730cSRavitheja Addepally //    iterator pos, end;
674069730cSRavitheja Addepally //    for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
684069730cSRavitheja Addepally //        if (pos->second->GetName() == name)
694069730cSRavitheja Addepally //            types.Insert (pos->second);
704069730cSRavitheja Addepally //    return types;
714069730cSRavitheja Addepally //}
724069730cSRavitheja Addepally 
Clear()73b9c1b51eSKate Stone void TypeMap::Clear() { m_types.clear(); }
744069730cSRavitheja Addepally 
GetSize() const75b9c1b51eSKate Stone uint32_t TypeMap::GetSize() const { return m_types.size(); }
764069730cSRavitheja Addepally 
Empty() const77b9c1b51eSKate Stone bool TypeMap::Empty() const { return m_types.empty(); }
784069730cSRavitheja Addepally 
794069730cSRavitheja Addepally // GetTypeAtIndex isn't used a lot for large type lists, currently only for
804069730cSRavitheja Addepally // type lists that are returned for "image dump -t TYPENAME" commands and other
814069730cSRavitheja Addepally // simple symbol queries that grab the first result...
824069730cSRavitheja Addepally 
GetTypeAtIndex(uint32_t idx)83b9c1b51eSKate Stone TypeSP TypeMap::GetTypeAtIndex(uint32_t idx) {
844069730cSRavitheja Addepally   iterator pos, end;
854069730cSRavitheja Addepally   uint32_t i = idx;
86b9c1b51eSKate Stone   for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
874069730cSRavitheja Addepally     if (i == 0)
884069730cSRavitheja Addepally       return pos->second;
894069730cSRavitheja Addepally     --i;
904069730cSRavitheja Addepally   }
914069730cSRavitheja Addepally   return TypeSP();
924069730cSRavitheja Addepally }
934069730cSRavitheja Addepally 
ForEach(std::function<bool (const lldb::TypeSP & type_sp)> const & callback) const94b9c1b51eSKate Stone void TypeMap::ForEach(
95b9c1b51eSKate Stone     std::function<bool(const lldb::TypeSP &type_sp)> const &callback) const {
96b9c1b51eSKate Stone   for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
974069730cSRavitheja Addepally     if (!callback(pos->second))
984069730cSRavitheja Addepally       break;
994069730cSRavitheja Addepally   }
1004069730cSRavitheja Addepally }
1014069730cSRavitheja Addepally 
ForEach(std::function<bool (lldb::TypeSP & type_sp)> const & callback)102b9c1b51eSKate Stone void TypeMap::ForEach(
103b9c1b51eSKate Stone     std::function<bool(lldb::TypeSP &type_sp)> const &callback) {
104b9c1b51eSKate Stone   for (auto pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
1054069730cSRavitheja Addepally     if (!callback(pos->second))
1064069730cSRavitheja Addepally       break;
1074069730cSRavitheja Addepally   }
1084069730cSRavitheja Addepally }
1094069730cSRavitheja Addepally 
Remove(const lldb::TypeSP & type_sp)110b9c1b51eSKate Stone bool TypeMap::Remove(const lldb::TypeSP &type_sp) {
111b9c1b51eSKate Stone   if (type_sp) {
1129d472121SGreg Clayton     lldb::user_id_t uid = type_sp->GetID();
113b9c1b51eSKate Stone     for (iterator pos = m_types.find(uid), end = m_types.end();
114b9c1b51eSKate Stone          pos != end && pos->first == uid; ++pos) {
115b9c1b51eSKate Stone       if (pos->second == type_sp) {
1164069730cSRavitheja Addepally         m_types.erase(pos);
1174069730cSRavitheja Addepally         return true;
1184069730cSRavitheja Addepally       }
1199d472121SGreg Clayton     }
1209d472121SGreg Clayton   }
1214069730cSRavitheja Addepally   return false;
1224069730cSRavitheja Addepally }
1234069730cSRavitheja Addepally 
Dump(Stream * s,bool show_context,lldb::DescriptionLevel level)124681466f5SAdrian Prantl void TypeMap::Dump(Stream *s, bool show_context, lldb::DescriptionLevel level) {
125b9c1b51eSKate Stone   for (iterator pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
126681466f5SAdrian Prantl     pos->second->Dump(s, show_context, level);
1274069730cSRavitheja Addepally   }
1284069730cSRavitheja Addepally }
1294069730cSRavitheja Addepally 
RemoveMismatchedTypes(llvm::StringRef type_scope,llvm::StringRef type_basename,TypeClass type_class,bool exact_match)130*65cac0edSDavid Blaikie void TypeMap::RemoveMismatchedTypes(llvm::StringRef type_scope,
131*65cac0edSDavid Blaikie                                     llvm::StringRef type_basename,
132b9c1b51eSKate Stone                                     TypeClass type_class, bool exact_match) {
13305097246SAdrian Prantl   // Our "collection" type currently is a std::map which doesn't have any good
13405097246SAdrian Prantl   // way to iterate and remove items from the map so we currently just make a
13505097246SAdrian Prantl   // new list and add all of the matching types to it, and then swap it into
13605097246SAdrian Prantl   // m_types at the end
1374069730cSRavitheja Addepally   collection matching_types;
1384069730cSRavitheja Addepally 
1394069730cSRavitheja Addepally   iterator pos, end = m_types.end();
1404069730cSRavitheja Addepally 
141b9c1b51eSKate Stone   for (pos = m_types.begin(); pos != end; ++pos) {
1424069730cSRavitheja Addepally     Type *the_type = pos->second.get();
1434069730cSRavitheja Addepally     bool keep_match = false;
1444069730cSRavitheja Addepally     TypeClass match_type_class = eTypeClassAny;
1454069730cSRavitheja Addepally 
146b9c1b51eSKate Stone     if (type_class != eTypeClassAny) {
1474069730cSRavitheja Addepally       match_type_class = the_type->GetForwardCompilerType().GetTypeClass();
1484069730cSRavitheja Addepally       if ((match_type_class & type_class) == 0)
1494069730cSRavitheja Addepally         continue;
1504069730cSRavitheja Addepally     }
1514069730cSRavitheja Addepally 
1524069730cSRavitheja Addepally     ConstString match_type_name_const_str(the_type->GetQualifiedName());
153b9c1b51eSKate Stone     if (match_type_name_const_str) {
1544069730cSRavitheja Addepally       const char *match_type_name = match_type_name_const_str.GetCString();
155556b1611STamas Berghammer       llvm::StringRef match_type_scope;
156556b1611STamas Berghammer       llvm::StringRef match_type_basename;
157b9c1b51eSKate Stone       if (Type::GetTypeScopeAndBasename(match_type_name, match_type_scope,
1584069730cSRavitheja Addepally                                         match_type_basename,
159b9c1b51eSKate Stone                                         match_type_class)) {
160b9c1b51eSKate Stone         if (match_type_basename == type_basename) {
1614069730cSRavitheja Addepally           const size_t type_scope_size = type_scope.size();
1624069730cSRavitheja Addepally           const size_t match_type_scope_size = match_type_scope.size();
163b9c1b51eSKate Stone           if (exact_match || (type_scope_size == match_type_scope_size)) {
1644069730cSRavitheja Addepally             keep_match = match_type_scope == type_scope;
165b9c1b51eSKate Stone           } else {
166b9c1b51eSKate Stone             if (match_type_scope_size > type_scope_size) {
1674069730cSRavitheja Addepally               const size_t type_scope_pos = match_type_scope.rfind(type_scope);
168b9c1b51eSKate Stone               if (type_scope_pos == match_type_scope_size - type_scope_size) {
169b9c1b51eSKate Stone                 if (type_scope_pos >= 2) {
170b9c1b51eSKate Stone                   // Our match scope ends with the type scope we were looking
17105097246SAdrian Prantl                   // for, but we need to make sure what comes before the
17205097246SAdrian Prantl                   // matching type scope is a namespace boundary in case we are
17305097246SAdrian Prantl                   // trying to match: type_basename = "d" type_scope = "b::c::"
1744069730cSRavitheja Addepally                   // We want to match:
1754069730cSRavitheja Addepally                   //  match_type_scope "a::b::c::"
1764069730cSRavitheja Addepally                   // But not:
1774069730cSRavitheja Addepally                   //  match_type_scope "a::bb::c::"
178b9c1b51eSKate Stone                   // So below we make sure what comes before "b::c::" in
17905097246SAdrian Prantl                   // match_type_scope is "::", or the namespace boundary
1804069730cSRavitheja Addepally                   if (match_type_scope[type_scope_pos - 1] == ':' &&
181b9c1b51eSKate Stone                       match_type_scope[type_scope_pos - 2] == ':') {
1824069730cSRavitheja Addepally                     keep_match = true;
1834069730cSRavitheja Addepally                   }
1844069730cSRavitheja Addepally                 }
1854069730cSRavitheja Addepally               }
1864069730cSRavitheja Addepally             }
1874069730cSRavitheja Addepally           }
1884069730cSRavitheja Addepally         }
189b9c1b51eSKate Stone       } else {
19005097246SAdrian Prantl         // The type we are currently looking at doesn't exists in a namespace
19105097246SAdrian Prantl         // or class, so it only matches if there is no type scope...
1928d20cfdfSJonas Devlieghere         keep_match = type_scope.empty() && type_basename == match_type_name;
1934069730cSRavitheja Addepally       }
1944069730cSRavitheja Addepally     }
1954069730cSRavitheja Addepally 
196b9c1b51eSKate Stone     if (keep_match) {
1974069730cSRavitheja Addepally       matching_types.insert(*pos);
1984069730cSRavitheja Addepally     }
1994069730cSRavitheja Addepally   }
2004069730cSRavitheja Addepally   m_types.swap(matching_types);
2014069730cSRavitheja Addepally }
202