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