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