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