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