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 /* A function to convert a linked list into a vector. */
347 static std::vector<symbol *>
348 pending_to_vector (const struct pending *symbol_list)
350 std::vector<symbol *> symlist;
352 for (const struct pending *list_counter = symbol_list;
353 list_counter != nullptr; list_counter = list_counter->next)
355 for (int i = list_counter->nsyms - 1; i >= 0; --i)
356 symlist.push_back (list_counter->symbol[i]);
362 /* The creation functions. */
364 /* A function to transition dict_create_hashed to new API. */
366 static struct dictionary *
367 dict_create_hashed_1 (struct obstack *obstack,
368 enum language language,
369 const std::vector<symbol *> &symbol_list)
371 /* Allocate the dictionary. */
372 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
373 DICT_VECTOR (retval) = &dict_hashed_vector;
374 DICT_LANGUAGE (retval) = language_def (language);
376 /* Allocate space for symbols. */
377 int nsyms = symbol_list.size ();
378 int nbuckets = DICT_HASHTABLE_SIZE (nsyms);
379 DICT_HASHED_NBUCKETS (retval) = nbuckets;
380 struct symbol **buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
381 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
382 DICT_HASHED_BUCKETS (retval) = buckets;
384 /* Now fill the buckets. */
385 for (const auto &sym : symbol_list)
386 insert_symbol_hashed (retval, sym);
391 /* See dictionary.h. */
394 dict_create_hashed (struct obstack *obstack,
395 enum language language,
396 const struct pending *symbol_list)
398 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
400 return dict_create_hashed_1 (obstack, language, symlist);
403 /* See dictionary.h. */
405 extern struct dictionary *
406 dict_create_hashed_expandable (enum language language)
408 struct dictionary *retval = XNEW (struct dictionary);
410 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
411 DICT_LANGUAGE (retval) = language_def (language);
412 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
413 DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
414 DICT_EXPANDABLE_INITIAL_CAPACITY);
415 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
420 /* A function to transition dict_create_linear to new API. */
422 static struct dictionary *
423 dict_create_linear_1 (struct obstack *obstack,
424 enum language language,
425 const std::vector<symbol *> &symbol_list)
427 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
428 DICT_VECTOR (retval) = &dict_linear_vector;
429 DICT_LANGUAGE (retval) = language_def (language);
431 /* Allocate space for symbols. */
432 int nsyms = symbol_list.size ();
433 DICT_LINEAR_NSYMS (retval) = nsyms;
434 struct symbol **syms = XOBNEWVEC (obstack, struct symbol *, nsyms);
435 DICT_LINEAR_SYMS (retval) = syms;
437 /* Now fill in the symbols. */
439 for (const auto &sym : symbol_list)
445 /* See dictionary.h. */
448 dict_create_linear (struct obstack *obstack,
449 enum language language,
450 const struct pending *symbol_list)
452 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
454 return dict_create_linear_1 (obstack, language, symlist);
457 /* See dictionary.h. */
460 dict_create_linear_expandable (enum language language)
462 struct dictionary *retval = XNEW (struct dictionary);
464 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
465 DICT_LANGUAGE (retval) = language_def (language);
466 DICT_LINEAR_NSYMS (retval) = 0;
467 DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
468 DICT_LINEAR_SYMS (retval)
469 = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
474 /* The functions providing the dictionary interface. */
476 /* Free the memory used by a dictionary that's not on an obstack. (If
480 dict_free (struct dictionary *dict)
482 (DICT_VECTOR (dict))->free (dict);
485 /* Add SYM to DICT. DICT had better be expandable. */
488 dict_add_symbol (struct dictionary *dict, struct symbol *sym)
490 (DICT_VECTOR (dict))->add_symbol (dict, sym);
493 /* A function to transition dict_add_pending to new API. */
496 dict_add_pending_1 (struct dictionary *dict,
497 const std::vector<symbol *> &symbol_list)
499 /* Preserve ordering by reversing the list. */
500 for (auto sym = symbol_list.rbegin (); sym != symbol_list.rend (); ++sym)
501 dict_add_symbol (dict, *sym);
504 /* Utility to add a list of symbols to a dictionary.
505 DICT must be an expandable dictionary. */
508 dict_add_pending (struct dictionary *dict, const struct pending *symbol_list)
510 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
512 dict_add_pending_1 (dict, symlist);
515 /* Initialize ITERATOR to point at the first symbol in DICT, and
516 return that first symbol, or NULL if DICT is empty. */
519 dict_iterator_first (const struct dictionary *dict,
520 struct dict_iterator *iterator)
522 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
525 /* Advance ITERATOR, and return the next symbol, or NULL if there are
529 dict_iterator_next (struct dict_iterator *iterator)
531 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
532 ->iterator_next (iterator);
536 dict_iter_match_first (const struct dictionary *dict,
537 const lookup_name_info &name,
538 struct dict_iterator *iterator)
540 return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
544 dict_iter_match_next (const lookup_name_info &name,
545 struct dict_iterator *iterator)
547 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
548 ->iter_match_next (name, iterator);
552 dict_size (const struct dictionary *dict)
554 return (DICT_VECTOR (dict))->size (dict);
557 /* Now come functions (well, one function, currently) that are
558 implemented generically by means of the vtable. Typically, they're
561 /* Test to see if DICT is empty. */
564 dict_empty (struct dictionary *dict)
566 struct dict_iterator iter;
568 return (dict_iterator_first (dict, &iter) == NULL);
572 /* The functions implementing the dictionary interface. */
574 /* Generic functions, where appropriate. */
577 free_obstack (struct dictionary *dict)
583 add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
585 internal_error (__FILE__, __LINE__,
586 _("dict_add_symbol: non-expandable dictionary"));
589 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
591 static struct symbol *
592 iterator_first_hashed (const struct dictionary *dict,
593 struct dict_iterator *iterator)
595 DICT_ITERATOR_DICT (iterator) = dict;
596 DICT_ITERATOR_INDEX (iterator) = -1;
597 return iterator_hashed_advance (iterator);
600 static struct symbol *
601 iterator_next_hashed (struct dict_iterator *iterator)
605 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
608 return iterator_hashed_advance (iterator);
611 DICT_ITERATOR_CURRENT (iterator) = next;
616 static struct symbol *
617 iterator_hashed_advance (struct dict_iterator *iterator)
619 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
620 int nbuckets = DICT_HASHED_NBUCKETS (dict);
623 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
625 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
629 DICT_ITERATOR_INDEX (iterator) = i;
630 DICT_ITERATOR_CURRENT (iterator) = sym;
638 static struct symbol *
639 iter_match_first_hashed (const struct dictionary *dict,
640 const lookup_name_info &name,
641 struct dict_iterator *iterator)
643 const language_defn *lang = DICT_LANGUAGE (dict);
644 unsigned int hash_index = (name.search_name_hash (lang->la_language)
645 % DICT_HASHED_NBUCKETS (dict));
646 symbol_name_matcher_ftype *matches_name
647 = get_symbol_name_matcher (lang, name);
650 DICT_ITERATOR_DICT (iterator) = dict;
652 /* Loop through the symbols in the given bucket, breaking when SYM
653 first matches. If SYM never matches, it will be set to NULL;
654 either way, we have the right return value. */
656 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
658 sym = sym->hash_next)
660 /* Warning: the order of arguments to compare matters! */
661 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
665 DICT_ITERATOR_CURRENT (iterator) = sym;
669 static struct symbol *
670 iter_match_next_hashed (const lookup_name_info &name,
671 struct dict_iterator *iterator)
673 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
674 symbol_name_matcher_ftype *matches_name
675 = get_symbol_name_matcher (lang, name);
678 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
680 next = next->hash_next)
682 if (matches_name (SYMBOL_SEARCH_NAME (next), name, NULL))
686 DICT_ITERATOR_CURRENT (iterator) = next;
691 /* Insert SYM into DICT. */
694 insert_symbol_hashed (struct dictionary *dict,
697 unsigned int hash_index;
699 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
701 /* We don't want to insert a symbol into a dictionary of a different
702 language. The two may not use the same hashing algorithm. */
703 gdb_assert (SYMBOL_LANGUAGE (sym) == DICT_LANGUAGE (dict)->la_language);
705 hash = search_name_hash (SYMBOL_LANGUAGE (sym), SYMBOL_SEARCH_NAME (sym));
706 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
707 sym->hash_next = buckets[hash_index];
708 buckets[hash_index] = sym;
712 size_hashed (const struct dictionary *dict)
714 return DICT_HASHED_NBUCKETS (dict);
717 /* Functions only for DICT_HASHED_EXPANDABLE. */
720 free_hashed_expandable (struct dictionary *dict)
722 xfree (DICT_HASHED_BUCKETS (dict));
727 add_symbol_hashed_expandable (struct dictionary *dict,
730 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
732 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
733 expand_hashtable (dict);
735 insert_symbol_hashed (dict, sym);
736 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
740 size_hashed_expandable (const struct dictionary *dict)
742 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
746 expand_hashtable (struct dictionary *dict)
748 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
749 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
750 int new_nbuckets = 2 * old_nbuckets + 1;
751 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
754 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
755 DICT_HASHED_BUCKETS (dict) = new_buckets;
757 for (i = 0; i < old_nbuckets; ++i)
759 struct symbol *sym, *next_sym;
761 sym = old_buckets[i];
764 for (next_sym = sym->hash_next;
766 next_sym = sym->hash_next)
768 insert_symbol_hashed (dict, sym);
772 insert_symbol_hashed (dict, sym);
779 /* See dictionary.h. */
782 default_search_name_hash (const char *string0)
784 /* The Ada-encoded version of a name P1.P2...Pn has either the form
785 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
786 are lower-cased identifiers). The <suffix> (which can be empty)
787 encodes additional information about the denoted entity. This
788 routine hashes such names to msymbol_hash_iw(Pn). It actually
789 does this for a superset of both valid Pi and of <suffix>, but
790 in other cases it simply returns msymbol_hash_iw(STRING0). */
798 if (startswith (string, "_ada_"))
801 return msymbol_hash_iw (string0);
812 if (string0 == string)
813 return msymbol_hash_iw (string0);
818 return msymbol_hash_iw (string0);
820 if (string[1] == '_' && string != string0)
824 if ((c < 'a' || c > 'z') && c != 'O')
832 /* Ignore "TKB" suffixes.
834 These are used by Ada for subprograms implementing a task body.
835 For instance for a task T inside package Pck, the name of the
836 subprogram implementing T's body is `pck__tTKB'. We need to
837 ignore the "TKB" suffix because searches for this task body
838 subprogram are going to be performed using `pck__t' (the encoded
839 version of the natural name `pck.t'). */
840 if (strcmp (string, "TKB") == 0)
845 hash = SYMBOL_HASH_NEXT (hash, *string);
851 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
853 static struct symbol *
854 iterator_first_linear (const struct dictionary *dict,
855 struct dict_iterator *iterator)
857 DICT_ITERATOR_DICT (iterator) = dict;
858 DICT_ITERATOR_INDEX (iterator) = 0;
859 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
862 static struct symbol *
863 iterator_next_linear (struct dict_iterator *iterator)
865 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
867 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
870 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
873 static struct symbol *
874 iter_match_first_linear (const struct dictionary *dict,
875 const lookup_name_info &name,
876 struct dict_iterator *iterator)
878 DICT_ITERATOR_DICT (iterator) = dict;
879 DICT_ITERATOR_INDEX (iterator) = -1;
881 return iter_match_next_linear (name, iterator);
884 static struct symbol *
885 iter_match_next_linear (const lookup_name_info &name,
886 struct dict_iterator *iterator)
888 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
889 const language_defn *lang = DICT_LANGUAGE (dict);
890 symbol_name_matcher_ftype *matches_name
891 = get_symbol_name_matcher (lang, name);
893 int i, nsyms = DICT_LINEAR_NSYMS (dict);
894 struct symbol *sym, *retval = NULL;
896 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
898 sym = DICT_LINEAR_SYM (dict, i);
900 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
907 DICT_ITERATOR_INDEX (iterator) = i;
913 size_linear (const struct dictionary *dict)
915 return DICT_LINEAR_NSYMS (dict);
918 /* Functions only for DICT_LINEAR_EXPANDABLE. */
921 free_linear_expandable (struct dictionary *dict)
923 xfree (DICT_LINEAR_SYMS (dict));
929 add_symbol_linear_expandable (struct dictionary *dict,
932 int nsyms = ++DICT_LINEAR_NSYMS (dict);
934 /* Do we have enough room? If not, grow it. */
935 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
937 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
938 DICT_LINEAR_SYMS (dict)
939 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
940 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
943 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
946 /* Multi-language dictionary support. */
948 /* The structure describing a multi-language dictionary. */
950 struct multidictionary
952 /* An array of dictionaries, one per language. All dictionaries
953 must be of the same type. This should be free'd for expandable
955 struct dictionary **dictionaries;
957 /* The number of language dictionaries currently allocated.
958 Only used for expandable dictionaries. */
959 unsigned short n_allocated_dictionaries;
962 /* A hasher for enum language. Injecting this into std is a convenience
963 when using unordered_map with C++11. */
967 template<> struct hash<enum language>
969 typedef enum language argument_type;
970 typedef std::size_t result_type;
972 result_type operator() (const argument_type &l) const noexcept
974 return static_cast<result_type> (l);
977 } /* namespace std */
979 /* A helper function to collate symbols on the pending list by language. */
981 static std::unordered_map<enum language, std::vector<symbol *>>
982 collate_pending_symbols_by_language (const struct pending *symbol_list)
984 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
986 for (const struct pending *list_counter = symbol_list;
987 list_counter != nullptr; list_counter = list_counter->next)
989 for (int i = list_counter->nsyms - 1; i >= 0; --i)
991 enum language language = SYMBOL_LANGUAGE (list_counter->symbol[i]);
992 nsyms[language].push_back (list_counter->symbol[i]);
999 /* See dictionary.h. */
1001 struct multidictionary *
1002 mdict_create_hashed (struct obstack *obstack,
1003 const struct pending *symbol_list)
1005 struct multidictionary *retval
1006 = XOBNEW (obstack, struct multidictionary);
1007 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1008 = collate_pending_symbols_by_language (symbol_list);
1010 /* Loop over all languages and create/populate dictionaries. */
1011 retval->dictionaries
1012 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1013 retval->n_allocated_dictionaries = nsyms.size ();
1016 for (const auto &pair : nsyms)
1018 enum language language = pair.first;
1019 std::vector<symbol *> symlist = pair.second;
1021 retval->dictionaries[idx++]
1022 = dict_create_hashed_1 (obstack, language, symlist);
1028 /* See dictionary.h. */
1030 struct multidictionary *
1031 mdict_create_hashed_expandable (enum language language)
1033 struct multidictionary *retval = XNEW (struct multidictionary);
1035 /* We have no symbol list to populate, but we create an empty
1036 dictionary of the requested language to populate later. */
1037 retval->n_allocated_dictionaries = 1;
1038 retval->dictionaries = XNEW (struct dictionary *);
1039 retval->dictionaries[0] = dict_create_hashed_expandable (language);
1044 /* See dictionary.h. */
1046 struct multidictionary *
1047 mdict_create_linear (struct obstack *obstack,
1048 const struct pending *symbol_list)
1050 struct multidictionary *retval
1051 = XOBNEW (obstack, struct multidictionary);
1052 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1053 = collate_pending_symbols_by_language (symbol_list);
1055 /* Loop over all languages and create/populate dictionaries. */
1056 retval->dictionaries
1057 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1058 retval->n_allocated_dictionaries = nsyms.size ();
1061 for (const auto &pair : nsyms)
1063 enum language language = pair.first;
1064 std::vector<symbol *> symlist = pair.second;
1066 retval->dictionaries[idx++]
1067 = dict_create_linear_1 (obstack, language, symlist);
1073 /* See dictionary.h. */
1075 struct multidictionary *
1076 mdict_create_linear_expandable (enum language language)
1078 struct multidictionary *retval = XNEW (struct multidictionary);
1080 /* We have no symbol list to populate, but we create an empty
1081 dictionary to populate later. */
1082 retval->n_allocated_dictionaries = 1;
1083 retval->dictionaries = XNEW (struct dictionary *);
1084 retval->dictionaries[0] = dict_create_linear_expandable (language);
1089 /* See dictionary.h. */
1092 mdict_free (struct multidictionary *mdict)
1094 /* Grab the type of dictionary being used. */
1095 enum dict_type type = mdict->dictionaries[0]->vector->type;
1097 /* Loop over all dictionaries and free them. */
1098 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1099 dict_free (mdict->dictionaries[idx]);
1101 /* Free the dictionary list, if needed. */
1106 /* Memory was allocated on an obstack when created. */
1109 case DICT_HASHED_EXPANDABLE:
1110 case DICT_LINEAR_EXPANDABLE:
1111 xfree (mdict->dictionaries);
1116 /* Helper function to find the dictionary associated with LANGUAGE
1117 or NULL if there is no dictionary of that language. */
1119 static struct dictionary *
1120 find_language_dictionary (const struct multidictionary *mdict,
1121 enum language language)
1123 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1125 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1126 return mdict->dictionaries[idx];
1132 /* Create a new language dictionary for LANGUAGE and add it to the
1133 multidictionary MDICT's list of dictionaries. If MDICT is not
1134 based on expandable dictionaries, this function throws an
1137 static struct dictionary *
1138 create_new_language_dictionary (struct multidictionary *mdict,
1139 enum language language)
1141 struct dictionary *retval = nullptr;
1143 /* We use the first dictionary entry to decide what create function
1144 to call. Not optimal but sufficient. */
1145 gdb_assert (mdict->dictionaries[0] != nullptr);
1146 switch (mdict->dictionaries[0]->vector->type)
1150 internal_error (__FILE__, __LINE__,
1151 _("create_new_language_dictionary: attempted to expand "
1152 "non-expandable multidictionary"));
1154 case DICT_HASHED_EXPANDABLE:
1155 retval = dict_create_hashed_expandable (language);
1158 case DICT_LINEAR_EXPANDABLE:
1159 retval = dict_create_linear_expandable (language);
1163 /* Grow the dictionary vector and save the new dictionary. */
1165 = (struct dictionary **) xrealloc (mdict->dictionaries,
1166 (++mdict->n_allocated_dictionaries
1167 * sizeof (struct dictionary *)));
1168 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1173 /* See dictionary.h. */
1176 mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1178 struct dictionary *dict
1179 = find_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1181 if (dict == nullptr)
1183 /* SYM is of a new language that we haven't previously seen.
1184 Create a new dictionary for it. */
1185 dict = create_new_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1188 dict_add_symbol (dict, sym);
1191 /* See dictionary.h. */
1194 mdict_add_pending (struct multidictionary *mdict,
1195 const struct pending *symbol_list)
1197 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1198 = collate_pending_symbols_by_language (symbol_list);
1200 for (const auto &pair : nsyms)
1202 enum language language = pair.first;
1203 std::vector<symbol *> symlist = pair.second;
1204 struct dictionary *dict = find_language_dictionary (mdict, language);
1206 if (dict == nullptr)
1208 /* The language was not previously seen. Create a new dictionary
1210 dict = create_new_language_dictionary (mdict, language);
1213 dict_add_pending_1 (dict, symlist);
1217 /* See dictionary.h. */
1220 mdict_iterator_first (const multidictionary *mdict,
1221 struct mdict_iterator *miterator)
1223 miterator->mdict = mdict;
1224 miterator->current_idx = 0;
1226 for (unsigned short idx = miterator->current_idx;
1227 idx < mdict->n_allocated_dictionaries; ++idx)
1229 struct symbol *result
1230 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1232 if (result != nullptr)
1234 miterator->current_idx = idx;
1242 /* See dictionary.h. */
1245 mdict_iterator_next (struct mdict_iterator *miterator)
1247 struct symbol *result = dict_iterator_next (&miterator->iterator);
1249 if (result != nullptr)
1252 /* The current dictionary had no matches -- move to the next
1253 dictionary, if any. */
1254 for (unsigned short idx = ++miterator->current_idx;
1255 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1258 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1259 &miterator->iterator);
1260 if (result != nullptr)
1262 miterator->current_idx = idx;
1270 /* See dictionary.h. */
1273 mdict_iter_match_first (const struct multidictionary *mdict,
1274 const lookup_name_info &name,
1275 struct mdict_iterator *miterator)
1277 miterator->mdict = mdict;
1278 miterator->current_idx = 0;
1280 for (unsigned short idx = miterator->current_idx;
1281 idx < mdict->n_allocated_dictionaries; ++idx)
1283 struct symbol *result
1284 = dict_iter_match_first (mdict->dictionaries[idx], name,
1285 &miterator->iterator);
1287 if (result != nullptr)
1294 /* See dictionary.h. */
1297 mdict_iter_match_next (const lookup_name_info &name,
1298 struct mdict_iterator *miterator)
1300 /* Search the current dictionary. */
1301 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1303 if (result != nullptr)
1306 /* The current dictionary had no matches -- move to the next
1307 dictionary, if any. */
1308 for (unsigned short idx = ++miterator->current_idx;
1309 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1312 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1313 name, &miterator->iterator);
1314 if (result != nullptr)
1316 miterator->current_idx = idx;
1324 /* See dictionary.h. */
1327 mdict_size (const struct multidictionary *mdict)
1331 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1332 size += dict_size (mdict->dictionaries[idx]);
1337 /* See dictionary.h. */
1340 mdict_empty (const struct multidictionary *mdict)
1342 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1344 if (!dict_empty (mdict->dictionaries[idx]))