1 //===-- UniqueDWARFASTType.h ------------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_UNIQUEDWARFASTTYPE_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_UNIQUEDWARFASTTYPE_H
11 
12 #include <vector>
13 
14 #include "llvm/ADT/DenseMap.h"
15 
16 #include "DWARFDIE.h"
17 #include "lldb/Core/Declaration.h"
18 
19 namespace lldb_private::plugin {
20 namespace dwarf {
21 class UniqueDWARFASTType {
22 public:
23   // Constructors and Destructors
UniqueDWARFASTType()24   UniqueDWARFASTType() : m_type_sp(), m_die(), m_declaration() {}
25 
UniqueDWARFASTType(lldb::TypeSP & type_sp,const DWARFDIE & die,const Declaration & decl,int32_t byte_size)26   UniqueDWARFASTType(lldb::TypeSP &type_sp, const DWARFDIE &die,
27                      const Declaration &decl, int32_t byte_size)
28       : m_type_sp(type_sp), m_die(die), m_declaration(decl),
29         m_byte_size(byte_size) {}
30 
UniqueDWARFASTType(const UniqueDWARFASTType & rhs)31   UniqueDWARFASTType(const UniqueDWARFASTType &rhs)
32       : m_type_sp(rhs.m_type_sp), m_die(rhs.m_die),
33         m_declaration(rhs.m_declaration), m_byte_size(rhs.m_byte_size) {}
34 
35   ~UniqueDWARFASTType() = default;
36 
37   UniqueDWARFASTType &operator=(const UniqueDWARFASTType &rhs) {
38     if (this != &rhs) {
39       m_type_sp = rhs.m_type_sp;
40       m_die = rhs.m_die;
41       m_declaration = rhs.m_declaration;
42       m_byte_size = rhs.m_byte_size;
43     }
44     return *this;
45   }
46 
47   lldb::TypeSP m_type_sp;
48   DWARFDIE m_die;
49   Declaration m_declaration;
50   int32_t m_byte_size = -1;
51 };
52 
53 class UniqueDWARFASTTypeList {
54 public:
UniqueDWARFASTTypeList()55   UniqueDWARFASTTypeList() : m_collection() {}
56 
57   ~UniqueDWARFASTTypeList() = default;
58 
GetSize()59   uint32_t GetSize() { return (uint32_t)m_collection.size(); }
60 
Append(const UniqueDWARFASTType & entry)61   void Append(const UniqueDWARFASTType &entry) {
62     m_collection.push_back(entry);
63   }
64 
65   bool Find(const DWARFDIE &die, const Declaration &decl,
66             const int32_t byte_size, UniqueDWARFASTType &entry) const;
67 
68 protected:
69   typedef std::vector<UniqueDWARFASTType> collection;
70   collection m_collection;
71 };
72 
73 class UniqueDWARFASTTypeMap {
74 public:
UniqueDWARFASTTypeMap()75   UniqueDWARFASTTypeMap() : m_collection() {}
76 
77   ~UniqueDWARFASTTypeMap() = default;
78 
Insert(ConstString name,const UniqueDWARFASTType & entry)79   void Insert(ConstString name, const UniqueDWARFASTType &entry) {
80     m_collection[name.GetCString()].Append(entry);
81   }
82 
Find(ConstString name,const DWARFDIE & die,const Declaration & decl,const int32_t byte_size,UniqueDWARFASTType & entry)83   bool Find(ConstString name, const DWARFDIE &die, const Declaration &decl,
84             const int32_t byte_size, UniqueDWARFASTType &entry) const {
85     const char *unique_name_cstr = name.GetCString();
86     collection::const_iterator pos = m_collection.find(unique_name_cstr);
87     if (pos != m_collection.end()) {
88       return pos->second.Find(die, decl, byte_size, entry);
89     }
90     return false;
91   }
92 
93 protected:
94   // A unique name string should be used
95   typedef llvm::DenseMap<const char *, UniqueDWARFASTTypeList> collection;
96   collection m_collection;
97 };
98 } // namespace dwarf
99 } // namespace lldb_private::plugin
100 
101 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_UNIQUEDWARFASTTYPE_H
102