]>
Commit | Line | Data |
---|---|---|
aed81407 GDR |
1 | /* Definitions for C++ name lookup routines. |
2 | Copyright (C) 2003 Free Software Foundation, Inc. | |
3 | Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> | |
4 | ||
ed3cf953 | 5 | This file is part of GCC. |
aed81407 | 6 | |
ed3cf953 | 7 | GCC is free software; you can redistribute it and/or modify |
aed81407 GDR |
8 | it under the terms of the GNU General Public License as published by |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
ed3cf953 | 12 | GCC is distributed in the hope that it will be useful, |
aed81407 GDR |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
ed3cf953 | 18 | along with GCC; see the file COPYING. If not, write to |
aed81407 GDR |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "tree.h" | |
27 | #include "cp-tree.h" | |
28 | #include "name-lookup.h" | |
ed3cf953 | 29 | #include "timevar.h" |
c87ceb13 | 30 | #include "toplev.h" |
aed81407 | 31 | |
5e0c54e5 GDR |
32 | /* Compute the chain index of a binding_entry given the HASH value of its |
33 | name and the total COUNT of chains. COUNT is assumed to be a power | |
34 | of 2. */ | |
35 | #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) | |
36 | ||
37 | /* A free list of "binding_entry"s awaiting for re-use. */ | |
7e8f3096 | 38 | static GTY((deletable(""))) binding_entry free_binding_entry = NULL; |
5e0c54e5 GDR |
39 | |
40 | /* Create a binding_entry object for (NAME, TYPE). */ | |
41 | static inline binding_entry | |
42 | binding_entry_make (tree name, tree type) | |
43 | { | |
44 | binding_entry entry; | |
45 | ||
46 | if (free_binding_entry) | |
47 | { | |
48 | entry = free_binding_entry; | |
49 | free_binding_entry = entry->chain; | |
50 | } | |
51 | else | |
52 | entry = ggc_alloc (sizeof (struct binding_entry_s)); | |
53 | ||
54 | entry->name = name; | |
55 | entry->type = type; | |
7e8f3096 | 56 | entry->chain = NULL; |
5e0c54e5 GDR |
57 | |
58 | return entry; | |
59 | } | |
60 | ||
61 | /* Put ENTRY back on the free list. */ | |
62 | static inline void | |
63 | binding_entry_free (binding_entry entry) | |
64 | { | |
65 | entry->chain = free_binding_entry; | |
66 | free_binding_entry = entry; | |
67 | } | |
68 | ||
69 | /* The datatype used to implement the mapping from names to types at | |
70 | a given scope. */ | |
71 | struct binding_table_s GTY(()) | |
72 | { | |
73 | /* Array of chains of "binding_entry"s */ | |
74 | binding_entry * GTY((length ("%h.chain_count"))) chain; | |
75 | ||
76 | /* The number of chains in this table. This is the length of the | |
7e8f3096 | 77 | the member "chain" considered as an array. */ |
5e0c54e5 GDR |
78 | size_t chain_count; |
79 | ||
80 | /* Number of "binding_entry"s in this table. */ | |
81 | size_t entry_count; | |
82 | }; | |
83 | ||
84 | /* Construct TABLE with an initial CHAIN_COUNT. */ | |
85 | static inline void | |
86 | binding_table_construct (binding_table table, size_t chain_count) | |
87 | { | |
88 | table->chain_count = chain_count; | |
89 | table->entry_count = 0; | |
90 | table->chain = ggc_alloc_cleared | |
91 | (table->chain_count * sizeof (binding_entry)); | |
92 | } | |
93 | ||
9bcb9aae | 94 | /* Free TABLE by making its entries ready for reuse. */ |
5e0c54e5 GDR |
95 | void |
96 | binding_table_free (binding_table table) | |
97 | { | |
98 | size_t i; | |
99 | if (table == NULL) | |
100 | return; | |
101 | ||
102 | for (i = 0; i < table->chain_count; ++i) | |
103 | { | |
7e8f3096 AP |
104 | binding_entry temp = table->chain[i]; |
105 | while (temp != NULL) | |
5e0c54e5 | 106 | { |
7e8f3096 AP |
107 | binding_entry entry = temp; |
108 | temp = entry->chain; | |
a1447166 | 109 | entry->chain = NULL; |
5e0c54e5 GDR |
110 | binding_entry_free (entry); |
111 | } | |
7e8f3096 | 112 | table->chain[i] = temp; |
5e0c54e5 GDR |
113 | } |
114 | table->entry_count = 0; | |
115 | } | |
116 | ||
117 | /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ | |
118 | binding_table | |
119 | binding_table_new (size_t chain_count) | |
120 | { | |
121 | binding_table table = ggc_alloc (sizeof (struct binding_table_s)); | |
7e8f3096 | 122 | table->chain = NULL; |
5e0c54e5 GDR |
123 | binding_table_construct (table, chain_count); |
124 | return table; | |
125 | } | |
126 | ||
127 | /* Expand TABLE to twice its current chain_count. */ | |
128 | static void | |
129 | binding_table_expand (binding_table table) | |
130 | { | |
131 | const size_t old_chain_count = table->chain_count; | |
132 | const size_t old_entry_count = table->entry_count; | |
133 | const size_t new_chain_count = 2 * old_chain_count; | |
134 | binding_entry *old_chains = table->chain; | |
135 | size_t i; | |
136 | ||
137 | binding_table_construct (table, new_chain_count); | |
138 | for (i = 0; i < old_chain_count; ++i) | |
139 | { | |
140 | binding_entry entry = old_chains[i]; | |
141 | for (; entry != NULL; entry = old_chains[i]) | |
142 | { | |
143 | const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); | |
144 | const size_t j = ENTRY_INDEX (hash, new_chain_count); | |
145 | ||
146 | old_chains[i] = entry->chain; | |
147 | entry->chain = table->chain[j]; | |
148 | table->chain[j] = entry; | |
149 | } | |
150 | } | |
151 | table->entry_count = old_entry_count; | |
152 | } | |
153 | ||
154 | /* Insert a binding for NAME to TYPe into TABLE. */ | |
155 | void | |
156 | binding_table_insert (binding_table table, tree name, tree type) | |
157 | { | |
158 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
159 | const size_t i = ENTRY_INDEX (hash, table->chain_count); | |
160 | binding_entry entry = binding_entry_make (name, type); | |
161 | ||
162 | entry->chain = table->chain[i]; | |
163 | table->chain[i] = entry; | |
164 | ++table->entry_count; | |
165 | ||
166 | if (3 * table->chain_count < 5 * table->entry_count) | |
167 | binding_table_expand (table); | |
168 | } | |
169 | ||
170 | /* Return the binding_entry, if any, that maps NAME. */ | |
171 | binding_entry | |
172 | binding_table_find (binding_table table, tree name) | |
173 | { | |
174 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
175 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
176 | ||
177 | while (entry != NULL && entry->name != name) | |
178 | entry = entry->chain; | |
179 | ||
180 | return entry; | |
181 | } | |
182 | ||
183 | /* Return the binding_entry, if any, that maps name to an anonymous type. */ | |
184 | tree | |
185 | binding_table_find_anon_type (binding_table table, tree name) | |
186 | { | |
187 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
188 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
189 | ||
190 | while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name) | |
191 | entry = entry->chain; | |
192 | ||
193 | return entry ? entry->type : NULL; | |
194 | } | |
195 | ||
196 | /* Return the binding_entry, if any, that has TYPE as target. If NAME | |
197 | is non-null, then set the domain and rehash that entry. */ | |
198 | binding_entry | |
199 | binding_table_reverse_maybe_remap (binding_table table, tree type, tree name) | |
200 | { | |
201 | const size_t chain_count = table->chain_count; | |
202 | binding_entry entry = NULL; | |
203 | binding_entry *p = NULL; | |
204 | size_t i; | |
205 | ||
206 | for (i = 0; i < chain_count && entry == NULL; ++i) | |
207 | { | |
208 | p = &table->chain[i]; | |
209 | while (*p != NULL && entry == NULL) | |
210 | if ((*p)->type == type) | |
211 | entry = *p; | |
212 | else | |
213 | p = &(*p)->chain; | |
214 | } | |
215 | ||
216 | if (entry != NULL && name != NULL && entry->name != name) | |
217 | { | |
218 | /* Remove the bucket from the previous chain. */ | |
219 | *p = (*p)->chain; | |
220 | ||
221 | /* Remap the name type to type. */ | |
222 | i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count); | |
223 | entry->chain = table->chain[i]; | |
224 | entry->name = name; | |
225 | table->chain[i] = entry; | |
226 | } | |
227 | ||
228 | return entry; | |
229 | } | |
230 | ||
231 | /* Remove from TABLE all entries that map to anonymous enums or | |
232 | class-types. */ | |
233 | void | |
234 | binding_table_remove_anonymous_types (binding_table table) | |
235 | { | |
236 | const size_t chain_count = table->chain_count; | |
237 | size_t i; | |
238 | ||
239 | for (i = 0; i < chain_count; ++i) | |
240 | { | |
241 | binding_entry *p = &table->chain[i]; | |
242 | ||
243 | while (*p != NULL) | |
244 | if (ANON_AGGRNAME_P ((*p)->name)) | |
245 | { | |
246 | binding_entry e = *p; | |
247 | *p = (*p)->chain; | |
248 | --table->entry_count; | |
249 | binding_entry_free (e); | |
250 | } | |
251 | else | |
252 | p = &(*p)->chain; | |
253 | } | |
254 | } | |
255 | ||
256 | /* Apply PROC -- with DATA -- to all entries in TABLE. */ | |
257 | void | |
258 | binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) | |
259 | { | |
260 | const size_t chain_count = table->chain_count; | |
261 | size_t i; | |
262 | ||
263 | for (i = 0; i < chain_count; ++i) | |
264 | { | |
265 | binding_entry entry = table->chain[i]; | |
266 | for (; entry != NULL; entry = entry->chain) | |
267 | proc (entry, data); | |
268 | } | |
269 | } | |
270 | \f | |
271 | ||
aed81407 GDR |
272 | /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ |
273 | static GTY((deletable (""))) cxx_binding *free_bindings; | |
274 | ||
275 | /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ | |
276 | cxx_binding * | |
277 | cxx_binding_make (tree value, tree type) | |
278 | { | |
279 | cxx_binding *binding; | |
280 | if (free_bindings) | |
281 | { | |
282 | binding = free_bindings; | |
283 | free_bindings = binding->previous; | |
284 | } | |
285 | else | |
7e8f3096 | 286 | binding = ggc_alloc (sizeof (cxx_binding)); |
aed81407 GDR |
287 | |
288 | binding->value = value; | |
289 | binding->type = type; | |
7e8f3096 | 290 | binding->previous = NULL; |
aed81407 GDR |
291 | |
292 | return binding; | |
293 | } | |
294 | ||
295 | /* Put BINDING back on the free list. */ | |
296 | void | |
297 | cxx_binding_free (cxx_binding *binding) | |
298 | { | |
299 | binding->previous = free_bindings; | |
300 | free_bindings = binding; | |
301 | } | |
c87ceb13 GDR |
302 | |
303 | /* BINDING records an existing declaration for a namein the current scope. | |
304 | But, DECL is another declaration for that same identifier in the | |
305 | same scope. This is the `struct stat' hack whereby a non-typedef | |
306 | class name or enum-name can be bound at the same level as some other | |
307 | kind of entity. | |
308 | 3.3.7/1 | |
309 | ||
310 | A class name (9.1) or enumeration name (7.2) can be hidden by the | |
311 | name of an object, function, or enumerator declared in the same scope. | |
312 | If a class or enumeration name and an object, function, or enumerator | |
313 | are declared in the same scope (in any order) with the same name, the | |
314 | class or enumeration name is hidden wherever the object, function, or | |
315 | enumerator name is visible. | |
316 | ||
317 | It's the responsibility of the caller to check that | |
318 | inserting this name is valid here. Returns nonzero if the new binding | |
319 | was successful. */ | |
320 | ||
321 | bool | |
322 | supplement_binding (cxx_binding *binding, tree decl) | |
323 | { | |
324 | tree bval = binding->value; | |
325 | bool ok = true; | |
326 | ||
327 | timevar_push (TV_NAME_LOOKUP); | |
328 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) | |
329 | /* The new name is the type name. */ | |
330 | binding->type = decl; | |
331 | else if (!bval) | |
332 | /* This situation arises when push_class_level_binding moves an | |
333 | inherited type-binding out of the way to make room for a new | |
334 | value binding. */ | |
335 | binding->value = decl; | |
336 | else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)) | |
337 | { | |
338 | /* The old binding was a type name. It was placed in | |
147135cc | 339 | VALUE field because it was thought, at the point it was |
c87ceb13 GDR |
340 | declared, to be the only entity with such a name. Move the |
341 | type name into the type slot; it is now hidden by the new | |
342 | binding. */ | |
343 | binding->type = bval; | |
344 | binding->value = decl; | |
345 | binding->value_is_inherited = false; | |
346 | } | |
347 | else if (TREE_CODE (bval) == TYPE_DECL | |
348 | && TREE_CODE (decl) == TYPE_DECL | |
349 | && DECL_NAME (decl) == DECL_NAME (bval) | |
350 | && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval)) | |
351 | /* If either type involves template parameters, we must | |
352 | wait until instantiation. */ | |
353 | || uses_template_parms (TREE_TYPE (decl)) | |
354 | || uses_template_parms (TREE_TYPE (bval)))) | |
355 | /* We have two typedef-names, both naming the same type to have | |
356 | the same name. This is OK because of: | |
357 | ||
358 | [dcl.typedef] | |
359 | ||
360 | In a given scope, a typedef specifier can be used to redefine | |
361 | the name of any type declared in that scope to refer to the | |
362 | type to which it already refers. */ | |
363 | ok = false; | |
364 | /* There can be two block-scope declarations of the same variable, | |
365 | so long as they are `extern' declarations. However, there cannot | |
366 | be two declarations of the same static data member: | |
367 | ||
368 | [class.mem] | |
369 | ||
370 | A member shall not be declared twice in the | |
371 | member-specification. */ | |
372 | else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL | |
373 | && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval) | |
374 | && !DECL_CLASS_SCOPE_P (decl)) | |
375 | { | |
376 | duplicate_decls (decl, binding->value); | |
377 | ok = false; | |
378 | } | |
379 | else | |
380 | { | |
381 | error ("declaration of `%#D'", decl); | |
382 | cp_error_at ("conflicts with previous declaration `%#D'", | |
383 | binding->value); | |
384 | ok = false; | |
385 | } | |
386 | ||
387 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok); | |
388 | } | |
ed3cf953 GDR |
389 | \f |
390 | /* Return (from the stack of) the BINDING, if any, establihsed at SCOPE. */ | |
391 | ||
392 | static inline cxx_binding * | |
393 | find_binding (cxx_scope *scope, cxx_binding *binding) | |
394 | { | |
395 | timevar_push (TV_NAME_LOOKUP); | |
396 | ||
397 | for (; binding != NULL; binding = binding->previous) | |
147135cc | 398 | if (binding->scope == scope) |
ed3cf953 GDR |
399 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding); |
400 | ||
da247ccc | 401 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0); |
ed3cf953 GDR |
402 | } |
403 | ||
404 | /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ | |
405 | cxx_binding * | |
406 | cxx_scope_find_binding_for_name (cxx_scope *scope, tree name) | |
407 | { | |
408 | cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
409 | if (b) | |
410 | { | |
411 | /* Fold-in case where NAME is used only once. */ | |
147135cc | 412 | if (scope == b->scope && b->previous == NULL) |
ed3cf953 GDR |
413 | return b; |
414 | return find_binding (scope, b); | |
415 | } | |
416 | return NULL; | |
417 | } | |
418 | ||
419 | /* Always returns a binding for name in scope. If no binding is | |
420 | found, make a new one. */ | |
421 | ||
422 | cxx_binding * | |
423 | binding_for_name (cxx_scope *scope, tree name) | |
424 | { | |
425 | cxx_binding *result; | |
426 | ||
427 | result = cxx_scope_find_binding_for_name (scope, name); | |
428 | if (result) | |
429 | return result; | |
430 | /* Not found, make a new one. */ | |
431 | result = cxx_binding_make (NULL, NULL); | |
432 | result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
147135cc | 433 | result->scope = scope; |
ed3cf953 GDR |
434 | result->is_local = false; |
435 | result->value_is_inherited = false; | |
436 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
437 | return result; | |
438 | } | |
439 | \f | |
440 | /* Namespace-scope manipulation routines. */ | |
441 | ||
442 | /* Return the binding value for name in scope. */ | |
443 | ||
444 | tree | |
445 | namespace_binding (tree name, tree scope) | |
446 | { | |
447 | cxx_binding *binding; | |
448 | ||
449 | if (scope == NULL) | |
450 | scope = global_namespace; | |
451 | scope = ORIGINAL_NAMESPACE (scope); | |
452 | binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
453 | ||
454 | return binding ? binding->value : NULL_TREE; | |
455 | } | |
456 | ||
457 | /* Set the binding value for name in scope. */ | |
458 | ||
459 | void | |
460 | set_namespace_binding (tree name, tree scope, tree val) | |
461 | { | |
462 | cxx_binding *b; | |
463 | ||
464 | timevar_push (TV_NAME_LOOKUP); | |
465 | if (scope == NULL_TREE) | |
466 | scope = global_namespace; | |
467 | b = binding_for_name (NAMESPACE_LEVEL (scope), name); | |
147135cc GDR |
468 | if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node) |
469 | b->value = val; | |
4b0d3cbe | 470 | else |
c87ceb13 | 471 | supplement_binding (b, val); |
ed3cf953 GDR |
472 | timevar_pop (TV_NAME_LOOKUP); |
473 | } | |
474 | ||
28ea4c88 | 475 | #include "gt-cp-name-lookup.h" |