* reloc.c: Add BFD_RELOC_RX_OP_NEG.
[deliverable/binutils-gdb.git] / gdb / dictionary.c
CommitLineData
de4f826b
DC
1/* Routines for name->symbol lookups in GDB.
2
7b6bb8da
JB
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
de4f826b
DC
5
6 Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
7 Inc.
8
9 This file is part of GDB.
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
a9762ec7
JB
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
de4f826b 15
a9762ec7
JB
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
de4f826b
DC
20
21 You should have received a copy of the GNU General Public License
a9762ec7 22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
de4f826b
DC
23
24#include "defs.h"
c4d840bd 25#include <ctype.h>
de4f826b
DC
26#include "gdb_obstack.h"
27#include "symtab.h"
28#include "buildsym.h"
29#include "gdb_assert.h"
30#include "dictionary.h"
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
86 dict_<op> to dictionary.h.
87
88*/
89
90/* An enum representing the various implementations of dictionaries.
91 Used only for debugging. */
92
93enum dict_type
94 {
95 /* Symbols are stored in a fixed-size hash table. */
96 DICT_HASHED,
97 /* Symbols are stored in an expandable hash table. */
98 DICT_HASHED_EXPANDABLE,
99 /* Symbols are stored in a fixed-size array. */
100 DICT_LINEAR,
101 /* Symbols are stored in an expandable array. */
20605361 102 DICT_LINEAR_EXPANDABLE
de4f826b
DC
103 };
104
105/* The virtual function table. */
106
107struct dict_vector
108{
109 /* The type of the dictionary. This is only here to make debugging
110 a bit easier; it's not actually used. */
111 enum dict_type type;
112 /* The function to free a dictionary. */
113 void (*free) (struct dictionary *dict);
114 /* Add a symbol to a dictionary, if possible. */
115 void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
116 /* Iterator functions. */
117 struct symbol *(*iterator_first) (const struct dictionary *dict,
118 struct dict_iterator *iterator);
119 struct symbol *(*iterator_next) (struct dict_iterator *iterator);
120 /* Functions to iterate over symbols with a given name. */
c4d840bd 121 struct symbol *(*iter_match_first) (const struct dictionary *dict,
2edb89d3
JK
122 const char *name,
123 symbol_compare_ftype *equiv,
124 struct dict_iterator *iterator);
c4d840bd 125 struct symbol *(*iter_match_next) (const char *name,
2edb89d3 126 symbol_compare_ftype *equiv,
de4f826b 127 struct dict_iterator *iterator);
de4f826b
DC
128 /* A size function, for maint print symtabs. */
129 int (*size) (const struct dictionary *dict);
130};
131
132/* Now comes the structs used to store the data for different
133 implementations. If two implementations have data in common, put
134 the common data at the top of their structs, ordered in the same
135 way. */
136
137struct dictionary_hashed
138{
139 int nbuckets;
140 struct symbol **buckets;
141};
142
143struct dictionary_hashed_expandable
144{
145 /* How many buckets we currently have. */
146 int nbuckets;
147 struct symbol **buckets;
148 /* How many syms we currently have; we need this so we will know
149 when to add more buckets. */
150 int nsyms;
151};
152
153struct dictionary_linear
154{
155 int nsyms;
156 struct symbol **syms;
157};
158
159struct dictionary_linear_expandable
160{
161 /* How many symbols we currently have. */
162 int nsyms;
163 struct symbol **syms;
164 /* How many symbols we can store before needing to reallocate. */
165 int capacity;
166};
167
168/* And now, the star of our show. */
169
170struct dictionary
171{
172 const struct dict_vector *vector;
173 union
174 {
175 struct dictionary_hashed hashed;
176 struct dictionary_hashed_expandable hashed_expandable;
177 struct dictionary_linear linear;
178 struct dictionary_linear_expandable linear_expandable;
179 }
180 data;
181};
182
183/* Accessor macros. */
184
185#define DICT_VECTOR(d) (d)->vector
186
187/* These can be used for DICT_HASHED_EXPANDABLE, too. */
188
189#define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
190#define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
191#define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
192
193#define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
194
195/* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
196
197#define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
198#define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
199#define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
200
201#define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
202 (d)->data.linear_expandable.capacity
203
204/* The initial size of a DICT_*_EXPANDABLE dictionary. */
205
206#define DICT_EXPANDABLE_INITIAL_CAPACITY 10
207
208/* This calculates the number of buckets we'll use in a hashtable,
209 given the number of symbols that it will contain. */
210
211#define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
212
213/* Accessor macros for dict_iterators; they're here rather than
214 dictionary.h because code elsewhere should treat dict_iterators as
215 opaque. */
216
217/* The dictionary that the iterator is associated to. */
218#define DICT_ITERATOR_DICT(iter) (iter)->dict
219/* For linear dictionaries, the index of the last symbol returned; for
220 hashed dictionaries, the bucket of the last symbol returned. */
221#define DICT_ITERATOR_INDEX(iter) (iter)->index
222/* For hashed dictionaries, this points to the last symbol returned;
223 otherwise, this is unused. */
224#define DICT_ITERATOR_CURRENT(iter) (iter)->current
225
226/* Declarations of functions for vectors. */
227
228/* Functions that might work across a range of dictionary types. */
229
230static void add_symbol_nonexpandable (struct dictionary *dict,
231 struct symbol *sym);
232
233static void free_obstack (struct dictionary *dict);
234
235/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
236 dictionaries. */
237
238static struct symbol *iterator_first_hashed (const struct dictionary *dict,
239 struct dict_iterator *iterator);
240
241static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
242
c4d840bd
PH
243static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
244 const char *name,
2edb89d3 245 symbol_compare_ftype *compare,
de4f826b
DC
246 struct dict_iterator *iterator);
247
c4d840bd 248static struct symbol *iter_match_next_hashed (const char *name,
2edb89d3 249 symbol_compare_ftype *compare,
c4d840bd
PH
250 struct dict_iterator *iterator);
251
252static unsigned int dict_hash (const char *string);
de4f826b
DC
253
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
PH
275static struct symbol *iter_match_first_linear (const struct dictionary *dict,
276 const char *name,
2edb89d3 277 symbol_compare_ftype *compare,
c4d840bd 278 struct dict_iterator *iterator);
de4f826b 279
c4d840bd 280static struct symbol *iter_match_next_linear (const char *name,
2edb89d3 281 symbol_compare_ftype *compare,
c4d840bd 282 struct dict_iterator *iterator);
de4f826b
DC
283
284static int size_linear (const struct dictionary *dict);
285
286/* Functions only for DICT_LINEAR_EXPANDABLE. */
287
288static void free_linear_expandable (struct dictionary *dict);
289
290static void add_symbol_linear_expandable (struct dictionary *dict,
291 struct symbol *sym);
292
293/* Various vectors that we'll actually use. */
294
295static const struct dict_vector dict_hashed_vector =
296 {
297 DICT_HASHED, /* type */
298 free_obstack, /* free */
299 add_symbol_nonexpandable, /* add_symbol */
a11a1416 300 iterator_first_hashed, /* iterator_first */
de4f826b 301 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
302 iter_match_first_hashed, /* iter_name_first */
303 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
304 size_hashed, /* size */
305 };
306
307static const struct dict_vector dict_hashed_expandable_vector =
308 {
309 DICT_HASHED_EXPANDABLE, /* type */
310 free_hashed_expandable, /* free */
311 add_symbol_hashed_expandable, /* add_symbol */
a11a1416 312 iterator_first_hashed, /* iterator_first */
de4f826b 313 iterator_next_hashed, /* iterator_next */
c4d840bd
PH
314 iter_match_first_hashed, /* iter_name_first */
315 iter_match_next_hashed, /* iter_name_next */
de4f826b
DC
316 size_hashed_expandable, /* size */
317 };
318
319static const struct dict_vector dict_linear_vector =
320 {
321 DICT_LINEAR, /* type */
322 free_obstack, /* free */
323 add_symbol_nonexpandable, /* add_symbol */
a11a1416 324 iterator_first_linear, /* iterator_first */
de4f826b 325 iterator_next_linear, /* iterator_next */
c4d840bd
PH
326 iter_match_first_linear, /* iter_name_first */
327 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
328 size_linear, /* size */
329 };
330
331static const struct dict_vector dict_linear_expandable_vector =
332 {
333 DICT_LINEAR_EXPANDABLE, /* type */
334 free_linear_expandable, /* free */
335 add_symbol_linear_expandable, /* add_symbol */
a11a1416 336 iterator_first_linear, /* iterator_first */
de4f826b 337 iterator_next_linear, /* iterator_next */
c4d840bd
PH
338 iter_match_first_linear, /* iter_name_first */
339 iter_match_next_linear, /* iter_name_next */
de4f826b
DC
340 size_linear, /* size */
341 };
342
343/* Declarations of helper functions (i.e. ones that don't go into
344 vectors). */
345
346static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
347
348static void insert_symbol_hashed (struct dictionary *dict,
349 struct symbol *sym);
350
351static void expand_hashtable (struct dictionary *dict);
352
353/* The creation functions. */
354
355/* Create a dictionary implemented via a fixed-size hashtable. All
356 memory it uses is allocated on OBSTACK; the environment is
357 initialized from SYMBOL_LIST. */
358
359struct dictionary *
360dict_create_hashed (struct obstack *obstack,
361 const struct pending *symbol_list)
362{
363 struct dictionary *retval;
364 int nsyms = 0, nbuckets, i;
365 struct symbol **buckets;
366 const struct pending *list_counter;
367
368 retval = obstack_alloc (obstack, sizeof (struct dictionary));
369 DICT_VECTOR (retval) = &dict_hashed_vector;
370
371 /* Calculate the number of symbols, and allocate space for them. */
372 for (list_counter = symbol_list;
373 list_counter != NULL;
374 list_counter = list_counter->next)
375 {
376 nsyms += list_counter->nsyms;
377 }
378 nbuckets = DICT_HASHTABLE_SIZE (nsyms);
379 DICT_HASHED_NBUCKETS (retval) = nbuckets;
380 buckets = obstack_alloc (obstack, nbuckets * sizeof (struct symbol *));
381 memset (buckets, 0, nbuckets * sizeof (struct symbol *));
382 DICT_HASHED_BUCKETS (retval) = buckets;
383
384 /* Now fill the buckets. */
385 for (list_counter = symbol_list;
386 list_counter != NULL;
387 list_counter = list_counter->next)
388 {
389 for (i = list_counter->nsyms - 1; i >= 0; --i)
390 {
391 insert_symbol_hashed (retval, list_counter->symbol[i]);
392 }
393 }
394
395 return retval;
396}
397
398/* Create a dictionary implemented via a hashtable that grows as
399 necessary. The dictionary is initially empty; to add symbols to
400 it, call dict_add_symbol(). Call dict_free() when you're done with
401 it. */
402
403extern struct dictionary *
404dict_create_hashed_expandable (void)
405{
406 struct dictionary *retval;
407
408 retval = xmalloc (sizeof (struct dictionary));
409 DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
410 DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
411 DICT_HASHED_BUCKETS (retval) = xcalloc (DICT_EXPANDABLE_INITIAL_CAPACITY,
412 sizeof (struct symbol *));
413 DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
414
415 return retval;
416}
417
418/* Create a dictionary implemented via a fixed-size array. All memory
419 it uses is allocated on OBSTACK; the environment is initialized
420 from the SYMBOL_LIST. The symbols are ordered in the same order
421 that they're found in SYMBOL_LIST. */
422
423struct dictionary *
424dict_create_linear (struct obstack *obstack,
425 const struct pending *symbol_list)
426{
427 struct dictionary *retval;
428 int nsyms = 0, i, j;
429 struct symbol **syms;
430 const struct pending *list_counter;
431
432 retval = obstack_alloc (obstack, sizeof (struct dictionary));
433 DICT_VECTOR (retval) = &dict_linear_vector;
434
435 /* Calculate the number of symbols, and allocate space for them. */
436 for (list_counter = symbol_list;
437 list_counter != NULL;
438 list_counter = list_counter->next)
439 {
440 nsyms += list_counter->nsyms;
441 }
442 DICT_LINEAR_NSYMS (retval) = nsyms;
443 syms = obstack_alloc (obstack, nsyms * sizeof (struct symbol *));
444 DICT_LINEAR_SYMS (retval) = syms;
445
446 /* Now fill in the symbols. Start filling in from the back, so as
447 to preserve the original order of the symbols. */
448 for (list_counter = symbol_list, j = nsyms - 1;
449 list_counter != NULL;
450 list_counter = list_counter->next)
451 {
452 for (i = list_counter->nsyms - 1;
453 i >= 0;
454 --i, --j)
455 {
456 syms[j] = list_counter->symbol[i];
457 }
458 }
459
460 return retval;
461}
462
463/* Create a dictionary implemented via an array that grows as
464 necessary. The dictionary is initially empty; to add symbols to
465 it, call dict_add_symbol(). Call dict_free() when you're done with
466 it. */
467
468struct dictionary *
469dict_create_linear_expandable (void)
470{
471 struct dictionary *retval;
472
473 retval = xmalloc (sizeof (struct dictionary));
474 DICT_VECTOR (retval) = &dict_linear_expandable_vector;
475 DICT_LINEAR_NSYMS (retval) = 0;
476 DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
477 = DICT_EXPANDABLE_INITIAL_CAPACITY;
478 DICT_LINEAR_SYMS (retval)
479 = xmalloc (DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
480 * sizeof (struct symbol *));
481
482 return retval;
483}
484
485/* The functions providing the dictionary interface. */
486
487/* Free the memory used by a dictionary that's not on an obstack. (If
488 any.) */
489
490void
491dict_free (struct dictionary *dict)
492{
493 (DICT_VECTOR (dict))->free (dict);
494}
495
496/* Add SYM to DICT. DICT had better be expandable. */
497
498void
499dict_add_symbol (struct dictionary *dict, struct symbol *sym)
500{
501 (DICT_VECTOR (dict))->add_symbol (dict, sym);
502}
503
504/* Initialize ITERATOR to point at the first symbol in DICT, and
505 return that first symbol, or NULL if DICT is empty. */
506
507struct symbol *
508dict_iterator_first (const struct dictionary *dict,
509 struct dict_iterator *iterator)
510{
511 return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
512}
513
514/* Advance ITERATOR, and return the next symbol, or NULL if there are
515 no more symbols. */
516
517struct symbol *
518dict_iterator_next (struct dict_iterator *iterator)
519{
520 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
521 ->iterator_next (iterator);
522}
523
524struct symbol *
525dict_iter_name_first (const struct dictionary *dict,
526 const char *name,
527 struct dict_iterator *iterator)
528{
c4d840bd 529 return dict_iter_match_first (dict, name, strcmp_iw, iterator);
de4f826b
DC
530}
531
532struct symbol *
533dict_iter_name_next (const char *name, struct dict_iterator *iterator)
c4d840bd
PH
534{
535 return dict_iter_match_next (name, strcmp_iw, iterator);
536}
537
538struct symbol *
539dict_iter_match_first (const struct dictionary *dict,
2edb89d3 540 const char *name, symbol_compare_ftype *compare,
c4d840bd
PH
541 struct dict_iterator *iterator)
542{
543 return (DICT_VECTOR (dict))->iter_match_first (dict, name, compare, iterator);
544}
545
546struct symbol *
2edb89d3 547dict_iter_match_next (const char *name, symbol_compare_ftype *compare,
c4d840bd 548 struct dict_iterator *iterator)
de4f826b
DC
549{
550 return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
c4d840bd 551 ->iter_match_next (name, compare, iterator);
de4f826b
DC
552}
553
554int
555dict_size (const struct dictionary *dict)
556{
557 return (DICT_VECTOR (dict))->size (dict);
558}
559
560/* Now come functions (well, one function, currently) that are
561 implemented generically by means of the vtable. Typically, they're
562 rarely used. */
563
564/* Test to see if DICT is empty. */
565
566int
567dict_empty (struct dictionary *dict)
568{
569 struct dict_iterator iter;
570
571 return (dict_iterator_first (dict, &iter) == NULL);
572}
573
574
575/* The functions implementing the dictionary interface. */
576
577/* Generic functions, where appropriate. */
578
579static void
580free_obstack (struct dictionary *dict)
581{
582 /* Do nothing! */
583}
584
585static void
586add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
587{
588 internal_error (__FILE__, __LINE__,
e2e0b3e5 589 _("dict_add_symbol: non-expandable dictionary"));
de4f826b
DC
590}
591
592/* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
593
594static struct symbol *
595iterator_first_hashed (const struct dictionary *dict,
596 struct dict_iterator *iterator)
597{
598 DICT_ITERATOR_DICT (iterator) = dict;
599 DICT_ITERATOR_INDEX (iterator) = -1;
600 return iterator_hashed_advance (iterator);
601}
602
603static struct symbol *
604iterator_next_hashed (struct dict_iterator *iterator)
605{
de4f826b
DC
606 struct symbol *next;
607
608 next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
609
610 if (next == NULL)
611 return iterator_hashed_advance (iterator);
612 else
613 {
614 DICT_ITERATOR_CURRENT (iterator) = next;
615 return next;
616 }
617}
618
619static struct symbol *
620iterator_hashed_advance (struct dict_iterator *iterator)
621{
622 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
623 int nbuckets = DICT_HASHED_NBUCKETS (dict);
624 int i;
625
626 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
627 {
628 struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
629
630 if (sym != NULL)
631 {
632 DICT_ITERATOR_INDEX (iterator) = i;
633 DICT_ITERATOR_CURRENT (iterator) = sym;
634 return sym;
635 }
636 }
637
638 return NULL;
639}
640
641static struct symbol *
2edb89d3
JK
642iter_match_first_hashed (const struct dictionary *dict, const char *name,
643 symbol_compare_ftype *compare,
c4d840bd 644 struct dict_iterator *iterator)
de4f826b 645{
c4d840bd 646 unsigned int hash_index = dict_hash (name) % DICT_HASHED_NBUCKETS (dict);
de4f826b
DC
647 struct symbol *sym;
648
649 DICT_ITERATOR_DICT (iterator) = dict;
650
651 /* Loop through the symbols in the given bucket, breaking when SYM
652 first matches. If SYM never matches, it will be set to NULL;
653 either way, we have the right return value. */
654
655 for (sym = DICT_HASHED_BUCKET (dict, hash_index);
656 sym != NULL;
657 sym = sym->hash_next)
658 {
c4d840bd
PH
659 /* Warning: the order of arguments to compare matters! */
660 if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
de4f826b
DC
661 {
662 break;
663 }
664
665 }
666
667 DICT_ITERATOR_CURRENT (iterator) = sym;
668 return sym;
669}
670
671static struct symbol *
2edb89d3 672iter_match_next_hashed (const char *name, symbol_compare_ftype *compare,
c4d840bd 673 struct dict_iterator *iterator)
de4f826b
DC
674{
675 struct symbol *next;
676
677 for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
678 next != NULL;
679 next = next->hash_next)
680 {
c4d840bd 681 if (compare (SYMBOL_SEARCH_NAME (next), name) == 0)
de4f826b
DC
682 break;
683 }
684
685 DICT_ITERATOR_CURRENT (iterator) = next;
686
687 return next;
688}
689
690/* Insert SYM into DICT. */
691
692static void
693insert_symbol_hashed (struct dictionary *dict,
694 struct symbol *sym)
695{
696 unsigned int hash_index;
697 struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
698
c4d840bd
PH
699 hash_index =
700 dict_hash (SYMBOL_SEARCH_NAME (sym)) % DICT_HASHED_NBUCKETS (dict);
de4f826b
DC
701 sym->hash_next = buckets[hash_index];
702 buckets[hash_index] = sym;
703}
704
705static int
706size_hashed (const struct dictionary *dict)
707{
708 return DICT_HASHED_NBUCKETS (dict);
709}
710
711/* Functions only for DICT_HASHED_EXPANDABLE. */
712
713static void
714free_hashed_expandable (struct dictionary *dict)
715{
716 xfree (DICT_HASHED_BUCKETS (dict));
717 xfree (dict);
718}
719
720static void
721add_symbol_hashed_expandable (struct dictionary *dict,
722 struct symbol *sym)
723{
724 int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
725
726 if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
727 expand_hashtable (dict);
728
729 insert_symbol_hashed (dict, sym);
730 DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
731}
732
733static int
734size_hashed_expandable (const struct dictionary *dict)
735{
736 return DICT_HASHED_EXPANDABLE_NSYMS (dict);
737}
738
739static void
740expand_hashtable (struct dictionary *dict)
741{
742 int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
743 struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
744 int new_nbuckets = 2*old_nbuckets + 1;
745 struct symbol **new_buckets = xcalloc (new_nbuckets,
746 sizeof (struct symbol *));
747 int i;
748
749 DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
750 DICT_HASHED_BUCKETS (dict) = new_buckets;
751
6595d32b
MS
752 for (i = 0; i < old_nbuckets; ++i)
753 {
754 struct symbol *sym, *next_sym;
755
756 sym = old_buckets[i];
757 if (sym != NULL)
758 {
759 for (next_sym = sym->hash_next;
760 next_sym != NULL;
761 next_sym = sym->hash_next)
762 {
763 insert_symbol_hashed (dict, sym);
764 sym = next_sym;
765 }
766
767 insert_symbol_hashed (dict, sym);
768 }
de4f826b 769 }
de4f826b
DC
770
771 xfree (old_buckets);
772}
773
c4d840bd
PH
774/* Produce an unsigned hash value from STRING0 that is consistent
775 with strcmp_iw, strcmp, and, at least on Ada symbols, wild_match.
776 That is, two identifiers equivalent according to any of those three
777 comparison operators hash to the same value. */
778
779static unsigned int
1d2a4540 780dict_hash (const char *string0)
c4d840bd
PH
781{
782 /* The Ada-encoded version of a name P1.P2...Pn has either the form
783 P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
784 are lower-cased identifiers). The <suffix> (which can be empty)
785 encodes additional information about the denoted entity. This
786 routine hashes such names to msymbol_hash_iw(Pn). It actually
787 does this for a superset of both valid Pi and of <suffix>, but
788 in other cases it simply returns msymbol_hash_iw(STRING0). */
789
1d2a4540 790 const char *string;
c4d840bd 791 unsigned int hash;
c4d840bd 792
1d2a4540
PH
793 string = string0;
794 if (*string == '_')
795 {
796 if (strncmp (string, "_ada_", 5) == 0)
797 string += 5;
798 else
799 return msymbol_hash_iw (string0);
800 }
c4d840bd
PH
801
802 hash = 0;
803 while (*string)
804 {
805 switch (*string)
806 {
807 case '$':
808 case '.':
809 case 'X':
1d2a4540
PH
810 if (string0 == string)
811 return msymbol_hash_iw (string0);
812 else
813 return hash;
c4d840bd 814 case ' ':
1d2a4540
PH
815 case '(':
816 return msymbol_hash_iw (string0);
c4d840bd 817 case '_':
1d2a4540 818 if (string[1] == '_' && string != string0)
c4d840bd 819 {
558b1900
JB
820 int c = string[2];
821
822 if ((c < 'a' || c > 'z') && c != 'O')
c4d840bd
PH
823 return hash;
824 hash = 0;
825 string += 2;
826 break;
827 }
828 /* FALL THROUGH */
829 default:
830 hash = hash * 67 + *string - 113;
831 string += 1;
832 break;
833 }
834 }
835 return hash;
836}
837
de4f826b
DC
838/* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
839
840static struct symbol *
841iterator_first_linear (const struct dictionary *dict,
842 struct dict_iterator *iterator)
843{
844 DICT_ITERATOR_DICT (iterator) = dict;
845 DICT_ITERATOR_INDEX (iterator) = 0;
846 return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
847}
848
849static struct symbol *
850iterator_next_linear (struct dict_iterator *iterator)
851{
852 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
853
854 if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
855 return NULL;
856 else
857 return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
858}
859
860static struct symbol *
c4d840bd 861iter_match_first_linear (const struct dictionary *dict,
2edb89d3 862 const char *name, symbol_compare_ftype *compare,
c4d840bd 863 struct dict_iterator *iterator)
de4f826b
DC
864{
865 DICT_ITERATOR_DICT (iterator) = dict;
866 DICT_ITERATOR_INDEX (iterator) = -1;
867
c4d840bd 868 return iter_match_next_linear (name, compare, iterator);
de4f826b
DC
869}
870
871static struct symbol *
2edb89d3 872iter_match_next_linear (const char *name, symbol_compare_ftype *compare,
c4d840bd 873 struct dict_iterator *iterator)
de4f826b
DC
874{
875 const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
876 int i, nsyms = DICT_LINEAR_NSYMS (dict);
877 struct symbol *sym, *retval = NULL;
878
879 for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
880 {
881 sym = DICT_LINEAR_SYM (dict, i);
c4d840bd 882 if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
de4f826b
DC
883 {
884 retval = sym;
885 break;
886 }
887 }
888
889 DICT_ITERATOR_INDEX (iterator) = i;
890
891 return retval;
892}
893
894static int
895size_linear (const struct dictionary *dict)
896{
897 return DICT_LINEAR_NSYMS (dict);
898}
899
900/* Functions only for DICT_LINEAR_EXPANDABLE. */
901
902static void
903free_linear_expandable (struct dictionary *dict)
904{
905 xfree (DICT_LINEAR_SYMS (dict));
906 xfree (dict);
907}
908
909
910static void
911add_symbol_linear_expandable (struct dictionary *dict,
912 struct symbol *sym)
913{
914 int nsyms = ++DICT_LINEAR_NSYMS (dict);
915
916 /* Do we have enough room? If not, grow it. */
6595d32b
MS
917 if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
918 {
919 DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
920 DICT_LINEAR_SYMS (dict)
921 = xrealloc (DICT_LINEAR_SYMS (dict),
922 DICT_LINEAR_EXPANDABLE_CAPACITY (dict)
923 * sizeof (struct symbol *));
924 }
de4f826b
DC
925
926 DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
927}
This page took 0.507605 seconds and 4 git commands to generate.