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