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