1 /* Routines for name->symbol lookups in GDB.
3 Copyright (C) 2003-2019 Free Software Foundation, Inc.
5 Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
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/>. */
25 #include "gdb_obstack.h"
28 #include "dictionary.h"
29 #include "safe-ctype.h"
30 #include <unordered_map>
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
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
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
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>
81 * Modify all existing dict_vector_<impl>'s to include the appropriate
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. */
93 /* Symbols are stored in a fixed-size hash table. */
95 /* Symbols are stored in an expandable hash table. */
96 DICT_HASHED_EXPANDABLE,
97 /* Symbols are stored in a fixed-size array. */
99 /* Symbols are stored in an expandable array. */
100 DICT_LINEAR_EXPANDABLE
103 /* The virtual function table. */
107 /* The type of the dictionary. This is only here to make debugging
108 a bit easier; it's not actually used. */
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
133 struct dictionary_hashed
136 struct symbol **buckets;
139 struct dictionary_hashed_expandable
141 /* How many buckets we currently have. */
143 struct symbol **buckets;
144 /* How many syms we currently have; we need this so we will know
145 when to add more buckets. */
149 struct dictionary_linear
152 struct symbol **syms;
155 struct dictionary_linear_expandable
157 /* How many symbols we currently have. */
159 struct symbol **syms;
160 /* How many symbols we can store before needing to reallocate. */
164 /* And now, the star of our show. */
168 const struct language_defn *language;
169 const struct dict_vector *vector;
172 struct dictionary_hashed hashed;
173 struct dictionary_hashed_expandable hashed_expandable;
174 struct dictionary_linear linear;
175 struct dictionary_linear_expandable linear_expandable;
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
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,
231 static void free_obstack (struct dictionary *dict);
233 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
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,
259 static int size_hashed_expandable (const struct dictionary *dict);
261 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
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,
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
338 static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
340 static void insert_symbol_hashed (struct dictionary *dict,
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);
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;
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. */
410 for (const auto &sym : symbol_list)
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));
433 /* The functions providing the dictionary interface. */
435 /* Free the memory used by a dictionary that's not on an obstack. (If
439 dict_free (struct dictionary *dict)
441 (DICT_VECTOR (dict))->free (dict);
444 /* Add SYM to DICT. DICT had better be expandable. */
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. */
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. */
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
478 dict_iterator_next (struct dict_iterator *iterator)
480 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
481 ->iterator_next (iterator);
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);
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);
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
510 /* Test to see if DICT is empty. */
513 dict_empty (struct dictionary *dict)
515 struct dict_iterator iter;
517 return (dict_iterator_first (dict, &iter) == NULL);
521 /* The functions implementing the dictionary interface. */
523 /* Generic functions, where appropriate. */
526 free_obstack (struct dictionary *dict)
532 add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
534 internal_error (__FILE__, __LINE__,
535 _("dict_add_symbol: non-expandable dictionary"));
538 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
540 static struct symbol *
541 iterator_first_hashed (const struct dictionary *dict,
542 struct dict_iterator *iterator)
544 DICT_ITERATOR_DICT (iterator) = dict;
545 DICT_ITERATOR_INDEX (iterator) = -1;
546 return iterator_hashed_advance (iterator);
549 static struct symbol *
550 iterator_next_hashed (struct dict_iterator *iterator)
554 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
557 return iterator_hashed_advance (iterator);
560 DICT_ITERATOR_CURRENT (iterator) = next;
565 static struct symbol *
566 iterator_hashed_advance (struct dict_iterator *iterator)
568 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
569 int nbuckets = DICT_HASHED_NBUCKETS (dict);
572 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
574 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
578 DICT_ITERATOR_INDEX (iterator) = i;
579 DICT_ITERATOR_CURRENT (iterator) = sym;
587 static struct symbol *
588 iter_match_first_hashed (const struct dictionary *dict,
589 const lookup_name_info &name,
590 struct dict_iterator *iterator)
592 const language_defn *lang = DICT_LANGUAGE (dict);
593 unsigned int hash_index = (name.search_name_hash (lang->la_language)
594 % DICT_HASHED_NBUCKETS (dict));
595 symbol_name_matcher_ftype *matches_name
596 = get_symbol_name_matcher (lang, name);
599 DICT_ITERATOR_DICT (iterator) = dict;
601 /* Loop through the symbols in the given bucket, breaking when SYM
602 first matches. If SYM never matches, it will be set to NULL;
603 either way, we have the right return value. */
605 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
607 sym = sym->hash_next)
609 /* Warning: the order of arguments to compare matters! */
610 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
614 DICT_ITERATOR_CURRENT (iterator) = sym;
618 static struct symbol *
619 iter_match_next_hashed (const lookup_name_info &name,
620 struct dict_iterator *iterator)
622 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
623 symbol_name_matcher_ftype *matches_name
624 = get_symbol_name_matcher (lang, name);
627 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
629 next = next->hash_next)
631 if (matches_name (SYMBOL_SEARCH_NAME (next), name, NULL))
635 DICT_ITERATOR_CURRENT (iterator) = next;
640 /* Insert SYM into DICT. */
643 insert_symbol_hashed (struct dictionary *dict,
646 unsigned int hash_index;
648 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
650 /* We don't want to insert a symbol into a dictionary of a different
651 language. The two may not use the same hashing algorithm. */
652 gdb_assert (SYMBOL_LANGUAGE (sym) == DICT_LANGUAGE (dict)->la_language);
654 hash = search_name_hash (SYMBOL_LANGUAGE (sym), SYMBOL_SEARCH_NAME (sym));
655 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
656 sym->hash_next = buckets[hash_index];
657 buckets[hash_index] = sym;
661 size_hashed (const struct dictionary *dict)
663 return DICT_HASHED_NBUCKETS (dict);
666 /* Functions only for DICT_HASHED_EXPANDABLE. */
669 free_hashed_expandable (struct dictionary *dict)
671 xfree (DICT_HASHED_BUCKETS (dict));
676 add_symbol_hashed_expandable (struct dictionary *dict,
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;
689 size_hashed_expandable (const struct dictionary *dict)
691 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
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);
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];
713 for (next_sym = sym->hash_next;
715 next_sym = sym->hash_next)
717 insert_symbol_hashed (dict, sym);
721 insert_symbol_hashed (dict, sym);
728 /* See dictionary.h. */
731 default_search_name_hash (const char *string0)
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). */
747 if (startswith (string, "_ada_"))
750 return msymbol_hash_iw (string0);
761 if (string0 == string)
762 return msymbol_hash_iw (string0);
767 return msymbol_hash_iw (string0);
769 if (string[1] == '_' && string != string0)
773 if ((c < 'a' || c > 'z') && c != 'O')
781 /* Ignore "TKB" suffixes.
783 These are used by Ada for subprograms implementing a task body.
784 For instance for a task T inside package Pck, the name of the
785 subprogram implementing T's body is `pck__tTKB'. We need to
786 ignore the "TKB" suffix because searches for this task body
787 subprogram are going to be performed using `pck__t' (the encoded
788 version of the natural name `pck.t'). */
789 if (strcmp (string, "TKB") == 0)
794 hash = SYMBOL_HASH_NEXT (hash, *string);
800 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
802 static struct symbol *
803 iterator_first_linear (const struct dictionary *dict,
804 struct dict_iterator *iterator)
806 DICT_ITERATOR_DICT (iterator) = dict;
807 DICT_ITERATOR_INDEX (iterator) = 0;
808 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
811 static struct symbol *
812 iterator_next_linear (struct dict_iterator *iterator)
814 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
816 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
819 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
822 static struct symbol *
823 iter_match_first_linear (const struct dictionary *dict,
824 const lookup_name_info &name,
825 struct dict_iterator *iterator)
827 DICT_ITERATOR_DICT (iterator) = dict;
828 DICT_ITERATOR_INDEX (iterator) = -1;
830 return iter_match_next_linear (name, iterator);
833 static struct symbol *
834 iter_match_next_linear (const lookup_name_info &name,
835 struct dict_iterator *iterator)
837 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
838 const language_defn *lang = DICT_LANGUAGE (dict);
839 symbol_name_matcher_ftype *matches_name
840 = get_symbol_name_matcher (lang, name);
842 int i, nsyms = DICT_LINEAR_NSYMS (dict);
843 struct symbol *sym, *retval = NULL;
845 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
847 sym = DICT_LINEAR_SYM (dict, i);
849 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
856 DICT_ITERATOR_INDEX (iterator) = i;
862 size_linear (const struct dictionary *dict)
864 return DICT_LINEAR_NSYMS (dict);
867 /* Functions only for DICT_LINEAR_EXPANDABLE. */
870 free_linear_expandable (struct dictionary *dict)
872 xfree (DICT_LINEAR_SYMS (dict));
878 add_symbol_linear_expandable (struct dictionary *dict,
881 int nsyms = ++DICT_LINEAR_NSYMS (dict);
883 /* Do we have enough room? If not, grow it. */
884 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
886 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
887 DICT_LINEAR_SYMS (dict)
888 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
889 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
892 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
895 /* Multi-language dictionary support. */
897 /* The structure describing a multi-language dictionary. */
899 struct multidictionary
901 /* An array of dictionaries, one per language. All dictionaries
902 must be of the same type. This should be free'd for expandable
904 struct dictionary **dictionaries;
906 /* The number of language dictionaries currently allocated.
907 Only used for expandable dictionaries. */
908 unsigned short n_allocated_dictionaries;
911 /* A hasher for enum language. Injecting this into std is a convenience
912 when using unordered_map with C++11. */
916 template<> struct hash<enum language>
918 typedef enum language argument_type;
919 typedef std::size_t result_type;
921 result_type operator() (const argument_type &l) const noexcept
923 return static_cast<result_type> (l);
926 } /* namespace std */
928 /* A helper function to collate symbols on the pending list by language. */
930 static std::unordered_map<enum language, std::vector<symbol *>>
931 collate_pending_symbols_by_language (const struct pending *symbol_list)
933 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
935 for (const struct pending *list_counter = symbol_list;
936 list_counter != nullptr; list_counter = list_counter->next)
938 for (int i = list_counter->nsyms - 1; i >= 0; --i)
940 enum language language = SYMBOL_LANGUAGE (list_counter->symbol[i]);
941 nsyms[language].push_back (list_counter->symbol[i]);
948 /* See dictionary.h. */
950 struct multidictionary *
951 mdict_create_hashed (struct obstack *obstack,
952 const struct pending *symbol_list)
954 struct multidictionary *retval
955 = XOBNEW (obstack, struct multidictionary);
956 std::unordered_map<enum language, std::vector<symbol *>> nsyms
957 = collate_pending_symbols_by_language (symbol_list);
959 /* Loop over all languages and create/populate dictionaries. */
961 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
962 retval->n_allocated_dictionaries = nsyms.size ();
965 for (const auto &pair : nsyms)
967 enum language language = pair.first;
968 std::vector<symbol *> symlist = pair.second;
970 retval->dictionaries[idx++]
971 = dict_create_hashed (obstack, language, symlist);
977 /* See dictionary.h. */
979 struct multidictionary *
980 mdict_create_hashed_expandable (enum language language)
982 struct multidictionary *retval = XNEW (struct multidictionary);
984 /* We have no symbol list to populate, but we create an empty
985 dictionary of the requested language to populate later. */
986 retval->n_allocated_dictionaries = 1;
987 retval->dictionaries = XNEW (struct dictionary *);
988 retval->dictionaries[0] = dict_create_hashed_expandable (language);
993 /* See dictionary.h. */
995 struct multidictionary *
996 mdict_create_linear (struct obstack *obstack,
997 const struct pending *symbol_list)
999 struct multidictionary *retval
1000 = XOBNEW (obstack, struct multidictionary);
1001 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1002 = collate_pending_symbols_by_language (symbol_list);
1004 /* Loop over all languages and create/populate dictionaries. */
1005 retval->dictionaries
1006 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1007 retval->n_allocated_dictionaries = nsyms.size ();
1010 for (const auto &pair : nsyms)
1012 enum language language = pair.first;
1013 std::vector<symbol *> symlist = pair.second;
1015 retval->dictionaries[idx++]
1016 = dict_create_linear (obstack, language, symlist);
1022 /* See dictionary.h. */
1024 struct multidictionary *
1025 mdict_create_linear_expandable (enum language language)
1027 struct multidictionary *retval = XNEW (struct multidictionary);
1029 /* We have no symbol list to populate, but we create an empty
1030 dictionary to populate later. */
1031 retval->n_allocated_dictionaries = 1;
1032 retval->dictionaries = XNEW (struct dictionary *);
1033 retval->dictionaries[0] = dict_create_linear_expandable (language);
1038 /* See dictionary.h. */
1041 mdict_free (struct multidictionary *mdict)
1043 /* Grab the type of dictionary being used. */
1044 enum dict_type type = mdict->dictionaries[0]->vector->type;
1046 /* Loop over all dictionaries and free them. */
1047 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1048 dict_free (mdict->dictionaries[idx]);
1050 /* Free the dictionary list, if needed. */
1055 /* Memory was allocated on an obstack when created. */
1058 case DICT_HASHED_EXPANDABLE:
1059 case DICT_LINEAR_EXPANDABLE:
1060 xfree (mdict->dictionaries);
1065 /* Helper function to find the dictionary associated with LANGUAGE
1066 or NULL if there is no dictionary of that language. */
1068 static struct dictionary *
1069 find_language_dictionary (const struct multidictionary *mdict,
1070 enum language language)
1072 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1074 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1075 return mdict->dictionaries[idx];
1081 /* Create a new language dictionary for LANGUAGE and add it to the
1082 multidictionary MDICT's list of dictionaries. If MDICT is not
1083 based on expandable dictionaries, this function throws an
1086 static struct dictionary *
1087 create_new_language_dictionary (struct multidictionary *mdict,
1088 enum language language)
1090 struct dictionary *retval = nullptr;
1092 /* We use the first dictionary entry to decide what create function
1093 to call. Not optimal but sufficient. */
1094 gdb_assert (mdict->dictionaries[0] != nullptr);
1095 switch (mdict->dictionaries[0]->vector->type)
1099 internal_error (__FILE__, __LINE__,
1100 _("create_new_language_dictionary: attempted to expand "
1101 "non-expandable multidictionary"));
1103 case DICT_HASHED_EXPANDABLE:
1104 retval = dict_create_hashed_expandable (language);
1107 case DICT_LINEAR_EXPANDABLE:
1108 retval = dict_create_linear_expandable (language);
1112 /* Grow the dictionary vector and save the new dictionary. */
1114 = (struct dictionary **) xrealloc (mdict->dictionaries,
1115 (++mdict->n_allocated_dictionaries
1116 * sizeof (struct dictionary *)));
1117 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1122 /* See dictionary.h. */
1125 mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1127 struct dictionary *dict
1128 = find_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1130 if (dict == nullptr)
1132 /* SYM is of a new language that we haven't previously seen.
1133 Create a new dictionary for it. */
1134 dict = create_new_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1137 dict_add_symbol (dict, sym);
1140 /* See dictionary.h. */
1143 mdict_add_pending (struct multidictionary *mdict,
1144 const struct pending *symbol_list)
1146 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1147 = collate_pending_symbols_by_language (symbol_list);
1149 for (const auto &pair : nsyms)
1151 enum language language = pair.first;
1152 std::vector<symbol *> symlist = pair.second;
1153 struct dictionary *dict = find_language_dictionary (mdict, language);
1155 if (dict == nullptr)
1157 /* The language was not previously seen. Create a new dictionary
1159 dict = create_new_language_dictionary (mdict, language);
1162 dict_add_pending (dict, symlist);
1166 /* See dictionary.h. */
1169 mdict_iterator_first (const multidictionary *mdict,
1170 struct mdict_iterator *miterator)
1172 miterator->mdict = mdict;
1173 miterator->current_idx = 0;
1175 for (unsigned short idx = miterator->current_idx;
1176 idx < mdict->n_allocated_dictionaries; ++idx)
1178 struct symbol *result
1179 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1181 if (result != nullptr)
1183 miterator->current_idx = idx;
1191 /* See dictionary.h. */
1194 mdict_iterator_next (struct mdict_iterator *miterator)
1196 struct symbol *result = dict_iterator_next (&miterator->iterator);
1198 if (result != nullptr)
1201 /* The current dictionary had no matches -- move to the next
1202 dictionary, if any. */
1203 for (unsigned short idx = ++miterator->current_idx;
1204 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1207 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1208 &miterator->iterator);
1209 if (result != nullptr)
1211 miterator->current_idx = idx;
1219 /* See dictionary.h. */
1222 mdict_iter_match_first (const struct multidictionary *mdict,
1223 const lookup_name_info &name,
1224 struct mdict_iterator *miterator)
1226 miterator->mdict = mdict;
1227 miterator->current_idx = 0;
1229 for (unsigned short idx = miterator->current_idx;
1230 idx < mdict->n_allocated_dictionaries; ++idx)
1232 struct symbol *result
1233 = dict_iter_match_first (mdict->dictionaries[idx], name,
1234 &miterator->iterator);
1236 if (result != nullptr)
1243 /* See dictionary.h. */
1246 mdict_iter_match_next (const lookup_name_info &name,
1247 struct mdict_iterator *miterator)
1249 /* Search the current dictionary. */
1250 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1252 if (result != nullptr)
1255 /* The current dictionary had no matches -- move to the next
1256 dictionary, if any. */
1257 for (unsigned short idx = ++miterator->current_idx;
1258 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1261 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1262 name, &miterator->iterator);
1263 if (result != nullptr)
1265 miterator->current_idx = idx;
1273 /* See dictionary.h. */
1276 mdict_size (const struct multidictionary *mdict)
1280 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1281 size += dict_size (mdict->dictionaries[idx]);
1286 /* See dictionary.h. */
1289 mdict_empty (const struct multidictionary *mdict)
1291 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1293 if (!dict_empty (mdict->dictionaries[idx]))