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