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