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