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