[lldb][DWARFUnit] Implement PeekDIEName query (#78486)
[llvm-project.git] / lldb / source / Plugins / SymbolFile / DWARF / DWARFDeclContext.h
bloba20a862d3402967e78cb48650c333954d384d7cf
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 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H
12 #include "lldb/Utility/ConstString.h"
13 #include "DWARFDefines.h"
15 #include <cassert>
16 #include <string>
17 #include <vector>
19 namespace lldb_private::plugin {
20 namespace dwarf {
21 // DWARFDeclContext
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.
27 class DWARFDeclContext {
28 public:
29 struct Entry {
30 Entry() = default;
31 Entry(dw_tag_t t, const char *n) : tag(t), name(n) {}
33 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;
41 // Test operator
42 explicit operator bool() const { return tag != 0; }
44 dw_tag_t tag = llvm::dwarf::DW_TAG_null;
45 const char *name = nullptr;
48 DWARFDeclContext() : m_entries() {}
50 void AppendDeclContext(dw_tag_t tag, const char *name) {
51 m_entries.push_back(Entry(tag, name));
54 bool operator==(const DWARFDeclContext &rhs) const;
55 bool operator!=(const DWARFDeclContext &rhs) const { return !(*this == rhs); }
57 uint32_t GetSize() const { return m_entries.size(); }
59 Entry &operator[](uint32_t idx) {
60 assert(idx < m_entries.size() && "invalid index");
61 return m_entries[idx];
64 const Entry &operator[](uint32_t idx) const {
65 assert(idx < m_entries.size() && "invalid index");
66 return m_entries[idx];
69 const char *GetQualifiedName() const;
71 // Same as GetQualifiedName, but the life time of the returned string will
72 // be that of the LLDB session.
73 ConstString GetQualifiedNameAsConstString() const {
74 return ConstString(GetQualifiedName());
77 void Clear() {
78 m_entries.clear();
79 m_qualified_name.clear();
82 protected:
83 typedef std::vector<Entry> collection;
84 collection m_entries;
85 mutable std::string m_qualified_name;
87 } // namespace dwarf
88 } // namespace lldb_private::plugin
90 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDECLCONTEXT_H