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