]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/block.h
Convert block_inlined_p to method
[thirdparty/binutils-gdb.git] / gdb / block.h
CommitLineData
fe898f56
DC
1/* Code dealing with blocks for GDB.
2
213516ef 3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
fe898f56
DC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fe898f56
DC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fe898f56
DC
19
20#ifndef BLOCK_H
21#define BLOCK_H
22
8157b174 23#include "dictionary.h"
f5cb8afd 24#include "gdbsupport/array-view.h"
8157b174 25
fe898f56
DC
26/* Opaque declarations. */
27
28struct symbol;
43f3e411 29struct compunit_symtab;
9219021c
DC
30struct block_namespace_info;
31struct using_direct;
32struct obstack;
801e3a5b 33struct addrmap;
fe898f56 34
26457a9c
KB
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
40struct blockrange
41{
6dd5a4bd
SM
42 blockrange (CORE_ADDR start, CORE_ADDR end)
43 : m_start (start),
44 m_end (end)
26457a9c
KB
45 {
46 }
47
6dd5a4bd
SM
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
26457a9c
KB
64 /* Lowest address in this range. */
65
6dd5a4bd 66 CORE_ADDR m_start;
26457a9c
KB
67
68 /* One past the highest address in the range. */
69
6dd5a4bd 70 CORE_ADDR m_end;
26457a9c
KB
71};
72
73/* Two or more non-contiguous ranges in the same order as that provided
74 via the debug info. */
75
76struct blockranges
77{
78 int nranges;
79 struct blockrange range[1];
80};
81
fe898f56
DC
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
108struct block
109{
4b8791e1
SM
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; }
fe898f56 125
6c00f721
SM
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
f135fe72
SM
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
24d74bb5
SM
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
3fe38936
SM
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
f5cb8afd
SM
158 /* Return a view on this block's ranges. */
159 gdb::array_view<blockrange> ranges ()
c42dd30d
AB
160 {
161 if (m_ranges == nullptr)
162 return {};
163 else
164 return gdb::make_array_view (m_ranges->range, m_ranges->nranges);
165 }
f5cb8afd
SM
166
167 /* Const version of the above. */
168 gdb::array_view<const blockrange> ranges () const
c42dd30d
AB
169 {
170 if (m_ranges == nullptr)
171 return {};
172 else
173 return gdb::make_array_view (m_ranges->range, m_ranges->nranges);
174 }
f5cb8afd
SM
175
176 /* Set this block's ranges array. */
177 void set_ranges (blockranges *ranges)
178 { m_ranges = ranges; }
179
086d03c9
SM
180 /* Return true if all addresses within this block are contiguous. */
181 bool is_contiguous () const
182 { return this->ranges ().size () <= 1; }
183
6395b628
SM
184 /* Return the "entry PC" of this block.
185
186 The entry PC is the lowest (start) address for the block when all addresses
187 within the block are contiguous. If non-contiguous, then use the start
188 address for the first range in the block.
189
190 At the moment, this almost matches what DWARF specifies as the entry
191 pc. (The missing bit is support for DW_AT_entry_pc which should be
192 preferred over range data and the low_pc.)
193
194 Once support for DW_AT_entry_pc is added, I expect that an entry_pc
195 field will be added to one of these data structures. Once that's done,
196 the entry_pc field can be set from the dwarf reader (and other readers
197 too). ENTRY_PC can then be redefined to be less DWARF-centric. */
198
199 CORE_ADDR entry_pc () const
200 {
201 if (this->is_contiguous ())
202 return this->start ();
203 else
204 return this->ranges ()[0].start ();
205 }
206
46baa3c6
TT
207 /* Return the objfile of this block. */
208
209 struct objfile *objfile () const;
210
7f5937df
TT
211 /* Return the architecture of this block. */
212
213 struct gdbarch *gdbarch () const;
214
a4dfe747
TT
215 /* Return true if BL represents an inlined function. */
216
217 bool inlined_p () const;
218
fe898f56
DC
219 /* Addresses in the executable code that are in this block. */
220
4b8791e1
SM
221 CORE_ADDR m_start;
222 CORE_ADDR m_end;
fe898f56
DC
223
224 /* The symbol that names this block, if the block is the body of a
edb3359d 225 function (real or inlined); otherwise, zero. */
fe898f56 226
6c00f721 227 struct symbol *m_function;
fe898f56
DC
228
229 /* The `struct block' for the containing block, or 0 if none.
230
231 The superblock of a top-level local block (i.e. a function in the
232 case of C) is the STATIC_BLOCK. The superblock of the
233 STATIC_BLOCK is the GLOBAL_BLOCK. */
234
f135fe72 235 const struct block *m_superblock;
fe898f56 236
de4f826b
DC
237 /* This is used to store the symbols in the block. */
238
24d74bb5 239 struct multidictionary *m_multidict;
de4f826b 240
22cee43f
PMR
241 /* Contains information about namespace-related info relevant to this block:
242 using directives and the current namespace scope. */
243
3fe38936 244 struct block_namespace_info *m_namespace_info;
26457a9c
KB
245
246 /* Address ranges for blocks with non-contiguous ranges. If this
247 is NULL, then there is only one range which is specified by
248 startaddr and endaddr above. */
249
f5cb8afd 250 struct blockranges *m_ranges;
fe898f56
DC
251};
252
84a146c9 253/* The global block is singled out so that we can provide a back-link
43f3e411 254 to the compunit symtab. */
84a146c9
TT
255
256struct global_block
257{
258 /* The block. */
259
260 struct block block;
261
43f3e411 262 /* This holds a pointer to the compunit symtab holding this block. */
84a146c9 263
43f3e411 264 struct compunit_symtab *compunit_symtab;
84a146c9
TT
265};
266
fe898f56
DC
267struct blockvector
268{
63d609de
SM
269 /* Return a view on the blocks of this blockvector. */
270 gdb::array_view<struct block *> blocks ()
271 {
272 return gdb::array_view<struct block *> (m_blocks, m_num_blocks);
273 }
274
275 /* Const version of the above. */
276 gdb::array_view<const struct block *const> blocks () const
277 {
278 const struct block **blocks = (const struct block **) m_blocks;
279 return gdb::array_view<const struct block *const> (blocks, m_num_blocks);
280 }
281
282 /* Return the block at index I. */
283 struct block *block (size_t i)
284 { return this->blocks ()[i]; }
285
286 /* Const version of the above. */
287 const struct block *block (size_t i) const
288 { return this->blocks ()[i]; }
289
290 /* Set the block at index I. */
291 void set_block (int i, struct block *block)
292 { m_blocks[i] = block; }
293
294 /* Set the number of blocks of this blockvector.
295
296 The storage of blocks is done using a flexible array member, so the number
297 of blocks set here must agree with what was effectively allocated. */
298 void set_num_blocks (int num_blocks)
299 { m_num_blocks = num_blocks; }
300
301 /* Return the number of blocks in this blockvector. */
302 int num_blocks () const
303 { return m_num_blocks; }
304
305 /* Return the global block of this blockvector. */
306 struct block *global_block ()
307 { return this->block (GLOBAL_BLOCK); }
308
309 /* Const version of the above. */
310 const struct block *global_block () const
311 { return this->block (GLOBAL_BLOCK); }
312
313 /* Return the static block of this blockvector. */
314 struct block *static_block ()
315 { return this->block (STATIC_BLOCK); }
316
317 /* Const version of the above. */
318 const struct block *static_block () const
319 { return this->block (STATIC_BLOCK); }
320
414705d1
SM
321 /* Return the address -> block map of this blockvector. */
322 addrmap *map ()
323 { return m_map; }
324
325 /* Const version of the above. */
326 const addrmap *map () const
327 { return m_map; }
328
329 /* Set this blockvector's address -> block map. */
330 void set_map (addrmap *map)
331 { m_map = map; }
332
333private:
801e3a5b
JB
334 /* An address map mapping addresses to blocks in this blockvector.
335 This pointer is zero if the blocks' start and end addresses are
336 enough. */
414705d1 337 struct addrmap *m_map;
63d609de 338
63d609de
SM
339 /* Number of blocks in the list. */
340 int m_num_blocks;
341
fe898f56 342 /* The blocks themselves. */
63d609de 343 struct block *m_blocks[1];
fe898f56
DC
344};
345
7f0df278 346extern struct symbol *block_linkage_function (const struct block *);
fe898f56 347
f8eba3c6
TT
348extern struct symbol *block_containing_function (const struct block *);
349
f21c2bd7
TT
350/* Return true if block A is lexically nested within block B, or if a
351 and b have the same pc range. Return false otherwise. If
352 ALLOW_NESTED is true, then block A is considered to be in block B
353 if A is in a nested function in B's function. If ALLOW_NESTED is
354 false (the default), then blocks in nested functions are not
355 considered to be contained. */
356
357extern bool contained_in (const struct block *a, const struct block *b,
358 bool allow_nested = false);
fe898f56 359
346d1dfe 360extern const struct blockvector *blockvector_for_pc (CORE_ADDR,
3977b71f 361 const struct block **);
fe898f56 362
43f3e411
DE
363extern const struct blockvector *
364 blockvector_for_pc_sect (CORE_ADDR, struct obj_section *,
365 const struct block **, struct compunit_symtab *);
fe898f56 366
346d1dfe 367extern int blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc);
9703b513 368
8e3b41a9
JK
369extern struct call_site *call_site_for_pc (struct gdbarch *gdbarch,
370 CORE_ADDR pc);
371
3977b71f 372extern const struct block *block_for_pc (CORE_ADDR);
fe898f56 373
3977b71f 374extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
fe898f56 375
1fcb5155
DC
376extern const char *block_scope (const struct block *block);
377
9219021c
DC
378extern void block_set_scope (struct block *block, const char *scope,
379 struct obstack *obstack);
380
1fcb5155
DC
381extern struct using_direct *block_using (const struct block *block);
382
9219021c 383extern void block_set_using (struct block *block,
fe978cb0 384 struct using_direct *using_decl,
9219021c
DC
385 struct obstack *obstack);
386
89a9d1b1
DC
387extern const struct block *block_static_block (const struct block *block);
388
1fcb5155
DC
389extern const struct block *block_global_block (const struct block *block);
390
5c4e30ca
DC
391extern struct block *allocate_block (struct obstack *obstack);
392
84a146c9
TT
393extern struct block *allocate_global_block (struct obstack *obstack);
394
43f3e411
DE
395extern void set_block_compunit_symtab (struct block *,
396 struct compunit_symtab *);
8157b174 397
63e43d3a
PMR
398/* Return a property to evaluate the static link associated to BLOCK.
399
400 In the context of nested functions (available in Pascal, Ada and GNU C, for
401 instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
402 function is a way to get the frame corresponding to the enclosing function.
403
404 Note that only objfile-owned and function-level blocks can have a static
405 link. Return NULL if there is no such property. */
406
407extern struct dynamic_prop *block_static_link (const struct block *block);
408
8157b174
TT
409/* A block iterator. This structure should be treated as though it
410 were opaque; it is only defined here because we want to support
411 stack allocation of iterators. */
412
413struct block_iterator
414{
b5b04b5b 415 /* If we're iterating over a single block, this holds the block.
43f3e411 416 Otherwise, it holds the canonical compunit. */
b5b04b5b
TT
417
418 union
419 {
43f3e411 420 struct compunit_symtab *compunit_symtab;
b5b04b5b
TT
421 const struct block *block;
422 } d;
423
424 /* If we're iterating over a single block, this is always -1.
425 Otherwise, it holds the index of the current "included" symtab in
426 the canonical symtab (that is, d.symtab->includes[idx]), with -1
427 meaning the canonical symtab itself. */
428
429 int idx;
430
431 /* Which block, either static or global, to iterate over. If this
432 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
433 This is used to select which field of 'd' is in use. */
434
435 enum block_enum which;
436
b026f593 437 /* The underlying multidictionary iterator. */
8157b174 438
b026f593 439 struct mdict_iterator mdict_iter;
8157b174
TT
440};
441
442/* Initialize ITERATOR to point at the first symbol in BLOCK, and
443 return that first symbol, or NULL if BLOCK is empty. */
444
445extern struct symbol *block_iterator_first (const struct block *block,
446 struct block_iterator *iterator);
447
448/* Advance ITERATOR, and return the next symbol, or NULL if there are
449 no more symbols. Don't call this if you've previously received
450 NULL from block_iterator_first or block_iterator_next on this
451 iteration. */
452
453extern struct symbol *block_iterator_next (struct block_iterator *iterator);
454
8157b174 455/* Initialize ITERATOR to point at the first symbol in BLOCK whose
987012b8 456 search_name () matches NAME, and return that first symbol, or
b5ec771e 457 NULL if there are no such symbols. */
8157b174
TT
458
459extern struct symbol *block_iter_match_first (const struct block *block,
b5ec771e 460 const lookup_name_info &name,
8157b174
TT
461 struct block_iterator *iterator);
462
463/* Advance ITERATOR to point at the next symbol in BLOCK whose
987012b8 464 search_name () matches NAME, or NULL if there are no more such
b5ec771e 465 symbols. Don't call this if you've previously received NULL from
8157b174
TT
466 block_iterator_match_first or block_iterator_match_next on this
467 iteration. And don't call it unless ITERATOR was created by a
b5ec771e 468 previous call to block_iter_match_first with the same NAME. */
8157b174 469
b5ec771e
PA
470extern struct symbol *block_iter_match_next
471 (const lookup_name_info &name, struct block_iterator *iterator);
8157b174 472
de82891c
TV
473/* Return true if symbol A is the best match possible for DOMAIN. */
474
475extern bool best_symbol (struct symbol *a, const domain_enum domain);
476
477/* Return symbol B if it is a better match than symbol A for DOMAIN.
478 Otherwise return A. */
479
480extern struct symbol *better_symbol (struct symbol *a, struct symbol *b,
481 const domain_enum domain);
482
16b2eaa1
DE
483/* Search BLOCK for symbol NAME in DOMAIN. */
484
485extern struct symbol *block_lookup_symbol (const struct block *block,
486 const char *name,
de63c46b 487 symbol_name_match_type match_type,
16b2eaa1
DE
488 const domain_enum domain);
489
ba715d7f
JK
490/* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
491 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
492 one iterates all global/static blocks of an objfile. */
493
494extern struct symbol *block_lookup_symbol_primary (const struct block *block,
495 const char *name,
496 const domain_enum domain);
497
b2e2f908
DE
498/* The type of the MATCHER argument to block_find_symbol. */
499
500typedef int (block_symbol_matcher_ftype) (struct symbol *, void *);
501
502/* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
503 DATA is passed unchanged to MATCHER.
504 BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
505
506extern struct symbol *block_find_symbol (const struct block *block,
507 const char *name,
508 const domain_enum domain,
509 block_symbol_matcher_ftype *matcher,
510 void *data);
511
512/* A matcher function for block_find_symbol to find only symbols with
513 non-opaque types. */
514
515extern int block_find_non_opaque_type (struct symbol *sym, void *data);
516
517/* A matcher function for block_find_symbol to prefer symbols with
518 non-opaque types. The way to use this function is as follows:
519
520 struct symbol *with_opaque = NULL;
521 struct symbol *sym
522 = block_find_symbol (block, name, domain,
dda83cd7 523 block_find_non_opaque_type_preferred, &with_opaque);
b2e2f908
DE
524
525 At this point if SYM is non-NULL then a non-opaque type has been found.
526 Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
527 Otherwise, the symbol was not found. */
528
529extern int block_find_non_opaque_type_preferred (struct symbol *sym,
530 void *data);
531
a023a30f
DE
532/* Macro to loop through all symbols in BLOCK, in no particular
533 order. ITER helps keep track of the iteration, and must be a
8157b174
TT
534 struct block_iterator. SYM points to the current symbol. */
535
536#define ALL_BLOCK_SYMBOLS(block, iter, sym) \
537 for ((sym) = block_iterator_first ((block), &(iter)); \
538 (sym); \
539 (sym) = block_iterator_next (&(iter)))
540
b5ec771e
PA
541/* Macro to loop through all symbols in BLOCK with a name that matches
542 NAME, in no particular order. ITER helps keep track of the
543 iteration, and must be a struct block_iterator. SYM points to the
544 current symbol. */
358d6ab3
DE
545
546#define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
b5ec771e 547 for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
358d6ab3 548 (sym) != NULL; \
b5ec771e 549 (sym) = block_iter_match_next ((name), &(iter)))
358d6ab3 550
26457a9c
KB
551/* Given a vector of pairs, allocate and build an obstack allocated
552 blockranges struct for a block. */
553struct blockranges *make_blockranges (struct objfile *objfile,
dda83cd7 554 const std::vector<blockrange> &rangevec);
26457a9c 555
fe898f56 556#endif /* BLOCK_H */