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