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