1130803Smarcel/* Routines for name->symbol lookups in GDB.
2130803Smarcel
3130803Smarcel   Copyright 2003 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   Contributed by David Carlton <carlton@bactrian.org> and by Kealia,
6130803Smarcel   Inc.
7130803Smarcel
8130803Smarcel   This file is part of GDB.
9130803Smarcel
10130803Smarcel   This program is free software; you can redistribute it and/or modify
11130803Smarcel   it under the terms of the GNU General Public License as published by
12130803Smarcel   the Free Software Foundation; either version 2 of the License, or (at
13130803Smarcel   your option) any later version.
14130803Smarcel
15130803Smarcel   This program is distributed in the hope that it will be useful, but
16130803Smarcel   WITHOUT ANY WARRANTY; without even the implied warranty of
17130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18130803Smarcel   General Public License for more details.
19130803Smarcel
20130803Smarcel   You should have received a copy of the GNU General Public License
21130803Smarcel   along with this program; if not, write to the Free Software
22130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
23130803Smarcel   Boston, MA 02111-1307, USA.  */
24130803Smarcel
25130803Smarcel#ifndef DICTIONARY_H
26130803Smarcel#define DICTIONARY_H
27130803Smarcel
28130803Smarcel/* An opaque type for dictionaries; only dictionary.c should know
29130803Smarcel   about its innards.  */
30130803Smarcel
31130803Smarcelstruct dictionary;
32130803Smarcel
33130803Smarcel/* Other types needed for declarations.  */
34130803Smarcel
35130803Smarcelstruct symbol;
36130803Smarcelstruct obstack;
37130803Smarcelstruct pending;
38130803Smarcel
39130803Smarcel
40130803Smarcel/* The creation functions for various implementations of
41130803Smarcel   dictionaries.  */
42130803Smarcel
43130803Smarcel/* Create a dictionary implemented via a fixed-size hashtable.  All
44130803Smarcel   memory it uses is allocated on OBSTACK; the environment is
45130803Smarcel   initialized from SYMBOL_LIST.  */
46130803Smarcel
47130803Smarcelextern struct dictionary *dict_create_hashed (struct obstack *obstack,
48130803Smarcel					      const struct pending
49130803Smarcel					      *symbol_list);
50130803Smarcel
51130803Smarcel/* Create a dictionary implemented via a hashtable that grows as
52130803Smarcel   necessary.  The dictionary is initially empty; to add symbols to
53130803Smarcel   it, call dict_add_symbol().  Call dict_free() when you're done with
54130803Smarcel   it.  */
55130803Smarcel
56130803Smarcelextern struct dictionary *dict_create_hashed_expandable (void);
57130803Smarcel
58130803Smarcel/* Create a dictionary implemented via a fixed-size array.  All memory
59130803Smarcel   it uses is allocated on OBSTACK; the environment is initialized
60130803Smarcel   from the SYMBOL_LIST.  The symbols are ordered in the same order
61130803Smarcel   that they're found in SYMBOL_LIST.  */
62130803Smarcel
63130803Smarcelextern struct dictionary *dict_create_linear (struct obstack *obstack,
64130803Smarcel					      const struct pending
65130803Smarcel					      *symbol_list);
66130803Smarcel
67130803Smarcel/* Create a dictionary implemented via an array that grows as
68130803Smarcel   necessary.  The dictionary is initially empty; to add symbols to
69130803Smarcel   it, call dict_add_symbol().  Call dict_free() when you're done with
70130803Smarcel   it.  */
71130803Smarcel
72130803Smarcelextern struct dictionary *dict_create_linear_expandable (void);
73130803Smarcel
74130803Smarcel
75130803Smarcel/* The functions providing the interface to dictionaries.  Note that
76130803Smarcel   the most common parts of the interface, namely symbol lookup, are
77130803Smarcel   only provided via iterator functions.  */
78130803Smarcel
79130803Smarcel/* Free the memory used by a dictionary that's not on an obstack.  (If
80130803Smarcel   any.)  */
81130803Smarcel
82130803Smarcelextern void dict_free (struct dictionary *dict);
83130803Smarcel
84130803Smarcel/* Add a symbol to an expandable dictionary.  */
85130803Smarcel
86130803Smarcelextern void dict_add_symbol (struct dictionary *dict, struct symbol *sym);
87130803Smarcel
88130803Smarcel/* Is the dictionary empty?  */
89130803Smarcel
90130803Smarcelextern int dict_empty (struct dictionary *dict);
91130803Smarcel
92130803Smarcel/* A type containing data that is used when iterating over all symbols
93130803Smarcel   in a dictionary.  Don't ever look at its innards; this type would
94130803Smarcel   be opaque if we didn't need to be able to allocate it on the
95130803Smarcel   stack.  */
96130803Smarcel
97130803Smarcelstruct dict_iterator
98130803Smarcel{
99130803Smarcel  /* The dictionary that this iterator is associated to.  */
100130803Smarcel  const struct dictionary *dict;
101130803Smarcel  /* The next two members are data that is used in a way that depends
102130803Smarcel     on DICT's implementation type.  */
103130803Smarcel  int index;
104130803Smarcel  struct symbol *current;
105130803Smarcel};
106130803Smarcel
107130803Smarcel/* Initialize ITERATOR to point at the first symbol in DICT, and
108130803Smarcel   return that first symbol, or NULL if DICT is empty.  */
109130803Smarcel
110130803Smarcelextern struct symbol *dict_iterator_first (const struct dictionary *dict,
111130803Smarcel					   struct dict_iterator *iterator);
112130803Smarcel
113130803Smarcel/* Advance ITERATOR, and return the next symbol, or NULL if there are
114130803Smarcel   no more symbols.  Don't call this if you've previously received
115130803Smarcel   NULL from dict_iterator_first or dict_iterator_next on this
116130803Smarcel   iteration.  */
117130803Smarcel
118130803Smarcelextern struct symbol *dict_iterator_next (struct dict_iterator *iterator);
119130803Smarcel
120130803Smarcel/* Initialize ITERATOR to point at the first symbol in DICT whose
121130803Smarcel   SYMBOL_BEST_NAME is NAME (as tested using strcmp_iw), and return
122130803Smarcel   that first symbol, or NULL if there are no such symbols.  */
123130803Smarcel
124130803Smarcelextern struct symbol *dict_iter_name_first (const struct dictionary *dict,
125130803Smarcel					    const char *name,
126130803Smarcel					    struct dict_iterator *iterator);
127130803Smarcel
128130803Smarcel/* Advance ITERATOR to point at the next symbol in DICT whose
129130803Smarcel   SYMBOL_BEST_NAME is NAME (as tested using strcmp_iw), or NULL if
130130803Smarcel   there are no more such symbols.  Don't call this if you've
131130803Smarcel   previously received NULL from dict_iterator_first or
132130803Smarcel   dict_iterator_next on this iteration.  And don't call it unless
133130803Smarcel   ITERATOR was created by a previous call to dict_iter_name_first
134130803Smarcel   with the same NAME.  */
135130803Smarcel
136130803Smarcelextern struct symbol *dict_iter_name_next (const char *name,
137130803Smarcel					   struct dict_iterator *iterator);
138130803Smarcel
139130803Smarcel/* Return some notion of the size of the dictionary: the number of
140130803Smarcel   symbols if we have that, the number of hash buckets otherwise.  */
141130803Smarcel
142130803Smarcelextern int dict_size (const struct dictionary *dict);
143130803Smarcel
144130803Smarcel/* Macro to loop through all symbols in a dictionary DICT, in no
145130803Smarcel   particular order.  ITER is a struct dict_iterator (NOTE: __not__ a
146130803Smarcel   struct dict_iterator *), and SYM points to the current symbol.
147130803Smarcel
148130803Smarcel   It's implemented as a single loop, so you can terminate the loop
149130803Smarcel   early by a break if you desire.  */
150130803Smarcel
151130803Smarcel#define ALL_DICT_SYMBOLS(dict, iter, sym)			\
152130803Smarcel	for ((sym) = dict_iterator_first ((dict), &(iter));	\
153130803Smarcel	     (sym);						\
154130803Smarcel	     (sym) = dict_iterator_next (&(iter)))
155130803Smarcel
156130803Smarcel#endif /* DICTIONARY_H */
157