]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/name-lookup.c
Daily bump.
[thirdparty/gcc.git] / gcc / cp / name-lookup.c
CommitLineData
8546e572 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
d36ac936 5This file is part of GCC.
8546e572 6
d36ac936 7GCC is free software; you can redistribute it and/or modify
8546e572 8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
d36ac936 12GCC is distributed in the hope that it will be useful,
8546e572 13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
d36ac936 18along with GCC; see the file COPYING. If not, write to
8546e572 19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, 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"
d36ac936 29#include "timevar.h"
3bd975bc 30#include "toplev.h"
8546e572 31
af694375 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. */
9449ebe7 38static GTY((deletable(""))) binding_entry free_binding_entry = NULL;
af694375 39
40/* Create a binding_entry object for (NAME, TYPE). */
41static inline binding_entry
42binding_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;
9449ebe7 56 entry->chain = NULL;
af694375 57
58 return entry;
59}
60
61/* Put ENTRY back on the free list. */
62static inline void
63binding_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. */
71struct 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
9449ebe7 77 the member "chain" considered as an array. */
af694375 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. */
85static inline void
86binding_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
6beb3f76 94/* Free TABLE by making its entries ready for reuse. */
af694375 95void
96binding_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 {
9449ebe7 104 binding_entry temp = table->chain[i];
105 while (temp != NULL)
af694375 106 {
9449ebe7 107 binding_entry entry = temp;
108 temp = entry->chain;
c9f8afbd 109 entry->chain = NULL;
af694375 110 binding_entry_free (entry);
111 }
9449ebe7 112 table->chain[i] = temp;
af694375 113 }
114 table->entry_count = 0;
115}
116
117/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
118binding_table
119binding_table_new (size_t chain_count)
120{
121 binding_table table = ggc_alloc (sizeof (struct binding_table_s));
9449ebe7 122 table->chain = NULL;
af694375 123 binding_table_construct (table, chain_count);
124 return table;
125}
126
127/* Expand TABLE to twice its current chain_count. */
128static void
129binding_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. */
155void
156binding_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. */
171binding_entry
172binding_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. */
184tree
185binding_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. */
198binding_entry
199binding_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. */
233void
234binding_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. */
257void
258binding_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
8546e572 272/* A free list of "cxx_binding"s, connected by their PREVIOUS. */
273static GTY((deletable (""))) cxx_binding *free_bindings;
274
275/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
276cxx_binding *
277cxx_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
9449ebe7 286 binding = ggc_alloc (sizeof (cxx_binding));
8546e572 287
288 binding->value = value;
289 binding->type = type;
9449ebe7 290 binding->previous = NULL;
8546e572 291
292 return binding;
293}
294
295/* Put BINDING back on the free list. */
296void
297cxx_binding_free (cxx_binding *binding)
298{
299 binding->previous = free_bindings;
300 free_bindings = binding;
301}
3bd975bc 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
321bool
322supplement_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
339 BINDING_VALUE because it was thought, at the point it was
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}
d36ac936 389\f
390/* Return (from the stack of) the BINDING, if any, establihsed at SCOPE. */
391
392static inline cxx_binding *
393find_binding (cxx_scope *scope, cxx_binding *binding)
394{
395 timevar_push (TV_NAME_LOOKUP);
396
397 for (; binding != NULL; binding = binding->previous)
398 if (BINDING_SCOPE (binding) == scope)
399 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
400
52ce909c 401 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
d36ac936 402}
403
404/* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */
405cxx_binding *
406cxx_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. */
412 if (scope == BINDING_SCOPE (b) && b->previous == NULL)
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
422cxx_binding *
423binding_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);
433 BINDING_SCOPE (result) = scope;
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
444tree
445namespace_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
459void
460set_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);
b7d1e8ea 468 if (!BINDING_VALUE (b)
469 || TREE_CODE (val) == OVERLOAD
470 || val == error_mark_node)
471 BINDING_VALUE (b) = val;
472 else
3bd975bc 473 supplement_binding (b, val);
d36ac936 474 timevar_pop (TV_NAME_LOOKUP);
475}
476
db90b1e5 477#include "gt-cp-name-lookup.h"