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>
33 /* This file implements dictionaries, which are tables that associate
34 symbols to names. They are represented by an opaque type 'struct
35 dictionary'. That type has various internal implementations, which
36 you can choose between depending on what properties you need
37 (e.g. fast lookup, order-preserving, expandable).
39 Each dictionary starts with a 'virtual function table' that
40 contains the functions that actually implement the various
41 operations that dictionaries provide. (Note, however, that, for
42 the sake of client code, we also provide some functions that can be
43 implemented generically in terms of the functions in the vtable.)
45 To add a new dictionary implementation <impl>, what you should do
48 * Add a new element DICT_<IMPL> to dict_type.
50 * Create a new structure dictionary_<impl>. If your new
51 implementation is a variant of an existing one, make sure that
52 their structs have the same initial data members. Define accessor
53 macros for your new data members.
55 * Implement all the functions in dict_vector as static functions,
56 whose name is the same as the corresponding member of dict_vector
57 plus _<impl>. You don't have to do this for those members where
58 you can reuse existing generic functions
59 (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
60 your new implementation is a variant of an existing implementation
61 and where the variant doesn't affect the member function in
64 * Define a static const struct dict_vector dict_<impl>_vector.
66 * Define a function dict_create_<impl> to create these
67 gizmos. Add its declaration to dictionary.h.
69 To add a new operation <op> on all existing implementations, what
72 * Add a new member <op> to struct dict_vector.
74 * If there is useful generic behavior <op>, define a static
75 function <op>_something_informative that implements that behavior.
76 (E.g. add_symbol_nonexpandable, free_obstack.)
78 * For every implementation <impl> that should have its own specific
79 behavior for <op>, define a static function <op>_<impl>
82 * Modify all existing dict_vector_<impl>'s to include the appropriate
85 * Define a function dict_<op> that looks up <op> in the dict_vector
86 and calls the appropriate function. Add a declaration for
87 dict_<op> to dictionary.h. */
89 /* An enum representing the various implementations of dictionaries.
90 Used only for debugging. */
94 /* Symbols are stored in a fixed-size hash table. */
96 /* Symbols are stored in an expandable hash table. */
97 DICT_HASHED_EXPANDABLE,
98 /* Symbols are stored in a fixed-size array. */
100 /* Symbols are stored in an expandable array. */
101 DICT_LINEAR_EXPANDABLE
104 /* The virtual function table. */
108 /* The type of the dictionary. This is only here to make debugging
109 a bit easier; it's not actually used. */
111 /* The function to free a dictionary. */
112 void (*free) (struct dictionary *dict);
113 /* Add a symbol to a dictionary, if possible. */
114 void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
115 /* Iterator functions. */
116 struct symbol *(*iterator_first) (const struct dictionary *dict,
117 struct dict_iterator *iterator);
118 struct symbol *(*iterator_next) (struct dict_iterator *iterator);
119 /* Functions to iterate over symbols with a given name. */
120 struct symbol *(*iter_match_first) (const struct dictionary *dict,
121 const lookup_name_info &name,
122 struct dict_iterator *iterator);
123 struct symbol *(*iter_match_next) (const lookup_name_info &name,
124 struct dict_iterator *iterator);
125 /* A size function, for maint print symtabs. */
126 int (*size) (const struct dictionary *dict);
129 /* Now comes the structs used to store the data for different
130 implementations. If two implementations have data in common, put
131 the common data at the top of their structs, ordered in the same
134 struct dictionary_hashed
137 struct symbol **buckets;
140 struct dictionary_hashed_expandable
142 /* How many buckets we currently have. */
144 struct symbol **buckets;
145 /* How many syms we currently have; we need this so we will know
146 when to add more buckets. */
150 struct dictionary_linear
153 struct symbol **syms;
156 struct dictionary_linear_expandable
158 /* How many symbols we currently have. */
160 struct symbol **syms;
161 /* How many symbols we can store before needing to reallocate. */
165 /* And now, the star of our show. */
169 const struct language_defn *language;
170 const struct dict_vector *vector;
173 struct dictionary_hashed hashed;
174 struct dictionary_hashed_expandable hashed_expandable;
175 struct dictionary_linear linear;
176 struct dictionary_linear_expandable linear_expandable;
181 /* Accessor macros. */
183 #define DICT_VECTOR(d) (d)->vector
184 #define DICT_LANGUAGE(d) (d)->language
186 /* These can be used for DICT_HASHED_EXPANDABLE, too. */
188 #define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
189 #define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
190 #define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
192 #define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
194 /* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
196 #define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
197 #define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
198 #define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
200 #define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
201 (d)->data.linear_expandable.capacity
203 /* The initial size of a DICT_*_EXPANDABLE dictionary. */
205 #define DICT_EXPANDABLE_INITIAL_CAPACITY 10
207 /* This calculates the number of buckets we'll use in a hashtable,
208 given the number of symbols that it will contain. */
210 #define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
212 /* Accessor macros for dict_iterators; they're here rather than
213 dictionary.h because code elsewhere should treat dict_iterators as
216 /* The dictionary that the iterator is associated to. */
217 #define DICT_ITERATOR_DICT(iter) (iter)->dict
218 /* For linear dictionaries, the index of the last symbol returned; for
219 hashed dictionaries, the bucket of the last symbol returned. */
220 #define DICT_ITERATOR_INDEX(iter) (iter)->index
221 /* For hashed dictionaries, this points to the last symbol returned;
222 otherwise, this is unused. */
223 #define DICT_ITERATOR_CURRENT(iter) (iter)->current
225 /* Declarations of functions for vectors. */
227 /* Functions that might work across a range of dictionary types. */
229 static void add_symbol_nonexpandable (struct dictionary *dict,
232 static void free_obstack (struct dictionary *dict);
234 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
237 static struct symbol *iterator_first_hashed (const struct dictionary *dict,
238 struct dict_iterator *iterator);
240 static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
242 static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
243 const lookup_name_info &name,
244 struct dict_iterator *iterator);
246 static struct symbol *iter_match_next_hashed (const lookup_name_info &name,
247 struct dict_iterator *iterator);
249 /* Functions only for DICT_HASHED. */
251 static int size_hashed (const struct dictionary *dict);
253 /* Functions only for DICT_HASHED_EXPANDABLE. */
255 static void free_hashed_expandable (struct dictionary *dict);
257 static void add_symbol_hashed_expandable (struct dictionary *dict,
260 static int size_hashed_expandable (const struct dictionary *dict);
262 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
265 static struct symbol *iterator_first_linear (const struct dictionary *dict,
266 struct dict_iterator *iterator);
268 static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
270 static struct symbol *iter_match_first_linear (const struct dictionary *dict,
271 const lookup_name_info &name,
272 struct dict_iterator *iterator);
274 static struct symbol *iter_match_next_linear (const lookup_name_info &name,
275 struct dict_iterator *iterator);
277 static int size_linear (const struct dictionary *dict);
279 /* Functions only for DICT_LINEAR_EXPANDABLE. */
281 static void free_linear_expandable (struct dictionary *dict);
283 static void add_symbol_linear_expandable (struct dictionary *dict,
286 /* Various vectors that we'll actually use. */
288 static const struct dict_vector dict_hashed_vector =
290 DICT_HASHED, /* type */
291 free_obstack, /* free */
292 add_symbol_nonexpandable, /* add_symbol */
293 iterator_first_hashed, /* iterator_first */
294 iterator_next_hashed, /* iterator_next */
295 iter_match_first_hashed, /* iter_name_first */
296 iter_match_next_hashed, /* iter_name_next */
297 size_hashed, /* size */
300 static const struct dict_vector dict_hashed_expandable_vector =
302 DICT_HASHED_EXPANDABLE, /* type */
303 free_hashed_expandable, /* free */
304 add_symbol_hashed_expandable, /* add_symbol */
305 iterator_first_hashed, /* iterator_first */
306 iterator_next_hashed, /* iterator_next */
307 iter_match_first_hashed, /* iter_name_first */
308 iter_match_next_hashed, /* iter_name_next */
309 size_hashed_expandable, /* size */
312 static const struct dict_vector dict_linear_vector =
314 DICT_LINEAR, /* type */
315 free_obstack, /* free */
316 add_symbol_nonexpandable, /* add_symbol */
317 iterator_first_linear, /* iterator_first */
318 iterator_next_linear, /* iterator_next */
319 iter_match_first_linear, /* iter_name_first */
320 iter_match_next_linear, /* iter_name_next */
321 size_linear, /* size */
324 static const struct dict_vector dict_linear_expandable_vector =
326 DICT_LINEAR_EXPANDABLE, /* type */
327 free_linear_expandable, /* free */
328 add_symbol_linear_expandable, /* add_symbol */
329 iterator_first_linear, /* iterator_first */
330 iterator_next_linear, /* iterator_next */
331 iter_match_first_linear, /* iter_name_first */
332 iter_match_next_linear, /* iter_name_next */
333 size_linear, /* size */
336 /* Declarations of helper functions (i.e. ones that don't go into
339 static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
341 static void insert_symbol_hashed (struct dictionary *dict,
344 static void expand_hashtable (struct dictionary *dict);
346 /* The creation functions. */
348 /* Create a hashed dictionary of a given language. */
350 static struct dictionary *
351 dict_create_hashed (struct obstack *obstack,
352 enum language language,
353 const std::vector<symbol *> &symbol_list)
355 /* Allocate the dictionary. */
356 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
357 DICT_VECTOR (retval) = &dict_hashed_vector;
358 DICT_LANGUAGE (retval) = language_def (language);
360 /* Allocate space for symbols. */
361 int nsyms = symbol_list.size ();
362 int nbuckets = DICT_HASHTABLE_SIZE (nsyms);
363 DICT_HASHED_NBUCKETS (retval) = nbuckets;
364 struct symbol **buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
365 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
366 DICT_HASHED_BUCKETS (retval) = buckets;
368 /* Now fill the buckets. */
369 for (const auto &sym : symbol_list)
370 insert_symbol_hashed (retval, sym);
375 /* Create an expandable hashed dictionary of a given language. */
377 static struct dictionary *
378 dict_create_hashed_expandable (enum language language)
380 struct dictionary *retval = XNEW (struct dictionary);
382 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
383 DICT_LANGUAGE (retval) = language_def (language);
384 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
385 DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
386 DICT_EXPANDABLE_INITIAL_CAPACITY);
387 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
392 /* Create a linear dictionary of a given language. */
394 static struct dictionary *
395 dict_create_linear (struct obstack *obstack,
396 enum language language,
397 const std::vector<symbol *> &symbol_list)
399 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
400 DICT_VECTOR (retval) = &dict_linear_vector;
401 DICT_LANGUAGE (retval) = language_def (language);
403 /* Allocate space for symbols. */
404 int nsyms = symbol_list.size ();
405 DICT_LINEAR_NSYMS (retval) = nsyms;
406 struct symbol **syms = XOBNEWVEC (obstack, struct symbol *, nsyms);
407 DICT_LINEAR_SYMS (retval) = syms;
409 /* Now fill in the symbols. */
411 for (const auto &sym : symbol_list)
417 /* Create an expandable linear dictionary of a given language. */
419 static struct dictionary *
420 dict_create_linear_expandable (enum language language)
422 struct dictionary *retval = XNEW (struct dictionary);
424 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
425 DICT_LANGUAGE (retval) = language_def (language);
426 DICT_LINEAR_NSYMS (retval) = 0;
427 DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
428 DICT_LINEAR_SYMS (retval)
429 = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
434 /* The functions providing the dictionary interface. */
436 /* Free the memory used by a dictionary that's not on an obstack. (If
440 dict_free (struct dictionary *dict)
442 (DICT_VECTOR (dict))->free (dict);
445 /* Add SYM to DICT. DICT had better be expandable. */
448 dict_add_symbol (struct dictionary *dict, struct symbol *sym)
450 (DICT_VECTOR (dict))->add_symbol (dict, sym);
453 /* Utility to add a list of symbols to a dictionary.
454 DICT must be an expandable dictionary. */
457 dict_add_pending (struct dictionary *dict,
458 const std::vector<symbol *> &symbol_list)
460 /* Preserve ordering by reversing the list. */
461 for (auto sym = symbol_list.rbegin (); sym != symbol_list.rend (); ++sym)
462 dict_add_symbol (dict, *sym);
465 /* Initialize ITERATOR to point at the first symbol in DICT, and
466 return that first symbol, or NULL if DICT is empty. */
469 dict_iterator_first (const struct dictionary *dict,
470 struct dict_iterator *iterator)
472 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
475 /* Advance ITERATOR, and return the next symbol, or NULL if there are
479 dict_iterator_next (struct dict_iterator *iterator)
481 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
482 ->iterator_next (iterator);
486 dict_iter_match_first (const struct dictionary *dict,
487 const lookup_name_info &name,
488 struct dict_iterator *iterator)
490 return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
494 dict_iter_match_next (const lookup_name_info &name,
495 struct dict_iterator *iterator)
497 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
498 ->iter_match_next (name, iterator);
502 dict_size (const struct dictionary *dict)
504 return (DICT_VECTOR (dict))->size (dict);
507 /* Now come functions (well, one function, currently) that are
508 implemented generically by means of the vtable. Typically, they're
511 /* Test to see if DICT is empty. */
514 dict_empty (struct dictionary *dict)
516 struct dict_iterator iter;
518 return (dict_iterator_first (dict, &iter) == NULL);
522 /* The functions implementing the dictionary interface. */
524 /* Generic functions, where appropriate. */
527 free_obstack (struct dictionary *dict)
533 add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
535 internal_error (__FILE__, __LINE__,
536 _("dict_add_symbol: non-expandable dictionary"));
539 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
541 static struct symbol *
542 iterator_first_hashed (const struct dictionary *dict,
543 struct dict_iterator *iterator)
545 DICT_ITERATOR_DICT (iterator) = dict;
546 DICT_ITERATOR_INDEX (iterator) = -1;
547 return iterator_hashed_advance (iterator);
550 static struct symbol *
551 iterator_next_hashed (struct dict_iterator *iterator)
555 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
558 return iterator_hashed_advance (iterator);
561 DICT_ITERATOR_CURRENT (iterator) = next;
566 static struct symbol *
567 iterator_hashed_advance (struct dict_iterator *iterator)
569 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
570 int nbuckets = DICT_HASHED_NBUCKETS (dict);
573 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
575 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
579 DICT_ITERATOR_INDEX (iterator) = i;
580 DICT_ITERATOR_CURRENT (iterator) = sym;
588 static struct symbol *
589 iter_match_first_hashed (const struct dictionary *dict,
590 const lookup_name_info &name,
591 struct dict_iterator *iterator)
593 const language_defn *lang = DICT_LANGUAGE (dict);
594 unsigned int hash_index = (name.search_name_hash (lang->la_language)
595 % DICT_HASHED_NBUCKETS (dict));
596 symbol_name_matcher_ftype *matches_name
597 = get_symbol_name_matcher (lang, name);
600 DICT_ITERATOR_DICT (iterator) = dict;
602 /* Loop through the symbols in the given bucket, breaking when SYM
603 first matches. If SYM never matches, it will be set to NULL;
604 either way, we have the right return value. */
606 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
608 sym = sym->hash_next)
610 /* Warning: the order of arguments to compare matters! */
611 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
615 DICT_ITERATOR_CURRENT (iterator) = sym;
619 static struct symbol *
620 iter_match_next_hashed (const lookup_name_info &name,
621 struct dict_iterator *iterator)
623 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
624 symbol_name_matcher_ftype *matches_name
625 = get_symbol_name_matcher (lang, name);
628 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
630 next = next->hash_next)
632 if (matches_name (SYMBOL_SEARCH_NAME (next), name, NULL))
636 DICT_ITERATOR_CURRENT (iterator) = next;
641 /* Insert SYM into DICT. */
644 insert_symbol_hashed (struct dictionary *dict,
647 unsigned int hash_index;
649 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
651 /* We don't want to insert a symbol into a dictionary of a different
652 language. The two may not use the same hashing algorithm. */
653 gdb_assert (SYMBOL_LANGUAGE (sym) == DICT_LANGUAGE (dict)->la_language);
655 hash = search_name_hash (SYMBOL_LANGUAGE (sym), SYMBOL_SEARCH_NAME (sym));
656 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
657 sym->hash_next = buckets[hash_index];
658 buckets[hash_index] = sym;
662 size_hashed (const struct dictionary *dict)
664 return DICT_HASHED_NBUCKETS (dict);
667 /* Functions only for DICT_HASHED_EXPANDABLE. */
670 free_hashed_expandable (struct dictionary *dict)
672 xfree (DICT_HASHED_BUCKETS (dict));
677 add_symbol_hashed_expandable (struct dictionary *dict,
680 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
682 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
683 expand_hashtable (dict);
685 insert_symbol_hashed (dict, sym);
686 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
690 size_hashed_expandable (const struct dictionary *dict)
692 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
696 expand_hashtable (struct dictionary *dict)
698 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
699 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
700 int new_nbuckets = 2 * old_nbuckets + 1;
701 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
704 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
705 DICT_HASHED_BUCKETS (dict) = new_buckets;
707 for (i = 0; i < old_nbuckets; ++i)
709 struct symbol *sym, *next_sym;
711 sym = old_buckets[i];
714 for (next_sym = sym->hash_next;
716 next_sym = sym->hash_next)
718 insert_symbol_hashed (dict, sym);
722 insert_symbol_hashed (dict, sym);
729 /* See dictionary.h. */
732 default_search_name_hash (const char *string0)
734 /* The Ada-encoded version of a name P1.P2...Pn has either the form
735 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
736 are lower-cased identifiers). The <suffix> (which can be empty)
737 encodes additional information about the denoted entity. This
738 routine hashes such names to msymbol_hash_iw(Pn). It actually
739 does this for a superset of both valid Pi and of <suffix>, but
740 in other cases it simply returns msymbol_hash_iw(STRING0). */
748 if (startswith (string, "_ada_"))
751 return msymbol_hash_iw (string0);
762 if (string0 == string)
763 return msymbol_hash_iw (string0);
768 return msymbol_hash_iw (string0);
770 if (string[1] == '_' && string != string0)
774 if ((c < 'a' || c > 'z') && c != 'O')
782 /* Ignore "TKB" suffixes.
784 These are used by Ada for subprograms implementing a task body.
785 For instance for a task T inside package Pck, the name of the
786 subprogram implementing T's body is `pck__tTKB'. We need to
787 ignore the "TKB" suffix because searches for this task body
788 subprogram are going to be performed using `pck__t' (the encoded
789 version of the natural name `pck.t'). */
790 if (strcmp (string, "TKB") == 0)
795 hash = SYMBOL_HASH_NEXT (hash, *string);
801 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
803 static struct symbol *
804 iterator_first_linear (const struct dictionary *dict,
805 struct dict_iterator *iterator)
807 DICT_ITERATOR_DICT (iterator) = dict;
808 DICT_ITERATOR_INDEX (iterator) = 0;
809 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
812 static struct symbol *
813 iterator_next_linear (struct dict_iterator *iterator)
815 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
817 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
820 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
823 static struct symbol *
824 iter_match_first_linear (const struct dictionary *dict,
825 const lookup_name_info &name,
826 struct dict_iterator *iterator)
828 DICT_ITERATOR_DICT (iterator) = dict;
829 DICT_ITERATOR_INDEX (iterator) = -1;
831 return iter_match_next_linear (name, iterator);
834 static struct symbol *
835 iter_match_next_linear (const lookup_name_info &name,
836 struct dict_iterator *iterator)
838 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
839 const language_defn *lang = DICT_LANGUAGE (dict);
840 symbol_name_matcher_ftype *matches_name
841 = get_symbol_name_matcher (lang, name);
843 int i, nsyms = DICT_LINEAR_NSYMS (dict);
844 struct symbol *sym, *retval = NULL;
846 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
848 sym = DICT_LINEAR_SYM (dict, i);
850 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
857 DICT_ITERATOR_INDEX (iterator) = i;
863 size_linear (const struct dictionary *dict)
865 return DICT_LINEAR_NSYMS (dict);
868 /* Functions only for DICT_LINEAR_EXPANDABLE. */
871 free_linear_expandable (struct dictionary *dict)
873 xfree (DICT_LINEAR_SYMS (dict));
879 add_symbol_linear_expandable (struct dictionary *dict,
882 int nsyms = ++DICT_LINEAR_NSYMS (dict);
884 /* Do we have enough room? If not, grow it. */
885 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
887 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
888 DICT_LINEAR_SYMS (dict)
889 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
890 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
893 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
896 /* Multi-language dictionary support. */
898 /* The structure describing a multi-language dictionary. */
900 struct multidictionary
902 /* An array of dictionaries, one per language. All dictionaries
903 must be of the same type. This should be free'd for expandable
905 struct dictionary **dictionaries;
907 /* The number of language dictionaries currently allocated.
908 Only used for expandable dictionaries. */
909 unsigned short n_allocated_dictionaries;
912 /* A hasher for enum language. Injecting this into std is a convenience
913 when using unordered_map with C++11. */
917 template<> struct hash<enum language>
919 typedef enum language argument_type;
920 typedef std::size_t result_type;
922 result_type operator() (const argument_type &l) const noexcept
924 return static_cast<result_type> (l);
927 } /* namespace std */
929 /* A helper function to collate symbols on the pending list by language. */
931 static std::unordered_map<enum language, std::vector<symbol *>>
932 collate_pending_symbols_by_language (const struct pending *symbol_list)
934 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
936 for (const pending *list_counter = symbol_list;
937 list_counter != nullptr; list_counter = list_counter->next)
939 for (int i = list_counter->nsyms - 1; i >= 0; --i)
941 enum language language = SYMBOL_LANGUAGE (list_counter->symbol[i]);
942 nsyms[language].push_back (list_counter->symbol[i]);
949 /* See dictionary.h. */
951 struct multidictionary *
952 mdict_create_hashed (struct obstack *obstack,
953 const struct pending *symbol_list)
955 struct multidictionary *retval
956 = XOBNEW (obstack, struct multidictionary);
957 std::unordered_map<enum language, std::vector<symbol *>> nsyms
958 = collate_pending_symbols_by_language (symbol_list);
960 /* Loop over all languages and create/populate dictionaries. */
962 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
963 retval->n_allocated_dictionaries = nsyms.size ();
966 for (const auto &pair : nsyms)
968 enum language language = pair.first;
969 std::vector<symbol *> symlist = pair.second;
971 retval->dictionaries[idx++]
972 = dict_create_hashed (obstack, language, symlist);
978 /* See dictionary.h. */
980 struct multidictionary *
981 mdict_create_hashed_expandable (enum language language)
983 struct multidictionary *retval = XNEW (struct multidictionary);
985 /* We have no symbol list to populate, but we create an empty
986 dictionary of the requested language to populate later. */
987 retval->n_allocated_dictionaries = 1;
988 retval->dictionaries = XNEW (struct dictionary *);
989 retval->dictionaries[0] = dict_create_hashed_expandable (language);
994 /* See dictionary.h. */
996 struct multidictionary *
997 mdict_create_linear (struct obstack *obstack,
998 const struct pending *symbol_list)
1000 struct multidictionary *retval
1001 = XOBNEW (obstack, struct multidictionary);
1002 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1003 = collate_pending_symbols_by_language (symbol_list);
1005 /* Loop over all languages and create/populate dictionaries. */
1006 retval->dictionaries
1007 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1008 retval->n_allocated_dictionaries = nsyms.size ();
1011 for (const auto &pair : nsyms)
1013 enum language language = pair.first;
1014 std::vector<symbol *> symlist = pair.second;
1016 retval->dictionaries[idx++]
1017 = dict_create_linear (obstack, language, symlist);
1023 /* See dictionary.h. */
1025 struct multidictionary *
1026 mdict_create_linear_expandable (enum language language)
1028 struct multidictionary *retval = XNEW (struct multidictionary);
1030 /* We have no symbol list to populate, but we create an empty
1031 dictionary to populate later. */
1032 retval->n_allocated_dictionaries = 1;
1033 retval->dictionaries = XNEW (struct dictionary *);
1034 retval->dictionaries[0] = dict_create_linear_expandable (language);
1039 /* See dictionary.h. */
1042 mdict_free (struct multidictionary *mdict)
1044 /* Grab the type of dictionary being used. */
1045 enum dict_type type = mdict->dictionaries[0]->vector->type;
1047 /* Loop over all dictionaries and free them. */
1048 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1049 dict_free (mdict->dictionaries[idx]);
1051 /* Free the dictionary list, if needed. */
1056 /* Memory was allocated on an obstack when created. */
1059 case DICT_HASHED_EXPANDABLE:
1060 case DICT_LINEAR_EXPANDABLE:
1061 xfree (mdict->dictionaries);
1066 /* Helper function to find the dictionary associated with LANGUAGE
1067 or NULL if there is no dictionary of that language. */
1069 static struct dictionary *
1070 find_language_dictionary (const struct multidictionary *mdict,
1071 enum language language)
1073 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1075 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1076 return mdict->dictionaries[idx];
1082 /* Create a new language dictionary for LANGUAGE and add it to the
1083 multidictionary MDICT's list of dictionaries. If MDICT is not
1084 based on expandable dictionaries, this function throws an
1087 static struct dictionary *
1088 create_new_language_dictionary (struct multidictionary *mdict,
1089 enum language language)
1091 struct dictionary *retval = nullptr;
1093 /* We use the first dictionary entry to decide what create function
1094 to call. Not optimal but sufficient. */
1095 gdb_assert (mdict->dictionaries[0] != nullptr);
1096 switch (mdict->dictionaries[0]->vector->type)
1100 internal_error (__FILE__, __LINE__,
1101 _("create_new_language_dictionary: attempted to expand "
1102 "non-expandable multidictionary"));
1104 case DICT_HASHED_EXPANDABLE:
1105 retval = dict_create_hashed_expandable (language);
1108 case DICT_LINEAR_EXPANDABLE:
1109 retval = dict_create_linear_expandable (language);
1113 /* Grow the dictionary vector and save the new dictionary. */
1115 = (struct dictionary **) xrealloc (mdict->dictionaries,
1116 (++mdict->n_allocated_dictionaries
1117 * sizeof (struct dictionary *)));
1118 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1123 /* See dictionary.h. */
1126 mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1128 struct dictionary *dict
1129 = find_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1131 if (dict == nullptr)
1133 /* SYM is of a new language that we haven't previously seen.
1134 Create a new dictionary for it. */
1135 dict = create_new_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1138 dict_add_symbol (dict, sym);
1141 /* See dictionary.h. */
1144 mdict_add_pending (struct multidictionary *mdict,
1145 const struct pending *symbol_list)
1147 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1148 = collate_pending_symbols_by_language (symbol_list);
1150 for (const auto &pair : nsyms)
1152 enum language language = pair.first;
1153 std::vector<symbol *> symlist = pair.second;
1154 struct dictionary *dict = find_language_dictionary (mdict, language);
1156 if (dict == nullptr)
1158 /* The language was not previously seen. Create a new dictionary
1160 dict = create_new_language_dictionary (mdict, language);
1163 dict_add_pending (dict, symlist);
1167 /* See dictionary.h. */
1170 mdict_iterator_first (const multidictionary *mdict,
1171 struct mdict_iterator *miterator)
1173 miterator->mdict = mdict;
1174 miterator->current_idx = 0;
1176 for (unsigned short idx = miterator->current_idx;
1177 idx < mdict->n_allocated_dictionaries; ++idx)
1179 struct symbol *result
1180 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1182 if (result != nullptr)
1184 miterator->current_idx = idx;
1192 /* See dictionary.h. */
1195 mdict_iterator_next (struct mdict_iterator *miterator)
1197 struct symbol *result = dict_iterator_next (&miterator->iterator);
1199 if (result != nullptr)
1202 /* The current dictionary had no matches -- move to the next
1203 dictionary, if any. */
1204 for (unsigned short idx = ++miterator->current_idx;
1205 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1208 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1209 &miterator->iterator);
1210 if (result != nullptr)
1212 miterator->current_idx = idx;
1220 /* See dictionary.h. */
1223 mdict_iter_match_first (const struct multidictionary *mdict,
1224 const lookup_name_info &name,
1225 struct mdict_iterator *miterator)
1227 miterator->mdict = mdict;
1228 miterator->current_idx = 0;
1230 for (unsigned short idx = miterator->current_idx;
1231 idx < mdict->n_allocated_dictionaries; ++idx)
1233 struct symbol *result
1234 = dict_iter_match_first (mdict->dictionaries[idx], name,
1235 &miterator->iterator);
1237 if (result != nullptr)
1244 /* See dictionary.h. */
1247 mdict_iter_match_next (const lookup_name_info &name,
1248 struct mdict_iterator *miterator)
1250 /* Search the current dictionary. */
1251 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1253 if (result != nullptr)
1256 /* The current dictionary had no matches -- move to the next
1257 dictionary, if any. */
1258 for (unsigned short idx = ++miterator->current_idx;
1259 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1262 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1263 name, &miterator->iterator);
1264 if (result != nullptr)
1266 miterator->current_idx = idx;
1274 /* See dictionary.h. */
1277 mdict_size (const struct multidictionary *mdict)
1281 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1282 size += dict_size (mdict->dictionaries[idx]);
1287 /* See dictionary.h. */
1290 mdict_empty (const struct multidictionary *mdict)
1292 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1294 if (!dict_empty (mdict->dictionaries[idx]))