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