Commit | Line | Data |
---|---|---|
fe898f56 DC |
1 | /* Block-related functions for the GNU debugger, GDB. |
2 | ||
42a4f53d | 3 | Copyright (C) 2003-2019 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 | #include "defs.h" | |
21 | #include "block.h" | |
4de283e4 TT |
22 | #include "symtab.h" |
23 | #include "symfile.h" | |
d55e5aa6 | 24 | #include "gdb_obstack.h" |
4de283e4 TT |
25 | #include "cp-support.h" |
26 | #include "addrmap.h" | |
8e3b41a9 | 27 | #include "gdbtypes.h" |
1994afbf | 28 | #include "objfiles.h" |
9219021c DC |
29 | |
30 | /* This is used by struct block to store namespace-related info for | |
31 | C++ files, namely using declarations and the current namespace in | |
32 | scope. */ | |
33 | ||
fd90ace4 | 34 | struct block_namespace_info : public allocate_on_obstack |
9219021c | 35 | { |
fd90ace4 YQ |
36 | const char *scope = nullptr; |
37 | struct using_direct *using_decl = nullptr; | |
9219021c DC |
38 | }; |
39 | ||
40 | static void block_initialize_namespace (struct block *block, | |
41 | struct obstack *obstack); | |
fe898f56 | 42 | |
1994afbf DE |
43 | /* See block.h. */ |
44 | ||
45 | struct objfile * | |
46 | block_objfile (const struct block *block) | |
47 | { | |
48 | const struct global_block *global_block; | |
49 | ||
50 | if (BLOCK_FUNCTION (block) != NULL) | |
51 | return symbol_objfile (BLOCK_FUNCTION (block)); | |
52 | ||
53 | global_block = (struct global_block *) block_global_block (block); | |
54 | return COMPUNIT_OBJFILE (global_block->compunit_symtab); | |
55 | } | |
56 | ||
57 | /* See block. */ | |
58 | ||
59 | struct gdbarch * | |
60 | block_gdbarch (const struct block *block) | |
61 | { | |
62 | if (BLOCK_FUNCTION (block) != NULL) | |
63 | return symbol_arch (BLOCK_FUNCTION (block)); | |
64 | ||
65 | return get_objfile_arch (block_objfile (block)); | |
66 | } | |
67 | ||
fe898f56 DC |
68 | /* Return Nonzero if block a is lexically nested within block b, |
69 | or if a and b have the same pc range. | |
4a64f543 | 70 | Return zero otherwise. */ |
fe898f56 DC |
71 | |
72 | int | |
0cf566ec | 73 | contained_in (const struct block *a, const struct block *b) |
fe898f56 DC |
74 | { |
75 | if (!a || !b) | |
76 | return 0; | |
edb3359d DJ |
77 | |
78 | do | |
79 | { | |
80 | if (a == b) | |
81 | return 1; | |
82 | a = BLOCK_SUPERBLOCK (a); | |
83 | } | |
84 | while (a != NULL); | |
85 | ||
86 | return 0; | |
fe898f56 DC |
87 | } |
88 | ||
89 | ||
90 | /* Return the symbol for the function which contains a specified | |
7f0df278 DJ |
91 | lexical block, described by a struct block BL. The return value |
92 | will not be an inlined function; the containing function will be | |
93 | returned instead. */ | |
fe898f56 DC |
94 | |
95 | struct symbol * | |
7f0df278 | 96 | block_linkage_function (const struct block *bl) |
fe898f56 | 97 | { |
edb3359d DJ |
98 | while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl)) |
99 | && BLOCK_SUPERBLOCK (bl) != NULL) | |
fe898f56 DC |
100 | bl = BLOCK_SUPERBLOCK (bl); |
101 | ||
102 | return BLOCK_FUNCTION (bl); | |
103 | } | |
104 | ||
f8eba3c6 TT |
105 | /* Return the symbol for the function which contains a specified |
106 | block, described by a struct block BL. The return value will be | |
107 | the closest enclosing function, which might be an inline | |
108 | function. */ | |
109 | ||
110 | struct symbol * | |
111 | block_containing_function (const struct block *bl) | |
112 | { | |
113 | while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL) | |
114 | bl = BLOCK_SUPERBLOCK (bl); | |
115 | ||
116 | return BLOCK_FUNCTION (bl); | |
117 | } | |
118 | ||
edb3359d DJ |
119 | /* Return one if BL represents an inlined function. */ |
120 | ||
121 | int | |
122 | block_inlined_p (const struct block *bl) | |
123 | { | |
124 | return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl)); | |
125 | } | |
126 | ||
9703b513 TT |
127 | /* A helper function that checks whether PC is in the blockvector BL. |
128 | It returns the containing block if there is one, or else NULL. */ | |
fe898f56 | 129 | |
582942f4 | 130 | static const struct block * |
346d1dfe | 131 | find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc) |
fe898f56 | 132 | { |
582942f4 | 133 | const struct block *b; |
b59661bd | 134 | int bot, top, half; |
fe898f56 | 135 | |
801e3a5b JB |
136 | /* If we have an addrmap mapping code addresses to blocks, then use |
137 | that. */ | |
138 | if (BLOCKVECTOR_MAP (bl)) | |
582942f4 | 139 | return (const struct block *) addrmap_find (BLOCKVECTOR_MAP (bl), pc); |
801e3a5b JB |
140 | |
141 | /* Otherwise, use binary search to find the last block that starts | |
6ac9ef80 DE |
142 | before PC. |
143 | Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1. | |
144 | They both have the same START,END values. | |
145 | Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the | |
146 | fact that this choice was made was subtle, now we make it explicit. */ | |
147 | gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2); | |
148 | bot = STATIC_BLOCK; | |
fe898f56 DC |
149 | top = BLOCKVECTOR_NBLOCKS (bl); |
150 | ||
151 | while (top - bot > 1) | |
152 | { | |
153 | half = (top - bot + 1) >> 1; | |
154 | b = BLOCKVECTOR_BLOCK (bl, bot + half); | |
155 | if (BLOCK_START (b) <= pc) | |
156 | bot += half; | |
157 | else | |
158 | top = bot + half; | |
159 | } | |
160 | ||
161 | /* Now search backward for a block that ends after PC. */ | |
162 | ||
6ac9ef80 | 163 | while (bot >= STATIC_BLOCK) |
fe898f56 DC |
164 | { |
165 | b = BLOCKVECTOR_BLOCK (bl, bot); | |
166 | if (BLOCK_END (b) > pc) | |
9703b513 | 167 | return b; |
fe898f56 DC |
168 | bot--; |
169 | } | |
9703b513 TT |
170 | |
171 | return NULL; | |
172 | } | |
173 | ||
174 | /* Return the blockvector immediately containing the innermost lexical | |
175 | block containing the specified pc value and section, or 0 if there | |
176 | is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we | |
177 | don't pass this information back to the caller. */ | |
178 | ||
346d1dfe | 179 | const struct blockvector * |
9703b513 | 180 | blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section, |
43f3e411 DE |
181 | const struct block **pblock, |
182 | struct compunit_symtab *cust) | |
9703b513 | 183 | { |
346d1dfe | 184 | const struct blockvector *bl; |
582942f4 | 185 | const struct block *b; |
9703b513 | 186 | |
43f3e411 | 187 | if (cust == NULL) |
9703b513 TT |
188 | { |
189 | /* First search all symtabs for one whose file contains our pc */ | |
43f3e411 DE |
190 | cust = find_pc_sect_compunit_symtab (pc, section); |
191 | if (cust == NULL) | |
9703b513 TT |
192 | return 0; |
193 | } | |
194 | ||
43f3e411 | 195 | bl = COMPUNIT_BLOCKVECTOR (cust); |
9703b513 TT |
196 | |
197 | /* Then search that symtab for the smallest block that wins. */ | |
198 | b = find_block_in_blockvector (bl, pc); | |
199 | if (b == NULL) | |
200 | return NULL; | |
201 | ||
202 | if (pblock) | |
203 | *pblock = b; | |
204 | return bl; | |
205 | } | |
206 | ||
207 | /* Return true if the blockvector BV contains PC, false otherwise. */ | |
208 | ||
209 | int | |
346d1dfe | 210 | blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc) |
9703b513 TT |
211 | { |
212 | return find_block_in_blockvector (bv, pc) != NULL; | |
fe898f56 DC |
213 | } |
214 | ||
8e3b41a9 JK |
215 | /* Return call_site for specified PC in GDBARCH. PC must match exactly, it |
216 | must be the next instruction after call (or after tail call jump). Throw | |
217 | NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */ | |
218 | ||
219 | struct call_site * | |
220 | call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc) | |
221 | { | |
43f3e411 | 222 | struct compunit_symtab *cust; |
8e3b41a9 JK |
223 | void **slot = NULL; |
224 | ||
225 | /* -1 as tail call PC can be already after the compilation unit range. */ | |
43f3e411 | 226 | cust = find_pc_compunit_symtab (pc - 1); |
8e3b41a9 | 227 | |
43f3e411 DE |
228 | if (cust != NULL && COMPUNIT_CALL_SITE_HTAB (cust) != NULL) |
229 | slot = htab_find_slot (COMPUNIT_CALL_SITE_HTAB (cust), &pc, NO_INSERT); | |
8e3b41a9 JK |
230 | |
231 | if (slot == NULL) | |
232 | { | |
7cbd4a93 | 233 | struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc); |
8e3b41a9 JK |
234 | |
235 | /* DW_TAG_gnu_call_site will be missing just if GCC could not determine | |
236 | the call target. */ | |
237 | throw_error (NO_ENTRY_VALUE_ERROR, | |
216f72a1 JK |
238 | _("DW_OP_entry_value resolving cannot find " |
239 | "DW_TAG_call_site %s in %s"), | |
8e3b41a9 | 240 | paddress (gdbarch, pc), |
7cbd4a93 | 241 | (msym.minsym == NULL ? "???" |
efd66ac6 | 242 | : MSYMBOL_PRINT_NAME (msym.minsym))); |
8e3b41a9 JK |
243 | } |
244 | ||
9a3c8263 | 245 | return (struct call_site *) *slot; |
8e3b41a9 JK |
246 | } |
247 | ||
fe898f56 DC |
248 | /* Return the blockvector immediately containing the innermost lexical block |
249 | containing the specified pc value, or 0 if there is none. | |
250 | Backward compatibility, no section. */ | |
251 | ||
346d1dfe | 252 | const struct blockvector * |
3977b71f | 253 | blockvector_for_pc (CORE_ADDR pc, const struct block **pblock) |
fe898f56 DC |
254 | { |
255 | return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc), | |
801e3a5b | 256 | pblock, NULL); |
fe898f56 DC |
257 | } |
258 | ||
259 | /* Return the innermost lexical block containing the specified pc value | |
260 | in the specified section, or 0 if there is none. */ | |
261 | ||
3977b71f | 262 | const struct block * |
714835d5 | 263 | block_for_pc_sect (CORE_ADDR pc, struct obj_section *section) |
fe898f56 | 264 | { |
346d1dfe | 265 | const struct blockvector *bl; |
3977b71f | 266 | const struct block *b; |
fe898f56 | 267 | |
801e3a5b | 268 | bl = blockvector_for_pc_sect (pc, section, &b, NULL); |
fe898f56 | 269 | if (bl) |
801e3a5b | 270 | return b; |
fe898f56 DC |
271 | return 0; |
272 | } | |
273 | ||
274 | /* Return the innermost lexical block containing the specified pc value, | |
275 | or 0 if there is none. Backward compatibility, no section. */ | |
276 | ||
3977b71f | 277 | const struct block * |
b59661bd | 278 | block_for_pc (CORE_ADDR pc) |
fe898f56 DC |
279 | { |
280 | return block_for_pc_sect (pc, find_pc_mapped_section (pc)); | |
281 | } | |
9219021c | 282 | |
1fcb5155 DC |
283 | /* Now come some functions designed to deal with C++ namespace issues. |
284 | The accessors are safe to use even in the non-C++ case. */ | |
285 | ||
286 | /* This returns the namespace that BLOCK is enclosed in, or "" if it | |
287 | isn't enclosed in a namespace at all. This travels the chain of | |
288 | superblocks looking for a scope, if necessary. */ | |
289 | ||
290 | const char * | |
291 | block_scope (const struct block *block) | |
292 | { | |
293 | for (; block != NULL; block = BLOCK_SUPERBLOCK (block)) | |
294 | { | |
295 | if (BLOCK_NAMESPACE (block) != NULL | |
296 | && BLOCK_NAMESPACE (block)->scope != NULL) | |
297 | return BLOCK_NAMESPACE (block)->scope; | |
298 | } | |
299 | ||
300 | return ""; | |
301 | } | |
9219021c DC |
302 | |
303 | /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via | |
304 | OBSTACK. (It won't make a copy of SCOPE, however, so that already | |
305 | has to be allocated correctly.) */ | |
306 | ||
307 | void | |
308 | block_set_scope (struct block *block, const char *scope, | |
309 | struct obstack *obstack) | |
310 | { | |
311 | block_initialize_namespace (block, obstack); | |
312 | ||
313 | BLOCK_NAMESPACE (block)->scope = scope; | |
314 | } | |
315 | ||
27aa8d6a | 316 | /* This returns the using directives list associated with BLOCK, if |
1fcb5155 DC |
317 | any. */ |
318 | ||
1fcb5155 DC |
319 | struct using_direct * |
320 | block_using (const struct block *block) | |
321 | { | |
27aa8d6a | 322 | if (block == NULL || BLOCK_NAMESPACE (block) == NULL) |
1fcb5155 DC |
323 | return NULL; |
324 | else | |
fe978cb0 | 325 | return BLOCK_NAMESPACE (block)->using_decl; |
1fcb5155 DC |
326 | } |
327 | ||
9219021c DC |
328 | /* Set BLOCK's using member to USING; if needed, allocate memory via |
329 | OBSTACK. (It won't make a copy of USING, however, so that already | |
330 | has to be allocated correctly.) */ | |
331 | ||
332 | void | |
333 | block_set_using (struct block *block, | |
fe978cb0 | 334 | struct using_direct *using_decl, |
9219021c DC |
335 | struct obstack *obstack) |
336 | { | |
337 | block_initialize_namespace (block, obstack); | |
338 | ||
fe978cb0 | 339 | BLOCK_NAMESPACE (block)->using_decl = using_decl; |
9219021c DC |
340 | } |
341 | ||
342 | /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and | |
343 | ititialize its members to zero. */ | |
344 | ||
345 | static void | |
346 | block_initialize_namespace (struct block *block, struct obstack *obstack) | |
347 | { | |
348 | if (BLOCK_NAMESPACE (block) == NULL) | |
fd90ace4 | 349 | BLOCK_NAMESPACE (block) = new (obstack) struct block_namespace_info (); |
9219021c | 350 | } |
89a9d1b1 DC |
351 | |
352 | /* Return the static block associated to BLOCK. Return NULL if block | |
353 | is NULL or if block is a global block. */ | |
354 | ||
355 | const struct block * | |
356 | block_static_block (const struct block *block) | |
357 | { | |
358 | if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL) | |
359 | return NULL; | |
360 | ||
361 | while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL) | |
362 | block = BLOCK_SUPERBLOCK (block); | |
363 | ||
364 | return block; | |
365 | } | |
1fcb5155 DC |
366 | |
367 | /* Return the static block associated to BLOCK. Return NULL if block | |
368 | is NULL. */ | |
369 | ||
370 | const struct block * | |
371 | block_global_block (const struct block *block) | |
372 | { | |
373 | if (block == NULL) | |
374 | return NULL; | |
375 | ||
376 | while (BLOCK_SUPERBLOCK (block) != NULL) | |
377 | block = BLOCK_SUPERBLOCK (block); | |
378 | ||
379 | return block; | |
380 | } | |
5c4e30ca DC |
381 | |
382 | /* Allocate a block on OBSTACK, and initialize its elements to | |
383 | zero/NULL. This is useful for creating "dummy" blocks that don't | |
384 | correspond to actual source files. | |
385 | ||
b026f593 | 386 | Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a |
5c4e30ca | 387 | valid value. If you really don't want the block to have a |
b026f593 | 388 | dictionary, then you should subsequently set its BLOCK_MULTIDICT to |
5c4e30ca DC |
389 | dict_create_linear (obstack, NULL). */ |
390 | ||
391 | struct block * | |
392 | allocate_block (struct obstack *obstack) | |
393 | { | |
4c35218e | 394 | struct block *bl = OBSTACK_ZALLOC (obstack, struct block); |
5c4e30ca DC |
395 | |
396 | return bl; | |
397 | } | |
8157b174 | 398 | |
84a146c9 TT |
399 | /* Allocate a global block. */ |
400 | ||
401 | struct block * | |
402 | allocate_global_block (struct obstack *obstack) | |
403 | { | |
404 | struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block); | |
405 | ||
406 | return &bl->block; | |
407 | } | |
408 | ||
43f3e411 | 409 | /* Set the compunit of the global block. */ |
84a146c9 TT |
410 | |
411 | void | |
43f3e411 | 412 | set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu) |
84a146c9 TT |
413 | { |
414 | struct global_block *gb; | |
415 | ||
416 | gdb_assert (BLOCK_SUPERBLOCK (block) == NULL); | |
417 | gb = (struct global_block *) block; | |
43f3e411 DE |
418 | gdb_assert (gb->compunit_symtab == NULL); |
419 | gb->compunit_symtab = cu; | |
84a146c9 TT |
420 | } |
421 | ||
63e43d3a PMR |
422 | /* See block.h. */ |
423 | ||
424 | struct dynamic_prop * | |
425 | block_static_link (const struct block *block) | |
426 | { | |
427 | struct objfile *objfile = block_objfile (block); | |
428 | ||
429 | /* Only objfile-owned blocks that materialize top function scopes can have | |
430 | static links. */ | |
431 | if (objfile == NULL || BLOCK_FUNCTION (block) == NULL) | |
432 | return NULL; | |
433 | ||
434 | return (struct dynamic_prop *) objfile_lookup_static_link (objfile, block); | |
435 | } | |
436 | ||
43f3e411 | 437 | /* Return the compunit of the global block. */ |
b5b04b5b | 438 | |
43f3e411 DE |
439 | static struct compunit_symtab * |
440 | get_block_compunit_symtab (const struct block *block) | |
b5b04b5b TT |
441 | { |
442 | struct global_block *gb; | |
443 | ||
444 | gdb_assert (BLOCK_SUPERBLOCK (block) == NULL); | |
445 | gb = (struct global_block *) block; | |
43f3e411 DE |
446 | gdb_assert (gb->compunit_symtab != NULL); |
447 | return gb->compunit_symtab; | |
b5b04b5b TT |
448 | } |
449 | ||
8157b174 TT |
450 | \f |
451 | ||
b5b04b5b TT |
452 | /* Initialize a block iterator, either to iterate over a single block, |
453 | or, for static and global blocks, all the included symtabs as | |
454 | well. */ | |
455 | ||
456 | static void | |
457 | initialize_block_iterator (const struct block *block, | |
458 | struct block_iterator *iter) | |
459 | { | |
460 | enum block_enum which; | |
43f3e411 | 461 | struct compunit_symtab *cu; |
b5b04b5b TT |
462 | |
463 | iter->idx = -1; | |
464 | ||
465 | if (BLOCK_SUPERBLOCK (block) == NULL) | |
466 | { | |
467 | which = GLOBAL_BLOCK; | |
43f3e411 | 468 | cu = get_block_compunit_symtab (block); |
b5b04b5b TT |
469 | } |
470 | else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL) | |
471 | { | |
472 | which = STATIC_BLOCK; | |
43f3e411 | 473 | cu = get_block_compunit_symtab (BLOCK_SUPERBLOCK (block)); |
b5b04b5b TT |
474 | } |
475 | else | |
476 | { | |
477 | iter->d.block = block; | |
478 | /* A signal value meaning that we're iterating over a single | |
479 | block. */ | |
480 | iter->which = FIRST_LOCAL_BLOCK; | |
481 | return; | |
482 | } | |
483 | ||
484 | /* If this is an included symtab, find the canonical includer and | |
485 | use it instead. */ | |
43f3e411 DE |
486 | while (cu->user != NULL) |
487 | cu = cu->user; | |
b5b04b5b TT |
488 | |
489 | /* Putting this check here simplifies the logic of the iterator | |
490 | functions. If there are no included symtabs, we only need to | |
491 | search a single block, so we might as well just do that | |
492 | directly. */ | |
43f3e411 | 493 | if (cu->includes == NULL) |
b5b04b5b TT |
494 | { |
495 | iter->d.block = block; | |
496 | /* A signal value meaning that we're iterating over a single | |
497 | block. */ | |
498 | iter->which = FIRST_LOCAL_BLOCK; | |
499 | } | |
500 | else | |
501 | { | |
43f3e411 | 502 | iter->d.compunit_symtab = cu; |
b5b04b5b TT |
503 | iter->which = which; |
504 | } | |
505 | } | |
506 | ||
43f3e411 | 507 | /* A helper function that finds the current compunit over whose static |
b5b04b5b TT |
508 | or global block we should iterate. */ |
509 | ||
43f3e411 DE |
510 | static struct compunit_symtab * |
511 | find_iterator_compunit_symtab (struct block_iterator *iterator) | |
b5b04b5b TT |
512 | { |
513 | if (iterator->idx == -1) | |
43f3e411 DE |
514 | return iterator->d.compunit_symtab; |
515 | return iterator->d.compunit_symtab->includes[iterator->idx]; | |
b5b04b5b TT |
516 | } |
517 | ||
518 | /* Perform a single step for a plain block iterator, iterating across | |
519 | symbol tables as needed. Returns the next symbol, or NULL when | |
520 | iteration is complete. */ | |
521 | ||
522 | static struct symbol * | |
523 | block_iterator_step (struct block_iterator *iterator, int first) | |
524 | { | |
525 | struct symbol *sym; | |
526 | ||
527 | gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); | |
528 | ||
529 | while (1) | |
530 | { | |
531 | if (first) | |
532 | { | |
43f3e411 DE |
533 | struct compunit_symtab *cust |
534 | = find_iterator_compunit_symtab (iterator); | |
b5b04b5b TT |
535 | const struct block *block; |
536 | ||
537 | /* Iteration is complete. */ | |
43f3e411 | 538 | if (cust == NULL) |
b5b04b5b TT |
539 | return NULL; |
540 | ||
43f3e411 | 541 | block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), |
439247b6 | 542 | iterator->which); |
b026f593 KS |
543 | sym = mdict_iterator_first (BLOCK_MULTIDICT (block), |
544 | &iterator->mdict_iter); | |
b5b04b5b TT |
545 | } |
546 | else | |
b026f593 | 547 | sym = mdict_iterator_next (&iterator->mdict_iter); |
b5b04b5b TT |
548 | |
549 | if (sym != NULL) | |
550 | return sym; | |
551 | ||
552 | /* We have finished iterating the appropriate block of one | |
553 | symtab. Now advance to the next symtab and begin iteration | |
554 | there. */ | |
555 | ++iterator->idx; | |
556 | first = 1; | |
557 | } | |
558 | } | |
559 | ||
8157b174 TT |
560 | /* See block.h. */ |
561 | ||
562 | struct symbol * | |
563 | block_iterator_first (const struct block *block, | |
564 | struct block_iterator *iterator) | |
565 | { | |
b5b04b5b TT |
566 | initialize_block_iterator (block, iterator); |
567 | ||
568 | if (iterator->which == FIRST_LOCAL_BLOCK) | |
b026f593 | 569 | return mdict_iterator_first (block->multidict, &iterator->mdict_iter); |
b5b04b5b TT |
570 | |
571 | return block_iterator_step (iterator, 1); | |
8157b174 TT |
572 | } |
573 | ||
574 | /* See block.h. */ | |
575 | ||
576 | struct symbol * | |
577 | block_iterator_next (struct block_iterator *iterator) | |
578 | { | |
b5b04b5b | 579 | if (iterator->which == FIRST_LOCAL_BLOCK) |
b026f593 | 580 | return mdict_iterator_next (&iterator->mdict_iter); |
b5b04b5b TT |
581 | |
582 | return block_iterator_step (iterator, 0); | |
583 | } | |
584 | ||
b5b04b5b TT |
585 | /* Perform a single step for a "match" block iterator, iterating |
586 | across symbol tables as needed. Returns the next symbol, or NULL | |
587 | when iteration is complete. */ | |
588 | ||
589 | static struct symbol * | |
590 | block_iter_match_step (struct block_iterator *iterator, | |
b5ec771e | 591 | const lookup_name_info &name, |
b5b04b5b TT |
592 | int first) |
593 | { | |
594 | struct symbol *sym; | |
595 | ||
596 | gdb_assert (iterator->which != FIRST_LOCAL_BLOCK); | |
597 | ||
598 | while (1) | |
599 | { | |
600 | if (first) | |
601 | { | |
43f3e411 DE |
602 | struct compunit_symtab *cust |
603 | = find_iterator_compunit_symtab (iterator); | |
b5b04b5b TT |
604 | const struct block *block; |
605 | ||
606 | /* Iteration is complete. */ | |
43f3e411 | 607 | if (cust == NULL) |
b5b04b5b TT |
608 | return NULL; |
609 | ||
43f3e411 | 610 | block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), |
439247b6 | 611 | iterator->which); |
b026f593 KS |
612 | sym = mdict_iter_match_first (BLOCK_MULTIDICT (block), name, |
613 | &iterator->mdict_iter); | |
b5b04b5b TT |
614 | } |
615 | else | |
b026f593 | 616 | sym = mdict_iter_match_next (name, &iterator->mdict_iter); |
b5b04b5b TT |
617 | |
618 | if (sym != NULL) | |
619 | return sym; | |
620 | ||
621 | /* We have finished iterating the appropriate block of one | |
622 | symtab. Now advance to the next symtab and begin iteration | |
623 | there. */ | |
624 | ++iterator->idx; | |
625 | first = 1; | |
626 | } | |
8157b174 TT |
627 | } |
628 | ||
629 | /* See block.h. */ | |
630 | ||
631 | struct symbol * | |
632 | block_iter_match_first (const struct block *block, | |
b5ec771e | 633 | const lookup_name_info &name, |
8157b174 TT |
634 | struct block_iterator *iterator) |
635 | { | |
b5b04b5b TT |
636 | initialize_block_iterator (block, iterator); |
637 | ||
638 | if (iterator->which == FIRST_LOCAL_BLOCK) | |
b026f593 KS |
639 | return mdict_iter_match_first (block->multidict, name, |
640 | &iterator->mdict_iter); | |
b5b04b5b | 641 | |
b5ec771e | 642 | return block_iter_match_step (iterator, name, 1); |
8157b174 TT |
643 | } |
644 | ||
645 | /* See block.h. */ | |
646 | ||
647 | struct symbol * | |
b5ec771e | 648 | block_iter_match_next (const lookup_name_info &name, |
8157b174 TT |
649 | struct block_iterator *iterator) |
650 | { | |
b5b04b5b | 651 | if (iterator->which == FIRST_LOCAL_BLOCK) |
b026f593 | 652 | return mdict_iter_match_next (name, &iterator->mdict_iter); |
b5b04b5b | 653 | |
b5ec771e | 654 | return block_iter_match_step (iterator, name, 0); |
8157b174 | 655 | } |
16b2eaa1 DE |
656 | |
657 | /* See block.h. | |
658 | ||
659 | Note that if NAME is the demangled form of a C++ symbol, we will fail | |
660 | to find a match during the binary search of the non-encoded names, but | |
661 | for now we don't worry about the slight inefficiency of looking for | |
662 | a match we'll never find, since it will go pretty quick. Once the | |
663 | binary search terminates, we drop through and do a straight linear | |
664 | search on the symbols. Each symbol which is marked as being a ObjC/C++ | |
665 | symbol (language_cplus or language_objc set) has both the encoded and | |
666 | non-encoded names tested for a match. */ | |
667 | ||
668 | struct symbol * | |
669 | block_lookup_symbol (const struct block *block, const char *name, | |
de63c46b | 670 | symbol_name_match_type match_type, |
16b2eaa1 DE |
671 | const domain_enum domain) |
672 | { | |
673 | struct block_iterator iter; | |
674 | struct symbol *sym; | |
675 | ||
de63c46b | 676 | lookup_name_info lookup_name (name, match_type); |
b5ec771e | 677 | |
16b2eaa1 DE |
678 | if (!BLOCK_FUNCTION (block)) |
679 | { | |
ee93cd5e KS |
680 | struct symbol *other = NULL; |
681 | ||
b5ec771e | 682 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
16b2eaa1 | 683 | { |
ee93cd5e KS |
684 | if (SYMBOL_DOMAIN (sym) == domain) |
685 | return sym; | |
686 | /* This is a bit of a hack, but symbol_matches_domain might ignore | |
687 | STRUCT vs VAR domain symbols. So if a matching symbol is found, | |
688 | make sure there is no "better" matching symbol, i.e., one with | |
689 | exactly the same domain. PR 16253. */ | |
16b2eaa1 DE |
690 | if (symbol_matches_domain (SYMBOL_LANGUAGE (sym), |
691 | SYMBOL_DOMAIN (sym), domain)) | |
ee93cd5e | 692 | other = sym; |
16b2eaa1 | 693 | } |
ee93cd5e | 694 | return other; |
16b2eaa1 DE |
695 | } |
696 | else | |
697 | { | |
698 | /* Note that parameter symbols do not always show up last in the | |
699 | list; this loop makes sure to take anything else other than | |
700 | parameter symbols first; it only uses parameter symbols as a | |
701 | last resort. Note that this only takes up extra computation | |
ee93cd5e KS |
702 | time on a match. |
703 | It's hard to define types in the parameter list (at least in | |
704 | C/C++) so we don't do the same PR 16253 hack here that is done | |
705 | for the !BLOCK_FUNCTION case. */ | |
16b2eaa1 DE |
706 | |
707 | struct symbol *sym_found = NULL; | |
708 | ||
b5ec771e | 709 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
16b2eaa1 DE |
710 | { |
711 | if (symbol_matches_domain (SYMBOL_LANGUAGE (sym), | |
712 | SYMBOL_DOMAIN (sym), domain)) | |
713 | { | |
714 | sym_found = sym; | |
715 | if (!SYMBOL_IS_ARGUMENT (sym)) | |
716 | { | |
717 | break; | |
718 | } | |
719 | } | |
720 | } | |
721 | return (sym_found); /* Will be NULL if not found. */ | |
722 | } | |
723 | } | |
ba715d7f JK |
724 | |
725 | /* See block.h. */ | |
726 | ||
727 | struct symbol * | |
728 | block_lookup_symbol_primary (const struct block *block, const char *name, | |
729 | const domain_enum domain) | |
730 | { | |
ee93cd5e | 731 | struct symbol *sym, *other; |
b026f593 | 732 | struct mdict_iterator mdict_iter; |
ba715d7f | 733 | |
b5ec771e PA |
734 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
735 | ||
ba715d7f JK |
736 | /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */ |
737 | gdb_assert (BLOCK_SUPERBLOCK (block) == NULL | |
738 | || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL); | |
739 | ||
ee93cd5e | 740 | other = NULL; |
b026f593 KS |
741 | for (sym |
742 | = mdict_iter_match_first (block->multidict, lookup_name, &mdict_iter); | |
ba715d7f | 743 | sym != NULL; |
b026f593 | 744 | sym = mdict_iter_match_next (lookup_name, &mdict_iter)) |
ba715d7f | 745 | { |
ee93cd5e KS |
746 | if (SYMBOL_DOMAIN (sym) == domain) |
747 | return sym; | |
748 | ||
749 | /* This is a bit of a hack, but symbol_matches_domain might ignore | |
750 | STRUCT vs VAR domain symbols. So if a matching symbol is found, | |
751 | make sure there is no "better" matching symbol, i.e., one with | |
752 | exactly the same domain. PR 16253. */ | |
ba715d7f JK |
753 | if (symbol_matches_domain (SYMBOL_LANGUAGE (sym), |
754 | SYMBOL_DOMAIN (sym), domain)) | |
ee93cd5e | 755 | other = sym; |
ba715d7f JK |
756 | } |
757 | ||
ee93cd5e | 758 | return other; |
ba715d7f | 759 | } |
b2e2f908 DE |
760 | |
761 | /* See block.h. */ | |
762 | ||
763 | struct symbol * | |
764 | block_find_symbol (const struct block *block, const char *name, | |
765 | const domain_enum domain, | |
766 | block_symbol_matcher_ftype *matcher, void *data) | |
767 | { | |
768 | struct block_iterator iter; | |
769 | struct symbol *sym; | |
770 | ||
b5ec771e PA |
771 | lookup_name_info lookup_name (name, symbol_name_match_type::FULL); |
772 | ||
b2e2f908 DE |
773 | /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */ |
774 | gdb_assert (BLOCK_SUPERBLOCK (block) == NULL | |
775 | || BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL); | |
776 | ||
b5ec771e | 777 | ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym) |
b2e2f908 DE |
778 | { |
779 | /* MATCHER is deliberately called second here so that it never sees | |
780 | a non-domain-matching symbol. */ | |
781 | if (symbol_matches_domain (SYMBOL_LANGUAGE (sym), | |
782 | SYMBOL_DOMAIN (sym), domain) | |
783 | && matcher (sym, data)) | |
784 | return sym; | |
785 | } | |
786 | return NULL; | |
787 | } | |
788 | ||
789 | /* See block.h. */ | |
790 | ||
791 | int | |
792 | block_find_non_opaque_type (struct symbol *sym, void *data) | |
793 | { | |
794 | return !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)); | |
795 | } | |
796 | ||
797 | /* See block.h. */ | |
798 | ||
799 | int | |
800 | block_find_non_opaque_type_preferred (struct symbol *sym, void *data) | |
801 | { | |
9a3c8263 | 802 | struct symbol **best = (struct symbol **) data; |
b2e2f908 DE |
803 | |
804 | if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym))) | |
805 | return 1; | |
806 | *best = sym; | |
807 | return 0; | |
808 | } | |
26457a9c KB |
809 | |
810 | /* See block.h. */ | |
811 | ||
812 | struct blockranges * | |
813 | make_blockranges (struct objfile *objfile, | |
814 | const std::vector<blockrange> &rangevec) | |
815 | { | |
816 | struct blockranges *blr; | |
817 | size_t n = rangevec.size(); | |
818 | ||
819 | blr = (struct blockranges *) | |
820 | obstack_alloc (&objfile->objfile_obstack, | |
821 | sizeof (struct blockranges) | |
822 | + (n - 1) * sizeof (struct blockrange)); | |
823 | ||
824 | blr->nranges = n; | |
825 | for (int i = 0; i < n; i++) | |
826 | blr->range[i] = rangevec[i]; | |
827 | return blr; | |
828 | } | |
829 |