gdb: remove BLOCK_NAMESPACE macro
[binutils-gdb.git] / gdb / block.h
1 /* Code dealing with blocks for GDB.
2
3 Copyright (C) 2003-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 BLOCK_H
21 #define BLOCK_H
22
23 #include "dictionary.h"
24
25 /* Opaque declarations. */
26
27 struct symbol;
28 struct compunit_symtab;
29 struct block_namespace_info;
30 struct using_direct;
31 struct obstack;
32 struct addrmap;
33
34 /* Blocks can occupy non-contiguous address ranges. When this occurs,
35 startaddr and endaddr within struct block (still) specify the lowest
36 and highest addresses of all ranges, but each individual range is
37 specified by the addresses in struct blockrange. */
38
39 struct blockrange
40 {
41 blockrange (CORE_ADDR startaddr_, CORE_ADDR endaddr_)
42 : startaddr (startaddr_),
43 endaddr (endaddr_)
44 {
45 }
46
47 /* Lowest address in this range. */
48
49 CORE_ADDR startaddr;
50
51 /* One past the highest address in the range. */
52
53 CORE_ADDR endaddr;
54 };
55
56 /* Two or more non-contiguous ranges in the same order as that provided
57 via the debug info. */
58
59 struct blockranges
60 {
61 int nranges;
62 struct blockrange range[1];
63 };
64
65 /* All of the name-scope contours of the program
66 are represented by `struct block' objects.
67 All of these objects are pointed to by the blockvector.
68
69 Each block represents one name scope.
70 Each lexical context has its own block.
71
72 The blockvector begins with some special blocks.
73 The GLOBAL_BLOCK contains all the symbols defined in this compilation
74 whose scope is the entire program linked together.
75 The STATIC_BLOCK contains all the symbols whose scope is the
76 entire compilation excluding other separate compilations.
77 Blocks starting with the FIRST_LOCAL_BLOCK are not special.
78
79 Each block records a range of core addresses for the code that
80 is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
81 give, for the range of code, the entire range of code produced
82 by the compilation that the symbol segment belongs to.
83
84 The blocks appear in the blockvector
85 in order of increasing starting-address,
86 and, within that, in order of decreasing ending-address.
87
88 This implies that within the body of one function
89 the blocks appear in the order of a depth-first tree walk. */
90
91 struct block
92 {
93 /* Return this block's start address. */
94 CORE_ADDR start () const
95 { return m_start; }
96
97 /* Set this block's start address. */
98 void set_start (CORE_ADDR start)
99 { m_start = start; }
100
101 /* Return this block's end address. */
102 CORE_ADDR end () const
103 { return m_end; }
104
105 /* Set this block's end address. */
106 void set_end (CORE_ADDR end)
107 { m_end = end; }
108
109 /* Return this block's function symbol. */
110 symbol *function () const
111 { return m_function; }
112
113 /* Set this block's function symbol. */
114 void set_function (symbol *function)
115 { m_function = function; }
116
117 /* Return this block's superblock. */
118 const block *superblock () const
119 { return m_superblock; }
120
121 /* Set this block's superblock. */
122 void set_superblock (const block *superblock)
123 { m_superblock = superblock; }
124
125 /* Return this block's multidict. */
126 multidictionary *multidict () const
127 { return m_multidict; }
128
129 /* Set this block's multidict. */
130 void set_multidict (multidictionary *multidict)
131 { m_multidict = multidict; }
132
133 /* Return this block's namespace info. */
134 block_namespace_info *namespace_info () const
135 { return m_namespace_info; }
136
137 /* Set this block's namespace info. */
138 void set_namespace_info (block_namespace_info *namespace_info)
139 { m_namespace_info = namespace_info; }
140
141 /* Addresses in the executable code that are in this block. */
142
143 CORE_ADDR m_start;
144 CORE_ADDR m_end;
145
146 /* The symbol that names this block, if the block is the body of a
147 function (real or inlined); otherwise, zero. */
148
149 struct symbol *m_function;
150
151 /* The `struct block' for the containing block, or 0 if none.
152
153 The superblock of a top-level local block (i.e. a function in the
154 case of C) is the STATIC_BLOCK. The superblock of the
155 STATIC_BLOCK is the GLOBAL_BLOCK. */
156
157 const struct block *m_superblock;
158
159 /* This is used to store the symbols in the block. */
160
161 struct multidictionary *m_multidict;
162
163 /* Contains information about namespace-related info relevant to this block:
164 using directives and the current namespace scope. */
165
166 struct block_namespace_info *m_namespace_info;
167
168 /* Address ranges for blocks with non-contiguous ranges. If this
169 is NULL, then there is only one range which is specified by
170 startaddr and endaddr above. */
171
172 struct blockranges *ranges;
173 };
174
175 /* The global block is singled out so that we can provide a back-link
176 to the compunit symtab. */
177
178 struct global_block
179 {
180 /* The block. */
181
182 struct block block;
183
184 /* This holds a pointer to the compunit symtab holding this block. */
185
186 struct compunit_symtab *compunit_symtab;
187 };
188
189 /* Accessor for ranges field within block BL. */
190
191 #define BLOCK_RANGES(bl) (bl)->ranges
192
193 /* Number of ranges within a block. */
194
195 #define BLOCK_NRANGES(bl) (bl)->ranges->nranges
196
197 /* Access range array for block BL. */
198
199 #define BLOCK_RANGE(bl) (bl)->ranges->range
200
201 /* Are all addresses within a block contiguous? */
202
203 #define BLOCK_CONTIGUOUS_P(bl) (BLOCK_RANGES (bl) == nullptr \
204 || BLOCK_NRANGES (bl) <= 1)
205
206 /* Obtain the start address of the Nth range for block BL. */
207
208 #define BLOCK_RANGE_START(bl,n) (BLOCK_RANGE (bl)[n].startaddr)
209
210 /* Obtain the end address of the Nth range for block BL. */
211
212 #define BLOCK_RANGE_END(bl,n) (BLOCK_RANGE (bl)[n].endaddr)
213
214 /* Define the "entry pc" for a block BL to be the lowest (start) address
215 for the block when all addresses within the block are contiguous. If
216 non-contiguous, then use the start address for the first range in the
217 block.
218
219 At the moment, this almost matches what DWARF specifies as the entry
220 pc. (The missing bit is support for DW_AT_entry_pc which should be
221 preferred over range data and the low_pc.)
222
223 Once support for DW_AT_entry_pc is added, I expect that an entry_pc
224 field will be added to one of these data structures. Once that's done,
225 the entry_pc field can be set from the dwarf reader (and other readers
226 too). BLOCK_ENTRY_PC can then be redefined to be less DWARF-centric. */
227
228 #define BLOCK_ENTRY_PC(bl) (BLOCK_CONTIGUOUS_P (bl) \
229 ? bl->start () \
230 : BLOCK_RANGE_START (bl,0))
231
232 struct blockvector
233 {
234 /* Number of blocks in the list. */
235 int nblocks;
236 /* An address map mapping addresses to blocks in this blockvector.
237 This pointer is zero if the blocks' start and end addresses are
238 enough. */
239 struct addrmap *map;
240 /* The blocks themselves. */
241 struct block *block[1];
242 };
243
244 #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks
245 #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n]
246 #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map)
247
248 /* Return the objfile of BLOCK, which must be non-NULL. */
249
250 extern struct objfile *block_objfile (const struct block *block);
251
252 /* Return the architecture of BLOCK, which must be non-NULL. */
253
254 extern struct gdbarch *block_gdbarch (const struct block *block);
255
256 extern struct symbol *block_linkage_function (const struct block *);
257
258 extern struct symbol *block_containing_function (const struct block *);
259
260 extern int block_inlined_p (const struct block *block);
261
262 /* Return true if block A is lexically nested within block B, or if a
263 and b have the same pc range. Return false otherwise. If
264 ALLOW_NESTED is true, then block A is considered to be in block B
265 if A is in a nested function in B's function. If ALLOW_NESTED is
266 false (the default), then blocks in nested functions are not
267 considered to be contained. */
268
269 extern bool contained_in (const struct block *a, const struct block *b,
270 bool allow_nested = false);
271
272 extern const struct blockvector *blockvector_for_pc (CORE_ADDR,
273 const struct block **);
274
275 extern const struct blockvector *
276 blockvector_for_pc_sect (CORE_ADDR, struct obj_section *,
277 const struct block **, struct compunit_symtab *);
278
279 extern int blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc);
280
281 extern struct call_site *call_site_for_pc (struct gdbarch *gdbarch,
282 CORE_ADDR pc);
283
284 extern const struct block *block_for_pc (CORE_ADDR);
285
286 extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
287
288 extern const char *block_scope (const struct block *block);
289
290 extern void block_set_scope (struct block *block, const char *scope,
291 struct obstack *obstack);
292
293 extern struct using_direct *block_using (const struct block *block);
294
295 extern void block_set_using (struct block *block,
296 struct using_direct *using_decl,
297 struct obstack *obstack);
298
299 extern const struct block *block_static_block (const struct block *block);
300
301 extern const struct block *block_global_block (const struct block *block);
302
303 extern struct block *allocate_block (struct obstack *obstack);
304
305 extern struct block *allocate_global_block (struct obstack *obstack);
306
307 extern void set_block_compunit_symtab (struct block *,
308 struct compunit_symtab *);
309
310 /* Return a property to evaluate the static link associated to BLOCK.
311
312 In the context of nested functions (available in Pascal, Ada and GNU C, for
313 instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
314 function is a way to get the frame corresponding to the enclosing function.
315
316 Note that only objfile-owned and function-level blocks can have a static
317 link. Return NULL if there is no such property. */
318
319 extern struct dynamic_prop *block_static_link (const struct block *block);
320
321 /* A block iterator. This structure should be treated as though it
322 were opaque; it is only defined here because we want to support
323 stack allocation of iterators. */
324
325 struct block_iterator
326 {
327 /* If we're iterating over a single block, this holds the block.
328 Otherwise, it holds the canonical compunit. */
329
330 union
331 {
332 struct compunit_symtab *compunit_symtab;
333 const struct block *block;
334 } d;
335
336 /* If we're iterating over a single block, this is always -1.
337 Otherwise, it holds the index of the current "included" symtab in
338 the canonical symtab (that is, d.symtab->includes[idx]), with -1
339 meaning the canonical symtab itself. */
340
341 int idx;
342
343 /* Which block, either static or global, to iterate over. If this
344 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
345 This is used to select which field of 'd' is in use. */
346
347 enum block_enum which;
348
349 /* The underlying multidictionary iterator. */
350
351 struct mdict_iterator mdict_iter;
352 };
353
354 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
355 return that first symbol, or NULL if BLOCK is empty. */
356
357 extern struct symbol *block_iterator_first (const struct block *block,
358 struct block_iterator *iterator);
359
360 /* Advance ITERATOR, and return the next symbol, or NULL if there are
361 no more symbols. Don't call this if you've previously received
362 NULL from block_iterator_first or block_iterator_next on this
363 iteration. */
364
365 extern struct symbol *block_iterator_next (struct block_iterator *iterator);
366
367 /* Initialize ITERATOR to point at the first symbol in BLOCK whose
368 search_name () matches NAME, and return that first symbol, or
369 NULL if there are no such symbols. */
370
371 extern struct symbol *block_iter_match_first (const struct block *block,
372 const lookup_name_info &name,
373 struct block_iterator *iterator);
374
375 /* Advance ITERATOR to point at the next symbol in BLOCK whose
376 search_name () matches NAME, or NULL if there are no more such
377 symbols. Don't call this if you've previously received NULL from
378 block_iterator_match_first or block_iterator_match_next on this
379 iteration. And don't call it unless ITERATOR was created by a
380 previous call to block_iter_match_first with the same NAME. */
381
382 extern struct symbol *block_iter_match_next
383 (const lookup_name_info &name, struct block_iterator *iterator);
384
385 /* Return true if symbol A is the best match possible for DOMAIN. */
386
387 extern bool best_symbol (struct symbol *a, const domain_enum domain);
388
389 /* Return symbol B if it is a better match than symbol A for DOMAIN.
390 Otherwise return A. */
391
392 extern struct symbol *better_symbol (struct symbol *a, struct symbol *b,
393 const domain_enum domain);
394
395 /* Search BLOCK for symbol NAME in DOMAIN. */
396
397 extern struct symbol *block_lookup_symbol (const struct block *block,
398 const char *name,
399 symbol_name_match_type match_type,
400 const domain_enum domain);
401
402 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
403 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
404 one iterates all global/static blocks of an objfile. */
405
406 extern struct symbol *block_lookup_symbol_primary (const struct block *block,
407 const char *name,
408 const domain_enum domain);
409
410 /* The type of the MATCHER argument to block_find_symbol. */
411
412 typedef int (block_symbol_matcher_ftype) (struct symbol *, void *);
413
414 /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
415 DATA is passed unchanged to MATCHER.
416 BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
417
418 extern struct symbol *block_find_symbol (const struct block *block,
419 const char *name,
420 const domain_enum domain,
421 block_symbol_matcher_ftype *matcher,
422 void *data);
423
424 /* A matcher function for block_find_symbol to find only symbols with
425 non-opaque types. */
426
427 extern int block_find_non_opaque_type (struct symbol *sym, void *data);
428
429 /* A matcher function for block_find_symbol to prefer symbols with
430 non-opaque types. The way to use this function is as follows:
431
432 struct symbol *with_opaque = NULL;
433 struct symbol *sym
434 = block_find_symbol (block, name, domain,
435 block_find_non_opaque_type_preferred, &with_opaque);
436
437 At this point if SYM is non-NULL then a non-opaque type has been found.
438 Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
439 Otherwise, the symbol was not found. */
440
441 extern int block_find_non_opaque_type_preferred (struct symbol *sym,
442 void *data);
443
444 /* Macro to loop through all symbols in BLOCK, in no particular
445 order. ITER helps keep track of the iteration, and must be a
446 struct block_iterator. SYM points to the current symbol. */
447
448 #define ALL_BLOCK_SYMBOLS(block, iter, sym) \
449 for ((sym) = block_iterator_first ((block), &(iter)); \
450 (sym); \
451 (sym) = block_iterator_next (&(iter)))
452
453 /* Macro to loop through all symbols in BLOCK with a name that matches
454 NAME, in no particular order. ITER helps keep track of the
455 iteration, and must be a struct block_iterator. SYM points to the
456 current symbol. */
457
458 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
459 for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
460 (sym) != NULL; \
461 (sym) = block_iter_match_next ((name), &(iter)))
462
463 /* Given a vector of pairs, allocate and build an obstack allocated
464 blockranges struct for a block. */
465 struct blockranges *make_blockranges (struct objfile *objfile,
466 const std::vector<blockrange> &rangevec);
467
468 #endif /* BLOCK_H */