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