[spu] Fix spu-linux gdbserver build
[deliverable/binutils-gdb.git] / gdb / dictionary.c
CommitLineData
de4f826b
DC
1/* Routines for name->symbol lookups in GDB.
2
61baf725 3 Copyright (C) 2003-2017 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
DC
28#include "dictionary.h"
29
30/* This file implements dictionaries, which are tables that associate
31 symbols to names. They are represented by an opaque type 'struct
32 dictionary'. That type has various internal implementations, which
33 you can choose between depending on what properties you need
34 (e.g. fast lookup, order-preserving, expandable).
35
36 Each dictionary starts with a 'virtual function table' that
37 contains the functions that actually implement the various
38 operations that dictionaries provide. (Note, however, that, for
39 the sake of client code, we also provide some functions that can be
40 implemented generically in terms of the functions in the vtable.)
41
42 To add a new dictionary implementation <impl>, what you should do
43 is:
44
45 * Add a new element DICT_<IMPL> to dict_type.
46
47 * Create a new structure dictionary_<impl>. If your new
48 implementation is a variant of an existing one, make sure that
49 their structs have the same initial data members. Define accessor
50 macros for your new data members.
51
52 * Implement all the functions in dict_vector as static functions,
53 whose name is the same as the corresponding member of dict_vector
54 plus _<impl>. You don't have to do this for those members where
55 you can reuse existing generic functions
56 (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
57 your new implementation is a variant of an existing implementation
58 and where the variant doesn't affect the member function in
59 question.
60
61 * Define a static const struct dict_vector dict_<impl>_vector.
62
63 * Define a function dict_create_<impl> to create these
64 gizmos. Add its declaration to dictionary.h.
65
66 To add a new operation <op> on all existing implementations, what
67 you should do is:
68
69 * Add a new member <op> to struct dict_vector.
70
71 * If there is useful generic behavior <op>, define a static
72 function <op>_something_informative that implements that behavior.
73 (E.g. add_symbol_nonexpandable, free_obstack.)
74
75 * For every implementation <impl> that should have its own specific
76 behavior for <op>, define a static function <op>_<impl>
77 implementing it.
78
79 * Modify all existing dict_vector_<impl>'s to include the appropriate
80 member.
81
82 * Define a function dict_<op> that looks up <op> in the dict_vector
83 and calls the appropriate function. Add a declaration for
0963b4bd 84 dict_<op> to dictionary.h. */
de4f826b
DC
85
86/* An enum representing the various implementations of dictionaries.
87 Used only for debugging. */
88
89enum dict_type
90 {
91 /* Symbols are stored in a fixed-size hash table. */
92 DICT_HASHED,
93 /* Symbols are stored in an expandable hash table. */
94 DICT_HASHED_EXPANDABLE,
95 /* Symbols are stored in a fixed-size array. */
96 DICT_LINEAR,
97 /* Symbols are stored in an expandable array. */
20605361 98 DICT_LINEAR_EXPANDABLE
de4f826b
DC
99 };
100
101/* The virtual function table. */
102
103struct dict_vector
104{
105 /* The type of the dictionary. This is only here to make debugging
106 a bit easier; it's not actually used. */
107 enum dict_type type;
108 /* The function to free a dictionary. */
109 void (*free) (struct dictionary *dict);
110 /* Add a symbol to a dictionary, if possible. */
111 void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
112 /* Iterator functions. */
113 struct symbol *(*iterator_first) (const struct dictionary *dict,
114 struct dict_iterator *iterator);
115 struct symbol *(*iterator_next) (struct dict_iterator *iterator);
116 /* Functions to iterate over symbols with a given name. */
c4d840bd 117 struct symbol *(*iter_match_first) (const struct dictionary *dict,
b5ec771e 118 const lookup_name_info &name,
2edb89d3 119 struct dict_iterator *iterator);
b5ec771e 120 struct symbol *(*iter_match_next) (const lookup_name_info &name,
de4f826b 121 struct dict_iterator *iterator);
de4f826b
DC
122 /* A size function, for maint print symtabs. */
123 int (*size) (const struct dictionary *dict);
124};
125
126/* Now comes the structs used to store the data for different
127 implementations. If two implementations have data in common, put
128 the common data at the top of their structs, ordered in the same
129 way. */
130
131struct dictionary_hashed
132{
133 int nbuckets;
134 struct symbol **buckets;
135};
136
137struct dictionary_hashed_expandable
138{
139 /* How many buckets we currently have. */
140 int nbuckets;
141 struct symbol **buckets;
142 /* How many syms we currently have; we need this so we will know
143 when to add more buckets. */
144 int nsyms;
145};
146
147struct dictionary_linear
148{
149 int nsyms;
150 struct symbol **syms;
151};
152
153struct dictionary_linear_expandable
154{
155 /* How many symbols we currently have. */
156 int nsyms;
157 struct symbol **syms;
158 /* How many symbols we can store before needing to reallocate. */
159 int capacity;
160};
161
162/* And now, the star of our show. */
163
164struct dictionary
165{
5ffa0793 166 const struct language_defn *language;
de4f826b
DC
167 const struct dict_vector *vector;
168 union
169 {
170 struct dictionary_hashed hashed;
171 struct dictionary_hashed_expandable hashed_expandable;
172 struct dictionary_linear linear;
173 struct dictionary_linear_expandable linear_expandable;
174 }
175 data;
176};
177
178/* Accessor macros. */
179
180#define DICT_VECTOR(d) (d)->vector
5ffa0793 181#define DICT_LANGUAGE(d) (d)->language
de4f826b
DC
182
183/* These can be used for DICT_HASHED_EXPANDABLE, too. */
184
185#define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
186#define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
187#define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
188
189#define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
190
191/* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
192
193#define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
194#define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
195#define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
196
197#define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
198 (d)->data.linear_expandable.capacity
199
200/* The initial size of a DICT_*_EXPANDABLE dictionary. */
201
202#define DICT_EXPANDABLE_INITIAL_CAPACITY 10
203
204/* This calculates the number of buckets we'll use in a hashtable,
205 given the number of symbols that it will contain. */
206
207#define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
208
209/* Accessor macros for dict_iterators; they're here rather than
210 dictionary.h because code elsewhere should treat dict_iterators as
211 opaque. */
212
213/* The dictionary that the iterator is associated to. */
214#define DICT_ITERATOR_DICT(iter) (iter)->dict
215/* For linear dictionaries, the index of the last symbol returned; for
216 hashed dictionaries, the bucket of the last symbol returned. */
217#define DICT_ITERATOR_INDEX(iter) (iter)->index
218/* For hashed dictionaries, this points to the last symbol returned;
219 otherwise, this is unused. */
220#define DICT_ITERATOR_CURRENT(iter) (iter)->current
221
222/* Declarations of functions for vectors. */
223
224/* Functions that might work across a range of dictionary types. */
225
226static void add_symbol_nonexpandable (struct dictionary *dict,
227 struct symbol *sym);
228
229static void free_obstack (struct dictionary *dict);
230
231/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
232 dictionaries. */
233
234static struct symbol *iterator_first_hashed (const struct dictionary *dict,
235 struct dict_iterator *iterator);
236
237static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
238
c4d840bd 239static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
b5ec771e 240 const lookup_name_info &name,
de4f826b
DC
241 struct dict_iterator *iterator);
242
b5ec771e 243static struct symbol *iter_match_next_hashed (const lookup_name_info &name,
c4d840bd
PH
244 struct dict_iterator *iterator);
245
de4f826b
DC
246/* Functions only for DICT_HASHED. */
247
248static int size_hashed (const struct dictionary *dict);
249
250/* Functions only for DICT_HASHED_EXPANDABLE. */
251
252static void free_hashed_expandable (struct dictionary *dict);
253
254static void add_symbol_hashed_expandable (struct dictionary *dict,
255 struct symbol *sym);
256
257static int size_hashed_expandable (const struct dictionary *dict);
258
259/* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
260 dictionaries. */
261
262static struct symbol *iterator_first_linear (const struct dictionary *dict,
263 struct dict_iterator *iterator);
264
265static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
266
c4d840bd 267static struct symbol *iter_match_first_linear (const struct dictionary *dict,
b5ec771e 268 const lookup_name_info &name,
c4d840bd 269 struct dict_iterator *iterator);
de4f826b 270
b5ec771e 271static struct symbol *iter_match_next_linear (const lookup_name_info &name,
c4d840bd 272 struct dict_iterator *iterator);
de4f826b
DC
273
274static int size_linear (const struct dictionary *dict);
275
276/* Functions only for DICT_LINEAR_EXPANDABLE. */
277
278static void free_linear_expandable (struct dictionary *dict);
279
280static void add_symbol_linear_expandable (struct dictionary *dict,
281 struct symbol *sym);
282
283/* Various vectors that we'll actually use. */
284
285static const struct dict_vector dict_hashed_vector =
286 {
287 DICT_HASHED, /* type */
288 free_obstack, /* free */
289 add_symbol_nonexpandable, /* add_symbol */
a11a1416 290 iterator_first_hashed, /* iterator_first */
de4f826b 291 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
292 iter_match_first_hashed, /* iter_name_first */
293 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
294 size_hashed, /* size */
295 };
296
297static const struct dict_vector dict_hashed_expandable_vector =
298 {
299 DICT_HASHED_EXPANDABLE, /* type */
300 free_hashed_expandable, /* free */
301 add_symbol_hashed_expandable, /* add_symbol */
a11a1416 302 iterator_first_hashed, /* iterator_first */
de4f826b 303 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
304 iter_match_first_hashed, /* iter_name_first */
305 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
306 size_hashed_expandable, /* size */
307 };
308
309static const struct dict_vector dict_linear_vector =
310 {
311 DICT_LINEAR, /* type */
312 free_obstack, /* free */
313 add_symbol_nonexpandable, /* add_symbol */
a11a1416 314 iterator_first_linear, /* iterator_first */
de4f826b 315 iterator_next_linear, /* iterator_next */
c4d840bd
PH
316 iter_match_first_linear, /* iter_name_first */
317 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
318 size_linear, /* size */
319 };
320
321static const struct dict_vector dict_linear_expandable_vector =
322 {
323 DICT_LINEAR_EXPANDABLE, /* type */
324 free_linear_expandable, /* free */
325 add_symbol_linear_expandable, /* add_symbol */
a11a1416 326 iterator_first_linear, /* iterator_first */
de4f826b 327 iterator_next_linear, /* iterator_next */
c4d840bd
PH
328 iter_match_first_linear, /* iter_name_first */
329 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
330 size_linear, /* size */
331 };
332
333/* Declarations of helper functions (i.e. ones that don't go into
334 vectors). */
335
336static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
337
338static void insert_symbol_hashed (struct dictionary *dict,
339 struct symbol *sym);
340
341static void expand_hashtable (struct dictionary *dict);
342
343/* The creation functions. */
344
5ffa0793 345/* See dictionary.h. */
de4f826b
DC
346
347struct dictionary *
348dict_create_hashed (struct obstack *obstack,
5ffa0793 349 enum language language,
de4f826b
DC
350 const struct pending *symbol_list)
351{
352 struct dictionary *retval;
353 int nsyms = 0, nbuckets, i;
354 struct symbol **buckets;
355 const struct pending *list_counter;
356
8d749320 357 retval = XOBNEW (obstack, struct dictionary);
de4f826b 358 DICT_VECTOR (retval) = &dict_hashed_vector;
5ffa0793 359 DICT_LANGUAGE (retval) = language_def (language);
de4f826b
DC
360
361 /* Calculate the number of symbols, and allocate space for them. */
362 for (list_counter = symbol_list;
363 list_counter != NULL;
364 list_counter = list_counter->next)
365 {
366 nsyms += list_counter->nsyms;
367 }
368 nbuckets = DICT_HASHTABLE_SIZE (nsyms);
369 DICT_HASHED_NBUCKETS (retval) = nbuckets;
8d749320 370 buckets = XOBNEWVEC (obstack, struct symbol *, nbuckets);
de4f826b
DC
371 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
372 DICT_HASHED_BUCKETS (retval) = buckets;
373
374 /* Now fill the buckets. */
375 for (list_counter = symbol_list;
376 list_counter != NULL;
377 list_counter = list_counter->next)
378 {
379 for (i = list_counter->nsyms - 1; i >= 0; --i)
380 {
381 insert_symbol_hashed (retval, list_counter->symbol[i]);
382 }
383 }
384
385 return retval;
386}
387
5ffa0793 388/* See dictionary.h. */
de4f826b
DC
389
390extern struct dictionary *
5ffa0793 391dict_create_hashed_expandable (enum language language)
de4f826b 392{
8d749320 393 struct dictionary *retval = XNEW (struct dictionary);
de4f826b 394
de4f826b 395 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
5ffa0793 396 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 397 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
224c3ddb
SM
398 DICT_HASHED_BUCKETS (retval) = XCNEWVEC (struct symbol *,
399 DICT_EXPANDABLE_INITIAL_CAPACITY);
de4f826b
DC
400 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
401
402 return retval;
403}
404
5ffa0793 405/* See dictionary.h. */
de4f826b
DC
406
407struct dictionary *
408dict_create_linear (struct obstack *obstack,
5ffa0793 409 enum language language,
de4f826b
DC
410 const struct pending *symbol_list)
411{
412 struct dictionary *retval;
413 int nsyms = 0, i, j;
414 struct symbol **syms;
415 const struct pending *list_counter;
416
8d749320 417 retval = XOBNEW (obstack, struct dictionary);
de4f826b 418 DICT_VECTOR (retval) = &dict_linear_vector;
5ffa0793 419 DICT_LANGUAGE (retval) = language_def (language);
de4f826b
DC
420
421 /* Calculate the number of symbols, and allocate space for them. */
422 for (list_counter = symbol_list;
423 list_counter != NULL;
424 list_counter = list_counter->next)
425 {
426 nsyms += list_counter->nsyms;
427 }
428 DICT_LINEAR_NSYMS (retval) = nsyms;
8d749320 429 syms = XOBNEWVEC (obstack, struct symbol *, nsyms );
de4f826b
DC
430 DICT_LINEAR_SYMS (retval) = syms;
431
432 /* Now fill in the symbols. Start filling in from the back, so as
433 to preserve the original order of the symbols. */
434 for (list_counter = symbol_list, j = nsyms - 1;
435 list_counter != NULL;
436 list_counter = list_counter->next)
437 {
438 for (i = list_counter->nsyms - 1;
439 i >= 0;
440 --i, --j)
441 {
442 syms[j] = list_counter->symbol[i];
443 }
444 }
445
446 return retval;
447}
448
5ffa0793 449/* See dictionary.h. */
de4f826b
DC
450
451struct dictionary *
5ffa0793 452dict_create_linear_expandable (enum language language)
de4f826b 453{
8d749320 454 struct dictionary *retval = XNEW (struct dictionary);
de4f826b 455
de4f826b 456 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
5ffa0793 457 DICT_LANGUAGE (retval) = language_def (language);
de4f826b 458 DICT_LINEAR_NSYMS (retval) = 0;
224c3ddb 459 DICT_LINEAR_EXPANDABLE_CAPACITY (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
de4f826b 460 DICT_LINEAR_SYMS (retval)
224c3ddb 461 = XNEWVEC (struct symbol *, DICT_LINEAR_EXPANDABLE_CAPACITY (retval));
de4f826b
DC
462
463 return retval;
464}
465
466/* The functions providing the dictionary interface. */
467
468/* Free the memory used by a dictionary that's not on an obstack. (If
469 any.) */
470
471void
472dict_free (struct dictionary *dict)
473{
474 (DICT_VECTOR (dict))->free (dict);
475}
476
477/* Add SYM to DICT. DICT had better be expandable. */
478
479void
480dict_add_symbol (struct dictionary *dict, struct symbol *sym)
481{
482 (DICT_VECTOR (dict))->add_symbol (dict, sym);
483}
484
0bfa869d
DE
485/* Utility to add a list of symbols to a dictionary.
486 DICT must be an expandable dictionary. */
487
488void
489dict_add_pending (struct dictionary *dict, const struct pending *symbol_list)
490{
491 const struct pending *list;
492 int i;
493
494 for (list = symbol_list; list != NULL; list = list->next)
495 {
496 for (i = 0; i < list->nsyms; ++i)
497 dict_add_symbol (dict, list->symbol[i]);
498 }
499}
500
de4f826b
DC
501/* Initialize ITERATOR to point at the first symbol in DICT, and
502 return that first symbol, or NULL if DICT is empty. */
503
504struct symbol *
505dict_iterator_first (const struct dictionary *dict,
506 struct dict_iterator *iterator)
507{
508 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
509}
510
511/* Advance ITERATOR, and return the next symbol, or NULL if there are
512 no more symbols. */
513
514struct symbol *
515dict_iterator_next (struct dict_iterator *iterator)
516{
517 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
518 ->iterator_next (iterator);
519}
520
c4d840bd
PH
521struct symbol *
522dict_iter_match_first (const struct dictionary *dict,
b5ec771e 523 const lookup_name_info &name,
c4d840bd
PH
524 struct dict_iterator *iterator)
525{
b5ec771e 526 return (DICT_VECTOR (dict))->iter_match_first (dict, name, iterator);
c4d840bd
PH
527}
528
529struct symbol *
b5ec771e 530dict_iter_match_next (const lookup_name_info &name,
c4d840bd 531 struct dict_iterator *iterator)
de4f826b
DC
532{
533 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
b5ec771e 534 ->iter_match_next (name, iterator);
de4f826b
DC
535}
536
537int
538dict_size (const struct dictionary *dict)
539{
540 return (DICT_VECTOR (dict))->size (dict);
541}
542
543/* Now come functions (well, one function, currently) that are
544 implemented generically by means of the vtable. Typically, they're
545 rarely used. */
546
547/* Test to see if DICT is empty. */
548
549int
550dict_empty (struct dictionary *dict)
551{
552 struct dict_iterator iter;
553
554 return (dict_iterator_first (dict, &iter) == NULL);
555}
556
557
558/* The functions implementing the dictionary interface. */
559
560/* Generic functions, where appropriate. */
561
562static void
563free_obstack (struct dictionary *dict)
564{
565 /* Do nothing! */
566}
567
568static void
569add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
570{
571 internal_error (__FILE__, __LINE__,
e2e0b3e5 572 _("dict_add_symbol: non-expandable dictionary"));
de4f826b
DC
573}
574
575/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
576
577static struct symbol *
578iterator_first_hashed (const struct dictionary *dict,
579 struct dict_iterator *iterator)
580{
581 DICT_ITERATOR_DICT (iterator) = dict;
582 DICT_ITERATOR_INDEX (iterator) = -1;
583 return iterator_hashed_advance (iterator);
584}
585
586static struct symbol *
587iterator_next_hashed (struct dict_iterator *iterator)
588{
de4f826b
DC
589 struct symbol *next;
590
591 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
592
593 if (next == NULL)
594 return iterator_hashed_advance (iterator);
595 else
596 {
597 DICT_ITERATOR_CURRENT (iterator) = next;
598 return next;
599 }
600}
601
602static struct symbol *
603iterator_hashed_advance (struct dict_iterator *iterator)
604{
605 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
606 int nbuckets = DICT_HASHED_NBUCKETS (dict);
607 int i;
608
609 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
610 {
611 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
612
613 if (sym != NULL)
614 {
615 DICT_ITERATOR_INDEX (iterator) = i;
616 DICT_ITERATOR_CURRENT (iterator) = sym;
617 return sym;
618 }
619 }
620
621 return NULL;
622}
623
624static struct symbol *
b5ec771e
PA
625iter_match_first_hashed (const struct dictionary *dict,
626 const lookup_name_info &name,
c4d840bd 627 struct dict_iterator *iterator)
de4f826b 628{
b5ec771e
PA
629 const language_defn *lang = DICT_LANGUAGE (dict);
630 unsigned int hash_index = (name.search_name_hash (lang->la_language)
631 % DICT_HASHED_NBUCKETS (dict));
632 symbol_name_matcher_ftype *matches_name
633 = language_get_symbol_name_matcher (lang, name);
de4f826b
DC
634 struct symbol *sym;
635
636 DICT_ITERATOR_DICT (iterator) = dict;
637
638 /* Loop through the symbols in the given bucket, breaking when SYM
639 first matches. If SYM never matches, it will be set to NULL;
640 either way, we have the right return value. */
641
642 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
643 sym != NULL;
644 sym = sym->hash_next)
645 {
c4d840bd 646 /* Warning: the order of arguments to compare matters! */
b5ec771e
PA
647 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
648 break;
de4f826b
DC
649 }
650
651 DICT_ITERATOR_CURRENT (iterator) = sym;
652 return sym;
653}
654
655static struct symbol *
b5ec771e 656iter_match_next_hashed (const lookup_name_info &name,
c4d840bd 657 struct dict_iterator *iterator)
de4f826b 658{
b5ec771e
PA
659 const language_defn *lang = DICT_LANGUAGE (DICT_ITERATOR_DICT (iterator));
660 symbol_name_matcher_ftype *matches_name
661 = language_get_symbol_name_matcher (lang, name);
de4f826b
DC
662 struct symbol *next;
663
664 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
665 next != NULL;
666 next = next->hash_next)
667 {
b5ec771e 668 if (matches_name (SYMBOL_SEARCH_NAME (next), name, NULL))
de4f826b
DC
669 break;
670 }
671
672 DICT_ITERATOR_CURRENT (iterator) = next;
673
674 return next;
675}
676
677/* Insert SYM into DICT. */
678
679static void
680insert_symbol_hashed (struct dictionary *dict,
681 struct symbol *sym)
682{
683 unsigned int hash_index;
5ffa0793 684 unsigned int hash;
de4f826b
DC
685 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
686
5ffa0793
PA
687 /* We don't want to insert a symbol into a dictionary of a different
688 language. The two may not use the same hashing algorithm. */
689 gdb_assert (SYMBOL_LANGUAGE (sym) == DICT_LANGUAGE (dict)->la_language);
690
691 hash = search_name_hash (SYMBOL_LANGUAGE (sym), SYMBOL_SEARCH_NAME (sym));
692 hash_index = hash % DICT_HASHED_NBUCKETS (dict);
de4f826b
DC
693 sym->hash_next = buckets[hash_index];
694 buckets[hash_index] = sym;
695}
696
697static int
698size_hashed (const struct dictionary *dict)
699{
700 return DICT_HASHED_NBUCKETS (dict);
701}
702
703/* Functions only for DICT_HASHED_EXPANDABLE. */
704
705static void
706free_hashed_expandable (struct dictionary *dict)
707{
708 xfree (DICT_HASHED_BUCKETS (dict));
709 xfree (dict);
710}
711
712static void
713add_symbol_hashed_expandable (struct dictionary *dict,
714 struct symbol *sym)
715{
716 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
717
718 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
719 expand_hashtable (dict);
720
721 insert_symbol_hashed (dict, sym);
722 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
723}
724
725static int
726size_hashed_expandable (const struct dictionary *dict)
727{
728 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
729}
730
731static void
732expand_hashtable (struct dictionary *dict)
733{
734 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
735 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
224c3ddb
SM
736 int new_nbuckets = 2 * old_nbuckets + 1;
737 struct symbol **new_buckets = XCNEWVEC (struct symbol *, new_nbuckets);
de4f826b
DC
738 int i;
739
740 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
741 DICT_HASHED_BUCKETS (dict) = new_buckets;
742
6595d32b
MS
743 for (i = 0; i < old_nbuckets; ++i)
744 {
745 struct symbol *sym, *next_sym;
746
747 sym = old_buckets[i];
748 if (sym != NULL)
749 {
750 for (next_sym = sym->hash_next;
751 next_sym != NULL;
752 next_sym = sym->hash_next)
753 {
754 insert_symbol_hashed (dict, sym);
755 sym = next_sym;
756 }
757
758 insert_symbol_hashed (dict, sym);
759 }
de4f826b 760 }
de4f826b
DC
761
762 xfree (old_buckets);
763}
764
5ffa0793 765/* See dictionary.h. */
c4d840bd 766
5ffa0793
PA
767unsigned int
768default_search_name_hash (const char *string0)
c4d840bd
PH
769{
770 /* The Ada-encoded version of a name P1.P2...Pn has either the form
771 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
772 are lower-cased identifiers). The <suffix> (which can be empty)
773 encodes additional information about the denoted entity. This
774 routine hashes such names to msymbol_hash_iw(Pn). It actually
775 does this for a superset of both valid Pi and of <suffix>, but
776 in other cases it simply returns msymbol_hash_iw(STRING0). */
777
1d2a4540 778 const char *string;
c4d840bd 779 unsigned int hash;
c4d840bd 780
1d2a4540
PH
781 string = string0;
782 if (*string == '_')
783 {
61012eef 784 if (startswith (string, "_ada_"))
1d2a4540
PH
785 string += 5;
786 else
787 return msymbol_hash_iw (string0);
788 }
c4d840bd
PH
789
790 hash = 0;
791 while (*string)
792 {
793 switch (*string)
794 {
795 case '$':
796 case '.':
797 case 'X':
1d2a4540
PH
798 if (string0 == string)
799 return msymbol_hash_iw (string0);
800 else
801 return hash;
c4d840bd 802 case ' ':
1d2a4540
PH
803 case '(':
804 return msymbol_hash_iw (string0);
c4d840bd 805 case '_':
1d2a4540 806 if (string[1] == '_' && string != string0)
c4d840bd 807 {
558b1900
JB
808 int c = string[2];
809
810 if ((c < 'a' || c > 'z') && c != 'O')
c4d840bd
PH
811 return hash;
812 hash = 0;
813 string += 2;
7e8835c5 814 continue;
c4d840bd 815 }
7e8835c5
TT
816 break;
817 case 'T':
818 /* Ignore "TKB" suffixes.
819
820 These are used by Ada for subprograms implementing a task body.
821 For instance for a task T inside package Pck, the name of the
822 subprogram implementing T's body is `pck__tTKB'. We need to
823 ignore the "TKB" suffix because searches for this task body
824 subprogram are going to be performed using `pck__t' (the encoded
825 version of the natural name `pck.t'). */
826 if (strcmp (string, "TKB") == 0)
827 return hash;
c4d840bd
PH
828 break;
829 }
7e8835c5
TT
830
831 hash = SYMBOL_HASH_NEXT (hash, *string);
832 string += 1;
c4d840bd
PH
833 }
834 return hash;
835}
836
de4f826b
DC
837/* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
838
839static struct symbol *
840iterator_first_linear (const struct dictionary *dict,
841 struct dict_iterator *iterator)
842{
843 DICT_ITERATOR_DICT (iterator) = dict;
844 DICT_ITERATOR_INDEX (iterator) = 0;
845 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
846}
847
848static struct symbol *
849iterator_next_linear (struct dict_iterator *iterator)
850{
851 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
852
853 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
854 return NULL;
855 else
856 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
857}
858
859static struct symbol *
c4d840bd 860iter_match_first_linear (const struct dictionary *dict,
b5ec771e 861 const lookup_name_info &name,
c4d840bd 862 struct dict_iterator *iterator)
de4f826b
DC
863{
864 DICT_ITERATOR_DICT (iterator) = dict;
865 DICT_ITERATOR_INDEX (iterator) = -1;
866
b5ec771e 867 return iter_match_next_linear (name, iterator);
de4f826b
DC
868}
869
870static struct symbol *
b5ec771e 871iter_match_next_linear (const lookup_name_info &name,
c4d840bd 872 struct dict_iterator *iterator)
de4f826b
DC
873{
874 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
b5ec771e
PA
875 const language_defn *lang = DICT_LANGUAGE (dict);
876 symbol_name_matcher_ftype *matches_name
877 = language_get_symbol_name_matcher (lang, name);
878
de4f826b
DC
879 int i, nsyms = DICT_LINEAR_NSYMS (dict);
880 struct symbol *sym, *retval = NULL;
881
882 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
883 {
884 sym = DICT_LINEAR_SYM (dict, i);
b5ec771e
PA
885
886 if (matches_name (SYMBOL_SEARCH_NAME (sym), name, NULL))
de4f826b
DC
887 {
888 retval = sym;
889 break;
890 }
891 }
892
893 DICT_ITERATOR_INDEX (iterator) = i;
894
895 return retval;
896}
897
898static int
899size_linear (const struct dictionary *dict)
900{
901 return DICT_LINEAR_NSYMS (dict);
902}
903
904/* Functions only for DICT_LINEAR_EXPANDABLE. */
905
906static void
907free_linear_expandable (struct dictionary *dict)
908{
909 xfree (DICT_LINEAR_SYMS (dict));
910 xfree (dict);
911}
912
913
914static void
915add_symbol_linear_expandable (struct dictionary *dict,
916 struct symbol *sym)
917{
918 int nsyms = ++DICT_LINEAR_NSYMS (dict);
919
920 /* Do we have enough room? If not, grow it. */
6595d32b
MS
921 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
922 {
923 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
924 DICT_LINEAR_SYMS (dict)
224c3ddb
SM
925 = XRESIZEVEC (struct symbol *, DICT_LINEAR_SYMS (dict),
926 DICT_LINEAR_EXPANDABLE_CAPACITY (dict));
6595d32b 927 }
de4f826b
DC
928
929 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
930}
This page took 1.268092 seconds and 4 git commands to generate.