1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
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 3, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
31 #include "c-family/c-pragma.h"
33 #include "gcc-rich-location.h"
34 #include "spellcheck-tree.h"
36 #include "c-family/name-hint.h"
37 #include "c-family/known-headers.h"
38 #include "c-family/c-spellcheck.h"
40 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
41 static cp_binding_level
*innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
45 /* Create an overload suitable for recording an artificial TYPE_DECL
46 and another decl. We use this machanism to implement the struct
47 stat hack within a namespace. It'd be nice to use it everywhere. */
49 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
50 #define STAT_TYPE(N) TREE_TYPE (N)
51 #define STAT_DECL(N) OVL_FUNCTION (N)
52 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
53 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
55 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
59 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
61 tree result
= make_node (OVERLOAD
);
63 /* Mark this as a lookup, so we can tell this is a stat hack. */
64 OVL_LOOKUP_P (result
) = true;
65 STAT_DECL (result
) = decl
;
66 STAT_TYPE (result
) = type
;
70 /* Create a local binding level for NAME. */
73 create_local_binding (cp_binding_level
*level
, tree name
)
75 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
77 INHERITED_VALUE_BINDING_P (binding
) = false;
78 LOCAL_BINDING_P (binding
) = true;
79 binding
->scope
= level
;
80 binding
->previous
= IDENTIFIER_BINDING (name
);
82 IDENTIFIER_BINDING (name
) = binding
;
87 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
88 make an empty binding if there wasn't one. */
91 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
93 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
94 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
95 create_p
? INSERT
: NO_INSERT
);
100 find_namespace_value (tree ns
, tree name
)
102 tree
*b
= find_namespace_slot (ns
, name
);
104 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
107 /* Add DECL to the list of things declared in B. */
110 add_decl_to_level (cp_binding_level
*b
, tree decl
)
112 gcc_assert (b
->kind
!= sk_class
);
114 /* Make sure we don't create a circular list. xref_tag can end
115 up pushing the same artificial decl more than once. We
116 should have already detected that in update_binding. */
117 gcc_assert (b
->names
!= decl
);
119 /* We build up the list in reverse order, and reverse it later if
121 TREE_CHAIN (decl
) = b
->names
;
124 /* If appropriate, add decl to separate list of statics. We
125 include extern variables because they might turn out to be
126 static later. It's OK for this list to contain a few false
128 if (b
->kind
== sk_namespace
130 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
131 || (TREE_CODE (decl
) == FUNCTION_DECL
132 && (!TREE_PUBLIC (decl
)
133 || decl_anon_ns_mem_p (decl
)
134 || DECL_DECLARED_INLINE_P (decl
)))))
135 vec_safe_push (static_decls
, decl
);
138 /* Find the binding for NAME in the local binding level B. */
141 find_local_binding (cp_binding_level
*b
, tree name
)
143 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
144 for (;; b
= b
->level_chain
)
146 if (binding
->scope
== b
147 && !(VAR_P (binding
->value
)
148 && DECL_DEAD_FOR_LOCAL (binding
->value
)))
151 /* Cleanup contours are transparent to the language. */
152 if (b
->kind
!= sk_cleanup
)
161 typedef std::pair
<tree
, tree
> using_pair
;
162 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
165 tree name
; /* The identifier being looked for. */
166 tree value
; /* A (possibly ambiguous) set of things found. */
167 tree type
; /* A type that has been found. */
168 int flags
; /* Lookup flags. */
169 bool deduping
; /* Full deduping is needed because using declarations
171 vec
<tree
, va_heap
, vl_embed
> *scopes
;
172 name_lookup
*previous
; /* Previously active lookup. */
175 /* Marked scope stack for outermost name lookup. */
176 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
177 /* Currently active lookup. */
178 static name_lookup
*active
;
181 name_lookup (tree n
, int f
= 0)
182 : name (n
), value (NULL_TREE
), type (NULL_TREE
), flags (f
),
183 deduping (false), scopes (NULL
), previous (NULL
)
192 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
193 name_lookup (const name_lookup
&);
194 name_lookup
&operator= (const name_lookup
&);
197 static bool seen_p (tree scope
)
199 return LOOKUP_SEEN_P (scope
);
201 static bool found_p (tree scope
)
203 return LOOKUP_FOUND_P (scope
);
206 void mark_seen (tree scope
); /* Mark and add to scope vector. */
207 static void mark_found (tree scope
)
209 gcc_checking_assert (seen_p (scope
));
210 LOOKUP_FOUND_P (scope
) = true;
212 bool see_and_mark (tree scope
)
214 bool ret
= seen_p (scope
);
219 bool find_and_mark (tree scope
);
222 void preserve_state ();
223 void restore_state ();
226 static tree
ambiguous (tree thing
, tree current
);
227 void add_overload (tree fns
);
228 void add_value (tree new_val
);
229 void add_type (tree new_type
);
230 bool process_binding (tree val_bind
, tree type_bind
);
232 /* Look in only namespace. */
233 bool search_namespace_only (tree scope
);
234 /* Look in namespace and its (recursive) inlines. Ignore using
235 directives. Return true if something found (inc dups). */
236 bool search_namespace (tree scope
);
237 /* Look in the using directives of namespace + inlines using
238 qualified lookup rules. */
239 bool search_usings (tree scope
);
242 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
243 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
244 vec
<tree
, va_gc
> *usings
);
245 using_queue
*queue_usings (using_queue
*queue
, int depth
,
246 vec
<tree
, va_gc
> *usings
)
249 queue
= do_queue_usings (queue
, depth
, usings
);
256 void adl_expr (tree
);
257 void adl_type (tree
);
258 void adl_template_arg (tree
);
259 void adl_class (tree
);
260 void adl_bases (tree
);
261 void adl_class_only (tree
);
262 void adl_namespace (tree
);
263 void adl_namespace_only (tree
);
266 /* Search namespace + inlines + maybe usings as qualified lookup. */
267 bool search_qualified (tree scope
, bool usings
= true);
269 /* Search namespace + inlines + usings as unqualified lookup. */
270 bool search_unqualified (tree scope
, cp_binding_level
*);
272 /* ADL lookup of ARGS. */
273 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
276 /* Scope stack shared by all outermost lookups. This avoids us
277 allocating and freeing on every single lookup. */
278 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
280 /* Currently active lookup. */
281 name_lookup
*name_lookup::active
;
283 /* Name lookup is recursive, becase ADL can cause template
284 instatiation. This is of course a rare event, so we optimize for
285 it not happening. When we discover an active name-lookup, which
286 must be an ADL lookup, we need to unmark the marked scopes and also
287 unmark the lookup we might have been accumulating. */
290 name_lookup::preserve_state ()
295 unsigned length
= vec_safe_length (previous
->scopes
);
296 vec_safe_reserve (previous
->scopes
, length
* 2);
297 for (unsigned ix
= length
; ix
--;)
299 tree decl
= (*previous
->scopes
)[ix
];
301 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
302 LOOKUP_SEEN_P (decl
) = false;
304 /* Preserve the FOUND_P state on the interrupted lookup's
306 if (LOOKUP_FOUND_P (decl
))
308 LOOKUP_FOUND_P (decl
) = false;
309 previous
->scopes
->quick_push (decl
);
313 /* Unmark the outer partial lookup. */
314 if (previous
->deduping
)
315 lookup_mark (previous
->value
, false);
318 scopes
= shared_scopes
;
322 /* Restore the marking state of a lookup we interrupted. */
325 name_lookup::restore_state ()
328 lookup_mark (value
, false);
330 /* Unmark and empty this lookup's scope stack. */
331 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
333 tree decl
= scopes
->pop ();
334 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
335 LOOKUP_SEEN_P (decl
) = false;
336 LOOKUP_FOUND_P (decl
) = false;
344 unsigned length
= vec_safe_length (previous
->scopes
);
345 for (unsigned ix
= 0; ix
!= length
; ix
++)
347 tree decl
= (*previous
->scopes
)[ix
];
348 if (LOOKUP_SEEN_P (decl
))
350 /* The remainder of the scope stack must be recording
351 FOUND_P decls, which we want to pop off. */
354 tree decl
= previous
->scopes
->pop ();
355 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
356 && !LOOKUP_FOUND_P (decl
));
357 LOOKUP_FOUND_P (decl
) = true;
359 while (++ix
!= length
);
363 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
364 LOOKUP_SEEN_P (decl
) = true;
367 /* Remark the outer partial lookup. */
368 if (previous
->deduping
)
369 lookup_mark (previous
->value
, true);
372 shared_scopes
= scopes
;
376 name_lookup::mark_seen (tree scope
)
378 gcc_checking_assert (!seen_p (scope
));
379 LOOKUP_SEEN_P (scope
) = true;
380 vec_safe_push (scopes
, scope
);
384 name_lookup::find_and_mark (tree scope
)
386 bool result
= LOOKUP_FOUND_P (scope
);
389 LOOKUP_FOUND_P (scope
) = true;
390 if (!LOOKUP_SEEN_P (scope
))
391 vec_safe_push (scopes
, scope
);
397 /* THING and CURRENT are ambiguous, concatenate them. */
400 name_lookup::ambiguous (tree thing
, tree current
)
402 if (TREE_CODE (current
) != TREE_LIST
)
404 current
= build_tree_list (NULL_TREE
, current
);
405 TREE_TYPE (current
) = error_mark_node
;
407 current
= tree_cons (NULL_TREE
, thing
, current
);
408 TREE_TYPE (current
) = error_mark_node
;
413 /* FNS is a new overload set to add to the exising set. */
416 name_lookup::add_overload (tree fns
)
418 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
421 if (flags
& LOOKUP_HIDDEN
)
422 probe
= ovl_skip_hidden (probe
);
423 if (probe
&& TREE_CODE (probe
) == OVERLOAD
&& OVL_USING_P (probe
))
425 /* We're about to add something found by a using
426 declaration, so need to engage deduping mode. */
427 lookup_mark (value
, true);
432 value
= lookup_maybe_add (fns
, value
, deduping
);
435 /* Add a NEW_VAL, a found value binding into the current value binding. */
438 name_lookup::add_value (tree new_val
)
440 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
441 add_overload (new_val
);
444 else if (value
== new_val
)
446 else if ((TREE_CODE (value
) == TYPE_DECL
447 && TREE_CODE (new_val
) == TYPE_DECL
448 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
449 /* Typedefs to the same type. */;
450 else if (TREE_CODE (value
) == NAMESPACE_DECL
451 && TREE_CODE (new_val
) == NAMESPACE_DECL
452 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
453 /* Namespace (possibly aliased) to the same namespace. Locate
455 value
= ORIGINAL_NAMESPACE (value
);
460 /* Disengage deduping mode. */
461 lookup_mark (value
, false);
464 value
= ambiguous (new_val
, value
);
468 /* Add a NEW_TYPE, a found type binding into the current type binding. */
471 name_lookup::add_type (tree new_type
)
475 else if (TREE_CODE (type
) == TREE_LIST
476 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
477 type
= ambiguous (new_type
, type
);
480 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
481 true if we actually found something noteworthy. */
484 name_lookup::process_binding (tree new_val
, tree new_type
)
486 /* Did we really see a type? */
488 && (LOOKUP_NAMESPACES_ONLY (flags
)
489 || (!(flags
& LOOKUP_HIDDEN
)
490 && DECL_LANG_SPECIFIC (new_type
)
491 && DECL_ANTICIPATED (new_type
))))
492 new_type
= NULL_TREE
;
494 if (new_val
&& !(flags
& LOOKUP_HIDDEN
))
495 new_val
= ovl_skip_hidden (new_val
);
497 /* Do we really see a value? */
499 switch (TREE_CODE (new_val
))
502 /* If we expect types or namespaces, and not templates,
503 or this is not a template class. */
504 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
505 && !DECL_TYPE_TEMPLATE_P (new_val
)))
509 if (LOOKUP_NAMESPACES_ONLY (flags
)
510 || (new_type
&& (flags
& LOOKUP_PREFER_TYPES
)))
514 if (LOOKUP_TYPES_ONLY (flags
))
518 if (LOOKUP_QUALIFIERS_ONLY (flags
))
525 new_type
= NULL_TREE
;
528 /* Merge into the lookup */
534 return new_val
!= NULL_TREE
;
537 /* Look in exactly namespace SCOPE. */
540 name_lookup::search_namespace_only (tree scope
)
544 if (tree
*binding
= find_namespace_slot (scope
, name
))
545 found
|= process_binding (MAYBE_STAT_DECL (*binding
),
546 MAYBE_STAT_TYPE (*binding
));
551 /* Conditionally look in namespace SCOPE and inline children. */
554 name_lookup::search_namespace (tree scope
)
556 if (see_and_mark (scope
))
557 /* We've visited this scope before. Return what we found then. */
558 return found_p (scope
);
560 /* Look in exactly namespace. */
561 bool found
= search_namespace_only (scope
);
563 /* Recursively look in its inline children. */
564 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
565 for (unsigned ix
= inlinees
->length (); ix
--;)
566 found
|= search_namespace ((*inlinees
)[ix
]);
574 /* Recursively follow using directives of SCOPE & its inline children.
575 Such following is essentially a flood-fill algorithm. */
578 name_lookup::search_usings (tree scope
)
580 /* We do not check seen_p here, as that was already set during the
581 namespace_only walk. */
586 if (vec
<tree
, va_gc
> *usings
= DECL_NAMESPACE_USING (scope
))
587 for (unsigned ix
= usings
->length (); ix
--;)
588 found
|= search_qualified ((*usings
)[ix
], true);
590 /* Look in its inline children. */
591 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
592 for (unsigned ix
= inlinees
->length (); ix
--;)
593 found
|= search_usings ((*inlinees
)[ix
]);
601 /* Qualified namespace lookup in SCOPE.
602 1) Look in SCOPE (+inlines). If found, we're done.
603 2) Otherwise, if USINGS is true,
604 recurse for every using directive of SCOPE (+inlines).
606 Trickiness is (a) loops and (b) multiple paths to same namespace.
607 In both cases we want to not repeat any lookups, and know whether
608 to stop the caller's step #2. Do this via the FOUND_P marker. */
611 name_lookup::search_qualified (tree scope
, bool usings
)
616 found
= found_p (scope
);
619 found
= search_namespace (scope
);
620 if (!found
&& usings
)
621 found
= search_usings (scope
);
627 /* Add SCOPE to the unqualified search queue, recursively add its
628 inlines and those via using directives. */
630 name_lookup::using_queue
*
631 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
633 if (see_and_mark (scope
))
638 while (SCOPE_DEPTH (common
) > depth
)
639 common
= CP_DECL_CONTEXT (common
);
640 vec_safe_push (queue
, using_pair (common
, scope
));
642 /* Queue its inline children. */
643 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
644 for (unsigned ix
= inlinees
->length (); ix
--;)
645 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
647 /* Queue its using targets. */
648 queue
= queue_usings (queue
, depth
, DECL_NAMESPACE_USING (scope
));
653 /* Add the namespaces in USINGS to the unqualified search queue. */
655 name_lookup::using_queue
*
656 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
657 vec
<tree
, va_gc
> *usings
)
659 for (unsigned ix
= usings
->length (); ix
--;)
660 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
665 /* Unqualified namespace lookup in SCOPE.
666 1) add scope+inlins to worklist.
667 2) recursively add target of every using directive
668 3) for each worklist item where SCOPE is common ancestor, search it
669 4) if nothing find, scope=parent, goto 1. */
672 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
674 /* Make static to avoid continual reallocation. We're not
676 static using_queue
*queue
= NULL
;
678 int length
= vec_safe_length (queue
);
680 /* Queue local using-directives. */
681 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
682 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
684 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
686 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
687 int depth
= SCOPE_DEPTH (scope
);
689 /* Queue namespaces reachable from SCOPE. */
690 queue
= queue_namespace (queue
, depth
, scope
);
692 /* Search every queued namespace where SCOPE is the common
693 ancestor. Adjust the others. */
694 unsigned ix
= length
;
697 using_pair
&pair
= (*queue
)[ix
];
698 while (pair
.first
== scope
)
700 found
|= search_namespace_only (pair
.second
);
701 pair
= queue
->pop ();
702 if (ix
== queue
->length ())
705 /* The depth is the same as SCOPE, find the parent scope. */
706 if (SCOPE_DEPTH (pair
.first
) == depth
)
707 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
710 while (ix
< queue
->length ());
712 if (scope
== global_namespace
)
716 vec_safe_truncate (queue
, length
);
721 /* FNS is a value binding. If it is a (set of overloaded) functions,
722 add them into the current value. */
725 name_lookup::add_fns (tree fns
)
729 else if (TREE_CODE (fns
) == OVERLOAD
)
731 if (TREE_TYPE (fns
) != unknown_type_node
)
732 fns
= OVL_FUNCTION (fns
);
734 else if (!DECL_DECLARES_FUNCTION_P (fns
))
740 /* Add functions of a namespace to the lookup structure. */
743 name_lookup::adl_namespace_only (tree scope
)
747 /* Look down into inline namespaces. */
748 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
749 for (unsigned ix
= inlinees
->length (); ix
--;)
750 adl_namespace_only ((*inlinees
)[ix
]);
752 if (tree fns
= find_namespace_value (scope
, name
))
753 add_fns (ovl_skip_hidden (fns
));
756 /* Find the containing non-inlined namespace, add it and all its
760 name_lookup::adl_namespace (tree scope
)
765 /* Find the containing non-inline namespace. */
766 while (DECL_NAMESPACE_INLINE_P (scope
))
767 scope
= CP_DECL_CONTEXT (scope
);
769 adl_namespace_only (scope
);
772 /* Adds the class and its friends to the lookup structure. */
775 name_lookup::adl_class_only (tree type
)
777 /* Backend-built structures, such as __builtin_va_list, aren't
778 affected by all this. */
779 if (!CLASS_TYPE_P (type
))
782 type
= TYPE_MAIN_VARIANT (type
);
784 if (see_and_mark (type
))
787 tree context
= decl_namespace_context (type
);
788 adl_namespace (context
);
790 complete_type (type
);
793 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
794 list
= TREE_CHAIN (list
))
795 if (name
== FRIEND_NAME (list
))
796 for (tree friends
= FRIEND_DECLS (list
); friends
;
797 friends
= TREE_CHAIN (friends
))
799 tree fn
= TREE_VALUE (friends
);
801 /* Only interested in global functions with potentially hidden
802 (i.e. unqualified) declarations. */
803 if (CP_DECL_CONTEXT (fn
) != context
)
806 /* Only interested in anticipated friends. (Non-anticipated
807 ones will have been inserted during the namespace
809 if (!DECL_ANTICIPATED (fn
))
812 /* Template specializations are never found by name lookup.
813 (Templates themselves can be found, but not template
815 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
822 /* Adds the class and its bases to the lookup structure.
823 Returns true on error. */
826 name_lookup::adl_bases (tree type
)
828 adl_class_only (type
);
830 /* Process baseclasses. */
831 if (tree binfo
= TYPE_BINFO (type
))
836 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
837 adl_bases (BINFO_TYPE (base_binfo
));
841 /* Adds everything associated with a class argument type to the lookup
842 structure. Returns true on error.
844 If T is a class type (including unions), its associated classes are: the
845 class itself; the class of which it is a member, if any; and its direct
846 and indirect base classes. Its associated namespaces are the namespaces
847 of which its associated classes are members. Furthermore, if T is a
848 class template specialization, its associated namespaces and classes
849 also include: the namespaces and classes associated with the types of
850 the template arguments provided for template type parameters (excluding
851 template template parameters); the namespaces of which any template
852 template arguments are members; and the classes of which any member
853 templates used as template template arguments are members. [ Note:
854 non-type template arguments do not contribute to the set of associated
855 namespaces. --end note] */
858 name_lookup::adl_class (tree type
)
860 /* Backend build structures, such as __builtin_va_list, aren't
861 affected by all this. */
862 if (!CLASS_TYPE_P (type
))
865 type
= TYPE_MAIN_VARIANT (type
);
866 /* We don't set found here because we have to have set seen first,
867 which is done in the adl_bases walk. */
874 if (TYPE_CLASS_SCOPE_P (type
))
875 adl_class_only (TYPE_CONTEXT (type
));
877 /* Process template arguments. */
878 if (CLASSTYPE_TEMPLATE_INFO (type
)
879 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
881 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
882 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
883 adl_template_arg (TREE_VEC_ELT (list
, i
));
888 name_lookup::adl_expr (tree expr
)
893 gcc_assert (!TYPE_P (expr
));
895 if (TREE_TYPE (expr
) != unknown_type_node
)
897 adl_type (TREE_TYPE (expr
));
901 if (TREE_CODE (expr
) == ADDR_EXPR
)
902 expr
= TREE_OPERAND (expr
, 0);
903 if (TREE_CODE (expr
) == COMPONENT_REF
904 || TREE_CODE (expr
) == OFFSET_REF
)
905 expr
= TREE_OPERAND (expr
, 1);
906 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
909 for (lkp_iterator
iter (expr
); iter
; ++iter
)
910 adl_type (TREE_TYPE (*iter
));
911 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
913 /* The working paper doesn't currently say how to handle
914 template-id arguments. The sensible thing would seem to be
915 to handle the list of template candidates like a normal
916 overload set, and handle the template arguments like we do
917 for class template specializations. */
919 /* First the templates. */
920 adl_expr (TREE_OPERAND (expr
, 0));
922 /* Now the arguments. */
923 if (tree args
= TREE_OPERAND (expr
, 1))
924 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
925 adl_template_arg (TREE_VEC_ELT (args
, ix
));
930 name_lookup::adl_type (tree type
)
935 if (TYPE_PTRDATAMEM_P (type
))
937 /* Pointer to member: associate class type and value type. */
938 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
939 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
943 switch (TREE_CODE (type
))
946 if (TYPE_PTRMEMFUNC_P (type
))
948 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
957 /* The basetype is referenced in the first arg type, so just
960 /* Associate the parameter types. */
961 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
962 adl_type (TREE_VALUE (args
));
968 adl_type (TREE_TYPE (type
));
972 if (TYPE_CLASS_SCOPE_P (type
))
973 adl_class_only (TYPE_CONTEXT (type
));
974 adl_namespace (decl_namespace_context (type
));
978 gcc_assert (type
== unknown_type_node
979 || type
== init_list_type_node
);
982 case TYPE_PACK_EXPANSION
:
983 adl_type (PACK_EXPANSION_PATTERN (type
));
991 /* Adds everything associated with a template argument to the lookup
995 name_lookup::adl_template_arg (tree arg
)
997 /* [basic.lookup.koenig]
999 If T is a template-id, its associated namespaces and classes are
1000 ... the namespaces and classes associated with the types of the
1001 template arguments provided for template type parameters
1002 (excluding template template parameters); the namespaces in which
1003 any template template arguments are defined; and the classes in
1004 which any member templates used as template template arguments
1005 are defined. [Note: non-type template arguments do not
1006 contribute to the set of associated namespaces. ] */
1008 /* Consider first template template arguments. */
1009 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1010 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1012 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1014 tree ctx
= CP_DECL_CONTEXT (arg
);
1016 /* It's not a member template. */
1017 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1018 adl_namespace (ctx
);
1019 /* Otherwise, it must be member template. */
1021 adl_class_only (ctx
);
1023 /* It's an argument pack; handle it recursively. */
1024 else if (ARGUMENT_PACK_P (arg
))
1026 tree args
= ARGUMENT_PACK_ARGS (arg
);
1027 int i
, len
= TREE_VEC_LENGTH (args
);
1028 for (i
= 0; i
< len
; ++i
)
1029 adl_template_arg (TREE_VEC_ELT (args
, i
));
1031 /* It's not a template template argument, but it is a type template
1033 else if (TYPE_P (arg
))
1037 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1038 the call arguments. */
1041 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1046 lookup_mark (fns
, true);
1053 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1054 /* OMP reduction operators put an ADL-significant type as the
1066 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1067 static void consider_binding_level (tree name
,
1068 best_match
<tree
, const char *> &bm
,
1069 cp_binding_level
*lvl
,
1070 bool look_within_fields
,
1071 enum lookup_name_fuzzy_kind kind
);
1072 static void diagnose_name_conflict (tree
, tree
);
1074 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1075 don't add duplicates to it. ARGS is the vector of call
1076 arguments (which will not be empty). */
1079 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1081 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1082 name_lookup
lookup (name
);
1083 fns
= lookup
.search_adl (fns
, args
);
1084 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1088 /* FNS is an overload set of conversion functions. Return the
1089 overloads converting to TYPE. */
1092 extract_conversion_operator (tree fns
, tree type
)
1094 tree convs
= NULL_TREE
;
1095 tree tpls
= NULL_TREE
;
1097 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1099 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1100 convs
= lookup_add (*iter
, convs
);
1102 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1103 tpls
= lookup_add (*iter
, tpls
);
1112 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1115 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1117 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1119 unsigned mid
= (lo
+ hi
) / 2;
1120 tree binding
= (*member_vec
)[mid
];
1121 tree binding_name
= OVL_NAME (binding
);
1123 if (binding_name
> name
)
1125 else if (binding_name
< name
)
1134 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1137 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1139 for (int ix
= member_vec
->length (); ix
--;)
1140 /* We can get a NULL binding during insertion of a new method
1141 name, because the identifier_binding machinery performs a
1142 lookup. If we find such a NULL slot, that's the thing we were
1143 looking for, so we might as well bail out immediately. */
1144 if (tree binding
= (*member_vec
)[ix
])
1146 if (OVL_NAME (binding
) == name
)
1155 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1158 fields_linear_search (tree klass
, tree name
, bool want_type
)
1160 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1165 && TREE_CODE (decl
) == FIELD_DECL
1166 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1168 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
))
1172 if (DECL_NAME (decl
) != name
)
1175 if (TREE_CODE (decl
) == USING_DECL
)
1177 decl
= strip_using_decl (decl
);
1178 if (is_overloaded_fn (decl
))
1182 if (DECL_DECLARES_FUNCTION_P (decl
))
1183 /* Functions are found separately. */
1186 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1193 /* Look for NAME field inside of anonymous aggregate ANON. */
1196 search_anon_aggr (tree anon
, tree name
)
1198 gcc_assert (COMPLETE_TYPE_P (anon
));
1201 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
1202 ret
= member_vec_linear_search (member_vec
, name
);
1204 ret
= fields_linear_search (anon
, name
, false);
1208 /* Anon members can only contain fields. */
1209 gcc_assert (!STAT_HACK_P (ret
) && !DECL_DECLARES_TYPE_P (ret
));
1215 /* Look for NAME as an immediate member of KLASS (including
1216 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1217 regular search. >0 to get a type binding (if there is one) and <0
1218 if you want (just) the member function binding.
1220 Use this if you do not want lazy member creation. */
1223 get_class_binding_direct (tree klass
, tree name
, int type_or_fns
)
1225 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1227 /* Conversion operators can only be found by the marker conversion
1229 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1230 tree lookup
= conv_op
? conv_op_identifier
: name
;
1231 tree val
= NULL_TREE
;
1232 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1234 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1236 val
= member_vec_binary_search (member_vec
, lookup
);
1239 else if (type_or_fns
> 0)
1241 if (STAT_HACK_P (val
))
1242 val
= STAT_TYPE (val
);
1243 else if (!DECL_DECLARES_TYPE_P (val
))
1246 else if (STAT_HACK_P (val
))
1247 val
= STAT_DECL (val
);
1249 if (val
&& TREE_CODE (val
) == OVERLOAD
1250 && TREE_CODE (OVL_FUNCTION (val
)) == USING_DECL
)
1252 /* An overload with a dependent USING_DECL. Does the caller
1253 want the USING_DECL or the functions? */
1254 if (type_or_fns
< 0)
1255 val
= OVL_CHAIN (val
);
1257 val
= OVL_FUNCTION (val
);
1262 if (member_vec
&& type_or_fns
<= 0)
1263 val
= member_vec_linear_search (member_vec
, lookup
);
1265 if (type_or_fns
< 0)
1266 /* Don't bother looking for field. We don't want it. */;
1267 else if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_USING_P (val
)))
1268 /* Dependent using declarations are a 'field', make sure we
1269 return that even if we saw an overload already. */
1270 if (tree field_val
= fields_linear_search (klass
, lookup
,
1272 if (!val
|| TREE_CODE (field_val
) == USING_DECL
)
1276 /* Extract the conversion operators asked for, unless the general
1277 conversion operator was requested. */
1280 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1281 val
= OVL_CHAIN (val
);
1282 if (tree type
= TREE_TYPE (name
))
1283 val
= extract_conversion_operator (val
, type
);
1289 /* Look for NAME's binding in exactly KLASS. See
1290 get_class_binding_direct for argument description. Does lazy
1291 special function creation as necessary. */
1294 get_class_binding (tree klass
, tree name
, int type_or_fns
)
1296 klass
= complete_type (klass
);
1298 if (COMPLETE_TYPE_P (klass
))
1300 /* Lazily declare functions, if we're going to search these. */
1301 if (IDENTIFIER_CTOR_P (name
))
1303 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1304 lazily_declare_fn (sfk_constructor
, klass
);
1305 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1306 lazily_declare_fn (sfk_copy_constructor
, klass
);
1307 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1308 lazily_declare_fn (sfk_move_constructor
, klass
);
1310 else if (IDENTIFIER_DTOR_P (name
))
1312 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1313 lazily_declare_fn (sfk_destructor
, klass
);
1315 else if (name
== assign_op_identifier
)
1317 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1318 lazily_declare_fn (sfk_copy_assignment
, klass
);
1319 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1320 lazily_declare_fn (sfk_move_assignment
, klass
);
1324 return get_class_binding_direct (klass
, name
, type_or_fns
);
1327 /* Find the slot containing overloads called 'NAME'. If there is no
1328 such slot, create an empty one. KLASS might be complete at this
1329 point, in which case we need to preserve ordering. Deals with
1330 conv_op marker handling. */
1333 get_member_slot (tree klass
, tree name
)
1335 bool complete_p
= COMPLETE_TYPE_P (klass
);
1337 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1340 vec_alloc (member_vec
, 8);
1341 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1344 /* If the class is complete but had no member_vec, we need
1345 to add the TYPE_FIELDS into it. We're also most likely
1346 to be adding ctors & dtors, so ask for 6 spare slots (the
1347 abstract cdtors and their clones). */
1348 set_class_bindings (klass
, 6);
1349 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1353 if (IDENTIFIER_CONV_OP_P (name
))
1354 name
= conv_op_identifier
;
1356 unsigned ix
, length
= member_vec
->length ();
1357 for (ix
= 0; ix
< length
; ix
++)
1359 tree
*slot
= &(*member_vec
)[ix
];
1360 tree fn_name
= OVL_NAME (*slot
);
1362 if (fn_name
== name
)
1364 /* If we found an existing slot, it must be a function set.
1365 Even with insertion after completion, because those only
1366 happen with artificial fns that have unspellable names.
1367 This means we do not have to deal with the stat hack
1369 gcc_checking_assert (OVL_P (*slot
));
1370 if (name
== conv_op_identifier
)
1372 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1373 /* Skip the conv-op marker. */
1374 slot
= &OVL_CHAIN (*slot
);
1379 if (complete_p
&& fn_name
> name
)
1383 /* No slot found. Create one at IX. We know in this case that our
1384 caller will succeed in adding the function. */
1387 /* Do exact allocation when complete, as we don't expect to add
1389 vec_safe_reserve_exact (member_vec
, 1);
1390 member_vec
->quick_insert (ix
, NULL_TREE
);
1394 gcc_checking_assert (ix
== length
);
1395 vec_safe_push (member_vec
, NULL_TREE
);
1397 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1399 tree
*slot
= &(*member_vec
)[ix
];
1400 if (name
== conv_op_identifier
)
1402 /* Install the marker prefix. */
1403 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1404 slot
= &OVL_CHAIN (*slot
);
1410 /* Comparison function to compare two MEMBER_VEC entries by name.
1411 Because we can have duplicates during insertion of TYPE_FIELDS, we
1412 do extra checking so deduping doesn't have to deal with so many
1416 member_name_cmp (const void *a_p
, const void *b_p
)
1418 tree a
= *(const tree
*)a_p
;
1419 tree b
= *(const tree
*)b_p
;
1420 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1421 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1423 gcc_checking_assert (name_a
&& name_b
);
1424 if (name_a
!= name_b
)
1425 return name_a
< name_b
? -1 : +1;
1427 if (name_a
== conv_op_identifier
)
1429 /* Strip the conv-op markers. */
1430 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1431 && OVL_FUNCTION (b
) == conv_op_marker
);
1436 if (TREE_CODE (a
) == OVERLOAD
)
1437 a
= OVL_FUNCTION (a
);
1438 if (TREE_CODE (b
) == OVERLOAD
)
1439 b
= OVL_FUNCTION (b
);
1441 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1442 if (TREE_CODE (a
) != TREE_CODE (b
))
1444 /* If one of them is a TYPE_DECL, it loses. */
1445 if (TREE_CODE (a
) == TYPE_DECL
)
1447 else if (TREE_CODE (b
) == TYPE_DECL
)
1450 /* If one of them is a USING_DECL, it loses. */
1451 if (TREE_CODE (a
) == USING_DECL
)
1453 else if (TREE_CODE (b
) == USING_DECL
)
1456 /* There are no other cases with different kinds of decls, as
1457 duplicate detection should have kicked in earlier. However,
1458 some erroneous cases get though. */
1459 gcc_assert (errorcount
);
1462 /* Using source location would be the best thing here, but we can
1463 get identically-located decls in the following circumstances:
1465 1) duplicate artificial type-decls for the same type.
1467 2) pack expansions of using-decls.
1469 We should not be doing #1, but in either case it doesn't matter
1470 how we order these. Use UID as a proxy for source ordering, so
1471 that identically-located decls still have a well-defined stable
1473 if (DECL_UID (a
) != DECL_UID (b
))
1474 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1475 gcc_assert (a
== b
);
1480 gt_pointer_operator new_value
;
1484 /* This routine compares two fields like member_name_cmp but using the
1485 pointer operator in resort_field_decl_data. We don't have to deal
1486 with duplicates here. */
1489 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1491 tree a
= *(const tree
*)a_p
;
1492 tree b
= *(const tree
*)b_p
;
1493 tree name_a
= OVL_NAME (a
);
1494 tree name_b
= OVL_NAME (b
);
1496 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1497 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1499 gcc_checking_assert (name_a
!= name_b
);
1501 return name_a
< name_b
? -1 : +1;
1504 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1507 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1508 gt_pointer_operator new_value
, void* cookie
)
1510 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1512 resort_data
.new_value
= new_value
;
1513 resort_data
.cookie
= cookie
;
1514 qsort (member_vec
->address (), member_vec
->length (),
1515 sizeof (tree
), resort_member_name_cmp
);
1519 /* Recursively count the number of fields in KLASS, including anonymous
1523 count_class_fields (tree klass
)
1525 unsigned n_fields
= 0;
1527 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1528 if (DECL_DECLARES_FUNCTION_P (fields
))
1529 /* Functions are dealt with separately. */;
1530 else if (TREE_CODE (fields
) == FIELD_DECL
1531 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1532 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1533 else if (DECL_NAME (fields
))
1539 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1540 Recurse for anonymous members. MEMBER_VEC must have space. */
1543 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1545 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1546 if (DECL_DECLARES_FUNCTION_P (fields
))
1547 /* Functions are handled separately. */;
1548 else if (TREE_CODE (fields
) == FIELD_DECL
1549 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1550 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1551 else if (DECL_NAME (fields
))
1553 tree field
= fields
;
1554 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1555 if (TREE_CODE (field
) == USING_DECL
1556 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1557 field
= ovl_make (conv_op_marker
, field
);
1558 member_vec
->quick_push (field
);
1562 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1563 MEMBER_VEC must have space. */
1566 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1568 for (tree values
= TYPE_VALUES (enumtype
);
1569 values
; values
= TREE_CHAIN (values
))
1570 member_vec
->quick_push (TREE_VALUE (values
));
1573 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1574 DeDup adjacent DECLS of the same name. We already dealt with
1575 conflict resolution when adding the fields or methods themselves.
1576 There are three cases (which could all be combined):
1577 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1578 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1579 it wins. Otherwise the OVERLOAD does.
1580 3) two USING_DECLS. ...
1582 member_name_cmp will have ordered duplicates as
1583 <fns><using><type> */
1586 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1588 unsigned len
= member_vec
->length ();
1591 tree current
= (*member_vec
)[0], name
= OVL_NAME (current
);
1592 tree next
= NULL_TREE
, next_name
= NULL_TREE
;
1593 for (unsigned jx
, ix
= 0; ix
< len
;
1594 ix
= jx
, current
= next
, name
= next_name
)
1596 tree to_type
= NULL_TREE
;
1597 tree to_using
= NULL_TREE
;
1598 tree marker
= NULL_TREE
;
1599 if (IDENTIFIER_CONV_OP_P (name
))
1602 current
= OVL_CHAIN (current
);
1603 name
= DECL_NAME (OVL_FUNCTION (marker
));
1604 gcc_checking_assert (name
== conv_op_identifier
);
1607 if (TREE_CODE (current
) == USING_DECL
)
1609 current
= strip_using_decl (current
);
1610 if (is_overloaded_fn (current
))
1611 current
= NULL_TREE
;
1612 else if (TREE_CODE (current
) == USING_DECL
)
1615 current
= NULL_TREE
;
1619 if (current
&& DECL_DECLARES_TYPE_P (current
))
1622 current
= NULL_TREE
;
1625 for (jx
= ix
+ 1; jx
< len
; jx
++)
1627 next
= (*member_vec
)[jx
];
1628 next_name
= OVL_NAME (next
);
1629 if (next_name
!= name
)
1634 gcc_checking_assert (OVL_FUNCTION (marker
)
1635 == OVL_FUNCTION (next
));
1636 next
= OVL_CHAIN (next
);
1639 if (TREE_CODE (next
) == USING_DECL
)
1641 next
= strip_using_decl (next
);
1642 if (is_overloaded_fn (next
))
1644 else if (TREE_CODE (next
) == USING_DECL
)
1651 if (next
&& DECL_DECLARES_TYPE_P (next
))
1660 current
= ovl_make (to_using
, current
);
1668 current
= stat_hack (current
, to_type
);
1671 gcc_assert (current
);
1674 OVL_CHAIN (marker
) = current
;
1677 (*member_vec
)[store
++] = current
;
1680 while (store
++ < len
)
1684 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1685 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1686 know there must be at least 1 field -- the self-reference
1687 TYPE_DECL, except for anon aggregates, which will have at least
1691 set_class_bindings (tree klass
, unsigned extra
)
1693 unsigned n_fields
= count_class_fields (klass
);
1694 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1696 if (member_vec
|| n_fields
>= 8)
1698 /* Append the new fields. */
1699 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1700 member_vec_append_class_fields (member_vec
, klass
);
1705 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1706 qsort (member_vec
->address (), member_vec
->length (),
1707 sizeof (tree
), member_name_cmp
);
1708 member_vec_dedup (member_vec
);
1712 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1715 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1718 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1720 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1721 count them twice. */
1723 n_fields
= count_class_fields (klass
);
1725 n_fields
= list_length (TYPE_VALUES (enumtype
));
1727 if (member_vec
|| n_fields
>= 8)
1729 vec_safe_reserve_exact (member_vec
, n_fields
);
1730 if (CLASSTYPE_MEMBER_VEC (klass
))
1731 member_vec_append_enum_values (member_vec
, enumtype
);
1733 member_vec_append_class_fields (member_vec
, klass
);
1734 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1735 qsort (member_vec
->address (), member_vec
->length (),
1736 sizeof (tree
), member_name_cmp
);
1737 member_vec_dedup (member_vec
);
1741 /* Compute the chain index of a binding_entry given the HASH value of its
1742 name and the total COUNT of chains. COUNT is assumed to be a power
1745 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1747 /* A free list of "binding_entry"s awaiting for re-use. */
1749 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1751 /* The binding oracle; see cp-tree.h. */
1753 cp_binding_oracle_function
*cp_binding_oracle
;
1755 /* If we have a binding oracle, ask it for all namespace-scoped
1756 definitions of NAME. */
1759 query_oracle (tree name
)
1761 if (!cp_binding_oracle
)
1764 /* LOOKED_UP holds the set of identifiers that we have already
1765 looked up with the oracle. */
1766 static hash_set
<tree
> looked_up
;
1767 if (looked_up
.add (name
))
1770 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1773 /* Create a binding_entry object for (NAME, TYPE). */
1775 static inline binding_entry
1776 binding_entry_make (tree name
, tree type
)
1778 binding_entry entry
;
1780 if (free_binding_entry
)
1782 entry
= free_binding_entry
;
1783 free_binding_entry
= entry
->chain
;
1786 entry
= ggc_alloc
<binding_entry_s
> ();
1790 entry
->chain
= NULL
;
1795 /* Put ENTRY back on the free list. */
1798 binding_entry_free (binding_entry entry
)
1802 entry
->chain
= free_binding_entry
;
1803 free_binding_entry
= entry
;
1807 /* The datatype used to implement the mapping from names to types at
1809 struct GTY(()) binding_table_s
{
1810 /* Array of chains of "binding_entry"s */
1811 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1813 /* The number of chains in this table. This is the length of the
1814 member "chain" considered as an array. */
1817 /* Number of "binding_entry"s in this table. */
1821 /* Construct TABLE with an initial CHAIN_COUNT. */
1824 binding_table_construct (binding_table table
, size_t chain_count
)
1826 table
->chain_count
= chain_count
;
1827 table
->entry_count
= 0;
1828 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1831 /* Make TABLE's entries ready for reuse. */
1834 binding_table_free (binding_table table
)
1842 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1844 binding_entry temp
= table
->chain
[i
];
1845 while (temp
!= NULL
)
1847 binding_entry entry
= temp
;
1848 temp
= entry
->chain
;
1849 binding_entry_free (entry
);
1851 table
->chain
[i
] = NULL
;
1853 table
->entry_count
= 0;
1857 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1859 static inline binding_table
1860 binding_table_new (size_t chain_count
)
1862 binding_table table
= ggc_alloc
<binding_table_s
> ();
1863 table
->chain
= NULL
;
1864 binding_table_construct (table
, chain_count
);
1868 /* Expand TABLE to twice its current chain_count. */
1871 binding_table_expand (binding_table table
)
1873 const size_t old_chain_count
= table
->chain_count
;
1874 const size_t old_entry_count
= table
->entry_count
;
1875 const size_t new_chain_count
= 2 * old_chain_count
;
1876 binding_entry
*old_chains
= table
->chain
;
1879 binding_table_construct (table
, new_chain_count
);
1880 for (i
= 0; i
< old_chain_count
; ++i
)
1882 binding_entry entry
= old_chains
[i
];
1883 for (; entry
!= NULL
; entry
= old_chains
[i
])
1885 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1886 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1888 old_chains
[i
] = entry
->chain
;
1889 entry
->chain
= table
->chain
[j
];
1890 table
->chain
[j
] = entry
;
1893 table
->entry_count
= old_entry_count
;
1896 /* Insert a binding for NAME to TYPE into TABLE. */
1899 binding_table_insert (binding_table table
, tree name
, tree type
)
1901 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1902 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1903 binding_entry entry
= binding_entry_make (name
, type
);
1905 entry
->chain
= table
->chain
[i
];
1906 table
->chain
[i
] = entry
;
1907 ++table
->entry_count
;
1909 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1910 binding_table_expand (table
);
1913 /* Return the binding_entry, if any, that maps NAME. */
1916 binding_table_find (binding_table table
, tree name
)
1918 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1919 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1921 while (entry
!= NULL
&& entry
->name
!= name
)
1922 entry
= entry
->chain
;
1927 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1930 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1938 chain_count
= table
->chain_count
;
1939 for (i
= 0; i
< chain_count
; ++i
)
1941 binding_entry entry
= table
->chain
[i
];
1942 for (; entry
!= NULL
; entry
= entry
->chain
)
1947 #ifndef ENABLE_SCOPE_CHECKING
1948 # define ENABLE_SCOPE_CHECKING 0
1950 # define ENABLE_SCOPE_CHECKING 1
1953 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1955 static GTY((deletable
)) cxx_binding
*free_bindings
;
1957 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1961 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1963 binding
->value
= value
;
1964 binding
->type
= type
;
1965 binding
->previous
= NULL
;
1968 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1970 static cxx_binding
*
1971 cxx_binding_make (tree value
, tree type
)
1973 cxx_binding
*binding
;
1976 binding
= free_bindings
;
1977 free_bindings
= binding
->previous
;
1980 binding
= ggc_alloc
<cxx_binding
> ();
1982 cxx_binding_init (binding
, value
, type
);
1987 /* Put BINDING back on the free list. */
1990 cxx_binding_free (cxx_binding
*binding
)
1992 binding
->scope
= NULL
;
1993 binding
->previous
= free_bindings
;
1994 free_bindings
= binding
;
1997 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1998 bindings) in the class scope indicated by SCOPE. */
2000 static cxx_binding
*
2001 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
2003 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
2004 cxx_binding
*binding
= cb
.base
;
2005 vec_safe_push (scope
->class_shadowed
, cb
);
2006 binding
->scope
= scope
;
2010 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2011 level at which this declaration is being bound. */
2014 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2016 cxx_binding
*binding
;
2018 if (level
!= class_binding_level
)
2020 binding
= cxx_binding_make (decl
, NULL_TREE
);
2021 binding
->scope
= level
;
2024 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2026 /* Now, fill in the binding information. */
2027 binding
->previous
= IDENTIFIER_BINDING (id
);
2028 INHERITED_VALUE_BINDING_P (binding
) = 0;
2029 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2031 /* And put it on the front of the list of bindings for ID. */
2032 IDENTIFIER_BINDING (id
) = binding
;
2035 /* Remove the binding for DECL which should be the innermost binding
2039 pop_local_binding (tree id
, tree decl
)
2041 cxx_binding
*binding
;
2043 if (id
== NULL_TREE
)
2044 /* It's easiest to write the loops that call this function without
2045 checking whether or not the entities involved have names. We
2046 get here for such an entity. */
2049 /* Get the innermost binding for ID. */
2050 binding
= IDENTIFIER_BINDING (id
);
2052 /* The name should be bound. */
2053 gcc_assert (binding
!= NULL
);
2055 /* The DECL will be either the ordinary binding or the type
2056 binding for this identifier. Remove that binding. */
2057 if (binding
->value
== decl
)
2058 binding
->value
= NULL_TREE
;
2061 gcc_assert (binding
->type
== decl
);
2062 binding
->type
= NULL_TREE
;
2065 if (!binding
->value
&& !binding
->type
)
2067 /* We're completely done with the innermost binding for this
2068 identifier. Unhook it from the list of bindings. */
2069 IDENTIFIER_BINDING (id
) = binding
->previous
;
2071 /* Add it to the free list. */
2072 cxx_binding_free (binding
);
2076 /* Remove the bindings for the decls of the current level and leave
2077 the current scope. */
2080 pop_bindings_and_leave_scope (void)
2082 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2084 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2085 tree name
= OVL_NAME (decl
);
2087 pop_local_binding (name
, decl
);
2093 /* Strip non dependent using declarations. If DECL is dependent,
2094 surreptitiously create a typename_type and return it. */
2097 strip_using_decl (tree decl
)
2099 if (decl
== NULL_TREE
)
2102 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2103 decl
= USING_DECL_DECLS (decl
);
2105 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2106 && USING_DECL_TYPENAME_P (decl
))
2108 /* We have found a type introduced by a using
2109 declaration at class scope that refers to a dependent
2112 using typename :: [opt] nested-name-specifier unqualified-id ;
2114 decl
= make_typename_type (TREE_TYPE (decl
),
2116 typename_type
, tf_error
);
2117 if (decl
!= error_mark_node
)
2118 decl
= TYPE_NAME (decl
);
2124 /* Return true if OVL is an overload for an anticipated builtin. */
2127 anticipated_builtin_p (tree ovl
)
2129 if (TREE_CODE (ovl
) != OVERLOAD
)
2132 if (!OVL_HIDDEN_P (ovl
))
2135 tree fn
= OVL_FUNCTION (ovl
);
2136 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2138 if (DECL_HIDDEN_FRIEND_P (fn
))
2144 /* BINDING records an existing declaration for a name in the current scope.
2145 But, DECL is another declaration for that same identifier in the
2146 same scope. This is the `struct stat' hack whereby a non-typedef
2147 class name or enum-name can be bound at the same level as some other
2151 A class name (9.1) or enumeration name (7.2) can be hidden by the
2152 name of an object, function, or enumerator declared in the same scope.
2153 If a class or enumeration name and an object, function, or enumerator
2154 are declared in the same scope (in any order) with the same name, the
2155 class or enumeration name is hidden wherever the object, function, or
2156 enumerator name is visible.
2158 It's the responsibility of the caller to check that
2159 inserting this name is valid here. Returns nonzero if the new binding
2163 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2165 tree bval
= binding
->value
;
2167 tree target_bval
= strip_using_decl (bval
);
2168 tree target_decl
= strip_using_decl (decl
);
2170 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2171 && target_decl
!= target_bval
2172 && (TREE_CODE (target_bval
) != TYPE_DECL
2173 /* We allow pushing an enum multiple times in a class
2174 template in order to handle late matching of underlying
2175 type on an opaque-enum-declaration followed by an
2177 || (processing_template_decl
2178 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2179 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2180 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2181 (TREE_TYPE (target_decl
)))
2182 || dependent_type_p (ENUM_UNDERLYING_TYPE
2183 (TREE_TYPE (target_bval
)))))))
2184 /* The new name is the type name. */
2185 binding
->type
= decl
;
2186 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2187 an inherited type-binding out of the way to make room
2188 for a new value binding. */
2190 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2191 has been used in a non-class scope prior declaration.
2192 In that case, we should have already issued a
2193 diagnostic; for graceful error recovery purpose, pretend
2194 this was the intended declaration for that name. */
2195 || target_bval
== error_mark_node
2196 /* If TARGET_BVAL is anticipated but has not yet been
2197 declared, pretend it is not there at all. */
2198 || anticipated_builtin_p (target_bval
))
2199 binding
->value
= decl
;
2200 else if (TREE_CODE (target_bval
) == TYPE_DECL
2201 && DECL_ARTIFICIAL (target_bval
)
2202 && target_decl
!= target_bval
2203 && (TREE_CODE (target_decl
) != TYPE_DECL
2204 || same_type_p (TREE_TYPE (target_decl
),
2205 TREE_TYPE (target_bval
))))
2207 /* The old binding was a type name. It was placed in
2208 VALUE field because it was thought, at the point it was
2209 declared, to be the only entity with such a name. Move the
2210 type name into the type slot; it is now hidden by the new
2212 binding
->type
= bval
;
2213 binding
->value
= decl
;
2214 binding
->value_is_inherited
= false;
2216 else if (TREE_CODE (target_bval
) == TYPE_DECL
2217 && TREE_CODE (target_decl
) == TYPE_DECL
2218 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2219 && binding
->scope
->kind
!= sk_class
2220 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2221 /* If either type involves template parameters, we must
2222 wait until instantiation. */
2223 || uses_template_parms (TREE_TYPE (target_decl
))
2224 || uses_template_parms (TREE_TYPE (target_bval
))))
2225 /* We have two typedef-names, both naming the same type to have
2226 the same name. In general, this is OK because of:
2230 In a given scope, a typedef specifier can be used to redefine
2231 the name of any type declared in that scope to refer to the
2232 type to which it already refers.
2234 However, in class scopes, this rule does not apply due to the
2235 stricter language in [class.mem] prohibiting redeclarations of
2238 /* There can be two block-scope declarations of the same variable,
2239 so long as they are `extern' declarations. However, there cannot
2240 be two declarations of the same static data member:
2244 A member shall not be declared twice in the
2245 member-specification. */
2246 else if (VAR_P (target_decl
)
2247 && VAR_P (target_bval
)
2248 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2249 && !DECL_CLASS_SCOPE_P (target_decl
))
2251 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2254 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2255 && TREE_CODE (bval
) == NAMESPACE_DECL
2256 && DECL_NAMESPACE_ALIAS (decl
)
2257 && DECL_NAMESPACE_ALIAS (bval
)
2258 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2259 /* [namespace.alias]
2261 In a declarative region, a namespace-alias-definition can be
2262 used to redefine a namespace-alias declared in that declarative
2263 region to refer only to the namespace to which it already
2268 if (!error_operand_p (bval
))
2269 diagnose_name_conflict (decl
, bval
);
2276 /* Diagnose a name conflict between DECL and BVAL. */
2279 diagnose_name_conflict (tree decl
, tree bval
)
2281 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2282 && TREE_CODE (decl
) != NAMESPACE_DECL
2283 && !DECL_DECLARES_FUNCTION_P (decl
)
2284 && (TREE_CODE (decl
) != TYPE_DECL
2285 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2286 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2287 error ("redeclaration of %q#D", decl
);
2289 error ("%q#D conflicts with a previous declaration", decl
);
2291 inform (location_of (bval
), "previous declaration %q#D", bval
);
2294 /* Wrapper for supplement_binding_1. */
2297 supplement_binding (cxx_binding
*binding
, tree decl
)
2300 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2301 ret
= supplement_binding_1 (binding
, decl
);
2302 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2306 /* Replace BINDING's current value on its scope's name list with
2310 update_local_overload (cxx_binding
*binding
, tree newval
)
2314 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2315 if (*d
== binding
->value
)
2317 /* Stitch new list node in. */
2318 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2321 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2324 TREE_VALUE (*d
) = newval
;
2327 /* Compares the parameter-type-lists of ONE and TWO and
2328 returns false if they are different. If the DECLs are template
2329 functions, the return types and the template parameter lists are
2330 compared too (DR 565). */
2333 matching_fn_p (tree one
, tree two
)
2335 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2336 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2339 if (TREE_CODE (one
) == TEMPLATE_DECL
2340 && TREE_CODE (two
) == TEMPLATE_DECL
)
2342 /* Compare template parms. */
2343 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2344 DECL_TEMPLATE_PARMS (two
)))
2347 /* And return type. */
2348 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2349 TREE_TYPE (TREE_TYPE (two
))))
2356 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2357 binding value (possibly with anticipated builtins stripped).
2358 Diagnose conflicts and return updated decl. */
2361 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2362 tree old
, tree decl
, bool is_friend
)
2365 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2366 tree to_type
= old_type
;
2368 gcc_assert (level
->kind
== sk_namespace
? !binding
2369 : level
->kind
!= sk_class
&& !slot
);
2370 if (old
== error_mark_node
)
2373 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2375 tree other
= to_type
;
2377 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2380 /* Pushing an artificial typedef. See if this matches either
2381 the type slot or the old value slot. */
2384 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2385 /* Two artificial decls to same type. Do nothing. */
2392 /* Slide decl into the type slot, keep old unaltered */
2399 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2401 /* Slide old into the type slot. */
2406 if (DECL_DECLARES_FUNCTION_P (decl
))
2410 else if (OVL_P (old
))
2412 for (ovl_iterator
iter (old
); iter
; ++iter
)
2416 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2418 /* If a function declaration in namespace scope or
2419 block scope has the same name and the same
2420 parameter-type- list (8.3.5) as a function
2421 introduced by a using-declaration, and the
2422 declarations do not declare the same function,
2423 the program is ill-formed. [namespace.udecl]/14 */
2424 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2427 /* FIXME: To preserve existing error behavior, we
2428 still push the decl. This might change. */
2429 diagnose_name_conflict (decl
, fn
);
2436 if (to_type
!= old_type
2438 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2439 && !(DECL_IN_SYSTEM_HEADER (decl
)
2440 && DECL_IN_SYSTEM_HEADER (to_type
)))
2441 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2444 to_val
= ovl_insert (decl
, old
);
2448 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2449 /* Different kinds of decls conflict. */
2451 else if (TREE_CODE (old
) == TYPE_DECL
)
2453 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2454 /* Two type decls to the same type. Do nothing. */
2459 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2461 /* Two maybe-aliased namespaces. If they're to the same target
2462 namespace, that's ok. */
2463 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2466 /* The new one must be an alias at this point. */
2467 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2470 else if (TREE_CODE (old
) == VAR_DECL
)
2472 /* There can be two block-scope declarations of the same
2473 variable, so long as they are `extern' declarations. */
2474 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2476 else if (tree match
= duplicate_decls (decl
, old
, false))
2484 diagnose_name_conflict (decl
, old
);
2491 if (level
->kind
!= sk_namespace
2492 && !to_type
&& binding
->value
&& OVL_P (to_val
))
2493 update_local_overload (binding
, to_val
);
2496 tree to_add
= to_val
;
2498 if (level
->kind
== sk_namespace
)
2500 else if (to_type
== decl
)
2502 else if (TREE_CODE (to_add
) == OVERLOAD
)
2503 to_add
= build_tree_list (NULL_TREE
, to_add
);
2505 add_decl_to_level (level
, to_add
);
2510 if (STAT_HACK_P (*slot
))
2512 STAT_TYPE (*slot
) = to_type
;
2513 STAT_DECL (*slot
) = to_val
;
2516 *slot
= stat_hack (to_val
, to_type
);
2522 binding
->type
= to_type
;
2523 binding
->value
= to_val
;
2530 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2532 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2534 /* DECL has C linkage. If we have an existing instance, make sure the
2535 new one is compatible. Make sure it has the same exception
2536 specification [7.5, 7.6]. Add DECL to the map. */
2539 check_extern_c_conflict (tree decl
)
2541 /* Ignore artificial or system header decls. */
2542 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2545 if (!extern_c_decls
)
2546 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2548 tree
*slot
= extern_c_decls
2549 ->find_slot_with_hash (DECL_NAME (decl
),
2550 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2551 if (tree old
= *slot
)
2553 if (TREE_CODE (old
) == OVERLOAD
)
2554 old
= OVL_FUNCTION (old
);
2557 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2558 ; /* If they're in the same context, we'll have already complained
2559 about a (possible) mismatch, when inserting the decl. */
2560 else if (!decls_match (decl
, old
))
2562 else if (TREE_CODE (decl
) == FUNCTION_DECL
2563 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2564 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2567 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2568 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2572 pedwarn (input_location
, 0,
2573 "conflicting C language linkage declaration %q#D", decl
);
2574 inform (DECL_SOURCE_LOCATION (old
),
2575 "previous declaration %q#D", old
);
2577 inform (input_location
,
2578 "due to different exception specifications");
2583 /* The hash table expects OVERLOADS, so construct one with
2584 OLD as both the function and the chain. This allocate
2585 an excess OVERLOAD node, but it's rare to have multiple
2586 extern "C" decls of the same name. And we save
2587 complicating the hash table logic (which is used
2589 *slot
= ovl_make (old
, old
);
2591 slot
= &OVL_CHAIN (*slot
);
2593 /* Chain it on for c_linkage_binding's use. */
2594 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2601 /* Returns a list of C-linkage decls with the name NAME. Used in
2602 c-family/c-pragma.c to implement redefine_extname pragma. */
2605 c_linkage_bindings (tree name
)
2608 if (tree
*slot
= extern_c_decls
2609 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2611 tree result
= *slot
;
2612 if (TREE_CODE (result
) == OVERLOAD
)
2613 result
= OVL_CHAIN (result
);
2620 /* DECL is being declared at a local scope. Emit suitable shadow
2624 check_local_shadow (tree decl
)
2626 /* Don't complain about the parms we push and then pop
2627 while tentatively parsing a function declarator. */
2628 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2631 /* Inline decls shadow nothing. */
2632 if (DECL_FROM_INLINE (decl
))
2635 /* External decls are something else. */
2636 if (DECL_EXTERNAL (decl
))
2639 tree old
= NULL_TREE
;
2640 cp_binding_level
*old_scope
= NULL
;
2641 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2643 old
= binding
->value
;
2644 old_scope
= binding
->scope
;
2646 while (old
&& VAR_P (old
) && DECL_DEAD_FOR_LOCAL (old
))
2647 old
= DECL_SHADOWED_FOR_VAR (old
);
2649 tree shadowed
= NULL_TREE
;
2651 && (TREE_CODE (old
) == PARM_DECL
2653 || (TREE_CODE (old
) == TYPE_DECL
2654 && (!DECL_ARTIFICIAL (old
)
2655 || TREE_CODE (decl
) == TYPE_DECL
)))
2656 && (!DECL_ARTIFICIAL (decl
)
2657 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2658 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2660 /* DECL shadows a local thing possibly of interest. */
2662 /* Don't complain if it's from an enclosing function. */
2663 if (DECL_CONTEXT (old
) == current_function_decl
2664 && TREE_CODE (decl
) != PARM_DECL
2665 && TREE_CODE (old
) == PARM_DECL
)
2667 /* Go to where the parms should be and see if we find
2669 cp_binding_level
*b
= current_binding_level
->level_chain
;
2671 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2672 /* Skip the ctor/dtor cleanup level. */
2676 if (b
->kind
== sk_function_parms
)
2678 error ("declaration of %q#D shadows a parameter", decl
);
2683 /* The local structure or class can't use parameters of
2684 the containing function anyway. */
2685 if (DECL_CONTEXT (old
) != current_function_decl
)
2687 for (cp_binding_level
*scope
= current_binding_level
;
2688 scope
!= old_scope
; scope
= scope
->level_chain
)
2689 if (scope
->kind
== sk_class
2690 && !LAMBDA_TYPE_P (scope
->this_entity
))
2693 /* Error if redeclaring a local declared in a
2694 init-statement or in the condition of an if or
2695 switch statement when the new declaration is in the
2696 outermost block of the controlled statement.
2697 Redeclaring a variable from a for or while condition is
2698 detected elsewhere. */
2699 else if (VAR_P (old
)
2700 && old_scope
== current_binding_level
->level_chain
2701 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2703 error ("redeclaration of %q#D", decl
);
2704 inform (DECL_SOURCE_LOCATION (old
),
2705 "%q#D previously declared here", old
);
2709 3.3.3/3: The name declared in an exception-declaration (...)
2710 shall not be redeclared in the outermost block of the handler.
2711 3.3.3/2: A parameter name shall not be redeclared (...) in
2712 the outermost block of any handler associated with a
2714 3.4.1/15: The function parameter names shall not be redeclared
2715 in the exception-declaration nor in the outermost block of a
2716 handler for the function-try-block. */
2717 else if ((TREE_CODE (old
) == VAR_DECL
2718 && old_scope
== current_binding_level
->level_chain
2719 && old_scope
->kind
== sk_catch
)
2720 || (TREE_CODE (old
) == PARM_DECL
2721 && (current_binding_level
->kind
== sk_catch
2722 || current_binding_level
->level_chain
->kind
== sk_catch
)
2723 && in_function_try_handler
))
2725 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2726 inform (DECL_SOURCE_LOCATION (old
),
2727 "%q#D previously declared here", old
);
2731 /* If '-Wshadow=compatible-local' is specified without other
2732 -Wshadow= flags, we will warn only when the type of the
2733 shadowing variable (DECL) can be converted to that of the
2734 shadowed parameter (OLD_LOCAL). The reason why we only check
2735 if DECL's type can be converted to OLD_LOCAL's type (but not the
2736 other way around) is because when users accidentally shadow a
2737 parameter, more than often they would use the variable
2738 thinking (mistakenly) it's still the parameter. It would be
2739 rare that users would use the variable in the place that
2740 expects the parameter but thinking it's a new decl. */
2742 enum opt_code warning_code
;
2744 warning_code
= OPT_Wshadow
;
2745 else if (warn_shadow_local
)
2746 warning_code
= OPT_Wshadow_local
;
2747 else if (warn_shadow_compatible_local
2748 && (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2749 || (!dependent_type_p (TREE_TYPE (decl
))
2750 && !dependent_type_p (TREE_TYPE (old
))
2751 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
),
2753 warning_code
= OPT_Wshadow_compatible_local
;
2758 if (TREE_CODE (old
) == PARM_DECL
)
2759 msg
= "declaration of %q#D shadows a parameter";
2760 else if (is_capture_proxy (old
))
2761 msg
= "declaration of %qD shadows a lambda capture";
2763 msg
= "declaration of %qD shadows a previous local";
2765 if (warning_at (input_location
, warning_code
, msg
, decl
))
2768 goto inform_shadowed
;
2776 /* Don't warn for artificial things that are not implicit typedefs. */
2777 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2780 if (nonlambda_method_basetype ())
2781 if (tree member
= lookup_member (current_nonlambda_class_type (),
2782 DECL_NAME (decl
), /*protect=*/0,
2783 /*want_type=*/false, tf_warning_or_error
))
2785 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2787 /* Warn if a variable shadows a non-function, or the variable
2788 is a function or a pointer-to-function. */
2790 || TREE_CODE (decl
) == FUNCTION_DECL
2791 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2792 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2794 if (warning_at (input_location
, OPT_Wshadow
,
2795 "declaration of %qD shadows a member of %qT",
2796 decl
, current_nonlambda_class_type ())
2800 goto inform_shadowed
;
2806 /* Now look for a namespace shadow. */
2807 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2810 || (TREE_CODE (old
) == TYPE_DECL
2811 && (!DECL_ARTIFICIAL (old
)
2812 || TREE_CODE (decl
) == TYPE_DECL
)))
2813 && !instantiating_current_function_p ())
2814 /* XXX shadow warnings in outer-more namespaces */
2816 if (warning_at (input_location
, OPT_Wshadow
,
2817 "declaration of %qD shadows a global declaration",
2821 goto inform_shadowed
;
2829 inform (DECL_SOURCE_LOCATION (shadowed
), "shadowed declaration is here");
2832 /* DECL is being pushed inside function CTX. Set its context, if
2836 set_decl_context_in_fn (tree ctx
, tree decl
)
2838 if (!DECL_CONTEXT (decl
)
2839 /* A local declaration for a function doesn't constitute
2841 && TREE_CODE (decl
) != FUNCTION_DECL
2842 /* A local declaration for an `extern' variable is in the
2843 scope of the current namespace, not the current
2845 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2846 /* When parsing the parameter list of a function declarator,
2847 don't set DECL_CONTEXT to an enclosing function. When we
2848 push the PARM_DECLs in order to process the function body,
2849 current_binding_level->this_entity will be set. */
2850 && !(TREE_CODE (decl
) == PARM_DECL
2851 && current_binding_level
->kind
== sk_function_parms
2852 && current_binding_level
->this_entity
== NULL
))
2853 DECL_CONTEXT (decl
) = ctx
;
2855 /* If this is the declaration for a namespace-scope function,
2856 but the declaration itself is in a local scope, mark the
2858 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2859 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2862 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2863 name is already bound at the current level.
2865 [basic.link] If there is a visible declaration of an entity with
2866 linkage having the same name and type, ignoring entities declared
2867 outside the innermost enclosing namespace scope, the block scope
2868 declaration declares that same entity and receives the linkage of
2869 the previous declaration.
2871 Also, make sure that this decl matches any existing external decl
2872 in the enclosing namespace. */
2875 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2877 tree ns_value
= decl
; /* Unique marker. */
2881 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2885 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2886 loc_value
= ns_value
;
2888 if (loc_value
== error_mark_node
)
2889 loc_value
= NULL_TREE
;
2891 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2892 if (!iter
.hidden_p ()
2893 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2894 && decls_match (*iter
, decl
))
2896 /* The standard only says that the local extern inherits
2897 linkage from the previous decl; in particular, default
2898 args are not shared. Add the decl into a hash table to
2899 make sure only the previous decl in this case is seen
2900 by the middle end. */
2901 struct cxx_int_tree_map
*h
;
2903 /* We inherit the outer decl's linkage. But we're a
2905 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2907 if (cp_function_chain
->extern_decl_map
== NULL
)
2908 cp_function_chain
->extern_decl_map
2909 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2911 h
= ggc_alloc
<cxx_int_tree_map
> ();
2912 h
->uid
= DECL_UID (decl
);
2914 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2915 ->find_slot (h
, INSERT
);
2921 if (TREE_PUBLIC (decl
))
2923 /* DECL is externally visible. Make sure it matches a matching
2924 decl in the namespace scope. We only really need to check
2925 this when inserting the decl, not when we find an existing
2926 match in the current scope. However, in practice we're
2927 going to be inserting a new decl in the majority of cases --
2928 who writes multiple extern decls for the same thing in the
2929 same local scope? Doing it here often avoids a duplicate
2930 namespace lookup. */
2932 /* Avoid repeating a lookup. */
2933 if (ns_value
== decl
)
2934 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2936 if (ns_value
== error_mark_node
)
2937 ns_value
= NULL_TREE
;
2939 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2943 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2944 ; /* Not externally visible. */
2945 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2946 ; /* Both are extern "C", we'll check via that mechanism. */
2947 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2948 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2949 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2950 COMPARE_REDECLARATION
)))
2952 if (permerror (DECL_SOURCE_LOCATION (decl
),
2953 "local external declaration %q#D", decl
))
2954 inform (DECL_SOURCE_LOCATION (other
),
2955 "does not match previous declaration %q#D", other
);
2962 /* Record DECL as belonging to the current lexical scope. Check for
2963 errors (such as an incompatible declaration for the same name
2964 already seen in the same scope). IS_FRIEND is true if DECL is
2965 declared as a friend.
2967 Returns either DECL or an old decl for the same name. If an old
2968 decl is returned, it may have been smashed to agree with what DECL
2972 do_pushdecl (tree decl
, bool is_friend
)
2974 if (decl
== error_mark_node
)
2975 return error_mark_node
;
2977 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2978 set_decl_context_in_fn (current_function_decl
, decl
);
2980 /* The binding level we will be pushing into. During local class
2981 pushing, we want to push to the containing scope. */
2982 cp_binding_level
*level
= current_binding_level
;
2983 while (level
->kind
== sk_class
)
2984 level
= level
->level_chain
;
2986 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2987 insert it. Other NULL-named decls, not so much. */
2988 tree name
= DECL_NAME (decl
);
2989 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
2991 cxx_binding
*binding
= NULL
; /* Local scope binding. */
2992 tree ns
= NULL_TREE
; /* Searched namespace. */
2993 tree
*slot
= NULL
; /* Binding slot in namespace. */
2994 tree old
= NULL_TREE
;
2996 if (level
->kind
== sk_namespace
)
2998 /* We look in the decl's namespace for an existing
2999 declaration, even though we push into the current
3001 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3002 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3003 /* Create the binding, if this is current namespace, because
3004 that's where we'll be pushing anyway. */
3005 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3007 old
= MAYBE_STAT_DECL (*slot
);
3011 binding
= find_local_binding (level
, name
);
3013 old
= binding
->value
;
3016 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3017 && DECL_EXTERNAL (decl
))
3018 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3020 if (old
== error_mark_node
)
3023 for (ovl_iterator
iter (old
); iter
; ++iter
)
3024 if (iter
.using_p ())
3025 ; /* Ignore using decls here. */
3026 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3028 if (match
== error_mark_node
)
3030 else if (TREE_CODE (match
) == TYPE_DECL
)
3031 /* The IDENTIFIER will have the type referring to the
3032 now-smashed TYPE_DECL, because ...? Reset it. */
3033 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3034 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3036 /* Unhiding a previously hidden decl. */
3037 tree head
= iter
.reveal_node (old
);
3042 update_local_overload (binding
, head
);
3043 binding
->value
= head
;
3045 else if (STAT_HACK_P (*slot
))
3046 STAT_DECL (*slot
) = head
;
3050 if (DECL_EXTERN_C_P (match
))
3051 /* We need to check and register the decl now. */
3052 check_extern_c_conflict (match
);
3057 /* We are pushing a new decl. */
3059 /* Skip a hidden builtin we failed to match already. There can
3061 if (old
&& anticipated_builtin_p (old
))
3062 old
= OVL_CHAIN (old
);
3064 check_template_shadow (decl
);
3066 if (DECL_DECLARES_FUNCTION_P (decl
))
3068 check_default_args (decl
);
3072 if (level
->kind
!= sk_namespace
)
3073 /* In a local class, a friend function declaration must
3074 find a matching decl in the innermost non-class scope.
3075 [class.friend/11] */
3076 error ("friend declaration %qD in local class without "
3077 "prior local declaration", decl
);
3078 else if (!flag_friend_injection
)
3079 /* Hide it from ordinary lookup. */
3080 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3084 if (level
->kind
!= sk_namespace
)
3086 check_local_shadow (decl
);
3088 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3089 /* A local namespace alias. */
3090 set_identifier_type_value (name
, NULL_TREE
);
3093 binding
= create_local_binding (level
, name
);
3097 ns
= current_namespace
;
3098 slot
= find_namespace_slot (ns
, name
, true);
3099 /* Update OLD to reflect the namespace we're going to be
3101 old
= MAYBE_STAT_DECL (*slot
);
3104 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3107 /* An existing decl matched, use it. */
3109 else if (TREE_CODE (decl
) == TYPE_DECL
)
3111 tree type
= TREE_TYPE (decl
);
3113 if (type
!= error_mark_node
)
3115 if (TYPE_NAME (type
) != decl
)
3116 set_underlying_type (decl
);
3119 set_identifier_type_value_with_scope (name
, decl
, level
);
3121 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3124 /* If this is a locally defined typedef in a function that
3125 is not a template instantation, record it to implement
3126 -Wunused-local-typedefs. */
3127 if (!instantiating_current_function_p ())
3128 record_locally_defined_typedef (decl
);
3130 else if (VAR_P (decl
))
3131 maybe_register_incomplete_var (decl
);
3133 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3134 && DECL_EXTERN_C_P (decl
))
3135 check_extern_c_conflict (decl
);
3138 add_decl_to_level (level
, decl
);
3143 /* Record a decl-node X as belonging to the current lexical scope.
3144 It's a friend if IS_FRIEND is true -- which affects exactly where
3148 pushdecl (tree x
, bool is_friend
)
3150 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3151 tree ret
= do_pushdecl (x
, is_friend
);
3152 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3156 /* Enter DECL into the symbol table, if that's appropriate. Returns
3157 DECL, or a modified version thereof. */
3160 maybe_push_decl (tree decl
)
3162 tree type
= TREE_TYPE (decl
);
3164 /* Add this decl to the current binding level, but not if it comes
3165 from another scope, e.g. a static member variable. TEM may equal
3166 DECL or it may be a previous decl of the same name. */
3167 if (decl
== error_mark_node
3168 || (TREE_CODE (decl
) != PARM_DECL
3169 && DECL_CONTEXT (decl
) != NULL_TREE
3170 /* Definitions of namespace members outside their namespace are
3172 && !DECL_NAMESPACE_SCOPE_P (decl
))
3173 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3174 || type
== unknown_type_node
3175 /* The declaration of a template specialization does not affect
3176 the functions available for overload resolution, so we do not
3178 || (TREE_CODE (decl
) == FUNCTION_DECL
3179 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3182 return pushdecl (decl
);
3185 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3186 binding level. If IS_USING is true, DECL got here through a
3187 using-declaration. */
3190 push_local_binding (tree id
, tree decl
, bool is_using
)
3192 /* Skip over any local classes. This makes sense if we call
3193 push_local_binding with a friend decl of a local class. */
3194 cp_binding_level
*b
= innermost_nonclass_level ();
3196 gcc_assert (b
->kind
!= sk_namespace
);
3197 if (find_local_binding (b
, id
))
3199 /* Supplement the existing binding. */
3200 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3201 /* It didn't work. Something else must be bound at this
3202 level. Do not add DECL to the list of things to pop
3207 /* Create a new binding. */
3208 push_binding (id
, decl
, b
);
3210 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3211 /* We must put the OVERLOAD or using into a TREE_LIST since we
3212 cannot use the decl's chain itself. */
3213 decl
= build_tree_list (NULL_TREE
, decl
);
3215 /* And put DECL on the list of things declared by the current
3217 add_decl_to_level (b
, decl
);
3220 /* Check to see whether or not DECL is a variable that would have been
3221 in scope under the ARM, but is not in scope under the ANSI/ISO
3222 standard. If so, issue an error message. If name lookup would
3223 work in both cases, but return a different result, this function
3224 returns the result of ANSI/ISO lookup. Otherwise, it returns
3228 check_for_out_of_scope_variable (tree decl
)
3232 /* We only care about out of scope variables. */
3233 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
3236 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
3237 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
3238 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
3239 && DECL_DEAD_FOR_LOCAL (shadowed
))
3240 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
3241 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
3243 shadowed
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3246 if (!DECL_ERROR_REPORTED (decl
))
3248 warning (0, "name lookup of %qD changed", DECL_NAME (decl
));
3249 warning_at (DECL_SOURCE_LOCATION (shadowed
), 0,
3250 " matches this %qD under ISO standard rules",
3252 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
3253 " matches this %qD under old rules", decl
);
3254 DECL_ERROR_REPORTED (decl
) = 1;
3259 /* If we have already complained about this declaration, there's no
3260 need to do it again. */
3261 if (DECL_ERROR_REPORTED (decl
))
3264 DECL_ERROR_REPORTED (decl
) = 1;
3266 if (TREE_TYPE (decl
) == error_mark_node
)
3269 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3271 error ("name lookup of %qD changed for ISO %<for%> scoping",
3273 error (" cannot use obsolete binding at %q+D because "
3274 "it has a destructor", decl
);
3275 return error_mark_node
;
3279 permerror (input_location
, "name lookup of %qD changed for ISO %<for%> scoping",
3281 if (flag_permissive
)
3282 permerror (DECL_SOURCE_LOCATION (decl
),
3283 " using obsolete binding at %qD", decl
);
3289 inform (input_location
, "(if you use %<-fpermissive%> G++ will accept your code)");
3298 /* true means unconditionally make a BLOCK for the next level pushed. */
3300 static bool keep_next_level_flag
;
3302 static int binding_depth
= 0;
3309 for (i
= 0; i
< depth
* 2; i
++)
3313 /* Return a string describing the kind of SCOPE we have. */
3315 cp_binding_level_descriptor (cp_binding_level
*scope
)
3317 /* The order of this table must match the "scope_kind"
3319 static const char* scope_kind_names
[] = {
3325 "function-parameter-scope",
3328 "template-parameter-scope",
3329 "template-explicit-spec-scope"
3331 const scope_kind kind
= scope
->explicit_spec_p
3332 ? sk_template_spec
: scope
->kind
;
3334 return scope_kind_names
[kind
];
3337 /* Output a debugging information about SCOPE when performing
3340 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3342 const char *desc
= cp_binding_level_descriptor (scope
);
3343 if (scope
->this_entity
)
3344 verbatim ("%s %<%s(%E)%> %p %d\n", action
, desc
,
3345 scope
->this_entity
, (void *) scope
, line
);
3347 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
3350 /* Return the estimated initial size of the hashtable of a NAMESPACE
3353 static inline size_t
3354 namespace_scope_ht_size (tree ns
)
3356 tree name
= DECL_NAME (ns
);
3358 return name
== std_identifier
3359 ? NAMESPACE_STD_HT_SIZE
3360 : (name
== global_identifier
3361 ? GLOBAL_SCOPE_HT_SIZE
3362 : NAMESPACE_ORDINARY_HT_SIZE
);
3365 /* A chain of binding_level structures awaiting reuse. */
3367 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3369 /* Insert SCOPE as the innermost binding level. */
3372 push_binding_level (cp_binding_level
*scope
)
3374 /* Add it to the front of currently active scopes stack. */
3375 scope
->level_chain
= current_binding_level
;
3376 current_binding_level
= scope
;
3377 keep_next_level_flag
= false;
3379 if (ENABLE_SCOPE_CHECKING
)
3381 scope
->binding_depth
= binding_depth
;
3382 indent (binding_depth
);
3383 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3389 /* Create a new KIND scope and make it the top of the active scopes stack.
3390 ENTITY is the scope of the associated C++ entity (namespace, class,
3391 function, C++0x enumeration); it is NULL otherwise. */
3394 begin_scope (scope_kind kind
, tree entity
)
3396 cp_binding_level
*scope
;
3398 /* Reuse or create a struct for this binding level. */
3399 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3401 scope
= free_binding_level
;
3402 free_binding_level
= scope
->level_chain
;
3403 memset (scope
, 0, sizeof (cp_binding_level
));
3406 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3408 scope
->this_entity
= entity
;
3409 scope
->more_cleanups_ok
= true;
3416 case sk_template_spec
:
3417 scope
->explicit_spec_p
= true;
3418 kind
= sk_template_parms
;
3420 case sk_template_parms
:
3427 case sk_scoped_enum
:
3428 case sk_function_parms
:
3429 case sk_transaction
:
3431 scope
->keep
= keep_next_level_flag
;
3435 NAMESPACE_LEVEL (entity
) = scope
;
3439 /* Should not happen. */
3445 push_binding_level (scope
);
3450 /* We're about to leave current scope. Pop the top of the stack of
3451 currently active scopes. Return the enclosing scope, now active. */
3456 cp_binding_level
*scope
= current_binding_level
;
3458 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3459 current_binding_level
= class_binding_level
;
3461 /* We cannot leave a scope, if there are none left. */
3462 if (NAMESPACE_LEVEL (global_namespace
))
3463 gcc_assert (!global_scope_p (scope
));
3465 if (ENABLE_SCOPE_CHECKING
)
3467 indent (--binding_depth
);
3468 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3472 /* Move one nesting level up. */
3473 current_binding_level
= scope
->level_chain
;
3475 /* Namespace-scopes are left most probably temporarily, not
3476 completely; they can be reopened later, e.g. in namespace-extension
3477 or any name binding activity that requires us to resume a
3478 namespace. For classes, we cache some binding levels. For other
3479 scopes, we just make the structure available for reuse. */
3480 if (scope
->kind
!= sk_namespace
3481 && scope
->kind
!= sk_class
)
3483 scope
->level_chain
= free_binding_level
;
3484 gcc_assert (!ENABLE_SCOPE_CHECKING
3485 || scope
->binding_depth
== binding_depth
);
3486 free_binding_level
= scope
;
3489 if (scope
->kind
== sk_class
)
3491 /* Reset DEFINING_CLASS_P to allow for reuse of a
3492 class-defining scope in a non-defining context. */
3493 scope
->defining_class_p
= 0;
3495 /* Find the innermost enclosing class scope, and reset
3496 CLASS_BINDING_LEVEL appropriately. */
3497 class_binding_level
= NULL
;
3498 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3499 if (scope
->kind
== sk_class
)
3501 class_binding_level
= scope
;
3506 return current_binding_level
;
3510 resume_scope (cp_binding_level
* b
)
3512 /* Resuming binding levels is meant only for namespaces,
3513 and those cannot nest into classes. */
3514 gcc_assert (!class_binding_level
);
3515 /* Also, resuming a non-directly nested namespace is a no-no. */
3516 gcc_assert (b
->level_chain
== current_binding_level
);
3517 current_binding_level
= b
;
3518 if (ENABLE_SCOPE_CHECKING
)
3520 b
->binding_depth
= binding_depth
;
3521 indent (binding_depth
);
3522 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3527 /* Return the innermost binding level that is not for a class scope. */
3529 static cp_binding_level
*
3530 innermost_nonclass_level (void)
3532 cp_binding_level
*b
;
3534 b
= current_binding_level
;
3535 while (b
->kind
== sk_class
)
3541 /* We're defining an object of type TYPE. If it needs a cleanup, but
3542 we're not allowed to add any more objects with cleanups to the current
3543 scope, create a new binding level. */
3546 maybe_push_cleanup_level (tree type
)
3548 if (type
!= error_mark_node
3549 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3550 && current_binding_level
->more_cleanups_ok
== 0)
3552 begin_scope (sk_cleanup
, NULL
);
3553 current_binding_level
->statement_list
= push_stmt_list ();
3557 /* Return true if we are in the global binding level. */
3560 global_bindings_p (void)
3562 return global_scope_p (current_binding_level
);
3565 /* True if we are currently in a toplevel binding level. This
3566 means either the global binding level or a namespace in a toplevel
3567 binding level. Since there are no non-toplevel namespace levels,
3568 this really means any namespace or template parameter level. We
3569 also include a class whose context is toplevel. */
3572 toplevel_bindings_p (void)
3574 cp_binding_level
*b
= innermost_nonclass_level ();
3576 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3579 /* True if this is a namespace scope, or if we are defining a class
3580 which is itself at namespace scope, or whose enclosing class is
3581 such a class, etc. */
3584 namespace_bindings_p (void)
3586 cp_binding_level
*b
= innermost_nonclass_level ();
3588 return b
->kind
== sk_namespace
;
3591 /* True if the innermost non-class scope is a block scope. */
3594 local_bindings_p (void)
3596 cp_binding_level
*b
= innermost_nonclass_level ();
3597 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3600 /* True if the current level needs to have a BLOCK made. */
3605 return (current_binding_level
->blocks
!= NULL_TREE
3606 || current_binding_level
->keep
3607 || current_binding_level
->kind
== sk_cleanup
3608 || current_binding_level
->names
!= NULL_TREE
3609 || current_binding_level
->using_directives
);
3612 /* Returns the kind of the innermost scope. */
3615 innermost_scope_kind (void)
3617 return current_binding_level
->kind
;
3620 /* Returns true if this scope was created to store template parameters. */
3623 template_parm_scope_p (void)
3625 return innermost_scope_kind () == sk_template_parms
;
3628 /* If KEEP is true, make a BLOCK node for the next binding level,
3629 unconditionally. Otherwise, use the normal logic to decide whether
3630 or not to create a BLOCK. */
3633 keep_next_level (bool keep
)
3635 keep_next_level_flag
= keep
;
3638 /* Return the list of declarations of the current local scope. */
3641 get_local_decls (void)
3643 gcc_assert (current_binding_level
->kind
!= sk_namespace
3644 && current_binding_level
->kind
!= sk_class
);
3645 return current_binding_level
->names
;
3648 /* Return how many function prototypes we are currently nested inside. */
3651 function_parm_depth (void)
3654 cp_binding_level
*b
;
3656 for (b
= current_binding_level
;
3657 b
->kind
== sk_function_parms
;
3664 /* For debugging. */
3665 static int no_print_functions
= 0;
3666 static int no_print_builtins
= 0;
3669 print_binding_level (cp_binding_level
* lvl
)
3673 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3674 if (lvl
->more_cleanups_ok
)
3675 fprintf (stderr
, " more-cleanups-ok");
3676 if (lvl
->have_cleanups
)
3677 fprintf (stderr
, " have-cleanups");
3678 fprintf (stderr
, "\n");
3681 fprintf (stderr
, " names:\t");
3682 /* We can probably fit 3 names to a line? */
3683 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3685 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3687 if (no_print_builtins
3688 && (TREE_CODE (t
) == TYPE_DECL
)
3689 && DECL_IS_BUILTIN (t
))
3692 /* Function decls tend to have longer names. */
3693 if (TREE_CODE (t
) == FUNCTION_DECL
)
3700 fprintf (stderr
, "\n\t");
3703 print_node_brief (stderr
, "", t
, 0);
3704 if (t
== error_mark_node
)
3708 fprintf (stderr
, "\n");
3710 if (vec_safe_length (lvl
->class_shadowed
))
3713 cp_class_binding
*b
;
3714 fprintf (stderr
, " class-shadowed:");
3715 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3716 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3717 fprintf (stderr
, "\n");
3719 if (lvl
->type_shadowed
)
3721 fprintf (stderr
, " type-shadowed:");
3722 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3724 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3726 fprintf (stderr
, "\n");
3731 debug (cp_binding_level
&ref
)
3733 print_binding_level (&ref
);
3737 debug (cp_binding_level
*ptr
)
3742 fprintf (stderr
, "<nil>\n");
3747 print_other_binding_stack (cp_binding_level
*stack
)
3749 cp_binding_level
*level
;
3750 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3752 fprintf (stderr
, "binding level %p\n", (void *) level
);
3753 print_binding_level (level
);
3758 print_binding_stack (void)
3760 cp_binding_level
*b
;
3761 fprintf (stderr
, "current_binding_level=%p\n"
3762 "class_binding_level=%p\n"
3763 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3764 (void *) current_binding_level
, (void *) class_binding_level
,
3765 (void *) NAMESPACE_LEVEL (global_namespace
));
3766 if (class_binding_level
)
3768 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3769 if (b
== current_binding_level
)
3772 b
= class_binding_level
;
3774 b
= current_binding_level
;
3777 b
= current_binding_level
;
3778 print_other_binding_stack (b
);
3779 fprintf (stderr
, "global:\n");
3780 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3783 /* Return the type associated with ID. */
3786 identifier_type_value_1 (tree id
)
3788 /* There is no type with that name, anywhere. */
3789 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3791 /* This is not the type marker, but the real thing. */
3792 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3793 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3794 /* Have to search for it. It must be on the global level, now.
3795 Ask lookup_name not to return non-types. */
3796 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3798 return TREE_TYPE (id
);
3802 /* Wrapper for identifier_type_value_1. */
3805 identifier_type_value (tree id
)
3808 timevar_start (TV_NAME_LOOKUP
);
3809 ret
= identifier_type_value_1 (id
);
3810 timevar_stop (TV_NAME_LOOKUP
);
3814 /* Push a definition of struct, union or enum tag named ID. into
3815 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3816 the tag ID is not already defined. */
3819 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3823 if (b
->kind
!= sk_namespace
)
3825 /* Shadow the marker, not the real thing, so that the marker
3826 gets restored later. */
3827 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3829 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3830 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3831 TREE_TYPE (b
->type_shadowed
) = type
;
3835 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3837 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3839 /* Store marker instead of real type. */
3840 type
= global_type_node
;
3842 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3845 /* As set_identifier_type_value_with_scope, but using
3846 current_binding_level. */
3849 set_identifier_type_value (tree id
, tree decl
)
3851 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3854 /* Return the name for the constructor (or destructor) for the
3858 constructor_name (tree type
)
3860 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3862 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3865 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3866 which must be a class type. */
3869 constructor_name_p (tree name
, tree type
)
3871 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3873 /* These don't have names. */
3874 if (TREE_CODE (type
) == DECLTYPE_TYPE
3875 || TREE_CODE (type
) == TYPEOF_TYPE
)
3878 if (name
&& name
== constructor_name (type
))
3884 /* Counter used to create anonymous type names. */
3886 static GTY(()) int anon_cnt
;
3888 /* Return an IDENTIFIER which can be used as a name for
3889 unnamed structs and unions. */
3892 make_anon_name (void)
3896 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
3897 return get_identifier (buf
);
3900 /* This code is practically identical to that for creating
3901 anonymous names, but is just used for lambdas instead. This isn't really
3902 necessary, but it's convenient to avoid treating lambdas like other
3905 static GTY(()) int lambda_cnt
= 0;
3908 make_lambda_name (void)
3912 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
3913 return get_identifier (buf
);
3916 /* Insert another USING_DECL into the current binding level, returning
3917 this declaration. If this is a redeclaration, do nothing, and
3918 return NULL_TREE if this not in namespace scope (in namespace
3919 scope, a using decl might extend any previous bindings). */
3922 push_using_decl_1 (tree scope
, tree name
)
3926 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
3927 gcc_assert (identifier_p (name
));
3928 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
3929 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
3932 return namespace_bindings_p () ? decl
: NULL_TREE
;
3933 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3934 USING_DECL_SCOPE (decl
) = scope
;
3935 DECL_CHAIN (decl
) = current_binding_level
->usings
;
3936 current_binding_level
->usings
= decl
;
3940 /* Wrapper for push_using_decl_1. */
3943 push_using_decl (tree scope
, tree name
)
3946 timevar_start (TV_NAME_LOOKUP
);
3947 ret
= push_using_decl_1 (scope
, name
);
3948 timevar_stop (TV_NAME_LOOKUP
);
3952 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3953 caller to set DECL_CONTEXT properly.
3955 Note that this must only be used when X will be the new innermost
3956 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3957 without checking to see if the current IDENTIFIER_BINDING comes from a
3958 closer binding level than LEVEL. */
3961 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3963 cp_binding_level
*b
;
3964 tree function_decl
= current_function_decl
;
3966 current_function_decl
= NULL_TREE
;
3967 if (level
->kind
== sk_class
)
3969 b
= class_binding_level
;
3970 class_binding_level
= level
;
3971 pushdecl_class_level (x
);
3972 class_binding_level
= b
;
3976 b
= current_binding_level
;
3977 current_binding_level
= level
;
3978 x
= pushdecl (x
, is_friend
);
3979 current_binding_level
= b
;
3981 current_function_decl
= function_decl
;
3985 /* Inject X into the local scope just before the function parms. */
3988 pushdecl_outermost_localscope (tree x
)
3990 cp_binding_level
*b
= NULL
;
3991 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3993 /* Find the scope just inside the function parms. */
3994 for (cp_binding_level
*n
= current_binding_level
;
3995 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3998 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3999 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4004 /* Check a non-member using-declaration. Return the name and scope
4005 being used, and the USING_DECL, or NULL_TREE on failure. */
4008 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
4010 /* [namespace.udecl]
4011 A using-declaration for a class member shall be a
4012 member-declaration. */
4015 error ("%qT is not a namespace or unscoped enum", scope
);
4018 else if (scope
== error_mark_node
)
4021 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
4024 A using-declaration shall not name a template-id. */
4025 error ("a using-declaration cannot specify a template-id. "
4026 "Try %<using %D%>", name
);
4030 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4032 error ("namespace %qD not allowed in using-declaration", decl
);
4036 if (TREE_CODE (decl
) == SCOPE_REF
)
4038 /* It's a nested name with template parameter dependent scope.
4039 This can only be using-declaration for class member. */
4040 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
4044 decl
= OVL_FIRST (decl
);
4046 /* Make a USING_DECL. */
4047 tree using_decl
= push_using_decl (scope
, name
);
4049 if (using_decl
== NULL_TREE
4050 && at_function_scope_p ()
4052 /* C++11 7.3.3/10. */
4053 error ("%qD is already declared in this scope", name
);
4058 /* Process a local-scope or namespace-scope using declaration. SCOPE
4059 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4060 point to the binding for NAME in the current scope and are
4064 do_nonmember_using_decl (tree scope
, tree name
, tree
*value_p
, tree
*type_p
)
4066 name_lookup
lookup (name
, 0);
4068 if (!qualified_namespace_lookup (scope
, &lookup
))
4070 error ("%qD not declared", name
);
4073 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
4075 error ("reference to %qD is ambiguous", name
);
4076 print_candidates (lookup
.value
);
4077 lookup
.value
= NULL_TREE
;
4080 if (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
)
4082 error ("reference to %qD is ambiguous", name
);
4083 print_candidates (lookup
.type
);
4084 lookup
.type
= NULL_TREE
;
4087 tree value
= *value_p
;
4088 tree type
= *type_p
;
4090 /* Shift the old and new bindings around so we're comparing class and
4091 enumeration names to each other. */
4092 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4098 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4100 lookup
.type
= lookup
.value
;
4101 lookup
.value
= NULL_TREE
;
4104 if (lookup
.value
&& lookup
.value
!= value
)
4106 /* Check for using functions. */
4107 if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4109 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4111 tree new_fn
= *usings
;
4113 /* [namespace.udecl]
4115 If a function declaration in namespace scope or block
4116 scope has the same name and the same parameter types as a
4117 function introduced by a using declaration the program is
4120 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4124 if (new_fn
== old_fn
)
4125 /* The function already exists in the current
4128 else if (old
.using_p ())
4129 continue; /* This is a using decl. */
4130 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
4131 continue; /* This is an anticipated builtin. */
4132 else if (!matching_fn_p (new_fn
, old_fn
))
4133 continue; /* Parameters do not match. */
4134 else if (decls_match (new_fn
, old_fn
))
4138 diagnose_name_conflict (new_fn
, old_fn
);
4144 /* Unlike the overload case we don't drop anticipated
4145 builtins here. They don't cause a problem, and
4146 we'd like to match them with a future
4148 value
= ovl_insert (new_fn
, value
, true);
4152 /* Ignore anticipated builtins. */
4153 && !anticipated_builtin_p (value
)
4154 && !decls_match (lookup
.value
, value
))
4155 diagnose_name_conflict (lookup
.value
, value
);
4157 value
= lookup
.value
;
4160 if (lookup
.type
&& lookup
.type
!= type
)
4162 if (type
&& !decls_match (lookup
.type
, type
))
4163 diagnose_name_conflict (lookup
.type
, type
);
4168 /* If bind->value is empty, shift any class or enumeration name back. */
4179 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4180 Both are namespaces. */
4183 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4185 int depth
= SCOPE_DEPTH (ancestor
);
4187 if (!depth
&& !inline_only
)
4188 /* The global namespace encloses everything. */
4191 while (SCOPE_DEPTH (descendant
) > depth
4192 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4193 descendant
= CP_DECL_CONTEXT (descendant
);
4195 return ancestor
== descendant
;
4198 /* Returns true if ROOT (a namespace, class, or function) encloses
4199 CHILD. CHILD may be either a class type or a namespace. */
4202 is_ancestor (tree root
, tree child
)
4204 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4205 || TREE_CODE (root
) == FUNCTION_DECL
4206 || CLASS_TYPE_P (root
)));
4207 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
4208 || CLASS_TYPE_P (child
)));
4210 /* The global namespace encloses everything. */
4211 if (root
== global_namespace
)
4214 /* Search until we reach namespace scope. */
4215 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4217 /* If we've reached the ROOT, it encloses CHILD. */
4220 /* Go out one level. */
4222 child
= TYPE_NAME (child
);
4223 child
= CP_DECL_CONTEXT (child
);
4226 if (TREE_CODE (root
) == NAMESPACE_DECL
)
4227 return is_nested_namespace (root
, child
);
4232 /* Enter the class or namespace scope indicated by T suitable for name
4233 lookup. T can be arbitrary scope, not necessary nested inside the
4234 current scope. Returns a non-null scope to pop iff pop_scope
4235 should be called later to exit this scope. */
4240 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4241 push_decl_namespace (t
);
4242 else if (CLASS_TYPE_P (t
))
4244 if (!at_class_scope_p ()
4245 || !same_type_p (current_class_type
, t
))
4246 push_nested_class (t
);
4248 /* T is the same as the current scope. There is therefore no
4249 need to re-enter the scope. Since we are not actually
4250 pushing a new scope, our caller should not call
4258 /* Leave scope pushed by push_scope. */
4265 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4266 pop_decl_namespace ();
4267 else if CLASS_TYPE_P (t
)
4268 pop_nested_class ();
4271 /* Subroutine of push_inner_scope. */
4274 push_inner_scope_r (tree outer
, tree inner
)
4279 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4282 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4284 push_inner_scope_r (outer
, prev
);
4285 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4287 cp_binding_level
*save_template_parm
= 0;
4288 /* Temporary take out template parameter scopes. They are saved
4289 in reversed order in save_template_parm. */
4290 while (current_binding_level
->kind
== sk_template_parms
)
4292 cp_binding_level
*b
= current_binding_level
;
4293 current_binding_level
= b
->level_chain
;
4294 b
->level_chain
= save_template_parm
;
4295 save_template_parm
= b
;
4298 resume_scope (NAMESPACE_LEVEL (inner
));
4299 current_namespace
= inner
;
4301 /* Restore template parameter scopes. */
4302 while (save_template_parm
)
4304 cp_binding_level
*b
= save_template_parm
;
4305 save_template_parm
= b
->level_chain
;
4306 b
->level_chain
= current_binding_level
;
4307 current_binding_level
= b
;
4314 /* Enter the scope INNER from current scope. INNER must be a scope
4315 nested inside current scope. This works with both name lookup and
4316 pushing name into scope. In case a template parameter scope is present,
4317 namespace is pushed under the template parameter scope according to
4318 name lookup rule in 14.6.1/6.
4320 Return the former current scope suitable for pop_inner_scope. */
4323 push_inner_scope (tree inner
)
4325 tree outer
= current_scope ();
4327 outer
= current_namespace
;
4329 push_inner_scope_r (outer
, inner
);
4333 /* Exit the current scope INNER back to scope OUTER. */
4336 pop_inner_scope (tree outer
, tree inner
)
4339 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4342 while (outer
!= inner
)
4344 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4346 cp_binding_level
*save_template_parm
= 0;
4347 /* Temporary take out template parameter scopes. They are saved
4348 in reversed order in save_template_parm. */
4349 while (current_binding_level
->kind
== sk_template_parms
)
4351 cp_binding_level
*b
= current_binding_level
;
4352 current_binding_level
= b
->level_chain
;
4353 b
->level_chain
= save_template_parm
;
4354 save_template_parm
= b
;
4359 /* Restore template parameter scopes. */
4360 while (save_template_parm
)
4362 cp_binding_level
*b
= save_template_parm
;
4363 save_template_parm
= b
->level_chain
;
4364 b
->level_chain
= current_binding_level
;
4365 current_binding_level
= b
;
4371 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4375 /* Do a pushlevel for class declarations. */
4378 pushlevel_class (void)
4380 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4383 /* ...and a poplevel for class declarations. */
4386 poplevel_class (void)
4388 cp_binding_level
*level
= class_binding_level
;
4389 cp_class_binding
*cb
;
4393 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4394 gcc_assert (level
!= 0);
4396 /* If we're leaving a toplevel class, cache its binding level. */
4397 if (current_class_depth
== 1)
4398 previous_class_level
= level
;
4399 for (shadowed
= level
->type_shadowed
;
4401 shadowed
= TREE_CHAIN (shadowed
))
4402 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4404 /* Remove the bindings for all of the class-level declarations. */
4405 if (level
->class_shadowed
)
4407 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4409 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4410 cxx_binding_free (cb
->base
);
4412 ggc_free (level
->class_shadowed
);
4413 level
->class_shadowed
= NULL
;
4416 /* Now, pop out of the binding level which we created up in the
4417 `pushlevel_class' routine. */
4418 gcc_assert (current_binding_level
== level
);
4420 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4423 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4424 appropriate. DECL is the value to which a name has just been
4425 bound. CLASS_TYPE is the class in which the lookup occurred. */
4428 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4431 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4435 if (TREE_CODE (decl
) == OVERLOAD
)
4436 context
= ovl_scope (decl
);
4439 gcc_assert (DECL_P (decl
));
4440 context
= context_for_name_lookup (decl
);
4443 if (is_properly_derived_from (class_type
, context
))
4444 INHERITED_VALUE_BINDING_P (binding
) = 1;
4446 INHERITED_VALUE_BINDING_P (binding
) = 0;
4448 else if (binding
->value
== decl
)
4449 /* We only encounter a TREE_LIST when there is an ambiguity in the
4450 base classes. Such an ambiguity can be overridden by a
4451 definition in this class. */
4452 INHERITED_VALUE_BINDING_P (binding
) = 1;
4454 INHERITED_VALUE_BINDING_P (binding
) = 0;
4457 /* Make the declaration of X appear in CLASS scope. */
4460 pushdecl_class_level (tree x
)
4462 bool is_valid
= true;
4465 /* Do nothing if we're adding to an outer lambda closure type,
4466 outer_binding will add it later if it's needed. */
4467 if (current_class_type
!= class_binding_level
->this_entity
)
4470 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4471 /* Get the name of X. */
4472 tree name
= OVL_NAME (x
);
4476 is_valid
= push_class_level_binding (name
, x
);
4477 if (TREE_CODE (x
) == TYPE_DECL
)
4478 set_identifier_type_value (name
, x
);
4480 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4482 /* If X is an anonymous aggregate, all of its members are
4483 treated as if they were members of the class containing the
4484 aggregate, for naming purposes. */
4487 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
4489 location_t save_location
= input_location
;
4490 input_location
= DECL_SOURCE_LOCATION (f
);
4491 if (!pushdecl_class_level (f
))
4493 input_location
= save_location
;
4496 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4500 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4501 scope. If the value returned is non-NULL, and the PREVIOUS field
4502 is not set, callers must set the PREVIOUS field explicitly. */
4504 static cxx_binding
*
4505 get_class_binding (tree name
, cp_binding_level
*scope
)
4510 cxx_binding
*binding
;
4512 class_type
= scope
->this_entity
;
4514 /* Get the type binding. */
4515 type_binding
= lookup_member (class_type
, name
,
4516 /*protect=*/2, /*want_type=*/true,
4517 tf_warning_or_error
);
4518 /* Get the value binding. */
4519 value_binding
= lookup_member (class_type
, name
,
4520 /*protect=*/2, /*want_type=*/false,
4521 tf_warning_or_error
);
4524 && (TREE_CODE (value_binding
) == TYPE_DECL
4525 || DECL_CLASS_TEMPLATE_P (value_binding
)
4526 || (TREE_CODE (value_binding
) == TREE_LIST
4527 && TREE_TYPE (value_binding
) == error_mark_node
4528 && (TREE_CODE (TREE_VALUE (value_binding
))
4530 /* We found a type binding, even when looking for a non-type
4531 binding. This means that we already processed this binding
4534 else if (value_binding
)
4536 if (TREE_CODE (value_binding
) == TREE_LIST
4537 && TREE_TYPE (value_binding
) == error_mark_node
)
4538 /* NAME is ambiguous. */
4540 else if (BASELINK_P (value_binding
))
4541 /* NAME is some overloaded functions. */
4542 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4545 /* If we found either a type binding or a value binding, create a
4546 new binding object. */
4547 if (type_binding
|| value_binding
)
4549 binding
= new_class_binding (name
,
4553 /* This is a class-scope binding, not a block-scope binding. */
4554 LOCAL_BINDING_P (binding
) = 0;
4555 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4563 /* Make the declaration(s) of X appear in CLASS scope under the name
4564 NAME. Returns true if the binding is valid. */
4567 push_class_level_binding_1 (tree name
, tree x
)
4569 cxx_binding
*binding
;
4573 /* The class_binding_level will be NULL if x is a template
4574 parameter name in a member template. */
4575 if (!class_binding_level
)
4578 if (name
== error_mark_node
)
4581 /* Can happen for an erroneous declaration (c++/60384). */
4582 if (!identifier_p (name
))
4584 gcc_assert (errorcount
|| sorrycount
);
4588 /* Check for invalid member names. But don't worry about a default
4589 argument-scope lambda being pushed after the class is complete. */
4590 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4591 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4592 /* Check that we're pushing into the right binding level. */
4593 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4595 /* We could have been passed a tree list if this is an ambiguous
4596 declaration. If so, pull the declaration out because
4597 check_template_shadow will not handle a TREE_LIST. */
4598 if (TREE_CODE (decl
) == TREE_LIST
4599 && TREE_TYPE (decl
) == error_mark_node
)
4600 decl
= TREE_VALUE (decl
);
4602 if (!check_template_shadow (decl
))
4607 If T is the name of a class, then each of the following shall
4608 have a name different from T:
4610 -- every static data member of class T;
4612 -- every member of class T that is itself a type;
4614 -- every enumerator of every member of class T that is an
4617 -- every member of every anonymous union that is a member of
4620 (Non-static data members were also forbidden to have the same
4621 name as T until TC1.) */
4623 || TREE_CODE (x
) == CONST_DECL
4624 || (TREE_CODE (x
) == TYPE_DECL
4625 && !DECL_SELF_REFERENCE_P (x
))
4626 /* A data member of an anonymous union. */
4627 || (TREE_CODE (x
) == FIELD_DECL
4628 && DECL_CONTEXT (x
) != current_class_type
))
4629 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4631 tree scope
= context_for_name_lookup (x
);
4632 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4634 error ("%qD has the same name as the class in which it is "
4641 /* Get the current binding for NAME in this class, if any. */
4642 binding
= IDENTIFIER_BINDING (name
);
4643 if (!binding
|| binding
->scope
!= class_binding_level
)
4645 binding
= get_class_binding (name
, class_binding_level
);
4646 /* If a new binding was created, put it at the front of the
4647 IDENTIFIER_BINDING list. */
4650 binding
->previous
= IDENTIFIER_BINDING (name
);
4651 IDENTIFIER_BINDING (name
) = binding
;
4655 /* If there is already a binding, then we may need to update the
4657 if (binding
&& binding
->value
)
4659 tree bval
= binding
->value
;
4660 tree old_decl
= NULL_TREE
;
4661 tree target_decl
= strip_using_decl (decl
);
4662 tree target_bval
= strip_using_decl (bval
);
4664 if (INHERITED_VALUE_BINDING_P (binding
))
4666 /* If the old binding was from a base class, and was for a
4667 tag name, slide it over to make room for the new binding.
4668 The old binding is still visible if explicitly qualified
4669 with a class-key. */
4670 if (TREE_CODE (target_bval
) == TYPE_DECL
4671 && DECL_ARTIFICIAL (target_bval
)
4672 && !(TREE_CODE (target_decl
) == TYPE_DECL
4673 && DECL_ARTIFICIAL (target_decl
)))
4675 old_decl
= binding
->type
;
4676 binding
->type
= bval
;
4677 binding
->value
= NULL_TREE
;
4678 INHERITED_VALUE_BINDING_P (binding
) = 0;
4683 /* Any inherited type declaration is hidden by the type
4684 declaration in the derived class. */
4685 if (TREE_CODE (target_decl
) == TYPE_DECL
4686 && DECL_ARTIFICIAL (target_decl
))
4687 binding
->type
= NULL_TREE
;
4690 else if (TREE_CODE (target_decl
) == OVERLOAD
4691 && OVL_P (target_bval
))
4693 else if (TREE_CODE (decl
) == USING_DECL
4694 && TREE_CODE (bval
) == USING_DECL
4695 && same_type_p (USING_DECL_SCOPE (decl
),
4696 USING_DECL_SCOPE (bval
)))
4697 /* This is a using redeclaration that will be diagnosed later
4698 in supplement_binding */
4700 else if (TREE_CODE (decl
) == USING_DECL
4701 && TREE_CODE (bval
) == USING_DECL
4702 && DECL_DEPENDENT_P (decl
)
4703 && DECL_DEPENDENT_P (bval
))
4705 else if (TREE_CODE (decl
) == USING_DECL
4706 && OVL_P (target_bval
))
4708 else if (TREE_CODE (bval
) == USING_DECL
4709 && OVL_P (target_decl
))
4712 if (old_decl
&& binding
->scope
== class_binding_level
)
4715 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4716 here. This function is only used to register bindings
4717 from with the class definition itself. */
4718 INHERITED_VALUE_BINDING_P (binding
) = 0;
4723 /* Note that we declared this value so that we can issue an error if
4724 this is an invalid redeclaration of a name already used for some
4726 note_name_declared_in_class (name
, decl
);
4728 /* If we didn't replace an existing binding, put the binding on the
4729 stack of bindings for the identifier, and update the shadowed
4731 if (binding
&& binding
->scope
== class_binding_level
)
4732 /* Supplement the existing binding. */
4733 ok
= supplement_binding (binding
, decl
);
4736 /* Create a new binding. */
4737 push_binding (name
, decl
, class_binding_level
);
4744 /* Wrapper for push_class_level_binding_1. */
4747 push_class_level_binding (tree name
, tree x
)
4750 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4751 ret
= push_class_level_binding_1 (name
, x
);
4752 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4756 /* Process "using SCOPE::NAME" in a class scope. Return the
4757 USING_DECL created. */
4760 do_class_using_decl (tree scope
, tree name
)
4762 if (name
== error_mark_node
)
4765 if (!scope
|| !TYPE_P (scope
))
4767 error ("using-declaration for non-member at class scope");
4771 /* Make sure the name is not invalid */
4772 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4774 error ("%<%T::%D%> names destructor", scope
, name
);
4778 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4779 if (MAYBE_CLASS_TYPE_P (scope
)
4780 && (name
== TYPE_IDENTIFIER (scope
)
4781 || constructor_name_p (name
, scope
)))
4783 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4784 name
= ctor_identifier
;
4785 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4788 /* Cannot introduce a constructor name. */
4789 if (constructor_name_p (name
, current_class_type
))
4791 error ("%<%T::%D%> names constructor in %qT",
4792 scope
, name
, current_class_type
);
4796 /* From [namespace.udecl]:
4798 A using-declaration used as a member-declaration shall refer to a
4799 member of a base class of the class being defined.
4801 In general, we cannot check this constraint in a template because
4802 we do not know the entire set of base classes of the current
4803 class type. Morover, if SCOPE is dependent, it might match a
4804 non-dependent base. */
4806 tree decl
= NULL_TREE
;
4807 if (!dependent_scope_p (scope
))
4810 tree binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4811 tf_warning_or_error
);
4812 if (b_kind
< bk_proper_base
)
4814 /* If there are dependent bases, scope might resolve at
4815 instantiation time, even if it isn't exactly one of the
4817 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4819 error_not_base_type (scope
, current_class_type
);
4823 else if (name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4825 error ("cannot inherit constructors from indirect base %qT", scope
);
4828 else if (!IDENTIFIER_CONV_OP_P (name
)
4829 || !dependent_type_p (TREE_TYPE (name
)))
4831 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4834 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4839 /* The binfo from which the functions came does not matter. */
4840 if (BASELINK_P (decl
))
4841 decl
= BASELINK_FUNCTIONS (decl
);
4845 tree value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4846 USING_DECL_DECLS (value
) = decl
;
4847 USING_DECL_SCOPE (value
) = scope
;
4848 DECL_DEPENDENT_P (value
) = !decl
;
4854 /* Return the binding for NAME in NS. If NS is NULL, look in
4855 global_namespace. */
4858 get_namespace_binding (tree ns
, tree name
)
4860 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4862 ns
= global_namespace
;
4863 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4864 tree ret
= find_namespace_value (ns
, name
);
4865 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4869 /* Push internal DECL into the global namespace. Does not do the
4870 full overload fn handling and does not add it to the list of things
4871 in the namespace. */
4874 set_global_binding (tree decl
)
4876 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4878 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4881 /* The user's placed something in the implementor's namespace. */
4882 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4884 /* Force the binding, so compiler internals continue to work. */
4887 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4890 /* Set the context of a declaration to scope. Complain if we are not
4894 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4896 /* Get rid of namespace aliases. */
4897 scope
= ORIGINAL_NAMESPACE (scope
);
4899 /* It is ok for friends to be qualified in parallel space. */
4900 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4901 error ("declaration of %qD not in a namespace surrounding %qD",
4903 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4905 /* See whether this has been declared in the namespace or inline
4907 tree old
= NULL_TREE
;
4909 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4910 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4911 /* No old declaration at all. */
4916 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4917 if (TREE_CODE (old
) == TREE_LIST
)
4920 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4921 error ("reference to %qD is ambiguous", decl
);
4922 print_candidates (old
);
4926 if (!DECL_DECLARES_FUNCTION_P (decl
))
4928 /* Don't compare non-function decls with decls_match here, since
4929 it can't check for the correct constness at this
4930 point. pushdecl will find those errors later. */
4932 /* We might have found it in an inline namespace child of SCOPE. */
4933 if (TREE_CODE (decl
) == TREE_CODE (old
))
4934 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4937 /* Writing "N::i" to declare something directly in "N" is invalid. */
4938 if (CP_DECL_CONTEXT (decl
) == current_namespace
4939 && at_namespace_scope_p ())
4940 error ("explicit qualification in declaration of %qD", decl
);
4944 /* Since decl is a function, old should contain a function decl. */
4948 /* We handle these in check_explicit_instantiation_namespace. */
4949 if (processing_explicit_instantiation
)
4951 if (processing_template_decl
|| processing_specialization
)
4952 /* We have not yet called push_template_decl to turn a
4953 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4954 match. But, we'll check later, when we construct the
4957 /* Instantiations or specializations of templates may be declared as
4958 friends in any namespace. */
4959 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4965 for (lkp_iterator
iter (old
); iter
; ++iter
)
4967 if (iter
.using_p ())
4972 /* Adjust DECL_CONTEXT first so decls_match will return true
4973 if DECL will match a declaration in an inline namespace. */
4974 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4975 if (decls_match (decl
, ofn
))
4979 /* We found more than one matching declaration. */
4980 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4989 if (DECL_HIDDEN_FRIEND_P (found
))
4991 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4992 "%qD has not been declared within %qD", decl
, scope
);
4993 inform (DECL_SOURCE_LOCATION (found
),
4994 "only here as a %<friend%>");
4996 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
5001 /* It didn't work, go back to the explicit scope. */
5002 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5003 error ("%qD should have been declared inside %qD", decl
, scope
);
5006 /* Return the namespace where the current declaration is declared. */
5009 current_decl_namespace (void)
5012 /* If we have been pushed into a different namespace, use it. */
5013 if (!vec_safe_is_empty (decl_namespace_list
))
5014 return decl_namespace_list
->last ();
5016 if (current_class_type
)
5017 result
= decl_namespace_context (current_class_type
);
5018 else if (current_function_decl
)
5019 result
= decl_namespace_context (current_function_decl
);
5021 result
= current_namespace
;
5025 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5026 attribute visibility is seen. */
5029 handle_namespace_attrs (tree ns
, tree attributes
)
5032 bool saw_vis
= false;
5034 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
5036 tree name
= get_attribute_name (d
);
5037 tree args
= TREE_VALUE (d
);
5039 if (is_attribute_p ("visibility", name
))
5041 /* attribute visibility is a property of the syntactic block
5042 rather than the namespace as a whole, so we don't touch the
5043 NAMESPACE_DECL at all. */
5044 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
5045 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
5047 warning (OPT_Wattributes
,
5048 "%qD attribute requires a single NTBS argument",
5053 if (!TREE_PUBLIC (ns
))
5054 warning (OPT_Wattributes
,
5055 "%qD attribute is meaningless since members of the "
5056 "anonymous namespace get local symbols", name
);
5058 push_visibility (TREE_STRING_POINTER (x
), 1);
5061 else if (is_attribute_p ("abi_tag", name
))
5063 if (!DECL_NAME (ns
))
5065 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5069 if (!DECL_NAMESPACE_INLINE_P (ns
))
5071 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5077 tree dn
= DECL_NAME (ns
);
5078 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5079 IDENTIFIER_POINTER (dn
));
5080 TREE_TYPE (args
) = char_array_type_node
;
5081 args
= fix_string_type (args
);
5082 args
= build_tree_list (NULL_TREE
, args
);
5084 if (check_abi_tag_args (args
, name
))
5085 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5086 DECL_ATTRIBUTES (ns
));
5090 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5099 /* Temporarily set the namespace for the current declaration. */
5102 push_decl_namespace (tree decl
)
5104 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5105 decl
= decl_namespace_context (decl
);
5106 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5109 /* [namespace.memdef]/2 */
5112 pop_decl_namespace (void)
5114 decl_namespace_list
->pop ();
5117 /* Process a namespace-alias declaration. */
5120 do_namespace_alias (tree alias
, tree name_space
)
5122 if (name_space
== error_mark_node
)
5125 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5127 name_space
= ORIGINAL_NAMESPACE (name_space
);
5129 /* Build the alias. */
5130 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5131 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5132 DECL_EXTERNAL (alias
) = 1;
5133 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5136 /* Emit debug info for namespace alias. */
5137 if (!building_stmt_list_p ())
5138 (*debug_hooks
->early_global_decl
) (alias
);
5141 /* Like pushdecl, only it places X in the current namespace,
5145 pushdecl_namespace_level (tree x
, bool is_friend
)
5147 cp_binding_level
*b
= current_binding_level
;
5150 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5151 t
= do_pushdecl_with_scope
5152 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
5154 /* Now, the type_shadowed stack may screw us. Munge it so it does
5156 if (TREE_CODE (t
) == TYPE_DECL
)
5158 tree name
= DECL_NAME (t
);
5160 tree
*ptr
= (tree
*)0;
5161 for (; !global_scope_p (b
); b
= b
->level_chain
)
5163 tree shadowed
= b
->type_shadowed
;
5164 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5165 if (TREE_PURPOSE (shadowed
) == name
)
5167 ptr
= &TREE_VALUE (shadowed
);
5168 /* Can't break out of the loop here because sometimes
5169 a binding level will have duplicate bindings for
5170 PT names. It's gross, but I haven't time to fix it. */
5173 newval
= TREE_TYPE (t
);
5174 if (ptr
== (tree
*)0)
5176 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5177 up here if this is changed to an assertion. --KR */
5178 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5185 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5189 /* Process a using-declaration appearing in namespace scope. */
5192 finish_namespace_using_decl (tree decl
, tree scope
, tree name
)
5194 tree orig_decl
= decl
;
5196 gcc_checking_assert (current_binding_level
->kind
== sk_namespace
5197 && !processing_template_decl
);
5198 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5199 if (decl
== NULL_TREE
)
5202 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5203 tree val
= slot
? MAYBE_STAT_DECL (*slot
) : NULL_TREE
;
5204 tree type
= slot
? MAYBE_STAT_TYPE (*slot
) : NULL_TREE
;
5205 do_nonmember_using_decl (scope
, name
, &val
, &type
);
5206 if (STAT_HACK_P (*slot
))
5208 STAT_DECL (*slot
) = val
;
5209 STAT_TYPE (*slot
) = type
;
5212 *slot
= stat_hack (val
, type
);
5216 /* Emit debug info. */
5217 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
5220 /* Process a using-declaration at function scope. */
5223 finish_local_using_decl (tree decl
, tree scope
, tree name
)
5225 tree orig_decl
= decl
;
5227 gcc_checking_assert (current_binding_level
->kind
!= sk_class
5228 && current_binding_level
->kind
!= sk_namespace
);
5229 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5230 if (decl
== NULL_TREE
)
5233 add_decl_expr (decl
);
5235 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5236 tree value
= binding
? binding
->value
: NULL_TREE
;
5237 tree type
= binding
? binding
->type
: NULL_TREE
;
5239 do_nonmember_using_decl (scope
, name
, &value
, &type
);
5243 else if (binding
&& value
== binding
->value
)
5245 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5247 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5248 IDENTIFIER_BINDING (name
)->value
= value
;
5251 /* Install the new binding. */
5252 push_local_binding (name
, value
, true);
5256 else if (binding
&& type
== binding
->type
)
5260 push_local_binding (name
, type
, true);
5261 set_identifier_type_value (name
, type
);
5264 /* Emit debug info. */
5265 if (!processing_template_decl
)
5266 cp_emit_debug_info_for_using (orig_decl
, current_scope ());
5269 /* Return the declarations that are members of the namespace NS. */
5272 cp_namespace_decls (tree ns
)
5274 return NAMESPACE_LEVEL (ns
)->names
;
5277 /* Combine prefer_type and namespaces_only into flags. */
5280 lookup_flags (int prefer_type
, int namespaces_only
)
5282 if (namespaces_only
)
5283 return LOOKUP_PREFER_NAMESPACES
;
5284 if (prefer_type
> 1)
5285 return LOOKUP_PREFER_TYPES
;
5286 if (prefer_type
> 0)
5287 return LOOKUP_PREFER_BOTH
;
5291 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5292 ignore it or not. Subroutine of lookup_name_real and
5293 lookup_type_scope. */
5296 qualify_lookup (tree val
, int flags
)
5298 if (val
== NULL_TREE
)
5300 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5302 if (flags
& LOOKUP_PREFER_TYPES
)
5304 tree target_val
= strip_using_decl (val
);
5305 if (TREE_CODE (target_val
) == TYPE_DECL
5306 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5309 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5311 /* Look through lambda things that we shouldn't be able to see. */
5312 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5317 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5318 lookup failed. Search through all available namespaces and print out
5319 possible candidates. If no exact matches are found, and
5320 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5321 suggest the best near-match, if there is one. */
5324 suggest_alternatives_for (location_t location
, tree name
,
5325 bool suggest_misspellings
)
5327 vec
<tree
> candidates
= vNULL
;
5328 vec
<tree
> worklist
= vNULL
;
5329 unsigned limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
5330 bool limited
= false;
5332 /* Breadth-first search of namespaces. Up to limit namespaces
5333 searched (limit zero == unlimited). */
5334 worklist
.safe_push (global_namespace
);
5335 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5337 tree ns
= worklist
[ix
];
5338 name_lookup
lookup (name
);
5340 if (lookup
.search_qualified (ns
, false))
5341 candidates
.safe_push (lookup
.value
);
5345 /* Look for child namespaces. We have to do this
5346 indirectly because they are chained in reverse order,
5347 which is confusing to the user. */
5348 vec
<tree
> children
= vNULL
;
5350 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5351 decl
; decl
= TREE_CHAIN (decl
))
5352 if (TREE_CODE (decl
) == NAMESPACE_DECL
5353 && !DECL_NAMESPACE_ALIAS (decl
)
5354 && !DECL_NAMESPACE_INLINE_P (decl
))
5355 children
.safe_push (decl
);
5357 while (!limited
&& !children
.is_empty ())
5359 if (worklist
.length () == limit
)
5361 /* Unconditionally warn that the search was truncated. */
5363 "maximum limit of %d namespaces searched for %qE",
5368 worklist
.safe_push (children
.pop ());
5370 children
.release ();
5373 worklist
.release ();
5375 if (candidates
.length ())
5377 inform_n (location
, candidates
.length (),
5378 "suggested alternative:",
5379 "suggested alternatives:");
5380 for (unsigned ix
= 0; ix
!= candidates
.length (); ix
++)
5382 tree val
= candidates
[ix
];
5384 inform (location_of (val
), " %qE", val
);
5386 candidates
.release ();
5388 else if (!suggest_misspellings
)
5390 else if (name_hint hint
= lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
,
5393 /* Show a spelling correction. */
5394 gcc_rich_location
richloc (location
);
5396 richloc
.add_fixit_replace (hint
.suggestion ());
5397 inform (&richloc
, "suggested alternative: %qs", hint
.suggestion ());
5401 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5402 for some of the most common names within "std::".
5403 Given non-NULL NAME, a name for lookup within "std::", return the header
5404 name defining it within the C++ Standard Library (with '<' and '>'),
5408 get_std_name_hint (const char *name
)
5410 struct std_name_hint
5415 static const std_name_hint hints
[] = {
5417 {"array", "<array>"}, // C++11
5419 {"complex", "<complex>"},
5420 {"complex_literals", "<complex>"},
5422 {"deque", "<deque>"},
5423 /* <forward_list>. */
5424 {"forward_list", "<forward_list>"}, // C++11
5426 {"basic_filebuf", "<fstream>"},
5427 {"basic_ifstream", "<fstream>"},
5428 {"basic_ofstream", "<fstream>"},
5429 {"basic_fstream", "<fstream>"},
5431 {"cin", "<iostream>"},
5432 {"cout", "<iostream>"},
5433 {"cerr", "<iostream>"},
5434 {"clog", "<iostream>"},
5435 {"wcin", "<iostream>"},
5436 {"wcout", "<iostream>"},
5437 {"wclog", "<iostream>"},
5442 {"multimap", "<map>"},
5444 {"queue", "<queue>"},
5445 {"priority_queue", "<queue>"},
5447 {"ostream", "<ostream>"},
5448 {"wostream", "<ostream>"},
5449 {"ends", "<ostream>"},
5450 {"flush", "<ostream>"},
5451 {"endl", "<ostream>"},
5454 {"multiset", "<set>"},
5456 {"basic_stringbuf", "<sstream>"},
5457 {"basic_istringstream", "<sstream>"},
5458 {"basic_ostringstream", "<sstream>"},
5459 {"basic_stringstream", "<sstream>"},
5461 {"stack", "<stack>"},
5463 {"string", "<string>"},
5464 {"wstring", "<string>"},
5465 {"u16string", "<string>"},
5466 {"u32string", "<string>"},
5467 /* <unordered_map>. */
5468 {"unordered_map", "<unordered_map>"}, // C++11
5469 {"unordered_multimap", "<unordered_map>"}, // C++11
5470 /* <unordered_set>. */
5471 {"unordered_set", "<unordered_set>"}, // C++11
5472 {"unordered_multiset", "<unordered_set>"}, // C++11
5474 {"vector", "<vector>"},
5476 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5477 for (size_t i
= 0; i
< num_hints
; i
++)
5479 if (0 == strcmp (name
, hints
[i
].name
))
5480 return hints
[i
].header
;
5485 /* If SCOPE is the "std" namespace, then suggest pertinent header
5486 files for NAME at LOCATION.
5487 Return true iff a suggestion was offered. */
5490 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5492 if (scope
== NULL_TREE
)
5494 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5496 /* We only offer suggestions for the "std" namespace. */
5497 if (scope
!= std_node
)
5499 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5501 const char *name_str
= IDENTIFIER_POINTER (name
);
5502 const char *header_hint
= get_std_name_hint (name_str
);
5506 gcc_rich_location
richloc (location
);
5507 maybe_add_include_fixit (&richloc
, header_hint
);
5509 "%<std::%s%> is defined in header %qs;"
5510 " did you forget to %<#include %s%>?",
5511 name_str
, header_hint
, header_hint
);
5515 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5516 lookup failed within the explicitly provided SCOPE. Suggest the
5517 the best meaningful candidates (if any) as a fix-it hint.
5518 Return true iff a suggestion was provided. */
5521 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5524 /* Resolve any namespace aliases. */
5525 scope
= ORIGINAL_NAMESPACE (scope
);
5527 if (maybe_suggest_missing_header (location
, name
, scope
))
5530 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5532 best_match
<tree
, const char *> bm (name
);
5533 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5535 /* See if we have a good suggesion for the user. */
5536 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5539 gcc_rich_location
richloc (location
);
5540 richloc
.add_fixit_replace (fuzzy_name
);
5541 inform (&richloc
, "suggested alternative: %qs",
5549 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5552 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5553 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5555 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5556 declaration found. If no suitable declaration can be found,
5557 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5558 neither a class-type nor a namespace a diagnostic is issued. */
5561 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
5566 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5568 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
5570 flags
|= LOOKUP_HIDDEN
;
5571 name_lookup
lookup (name
, flags
);
5573 if (qualified_namespace_lookup (scope
, &lookup
))
5576 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
5577 t
= lookup_enumerator (scope
, name
);
5578 else if (is_class_type (scope
, complain
))
5579 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
5582 return error_mark_node
;
5587 Accepts the NAME to lookup and its qualifying SCOPE.
5588 Returns the name/type pair found into the cxx_binding *RESULT,
5589 or false on error. */
5592 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
5594 timevar_start (TV_NAME_LOOKUP
);
5595 query_oracle (lookup
->name
);
5596 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
5597 timevar_stop (TV_NAME_LOOKUP
);
5601 /* Helper function for lookup_name_fuzzy.
5602 Traverse binding level LVL, looking for good name matches for NAME
5605 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
5606 cp_binding_level
*lvl
, bool look_within_fields
,
5607 enum lookup_name_fuzzy_kind kind
)
5609 if (look_within_fields
)
5610 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
5612 tree type
= lvl
->this_entity
;
5613 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
5614 tree best_matching_field
5615 = lookup_member_fuzzy (type
, name
, want_type_p
);
5616 if (best_matching_field
)
5617 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
5620 /* Only suggest names reserved for the implementation if NAME begins
5621 with an underscore. */
5622 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
5624 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
5628 /* OVERLOADs or decls from using declaration are wrapped into
5630 if (TREE_CODE (d
) == TREE_LIST
)
5631 d
= OVL_FIRST (TREE_VALUE (d
));
5633 /* Don't use bindings from implicitly declared functions,
5634 as they were likely misspellings themselves. */
5635 if (TREE_TYPE (d
) == error_mark_node
)
5638 /* Skip anticipated decls of builtin functions. */
5639 if (TREE_CODE (d
) == FUNCTION_DECL
5640 && DECL_BUILT_IN (d
)
5641 && DECL_ANTICIPATED (d
))
5644 tree suggestion
= DECL_NAME (d
);
5648 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
5650 /* Ignore internal names with spaces in them. */
5651 if (strchr (suggestion_str
, ' '))
5654 /* Don't suggest names that are reserved for use by the
5655 implementation, unless NAME began with an underscore. */
5656 if (name_reserved_for_implementation_p (suggestion_str
)
5657 && !consider_implementation_names
)
5660 bm
.consider (suggestion_str
);
5664 /* Subclass of deferred_diagnostic. Notify the user that the
5665 given macro was used before it was defined.
5666 This can be done in the C++ frontend since tokenization happens
5669 class macro_use_before_def
: public deferred_diagnostic
5672 /* Ctor. LOC is the location of the usage. MACRO is the
5673 macro that was used. */
5674 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
5675 : deferred_diagnostic (loc
), m_macro (macro
)
5680 ~macro_use_before_def ()
5682 if (is_suppressed_p ())
5685 source_location def_loc
= cpp_macro_definition_location (m_macro
);
5686 if (def_loc
!= UNKNOWN_LOCATION
)
5688 inform (get_location (), "the macro %qs had not yet been defined",
5689 (const char *)m_macro
->ident
.str
);
5690 inform (def_loc
, "it was later defined here");
5695 cpp_hashnode
*m_macro
;
5699 /* Search for near-matches for NAME within the current bindings, and within
5700 macro names, returning the best match as a const char *, or NULL if
5701 no reasonable match is found.
5703 Use LOC for any deferred diagnostics. */
5706 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
5708 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5710 /* First, try some well-known names in the C++ standard library, in case
5711 the user forgot a #include. */
5712 const char *header_hint
5713 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
5715 return name_hint (NULL
,
5716 new suggest_missing_header (loc
,
5717 IDENTIFIER_POINTER (name
),
5720 best_match
<tree
, const char *> bm (name
);
5722 cp_binding_level
*lvl
;
5723 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
5724 consider_binding_level (name
, bm
, lvl
, true, kind
);
5726 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
5727 consider_binding_level (name
, bm
, lvl
, false, kind
);
5729 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5731 x = SOME_OTHER_MACRO (y);
5732 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5733 as a misspelled identifier.
5735 Use the best distance so far so that a candidate is only set if
5736 a macro is better than anything so far. This allows early rejection
5737 (without calculating the edit distance) of macro names that must have
5738 distance >= bm.get_best_distance (), and means that we only get a
5739 non-NULL result for best_macro_match if it's better than any of
5740 the identifiers already checked. */
5741 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
5742 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
5743 /* If a macro is the closest so far to NAME, consider it. */
5745 bm
.consider ((const char *)best_macro
->ident
.str
);
5746 else if (bmm
.get_best_distance () == 0)
5748 /* If we have an exact match for a macro name, then the
5749 macro has been used before it was defined. */
5750 cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ();
5752 return name_hint (NULL
,
5753 new macro_use_before_def (loc
, macro
));
5756 /* Try the "starts_decl_specifier_p" keywords to detect
5757 "singed" vs "signed" typos. */
5758 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
5760 const c_common_resword
*resword
= &c_common_reswords
[i
];
5762 if (kind
== FUZZY_LOOKUP_TYPENAME
)
5763 if (!cp_keyword_starts_decl_specifier_p (resword
->rid
))
5766 tree resword_identifier
= ridpointers
[resword
->rid
];
5767 if (!resword_identifier
)
5769 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
5771 /* Only consider reserved words that survived the
5772 filtering in init_reswords (e.g. for -std). */
5773 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
5776 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
5779 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
5782 /* Subroutine of outer_binding.
5784 Returns TRUE if BINDING is a binding to a template parameter of
5785 SCOPE. In that case SCOPE is the scope of a primary template
5786 parameter -- in the sense of G++, i.e, a template that has its own
5789 Returns FALSE otherwise. */
5792 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
5793 cp_binding_level
*scope
)
5795 tree binding_value
, tmpl
, tinfo
;
5798 if (!binding
|| !scope
|| !scope
->this_entity
)
5801 binding_value
= binding
->value
? binding
->value
: binding
->type
;
5802 tinfo
= get_template_info (scope
->this_entity
);
5804 /* BINDING_VALUE must be a template parm. */
5805 if (binding_value
== NULL_TREE
5806 || (!DECL_P (binding_value
)
5807 || !DECL_TEMPLATE_PARM_P (binding_value
)))
5810 /* The level of BINDING_VALUE. */
5812 template_type_parameter_p (binding_value
)
5813 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5814 (TREE_TYPE (binding_value
)))
5815 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
5817 /* The template of the current scope, iff said scope is a primary
5820 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
5821 ? TI_TEMPLATE (tinfo
)
5824 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5825 then BINDING_VALUE is a parameter of TMPL. */
5826 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
5829 /* Return the innermost non-namespace binding for NAME from a scope
5830 containing BINDING, or, if BINDING is NULL, the current scope.
5831 Please note that for a given template, the template parameters are
5832 considered to be in the scope containing the current scope.
5833 If CLASS_P is false, then class bindings are ignored. */
5836 outer_binding (tree name
,
5837 cxx_binding
*binding
,
5841 cp_binding_level
*scope
;
5842 cp_binding_level
*outer_scope
;
5846 scope
= binding
->scope
->level_chain
;
5847 outer
= binding
->previous
;
5851 scope
= current_binding_level
;
5852 outer
= IDENTIFIER_BINDING (name
);
5854 outer_scope
= outer
? outer
->scope
: NULL
;
5856 /* Because we create class bindings lazily, we might be missing a
5857 class binding for NAME. If there are any class binding levels
5858 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5859 declared, we must lookup NAME in those class scopes. */
5861 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
5863 if (scope
->kind
== sk_class
)
5865 cxx_binding
*class_binding
;
5867 class_binding
= get_class_binding (name
, scope
);
5870 /* Thread this new class-scope binding onto the
5871 IDENTIFIER_BINDING list so that future lookups
5873 class_binding
->previous
= outer
;
5875 binding
->previous
= class_binding
;
5877 IDENTIFIER_BINDING (name
) = class_binding
;
5878 return class_binding
;
5881 /* If we are in a member template, the template parms of the member
5882 template are considered to be inside the scope of the containing
5883 class, but within G++ the class bindings are all pushed between the
5884 template parms and the function body. So if the outer binding is
5885 a template parm for the current scope, return it now rather than
5886 look for a class binding. */
5887 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
5888 && binding_to_template_parms_of_scope_p (outer
, scope
))
5891 scope
= scope
->level_chain
;
5897 /* Return the innermost block-scope or class-scope value binding for
5898 NAME, or NULL_TREE if there is no such binding. */
5901 innermost_non_namespace_value (tree name
)
5903 cxx_binding
*binding
;
5904 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
5905 return binding
? binding
->value
: NULL_TREE
;
5908 /* Look up NAME in the current binding level and its superiors in the
5909 namespace of variables, functions and typedefs. Return a ..._DECL
5910 node of some kind representing its definition if there is only one
5911 such declaration, or return a TREE_LIST with all the overloaded
5912 definitions if there are many, or return 0 if it is undefined.
5913 Hidden name, either friend declaration or built-in function, are
5916 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5917 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5918 Otherwise we prefer non-TYPE_DECLs.
5920 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5921 BLOCK_P is false, bindings in block scopes are ignored. */
5924 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5925 int namespaces_only
, int flags
)
5928 tree val
= NULL_TREE
;
5930 query_oracle (name
);
5932 /* Conversion operators are handled specially because ordinary
5933 unqualified name lookup will not find template conversion
5935 if (IDENTIFIER_CONV_OP_P (name
))
5937 cp_binding_level
*level
;
5939 for (level
= current_binding_level
;
5940 level
&& level
->kind
!= sk_namespace
;
5941 level
= level
->level_chain
)
5946 /* A conversion operator can only be declared in a class
5948 if (level
->kind
!= sk_class
)
5951 /* Lookup the conversion operator in the class. */
5952 class_type
= level
->this_entity
;
5953 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
5961 flags
|= lookup_flags (prefer_type
, namespaces_only
);
5963 /* First, look in non-namespace scopes. */
5965 if (current_class_type
== NULL_TREE
)
5968 if (block_p
|| !nonclass
)
5969 for (iter
= outer_binding (name
, NULL
, !nonclass
);
5971 iter
= outer_binding (name
, iter
, !nonclass
))
5975 /* Skip entities we don't want. */
5976 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
5979 /* If this is the kind of thing we're looking for, we're done. */
5980 if (qualify_lookup (iter
->value
, flags
))
5981 binding
= iter
->value
;
5982 else if ((flags
& LOOKUP_PREFER_TYPES
)
5983 && qualify_lookup (iter
->type
, flags
))
5984 binding
= iter
->type
;
5986 binding
= NULL_TREE
;
5990 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
5992 /* A non namespace-scope binding can only be hidden in the
5993 presence of a local class, due to friend declarations.
5995 In particular, consider:
6003 B* b; // error: B is hidden
6004 C* c; // OK, finds ::C
6007 B *b; // error: B is hidden
6008 C *c; // OK, finds ::C
6013 The standard says that "B" is a local class in "f"
6014 (but not nested within "A") -- but that name lookup
6015 for "B" does not find this declaration until it is
6016 declared directly with "f".
6022 If a friend declaration appears in a local class and
6023 the name specified is an unqualified name, a prior
6024 declaration is looked up without considering scopes
6025 that are outside the innermost enclosing non-class
6026 scope. For a friend function declaration, if there is
6027 no prior declaration, the program is ill-formed. For a
6028 friend class declaration, if there is no prior
6029 declaration, the class that is specified belongs to the
6030 innermost enclosing non-class scope, but if it is
6031 subsequently referenced, its name is not found by name
6032 lookup until a matching declaration is provided in the
6033 innermost enclosing nonclass scope.
6035 So just keep looking for a non-hidden binding.
6037 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
6045 /* Now lookup in namespace scopes. */
6048 name_lookup
lookup (name
, flags
);
6049 if (lookup
.search_unqualified
6050 (current_decl_namespace (), current_binding_level
))
6054 /* If we have a single function from a using decl, pull it out. */
6055 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6056 val
= OVL_FUNCTION (val
);
6061 /* Wrapper for lookup_name_real_1. */
6064 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6065 int namespaces_only
, int flags
)
6068 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6069 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
6070 namespaces_only
, flags
);
6071 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6076 lookup_name_nonclass (tree name
)
6078 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
6082 lookup_name (tree name
)
6084 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
6088 lookup_name_prefer_type (tree name
, int prefer_type
)
6090 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6093 /* Look up NAME for type used in elaborated name specifier in
6094 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6095 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6096 name, more scopes are checked if cleanup or template parameter
6097 scope is encountered.
6099 Unlike lookup_name_real, we make sure that NAME is actually
6100 declared in the desired scope, not from inheritance, nor using
6101 directive. For using declaration, there is DR138 still waiting
6102 to be resolved. Hidden name coming from an earlier friend
6103 declaration is also returned.
6105 A TYPE_DECL best matching the NAME is returned. Catching error
6106 and issuing diagnostics are caller's responsibility. */
6109 lookup_type_scope_1 (tree name
, tag_scope scope
)
6111 cxx_binding
*iter
= NULL
;
6112 tree val
= NULL_TREE
;
6113 cp_binding_level
*level
= NULL
;
6115 /* Look in non-namespace scope first. */
6116 if (current_binding_level
->kind
!= sk_namespace
)
6117 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
6118 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
6120 /* Check if this is the kind of thing we're looking for.
6121 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6122 base class. For ITER->VALUE, we can simply use
6123 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6126 We check ITER->TYPE before ITER->VALUE in order to handle
6127 typedef struct C {} C;
6130 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
6131 && (scope
!= ts_current
6132 || LOCAL_BINDING_P (iter
)
6133 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
6135 else if ((scope
!= ts_current
6136 || !INHERITED_VALUE_BINDING_P (iter
))
6137 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
6144 /* Look in namespace scope. */
6146 level
= iter
->scope
;
6149 tree ns
= current_decl_namespace ();
6151 if (tree
*slot
= find_namespace_slot (ns
, name
))
6153 /* If this is the kind of thing we're looking for, we're done. */
6154 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6155 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6159 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6160 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6163 level
= NAMESPACE_LEVEL (ns
);
6167 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6168 and template parameter scopes. */
6171 cp_binding_level
*b
= current_binding_level
;
6177 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
6178 || b
->kind
== sk_function_parms
)
6180 else if (b
->kind
== sk_class
6181 && scope
== ts_within_enclosing_non_class
)
6191 /* Wrapper for lookup_type_scope_1. */
6194 lookup_type_scope (tree name
, tag_scope scope
)
6197 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6198 ret
= lookup_type_scope_1 (name
, scope
);
6199 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6203 /* Returns true iff DECL is a block-scope extern declaration of a function
6207 is_local_extern (tree decl
)
6209 cxx_binding
*binding
;
6211 /* For functions, this is easy. */
6212 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6213 return DECL_LOCAL_FUNCTION_P (decl
);
6217 if (!current_function_decl
)
6220 /* For variables, this is not easy. We need to look at the binding stack
6221 for the identifier to see whether the decl we have is a local. */
6222 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6223 binding
&& binding
->scope
->kind
!= sk_namespace
;
6224 binding
= binding
->previous
)
6225 if (binding
->value
== decl
)
6226 return LOCAL_BINDING_P (binding
);
6231 /* The type TYPE is being declared. If it is a class template, or a
6232 specialization of a class template, do any processing required and
6233 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6234 being declared a friend. B is the binding level at which this TYPE
6237 Returns the TYPE_DECL for TYPE, which may have been altered by this
6241 maybe_process_template_type_declaration (tree type
, int is_friend
,
6242 cp_binding_level
*b
)
6244 tree decl
= TYPE_NAME (type
);
6246 if (processing_template_parmlist
)
6247 /* You can't declare a new template type in a template parameter
6248 list. But, you can declare a non-template type:
6250 template <class A*> struct S;
6252 is a forward-declaration of `A'. */
6254 else if (b
->kind
== sk_namespace
6255 && current_binding_level
->kind
!= sk_namespace
)
6256 /* If this new type is being injected into a containing scope,
6257 then it's not a template type. */
6261 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6262 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6264 if (processing_template_decl
)
6266 /* This may change after the call to
6267 push_template_decl_real, but we want the original value. */
6268 tree name
= DECL_NAME (decl
);
6270 decl
= push_template_decl_real (decl
, is_friend
);
6271 if (decl
== error_mark_node
)
6272 return error_mark_node
;
6274 /* If the current binding level is the binding level for the
6275 template parameters (see the comment in
6276 begin_template_parm_list) and the enclosing level is a class
6277 scope, and we're not looking at a friend, push the
6278 declaration of the member class into the class scope. In the
6279 friend case, push_template_decl will already have put the
6280 friend into global scope, if appropriate. */
6281 if (TREE_CODE (type
) != ENUMERAL_TYPE
6282 && !is_friend
&& b
->kind
== sk_template_parms
6283 && b
->level_chain
->kind
== sk_class
)
6285 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6287 if (!COMPLETE_TYPE_P (current_class_type
))
6289 maybe_add_class_template_decl_list (current_class_type
,
6290 type
, /*friend_p=*/0);
6291 /* Put this UTD in the table of UTDs for the class. */
6292 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6293 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6294 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6296 binding_table_insert
6297 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6306 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6307 that the NAME is a class template, the tag is processed but not pushed.
6309 The pushed scope depend on the SCOPE parameter:
6310 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6312 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6313 non-template-parameter scope. This case is needed for forward
6315 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6316 TS_GLOBAL case except that names within template-parameter scopes
6317 are not pushed at all.
6319 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6322 do_pushtag (tree name
, tree type
, tag_scope scope
)
6326 cp_binding_level
*b
= current_binding_level
;
6327 while (/* Cleanup scopes are not scopes from the point of view of
6329 b
->kind
== sk_cleanup
6330 /* Neither are function parameter scopes. */
6331 || b
->kind
== sk_function_parms
6332 /* Neither are the scopes used to hold template parameters
6333 for an explicit specialization. For an ordinary template
6334 declaration, these scopes are not scopes from the point of
6335 view of the language. */
6336 || (b
->kind
== sk_template_parms
6337 && (b
->explicit_spec_p
|| scope
== ts_global
))
6338 || (b
->kind
== sk_class
6339 && (scope
!= ts_current
6340 /* We may be defining a new type in the initializer
6341 of a static member variable. We allow this when
6342 not pedantic, and it is particularly useful for
6343 type punning via an anonymous union. */
6344 || COMPLETE_TYPE_P (b
->this_entity
))))
6347 gcc_assert (identifier_p (name
));
6349 /* Do C++ gratuitous typedefing. */
6350 if (identifier_type_value_1 (name
) != type
)
6354 tree context
= TYPE_CONTEXT (type
);
6358 tree cs
= current_scope ();
6360 if (scope
== ts_current
6361 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6363 else if (cs
&& TYPE_P (cs
))
6364 /* When declaring a friend class of a local class, we want
6365 to inject the newly named class into the scope
6366 containing the local class, not the namespace
6368 context
= decl_function_context (get_type_decl (cs
));
6371 context
= current_namespace
;
6373 if (b
->kind
== sk_class
6374 || (b
->kind
== sk_template_parms
6375 && b
->level_chain
->kind
== sk_class
))
6378 tdef
= create_implicit_typedef (name
, type
);
6379 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6380 if (scope
== ts_within_enclosing_non_class
)
6382 /* This is a friend. Make this TYPE_DECL node hidden from
6383 ordinary name lookup. Its corresponding TEMPLATE_DECL
6384 will be marked in push_template_decl_real. */
6385 retrofit_lang_decl (tdef
);
6386 DECL_ANTICIPATED (tdef
) = 1;
6387 DECL_FRIEND_P (tdef
) = 1;
6390 decl
= maybe_process_template_type_declaration
6391 (type
, scope
== ts_within_enclosing_non_class
, b
);
6392 if (decl
== error_mark_node
)
6395 if (b
->kind
== sk_class
)
6397 if (!TYPE_BEING_DEFINED (current_class_type
))
6398 return error_mark_node
;
6400 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6401 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6402 class. But if it's a member template class, we want
6403 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6405 finish_member_declaration (decl
);
6407 pushdecl_class_level (decl
);
6409 else if (b
->kind
!= sk_template_parms
)
6411 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6412 if (decl
== error_mark_node
)
6415 if (DECL_CONTEXT (decl
) == std_node
6416 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6417 && !CLASSTYPE_TEMPLATE_INFO (type
))
6419 error ("declaration of std::initializer_list does not match "
6420 "#include <initializer_list>, isn't a template");
6421 return error_mark_node
;
6426 set_identifier_type_value_with_scope (name
, tdef
, b
);
6428 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6430 /* If this is a local class, keep track of it. We need this
6431 information for name-mangling, and so that it is possible to
6432 find all function definitions in a translation unit in a
6433 convenient way. (It's otherwise tricky to find a member
6434 function definition it's only pointed to from within a local
6436 if (TYPE_FUNCTION_SCOPE_P (type
))
6438 if (processing_template_decl
)
6440 /* Push a DECL_EXPR so we call pushtag at the right time in
6441 template instantiation rather than in some nested context. */
6442 add_decl_expr (decl
);
6445 vec_safe_push (local_classes
, type
);
6449 if (b
->kind
== sk_class
6450 && !COMPLETE_TYPE_P (current_class_type
))
6452 maybe_add_class_template_decl_list (current_class_type
,
6453 type
, /*friend_p=*/0);
6455 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6456 CLASSTYPE_NESTED_UTDS (current_class_type
)
6457 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6459 binding_table_insert
6460 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6463 decl
= TYPE_NAME (type
);
6464 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6466 /* Set type visibility now if this is a forward declaration. */
6467 TREE_PUBLIC (decl
) = 1;
6468 determine_visibility (decl
);
6473 /* Wrapper for do_pushtag. */
6476 pushtag (tree name
, tree type
, tag_scope scope
)
6479 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6480 ret
= do_pushtag (name
, type
, scope
);
6481 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6486 /* Subroutines for reverting temporarily to top-level for instantiation
6487 of templates and such. We actually need to clear out the class- and
6488 local-value slots of all identifiers, so that only the global values
6489 are at all visible. Simply setting current_binding_level to the global
6490 scope isn't enough, because more binding levels may be pushed. */
6491 struct saved_scope
*scope_chain
;
6493 /* Return true if ID has not already been marked. */
6496 store_binding_p (tree id
)
6498 if (!id
|| !IDENTIFIER_BINDING (id
))
6501 if (IDENTIFIER_MARKED (id
))
6507 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6508 have enough space reserved. */
6511 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6513 cxx_saved_binding saved
;
6515 gcc_checking_assert (store_binding_p (id
));
6517 IDENTIFIER_MARKED (id
) = 1;
6519 saved
.identifier
= id
;
6520 saved
.binding
= IDENTIFIER_BINDING (id
);
6521 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6522 (*old_bindings
)->quick_push (saved
);
6523 IDENTIFIER_BINDING (id
) = NULL
;
6527 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6529 static vec
<tree
> bindings_need_stored
;
6533 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6534 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6536 if (TREE_CODE (t
) == TREE_LIST
)
6537 id
= TREE_PURPOSE (t
);
6541 if (store_binding_p (id
))
6542 bindings_need_stored
.safe_push (id
);
6544 if (!bindings_need_stored
.is_empty ())
6546 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6547 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6549 /* We can apparently have duplicates in NAMES. */
6550 if (store_binding_p (id
))
6551 store_binding (id
, old_bindings
);
6553 bindings_need_stored
.truncate (0);
6555 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6558 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6559 objects, rather than a TREE_LIST. */
6562 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6563 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6565 static vec
<tree
> bindings_need_stored
;
6567 cp_class_binding
*cb
;
6569 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6570 if (store_binding_p (cb
->identifier
))
6571 bindings_need_stored
.safe_push (cb
->identifier
);
6572 if (!bindings_need_stored
.is_empty ())
6575 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6576 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6577 store_binding (id
, old_bindings
);
6578 bindings_need_stored
.truncate (0);
6582 /* A chain of saved_scope structures awaiting reuse. */
6584 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
6587 do_push_to_top_level (void)
6589 struct saved_scope
*s
;
6590 cp_binding_level
*b
;
6591 cxx_saved_binding
*sb
;
6595 /* Reuse or create a new structure for this saved scope. */
6596 if (free_saved_scope
!= NULL
)
6598 s
= free_saved_scope
;
6599 free_saved_scope
= s
->prev
;
6601 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
6602 memset (s
, 0, sizeof (*s
));
6603 /* Also reuse the structure's old_bindings vector. */
6604 vec_safe_truncate (old_bindings
, 0);
6605 s
->old_bindings
= old_bindings
;
6608 s
= ggc_cleared_alloc
<saved_scope
> ();
6610 b
= scope_chain
? current_binding_level
: 0;
6612 /* If we're in the middle of some function, save our state. */
6616 push_function_context ();
6621 if (scope_chain
&& previous_class_level
)
6622 store_class_bindings (previous_class_level
->class_shadowed
,
6625 /* Have to include the global scope, because class-scope decls
6626 aren't listed anywhere useful. */
6627 for (; b
; b
= b
->level_chain
)
6631 /* Template IDs are inserted into the global level. If they were
6632 inserted into namespace level, finish_file wouldn't find them
6633 when doing pending instantiations. Therefore, don't stop at
6634 namespace level, but continue until :: . */
6635 if (global_scope_p (b
))
6638 store_bindings (b
->names
, &s
->old_bindings
);
6639 /* We also need to check class_shadowed to save class-level type
6640 bindings, since pushclass doesn't fill in b->names. */
6641 if (b
->kind
== sk_class
)
6642 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
6644 /* Unwind type-value slots back to top level. */
6645 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
6646 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
6649 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
6650 IDENTIFIER_MARKED (sb
->identifier
) = 0;
6652 s
->prev
= scope_chain
;
6654 s
->need_pop_function_context
= need_pop
;
6655 s
->function_decl
= current_function_decl
;
6656 s
->unevaluated_operand
= cp_unevaluated_operand
;
6657 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6658 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
6661 current_function_decl
= NULL_TREE
;
6662 vec_alloc (current_lang_base
, 10);
6663 current_lang_name
= lang_name_cplusplus
;
6664 current_namespace
= global_namespace
;
6665 push_class_stack ();
6666 cp_unevaluated_operand
= 0;
6667 c_inhibit_evaluation_warnings
= 0;
6671 do_pop_from_top_level (void)
6673 struct saved_scope
*s
= scope_chain
;
6674 cxx_saved_binding
*saved
;
6677 /* Clear out class-level bindings cache. */
6678 if (previous_class_level
)
6679 invalidate_class_lookup_cache ();
6682 current_lang_base
= 0;
6684 scope_chain
= s
->prev
;
6685 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
6687 tree id
= saved
->identifier
;
6689 IDENTIFIER_BINDING (id
) = saved
->binding
;
6690 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
6693 /* If we were in the middle of compiling a function, restore our
6695 if (s
->need_pop_function_context
)
6696 pop_function_context ();
6697 current_function_decl
= s
->function_decl
;
6698 cp_unevaluated_operand
= s
->unevaluated_operand
;
6699 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
6701 /* Make this saved_scope structure available for reuse by
6702 push_to_top_level. */
6703 s
->prev
= free_saved_scope
;
6704 free_saved_scope
= s
;
6707 /* Push into the scope of the namespace NS, even if it is deeply
6708 nested within another namespace. */
6711 do_push_nested_namespace (tree ns
)
6713 if (ns
== global_namespace
)
6714 do_push_to_top_level ();
6717 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
6719 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
6720 resume_scope (NAMESPACE_LEVEL (ns
));
6721 current_namespace
= ns
;
6725 /* Pop back from the scope of the namespace NS, which was previously
6726 entered with push_nested_namespace. */
6729 do_pop_nested_namespace (tree ns
)
6731 while (ns
!= global_namespace
)
6733 ns
= CP_DECL_CONTEXT (ns
);
6734 current_namespace
= ns
;
6738 do_pop_from_top_level ();
6741 /* Add TARGET to USINGS, if it does not already exist there.
6742 We used to build the complete graph of usings at this point, from
6743 the POV of the source namespaces. Now we build that as we perform
6744 the unqualified search. */
6747 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
6750 for (unsigned ix
= usings
->length (); ix
--;)
6751 if ((*usings
)[ix
] == target
)
6754 vec_safe_push (usings
, target
);
6757 /* Tell the debug system of a using directive. */
6760 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
6762 /* Emit debugging info. */
6763 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
6764 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
6768 /* Process a namespace-scope using directive. */
6771 finish_namespace_using_directive (tree target
, tree attribs
)
6773 gcc_checking_assert (namespace_bindings_p ());
6774 if (target
== error_mark_node
)
6777 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6778 ORIGINAL_NAMESPACE (target
));
6779 emit_debug_info_using_namespace (current_namespace
,
6780 ORIGINAL_NAMESPACE (target
), false);
6782 if (attribs
== error_mark_node
)
6785 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
6787 tree name
= get_attribute_name (a
);
6788 if (is_attribute_p ("strong", name
))
6790 warning (0, "strong using directive no longer supported");
6791 if (CP_DECL_CONTEXT (target
) == current_namespace
)
6792 inform (DECL_SOURCE_LOCATION (target
),
6793 "you may use an inline namespace instead");
6796 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
6800 /* Process a function-scope using-directive. */
6803 finish_local_using_directive (tree target
, tree attribs
)
6805 gcc_checking_assert (local_bindings_p ());
6806 if (target
== error_mark_node
)
6810 warning (OPT_Wattributes
, "attributes ignored on local using directive");
6812 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
6814 add_using_namespace (current_binding_level
->using_directives
,
6815 ORIGINAL_NAMESPACE (target
));
6818 /* Pushes X into the global namespace. */
6821 pushdecl_top_level (tree x
, bool is_friend
)
6823 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6824 do_push_to_top_level ();
6825 x
= pushdecl_namespace_level (x
, is_friend
);
6826 do_pop_from_top_level ();
6827 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6831 /* Pushes X into the global namespace and calls cp_finish_decl to
6832 register the variable, initializing it with INIT. */
6835 pushdecl_top_level_and_finish (tree x
, tree init
)
6837 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6838 do_push_to_top_level ();
6839 x
= pushdecl_namespace_level (x
, false);
6840 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
6841 do_pop_from_top_level ();
6842 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6846 /* Enter the namespaces from current_namerspace to NS. */
6849 push_inline_namespaces (tree ns
)
6852 if (ns
!= current_namespace
)
6854 gcc_assert (ns
!= global_namespace
);
6855 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6856 resume_scope (NAMESPACE_LEVEL (ns
));
6857 current_namespace
= ns
;
6863 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6864 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6865 we create an inline namespace (it is up to the caller to check upon
6866 redefinition). Return the number of namespaces entered. */
6869 push_namespace (tree name
, bool make_inline
)
6871 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6874 /* We should not get here if the global_namespace is not yet constructed
6875 nor if NAME designates the global namespace: The global scope is
6876 constructed elsewhere. */
6877 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
6879 tree ns
= NULL_TREE
;
6881 name_lookup
lookup (name
, 0);
6882 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
6884 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
6886 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
6888 /* A namespace alias is not allowed here, but if the alias
6889 is for a namespace also inside the current scope,
6890 accept it with a diagnostic. That's better than dying
6892 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
6894 error ("namespace alias %qD not allowed here, "
6895 "assuming %qD", lookup
.value
, dna
);
6903 bool new_ns
= false;
6905 /* DR2061. NS might be a member of an inline namespace. We
6906 need to push into those namespaces. */
6907 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6910 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
6911 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
6912 if (!SCOPE_DEPTH (ns
))
6913 /* We only allow depth 255. */
6914 sorry ("cannot nest more than %d namespaces",
6915 SCOPE_DEPTH (current_namespace
));
6916 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
6919 if (pushdecl (ns
) == error_mark_node
)
6925 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
6928 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6931 else if (TREE_PUBLIC (current_namespace
))
6932 TREE_PUBLIC (ns
) = 1;
6936 DECL_NAMESPACE_INLINE_P (ns
) = true;
6937 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
6940 if (!name
|| make_inline
)
6941 emit_debug_info_using_namespace (current_namespace
, ns
, true);
6947 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
6949 error ("inline namespace must be specified at initial definition");
6950 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
6953 begin_scope (sk_namespace
, ns
);
6955 resume_scope (NAMESPACE_LEVEL (ns
));
6956 current_namespace
= ns
;
6960 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6964 /* Pop from the scope of the current namespace. */
6967 pop_namespace (void)
6969 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6971 gcc_assert (current_namespace
!= global_namespace
);
6972 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
6973 /* The binding level is not popped, as it might be re-opened later. */
6976 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6979 /* External entry points for do_{push_to/pop_from}_top_level. */
6982 push_to_top_level (void)
6984 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6985 do_push_to_top_level ();
6986 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6990 pop_from_top_level (void)
6992 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6993 do_pop_from_top_level ();
6994 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6997 /* External entry points for do_{push,pop}_nested_namespace. */
7000 push_nested_namespace (tree ns
)
7002 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7003 do_push_nested_namespace (ns
);
7004 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7008 pop_nested_namespace (tree ns
)
7010 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7011 gcc_assert (current_namespace
== ns
);
7012 do_pop_nested_namespace (ns
);
7013 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7016 /* Pop off extraneous binding levels left over due to syntax errors.
7017 We don't pop past namespaces, as they might be valid. */
7020 pop_everything (void)
7022 if (ENABLE_SCOPE_CHECKING
)
7023 verbatim ("XXX entering pop_everything ()\n");
7024 while (!namespace_bindings_p ())
7026 if (current_binding_level
->kind
== sk_class
)
7027 pop_nested_class ();
7031 if (ENABLE_SCOPE_CHECKING
)
7032 verbatim ("XXX leaving pop_everything ()\n");
7035 /* Emit debugging information for using declarations and directives.
7036 If input tree is overloaded fn then emit debug info for all
7040 cp_emit_debug_info_for_using (tree t
, tree context
)
7042 /* Don't try to emit any debug information if we have errors. */
7046 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7047 of a builtin function. */
7048 if (TREE_CODE (t
) == FUNCTION_DECL
7049 && DECL_EXTERNAL (t
)
7050 && DECL_BUILT_IN (t
))
7053 /* Do not supply context to imported_module_or_decl, if
7054 it is a global namespace. */
7055 if (context
== global_namespace
)
7056 context
= NULL_TREE
;
7058 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7060 /* FIXME: Handle TEMPLATE_DECLs. */
7061 for (lkp_iterator
iter (t
); iter
; ++iter
)
7064 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7066 if (building_stmt_list_p ())
7067 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7069 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7075 #include "gt-cp-name-lookup.h"