arm, objdump: print obsolote warning when 26-bit set in instructions
[binutils-gdb.git] / gdb / dictionary.c
blob6925ad4d46ce4067feae18d5796f9e802f79e830
1 /* Routines for name->symbol lookups in GDB.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
6 Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include <ctype.h>
24 #include "gdbsupport/gdb_obstack.h"
25 #include "symtab.h"
26 #include "buildsym.h"
27 #include "dictionary.h"
28 #include "gdbsupport/gdb-safe-ctype.h"
29 #include <unordered_map>
30 #include "language.h"
32 /* This file implements dictionaries, which are tables that associate
33 symbols to names. They are represented by an opaque type 'struct
34 dictionary'. That type has various internal implementations, which
35 you can choose between depending on what properties you need
36 (e.g. fast lookup, order-preserving, expandable).
38 Each dictionary starts with a 'virtual function table' that
39 contains the functions that actually implement the various
40 operations that dictionaries provide. (Note, however, that, for
41 the sake of client code, we also provide some functions that can be
42 implemented generically in terms of the functions in the vtable.)
44 To add a new dictionary implementation <impl>, what you should do
45 is:
47 * Add a new element DICT_<IMPL> to dict_type.
49 * Create a new structure dictionary_<impl>. If your new
50 implementation is a variant of an existing one, make sure that
51 their structs have the same initial data members. Define accessor
52 macros for your new data members.
54 * Implement all the functions in dict_vector as static functions,
55 whose name is the same as the corresponding member of dict_vector
56 plus _<impl>. You don't have to do this for those members where
57 you can reuse existing generic functions
58 (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
59 your new implementation is a variant of an existing implementation
60 and where the variant doesn't affect the member function in
61 question.
63 * Define a static const struct dict_vector dict_<impl>_vector.
65 * Define a function dict_create_<impl> to create these
66 gizmos. Add its declaration to dictionary.h.
68 To add a new operation <op> on all existing implementations, what
69 you should do is:
71 * Add a new member <op> to struct dict_vector.
73 * If there is useful generic behavior <op>, define a static
74 function <op>_something_informative that implements that behavior.
75 (E.g. add_symbol_nonexpandable, free_obstack.)
77 * For every implementation <impl> that should have its own specific
78 behavior for <op>, define a static function <op>_<impl>
79 implementing it.
81 * Modify all existing dict_vector_<impl>'s to include the appropriate
82 member.
84 * Define a function dict_<op> that looks up <op> in the dict_vector
85 and calls the appropriate function. Add a declaration for
86 dict_<op> to dictionary.h. */
88 /* An enum representing the various implementations of dictionaries.
89 Used only for debugging. */
91 enum dict_type
93 /* Symbols are stored in a fixed-size hash table. */
94 DICT_HASHED,
95 /* Symbols are stored in an expandable hash table. */
96 DICT_HASHED_EXPANDABLE,
97 /* Symbols are stored in a fixed-size array. */
98 DICT_LINEAR,
99 /* Symbols are stored in an expandable array. */
100 DICT_LINEAR_EXPANDABLE
103 /* The virtual function table. */
105 struct dict_vector
107 /* The type of the dictionary. This is only here to make debugging
108 a bit easier; it's not actually used. */
109 enum dict_type type;
110 /* The function to free a dictionary. */
111 void (*free) (struct dictionary *dict);
112 /* Add a symbol to a dictionary, if possible. */
113 void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
114 /* Iterator functions. */
115 struct symbol *(*iterator_first) (const struct dictionary *dict,
116 struct dict_iterator *iterator);
117 struct symbol *(*iterator_next) (struct dict_iterator *iterator);
118 /* Functions to iterate over symbols with a given name. */
119 struct symbol *(*iter_match_first) (const struct dictionary *dict,
120 const lookup_name_info &name,
121 struct dict_iterator *iterator);
122 struct symbol *(*iter_match_next) (const lookup_name_info &name,
123 struct dict_iterator *iterator);
124 /* A size function, for maint print symtabs. */
125 int (*size) (const struct dictionary *dict);
128 /* Now comes the structs used to store the data for different
129 implementations. If two implementations have data in common, put
130 the common data at the top of their structs, ordered in the same
131 way. */
133 struct dictionary_hashed
135 int nbuckets;
136 struct symbol **buckets;
139 struct dictionary_hashed_expandable
141 /* How many buckets we currently have. */
142 int nbuckets;
143 struct symbol **buckets;
144 /* How many syms we currently have; we need this so we will know
145 when to add more buckets. */
146 int nsyms;
149 struct dictionary_linear
151 int nsyms;
152 struct symbol **syms;
155 struct dictionary_linear_expandable
157 /* How many symbols we currently have. */
158 int nsyms;
159 struct symbol **syms;
160 /* How many symbols we can store before needing to reallocate. */
161 int capacity;
164 /* And now, the star of our show. */
166 struct dictionary
168 const struct language_defn *language;
169 const struct dict_vector *vector;
170 union
172 struct dictionary_hashed hashed;
173 struct dictionary_hashed_expandable hashed_expandable;
174 struct dictionary_linear linear;
175 struct dictionary_linear_expandable linear_expandable;
177 data;
180 /* Accessor macros. */
182 #define DICT_VECTOR(d) (d)->vector
183 #define DICT_LANGUAGE(d) (d)->language
185 /* These can be used for DICT_HASHED_EXPANDABLE, too. */
187 #define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
188 #define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
189 #define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
191 #define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
193 /* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
195 #define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
196 #define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
197 #define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
199 #define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
200 (d)->data.linear_expandable.capacity
202 /* The initial size of a DICT_*_EXPANDABLE dictionary. */
204 #define DICT_EXPANDABLE_INITIAL_CAPACITY 10
206 /* This calculates the number of buckets we'll use in a hashtable,
207 given the number of symbols that it will contain. */
209 #define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
211 /* Accessor macros for dict_iterators; they're here rather than
212 dictionary.h because code elsewhere should treat dict_iterators as
213 opaque. */
215 /* The dictionary that the iterator is associated to. */
216 #define DICT_ITERATOR_DICT(iter) (iter)->dict
217 /* For linear dictionaries, the index of the last symbol returned; for
218 hashed dictionaries, the bucket of the last symbol returned. */
219 #define DICT_ITERATOR_INDEX(iter) (iter)->index
220 /* For hashed dictionaries, this points to the last symbol returned;
221 otherwise, this is unused. */
222 #define DICT_ITERATOR_CURRENT(iter) (iter)->current
224 /* Declarations of functions for vectors. */
226 /* Functions that might work across a range of dictionary types. */
228 static void add_symbol_nonexpandable (struct dictionary *dict,
229 struct symbol *sym);
231 static void free_obstack (struct dictionary *dict);
233 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
234 dictionaries. */
236 static struct symbol *iterator_first_hashed (const struct dictionary *dict,
237 struct dict_iterator *iterator);
239 static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
241 static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
242 const lookup_name_info &name,
243 struct dict_iterator *iterator);
245 static struct symbol *iter_match_next_hashed (const lookup_name_info &name,
246 struct dict_iterator *iterator);
248 /* Functions only for DICT_HASHED. */
250 static int size_hashed (const struct dictionary *dict);
252 /* Functions only for DICT_HASHED_EXPANDABLE. */
254 static void free_hashed_expandable (struct dictionary *dict);
256 static void add_symbol_hashed_expandable (struct dictionary *dict,
257 struct symbol *sym);
259 static int size_hashed_expandable (const struct dictionary *dict);
261 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
262 dictionaries. */
264 static struct symbol *iterator_first_linear (const struct dictionary *dict,
265 struct dict_iterator *iterator);
267 static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
269 static struct symbol *iter_match_first_linear (const struct dictionary *dict,
270 const lookup_name_info &name,
271 struct dict_iterator *iterator);
273 static struct symbol *iter_match_next_linear (const lookup_name_info &name,
274 struct dict_iterator *iterator);
276 static int size_linear (const struct dictionary *dict);
278 /* Functions only for DICT_LINEAR_EXPANDABLE. */
280 static void free_linear_expandable (struct dictionary *dict);
282 static void add_symbol_linear_expandable (struct dictionary *dict,
283 struct symbol *sym);
285 /* Various vectors that we'll actually use. */
287 static const struct dict_vector dict_hashed_vector =
289 DICT_HASHED, /* type */
290 free_obstack, /* free */
291 add_symbol_nonexpandable, /* add_symbol */
292 iterator_first_hashed, /* iterator_first */
293 iterator_next_hashed, /* iterator_next */
294 iter_match_first_hashed, /* iter_name_first */
295 iter_match_next_hashed, /* iter_name_next */
296 size_hashed, /* size */
299 static const struct dict_vector dict_hashed_expandable_vector =
301 DICT_HASHED_EXPANDABLE, /* type */
302 free_hashed_expandable, /* free */
303 add_symbol_hashed_expandable, /* add_symbol */
304 iterator_first_hashed, /* iterator_first */
305 iterator_next_hashed, /* iterator_next */
306 iter_match_first_hashed, /* iter_name_first */
307 iter_match_next_hashed, /* iter_name_next */
308 size_hashed_expandable, /* size */
311 static const struct dict_vector dict_linear_vector =
313 DICT_LINEAR, /* type */
314 free_obstack, /* free */
315 add_symbol_nonexpandable, /* add_symbol */
316 iterator_first_linear, /* iterator_first */
317 iterator_next_linear, /* iterator_next */
318 iter_match_first_linear, /* iter_name_first */
319 iter_match_next_linear, /* iter_name_next */
320 size_linear, /* size */
323 static const struct dict_vector dict_linear_expandable_vector =
325 DICT_LINEAR_EXPANDABLE, /* type */
326 free_linear_expandable, /* free */
327 add_symbol_linear_expandable, /* add_symbol */
328 iterator_first_linear, /* iterator_first */
329 iterator_next_linear, /* iterator_next */
330 iter_match_first_linear, /* iter_name_first */
331 iter_match_next_linear, /* iter_name_next */
332 size_linear, /* size */
335 /* Declarations of helper functions (i.e. ones that don't go into
336 vectors). */
338 static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
340 static void insert_symbol_hashed (struct dictionary *dict,
341 struct symbol *sym);
343 static void expand_hashtable (struct dictionary *dict);
345 /* The creation functions. */
347 /* Create a hashed dictionary of a given language. */
349 static struct dictionary *
350 dict_create_hashed (struct obstack *obstack,
351 enum language language,
352 const std::vector<symbol *> &symbol_list)
354 /* Allocate the dictionary. */
355 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
356 DICT_VECTOR (retval) = &dict_hashed_vector;
357 DICT_LANGUAGE (retval) = language_def (language);
359 /* Allocate space for symbols. */
360 int nsyms = symbol_list.size ();
361 int nbuckets = DICT_HASHTABLE_SIZE (nsyms);
362 DICT_HASHED_NBUCKETS (retval) = nbuckets;
363 struct symbol **buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
364 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
365 DICT_HASHED_BUCKETS (retval) = buckets;
367 /* Now fill the buckets. */
368 for (const auto &sym : symbol_list)
369 insert_symbol_hashed (retval, sym);
371 return retval;
374 /* Create an expandable hashed dictionary of a given language. */
376 static struct dictionary *
377 dict_create_hashed_expandable (enum language language)
379 struct dictionary *retval = XNEW (struct dictionary);
381 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
382 DICT_LANGUAGE (retval) = language_def (language);
383 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
384 DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
385 DICT_EXPANDABLE_INITIAL_CAPACITY);
386 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
388 return retval;
391 /* Create a linear dictionary of a given language. */
393 static struct dictionary *
394 dict_create_linear (struct obstack *obstack,
395 enum language language,
396 const std::vector<symbol *> &symbol_list)
398 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
399 DICT_VECTOR (retval) = &dict_linear_vector;
400 DICT_LANGUAGE (retval) = language_def (language);
402 /* Allocate space for symbols. */
403 int nsyms = symbol_list.size ();
404 DICT_LINEAR_NSYMS (retval) = nsyms;
405 struct symbol **syms = XOBNEWVEC (obstack, struct symbol *, nsyms);
406 DICT_LINEAR_SYMS (retval) = syms;
408 /* Now fill in the symbols. */
409 int idx = nsyms - 1;
410 for (const auto &sym : symbol_list)
411 syms[idx--] = sym;
413 return retval;
416 /* Create an expandable linear dictionary of a given language. */
418 static struct dictionary *
419 dict_create_linear_expandable (enum language language)
421 struct dictionary *retval = XNEW (struct dictionary);
423 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
424 DICT_LANGUAGE (retval) = language_def (language);
425 DICT_LINEAR_NSYMS (retval) = 0;
426 DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
427 DICT_LINEAR_SYMS (retval)
428 = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
430 return retval;
433 /* The functions providing the dictionary interface. */
435 /* Free the memory used by a dictionary that's not on an obstack. (If
436 any.) */
438 static void
439 dict_free (struct dictionary *dict)
441 (DICT_VECTOR (dict))->free (dict);
444 /* Add SYM to DICT. DICT had better be expandable. */
446 static void
447 dict_add_symbol (struct dictionary *dict, struct symbol *sym)
449 (DICT_VECTOR (dict))->add_symbol (dict, sym);
452 /* Utility to add a list of symbols to a dictionary.
453 DICT must be an expandable dictionary. */
455 static void
456 dict_add_pending (struct dictionary *dict,
457 const std::vector<symbol *> &symbol_list)
459 /* Preserve ordering by reversing the list. */
460 for (auto sym = symbol_list.rbegin (); sym != symbol_list.rend (); ++sym)
461 dict_add_symbol (dict, *sym);
464 /* Initialize ITERATOR to point at the first symbol in DICT, and
465 return that first symbol, or NULL if DICT is empty. */
467 static struct symbol *
468 dict_iterator_first (const struct dictionary *dict,
469 struct dict_iterator *iterator)
471 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
474 /* Advance ITERATOR, and return the next symbol, or NULL if there are
475 no more symbols. */
477 static struct symbol *
478 dict_iterator_next (struct dict_iterator *iterator)
480 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
481 ->iterator_next (iterator);
484 static struct symbol *
485 dict_iter_match_first (const struct dictionary *dict,
486 const lookup_name_info &name,
487 struct dict_iterator *iterator)
489 return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
492 static struct symbol *
493 dict_iter_match_next (const lookup_name_info &name,
494 struct dict_iterator *iterator)
496 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
497 ->iter_match_next (name, iterator);
500 static int
501 dict_size (const struct dictionary *dict)
503 return (DICT_VECTOR (dict))->size (dict);
506 /* Now come functions (well, one function, currently) that are
507 implemented generically by means of the vtable. Typically, they're
508 rarely used. */
511 /* The functions implementing the dictionary interface. */
513 /* Generic functions, where appropriate. */
515 static void
516 free_obstack (struct dictionary *dict)
518 /* Do nothing! */
521 static void
522 add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
524 internal_error (_("dict_add_symbol: non-expandable dictionary"));
527 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
529 static struct symbol *
530 iterator_first_hashed (const struct dictionary *dict,
531 struct dict_iterator *iterator)
533 DICT_ITERATOR_DICT (iterator) = dict;
534 DICT_ITERATOR_INDEX (iterator) = -1;
535 return iterator_hashed_advance (iterator);
538 static struct symbol *
539 iterator_next_hashed (struct dict_iterator *iterator)
541 struct symbol *next;
543 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
545 if (next == NULL)
546 return iterator_hashed_advance (iterator);
547 else
549 DICT_ITERATOR_CURRENT (iterator) = next;
550 return next;
554 static struct symbol *
555 iterator_hashed_advance (struct dict_iterator *iterator)
557 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
558 int nbuckets = DICT_HASHED_NBUCKETS (dict);
559 int i;
561 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
563 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
565 if (sym != NULL)
567 DICT_ITERATOR_INDEX (iterator) = i;
568 DICT_ITERATOR_CURRENT (iterator) = sym;
569 return sym;
573 return NULL;
576 static struct symbol *
577 iter_match_first_hashed (const struct dictionary *dict,
578 const lookup_name_info &name,
579 struct dict_iterator *iterator)
581 const language_defn *lang = DICT_LANGUAGE (dict);
582 unsigned int hash_index = (name.search_name_hash (lang->la_language)
583 % DICT_HASHED_NBUCKETS (dict));
584 symbol_name_matcher_ftype *matches_name
585 = lang->get_symbol_name_matcher (name);
586 struct symbol *sym;
588 DICT_ITERATOR_DICT (iterator) = dict;
590 /* Loop through the symbols in the given bucket, breaking when SYM
591 first matches. If SYM never matches, it will be set to NULL;
592 either way, we have the right return value. */
594 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
595 sym != NULL;
596 sym = sym->hash_next)
598 /* Warning: the order of arguments to compare matters! */
599 if (matches_name (sym->search_name (), name, NULL))
600 break;
603 DICT_ITERATOR_CURRENT (iterator) = sym;
604 return sym;
607 static struct symbol *
608 iter_match_next_hashed (const lookup_name_info &name,
609 struct dict_iterator *iterator)
611 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
612 symbol_name_matcher_ftype *matches_name
613 = lang->get_symbol_name_matcher (name);
614 struct symbol *next;
616 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
617 next != NULL;
618 next = next->hash_next)
620 if (matches_name (next->search_name (), name, NULL))
621 break;
624 DICT_ITERATOR_CURRENT (iterator) = next;
626 return next;
629 /* Insert SYM into DICT. */
631 static void
632 insert_symbol_hashed (struct dictionary *dict,
633 struct symbol *sym)
635 unsigned int hash_index;
636 unsigned int hash;
637 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
639 /* We don't want to insert a symbol into a dictionary of a different
640 language. The two may not use the same hashing algorithm. */
641 gdb_assert (sym->language () == DICT_LANGUAGE (dict)->la_language);
643 hash = search_name_hash (sym->language (), sym->search_name ());
644 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
645 sym->hash_next = buckets[hash_index];
646 buckets[hash_index] = sym;
649 static int
650 size_hashed (const struct dictionary *dict)
652 int nbuckets = DICT_HASHED_NBUCKETS (dict);
653 int total = 0;
655 for (int i = 0; i < nbuckets; ++i)
657 for (struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
658 sym != nullptr;
659 sym = sym->hash_next)
660 total++;
663 return total;
666 /* Functions only for DICT_HASHED_EXPANDABLE. */
668 static void
669 free_hashed_expandable (struct dictionary *dict)
671 xfree (DICT_HASHED_BUCKETS (dict));
672 xfree (dict);
675 static void
676 add_symbol_hashed_expandable (struct dictionary *dict,
677 struct symbol *sym)
679 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
681 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
682 expand_hashtable (dict);
684 insert_symbol_hashed (dict, sym);
685 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
688 static int
689 size_hashed_expandable (const struct dictionary *dict)
691 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
694 static void
695 expand_hashtable (struct dictionary *dict)
697 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
698 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
699 int new_nbuckets = 2 * old_nbuckets + 1;
700 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
701 int i;
703 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
704 DICT_HASHED_BUCKETS (dict) = new_buckets;
706 for (i = 0; i < old_nbuckets; ++i)
708 struct symbol *sym, *next_sym;
710 sym = old_buckets[i];
711 if (sym != NULL)
713 for (next_sym = sym->hash_next;
714 next_sym != NULL;
715 next_sym = sym->hash_next)
717 insert_symbol_hashed (dict, sym);
718 sym = next_sym;
721 insert_symbol_hashed (dict, sym);
725 xfree (old_buckets);
728 /* See dictionary.h. */
730 unsigned int
731 language_defn::search_name_hash (const char *string0) const
733 /* The Ada-encoded version of a name P1.P2...Pn has either the form
734 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
735 are lower-cased identifiers). The <suffix> (which can be empty)
736 encodes additional information about the denoted entity. This
737 routine hashes such names to msymbol_hash_iw(Pn). It actually
738 does this for a superset of both valid Pi and of <suffix>, but
739 in other cases it simply returns msymbol_hash_iw(STRING0). */
741 const char *string;
742 unsigned int hash;
744 string = string0;
745 if (*string == '_')
747 if (startswith (string, "_ada_"))
748 string += 5;
749 else
750 return msymbol_hash_iw (string0);
753 hash = 0;
754 while (*string)
756 switch (*string)
758 case '$':
759 case '.':
760 case 'X':
761 if (string0 == string)
762 return msymbol_hash_iw (string0);
763 else
764 return hash;
765 case ' ':
766 case '(':
767 return msymbol_hash_iw (string0);
768 case '_':
769 if (string[1] == '_' && string != string0)
771 int c = string[2];
773 if (c == 'B' && string[3] == '_')
775 for (string += 4; ISDIGIT (*string); ++string)
777 continue;
780 if ((c < 'a' || c > 'z') && c != 'O')
781 return hash;
782 hash = 0;
783 string += 2;
784 continue;
786 break;
787 case 'T':
788 /* Ignore "TKB" suffixes.
790 These are used by Ada for subprograms implementing a task body.
791 For instance for a task T inside package Pck, the name of the
792 subprogram implementing T's body is `pck__tTKB'. We need to
793 ignore the "TKB" suffix because searches for this task body
794 subprogram are going to be performed using `pck__t' (the encoded
795 version of the natural name `pck.t'). */
796 if (strcmp (string, "TKB") == 0)
797 return hash;
798 break;
801 hash = SYMBOL_HASH_NEXT (hash, *string);
802 string += 1;
804 return hash;
807 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
809 static struct symbol *
810 iterator_first_linear (const struct dictionary *dict,
811 struct dict_iterator *iterator)
813 DICT_ITERATOR_DICT (iterator) = dict;
814 DICT_ITERATOR_INDEX (iterator) = 0;
815 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
818 static struct symbol *
819 iterator_next_linear (struct dict_iterator *iterator)
821 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
823 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
824 return NULL;
825 else
826 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
829 static struct symbol *
830 iter_match_first_linear (const struct dictionary *dict,
831 const lookup_name_info &name,
832 struct dict_iterator *iterator)
834 DICT_ITERATOR_DICT (iterator) = dict;
835 DICT_ITERATOR_INDEX (iterator) = -1;
837 return iter_match_next_linear (name, iterator);
840 static struct symbol *
841 iter_match_next_linear (const lookup_name_info &name,
842 struct dict_iterator *iterator)
844 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
845 const language_defn *lang = DICT_LANGUAGE (dict);
846 symbol_name_matcher_ftype *matches_name
847 = lang->get_symbol_name_matcher (name);
849 int i, nsyms = DICT_LINEAR_NSYMS (dict);
850 struct symbol *sym, *retval = NULL;
852 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
854 sym = DICT_LINEAR_SYM (dict, i);
856 if (matches_name (sym->search_name (), name, NULL))
858 retval = sym;
859 break;
863 DICT_ITERATOR_INDEX (iterator) = i;
865 return retval;
868 static int
869 size_linear (const struct dictionary *dict)
871 return DICT_LINEAR_NSYMS (dict);
874 /* Functions only for DICT_LINEAR_EXPANDABLE. */
876 static void
877 free_linear_expandable (struct dictionary *dict)
879 xfree (DICT_LINEAR_SYMS (dict));
880 xfree (dict);
884 static void
885 add_symbol_linear_expandable (struct dictionary *dict,
886 struct symbol *sym)
888 int nsyms = ++DICT_LINEAR_NSYMS (dict);
890 /* Do we have enough room? If not, grow it. */
891 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
893 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
894 DICT_LINEAR_SYMS (dict)
895 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
896 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
899 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
902 /* Multi-language dictionary support. */
904 /* The structure describing a multi-language dictionary. */
906 struct multidictionary
908 /* An array of dictionaries, one per language. All dictionaries
909 must be of the same type. This should be free'd for expandable
910 dictionary types. */
911 struct dictionary **dictionaries;
913 /* The number of language dictionaries currently allocated.
914 Only used for expandable dictionaries. */
915 unsigned short n_allocated_dictionaries;
918 /* A hasher for enum language. Injecting this into std is a convenience
919 when using unordered_map with C++11. */
921 namespace std
923 template<> struct hash<enum language>
925 typedef enum language argument_type;
926 typedef std::size_t result_type;
928 result_type operator() (const argument_type &l) const noexcept
930 return static_cast<result_type> (l);
933 } /* namespace std */
935 /* A helper function to collate symbols on the pending list by language. */
937 static std::unordered_map<enum language, std::vector<symbol *>>
938 collate_pending_symbols_by_language (const struct pending *symbol_list)
940 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
942 for (const pending *list_counter = symbol_list;
943 list_counter != nullptr; list_counter = list_counter->next)
945 for (int i = list_counter->nsyms - 1; i >= 0; --i)
947 enum language language = list_counter->symbol[i]->language ();
948 nsyms[language].push_back (list_counter->symbol[i]);
952 return nsyms;
955 /* See dictionary.h. */
957 struct multidictionary *
958 mdict_create_hashed (struct obstack *obstack,
959 const struct pending *symbol_list)
961 struct multidictionary *retval
962 = XOBNEW (obstack, struct multidictionary);
963 std::unordered_map<enum language, std::vector<symbol *>> nsyms
964 = collate_pending_symbols_by_language (symbol_list);
966 /* Loop over all languages and create/populate dictionaries. */
967 retval->dictionaries
968 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
969 retval->n_allocated_dictionaries = nsyms.size ();
971 int idx = 0;
972 for (const auto &pair : nsyms)
974 enum language language = pair.first;
975 std::vector<symbol *> symlist = pair.second;
977 retval->dictionaries[idx++]
978 = dict_create_hashed (obstack, language, symlist);
981 return retval;
984 /* See dictionary.h. */
986 struct multidictionary *
987 mdict_create_hashed_expandable (enum language language)
989 struct multidictionary *retval = XNEW (struct multidictionary);
991 /* We have no symbol list to populate, but we create an empty
992 dictionary of the requested language to populate later. */
993 retval->n_allocated_dictionaries = 1;
994 retval->dictionaries = XNEW (struct dictionary *);
995 retval->dictionaries[0] = dict_create_hashed_expandable (language);
997 return retval;
1000 /* See dictionary.h. */
1002 struct multidictionary *
1003 mdict_create_linear (struct obstack *obstack,
1004 const struct pending *symbol_list)
1006 struct multidictionary *retval
1007 = XOBNEW (obstack, struct multidictionary);
1008 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1009 = collate_pending_symbols_by_language (symbol_list);
1011 /* Loop over all languages and create/populate dictionaries. */
1012 retval->dictionaries
1013 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1014 retval->n_allocated_dictionaries = nsyms.size ();
1016 int idx = 0;
1017 for (const auto &pair : nsyms)
1019 enum language language = pair.first;
1020 std::vector<symbol *> symlist = pair.second;
1022 retval->dictionaries[idx++]
1023 = dict_create_linear (obstack, language, symlist);
1026 return retval;
1029 /* See dictionary.h. */
1031 struct multidictionary *
1032 mdict_create_linear_expandable (enum language language)
1034 struct multidictionary *retval = XNEW (struct multidictionary);
1036 /* We have no symbol list to populate, but we create an empty
1037 dictionary to populate later. */
1038 retval->n_allocated_dictionaries = 1;
1039 retval->dictionaries = XNEW (struct dictionary *);
1040 retval->dictionaries[0] = dict_create_linear_expandable (language);
1042 return retval;
1045 /* See dictionary.h. */
1047 void
1048 mdict_free (struct multidictionary *mdict)
1050 /* Grab the type of dictionary being used. */
1051 enum dict_type type = mdict->dictionaries[0]->vector->type;
1053 /* Loop over all dictionaries and free them. */
1054 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1055 dict_free (mdict->dictionaries[idx]);
1057 /* Free the dictionary list, if needed. */
1058 switch (type)
1060 case DICT_HASHED:
1061 case DICT_LINEAR:
1062 /* Memory was allocated on an obstack when created. */
1063 break;
1065 case DICT_HASHED_EXPANDABLE:
1066 case DICT_LINEAR_EXPANDABLE:
1067 xfree (mdict->dictionaries);
1068 break;
1072 /* Helper function to find the dictionary associated with LANGUAGE
1073 or NULL if there is no dictionary of that language. */
1075 static struct dictionary *
1076 find_language_dictionary (const struct multidictionary *mdict,
1077 enum language language)
1079 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1081 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1082 return mdict->dictionaries[idx];
1085 return nullptr;
1088 /* Create a new language dictionary for LANGUAGE and add it to the
1089 multidictionary MDICT's list of dictionaries. If MDICT is not
1090 based on expandable dictionaries, this function throws an
1091 internal error. */
1093 static struct dictionary *
1094 create_new_language_dictionary (struct multidictionary *mdict,
1095 enum language language)
1097 struct dictionary *retval = nullptr;
1099 /* We use the first dictionary entry to decide what create function
1100 to call. Not optimal but sufficient. */
1101 gdb_assert (mdict->dictionaries[0] != nullptr);
1102 switch (mdict->dictionaries[0]->vector->type)
1104 case DICT_HASHED:
1105 case DICT_LINEAR:
1106 internal_error (_("create_new_language_dictionary: attempted to expand "
1107 "non-expandable multidictionary"));
1109 case DICT_HASHED_EXPANDABLE:
1110 retval = dict_create_hashed_expandable (language);
1111 break;
1113 case DICT_LINEAR_EXPANDABLE:
1114 retval = dict_create_linear_expandable (language);
1115 break;
1118 /* Grow the dictionary vector and save the new dictionary. */
1119 mdict->dictionaries
1120 = (struct dictionary **) xrealloc (mdict->dictionaries,
1121 (++mdict->n_allocated_dictionaries
1122 * sizeof (struct dictionary *)));
1123 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1125 return retval;
1128 /* See dictionary.h. */
1130 void
1131 mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1133 struct dictionary *dict
1134 = find_language_dictionary (mdict, sym->language ());
1136 if (dict == nullptr)
1138 /* SYM is of a new language that we haven't previously seen.
1139 Create a new dictionary for it. */
1140 dict = create_new_language_dictionary (mdict, sym->language ());
1143 dict_add_symbol (dict, sym);
1146 /* See dictionary.h. */
1148 void
1149 mdict_add_pending (struct multidictionary *mdict,
1150 const struct pending *symbol_list)
1152 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1153 = collate_pending_symbols_by_language (symbol_list);
1155 for (const auto &pair : nsyms)
1157 enum language language = pair.first;
1158 std::vector<symbol *> symlist = pair.second;
1159 struct dictionary *dict = find_language_dictionary (mdict, language);
1161 if (dict == nullptr)
1163 /* The language was not previously seen. Create a new dictionary
1164 for it. */
1165 dict = create_new_language_dictionary (mdict, language);
1168 dict_add_pending (dict, symlist);
1172 /* See dictionary.h. */
1174 struct symbol *
1175 mdict_iterator_first (const multidictionary *mdict,
1176 struct mdict_iterator *miterator)
1178 miterator->mdict = mdict;
1179 miterator->current_idx = 0;
1181 for (unsigned short idx = miterator->current_idx;
1182 idx < mdict->n_allocated_dictionaries; ++idx)
1184 struct symbol *result
1185 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1187 if (result != nullptr)
1189 miterator->current_idx = idx;
1190 return result;
1194 return nullptr;
1197 /* See dictionary.h. */
1199 struct symbol *
1200 mdict_iterator_next (struct mdict_iterator *miterator)
1202 struct symbol *result = dict_iterator_next (&miterator->iterator);
1204 if (result != nullptr)
1205 return result;
1207 /* The current dictionary had no matches -- move to the next
1208 dictionary, if any. */
1209 for (unsigned short idx = ++miterator->current_idx;
1210 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1212 result
1213 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1214 &miterator->iterator);
1215 if (result != nullptr)
1217 miterator->current_idx = idx;
1218 return result;
1222 return nullptr;
1225 /* See dictionary.h. */
1227 struct symbol *
1228 mdict_iter_match_first (const struct multidictionary *mdict,
1229 const lookup_name_info &name,
1230 struct mdict_iterator *miterator)
1232 miterator->mdict = mdict;
1233 miterator->current_idx = 0;
1235 for (unsigned short idx = miterator->current_idx;
1236 idx < mdict->n_allocated_dictionaries; ++idx)
1238 struct symbol *result
1239 = dict_iter_match_first (mdict->dictionaries[idx], name,
1240 &miterator->iterator);
1242 if (result != nullptr)
1243 return result;
1246 return nullptr;
1249 /* See dictionary.h. */
1251 struct symbol *
1252 mdict_iter_match_next (const lookup_name_info &name,
1253 struct mdict_iterator *miterator)
1255 /* Search the current dictionary. */
1256 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1258 if (result != nullptr)
1259 return result;
1261 /* The current dictionary had no matches -- move to the next
1262 dictionary, if any. */
1263 for (unsigned short idx = ++miterator->current_idx;
1264 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1266 result
1267 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1268 name, &miterator->iterator);
1269 if (result != nullptr)
1271 miterator->current_idx = idx;
1272 return result;
1276 return nullptr;
1279 /* See dictionary.h. */
1282 mdict_size (const struct multidictionary *mdict)
1284 int size = 0;
1286 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1287 size += dict_size (mdict->dictionaries[idx]);
1289 return size;