15796c8dcSSimon Schubert /* Routines for name->symbol lookups in GDB.
25796c8dcSSimon Schubert
3*ef5ccd6cSJohn Marino Copyright (C) 2003-2013 Free Software Foundation, Inc.
45796c8dcSSimon Schubert
55796c8dcSSimon Schubert Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
65796c8dcSSimon Schubert Inc.
75796c8dcSSimon Schubert
85796c8dcSSimon Schubert This file is part of GDB.
95796c8dcSSimon Schubert
105796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify
115796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by
125796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or
135796c8dcSSimon Schubert (at your option) any later version.
145796c8dcSSimon Schubert
155796c8dcSSimon Schubert This program is distributed in the hope that it will be useful,
165796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of
175796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
185796c8dcSSimon Schubert GNU General Public License for more details.
195796c8dcSSimon Schubert
205796c8dcSSimon Schubert You should have received a copy of the GNU General Public License
215796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */
225796c8dcSSimon Schubert
235796c8dcSSimon Schubert #include "defs.h"
24c50c785cSJohn Marino #include <ctype.h>
255796c8dcSSimon Schubert #include "gdb_obstack.h"
265796c8dcSSimon Schubert #include "symtab.h"
275796c8dcSSimon Schubert #include "buildsym.h"
285796c8dcSSimon Schubert #include "gdb_assert.h"
295796c8dcSSimon Schubert #include "dictionary.h"
305796c8dcSSimon Schubert
315796c8dcSSimon Schubert /* This file implements dictionaries, which are tables that associate
325796c8dcSSimon Schubert symbols to names. They are represented by an opaque type 'struct
335796c8dcSSimon Schubert dictionary'. That type has various internal implementations, which
345796c8dcSSimon Schubert you can choose between depending on what properties you need
355796c8dcSSimon Schubert (e.g. fast lookup, order-preserving, expandable).
365796c8dcSSimon Schubert
375796c8dcSSimon Schubert Each dictionary starts with a 'virtual function table' that
385796c8dcSSimon Schubert contains the functions that actually implement the various
395796c8dcSSimon Schubert operations that dictionaries provide. (Note, however, that, for
405796c8dcSSimon Schubert the sake of client code, we also provide some functions that can be
415796c8dcSSimon Schubert implemented generically in terms of the functions in the vtable.)
425796c8dcSSimon Schubert
435796c8dcSSimon Schubert To add a new dictionary implementation <impl>, what you should do
445796c8dcSSimon Schubert is:
455796c8dcSSimon Schubert
465796c8dcSSimon Schubert * Add a new element DICT_<IMPL> to dict_type.
475796c8dcSSimon Schubert
485796c8dcSSimon Schubert * Create a new structure dictionary_<impl>. If your new
495796c8dcSSimon Schubert implementation is a variant of an existing one, make sure that
505796c8dcSSimon Schubert their structs have the same initial data members. Define accessor
515796c8dcSSimon Schubert macros for your new data members.
525796c8dcSSimon Schubert
535796c8dcSSimon Schubert * Implement all the functions in dict_vector as static functions,
545796c8dcSSimon Schubert whose name is the same as the corresponding member of dict_vector
555796c8dcSSimon Schubert plus _<impl>. You don't have to do this for those members where
565796c8dcSSimon Schubert you can reuse existing generic functions
575796c8dcSSimon Schubert (e.g. add_symbol_nonexpandable, free_obstack) or in the case where
585796c8dcSSimon Schubert your new implementation is a variant of an existing implementation
595796c8dcSSimon Schubert and where the variant doesn't affect the member function in
605796c8dcSSimon Schubert question.
615796c8dcSSimon Schubert
625796c8dcSSimon Schubert * Define a static const struct dict_vector dict_<impl>_vector.
635796c8dcSSimon Schubert
645796c8dcSSimon Schubert * Define a function dict_create_<impl> to create these
655796c8dcSSimon Schubert gizmos. Add its declaration to dictionary.h.
665796c8dcSSimon Schubert
675796c8dcSSimon Schubert To add a new operation <op> on all existing implementations, what
685796c8dcSSimon Schubert you should do is:
695796c8dcSSimon Schubert
705796c8dcSSimon Schubert * Add a new member <op> to struct dict_vector.
715796c8dcSSimon Schubert
725796c8dcSSimon Schubert * If there is useful generic behavior <op>, define a static
735796c8dcSSimon Schubert function <op>_something_informative that implements that behavior.
745796c8dcSSimon Schubert (E.g. add_symbol_nonexpandable, free_obstack.)
755796c8dcSSimon Schubert
765796c8dcSSimon Schubert * For every implementation <impl> that should have its own specific
775796c8dcSSimon Schubert behavior for <op>, define a static function <op>_<impl>
785796c8dcSSimon Schubert implementing it.
795796c8dcSSimon Schubert
805796c8dcSSimon Schubert * Modify all existing dict_vector_<impl>'s to include the appropriate
815796c8dcSSimon Schubert member.
825796c8dcSSimon Schubert
835796c8dcSSimon Schubert * Define a function dict_<op> that looks up <op> in the dict_vector
845796c8dcSSimon Schubert and calls the appropriate function. Add a declaration for
85c50c785cSJohn Marino dict_<op> to dictionary.h. */
865796c8dcSSimon Schubert
875796c8dcSSimon Schubert /* An enum representing the various implementations of dictionaries.
885796c8dcSSimon Schubert Used only for debugging. */
895796c8dcSSimon Schubert
905796c8dcSSimon Schubert enum dict_type
915796c8dcSSimon Schubert {
925796c8dcSSimon Schubert /* Symbols are stored in a fixed-size hash table. */
935796c8dcSSimon Schubert DICT_HASHED,
945796c8dcSSimon Schubert /* Symbols are stored in an expandable hash table. */
955796c8dcSSimon Schubert DICT_HASHED_EXPANDABLE,
965796c8dcSSimon Schubert /* Symbols are stored in a fixed-size array. */
975796c8dcSSimon Schubert DICT_LINEAR,
985796c8dcSSimon Schubert /* Symbols are stored in an expandable array. */
995796c8dcSSimon Schubert DICT_LINEAR_EXPANDABLE
1005796c8dcSSimon Schubert };
1015796c8dcSSimon Schubert
1025796c8dcSSimon Schubert /* The virtual function table. */
1035796c8dcSSimon Schubert
1045796c8dcSSimon Schubert struct dict_vector
1055796c8dcSSimon Schubert {
1065796c8dcSSimon Schubert /* The type of the dictionary. This is only here to make debugging
1075796c8dcSSimon Schubert a bit easier; it's not actually used. */
1085796c8dcSSimon Schubert enum dict_type type;
1095796c8dcSSimon Schubert /* The function to free a dictionary. */
1105796c8dcSSimon Schubert void (*free) (struct dictionary *dict);
1115796c8dcSSimon Schubert /* Add a symbol to a dictionary, if possible. */
1125796c8dcSSimon Schubert void (*add_symbol) (struct dictionary *dict, struct symbol *sym);
1135796c8dcSSimon Schubert /* Iterator functions. */
1145796c8dcSSimon Schubert struct symbol *(*iterator_first) (const struct dictionary *dict,
1155796c8dcSSimon Schubert struct dict_iterator *iterator);
1165796c8dcSSimon Schubert struct symbol *(*iterator_next) (struct dict_iterator *iterator);
1175796c8dcSSimon Schubert /* Functions to iterate over symbols with a given name. */
118c50c785cSJohn Marino struct symbol *(*iter_match_first) (const struct dictionary *dict,
1195796c8dcSSimon Schubert const char *name,
120c50c785cSJohn Marino symbol_compare_ftype *equiv,
1215796c8dcSSimon Schubert struct dict_iterator *iterator);
122c50c785cSJohn Marino struct symbol *(*iter_match_next) (const char *name,
123c50c785cSJohn Marino symbol_compare_ftype *equiv,
1245796c8dcSSimon Schubert struct dict_iterator *iterator);
1255796c8dcSSimon Schubert /* A size function, for maint print symtabs. */
1265796c8dcSSimon Schubert int (*size) (const struct dictionary *dict);
1275796c8dcSSimon Schubert };
1285796c8dcSSimon Schubert
1295796c8dcSSimon Schubert /* Now comes the structs used to store the data for different
1305796c8dcSSimon Schubert implementations. If two implementations have data in common, put
1315796c8dcSSimon Schubert the common data at the top of their structs, ordered in the same
1325796c8dcSSimon Schubert way. */
1335796c8dcSSimon Schubert
1345796c8dcSSimon Schubert struct dictionary_hashed
1355796c8dcSSimon Schubert {
1365796c8dcSSimon Schubert int nbuckets;
1375796c8dcSSimon Schubert struct symbol **buckets;
1385796c8dcSSimon Schubert };
1395796c8dcSSimon Schubert
1405796c8dcSSimon Schubert struct dictionary_hashed_expandable
1415796c8dcSSimon Schubert {
1425796c8dcSSimon Schubert /* How many buckets we currently have. */
1435796c8dcSSimon Schubert int nbuckets;
1445796c8dcSSimon Schubert struct symbol **buckets;
1455796c8dcSSimon Schubert /* How many syms we currently have; we need this so we will know
1465796c8dcSSimon Schubert when to add more buckets. */
1475796c8dcSSimon Schubert int nsyms;
1485796c8dcSSimon Schubert };
1495796c8dcSSimon Schubert
1505796c8dcSSimon Schubert struct dictionary_linear
1515796c8dcSSimon Schubert {
1525796c8dcSSimon Schubert int nsyms;
1535796c8dcSSimon Schubert struct symbol **syms;
1545796c8dcSSimon Schubert };
1555796c8dcSSimon Schubert
1565796c8dcSSimon Schubert struct dictionary_linear_expandable
1575796c8dcSSimon Schubert {
1585796c8dcSSimon Schubert /* How many symbols we currently have. */
1595796c8dcSSimon Schubert int nsyms;
1605796c8dcSSimon Schubert struct symbol **syms;
1615796c8dcSSimon Schubert /* How many symbols we can store before needing to reallocate. */
1625796c8dcSSimon Schubert int capacity;
1635796c8dcSSimon Schubert };
1645796c8dcSSimon Schubert
1655796c8dcSSimon Schubert /* And now, the star of our show. */
1665796c8dcSSimon Schubert
1675796c8dcSSimon Schubert struct dictionary
1685796c8dcSSimon Schubert {
1695796c8dcSSimon Schubert const struct dict_vector *vector;
1705796c8dcSSimon Schubert union
1715796c8dcSSimon Schubert {
1725796c8dcSSimon Schubert struct dictionary_hashed hashed;
1735796c8dcSSimon Schubert struct dictionary_hashed_expandable hashed_expandable;
1745796c8dcSSimon Schubert struct dictionary_linear linear;
1755796c8dcSSimon Schubert struct dictionary_linear_expandable linear_expandable;
1765796c8dcSSimon Schubert }
1775796c8dcSSimon Schubert data;
1785796c8dcSSimon Schubert };
1795796c8dcSSimon Schubert
1805796c8dcSSimon Schubert /* Accessor macros. */
1815796c8dcSSimon Schubert
1825796c8dcSSimon Schubert #define DICT_VECTOR(d) (d)->vector
1835796c8dcSSimon Schubert
1845796c8dcSSimon Schubert /* These can be used for DICT_HASHED_EXPANDABLE, too. */
1855796c8dcSSimon Schubert
1865796c8dcSSimon Schubert #define DICT_HASHED_NBUCKETS(d) (d)->data.hashed.nbuckets
1875796c8dcSSimon Schubert #define DICT_HASHED_BUCKETS(d) (d)->data.hashed.buckets
1885796c8dcSSimon Schubert #define DICT_HASHED_BUCKET(d,i) DICT_HASHED_BUCKETS (d) [i]
1895796c8dcSSimon Schubert
1905796c8dcSSimon Schubert #define DICT_HASHED_EXPANDABLE_NSYMS(d) (d)->data.hashed_expandable.nsyms
1915796c8dcSSimon Schubert
1925796c8dcSSimon Schubert /* These can be used for DICT_LINEAR_EXPANDABLEs, too. */
1935796c8dcSSimon Schubert
1945796c8dcSSimon Schubert #define DICT_LINEAR_NSYMS(d) (d)->data.linear.nsyms
1955796c8dcSSimon Schubert #define DICT_LINEAR_SYMS(d) (d)->data.linear.syms
1965796c8dcSSimon Schubert #define DICT_LINEAR_SYM(d,i) DICT_LINEAR_SYMS (d) [i]
1975796c8dcSSimon Schubert
1985796c8dcSSimon Schubert #define DICT_LINEAR_EXPANDABLE_CAPACITY(d) \
1995796c8dcSSimon Schubert (d)->data.linear_expandable.capacity
2005796c8dcSSimon Schubert
2015796c8dcSSimon Schubert /* The initial size of a DICT_*_EXPANDABLE dictionary. */
2025796c8dcSSimon Schubert
2035796c8dcSSimon Schubert #define DICT_EXPANDABLE_INITIAL_CAPACITY 10
2045796c8dcSSimon Schubert
2055796c8dcSSimon Schubert /* This calculates the number of buckets we'll use in a hashtable,
2065796c8dcSSimon Schubert given the number of symbols that it will contain. */
2075796c8dcSSimon Schubert
2085796c8dcSSimon Schubert #define DICT_HASHTABLE_SIZE(n) ((n)/5 + 1)
2095796c8dcSSimon Schubert
2105796c8dcSSimon Schubert /* Accessor macros for dict_iterators; they're here rather than
2115796c8dcSSimon Schubert dictionary.h because code elsewhere should treat dict_iterators as
2125796c8dcSSimon Schubert opaque. */
2135796c8dcSSimon Schubert
2145796c8dcSSimon Schubert /* The dictionary that the iterator is associated to. */
2155796c8dcSSimon Schubert #define DICT_ITERATOR_DICT(iter) (iter)->dict
2165796c8dcSSimon Schubert /* For linear dictionaries, the index of the last symbol returned; for
2175796c8dcSSimon Schubert hashed dictionaries, the bucket of the last symbol returned. */
2185796c8dcSSimon Schubert #define DICT_ITERATOR_INDEX(iter) (iter)->index
2195796c8dcSSimon Schubert /* For hashed dictionaries, this points to the last symbol returned;
2205796c8dcSSimon Schubert otherwise, this is unused. */
2215796c8dcSSimon Schubert #define DICT_ITERATOR_CURRENT(iter) (iter)->current
2225796c8dcSSimon Schubert
2235796c8dcSSimon Schubert /* Declarations of functions for vectors. */
2245796c8dcSSimon Schubert
2255796c8dcSSimon Schubert /* Functions that might work across a range of dictionary types. */
2265796c8dcSSimon Schubert
2275796c8dcSSimon Schubert static void add_symbol_nonexpandable (struct dictionary *dict,
2285796c8dcSSimon Schubert struct symbol *sym);
2295796c8dcSSimon Schubert
2305796c8dcSSimon Schubert static void free_obstack (struct dictionary *dict);
2315796c8dcSSimon Schubert
2325796c8dcSSimon Schubert /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE
2335796c8dcSSimon Schubert dictionaries. */
2345796c8dcSSimon Schubert
2355796c8dcSSimon Schubert static struct symbol *iterator_first_hashed (const struct dictionary *dict,
2365796c8dcSSimon Schubert struct dict_iterator *iterator);
2375796c8dcSSimon Schubert
2385796c8dcSSimon Schubert static struct symbol *iterator_next_hashed (struct dict_iterator *iterator);
2395796c8dcSSimon Schubert
240c50c785cSJohn Marino static struct symbol *iter_match_first_hashed (const struct dictionary *dict,
2415796c8dcSSimon Schubert const char *name,
242c50c785cSJohn Marino symbol_compare_ftype *compare,
2435796c8dcSSimon Schubert struct dict_iterator *iterator);
2445796c8dcSSimon Schubert
245c50c785cSJohn Marino static struct symbol *iter_match_next_hashed (const char *name,
246c50c785cSJohn Marino symbol_compare_ftype *compare,
2475796c8dcSSimon Schubert struct dict_iterator *iterator);
2485796c8dcSSimon Schubert
249c50c785cSJohn Marino static unsigned int dict_hash (const char *string);
250c50c785cSJohn Marino
2515796c8dcSSimon Schubert /* Functions only for DICT_HASHED. */
2525796c8dcSSimon Schubert
2535796c8dcSSimon Schubert static int size_hashed (const struct dictionary *dict);
2545796c8dcSSimon Schubert
2555796c8dcSSimon Schubert /* Functions only for DICT_HASHED_EXPANDABLE. */
2565796c8dcSSimon Schubert
2575796c8dcSSimon Schubert static void free_hashed_expandable (struct dictionary *dict);
2585796c8dcSSimon Schubert
2595796c8dcSSimon Schubert static void add_symbol_hashed_expandable (struct dictionary *dict,
2605796c8dcSSimon Schubert struct symbol *sym);
2615796c8dcSSimon Schubert
2625796c8dcSSimon Schubert static int size_hashed_expandable (const struct dictionary *dict);
2635796c8dcSSimon Schubert
2645796c8dcSSimon Schubert /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE
2655796c8dcSSimon Schubert dictionaries. */
2665796c8dcSSimon Schubert
2675796c8dcSSimon Schubert static struct symbol *iterator_first_linear (const struct dictionary *dict,
2685796c8dcSSimon Schubert struct dict_iterator *iterator);
2695796c8dcSSimon Schubert
2705796c8dcSSimon Schubert static struct symbol *iterator_next_linear (struct dict_iterator *iterator);
2715796c8dcSSimon Schubert
272c50c785cSJohn Marino static struct symbol *iter_match_first_linear (const struct dictionary *dict,
2735796c8dcSSimon Schubert const char *name,
274c50c785cSJohn Marino symbol_compare_ftype *compare,
2755796c8dcSSimon Schubert struct dict_iterator *iterator);
2765796c8dcSSimon Schubert
277c50c785cSJohn Marino static struct symbol *iter_match_next_linear (const char *name,
278c50c785cSJohn Marino symbol_compare_ftype *compare,
2795796c8dcSSimon Schubert struct dict_iterator *iterator);
2805796c8dcSSimon Schubert
2815796c8dcSSimon Schubert static int size_linear (const struct dictionary *dict);
2825796c8dcSSimon Schubert
2835796c8dcSSimon Schubert /* Functions only for DICT_LINEAR_EXPANDABLE. */
2845796c8dcSSimon Schubert
2855796c8dcSSimon Schubert static void free_linear_expandable (struct dictionary *dict);
2865796c8dcSSimon Schubert
2875796c8dcSSimon Schubert static void add_symbol_linear_expandable (struct dictionary *dict,
2885796c8dcSSimon Schubert struct symbol *sym);
2895796c8dcSSimon Schubert
2905796c8dcSSimon Schubert /* Various vectors that we'll actually use. */
2915796c8dcSSimon Schubert
2925796c8dcSSimon Schubert static const struct dict_vector dict_hashed_vector =
2935796c8dcSSimon Schubert {
2945796c8dcSSimon Schubert DICT_HASHED, /* type */
2955796c8dcSSimon Schubert free_obstack, /* free */
2965796c8dcSSimon Schubert add_symbol_nonexpandable, /* add_symbol */
2975796c8dcSSimon Schubert iterator_first_hashed, /* iterator_first */
2985796c8dcSSimon Schubert iterator_next_hashed, /* iterator_next */
299c50c785cSJohn Marino iter_match_first_hashed, /* iter_name_first */
300c50c785cSJohn Marino iter_match_next_hashed, /* iter_name_next */
3015796c8dcSSimon Schubert size_hashed, /* size */
3025796c8dcSSimon Schubert };
3035796c8dcSSimon Schubert
3045796c8dcSSimon Schubert static const struct dict_vector dict_hashed_expandable_vector =
3055796c8dcSSimon Schubert {
3065796c8dcSSimon Schubert DICT_HASHED_EXPANDABLE, /* type */
3075796c8dcSSimon Schubert free_hashed_expandable, /* free */
3085796c8dcSSimon Schubert add_symbol_hashed_expandable, /* add_symbol */
3095796c8dcSSimon Schubert iterator_first_hashed, /* iterator_first */
3105796c8dcSSimon Schubert iterator_next_hashed, /* iterator_next */
311c50c785cSJohn Marino iter_match_first_hashed, /* iter_name_first */
312c50c785cSJohn Marino iter_match_next_hashed, /* iter_name_next */
3135796c8dcSSimon Schubert size_hashed_expandable, /* size */
3145796c8dcSSimon Schubert };
3155796c8dcSSimon Schubert
3165796c8dcSSimon Schubert static const struct dict_vector dict_linear_vector =
3175796c8dcSSimon Schubert {
3185796c8dcSSimon Schubert DICT_LINEAR, /* type */
3195796c8dcSSimon Schubert free_obstack, /* free */
3205796c8dcSSimon Schubert add_symbol_nonexpandable, /* add_symbol */
3215796c8dcSSimon Schubert iterator_first_linear, /* iterator_first */
3225796c8dcSSimon Schubert iterator_next_linear, /* iterator_next */
323c50c785cSJohn Marino iter_match_first_linear, /* iter_name_first */
324c50c785cSJohn Marino iter_match_next_linear, /* iter_name_next */
3255796c8dcSSimon Schubert size_linear, /* size */
3265796c8dcSSimon Schubert };
3275796c8dcSSimon Schubert
3285796c8dcSSimon Schubert static const struct dict_vector dict_linear_expandable_vector =
3295796c8dcSSimon Schubert {
3305796c8dcSSimon Schubert DICT_LINEAR_EXPANDABLE, /* type */
3315796c8dcSSimon Schubert free_linear_expandable, /* free */
3325796c8dcSSimon Schubert add_symbol_linear_expandable, /* add_symbol */
3335796c8dcSSimon Schubert iterator_first_linear, /* iterator_first */
3345796c8dcSSimon Schubert iterator_next_linear, /* iterator_next */
335c50c785cSJohn Marino iter_match_first_linear, /* iter_name_first */
336c50c785cSJohn Marino iter_match_next_linear, /* iter_name_next */
3375796c8dcSSimon Schubert size_linear, /* size */
3385796c8dcSSimon Schubert };
3395796c8dcSSimon Schubert
3405796c8dcSSimon Schubert /* Declarations of helper functions (i.e. ones that don't go into
3415796c8dcSSimon Schubert vectors). */
3425796c8dcSSimon Schubert
3435796c8dcSSimon Schubert static struct symbol *iterator_hashed_advance (struct dict_iterator *iter);
3445796c8dcSSimon Schubert
3455796c8dcSSimon Schubert static void insert_symbol_hashed (struct dictionary *dict,
3465796c8dcSSimon Schubert struct symbol *sym);
3475796c8dcSSimon Schubert
3485796c8dcSSimon Schubert static void expand_hashtable (struct dictionary *dict);
3495796c8dcSSimon Schubert
3505796c8dcSSimon Schubert /* The creation functions. */
3515796c8dcSSimon Schubert
3525796c8dcSSimon Schubert /* Create a dictionary implemented via a fixed-size hashtable. All
3535796c8dcSSimon Schubert memory it uses is allocated on OBSTACK; the environment is
3545796c8dcSSimon Schubert initialized from SYMBOL_LIST. */
3555796c8dcSSimon Schubert
3565796c8dcSSimon Schubert struct dictionary *
dict_create_hashed(struct obstack * obstack,const struct pending * symbol_list)3575796c8dcSSimon Schubert dict_create_hashed (struct obstack *obstack,
3585796c8dcSSimon Schubert const struct pending *symbol_list)
3595796c8dcSSimon Schubert {
3605796c8dcSSimon Schubert struct dictionary *retval;
3615796c8dcSSimon Schubert int nsyms = 0, nbuckets, i;
3625796c8dcSSimon Schubert struct symbol **buckets;
3635796c8dcSSimon Schubert const struct pending *list_counter;
3645796c8dcSSimon Schubert
3655796c8dcSSimon Schubert retval = obstack_alloc (obstack, sizeof (struct dictionary));
3665796c8dcSSimon Schubert DICT_VECTOR (retval) = &dict_hashed_vector;
3675796c8dcSSimon Schubert
3685796c8dcSSimon Schubert /* Calculate the number of symbols, and allocate space for them. */
3695796c8dcSSimon Schubert for (list_counter = symbol_list;
3705796c8dcSSimon Schubert list_counter != NULL;
3715796c8dcSSimon Schubert list_counter = list_counter->next)
3725796c8dcSSimon Schubert {
3735796c8dcSSimon Schubert nsyms += list_counter->nsyms;
3745796c8dcSSimon Schubert }
3755796c8dcSSimon Schubert nbuckets = DICT_HASHTABLE_SIZE (nsyms);
3765796c8dcSSimon Schubert DICT_HASHED_NBUCKETS (retval) = nbuckets;
3775796c8dcSSimon Schubert buckets = obstack_alloc (obstack, nbuckets * sizeof (struct symbol *));
3785796c8dcSSimon Schubert memset (buckets, 0, nbuckets * sizeof (struct symbol *));
3795796c8dcSSimon Schubert DICT_HASHED_BUCKETS (retval) = buckets;
3805796c8dcSSimon Schubert
3815796c8dcSSimon Schubert /* Now fill the buckets. */
3825796c8dcSSimon Schubert for (list_counter = symbol_list;
3835796c8dcSSimon Schubert list_counter != NULL;
3845796c8dcSSimon Schubert list_counter = list_counter->next)
3855796c8dcSSimon Schubert {
3865796c8dcSSimon Schubert for (i = list_counter->nsyms - 1; i >= 0; --i)
3875796c8dcSSimon Schubert {
3885796c8dcSSimon Schubert insert_symbol_hashed (retval, list_counter->symbol[i]);
3895796c8dcSSimon Schubert }
3905796c8dcSSimon Schubert }
3915796c8dcSSimon Schubert
3925796c8dcSSimon Schubert return retval;
3935796c8dcSSimon Schubert }
3945796c8dcSSimon Schubert
3955796c8dcSSimon Schubert /* Create a dictionary implemented via a hashtable that grows as
3965796c8dcSSimon Schubert necessary. The dictionary is initially empty; to add symbols to
3975796c8dcSSimon Schubert it, call dict_add_symbol(). Call dict_free() when you're done with
3985796c8dcSSimon Schubert it. */
3995796c8dcSSimon Schubert
4005796c8dcSSimon Schubert extern struct dictionary *
dict_create_hashed_expandable(void)4015796c8dcSSimon Schubert dict_create_hashed_expandable (void)
4025796c8dcSSimon Schubert {
4035796c8dcSSimon Schubert struct dictionary *retval;
4045796c8dcSSimon Schubert
4055796c8dcSSimon Schubert retval = xmalloc (sizeof (struct dictionary));
4065796c8dcSSimon Schubert DICT_VECTOR (retval) = &dict_hashed_expandable_vector;
4075796c8dcSSimon Schubert DICT_HASHED_NBUCKETS (retval) = DICT_EXPANDABLE_INITIAL_CAPACITY;
4085796c8dcSSimon Schubert DICT_HASHED_BUCKETS (retval) = xcalloc (DICT_EXPANDABLE_INITIAL_CAPACITY,
4095796c8dcSSimon Schubert sizeof (struct symbol *));
4105796c8dcSSimon Schubert DICT_HASHED_EXPANDABLE_NSYMS (retval) = 0;
4115796c8dcSSimon Schubert
4125796c8dcSSimon Schubert return retval;
4135796c8dcSSimon Schubert }
4145796c8dcSSimon Schubert
4155796c8dcSSimon Schubert /* Create a dictionary implemented via a fixed-size array. All memory
4165796c8dcSSimon Schubert it uses is allocated on OBSTACK; the environment is initialized
4175796c8dcSSimon Schubert from the SYMBOL_LIST. The symbols are ordered in the same order
4185796c8dcSSimon Schubert that they're found in SYMBOL_LIST. */
4195796c8dcSSimon Schubert
4205796c8dcSSimon Schubert struct dictionary *
dict_create_linear(struct obstack * obstack,const struct pending * symbol_list)4215796c8dcSSimon Schubert dict_create_linear (struct obstack *obstack,
4225796c8dcSSimon Schubert const struct pending *symbol_list)
4235796c8dcSSimon Schubert {
4245796c8dcSSimon Schubert struct dictionary *retval;
4255796c8dcSSimon Schubert int nsyms = 0, i, j;
4265796c8dcSSimon Schubert struct symbol **syms;
4275796c8dcSSimon Schubert const struct pending *list_counter;
4285796c8dcSSimon Schubert
4295796c8dcSSimon Schubert retval = obstack_alloc (obstack, sizeof (struct dictionary));
4305796c8dcSSimon Schubert DICT_VECTOR (retval) = &dict_linear_vector;
4315796c8dcSSimon Schubert
4325796c8dcSSimon Schubert /* Calculate the number of symbols, and allocate space for them. */
4335796c8dcSSimon Schubert for (list_counter = symbol_list;
4345796c8dcSSimon Schubert list_counter != NULL;
4355796c8dcSSimon Schubert list_counter = list_counter->next)
4365796c8dcSSimon Schubert {
4375796c8dcSSimon Schubert nsyms += list_counter->nsyms;
4385796c8dcSSimon Schubert }
4395796c8dcSSimon Schubert DICT_LINEAR_NSYMS (retval) = nsyms;
4405796c8dcSSimon Schubert syms = obstack_alloc (obstack, nsyms * sizeof (struct symbol *));
4415796c8dcSSimon Schubert DICT_LINEAR_SYMS (retval) = syms;
4425796c8dcSSimon Schubert
4435796c8dcSSimon Schubert /* Now fill in the symbols. Start filling in from the back, so as
4445796c8dcSSimon Schubert to preserve the original order of the symbols. */
4455796c8dcSSimon Schubert for (list_counter = symbol_list, j = nsyms - 1;
4465796c8dcSSimon Schubert list_counter != NULL;
4475796c8dcSSimon Schubert list_counter = list_counter->next)
4485796c8dcSSimon Schubert {
4495796c8dcSSimon Schubert for (i = list_counter->nsyms - 1;
4505796c8dcSSimon Schubert i >= 0;
4515796c8dcSSimon Schubert --i, --j)
4525796c8dcSSimon Schubert {
4535796c8dcSSimon Schubert syms[j] = list_counter->symbol[i];
4545796c8dcSSimon Schubert }
4555796c8dcSSimon Schubert }
4565796c8dcSSimon Schubert
4575796c8dcSSimon Schubert return retval;
4585796c8dcSSimon Schubert }
4595796c8dcSSimon Schubert
4605796c8dcSSimon Schubert /* Create a dictionary implemented via an array that grows as
4615796c8dcSSimon Schubert necessary. The dictionary is initially empty; to add symbols to
4625796c8dcSSimon Schubert it, call dict_add_symbol(). Call dict_free() when you're done with
4635796c8dcSSimon Schubert it. */
4645796c8dcSSimon Schubert
4655796c8dcSSimon Schubert struct dictionary *
dict_create_linear_expandable(void)4665796c8dcSSimon Schubert dict_create_linear_expandable (void)
4675796c8dcSSimon Schubert {
4685796c8dcSSimon Schubert struct dictionary *retval;
4695796c8dcSSimon Schubert
4705796c8dcSSimon Schubert retval = xmalloc (sizeof (struct dictionary));
4715796c8dcSSimon Schubert DICT_VECTOR (retval) = &dict_linear_expandable_vector;
4725796c8dcSSimon Schubert DICT_LINEAR_NSYMS (retval) = 0;
4735796c8dcSSimon Schubert DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
4745796c8dcSSimon Schubert = DICT_EXPANDABLE_INITIAL_CAPACITY;
4755796c8dcSSimon Schubert DICT_LINEAR_SYMS (retval)
4765796c8dcSSimon Schubert = xmalloc (DICT_LINEAR_EXPANDABLE_CAPACITY (retval)
4775796c8dcSSimon Schubert * sizeof (struct symbol *));
4785796c8dcSSimon Schubert
4795796c8dcSSimon Schubert return retval;
4805796c8dcSSimon Schubert }
4815796c8dcSSimon Schubert
4825796c8dcSSimon Schubert /* The functions providing the dictionary interface. */
4835796c8dcSSimon Schubert
4845796c8dcSSimon Schubert /* Free the memory used by a dictionary that's not on an obstack. (If
4855796c8dcSSimon Schubert any.) */
4865796c8dcSSimon Schubert
4875796c8dcSSimon Schubert void
dict_free(struct dictionary * dict)4885796c8dcSSimon Schubert dict_free (struct dictionary *dict)
4895796c8dcSSimon Schubert {
4905796c8dcSSimon Schubert (DICT_VECTOR (dict))->free (dict);
4915796c8dcSSimon Schubert }
4925796c8dcSSimon Schubert
4935796c8dcSSimon Schubert /* Add SYM to DICT. DICT had better be expandable. */
4945796c8dcSSimon Schubert
4955796c8dcSSimon Schubert void
dict_add_symbol(struct dictionary * dict,struct symbol * sym)4965796c8dcSSimon Schubert dict_add_symbol (struct dictionary *dict, struct symbol *sym)
4975796c8dcSSimon Schubert {
4985796c8dcSSimon Schubert (DICT_VECTOR (dict))->add_symbol (dict, sym);
4995796c8dcSSimon Schubert }
5005796c8dcSSimon Schubert
501*ef5ccd6cSJohn Marino /* Utility to add a list of symbols to a dictionary.
502*ef5ccd6cSJohn Marino DICT must be an expandable dictionary. */
503*ef5ccd6cSJohn Marino
504*ef5ccd6cSJohn Marino void
dict_add_pending(struct dictionary * dict,const struct pending * symbol_list)505*ef5ccd6cSJohn Marino dict_add_pending (struct dictionary *dict, const struct pending *symbol_list)
506*ef5ccd6cSJohn Marino {
507*ef5ccd6cSJohn Marino const struct pending *list;
508*ef5ccd6cSJohn Marino int i;
509*ef5ccd6cSJohn Marino
510*ef5ccd6cSJohn Marino for (list = symbol_list; list != NULL; list = list->next)
511*ef5ccd6cSJohn Marino {
512*ef5ccd6cSJohn Marino for (i = 0; i < list->nsyms; ++i)
513*ef5ccd6cSJohn Marino dict_add_symbol (dict, list->symbol[i]);
514*ef5ccd6cSJohn Marino }
515*ef5ccd6cSJohn Marino }
516*ef5ccd6cSJohn Marino
5175796c8dcSSimon Schubert /* Initialize ITERATOR to point at the first symbol in DICT, and
5185796c8dcSSimon Schubert return that first symbol, or NULL if DICT is empty. */
5195796c8dcSSimon Schubert
5205796c8dcSSimon Schubert struct symbol *
dict_iterator_first(const struct dictionary * dict,struct dict_iterator * iterator)5215796c8dcSSimon Schubert dict_iterator_first (const struct dictionary *dict,
5225796c8dcSSimon Schubert struct dict_iterator *iterator)
5235796c8dcSSimon Schubert {
5245796c8dcSSimon Schubert return (DICT_VECTOR (dict))->iterator_first (dict, iterator);
5255796c8dcSSimon Schubert }
5265796c8dcSSimon Schubert
5275796c8dcSSimon Schubert /* Advance ITERATOR, and return the next symbol, or NULL if there are
5285796c8dcSSimon Schubert no more symbols. */
5295796c8dcSSimon Schubert
5305796c8dcSSimon Schubert struct symbol *
dict_iterator_next(struct dict_iterator * iterator)5315796c8dcSSimon Schubert dict_iterator_next (struct dict_iterator *iterator)
5325796c8dcSSimon Schubert {
5335796c8dcSSimon Schubert return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
5345796c8dcSSimon Schubert ->iterator_next (iterator);
5355796c8dcSSimon Schubert }
5365796c8dcSSimon Schubert
5375796c8dcSSimon Schubert struct symbol *
dict_iter_name_first(const struct dictionary * dict,const char * name,struct dict_iterator * iterator)5385796c8dcSSimon Schubert dict_iter_name_first (const struct dictionary *dict,
5395796c8dcSSimon Schubert const char *name,
5405796c8dcSSimon Schubert struct dict_iterator *iterator)
5415796c8dcSSimon Schubert {
542c50c785cSJohn Marino return dict_iter_match_first (dict, name, strcmp_iw, iterator);
5435796c8dcSSimon Schubert }
5445796c8dcSSimon Schubert
5455796c8dcSSimon Schubert struct symbol *
dict_iter_name_next(const char * name,struct dict_iterator * iterator)5465796c8dcSSimon Schubert dict_iter_name_next (const char *name, struct dict_iterator *iterator)
5475796c8dcSSimon Schubert {
548c50c785cSJohn Marino return dict_iter_match_next (name, strcmp_iw, iterator);
549c50c785cSJohn Marino }
550c50c785cSJohn Marino
551c50c785cSJohn Marino struct symbol *
dict_iter_match_first(const struct dictionary * dict,const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)552c50c785cSJohn Marino dict_iter_match_first (const struct dictionary *dict,
553c50c785cSJohn Marino const char *name, symbol_compare_ftype *compare,
554c50c785cSJohn Marino struct dict_iterator *iterator)
555c50c785cSJohn Marino {
556c50c785cSJohn Marino return (DICT_VECTOR (dict))->iter_match_first (dict, name,
557c50c785cSJohn Marino compare, iterator);
558c50c785cSJohn Marino }
559c50c785cSJohn Marino
560c50c785cSJohn Marino struct symbol *
dict_iter_match_next(const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)561c50c785cSJohn Marino dict_iter_match_next (const char *name, symbol_compare_ftype *compare,
562c50c785cSJohn Marino struct dict_iterator *iterator)
563c50c785cSJohn Marino {
5645796c8dcSSimon Schubert return (DICT_VECTOR (DICT_ITERATOR_DICT (iterator)))
565c50c785cSJohn Marino ->iter_match_next (name, compare, iterator);
5665796c8dcSSimon Schubert }
5675796c8dcSSimon Schubert
5685796c8dcSSimon Schubert int
dict_size(const struct dictionary * dict)5695796c8dcSSimon Schubert dict_size (const struct dictionary *dict)
5705796c8dcSSimon Schubert {
5715796c8dcSSimon Schubert return (DICT_VECTOR (dict))->size (dict);
5725796c8dcSSimon Schubert }
5735796c8dcSSimon Schubert
5745796c8dcSSimon Schubert /* Now come functions (well, one function, currently) that are
5755796c8dcSSimon Schubert implemented generically by means of the vtable. Typically, they're
5765796c8dcSSimon Schubert rarely used. */
5775796c8dcSSimon Schubert
5785796c8dcSSimon Schubert /* Test to see if DICT is empty. */
5795796c8dcSSimon Schubert
5805796c8dcSSimon Schubert int
dict_empty(struct dictionary * dict)5815796c8dcSSimon Schubert dict_empty (struct dictionary *dict)
5825796c8dcSSimon Schubert {
5835796c8dcSSimon Schubert struct dict_iterator iter;
5845796c8dcSSimon Schubert
5855796c8dcSSimon Schubert return (dict_iterator_first (dict, &iter) == NULL);
5865796c8dcSSimon Schubert }
5875796c8dcSSimon Schubert
5885796c8dcSSimon Schubert
5895796c8dcSSimon Schubert /* The functions implementing the dictionary interface. */
5905796c8dcSSimon Schubert
5915796c8dcSSimon Schubert /* Generic functions, where appropriate. */
5925796c8dcSSimon Schubert
5935796c8dcSSimon Schubert static void
free_obstack(struct dictionary * dict)5945796c8dcSSimon Schubert free_obstack (struct dictionary *dict)
5955796c8dcSSimon Schubert {
5965796c8dcSSimon Schubert /* Do nothing! */
5975796c8dcSSimon Schubert }
5985796c8dcSSimon Schubert
5995796c8dcSSimon Schubert static void
add_symbol_nonexpandable(struct dictionary * dict,struct symbol * sym)6005796c8dcSSimon Schubert add_symbol_nonexpandable (struct dictionary *dict, struct symbol *sym)
6015796c8dcSSimon Schubert {
6025796c8dcSSimon Schubert internal_error (__FILE__, __LINE__,
6035796c8dcSSimon Schubert _("dict_add_symbol: non-expandable dictionary"));
6045796c8dcSSimon Schubert }
6055796c8dcSSimon Schubert
6065796c8dcSSimon Schubert /* Functions for DICT_HASHED and DICT_HASHED_EXPANDABLE. */
6075796c8dcSSimon Schubert
6085796c8dcSSimon Schubert static struct symbol *
iterator_first_hashed(const struct dictionary * dict,struct dict_iterator * iterator)6095796c8dcSSimon Schubert iterator_first_hashed (const struct dictionary *dict,
6105796c8dcSSimon Schubert struct dict_iterator *iterator)
6115796c8dcSSimon Schubert {
6125796c8dcSSimon Schubert DICT_ITERATOR_DICT (iterator) = dict;
6135796c8dcSSimon Schubert DICT_ITERATOR_INDEX (iterator) = -1;
6145796c8dcSSimon Schubert return iterator_hashed_advance (iterator);
6155796c8dcSSimon Schubert }
6165796c8dcSSimon Schubert
6175796c8dcSSimon Schubert static struct symbol *
iterator_next_hashed(struct dict_iterator * iterator)6185796c8dcSSimon Schubert iterator_next_hashed (struct dict_iterator *iterator)
6195796c8dcSSimon Schubert {
6205796c8dcSSimon Schubert struct symbol *next;
6215796c8dcSSimon Schubert
6225796c8dcSSimon Schubert next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
6235796c8dcSSimon Schubert
6245796c8dcSSimon Schubert if (next == NULL)
6255796c8dcSSimon Schubert return iterator_hashed_advance (iterator);
6265796c8dcSSimon Schubert else
6275796c8dcSSimon Schubert {
6285796c8dcSSimon Schubert DICT_ITERATOR_CURRENT (iterator) = next;
6295796c8dcSSimon Schubert return next;
6305796c8dcSSimon Schubert }
6315796c8dcSSimon Schubert }
6325796c8dcSSimon Schubert
6335796c8dcSSimon Schubert static struct symbol *
iterator_hashed_advance(struct dict_iterator * iterator)6345796c8dcSSimon Schubert iterator_hashed_advance (struct dict_iterator *iterator)
6355796c8dcSSimon Schubert {
6365796c8dcSSimon Schubert const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
6375796c8dcSSimon Schubert int nbuckets = DICT_HASHED_NBUCKETS (dict);
6385796c8dcSSimon Schubert int i;
6395796c8dcSSimon Schubert
6405796c8dcSSimon Schubert for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nbuckets; ++i)
6415796c8dcSSimon Schubert {
6425796c8dcSSimon Schubert struct symbol *sym = DICT_HASHED_BUCKET (dict, i);
6435796c8dcSSimon Schubert
6445796c8dcSSimon Schubert if (sym != NULL)
6455796c8dcSSimon Schubert {
6465796c8dcSSimon Schubert DICT_ITERATOR_INDEX (iterator) = i;
6475796c8dcSSimon Schubert DICT_ITERATOR_CURRENT (iterator) = sym;
6485796c8dcSSimon Schubert return sym;
6495796c8dcSSimon Schubert }
6505796c8dcSSimon Schubert }
6515796c8dcSSimon Schubert
6525796c8dcSSimon Schubert return NULL;
6535796c8dcSSimon Schubert }
6545796c8dcSSimon Schubert
6555796c8dcSSimon Schubert static struct symbol *
iter_match_first_hashed(const struct dictionary * dict,const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)656c50c785cSJohn Marino iter_match_first_hashed (const struct dictionary *dict, const char *name,
657c50c785cSJohn Marino symbol_compare_ftype *compare,
6585796c8dcSSimon Schubert struct dict_iterator *iterator)
6595796c8dcSSimon Schubert {
660c50c785cSJohn Marino unsigned int hash_index = dict_hash (name) % DICT_HASHED_NBUCKETS (dict);
6615796c8dcSSimon Schubert struct symbol *sym;
6625796c8dcSSimon Schubert
6635796c8dcSSimon Schubert DICT_ITERATOR_DICT (iterator) = dict;
6645796c8dcSSimon Schubert
6655796c8dcSSimon Schubert /* Loop through the symbols in the given bucket, breaking when SYM
6665796c8dcSSimon Schubert first matches. If SYM never matches, it will be set to NULL;
6675796c8dcSSimon Schubert either way, we have the right return value. */
6685796c8dcSSimon Schubert
6695796c8dcSSimon Schubert for (sym = DICT_HASHED_BUCKET (dict, hash_index);
6705796c8dcSSimon Schubert sym != NULL;
6715796c8dcSSimon Schubert sym = sym->hash_next)
6725796c8dcSSimon Schubert {
673c50c785cSJohn Marino /* Warning: the order of arguments to compare matters! */
674c50c785cSJohn Marino if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
6755796c8dcSSimon Schubert {
6765796c8dcSSimon Schubert break;
6775796c8dcSSimon Schubert }
6785796c8dcSSimon Schubert
6795796c8dcSSimon Schubert }
6805796c8dcSSimon Schubert
6815796c8dcSSimon Schubert DICT_ITERATOR_CURRENT (iterator) = sym;
6825796c8dcSSimon Schubert return sym;
6835796c8dcSSimon Schubert }
6845796c8dcSSimon Schubert
6855796c8dcSSimon Schubert static struct symbol *
iter_match_next_hashed(const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)686c50c785cSJohn Marino iter_match_next_hashed (const char *name, symbol_compare_ftype *compare,
687c50c785cSJohn Marino struct dict_iterator *iterator)
6885796c8dcSSimon Schubert {
6895796c8dcSSimon Schubert struct symbol *next;
6905796c8dcSSimon Schubert
6915796c8dcSSimon Schubert for (next = DICT_ITERATOR_CURRENT (iterator)->hash_next;
6925796c8dcSSimon Schubert next != NULL;
6935796c8dcSSimon Schubert next = next->hash_next)
6945796c8dcSSimon Schubert {
695c50c785cSJohn Marino if (compare (SYMBOL_SEARCH_NAME (next), name) == 0)
6965796c8dcSSimon Schubert break;
6975796c8dcSSimon Schubert }
6985796c8dcSSimon Schubert
6995796c8dcSSimon Schubert DICT_ITERATOR_CURRENT (iterator) = next;
7005796c8dcSSimon Schubert
7015796c8dcSSimon Schubert return next;
7025796c8dcSSimon Schubert }
7035796c8dcSSimon Schubert
7045796c8dcSSimon Schubert /* Insert SYM into DICT. */
7055796c8dcSSimon Schubert
7065796c8dcSSimon Schubert static void
insert_symbol_hashed(struct dictionary * dict,struct symbol * sym)7075796c8dcSSimon Schubert insert_symbol_hashed (struct dictionary *dict,
7085796c8dcSSimon Schubert struct symbol *sym)
7095796c8dcSSimon Schubert {
7105796c8dcSSimon Schubert unsigned int hash_index;
7115796c8dcSSimon Schubert struct symbol **buckets = DICT_HASHED_BUCKETS (dict);
7125796c8dcSSimon Schubert
713c50c785cSJohn Marino hash_index =
714c50c785cSJohn Marino dict_hash (SYMBOL_SEARCH_NAME (sym)) % DICT_HASHED_NBUCKETS (dict);
7155796c8dcSSimon Schubert sym->hash_next = buckets[hash_index];
7165796c8dcSSimon Schubert buckets[hash_index] = sym;
7175796c8dcSSimon Schubert }
7185796c8dcSSimon Schubert
7195796c8dcSSimon Schubert static int
size_hashed(const struct dictionary * dict)7205796c8dcSSimon Schubert size_hashed (const struct dictionary *dict)
7215796c8dcSSimon Schubert {
7225796c8dcSSimon Schubert return DICT_HASHED_NBUCKETS (dict);
7235796c8dcSSimon Schubert }
7245796c8dcSSimon Schubert
7255796c8dcSSimon Schubert /* Functions only for DICT_HASHED_EXPANDABLE. */
7265796c8dcSSimon Schubert
7275796c8dcSSimon Schubert static void
free_hashed_expandable(struct dictionary * dict)7285796c8dcSSimon Schubert free_hashed_expandable (struct dictionary *dict)
7295796c8dcSSimon Schubert {
7305796c8dcSSimon Schubert xfree (DICT_HASHED_BUCKETS (dict));
7315796c8dcSSimon Schubert xfree (dict);
7325796c8dcSSimon Schubert }
7335796c8dcSSimon Schubert
7345796c8dcSSimon Schubert static void
add_symbol_hashed_expandable(struct dictionary * dict,struct symbol * sym)7355796c8dcSSimon Schubert add_symbol_hashed_expandable (struct dictionary *dict,
7365796c8dcSSimon Schubert struct symbol *sym)
7375796c8dcSSimon Schubert {
7385796c8dcSSimon Schubert int nsyms = ++DICT_HASHED_EXPANDABLE_NSYMS (dict);
7395796c8dcSSimon Schubert
7405796c8dcSSimon Schubert if (DICT_HASHTABLE_SIZE (nsyms) > DICT_HASHED_NBUCKETS (dict))
7415796c8dcSSimon Schubert expand_hashtable (dict);
7425796c8dcSSimon Schubert
7435796c8dcSSimon Schubert insert_symbol_hashed (dict, sym);
7445796c8dcSSimon Schubert DICT_HASHED_EXPANDABLE_NSYMS (dict) = nsyms;
7455796c8dcSSimon Schubert }
7465796c8dcSSimon Schubert
7475796c8dcSSimon Schubert static int
size_hashed_expandable(const struct dictionary * dict)7485796c8dcSSimon Schubert size_hashed_expandable (const struct dictionary *dict)
7495796c8dcSSimon Schubert {
7505796c8dcSSimon Schubert return DICT_HASHED_EXPANDABLE_NSYMS (dict);
7515796c8dcSSimon Schubert }
7525796c8dcSSimon Schubert
7535796c8dcSSimon Schubert static void
expand_hashtable(struct dictionary * dict)7545796c8dcSSimon Schubert expand_hashtable (struct dictionary *dict)
7555796c8dcSSimon Schubert {
7565796c8dcSSimon Schubert int old_nbuckets = DICT_HASHED_NBUCKETS (dict);
7575796c8dcSSimon Schubert struct symbol **old_buckets = DICT_HASHED_BUCKETS (dict);
7585796c8dcSSimon Schubert int new_nbuckets = 2*old_nbuckets + 1;
7595796c8dcSSimon Schubert struct symbol **new_buckets = xcalloc (new_nbuckets,
7605796c8dcSSimon Schubert sizeof (struct symbol *));
7615796c8dcSSimon Schubert int i;
7625796c8dcSSimon Schubert
7635796c8dcSSimon Schubert DICT_HASHED_NBUCKETS (dict) = new_nbuckets;
7645796c8dcSSimon Schubert DICT_HASHED_BUCKETS (dict) = new_buckets;
7655796c8dcSSimon Schubert
766cf7f2e2dSJohn Marino for (i = 0; i < old_nbuckets; ++i)
767cf7f2e2dSJohn Marino {
7685796c8dcSSimon Schubert struct symbol *sym, *next_sym;
7695796c8dcSSimon Schubert
7705796c8dcSSimon Schubert sym = old_buckets[i];
771cf7f2e2dSJohn Marino if (sym != NULL)
772cf7f2e2dSJohn Marino {
7735796c8dcSSimon Schubert for (next_sym = sym->hash_next;
7745796c8dcSSimon Schubert next_sym != NULL;
775cf7f2e2dSJohn Marino next_sym = sym->hash_next)
776cf7f2e2dSJohn Marino {
7775796c8dcSSimon Schubert insert_symbol_hashed (dict, sym);
7785796c8dcSSimon Schubert sym = next_sym;
7795796c8dcSSimon Schubert }
7805796c8dcSSimon Schubert
7815796c8dcSSimon Schubert insert_symbol_hashed (dict, sym);
7825796c8dcSSimon Schubert }
7835796c8dcSSimon Schubert }
7845796c8dcSSimon Schubert
7855796c8dcSSimon Schubert xfree (old_buckets);
7865796c8dcSSimon Schubert }
7875796c8dcSSimon Schubert
788c50c785cSJohn Marino /* Produce an unsigned hash value from STRING0 that is consistent
789c50c785cSJohn Marino with strcmp_iw, strcmp, and, at least on Ada symbols, wild_match.
790c50c785cSJohn Marino That is, two identifiers equivalent according to any of those three
791c50c785cSJohn Marino comparison operators hash to the same value. */
792c50c785cSJohn Marino
793c50c785cSJohn Marino static unsigned int
dict_hash(const char * string0)794c50c785cSJohn Marino dict_hash (const char *string0)
795c50c785cSJohn Marino {
796c50c785cSJohn Marino /* The Ada-encoded version of a name P1.P2...Pn has either the form
797c50c785cSJohn Marino P1__P2__...Pn<suffix> or _ada_P1__P2__...Pn<suffix> (where the Pi
798c50c785cSJohn Marino are lower-cased identifiers). The <suffix> (which can be empty)
799c50c785cSJohn Marino encodes additional information about the denoted entity. This
800c50c785cSJohn Marino routine hashes such names to msymbol_hash_iw(Pn). It actually
801c50c785cSJohn Marino does this for a superset of both valid Pi and of <suffix>, but
802c50c785cSJohn Marino in other cases it simply returns msymbol_hash_iw(STRING0). */
803c50c785cSJohn Marino
804c50c785cSJohn Marino const char *string;
805c50c785cSJohn Marino unsigned int hash;
806c50c785cSJohn Marino
807c50c785cSJohn Marino string = string0;
808c50c785cSJohn Marino if (*string == '_')
809c50c785cSJohn Marino {
810c50c785cSJohn Marino if (strncmp (string, "_ada_", 5) == 0)
811c50c785cSJohn Marino string += 5;
812c50c785cSJohn Marino else
813c50c785cSJohn Marino return msymbol_hash_iw (string0);
814c50c785cSJohn Marino }
815c50c785cSJohn Marino
816c50c785cSJohn Marino hash = 0;
817c50c785cSJohn Marino while (*string)
818c50c785cSJohn Marino {
819*ef5ccd6cSJohn Marino /* Ignore "TKB" suffixes.
820*ef5ccd6cSJohn Marino
821*ef5ccd6cSJohn Marino These are used by Ada for subprograms implementing a task body.
822*ef5ccd6cSJohn Marino For instance for a task T inside package Pck, the name of the
823*ef5ccd6cSJohn Marino subprogram implementing T's body is `pck__tTKB'. We need to
824*ef5ccd6cSJohn Marino ignore the "TKB" suffix because searches for this task body
825*ef5ccd6cSJohn Marino subprogram are going to be performed using `pck__t' (the encoded
826*ef5ccd6cSJohn Marino version of the natural name `pck.t'). */
827*ef5ccd6cSJohn Marino if (strcmp (string, "TKB") == 0)
828*ef5ccd6cSJohn Marino return hash;
829*ef5ccd6cSJohn Marino
830c50c785cSJohn Marino switch (*string)
831c50c785cSJohn Marino {
832c50c785cSJohn Marino case '$':
833c50c785cSJohn Marino case '.':
834c50c785cSJohn Marino case 'X':
835c50c785cSJohn Marino if (string0 == string)
836c50c785cSJohn Marino return msymbol_hash_iw (string0);
837c50c785cSJohn Marino else
838c50c785cSJohn Marino return hash;
839c50c785cSJohn Marino case ' ':
840c50c785cSJohn Marino case '(':
841c50c785cSJohn Marino return msymbol_hash_iw (string0);
842c50c785cSJohn Marino case '_':
843c50c785cSJohn Marino if (string[1] == '_' && string != string0)
844c50c785cSJohn Marino {
845c50c785cSJohn Marino int c = string[2];
846c50c785cSJohn Marino
847c50c785cSJohn Marino if ((c < 'a' || c > 'z') && c != 'O')
848c50c785cSJohn Marino return hash;
849c50c785cSJohn Marino hash = 0;
850c50c785cSJohn Marino string += 2;
851c50c785cSJohn Marino break;
852c50c785cSJohn Marino }
853c50c785cSJohn Marino /* FALL THROUGH */
854c50c785cSJohn Marino default:
855a45ae5f8SJohn Marino hash = SYMBOL_HASH_NEXT (hash, *string);
856c50c785cSJohn Marino string += 1;
857c50c785cSJohn Marino break;
858c50c785cSJohn Marino }
859c50c785cSJohn Marino }
860c50c785cSJohn Marino return hash;
861c50c785cSJohn Marino }
862c50c785cSJohn Marino
8635796c8dcSSimon Schubert /* Functions for DICT_LINEAR and DICT_LINEAR_EXPANDABLE. */
8645796c8dcSSimon Schubert
8655796c8dcSSimon Schubert static struct symbol *
iterator_first_linear(const struct dictionary * dict,struct dict_iterator * iterator)8665796c8dcSSimon Schubert iterator_first_linear (const struct dictionary *dict,
8675796c8dcSSimon Schubert struct dict_iterator *iterator)
8685796c8dcSSimon Schubert {
8695796c8dcSSimon Schubert DICT_ITERATOR_DICT (iterator) = dict;
8705796c8dcSSimon Schubert DICT_ITERATOR_INDEX (iterator) = 0;
8715796c8dcSSimon Schubert return DICT_LINEAR_NSYMS (dict) ? DICT_LINEAR_SYM (dict, 0) : NULL;
8725796c8dcSSimon Schubert }
8735796c8dcSSimon Schubert
8745796c8dcSSimon Schubert static struct symbol *
iterator_next_linear(struct dict_iterator * iterator)8755796c8dcSSimon Schubert iterator_next_linear (struct dict_iterator *iterator)
8765796c8dcSSimon Schubert {
8775796c8dcSSimon Schubert const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
8785796c8dcSSimon Schubert
8795796c8dcSSimon Schubert if (++DICT_ITERATOR_INDEX (iterator) >= DICT_LINEAR_NSYMS (dict))
8805796c8dcSSimon Schubert return NULL;
8815796c8dcSSimon Schubert else
8825796c8dcSSimon Schubert return DICT_LINEAR_SYM (dict, DICT_ITERATOR_INDEX (iterator));
8835796c8dcSSimon Schubert }
8845796c8dcSSimon Schubert
8855796c8dcSSimon Schubert static struct symbol *
iter_match_first_linear(const struct dictionary * dict,const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)886c50c785cSJohn Marino iter_match_first_linear (const struct dictionary *dict,
887c50c785cSJohn Marino const char *name, symbol_compare_ftype *compare,
8885796c8dcSSimon Schubert struct dict_iterator *iterator)
8895796c8dcSSimon Schubert {
8905796c8dcSSimon Schubert DICT_ITERATOR_DICT (iterator) = dict;
8915796c8dcSSimon Schubert DICT_ITERATOR_INDEX (iterator) = -1;
8925796c8dcSSimon Schubert
893c50c785cSJohn Marino return iter_match_next_linear (name, compare, iterator);
8945796c8dcSSimon Schubert }
8955796c8dcSSimon Schubert
8965796c8dcSSimon Schubert static struct symbol *
iter_match_next_linear(const char * name,symbol_compare_ftype * compare,struct dict_iterator * iterator)897c50c785cSJohn Marino iter_match_next_linear (const char *name, symbol_compare_ftype *compare,
898c50c785cSJohn Marino struct dict_iterator *iterator)
8995796c8dcSSimon Schubert {
9005796c8dcSSimon Schubert const struct dictionary *dict = DICT_ITERATOR_DICT (iterator);
9015796c8dcSSimon Schubert int i, nsyms = DICT_LINEAR_NSYMS (dict);
9025796c8dcSSimon Schubert struct symbol *sym, *retval = NULL;
9035796c8dcSSimon Schubert
9045796c8dcSSimon Schubert for (i = DICT_ITERATOR_INDEX (iterator) + 1; i < nsyms; ++i)
9055796c8dcSSimon Schubert {
9065796c8dcSSimon Schubert sym = DICT_LINEAR_SYM (dict, i);
907c50c785cSJohn Marino if (compare (SYMBOL_SEARCH_NAME (sym), name) == 0)
9085796c8dcSSimon Schubert {
9095796c8dcSSimon Schubert retval = sym;
9105796c8dcSSimon Schubert break;
9115796c8dcSSimon Schubert }
9125796c8dcSSimon Schubert }
9135796c8dcSSimon Schubert
9145796c8dcSSimon Schubert DICT_ITERATOR_INDEX (iterator) = i;
9155796c8dcSSimon Schubert
9165796c8dcSSimon Schubert return retval;
9175796c8dcSSimon Schubert }
9185796c8dcSSimon Schubert
9195796c8dcSSimon Schubert static int
size_linear(const struct dictionary * dict)9205796c8dcSSimon Schubert size_linear (const struct dictionary *dict)
9215796c8dcSSimon Schubert {
9225796c8dcSSimon Schubert return DICT_LINEAR_NSYMS (dict);
9235796c8dcSSimon Schubert }
9245796c8dcSSimon Schubert
9255796c8dcSSimon Schubert /* Functions only for DICT_LINEAR_EXPANDABLE. */
9265796c8dcSSimon Schubert
9275796c8dcSSimon Schubert static void
free_linear_expandable(struct dictionary * dict)9285796c8dcSSimon Schubert free_linear_expandable (struct dictionary *dict)
9295796c8dcSSimon Schubert {
9305796c8dcSSimon Schubert xfree (DICT_LINEAR_SYMS (dict));
9315796c8dcSSimon Schubert xfree (dict);
9325796c8dcSSimon Schubert }
9335796c8dcSSimon Schubert
9345796c8dcSSimon Schubert
9355796c8dcSSimon Schubert static void
add_symbol_linear_expandable(struct dictionary * dict,struct symbol * sym)9365796c8dcSSimon Schubert add_symbol_linear_expandable (struct dictionary *dict,
9375796c8dcSSimon Schubert struct symbol *sym)
9385796c8dcSSimon Schubert {
9395796c8dcSSimon Schubert int nsyms = ++DICT_LINEAR_NSYMS (dict);
9405796c8dcSSimon Schubert
9415796c8dcSSimon Schubert /* Do we have enough room? If not, grow it. */
942cf7f2e2dSJohn Marino if (nsyms > DICT_LINEAR_EXPANDABLE_CAPACITY (dict))
943cf7f2e2dSJohn Marino {
9445796c8dcSSimon Schubert DICT_LINEAR_EXPANDABLE_CAPACITY (dict) *= 2;
9455796c8dcSSimon Schubert DICT_LINEAR_SYMS (dict)
9465796c8dcSSimon Schubert = xrealloc (DICT_LINEAR_SYMS (dict),
9475796c8dcSSimon Schubert DICT_LINEAR_EXPANDABLE_CAPACITY (dict)
9485796c8dcSSimon Schubert * sizeof (struct symbol *));
9495796c8dcSSimon Schubert }
9505796c8dcSSimon Schubert
9515796c8dcSSimon Schubert DICT_LINEAR_SYM (dict, nsyms - 1) = sym;
9525796c8dcSSimon Schubert }
953