]>
Commit | Line | Data |
---|---|---|
aed81407 | 1 | /* Definitions for C++ name lookup routines. |
c404ab02 | 2 | Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
aed81407 GDR |
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" | |
a5e6b29b | 26 | #include "flags.h" |
aed81407 GDR |
27 | #include "tree.h" |
28 | #include "cp-tree.h" | |
29 | #include "name-lookup.h" | |
ed3cf953 | 30 | #include "timevar.h" |
c87ceb13 | 31 | #include "toplev.h" |
00e8de68 GDR |
32 | #include "diagnostic.h" |
33 | ||
34 | static cxx_scope *innermost_nonclass_level (void); | |
35 | static tree select_decl (cxx_binding *, int); | |
5a167978 | 36 | static cxx_binding *binding_for_name (cxx_scope *, tree); |
a5e6b29b GDR |
37 | static tree lookup_name_current_level (tree); |
38 | static void push_local_binding (tree, tree, int); | |
39 | static tree push_overloaded_decl (tree, int); | |
40 | static bool lookup_using_namespace (tree, cxx_binding *, tree, | |
543ebd4a | 41 | tree, int); |
a5e6b29b GDR |
42 | static bool qualified_lookup_using_namespace (tree, tree, cxx_binding *, int); |
43 | static tree lookup_type_current_level (tree); | |
44 | static tree push_using_directive (tree); | |
45 | ||
5a167978 GDR |
46 | |
47 | /* The :: namespace. */ | |
48 | ||
49 | tree global_namespace; | |
00e8de68 | 50 | |
ed36980c JM |
51 | /* The name of the anonymous namespace, throughout this translation |
52 | unit. */ | |
53 | GTY(()) tree anonymous_namespace_name; | |
54 | ||
aed81407 | 55 | |
5e0c54e5 GDR |
56 | /* Compute the chain index of a binding_entry given the HASH value of its |
57 | name and the total COUNT of chains. COUNT is assumed to be a power | |
58 | of 2. */ | |
daafa301 | 59 | |
5e0c54e5 GDR |
60 | #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) |
61 | ||
62 | /* A free list of "binding_entry"s awaiting for re-use. */ | |
daafa301 | 63 | |
7e8f3096 | 64 | static GTY((deletable(""))) binding_entry free_binding_entry = NULL; |
5e0c54e5 GDR |
65 | |
66 | /* Create a binding_entry object for (NAME, TYPE). */ | |
daafa301 | 67 | |
5e0c54e5 GDR |
68 | static inline binding_entry |
69 | binding_entry_make (tree name, tree type) | |
70 | { | |
71 | binding_entry entry; | |
72 | ||
73 | if (free_binding_entry) | |
74 | { | |
75 | entry = free_binding_entry; | |
76 | free_binding_entry = entry->chain; | |
77 | } | |
78 | else | |
79 | entry = ggc_alloc (sizeof (struct binding_entry_s)); | |
80 | ||
81 | entry->name = name; | |
82 | entry->type = type; | |
7e8f3096 | 83 | entry->chain = NULL; |
5e0c54e5 GDR |
84 | |
85 | return entry; | |
86 | } | |
87 | ||
88 | /* Put ENTRY back on the free list. */ | |
daafa301 | 89 | |
5e0c54e5 GDR |
90 | static inline void |
91 | binding_entry_free (binding_entry entry) | |
92 | { | |
04693f2f GDR |
93 | entry->name = NULL; |
94 | entry->type = NULL; | |
5e0c54e5 GDR |
95 | entry->chain = free_binding_entry; |
96 | free_binding_entry = entry; | |
97 | } | |
98 | ||
99 | /* The datatype used to implement the mapping from names to types at | |
100 | a given scope. */ | |
101 | struct binding_table_s GTY(()) | |
102 | { | |
103 | /* Array of chains of "binding_entry"s */ | |
104 | binding_entry * GTY((length ("%h.chain_count"))) chain; | |
105 | ||
106 | /* The number of chains in this table. This is the length of the | |
7e8f3096 | 107 | the member "chain" considered as an array. */ |
5e0c54e5 GDR |
108 | size_t chain_count; |
109 | ||
110 | /* Number of "binding_entry"s in this table. */ | |
111 | size_t entry_count; | |
112 | }; | |
113 | ||
114 | /* Construct TABLE with an initial CHAIN_COUNT. */ | |
daafa301 | 115 | |
5e0c54e5 GDR |
116 | static inline void |
117 | binding_table_construct (binding_table table, size_t chain_count) | |
118 | { | |
119 | table->chain_count = chain_count; | |
120 | table->entry_count = 0; | |
121 | table->chain = ggc_alloc_cleared | |
122 | (table->chain_count * sizeof (binding_entry)); | |
123 | } | |
124 | ||
daafa301 GDR |
125 | /* Make TABLE's entries ready for reuse. */ |
126 | ||
00e8de68 | 127 | static void |
5e0c54e5 GDR |
128 | binding_table_free (binding_table table) |
129 | { | |
130 | size_t i; | |
daafa301 GDR |
131 | size_t count; |
132 | ||
5e0c54e5 GDR |
133 | if (table == NULL) |
134 | return; | |
135 | ||
daafa301 | 136 | for (i = 0, count = table->chain_count; i < count; ++i) |
5e0c54e5 | 137 | { |
7e8f3096 AP |
138 | binding_entry temp = table->chain[i]; |
139 | while (temp != NULL) | |
5e0c54e5 | 140 | { |
7e8f3096 AP |
141 | binding_entry entry = temp; |
142 | temp = entry->chain; | |
5e0c54e5 GDR |
143 | binding_entry_free (entry); |
144 | } | |
daafa301 | 145 | table->chain[i] = NULL; |
5e0c54e5 GDR |
146 | } |
147 | table->entry_count = 0; | |
148 | } | |
149 | ||
150 | /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ | |
daafa301 | 151 | |
00e8de68 | 152 | static inline binding_table |
5e0c54e5 GDR |
153 | binding_table_new (size_t chain_count) |
154 | { | |
155 | binding_table table = ggc_alloc (sizeof (struct binding_table_s)); | |
7e8f3096 | 156 | table->chain = NULL; |
5e0c54e5 GDR |
157 | binding_table_construct (table, chain_count); |
158 | return table; | |
159 | } | |
160 | ||
161 | /* Expand TABLE to twice its current chain_count. */ | |
daafa301 | 162 | |
5e0c54e5 GDR |
163 | static void |
164 | binding_table_expand (binding_table table) | |
165 | { | |
166 | const size_t old_chain_count = table->chain_count; | |
167 | const size_t old_entry_count = table->entry_count; | |
168 | const size_t new_chain_count = 2 * old_chain_count; | |
169 | binding_entry *old_chains = table->chain; | |
170 | size_t i; | |
171 | ||
172 | binding_table_construct (table, new_chain_count); | |
173 | for (i = 0; i < old_chain_count; ++i) | |
174 | { | |
175 | binding_entry entry = old_chains[i]; | |
176 | for (; entry != NULL; entry = old_chains[i]) | |
177 | { | |
178 | const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); | |
179 | const size_t j = ENTRY_INDEX (hash, new_chain_count); | |
180 | ||
181 | old_chains[i] = entry->chain; | |
182 | entry->chain = table->chain[j]; | |
183 | table->chain[j] = entry; | |
184 | } | |
185 | } | |
186 | table->entry_count = old_entry_count; | |
187 | } | |
188 | ||
daafa301 GDR |
189 | /* Insert a binding for NAME to TYPE into TABLE. */ |
190 | ||
00e8de68 | 191 | static void |
5e0c54e5 GDR |
192 | binding_table_insert (binding_table table, tree name, tree type) |
193 | { | |
194 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
195 | const size_t i = ENTRY_INDEX (hash, table->chain_count); | |
196 | binding_entry entry = binding_entry_make (name, type); | |
197 | ||
198 | entry->chain = table->chain[i]; | |
199 | table->chain[i] = entry; | |
200 | ++table->entry_count; | |
201 | ||
202 | if (3 * table->chain_count < 5 * table->entry_count) | |
203 | binding_table_expand (table); | |
204 | } | |
205 | ||
206 | /* Return the binding_entry, if any, that maps NAME. */ | |
daafa301 | 207 | |
5e0c54e5 GDR |
208 | binding_entry |
209 | binding_table_find (binding_table table, tree name) | |
210 | { | |
211 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
212 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
213 | ||
214 | while (entry != NULL && entry->name != name) | |
215 | entry = entry->chain; | |
216 | ||
217 | return entry; | |
218 | } | |
219 | ||
daafa301 GDR |
220 | /* Return the binding_entry, if any, that maps NAME to an anonymous type. */ |
221 | ||
00e8de68 | 222 | static tree |
5e0c54e5 GDR |
223 | binding_table_find_anon_type (binding_table table, tree name) |
224 | { | |
225 | const unsigned int hash = IDENTIFIER_HASH_VALUE (name); | |
226 | binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; | |
227 | ||
228 | while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name) | |
229 | entry = entry->chain; | |
230 | ||
231 | return entry ? entry->type : NULL; | |
232 | } | |
233 | ||
234 | /* Return the binding_entry, if any, that has TYPE as target. If NAME | |
235 | is non-null, then set the domain and rehash that entry. */ | |
daafa301 | 236 | |
00e8de68 | 237 | static binding_entry |
5e0c54e5 GDR |
238 | binding_table_reverse_maybe_remap (binding_table table, tree type, tree name) |
239 | { | |
240 | const size_t chain_count = table->chain_count; | |
241 | binding_entry entry = NULL; | |
242 | binding_entry *p = NULL; | |
243 | size_t i; | |
244 | ||
245 | for (i = 0; i < chain_count && entry == NULL; ++i) | |
246 | { | |
247 | p = &table->chain[i]; | |
248 | while (*p != NULL && entry == NULL) | |
249 | if ((*p)->type == type) | |
250 | entry = *p; | |
251 | else | |
252 | p = &(*p)->chain; | |
253 | } | |
254 | ||
255 | if (entry != NULL && name != NULL && entry->name != name) | |
256 | { | |
257 | /* Remove the bucket from the previous chain. */ | |
258 | *p = (*p)->chain; | |
259 | ||
260 | /* Remap the name type to type. */ | |
261 | i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count); | |
262 | entry->chain = table->chain[i]; | |
263 | entry->name = name; | |
264 | table->chain[i] = entry; | |
265 | } | |
266 | ||
267 | return entry; | |
268 | } | |
269 | ||
270 | /* Remove from TABLE all entries that map to anonymous enums or | |
271 | class-types. */ | |
daafa301 | 272 | |
5e0c54e5 GDR |
273 | void |
274 | binding_table_remove_anonymous_types (binding_table table) | |
275 | { | |
276 | const size_t chain_count = table->chain_count; | |
277 | size_t i; | |
278 | ||
279 | for (i = 0; i < chain_count; ++i) | |
280 | { | |
281 | binding_entry *p = &table->chain[i]; | |
282 | ||
283 | while (*p != NULL) | |
284 | if (ANON_AGGRNAME_P ((*p)->name)) | |
285 | { | |
286 | binding_entry e = *p; | |
287 | *p = (*p)->chain; | |
288 | --table->entry_count; | |
289 | binding_entry_free (e); | |
290 | } | |
291 | else | |
292 | p = &(*p)->chain; | |
293 | } | |
294 | } | |
295 | ||
296 | /* Apply PROC -- with DATA -- to all entries in TABLE. */ | |
daafa301 | 297 | |
5e0c54e5 GDR |
298 | void |
299 | binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) | |
300 | { | |
301 | const size_t chain_count = table->chain_count; | |
302 | size_t i; | |
303 | ||
304 | for (i = 0; i < chain_count; ++i) | |
305 | { | |
306 | binding_entry entry = table->chain[i]; | |
307 | for (; entry != NULL; entry = entry->chain) | |
308 | proc (entry, data); | |
309 | } | |
310 | } | |
311 | \f | |
00e8de68 GDR |
312 | #ifndef ENABLE_SCOPE_CHECKING |
313 | # define ENABLE_SCOPE_CHECKING 0 | |
314 | #else | |
315 | # define ENABLE_SCOPE_CHECKING 1 | |
316 | #endif | |
5e0c54e5 | 317 | |
aed81407 | 318 | /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ |
daafa301 | 319 | |
aed81407 GDR |
320 | static GTY((deletable (""))) cxx_binding *free_bindings; |
321 | ||
5a167978 GDR |
322 | /* Zero out a cxx_binding pointed to by B. */ |
323 | #define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding)) | |
324 | ||
aed81407 | 325 | /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ |
daafa301 | 326 | |
00e8de68 | 327 | static cxx_binding * |
aed81407 GDR |
328 | cxx_binding_make (tree value, tree type) |
329 | { | |
330 | cxx_binding *binding; | |
331 | if (free_bindings) | |
332 | { | |
333 | binding = free_bindings; | |
334 | free_bindings = binding->previous; | |
335 | } | |
336 | else | |
7e8f3096 | 337 | binding = ggc_alloc (sizeof (cxx_binding)); |
aed81407 GDR |
338 | |
339 | binding->value = value; | |
340 | binding->type = type; | |
7e8f3096 | 341 | binding->previous = NULL; |
aed81407 GDR |
342 | |
343 | return binding; | |
344 | } | |
345 | ||
346 | /* Put BINDING back on the free list. */ | |
daafa301 | 347 | |
00e8de68 | 348 | static inline void |
aed81407 GDR |
349 | cxx_binding_free (cxx_binding *binding) |
350 | { | |
daafa301 | 351 | binding->scope = NULL; |
aed81407 GDR |
352 | binding->previous = free_bindings; |
353 | free_bindings = binding; | |
354 | } | |
c87ceb13 | 355 | |
00e8de68 GDR |
356 | /* Make DECL the innermost binding for ID. The LEVEL is the binding |
357 | level at which this declaration is being bound. */ | |
358 | ||
359 | static void | |
360 | push_binding (tree id, tree decl, cxx_scope* level) | |
361 | { | |
362 | cxx_binding *binding = cxx_binding_make (decl, NULL); | |
363 | ||
364 | /* Now, fill in the binding information. */ | |
365 | binding->previous = IDENTIFIER_BINDING (id); | |
366 | binding->scope = level; | |
367 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
368 | LOCAL_BINDING_P (binding) = (level != class_binding_level); | |
369 | ||
370 | /* And put it on the front of the list of bindings for ID. */ | |
371 | IDENTIFIER_BINDING (id) = binding; | |
372 | } | |
373 | ||
374 | /* Remove the binding for DECL which should be the innermost binding | |
375 | for ID. */ | |
376 | ||
377 | void | |
378 | pop_binding (tree id, tree decl) | |
379 | { | |
380 | cxx_binding *binding; | |
381 | ||
382 | if (id == NULL_TREE) | |
383 | /* It's easiest to write the loops that call this function without | |
384 | checking whether or not the entities involved have names. We | |
385 | get here for such an entity. */ | |
386 | return; | |
387 | ||
388 | /* Get the innermost binding for ID. */ | |
389 | binding = IDENTIFIER_BINDING (id); | |
390 | ||
391 | /* The name should be bound. */ | |
392 | my_friendly_assert (binding != NULL, 0); | |
393 | ||
394 | /* The DECL will be either the ordinary binding or the type | |
395 | binding for this identifier. Remove that binding. */ | |
396 | if (binding->value == decl) | |
397 | binding->value = NULL_TREE; | |
398 | else if (binding->type == decl) | |
399 | binding->type = NULL_TREE; | |
400 | else | |
401 | abort (); | |
402 | ||
403 | if (!binding->value && !binding->type) | |
404 | { | |
405 | /* We're completely done with the innermost binding for this | |
406 | identifier. Unhook it from the list of bindings. */ | |
407 | IDENTIFIER_BINDING (id) = binding->previous; | |
408 | ||
409 | /* Add it to the free list. */ | |
410 | cxx_binding_free (binding); | |
411 | } | |
412 | } | |
413 | ||
c87ceb13 GDR |
414 | /* BINDING records an existing declaration for a namein the current scope. |
415 | But, DECL is another declaration for that same identifier in the | |
416 | same scope. This is the `struct stat' hack whereby a non-typedef | |
417 | class name or enum-name can be bound at the same level as some other | |
418 | kind of entity. | |
419 | 3.3.7/1 | |
420 | ||
421 | A class name (9.1) or enumeration name (7.2) can be hidden by the | |
422 | name of an object, function, or enumerator declared in the same scope. | |
423 | If a class or enumeration name and an object, function, or enumerator | |
424 | are declared in the same scope (in any order) with the same name, the | |
425 | class or enumeration name is hidden wherever the object, function, or | |
426 | enumerator name is visible. | |
427 | ||
428 | It's the responsibility of the caller to check that | |
429 | inserting this name is valid here. Returns nonzero if the new binding | |
430 | was successful. */ | |
431 | ||
00e8de68 | 432 | static bool |
c87ceb13 GDR |
433 | supplement_binding (cxx_binding *binding, tree decl) |
434 | { | |
435 | tree bval = binding->value; | |
436 | bool ok = true; | |
437 | ||
438 | timevar_push (TV_NAME_LOOKUP); | |
439 | if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) | |
440 | /* The new name is the type name. */ | |
441 | binding->type = decl; | |
64323f62 GDR |
442 | else if (!bval || bval == error_mark_node) |
443 | /* VALUE is null when push_class_level_binding moves an inherited | |
444 | type-binding out of the way to make room for a new value binding. | |
445 | It is an error_mark_node when DECL's name has been used in a | |
446 | non-class scope prior declaration. In that case, we should have | |
447 | already issued a diagnostic; for graceful error recovery purpose, | |
448 | pretend this was the intended declaration for that name. */ | |
c87ceb13 GDR |
449 | binding->value = decl; |
450 | else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)) | |
451 | { | |
452 | /* The old binding was a type name. It was placed in | |
147135cc | 453 | VALUE field because it was thought, at the point it was |
c87ceb13 GDR |
454 | declared, to be the only entity with such a name. Move the |
455 | type name into the type slot; it is now hidden by the new | |
456 | binding. */ | |
457 | binding->type = bval; | |
458 | binding->value = decl; | |
459 | binding->value_is_inherited = false; | |
460 | } | |
461 | else if (TREE_CODE (bval) == TYPE_DECL | |
462 | && TREE_CODE (decl) == TYPE_DECL | |
463 | && DECL_NAME (decl) == DECL_NAME (bval) | |
464 | && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval)) | |
465 | /* If either type involves template parameters, we must | |
466 | wait until instantiation. */ | |
467 | || uses_template_parms (TREE_TYPE (decl)) | |
468 | || uses_template_parms (TREE_TYPE (bval)))) | |
469 | /* We have two typedef-names, both naming the same type to have | |
470 | the same name. This is OK because of: | |
471 | ||
472 | [dcl.typedef] | |
473 | ||
474 | In a given scope, a typedef specifier can be used to redefine | |
475 | the name of any type declared in that scope to refer to the | |
476 | type to which it already refers. */ | |
477 | ok = false; | |
478 | /* There can be two block-scope declarations of the same variable, | |
479 | so long as they are `extern' declarations. However, there cannot | |
480 | be two declarations of the same static data member: | |
481 | ||
482 | [class.mem] | |
483 | ||
484 | A member shall not be declared twice in the | |
485 | member-specification. */ | |
486 | else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL | |
487 | && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval) | |
488 | && !DECL_CLASS_SCOPE_P (decl)) | |
489 | { | |
490 | duplicate_decls (decl, binding->value); | |
491 | ok = false; | |
492 | } | |
493 | else | |
494 | { | |
495 | error ("declaration of `%#D'", decl); | |
496 | cp_error_at ("conflicts with previous declaration `%#D'", | |
497 | binding->value); | |
498 | ok = false; | |
499 | } | |
500 | ||
501 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok); | |
502 | } | |
00e8de68 GDR |
503 | |
504 | /* Add DECL to the list of things declared in B. */ | |
505 | ||
a5e6b29b | 506 | static void |
00e8de68 GDR |
507 | add_decl_to_level (tree decl, cxx_scope *b) |
508 | { | |
509 | if (TREE_CODE (decl) == NAMESPACE_DECL | |
510 | && !DECL_NAMESPACE_ALIAS (decl)) | |
511 | { | |
512 | TREE_CHAIN (decl) = b->namespaces; | |
513 | b->namespaces = decl; | |
514 | } | |
515 | else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)) | |
516 | { | |
517 | TREE_CHAIN (decl) = b->vtables; | |
518 | b->vtables = decl; | |
519 | } | |
520 | else | |
521 | { | |
522 | /* We build up the list in reverse order, and reverse it later if | |
523 | necessary. */ | |
524 | TREE_CHAIN (decl) = b->names; | |
525 | b->names = decl; | |
526 | b->names_size++; | |
527 | ||
f4f206f4 | 528 | /* If appropriate, add decl to separate list of statics. */ |
00e8de68 GDR |
529 | if (b->kind == sk_namespace) |
530 | if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl)) | |
531 | || (TREE_CODE (decl) == FUNCTION_DECL | |
532 | && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)))) | |
533 | VARRAY_PUSH_TREE (b->static_decls, decl); | |
534 | } | |
535 | } | |
536 | ||
a5e6b29b GDR |
537 | /* Record a decl-node X as belonging to the current lexical scope. |
538 | Check for errors (such as an incompatible declaration for the same | |
539 | name already seen in the same scope). | |
540 | ||
541 | Returns either X or an old decl for the same name. | |
542 | If an old decl is returned, it may have been smashed | |
543 | to agree with what X says. */ | |
544 | ||
545 | tree | |
546 | pushdecl (tree x) | |
547 | { | |
926ce8bd KH |
548 | tree t; |
549 | tree name; | |
a5e6b29b GDR |
550 | int need_new_binding; |
551 | ||
552 | timevar_push (TV_NAME_LOOKUP); | |
553 | ||
554 | need_new_binding = 1; | |
555 | ||
556 | if (DECL_TEMPLATE_PARM_P (x)) | |
557 | /* Template parameters have no context; they are not X::T even | |
558 | when declared within a class or namespace. */ | |
559 | ; | |
560 | else | |
561 | { | |
562 | if (current_function_decl && x != current_function_decl | |
563 | /* A local declaration for a function doesn't constitute | |
564 | nesting. */ | |
4656bc85 | 565 | && TREE_CODE (x) != FUNCTION_DECL |
a5e6b29b GDR |
566 | /* A local declaration for an `extern' variable is in the |
567 | scope of the current namespace, not the current | |
568 | function. */ | |
569 | && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) | |
570 | && !DECL_CONTEXT (x)) | |
571 | DECL_CONTEXT (x) = current_function_decl; | |
572 | ||
573 | /* If this is the declaration for a namespace-scope function, | |
574 | but the declaration itself is in a local scope, mark the | |
575 | declaration. */ | |
576 | if (TREE_CODE (x) == FUNCTION_DECL | |
577 | && DECL_NAMESPACE_SCOPE_P (x) | |
578 | && current_function_decl | |
579 | && x != current_function_decl) | |
580 | DECL_LOCAL_FUNCTION_P (x) = 1; | |
581 | } | |
582 | ||
583 | name = DECL_NAME (x); | |
584 | if (name) | |
585 | { | |
586 | int different_binding_level = 0; | |
587 | ||
588 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
589 | name = TREE_OPERAND (name, 0); | |
590 | ||
591 | /* In case this decl was explicitly namespace-qualified, look it | |
592 | up in its namespace context. */ | |
7813d14c | 593 | if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ()) |
a5e6b29b GDR |
594 | t = namespace_binding (name, DECL_CONTEXT (x)); |
595 | else | |
596 | t = lookup_name_current_level (name); | |
597 | ||
598 | /* [basic.link] If there is a visible declaration of an entity | |
599 | with linkage having the same name and type, ignoring entities | |
600 | declared outside the innermost enclosing namespace scope, the | |
601 | block scope declaration declares that same entity and | |
602 | receives the linkage of the previous declaration. */ | |
603 | if (! t && current_function_decl && x != current_function_decl | |
604 | && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) | |
605 | && DECL_EXTERNAL (x)) | |
606 | { | |
607 | /* Look in block scope. */ | |
608 | t = IDENTIFIER_VALUE (name); | |
609 | /* Or in the innermost namespace. */ | |
610 | if (! t) | |
611 | t = namespace_binding (name, DECL_CONTEXT (x)); | |
612 | /* Does it have linkage? Note that if this isn't a DECL, it's an | |
613 | OVERLOAD, which is OK. */ | |
614 | if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) | |
615 | t = NULL_TREE; | |
616 | if (t) | |
617 | different_binding_level = 1; | |
618 | } | |
619 | ||
620 | /* If we are declaring a function, and the result of name-lookup | |
621 | was an OVERLOAD, look for an overloaded instance that is | |
622 | actually the same as the function we are declaring. (If | |
623 | there is one, we have to merge our declaration with the | |
624 | previous declaration.) */ | |
625 | if (t && TREE_CODE (t) == OVERLOAD) | |
626 | { | |
627 | tree match; | |
628 | ||
629 | if (TREE_CODE (x) == FUNCTION_DECL) | |
630 | for (match = t; match; match = OVL_NEXT (match)) | |
631 | { | |
632 | if (decls_match (OVL_CURRENT (match), x)) | |
633 | break; | |
634 | } | |
635 | else | |
636 | /* Just choose one. */ | |
637 | match = t; | |
638 | ||
639 | if (match) | |
640 | t = OVL_CURRENT (match); | |
641 | else | |
642 | t = NULL_TREE; | |
643 | } | |
644 | ||
645 | if (t == error_mark_node) | |
646 | { | |
647 | /* error_mark_node is 0 for a while during initialization! */ | |
648 | t = NULL_TREE; | |
649 | cp_error_at ("`%#D' used prior to declaration", x); | |
650 | } | |
651 | else if (t != NULL_TREE) | |
652 | { | |
653 | if (different_binding_level) | |
654 | { | |
655 | if (decls_match (x, t)) | |
656 | /* The standard only says that the local extern | |
657 | inherits linkage from the previous decl; in | |
658 | particular, default args are not shared. It would | |
659 | be nice to propagate inlining info, though. FIXME. */ | |
660 | TREE_PUBLIC (x) = TREE_PUBLIC (t); | |
661 | } | |
662 | else if (TREE_CODE (t) == PARM_DECL) | |
663 | { | |
664 | if (DECL_CONTEXT (t) == NULL_TREE) | |
665 | /* This is probably caused by too many errors, but calling | |
666 | abort will say that if errors have occurred. */ | |
667 | abort (); | |
668 | ||
669 | /* Check for duplicate params. */ | |
670 | if (duplicate_decls (x, t)) | |
671 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
672 | } | |
673 | else if ((DECL_EXTERN_C_FUNCTION_P (x) | |
674 | || DECL_FUNCTION_TEMPLATE_P (x)) | |
675 | && is_overloaded_fn (t)) | |
676 | /* Don't do anything just yet. */; | |
677 | else if (t == wchar_decl_node) | |
678 | { | |
679 | if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) | |
680 | pedwarn ("redeclaration of `wchar_t' as `%T'", | |
681 | TREE_TYPE (x)); | |
682 | ||
683 | /* Throw away the redeclaration. */ | |
684 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
685 | } | |
b1a19c7c | 686 | else |
a5e6b29b | 687 | { |
b1a19c7c MM |
688 | tree olddecl = duplicate_decls (x, t); |
689 | ||
690 | /* If the redeclaration failed, we can stop at this | |
691 | point. */ | |
692 | if (olddecl == error_mark_node) | |
693 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
694 | ||
695 | if (olddecl) | |
696 | { | |
697 | if (TREE_CODE (t) == TYPE_DECL) | |
698 | SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); | |
699 | else if (TREE_CODE (t) == FUNCTION_DECL) | |
700 | check_default_args (t); | |
a5e6b29b | 701 | |
b1a19c7c MM |
702 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); |
703 | } | |
704 | else if (DECL_MAIN_P (x)) | |
705 | { | |
706 | /* A redeclaration of main, but not a duplicate of the | |
707 | previous one. | |
708 | ||
709 | [basic.start.main] | |
710 | ||
711 | This function shall not be overloaded. */ | |
712 | cp_error_at ("invalid redeclaration of `%D'", t); | |
713 | error ("as `%D'", x); | |
714 | /* We don't try to push this declaration since that | |
715 | causes a crash. */ | |
716 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); | |
717 | } | |
a5e6b29b GDR |
718 | } |
719 | } | |
720 | ||
721 | check_template_shadow (x); | |
722 | ||
723 | /* If this is a function conjured up by the backend, massage it | |
724 | so it looks friendly. */ | |
725 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) | |
726 | { | |
727 | retrofit_lang_decl (x); | |
728 | SET_DECL_LANGUAGE (x, lang_c); | |
729 | } | |
730 | ||
731 | if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) | |
732 | { | |
733 | t = push_overloaded_decl (x, PUSH_LOCAL); | |
734 | if (t != x) | |
735 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
736 | if (!namespace_bindings_p ()) | |
737 | /* We do not need to create a binding for this name; | |
738 | push_overloaded_decl will have already done so if | |
739 | necessary. */ | |
740 | need_new_binding = 0; | |
741 | } | |
742 | else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) | |
743 | { | |
744 | t = push_overloaded_decl (x, PUSH_GLOBAL); | |
745 | if (t == x) | |
746 | add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); | |
747 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
748 | } | |
749 | ||
750 | /* If declaring a type as a typedef, copy the type (unless we're | |
751 | at line 0), and install this TYPE_DECL as the new type's typedef | |
752 | name. See the extensive comment in ../c-decl.c (pushdecl). */ | |
753 | if (TREE_CODE (x) == TYPE_DECL) | |
754 | { | |
755 | tree type = TREE_TYPE (x); | |
756 | if (DECL_SOURCE_LINE (x) == 0) | |
757 | { | |
758 | if (TYPE_NAME (type) == 0) | |
759 | TYPE_NAME (type) = x; | |
760 | } | |
761 | else if (type != error_mark_node && TYPE_NAME (type) != x | |
762 | /* We don't want to copy the type when all we're | |
763 | doing is making a TYPE_DECL for the purposes of | |
764 | inlining. */ | |
765 | && (!TYPE_NAME (type) | |
766 | || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) | |
767 | { | |
768 | DECL_ORIGINAL_TYPE (x) = type; | |
769 | type = build_type_copy (type); | |
770 | TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); | |
771 | TYPE_NAME (type) = x; | |
772 | TREE_TYPE (x) = type; | |
773 | } | |
774 | ||
775 | if (type != error_mark_node | |
776 | && TYPE_NAME (type) | |
777 | && TYPE_IDENTIFIER (type)) | |
778 | set_identifier_type_value (DECL_NAME (x), x); | |
779 | } | |
780 | ||
781 | /* Multiple external decls of the same identifier ought to match. | |
782 | ||
783 | We get warnings about inline functions where they are defined. | |
784 | We get warnings about other functions from push_overloaded_decl. | |
785 | ||
786 | Avoid duplicate warnings where they are used. */ | |
787 | if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) | |
788 | { | |
789 | tree decl; | |
790 | ||
791 | decl = IDENTIFIER_NAMESPACE_VALUE (name); | |
792 | if (decl && TREE_CODE (decl) == OVERLOAD) | |
793 | decl = OVL_FUNCTION (decl); | |
794 | ||
795 | if (decl && decl != error_mark_node | |
796 | && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) | |
797 | /* If different sort of thing, we already gave an error. */ | |
798 | && TREE_CODE (decl) == TREE_CODE (x) | |
799 | && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) | |
800 | { | |
801 | pedwarn ("type mismatch with previous external decl of `%#D'", x); | |
802 | cp_pedwarn_at ("previous external decl of `%#D'", decl); | |
803 | } | |
804 | } | |
805 | ||
806 | /* This name is new in its binding level. | |
807 | Install the new declaration and return it. */ | |
808 | if (namespace_bindings_p ()) | |
809 | { | |
810 | /* Install a global value. */ | |
811 | ||
812 | /* If the first global decl has external linkage, | |
813 | warn if we later see static one. */ | |
814 | if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) | |
815 | TREE_PUBLIC (name) = 1; | |
816 | ||
817 | /* Bind the name for the entity. */ | |
818 | if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) | |
819 | && t != NULL_TREE) | |
820 | && (TREE_CODE (x) == TYPE_DECL | |
821 | || TREE_CODE (x) == VAR_DECL | |
822 | || TREE_CODE (x) == ALIAS_DECL | |
823 | || TREE_CODE (x) == NAMESPACE_DECL | |
824 | || TREE_CODE (x) == CONST_DECL | |
825 | || TREE_CODE (x) == TEMPLATE_DECL)) | |
826 | SET_IDENTIFIER_NAMESPACE_VALUE (name, x); | |
827 | ||
828 | /* Don't forget if the function was used via an implicit decl. */ | |
829 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
830 | && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name))) | |
831 | TREE_USED (x) = 1; | |
832 | ||
833 | /* Don't forget if its address was taken in that way. */ | |
834 | if (IDENTIFIER_IMPLICIT_DECL (name) | |
835 | && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name))) | |
836 | TREE_ADDRESSABLE (x) = 1; | |
837 | ||
838 | /* Warn about mismatches against previous implicit decl. */ | |
839 | if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE | |
840 | /* If this real decl matches the implicit, don't complain. */ | |
841 | && ! (TREE_CODE (x) == FUNCTION_DECL | |
842 | && TREE_TYPE (TREE_TYPE (x)) == integer_type_node)) | |
843 | warning | |
844 | ("`%D' was previously implicitly declared to return `int'", x); | |
845 | ||
846 | /* If new decl is `static' and an `extern' was seen previously, | |
847 | warn about it. */ | |
848 | if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) | |
849 | warn_extern_redeclared_static (x, t); | |
850 | } | |
851 | else | |
852 | { | |
853 | /* Here to install a non-global value. */ | |
854 | tree oldlocal = IDENTIFIER_VALUE (name); | |
855 | tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); | |
856 | ||
857 | if (need_new_binding) | |
858 | { | |
859 | push_local_binding (name, x, 0); | |
860 | /* Because push_local_binding will hook X on to the | |
861 | current_binding_level's name list, we don't want to | |
862 | do that again below. */ | |
863 | need_new_binding = 0; | |
864 | } | |
865 | ||
866 | /* If this is a TYPE_DECL, push it into the type value slot. */ | |
867 | if (TREE_CODE (x) == TYPE_DECL) | |
868 | set_identifier_type_value (name, x); | |
869 | ||
870 | /* Clear out any TYPE_DECL shadowed by a namespace so that | |
871 | we won't think this is a type. The C struct hack doesn't | |
872 | go through namespaces. */ | |
873 | if (TREE_CODE (x) == NAMESPACE_DECL) | |
874 | set_identifier_type_value (name, NULL_TREE); | |
875 | ||
876 | if (oldlocal) | |
877 | { | |
878 | tree d = oldlocal; | |
879 | ||
880 | while (oldlocal | |
881 | && TREE_CODE (oldlocal) == VAR_DECL | |
882 | && DECL_DEAD_FOR_LOCAL (oldlocal)) | |
883 | oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); | |
884 | ||
885 | if (oldlocal == NULL_TREE) | |
886 | oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); | |
887 | } | |
888 | ||
889 | /* If this is an extern function declaration, see if we | |
890 | have a global definition or declaration for the function. */ | |
891 | if (oldlocal == NULL_TREE | |
892 | && DECL_EXTERNAL (x) | |
893 | && oldglobal != NULL_TREE | |
894 | && TREE_CODE (x) == FUNCTION_DECL | |
895 | && TREE_CODE (oldglobal) == FUNCTION_DECL) | |
896 | { | |
897 | /* We have one. Their types must agree. */ | |
898 | if (decls_match (x, oldglobal)) | |
899 | /* OK */; | |
900 | else | |
901 | { | |
902 | warning ("extern declaration of `%#D' doesn't match", x); | |
903 | cp_warning_at ("global declaration `%#D'", oldglobal); | |
904 | } | |
905 | } | |
906 | /* If we have a local external declaration, | |
907 | and no file-scope declaration has yet been seen, | |
908 | then if we later have a file-scope decl it must not be static. */ | |
909 | if (oldlocal == NULL_TREE | |
910 | && oldglobal == NULL_TREE | |
911 | && DECL_EXTERNAL (x) | |
912 | && TREE_PUBLIC (x)) | |
913 | TREE_PUBLIC (name) = 1; | |
914 | ||
915 | /* Warn if shadowing an argument at the top level of the body. */ | |
916 | if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) | |
917 | /* Inline decls shadow nothing. */ | |
918 | && !DECL_FROM_INLINE (x) | |
919 | && TREE_CODE (oldlocal) == PARM_DECL | |
920 | /* Don't check the `this' parameter. */ | |
921 | && !DECL_ARTIFICIAL (oldlocal)) | |
922 | { | |
923 | bool err = false; | |
924 | ||
925 | /* Don't complain if it's from an enclosing function. */ | |
926 | if (DECL_CONTEXT (oldlocal) == current_function_decl | |
927 | && TREE_CODE (x) != PARM_DECL) | |
928 | { | |
929 | /* Go to where the parms should be and see if we find | |
930 | them there. */ | |
931 | struct cp_binding_level *b = current_binding_level->level_chain; | |
932 | ||
933 | /* Skip the ctor/dtor cleanup level. */ | |
934 | b = b->level_chain; | |
935 | ||
936 | /* ARM $8.3 */ | |
937 | if (b->kind == sk_function_parms) | |
938 | { | |
939 | error ("declaration of `%#D' shadows a parameter", | |
940 | name); | |
941 | err = true; | |
942 | } | |
943 | } | |
944 | ||
945 | if (warn_shadow && !err) | |
946 | shadow_warning (SW_PARAM, | |
947 | IDENTIFIER_POINTER (name), oldlocal); | |
948 | } | |
949 | ||
950 | /* Maybe warn if shadowing something else. */ | |
951 | else if (warn_shadow && !DECL_EXTERNAL (x) | |
952 | /* No shadow warnings for internally generated vars. */ | |
953 | && ! DECL_ARTIFICIAL (x) | |
954 | /* No shadow warnings for vars made for inlining. */ | |
955 | && ! DECL_FROM_INLINE (x)) | |
956 | { | |
957 | if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE | |
958 | && current_class_ptr | |
959 | && !TREE_STATIC (name)) | |
960 | warning ("declaration of `%s' shadows a member of `this'", | |
961 | IDENTIFIER_POINTER (name)); | |
962 | else if (oldlocal != NULL_TREE | |
963 | && TREE_CODE (oldlocal) == VAR_DECL) | |
964 | shadow_warning (SW_LOCAL, | |
965 | IDENTIFIER_POINTER (name), oldlocal); | |
966 | else if (oldglobal != NULL_TREE | |
967 | && TREE_CODE (oldglobal) == VAR_DECL) | |
968 | /* XXX shadow warnings in outer-more namespaces */ | |
969 | shadow_warning (SW_GLOBAL, | |
970 | IDENTIFIER_POINTER (name), oldglobal); | |
971 | } | |
972 | } | |
973 | ||
974 | if (TREE_CODE (x) == FUNCTION_DECL) | |
975 | check_default_args (x); | |
976 | ||
977 | if (TREE_CODE (x) == VAR_DECL) | |
978 | maybe_register_incomplete_var (x); | |
979 | } | |
980 | ||
981 | if (need_new_binding) | |
982 | add_decl_to_level (x, | |
983 | DECL_NAMESPACE_SCOPE_P (x) | |
984 | ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) | |
985 | : current_binding_level); | |
986 | ||
987 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); | |
988 | } | |
989 | ||
990 | /* Enter DECL into the symbol table, if that's appropriate. Returns | |
991 | DECL, or a modified version thereof. */ | |
992 | ||
993 | tree | |
994 | maybe_push_decl (tree decl) | |
995 | { | |
996 | tree type = TREE_TYPE (decl); | |
997 | ||
998 | /* Add this decl to the current binding level, but not if it comes | |
999 | from another scope, e.g. a static member variable. TEM may equal | |
1000 | DECL or it may be a previous decl of the same name. */ | |
1001 | if (decl == error_mark_node | |
1002 | || (TREE_CODE (decl) != PARM_DECL | |
1003 | && DECL_CONTEXT (decl) != NULL_TREE | |
1004 | /* Definitions of namespace members outside their namespace are | |
1005 | possible. */ | |
1006 | && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) | |
1007 | || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) | |
1008 | || TREE_CODE (type) == UNKNOWN_TYPE | |
1009 | /* The declaration of a template specialization does not affect | |
1010 | the functions available for overload resolution, so we do not | |
1011 | call pushdecl. */ | |
1012 | || (TREE_CODE (decl) == FUNCTION_DECL | |
1013 | && DECL_TEMPLATE_SPECIALIZATION (decl))) | |
1014 | return decl; | |
1015 | else | |
1016 | return pushdecl (decl); | |
1017 | } | |
1018 | ||
00e8de68 GDR |
1019 | /* Bind DECL to ID in the current_binding_level, assumed to be a local |
1020 | binding level. If PUSH_USING is set in FLAGS, we know that DECL | |
1021 | doesn't really belong to this binding level, that it got here | |
1022 | through a using-declaration. */ | |
1023 | ||
a5e6b29b | 1024 | static void |
00e8de68 GDR |
1025 | push_local_binding (tree id, tree decl, int flags) |
1026 | { | |
1027 | struct cp_binding_level *b; | |
1028 | ||
1029 | /* Skip over any local classes. This makes sense if we call | |
1030 | push_local_binding with a friend decl of a local class. */ | |
1031 | b = innermost_nonclass_level (); | |
1032 | ||
1033 | if (lookup_name_current_level (id)) | |
1034 | { | |
1035 | /* Supplement the existing binding. */ | |
1036 | if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) | |
1037 | /* It didn't work. Something else must be bound at this | |
1038 | level. Do not add DECL to the list of things to pop | |
1039 | later. */ | |
1040 | return; | |
1041 | } | |
1042 | else | |
1043 | /* Create a new binding. */ | |
1044 | push_binding (id, decl, b); | |
1045 | ||
1046 | if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) | |
1047 | /* We must put the OVERLOAD into a TREE_LIST since the | |
1048 | TREE_CHAIN of an OVERLOAD is already used. Similarly for | |
1049 | decls that got here through a using-declaration. */ | |
1050 | decl = build_tree_list (NULL_TREE, decl); | |
1051 | ||
1052 | /* And put DECL on the list of things declared by the current | |
1053 | binding level. */ | |
1054 | add_decl_to_level (decl, b); | |
1055 | } | |
a5e6b29b GDR |
1056 | |
1057 | /* The old ARM scoping rules injected variables declared in the | |
1058 | initialization statement of a for-statement into the surrounding | |
1059 | scope. We support this usage, in order to be backward-compatible. | |
1060 | DECL is a just-declared VAR_DECL; if necessary inject its | |
1061 | declaration into the surrounding scope. */ | |
1062 | ||
1063 | void | |
1064 | maybe_inject_for_scope_var (tree decl) | |
1065 | { | |
1066 | timevar_push (TV_NAME_LOOKUP); | |
1067 | if (!DECL_NAME (decl)) | |
1068 | { | |
1069 | timevar_pop (TV_NAME_LOOKUP); | |
1070 | return; | |
1071 | } | |
1072 | ||
1073 | /* Declarations of __FUNCTION__ and its ilk appear magically when | |
1074 | the variable is first used. If that happens to be inside a | |
1075 | for-loop, we don't want to do anything special. */ | |
1076 | if (DECL_PRETTY_FUNCTION_P (decl)) | |
1077 | { | |
1078 | timevar_pop (TV_NAME_LOOKUP); | |
1079 | return; | |
1080 | } | |
1081 | ||
1082 | if (current_binding_level->kind == sk_for) | |
1083 | { | |
1084 | struct cp_binding_level *outer | |
1085 | = current_binding_level->level_chain; | |
1086 | ||
1087 | /* Check to see if the same name is already bound at the outer | |
1088 | level, either because it was directly declared, or because a | |
1089 | dead for-decl got preserved. In either case, the code would | |
1090 | not have been valid under the ARM scope rules, so clear | |
1091 | is_for_scope for the current_binding_level. | |
1092 | ||
1093 | Otherwise, we need to preserve the temp slot for decl to last | |
1094 | into the outer binding level. */ | |
1095 | ||
1096 | cxx_binding *outer_binding | |
1097 | = IDENTIFIER_BINDING (DECL_NAME (decl))->previous; | |
1098 | ||
1099 | if (outer_binding && outer_binding->scope == outer | |
1100 | && (TREE_CODE (outer_binding->value) == VAR_DECL) | |
1101 | && DECL_DEAD_FOR_LOCAL (outer_binding->value)) | |
1102 | { | |
1103 | outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value); | |
1104 | current_binding_level->kind = sk_block; | |
1105 | } | |
1106 | } | |
1107 | timevar_pop (TV_NAME_LOOKUP); | |
1108 | } | |
1109 | ||
1110 | /* Check to see whether or not DECL is a variable that would have been | |
1111 | in scope under the ARM, but is not in scope under the ANSI/ISO | |
1112 | standard. If so, issue an error message. If name lookup would | |
1113 | work in both cases, but return a different result, this function | |
1114 | returns the result of ANSI/ISO lookup. Otherwise, it returns | |
1115 | DECL. */ | |
1116 | ||
1117 | tree | |
1118 | check_for_out_of_scope_variable (tree decl) | |
1119 | { | |
1120 | tree shadowed; | |
1121 | ||
1122 | /* We only care about out of scope variables. */ | |
1123 | if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl))) | |
1124 | return decl; | |
1125 | ||
1126 | shadowed = DECL_SHADOWED_FOR_VAR (decl); | |
1127 | while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL | |
1128 | && DECL_DEAD_FOR_LOCAL (shadowed)) | |
1129 | shadowed = DECL_SHADOWED_FOR_VAR (shadowed); | |
1130 | if (!shadowed) | |
1131 | shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); | |
1132 | if (shadowed) | |
1133 | { | |
1134 | if (!DECL_ERROR_REPORTED (decl)) | |
1135 | { | |
1136 | warning ("name lookup of `%D' changed", | |
1137 | DECL_NAME (decl)); | |
1138 | cp_warning_at (" matches this `%D' under ISO standard rules", | |
1139 | shadowed); | |
1140 | cp_warning_at (" matches this `%D' under old rules", decl); | |
1141 | DECL_ERROR_REPORTED (decl) = 1; | |
1142 | } | |
1143 | return shadowed; | |
1144 | } | |
1145 | ||
1146 | /* If we have already complained about this declaration, there's no | |
1147 | need to do it again. */ | |
1148 | if (DECL_ERROR_REPORTED (decl)) | |
1149 | return decl; | |
1150 | ||
1151 | DECL_ERROR_REPORTED (decl) = 1; | |
1152 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) | |
1153 | { | |
1154 | error ("name lookup of `%D' changed for new ISO `for' scoping", | |
1155 | DECL_NAME (decl)); | |
1156 | cp_error_at (" cannot use obsolete binding at `%D' because it has a destructor", decl); | |
1157 | return error_mark_node; | |
1158 | } | |
1159 | else | |
1160 | { | |
1161 | pedwarn ("name lookup of `%D' changed for new ISO `for' scoping", | |
1162 | DECL_NAME (decl)); | |
1163 | cp_pedwarn_at (" using obsolete binding at `%D'", decl); | |
1164 | } | |
1165 | ||
1166 | return decl; | |
1167 | } | |
00e8de68 GDR |
1168 | \f |
1169 | /* true means unconditionally make a BLOCK for the next level pushed. */ | |
1170 | ||
1171 | static bool keep_next_level_flag; | |
1172 | ||
1173 | static int binding_depth = 0; | |
1174 | static int is_class_level = 0; | |
1175 | ||
1176 | static void | |
1177 | indent (int depth) | |
1178 | { | |
1179 | int i; | |
1180 | ||
1181 | for (i = 0; i < depth * 2; i++) | |
1182 | putc (' ', stderr); | |
1183 | } | |
1184 | ||
1185 | /* Return a string describing the kind of SCOPE we have. */ | |
1186 | static const char * | |
1187 | cxx_scope_descriptor (cxx_scope *scope) | |
1188 | { | |
1189 | /* The order of this table must match the "scope_kind" | |
1190 | enumerators. */ | |
1191 | static const char* scope_kind_names[] = { | |
1192 | "block-scope", | |
1193 | "cleanup-scope", | |
1194 | "try-scope", | |
1195 | "catch-scope", | |
1196 | "for-scope", | |
1197 | "function-parameter-scope", | |
1198 | "class-scope", | |
1199 | "namespace-scope", | |
1200 | "template-parameter-scope", | |
1201 | "template-explicit-spec-scope" | |
1202 | }; | |
1203 | const scope_kind kind = scope->explicit_spec_p | |
1204 | ? sk_template_spec : scope->kind; | |
1205 | ||
1206 | return scope_kind_names[kind]; | |
1207 | } | |
1208 | ||
cd0be382 | 1209 | /* Output a debugging information about SCOPE when performing |
00e8de68 GDR |
1210 | ACTION at LINE. */ |
1211 | static void | |
1212 | cxx_scope_debug (cxx_scope *scope, int line, const char *action) | |
1213 | { | |
1214 | const char *desc = cxx_scope_descriptor (scope); | |
1215 | if (scope->this_entity) | |
1216 | verbatim ("%s %s(%E) %p %d\n", action, desc, | |
1217 | scope->this_entity, (void *) scope, line); | |
1218 | else | |
1219 | verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); | |
1220 | } | |
1221 | ||
1222 | /* Return the estimated initial size of the hashtable of a NAMESPACE | |
1223 | scope. */ | |
1224 | ||
1225 | static inline size_t | |
1226 | namespace_scope_ht_size (tree ns) | |
1227 | { | |
1228 | tree name = DECL_NAME (ns); | |
1229 | ||
1230 | return name == std_identifier | |
1231 | ? NAMESPACE_STD_HT_SIZE | |
1232 | : (name == global_scope_name | |
1233 | ? GLOBAL_SCOPE_HT_SIZE | |
1234 | : NAMESPACE_ORDINARY_HT_SIZE); | |
1235 | } | |
1236 | ||
1237 | /* A chain of binding_level structures awaiting reuse. */ | |
1238 | ||
1239 | static GTY((deletable (""))) struct cp_binding_level *free_binding_level; | |
1240 | ||
1241 | /* Create a new KIND scope and make it the top of the active scopes stack. | |
1242 | ENTITY is the scope of the associated C++ entity (namespace, class, | |
1243 | function); it is NULL otherwise. */ | |
1244 | ||
1245 | cxx_scope * | |
1246 | begin_scope (scope_kind kind, tree entity) | |
1247 | { | |
1248 | cxx_scope *scope; | |
1249 | ||
1250 | /* Reuse or create a struct for this binding level. */ | |
1251 | if (!ENABLE_SCOPE_CHECKING && free_binding_level) | |
1252 | { | |
1253 | scope = free_binding_level; | |
1254 | free_binding_level = scope->level_chain; | |
1255 | } | |
1256 | else | |
1257 | scope = ggc_alloc (sizeof (cxx_scope)); | |
1258 | memset (scope, 0, sizeof (cxx_scope)); | |
1259 | ||
1260 | scope->this_entity = entity; | |
1261 | scope->more_cleanups_ok = true; | |
1262 | switch (kind) | |
1263 | { | |
1264 | case sk_cleanup: | |
1265 | scope->keep = true; | |
1266 | break; | |
1267 | ||
1268 | case sk_template_spec: | |
1269 | scope->explicit_spec_p = true; | |
1270 | kind = sk_template_parms; | |
f4f206f4 | 1271 | /* Fall through. */ |
00e8de68 GDR |
1272 | case sk_template_parms: |
1273 | case sk_block: | |
1274 | case sk_try: | |
1275 | case sk_catch: | |
1276 | case sk_for: | |
1277 | case sk_class: | |
1278 | case sk_function_parms: | |
1279 | scope->keep = keep_next_level_flag; | |
1280 | break; | |
1281 | ||
1282 | case sk_namespace: | |
1283 | scope->type_decls = binding_table_new (namespace_scope_ht_size (entity)); | |
1284 | NAMESPACE_LEVEL (entity) = scope; | |
1285 | VARRAY_TREE_INIT (scope->static_decls, | |
1286 | DECL_NAME (entity) == std_identifier | |
1287 | || DECL_NAME (entity) == global_scope_name | |
1288 | ? 200 : 10, | |
1289 | "Static declarations"); | |
1290 | break; | |
1291 | ||
1292 | default: | |
1293 | /* Should not happen. */ | |
1294 | my_friendly_assert (false, 20030922); | |
1295 | break; | |
1296 | } | |
1297 | scope->kind = kind; | |
1298 | ||
1299 | /* Add it to the front of currently active scopes stack. */ | |
1300 | scope->level_chain = current_binding_level; | |
1301 | current_binding_level = scope; | |
1302 | keep_next_level_flag = false; | |
1303 | ||
1304 | if (ENABLE_SCOPE_CHECKING) | |
1305 | { | |
1306 | scope->binding_depth = binding_depth; | |
1307 | indent (binding_depth); | |
1308 | cxx_scope_debug (scope, input_location.line, "push"); | |
1309 | is_class_level = 0; | |
1310 | binding_depth++; | |
1311 | } | |
1312 | ||
1313 | return scope; | |
1314 | } | |
1315 | ||
1316 | /* We're about to leave current scope. Pop the top of the stack of | |
1317 | currently active scopes. Return the enclosing scope, now active. */ | |
1318 | ||
1319 | cxx_scope * | |
1320 | leave_scope (void) | |
1321 | { | |
1322 | cxx_scope *scope = current_binding_level; | |
1323 | ||
1324 | if (scope->kind == sk_namespace && class_binding_level) | |
1325 | current_binding_level = class_binding_level; | |
1326 | ||
1327 | /* We cannot leave a scope, if there are none left. */ | |
1328 | if (NAMESPACE_LEVEL (global_namespace)) | |
1329 | my_friendly_assert (!global_scope_p (scope), 20030527); | |
1330 | ||
1331 | if (ENABLE_SCOPE_CHECKING) | |
1332 | { | |
1333 | indent (--binding_depth); | |
1334 | cxx_scope_debug (scope, input_location.line, "leave"); | |
1335 | if (is_class_level != (scope == class_binding_level)) | |
1336 | { | |
1337 | indent (binding_depth); | |
1338 | verbatim ("XXX is_class_level != (current_scope == class_scope)\n"); | |
1339 | } | |
1340 | is_class_level = 0; | |
1341 | } | |
1342 | ||
1343 | /* Move one nesting level up. */ | |
1344 | current_binding_level = scope->level_chain; | |
1345 | ||
1346 | /* Namespace-scopes are left most probably temporarily, not completely; | |
1347 | they can be reopen later, e.g. in namespace-extension or any name | |
cd0be382 | 1348 | binding activity that requires us to resume a namespace. For other |
00e8de68 GDR |
1349 | scopes, we just make the structure available for reuse. */ |
1350 | if (scope->kind != sk_namespace) | |
1351 | { | |
1352 | scope->level_chain = free_binding_level; | |
1353 | if (scope->kind == sk_class) | |
1354 | scope->type_decls = NULL; | |
1355 | else | |
1356 | binding_table_free (scope->type_decls); | |
1357 | my_friendly_assert (!ENABLE_SCOPE_CHECKING | |
1358 | || scope->binding_depth == binding_depth, | |
1359 | 20030529); | |
1360 | free_binding_level = scope; | |
1361 | } | |
1362 | ||
1363 | /* Find the innermost enclosing class scope, and reset | |
1364 | CLASS_BINDING_LEVEL appropriately. */ | |
1365 | for (scope = current_binding_level; | |
1366 | scope && scope->kind != sk_class; | |
1367 | scope = scope->level_chain) | |
1368 | ; | |
1369 | class_binding_level = scope && scope->kind == sk_class ? scope : NULL; | |
1370 | ||
1371 | return current_binding_level; | |
1372 | } | |
1373 | ||
1374 | static void | |
1375 | resume_scope (struct cp_binding_level* b) | |
1376 | { | |
1377 | /* Resuming binding levels is meant only for namespaces, | |
1378 | and those cannot nest into classes. */ | |
1379 | my_friendly_assert(!class_binding_level, 386); | |
1380 | /* Also, resuming a non-directly nested namespace is a no-no. */ | |
1381 | my_friendly_assert(b->level_chain == current_binding_level, 386); | |
1382 | current_binding_level = b; | |
1383 | if (ENABLE_SCOPE_CHECKING) | |
1384 | { | |
1385 | b->binding_depth = binding_depth; | |
1386 | indent (binding_depth); | |
1387 | cxx_scope_debug (b, input_location.line, "resume"); | |
1388 | is_class_level = 0; | |
1389 | binding_depth++; | |
1390 | } | |
1391 | } | |
1392 | ||
1393 | /* Return the innermost binding level that is not for a class scope. */ | |
1394 | ||
1395 | static cxx_scope * | |
1396 | innermost_nonclass_level (void) | |
1397 | { | |
1398 | cxx_scope *b; | |
1399 | ||
1400 | b = current_binding_level; | |
1401 | while (b->kind == sk_class) | |
1402 | b = b->level_chain; | |
1403 | ||
1404 | return b; | |
1405 | } | |
1406 | ||
1407 | /* We're defining an object of type TYPE. If it needs a cleanup, but | |
1408 | we're not allowed to add any more objects with cleanups to the current | |
1409 | scope, create a new binding level. */ | |
1410 | ||
1411 | void | |
1412 | maybe_push_cleanup_level (tree type) | |
1413 | { | |
1414 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
1415 | && current_binding_level->more_cleanups_ok == 0) | |
1416 | { | |
1417 | begin_scope (sk_cleanup, NULL); | |
1418 | clear_last_expr (); | |
1419 | add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1); | |
1420 | } | |
1421 | } | |
1422 | ||
1423 | /* Nonzero if we are currently in the global binding level. */ | |
1424 | ||
1425 | int | |
1426 | global_bindings_p (void) | |
1427 | { | |
1428 | return global_scope_p (current_binding_level); | |
1429 | } | |
1430 | ||
1431 | /* True if we are currently in a toplevel binding level. This | |
1432 | means either the global binding level or a namespace in a toplevel | |
1433 | binding level. Since there are no non-toplevel namespace levels, | |
1434 | this really means any namespace or template parameter level. We | |
1435 | also include a class whose context is toplevel. */ | |
1436 | ||
1437 | bool | |
1438 | toplevel_bindings_p (void) | |
1439 | { | |
1440 | struct cp_binding_level *b = innermost_nonclass_level (); | |
1441 | ||
1442 | return b->kind == sk_namespace || b->kind == sk_template_parms; | |
1443 | } | |
1444 | ||
1445 | /* True if this is a namespace scope, or if we are defining a class | |
1446 | which is itself at namespace scope, or whose enclosing class is | |
1447 | such a class, etc. */ | |
1448 | ||
1449 | bool | |
1450 | namespace_bindings_p (void) | |
1451 | { | |
1452 | struct cp_binding_level *b = innermost_nonclass_level (); | |
1453 | ||
1454 | return b->kind == sk_namespace; | |
1455 | } | |
1456 | ||
1457 | /* True if the current level needs to have a BLOCK made. */ | |
1458 | ||
1459 | bool | |
1460 | kept_level_p (void) | |
1461 | { | |
1462 | return (current_binding_level->blocks != NULL_TREE | |
1463 | || current_binding_level->keep | |
1464 | || current_binding_level->kind == sk_cleanup | |
1465 | || current_binding_level->names != NULL_TREE | |
1466 | || current_binding_level->type_decls != NULL); | |
1467 | } | |
1468 | ||
1469 | /* Returns the kind of the innermost scope. */ | |
1470 | ||
1471 | scope_kind | |
1472 | innermost_scope_kind (void) | |
1473 | { | |
1474 | return current_binding_level->kind; | |
1475 | } | |
1476 | ||
1477 | /* Returns true if this scope was created to store template parameters. */ | |
1478 | ||
1479 | bool | |
1480 | template_parm_scope_p (void) | |
1481 | { | |
1482 | return innermost_scope_kind () == sk_template_parms; | |
1483 | } | |
1484 | ||
1485 | /* If KEEP is true, make a BLOCK node for the next binding level, | |
1486 | unconditionally. Otherwise, use the normal logic to decide whether | |
1487 | or not to create a BLOCK. */ | |
1488 | ||
1489 | void | |
1490 | keep_next_level (bool keep) | |
1491 | { | |
1492 | keep_next_level_flag = keep; | |
1493 | } | |
1494 | ||
1495 | /* Return the list of declarations of the current level. | |
1496 | Note that this list is in reverse order unless/until | |
1497 | you nreverse it; and when you do nreverse it, you must | |
1498 | store the result back using `storedecls' or you will lose. */ | |
1499 | ||
1500 | tree | |
1501 | getdecls (void) | |
1502 | { | |
1503 | return current_binding_level->names; | |
1504 | } | |
1505 | ||
1506 | /* Set the current binding TABLE for type declarations.. This is a | |
1507 | temporary workaround of the fact that the data structure classtypes | |
1508 | does not currently carry its allocated cxx_scope structure. */ | |
1509 | void | |
1510 | cxx_remember_type_decls (binding_table table) | |
1511 | { | |
1512 | current_binding_level->type_decls = table; | |
1513 | } | |
1514 | ||
1515 | /* For debugging. */ | |
1516 | static int no_print_functions = 0; | |
1517 | static int no_print_builtins = 0; | |
1518 | ||
1519 | /* Called from print_binding_level through binding_table_foreach to | |
1520 | print the content of binding ENTRY. DATA is a pointer to line offset | |
1521 | marker. */ | |
1522 | static void | |
1523 | bt_print_entry (binding_entry entry, void *data) | |
1524 | { | |
1525 | int *p = (int *) data; | |
1526 | int len; | |
1527 | ||
1528 | if (entry->name == NULL) | |
1529 | len = 3; | |
1530 | else if (entry->name == TYPE_IDENTIFIER (entry->type)) | |
1531 | len = 2; | |
1532 | else | |
1533 | len = 4; | |
1534 | len = 4; | |
1535 | ||
1536 | *p += len; | |
1537 | ||
1538 | if (*p > 5) | |
1539 | { | |
1540 | fprintf (stderr, "\n\t"); | |
1541 | *p = len; | |
1542 | } | |
1543 | if (entry->name == NULL) | |
1544 | { | |
1545 | print_node_brief (stderr, "<unnamed-typedef", entry->type, 0); | |
1546 | fprintf (stderr, ">"); | |
1547 | } | |
1548 | else if (entry->name == TYPE_IDENTIFIER (entry->type)) | |
1549 | print_node_brief (stderr, "", entry->type, 0); | |
1550 | else | |
1551 | { | |
1552 | print_node_brief (stderr, "<typedef", entry->name, 0); | |
1553 | print_node_brief (stderr, "", entry->type, 0); | |
1554 | fprintf (stderr, ">"); | |
1555 | } | |
1556 | } | |
1557 | ||
1558 | void | |
1559 | print_binding_level (struct cp_binding_level* lvl) | |
1560 | { | |
1561 | tree t; | |
1562 | int i = 0, len; | |
1563 | fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks); | |
1564 | if (lvl->more_cleanups_ok) | |
1565 | fprintf (stderr, " more-cleanups-ok"); | |
1566 | if (lvl->have_cleanups) | |
1567 | fprintf (stderr, " have-cleanups"); | |
1568 | fprintf (stderr, "\n"); | |
1569 | if (lvl->names) | |
1570 | { | |
1571 | fprintf (stderr, " names:\t"); | |
1572 | /* We can probably fit 3 names to a line? */ | |
1573 | for (t = lvl->names; t; t = TREE_CHAIN (t)) | |
1574 | { | |
1575 | if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) | |
1576 | continue; | |
1577 | if (no_print_builtins | |
1578 | && (TREE_CODE (t) == TYPE_DECL) | |
1579 | && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>"))) | |
1580 | continue; | |
1581 | ||
1582 | /* Function decls tend to have longer names. */ | |
1583 | if (TREE_CODE (t) == FUNCTION_DECL) | |
1584 | len = 3; | |
1585 | else | |
1586 | len = 2; | |
1587 | i += len; | |
1588 | if (i > 6) | |
1589 | { | |
1590 | fprintf (stderr, "\n\t"); | |
1591 | i = len; | |
1592 | } | |
1593 | print_node_brief (stderr, "", t, 0); | |
1594 | if (t == error_mark_node) | |
1595 | break; | |
1596 | } | |
1597 | if (i) | |
1598 | fprintf (stderr, "\n"); | |
1599 | } | |
1600 | if (lvl->type_decls) | |
1601 | { | |
1602 | fprintf (stderr, " tags:\t"); | |
1603 | i = 0; | |
1604 | binding_table_foreach (lvl->type_decls, bt_print_entry, &i); | |
1605 | if (i) | |
1606 | fprintf (stderr, "\n"); | |
1607 | } | |
1608 | if (lvl->class_shadowed) | |
1609 | { | |
1610 | fprintf (stderr, " class-shadowed:"); | |
1611 | for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t)) | |
1612 | { | |
1613 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
1614 | } | |
1615 | fprintf (stderr, "\n"); | |
1616 | } | |
1617 | if (lvl->type_shadowed) | |
1618 | { | |
1619 | fprintf (stderr, " type-shadowed:"); | |
1620 | for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) | |
1621 | { | |
1622 | fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); | |
1623 | } | |
1624 | fprintf (stderr, "\n"); | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | void | |
1629 | print_other_binding_stack (struct cp_binding_level *stack) | |
1630 | { | |
1631 | struct cp_binding_level *level; | |
1632 | for (level = stack; !global_scope_p (level); level = level->level_chain) | |
1633 | { | |
1634 | fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level); | |
1635 | print_binding_level (level); | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | void | |
1640 | print_binding_stack (void) | |
1641 | { | |
1642 | struct cp_binding_level *b; | |
1643 | fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF | |
1644 | "\nclass_binding_level=" HOST_PTR_PRINTF | |
1645 | "\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n", | |
1646 | (void *) current_binding_level, (void *) class_binding_level, | |
1647 | (void *) NAMESPACE_LEVEL (global_namespace)); | |
1648 | if (class_binding_level) | |
1649 | { | |
1650 | for (b = class_binding_level; b; b = b->level_chain) | |
1651 | if (b == current_binding_level) | |
1652 | break; | |
1653 | if (b) | |
1654 | b = class_binding_level; | |
1655 | else | |
1656 | b = current_binding_level; | |
1657 | } | |
1658 | else | |
1659 | b = current_binding_level; | |
1660 | print_other_binding_stack (b); | |
1661 | fprintf (stderr, "global:\n"); | |
1662 | print_binding_level (NAMESPACE_LEVEL (global_namespace)); | |
1663 | } | |
ed3cf953 | 1664 | \f |
00e8de68 GDR |
1665 | /* Return the type associated with id. */ |
1666 | ||
1667 | tree | |
1668 | identifier_type_value (tree id) | |
1669 | { | |
1670 | timevar_push (TV_NAME_LOOKUP); | |
1671 | /* There is no type with that name, anywhere. */ | |
1672 | if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) | |
1673 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
1674 | /* This is not the type marker, but the real thing. */ | |
1675 | if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) | |
1676 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id)); | |
1677 | /* Have to search for it. It must be on the global level, now. | |
1678 | Ask lookup_name not to return non-types. */ | |
1679 | id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN); | |
1680 | if (id) | |
1681 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id)); | |
1682 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
1683 | } | |
1684 | ||
1685 | /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since | |
1686 | the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ | |
1687 | ||
1688 | tree | |
1689 | identifier_global_value (tree t) | |
1690 | { | |
1691 | return IDENTIFIER_GLOBAL_VALUE (t); | |
1692 | } | |
1693 | ||
1694 | /* Push a definition of struct, union or enum tag named ID. into | |
1695 | binding_level B. DECL is a TYPE_DECL for the type. We assume that | |
1696 | the tag ID is not already defined. */ | |
1697 | ||
1698 | static void | |
1699 | set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b) | |
1700 | { | |
1701 | tree type; | |
1702 | ||
1703 | if (b->kind != sk_namespace) | |
1704 | { | |
1705 | /* Shadow the marker, not the real thing, so that the marker | |
1706 | gets restored later. */ | |
1707 | tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
1708 | b->type_shadowed | |
1709 | = tree_cons (id, old_type_value, b->type_shadowed); | |
1710 | type = decl ? TREE_TYPE (decl) : NULL_TREE; | |
1711 | } | |
1712 | else | |
1713 | { | |
1714 | cxx_binding *binding = | |
1715 | binding_for_name (NAMESPACE_LEVEL (current_namespace), id); | |
1716 | if (decl) | |
1717 | { | |
1718 | if (binding->value) | |
1719 | supplement_binding (binding, decl); | |
1720 | else | |
1721 | binding->value = decl; | |
1722 | } | |
1723 | else | |
1724 | abort (); | |
1725 | /* Store marker instead of real type. */ | |
1726 | type = global_type_node; | |
1727 | } | |
1728 | SET_IDENTIFIER_TYPE_VALUE (id, type); | |
1729 | } | |
1730 | ||
1731 | /* As set_identifier_type_value_with_scope, but using | |
1732 | current_binding_level. */ | |
1733 | ||
1734 | void | |
1735 | set_identifier_type_value (tree id, tree decl) | |
1736 | { | |
1737 | set_identifier_type_value_with_scope (id, decl, current_binding_level); | |
1738 | } | |
1739 | ||
5a167978 GDR |
1740 | /* Return the name for the constructor (or destructor) for the |
1741 | specified class TYPE. When given a template, this routine doesn't | |
1742 | lose the specialization. */ | |
1743 | ||
1744 | tree | |
1745 | constructor_name_full (tree type) | |
1746 | { | |
1747 | type = TYPE_MAIN_VARIANT (type); | |
1748 | if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type) | |
1749 | && TYPE_HAS_CONSTRUCTOR (type)) | |
1750 | return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type))); | |
1751 | else | |
1752 | return TYPE_IDENTIFIER (type); | |
1753 | } | |
1754 | ||
1755 | /* Return the name for the constructor (or destructor) for the | |
1756 | specified class. When given a template, return the plain | |
1757 | unspecialized name. */ | |
1758 | ||
1759 | tree | |
1760 | constructor_name (tree type) | |
1761 | { | |
1762 | tree name; | |
1763 | name = constructor_name_full (type); | |
1764 | if (IDENTIFIER_TEMPLATE (name)) | |
1765 | name = IDENTIFIER_TEMPLATE (name); | |
1766 | return name; | |
1767 | } | |
1768 | ||
1769 | /* Returns TRUE if NAME is the name for the constructor for TYPE. */ | |
1770 | ||
1771 | bool | |
1772 | constructor_name_p (tree name, tree type) | |
1773 | { | |
1774 | tree ctor_name; | |
1775 | ||
1776 | if (!name) | |
1777 | return false; | |
1778 | ||
1779 | if (TREE_CODE (name) != IDENTIFIER_NODE) | |
1780 | return false; | |
1781 | ||
1782 | ctor_name = constructor_name_full (type); | |
1783 | if (name == ctor_name) | |
1784 | return true; | |
1785 | if (IDENTIFIER_TEMPLATE (ctor_name) | |
1786 | && name == IDENTIFIER_TEMPLATE (ctor_name)) | |
1787 | return true; | |
1788 | return false; | |
1789 | } | |
1790 | ||
a5e6b29b GDR |
1791 | /* Counter used to create anonymous type names. */ |
1792 | ||
1793 | static GTY(()) int anon_cnt; | |
1794 | ||
1795 | /* Return an IDENTIFIER which can be used as a name for | |
1796 | anonymous structs and unions. */ | |
1797 | ||
1798 | tree | |
1799 | make_anon_name (void) | |
1800 | { | |
1801 | char buf[32]; | |
1802 | ||
1803 | sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); | |
1804 | return get_identifier (buf); | |
1805 | } | |
1806 | ||
1807 | /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames. | |
1808 | This keeps dbxout from getting confused. */ | |
1809 | ||
1810 | void | |
1811 | clear_anon_tags (void) | |
1812 | { | |
926ce8bd | 1813 | struct cp_binding_level *b; |
a5e6b29b GDR |
1814 | static int last_cnt = 0; |
1815 | ||
1816 | /* Fast out if no new anon names were declared. */ | |
1817 | if (last_cnt == anon_cnt) | |
1818 | return; | |
1819 | ||
1820 | b = current_binding_level; | |
1821 | while (b->kind == sk_cleanup) | |
1822 | b = b->level_chain; | |
1823 | if (b->type_decls != NULL) | |
1824 | binding_table_remove_anonymous_types (b->type_decls); | |
1825 | last_cnt = anon_cnt; | |
1826 | } | |
1827 | \f | |
cd0be382 | 1828 | /* Return (from the stack of) the BINDING, if any, established at SCOPE. */ |
ed3cf953 GDR |
1829 | |
1830 | static inline cxx_binding * | |
1831 | find_binding (cxx_scope *scope, cxx_binding *binding) | |
1832 | { | |
1833 | timevar_push (TV_NAME_LOOKUP); | |
1834 | ||
1835 | for (; binding != NULL; binding = binding->previous) | |
147135cc | 1836 | if (binding->scope == scope) |
ed3cf953 GDR |
1837 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding); |
1838 | ||
da247ccc | 1839 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0); |
ed3cf953 GDR |
1840 | } |
1841 | ||
1842 | /* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ | |
daafa301 | 1843 | |
5a167978 | 1844 | static inline cxx_binding * |
ed3cf953 GDR |
1845 | cxx_scope_find_binding_for_name (cxx_scope *scope, tree name) |
1846 | { | |
1847 | cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
1848 | if (b) | |
1849 | { | |
1850 | /* Fold-in case where NAME is used only once. */ | |
147135cc | 1851 | if (scope == b->scope && b->previous == NULL) |
ed3cf953 GDR |
1852 | return b; |
1853 | return find_binding (scope, b); | |
1854 | } | |
1855 | return NULL; | |
1856 | } | |
1857 | ||
1858 | /* Always returns a binding for name in scope. If no binding is | |
1859 | found, make a new one. */ | |
1860 | ||
5a167978 | 1861 | static cxx_binding * |
ed3cf953 GDR |
1862 | binding_for_name (cxx_scope *scope, tree name) |
1863 | { | |
1864 | cxx_binding *result; | |
1865 | ||
1866 | result = cxx_scope_find_binding_for_name (scope, name); | |
1867 | if (result) | |
1868 | return result; | |
1869 | /* Not found, make a new one. */ | |
1870 | result = cxx_binding_make (NULL, NULL); | |
1871 | result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); | |
147135cc | 1872 | result->scope = scope; |
ed3cf953 GDR |
1873 | result->is_local = false; |
1874 | result->value_is_inherited = false; | |
1875 | IDENTIFIER_NAMESPACE_BINDINGS (name) = result; | |
1876 | return result; | |
1877 | } | |
ed3cf953 | 1878 | |
a5e6b29b GDR |
1879 | /* Insert another USING_DECL into the current binding level, returning |
1880 | this declaration. If this is a redeclaration, do nothing, and | |
1881 | return NULL_TREE if this not in namespace scope (in namespace | |
1882 | scope, a using decl might extend any previous bindings). */ | |
1883 | ||
1884 | tree | |
1885 | push_using_decl (tree scope, tree name) | |
1886 | { | |
1887 | tree decl; | |
1888 | ||
1889 | timevar_push (TV_NAME_LOOKUP); | |
1890 | my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383); | |
1891 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384); | |
1892 | for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) | |
1893 | if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name) | |
1894 | break; | |
1895 | if (decl) | |
1896 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, | |
1897 | namespace_bindings_p () ? decl : NULL_TREE); | |
1898 | decl = build_lang_decl (USING_DECL, name, void_type_node); | |
1899 | DECL_INITIAL (decl) = scope; | |
1900 | TREE_CHAIN (decl) = current_binding_level->usings; | |
1901 | current_binding_level->usings = decl; | |
1902 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); | |
1903 | } | |
1904 | ||
00e8de68 GDR |
1905 | /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the |
1906 | caller to set DECL_CONTEXT properly. */ | |
ed3cf953 GDR |
1907 | |
1908 | tree | |
00e8de68 | 1909 | pushdecl_with_scope (tree x, cxx_scope *level) |
ed3cf953 | 1910 | { |
926ce8bd | 1911 | struct cp_binding_level *b; |
00e8de68 | 1912 | tree function_decl = current_function_decl; |
ed3cf953 | 1913 | |
00e8de68 GDR |
1914 | timevar_push (TV_NAME_LOOKUP); |
1915 | current_function_decl = NULL_TREE; | |
1916 | if (level->kind == sk_class) | |
1917 | { | |
1918 | b = class_binding_level; | |
1919 | class_binding_level = level; | |
1920 | pushdecl_class_level (x); | |
1921 | class_binding_level = b; | |
1922 | } | |
1923 | else | |
1924 | { | |
1925 | b = current_binding_level; | |
1926 | current_binding_level = level; | |
1927 | x = pushdecl (x); | |
1928 | current_binding_level = b; | |
1929 | } | |
1930 | current_function_decl = function_decl; | |
1931 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); | |
1932 | } | |
1933 | ||
a5e6b29b GDR |
1934 | /* DECL is a FUNCTION_DECL for a non-member function, which may have |
1935 | other definitions already in place. We get around this by making | |
1936 | the value of the identifier point to a list of all the things that | |
1937 | want to be referenced by that name. It is then up to the users of | |
1938 | that name to decide what to do with that list. | |
1939 | ||
1940 | DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its | |
1941 | DECL_TEMPLATE_RESULT. It is dealt with the same way. | |
1942 | ||
1943 | FLAGS is a bitwise-or of the following values: | |
1944 | PUSH_LOCAL: Bind DECL in the current scope, rather than at | |
1945 | namespace scope. | |
1946 | PUSH_USING: DECL is being pushed as the result of a using | |
1947 | declaration. | |
1948 | ||
1949 | The value returned may be a previous declaration if we guessed wrong | |
1950 | about what language DECL should belong to (C or C++). Otherwise, | |
1951 | it's always DECL (and never something that's not a _DECL). */ | |
1952 | ||
1953 | static tree | |
1954 | push_overloaded_decl (tree decl, int flags) | |
1955 | { | |
1956 | tree name = DECL_NAME (decl); | |
1957 | tree old; | |
1958 | tree new_binding; | |
1959 | int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); | |
1960 | ||
1961 | timevar_push (TV_NAME_LOOKUP); | |
1962 | if (doing_global) | |
1963 | old = namespace_binding (name, DECL_CONTEXT (decl)); | |
1964 | else | |
1965 | old = lookup_name_current_level (name); | |
1966 | ||
1967 | if (old) | |
1968 | { | |
1969 | if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) | |
1970 | { | |
1971 | tree t = TREE_TYPE (old); | |
1972 | if (IS_AGGR_TYPE (t) && warn_shadow | |
1973 | && (! DECL_IN_SYSTEM_HEADER (decl) | |
1974 | || ! DECL_IN_SYSTEM_HEADER (old))) | |
1975 | warning ("`%#D' hides constructor for `%#T'", decl, t); | |
1976 | old = NULL_TREE; | |
1977 | } | |
1978 | else if (is_overloaded_fn (old)) | |
1979 | { | |
1980 | tree tmp; | |
1981 | ||
1982 | for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) | |
1983 | { | |
1984 | tree fn = OVL_CURRENT (tmp); | |
1985 | ||
1986 | if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) | |
1987 | && !(flags & PUSH_USING) | |
1988 | && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), | |
1989 | TYPE_ARG_TYPES (TREE_TYPE (decl)))) | |
1990 | error ("`%#D' conflicts with previous using declaration `%#D'", | |
1991 | decl, fn); | |
1992 | ||
b1a19c7c | 1993 | if (duplicate_decls (decl, fn) == fn) |
a5e6b29b GDR |
1994 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn); |
1995 | } | |
1996 | } | |
1997 | else if (old == error_mark_node) | |
1998 | /* Ignore the undefined symbol marker. */ | |
1999 | old = NULL_TREE; | |
2000 | else | |
2001 | { | |
2002 | cp_error_at ("previous non-function declaration `%#D'", old); | |
2003 | error ("conflicts with function declaration `%#D'", decl); | |
2004 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); | |
2005 | } | |
2006 | } | |
2007 | ||
f3162000 GB |
2008 | if (old || TREE_CODE (decl) == TEMPLATE_DECL |
2009 | /* If it's a using declaration, we always need to build an OVERLOAD, | |
2010 | because it's the only way to remember that the declaration comes | |
2011 | from 'using', and have the lookup behave correctly. */ | |
2012 | || (flags & PUSH_USING)) | |
a5e6b29b GDR |
2013 | { |
2014 | if (old && TREE_CODE (old) != OVERLOAD) | |
2015 | new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); | |
2016 | else | |
2017 | new_binding = ovl_cons (decl, old); | |
2018 | if (flags & PUSH_USING) | |
2019 | OVL_USED (new_binding) = 1; | |
2020 | } | |
2021 | else | |
3c28fc74 | 2022 | /* NAME is not ambiguous. */ |
a5e6b29b GDR |
2023 | new_binding = decl; |
2024 | ||
2025 | if (doing_global) | |
2026 | set_namespace_binding (name, current_namespace, new_binding); | |
2027 | else | |
2028 | { | |
2029 | /* We only create an OVERLOAD if there was a previous binding at | |
2030 | this level, or if decl is a template. In the former case, we | |
2031 | need to remove the old binding and replace it with the new | |
2032 | binding. We must also run through the NAMES on the binding | |
2033 | level where the name was bound to update the chain. */ | |
2034 | ||
2035 | if (TREE_CODE (new_binding) == OVERLOAD && old) | |
2036 | { | |
2037 | tree *d; | |
2038 | ||
2039 | for (d = &IDENTIFIER_BINDING (name)->scope->names; | |
2040 | *d; | |
2041 | d = &TREE_CHAIN (*d)) | |
2042 | if (*d == old | |
2043 | || (TREE_CODE (*d) == TREE_LIST | |
2044 | && TREE_VALUE (*d) == old)) | |
2045 | { | |
2046 | if (TREE_CODE (*d) == TREE_LIST) | |
2047 | /* Just replace the old binding with the new. */ | |
2048 | TREE_VALUE (*d) = new_binding; | |
2049 | else | |
2050 | /* Build a TREE_LIST to wrap the OVERLOAD. */ | |
2051 | *d = tree_cons (NULL_TREE, new_binding, | |
2052 | TREE_CHAIN (*d)); | |
2053 | ||
2054 | /* And update the cxx_binding node. */ | |
2055 | IDENTIFIER_BINDING (name)->value = new_binding; | |
2056 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); | |
2057 | } | |
2058 | ||
2059 | /* We should always find a previous binding in this case. */ | |
2060 | abort (); | |
2061 | } | |
2062 | ||
2063 | /* Install the new binding. */ | |
2064 | push_local_binding (name, new_binding, flags); | |
2065 | } | |
2066 | ||
2067 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); | |
2068 | } | |
2069 | ||
5a167978 GDR |
2070 | /* Check a non-member using-declaration. Return the name and scope |
2071 | being used, and the USING_DECL, or NULL_TREE on failure. */ | |
2072 | ||
2073 | static tree | |
ed5f054f | 2074 | validate_nonmember_using_decl (tree decl, tree scope, tree name) |
5a167978 | 2075 | { |
5a167978 GDR |
2076 | if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) |
2077 | { | |
5a167978 GDR |
2078 | /* 7.3.3/5 |
2079 | A using-declaration shall not name a template-id. */ | |
ed5f054f | 2080 | error ("a using-declaration cannot specify a template-id. Try `using %D'", name); |
5a167978 GDR |
2081 | return NULL_TREE; |
2082 | } | |
2083 | ||
2084 | if (TREE_CODE (decl) == NAMESPACE_DECL) | |
2085 | { | |
2086 | error ("namespace `%D' not allowed in using-declaration", decl); | |
2087 | return NULL_TREE; | |
2088 | } | |
2089 | ||
2090 | if (TREE_CODE (decl) == SCOPE_REF) | |
2091 | { | |
2092 | /* It's a nested name with template parameter dependent scope. | |
2093 | This can only be using-declaration for class member. */ | |
2094 | error ("`%T' is not a namespace", TREE_OPERAND (decl, 0)); | |
2095 | return NULL_TREE; | |
2096 | } | |
2097 | ||
2098 | if (is_overloaded_fn (decl)) | |
2099 | decl = get_first_fn (decl); | |
2100 | ||
2101 | my_friendly_assert (DECL_P (decl), 20020908); | |
2102 | ||
5a167978 GDR |
2103 | /* [namespace.udecl] |
2104 | A using-declaration for a class member shall be a | |
2105 | member-declaration. */ | |
ed5f054f | 2106 | if (TYPE_P (scope)) |
5a167978 | 2107 | { |
ed5f054f | 2108 | error ("`%T' is not a namespace", scope); |
5a167978 GDR |
2109 | return NULL_TREE; |
2110 | } | |
ed5f054f | 2111 | |
5a167978 | 2112 | /* Make a USING_DECL. */ |
ed5f054f | 2113 | return push_using_decl (scope, name); |
5a167978 GDR |
2114 | } |
2115 | ||
2116 | /* Process local and global using-declarations. */ | |
2117 | ||
2118 | static void | |
2119 | do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, | |
2120 | tree *newval, tree *newtype) | |
2121 | { | |
2122 | cxx_binding decls; | |
2123 | ||
2124 | *newval = *newtype = NULL_TREE; | |
2125 | cxx_binding_clear (&decls); | |
2126 | if (!qualified_lookup_using_namespace (name, scope, &decls, 0)) | |
2127 | /* Lookup error */ | |
2128 | return; | |
2129 | ||
2130 | if (!decls.value && !decls.type) | |
2131 | { | |
2132 | error ("`%D' not declared", name); | |
2133 | return; | |
2134 | } | |
2135 | ||
2136 | /* Check for using functions. */ | |
2137 | if (decls.value && is_overloaded_fn (decls.value)) | |
2138 | { | |
2139 | tree tmp, tmp1; | |
2140 | ||
2141 | if (oldval && !is_overloaded_fn (oldval)) | |
2142 | { | |
2143 | if (!DECL_IMPLICIT_TYPEDEF_P (oldval)) | |
2144 | error ("`%D' is already declared in this scope", name); | |
2145 | oldval = NULL_TREE; | |
2146 | } | |
2147 | ||
2148 | *newval = oldval; | |
2149 | for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp)) | |
2150 | { | |
2151 | tree new_fn = OVL_CURRENT (tmp); | |
2152 | ||
2153 | /* [namespace.udecl] | |
2154 | ||
2155 | If a function declaration in namespace scope or block | |
2156 | scope has the same name and the same parameter types as a | |
2157 | function introduced by a using declaration the program is | |
2158 | ill-formed. */ | |
2159 | for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) | |
2160 | { | |
2161 | tree old_fn = OVL_CURRENT (tmp1); | |
2162 | ||
2163 | if (new_fn == old_fn) | |
2164 | /* The function already exists in the current namespace. */ | |
2165 | break; | |
2166 | else if (OVL_USED (tmp1)) | |
2167 | continue; /* this is a using decl */ | |
2168 | else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)), | |
2169 | TYPE_ARG_TYPES (TREE_TYPE (old_fn)))) | |
2170 | { | |
2171 | /* There was already a non-using declaration in | |
2172 | this scope with the same parameter types. If both | |
2173 | are the same extern "C" functions, that's ok. */ | |
2174 | if (decls_match (new_fn, old_fn)) | |
2175 | { | |
2176 | /* If the OLD_FN was a builtin, there is now a | |
2177 | real declaration. */ | |
2178 | if (DECL_ANTICIPATED (old_fn)) | |
2179 | DECL_ANTICIPATED (old_fn) = 0; | |
2180 | break; | |
2181 | } | |
2182 | else if (!DECL_ANTICIPATED (old_fn)) | |
2183 | { | |
2184 | /* If the OLD_FN was really declared, the | |
2185 | declarations don't match. */ | |
2186 | error ("`%D' is already declared in this scope", name); | |
2187 | break; | |
2188 | } | |
2189 | ||
2190 | /* If the OLD_FN was not really there, just ignore | |
2191 | it and keep going. */ | |
2192 | } | |
2193 | } | |
2194 | ||
2195 | /* If we broke out of the loop, there's no reason to add | |
2196 | this function to the using declarations for this | |
2197 | scope. */ | |
2198 | if (tmp1) | |
2199 | continue; | |
2200 | ||
2201 | *newval = build_overload (OVL_CURRENT (tmp), *newval); | |
2202 | if (TREE_CODE (*newval) != OVERLOAD) | |
2203 | *newval = ovl_cons (*newval, NULL_TREE); | |
2204 | OVL_USED (*newval) = 1; | |
2205 | } | |
2206 | } | |
2207 | else | |
2208 | { | |
2209 | *newval = decls.value; | |
2210 | if (oldval && !decls_match (*newval, oldval)) | |
2211 | error ("`%D' is already declared in this scope", name); | |
2212 | } | |
2213 | ||
2214 | *newtype = decls.type; | |
2215 | if (oldtype && *newtype && !same_type_p (oldtype, *newtype)) | |
2216 | { | |
2217 | error ("using declaration `%D' introduced ambiguous type `%T'", | |
2218 | name, oldtype); | |
2219 | return; | |
2220 | } | |
2221 | } | |
2222 | ||
2223 | /* Process a using-declaration at function scope. */ | |
2224 | ||
2225 | void | |
ed5f054f | 2226 | do_local_using_decl (tree decl, tree scope, tree name) |
5a167978 | 2227 | { |
5a167978 GDR |
2228 | tree oldval, oldtype, newval, newtype; |
2229 | ||
ed5f054f | 2230 | decl = validate_nonmember_using_decl (decl, scope, name); |
5a167978 GDR |
2231 | if (decl == NULL_TREE) |
2232 | return; | |
2233 | ||
2234 | if (building_stmt_tree () | |
2235 | && at_function_scope_p ()) | |
2236 | add_decl_stmt (decl); | |
2237 | ||
2238 | oldval = lookup_name_current_level (name); | |
2239 | oldtype = lookup_type_current_level (name); | |
2240 | ||
2241 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); | |
2242 | ||
2243 | if (newval) | |
2244 | { | |
2245 | if (is_overloaded_fn (newval)) | |
2246 | { | |
2247 | tree fn, term; | |
2248 | ||
2249 | /* We only need to push declarations for those functions | |
2250 | that were not already bound in the current level. | |
2251 | The old value might be NULL_TREE, it might be a single | |
2252 | function, or an OVERLOAD. */ | |
2253 | if (oldval && TREE_CODE (oldval) == OVERLOAD) | |
2254 | term = OVL_FUNCTION (oldval); | |
2255 | else | |
2256 | term = oldval; | |
2257 | for (fn = newval; fn && OVL_CURRENT (fn) != term; | |
2258 | fn = OVL_NEXT (fn)) | |
2259 | push_overloaded_decl (OVL_CURRENT (fn), | |
2260 | PUSH_LOCAL | PUSH_USING); | |
2261 | } | |
2262 | else | |
2263 | push_local_binding (name, newval, PUSH_USING); | |
2264 | } | |
2265 | if (newtype) | |
2266 | set_identifier_type_value (name, newtype); | |
2267 | } | |
2268 | ||
00e8de68 GDR |
2269 | /* Return the type that should be used when TYPE's name is preceded |
2270 | by a tag such as 'struct' or 'union', or null if the name cannot | |
2271 | be used in this way. | |
2272 | ||
2273 | For example, when processing the third line of: | |
2274 | ||
2275 | struct A; | |
2276 | typedef struct A A; | |
2277 | struct A; | |
2278 | ||
2279 | lookup of A will find the typedef. Given A's typedef, this function | |
2280 | will return the type associated with "struct A". For the tag to be | |
2281 | anything other than TYPE, TYPE must be a typedef whose original type | |
2282 | has the same name and context as TYPE itself. | |
2283 | ||
2284 | It is not valid for a typedef of an anonymous type to be used with | |
2285 | an explicit tag: | |
2286 | ||
2287 | typedef struct { ... } B; | |
2288 | struct B; | |
2289 | ||
2290 | Return null for this case. */ | |
2291 | ||
2292 | static tree | |
2293 | follow_tag_typedef (tree type) | |
2294 | { | |
2295 | tree original; | |
2296 | ||
2297 | original = original_type (type); | |
2298 | if (! TYPE_NAME (original)) | |
2299 | return NULL_TREE; | |
2300 | if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type) | |
2301 | && (CP_DECL_CONTEXT (TYPE_NAME (original)) | |
2302 | == CP_DECL_CONTEXT (TYPE_NAME (type))) | |
2303 | && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original))) | |
2304 | return original; | |
2305 | else | |
2306 | return NULL_TREE; | |
2307 | } | |
2308 | ||
2309 | /* Given NAME, an IDENTIFIER_NODE, | |
2310 | return the structure (or union or enum) definition for that name. | |
2311 | Searches binding levels from its SCOPE up to the global level. | |
2312 | If THISLEVEL_ONLY is nonzero, searches only the specified context | |
2313 | (but skips any sk_cleanup contexts to find one that is | |
2314 | meaningful for tags). | |
2315 | FORM says which kind of type the caller wants; | |
2316 | it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE. | |
2317 | If the wrong kind of type is found, and it's not a template, an error is | |
2318 | reported. */ | |
2319 | ||
2320 | tree | |
2321 | lookup_tag (enum tree_code form, tree name, | |
2322 | cxx_scope *binding_level, int thislevel_only) | |
2323 | { | |
926ce8bd | 2324 | struct cp_binding_level *level; |
00e8de68 GDR |
2325 | /* Nonzero if, we should look past a template parameter level, even |
2326 | if THISLEVEL_ONLY. */ | |
2327 | int allow_template_parms_p = 1; | |
2328 | bool type_is_anonymous = ANON_AGGRNAME_P (name); | |
2329 | ||
2330 | timevar_push (TV_NAME_LOOKUP); | |
2331 | for (level = binding_level; level; level = level->level_chain) | |
2332 | { | |
926ce8bd | 2333 | tree tail; |
00e8de68 GDR |
2334 | if (type_is_anonymous && level->type_decls != NULL) |
2335 | { | |
2336 | tree type = binding_table_find_anon_type (level->type_decls, name); | |
2337 | /* There is no need for error checking here, because | |
2338 | anon names are unique throughout the compilation. */ | |
2339 | if (type != NULL) | |
2340 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type); | |
2341 | } | |
2342 | else if (level->kind == sk_namespace) | |
2343 | /* Do namespace lookup. */ | |
2344 | for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail)) | |
2345 | { | |
2346 | cxx_binding *binding = | |
2347 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name); | |
2348 | tree old; | |
2349 | ||
2350 | /* If we just skipped past a template parameter level, | |
2351 | even though THISLEVEL_ONLY, and we find a template | |
2352 | class declaration, then we use the _TYPE node for the | |
2353 | template. See the example below. */ | |
2354 | if (thislevel_only && !allow_template_parms_p | |
2355 | && binding && binding->value | |
2356 | && DECL_CLASS_TEMPLATE_P (binding->value)) | |
2357 | old = binding->value; | |
2358 | else if (binding) | |
2359 | old = select_decl (binding, LOOKUP_PREFER_TYPES); | |
2360 | else | |
2361 | old = NULL_TREE; | |
2362 | ||
2363 | if (old) | |
2364 | { | |
2365 | /* We've found something at this binding level. If it is | |
2366 | a typedef, extract the tag it refers to. Lookup fails | |
2367 | if the typedef doesn't refer to a taggable type. */ | |
2368 | old = TREE_TYPE (old); | |
2369 | old = follow_tag_typedef (old); | |
2370 | if (!old) | |
2371 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2372 | if (TREE_CODE (old) != form | |
2373 | && (form == ENUMERAL_TYPE | |
2374 | || TREE_CODE (old) == ENUMERAL_TYPE)) | |
2375 | { | |
2376 | error ("`%#D' redeclared as %C", old, form); | |
2377 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2378 | } | |
2379 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old); | |
2380 | } | |
2381 | if (thislevel_only || tail == global_namespace) | |
2382 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2383 | } | |
2384 | else if (level->type_decls != NULL) | |
2385 | { | |
2386 | binding_entry entry = binding_table_find (level->type_decls, name); | |
2387 | if (entry != NULL) | |
2388 | { | |
2389 | enum tree_code code = TREE_CODE (entry->type); | |
2390 | ||
2391 | if (code != form | |
2392 | && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE)) | |
2393 | { | |
2394 | /* Definition isn't the kind we were looking for. */ | |
2395 | error ("`%#D' redeclared as %C", entry->type, form); | |
2396 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2397 | } | |
2398 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type); | |
2399 | } | |
2400 | } | |
2401 | if (thislevel_only && level->kind != sk_cleanup) | |
2402 | { | |
2403 | if (level->kind == sk_template_parms && allow_template_parms_p) | |
2404 | { | |
2405 | /* We must deal with cases like this: | |
2406 | ||
2407 | template <class T> struct S; | |
2408 | template <class T> struct S {}; | |
2409 | ||
2410 | When looking up `S', for the second declaration, we | |
2411 | would like to find the first declaration. But, we | |
2412 | are in the pseudo-global level created for the | |
2413 | template parameters, rather than the (surrounding) | |
2414 | namespace level. Thus, we keep going one more level, | |
2415 | even though THISLEVEL_ONLY is nonzero. */ | |
2416 | allow_template_parms_p = 0; | |
2417 | continue; | |
2418 | } | |
2419 | else | |
2420 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2421 | } | |
2422 | } | |
2423 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2424 | } | |
2425 | ||
2426 | /* Given a type, find the tag that was defined for it and return the tag name. | |
2427 | Otherwise return 0. However, the value can never be 0 | |
2428 | in the cases in which this is used. | |
2429 | ||
2430 | C++: If NAME is nonzero, this is the new name to install. This is | |
2431 | done when replacing anonymous tags with real tag names. */ | |
2432 | ||
2433 | tree | |
2434 | lookup_tag_reverse (tree type, tree name) | |
2435 | { | |
926ce8bd | 2436 | struct cp_binding_level *level; |
00e8de68 GDR |
2437 | |
2438 | timevar_push (TV_NAME_LOOKUP); | |
2439 | for (level = current_binding_level; level; level = level->level_chain) | |
2440 | { | |
2441 | binding_entry entry = level->type_decls == NULL | |
2442 | ? NULL | |
2443 | : binding_table_reverse_maybe_remap (level->type_decls, type, name); | |
2444 | if (entry) | |
2445 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name); | |
2446 | } | |
2447 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
2448 | } | |
5a167978 GDR |
2449 | |
2450 | /* Returns true if ROOT (a namespace, class, or function) encloses | |
2451 | CHILD. CHILD may be either a class type or a namespace. */ | |
2452 | ||
2453 | bool | |
2454 | is_ancestor (tree root, tree child) | |
2455 | { | |
2456 | my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL | |
2457 | || TREE_CODE (root) == FUNCTION_DECL | |
2458 | || CLASS_TYPE_P (root)), 20030307); | |
2459 | my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL | |
2460 | || CLASS_TYPE_P (child)), | |
2461 | 20030307); | |
2462 | ||
2463 | /* The global namespace encloses everything. */ | |
2464 | if (root == global_namespace) | |
2465 | return true; | |
2466 | ||
2467 | while (true) | |
2468 | { | |
2469 | /* If we've run out of scopes, stop. */ | |
2470 | if (!child) | |
2471 | return false; | |
2472 | /* If we've reached the ROOT, it encloses CHILD. */ | |
2473 | if (root == child) | |
2474 | return true; | |
2475 | /* Go out one level. */ | |
2476 | if (TYPE_P (child)) | |
2477 | child = TYPE_NAME (child); | |
2478 | child = DECL_CONTEXT (child); | |
2479 | } | |
2480 | } | |
2481 | ||
2482 | /* Enter a class or namespace scope. */ | |
2483 | ||
2484 | void | |
2485 | push_scope (tree t) | |
2486 | { | |
2487 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
2488 | push_decl_namespace (t); | |
2489 | else if CLASS_TYPE_P (t) | |
2490 | push_nested_class (t); | |
2491 | } | |
2492 | ||
2493 | /* Leave scope pushed by push_scope. */ | |
2494 | ||
2495 | void | |
2496 | pop_scope (tree t) | |
2497 | { | |
2498 | if (TREE_CODE (t) == NAMESPACE_DECL) | |
2499 | pop_decl_namespace (); | |
2500 | else if CLASS_TYPE_P (t) | |
2501 | pop_nested_class (); | |
2502 | } | |
00e8de68 GDR |
2503 | \f |
2504 | /* Do a pushlevel for class declarations. */ | |
2505 | ||
2506 | void | |
2507 | pushlevel_class (void) | |
2508 | { | |
2509 | if (ENABLE_SCOPE_CHECKING) | |
2510 | is_class_level = 1; | |
2511 | ||
2512 | class_binding_level = begin_scope (sk_class, current_class_type); | |
2513 | } | |
2514 | ||
2515 | /* ...and a poplevel for class declarations. */ | |
2516 | ||
2517 | void | |
2518 | poplevel_class (void) | |
2519 | { | |
926ce8bd | 2520 | struct cp_binding_level *level = class_binding_level; |
00e8de68 GDR |
2521 | tree shadowed; |
2522 | ||
2523 | timevar_push (TV_NAME_LOOKUP); | |
2524 | my_friendly_assert (level != 0, 354); | |
2525 | ||
2526 | /* If we're leaving a toplevel class, don't bother to do the setting | |
2527 | of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot | |
2528 | shouldn't even be used when current_class_type isn't set, and second, | |
2529 | if we don't touch it here, we're able to use the cache effect if the | |
2530 | next time we're entering a class scope, it is the same class. */ | |
2531 | if (current_class_depth != 1) | |
2532 | { | |
2533 | struct cp_binding_level* b; | |
2534 | ||
2535 | /* Clear out our IDENTIFIER_CLASS_VALUEs. */ | |
2536 | for (shadowed = level->class_shadowed; | |
2537 | shadowed; | |
2538 | shadowed = TREE_CHAIN (shadowed)) | |
2539 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE; | |
2540 | ||
2541 | /* Find the next enclosing class, and recreate | |
2542 | IDENTIFIER_CLASS_VALUEs appropriate for that class. */ | |
2543 | b = level->level_chain; | |
2544 | while (b && b->kind != sk_class) | |
2545 | b = b->level_chain; | |
2546 | ||
2547 | if (b) | |
2548 | for (shadowed = b->class_shadowed; | |
2549 | shadowed; | |
2550 | shadowed = TREE_CHAIN (shadowed)) | |
2551 | { | |
2552 | cxx_binding *binding; | |
2553 | ||
2554 | binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed)); | |
2555 | while (binding && binding->scope != b) | |
2556 | binding = binding->previous; | |
2557 | ||
2558 | if (binding) | |
2559 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) | |
2560 | = binding->value; | |
2561 | } | |
2562 | } | |
2563 | else | |
2564 | /* Remember to save what IDENTIFIER's were bound in this scope so we | |
2565 | can recover from cache misses. */ | |
2566 | { | |
2567 | previous_class_type = current_class_type; | |
2568 | previous_class_values = class_binding_level->class_shadowed; | |
2569 | } | |
2570 | for (shadowed = level->type_shadowed; | |
2571 | shadowed; | |
2572 | shadowed = TREE_CHAIN (shadowed)) | |
2573 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); | |
2574 | ||
2575 | /* Remove the bindings for all of the class-level declarations. */ | |
2576 | for (shadowed = level->class_shadowed; | |
2577 | shadowed; | |
2578 | shadowed = TREE_CHAIN (shadowed)) | |
2579 | pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed)); | |
2580 | ||
2581 | /* Now, pop out of the binding level which we created up in the | |
2582 | `pushlevel_class' routine. */ | |
2583 | if (ENABLE_SCOPE_CHECKING) | |
2584 | is_class_level = 1; | |
2585 | ||
2586 | leave_scope (); | |
2587 | timevar_pop (TV_NAME_LOOKUP); | |
2588 | } | |
2589 | ||
2590 | /* Bind DECL to ID in the class_binding_level. Returns nonzero if the | |
2591 | binding was successful. */ | |
2592 | ||
2593 | int | |
2594 | push_class_binding (tree id, tree decl) | |
2595 | { | |
2596 | int result = 1; | |
2597 | cxx_binding *binding = IDENTIFIER_BINDING (id); | |
2598 | tree context; | |
2599 | ||
2600 | timevar_push (TV_NAME_LOOKUP); | |
2601 | /* Note that we declared this value so that we can issue an error if | |
2602 | this is an invalid redeclaration of a name already used for some | |
2603 | other purpose. */ | |
2604 | note_name_declared_in_class (id, decl); | |
2605 | ||
2606 | if (binding && binding->scope == class_binding_level) | |
2607 | /* Supplement the existing binding. */ | |
2608 | result = supplement_binding (IDENTIFIER_BINDING (id), decl); | |
2609 | else | |
2610 | /* Create a new binding. */ | |
2611 | push_binding (id, decl, class_binding_level); | |
2612 | ||
2613 | /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the | |
2614 | class-level declaration. Note that we do not use DECL here | |
2615 | because of the possibility of the `struct stat' hack; if DECL is | |
2616 | a class-name or enum-name we might prefer a field-name, or some | |
2617 | such. */ | |
2618 | IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value; | |
2619 | ||
2620 | /* If this is a binding from a base class, mark it as such. */ | |
2621 | binding = IDENTIFIER_BINDING (id); | |
2622 | if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) | |
2623 | { | |
2624 | if (TREE_CODE (decl) == OVERLOAD) | |
2625 | context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); | |
2626 | else | |
2627 | { | |
2628 | my_friendly_assert (DECL_P (decl), 0); | |
2629 | context = context_for_name_lookup (decl); | |
2630 | } | |
2631 | ||
2632 | if (is_properly_derived_from (current_class_type, context)) | |
2633 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
2634 | else | |
2635 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
2636 | } | |
2637 | else if (binding->value == decl) | |
2638 | /* We only encounter a TREE_LIST when push_class_decls detects an | |
2639 | ambiguity. Such an ambiguity can be overridden by a definition | |
2640 | in this class. */ | |
2641 | INHERITED_VALUE_BINDING_P (binding) = 1; | |
2642 | ||
2643 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result); | |
2644 | } | |
2645 | ||
2646 | /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE | |
2647 | for any names in enclosing classes. */ | |
2648 | ||
2649 | void | |
2650 | clear_identifier_class_values (void) | |
2651 | { | |
2652 | tree t; | |
2653 | ||
2654 | if (!class_binding_level) | |
2655 | return; | |
2656 | ||
2657 | for (t = class_binding_level->class_shadowed; | |
2658 | t; | |
2659 | t = TREE_CHAIN (t)) | |
2660 | IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE; | |
2661 | } | |
2662 | ||
2663 | /* Make the declaration of X appear in CLASS scope. */ | |
2664 | ||
2665 | bool | |
2666 | pushdecl_class_level (tree x) | |
2667 | { | |
2668 | tree name; | |
2669 | bool is_valid = true; | |
2670 | ||
2671 | timevar_push (TV_NAME_LOOKUP); | |
2672 | /* Get the name of X. */ | |
2673 | if (TREE_CODE (x) == OVERLOAD) | |
2674 | name = DECL_NAME (get_first_fn (x)); | |
2675 | else | |
2676 | name = DECL_NAME (x); | |
2677 | ||
2678 | if (name) | |
2679 | { | |
2680 | is_valid = push_class_level_binding (name, x); | |
2681 | if (TREE_CODE (x) == TYPE_DECL) | |
2682 | set_identifier_type_value (name, x); | |
2683 | } | |
2684 | else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) | |
2685 | { | |
2686 | /* If X is an anonymous aggregate, all of its members are | |
2687 | treated as if they were members of the class containing the | |
2688 | aggregate, for naming purposes. */ | |
2689 | tree f; | |
2690 | ||
2691 | for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f)) | |
2692 | { | |
2693 | location_t save_location = input_location; | |
2694 | input_location = DECL_SOURCE_LOCATION (f); | |
2695 | if (!pushdecl_class_level (f)) | |
2696 | is_valid = false; | |
2697 | input_location = save_location; | |
2698 | } | |
2699 | } | |
2700 | timevar_pop (TV_NAME_LOOKUP); | |
2701 | ||
2702 | return is_valid; | |
2703 | } | |
2704 | ||
2705 | /* Make the declaration(s) of X appear in CLASS scope under the name | |
2706 | NAME. Returns true if the binding is valid. */ | |
2707 | ||
2708 | bool | |
2709 | push_class_level_binding (tree name, tree x) | |
2710 | { | |
2711 | cxx_binding *binding; | |
2712 | ||
2713 | timevar_push (TV_NAME_LOOKUP); | |
2714 | /* The class_binding_level will be NULL if x is a template | |
2715 | parameter name in a member template. */ | |
2716 | if (!class_binding_level) | |
2717 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); | |
2718 | ||
2719 | /* Make sure that this new member does not have the same name | |
2720 | as a template parameter. */ | |
2721 | if (TYPE_BEING_DEFINED (current_class_type)) | |
2722 | check_template_shadow (x); | |
2723 | ||
2724 | /* If this declaration shadows a declaration from an enclosing | |
2725 | class, then we will need to restore IDENTIFIER_CLASS_VALUE when | |
2726 | we leave this class. Record the shadowed declaration here. */ | |
2727 | binding = IDENTIFIER_BINDING (name); | |
2728 | if (binding && binding->value) | |
2729 | { | |
2730 | tree bval = binding->value; | |
2731 | tree old_decl = NULL_TREE; | |
2732 | ||
2733 | if (INHERITED_VALUE_BINDING_P (binding)) | |
2734 | { | |
2735 | /* If the old binding was from a base class, and was for a | |
2736 | tag name, slide it over to make room for the new binding. | |
2737 | The old binding is still visible if explicitly qualified | |
2738 | with a class-key. */ | |
2739 | if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval) | |
2740 | && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) | |
2741 | { | |
2742 | old_decl = binding->type; | |
2743 | binding->type = bval; | |
2744 | binding->value = NULL_TREE; | |
2745 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
2746 | } | |
2747 | else | |
2748 | old_decl = bval; | |
2749 | } | |
2750 | else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval)) | |
2751 | old_decl = bval; | |
2752 | else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL) | |
2753 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); | |
2754 | else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval)) | |
2755 | old_decl = bval; | |
2756 | else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x)) | |
2757 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); | |
2758 | ||
2759 | if (old_decl) | |
2760 | { | |
2761 | tree shadow; | |
2762 | ||
2763 | /* Find the previous binding of name on the class-shadowed | |
2764 | list, and update it. */ | |
2765 | for (shadow = class_binding_level->class_shadowed; | |
2766 | shadow; | |
2767 | shadow = TREE_CHAIN (shadow)) | |
2768 | if (TREE_PURPOSE (shadow) == name | |
2769 | && TREE_TYPE (shadow) == old_decl) | |
2770 | { | |
2771 | binding->value = x; | |
2772 | INHERITED_VALUE_BINDING_P (binding) = 0; | |
2773 | TREE_TYPE (shadow) = x; | |
2774 | IDENTIFIER_CLASS_VALUE (name) = x; | |
2775 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); | |
2776 | } | |
2777 | } | |
2778 | } | |
2779 | ||
2780 | /* If we didn't replace an existing binding, put the binding on the | |
2781 | stack of bindings for the identifier, and update the shadowed list. */ | |
2782 | if (push_class_binding (name, x)) | |
2783 | { | |
2784 | class_binding_level->class_shadowed | |
2785 | = tree_cons (name, NULL, | |
2786 | class_binding_level->class_shadowed); | |
2787 | /* Record the value we are binding NAME to so that we can know | |
2788 | what to pop later. */ | |
2789 | TREE_TYPE (class_binding_level->class_shadowed) = x; | |
2790 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); | |
2791 | } | |
2792 | ||
2793 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false); | |
2794 | } | |
2795 | ||
5a167978 GDR |
2796 | tree |
2797 | do_class_using_decl (tree decl) | |
2798 | { | |
2799 | tree name, value, scope, type; | |
2800 | ||
2801 | if (TREE_CODE (decl) != SCOPE_REF | |
2802 | || !TREE_OPERAND (decl, 0) | |
2803 | || !TYPE_P (TREE_OPERAND (decl, 0))) | |
2804 | { | |
2805 | error ("using-declaration for non-member at class scope"); | |
2806 | return NULL_TREE; | |
2807 | } | |
2808 | scope = TREE_OPERAND (decl, 0); | |
2809 | name = TREE_OPERAND (decl, 1); | |
2810 | if (TREE_CODE (name) == BIT_NOT_EXPR) | |
2811 | { | |
2812 | error ("using-declaration cannot name destructor"); | |
2813 | return NULL_TREE; | |
2814 | } | |
2815 | if (TREE_CODE (name) == TYPE_DECL) | |
2816 | name = DECL_NAME (name); | |
2817 | else if (TREE_CODE (name) == TEMPLATE_DECL) | |
2818 | name = DECL_NAME (name); | |
2819 | else if (BASELINK_P (name)) | |
2820 | { | |
2821 | tree fns = BASELINK_FUNCTIONS (name); | |
2822 | name = DECL_NAME (get_first_fn (fns)); | |
2823 | } | |
2824 | ||
2825 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716); | |
2826 | ||
2827 | /* Dependent using decls have a NULL type, non-dependent ones have a | |
2828 | void type. */ | |
2829 | type = dependent_type_p (scope) ? NULL_TREE : void_type_node; | |
2830 | value = build_lang_decl (USING_DECL, name, type); | |
2831 | DECL_INITIAL (value) = scope; | |
2832 | return value; | |
2833 | } | |
2834 | ||
00e8de68 GDR |
2835 | void |
2836 | set_class_shadows (tree shadows) | |
2837 | { | |
2838 | class_binding_level->class_shadowed = shadows; | |
2839 | } | |
2840 | \f | |
2841 | /* Return the binding value for name in scope. */ | |
2842 | ||
2843 | tree | |
2844 | namespace_binding (tree name, tree scope) | |
2845 | { | |
2846 | cxx_binding *binding; | |
2847 | ||
2848 | if (scope == NULL) | |
2849 | scope = global_namespace; | |
2850 | scope = ORIGINAL_NAMESPACE (scope); | |
2851 | binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
2852 | ||
2853 | return binding ? binding->value : NULL_TREE; | |
2854 | } | |
2855 | ||
2856 | /* Set the binding value for name in scope. */ | |
ed3cf953 GDR |
2857 | |
2858 | void | |
2859 | set_namespace_binding (tree name, tree scope, tree val) | |
2860 | { | |
2861 | cxx_binding *b; | |
2862 | ||
2863 | timevar_push (TV_NAME_LOOKUP); | |
2864 | if (scope == NULL_TREE) | |
2865 | scope = global_namespace; | |
2866 | b = binding_for_name (NAMESPACE_LEVEL (scope), name); | |
3c28fc74 | 2867 | if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node) |
147135cc | 2868 | b->value = val; |
4b0d3cbe | 2869 | else |
c87ceb13 | 2870 | supplement_binding (b, val); |
ed3cf953 GDR |
2871 | timevar_pop (TV_NAME_LOOKUP); |
2872 | } | |
2873 | ||
5a167978 GDR |
2874 | /* Compute the namespace where a declaration is defined. */ |
2875 | ||
2876 | static tree | |
2877 | decl_namespace (tree decl) | |
2878 | { | |
2879 | timevar_push (TV_NAME_LOOKUP); | |
2880 | if (TYPE_P (decl)) | |
2881 | decl = TYPE_STUB_DECL (decl); | |
2882 | while (DECL_CONTEXT (decl)) | |
2883 | { | |
2884 | decl = DECL_CONTEXT (decl); | |
2885 | if (TREE_CODE (decl) == NAMESPACE_DECL) | |
2886 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); | |
2887 | if (TYPE_P (decl)) | |
2888 | decl = TYPE_STUB_DECL (decl); | |
2889 | my_friendly_assert (DECL_P (decl), 390); | |
2890 | } | |
2891 | ||
2892 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, global_namespace); | |
2893 | } | |
2894 | ||
2895 | /* Set the context of a declaration to scope. Complain if we are not | |
2896 | outside scope. */ | |
2897 | ||
2898 | void | |
2899 | set_decl_namespace (tree decl, tree scope, bool friendp) | |
2900 | { | |
2901 | tree old; | |
2902 | ||
2903 | /* Get rid of namespace aliases. */ | |
2904 | scope = ORIGINAL_NAMESPACE (scope); | |
2905 | ||
2906 | /* It is ok for friends to be qualified in parallel space. */ | |
2907 | if (!friendp && !is_ancestor (current_namespace, scope)) | |
2908 | error ("declaration of `%D' not in a namespace surrounding `%D'", | |
2909 | decl, scope); | |
2910 | DECL_CONTEXT (decl) = FROB_CONTEXT (scope); | |
2911 | if (scope != current_namespace) | |
2912 | { | |
2913 | /* See whether this has been declared in the namespace. */ | |
2914 | old = namespace_binding (DECL_NAME (decl), scope); | |
2915 | if (!old) | |
2916 | /* No old declaration at all. */ | |
2917 | goto complain; | |
2918 | /* A template can be explicitly specialized in any namespace. */ | |
2919 | if (processing_explicit_instantiation) | |
2920 | return; | |
2921 | if (!is_overloaded_fn (decl)) | |
2922 | /* Don't compare non-function decls with decls_match here, | |
2923 | since it can't check for the correct constness at this | |
2924 | point. pushdecl will find those errors later. */ | |
2925 | return; | |
2926 | /* Since decl is a function, old should contain a function decl. */ | |
2927 | if (!is_overloaded_fn (old)) | |
2928 | goto complain; | |
2929 | if (processing_template_decl || processing_specialization) | |
2930 | /* We have not yet called push_template_decl to turn a | |
2931 | FUNCTION_DECL into a TEMPLATE_DECL, so the declarations | |
2932 | won't match. But, we'll check later, when we construct the | |
2933 | template. */ | |
2934 | return; | |
2935 | if (is_overloaded_fn (old)) | |
2936 | { | |
2937 | for (; old; old = OVL_NEXT (old)) | |
2938 | if (decls_match (decl, OVL_CURRENT (old))) | |
2939 | return; | |
2940 | } | |
2941 | else | |
2942 | if (decls_match (decl, old)) | |
2943 | return; | |
2944 | } | |
2945 | else | |
2946 | return; | |
2947 | complain: | |
2948 | error ("`%D' should have been declared inside `%D'", | |
2949 | decl, scope); | |
2950 | } | |
2951 | ||
2952 | /* Return the namespace where the current declaration is declared. */ | |
2953 | ||
2954 | tree | |
2955 | current_decl_namespace (void) | |
2956 | { | |
2957 | tree result; | |
2958 | /* If we have been pushed into a different namespace, use it. */ | |
2959 | if (decl_namespace_list) | |
2960 | return TREE_PURPOSE (decl_namespace_list); | |
2961 | ||
2962 | if (current_class_type) | |
2963 | result = decl_namespace (TYPE_STUB_DECL (current_class_type)); | |
2964 | else if (current_function_decl) | |
2965 | result = decl_namespace (current_function_decl); | |
2966 | else | |
2967 | result = current_namespace; | |
2968 | return result; | |
2969 | } | |
2970 | ||
00e8de68 GDR |
2971 | /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we |
2972 | select a name that is unique to this compilation unit. */ | |
2973 | ||
2974 | void | |
2975 | push_namespace (tree name) | |
2976 | { | |
2977 | tree d = NULL_TREE; | |
2978 | int need_new = 1; | |
2979 | int implicit_use = 0; | |
ed36980c | 2980 | bool anon = !name; |
00e8de68 GDR |
2981 | |
2982 | timevar_push (TV_NAME_LOOKUP); | |
2983 | ||
2984 | /* We should not get here if the global_namespace is not yet constructed | |
2985 | nor if NAME designates the global namespace: The global scope is | |
2986 | constructed elsewhere. */ | |
2987 | my_friendly_assert (global_namespace != NULL && name != global_scope_name, | |
2988 | 20030531); | |
2989 | ||
ed36980c | 2990 | if (anon) |
00e8de68 GDR |
2991 | { |
2992 | /* The name of anonymous namespace is unique for the translation | |
2993 | unit. */ | |
2994 | if (!anonymous_namespace_name) | |
2995 | anonymous_namespace_name = get_file_function_name ('N'); | |
2996 | name = anonymous_namespace_name; | |
2997 | d = IDENTIFIER_NAMESPACE_VALUE (name); | |
2998 | if (d) | |
2999 | /* Reopening anonymous namespace. */ | |
3000 | need_new = 0; | |
3001 | implicit_use = 1; | |
3002 | } | |
3003 | else | |
3004 | { | |
3005 | /* Check whether this is an extended namespace definition. */ | |
3006 | d = IDENTIFIER_NAMESPACE_VALUE (name); | |
3007 | if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) | |
3008 | { | |
3009 | need_new = 0; | |
3010 | if (DECL_NAMESPACE_ALIAS (d)) | |
3011 | { | |
3012 | error ("namespace alias `%D' not allowed here, assuming `%D'", | |
3013 | d, DECL_NAMESPACE_ALIAS (d)); | |
3014 | d = DECL_NAMESPACE_ALIAS (d); | |
3015 | } | |
3016 | } | |
3017 | } | |
3018 | ||
3019 | if (need_new) | |
3020 | { | |
3021 | /* Make a new namespace, binding the name to it. */ | |
3022 | d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); | |
3023 | DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); | |
3024 | d = pushdecl (d); | |
ed36980c JM |
3025 | if (anon) |
3026 | { | |
3027 | /* Clear DECL_NAME for the benefit of debugging back ends. */ | |
3028 | SET_DECL_ASSEMBLER_NAME (d, name); | |
3029 | DECL_NAME (d) = NULL_TREE; | |
3030 | } | |
00e8de68 GDR |
3031 | begin_scope (sk_namespace, d); |
3032 | } | |
3033 | else | |
3034 | resume_scope (NAMESPACE_LEVEL (d)); | |
3035 | ||
3036 | if (implicit_use) | |
3037 | do_using_directive (d); | |
3038 | /* Enter the name space. */ | |
3039 | current_namespace = d; | |
3040 | ||
3041 | timevar_pop (TV_NAME_LOOKUP); | |
3042 | } | |
3043 | ||
3044 | /* Pop from the scope of the current namespace. */ | |
3045 | ||
3046 | void | |
3047 | pop_namespace (void) | |
3048 | { | |
3049 | my_friendly_assert (current_namespace != global_namespace, 20010801); | |
3050 | current_namespace = CP_DECL_CONTEXT (current_namespace); | |
3051 | /* The binding level is not popped, as it might be re-opened later. */ | |
3052 | leave_scope (); | |
3053 | } | |
3054 | ||
3055 | /* Push into the scope of the namespace NS, even if it is deeply | |
3056 | nested within another namespace. */ | |
3057 | ||
3058 | void | |
3059 | push_nested_namespace (tree ns) | |
3060 | { | |
3061 | if (ns == global_namespace) | |
3062 | push_to_top_level (); | |
3063 | else | |
3064 | { | |
3065 | push_nested_namespace (CP_DECL_CONTEXT (ns)); | |
3066 | push_namespace (DECL_NAME (ns)); | |
3067 | } | |
3068 | } | |
3069 | ||
3070 | /* Pop back from the scope of the namespace NS, which was previously | |
3071 | entered with push_nested_namespace. */ | |
3072 | ||
3073 | void | |
3074 | pop_nested_namespace (tree ns) | |
3075 | { | |
3076 | timevar_push (TV_NAME_LOOKUP); | |
3077 | while (ns != global_namespace) | |
3078 | { | |
3079 | pop_namespace (); | |
3080 | ns = CP_DECL_CONTEXT (ns); | |
3081 | } | |
3082 | ||
3083 | pop_from_top_level (); | |
3084 | timevar_pop (TV_NAME_LOOKUP); | |
3085 | } | |
3086 | ||
5a167978 GDR |
3087 | /* Temporarily set the namespace for the current declaration. */ |
3088 | ||
3089 | void | |
3090 | push_decl_namespace (tree decl) | |
3091 | { | |
3092 | if (TREE_CODE (decl) != NAMESPACE_DECL) | |
3093 | decl = decl_namespace (decl); | |
3094 | decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl), | |
3095 | NULL_TREE, decl_namespace_list); | |
3096 | } | |
3097 | ||
3098 | /* [namespace.memdef]/2 */ | |
3099 | ||
3100 | void | |
3101 | pop_decl_namespace (void) | |
3102 | { | |
3103 | decl_namespace_list = TREE_CHAIN (decl_namespace_list); | |
3104 | } | |
3105 | ||
3106 | /* Return the namespace that is the common ancestor | |
3107 | of two given namespaces. */ | |
3108 | ||
a5e6b29b | 3109 | static tree |
5a167978 GDR |
3110 | namespace_ancestor (tree ns1, tree ns2) |
3111 | { | |
3112 | timevar_push (TV_NAME_LOOKUP); | |
3113 | if (is_ancestor (ns1, ns2)) | |
3114 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1); | |
3115 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, | |
3116 | namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2)); | |
3117 | } | |
3118 | ||
3119 | /* Process a namespace-alias declaration. */ | |
3120 | ||
3121 | void | |
3122 | do_namespace_alias (tree alias, tree namespace) | |
3123 | { | |
3124 | if (TREE_CODE (namespace) != NAMESPACE_DECL) | |
3125 | { | |
3126 | /* The parser did not find it, so it's not there. */ | |
3127 | error ("unknown namespace `%D'", namespace); | |
3128 | return; | |
3129 | } | |
3130 | ||
3131 | namespace = ORIGINAL_NAMESPACE (namespace); | |
3132 | ||
3133 | /* Build the alias. */ | |
3134 | alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); | |
3135 | DECL_NAMESPACE_ALIAS (alias) = namespace; | |
3136 | DECL_EXTERNAL (alias) = 1; | |
3137 | pushdecl (alias); | |
3138 | } | |
3139 | ||
00e8de68 GDR |
3140 | /* Like pushdecl, only it places X in the current namespace, |
3141 | if appropriate. */ | |
3142 | ||
3143 | tree | |
3144 | pushdecl_namespace_level (tree x) | |
3145 | { | |
926ce8bd KH |
3146 | struct cp_binding_level *b = current_binding_level; |
3147 | tree t; | |
00e8de68 GDR |
3148 | |
3149 | timevar_push (TV_NAME_LOOKUP); | |
3150 | t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace)); | |
3151 | ||
3152 | /* Now, the type_shadowed stack may screw us. Munge it so it does | |
3153 | what we want. */ | |
3154 | if (TREE_CODE (x) == TYPE_DECL) | |
3155 | { | |
3156 | tree name = DECL_NAME (x); | |
3157 | tree newval; | |
3158 | tree *ptr = (tree *)0; | |
3159 | for (; !global_scope_p (b); b = b->level_chain) | |
3160 | { | |
3161 | tree shadowed = b->type_shadowed; | |
3162 | for (; shadowed; shadowed = TREE_CHAIN (shadowed)) | |
3163 | if (TREE_PURPOSE (shadowed) == name) | |
3164 | { | |
3165 | ptr = &TREE_VALUE (shadowed); | |
3166 | /* Can't break out of the loop here because sometimes | |
3167 | a binding level will have duplicate bindings for | |
3168 | PT names. It's gross, but I haven't time to fix it. */ | |
3169 | } | |
3170 | } | |
3171 | newval = TREE_TYPE (x); | |
3172 | if (ptr == (tree *)0) | |
3173 | { | |
3174 | /* @@ This shouldn't be needed. My test case "zstring.cc" trips | |
3175 | up here if this is changed to an assertion. --KR */ | |
3176 | SET_IDENTIFIER_TYPE_VALUE (name, x); | |
3177 | } | |
3178 | else | |
3179 | { | |
3180 | *ptr = newval; | |
3181 | } | |
3182 | } | |
3183 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
3184 | } | |
3185 | ||
5a167978 GDR |
3186 | /* Insert USED into the using list of USER. Set INDIRECT_flag if this |
3187 | directive is not directly from the source. Also find the common | |
3188 | ancestor and let our users know about the new namespace */ | |
3189 | static void | |
3190 | add_using_namespace (tree user, tree used, bool indirect) | |
3191 | { | |
3192 | tree t; | |
3193 | timevar_push (TV_NAME_LOOKUP); | |
3194 | /* Using oneself is a no-op. */ | |
3195 | if (user == used) | |
3196 | { | |
3197 | timevar_pop (TV_NAME_LOOKUP); | |
3198 | return; | |
3199 | } | |
3200 | my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380); | |
3201 | my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380); | |
3202 | /* Check if we already have this. */ | |
3203 | t = purpose_member (used, DECL_NAMESPACE_USING (user)); | |
3204 | if (t != NULL_TREE) | |
3205 | { | |
3206 | if (!indirect) | |
3207 | /* Promote to direct usage. */ | |
3208 | TREE_INDIRECT_USING (t) = 0; | |
3209 | timevar_pop (TV_NAME_LOOKUP); | |
3210 | return; | |
3211 | } | |
3212 | ||
3213 | /* Add used to the user's using list. */ | |
3214 | DECL_NAMESPACE_USING (user) | |
3215 | = tree_cons (used, namespace_ancestor (user, used), | |
3216 | DECL_NAMESPACE_USING (user)); | |
3217 | ||
3218 | TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; | |
3219 | ||
3220 | /* Add user to the used's users list. */ | |
3221 | DECL_NAMESPACE_USERS (used) | |
3222 | = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); | |
3223 | ||
3224 | /* Recursively add all namespaces used. */ | |
3225 | for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) | |
3226 | /* indirect usage */ | |
3227 | add_using_namespace (user, TREE_PURPOSE (t), 1); | |
3228 | ||
3229 | /* Tell everyone using us about the new used namespaces. */ | |
3230 | for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) | |
3231 | add_using_namespace (TREE_PURPOSE (t), used, 1); | |
3232 | timevar_pop (TV_NAME_LOOKUP); | |
3233 | } | |
3234 | ||
3235 | /* Process a using-declaration not appearing in class or local scope. */ | |
3236 | ||
3237 | void | |
ed5f054f | 3238 | do_toplevel_using_decl (tree decl, tree scope, tree name) |
5a167978 | 3239 | { |
5a167978 GDR |
3240 | tree oldval, oldtype, newval, newtype; |
3241 | cxx_binding *binding; | |
3242 | ||
ed5f054f | 3243 | decl = validate_nonmember_using_decl (decl, scope, name); |
5a167978 GDR |
3244 | if (decl == NULL_TREE) |
3245 | return; | |
3246 | ||
3247 | binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name); | |
3248 | ||
3249 | oldval = binding->value; | |
3250 | oldtype = binding->type; | |
3251 | ||
3252 | do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); | |
3253 | ||
3254 | /* Copy declarations found. */ | |
3255 | if (newval) | |
3256 | binding->value = newval; | |
3257 | if (newtype) | |
3258 | binding->type = newtype; | |
3259 | return; | |
3260 | } | |
3261 | ||
3262 | /* Process a using-directive. */ | |
3263 | ||
3264 | void | |
3265 | do_using_directive (tree namespace) | |
3266 | { | |
3267 | if (building_stmt_tree ()) | |
3268 | add_stmt (build_stmt (USING_STMT, namespace)); | |
3269 | ||
3270 | /* using namespace A::B::C; */ | |
3271 | if (TREE_CODE (namespace) == SCOPE_REF) | |
3272 | namespace = TREE_OPERAND (namespace, 1); | |
3273 | if (TREE_CODE (namespace) == IDENTIFIER_NODE) | |
3274 | { | |
3275 | /* Lookup in lexer did not find a namespace. */ | |
3276 | if (!processing_template_decl) | |
3277 | error ("namespace `%T' undeclared", namespace); | |
3278 | return; | |
3279 | } | |
3280 | if (TREE_CODE (namespace) != NAMESPACE_DECL) | |
3281 | { | |
3282 | if (!processing_template_decl) | |
3283 | error ("`%T' is not a namespace", namespace); | |
3284 | return; | |
3285 | } | |
3286 | namespace = ORIGINAL_NAMESPACE (namespace); | |
3287 | if (!toplevel_bindings_p ()) | |
3288 | push_using_directive (namespace); | |
3289 | else | |
3290 | /* direct usage */ | |
3291 | add_using_namespace (current_namespace, namespace, 0); | |
3292 | } | |
3293 | ||
86098eb8 JM |
3294 | /* Deal with a using-directive seen by the parser. Currently we only |
3295 | handle attributes here, since they cannot appear inside a template. */ | |
3296 | ||
3297 | void | |
3298 | parse_using_directive (tree namespace, tree attribs) | |
3299 | { | |
3300 | tree a; | |
3301 | ||
3302 | do_using_directive (namespace); | |
3303 | ||
3304 | for (a = attribs; a; a = TREE_CHAIN (a)) | |
3305 | { | |
3306 | tree name = TREE_PURPOSE (a); | |
3307 | if (is_attribute_p ("strong", name)) | |
3308 | { | |
3309 | if (!toplevel_bindings_p ()) | |
3310 | error ("strong using only meaningful at namespace scope"); | |
3311 | else | |
3312 | DECL_NAMESPACE_ASSOCIATIONS (namespace) | |
3313 | = tree_cons (current_namespace, 0, | |
3314 | DECL_NAMESPACE_ASSOCIATIONS (namespace)); | |
3315 | } | |
3316 | else | |
3317 | warning ("`%D' attribute directive ignored", name); | |
3318 | } | |
3319 | } | |
3320 | ||
a5e6b29b GDR |
3321 | /* Like pushdecl, only it places X in the global scope if appropriate. |
3322 | Calls cp_finish_decl to register the variable, initializing it with | |
3323 | *INIT, if INIT is non-NULL. */ | |
3324 | ||
3325 | static tree | |
3326 | pushdecl_top_level_1 (tree x, tree *init) | |
3327 | { | |
3328 | timevar_push (TV_NAME_LOOKUP); | |
3329 | push_to_top_level (); | |
3330 | x = pushdecl_namespace_level (x); | |
3331 | if (init) | |
3332 | cp_finish_decl (x, *init, NULL_TREE, 0); | |
3333 | pop_from_top_level (); | |
3334 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); | |
3335 | } | |
3336 | ||
3337 | /* Like pushdecl, only it places X in the global scope if appropriate. */ | |
3338 | ||
3339 | tree | |
3340 | pushdecl_top_level (tree x) | |
3341 | { | |
3342 | return pushdecl_top_level_1 (x, NULL); | |
3343 | } | |
3344 | ||
3345 | /* Like pushdecl, only it places X in the global scope if | |
3346 | appropriate. Calls cp_finish_decl to register the variable, | |
3347 | initializing it with INIT. */ | |
3348 | ||
3349 | tree | |
3350 | pushdecl_top_level_and_finish (tree x, tree init) | |
3351 | { | |
3352 | return pushdecl_top_level_1 (x, &init); | |
3353 | } | |
3354 | ||
5a167978 GDR |
3355 | /* Combines two sets of overloaded functions into an OVERLOAD chain, removing |
3356 | duplicates. The first list becomes the tail of the result. | |
3357 | ||
3358 | The algorithm is O(n^2). We could get this down to O(n log n) by | |
3359 | doing a sort on the addresses of the functions, if that becomes | |
3360 | necessary. */ | |
3361 | ||
3362 | static tree | |
3363 | merge_functions (tree s1, tree s2) | |
3364 | { | |
3365 | for (; s2; s2 = OVL_NEXT (s2)) | |
3366 | { | |
3367 | tree fn2 = OVL_CURRENT (s2); | |
3368 | tree fns1; | |
3369 | ||
3370 | for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) | |
3371 | { | |
3372 | tree fn1 = OVL_CURRENT (fns1); | |
3373 | ||
3374 | /* If the function from S2 is already in S1, there is no | |
3375 | need to add it again. For `extern "C"' functions, we | |
3376 | might have two FUNCTION_DECLs for the same function, in | |
3377 | different namespaces; again, we only need one of them. */ | |
3378 | if (fn1 == fn2 | |
3379 | || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2) | |
3380 | && DECL_NAME (fn1) == DECL_NAME (fn2))) | |
3381 | break; | |
3382 | } | |
3383 | ||
3384 | /* If we exhausted all of the functions in S1, FN2 is new. */ | |
3385 | if (!fns1) | |
3386 | s1 = build_overload (fn2, s1); | |
3387 | } | |
3388 | return s1; | |
3389 | } | |
3390 | ||
3391 | /* This should return an error not all definitions define functions. | |
3392 | It is not an error if we find two functions with exactly the | |
3393 | same signature, only if these are selected in overload resolution. | |
3394 | old is the current set of bindings, new the freshly-found binding. | |
3395 | XXX Do we want to give *all* candidates in case of ambiguity? | |
3396 | XXX In what way should I treat extern declarations? | |
3397 | XXX I don't want to repeat the entire duplicate_decls here */ | |
3398 | ||
3399 | static cxx_binding * | |
3400 | ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags) | |
3401 | { | |
3402 | tree val, type; | |
3403 | my_friendly_assert (old != NULL, 393); | |
3404 | /* Copy the value. */ | |
3405 | val = new->value; | |
3406 | if (val) | |
3407 | switch (TREE_CODE (val)) | |
3408 | { | |
3409 | case TEMPLATE_DECL: | |
3410 | /* If we expect types or namespaces, and not templates, | |
3411 | or this is not a template class. */ | |
3412 | if (LOOKUP_QUALIFIERS_ONLY (flags) | |
3413 | && !DECL_CLASS_TEMPLATE_P (val)) | |
3414 | val = NULL_TREE; | |
3415 | break; | |
3416 | case TYPE_DECL: | |
3417 | if (LOOKUP_NAMESPACES_ONLY (flags)) | |
3418 | val = NULL_TREE; | |
3419 | break; | |
3420 | case NAMESPACE_DECL: | |
3421 | if (LOOKUP_TYPES_ONLY (flags)) | |
3422 | val = NULL_TREE; | |
3423 | break; | |
3424 | case FUNCTION_DECL: | |
3425 | /* Ignore built-in functions that are still anticipated. */ | |
3426 | if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val)) | |
3427 | val = NULL_TREE; | |
3428 | break; | |
3429 | default: | |
3430 | if (LOOKUP_QUALIFIERS_ONLY (flags)) | |
3431 | val = NULL_TREE; | |
3432 | } | |
3433 | ||
3434 | if (!old->value) | |
3435 | old->value = val; | |
3436 | else if (val && val != old->value) | |
3437 | { | |
3438 | if (is_overloaded_fn (old->value) && is_overloaded_fn (val)) | |
3439 | old->value = merge_functions (old->value, val); | |
3440 | else | |
3441 | { | |
3442 | /* Some declarations are functions, some are not. */ | |
3443 | if (flags & LOOKUP_COMPLAIN) | |
3444 | { | |
3445 | /* If we've already given this error for this lookup, | |
3446 | old->value is error_mark_node, so let's not | |
3447 | repeat ourselves. */ | |
3448 | if (old->value != error_mark_node) | |
3449 | { | |
3450 | error ("use of `%D' is ambiguous", name); | |
3451 | cp_error_at (" first declared as `%#D' here", | |
3452 | old->value); | |
3453 | } | |
3454 | cp_error_at (" also declared as `%#D' here", val); | |
3455 | } | |
3456 | old->value = error_mark_node; | |
3457 | } | |
3458 | } | |
3459 | /* ... and copy the type. */ | |
3460 | type = new->type; | |
3461 | if (LOOKUP_NAMESPACES_ONLY (flags)) | |
3462 | type = NULL_TREE; | |
3463 | if (!old->type) | |
3464 | old->type = type; | |
3465 | else if (type && old->type != type) | |
3466 | { | |
3467 | if (flags & LOOKUP_COMPLAIN) | |
3468 | { | |
3469 | error ("`%D' denotes an ambiguous type",name); | |
3470 | error ("%J first type here", TYPE_MAIN_DECL (old->type)); | |
3471 | error ("%J other type here", TYPE_MAIN_DECL (type)); | |
3472 | } | |
3473 | } | |
3474 | return old; | |
3475 | } | |
3476 | ||
00e8de68 GDR |
3477 | /* Return the declarations that are members of the namespace NS. */ |
3478 | ||
3479 | tree | |
3480 | cp_namespace_decls (tree ns) | |
3481 | { | |
3482 | return NAMESPACE_LEVEL (ns)->names; | |
3483 | } | |
3484 | ||
3485 | /* Combine prefer_type and namespaces_only into flags. */ | |
3486 | ||
3487 | static int | |
3488 | lookup_flags (int prefer_type, int namespaces_only) | |
3489 | { | |
3490 | if (namespaces_only) | |
3491 | return LOOKUP_PREFER_NAMESPACES; | |
3492 | if (prefer_type > 1) | |
3493 | return LOOKUP_PREFER_TYPES; | |
3494 | if (prefer_type > 0) | |
3495 | return LOOKUP_PREFER_BOTH; | |
3496 | return 0; | |
3497 | } | |
3498 | ||
3499 | /* Given a lookup that returned VAL, use FLAGS to decide if we want to | |
3500 | ignore it or not. Subroutine of lookup_name_real. */ | |
3501 | ||
3502 | static tree | |
3503 | qualify_lookup (tree val, int flags) | |
3504 | { | |
3505 | if (val == NULL_TREE) | |
3506 | return val; | |
3507 | if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) | |
3508 | return val; | |
3509 | if ((flags & LOOKUP_PREFER_TYPES) | |
3510 | && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL)) | |
3511 | return val; | |
3512 | if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) | |
3513 | return NULL_TREE; | |
3514 | return val; | |
3515 | } | |
3516 | ||
3517 | /* Look up NAME in the NAMESPACE. */ | |
3518 | ||
3519 | tree | |
3520 | lookup_namespace_name (tree namespace, tree name) | |
3521 | { | |
3522 | tree val; | |
3523 | tree template_id = NULL_TREE; | |
3524 | cxx_binding binding; | |
3525 | ||
3526 | timevar_push (TV_NAME_LOOKUP); | |
3527 | my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370); | |
3528 | ||
3529 | if (TREE_CODE (name) == NAMESPACE_DECL) | |
3530 | /* This happens for A::B<int> when B is a namespace. */ | |
3531 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name); | |
3532 | else if (TREE_CODE (name) == TEMPLATE_DECL) | |
3533 | { | |
3534 | /* This happens for A::B where B is a template, and there are no | |
3535 | template arguments. */ | |
3536 | error ("invalid use of `%D'", name); | |
3537 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3538 | } | |
3539 | ||
3540 | namespace = ORIGINAL_NAMESPACE (namespace); | |
3541 | ||
3542 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) | |
3543 | { | |
3544 | template_id = name; | |
3545 | name = TREE_OPERAND (name, 0); | |
3546 | if (TREE_CODE (name) == OVERLOAD) | |
3547 | name = DECL_NAME (OVL_CURRENT (name)); | |
3548 | else if (DECL_P (name)) | |
3549 | name = DECL_NAME (name); | |
3550 | } | |
3551 | ||
3552 | my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373); | |
3553 | ||
3554 | cxx_binding_clear (&binding); | |
3555 | if (!qualified_lookup_using_namespace (name, namespace, &binding, 0)) | |
3556 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3557 | ||
3558 | if (binding.value) | |
3559 | { | |
3560 | val = binding.value; | |
3561 | ||
3562 | if (template_id) | |
3563 | { | |
3564 | if (DECL_CLASS_TEMPLATE_P (val)) | |
3565 | val = lookup_template_class (val, | |
3566 | TREE_OPERAND (template_id, 1), | |
3567 | /*in_decl=*/NULL_TREE, | |
3568 | /*context=*/NULL_TREE, | |
3569 | /*entering_scope=*/0, | |
3570 | tf_error | tf_warning); | |
3571 | else if (DECL_FUNCTION_TEMPLATE_P (val) | |
3572 | || TREE_CODE (val) == OVERLOAD) | |
3573 | val = lookup_template_function (val, | |
3574 | TREE_OPERAND (template_id, 1)); | |
3575 | else | |
3576 | { | |
3577 | error ("`%D::%D' is not a template", | |
3578 | namespace, name); | |
3579 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3580 | } | |
3581 | } | |
3582 | ||
3583 | /* If we have a single function from a using decl, pull it out. */ | |
3584 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
3585 | val = OVL_FUNCTION (val); | |
3586 | ||
3587 | /* Ignore built-in functions that haven't been prototyped yet. */ | |
3588 | if (!val || !DECL_P(val) | |
3589 | || !DECL_LANG_SPECIFIC(val) | |
3590 | || !DECL_ANTICIPATED (val)) | |
3591 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); | |
3592 | } | |
3593 | ||
3594 | error ("`%D' undeclared in namespace `%D'", name, namespace); | |
3595 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3596 | } | |
3597 | ||
3598 | /* Select the right _DECL from multiple choices. */ | |
3599 | ||
3600 | static tree | |
3601 | select_decl (cxx_binding *binding, int flags) | |
3602 | { | |
3603 | tree val; | |
3604 | val = binding->value; | |
3605 | ||
3606 | timevar_push (TV_NAME_LOOKUP); | |
3607 | if (LOOKUP_NAMESPACES_ONLY (flags)) | |
3608 | { | |
3609 | /* We are not interested in types. */ | |
3610 | if (val && TREE_CODE (val) == NAMESPACE_DECL) | |
3611 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); | |
3612 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
3613 | } | |
3614 | ||
3615 | /* If looking for a type, or if there is no non-type binding, select | |
3616 | the value binding. */ | |
3617 | if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES))) | |
3618 | val = binding->type; | |
3619 | /* Don't return non-types if we really prefer types. */ | |
3620 | else if (val && LOOKUP_TYPES_ONLY (flags) && TREE_CODE (val) != TYPE_DECL | |
3621 | && (TREE_CODE (val) != TEMPLATE_DECL | |
3622 | || !DECL_CLASS_TEMPLATE_P (val))) | |
3623 | val = NULL_TREE; | |
3624 | ||
3625 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); | |
3626 | } | |
3627 | ||
3628 | /* Unscoped lookup of a global: iterate over current namespaces, | |
543ebd4a | 3629 | considering using-directives. */ |
00e8de68 | 3630 | |
a5e6b29b | 3631 | static tree |
543ebd4a | 3632 | unqualified_namespace_lookup (tree name, int flags) |
00e8de68 GDR |
3633 | { |
3634 | tree initial = current_decl_namespace (); | |
3635 | tree scope = initial; | |
3636 | tree siter; | |
3637 | struct cp_binding_level *level; | |
3638 | tree val = NULL_TREE; | |
3639 | cxx_binding binding; | |
3640 | ||
3641 | timevar_push (TV_NAME_LOOKUP); | |
3642 | cxx_binding_clear (&binding); | |
00e8de68 GDR |
3643 | |
3644 | for (; !val; scope = CP_DECL_CONTEXT (scope)) | |
3645 | { | |
3646 | cxx_binding *b = | |
3647 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); | |
00e8de68 GDR |
3648 | |
3649 | /* Ignore anticipated built-in functions. */ | |
3650 | if (b && b->value && DECL_P (b->value) | |
3651 | && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value)) | |
3652 | /* Keep binding cleared. */; | |
3653 | else if (b) | |
3654 | { | |
3655 | /* Initialize binding for this context. */ | |
3656 | binding.value = b->value; | |
3657 | binding.type = b->type; | |
3658 | } | |
3659 | ||
3660 | /* Add all _DECLs seen through local using-directives. */ | |
3661 | for (level = current_binding_level; | |
3662 | level->kind != sk_namespace; | |
3663 | level = level->level_chain) | |
3664 | if (!lookup_using_namespace (name, &binding, level->using_directives, | |
543ebd4a | 3665 | scope, flags)) |
00e8de68 GDR |
3666 | /* Give up because of error. */ |
3667 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3668 | ||
3669 | /* Add all _DECLs seen through global using-directives. */ | |
3670 | /* XXX local and global using lists should work equally. */ | |
3671 | siter = initial; | |
3672 | while (1) | |
3673 | { | |
3674 | if (!lookup_using_namespace (name, &binding, | |
3675 | DECL_NAMESPACE_USING (siter), | |
543ebd4a | 3676 | scope, flags)) |
00e8de68 GDR |
3677 | /* Give up because of error. */ |
3678 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); | |
3679 | if (siter == scope) break; | |
3680 | siter = CP_DECL_CONTEXT (siter); | |
3681 | } | |
3682 | ||
3683 | val = select_decl (&binding, flags); | |
3684 | if (scope == global_namespace) | |
3685 | break; | |
3686 | } | |
3687 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); | |
3688 | } | |
3689 | ||
3690 | /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL | |
3691 | or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type | |
3692 | bindings. | |
3693 | ||
3694 | Returns a DECL (or OVERLOAD, or BASELINK) representing the | |
3695 | declaration found. If no suitable declaration can be found, | |
3696 | ERROR_MARK_NODE is returned. Iif COMPLAIN is true and SCOPE is | |
3697 | neither a class-type nor a namespace a diagnostic is issued. */ | |
3698 | ||
3699 | tree | |
3700 | lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain) | |
3701 | { | |
3702 | int flags = 0; | |
3703 | ||
3704 | if (TREE_CODE (scope) == NAMESPACE_DECL) | |
3705 | { | |
3706 | cxx_binding binding; | |
3707 | ||
3708 | cxx_binding_clear (&binding); | |
3709 | flags |= LOOKUP_COMPLAIN; | |
3710 | if (is_type_p) | |
3711 | flags |= LOOKUP_PREFER_TYPES; | |
543ebd4a | 3712 | if (qualified_lookup_using_namespace (name, scope, &binding, flags)) |
00e8de68 GDR |
3713 | return select_decl (&binding, flags); |
3714 | } | |
3715 | else if (is_aggr_type (scope, complain)) | |
3716 | { | |
3717 | tree t; | |
3718 | t = lookup_member (scope, name, 0, is_type_p); | |
3719 | if (t) | |
3720 | return t; | |
3721 | } | |
3722 | ||
3723 | return error_mark_node; | |
3724 | } | |
3725 | ||
cd0be382 | 3726 | /* Subroutine of unqualified_namespace_lookup: |
5a167978 GDR |
3727 | Add the bindings of NAME in used namespaces to VAL. |
3728 | We are currently looking for names in namespace SCOPE, so we | |
3729 | look through USINGS for using-directives of namespaces | |
3730 | which have SCOPE as a common ancestor with the current scope. | |
3731 | Returns false on errors. */ | |
3732 | ||
a5e6b29b | 3733 | static bool |
5a167978 | 3734 | lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope, |
543ebd4a | 3735 | int flags) |
5a167978 GDR |
3736 | { |
3737 | tree iter; | |
3738 | timevar_push (TV_NAME_LOOKUP); | |
3739 | /* Iterate over all used namespaces in current, searching for using | |
3740 | directives of scope. */ | |
3741 | for (iter = usings; iter; iter = TREE_CHAIN (iter)) | |
3742 | if (TREE_VALUE (iter) == scope) | |
3743 | { | |
3744 | tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter)); | |
3745 | cxx_binding *val1 = | |
3746 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name); | |
5a167978 GDR |
3747 | /* Resolve ambiguities. */ |
3748 | if (val1) | |
3749 | val = ambiguous_decl (name, val, val1, flags); | |
3750 | } | |
3751 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node); | |
3752 | } | |
3753 | ||
3754 | /* [namespace.qual] | |
3755 | Accepts the NAME to lookup and its qualifying SCOPE. | |
3756 | Returns the name/type pair found into the cxx_binding *RESULT, | |
3757 | or false on error. */ | |
3758 | ||
a5e6b29b | 3759 | static bool |
5a167978 GDR |
3760 | qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result, |
3761 | int flags) | |
3762 | { | |
3763 | /* Maintain a list of namespaces visited... */ | |
3764 | tree seen = NULL_TREE; | |
3765 | /* ... and a list of namespace yet to see. */ | |
3766 | tree todo = NULL_TREE; | |
dc55c941 | 3767 | tree todo_maybe = NULL_TREE; |
5a167978 GDR |
3768 | tree usings; |
3769 | timevar_push (TV_NAME_LOOKUP); | |
3770 | /* Look through namespace aliases. */ | |
3771 | scope = ORIGINAL_NAMESPACE (scope); | |
3772 | while (scope && result->value != error_mark_node) | |
3773 | { | |
3774 | cxx_binding *binding = | |
dc55c941 | 3775 | cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); |
5a167978 GDR |
3776 | seen = tree_cons (scope, NULL_TREE, seen); |
3777 | if (binding) | |
3778 | result = ambiguous_decl (name, result, binding, flags); | |
c404ab02 AO |
3779 | |
3780 | /* Consider strong using directives always, and non-strong ones | |
3781 | if we haven't found a binding yet. ??? Shouldn't we consider | |
3782 | non-strong ones if the initial RESULT is non-NULL, but the | |
3783 | binding in the given namespace is? */ | |
3784 | for (usings = DECL_NAMESPACE_USING (scope); usings; | |
3785 | usings = TREE_CHAIN (usings)) | |
3786 | /* If this was a real directive, and we have not seen it. */ | |
dc55c941 AO |
3787 | if (!TREE_INDIRECT_USING (usings)) |
3788 | { | |
3789 | /* Try to avoid queuing the same namespace more than once, | |
3790 | the exception being when a namespace was already | |
3791 | enqueued for todo_maybe and then a strong using is | |
3792 | found for it. We could try to remove it from | |
3793 | todo_maybe, but it's probably not worth the effort. */ | |
3794 | if (is_associated_namespace (scope, TREE_PURPOSE (usings)) | |
3795 | && !purpose_member (TREE_PURPOSE (usings), seen) | |
3796 | && !purpose_member (TREE_PURPOSE (usings), todo)) | |
3797 | todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo); | |
3798 | else if ((!result->value && !result->type) | |
3799 | && !purpose_member (TREE_PURPOSE (usings), seen) | |
3800 | && !purpose_member (TREE_PURPOSE (usings), todo) | |
3801 | && !purpose_member (TREE_PURPOSE (usings), todo_maybe)) | |
3802 | todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE, | |
3803 | todo_maybe); | |
3804 | } | |
5a167978 GDR |
3805 | if (todo) |
3806 | { | |
3807 | scope = TREE_PURPOSE (todo); | |
3808 | todo = TREE_CHAIN (todo); | |
3809 | } | |
dc55c941 AO |
3810 | else if (todo_maybe |
3811 | && (!result->value && !result->type)) | |
3812 | { | |
3813 | scope = TREE_PURPOSE (todo_maybe); | |
3814 | todo = TREE_CHAIN (todo_maybe); | |
3815 | todo_maybe = NULL_TREE; | |
3816 | } | |
5a167978 GDR |
3817 | else |
3818 | scope = NULL_TREE; /* If there never was a todo list. */ | |
3819 | } | |
3820 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node); | |
3821 | } | |
3822 | ||
00e8de68 GDR |
3823 | /* Look up NAME in the current binding level and its superiors in the |
3824 | namespace of variables, functions and typedefs. Return a ..._DECL | |
3825 | node of some kind representing its definition if there is only one | |
3826 | such declaration, or return a TREE_LIST with all the overloaded | |
3827 | definitions if there are many, or return 0 if it is undefined. | |
3828 | ||
3829 | If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. | |
3830 | If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). | |
3831 | Otherwise we prefer non-TYPE_DECLs. | |
3832 | ||
3833 | If NONCLASS is nonzero, we don't look for the NAME in class scope, | |
3834 | using IDENTIFIER_CLASS_VALUE. */ | |
3835 | ||
3836 | tree | |
3837 | lookup_name_real (tree name, int prefer_type, int nonclass, | |
3838 | int namespaces_only, int flags) | |
3839 | { | |
3840 | cxx_binding *iter; | |
3841 | tree val = NULL_TREE; | |
3842 | ||
3843 | timevar_push (TV_NAME_LOOKUP); | |
3844 | /* Conversion operators are handled specially because ordinary | |
3845 | unqualified name lookup will not find template conversion | |
3846 | operators. */ | |
3847 | if (IDENTIFIER_TYPENAME_P (name)) | |
3848 | { | |
3849 | struct cp_binding_level *level; | |
3850 | ||
3851 | for (level = current_binding_level; | |
3852 | level && level->kind != sk_namespace; | |
3853 | level = level->level_chain) | |
3854 | { | |
3855 | tree class_type; | |
3856 | tree operators; | |
3857 | ||
3858 | /* A conversion operator can only be declared in a class | |
3859 | scope. */ | |
3860 | if (level->kind != sk_class) | |
3861 | continue; | |
3862 | ||
3863 | /* Lookup the conversion operator in the class. */ | |
3864 | class_type = level->this_entity; | |
3865 | operators = lookup_fnfields (class_type, name, /*protect=*/0); | |
3866 | if (operators) | |
3867 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators); | |
3868 | } | |
3869 | ||
3870 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
3871 | } | |
3872 | ||
3873 | flags |= lookup_flags (prefer_type, namespaces_only); | |
3874 | ||
3875 | /* First, look in non-namespace scopes. */ | |
3876 | ||
3877 | if (current_class_type == NULL_TREE) | |
3878 | nonclass = 1; | |
3879 | ||
3880 | for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous) | |
3881 | { | |
3882 | tree binding; | |
3883 | ||
3884 | if (!LOCAL_BINDING_P (iter) && nonclass) | |
3885 | /* We're not looking for class-scoped bindings, so keep going. */ | |
3886 | continue; | |
3887 | ||
3888 | /* If this is the kind of thing we're looking for, we're done. */ | |
3889 | if (qualify_lookup (iter->value, flags)) | |
3890 | binding = iter->value; | |
3891 | else if ((flags & LOOKUP_PREFER_TYPES) | |
3892 | && qualify_lookup (iter->type, flags)) | |
3893 | binding = iter->type; | |
3894 | else | |
3895 | binding = NULL_TREE; | |
3896 | ||
3897 | if (binding) | |
3898 | { | |
3899 | val = binding; | |
3900 | break; | |
3901 | } | |
3902 | } | |
3903 | ||
3904 | /* Now lookup in namespace scopes. */ | |
3905 | if (!val) | |
3906 | { | |
543ebd4a | 3907 | tree t = unqualified_namespace_lookup (name, flags); |
00e8de68 GDR |
3908 | if (t) |
3909 | val = t; | |
3910 | } | |
3911 | ||
3912 | if (val) | |
3913 | { | |
3914 | /* If we have a single function from a using decl, pull it out. */ | |
3915 | if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val)) | |
3916 | val = OVL_FUNCTION (val); | |
3917 | } | |
3918 | ||
3919 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); | |
3920 | } | |
3921 | ||
3922 | tree | |
3923 | lookup_name_nonclass (tree name) | |
3924 | { | |
3925 | return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN); | |
3926 | } | |
3927 | ||
3928 | tree | |
3929 | lookup_function_nonclass (tree name, tree args) | |
3930 | { | |
3931 | return lookup_arg_dependent (name, lookup_name_nonclass (name), args); | |
3932 | } | |
3933 | ||
3934 | tree | |
3935 | lookup_name (tree name, int prefer_type) | |
3936 | { | |
3937 | return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN); | |
3938 | } | |
3939 | ||
3940 | /* Similar to `lookup_name' but look only in the innermost non-class | |
3941 | binding level. */ | |
3942 | ||
a5e6b29b | 3943 | static tree |
00e8de68 GDR |
3944 | lookup_name_current_level (tree name) |
3945 | { | |
3946 | struct cp_binding_level *b; | |
3947 | tree t = NULL_TREE; | |
3948 | ||
3949 | timevar_push (TV_NAME_LOOKUP); | |
3950 | b = innermost_nonclass_level (); | |
3951 | ||
3952 | if (b->kind == sk_namespace) | |
3953 | { | |
3954 | t = IDENTIFIER_NAMESPACE_VALUE (name); | |
3955 | ||
3956 | /* extern "C" function() */ | |
3957 | if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) | |
3958 | t = TREE_VALUE (t); | |
3959 | } | |
3960 | else if (IDENTIFIER_BINDING (name) | |
3961 | && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) | |
3962 | { | |
3963 | while (1) | |
3964 | { | |
3965 | if (IDENTIFIER_BINDING (name)->scope == b) | |
3966 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name)); | |
3967 | ||
3968 | if (b->kind == sk_cleanup) | |
3969 | b = b->level_chain; | |
3970 | else | |
3971 | break; | |
3972 | } | |
3973 | } | |
3974 | ||
3975 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
3976 | } | |
3977 | ||
3978 | /* Like lookup_name_current_level, but for types. */ | |
3979 | ||
a5e6b29b | 3980 | static tree |
00e8de68 GDR |
3981 | lookup_type_current_level (tree name) |
3982 | { | |
926ce8bd | 3983 | tree t = NULL_TREE; |
00e8de68 GDR |
3984 | |
3985 | timevar_push (TV_NAME_LOOKUP); | |
3986 | my_friendly_assert (current_binding_level->kind != sk_namespace, | |
3987 | 980716); | |
3988 | ||
3989 | if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE | |
3990 | && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) | |
3991 | { | |
3992 | struct cp_binding_level *b = current_binding_level; | |
3993 | while (1) | |
3994 | { | |
3995 | if (purpose_member (name, b->type_shadowed)) | |
3996 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, | |
3997 | REAL_IDENTIFIER_TYPE_VALUE (name)); | |
3998 | if (b->kind == sk_cleanup) | |
3999 | b = b->level_chain; | |
4000 | else | |
4001 | break; | |
4002 | } | |
4003 | } | |
4004 | ||
4005 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); | |
4006 | } | |
4007 | ||
5a167978 GDR |
4008 | /* [basic.lookup.koenig] */ |
4009 | /* A nonzero return value in the functions below indicates an error. */ | |
4010 | ||
4011 | struct arg_lookup | |
4012 | { | |
4013 | tree name; | |
4014 | tree namespaces; | |
4015 | tree classes; | |
4016 | tree functions; | |
4017 | }; | |
4018 | ||
4019 | static bool arg_assoc (struct arg_lookup*, tree); | |
4020 | static bool arg_assoc_args (struct arg_lookup*, tree); | |
4021 | static bool arg_assoc_type (struct arg_lookup*, tree); | |
4022 | static bool add_function (struct arg_lookup *, tree); | |
4023 | static bool arg_assoc_namespace (struct arg_lookup *, tree); | |
4024 | static bool arg_assoc_class (struct arg_lookup *, tree); | |
4025 | static bool arg_assoc_template_arg (struct arg_lookup*, tree); | |
4026 | ||
4027 | /* Add a function to the lookup structure. | |
4028 | Returns true on error. */ | |
4029 | ||
4030 | static bool | |
4031 | add_function (struct arg_lookup *k, tree fn) | |
4032 | { | |
4033 | /* We used to check here to see if the function was already in the list, | |
4034 | but that's O(n^2), which is just too expensive for function lookup. | |
4035 | Now we deal with the occasional duplicate in joust. In doing this, we | |
4036 | assume that the number of duplicates will be small compared to the | |
4037 | total number of functions being compared, which should usually be the | |
4038 | case. */ | |
4039 | ||
4040 | /* We must find only functions, or exactly one non-function. */ | |
4041 | if (!k->functions) | |
4042 | k->functions = fn; | |
4043 | else if (fn == k->functions) | |
4044 | ; | |
4045 | else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn)) | |
4046 | k->functions = build_overload (fn, k->functions); | |
4047 | else | |
4048 | { | |
4049 | tree f1 = OVL_CURRENT (k->functions); | |
4050 | tree f2 = fn; | |
4051 | if (is_overloaded_fn (f1)) | |
4052 | { | |
4053 | fn = f1; f1 = f2; f2 = fn; | |
4054 | } | |
4055 | cp_error_at ("`%D' is not a function,", f1); | |
4056 | cp_error_at (" conflict with `%D'", f2); | |
4057 | error (" in call to `%D'", k->name); | |
4058 | return true; | |
4059 | } | |
4060 | ||
4061 | return false; | |
4062 | } | |
4063 | ||
86098eb8 JM |
4064 | /* Returns true iff CURRENT has declared itself to be an associated |
4065 | namespace of SCOPE via a strong using-directive (or transitive chain | |
4066 | thereof). Both are namespaces. */ | |
4067 | ||
4068 | bool | |
4069 | is_associated_namespace (tree current, tree scope) | |
4070 | { | |
4071 | tree seen = NULL_TREE; | |
4072 | tree todo = NULL_TREE; | |
4073 | tree t; | |
4074 | while (1) | |
4075 | { | |
4076 | if (scope == current) | |
4077 | return true; | |
4078 | seen = tree_cons (scope, NULL_TREE, seen); | |
4079 | for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t)) | |
4080 | if (!purpose_member (TREE_PURPOSE (t), seen)) | |
4081 | todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo); | |
4082 | if (todo) | |
4083 | { | |
4084 | scope = TREE_PURPOSE (todo); | |
4085 | todo = TREE_CHAIN (todo); | |
4086 | } | |
4087 | else | |
4088 | return false; | |
4089 | } | |
4090 | } | |
4091 | ||
5a167978 GDR |
4092 | /* Add functions of a namespace to the lookup structure. |
4093 | Returns true on error. */ | |
4094 | ||
4095 | static bool | |
4096 | arg_assoc_namespace (struct arg_lookup *k, tree scope) | |
4097 | { | |
4098 | tree value; | |
4099 | ||
4100 | if (purpose_member (scope, k->namespaces)) | |
4101 | return 0; | |
4102 | k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces); | |
86098eb8 JM |
4103 | |
4104 | /* Check out our super-users. */ | |
4105 | for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value; | |
4106 | value = TREE_CHAIN (value)) | |
4107 | if (arg_assoc_namespace (k, TREE_PURPOSE (value))) | |
4108 | return true; | |
5a167978 GDR |
4109 | |
4110 | value = namespace_binding (k->name, scope); | |
4111 | if (!value) | |
4112 | return false; | |
4113 | ||
4114 | for (; value; value = OVL_NEXT (value)) | |
4115 | if (add_function (k, OVL_CURRENT (value))) | |
4116 | return true; | |
4117 | ||
4118 | return false; | |
4119 | } | |
4120 | ||
4121 | /* Adds everything associated with a template argument to the lookup | |
4122 | structure. Returns true on error. */ | |
4123 | ||
4124 | static bool | |
4125 | arg_assoc_template_arg (struct arg_lookup *k, tree arg) | |
4126 | { | |
4127 | /* [basic.lookup.koenig] | |
4128 | ||
4129 | If T is a template-id, its associated namespaces and classes are | |
4130 | ... the namespaces and classes associated with the types of the | |
4131 | template arguments provided for template type parameters | |
4132 | (excluding template template parameters); the namespaces in which | |
4133 | any template template arguments are defined; and the classes in | |
4134 | which any member templates used as template template arguments | |
4135 | are defined. [Note: non-type template arguments do not | |
4136 | contribute to the set of associated namespaces. ] */ | |
4137 | ||
4138 | /* Consider first template template arguments. */ | |
4139 | if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM | |
4140 | || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) | |
4141 | return false; | |
4142 | else if (TREE_CODE (arg) == TEMPLATE_DECL) | |
4143 | { | |
4144 | tree ctx = CP_DECL_CONTEXT (arg); | |
4145 | ||
4146 | /* It's not a member template. */ | |
4147 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
4148 | return arg_assoc_namespace (k, ctx); | |
4149 | /* Otherwise, it must be member template. */ | |
4150 | else | |
4151 | return arg_assoc_class (k, ctx); | |
4152 | } | |
4153 | /* It's not a template template argument, but it is a type template | |
4154 | argument. */ | |
4155 | else if (TYPE_P (arg)) | |
4156 | return arg_assoc_type (k, arg); | |
4157 | /* It's a non-type template argument. */ | |
4158 | else | |
4159 | return false; | |
4160 | } | |
4161 | ||
4162 | /* Adds everything associated with class to the lookup structure. | |
4163 | Returns true on error. */ | |
4164 | ||
4165 | static bool | |
4166 | arg_assoc_class (struct arg_lookup *k, tree type) | |
4167 | { | |
4168 | tree list, friends, context; | |
4169 | int i; | |
4170 | ||
4171 | /* Backend build structures, such as __builtin_va_list, aren't | |
4172 | affected by all this. */ | |
4173 | if (!CLASS_TYPE_P (type)) | |
4174 | return false; | |
4175 | ||
4176 | if (purpose_member (type, k->classes)) | |
4177 | return false; | |
4178 | k->classes = tree_cons (type, NULL_TREE, k->classes); | |
4179 | ||
4180 | context = decl_namespace (TYPE_MAIN_DECL (type)); | |
4181 | if (arg_assoc_namespace (k, context)) | |
4182 | return true; | |
4183 | ||
4184 | /* Process baseclasses. */ | |
4185 | for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++) | |
4186 | if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i))) | |
4187 | return true; | |
4188 | ||
4189 | /* Process friends. */ | |
4190 | for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; | |
4191 | list = TREE_CHAIN (list)) | |
4192 | if (k->name == FRIEND_NAME (list)) | |
4193 | for (friends = FRIEND_DECLS (list); friends; | |
4194 | friends = TREE_CHAIN (friends)) | |
4195 | /* Only interested in global functions with potentially hidden | |
4196 | (i.e. unqualified) declarations. */ | |
4197 | if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context) | |
4198 | if (add_function (k, TREE_VALUE (friends))) | |
4199 | return true; | |
4200 | ||
4201 | /* Process template arguments. */ | |
4202 | if (CLASSTYPE_TEMPLATE_INFO (type)) | |
4203 | { | |
4204 | list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); | |
4205 | for (i = 0; i < TREE_VEC_LENGTH (list); ++i) | |
4206 | arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)); | |
4207 | } | |
4208 | ||
4209 | return false; | |
4210 | } | |
4211 | ||
4212 | /* Adds everything associated with a given type. | |
4213 | Returns 1 on error. */ | |
4214 | ||
4215 | static bool | |
4216 | arg_assoc_type (struct arg_lookup *k, tree type) | |
4217 | { | |
4218 | /* As we do not get the type of non-type dependent expressions | |
4219 | right, we can end up with such things without a type. */ | |
4220 | if (!type) | |
4221 | return false; | |
4222 | ||
4223 | if (TYPE_PTRMEM_P (type)) | |
4224 | { | |
4225 | /* Pointer to member: associate class type and value type. */ | |
4226 | if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type))) | |
4227 | return true; | |
4228 | return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type)); | |
4229 | } | |
4230 | else switch (TREE_CODE (type)) | |
4231 | { | |
4232 | case ERROR_MARK: | |
4233 | return false; | |
4234 | case VOID_TYPE: | |
4235 | case INTEGER_TYPE: | |
4236 | case REAL_TYPE: | |
4237 | case COMPLEX_TYPE: | |
4238 | case VECTOR_TYPE: | |
4239 | case CHAR_TYPE: | |
4240 | case BOOLEAN_TYPE: | |
4241 | return false; | |
4242 | case RECORD_TYPE: | |
4243 | if (TYPE_PTRMEMFUNC_P (type)) | |
4244 | return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); | |
4245 | return arg_assoc_class (k, type); | |
4246 | case POINTER_TYPE: | |
4247 | case REFERENCE_TYPE: | |
4248 | case ARRAY_TYPE: | |
4249 | return arg_assoc_type (k, TREE_TYPE (type)); | |
4250 | case UNION_TYPE: | |
4251 | case ENUMERAL_TYPE: | |
4252 | return arg_assoc_namespace (k, decl_namespace (TYPE_MAIN_DECL (type))); | |
4253 | case METHOD_TYPE: | |
4254 | /* The basetype is referenced in the first arg type, so just | |
4255 | fall through. */ | |
4256 | case FUNCTION_TYPE: | |
4257 | /* Associate the parameter types. */ | |
4258 | if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) | |
4259 | return true; | |
4260 | /* Associate the return type. */ | |
4261 | return arg_assoc_type (k, TREE_TYPE (type)); | |
4262 | case TEMPLATE_TYPE_PARM: | |
4263 | case BOUND_TEMPLATE_TEMPLATE_PARM: | |
4264 | return false; | |
4265 | case TYPENAME_TYPE: | |
4266 | return false; | |
4267 | case LANG_TYPE: | |
4268 | if (type == unknown_type_node) | |
4269 | return false; | |
4270 | /* else fall through */ | |
4271 | default: | |
4272 | abort (); | |
4273 | } | |
4274 | return false; | |
4275 | } | |
4276 | ||
4277 | /* Adds everything associated with arguments. Returns true on error. */ | |
4278 | ||
4279 | static bool | |
4280 | arg_assoc_args (struct arg_lookup *k, tree args) | |
4281 | { | |
4282 | for (; args; args = TREE_CHAIN (args)) | |
4283 | if (arg_assoc (k, TREE_VALUE (args))) | |
4284 | return true; | |
4285 | return false; | |
4286 | } | |
4287 | ||
4288 | /* Adds everything associated with a given tree_node. Returns 1 on error. */ | |
4289 | ||
4290 | static bool | |
4291 | arg_assoc (struct arg_lookup *k, tree n) | |
4292 | { | |
4293 | if (n == error_mark_node) | |
4294 | return false; | |
4295 | ||
4296 | if (TYPE_P (n)) | |
4297 | return arg_assoc_type (k, n); | |
4298 | ||
4299 | if (! type_unknown_p (n)) | |
4300 | return arg_assoc_type (k, TREE_TYPE (n)); | |
4301 | ||
4302 | if (TREE_CODE (n) == ADDR_EXPR) | |
4303 | n = TREE_OPERAND (n, 0); | |
4304 | if (TREE_CODE (n) == COMPONENT_REF) | |
4305 | n = TREE_OPERAND (n, 1); | |
4306 | if (TREE_CODE (n) == OFFSET_REF) | |
4307 | n = TREE_OPERAND (n, 1); | |
4308 | while (TREE_CODE (n) == TREE_LIST) | |
4309 | n = TREE_VALUE (n); | |
4310 | if (TREE_CODE (n) == BASELINK) | |
4311 | n = BASELINK_FUNCTIONS (n); | |
4312 | ||
4313 | if (TREE_CODE (n) == FUNCTION_DECL) | |
4314 | return arg_assoc_type (k, TREE_TYPE (n)); | |
4315 | if (TREE_CODE (n) == TEMPLATE_ID_EXPR) | |
4316 | { | |
4317 | /* [basic.lookup.koenig] | |
4318 | ||
4319 | If T is a template-id, its associated namespaces and classes | |
4320 | are the namespace in which the template is defined; for | |
4321 | member templates, the member template's class... */ | |
4322 | tree template = TREE_OPERAND (n, 0); | |
4323 | tree args = TREE_OPERAND (n, 1); | |
4324 | tree ctx; | |
4325 | int ix; | |
4326 | ||
4327 | if (TREE_CODE (template) == COMPONENT_REF) | |
4328 | template = TREE_OPERAND (template, 1); | |
4329 | ||
4330 | /* First, the template. There may actually be more than one if | |
4331 | this is an overloaded function template. But, in that case, | |
4332 | we only need the first; all the functions will be in the same | |
4333 | namespace. */ | |
4334 | template = OVL_CURRENT (template); | |
4335 | ||
4336 | ctx = CP_DECL_CONTEXT (template); | |
4337 | ||
4338 | if (TREE_CODE (ctx) == NAMESPACE_DECL) | |
4339 | { | |
4340 | if (arg_assoc_namespace (k, ctx) == 1) | |
4341 | return true; | |
4342 | } | |
4343 | /* It must be a member template. */ | |
4344 | else if (arg_assoc_class (k, ctx) == 1) | |
4345 | return true; | |
4346 | ||
4347 | /* Now the arguments. */ | |
4348 | for (ix = TREE_VEC_LENGTH (args); ix--;) | |
4349 | if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1) | |
4350 | return true; | |
4351 | } | |
4352 | else | |
4353 | { | |
4354 | my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715); | |
4355 | ||
4356 | for (; n; n = OVL_CHAIN (n)) | |
4357 | if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n)))) | |
4358 | return true; | |
4359 | } | |
4360 | ||
4361 | return false; | |
4362 | } | |
4363 | ||
4364 | /* Performs Koenig lookup depending on arguments, where fns | |
4365 | are the functions found in normal lookup. */ | |
4366 | ||
4367 | tree | |
4368 | lookup_arg_dependent (tree name, tree fns, tree args) | |
4369 | { | |
4370 | struct arg_lookup k; | |
4371 | tree fn = NULL_TREE; | |
4372 | ||
4373 | timevar_push (TV_NAME_LOOKUP); | |
4374 | k.name = name; | |
4375 | k.functions = fns; | |
4376 | k.classes = NULL_TREE; | |
4377 | ||
543ebd4a MM |
4378 | /* We've already looked at some namespaces during normal unqualified |
4379 | lookup -- but we don't know exactly which ones. If the functions | |
4380 | we found were brought into the current namespace via a using | |
4381 | declaration, we have not really checked the namespace from which | |
4382 | they came. Therefore, we check all namespaces here -- unless the | |
4383 | function we have is from the current namespace. */ | |
5a167978 GDR |
4384 | if (fns) |
4385 | fn = OVL_CURRENT (fns); | |
543ebd4a MM |
4386 | if (fn && TREE_CODE (fn) == FUNCTION_DECL |
4387 | && CP_DECL_CONTEXT (fn) != current_decl_namespace ()) | |
5a167978 GDR |
4388 | k.namespaces = NULL_TREE; |
4389 | else | |
543ebd4a MM |
4390 | /* Setting NAMESPACES is purely an optimization; it prevents |
4391 | adding functions which are already in FNS. Adding them would | |
4392 | be safe -- "joust" will eliminate the duplicates -- but | |
4393 | wasteful. */ | |
4394 | k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE); | |
5a167978 GDR |
4395 | |
4396 | arg_assoc_args (&k, args); | |
4397 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions); | |
4398 | } | |
4399 | ||
00e8de68 GDR |
4400 | /* Add namespace to using_directives. Return NULL_TREE if nothing was |
4401 | changed (i.e. there was already a directive), or the fresh | |
4402 | TREE_LIST otherwise. */ | |
4403 | ||
a5e6b29b | 4404 | static tree |
00e8de68 GDR |
4405 | push_using_directive (tree used) |
4406 | { | |
4407 | tree ud = current_binding_level->using_directives; | |
4408 | tree iter, ancestor; | |
4409 | ||
4410 | timevar_push (TV_NAME_LOOKUP); | |
4411 | /* Check if we already have this. */ | |
4412 | if (purpose_member (used, ud) != NULL_TREE) | |
4413 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); | |
4414 | ||
4415 | ancestor = namespace_ancestor (current_decl_namespace (), used); | |
4416 | ud = current_binding_level->using_directives; | |
4417 | ud = tree_cons (used, ancestor, ud); | |
4418 | current_binding_level->using_directives = ud; | |
4419 | ||
4420 | /* Recursively add all namespaces used. */ | |
4421 | for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) | |
4422 | push_using_directive (TREE_PURPOSE (iter)); | |
4423 | ||
4424 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud); | |
4425 | } | |
4426 | ||
4427 | /* The type TYPE is being declared. If it is a class template, or a | |
4428 | specialization of a class template, do any processing required and | |
4429 | perform error-checking. If IS_FRIEND is nonzero, this TYPE is | |
4430 | being declared a friend. B is the binding level at which this TYPE | |
4431 | should be bound. | |
4432 | ||
4433 | Returns the TYPE_DECL for TYPE, which may have been altered by this | |
4434 | processing. */ | |
4435 | ||
4436 | static tree | |
4437 | maybe_process_template_type_declaration (tree type, int globalize, | |
4438 | cxx_scope *b) | |
4439 | { | |
4440 | tree decl = TYPE_NAME (type); | |
4441 | ||
4442 | if (processing_template_parmlist) | |
4443 | /* You can't declare a new template type in a template parameter | |
4444 | list. But, you can declare a non-template type: | |
4445 | ||
4446 | template <class A*> struct S; | |
4447 | ||
4448 | is a forward-declaration of `A'. */ | |
4449 | ; | |
4450 | else | |
4451 | { | |
4452 | maybe_check_template_type (type); | |
4453 | ||
4454 | my_friendly_assert (IS_AGGR_TYPE (type) | |
4455 | || TREE_CODE (type) == ENUMERAL_TYPE, 0); | |
4456 | ||
4457 | ||
4458 | if (processing_template_decl) | |
4459 | { | |
4460 | /* This may change after the call to | |
4461 | push_template_decl_real, but we want the original value. */ | |
4462 | tree name = DECL_NAME (decl); | |
4463 | ||
4464 | decl = push_template_decl_real (decl, globalize); | |
4465 | /* If the current binding level is the binding level for the | |
4466 | template parameters (see the comment in | |
4467 | begin_template_parm_list) and the enclosing level is a class | |
4468 | scope, and we're not looking at a friend, push the | |
4469 | declaration of the member class into the class scope. In the | |
4470 | friend case, push_template_decl will already have put the | |
4471 | friend into global scope, if appropriate. */ | |
4472 | if (TREE_CODE (type) != ENUMERAL_TYPE | |
4473 | && !globalize && b->kind == sk_template_parms | |
4474 | && b->level_chain->kind == sk_class) | |
4475 | { | |
4476 | finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); | |
4477 | /* Put this UDT in the table of UDTs for the class, since | |
4478 | that won't happen below because B is not the class | |
4479 | binding level, but is instead the pseudo-global level. */ | |
4480 | if (b->level_chain->type_decls == NULL) | |
4481 | b->level_chain->type_decls = | |
4482 | binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
4483 | binding_table_insert (b->level_chain->type_decls, name, type); | |
4484 | if (!COMPLETE_TYPE_P (current_class_type)) | |
4485 | { | |
4486 | maybe_add_class_template_decl_list (current_class_type, | |
4487 | type, /*friend_p=*/0); | |
4488 | CLASSTYPE_NESTED_UTDS (current_class_type) = | |
4489 | b->level_chain->type_decls; | |
4490 | } | |
4491 | } | |
4492 | } | |
4493 | } | |
4494 | ||
4495 | return decl; | |
4496 | } | |
4497 | ||
4498 | /* Push a tag name NAME for struct/class/union/enum type TYPE. | |
4499 | Normally put it into the inner-most non-sk_cleanup scope, | |
4500 | but if GLOBALIZE is true, put it in the inner-most non-class scope. | |
4501 | The latter is needed for implicit declarations. */ | |
4502 | ||
4503 | void | |
4504 | pushtag (tree name, tree type, int globalize) | |
4505 | { | |
926ce8bd | 4506 | struct cp_binding_level *b; |
00e8de68 GDR |
4507 | |
4508 | timevar_push (TV_NAME_LOOKUP); | |
4509 | b = current_binding_level; | |
4510 | while (b->kind == sk_cleanup | |
4511 | || (b->kind == sk_class | |
4512 | && (globalize | |
4513 | /* We may be defining a new type in the initializer | |
4514 | of a static member variable. We allow this when | |
4515 | not pedantic, and it is particularly useful for | |
4516 | type punning via an anonymous union. */ | |
4517 | || COMPLETE_TYPE_P (b->this_entity)))) | |
4518 | b = b->level_chain; | |
4519 | ||
4520 | if (b->type_decls == NULL) | |
4521 | b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE); | |
4522 | binding_table_insert (b->type_decls, name, type); | |
4523 | ||
4524 | if (name) | |
4525 | { | |
4526 | /* Do C++ gratuitous typedefing. */ | |
4527 | if (IDENTIFIER_TYPE_VALUE (name) != type) | |
4528 | { | |
926ce8bd | 4529 | tree d = NULL_TREE; |
00e8de68 GDR |
4530 | int in_class = 0; |
4531 | tree context = TYPE_CONTEXT (type); | |
4532 | ||
4533 | if (! context) | |
4534 | { | |
4535 | tree cs = current_scope (); | |
4536 | ||
4537 | if (! globalize) | |
4538 | context = cs; | |
4539 | else if (cs != NULL_TREE && TYPE_P (cs)) | |
4540 | /* When declaring a friend class of a local class, we want | |
4541 | to inject the newly named class into the scope | |
4542 | containing the local class, not the namespace scope. */ | |
4543 | context = decl_function_context (get_type_decl (cs)); | |
4544 | } | |
4545 | if (!context) | |
4546 | context = current_namespace; | |
4547 | ||
4548 | if (b->kind == sk_class | |
4549 | || (b->kind == sk_template_parms | |
4550 | && b->level_chain->kind == sk_class)) | |
4551 | in_class = 1; | |
4552 | ||
4553 | if (current_lang_name == lang_name_java) | |
4554 | TYPE_FOR_JAVA (type) = 1; | |
4555 | ||
4556 | d = create_implicit_typedef (name, type); | |
4557 | DECL_CONTEXT (d) = FROB_CONTEXT (context); | |
4558 | if (! in_class) | |
4559 | set_identifier_type_value_with_scope (name, d, b); | |
4560 | ||
4561 | d = maybe_process_template_type_declaration (type, | |
4562 | globalize, b); | |
4563 | ||
4564 | if (b->kind == sk_class) | |
4565 | { | |
4566 | if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) | |
4567 | /* Put this TYPE_DECL on the TYPE_FIELDS list for the | |
4568 | class. But if it's a member template class, we | |
4569 | want the TEMPLATE_DECL, not the TYPE_DECL, so this | |
4570 | is done later. */ | |
4571 | finish_member_declaration (d); | |
4572 | else | |
4573 | pushdecl_class_level (d); | |
4574 | } | |
4575 | else | |
4576 | d = pushdecl_with_scope (d, b); | |
4577 | ||
4578 | /* FIXME what if it gets a name from typedef? */ | |
4579 | if (ANON_AGGRNAME_P (name)) | |
4580 | DECL_IGNORED_P (d) = 1; | |
4581 | ||
4582 | TYPE_CONTEXT (type) = DECL_CONTEXT (d); | |
4583 | ||
4584 | /* If this is a local class, keep track of it. We need this | |
4585 | information for name-mangling, and so that it is possible to find | |
4586 | all function definitions in a translation unit in a convenient | |
4587 | way. (It's otherwise tricky to find a member function definition | |
4588 | it's only pointed to from within a local class.) */ | |
4589 | if (TYPE_CONTEXT (type) | |
4590 | && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL | |
4591 | && !processing_template_decl) | |
4592 | VARRAY_PUSH_TREE (local_classes, type); | |
4593 | } | |
4594 | if (b->kind == sk_class | |
4595 | && !COMPLETE_TYPE_P (current_class_type)) | |
4596 | { | |
4597 | maybe_add_class_template_decl_list (current_class_type, | |
4598 | type, /*friend_p=*/0); | |
4599 | CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls; | |
4600 | } | |
4601 | } | |
4602 | ||
4603 | if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL) | |
4604 | /* Use the canonical TYPE_DECL for this node. */ | |
4605 | TYPE_STUB_DECL (type) = TYPE_NAME (type); | |
4606 | else | |
4607 | { | |
4608 | /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE | |
4609 | will be the tagged type we just added to the current | |
4610 | binding level. This fake NULL-named TYPE_DECL node helps | |
4611 | dwarfout.c to know when it needs to output a | |
4612 | representation of a tagged type, and it also gives us a | |
4613 | convenient place to record the "scope start" address for | |
4614 | the tagged type. */ | |
4615 | ||
4616 | tree d = build_decl (TYPE_DECL, NULL_TREE, type); | |
4617 | TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b); | |
4618 | } | |
4619 | timevar_pop (TV_NAME_LOOKUP); | |
4620 | } | |
4621 | \f | |
4622 | /* Allocate storage for saving a C++ binding. */ | |
4623 | #define cxx_saved_binding_make() \ | |
4624 | (ggc_alloc (sizeof (cxx_saved_binding))) | |
4625 | ||
4626 | struct cxx_saved_binding GTY(()) | |
4627 | { | |
4628 | /* Link that chains saved C++ bindings for a given name into a stack. */ | |
4629 | cxx_saved_binding *previous; | |
4630 | /* The name of the current binding. */ | |
4631 | tree identifier; | |
4632 | /* The binding we're saving. */ | |
4633 | cxx_binding *binding; | |
4634 | tree class_value; | |
4635 | tree real_type_value; | |
4636 | }; | |
4637 | ||
4638 | /* Subroutines for reverting temporarily to top-level for instantiation | |
4639 | of templates and such. We actually need to clear out the class- and | |
4640 | local-value slots of all identifiers, so that only the global values | |
4641 | are at all visible. Simply setting current_binding_level to the global | |
4642 | scope isn't enough, because more binding levels may be pushed. */ | |
4643 | struct saved_scope *scope_chain; | |
4644 | ||
4645 | static cxx_saved_binding * | |
4646 | store_bindings (tree names, cxx_saved_binding *old_bindings) | |
4647 | { | |
4648 | tree t; | |
4649 | cxx_saved_binding *search_bindings = old_bindings; | |
4650 | ||
4651 | timevar_push (TV_NAME_LOOKUP); | |
4652 | for (t = names; t; t = TREE_CHAIN (t)) | |
4653 | { | |
4654 | tree id; | |
4655 | cxx_saved_binding *saved; | |
4656 | cxx_saved_binding *t1; | |
4657 | ||
4658 | if (TREE_CODE (t) == TREE_LIST) | |
4659 | id = TREE_PURPOSE (t); | |
4660 | else | |
4661 | id = DECL_NAME (t); | |
4662 | ||
4663 | if (!id | |
4664 | /* Note that we may have an IDENTIFIER_CLASS_VALUE even when | |
4665 | we have no IDENTIFIER_BINDING if we have left the class | |
4666 | scope, but cached the class-level declarations. */ | |
4667 | || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id))) | |
4668 | continue; | |
4669 | ||
4670 | for (t1 = search_bindings; t1; t1 = t1->previous) | |
4671 | if (t1->identifier == id) | |
4672 | goto skip_it; | |
4673 | ||
4674 | my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135); | |
4675 | saved = cxx_saved_binding_make (); | |
4676 | saved->previous = old_bindings; | |
4677 | saved->identifier = id; | |
4678 | saved->binding = IDENTIFIER_BINDING (id); | |
4679 | saved->class_value = IDENTIFIER_CLASS_VALUE (id);; | |
4680 | saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); | |
4681 | IDENTIFIER_BINDING (id) = NULL; | |
4682 | IDENTIFIER_CLASS_VALUE (id) = NULL_TREE; | |
4683 | old_bindings = saved; | |
4684 | skip_it: | |
4685 | ; | |
4686 | } | |
4687 | POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings); | |
4688 | } | |
4689 | ||
4690 | void | |
c353b8e3 | 4691 | push_to_top_level (void) |
00e8de68 GDR |
4692 | { |
4693 | struct saved_scope *s; | |
4694 | struct cp_binding_level *b; | |
4695 | cxx_saved_binding *old_bindings; | |
4696 | int need_pop; | |
4697 | ||
4698 | timevar_push (TV_NAME_LOOKUP); | |
4699 | s = ggc_alloc_cleared (sizeof (struct saved_scope)); | |
4700 | ||
4701 | b = scope_chain ? current_binding_level : 0; | |
4702 | ||
4703 | /* If we're in the middle of some function, save our state. */ | |
4704 | if (cfun) | |
4705 | { | |
4706 | need_pop = 1; | |
4707 | push_function_context_to (NULL_TREE); | |
4708 | } | |
4709 | else | |
4710 | need_pop = 0; | |
4711 | ||
4712 | old_bindings = NULL; | |
4713 | if (scope_chain && previous_class_type) | |
4714 | old_bindings = store_bindings (previous_class_values, old_bindings); | |
4715 | ||
4716 | /* Have to include the global scope, because class-scope decls | |
4717 | aren't listed anywhere useful. */ | |
4718 | for (; b; b = b->level_chain) | |
4719 | { | |
4720 | tree t; | |
4721 | ||
4722 | /* Template IDs are inserted into the global level. If they were | |
4723 | inserted into namespace level, finish_file wouldn't find them | |
4724 | when doing pending instantiations. Therefore, don't stop at | |
4725 | namespace level, but continue until :: . */ | |
c353b8e3 | 4726 | if (global_scope_p (b)) |
00e8de68 GDR |
4727 | break; |
4728 | ||
4729 | old_bindings = store_bindings (b->names, old_bindings); | |
4730 | /* We also need to check class_shadowed to save class-level type | |
4731 | bindings, since pushclass doesn't fill in b->names. */ | |
4732 | if (b->kind == sk_class) | |
4733 | old_bindings = store_bindings (b->class_shadowed, old_bindings); | |
4734 | ||
4735 | /* Unwind type-value slots back to top level. */ | |
4736 | for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) | |
4737 | SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); | |
4738 | } | |
4739 | s->prev = scope_chain; | |
4740 | s->old_bindings = old_bindings; | |
4741 | s->bindings = b; | |
4742 | s->need_pop_function_context = need_pop; | |
4743 | s->function_decl = current_function_decl; | |
4744 | s->last_parms = last_function_parms; | |
4745 | ||
4746 | scope_chain = s; | |
4747 | current_function_decl = NULL_TREE; | |
4748 | VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base"); | |
4749 | current_lang_name = lang_name_cplusplus; | |
4750 | current_namespace = global_namespace; | |
4751 | timevar_pop (TV_NAME_LOOKUP); | |
4752 | } | |
4753 | ||
00e8de68 GDR |
4754 | void |
4755 | pop_from_top_level (void) | |
4756 | { | |
4757 | struct saved_scope *s = scope_chain; | |
4758 | cxx_saved_binding *saved; | |
4759 | ||
4760 | timevar_push (TV_NAME_LOOKUP); | |
4761 | /* Clear out class-level bindings cache. */ | |
4762 | if (previous_class_type) | |
4763 | invalidate_class_lookup_cache (); | |
4764 | ||
4765 | current_lang_base = 0; | |
4766 | ||
4767 | scope_chain = s->prev; | |
4768 | for (saved = s->old_bindings; saved; saved = saved->previous) | |
4769 | { | |
4770 | tree id = saved->identifier; | |
4771 | ||
4772 | IDENTIFIER_BINDING (id) = saved->binding; | |
4773 | IDENTIFIER_CLASS_VALUE (id) = saved->class_value; | |
4774 | SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); | |
4775 | } | |
4776 | ||
4777 | /* If we were in the middle of compiling a function, restore our | |
4778 | state. */ | |
4779 | if (s->need_pop_function_context) | |
4780 | pop_function_context_from (NULL_TREE); | |
4781 | current_function_decl = s->function_decl; | |
4782 | last_function_parms = s->last_parms; | |
4783 | timevar_pop (TV_NAME_LOOKUP); | |
4784 | } | |
4785 | ||
4786 | /* Pop off extraneous binding levels left over due to syntax errors. | |
4787 | ||
4788 | We don't pop past namespaces, as they might be valid. */ | |
4789 | ||
4790 | void | |
4791 | pop_everything (void) | |
4792 | { | |
4793 | if (ENABLE_SCOPE_CHECKING) | |
4794 | verbatim ("XXX entering pop_everything ()\n"); | |
4795 | while (!toplevel_bindings_p ()) | |
4796 | { | |
4797 | if (current_binding_level->kind == sk_class) | |
4798 | pop_nested_class (); | |
4799 | else | |
4800 | poplevel (0, 0, 0); | |
4801 | } | |
4802 | if (ENABLE_SCOPE_CHECKING) | |
4803 | verbatim ("XXX leaving pop_everything ()\n"); | |
4804 | } | |
4805 | ||
28ea4c88 | 4806 | #include "gt-cp-name-lookup.h" |