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