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