0a38fc88e52bca428c19c4fae968a76ee5998063
[binutils-gdb.git] / gdb / dwarf2 / cooked-index.h
1 /* DIE indexing
2
3 Copyright (C) 2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef GDB_DWARF2_COOKED_INDEX_H
21 #define GDB_DWARF2_COOKED_INDEX_H
22
23 #include "dwarf2.h"
24 #include "gdbtypes.h"
25 #include "symtab.h"
26 #include "hashtab.h"
27 #include "dwarf2/index-common.h"
28 #include "gdbsupport/gdb_string_view.h"
29 #include "quick-symbol.h"
30 #include "gdbsupport/gdb_obstack.h"
31 #include "addrmap.h"
32 #include "gdbsupport/iterator-range.h"
33
34 struct dwarf2_per_cu_data;
35
36 /* Flags that describe an entry in the index. */
37 enum cooked_index_flag_enum : unsigned char
38 {
39 /* True if this entry is the program's "main". */
40 IS_MAIN = 1,
41 /* True if this entry represents a "static" object. */
42 IS_STATIC = 2,
43 /* True if this entry is an "enum class". */
44 IS_ENUM_CLASS = 4,
45 /* True if this entry uses the linkage name. */
46 IS_LINKAGE = 8,
47 };
48 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag);
49
50 /* A cooked_index_entry represents a single item in the index. Note
51 that two entries can be created for the same DIE -- one using the
52 name, and another one using the linkage name, if any.
53
54 This is an "open" class and the members are all directly
55 accessible. It is read-only after the index has been fully read
56 and processed. */
57 struct cooked_index_entry : public allocate_on_obstack
58 {
59 cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_,
60 cooked_index_flag flags_, const char *name_,
61 const cooked_index_entry *parent_entry_,
62 dwarf2_per_cu_data *per_cu_)
63 : name (name_),
64 tag (tag_),
65 flags (flags_),
66 die_offset (die_offset_),
67 parent_entry (parent_entry_),
68 per_cu (per_cu_)
69 {
70 }
71
72 /* Return true if this entry matches SEARCH_FLAGS. */
73 bool matches (block_search_flags search_flags) const
74 {
75 if ((search_flags & SEARCH_STATIC_BLOCK) != 0
76 && (flags & IS_STATIC) != 0)
77 return true;
78 if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0
79 && (flags & IS_STATIC) == 0)
80 return true;
81 return false;
82 }
83
84 /* Return true if this entry matches DOMAIN. */
85 bool matches (domain_enum domain) const
86 {
87 switch (domain)
88 {
89 case LABEL_DOMAIN:
90 return false;
91
92 case MODULE_DOMAIN:
93 return tag == DW_TAG_module;
94
95 case COMMON_BLOCK_DOMAIN:
96 return tag == DW_TAG_common_block;
97 }
98
99 return true;
100 }
101
102 /* Return true if this entry matches KIND. */
103 bool matches (enum search_domain kind) const
104 {
105 switch (kind)
106 {
107 case VARIABLES_DOMAIN:
108 return tag == DW_TAG_variable;
109 case FUNCTIONS_DOMAIN:
110 return tag == DW_TAG_subprogram;
111 case TYPES_DOMAIN:
112 return tag == DW_TAG_typedef || tag == DW_TAG_structure_type;
113 case MODULES_DOMAIN:
114 return tag == DW_TAG_module;
115 }
116
117 return true;
118 }
119
120 /* Construct the fully-qualified name of this entry and return a
121 pointer to it. If allocation is needed, it will be done on
122 STORAGE. */
123 const char *full_name (struct obstack *storage) const;
124
125 /* Entries must be sorted case-insensitively; this compares two
126 entries. */
127 bool operator< (const cooked_index_entry &other) const
128 {
129 return strcasecmp (canonical, other.canonical) < 0;
130 }
131
132 /* The name as it appears in DWARF. This always points into one of
133 the mapped DWARF sections. Note that this may be the name or the
134 linkage name -- two entries are created for DIEs which have both
135 attributes. */
136 const char *name;
137 /* The canonical name. For C++ names, this may differ from NAME.
138 In all other cases, this is equal to NAME. */
139 const char *canonical = nullptr;
140 /* The DWARF tag. */
141 enum dwarf_tag tag;
142 /* Any flags attached to this entry. */
143 cooked_index_flag flags;
144 /* The offset of this DIE. */
145 sect_offset die_offset;
146 /* The parent entry. This is NULL for top-level entries.
147 Otherwise, it points to the parent entry, such as a namespace or
148 class. */
149 const cooked_index_entry *parent_entry;
150 /* The CU from which this entry originates. */
151 dwarf2_per_cu_data *per_cu;
152
153 private:
154
155 void write_scope (struct obstack *storage, const char *sep) const;
156 };
157
158 /* An index of interesting DIEs. This is "cooked", in contrast to a
159 mapped .debug_names or .gdb_index, which are "raw". An entry in
160 the index is of type cooked_index_entry.
161
162 Operations on the index are described below. They are chosen to
163 make it relatively simple to implement the symtab "quick"
164 methods. */
165 class cooked_index
166 {
167 public:
168 cooked_index () = default;
169 explicit cooked_index (cooked_index &&other) = default;
170 DISABLE_COPY_AND_ASSIGN (cooked_index);
171 cooked_index &operator= (cooked_index &&other) = default;
172
173 /* Create a new cooked_index_entry and register it with this object.
174 Entries are owned by this object. The new item is returned. */
175 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
176 cooked_index_flag flags,
177 const char *name,
178 const cooked_index_entry *parent_entry,
179 dwarf2_per_cu_data *per_cu);
180
181 /* Return the entry that is believed to represent the program's
182 "main". This will return NULL if no such entry is available. */
183 const cooked_index_entry *get_main () const
184 {
185 return m_main;
186 }
187
188 /* Install a new fixed addrmap from the given mutable addrmap. */
189 void install_addrmap (addrmap *map)
190 {
191 gdb_assert (m_addrmap == nullptr);
192 m_addrmap = addrmap_create_fixed (map, &m_storage);
193 }
194
195 /* Look up ADDR in the address map, and return either the
196 corresponding CU, or nullptr if the address could not be
197 found. */
198 dwarf2_per_cu_data *lookup (CORE_ADDR addr)
199 {
200 return (dwarf2_per_cu_data *) addrmap_find (m_addrmap, addr);
201 }
202
203 /* Finalize the index. This should be called a single time, when
204 the index has been fully populated. It enters all the entries
205 into the internal hash table. */
206 void finalize ();
207
208 /* A simple range over part of m_entries. */
209 typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
210
211 /* Look up an entry by name. Returns a range of all matching
212 results. If COMPLETING is true, then a larger range, suitable
213 for completion, will be returned. */
214 range find (gdb::string_view name, bool completing);
215
216 /* Return a range of all the entries. */
217 range all_entries ()
218 {
219 return { m_entries.begin (), m_entries.end () };
220 }
221
222 private:
223
224 /* GNAT only emits mangled ("encoded") names in the DWARF, and does
225 not emit the module structure. However, we need this structure
226 to do lookups. This function recreates that structure for an
227 existing entry. It returns the base name (last element) of the
228 full decoded name. */
229 gdb::unique_xmalloc_ptr<char> handle_gnat_encoded_entry
230 (cooked_index_entry *entry, htab_t gnat_entries);
231
232 /* Create a new cooked_index_entry and register it with this object.
233 Entries are owned by this object. The new item is returned. */
234 cooked_index_entry *create (sect_offset die_offset,
235 enum dwarf_tag tag,
236 cooked_index_flag flags,
237 const char *name,
238 const cooked_index_entry *parent_entry,
239 dwarf2_per_cu_data *per_cu)
240 {
241 return new (&m_storage) cooked_index_entry (die_offset, tag, flags,
242 name, parent_entry,
243 per_cu);
244 }
245
246 /* Storage for the entries. */
247 auto_obstack m_storage;
248 /* List of all entries. */
249 std::vector<cooked_index_entry *> m_entries;
250 /* If we found "main" or an entry with 'is_main' set, store it
251 here. */
252 cooked_index_entry *m_main = nullptr;
253 /* Storage for canonical names. */
254 std::vector<gdb::unique_xmalloc_ptr<char>> m_names;
255 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
256 objects. */
257 addrmap *m_addrmap = nullptr;
258 };
259
260 #endif /* GDB_DWARF2_COOKED_INDEX_H */