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