gdb/23712: Use new multidictionary API
[deliverable/binutils-gdb.git] / gdb / dictionary.c
CommitLineData
de4f826b
DC
1/* Routines for name->symbol lookups in GDB.
2
42a4f53d 3 Copyright (C) 2003-2019 Free Software Foundation, Inc.
de4f826b
DC
4
5 Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
6 Inc.
7
8 This file is part of GDB.
9
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
a9762ec7
JB
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
de4f826b 14
a9762ec7
JB
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.
de4f826b
DC
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
de4f826b
DC
22
23#include "defs.h"
c4d840bd 24#include <ctype.h>
de4f826b
DC
25#include "gdb_obstack.h"
26#include "symtab.h"
27#include "buildsym.h"
de4f826b 28#include "dictionary.h"
deeeba55 29#include "safe-ctype.h"
c7748ee9 30#include <unordered_map>
de4f826b
DC
31
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).
37
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.)
43
44 To add a new dictionary implementation <impl>, what you should do
45 is:
46
47 * Add a new element DICT_<IMPL> to dict_type.
48
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.
53
54 * Implement all the functions in dict_vector as static functions,
55 whose name is the same as the corresponding member of dict_vector
56 plus _<impl>. You don't have to do this for those members where
57 you can reuse existing generic functions
58 (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
59 your new implementation is a variant of an existing implementation
60 and where the variant doesn't affect the member function in
61 question.
62
63 * Define a static const struct dict_vector dict_<impl>_vector.
64
65 * Define a function dict_create_<impl> to create these
66 gizmos. Add its declaration to dictionary.h.
67
68 To add a new operation <op> on all existing implementations, what
69 you should do is:
70
71 * Add a new member <op> to struct dict_vector.
72
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.)
76
77 * For every implementation <impl> that should have its own specific
78 behavior for <op>, define a static function <op>_<impl>
79 implementing it.
80
81 * Modify all existing dict_vector_<impl>'s to include the appropriate
82 member.
83
84 * Define a function dict_<op> that looks up <op> in the dict_vector
85 and calls the appropriate function. Add a declaration for
0963b4bd 86 dict_<op> to dictionary.h. */
de4f826b
DC
87
88/* An enum representing the various implementations of dictionaries.
89 Used only for debugging. */
90
91enum dict_type
92 {
93 /* Symbols are stored in a fixed-size hash table. */
94 DICT_HASHED,
95 /* Symbols are stored in an expandable hash table. */
96 DICT_HASHED_EXPANDABLE,
97 /* Symbols are stored in a fixed-size array. */
98 DICT_LINEAR,
99 /* Symbols are stored in an expandable array. */
20605361 100 DICT_LINEAR_EXPANDABLE
de4f826b
DC
101 };
102
103/* The virtual function table. */
104
105struct dict_vector
106{
107 /* The type of the dictionary. This is only here to make debugging
108 a bit easier; it's not actually used. */
109 enum dict_type type;
110 /* The function to free a dictionary. */
111 void (*free) (struct dictionary *dict);
112 /* Add a symbol to a dictionary, if possible. */
113 void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
114 /* Iterator functions. */
115 struct symbol *(*iterator_first) (const struct dictionary *dict,
116 struct dict_iterator *iterator);
117 struct symbol *(*iterator_next) (struct dict_iterator *iterator);
118 /* Functions to iterate over symbols with a given name. */
c4d840bd 119 struct symbol *(*iter_match_first) (const struct dictionary *dict,
b5ec771e 120 const lookup_name_info &name,
2edb89d3 121 struct dict_iterator *iterator);
b5ec771e 122 struct symbol *(*iter_match_next) (const lookup_name_info &name,
de4f826b 123 struct dict_iterator *iterator);
de4f826b
DC
124 /* A size function, for maint print symtabs. */
125 int (*size) (const struct dictionary *dict);
126};
127
128/* Now comes the structs used to store the data for different
129 implementations. If two implementations have data in common, put
130 the common data at the top of their structs, ordered in the same
131 way. */
132
133struct dictionary_hashed
134{
135 int nbuckets;
136 struct symbol **buckets;
137};
138
139struct dictionary_hashed_expandable
140{
141 /* How many buckets we currently have. */
142 int nbuckets;
143 struct symbol **buckets;
144 /* How many syms we currently have; we need this so we will know
145 when to add more buckets. */
146 int nsyms;
147};
148
149struct dictionary_linear
150{
151 int nsyms;
152 struct symbol **syms;
153};
154
155struct dictionary_linear_expandable
156{
157 /* How many symbols we currently have. */
158 int nsyms;
159 struct symbol **syms;
160 /* How many symbols we can store before needing to reallocate. */
161 int capacity;
162};
163
164/* And now, the star of our show. */
165
166struct dictionary
167{
5ffa0793 168 const struct language_defn *language;
de4f826b
DC
169 const struct dict_vector *vector;
170 union
171 {
172 struct dictionary_hashed hashed;
173 struct dictionary_hashed_expandable hashed_expandable;
174 struct dictionary_linear linear;
175 struct dictionary_linear_expandable linear_expandable;
176 }
177 data;
178};
179
180/* Accessor macros. */
181
182#define DICT_VECTOR(d) (d)->vector
5ffa0793 183#define DICT_LANGUAGE(d) (d)->language
de4f826b
DC
184
185/* These can be used for DICT_HASHED_EXPANDABLE, too. */
186
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]
190
191#define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
192
193/* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
194
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]
198
199#define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
200 (d)->data.linear_expandable.capacity
201
202/* The initial size of a DICT_*_EXPANDABLE dictionary. */
203
204#define DICT_EXPANDABLE_INITIAL_CAPACITY 10
205
206/* This calculates the number of buckets we'll use in a hashtable,
207 given the number of symbols that it will contain. */
208
209#define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
210
211/* Accessor macros for dict_iterators; they're here rather than
212 dictionary.h because code elsewhere should treat dict_iterators as
213 opaque. */
214
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
223
224/* Declarations of functions for vectors. */
225
226/* Functions that might work across a range of dictionary types. */
227
228static void add_symbol_nonexpandable (struct dictionary *dict,
229 struct symbol *sym);
230
231static void free_obstack (struct dictionary *dict);
232
233/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
234 dictionaries. */
235
236static struct symbol *iterator_first_hashed (const struct dictionary *dict,
237 struct dict_iterator *iterator);
238
239static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
240
c4d840bd 241static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
b5ec771e 242 const lookup_name_info &name,
de4f826b
DC
243 struct dict_iterator *iterator);
244
b5ec771e 245static struct symbol *iter_match_next_hashed (const lookup_name_info &name,
c4d840bd
PH
246 struct dict_iterator *iterator);
247
de4f826b
DC
248/* Functions only for DICT_HASHED. */
249
250static int size_hashed (const struct dictionary *dict);
251
252/* Functions only for DICT_HASHED_EXPANDABLE. */
253
254static void free_hashed_expandable (struct dictionary *dict);
255
256static void add_symbol_hashed_expandable (struct dictionary *dict,
257 struct symbol *sym);
258
259static int size_hashed_expandable (const struct dictionary *dict);
260
261/* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
262 dictionaries. */
263
264static struct symbol *iterator_first_linear (const struct dictionary *dict,
265 struct dict_iterator *iterator);
266
267static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
268
c4d840bd 269static struct symbol *iter_match_first_linear (const struct dictionary *dict,
b5ec771e 270 const lookup_name_info &name,
c4d840bd 271 struct dict_iterator *iterator);
de4f826b 272
b5ec771e 273static struct symbol *iter_match_next_linear (const lookup_name_info &name,
c4d840bd 274 struct dict_iterator *iterator);
de4f826b
DC
275
276static int size_linear (const struct dictionary *dict);
277
278/* Functions only for DICT_LINEAR_EXPANDABLE. */
279
280static void free_linear_expandable (struct dictionary *dict);
281
282static void add_symbol_linear_expandable (struct dictionary *dict,
283 struct symbol *sym);
284
285/* Various vectors that we'll actually use. */
286
287static const struct dict_vector dict_hashed_vector =
288 {
289 DICT_HASHED, /* type */
290 free_obstack, /* free */
291 add_symbol_nonexpandable, /* add_symbol */
a11a1416 292 iterator_first_hashed, /* iterator_first */
de4f826b 293 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
294 iter_match_first_hashed, /* iter_name_first */
295 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
296 size_hashed, /* size */
297 };
298
299static const struct dict_vector dict_hashed_expandable_vector =
300 {
301 DICT_HASHED_EXPANDABLE, /* type */
302 free_hashed_expandable, /* free */
303 add_symbol_hashed_expandable, /* add_symbol */
a11a1416 304 iterator_first_hashed, /* iterator_first */
de4f826b 305 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
306 iter_match_first_hashed, /* iter_name_first */
307 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
308 size_hashed_expandable, /* size */
309 };
310
311static const struct dict_vector dict_linear_vector =
312 {
313 DICT_LINEAR, /* type */
314 free_obstack, /* free */
315 add_symbol_nonexpandable, /* add_symbol */
a11a1416 316 iterator_first_linear, /* iterator_first */
de4f826b 317 iterator_next_linear, /* iterator_next */
c4d840bd
PH
318 iter_match_first_linear, /* iter_name_first */
319 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
320 size_linear, /* size */
321 };
322
323static const struct dict_vector dict_linear_expandable_vector =
324 {
325 DICT_LINEAR_EXPANDABLE, /* type */
326 free_linear_expandable, /* free */
327 add_symbol_linear_expandable, /* add_symbol */
a11a1416 328 iterator_first_linear, /* iterator_first */
de4f826b 329 iterator_next_linear, /* iterator_next */
c4d840bd
PH
330 iter_match_first_linear, /* iter_name_first */
331 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
332 size_linear, /* size */
333 };
334
335/* Declarations of helper functions (i.e. ones that don't go into
336 vectors). */
337
338static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
339
340static void insert_symbol_hashed (struct dictionary *dict,
341 struct symbol *sym);
342
343static void expand_hashtable (struct dictionary *dict);
344
c7748ee9
KS
345/* A function to convert a linked list into a vector. */
346
347static std::vector<symbol *>
348pending_to_vector (const struct pending *symbol_list)
349{
350 std::vector<symbol *> symlist;
351
352 for (const struct pending *list_counter = symbol_list;
353 list_counter != nullptr; list_counter = list_counter->next)
354 {
355 for (int i = list_counter->nsyms - 1; i >= 0; --i)
356 symlist.push_back (list_counter->symbol[i]);
357 }
358
359 return symlist;
360}
361
de4f826b
DC
362/* The creation functions. */
363
c7748ee9 364/* A function to transition dict_create_hashed to new API. */
de4f826b 365
c7748ee9
KS
366static struct dictionary *
367dict_create_hashed_1 (struct obstack *obstack,
368 enum language language,
369 const std::vector<symbol *> &symbol_list)
de4f826b 370{
c7748ee9
KS
371 /* Allocate the dictionary. */
372 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
de4f826b 373 DICT_VECTOR (retval) = &dict_hashed_vector;
5ffa0793 374 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 375
c7748ee9
KS
376 /* Allocate space for symbols. */
377 int nsyms = symbol_list.size ();
378 int nbuckets = DICT_HASHTABLE_SIZE (nsyms);
de4f826b 379 DICT_HASHED_NBUCKETS (retval) = nbuckets;
c7748ee9 380 struct symbol **buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
de4f826b
DC
381 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
382 DICT_HASHED_BUCKETS (retval) = buckets;
383
384 /* Now fill the buckets. */
c7748ee9
KS
385 for (const auto &sym : symbol_list)
386 insert_symbol_hashed (retval, sym);
de4f826b
DC
387
388 return retval;
389}
390
5ffa0793 391/* See dictionary.h. */
de4f826b 392
c7748ee9
KS
393struct dictionary *
394dict_create_hashed (struct obstack *obstack,
395 enum language language,
396 const struct pending *symbol_list)
397{
398 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
399
400 return dict_create_hashed_1 (obstack, language, symlist);
401}
402
403/* See dictionary.h. */
404
de4f826b 405extern struct dictionary *
5ffa0793 406dict_create_hashed_expandable (enum language language)
de4f826b 407{
8d749320 408 struct dictionary *retval = XNEW (struct dictionary);
de4f826b 409
de4f826b 410 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
5ffa0793 411 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 412 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
224c3ddb
SM
413 DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
414 DICT_EXPANDABLE_INITIAL_CAPACITY);
de4f826b
DC
415 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
416
417 return retval;
418}
419
c7748ee9 420/* A function to transition dict_create_linear to new API. */
de4f826b 421
c7748ee9
KS
422static struct dictionary *
423dict_create_linear_1 (struct obstack *obstack,
424 enum language language,
425 const std::vector<symbol *> &symbol_list)
de4f826b 426{
c7748ee9 427 struct dictionary *retval = XOBNEW (obstack, struct dictionary);
de4f826b 428 DICT_VECTOR (retval) = &dict_linear_vector;
5ffa0793 429 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 430
c7748ee9
KS
431 /* Allocate space for symbols. */
432 int nsyms = symbol_list.size ();
de4f826b 433 DICT_LINEAR_NSYMS (retval) = nsyms;
c7748ee9 434 struct symbol **syms = XOBNEWVEC (obstack, struct symbol *, nsyms);
de4f826b
DC
435 DICT_LINEAR_SYMS (retval) = syms;
436
c7748ee9
KS
437 /* Now fill in the symbols. */
438 int idx = nsyms - 1;
439 for (const auto &sym : symbol_list)
440 syms[idx--] = sym;
de4f826b
DC
441
442 return retval;
443}
444
5ffa0793 445/* See dictionary.h. */
de4f826b 446
c7748ee9
KS
447struct dictionary *
448dict_create_linear (struct obstack *obstack,
449 enum language language,
450 const struct pending *symbol_list)
451{
452 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
453
454 return dict_create_linear_1 (obstack, language, symlist);
455}
456
457/* See dictionary.h. */
458
de4f826b 459struct dictionary *
5ffa0793 460dict_create_linear_expandable (enum language language)
de4f826b 461{
8d749320 462 struct dictionary *retval = XNEW (struct dictionary);
de4f826b 463
de4f826b 464 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
5ffa0793 465 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 466 DICT_LINEAR_NSYMS (retval) = 0;
224c3ddb 467 DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
de4f826b 468 DICT_LINEAR_SYMS (retval)
224c3ddb 469 = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
de4f826b
DC
470
471 return retval;
472}
473
474/* The functions providing the dictionary interface. */
475
476/* Free the memory used by a dictionary that's not on an obstack. (If
477 any.) */
478
479void
480dict_free (struct dictionary *dict)
481{
482 (DICT_VECTOR (dict))->free (dict);
483}
484
485/* Add SYM to DICT. DICT had better be expandable. */
486
487void
488dict_add_symbol (struct dictionary *dict, struct symbol *sym)
489{
490 (DICT_VECTOR (dict))->add_symbol (dict, sym);
491}
492
c7748ee9
KS
493/* A function to transition dict_add_pending to new API. */
494
495static void
496dict_add_pending_1 (struct dictionary *dict,
497 const std::vector<symbol *> &symbol_list)
498{
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);
502}
503
0bfa869d
DE
504/* Utility to add a list of symbols to a dictionary.
505 DICT must be an expandable dictionary. */
506
507void
508dict_add_pending (struct dictionary *dict, const struct pending *symbol_list)
509{
c7748ee9 510 std::vector<symbol *> symlist = pending_to_vector (symbol_list);
0bfa869d 511
c7748ee9 512 dict_add_pending_1 (dict, symlist);
0bfa869d
DE
513}
514
de4f826b
DC
515/* Initialize ITERATOR to point at the first symbol in DICT, and
516 return that first symbol, or NULL if DICT is empty. */
517
518struct symbol *
519dict_iterator_first (const struct dictionary *dict,
520 struct dict_iterator *iterator)
521{
522 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
523}
524
525/* Advance ITERATOR, and return the next symbol, or NULL if there are
526 no more symbols. */
527
528struct symbol *
529dict_iterator_next (struct dict_iterator *iterator)
530{
531 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
532 ->iterator_next (iterator);
533}
534
c4d840bd
PH
535struct symbol *
536dict_iter_match_first (const struct dictionary *dict,
b5ec771e 537 const lookup_name_info &name,
c4d840bd
PH
538 struct dict_iterator *iterator)
539{
b5ec771e 540 return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
c4d840bd
PH
541}
542
543struct symbol *
b5ec771e 544dict_iter_match_next (const lookup_name_info &name,
c4d840bd 545 struct dict_iterator *iterator)
de4f826b
DC
546{
547 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
b5ec771e 548 ->iter_match_next (name, iterator);
de4f826b
DC
549}
550
551int
552dict_size (const struct dictionary *dict)
553{
554 return (DICT_VECTOR (dict))->size (dict);
555}
556
557/* Now come functions (well, one function, currently) that are
558 implemented generically by means of the vtable. Typically, they're
559 rarely used. */
560
561/* Test to see if DICT is empty. */
562
563int
564dict_empty (struct dictionary *dict)
565{
566 struct dict_iterator iter;
567
568 return (dict_iterator_first (dict, &iter) == NULL);
569}
570
571
572/* The functions implementing the dictionary interface. */
573
574/* Generic functions, where appropriate. */
575
576static void
577free_obstack (struct dictionary *dict)
578{
579 /* Do nothing! */
580}
581
582static void
583add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
584{
585 internal_error (__FILE__, __LINE__,
e2e0b3e5 586 _("dict_add_symbol: non-expandable dictionary"));
de4f826b
DC
587}
588
589/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
590
591static struct symbol *
592iterator_first_hashed (const struct dictionary *dict,
593 struct dict_iterator *iterator)
594{
595 DICT_ITERATOR_DICT (iterator) = dict;
596 DICT_ITERATOR_INDEX (iterator) = -1;
597 return iterator_hashed_advance (iterator);
598}
599
600static struct symbol *
601iterator_next_hashed (struct dict_iterator *iterator)
602{
de4f826b
DC
603 struct symbol *next;
604
605 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
606
607 if (next == NULL)
608 return iterator_hashed_advance (iterator);
609 else
610 {
611 DICT_ITERATOR_CURRENT (iterator) = next;
612 return next;
613 }
614}
615
616static struct symbol *
617iterator_hashed_advance (struct dict_iterator *iterator)
618{
619 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
620 int nbuckets = DICT_HASHED_NBUCKETS (dict);
621 int i;
622
623 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
624 {
625 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
626
627 if (sym != NULL)
628 {
629 DICT_ITERATOR_INDEX (iterator) = i;
630 DICT_ITERATOR_CURRENT (iterator) = sym;
631 return sym;
632 }
633 }
634
635 return NULL;
636}
637
638static struct symbol *
b5ec771e
PA
639iter_match_first_hashed (const struct dictionary *dict,
640 const lookup_name_info &name,
c4d840bd 641 struct dict_iterator *iterator)
de4f826b 642{
b5ec771e
PA
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
618daa93 647 = get_symbol_name_matcher (lang, name);
de4f826b
DC
648 struct symbol *sym;
649
650 DICT_ITERATOR_DICT (iterator) = dict;
651
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. */
655
656 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
657 sym != NULL;
658 sym = sym->hash_next)
659 {
c4d840bd 660 /* Warning: the order of arguments to compare matters! */
b5ec771e
PA
661 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
662 break;
de4f826b
DC
663 }
664
665 DICT_ITERATOR_CURRENT (iterator) = sym;
666 return sym;
667}
668
669static struct symbol *
b5ec771e 670iter_match_next_hashed (const lookup_name_info &name,
c4d840bd 671 struct dict_iterator *iterator)
de4f826b 672{
b5ec771e
PA
673 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
674 symbol_name_matcher_ftype *matches_name
618daa93 675 = get_symbol_name_matcher (lang, name);
de4f826b
DC
676 struct symbol *next;
677
678 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
679 next != NULL;
680 next = next->hash_next)
681 {
b5ec771e 682 if (matches_name (SYMBOL_SEARCH_NAME (next), name, NULL))
de4f826b
DC
683 break;
684 }
685
686 DICT_ITERATOR_CURRENT (iterator) = next;
687
688 return next;
689}
690
691/* Insert SYM into DICT. */
692
693static void
694insert_symbol_hashed (struct dictionary *dict,
695 struct symbol *sym)
696{
697 unsigned int hash_index;
5ffa0793 698 unsigned int hash;
de4f826b
DC
699 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
700
5ffa0793
PA
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);
704
705 hash = search_name_hash (SYMBOL_LANGUAGE (sym), SYMBOL_SEARCH_NAME (sym));
706 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
de4f826b
DC
707 sym->hash_next = buckets[hash_index];
708 buckets[hash_index] = sym;
709}
710
711static int
712size_hashed (const struct dictionary *dict)
713{
714 return DICT_HASHED_NBUCKETS (dict);
715}
716
717/* Functions only for DICT_HASHED_EXPANDABLE. */
718
719static void
720free_hashed_expandable (struct dictionary *dict)
721{
722 xfree (DICT_HASHED_BUCKETS (dict));
723 xfree (dict);
724}
725
726static void
727add_symbol_hashed_expandable (struct dictionary *dict,
728 struct symbol *sym)
729{
730 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
731
732 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
733 expand_hashtable (dict);
734
735 insert_symbol_hashed (dict, sym);
736 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
737}
738
739static int
740size_hashed_expandable (const struct dictionary *dict)
741{
742 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
743}
744
745static void
746expand_hashtable (struct dictionary *dict)
747{
748 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
749 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
224c3ddb
SM
750 int new_nbuckets = 2 * old_nbuckets + 1;
751 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
de4f826b
DC
752 int i;
753
754 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
755 DICT_HASHED_BUCKETS (dict) = new_buckets;
756
6595d32b
MS
757 for (i = 0; i < old_nbuckets; ++i)
758 {
759 struct symbol *sym, *next_sym;
760
761 sym = old_buckets[i];
762 if (sym != NULL)
763 {
764 for (next_sym = sym->hash_next;
765 next_sym != NULL;
766 next_sym = sym->hash_next)
767 {
768 insert_symbol_hashed (dict, sym);
769 sym = next_sym;
770 }
771
772 insert_symbol_hashed (dict, sym);
773 }
de4f826b 774 }
de4f826b
DC
775
776 xfree (old_buckets);
777}
778
5ffa0793 779/* See dictionary.h. */
c4d840bd 780
5ffa0793
PA
781unsigned int
782default_search_name_hash (const char *string0)
c4d840bd
PH
783{
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). */
791
1d2a4540 792 const char *string;
c4d840bd 793 unsigned int hash;
c4d840bd 794
1d2a4540
PH
795 string = string0;
796 if (*string == '_')
797 {
61012eef 798 if (startswith (string, "_ada_"))
1d2a4540
PH
799 string += 5;
800 else
801 return msymbol_hash_iw (string0);
802 }
c4d840bd
PH
803
804 hash = 0;
805 while (*string)
806 {
807 switch (*string)
808 {
809 case '$':
810 case '.':
811 case 'X':
1d2a4540
PH
812 if (string0 == string)
813 return msymbol_hash_iw (string0);
814 else
815 return hash;
c4d840bd 816 case ' ':
1d2a4540
PH
817 case '(':
818 return msymbol_hash_iw (string0);
c4d840bd 819 case '_':
1d2a4540 820 if (string[1] == '_' && string != string0)
c4d840bd 821 {
558b1900
JB
822 int c = string[2];
823
824 if ((c < 'a' || c > 'z') && c != 'O')
c4d840bd
PH
825 return hash;
826 hash = 0;
827 string += 2;
7e8835c5 828 continue;
c4d840bd 829 }
7e8835c5
TT
830 break;
831 case 'T':
832 /* Ignore "TKB" suffixes.
833
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)
841 return hash;
c4d840bd
PH
842 break;
843 }
7e8835c5
TT
844
845 hash = SYMBOL_HASH_NEXT (hash, *string);
846 string += 1;
c4d840bd
PH
847 }
848 return hash;
849}
850
de4f826b
DC
851/* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
852
853static struct symbol *
854iterator_first_linear (const struct dictionary *dict,
855 struct dict_iterator *iterator)
856{
857 DICT_ITERATOR_DICT (iterator) = dict;
858 DICT_ITERATOR_INDEX (iterator) = 0;
859 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
860}
861
862static struct symbol *
863iterator_next_linear (struct dict_iterator *iterator)
864{
865 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
866
867 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
868 return NULL;
869 else
870 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
871}
872
873static struct symbol *
c4d840bd 874iter_match_first_linear (const struct dictionary *dict,
b5ec771e 875 const lookup_name_info &name,
c4d840bd 876 struct dict_iterator *iterator)
de4f826b
DC
877{
878 DICT_ITERATOR_DICT (iterator) = dict;
879 DICT_ITERATOR_INDEX (iterator) = -1;
880
b5ec771e 881 return iter_match_next_linear (name, iterator);
de4f826b
DC
882}
883
884static struct symbol *
b5ec771e 885iter_match_next_linear (const lookup_name_info &name,
c4d840bd 886 struct dict_iterator *iterator)
de4f826b
DC
887{
888 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
b5ec771e
PA
889 const language_defn *lang = DICT_LANGUAGE (dict);
890 symbol_name_matcher_ftype *matches_name
618daa93 891 = get_symbol_name_matcher (lang, name);
b5ec771e 892
de4f826b
DC
893 int i, nsyms = DICT_LINEAR_NSYMS (dict);
894 struct symbol *sym, *retval = NULL;
895
896 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
897 {
898 sym = DICT_LINEAR_SYM (dict, i);
b5ec771e
PA
899
900 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
de4f826b
DC
901 {
902 retval = sym;
903 break;
904 }
905 }
906
907 DICT_ITERATOR_INDEX (iterator) = i;
908
909 return retval;
910}
911
912static int
913size_linear (const struct dictionary *dict)
914{
915 return DICT_LINEAR_NSYMS (dict);
916}
917
918/* Functions only for DICT_LINEAR_EXPANDABLE. */
919
920static void
921free_linear_expandable (struct dictionary *dict)
922{
923 xfree (DICT_LINEAR_SYMS (dict));
924 xfree (dict);
925}
926
927
928static void
929add_symbol_linear_expandable (struct dictionary *dict,
930 struct symbol *sym)
931{
932 int nsyms = ++DICT_LINEAR_NSYMS (dict);
933
934 /* Do we have enough room? If not, grow it. */
6595d32b
MS
935 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
936 {
937 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
938 DICT_LINEAR_SYMS (dict)
224c3ddb
SM
939 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
940 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
6595d32b 941 }
de4f826b
DC
942
943 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
944}
c7748ee9
KS
945
946/* Multi-language dictionary support. */
947
948/* The structure describing a multi-language dictionary. */
949
950struct multidictionary
951{
952 /* An array of dictionaries, one per language. All dictionaries
953 must be of the same type. This should be free'd for expandable
954 dictionary types. */
955 struct dictionary **dictionaries;
956
957 /* The number of language dictionaries currently allocated.
958 Only used for expandable dictionaries. */
959 unsigned short n_allocated_dictionaries;
960};
961
962/* A hasher for enum language. Injecting this into std is a convenience
963 when using unordered_map with C++11. */
964
965namespace std
966{
967 template<> struct hash<enum language>
968 {
969 typedef enum language argument_type;
970 typedef std::size_t result_type;
971
972 result_type operator() (const argument_type &l) const noexcept
973 {
974 return static_cast<result_type> (l);
975 }
976 };
977} /* namespace std */
978
979/* A helper function to collate symbols on the pending list by language. */
980
981static std::unordered_map<enum language, std::vector<symbol *>>
982collate_pending_symbols_by_language (const struct pending *symbol_list)
983{
984 std::unordered_map<enum language, std::vector<symbol *>> nsyms;
985
986 for (const struct pending *list_counter = symbol_list;
987 list_counter != nullptr; list_counter = list_counter->next)
988 {
989 for (int i = list_counter->nsyms - 1; i >= 0; --i)
990 {
991 enum language language = SYMBOL_LANGUAGE (list_counter->symbol[i]);
992 nsyms[language].push_back (list_counter->symbol[i]);
993 }
994 }
995
996 return nsyms;
997}
998
999/* See dictionary.h. */
1000
1001struct multidictionary *
1002mdict_create_hashed (struct obstack *obstack,
1003 const struct pending *symbol_list)
1004{
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);
1009
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 ();
1014
1015 int idx = 0;
1016 for (const auto &pair : nsyms)
1017 {
1018 enum language language = pair.first;
1019 std::vector<symbol *> symlist = pair.second;
1020
1021 retval->dictionaries[idx++]
1022 = dict_create_hashed_1 (obstack, language, symlist);
1023 }
1024
1025 return retval;
1026}
1027
1028/* See dictionary.h. */
1029
1030struct multidictionary *
1031mdict_create_hashed_expandable (enum language language)
1032{
1033 struct multidictionary *retval = XNEW (struct multidictionary);
1034
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);
1040
1041 return retval;
1042}
1043
1044/* See dictionary.h. */
1045
1046struct multidictionary *
1047mdict_create_linear (struct obstack *obstack,
1048 const struct pending *symbol_list)
1049{
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);
1054
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 ();
1059
1060 int idx = 0;
1061 for (const auto &pair : nsyms)
1062 {
1063 enum language language = pair.first;
1064 std::vector<symbol *> symlist = pair.second;
1065
1066 retval->dictionaries[idx++]
1067 = dict_create_linear_1 (obstack, language, symlist);
1068 }
1069
1070 return retval;
1071}
1072
1073/* See dictionary.h. */
1074
1075struct multidictionary *
1076mdict_create_linear_expandable (enum language language)
1077{
1078 struct multidictionary *retval = XNEW (struct multidictionary);
1079
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);
1085
1086 return retval;
1087}
1088
1089/* See dictionary.h. */
1090
1091void
1092mdict_free (struct multidictionary *mdict)
1093{
1094 /* Grab the type of dictionary being used. */
1095 enum dict_type type = mdict->dictionaries[0]->vector->type;
1096
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]);
1100
1101 /* Free the dictionary list, if needed. */
1102 switch (type)
1103 {
1104 case DICT_HASHED:
1105 case DICT_LINEAR:
1106 /* Memory was allocated on an obstack when created. */
1107 break;
1108
1109 case DICT_HASHED_EXPANDABLE:
1110 case DICT_LINEAR_EXPANDABLE:
1111 xfree (mdict->dictionaries);
1112 break;
1113 }
1114}
1115
1116/* Helper function to find the dictionary associated with LANGUAGE
1117 or NULL if there is no dictionary of that language. */
1118
1119static struct dictionary *
1120find_language_dictionary (const struct multidictionary *mdict,
1121 enum language language)
1122{
1123 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1124 {
1125 if (DICT_LANGUAGE (mdict->dictionaries[idx])->la_language == language)
1126 return mdict->dictionaries[idx];
1127 }
1128
1129 return nullptr;
1130}
1131
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
1135 internal error. */
1136
1137static struct dictionary *
1138create_new_language_dictionary (struct multidictionary *mdict,
1139 enum language language)
1140{
1141 struct dictionary *retval = nullptr;
1142
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)
1147 {
1148 case DICT_HASHED:
1149 case DICT_LINEAR:
1150 internal_error (__FILE__, __LINE__,
1151 _("create_new_language_dictionary: attempted to expand "
1152 "non-expandable multidictionary"));
1153
1154 case DICT_HASHED_EXPANDABLE:
1155 retval = dict_create_hashed_expandable (language);
1156 break;
1157
1158 case DICT_LINEAR_EXPANDABLE:
1159 retval = dict_create_linear_expandable (language);
1160 break;
1161 }
1162
1163 /* Grow the dictionary vector and save the new dictionary. */
1164 mdict->dictionaries
1165 = (struct dictionary **) xrealloc (mdict->dictionaries,
1166 (++mdict->n_allocated_dictionaries
1167 * sizeof (struct dictionary *)));
1168 mdict->dictionaries[mdict->n_allocated_dictionaries - 1] = retval;
1169
1170 return retval;
1171}
1172
1173/* See dictionary.h. */
1174
1175void
1176mdict_add_symbol (struct multidictionary *mdict, struct symbol *sym)
1177{
1178 struct dictionary *dict
1179 = find_language_dictionary (mdict, SYMBOL_LANGUAGE (sym));
1180
1181 if (dict == nullptr)
1182 {
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));
1186 }
1187
1188 dict_add_symbol (dict, sym);
1189}
1190
1191/* See dictionary.h. */
1192
1193void
1194mdict_add_pending (struct multidictionary *mdict,
1195 const struct pending *symbol_list)
1196{
1197 std::unordered_map<enum language, std::vector<symbol *>> nsyms
1198 = collate_pending_symbols_by_language (symbol_list);
1199
1200 for (const auto &pair : nsyms)
1201 {
1202 enum language language = pair.first;
1203 std::vector<symbol *> symlist = pair.second;
1204 struct dictionary *dict = find_language_dictionary (mdict, language);
1205
1206 if (dict == nullptr)
1207 {
1208 /* The language was not previously seen. Create a new dictionary
1209 for it. */
1210 dict = create_new_language_dictionary (mdict, language);
1211 }
1212
1213 dict_add_pending_1 (dict, symlist);
1214 }
1215}
1216
1217/* See dictionary.h. */
1218
1219struct symbol *
1220mdict_iterator_first (const multidictionary *mdict,
1221 struct mdict_iterator *miterator)
1222{
1223 miterator->mdict = mdict;
1224 miterator->current_idx = 0;
1225
1226 for (unsigned short idx = miterator->current_idx;
1227 idx < mdict->n_allocated_dictionaries; ++idx)
1228 {
1229 struct symbol *result
1230 = dict_iterator_first (mdict->dictionaries[idx], &miterator->iterator);
1231
1232 if (result != nullptr)
1233 {
1234 miterator->current_idx = idx;
1235 return result;
1236 }
1237 }
1238
1239 return nullptr;
1240}
1241
1242/* See dictionary.h. */
1243
1244struct symbol *
1245mdict_iterator_next (struct mdict_iterator *miterator)
1246{
1247 struct symbol *result = dict_iterator_next (&miterator->iterator);
1248
1249 if (result != nullptr)
1250 return result;
1251
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)
1256 {
1257 result
1258 = dict_iterator_first (miterator->mdict->dictionaries[idx],
1259 &miterator->iterator);
1260 if (result != nullptr)
1261 {
1262 miterator->current_idx = idx;
1263 return result;
1264 }
1265 }
1266
1267 return nullptr;
1268}
1269
1270/* See dictionary.h. */
1271
1272struct symbol *
1273mdict_iter_match_first (const struct multidictionary *mdict,
1274 const lookup_name_info &name,
1275 struct mdict_iterator *miterator)
1276{
1277 miterator->mdict = mdict;
1278 miterator->current_idx = 0;
1279
1280 for (unsigned short idx = miterator->current_idx;
1281 idx < mdict->n_allocated_dictionaries; ++idx)
1282 {
1283 struct symbol *result
1284 = dict_iter_match_first (mdict->dictionaries[idx], name,
1285 &miterator->iterator);
1286
1287 if (result != nullptr)
1288 return result;
1289 }
1290
1291 return nullptr;
1292}
1293
1294/* See dictionary.h. */
1295
1296struct symbol *
1297mdict_iter_match_next (const lookup_name_info &name,
1298 struct mdict_iterator *miterator)
1299{
1300 /* Search the current dictionary. */
1301 struct symbol *result = dict_iter_match_next (name, &miterator->iterator);
1302
1303 if (result != nullptr)
1304 return result;
1305
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)
1310 {
1311 result
1312 = dict_iter_match_first (miterator->mdict->dictionaries[idx],
1313 name, &miterator->iterator);
1314 if (result != nullptr)
1315 {
1316 miterator->current_idx = idx;
1317 return result;
1318 }
1319 }
1320
1321 return nullptr;
1322}
1323
1324/* See dictionary.h. */
1325
1326int
1327mdict_size (const struct multidictionary *mdict)
1328{
1329 int size = 0;
1330
1331 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1332 size += dict_size (mdict->dictionaries[idx]);
1333
1334 return size;
1335}
1336
1337/* See dictionary.h. */
1338
1339bool
1340mdict_empty (const struct multidictionary *mdict)
1341{
1342 for (unsigned short idx = 0; idx < mdict->n_allocated_dictionaries; ++idx)
1343 {
1344 if (!dict_empty (mdict->dictionaries[idx]))
1345 return false;
1346 }
1347
1348 return true;
1349}
This page took 1.316393 seconds and 4 git commands to generate.