Fix snafu in version number. Regenerate files
[binutils-gdb.git] / gdb / dictionary.c
blobf87f183fe6843252a90af26ef78aa5f7f05c2e8b
1 /* Routines for name->symbol lookups in GDB.
3 Copyright (C) 2003-2022 Free Software Foundation, Inc.
5 Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
6 Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "defs.h"
24 #include <ctype.h>
25 #include "gdbsupport/gdb_obstack.h"
26 #include "symtab.h"
27 #include "buildsym.h"
28 #include "dictionary.h"
29 #include "safe-ctype.h"
30 #include <unordered_map>
31 #include "language.h"
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
46 is:
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
62 question.
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
70 you should do is:
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>
80 implementing it.
82 * Modify all existing dict_vector_<impl>'s to include the appropriate
83 member.
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. */
92 enum dict_type
94 /* Symbols are stored in a fixed-size hash table. */
95 DICT_HASHED,
96 /* Symbols are stored in an expandable hash table. */
97 DICT_HASHED_EXPANDABLE,
98 /* Symbols are stored in a fixed-size array. */
99 DICT_LINEAR,
100 /* Symbols are stored in an expandable array. */
101 DICT_LINEAR_EXPANDABLE
104 /* The virtual function table. */
106 struct dict_vector
108 /* The type of the dictionary. This is only here to make debugging
109 a bit easier; it's not actually used. */
110 enum dict_type type;
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
132 way. */
134 struct dictionary_hashed
136 int nbuckets;
137 struct symbol **buckets;
140 struct dictionary_hashed_expandable
142 /* How many buckets we currently have. */
143 int nbuckets;
144 struct symbol **buckets;
145 /* How many syms we currently have; we need this so we will know
146 when to add more buckets. */
147 int nsyms;
150 struct dictionary_linear
152 int nsyms;
153 struct symbol **syms;
156 struct dictionary_linear_expandable
158 /* How many symbols we currently have. */
159 int nsyms;
160 struct symbol **syms;
161 /* How many symbols we can store before needing to reallocate. */
162 int capacity;
165 /* And now, the star of our show. */
167 struct dictionary
169 const struct language_defn *language;
170 const struct dict_vector *vector;
171 union
173 struct dictionary_hashed hashed;
174 struct dictionary_hashed_expandable hashed_expandable;
175 struct dictionary_linear linear;
176 struct dictionary_linear_expandable linear_expandable;
178 data;
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
214 opaque. */
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,
230 struct symbol *sym);
232 static void free_obstack (struct dictionary *dict);
234 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
235 dictionaries. */
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,
258 struct symbol *sym);
260 static int size_hashed_expandable (const struct dictionary *dict);
262 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
263 dictionaries. */
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,
284 struct symbol *sym);
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
337 vectors). */
339 static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
341 static void insert_symbol_hashed (struct dictionary *dict,
342 struct symbol *sym);
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);
372 return retval;
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;
389 return retval;
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. */
410 int idx = nsyms - 1;
411 for (const auto &sym : symbol_list)
412 syms[idx--] = sym;
414 return retval;
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));
431 return retval;
434 /* The functions providing the dictionary interface. */
436 /* Free the memory used by a dictionary that's not on an obstack. (If
437 any.) */
439 static void
440 dict_free (struct dictionary *dict)
442 (DICT_VECTOR (dict))->free (dict);
445 /* Add SYM to DICT. DICT had better be expandable. */
447 static void
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. */
456 static void
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. */
468 static struct symbol *
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
476 no more symbols. */
478 static struct symbol *
479 dict_iterator_next (struct dict_iterator *iterator)
481 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
482 ->iterator_next (iterator);
485 static struct symbol *
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);
493 static struct symbol *
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);
501 static int
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
509 rarely used. */
512 /* The functions implementing the dictionary interface. */
514 /* Generic functions, where appropriate. */
516 static void
517 free_obstack (struct dictionary *dict)
519 /* Do nothing! */
522 static void
523 add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
525 internal_error (_("dict_add_symbol: non-expandable dictionary"));
528 /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
530 static struct symbol *
531 iterator_first_hashed (const struct dictionary *dict,
532 struct dict_iterator *iterator)
534 DICT_ITERATOR_DICT (iterator) = dict;
535 DICT_ITERATOR_INDEX (iterator) = -1;
536 return iterator_hashed_advance (iterator);
539 static struct symbol *
540 iterator_next_hashed (struct dict_iterator *iterator)
542 struct symbol *next;
544 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
546 if (next == NULL)
547 return iterator_hashed_advance (iterator);
548 else
550 DICT_ITERATOR_CURRENT (iterator) = next;
551 return next;
555 static struct symbol *
556 iterator_hashed_advance (struct dict_iterator *iterator)
558 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
559 int nbuckets = DICT_HASHED_NBUCKETS (dict);
560 int i;
562 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
564 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
566 if (sym != NULL)
568 DICT_ITERATOR_INDEX (iterator) = i;
569 DICT_ITERATOR_CURRENT (iterator) = sym;
570 return sym;
574 return NULL;
577 static struct symbol *
578 iter_match_first_hashed (const struct dictionary *dict,
579 const lookup_name_info &name,
580 struct dict_iterator *iterator)
582 const language_defn *lang = DICT_LANGUAGE (dict);
583 unsigned int hash_index = (name.search_name_hash (lang->la_language)
584 % DICT_HASHED_NBUCKETS (dict));
585 symbol_name_matcher_ftype *matches_name
586 = lang->get_symbol_name_matcher (name);
587 struct symbol *sym;
589 DICT_ITERATOR_DICT (iterator) = dict;
591 /* Loop through the symbols in the given bucket, breaking when SYM
592 first matches. If SYM never matches, it will be set to NULL;
593 either way, we have the right return value. */
595 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
596 sym != NULL;
597 sym = sym->hash_next)
599 /* Warning: the order of arguments to compare matters! */
600 if (matches_name (sym->search_name (), name, NULL))
601 break;
604 DICT_ITERATOR_CURRENT (iterator) = sym;
605 return sym;
608 static struct symbol *
609 iter_match_next_hashed (const lookup_name_info &name,
610 struct dict_iterator *iterator)
612 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
613 symbol_name_matcher_ftype *matches_name
614 = lang->get_symbol_name_matcher (name);
615 struct symbol *next;
617 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
618 next != NULL;
619 next = next->hash_next)
621 if (matches_name (next->search_name (), name, NULL))
622 break;
625 DICT_ITERATOR_CURRENT (iterator) = next;
627 return next;
630 /* Insert SYM into DICT. */
632 static void
633 insert_symbol_hashed (struct dictionary *dict,
634 struct symbol *sym)
636 unsigned int hash_index;
637 unsigned int hash;
638 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
640 /* We don't want to insert a symbol into a dictionary of a different
641 language. The two may not use the same hashing algorithm. */
642 gdb_assert (sym->language () == DICT_LANGUAGE (dict)->la_language);
644 hash = search_name_hash (sym->language (), sym->search_name ());
645 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
646 sym->hash_next = buckets[hash_index];
647 buckets[hash_index] = sym;
650 static int
651 size_hashed (const struct dictionary *dict)
653 return DICT_HASHED_NBUCKETS (dict);
656 /* Functions only for DICT_HASHED_EXPANDABLE. */
658 static void
659 free_hashed_expandable (struct dictionary *dict)
661 xfree (DICT_HASHED_BUCKETS (dict));
662 xfree (dict);
665 static void
666 add_symbol_hashed_expandable (struct dictionary *dict,
667 struct symbol *sym)
669 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
671 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
672 expand_hashtable (dict);
674 insert_symbol_hashed (dict, sym);
675 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
678 static int
679 size_hashed_expandable (const struct dictionary *dict)
681 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
684 static void
685 expand_hashtable (struct dictionary *dict)
687 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
688 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
689 int new_nbuckets = 2 * old_nbuckets + 1;
690 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
691 int i;
693 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
694 DICT_HASHED_BUCKETS (dict) = new_buckets;
696 for (i = 0; i < old_nbuckets; ++i)
698 struct symbol *sym, *next_sym;
700 sym = old_buckets[i];
701 if (sym != NULL)
703 for (next_sym = sym->hash_next;
704 next_sym != NULL;
705 next_sym = sym->hash_next)
707 insert_symbol_hashed (dict, sym);
708 sym = next_sym;
711 insert_symbol_hashed (dict, sym);
715 xfree (old_buckets);
718 /* See dictionary.h. */
720 unsigned int
721 language_defn::search_name_hash (const char *string0) const
723 /* The Ada-encoded version of a name P1.P2...Pn has either the form
724 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
725 are lower-cased identifiers). The <suffix> (which can be empty)
726 encodes additional information about the denoted entity. This
727 routine hashes such names to msymbol_hash_iw(Pn). It actually
728 does this for a superset of both valid Pi and of <suffix>, but
729 in other cases it simply returns msymbol_hash_iw(STRING0). */
731 const char *string;
732 unsigned int hash;
734 string = string0;
735 if (*string == '_')
737 if (startswith (string, "_ada_"))
738 string += 5;
739 else
740 return msymbol_hash_iw (string0);
743 hash = 0;
744 while (*string)
746 switch (*string)
748 case '$':
749 case '.':
750 case 'X':
751 if (string0 == string)
752 return msymbol_hash_iw (string0);
753 else
754 return hash;
755 case ' ':
756 case '(':
757 return msymbol_hash_iw (string0);
758 case '_':
759 if (string[1] == '_' && string != string0)
761 int c = string[2];
763 if (c == 'B' && string[3] == '_')
765 for (string += 4; ISDIGIT (*string); ++string)
767 continue;
770 if ((c < 'a' || c > 'z') && c != 'O')
771 return hash;
772 hash = 0;
773 string += 2;
774 continue;
776 break;
777 case 'T':
778 /* Ignore "TKB" suffixes.
780 These are used by Ada for subprograms implementing a task body.
781 For instance for a task T inside package Pck, the name of the
782 subprogram implementing T's body is `pck__tTKB'. We need to
783 ignore the "TKB" suffix because searches for this task body
784 subprogram are going to be performed using `pck__t' (the encoded
785 version of the natural name `pck.t'). */
786 if (strcmp (string, "TKB") == 0)
787 return hash;
788 break;
791 hash = SYMBOL_HASH_NEXT (hash, *string);
792 string += 1;
794 return hash;
797 /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
799 static struct symbol *
800 iterator_first_linear (const struct dictionary *dict,
801 struct dict_iterator *iterator)
803 DICT_ITERATOR_DICT (iterator) = dict;
804 DICT_ITERATOR_INDEX (iterator) = 0;
805 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
808 static struct symbol *
809 iterator_next_linear (struct dict_iterator *iterator)
811 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
813 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
814 return NULL;
815 else
816 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
819 static struct symbol *
820 iter_match_first_linear (const struct dictionary *dict,
821 const lookup_name_info &name,
822 struct dict_iterator *iterator)
824 DICT_ITERATOR_DICT (iterator) = dict;
825 DICT_ITERATOR_INDEX (iterator) = -1;
827 return iter_match_next_linear (name, iterator);
830 static struct symbol *
831 iter_match_next_linear (const lookup_name_info &name,
832 struct dict_iterator *iterator)
834 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
835 const language_defn *lang = DICT_LANGUAGE (dict);
836 symbol_name_matcher_ftype *matches_name
837 = lang->get_symbol_name_matcher (name);
839 int i, nsyms = DICT_LINEAR_NSYMS (dict);
840 struct symbol *sym, *retval = NULL;
842 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
844 sym = DICT_LINEAR_SYM (dict, i);
846 if (matches_name (sym->search_name (), name, NULL))
848 retval = sym;
849 break;
853 DICT_ITERATOR_INDEX (iterator) = i;
855 return retval;
858 static int
859 size_linear (const struct dictionary *dict)
861 return DICT_LINEAR_NSYMS (dict);
864 /* Functions only for DICT_LINEAR_EXPANDABLE. */
866 static void
867 free_linear_expandable (struct dictionary *dict)
869 xfree (DICT_LINEAR_SYMS (dict));
870 xfree (dict);
874 static void
875 add_symbol_linear_expandable (struct dictionary *dict,
876 struct symbol *sym)
878 int nsyms = ++DICT_LINEAR_NSYMS (dict);
880 /* Do we have enough room? If not, grow it. */
881 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
883 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
884 DICT_LINEAR_SYMS (dict)
885 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
886 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
889 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
892 /* Multi-language dictionary support. */
894 /* The structure describing a multi-language dictionary. */
896 struct multidictionary
898 /* An array of dictionaries, one per language. All dictionaries
899 must be of the same type. This should be free'd for expandable
900 dictionary types. */
901 struct dictionary **dictionaries;
903 /* The number of language dictionaries currently allocated.
904 Only used for expandable dictionaries. */
905 unsigned short n_allocated_dictionaries;
908 /* A hasher for enum language. Injecting this into std is a convenience
909 when using unordered_map with C++11. */
911 namespace std
913 template<> struct hash<enum language>
915 typedef enum language argument_type;
916 typedef std::size_t result_type;
918 result_type operator() (const argument_type &l) const noexcept
920 return static_cast<result_type> (l);
923 } /* namespace std */
925 /* A helper function to collate symbols on the pending list by language. */
927 static std::unordered_map<enum language, std::vector<symbol *>>
928 collate_pending_symbols_by_language (const struct pending *symbol_list)
930 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
932 for (const pending *list_counter = symbol_list;
933 list_counter != nullptr; list_counter = list_counter->next)
935 for (int i = list_counter->nsyms - 1; i >= 0; --i)
937 enum language language = list_counter->symbol[i]->language ();
938 nsyms[language].push_back (list_counter->symbol[i]);
942 return nsyms;
945 /* See dictionary.h. */
947 struct multidictionary *
948 mdict_create_hashed (struct obstack *obstack,
949 const struct pending *symbol_list)
951 struct multidictionary *retval
952 = XOBNEW (obstack, struct multidictionary);
953 std::unordered_map<enum language, std::vector<symbol *>> nsyms
954 = collate_pending_symbols_by_language (symbol_list);
956 /* Loop over all languages and create/populate dictionaries. */
957 retval->dictionaries
958 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
959 retval->n_allocated_dictionaries = nsyms.size ();
961 int idx = 0;
962 for (const auto &pair : nsyms)
964 enum language language = pair.first;
965 std::vector<symbol *> symlist = pair.second;
967 retval->dictionaries[idx++]
968 = dict_create_hashed (obstack, language, symlist);
971 return retval;
974 /* See dictionary.h. */
976 struct multidictionary *
977 mdict_create_hashed_expandable (enum language language)
979 struct multidictionary *retval = XNEW (struct multidictionary);
981 /* We have no symbol list to populate, but we create an empty
982 dictionary of the requested language to populate later. */
983 retval->n_allocated_dictionaries = 1;
984 retval->dictionaries = XNEW (struct dictionary *);
985 retval->dictionaries[0] = dict_create_hashed_expandable (language);
987 return retval;
990 /* See dictionary.h. */
992 struct multidictionary *
993 mdict_create_linear (struct obstack *obstack,
994 const struct pending *symbol_list)
996 struct multidictionary *retval
997 = XOBNEW (obstack, struct multidictionary);
998 std::unordered_map<enum language, std::vector<symbol *>> nsyms
999 = collate_pending_symbols_by_language (symbol_list);
1001 /* Loop over all languages and create/populate dictionaries. */
1002 retval->dictionaries
1003 = XOBNEWVEC (obstack, struct dictionary *, nsyms.size ());
1004 retval->n_allocated_dictionaries = nsyms.size ();
1006 int idx = 0;
1007 for (const auto &pair : nsyms)
1009 enum language language = pair.first;
1010 std::vector<symbol *> symlist = pair.second;
1012 retval->dictionaries[idx++]
1013 = dict_create_linear (obstack, language, symlist);
1016 return retval;
1019 /* See dictionary.h. */
1021 struct multidictionary *
1022 mdict_create_linear_expandable (enum language language)
1024 struct multidictionary *retval = XNEW (struct multidictionary);
1026 /* We have no symbol list to populate, but we create an empty
1027 dictionary to populate later. */
1028 retval->n_allocated_dictionaries = 1;
1029 retval->dictionaries = XNEW (struct dictionary *);
1030 retval->dictionaries[0] = dict_create_linear_expandable (language);
1032 return retval;
1035 /* See dictionary.h. */
1037 void
1038 mdict_free (struct multidictionary *mdict)
1040 /* Grab the type of dictionary being used. */
1041 enum dict_type type = mdict->dictionaries[0]->vector->type;
1043 /* Loop over all dictionaries and free them. */
1044 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1045 dict_free (mdict->dictionaries[idx]);
1047 /* Free the dictionary list, if needed. */
1048 switch (type)
1050 case DICT_HASHED:
1051 case DICT_LINEAR:
1052 /* Memory was allocated on an obstack when created. */
1053 break;
1055 case DICT_HASHED_EXPANDABLE:
1056 case DICT_LINEAR_EXPANDABLE:
1057 xfree (mdict->dictionaries);
1058 break;
1062 /* Helper function to find the dictionary associated with LANGUAGE
1063 or NULL if there is no dictionary of that language. */
1065 static struct dictionary *
1066 find_language_dictionary (const struct multidictionary *mdict,
1067 enum language language)
1069 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1071 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1072 return mdict->dictionaries[idx];
1075 return nullptr;
1078 /* Create a new language dictionary for LANGUAGE and add it to the
1079 multidictionary MDICT's list of dictionaries. If MDICT is not
1080 based on expandable dictionaries, this function throws an
1081 internal error. */
1083 static struct dictionary *
1084 create_new_language_dictionary (struct multidictionary *mdict,
1085 enum language language)
1087 struct dictionary *retval = nullptr;
1089 /* We use the first dictionary entry to decide what create function
1090 to call. Not optimal but sufficient. */
1091 gdb_assert (mdict->dictionaries[0] != nullptr);
1092 switch (mdict->dictionaries[0]->vector->type)
1094 case DICT_HASHED:
1095 case DICT_LINEAR:
1096 internal_error (_("create_new_language_dictionary: attempted to expand "
1097 "non-expandable multidictionary"));
1099 case DICT_HASHED_EXPANDABLE:
1100 retval = dict_create_hashed_expandable (language);
1101 break;
1103 case DICT_LINEAR_EXPANDABLE:
1104 retval = dict_create_linear_expandable (language);
1105 break;
1108 /* Grow the dictionary vector and save the new dictionary. */
1109 mdict->dictionaries
1110 = (struct dictionary **) xrealloc (mdict->dictionaries,
1111 (++mdict->n_allocated_dictionaries
1112 * sizeof (struct dictionary *)));
1113 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1115 return retval;
1118 /* See dictionary.h. */
1120 void
1121 mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1123 struct dictionary *dict
1124 = find_language_dictionary (mdict, sym->language ());
1126 if (dict == nullptr)
1128 /* SYM is of a new language that we haven't previously seen.
1129 Create a new dictionary for it. */
1130 dict = create_new_language_dictionary (mdict, sym->language ());
1133 dict_add_symbol (dict, sym);
1136 /* See dictionary.h. */
1138 void
1139 mdict_add_pending (struct multidictionary *mdict,
1140 const struct pending *symbol_list)
1142 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1143 = collate_pending_symbols_by_language (symbol_list);
1145 for (const auto &pair : nsyms)
1147 enum language language = pair.first;
1148 std::vector<symbol *> symlist = pair.second;
1149 struct dictionary *dict = find_language_dictionary (mdict, language);
1151 if (dict == nullptr)
1153 /* The language was not previously seen. Create a new dictionary
1154 for it. */
1155 dict = create_new_language_dictionary (mdict, language);
1158 dict_add_pending (dict, symlist);
1162 /* See dictionary.h. */
1164 struct symbol *
1165 mdict_iterator_first (const multidictionary *mdict,
1166 struct mdict_iterator *miterator)
1168 miterator->mdict = mdict;
1169 miterator->current_idx = 0;
1171 for (unsigned short idx = miterator->current_idx;
1172 idx < mdict->n_allocated_dictionaries; ++idx)
1174 struct symbol *result
1175 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1177 if (result != nullptr)
1179 miterator->current_idx = idx;
1180 return result;
1184 return nullptr;
1187 /* See dictionary.h. */
1189 struct symbol *
1190 mdict_iterator_next (struct mdict_iterator *miterator)
1192 struct symbol *result = dict_iterator_next (&miterator->iterator);
1194 if (result != nullptr)
1195 return result;
1197 /* The current dictionary had no matches -- move to the next
1198 dictionary, if any. */
1199 for (unsigned short idx = ++miterator->current_idx;
1200 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1202 result
1203 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1204 &miterator->iterator);
1205 if (result != nullptr)
1207 miterator->current_idx = idx;
1208 return result;
1212 return nullptr;
1215 /* See dictionary.h. */
1217 struct symbol *
1218 mdict_iter_match_first (const struct multidictionary *mdict,
1219 const lookup_name_info &name,
1220 struct mdict_iterator *miterator)
1222 miterator->mdict = mdict;
1223 miterator->current_idx = 0;
1225 for (unsigned short idx = miterator->current_idx;
1226 idx < mdict->n_allocated_dictionaries; ++idx)
1228 struct symbol *result
1229 = dict_iter_match_first (mdict->dictionaries[idx], name,
1230 &miterator->iterator);
1232 if (result != nullptr)
1233 return result;
1236 return nullptr;
1239 /* See dictionary.h. */
1241 struct symbol *
1242 mdict_iter_match_next (const lookup_name_info &name,
1243 struct mdict_iterator *miterator)
1245 /* Search the current dictionary. */
1246 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1248 if (result != nullptr)
1249 return result;
1251 /* The current dictionary had no matches -- move to the next
1252 dictionary, if any. */
1253 for (unsigned short idx = ++miterator->current_idx;
1254 idx < miterator->mdict->n_allocated_dictionaries; ++idx)
1256 result
1257 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1258 name, &miterator->iterator);
1259 if (result != nullptr)
1261 miterator->current_idx = idx;
1262 return result;
1266 return nullptr;
1269 /* See dictionary.h. */
1272 mdict_size (const struct multidictionary *mdict)
1274 int size = 0;
1276 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1277 size += dict_size (mdict->dictionaries[idx]);
1279 return size;