1 //===-- DWARFDeclContext.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_DWARFDECLCONTEXT_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
11 
12 #include "lldb/Utility/ConstString.h"
13 #include "DWARFDefines.h"
14 
15 #include <cassert>
16 #include <string>
17 #include <vector>
18 
19 namespace lldb_private::plugin {
20 namespace dwarf {
21 // DWARFDeclContext
22 //
23 // A class that represents a declaration context all the way down to a
24 // DIE. This is useful when trying to find a DIE in one DWARF to a DIE
25 // in another DWARF file.
26 
27 class DWARFDeclContext {
28 public:
29   struct Entry {
30     Entry() = default;
EntryEntry31     Entry(dw_tag_t t, const char *n) : tag(t), name(n) {}
32 
NameMatchesEntry33     bool NameMatches(const Entry &rhs) const {
34       if (name == rhs.name)
35         return true;
36       else if (name && rhs.name)
37         return strcmp(name, rhs.name) == 0;
38       return false;
39     }
40 
41     // Test operator
42     explicit operator bool() const { return tag != 0; }
43 
44     dw_tag_t tag = llvm::dwarf::DW_TAG_null;
45     const char *name = nullptr;
46   };
47 
DWARFDeclContext()48   DWARFDeclContext() : m_entries() {}
49 
AppendDeclContext(dw_tag_t tag,const char * name)50   void AppendDeclContext(dw_tag_t tag, const char *name) {
51     m_entries.push_back(Entry(tag, name));
52   }
53 
54   bool operator==(const DWARFDeclContext &rhs) const;
55   bool operator!=(const DWARFDeclContext &rhs) const { return !(*this == rhs); }
56 
GetSize()57   uint32_t GetSize() const { return m_entries.size(); }
58 
59   Entry &operator[](uint32_t idx) {
60     assert(idx < m_entries.size() && "invalid index");
61     return m_entries[idx];
62   }
63 
64   const Entry &operator[](uint32_t idx) const {
65     assert(idx < m_entries.size() && "invalid index");
66     return m_entries[idx];
67   }
68 
69   const char *GetQualifiedName() const;
70 
71   // Same as GetQualifiedName, but the life time of the returned string will
72   // be that of the LLDB session.
GetQualifiedNameAsConstString()73   ConstString GetQualifiedNameAsConstString() const {
74     return ConstString(GetQualifiedName());
75   }
76 
Clear()77   void Clear() {
78     m_entries.clear();
79     m_qualified_name.clear();
80   }
81 
82 protected:
83   typedef std::vector<Entry> collection;
84   collection m_entries;
85   mutable std::string m_qualified_name;
86 };
87 } // namespace dwarf
88 } // namespace lldb_private::plugin
89 
90 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
91