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