1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2018 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
)
715 /* If looking for hidden names, we only look in the innermost
716 namespace scope. [namespace.memdef]/3 If a friend
717 declaration in a non-local class first declares a class,
718 function, class template or function template the friend is a
719 member of the innermost enclosing namespace. See also
720 [basic.lookup.unqual]/7 */
721 if (flags
& LOOKUP_HIDDEN
)
725 vec_safe_truncate (queue
, length
);
730 /* FNS is a value binding. If it is a (set of overloaded) functions,
731 add them into the current value. */
734 name_lookup::add_fns (tree fns
)
738 else if (TREE_CODE (fns
) == OVERLOAD
)
740 if (TREE_TYPE (fns
) != unknown_type_node
)
741 fns
= OVL_FUNCTION (fns
);
743 else if (!DECL_DECLARES_FUNCTION_P (fns
))
749 /* Add functions of a namespace to the lookup structure. */
752 name_lookup::adl_namespace_only (tree scope
)
756 /* Look down into inline namespaces. */
757 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
758 for (unsigned ix
= inlinees
->length (); ix
--;)
759 adl_namespace_only ((*inlinees
)[ix
]);
761 if (tree fns
= find_namespace_value (scope
, name
))
762 add_fns (ovl_skip_hidden (fns
));
765 /* Find the containing non-inlined namespace, add it and all its
769 name_lookup::adl_namespace (tree scope
)
774 /* Find the containing non-inline namespace. */
775 while (DECL_NAMESPACE_INLINE_P (scope
))
776 scope
= CP_DECL_CONTEXT (scope
);
778 adl_namespace_only (scope
);
781 /* Adds the class and its friends to the lookup structure. */
784 name_lookup::adl_class_only (tree type
)
786 /* Backend-built structures, such as __builtin_va_list, aren't
787 affected by all this. */
788 if (!CLASS_TYPE_P (type
))
791 type
= TYPE_MAIN_VARIANT (type
);
793 if (see_and_mark (type
))
796 tree context
= decl_namespace_context (type
);
797 adl_namespace (context
);
799 complete_type (type
);
802 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
803 list
= TREE_CHAIN (list
))
804 if (name
== FRIEND_NAME (list
))
805 for (tree friends
= FRIEND_DECLS (list
); friends
;
806 friends
= TREE_CHAIN (friends
))
808 tree fn
= TREE_VALUE (friends
);
810 /* Only interested in global functions with potentially hidden
811 (i.e. unqualified) declarations. */
812 if (CP_DECL_CONTEXT (fn
) != context
)
815 /* Only interested in anticipated friends. (Non-anticipated
816 ones will have been inserted during the namespace
818 if (!DECL_ANTICIPATED (fn
))
821 /* Template specializations are never found by name lookup.
822 (Templates themselves can be found, but not template
824 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
831 /* Adds the class and its bases to the lookup structure.
832 Returns true on error. */
835 name_lookup::adl_bases (tree type
)
837 adl_class_only (type
);
839 /* Process baseclasses. */
840 if (tree binfo
= TYPE_BINFO (type
))
845 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
846 adl_bases (BINFO_TYPE (base_binfo
));
850 /* Adds everything associated with a class argument type to the lookup
851 structure. Returns true on error.
853 If T is a class type (including unions), its associated classes are: the
854 class itself; the class of which it is a member, if any; and its direct
855 and indirect base classes. Its associated namespaces are the namespaces
856 of which its associated classes are members. Furthermore, if T is a
857 class template specialization, its associated namespaces and classes
858 also include: the namespaces and classes associated with the types of
859 the template arguments provided for template type parameters (excluding
860 template template parameters); the namespaces of which any template
861 template arguments are members; and the classes of which any member
862 templates used as template template arguments are members. [ Note:
863 non-type template arguments do not contribute to the set of associated
864 namespaces. --end note] */
867 name_lookup::adl_class (tree type
)
869 /* Backend build structures, such as __builtin_va_list, aren't
870 affected by all this. */
871 if (!CLASS_TYPE_P (type
))
874 type
= TYPE_MAIN_VARIANT (type
);
875 /* We don't set found here because we have to have set seen first,
876 which is done in the adl_bases walk. */
883 if (TYPE_CLASS_SCOPE_P (type
))
884 adl_class_only (TYPE_CONTEXT (type
));
886 /* Process template arguments. */
887 if (CLASSTYPE_TEMPLATE_INFO (type
)
888 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
890 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
891 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
892 adl_template_arg (TREE_VEC_ELT (list
, i
));
897 name_lookup::adl_expr (tree expr
)
902 gcc_assert (!TYPE_P (expr
));
904 if (TREE_TYPE (expr
) != unknown_type_node
)
906 adl_type (TREE_TYPE (expr
));
910 if (TREE_CODE (expr
) == ADDR_EXPR
)
911 expr
= TREE_OPERAND (expr
, 0);
912 if (TREE_CODE (expr
) == COMPONENT_REF
913 || TREE_CODE (expr
) == OFFSET_REF
)
914 expr
= TREE_OPERAND (expr
, 1);
915 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
918 for (lkp_iterator
iter (expr
); iter
; ++iter
)
919 adl_type (TREE_TYPE (*iter
));
920 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
922 /* The working paper doesn't currently say how to handle
923 template-id arguments. The sensible thing would seem to be
924 to handle the list of template candidates like a normal
925 overload set, and handle the template arguments like we do
926 for class template specializations. */
928 /* First the templates. */
929 adl_expr (TREE_OPERAND (expr
, 0));
931 /* Now the arguments. */
932 if (tree args
= TREE_OPERAND (expr
, 1))
933 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
934 adl_template_arg (TREE_VEC_ELT (args
, ix
));
939 name_lookup::adl_type (tree type
)
944 if (TYPE_PTRDATAMEM_P (type
))
946 /* Pointer to member: associate class type and value type. */
947 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
948 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
952 switch (TREE_CODE (type
))
955 if (TYPE_PTRMEMFUNC_P (type
))
957 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
966 /* The basetype is referenced in the first arg type, so just
969 /* Associate the parameter types. */
970 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
971 adl_type (TREE_VALUE (args
));
977 adl_type (TREE_TYPE (type
));
981 if (TYPE_CLASS_SCOPE_P (type
))
982 adl_class_only (TYPE_CONTEXT (type
));
983 adl_namespace (decl_namespace_context (type
));
987 gcc_assert (type
== unknown_type_node
988 || type
== init_list_type_node
);
991 case TYPE_PACK_EXPANSION
:
992 adl_type (PACK_EXPANSION_PATTERN (type
));
1000 /* Adds everything associated with a template argument to the lookup
1004 name_lookup::adl_template_arg (tree arg
)
1006 /* [basic.lookup.koenig]
1008 If T is a template-id, its associated namespaces and classes are
1009 ... the namespaces and classes associated with the types of the
1010 template arguments provided for template type parameters
1011 (excluding template template parameters); the namespaces in which
1012 any template template arguments are defined; and the classes in
1013 which any member templates used as template template arguments
1014 are defined. [Note: non-type template arguments do not
1015 contribute to the set of associated namespaces. ] */
1017 /* Consider first template template arguments. */
1018 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1019 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1021 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1023 tree ctx
= CP_DECL_CONTEXT (arg
);
1025 /* It's not a member template. */
1026 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1027 adl_namespace (ctx
);
1028 /* Otherwise, it must be member template. */
1030 adl_class_only (ctx
);
1032 /* It's an argument pack; handle it recursively. */
1033 else if (ARGUMENT_PACK_P (arg
))
1035 tree args
= ARGUMENT_PACK_ARGS (arg
);
1036 int i
, len
= TREE_VEC_LENGTH (args
);
1037 for (i
= 0; i
< len
; ++i
)
1038 adl_template_arg (TREE_VEC_ELT (args
, i
));
1040 /* It's not a template template argument, but it is a type template
1042 else if (TYPE_P (arg
))
1046 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1047 the call arguments. */
1050 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1055 lookup_mark (fns
, true);
1062 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1063 /* OMP reduction operators put an ADL-significant type as the
1075 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1076 static void consider_binding_level (tree name
,
1077 best_match
<tree
, const char *> &bm
,
1078 cp_binding_level
*lvl
,
1079 bool look_within_fields
,
1080 enum lookup_name_fuzzy_kind kind
);
1081 static void diagnose_name_conflict (tree
, tree
);
1083 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1084 don't add duplicates to it. ARGS is the vector of call
1085 arguments (which will not be empty). */
1088 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1090 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1091 name_lookup
lookup (name
);
1092 fns
= lookup
.search_adl (fns
, args
);
1093 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1097 /* FNS is an overload set of conversion functions. Return the
1098 overloads converting to TYPE. */
1101 extract_conversion_operator (tree fns
, tree type
)
1103 tree convs
= NULL_TREE
;
1104 tree tpls
= NULL_TREE
;
1106 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1108 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1109 convs
= lookup_add (*iter
, convs
);
1111 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1112 tpls
= lookup_add (*iter
, tpls
);
1121 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1124 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1126 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1128 unsigned mid
= (lo
+ hi
) / 2;
1129 tree binding
= (*member_vec
)[mid
];
1130 tree binding_name
= OVL_NAME (binding
);
1132 if (binding_name
> name
)
1134 else if (binding_name
< name
)
1143 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1146 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1148 for (int ix
= member_vec
->length (); ix
--;)
1149 /* We can get a NULL binding during insertion of a new method
1150 name, because the identifier_binding machinery performs a
1151 lookup. If we find such a NULL slot, that's the thing we were
1152 looking for, so we might as well bail out immediately. */
1153 if (tree binding
= (*member_vec
)[ix
])
1155 if (OVL_NAME (binding
) == name
)
1164 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1167 fields_linear_search (tree klass
, tree name
, bool want_type
)
1169 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1174 && TREE_CODE (decl
) == FIELD_DECL
1175 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1177 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
))
1181 if (DECL_NAME (decl
) != name
)
1184 if (TREE_CODE (decl
) == USING_DECL
)
1186 decl
= strip_using_decl (decl
);
1187 if (is_overloaded_fn (decl
))
1191 if (DECL_DECLARES_FUNCTION_P (decl
))
1192 /* Functions are found separately. */
1195 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1202 /* Look for NAME field inside of anonymous aggregate ANON. */
1205 search_anon_aggr (tree anon
, tree name
)
1207 gcc_assert (COMPLETE_TYPE_P (anon
));
1210 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
1211 ret
= member_vec_linear_search (member_vec
, name
);
1213 ret
= fields_linear_search (anon
, name
, false);
1217 /* Anon members can only contain fields. */
1218 gcc_assert (!STAT_HACK_P (ret
) && !DECL_DECLARES_TYPE_P (ret
));
1224 /* Look for NAME as an immediate member of KLASS (including
1225 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1226 regular search. >0 to get a type binding (if there is one) and <0
1227 if you want (just) the member function binding.
1229 Use this if you do not want lazy member creation. */
1232 get_class_binding_direct (tree klass
, tree name
, int type_or_fns
)
1234 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1236 /* Conversion operators can only be found by the marker conversion
1238 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1239 tree lookup
= conv_op
? conv_op_identifier
: name
;
1240 tree val
= NULL_TREE
;
1241 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1243 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1245 val
= member_vec_binary_search (member_vec
, lookup
);
1248 else if (type_or_fns
> 0)
1250 if (STAT_HACK_P (val
))
1251 val
= STAT_TYPE (val
);
1252 else if (!DECL_DECLARES_TYPE_P (val
))
1255 else if (STAT_HACK_P (val
))
1256 val
= STAT_DECL (val
);
1258 if (val
&& TREE_CODE (val
) == OVERLOAD
1259 && TREE_CODE (OVL_FUNCTION (val
)) == USING_DECL
)
1261 /* An overload with a dependent USING_DECL. Does the caller
1262 want the USING_DECL or the functions? */
1263 if (type_or_fns
< 0)
1264 val
= OVL_CHAIN (val
);
1266 val
= OVL_FUNCTION (val
);
1271 if (member_vec
&& type_or_fns
<= 0)
1272 val
= member_vec_linear_search (member_vec
, lookup
);
1274 if (type_or_fns
< 0)
1275 /* Don't bother looking for field. We don't want it. */;
1276 else if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_USING_P (val
)))
1277 /* Dependent using declarations are a 'field', make sure we
1278 return that even if we saw an overload already. */
1279 if (tree field_val
= fields_linear_search (klass
, lookup
,
1281 if (!val
|| TREE_CODE (field_val
) == USING_DECL
)
1285 /* Extract the conversion operators asked for, unless the general
1286 conversion operator was requested. */
1289 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1290 val
= OVL_CHAIN (val
);
1291 if (tree type
= TREE_TYPE (name
))
1292 val
= extract_conversion_operator (val
, type
);
1298 /* Look for NAME's binding in exactly KLASS. See
1299 get_class_binding_direct for argument description. Does lazy
1300 special function creation as necessary. */
1303 get_class_binding (tree klass
, tree name
, int type_or_fns
)
1305 klass
= complete_type (klass
);
1307 if (COMPLETE_TYPE_P (klass
))
1309 /* Lazily declare functions, if we're going to search these. */
1310 if (IDENTIFIER_CTOR_P (name
))
1312 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1313 lazily_declare_fn (sfk_constructor
, klass
);
1314 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1315 lazily_declare_fn (sfk_copy_constructor
, klass
);
1316 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1317 lazily_declare_fn (sfk_move_constructor
, klass
);
1319 else if (IDENTIFIER_DTOR_P (name
))
1321 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1322 lazily_declare_fn (sfk_destructor
, klass
);
1324 else if (name
== assign_op_identifier
)
1326 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1327 lazily_declare_fn (sfk_copy_assignment
, klass
);
1328 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1329 lazily_declare_fn (sfk_move_assignment
, klass
);
1333 return get_class_binding_direct (klass
, name
, type_or_fns
);
1336 /* Find the slot containing overloads called 'NAME'. If there is no
1337 such slot, create an empty one. KLASS might be complete at this
1338 point, in which case we need to preserve ordering. Deals with
1339 conv_op marker handling. */
1342 get_member_slot (tree klass
, tree name
)
1344 bool complete_p
= COMPLETE_TYPE_P (klass
);
1346 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1349 vec_alloc (member_vec
, 8);
1350 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1353 /* If the class is complete but had no member_vec, we need
1354 to add the TYPE_FIELDS into it. We're also most likely
1355 to be adding ctors & dtors, so ask for 6 spare slots (the
1356 abstract cdtors and their clones). */
1357 set_class_bindings (klass
, 6);
1358 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1362 if (IDENTIFIER_CONV_OP_P (name
))
1363 name
= conv_op_identifier
;
1365 unsigned ix
, length
= member_vec
->length ();
1366 for (ix
= 0; ix
< length
; ix
++)
1368 tree
*slot
= &(*member_vec
)[ix
];
1369 tree fn_name
= OVL_NAME (*slot
);
1371 if (fn_name
== name
)
1373 /* If we found an existing slot, it must be a function set.
1374 Even with insertion after completion, because those only
1375 happen with artificial fns that have unspellable names.
1376 This means we do not have to deal with the stat hack
1378 gcc_checking_assert (OVL_P (*slot
));
1379 if (name
== conv_op_identifier
)
1381 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1382 /* Skip the conv-op marker. */
1383 slot
= &OVL_CHAIN (*slot
);
1388 if (complete_p
&& fn_name
> name
)
1392 /* No slot found. Create one at IX. We know in this case that our
1393 caller will succeed in adding the function. */
1396 /* Do exact allocation when complete, as we don't expect to add
1398 vec_safe_reserve_exact (member_vec
, 1);
1399 member_vec
->quick_insert (ix
, NULL_TREE
);
1403 gcc_checking_assert (ix
== length
);
1404 vec_safe_push (member_vec
, NULL_TREE
);
1406 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1408 tree
*slot
= &(*member_vec
)[ix
];
1409 if (name
== conv_op_identifier
)
1411 /* Install the marker prefix. */
1412 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1413 slot
= &OVL_CHAIN (*slot
);
1419 /* Comparison function to compare two MEMBER_VEC entries by name.
1420 Because we can have duplicates during insertion of TYPE_FIELDS, we
1421 do extra checking so deduping doesn't have to deal with so many
1425 member_name_cmp (const void *a_p
, const void *b_p
)
1427 tree a
= *(const tree
*)a_p
;
1428 tree b
= *(const tree
*)b_p
;
1429 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1430 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1432 gcc_checking_assert (name_a
&& name_b
);
1433 if (name_a
!= name_b
)
1434 return name_a
< name_b
? -1 : +1;
1436 if (name_a
== conv_op_identifier
)
1438 /* Strip the conv-op markers. */
1439 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1440 && OVL_FUNCTION (b
) == conv_op_marker
);
1445 if (TREE_CODE (a
) == OVERLOAD
)
1446 a
= OVL_FUNCTION (a
);
1447 if (TREE_CODE (b
) == OVERLOAD
)
1448 b
= OVL_FUNCTION (b
);
1450 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1451 if (TREE_CODE (a
) != TREE_CODE (b
))
1453 /* If one of them is a TYPE_DECL, it loses. */
1454 if (TREE_CODE (a
) == TYPE_DECL
)
1456 else if (TREE_CODE (b
) == TYPE_DECL
)
1459 /* If one of them is a USING_DECL, it loses. */
1460 if (TREE_CODE (a
) == USING_DECL
)
1462 else if (TREE_CODE (b
) == USING_DECL
)
1465 /* There are no other cases with different kinds of decls, as
1466 duplicate detection should have kicked in earlier. However,
1467 some erroneous cases get though. */
1468 gcc_assert (errorcount
);
1471 /* Using source location would be the best thing here, but we can
1472 get identically-located decls in the following circumstances:
1474 1) duplicate artificial type-decls for the same type.
1476 2) pack expansions of using-decls.
1478 We should not be doing #1, but in either case it doesn't matter
1479 how we order these. Use UID as a proxy for source ordering, so
1480 that identically-located decls still have a well-defined stable
1482 if (DECL_UID (a
) != DECL_UID (b
))
1483 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1484 gcc_assert (a
== b
);
1489 gt_pointer_operator new_value
;
1493 /* This routine compares two fields like member_name_cmp but using the
1494 pointer operator in resort_field_decl_data. We don't have to deal
1495 with duplicates here. */
1498 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1500 tree a
= *(const tree
*)a_p
;
1501 tree b
= *(const tree
*)b_p
;
1502 tree name_a
= OVL_NAME (a
);
1503 tree name_b
= OVL_NAME (b
);
1505 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1506 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1508 gcc_checking_assert (name_a
!= name_b
);
1510 return name_a
< name_b
? -1 : +1;
1513 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1516 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1517 gt_pointer_operator new_value
, void* cookie
)
1519 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1521 resort_data
.new_value
= new_value
;
1522 resort_data
.cookie
= cookie
;
1523 member_vec
->qsort (resort_member_name_cmp
);
1527 /* Recursively count the number of fields in KLASS, including anonymous
1531 count_class_fields (tree klass
)
1533 unsigned n_fields
= 0;
1535 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1536 if (DECL_DECLARES_FUNCTION_P (fields
))
1537 /* Functions are dealt with separately. */;
1538 else if (TREE_CODE (fields
) == FIELD_DECL
1539 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1540 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1541 else if (DECL_NAME (fields
))
1547 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1548 Recurse for anonymous members. MEMBER_VEC must have space. */
1551 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1553 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1554 if (DECL_DECLARES_FUNCTION_P (fields
))
1555 /* Functions are handled separately. */;
1556 else if (TREE_CODE (fields
) == FIELD_DECL
1557 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1558 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1559 else if (DECL_NAME (fields
))
1561 tree field
= fields
;
1562 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1563 if (TREE_CODE (field
) == USING_DECL
1564 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1565 field
= ovl_make (conv_op_marker
, field
);
1566 member_vec
->quick_push (field
);
1570 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1571 MEMBER_VEC must have space. */
1574 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1576 for (tree values
= TYPE_VALUES (enumtype
);
1577 values
; values
= TREE_CHAIN (values
))
1578 member_vec
->quick_push (TREE_VALUE (values
));
1581 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1582 DeDup adjacent DECLS of the same name. We already dealt with
1583 conflict resolution when adding the fields or methods themselves.
1584 There are three cases (which could all be combined):
1585 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1586 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1587 it wins. Otherwise the OVERLOAD does.
1588 3) two USING_DECLS. ...
1590 member_name_cmp will have ordered duplicates as
1591 <fns><using><type> */
1594 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1596 unsigned len
= member_vec
->length ();
1602 tree current
= (*member_vec
)[0], name
= OVL_NAME (current
);
1603 tree next
= NULL_TREE
, next_name
= NULL_TREE
;
1604 for (unsigned jx
, ix
= 0; ix
< len
;
1605 ix
= jx
, current
= next
, name
= next_name
)
1607 tree to_type
= NULL_TREE
;
1608 tree to_using
= NULL_TREE
;
1609 tree marker
= NULL_TREE
;
1610 if (IDENTIFIER_CONV_OP_P (name
))
1613 current
= OVL_CHAIN (current
);
1614 name
= DECL_NAME (OVL_FUNCTION (marker
));
1615 gcc_checking_assert (name
== conv_op_identifier
);
1618 if (TREE_CODE (current
) == USING_DECL
)
1620 current
= strip_using_decl (current
);
1621 if (is_overloaded_fn (current
))
1622 current
= NULL_TREE
;
1623 else if (TREE_CODE (current
) == USING_DECL
)
1626 current
= NULL_TREE
;
1630 if (current
&& DECL_DECLARES_TYPE_P (current
))
1633 current
= NULL_TREE
;
1636 for (jx
= ix
+ 1; jx
< len
; jx
++)
1638 next
= (*member_vec
)[jx
];
1639 next_name
= OVL_NAME (next
);
1640 if (next_name
!= name
)
1645 gcc_checking_assert (OVL_FUNCTION (marker
)
1646 == OVL_FUNCTION (next
));
1647 next
= OVL_CHAIN (next
);
1650 if (TREE_CODE (next
) == USING_DECL
)
1652 next
= strip_using_decl (next
);
1653 if (is_overloaded_fn (next
))
1655 else if (TREE_CODE (next
) == USING_DECL
)
1662 if (next
&& DECL_DECLARES_TYPE_P (next
))
1671 current
= ovl_make (to_using
, current
);
1679 current
= stat_hack (current
, to_type
);
1682 gcc_assert (current
);
1685 OVL_CHAIN (marker
) = current
;
1688 (*member_vec
)[store
++] = current
;
1691 while (store
++ < len
)
1695 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1696 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1697 know there must be at least 1 field -- the self-reference
1698 TYPE_DECL, except for anon aggregates, which will have at least
1702 set_class_bindings (tree klass
, unsigned extra
)
1704 unsigned n_fields
= count_class_fields (klass
);
1705 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1707 if (member_vec
|| n_fields
>= 8)
1709 /* Append the new fields. */
1710 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1711 member_vec_append_class_fields (member_vec
, klass
);
1716 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1717 member_vec
->qsort (member_name_cmp
);
1718 member_vec_dedup (member_vec
);
1722 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1725 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1728 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1730 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1731 count them twice. */
1733 n_fields
= count_class_fields (klass
);
1735 n_fields
= list_length (TYPE_VALUES (enumtype
));
1737 if (member_vec
|| n_fields
>= 8)
1739 vec_safe_reserve_exact (member_vec
, n_fields
);
1740 if (CLASSTYPE_MEMBER_VEC (klass
))
1741 member_vec_append_enum_values (member_vec
, enumtype
);
1743 member_vec_append_class_fields (member_vec
, klass
);
1744 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1745 member_vec
->qsort (member_name_cmp
);
1746 member_vec_dedup (member_vec
);
1750 /* Compute the chain index of a binding_entry given the HASH value of its
1751 name and the total COUNT of chains. COUNT is assumed to be a power
1754 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1756 /* A free list of "binding_entry"s awaiting for re-use. */
1758 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1760 /* The binding oracle; see cp-tree.h. */
1762 cp_binding_oracle_function
*cp_binding_oracle
;
1764 /* If we have a binding oracle, ask it for all namespace-scoped
1765 definitions of NAME. */
1768 query_oracle (tree name
)
1770 if (!cp_binding_oracle
)
1773 /* LOOKED_UP holds the set of identifiers that we have already
1774 looked up with the oracle. */
1775 static hash_set
<tree
> looked_up
;
1776 if (looked_up
.add (name
))
1779 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1782 /* Create a binding_entry object for (NAME, TYPE). */
1784 static inline binding_entry
1785 binding_entry_make (tree name
, tree type
)
1787 binding_entry entry
;
1789 if (free_binding_entry
)
1791 entry
= free_binding_entry
;
1792 free_binding_entry
= entry
->chain
;
1795 entry
= ggc_alloc
<binding_entry_s
> ();
1799 entry
->chain
= NULL
;
1804 /* Put ENTRY back on the free list. */
1807 binding_entry_free (binding_entry entry
)
1811 entry
->chain
= free_binding_entry
;
1812 free_binding_entry
= entry
;
1816 /* The datatype used to implement the mapping from names to types at
1818 struct GTY(()) binding_table_s
{
1819 /* Array of chains of "binding_entry"s */
1820 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1822 /* The number of chains in this table. This is the length of the
1823 member "chain" considered as an array. */
1826 /* Number of "binding_entry"s in this table. */
1830 /* Construct TABLE with an initial CHAIN_COUNT. */
1833 binding_table_construct (binding_table table
, size_t chain_count
)
1835 table
->chain_count
= chain_count
;
1836 table
->entry_count
= 0;
1837 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1840 /* Make TABLE's entries ready for reuse. */
1843 binding_table_free (binding_table table
)
1851 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1853 binding_entry temp
= table
->chain
[i
];
1854 while (temp
!= NULL
)
1856 binding_entry entry
= temp
;
1857 temp
= entry
->chain
;
1858 binding_entry_free (entry
);
1860 table
->chain
[i
] = NULL
;
1862 table
->entry_count
= 0;
1866 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1868 static inline binding_table
1869 binding_table_new (size_t chain_count
)
1871 binding_table table
= ggc_alloc
<binding_table_s
> ();
1872 table
->chain
= NULL
;
1873 binding_table_construct (table
, chain_count
);
1877 /* Expand TABLE to twice its current chain_count. */
1880 binding_table_expand (binding_table table
)
1882 const size_t old_chain_count
= table
->chain_count
;
1883 const size_t old_entry_count
= table
->entry_count
;
1884 const size_t new_chain_count
= 2 * old_chain_count
;
1885 binding_entry
*old_chains
= table
->chain
;
1888 binding_table_construct (table
, new_chain_count
);
1889 for (i
= 0; i
< old_chain_count
; ++i
)
1891 binding_entry entry
= old_chains
[i
];
1892 for (; entry
!= NULL
; entry
= old_chains
[i
])
1894 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1895 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1897 old_chains
[i
] = entry
->chain
;
1898 entry
->chain
= table
->chain
[j
];
1899 table
->chain
[j
] = entry
;
1902 table
->entry_count
= old_entry_count
;
1905 /* Insert a binding for NAME to TYPE into TABLE. */
1908 binding_table_insert (binding_table table
, tree name
, tree type
)
1910 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1911 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1912 binding_entry entry
= binding_entry_make (name
, type
);
1914 entry
->chain
= table
->chain
[i
];
1915 table
->chain
[i
] = entry
;
1916 ++table
->entry_count
;
1918 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1919 binding_table_expand (table
);
1922 /* Return the binding_entry, if any, that maps NAME. */
1925 binding_table_find (binding_table table
, tree name
)
1927 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1928 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1930 while (entry
!= NULL
&& entry
->name
!= name
)
1931 entry
= entry
->chain
;
1936 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1939 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1947 chain_count
= table
->chain_count
;
1948 for (i
= 0; i
< chain_count
; ++i
)
1950 binding_entry entry
= table
->chain
[i
];
1951 for (; entry
!= NULL
; entry
= entry
->chain
)
1956 #ifndef ENABLE_SCOPE_CHECKING
1957 # define ENABLE_SCOPE_CHECKING 0
1959 # define ENABLE_SCOPE_CHECKING 1
1962 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1964 static GTY((deletable
)) cxx_binding
*free_bindings
;
1966 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1970 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1972 binding
->value
= value
;
1973 binding
->type
= type
;
1974 binding
->previous
= NULL
;
1977 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1979 static cxx_binding
*
1980 cxx_binding_make (tree value
, tree type
)
1982 cxx_binding
*binding
;
1985 binding
= free_bindings
;
1986 free_bindings
= binding
->previous
;
1989 binding
= ggc_alloc
<cxx_binding
> ();
1991 cxx_binding_init (binding
, value
, type
);
1996 /* Put BINDING back on the free list. */
1999 cxx_binding_free (cxx_binding
*binding
)
2001 binding
->scope
= NULL
;
2002 binding
->previous
= free_bindings
;
2003 free_bindings
= binding
;
2006 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2007 bindings) in the class scope indicated by SCOPE. */
2009 static cxx_binding
*
2010 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
2012 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
2013 cxx_binding
*binding
= cb
.base
;
2014 vec_safe_push (scope
->class_shadowed
, cb
);
2015 binding
->scope
= scope
;
2019 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2020 level at which this declaration is being bound. */
2023 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2025 cxx_binding
*binding
;
2027 if (level
!= class_binding_level
)
2029 binding
= cxx_binding_make (decl
, NULL_TREE
);
2030 binding
->scope
= level
;
2033 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2035 /* Now, fill in the binding information. */
2036 binding
->previous
= IDENTIFIER_BINDING (id
);
2037 INHERITED_VALUE_BINDING_P (binding
) = 0;
2038 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2040 /* And put it on the front of the list of bindings for ID. */
2041 IDENTIFIER_BINDING (id
) = binding
;
2044 /* Remove the binding for DECL which should be the innermost binding
2048 pop_local_binding (tree id
, tree decl
)
2050 cxx_binding
*binding
;
2052 if (id
== NULL_TREE
)
2053 /* It's easiest to write the loops that call this function without
2054 checking whether or not the entities involved have names. We
2055 get here for such an entity. */
2058 /* Get the innermost binding for ID. */
2059 binding
= IDENTIFIER_BINDING (id
);
2061 /* The name should be bound. */
2062 gcc_assert (binding
!= NULL
);
2064 /* The DECL will be either the ordinary binding or the type
2065 binding for this identifier. Remove that binding. */
2066 if (binding
->value
== decl
)
2067 binding
->value
= NULL_TREE
;
2070 gcc_assert (binding
->type
== decl
);
2071 binding
->type
= NULL_TREE
;
2074 if (!binding
->value
&& !binding
->type
)
2076 /* We're completely done with the innermost binding for this
2077 identifier. Unhook it from the list of bindings. */
2078 IDENTIFIER_BINDING (id
) = binding
->previous
;
2080 /* Add it to the free list. */
2081 cxx_binding_free (binding
);
2085 /* Remove the bindings for the decls of the current level and leave
2086 the current scope. */
2089 pop_bindings_and_leave_scope (void)
2091 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2093 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2094 tree name
= OVL_NAME (decl
);
2096 pop_local_binding (name
, decl
);
2102 /* Strip non dependent using declarations. If DECL is dependent,
2103 surreptitiously create a typename_type and return it. */
2106 strip_using_decl (tree decl
)
2108 if (decl
== NULL_TREE
)
2111 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2112 decl
= USING_DECL_DECLS (decl
);
2114 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2115 && USING_DECL_TYPENAME_P (decl
))
2117 /* We have found a type introduced by a using
2118 declaration at class scope that refers to a dependent
2121 using typename :: [opt] nested-name-specifier unqualified-id ;
2123 decl
= make_typename_type (TREE_TYPE (decl
),
2125 typename_type
, tf_error
);
2126 if (decl
!= error_mark_node
)
2127 decl
= TYPE_NAME (decl
);
2133 /* Return true if OVL is an overload for an anticipated builtin. */
2136 anticipated_builtin_p (tree ovl
)
2138 if (TREE_CODE (ovl
) != OVERLOAD
)
2141 if (!OVL_HIDDEN_P (ovl
))
2144 tree fn
= OVL_FUNCTION (ovl
);
2145 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2147 if (DECL_HIDDEN_FRIEND_P (fn
))
2153 /* BINDING records an existing declaration for a name in the current scope.
2154 But, DECL is another declaration for that same identifier in the
2155 same scope. This is the `struct stat' hack whereby a non-typedef
2156 class name or enum-name can be bound at the same level as some other
2160 A class name (9.1) or enumeration name (7.2) can be hidden by the
2161 name of an object, function, or enumerator declared in the same scope.
2162 If a class or enumeration name and an object, function, or enumerator
2163 are declared in the same scope (in any order) with the same name, the
2164 class or enumeration name is hidden wherever the object, function, or
2165 enumerator name is visible.
2167 It's the responsibility of the caller to check that
2168 inserting this name is valid here. Returns nonzero if the new binding
2172 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2174 tree bval
= binding
->value
;
2176 tree target_bval
= strip_using_decl (bval
);
2177 tree target_decl
= strip_using_decl (decl
);
2179 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2180 && target_decl
!= target_bval
2181 && (TREE_CODE (target_bval
) != TYPE_DECL
2182 /* We allow pushing an enum multiple times in a class
2183 template in order to handle late matching of underlying
2184 type on an opaque-enum-declaration followed by an
2186 || (processing_template_decl
2187 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2188 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2189 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2190 (TREE_TYPE (target_decl
)))
2191 || dependent_type_p (ENUM_UNDERLYING_TYPE
2192 (TREE_TYPE (target_bval
)))))))
2193 /* The new name is the type name. */
2194 binding
->type
= decl
;
2195 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2196 an inherited type-binding out of the way to make room
2197 for a new value binding. */
2199 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2200 has been used in a non-class scope prior declaration.
2201 In that case, we should have already issued a
2202 diagnostic; for graceful error recovery purpose, pretend
2203 this was the intended declaration for that name. */
2204 || target_bval
== error_mark_node
2205 /* If TARGET_BVAL is anticipated but has not yet been
2206 declared, pretend it is not there at all. */
2207 || anticipated_builtin_p (target_bval
))
2208 binding
->value
= decl
;
2209 else if (TREE_CODE (target_bval
) == TYPE_DECL
2210 && DECL_ARTIFICIAL (target_bval
)
2211 && target_decl
!= target_bval
2212 && (TREE_CODE (target_decl
) != TYPE_DECL
2213 || same_type_p (TREE_TYPE (target_decl
),
2214 TREE_TYPE (target_bval
))))
2216 /* The old binding was a type name. It was placed in
2217 VALUE field because it was thought, at the point it was
2218 declared, to be the only entity with such a name. Move the
2219 type name into the type slot; it is now hidden by the new
2221 binding
->type
= bval
;
2222 binding
->value
= decl
;
2223 binding
->value_is_inherited
= false;
2225 else if (TREE_CODE (target_bval
) == TYPE_DECL
2226 && TREE_CODE (target_decl
) == TYPE_DECL
2227 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2228 && binding
->scope
->kind
!= sk_class
2229 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2230 /* If either type involves template parameters, we must
2231 wait until instantiation. */
2232 || uses_template_parms (TREE_TYPE (target_decl
))
2233 || uses_template_parms (TREE_TYPE (target_bval
))))
2234 /* We have two typedef-names, both naming the same type to have
2235 the same name. In general, this is OK because of:
2239 In a given scope, a typedef specifier can be used to redefine
2240 the name of any type declared in that scope to refer to the
2241 type to which it already refers.
2243 However, in class scopes, this rule does not apply due to the
2244 stricter language in [class.mem] prohibiting redeclarations of
2247 /* There can be two block-scope declarations of the same variable,
2248 so long as they are `extern' declarations. However, there cannot
2249 be two declarations of the same static data member:
2253 A member shall not be declared twice in the
2254 member-specification. */
2255 else if (VAR_P (target_decl
)
2256 && VAR_P (target_bval
)
2257 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2258 && !DECL_CLASS_SCOPE_P (target_decl
))
2260 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2263 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2264 && TREE_CODE (bval
) == NAMESPACE_DECL
2265 && DECL_NAMESPACE_ALIAS (decl
)
2266 && DECL_NAMESPACE_ALIAS (bval
)
2267 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2268 /* [namespace.alias]
2270 In a declarative region, a namespace-alias-definition can be
2271 used to redefine a namespace-alias declared in that declarative
2272 region to refer only to the namespace to which it already
2277 if (!error_operand_p (bval
))
2278 diagnose_name_conflict (decl
, bval
);
2285 /* Diagnose a name conflict between DECL and BVAL. */
2288 diagnose_name_conflict (tree decl
, tree bval
)
2290 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2291 && TREE_CODE (decl
) != NAMESPACE_DECL
2292 && !DECL_DECLARES_FUNCTION_P (decl
)
2293 && (TREE_CODE (decl
) != TYPE_DECL
2294 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2295 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2296 error ("redeclaration of %q#D", decl
);
2298 error ("%q#D conflicts with a previous declaration", decl
);
2300 inform (location_of (bval
), "previous declaration %q#D", bval
);
2303 /* Wrapper for supplement_binding_1. */
2306 supplement_binding (cxx_binding
*binding
, tree decl
)
2309 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2310 ret
= supplement_binding_1 (binding
, decl
);
2311 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2315 /* Replace BINDING's current value on its scope's name list with
2319 update_local_overload (cxx_binding
*binding
, tree newval
)
2323 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2324 if (*d
== binding
->value
)
2326 /* Stitch new list node in. */
2327 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2330 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2333 TREE_VALUE (*d
) = newval
;
2336 /* Compares the parameter-type-lists of ONE and TWO and
2337 returns false if they are different. If the DECLs are template
2338 functions, the return types and the template parameter lists are
2339 compared too (DR 565). */
2342 matching_fn_p (tree one
, tree two
)
2344 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2345 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2348 if (TREE_CODE (one
) == TEMPLATE_DECL
2349 && TREE_CODE (two
) == TEMPLATE_DECL
)
2351 /* Compare template parms. */
2352 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2353 DECL_TEMPLATE_PARMS (two
)))
2356 /* And return type. */
2357 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2358 TREE_TYPE (TREE_TYPE (two
))))
2365 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2366 binding value (possibly with anticipated builtins stripped).
2367 Diagnose conflicts and return updated decl. */
2370 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2371 tree old
, tree decl
, bool is_friend
)
2374 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2375 tree to_type
= old_type
;
2377 gcc_assert (level
->kind
== sk_namespace
? !binding
2378 : level
->kind
!= sk_class
&& !slot
);
2379 if (old
== error_mark_node
)
2382 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2384 tree other
= to_type
;
2386 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2389 /* Pushing an artificial typedef. See if this matches either
2390 the type slot or the old value slot. */
2393 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2394 /* Two artificial decls to same type. Do nothing. */
2401 /* Slide decl into the type slot, keep old unaltered */
2408 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2410 /* Slide old into the type slot. */
2415 if (DECL_DECLARES_FUNCTION_P (decl
))
2419 else if (OVL_P (old
))
2421 for (ovl_iterator
iter (old
); iter
; ++iter
)
2425 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2427 /* If a function declaration in namespace scope or
2428 block scope has the same name and the same
2429 parameter-type- list (8.3.5) as a function
2430 introduced by a using-declaration, and the
2431 declarations do not declare the same function,
2432 the program is ill-formed. [namespace.udecl]/14 */
2433 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2436 /* FIXME: To preserve existing error behavior, we
2437 still push the decl. This might change. */
2438 diagnose_name_conflict (decl
, fn
);
2445 if (to_type
!= old_type
2447 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2448 && !(DECL_IN_SYSTEM_HEADER (decl
)
2449 && DECL_IN_SYSTEM_HEADER (to_type
)))
2450 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2453 to_val
= ovl_insert (decl
, old
);
2457 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2458 /* Different kinds of decls conflict. */
2460 else if (TREE_CODE (old
) == TYPE_DECL
)
2462 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2463 /* Two type decls to the same type. Do nothing. */
2468 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2470 /* Two maybe-aliased namespaces. If they're to the same target
2471 namespace, that's ok. */
2472 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2475 /* The new one must be an alias at this point. */
2476 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2479 else if (TREE_CODE (old
) == VAR_DECL
)
2481 /* There can be two block-scope declarations of the same
2482 variable, so long as they are `extern' declarations. */
2483 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2485 else if (tree match
= duplicate_decls (decl
, old
, false))
2493 diagnose_name_conflict (decl
, old
);
2500 if (level
->kind
!= sk_namespace
2501 && !to_type
&& binding
->value
&& OVL_P (to_val
))
2502 update_local_overload (binding
, to_val
);
2505 tree to_add
= to_val
;
2507 if (level
->kind
== sk_namespace
)
2509 else if (to_type
== decl
)
2511 else if (TREE_CODE (to_add
) == OVERLOAD
)
2512 to_add
= build_tree_list (NULL_TREE
, to_add
);
2514 add_decl_to_level (level
, to_add
);
2519 if (STAT_HACK_P (*slot
))
2521 STAT_TYPE (*slot
) = to_type
;
2522 STAT_DECL (*slot
) = to_val
;
2525 *slot
= stat_hack (to_val
, to_type
);
2531 binding
->type
= to_type
;
2532 binding
->value
= to_val
;
2539 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2541 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2543 /* DECL has C linkage. If we have an existing instance, make sure the
2544 new one is compatible. Make sure it has the same exception
2545 specification [7.5, 7.6]. Add DECL to the map. */
2548 check_extern_c_conflict (tree decl
)
2550 /* Ignore artificial or system header decls. */
2551 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2554 if (!extern_c_decls
)
2555 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2557 tree
*slot
= extern_c_decls
2558 ->find_slot_with_hash (DECL_NAME (decl
),
2559 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2560 if (tree old
= *slot
)
2562 if (TREE_CODE (old
) == OVERLOAD
)
2563 old
= OVL_FUNCTION (old
);
2566 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2567 ; /* If they're in the same context, we'll have already complained
2568 about a (possible) mismatch, when inserting the decl. */
2569 else if (!decls_match (decl
, old
))
2571 else if (TREE_CODE (decl
) == FUNCTION_DECL
2572 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2573 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2576 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2577 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2581 pedwarn (input_location
, 0,
2582 "conflicting C language linkage declaration %q#D", decl
);
2583 inform (DECL_SOURCE_LOCATION (old
),
2584 "previous declaration %q#D", old
);
2586 inform (input_location
,
2587 "due to different exception specifications");
2592 /* The hash table expects OVERLOADS, so construct one with
2593 OLD as both the function and the chain. This allocate
2594 an excess OVERLOAD node, but it's rare to have multiple
2595 extern "C" decls of the same name. And we save
2596 complicating the hash table logic (which is used
2598 *slot
= ovl_make (old
, old
);
2600 slot
= &OVL_CHAIN (*slot
);
2602 /* Chain it on for c_linkage_binding's use. */
2603 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2610 /* Returns a list of C-linkage decls with the name NAME. Used in
2611 c-family/c-pragma.c to implement redefine_extname pragma. */
2614 c_linkage_bindings (tree name
)
2617 if (tree
*slot
= extern_c_decls
2618 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2620 tree result
= *slot
;
2621 if (TREE_CODE (result
) == OVERLOAD
)
2622 result
= OVL_CHAIN (result
);
2629 /* DECL is being declared at a local scope. Emit suitable shadow
2633 check_local_shadow (tree decl
)
2635 /* Don't complain about the parms we push and then pop
2636 while tentatively parsing a function declarator. */
2637 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2640 /* Inline decls shadow nothing. */
2641 if (DECL_FROM_INLINE (decl
))
2644 /* External decls are something else. */
2645 if (DECL_EXTERNAL (decl
))
2648 tree old
= NULL_TREE
;
2649 cp_binding_level
*old_scope
= NULL
;
2650 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2652 old
= binding
->value
;
2653 old_scope
= binding
->scope
;
2655 while (old
&& VAR_P (old
) && DECL_DEAD_FOR_LOCAL (old
))
2656 old
= DECL_SHADOWED_FOR_VAR (old
);
2658 tree shadowed
= NULL_TREE
;
2660 && (TREE_CODE (old
) == PARM_DECL
2662 || (TREE_CODE (old
) == TYPE_DECL
2663 && (!DECL_ARTIFICIAL (old
)
2664 || TREE_CODE (decl
) == TYPE_DECL
)))
2665 && (!DECL_ARTIFICIAL (decl
)
2666 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2667 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2669 /* DECL shadows a local thing possibly of interest. */
2671 /* Don't complain if it's from an enclosing function. */
2672 if (DECL_CONTEXT (old
) == current_function_decl
2673 && TREE_CODE (decl
) != PARM_DECL
2674 && TREE_CODE (old
) == PARM_DECL
)
2676 /* Go to where the parms should be and see if we find
2678 cp_binding_level
*b
= current_binding_level
->level_chain
;
2680 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2681 /* Skip the ctor/dtor cleanup level. */
2685 if (b
->kind
== sk_function_parms
)
2687 error ("declaration of %q#D shadows a parameter", decl
);
2692 /* The local structure or class can't use parameters of
2693 the containing function anyway. */
2694 if (DECL_CONTEXT (old
) != current_function_decl
)
2696 for (cp_binding_level
*scope
= current_binding_level
;
2697 scope
!= old_scope
; scope
= scope
->level_chain
)
2698 if (scope
->kind
== sk_class
2699 && !LAMBDA_TYPE_P (scope
->this_entity
))
2702 /* Error if redeclaring a local declared in a
2703 init-statement or in the condition of an if or
2704 switch statement when the new declaration is in the
2705 outermost block of the controlled statement.
2706 Redeclaring a variable from a for or while condition is
2707 detected elsewhere. */
2708 else if (VAR_P (old
)
2709 && old_scope
== current_binding_level
->level_chain
2710 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2712 error ("redeclaration of %q#D", decl
);
2713 inform (DECL_SOURCE_LOCATION (old
),
2714 "%q#D previously declared here", old
);
2718 3.3.3/3: The name declared in an exception-declaration (...)
2719 shall not be redeclared in the outermost block of the handler.
2720 3.3.3/2: A parameter name shall not be redeclared (...) in
2721 the outermost block of any handler associated with a
2723 3.4.1/15: The function parameter names shall not be redeclared
2724 in the exception-declaration nor in the outermost block of a
2725 handler for the function-try-block. */
2726 else if ((TREE_CODE (old
) == VAR_DECL
2727 && old_scope
== current_binding_level
->level_chain
2728 && old_scope
->kind
== sk_catch
)
2729 || (TREE_CODE (old
) == PARM_DECL
2730 && (current_binding_level
->kind
== sk_catch
2731 || current_binding_level
->level_chain
->kind
== sk_catch
)
2732 && in_function_try_handler
))
2734 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2735 inform (DECL_SOURCE_LOCATION (old
),
2736 "%q#D previously declared here", old
);
2740 /* If '-Wshadow=compatible-local' is specified without other
2741 -Wshadow= flags, we will warn only when the type of the
2742 shadowing variable (DECL) can be converted to that of the
2743 shadowed parameter (OLD_LOCAL). The reason why we only check
2744 if DECL's type can be converted to OLD_LOCAL's type (but not the
2745 other way around) is because when users accidentally shadow a
2746 parameter, more than often they would use the variable
2747 thinking (mistakenly) it's still the parameter. It would be
2748 rare that users would use the variable in the place that
2749 expects the parameter but thinking it's a new decl. */
2751 enum opt_code warning_code
;
2753 warning_code
= OPT_Wshadow
;
2754 else if (warn_shadow_local
)
2755 warning_code
= OPT_Wshadow_local
;
2756 else if (warn_shadow_compatible_local
2757 && (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2758 || (!dependent_type_p (TREE_TYPE (decl
))
2759 && !dependent_type_p (TREE_TYPE (old
))
2760 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
),
2762 warning_code
= OPT_Wshadow_compatible_local
;
2767 if (TREE_CODE (old
) == PARM_DECL
)
2768 msg
= "declaration of %q#D shadows a parameter";
2769 else if (is_capture_proxy (old
))
2770 msg
= "declaration of %qD shadows a lambda capture";
2772 msg
= "declaration of %qD shadows a previous local";
2774 if (warning_at (input_location
, warning_code
, msg
, decl
))
2777 goto inform_shadowed
;
2785 /* Don't warn for artificial things that are not implicit typedefs. */
2786 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2789 if (nonlambda_method_basetype ())
2790 if (tree member
= lookup_member (current_nonlambda_class_type (),
2791 DECL_NAME (decl
), /*protect=*/0,
2792 /*want_type=*/false, tf_warning_or_error
))
2794 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2796 /* Warn if a variable shadows a non-function, or the variable
2797 is a function or a pointer-to-function. */
2799 || TREE_CODE (decl
) == FUNCTION_DECL
2800 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2801 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2803 if (warning_at (input_location
, OPT_Wshadow
,
2804 "declaration of %qD shadows a member of %qT",
2805 decl
, current_nonlambda_class_type ())
2809 goto inform_shadowed
;
2815 /* Now look for a namespace shadow. */
2816 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2819 || (TREE_CODE (old
) == TYPE_DECL
2820 && (!DECL_ARTIFICIAL (old
)
2821 || TREE_CODE (decl
) == TYPE_DECL
)))
2822 && !instantiating_current_function_p ())
2823 /* XXX shadow warnings in outer-more namespaces */
2825 if (warning_at (input_location
, OPT_Wshadow
,
2826 "declaration of %qD shadows a global declaration",
2830 goto inform_shadowed
;
2838 inform (DECL_SOURCE_LOCATION (shadowed
), "shadowed declaration is here");
2841 /* DECL is being pushed inside function CTX. Set its context, if
2845 set_decl_context_in_fn (tree ctx
, tree decl
)
2847 if (!DECL_CONTEXT (decl
)
2848 /* A local declaration for a function doesn't constitute
2850 && TREE_CODE (decl
) != FUNCTION_DECL
2851 /* A local declaration for an `extern' variable is in the
2852 scope of the current namespace, not the current
2854 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2855 /* When parsing the parameter list of a function declarator,
2856 don't set DECL_CONTEXT to an enclosing function. When we
2857 push the PARM_DECLs in order to process the function body,
2858 current_binding_level->this_entity will be set. */
2859 && !(TREE_CODE (decl
) == PARM_DECL
2860 && current_binding_level
->kind
== sk_function_parms
2861 && current_binding_level
->this_entity
== NULL
))
2862 DECL_CONTEXT (decl
) = ctx
;
2864 /* If this is the declaration for a namespace-scope function,
2865 but the declaration itself is in a local scope, mark the
2867 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2868 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2871 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2872 name is already bound at the current level.
2874 [basic.link] If there is a visible declaration of an entity with
2875 linkage having the same name and type, ignoring entities declared
2876 outside the innermost enclosing namespace scope, the block scope
2877 declaration declares that same entity and receives the linkage of
2878 the previous declaration.
2880 Also, make sure that this decl matches any existing external decl
2881 in the enclosing namespace. */
2884 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2886 tree ns_value
= decl
; /* Unique marker. */
2890 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2894 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2895 loc_value
= ns_value
;
2897 if (loc_value
== error_mark_node
)
2898 loc_value
= NULL_TREE
;
2900 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2901 if (!iter
.hidden_p ()
2902 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2903 && decls_match (*iter
, decl
))
2905 /* The standard only says that the local extern inherits
2906 linkage from the previous decl; in particular, default
2907 args are not shared. Add the decl into a hash table to
2908 make sure only the previous decl in this case is seen
2909 by the middle end. */
2910 struct cxx_int_tree_map
*h
;
2912 /* We inherit the outer decl's linkage. But we're a
2914 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2916 if (cp_function_chain
->extern_decl_map
== NULL
)
2917 cp_function_chain
->extern_decl_map
2918 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2920 h
= ggc_alloc
<cxx_int_tree_map
> ();
2921 h
->uid
= DECL_UID (decl
);
2923 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2924 ->find_slot (h
, INSERT
);
2930 if (TREE_PUBLIC (decl
))
2932 /* DECL is externally visible. Make sure it matches a matching
2933 decl in the namespace scope. We only really need to check
2934 this when inserting the decl, not when we find an existing
2935 match in the current scope. However, in practice we're
2936 going to be inserting a new decl in the majority of cases --
2937 who writes multiple extern decls for the same thing in the
2938 same local scope? Doing it here often avoids a duplicate
2939 namespace lookup. */
2941 /* Avoid repeating a lookup. */
2942 if (ns_value
== decl
)
2943 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2945 if (ns_value
== error_mark_node
)
2946 ns_value
= NULL_TREE
;
2948 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2952 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2953 ; /* Not externally visible. */
2954 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2955 ; /* Both are extern "C", we'll check via that mechanism. */
2956 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2957 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2958 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2959 COMPARE_REDECLARATION
)))
2961 if (permerror (DECL_SOURCE_LOCATION (decl
),
2962 "local external declaration %q#D", decl
))
2963 inform (DECL_SOURCE_LOCATION (other
),
2964 "does not match previous declaration %q#D", other
);
2971 /* Record DECL as belonging to the current lexical scope. Check for
2972 errors (such as an incompatible declaration for the same name
2973 already seen in the same scope). IS_FRIEND is true if DECL is
2974 declared as a friend.
2976 Returns either DECL or an old decl for the same name. If an old
2977 decl is returned, it may have been smashed to agree with what DECL
2981 do_pushdecl (tree decl
, bool is_friend
)
2983 if (decl
== error_mark_node
)
2984 return error_mark_node
;
2986 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2987 set_decl_context_in_fn (current_function_decl
, decl
);
2989 /* The binding level we will be pushing into. During local class
2990 pushing, we want to push to the containing scope. */
2991 cp_binding_level
*level
= current_binding_level
;
2992 while (level
->kind
== sk_class
)
2993 level
= level
->level_chain
;
2995 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
2996 insert it. Other NULL-named decls, not so much. */
2997 tree name
= DECL_NAME (decl
);
2998 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
3000 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3001 tree ns
= NULL_TREE
; /* Searched namespace. */
3002 tree
*slot
= NULL
; /* Binding slot in namespace. */
3003 tree old
= NULL_TREE
;
3005 if (level
->kind
== sk_namespace
)
3007 /* We look in the decl's namespace for an existing
3008 declaration, even though we push into the current
3010 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3011 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3012 /* Create the binding, if this is current namespace, because
3013 that's where we'll be pushing anyway. */
3014 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3016 old
= MAYBE_STAT_DECL (*slot
);
3020 binding
= find_local_binding (level
, name
);
3022 old
= binding
->value
;
3025 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3026 && DECL_EXTERNAL (decl
))
3027 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3029 if (old
== error_mark_node
)
3032 for (ovl_iterator
iter (old
); iter
; ++iter
)
3033 if (iter
.using_p ())
3034 ; /* Ignore using decls here. */
3035 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3037 if (match
== error_mark_node
)
3039 else if (TREE_CODE (match
) == TYPE_DECL
)
3040 /* The IDENTIFIER will have the type referring to the
3041 now-smashed TYPE_DECL, because ...? Reset it. */
3042 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3043 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3045 /* Unhiding a previously hidden decl. */
3046 tree head
= iter
.reveal_node (old
);
3051 update_local_overload (binding
, head
);
3052 binding
->value
= head
;
3054 else if (STAT_HACK_P (*slot
))
3055 STAT_DECL (*slot
) = head
;
3059 if (DECL_EXTERN_C_P (match
))
3060 /* We need to check and register the decl now. */
3061 check_extern_c_conflict (match
);
3066 /* We are pushing a new decl. */
3068 /* Skip a hidden builtin we failed to match already. There can
3070 if (old
&& anticipated_builtin_p (old
))
3071 old
= OVL_CHAIN (old
);
3073 check_template_shadow (decl
);
3074 bool visible_injection
= false;
3076 if (DECL_DECLARES_FUNCTION_P (decl
))
3078 check_default_args (decl
);
3082 if (level
->kind
!= sk_namespace
)
3084 /* In a local class, a friend function declaration must
3085 find a matching decl in the innermost non-class scope.
3086 [class.friend/11] */
3087 error ("friend declaration %qD in local class without "
3088 "prior local declaration", decl
);
3089 /* Don't attempt to push it. */
3090 return error_mark_node
;
3092 if (!flag_friend_injection
)
3093 /* Hide it from ordinary lookup. */
3094 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3096 visible_injection
= true;
3100 if (level
->kind
!= sk_namespace
)
3102 check_local_shadow (decl
);
3104 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3105 /* A local namespace alias. */
3106 set_identifier_type_value (name
, NULL_TREE
);
3109 binding
= create_local_binding (level
, name
);
3113 ns
= current_namespace
;
3114 slot
= find_namespace_slot (ns
, name
, true);
3115 /* Update OLD to reflect the namespace we're going to be
3117 old
= MAYBE_STAT_DECL (*slot
);
3120 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3123 /* An existing decl matched, use it. */
3125 else if (TREE_CODE (decl
) == TYPE_DECL
)
3127 tree type
= TREE_TYPE (decl
);
3129 if (type
!= error_mark_node
)
3131 if (TYPE_NAME (type
) != decl
)
3132 set_underlying_type (decl
);
3135 set_identifier_type_value_with_scope (name
, decl
, level
);
3137 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3140 /* If this is a locally defined typedef in a function that
3141 is not a template instantation, record it to implement
3142 -Wunused-local-typedefs. */
3143 if (!instantiating_current_function_p ())
3144 record_locally_defined_typedef (decl
);
3146 else if (VAR_P (decl
))
3147 maybe_register_incomplete_var (decl
);
3148 else if (visible_injection
)
3149 warning (0, "injected friend %qD is visible"
3150 " due to %<-ffriend-injection%>", decl
);
3152 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3153 && DECL_EXTERN_C_P (decl
))
3154 check_extern_c_conflict (decl
);
3157 add_decl_to_level (level
, decl
);
3162 /* Record a decl-node X as belonging to the current lexical scope.
3163 It's a friend if IS_FRIEND is true -- which affects exactly where
3167 pushdecl (tree x
, bool is_friend
)
3169 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3170 tree ret
= do_pushdecl (x
, is_friend
);
3171 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3175 /* Enter DECL into the symbol table, if that's appropriate. Returns
3176 DECL, or a modified version thereof. */
3179 maybe_push_decl (tree decl
)
3181 tree type
= TREE_TYPE (decl
);
3183 /* Add this decl to the current binding level, but not if it comes
3184 from another scope, e.g. a static member variable. TEM may equal
3185 DECL or it may be a previous decl of the same name. */
3186 if (decl
== error_mark_node
3187 || (TREE_CODE (decl
) != PARM_DECL
3188 && DECL_CONTEXT (decl
) != NULL_TREE
3189 /* Definitions of namespace members outside their namespace are
3191 && !DECL_NAMESPACE_SCOPE_P (decl
))
3192 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3193 || type
== unknown_type_node
3194 /* The declaration of a template specialization does not affect
3195 the functions available for overload resolution, so we do not
3197 || (TREE_CODE (decl
) == FUNCTION_DECL
3198 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3201 return pushdecl (decl
);
3204 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3205 binding level. If IS_USING is true, DECL got here through a
3206 using-declaration. */
3209 push_local_binding (tree id
, tree decl
, bool is_using
)
3211 /* Skip over any local classes. This makes sense if we call
3212 push_local_binding with a friend decl of a local class. */
3213 cp_binding_level
*b
= innermost_nonclass_level ();
3215 gcc_assert (b
->kind
!= sk_namespace
);
3216 if (find_local_binding (b
, id
))
3218 /* Supplement the existing binding. */
3219 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3220 /* It didn't work. Something else must be bound at this
3221 level. Do not add DECL to the list of things to pop
3226 /* Create a new binding. */
3227 push_binding (id
, decl
, b
);
3229 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3230 /* We must put the OVERLOAD or using into a TREE_LIST since we
3231 cannot use the decl's chain itself. */
3232 decl
= build_tree_list (NULL_TREE
, decl
);
3234 /* And put DECL on the list of things declared by the current
3236 add_decl_to_level (b
, decl
);
3239 /* Check to see whether or not DECL is a variable that would have been
3240 in scope under the ARM, but is not in scope under the ANSI/ISO
3241 standard. If so, issue an error message. If name lookup would
3242 work in both cases, but return a different result, this function
3243 returns the result of ANSI/ISO lookup. Otherwise, it returns
3246 FIXME: Scheduled for removal after GCC-8 is done. */
3249 check_for_out_of_scope_variable (tree decl
)
3253 /* We only care about out of scope variables. */
3254 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
3257 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
3258 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
3259 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
3260 && DECL_DEAD_FOR_LOCAL (shadowed
))
3261 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
3262 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
3264 shadowed
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3267 if (!DECL_ERROR_REPORTED (decl
)
3269 && warning (0, "name lookup of %qD changed", DECL_NAME (decl
)))
3271 inform (DECL_SOURCE_LOCATION (shadowed
),
3272 "matches this %qD under ISO standard rules", shadowed
);
3273 inform (DECL_SOURCE_LOCATION (decl
),
3274 " matches this %qD under old rules", decl
);
3276 DECL_ERROR_REPORTED (decl
) = 1;
3280 /* If we have already complained about this declaration, there's no
3281 need to do it again. */
3282 if (DECL_ERROR_REPORTED (decl
))
3285 DECL_ERROR_REPORTED (decl
) = 1;
3287 if (TREE_TYPE (decl
) == error_mark_node
)
3290 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
3292 error ("name lookup of %qD changed for ISO %<for%> scoping",
3294 inform (DECL_SOURCE_LOCATION (decl
),
3295 "cannot use obsolete binding %qD because it has a destructor",
3297 return error_mark_node
;
3301 permerror (input_location
,
3302 "name lookup of %qD changed for ISO %<for%> scoping",
3304 if (flag_permissive
)
3305 inform (DECL_SOURCE_LOCATION (decl
),
3306 "using obsolete binding %qD", decl
);
3309 inform (input_location
, flag_permissive
3310 ? "this flexibility is deprecated and will be removed"
3311 : "if you use %<-fpermissive%> G++ will accept your code");
3318 /* true means unconditionally make a BLOCK for the next level pushed. */
3320 static bool keep_next_level_flag
;
3322 static int binding_depth
= 0;
3329 for (i
= 0; i
< depth
* 2; i
++)
3333 /* Return a string describing the kind of SCOPE we have. */
3335 cp_binding_level_descriptor (cp_binding_level
*scope
)
3337 /* The order of this table must match the "scope_kind"
3339 static const char* scope_kind_names
[] = {
3345 "function-parameter-scope",
3348 "template-parameter-scope",
3349 "template-explicit-spec-scope"
3351 const scope_kind kind
= scope
->explicit_spec_p
3352 ? sk_template_spec
: scope
->kind
;
3354 return scope_kind_names
[kind
];
3357 /* Output a debugging information about SCOPE when performing
3360 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3362 const char *desc
= cp_binding_level_descriptor (scope
);
3363 if (scope
->this_entity
)
3364 verbatim ("%s %<%s(%E)%> %p %d\n", action
, desc
,
3365 scope
->this_entity
, (void *) scope
, line
);
3367 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
3370 /* Return the estimated initial size of the hashtable of a NAMESPACE
3373 static inline size_t
3374 namespace_scope_ht_size (tree ns
)
3376 tree name
= DECL_NAME (ns
);
3378 return name
== std_identifier
3379 ? NAMESPACE_STD_HT_SIZE
3380 : (name
== global_identifier
3381 ? GLOBAL_SCOPE_HT_SIZE
3382 : NAMESPACE_ORDINARY_HT_SIZE
);
3385 /* A chain of binding_level structures awaiting reuse. */
3387 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3389 /* Insert SCOPE as the innermost binding level. */
3392 push_binding_level (cp_binding_level
*scope
)
3394 /* Add it to the front of currently active scopes stack. */
3395 scope
->level_chain
= current_binding_level
;
3396 current_binding_level
= scope
;
3397 keep_next_level_flag
= false;
3399 if (ENABLE_SCOPE_CHECKING
)
3401 scope
->binding_depth
= binding_depth
;
3402 indent (binding_depth
);
3403 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3409 /* Create a new KIND scope and make it the top of the active scopes stack.
3410 ENTITY is the scope of the associated C++ entity (namespace, class,
3411 function, C++0x enumeration); it is NULL otherwise. */
3414 begin_scope (scope_kind kind
, tree entity
)
3416 cp_binding_level
*scope
;
3418 /* Reuse or create a struct for this binding level. */
3419 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3421 scope
= free_binding_level
;
3422 free_binding_level
= scope
->level_chain
;
3423 memset (scope
, 0, sizeof (cp_binding_level
));
3426 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3428 scope
->this_entity
= entity
;
3429 scope
->more_cleanups_ok
= true;
3436 case sk_template_spec
:
3437 scope
->explicit_spec_p
= true;
3438 kind
= sk_template_parms
;
3440 case sk_template_parms
:
3447 case sk_scoped_enum
:
3448 case sk_function_parms
:
3449 case sk_transaction
:
3451 scope
->keep
= keep_next_level_flag
;
3455 NAMESPACE_LEVEL (entity
) = scope
;
3459 /* Should not happen. */
3465 push_binding_level (scope
);
3470 /* We're about to leave current scope. Pop the top of the stack of
3471 currently active scopes. Return the enclosing scope, now active. */
3476 cp_binding_level
*scope
= current_binding_level
;
3478 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3479 current_binding_level
= class_binding_level
;
3481 /* We cannot leave a scope, if there are none left. */
3482 if (NAMESPACE_LEVEL (global_namespace
))
3483 gcc_assert (!global_scope_p (scope
));
3485 if (ENABLE_SCOPE_CHECKING
)
3487 indent (--binding_depth
);
3488 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3492 /* Move one nesting level up. */
3493 current_binding_level
= scope
->level_chain
;
3495 /* Namespace-scopes are left most probably temporarily, not
3496 completely; they can be reopened later, e.g. in namespace-extension
3497 or any name binding activity that requires us to resume a
3498 namespace. For classes, we cache some binding levels. For other
3499 scopes, we just make the structure available for reuse. */
3500 if (scope
->kind
!= sk_namespace
3501 && scope
->kind
!= sk_class
)
3503 scope
->level_chain
= free_binding_level
;
3504 gcc_assert (!ENABLE_SCOPE_CHECKING
3505 || scope
->binding_depth
== binding_depth
);
3506 free_binding_level
= scope
;
3509 if (scope
->kind
== sk_class
)
3511 /* Reset DEFINING_CLASS_P to allow for reuse of a
3512 class-defining scope in a non-defining context. */
3513 scope
->defining_class_p
= 0;
3515 /* Find the innermost enclosing class scope, and reset
3516 CLASS_BINDING_LEVEL appropriately. */
3517 class_binding_level
= NULL
;
3518 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3519 if (scope
->kind
== sk_class
)
3521 class_binding_level
= scope
;
3526 return current_binding_level
;
3530 resume_scope (cp_binding_level
* b
)
3532 /* Resuming binding levels is meant only for namespaces,
3533 and those cannot nest into classes. */
3534 gcc_assert (!class_binding_level
);
3535 /* Also, resuming a non-directly nested namespace is a no-no. */
3536 gcc_assert (b
->level_chain
== current_binding_level
);
3537 current_binding_level
= b
;
3538 if (ENABLE_SCOPE_CHECKING
)
3540 b
->binding_depth
= binding_depth
;
3541 indent (binding_depth
);
3542 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3547 /* Return the innermost binding level that is not for a class scope. */
3549 static cp_binding_level
*
3550 innermost_nonclass_level (void)
3552 cp_binding_level
*b
;
3554 b
= current_binding_level
;
3555 while (b
->kind
== sk_class
)
3561 /* We're defining an object of type TYPE. If it needs a cleanup, but
3562 we're not allowed to add any more objects with cleanups to the current
3563 scope, create a new binding level. */
3566 maybe_push_cleanup_level (tree type
)
3568 if (type
!= error_mark_node
3569 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3570 && current_binding_level
->more_cleanups_ok
== 0)
3572 begin_scope (sk_cleanup
, NULL
);
3573 current_binding_level
->statement_list
= push_stmt_list ();
3577 /* Return true if we are in the global binding level. */
3580 global_bindings_p (void)
3582 return global_scope_p (current_binding_level
);
3585 /* True if we are currently in a toplevel binding level. This
3586 means either the global binding level or a namespace in a toplevel
3587 binding level. Since there are no non-toplevel namespace levels,
3588 this really means any namespace or template parameter level. We
3589 also include a class whose context is toplevel. */
3592 toplevel_bindings_p (void)
3594 cp_binding_level
*b
= innermost_nonclass_level ();
3596 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3599 /* True if this is a namespace scope, or if we are defining a class
3600 which is itself at namespace scope, or whose enclosing class is
3601 such a class, etc. */
3604 namespace_bindings_p (void)
3606 cp_binding_level
*b
= innermost_nonclass_level ();
3608 return b
->kind
== sk_namespace
;
3611 /* True if the innermost non-class scope is a block scope. */
3614 local_bindings_p (void)
3616 cp_binding_level
*b
= innermost_nonclass_level ();
3617 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3620 /* True if the current level needs to have a BLOCK made. */
3625 return (current_binding_level
->blocks
!= NULL_TREE
3626 || current_binding_level
->keep
3627 || current_binding_level
->kind
== sk_cleanup
3628 || current_binding_level
->names
!= NULL_TREE
3629 || current_binding_level
->using_directives
);
3632 /* Returns the kind of the innermost scope. */
3635 innermost_scope_kind (void)
3637 return current_binding_level
->kind
;
3640 /* Returns true if this scope was created to store template parameters. */
3643 template_parm_scope_p (void)
3645 return innermost_scope_kind () == sk_template_parms
;
3648 /* If KEEP is true, make a BLOCK node for the next binding level,
3649 unconditionally. Otherwise, use the normal logic to decide whether
3650 or not to create a BLOCK. */
3653 keep_next_level (bool keep
)
3655 keep_next_level_flag
= keep
;
3658 /* Return the list of declarations of the current local scope. */
3661 get_local_decls (void)
3663 gcc_assert (current_binding_level
->kind
!= sk_namespace
3664 && current_binding_level
->kind
!= sk_class
);
3665 return current_binding_level
->names
;
3668 /* Return how many function prototypes we are currently nested inside. */
3671 function_parm_depth (void)
3674 cp_binding_level
*b
;
3676 for (b
= current_binding_level
;
3677 b
->kind
== sk_function_parms
;
3684 /* For debugging. */
3685 static int no_print_functions
= 0;
3686 static int no_print_builtins
= 0;
3689 print_binding_level (cp_binding_level
* lvl
)
3693 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3694 if (lvl
->more_cleanups_ok
)
3695 fprintf (stderr
, " more-cleanups-ok");
3696 if (lvl
->have_cleanups
)
3697 fprintf (stderr
, " have-cleanups");
3698 fprintf (stderr
, "\n");
3701 fprintf (stderr
, " names:\t");
3702 /* We can probably fit 3 names to a line? */
3703 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3705 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3707 if (no_print_builtins
3708 && (TREE_CODE (t
) == TYPE_DECL
)
3709 && DECL_IS_BUILTIN (t
))
3712 /* Function decls tend to have longer names. */
3713 if (TREE_CODE (t
) == FUNCTION_DECL
)
3720 fprintf (stderr
, "\n\t");
3723 print_node_brief (stderr
, "", t
, 0);
3724 if (t
== error_mark_node
)
3728 fprintf (stderr
, "\n");
3730 if (vec_safe_length (lvl
->class_shadowed
))
3733 cp_class_binding
*b
;
3734 fprintf (stderr
, " class-shadowed:");
3735 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3736 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3737 fprintf (stderr
, "\n");
3739 if (lvl
->type_shadowed
)
3741 fprintf (stderr
, " type-shadowed:");
3742 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3744 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3746 fprintf (stderr
, "\n");
3751 debug (cp_binding_level
&ref
)
3753 print_binding_level (&ref
);
3757 debug (cp_binding_level
*ptr
)
3762 fprintf (stderr
, "<nil>\n");
3767 print_other_binding_stack (cp_binding_level
*stack
)
3769 cp_binding_level
*level
;
3770 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3772 fprintf (stderr
, "binding level %p\n", (void *) level
);
3773 print_binding_level (level
);
3778 print_binding_stack (void)
3780 cp_binding_level
*b
;
3781 fprintf (stderr
, "current_binding_level=%p\n"
3782 "class_binding_level=%p\n"
3783 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3784 (void *) current_binding_level
, (void *) class_binding_level
,
3785 (void *) NAMESPACE_LEVEL (global_namespace
));
3786 if (class_binding_level
)
3788 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3789 if (b
== current_binding_level
)
3792 b
= class_binding_level
;
3794 b
= current_binding_level
;
3797 b
= current_binding_level
;
3798 print_other_binding_stack (b
);
3799 fprintf (stderr
, "global:\n");
3800 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3803 /* Return the type associated with ID. */
3806 identifier_type_value_1 (tree id
)
3808 /* There is no type with that name, anywhere. */
3809 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3811 /* This is not the type marker, but the real thing. */
3812 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3813 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3814 /* Have to search for it. It must be on the global level, now.
3815 Ask lookup_name not to return non-types. */
3816 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3818 return TREE_TYPE (id
);
3822 /* Wrapper for identifier_type_value_1. */
3825 identifier_type_value (tree id
)
3828 timevar_start (TV_NAME_LOOKUP
);
3829 ret
= identifier_type_value_1 (id
);
3830 timevar_stop (TV_NAME_LOOKUP
);
3834 /* Push a definition of struct, union or enum tag named ID. into
3835 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3836 the tag ID is not already defined. */
3839 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3843 if (b
->kind
!= sk_namespace
)
3845 /* Shadow the marker, not the real thing, so that the marker
3846 gets restored later. */
3847 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3849 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3850 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3851 TREE_TYPE (b
->type_shadowed
) = type
;
3855 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3857 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3859 /* Store marker instead of real type. */
3860 type
= global_type_node
;
3862 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3865 /* As set_identifier_type_value_with_scope, but using
3866 current_binding_level. */
3869 set_identifier_type_value (tree id
, tree decl
)
3871 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3874 /* Return the name for the constructor (or destructor) for the
3878 constructor_name (tree type
)
3880 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3882 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3885 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3886 which must be a class type. */
3889 constructor_name_p (tree name
, tree type
)
3891 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3893 /* These don't have names. */
3894 if (TREE_CODE (type
) == DECLTYPE_TYPE
3895 || TREE_CODE (type
) == TYPEOF_TYPE
)
3898 if (name
&& name
== constructor_name (type
))
3904 /* Counter used to create anonymous type names. */
3906 static GTY(()) int anon_cnt
;
3908 /* Return an IDENTIFIER which can be used as a name for
3909 unnamed structs and unions. */
3912 make_anon_name (void)
3916 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
3917 return get_identifier (buf
);
3920 /* This code is practically identical to that for creating
3921 anonymous names, but is just used for lambdas instead. This isn't really
3922 necessary, but it's convenient to avoid treating lambdas like other
3925 static GTY(()) int lambda_cnt
= 0;
3928 make_lambda_name (void)
3932 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
3933 return get_identifier (buf
);
3936 /* Insert another USING_DECL into the current binding level, returning
3937 this declaration. If this is a redeclaration, do nothing, and
3938 return NULL_TREE if this not in namespace scope (in namespace
3939 scope, a using decl might extend any previous bindings). */
3942 push_using_decl_1 (tree scope
, tree name
)
3946 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
3947 gcc_assert (identifier_p (name
));
3948 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
3949 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
3952 return namespace_bindings_p () ? decl
: NULL_TREE
;
3953 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3954 USING_DECL_SCOPE (decl
) = scope
;
3955 DECL_CHAIN (decl
) = current_binding_level
->usings
;
3956 current_binding_level
->usings
= decl
;
3960 /* Wrapper for push_using_decl_1. */
3963 push_using_decl (tree scope
, tree name
)
3966 timevar_start (TV_NAME_LOOKUP
);
3967 ret
= push_using_decl_1 (scope
, name
);
3968 timevar_stop (TV_NAME_LOOKUP
);
3972 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3973 caller to set DECL_CONTEXT properly.
3975 Note that this must only be used when X will be the new innermost
3976 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3977 without checking to see if the current IDENTIFIER_BINDING comes from a
3978 closer binding level than LEVEL. */
3981 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3983 cp_binding_level
*b
;
3984 tree function_decl
= current_function_decl
;
3986 current_function_decl
= NULL_TREE
;
3987 if (level
->kind
== sk_class
)
3989 b
= class_binding_level
;
3990 class_binding_level
= level
;
3991 pushdecl_class_level (x
);
3992 class_binding_level
= b
;
3996 b
= current_binding_level
;
3997 current_binding_level
= level
;
3998 x
= pushdecl (x
, is_friend
);
3999 current_binding_level
= b
;
4001 current_function_decl
= function_decl
;
4005 /* Inject X into the local scope just before the function parms. */
4008 pushdecl_outermost_localscope (tree x
)
4010 cp_binding_level
*b
= NULL
;
4011 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4013 /* Find the scope just inside the function parms. */
4014 for (cp_binding_level
*n
= current_binding_level
;
4015 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
4018 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
4019 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4024 /* Check a non-member using-declaration. Return the name and scope
4025 being used, and the USING_DECL, or NULL_TREE on failure. */
4028 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
4030 /* [namespace.udecl]
4031 A using-declaration for a class member shall be a
4032 member-declaration. */
4035 error ("%qT is not a namespace or unscoped enum", scope
);
4038 else if (scope
== error_mark_node
)
4041 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
4044 A using-declaration shall not name a template-id. */
4045 error ("a using-declaration cannot specify a template-id. "
4046 "Try %<using %D%>", name
);
4050 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
4052 error ("namespace %qD not allowed in using-declaration", decl
);
4056 if (TREE_CODE (decl
) == SCOPE_REF
)
4058 /* It's a nested name with template parameter dependent scope.
4059 This can only be using-declaration for class member. */
4060 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
4064 decl
= OVL_FIRST (decl
);
4066 /* Make a USING_DECL. */
4067 tree using_decl
= push_using_decl (scope
, name
);
4069 if (using_decl
== NULL_TREE
4070 && at_function_scope_p ()
4072 /* C++11 7.3.3/10. */
4073 error ("%qD is already declared in this scope", name
);
4078 /* Process a local-scope or namespace-scope using declaration. SCOPE
4079 is the nominated scope to search for NAME. VALUE_P and TYPE_P
4080 point to the binding for NAME in the current scope and are
4084 do_nonmember_using_decl (tree scope
, tree name
, tree
*value_p
, tree
*type_p
)
4086 name_lookup
lookup (name
, 0);
4088 if (!qualified_namespace_lookup (scope
, &lookup
))
4090 error ("%qD not declared", name
);
4093 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
4095 error ("reference to %qD is ambiguous", name
);
4096 print_candidates (lookup
.value
);
4097 lookup
.value
= NULL_TREE
;
4100 if (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
)
4102 error ("reference to %qD is ambiguous", name
);
4103 print_candidates (lookup
.type
);
4104 lookup
.type
= NULL_TREE
;
4107 tree value
= *value_p
;
4108 tree type
= *type_p
;
4110 /* Shift the old and new bindings around so we're comparing class and
4111 enumeration names to each other. */
4112 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4118 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4120 lookup
.type
= lookup
.value
;
4121 lookup
.value
= NULL_TREE
;
4124 if (lookup
.value
&& lookup
.value
!= value
)
4126 /* Check for using functions. */
4127 if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4129 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4131 tree new_fn
= *usings
;
4133 /* [namespace.udecl]
4135 If a function declaration in namespace scope or block
4136 scope has the same name and the same parameter types as a
4137 function introduced by a using declaration the program is
4140 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4144 if (new_fn
== old_fn
)
4145 /* The function already exists in the current
4148 else if (old
.using_p ())
4149 continue; /* This is a using decl. */
4150 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
4151 continue; /* This is an anticipated builtin. */
4152 else if (!matching_fn_p (new_fn
, old_fn
))
4153 continue; /* Parameters do not match. */
4154 else if (decls_match (new_fn
, old_fn
))
4158 diagnose_name_conflict (new_fn
, old_fn
);
4164 /* Unlike the overload case we don't drop anticipated
4165 builtins here. They don't cause a problem, and
4166 we'd like to match them with a future
4168 value
= ovl_insert (new_fn
, value
, true);
4172 /* Ignore anticipated builtins. */
4173 && !anticipated_builtin_p (value
)
4174 && !decls_match (lookup
.value
, value
))
4175 diagnose_name_conflict (lookup
.value
, value
);
4177 value
= lookup
.value
;
4180 if (lookup
.type
&& lookup
.type
!= type
)
4182 if (type
&& !decls_match (lookup
.type
, type
))
4183 diagnose_name_conflict (lookup
.type
, type
);
4188 /* If bind->value is empty, shift any class or enumeration name back. */
4199 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4200 Both are namespaces. */
4203 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4205 int depth
= SCOPE_DEPTH (ancestor
);
4207 if (!depth
&& !inline_only
)
4208 /* The global namespace encloses everything. */
4211 while (SCOPE_DEPTH (descendant
) > depth
4212 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4213 descendant
= CP_DECL_CONTEXT (descendant
);
4215 return ancestor
== descendant
;
4218 /* Returns true if ROOT (a namespace, class, or function) encloses
4219 CHILD. CHILD may be either a class type or a namespace. */
4222 is_ancestor (tree root
, tree child
)
4224 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4225 || TREE_CODE (root
) == FUNCTION_DECL
4226 || CLASS_TYPE_P (root
)));
4227 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
4228 || CLASS_TYPE_P (child
)));
4230 /* The global namespace encloses everything. */
4231 if (root
== global_namespace
)
4234 /* Search until we reach namespace scope. */
4235 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4237 /* If we've reached the ROOT, it encloses CHILD. */
4240 /* Go out one level. */
4242 child
= TYPE_NAME (child
);
4243 child
= CP_DECL_CONTEXT (child
);
4246 if (TREE_CODE (root
) == NAMESPACE_DECL
)
4247 return is_nested_namespace (root
, child
);
4252 /* Enter the class or namespace scope indicated by T suitable for name
4253 lookup. T can be arbitrary scope, not necessary nested inside the
4254 current scope. Returns a non-null scope to pop iff pop_scope
4255 should be called later to exit this scope. */
4260 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4261 push_decl_namespace (t
);
4262 else if (CLASS_TYPE_P (t
))
4264 if (!at_class_scope_p ()
4265 || !same_type_p (current_class_type
, t
))
4266 push_nested_class (t
);
4268 /* T is the same as the current scope. There is therefore no
4269 need to re-enter the scope. Since we are not actually
4270 pushing a new scope, our caller should not call
4278 /* Leave scope pushed by push_scope. */
4285 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4286 pop_decl_namespace ();
4287 else if CLASS_TYPE_P (t
)
4288 pop_nested_class ();
4291 /* Subroutine of push_inner_scope. */
4294 push_inner_scope_r (tree outer
, tree inner
)
4299 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4302 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4304 push_inner_scope_r (outer
, prev
);
4305 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4307 cp_binding_level
*save_template_parm
= 0;
4308 /* Temporary take out template parameter scopes. They are saved
4309 in reversed order in save_template_parm. */
4310 while (current_binding_level
->kind
== sk_template_parms
)
4312 cp_binding_level
*b
= current_binding_level
;
4313 current_binding_level
= b
->level_chain
;
4314 b
->level_chain
= save_template_parm
;
4315 save_template_parm
= b
;
4318 resume_scope (NAMESPACE_LEVEL (inner
));
4319 current_namespace
= inner
;
4321 /* Restore template parameter scopes. */
4322 while (save_template_parm
)
4324 cp_binding_level
*b
= save_template_parm
;
4325 save_template_parm
= b
->level_chain
;
4326 b
->level_chain
= current_binding_level
;
4327 current_binding_level
= b
;
4334 /* Enter the scope INNER from current scope. INNER must be a scope
4335 nested inside current scope. This works with both name lookup and
4336 pushing name into scope. In case a template parameter scope is present,
4337 namespace is pushed under the template parameter scope according to
4338 name lookup rule in 14.6.1/6.
4340 Return the former current scope suitable for pop_inner_scope. */
4343 push_inner_scope (tree inner
)
4345 tree outer
= current_scope ();
4347 outer
= current_namespace
;
4349 push_inner_scope_r (outer
, inner
);
4353 /* Exit the current scope INNER back to scope OUTER. */
4356 pop_inner_scope (tree outer
, tree inner
)
4359 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4362 while (outer
!= inner
)
4364 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4366 cp_binding_level
*save_template_parm
= 0;
4367 /* Temporary take out template parameter scopes. They are saved
4368 in reversed order in save_template_parm. */
4369 while (current_binding_level
->kind
== sk_template_parms
)
4371 cp_binding_level
*b
= current_binding_level
;
4372 current_binding_level
= b
->level_chain
;
4373 b
->level_chain
= save_template_parm
;
4374 save_template_parm
= b
;
4379 /* Restore template parameter scopes. */
4380 while (save_template_parm
)
4382 cp_binding_level
*b
= save_template_parm
;
4383 save_template_parm
= b
->level_chain
;
4384 b
->level_chain
= current_binding_level
;
4385 current_binding_level
= b
;
4391 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4395 /* Do a pushlevel for class declarations. */
4398 pushlevel_class (void)
4400 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4403 /* ...and a poplevel for class declarations. */
4406 poplevel_class (void)
4408 cp_binding_level
*level
= class_binding_level
;
4409 cp_class_binding
*cb
;
4413 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4414 gcc_assert (level
!= 0);
4416 /* If we're leaving a toplevel class, cache its binding level. */
4417 if (current_class_depth
== 1)
4418 previous_class_level
= level
;
4419 for (shadowed
= level
->type_shadowed
;
4421 shadowed
= TREE_CHAIN (shadowed
))
4422 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4424 /* Remove the bindings for all of the class-level declarations. */
4425 if (level
->class_shadowed
)
4427 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4429 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4430 cxx_binding_free (cb
->base
);
4432 ggc_free (level
->class_shadowed
);
4433 level
->class_shadowed
= NULL
;
4436 /* Now, pop out of the binding level which we created up in the
4437 `pushlevel_class' routine. */
4438 gcc_assert (current_binding_level
== level
);
4440 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4443 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4444 appropriate. DECL is the value to which a name has just been
4445 bound. CLASS_TYPE is the class in which the lookup occurred. */
4448 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4451 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4455 if (TREE_CODE (decl
) == OVERLOAD
)
4456 context
= ovl_scope (decl
);
4459 gcc_assert (DECL_P (decl
));
4460 context
= context_for_name_lookup (decl
);
4463 if (is_properly_derived_from (class_type
, context
))
4464 INHERITED_VALUE_BINDING_P (binding
) = 1;
4466 INHERITED_VALUE_BINDING_P (binding
) = 0;
4468 else if (binding
->value
== decl
)
4469 /* We only encounter a TREE_LIST when there is an ambiguity in the
4470 base classes. Such an ambiguity can be overridden by a
4471 definition in this class. */
4472 INHERITED_VALUE_BINDING_P (binding
) = 1;
4474 INHERITED_VALUE_BINDING_P (binding
) = 0;
4477 /* Make the declaration of X appear in CLASS scope. */
4480 pushdecl_class_level (tree x
)
4482 bool is_valid
= true;
4485 /* Do nothing if we're adding to an outer lambda closure type,
4486 outer_binding will add it later if it's needed. */
4487 if (current_class_type
!= class_binding_level
->this_entity
)
4490 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4491 /* Get the name of X. */
4492 tree name
= OVL_NAME (x
);
4496 is_valid
= push_class_level_binding (name
, x
);
4497 if (TREE_CODE (x
) == TYPE_DECL
)
4498 set_identifier_type_value (name
, x
);
4500 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4502 /* If X is an anonymous aggregate, all of its members are
4503 treated as if they were members of the class containing the
4504 aggregate, for naming purposes. */
4507 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
4509 location_t save_location
= input_location
;
4510 input_location
= DECL_SOURCE_LOCATION (f
);
4511 if (!pushdecl_class_level (f
))
4513 input_location
= save_location
;
4516 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4520 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4521 scope. If the value returned is non-NULL, and the PREVIOUS field
4522 is not set, callers must set the PREVIOUS field explicitly. */
4524 static cxx_binding
*
4525 get_class_binding (tree name
, cp_binding_level
*scope
)
4530 cxx_binding
*binding
;
4532 class_type
= scope
->this_entity
;
4534 /* Get the type binding. */
4535 type_binding
= lookup_member (class_type
, name
,
4536 /*protect=*/2, /*want_type=*/true,
4537 tf_warning_or_error
);
4538 /* Get the value binding. */
4539 value_binding
= lookup_member (class_type
, name
,
4540 /*protect=*/2, /*want_type=*/false,
4541 tf_warning_or_error
);
4544 && (TREE_CODE (value_binding
) == TYPE_DECL
4545 || DECL_CLASS_TEMPLATE_P (value_binding
)
4546 || (TREE_CODE (value_binding
) == TREE_LIST
4547 && TREE_TYPE (value_binding
) == error_mark_node
4548 && (TREE_CODE (TREE_VALUE (value_binding
))
4550 /* We found a type binding, even when looking for a non-type
4551 binding. This means that we already processed this binding
4554 else if (value_binding
)
4556 if (TREE_CODE (value_binding
) == TREE_LIST
4557 && TREE_TYPE (value_binding
) == error_mark_node
)
4558 /* NAME is ambiguous. */
4560 else if (BASELINK_P (value_binding
))
4561 /* NAME is some overloaded functions. */
4562 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4565 /* If we found either a type binding or a value binding, create a
4566 new binding object. */
4567 if (type_binding
|| value_binding
)
4569 binding
= new_class_binding (name
,
4573 /* This is a class-scope binding, not a block-scope binding. */
4574 LOCAL_BINDING_P (binding
) = 0;
4575 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4583 /* Make the declaration(s) of X appear in CLASS scope under the name
4584 NAME. Returns true if the binding is valid. */
4587 push_class_level_binding_1 (tree name
, tree x
)
4589 cxx_binding
*binding
;
4593 /* The class_binding_level will be NULL if x is a template
4594 parameter name in a member template. */
4595 if (!class_binding_level
)
4598 if (name
== error_mark_node
)
4601 /* Can happen for an erroneous declaration (c++/60384). */
4602 if (!identifier_p (name
))
4604 gcc_assert (errorcount
|| sorrycount
);
4608 /* Check for invalid member names. But don't worry about a default
4609 argument-scope lambda being pushed after the class is complete. */
4610 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4611 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4612 /* Check that we're pushing into the right binding level. */
4613 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4615 /* We could have been passed a tree list if this is an ambiguous
4616 declaration. If so, pull the declaration out because
4617 check_template_shadow will not handle a TREE_LIST. */
4618 if (TREE_CODE (decl
) == TREE_LIST
4619 && TREE_TYPE (decl
) == error_mark_node
)
4620 decl
= TREE_VALUE (decl
);
4622 if (!check_template_shadow (decl
))
4627 If T is the name of a class, then each of the following shall
4628 have a name different from T:
4630 -- every static data member of class T;
4632 -- every member of class T that is itself a type;
4634 -- every enumerator of every member of class T that is an
4637 -- every member of every anonymous union that is a member of
4640 (Non-static data members were also forbidden to have the same
4641 name as T until TC1.) */
4643 || TREE_CODE (x
) == CONST_DECL
4644 || (TREE_CODE (x
) == TYPE_DECL
4645 && !DECL_SELF_REFERENCE_P (x
))
4646 /* A data member of an anonymous union. */
4647 || (TREE_CODE (x
) == FIELD_DECL
4648 && DECL_CONTEXT (x
) != current_class_type
))
4649 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4651 tree scope
= context_for_name_lookup (x
);
4652 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4654 error ("%qD has the same name as the class in which it is "
4661 /* Get the current binding for NAME in this class, if any. */
4662 binding
= IDENTIFIER_BINDING (name
);
4663 if (!binding
|| binding
->scope
!= class_binding_level
)
4665 binding
= get_class_binding (name
, class_binding_level
);
4666 /* If a new binding was created, put it at the front of the
4667 IDENTIFIER_BINDING list. */
4670 binding
->previous
= IDENTIFIER_BINDING (name
);
4671 IDENTIFIER_BINDING (name
) = binding
;
4675 /* If there is already a binding, then we may need to update the
4677 if (binding
&& binding
->value
)
4679 tree bval
= binding
->value
;
4680 tree old_decl
= NULL_TREE
;
4681 tree target_decl
= strip_using_decl (decl
);
4682 tree target_bval
= strip_using_decl (bval
);
4684 if (INHERITED_VALUE_BINDING_P (binding
))
4686 /* If the old binding was from a base class, and was for a
4687 tag name, slide it over to make room for the new binding.
4688 The old binding is still visible if explicitly qualified
4689 with a class-key. */
4690 if (TREE_CODE (target_bval
) == TYPE_DECL
4691 && DECL_ARTIFICIAL (target_bval
)
4692 && !(TREE_CODE (target_decl
) == TYPE_DECL
4693 && DECL_ARTIFICIAL (target_decl
)))
4695 old_decl
= binding
->type
;
4696 binding
->type
= bval
;
4697 binding
->value
= NULL_TREE
;
4698 INHERITED_VALUE_BINDING_P (binding
) = 0;
4703 /* Any inherited type declaration is hidden by the type
4704 declaration in the derived class. */
4705 if (TREE_CODE (target_decl
) == TYPE_DECL
4706 && DECL_ARTIFICIAL (target_decl
))
4707 binding
->type
= NULL_TREE
;
4710 else if (TREE_CODE (target_decl
) == OVERLOAD
4711 && OVL_P (target_bval
))
4713 else if (TREE_CODE (decl
) == USING_DECL
4714 && TREE_CODE (bval
) == USING_DECL
4715 && same_type_p (USING_DECL_SCOPE (decl
),
4716 USING_DECL_SCOPE (bval
)))
4717 /* This is a using redeclaration that will be diagnosed later
4718 in supplement_binding */
4720 else if (TREE_CODE (decl
) == USING_DECL
4721 && TREE_CODE (bval
) == USING_DECL
4722 && DECL_DEPENDENT_P (decl
)
4723 && DECL_DEPENDENT_P (bval
))
4725 else if (TREE_CODE (decl
) == USING_DECL
4726 && OVL_P (target_bval
))
4728 else if (TREE_CODE (bval
) == USING_DECL
4729 && OVL_P (target_decl
))
4732 if (old_decl
&& binding
->scope
== class_binding_level
)
4735 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4736 here. This function is only used to register bindings
4737 from with the class definition itself. */
4738 INHERITED_VALUE_BINDING_P (binding
) = 0;
4743 /* Note that we declared this value so that we can issue an error if
4744 this is an invalid redeclaration of a name already used for some
4746 note_name_declared_in_class (name
, decl
);
4748 /* If we didn't replace an existing binding, put the binding on the
4749 stack of bindings for the identifier, and update the shadowed
4751 if (binding
&& binding
->scope
== class_binding_level
)
4752 /* Supplement the existing binding. */
4753 ok
= supplement_binding (binding
, decl
);
4756 /* Create a new binding. */
4757 push_binding (name
, decl
, class_binding_level
);
4764 /* Wrapper for push_class_level_binding_1. */
4767 push_class_level_binding (tree name
, tree x
)
4770 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4771 ret
= push_class_level_binding_1 (name
, x
);
4772 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4776 /* Process "using SCOPE::NAME" in a class scope. Return the
4777 USING_DECL created. */
4780 do_class_using_decl (tree scope
, tree name
)
4782 if (name
== error_mark_node
)
4785 if (!scope
|| !TYPE_P (scope
))
4787 error ("using-declaration for non-member at class scope");
4791 /* Make sure the name is not invalid */
4792 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4794 error ("%<%T::%D%> names destructor", scope
, name
);
4798 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4799 if (MAYBE_CLASS_TYPE_P (scope
)
4800 && (name
== TYPE_IDENTIFIER (scope
)
4801 || constructor_name_p (name
, scope
)))
4803 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4804 name
= ctor_identifier
;
4805 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4808 /* Cannot introduce a constructor name. */
4809 if (constructor_name_p (name
, current_class_type
))
4811 error ("%<%T::%D%> names constructor in %qT",
4812 scope
, name
, current_class_type
);
4816 /* From [namespace.udecl]:
4818 A using-declaration used as a member-declaration shall refer to a
4819 member of a base class of the class being defined.
4821 In general, we cannot check this constraint in a template because
4822 we do not know the entire set of base classes of the current
4823 class type. Morover, if SCOPE is dependent, it might match a
4824 non-dependent base. */
4826 tree decl
= NULL_TREE
;
4827 if (!dependent_scope_p (scope
))
4830 tree binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4831 tf_warning_or_error
);
4832 if (b_kind
< bk_proper_base
)
4834 /* If there are dependent bases, scope might resolve at
4835 instantiation time, even if it isn't exactly one of the
4837 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4839 error_not_base_type (scope
, current_class_type
);
4843 else if (name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4845 error ("cannot inherit constructors from indirect base %qT", scope
);
4848 else if (!IDENTIFIER_CONV_OP_P (name
)
4849 || !dependent_type_p (TREE_TYPE (name
)))
4851 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4854 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4859 /* The binfo from which the functions came does not matter. */
4860 if (BASELINK_P (decl
))
4861 decl
= BASELINK_FUNCTIONS (decl
);
4865 tree value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4866 USING_DECL_DECLS (value
) = decl
;
4867 USING_DECL_SCOPE (value
) = scope
;
4868 DECL_DEPENDENT_P (value
) = !decl
;
4874 /* Return the binding for NAME in NS. If NS is NULL, look in
4875 global_namespace. */
4878 get_namespace_binding (tree ns
, tree name
)
4880 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4882 ns
= global_namespace
;
4883 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4884 tree ret
= find_namespace_value (ns
, name
);
4885 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4889 /* Push internal DECL into the global namespace. Does not do the
4890 full overload fn handling and does not add it to the list of things
4891 in the namespace. */
4894 set_global_binding (tree decl
)
4896 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4898 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4901 /* The user's placed something in the implementor's namespace. */
4902 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4904 /* Force the binding, so compiler internals continue to work. */
4907 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4910 /* Set the context of a declaration to scope. Complain if we are not
4914 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4916 /* Get rid of namespace aliases. */
4917 scope
= ORIGINAL_NAMESPACE (scope
);
4919 /* It is ok for friends to be qualified in parallel space. */
4920 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4921 error ("declaration of %qD not in a namespace surrounding %qD",
4923 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4925 /* See whether this has been declared in the namespace or inline
4927 tree old
= NULL_TREE
;
4929 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4930 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4931 /* No old declaration at all. */
4936 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4937 if (TREE_CODE (old
) == TREE_LIST
)
4940 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4941 error ("reference to %qD is ambiguous", decl
);
4942 print_candidates (old
);
4946 if (!DECL_DECLARES_FUNCTION_P (decl
))
4948 /* Don't compare non-function decls with decls_match here, since
4949 it can't check for the correct constness at this
4950 point. pushdecl will find those errors later. */
4952 /* We might have found it in an inline namespace child of SCOPE. */
4953 if (TREE_CODE (decl
) == TREE_CODE (old
))
4954 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4957 /* Writing "N::i" to declare something directly in "N" is invalid. */
4958 if (CP_DECL_CONTEXT (decl
) == current_namespace
4959 && at_namespace_scope_p ())
4960 error ("explicit qualification in declaration of %qD", decl
);
4964 /* Since decl is a function, old should contain a function decl. */
4968 /* We handle these in check_explicit_instantiation_namespace. */
4969 if (processing_explicit_instantiation
)
4971 if (processing_template_decl
|| processing_specialization
)
4972 /* We have not yet called push_template_decl to turn a
4973 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4974 match. But, we'll check later, when we construct the
4977 /* Instantiations or specializations of templates may be declared as
4978 friends in any namespace. */
4979 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4985 for (lkp_iterator
iter (old
); iter
; ++iter
)
4987 if (iter
.using_p ())
4992 /* Adjust DECL_CONTEXT first so decls_match will return true
4993 if DECL will match a declaration in an inline namespace. */
4994 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4995 if (decls_match (decl
, ofn
))
4999 /* We found more than one matching declaration. */
5000 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5009 if (DECL_HIDDEN_FRIEND_P (found
))
5011 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
5012 "%qD has not been declared within %qD", decl
, scope
);
5013 inform (DECL_SOURCE_LOCATION (found
),
5014 "only here as a %<friend%>");
5016 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
5021 /* It didn't work, go back to the explicit scope. */
5022 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5023 error ("%qD should have been declared inside %qD", decl
, scope
);
5026 /* Return the namespace where the current declaration is declared. */
5029 current_decl_namespace (void)
5032 /* If we have been pushed into a different namespace, use it. */
5033 if (!vec_safe_is_empty (decl_namespace_list
))
5034 return decl_namespace_list
->last ();
5036 if (current_class_type
)
5037 result
= decl_namespace_context (current_class_type
);
5038 else if (current_function_decl
)
5039 result
= decl_namespace_context (current_function_decl
);
5041 result
= current_namespace
;
5045 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5046 attribute visibility is seen. */
5049 handle_namespace_attrs (tree ns
, tree attributes
)
5052 bool saw_vis
= false;
5054 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
5056 tree name
= get_attribute_name (d
);
5057 tree args
= TREE_VALUE (d
);
5059 if (is_attribute_p ("visibility", name
))
5061 /* attribute visibility is a property of the syntactic block
5062 rather than the namespace as a whole, so we don't touch the
5063 NAMESPACE_DECL at all. */
5064 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
5065 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
5067 warning (OPT_Wattributes
,
5068 "%qD attribute requires a single NTBS argument",
5073 if (!TREE_PUBLIC (ns
))
5074 warning (OPT_Wattributes
,
5075 "%qD attribute is meaningless since members of the "
5076 "anonymous namespace get local symbols", name
);
5078 push_visibility (TREE_STRING_POINTER (x
), 1);
5081 else if (is_attribute_p ("abi_tag", name
))
5083 if (!DECL_NAME (ns
))
5085 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5089 if (!DECL_NAMESPACE_INLINE_P (ns
))
5091 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5097 tree dn
= DECL_NAME (ns
);
5098 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5099 IDENTIFIER_POINTER (dn
));
5100 TREE_TYPE (args
) = char_array_type_node
;
5101 args
= fix_string_type (args
);
5102 args
= build_tree_list (NULL_TREE
, args
);
5104 if (check_abi_tag_args (args
, name
))
5105 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5106 DECL_ATTRIBUTES (ns
));
5110 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5119 /* Temporarily set the namespace for the current declaration. */
5122 push_decl_namespace (tree decl
)
5124 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5125 decl
= decl_namespace_context (decl
);
5126 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5129 /* [namespace.memdef]/2 */
5132 pop_decl_namespace (void)
5134 decl_namespace_list
->pop ();
5137 /* Process a namespace-alias declaration. */
5140 do_namespace_alias (tree alias
, tree name_space
)
5142 if (name_space
== error_mark_node
)
5145 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5147 name_space
= ORIGINAL_NAMESPACE (name_space
);
5149 /* Build the alias. */
5150 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5151 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5152 DECL_EXTERNAL (alias
) = 1;
5153 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5156 /* Emit debug info for namespace alias. */
5157 if (!building_stmt_list_p ())
5158 (*debug_hooks
->early_global_decl
) (alias
);
5161 /* Like pushdecl, only it places X in the current namespace,
5165 pushdecl_namespace_level (tree x
, bool is_friend
)
5167 cp_binding_level
*b
= current_binding_level
;
5170 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5171 t
= do_pushdecl_with_scope
5172 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
5174 /* Now, the type_shadowed stack may screw us. Munge it so it does
5176 if (TREE_CODE (t
) == TYPE_DECL
)
5178 tree name
= DECL_NAME (t
);
5180 tree
*ptr
= (tree
*)0;
5181 for (; !global_scope_p (b
); b
= b
->level_chain
)
5183 tree shadowed
= b
->type_shadowed
;
5184 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5185 if (TREE_PURPOSE (shadowed
) == name
)
5187 ptr
= &TREE_VALUE (shadowed
);
5188 /* Can't break out of the loop here because sometimes
5189 a binding level will have duplicate bindings for
5190 PT names. It's gross, but I haven't time to fix it. */
5193 newval
= TREE_TYPE (t
);
5194 if (ptr
== (tree
*)0)
5196 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5197 up here if this is changed to an assertion. --KR */
5198 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5205 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5209 /* Process a using-declaration appearing in namespace scope. */
5212 finish_namespace_using_decl (tree decl
, tree scope
, tree name
)
5214 tree orig_decl
= decl
;
5216 gcc_checking_assert (current_binding_level
->kind
== sk_namespace
5217 && !processing_template_decl
);
5218 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5219 if (decl
== NULL_TREE
)
5222 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5223 tree val
= slot
? MAYBE_STAT_DECL (*slot
) : NULL_TREE
;
5224 tree type
= slot
? MAYBE_STAT_TYPE (*slot
) : NULL_TREE
;
5225 do_nonmember_using_decl (scope
, name
, &val
, &type
);
5226 if (STAT_HACK_P (*slot
))
5228 STAT_DECL (*slot
) = val
;
5229 STAT_TYPE (*slot
) = type
;
5232 *slot
= stat_hack (val
, type
);
5236 /* Emit debug info. */
5237 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
5240 /* Process a using-declaration at function scope. */
5243 finish_local_using_decl (tree decl
, tree scope
, tree name
)
5245 tree orig_decl
= decl
;
5247 gcc_checking_assert (current_binding_level
->kind
!= sk_class
5248 && current_binding_level
->kind
!= sk_namespace
);
5249 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
5250 if (decl
== NULL_TREE
)
5253 add_decl_expr (decl
);
5255 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5256 tree value
= binding
? binding
->value
: NULL_TREE
;
5257 tree type
= binding
? binding
->type
: NULL_TREE
;
5259 do_nonmember_using_decl (scope
, name
, &value
, &type
);
5263 else if (binding
&& value
== binding
->value
)
5265 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5267 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5268 IDENTIFIER_BINDING (name
)->value
= value
;
5271 /* Install the new binding. */
5272 push_local_binding (name
, value
, true);
5276 else if (binding
&& type
== binding
->type
)
5280 push_local_binding (name
, type
, true);
5281 set_identifier_type_value (name
, type
);
5284 /* Emit debug info. */
5285 if (!processing_template_decl
)
5286 cp_emit_debug_info_for_using (orig_decl
, current_scope ());
5289 /* Return the declarations that are members of the namespace NS. */
5292 cp_namespace_decls (tree ns
)
5294 return NAMESPACE_LEVEL (ns
)->names
;
5297 /* Combine prefer_type and namespaces_only into flags. */
5300 lookup_flags (int prefer_type
, int namespaces_only
)
5302 if (namespaces_only
)
5303 return LOOKUP_PREFER_NAMESPACES
;
5304 if (prefer_type
> 1)
5305 return LOOKUP_PREFER_TYPES
;
5306 if (prefer_type
> 0)
5307 return LOOKUP_PREFER_BOTH
;
5311 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5312 ignore it or not. Subroutine of lookup_name_real and
5313 lookup_type_scope. */
5316 qualify_lookup (tree val
, int flags
)
5318 if (val
== NULL_TREE
)
5320 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5322 if (flags
& LOOKUP_PREFER_TYPES
)
5324 tree target_val
= strip_using_decl (val
);
5325 if (TREE_CODE (target_val
) == TYPE_DECL
5326 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5329 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5331 /* Look through lambda things that we shouldn't be able to see. */
5332 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5337 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
5338 lookup failed. Search through all available namespaces and print out
5339 possible candidates. If no exact matches are found, and
5340 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
5341 suggest the best near-match, if there is one. */
5344 suggest_alternatives_for (location_t location
, tree name
,
5345 bool suggest_misspellings
)
5347 vec
<tree
> candidates
= vNULL
;
5348 vec
<tree
> worklist
= vNULL
;
5349 unsigned limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
5350 bool limited
= false;
5352 /* Breadth-first search of namespaces. Up to limit namespaces
5353 searched (limit zero == unlimited). */
5354 worklist
.safe_push (global_namespace
);
5355 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5357 tree ns
= worklist
[ix
];
5358 name_lookup
lookup (name
);
5360 if (lookup
.search_qualified (ns
, false))
5361 candidates
.safe_push (lookup
.value
);
5365 /* Look for child namespaces. We have to do this
5366 indirectly because they are chained in reverse order,
5367 which is confusing to the user. */
5368 vec
<tree
> children
= vNULL
;
5370 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5371 decl
; decl
= TREE_CHAIN (decl
))
5372 if (TREE_CODE (decl
) == NAMESPACE_DECL
5373 && !DECL_NAMESPACE_ALIAS (decl
)
5374 && !DECL_NAMESPACE_INLINE_P (decl
))
5375 children
.safe_push (decl
);
5377 while (!limited
&& !children
.is_empty ())
5379 if (worklist
.length () == limit
)
5381 /* Unconditionally warn that the search was truncated. */
5383 "maximum limit of %d namespaces searched for %qE",
5388 worklist
.safe_push (children
.pop ());
5390 children
.release ();
5393 worklist
.release ();
5395 if (candidates
.length ())
5397 inform_n (location
, candidates
.length (),
5398 "suggested alternative:",
5399 "suggested alternatives:");
5400 for (unsigned ix
= 0; ix
!= candidates
.length (); ix
++)
5402 tree val
= candidates
[ix
];
5404 inform (location_of (val
), " %qE", val
);
5406 candidates
.release ();
5408 else if (!suggest_misspellings
)
5410 else if (name_hint hint
= lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
,
5413 /* Show a spelling correction. */
5414 gcc_rich_location
richloc (location
);
5416 richloc
.add_fixit_replace (hint
.suggestion ());
5417 inform (&richloc
, "suggested alternative: %qs", hint
.suggestion ());
5421 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5422 for some of the most common names within "std::".
5423 Given non-NULL NAME, a name for lookup within "std::", return the header
5424 name defining it within the C++ Standard Library (with '<' and '>'),
5428 get_std_name_hint (const char *name
)
5430 struct std_name_hint
5435 static const std_name_hint hints
[] = {
5437 {"array", "<array>"}, // C++11
5439 {"complex", "<complex>"},
5440 {"complex_literals", "<complex>"},
5442 {"deque", "<deque>"},
5443 /* <forward_list>. */
5444 {"forward_list", "<forward_list>"}, // C++11
5446 {"basic_filebuf", "<fstream>"},
5447 {"basic_ifstream", "<fstream>"},
5448 {"basic_ofstream", "<fstream>"},
5449 {"basic_fstream", "<fstream>"},
5451 {"cin", "<iostream>"},
5452 {"cout", "<iostream>"},
5453 {"cerr", "<iostream>"},
5454 {"clog", "<iostream>"},
5455 {"wcin", "<iostream>"},
5456 {"wcout", "<iostream>"},
5457 {"wclog", "<iostream>"},
5462 {"multimap", "<map>"},
5464 {"queue", "<queue>"},
5465 {"priority_queue", "<queue>"},
5467 {"ostream", "<ostream>"},
5468 {"wostream", "<ostream>"},
5469 {"ends", "<ostream>"},
5470 {"flush", "<ostream>"},
5471 {"endl", "<ostream>"},
5474 {"multiset", "<set>"},
5476 {"basic_stringbuf", "<sstream>"},
5477 {"basic_istringstream", "<sstream>"},
5478 {"basic_ostringstream", "<sstream>"},
5479 {"basic_stringstream", "<sstream>"},
5481 {"stack", "<stack>"},
5483 {"string", "<string>"},
5484 {"wstring", "<string>"},
5485 {"u16string", "<string>"},
5486 {"u32string", "<string>"},
5487 /* <unordered_map>. */
5488 {"unordered_map", "<unordered_map>"}, // C++11
5489 {"unordered_multimap", "<unordered_map>"}, // C++11
5490 /* <unordered_set>. */
5491 {"unordered_set", "<unordered_set>"}, // C++11
5492 {"unordered_multiset", "<unordered_set>"}, // C++11
5494 {"vector", "<vector>"},
5496 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5497 for (size_t i
= 0; i
< num_hints
; i
++)
5499 if (strcmp (name
, hints
[i
].name
) == 0)
5500 return hints
[i
].header
;
5505 /* If SCOPE is the "std" namespace, then suggest pertinent header
5506 files for NAME at LOCATION.
5507 Return true iff a suggestion was offered. */
5510 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5512 if (scope
== NULL_TREE
)
5514 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5516 /* We only offer suggestions for the "std" namespace. */
5517 if (scope
!= std_node
)
5519 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5521 const char *name_str
= IDENTIFIER_POINTER (name
);
5522 const char *header_hint
= get_std_name_hint (name_str
);
5526 gcc_rich_location
richloc (location
);
5527 maybe_add_include_fixit (&richloc
, header_hint
);
5529 "%<std::%s%> is defined in header %qs;"
5530 " did you forget to %<#include %s%>?",
5531 name_str
, header_hint
, header_hint
);
5535 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
5536 lookup failed within the explicitly provided SCOPE. Suggest the
5537 the best meaningful candidates (if any) as a fix-it hint.
5538 Return true iff a suggestion was provided. */
5541 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5544 /* Resolve any namespace aliases. */
5545 scope
= ORIGINAL_NAMESPACE (scope
);
5547 if (maybe_suggest_missing_header (location
, name
, scope
))
5550 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5552 best_match
<tree
, const char *> bm (name
);
5553 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5555 /* See if we have a good suggesion for the user. */
5556 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5559 gcc_rich_location
richloc (location
);
5560 richloc
.add_fixit_replace (fuzzy_name
);
5561 inform (&richloc
, "suggested alternative: %qs",
5569 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5572 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5573 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5575 Returns a DECL (or OVERLOAD, or BASELINK) representing the
5576 declaration found. If no suitable declaration can be found,
5577 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
5578 neither a class-type nor a namespace a diagnostic is issued. */
5581 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
5586 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5588 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
5590 flags
|= LOOKUP_HIDDEN
;
5591 name_lookup
lookup (name
, flags
);
5593 if (qualified_namespace_lookup (scope
, &lookup
))
5596 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
5597 t
= lookup_enumerator (scope
, name
);
5598 else if (is_class_type (scope
, complain
))
5599 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
5602 return error_mark_node
;
5607 Accepts the NAME to lookup and its qualifying SCOPE.
5608 Returns the name/type pair found into the cxx_binding *RESULT,
5609 or false on error. */
5612 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
5614 timevar_start (TV_NAME_LOOKUP
);
5615 query_oracle (lookup
->name
);
5616 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
5617 timevar_stop (TV_NAME_LOOKUP
);
5621 /* Helper function for lookup_name_fuzzy.
5622 Traverse binding level LVL, looking for good name matches for NAME
5625 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
5626 cp_binding_level
*lvl
, bool look_within_fields
,
5627 enum lookup_name_fuzzy_kind kind
)
5629 if (look_within_fields
)
5630 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
5632 tree type
= lvl
->this_entity
;
5633 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
5634 tree best_matching_field
5635 = lookup_member_fuzzy (type
, name
, want_type_p
);
5636 if (best_matching_field
)
5637 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
5640 /* Only suggest names reserved for the implementation if NAME begins
5641 with an underscore. */
5642 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
5644 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
5648 /* OVERLOADs or decls from using declaration are wrapped into
5650 if (TREE_CODE (d
) == TREE_LIST
)
5651 d
= OVL_FIRST (TREE_VALUE (d
));
5653 /* Don't use bindings from implicitly declared functions,
5654 as they were likely misspellings themselves. */
5655 if (TREE_TYPE (d
) == error_mark_node
)
5658 /* Skip anticipated decls of builtin functions. */
5659 if (TREE_CODE (d
) == FUNCTION_DECL
5660 && DECL_BUILT_IN (d
)
5661 && DECL_ANTICIPATED (d
))
5664 tree suggestion
= DECL_NAME (d
);
5668 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
5670 /* Ignore internal names with spaces in them. */
5671 if (strchr (suggestion_str
, ' '))
5674 /* Don't suggest names that are reserved for use by the
5675 implementation, unless NAME began with an underscore. */
5676 if (name_reserved_for_implementation_p (suggestion_str
)
5677 && !consider_implementation_names
)
5680 bm
.consider (suggestion_str
);
5684 /* Subclass of deferred_diagnostic. Notify the user that the
5685 given macro was used before it was defined.
5686 This can be done in the C++ frontend since tokenization happens
5689 class macro_use_before_def
: public deferred_diagnostic
5692 /* Ctor. LOC is the location of the usage. MACRO is the
5693 macro that was used. */
5694 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
5695 : deferred_diagnostic (loc
), m_macro (macro
)
5700 ~macro_use_before_def ()
5702 if (is_suppressed_p ())
5705 source_location def_loc
= cpp_macro_definition_location (m_macro
);
5706 if (def_loc
!= UNKNOWN_LOCATION
)
5708 inform (get_location (), "the macro %qs had not yet been defined",
5709 (const char *)m_macro
->ident
.str
);
5710 inform (def_loc
, "it was later defined here");
5715 cpp_hashnode
*m_macro
;
5718 /* Determine if it can ever make sense to offer RID as a suggestion for
5721 Subroutine of lookup_name_fuzzy. */
5724 suggest_rid_p (enum rid rid
)
5728 /* Support suggesting function-like keywords. */
5729 case RID_STATIC_ASSERT
:
5733 /* Support suggesting the various decl-specifier words, to handle
5734 e.g. "singed" vs "signed" typos. */
5735 if (cp_keyword_starts_decl_specifier_p (rid
))
5738 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
5739 and "do" for short misspellings, which are likely to lead to
5740 nonsensical results. */
5745 /* Search for near-matches for NAME within the current bindings, and within
5746 macro names, returning the best match as a const char *, or NULL if
5747 no reasonable match is found.
5749 Use LOC for any deferred diagnostics. */
5752 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
5754 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5756 /* First, try some well-known names in the C++ standard library, in case
5757 the user forgot a #include. */
5758 const char *header_hint
5759 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
5761 return name_hint (NULL
,
5762 new suggest_missing_header (loc
,
5763 IDENTIFIER_POINTER (name
),
5766 best_match
<tree
, const char *> bm (name
);
5768 cp_binding_level
*lvl
;
5769 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
5770 consider_binding_level (name
, bm
, lvl
, true, kind
);
5772 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
5773 consider_binding_level (name
, bm
, lvl
, false, kind
);
5775 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5777 x = SOME_OTHER_MACRO (y);
5778 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5779 as a misspelled identifier.
5781 Use the best distance so far so that a candidate is only set if
5782 a macro is better than anything so far. This allows early rejection
5783 (without calculating the edit distance) of macro names that must have
5784 distance >= bm.get_best_distance (), and means that we only get a
5785 non-NULL result for best_macro_match if it's better than any of
5786 the identifiers already checked. */
5787 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
5788 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
5789 /* If a macro is the closest so far to NAME, consider it. */
5791 bm
.consider ((const char *)best_macro
->ident
.str
);
5792 else if (bmm
.get_best_distance () == 0)
5794 /* If we have an exact match for a macro name, then the
5795 macro has been used before it was defined. */
5796 cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ();
5797 if (macro
&& (macro
->flags
& NODE_BUILTIN
) == 0)
5798 return name_hint (NULL
,
5799 new macro_use_before_def (loc
, macro
));
5802 /* Try the "starts_decl_specifier_p" keywords to detect
5803 "singed" vs "signed" typos. */
5804 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
5806 const c_common_resword
*resword
= &c_common_reswords
[i
];
5808 if (!suggest_rid_p (resword
->rid
))
5811 tree resword_identifier
= ridpointers
[resword
->rid
];
5812 if (!resword_identifier
)
5814 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
5816 /* Only consider reserved words that survived the
5817 filtering in init_reswords (e.g. for -std). */
5818 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
5821 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
5824 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
5827 /* Subroutine of outer_binding.
5829 Returns TRUE if BINDING is a binding to a template parameter of
5830 SCOPE. In that case SCOPE is the scope of a primary template
5831 parameter -- in the sense of G++, i.e, a template that has its own
5834 Returns FALSE otherwise. */
5837 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
5838 cp_binding_level
*scope
)
5840 tree binding_value
, tmpl
, tinfo
;
5843 if (!binding
|| !scope
|| !scope
->this_entity
)
5846 binding_value
= binding
->value
? binding
->value
: binding
->type
;
5847 tinfo
= get_template_info (scope
->this_entity
);
5849 /* BINDING_VALUE must be a template parm. */
5850 if (binding_value
== NULL_TREE
5851 || (!DECL_P (binding_value
)
5852 || !DECL_TEMPLATE_PARM_P (binding_value
)))
5855 /* The level of BINDING_VALUE. */
5857 template_type_parameter_p (binding_value
)
5858 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5859 (TREE_TYPE (binding_value
)))
5860 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
5862 /* The template of the current scope, iff said scope is a primary
5865 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
5866 ? TI_TEMPLATE (tinfo
)
5869 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5870 then BINDING_VALUE is a parameter of TMPL. */
5871 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
5874 /* Return the innermost non-namespace binding for NAME from a scope
5875 containing BINDING, or, if BINDING is NULL, the current scope.
5876 Please note that for a given template, the template parameters are
5877 considered to be in the scope containing the current scope.
5878 If CLASS_P is false, then class bindings are ignored. */
5881 outer_binding (tree name
,
5882 cxx_binding
*binding
,
5886 cp_binding_level
*scope
;
5887 cp_binding_level
*outer_scope
;
5891 scope
= binding
->scope
->level_chain
;
5892 outer
= binding
->previous
;
5896 scope
= current_binding_level
;
5897 outer
= IDENTIFIER_BINDING (name
);
5899 outer_scope
= outer
? outer
->scope
: NULL
;
5901 /* Because we create class bindings lazily, we might be missing a
5902 class binding for NAME. If there are any class binding levels
5903 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5904 declared, we must lookup NAME in those class scopes. */
5906 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
5908 if (scope
->kind
== sk_class
)
5910 cxx_binding
*class_binding
;
5912 class_binding
= get_class_binding (name
, scope
);
5915 /* Thread this new class-scope binding onto the
5916 IDENTIFIER_BINDING list so that future lookups
5918 class_binding
->previous
= outer
;
5920 binding
->previous
= class_binding
;
5922 IDENTIFIER_BINDING (name
) = class_binding
;
5923 return class_binding
;
5926 /* If we are in a member template, the template parms of the member
5927 template are considered to be inside the scope of the containing
5928 class, but within G++ the class bindings are all pushed between the
5929 template parms and the function body. So if the outer binding is
5930 a template parm for the current scope, return it now rather than
5931 look for a class binding. */
5932 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
5933 && binding_to_template_parms_of_scope_p (outer
, scope
))
5936 scope
= scope
->level_chain
;
5942 /* Return the innermost block-scope or class-scope value binding for
5943 NAME, or NULL_TREE if there is no such binding. */
5946 innermost_non_namespace_value (tree name
)
5948 cxx_binding
*binding
;
5949 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
5950 return binding
? binding
->value
: NULL_TREE
;
5953 /* Look up NAME in the current binding level and its superiors in the
5954 namespace of variables, functions and typedefs. Return a ..._DECL
5955 node of some kind representing its definition if there is only one
5956 such declaration, or return a TREE_LIST with all the overloaded
5957 definitions if there are many, or return 0 if it is undefined.
5958 Hidden name, either friend declaration or built-in function, are
5961 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5962 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5963 Otherwise we prefer non-TYPE_DECLs.
5965 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5966 BLOCK_P is false, bindings in block scopes are ignored. */
5969 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5970 int namespaces_only
, int flags
)
5973 tree val
= NULL_TREE
;
5975 query_oracle (name
);
5977 /* Conversion operators are handled specially because ordinary
5978 unqualified name lookup will not find template conversion
5980 if (IDENTIFIER_CONV_OP_P (name
))
5982 cp_binding_level
*level
;
5984 for (level
= current_binding_level
;
5985 level
&& level
->kind
!= sk_namespace
;
5986 level
= level
->level_chain
)
5991 /* A conversion operator can only be declared in a class
5993 if (level
->kind
!= sk_class
)
5996 /* Lookup the conversion operator in the class. */
5997 class_type
= level
->this_entity
;
5998 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
6006 flags
|= lookup_flags (prefer_type
, namespaces_only
);
6008 /* First, look in non-namespace scopes. */
6010 if (current_class_type
== NULL_TREE
)
6013 if (block_p
|| !nonclass
)
6014 for (iter
= outer_binding (name
, NULL
, !nonclass
);
6016 iter
= outer_binding (name
, iter
, !nonclass
))
6020 /* Skip entities we don't want. */
6021 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
6024 /* If this is the kind of thing we're looking for, we're done. */
6025 if (qualify_lookup (iter
->value
, flags
))
6026 binding
= iter
->value
;
6027 else if ((flags
& LOOKUP_PREFER_TYPES
)
6028 && qualify_lookup (iter
->type
, flags
))
6029 binding
= iter
->type
;
6031 binding
= NULL_TREE
;
6035 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
6037 /* A non namespace-scope binding can only be hidden in the
6038 presence of a local class, due to friend declarations.
6040 In particular, consider:
6048 B* b; // error: B is hidden
6049 C* c; // OK, finds ::C
6052 B *b; // error: B is hidden
6053 C *c; // OK, finds ::C
6058 The standard says that "B" is a local class in "f"
6059 (but not nested within "A") -- but that name lookup
6060 for "B" does not find this declaration until it is
6061 declared directly with "f".
6067 If a friend declaration appears in a local class and
6068 the name specified is an unqualified name, a prior
6069 declaration is looked up without considering scopes
6070 that are outside the innermost enclosing non-class
6071 scope. For a friend function declaration, if there is
6072 no prior declaration, the program is ill-formed. For a
6073 friend class declaration, if there is no prior
6074 declaration, the class that is specified belongs to the
6075 innermost enclosing non-class scope, but if it is
6076 subsequently referenced, its name is not found by name
6077 lookup until a matching declaration is provided in the
6078 innermost enclosing nonclass scope.
6080 So just keep looking for a non-hidden binding.
6082 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
6090 /* Now lookup in namespace scopes. */
6093 name_lookup
lookup (name
, flags
);
6094 if (lookup
.search_unqualified
6095 (current_decl_namespace (), current_binding_level
))
6099 /* If we have a single function from a using decl, pull it out. */
6100 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6101 val
= OVL_FUNCTION (val
);
6106 /* Wrapper for lookup_name_real_1. */
6109 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6110 int namespaces_only
, int flags
)
6113 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6114 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
6115 namespaces_only
, flags
);
6116 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6121 lookup_name_nonclass (tree name
)
6123 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
6127 lookup_name (tree name
)
6129 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
6133 lookup_name_prefer_type (tree name
, int prefer_type
)
6135 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6138 /* Look up NAME for type used in elaborated name specifier in
6139 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6140 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6141 name, more scopes are checked if cleanup or template parameter
6142 scope is encountered.
6144 Unlike lookup_name_real, we make sure that NAME is actually
6145 declared in the desired scope, not from inheritance, nor using
6146 directive. For using declaration, there is DR138 still waiting
6147 to be resolved. Hidden name coming from an earlier friend
6148 declaration is also returned.
6150 A TYPE_DECL best matching the NAME is returned. Catching error
6151 and issuing diagnostics are caller's responsibility. */
6154 lookup_type_scope_1 (tree name
, tag_scope scope
)
6156 cxx_binding
*iter
= NULL
;
6157 tree val
= NULL_TREE
;
6158 cp_binding_level
*level
= NULL
;
6160 /* Look in non-namespace scope first. */
6161 if (current_binding_level
->kind
!= sk_namespace
)
6162 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
6163 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
6165 /* Check if this is the kind of thing we're looking for.
6166 If SCOPE is TS_CURRENT, also make sure it doesn't come from
6167 base class. For ITER->VALUE, we can simply use
6168 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6171 We check ITER->TYPE before ITER->VALUE in order to handle
6172 typedef struct C {} C;
6175 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
6176 && (scope
!= ts_current
6177 || LOCAL_BINDING_P (iter
)
6178 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
6180 else if ((scope
!= ts_current
6181 || !INHERITED_VALUE_BINDING_P (iter
))
6182 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
6189 /* Look in namespace scope. */
6191 level
= iter
->scope
;
6194 tree ns
= current_decl_namespace ();
6196 if (tree
*slot
= find_namespace_slot (ns
, name
))
6198 /* If this is the kind of thing we're looking for, we're done. */
6199 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6200 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6204 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6205 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6208 level
= NAMESPACE_LEVEL (ns
);
6212 /* Type found, check if it is in the allowed scopes, ignoring cleanup
6213 and template parameter scopes. */
6216 cp_binding_level
*b
= current_binding_level
;
6222 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
6223 || b
->kind
== sk_function_parms
)
6225 else if (b
->kind
== sk_class
6226 && scope
== ts_within_enclosing_non_class
)
6236 /* Wrapper for lookup_type_scope_1. */
6239 lookup_type_scope (tree name
, tag_scope scope
)
6242 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6243 ret
= lookup_type_scope_1 (name
, scope
);
6244 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6248 /* Returns true iff DECL is a block-scope extern declaration of a function
6252 is_local_extern (tree decl
)
6254 cxx_binding
*binding
;
6256 /* For functions, this is easy. */
6257 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6258 return DECL_LOCAL_FUNCTION_P (decl
);
6262 if (!current_function_decl
)
6265 /* For variables, this is not easy. We need to look at the binding stack
6266 for the identifier to see whether the decl we have is a local. */
6267 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6268 binding
&& binding
->scope
->kind
!= sk_namespace
;
6269 binding
= binding
->previous
)
6270 if (binding
->value
== decl
)
6271 return LOCAL_BINDING_P (binding
);
6276 /* The type TYPE is being declared. If it is a class template, or a
6277 specialization of a class template, do any processing required and
6278 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6279 being declared a friend. B is the binding level at which this TYPE
6282 Returns the TYPE_DECL for TYPE, which may have been altered by this
6286 maybe_process_template_type_declaration (tree type
, int is_friend
,
6287 cp_binding_level
*b
)
6289 tree decl
= TYPE_NAME (type
);
6291 if (processing_template_parmlist
)
6292 /* You can't declare a new template type in a template parameter
6293 list. But, you can declare a non-template type:
6295 template <class A*> struct S;
6297 is a forward-declaration of `A'. */
6299 else if (b
->kind
== sk_namespace
6300 && current_binding_level
->kind
!= sk_namespace
)
6301 /* If this new type is being injected into a containing scope,
6302 then it's not a template type. */
6306 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6307 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6309 if (processing_template_decl
)
6311 /* This may change after the call to
6312 push_template_decl_real, but we want the original value. */
6313 tree name
= DECL_NAME (decl
);
6315 decl
= push_template_decl_real (decl
, is_friend
);
6316 if (decl
== error_mark_node
)
6317 return error_mark_node
;
6319 /* If the current binding level is the binding level for the
6320 template parameters (see the comment in
6321 begin_template_parm_list) and the enclosing level is a class
6322 scope, and we're not looking at a friend, push the
6323 declaration of the member class into the class scope. In the
6324 friend case, push_template_decl will already have put the
6325 friend into global scope, if appropriate. */
6326 if (TREE_CODE (type
) != ENUMERAL_TYPE
6327 && !is_friend
&& b
->kind
== sk_template_parms
6328 && b
->level_chain
->kind
== sk_class
)
6330 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6332 if (!COMPLETE_TYPE_P (current_class_type
))
6334 maybe_add_class_template_decl_list (current_class_type
,
6335 type
, /*friend_p=*/0);
6336 /* Put this UTD in the table of UTDs for the class. */
6337 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6338 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6339 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6341 binding_table_insert
6342 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6351 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6352 that the NAME is a class template, the tag is processed but not pushed.
6354 The pushed scope depend on the SCOPE parameter:
6355 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6357 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6358 non-template-parameter scope. This case is needed for forward
6360 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6361 TS_GLOBAL case except that names within template-parameter scopes
6362 are not pushed at all.
6364 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6367 do_pushtag (tree name
, tree type
, tag_scope scope
)
6371 cp_binding_level
*b
= current_binding_level
;
6372 while (/* Cleanup scopes are not scopes from the point of view of
6374 b
->kind
== sk_cleanup
6375 /* Neither are function parameter scopes. */
6376 || b
->kind
== sk_function_parms
6377 /* Neither are the scopes used to hold template parameters
6378 for an explicit specialization. For an ordinary template
6379 declaration, these scopes are not scopes from the point of
6380 view of the language. */
6381 || (b
->kind
== sk_template_parms
6382 && (b
->explicit_spec_p
|| scope
== ts_global
))
6383 || (b
->kind
== sk_class
6384 && (scope
!= ts_current
6385 /* We may be defining a new type in the initializer
6386 of a static member variable. We allow this when
6387 not pedantic, and it is particularly useful for
6388 type punning via an anonymous union. */
6389 || COMPLETE_TYPE_P (b
->this_entity
))))
6392 gcc_assert (identifier_p (name
));
6394 /* Do C++ gratuitous typedefing. */
6395 if (identifier_type_value_1 (name
) != type
)
6399 tree context
= TYPE_CONTEXT (type
);
6403 tree cs
= current_scope ();
6405 if (scope
== ts_current
6406 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6408 else if (cs
&& TYPE_P (cs
))
6409 /* When declaring a friend class of a local class, we want
6410 to inject the newly named class into the scope
6411 containing the local class, not the namespace
6413 context
= decl_function_context (get_type_decl (cs
));
6416 context
= current_namespace
;
6418 if (b
->kind
== sk_class
6419 || (b
->kind
== sk_template_parms
6420 && b
->level_chain
->kind
== sk_class
))
6423 tdef
= create_implicit_typedef (name
, type
);
6424 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6425 if (scope
== ts_within_enclosing_non_class
)
6427 /* This is a friend. Make this TYPE_DECL node hidden from
6428 ordinary name lookup. Its corresponding TEMPLATE_DECL
6429 will be marked in push_template_decl_real. */
6430 retrofit_lang_decl (tdef
);
6431 DECL_ANTICIPATED (tdef
) = 1;
6432 DECL_FRIEND_P (tdef
) = 1;
6435 decl
= maybe_process_template_type_declaration
6436 (type
, scope
== ts_within_enclosing_non_class
, b
);
6437 if (decl
== error_mark_node
)
6440 if (b
->kind
== sk_class
)
6442 if (!TYPE_BEING_DEFINED (current_class_type
))
6443 return error_mark_node
;
6445 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6446 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6447 class. But if it's a member template class, we want
6448 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6450 finish_member_declaration (decl
);
6452 pushdecl_class_level (decl
);
6454 else if (b
->kind
!= sk_template_parms
)
6456 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6457 if (decl
== error_mark_node
)
6460 if (DECL_CONTEXT (decl
) == std_node
6461 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6462 && !CLASSTYPE_TEMPLATE_INFO (type
))
6464 error ("declaration of std::initializer_list does not match "
6465 "#include <initializer_list>, isn't a template");
6466 return error_mark_node
;
6471 set_identifier_type_value_with_scope (name
, tdef
, b
);
6473 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6475 /* If this is a local class, keep track of it. We need this
6476 information for name-mangling, and so that it is possible to
6477 find all function definitions in a translation unit in a
6478 convenient way. (It's otherwise tricky to find a member
6479 function definition it's only pointed to from within a local
6481 if (TYPE_FUNCTION_SCOPE_P (type
))
6483 if (processing_template_decl
)
6485 /* Push a DECL_EXPR so we call pushtag at the right time in
6486 template instantiation rather than in some nested context. */
6487 add_decl_expr (decl
);
6489 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6490 else if (!LAMBDA_TYPE_P (type
))
6491 vec_safe_push (local_classes
, type
);
6495 if (b
->kind
== sk_class
6496 && !COMPLETE_TYPE_P (current_class_type
))
6498 maybe_add_class_template_decl_list (current_class_type
,
6499 type
, /*friend_p=*/0);
6501 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6502 CLASSTYPE_NESTED_UTDS (current_class_type
)
6503 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6505 binding_table_insert
6506 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6509 decl
= TYPE_NAME (type
);
6510 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6512 /* Set type visibility now if this is a forward declaration. */
6513 TREE_PUBLIC (decl
) = 1;
6514 determine_visibility (decl
);
6519 /* Wrapper for do_pushtag. */
6522 pushtag (tree name
, tree type
, tag_scope scope
)
6525 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6526 ret
= do_pushtag (name
, type
, scope
);
6527 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6532 /* Subroutines for reverting temporarily to top-level for instantiation
6533 of templates and such. We actually need to clear out the class- and
6534 local-value slots of all identifiers, so that only the global values
6535 are at all visible. Simply setting current_binding_level to the global
6536 scope isn't enough, because more binding levels may be pushed. */
6537 struct saved_scope
*scope_chain
;
6539 /* Return true if ID has not already been marked. */
6542 store_binding_p (tree id
)
6544 if (!id
|| !IDENTIFIER_BINDING (id
))
6547 if (IDENTIFIER_MARKED (id
))
6553 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
6554 have enough space reserved. */
6557 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6559 cxx_saved_binding saved
;
6561 gcc_checking_assert (store_binding_p (id
));
6563 IDENTIFIER_MARKED (id
) = 1;
6565 saved
.identifier
= id
;
6566 saved
.binding
= IDENTIFIER_BINDING (id
);
6567 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
6568 (*old_bindings
)->quick_push (saved
);
6569 IDENTIFIER_BINDING (id
) = NULL
;
6573 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6575 static vec
<tree
> bindings_need_stored
;
6579 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6580 for (t
= names
; t
; t
= TREE_CHAIN (t
))
6582 if (TREE_CODE (t
) == TREE_LIST
)
6583 id
= TREE_PURPOSE (t
);
6587 if (store_binding_p (id
))
6588 bindings_need_stored
.safe_push (id
);
6590 if (!bindings_need_stored
.is_empty ())
6592 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6593 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6595 /* We can apparently have duplicates in NAMES. */
6596 if (store_binding_p (id
))
6597 store_binding (id
, old_bindings
);
6599 bindings_need_stored
.truncate (0);
6601 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6604 /* Like store_bindings, but NAMES is a vector of cp_class_binding
6605 objects, rather than a TREE_LIST. */
6608 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
6609 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
6611 static vec
<tree
> bindings_need_stored
;
6613 cp_class_binding
*cb
;
6615 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
6616 if (store_binding_p (cb
->identifier
))
6617 bindings_need_stored
.safe_push (cb
->identifier
);
6618 if (!bindings_need_stored
.is_empty ())
6621 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
6622 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
6623 store_binding (id
, old_bindings
);
6624 bindings_need_stored
.truncate (0);
6628 /* A chain of saved_scope structures awaiting reuse. */
6630 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
6633 do_push_to_top_level (void)
6635 struct saved_scope
*s
;
6636 cp_binding_level
*b
;
6637 cxx_saved_binding
*sb
;
6641 /* Reuse or create a new structure for this saved scope. */
6642 if (free_saved_scope
!= NULL
)
6644 s
= free_saved_scope
;
6645 free_saved_scope
= s
->prev
;
6647 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
6648 memset (s
, 0, sizeof (*s
));
6649 /* Also reuse the structure's old_bindings vector. */
6650 vec_safe_truncate (old_bindings
, 0);
6651 s
->old_bindings
= old_bindings
;
6654 s
= ggc_cleared_alloc
<saved_scope
> ();
6656 b
= scope_chain
? current_binding_level
: 0;
6658 /* If we're in the middle of some function, save our state. */
6662 push_function_context ();
6667 if (scope_chain
&& previous_class_level
)
6668 store_class_bindings (previous_class_level
->class_shadowed
,
6671 /* Have to include the global scope, because class-scope decls
6672 aren't listed anywhere useful. */
6673 for (; b
; b
= b
->level_chain
)
6677 /* Template IDs are inserted into the global level. If they were
6678 inserted into namespace level, finish_file wouldn't find them
6679 when doing pending instantiations. Therefore, don't stop at
6680 namespace level, but continue until :: . */
6681 if (global_scope_p (b
))
6684 store_bindings (b
->names
, &s
->old_bindings
);
6685 /* We also need to check class_shadowed to save class-level type
6686 bindings, since pushclass doesn't fill in b->names. */
6687 if (b
->kind
== sk_class
)
6688 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
6690 /* Unwind type-value slots back to top level. */
6691 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
6692 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
6695 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
6696 IDENTIFIER_MARKED (sb
->identifier
) = 0;
6698 s
->prev
= scope_chain
;
6700 s
->need_pop_function_context
= need_pop
;
6701 s
->function_decl
= current_function_decl
;
6702 s
->unevaluated_operand
= cp_unevaluated_operand
;
6703 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
6704 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
6707 current_function_decl
= NULL_TREE
;
6708 vec_alloc (current_lang_base
, 10);
6709 current_lang_name
= lang_name_cplusplus
;
6710 current_namespace
= global_namespace
;
6711 push_class_stack ();
6712 cp_unevaluated_operand
= 0;
6713 c_inhibit_evaluation_warnings
= 0;
6717 do_pop_from_top_level (void)
6719 struct saved_scope
*s
= scope_chain
;
6720 cxx_saved_binding
*saved
;
6723 /* Clear out class-level bindings cache. */
6724 if (previous_class_level
)
6725 invalidate_class_lookup_cache ();
6728 current_lang_base
= 0;
6730 scope_chain
= s
->prev
;
6731 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
6733 tree id
= saved
->identifier
;
6735 IDENTIFIER_BINDING (id
) = saved
->binding
;
6736 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
6739 /* If we were in the middle of compiling a function, restore our
6741 if (s
->need_pop_function_context
)
6742 pop_function_context ();
6743 current_function_decl
= s
->function_decl
;
6744 cp_unevaluated_operand
= s
->unevaluated_operand
;
6745 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
6747 /* Make this saved_scope structure available for reuse by
6748 push_to_top_level. */
6749 s
->prev
= free_saved_scope
;
6750 free_saved_scope
= s
;
6753 /* Push into the scope of the namespace NS, even if it is deeply
6754 nested within another namespace. */
6757 do_push_nested_namespace (tree ns
)
6759 if (ns
== global_namespace
)
6760 do_push_to_top_level ();
6763 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
6765 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
6766 resume_scope (NAMESPACE_LEVEL (ns
));
6767 current_namespace
= ns
;
6771 /* Pop back from the scope of the namespace NS, which was previously
6772 entered with push_nested_namespace. */
6775 do_pop_nested_namespace (tree ns
)
6777 while (ns
!= global_namespace
)
6779 ns
= CP_DECL_CONTEXT (ns
);
6780 current_namespace
= ns
;
6784 do_pop_from_top_level ();
6787 /* Add TARGET to USINGS, if it does not already exist there.
6788 We used to build the complete graph of usings at this point, from
6789 the POV of the source namespaces. Now we build that as we perform
6790 the unqualified search. */
6793 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
6796 for (unsigned ix
= usings
->length (); ix
--;)
6797 if ((*usings
)[ix
] == target
)
6800 vec_safe_push (usings
, target
);
6803 /* Tell the debug system of a using directive. */
6806 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
6808 /* Emit debugging info. */
6809 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
6810 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
6814 /* Process a namespace-scope using directive. */
6817 finish_namespace_using_directive (tree target
, tree attribs
)
6819 gcc_checking_assert (namespace_bindings_p ());
6820 if (target
== error_mark_node
)
6823 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6824 ORIGINAL_NAMESPACE (target
));
6825 emit_debug_info_using_namespace (current_namespace
,
6826 ORIGINAL_NAMESPACE (target
), false);
6828 if (attribs
== error_mark_node
)
6831 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
6833 tree name
= get_attribute_name (a
);
6834 if (is_attribute_p ("strong", name
))
6836 warning (0, "strong using directive no longer supported");
6837 if (CP_DECL_CONTEXT (target
) == current_namespace
)
6838 inform (DECL_SOURCE_LOCATION (target
),
6839 "you may use an inline namespace instead");
6842 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
6846 /* Process a function-scope using-directive. */
6849 finish_local_using_directive (tree target
, tree attribs
)
6851 gcc_checking_assert (local_bindings_p ());
6852 if (target
== error_mark_node
)
6856 warning (OPT_Wattributes
, "attributes ignored on local using directive");
6858 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
6860 add_using_namespace (current_binding_level
->using_directives
,
6861 ORIGINAL_NAMESPACE (target
));
6864 /* Pushes X into the global namespace. */
6867 pushdecl_top_level (tree x
, bool is_friend
)
6869 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6870 do_push_to_top_level ();
6871 x
= pushdecl_namespace_level (x
, is_friend
);
6872 do_pop_from_top_level ();
6873 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6877 /* Pushes X into the global namespace and calls cp_finish_decl to
6878 register the variable, initializing it with INIT. */
6881 pushdecl_top_level_and_finish (tree x
, tree init
)
6883 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6884 do_push_to_top_level ();
6885 x
= pushdecl_namespace_level (x
, false);
6886 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
6887 do_pop_from_top_level ();
6888 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6892 /* Enter the namespaces from current_namerspace to NS. */
6895 push_inline_namespaces (tree ns
)
6898 if (ns
!= current_namespace
)
6900 gcc_assert (ns
!= global_namespace
);
6901 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6902 resume_scope (NAMESPACE_LEVEL (ns
));
6903 current_namespace
= ns
;
6909 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6910 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6911 we create an inline namespace (it is up to the caller to check upon
6912 redefinition). Return the number of namespaces entered. */
6915 push_namespace (tree name
, bool make_inline
)
6917 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6920 /* We should not get here if the global_namespace is not yet constructed
6921 nor if NAME designates the global namespace: The global scope is
6922 constructed elsewhere. */
6923 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
6925 tree ns
= NULL_TREE
;
6927 name_lookup
lookup (name
, 0);
6928 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
6930 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
6932 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
6934 /* A namespace alias is not allowed here, but if the alias
6935 is for a namespace also inside the current scope,
6936 accept it with a diagnostic. That's better than dying
6938 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
6940 error ("namespace alias %qD not allowed here, "
6941 "assuming %qD", lookup
.value
, dna
);
6949 bool new_ns
= false;
6951 /* DR2061. NS might be a member of an inline namespace. We
6952 need to push into those namespaces. */
6953 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6956 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
6957 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
6958 if (!SCOPE_DEPTH (ns
))
6959 /* We only allow depth 255. */
6960 sorry ("cannot nest more than %d namespaces",
6961 SCOPE_DEPTH (current_namespace
));
6962 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
6965 if (pushdecl (ns
) == error_mark_node
)
6971 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
6974 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6977 else if (TREE_PUBLIC (current_namespace
))
6978 TREE_PUBLIC (ns
) = 1;
6982 DECL_NAMESPACE_INLINE_P (ns
) = true;
6983 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
6986 if (!name
|| make_inline
)
6987 emit_debug_info_using_namespace (current_namespace
, ns
, true);
6993 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
6995 error ("inline namespace must be specified at initial definition");
6996 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
6999 begin_scope (sk_namespace
, ns
);
7001 resume_scope (NAMESPACE_LEVEL (ns
));
7002 current_namespace
= ns
;
7006 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7010 /* Pop from the scope of the current namespace. */
7013 pop_namespace (void)
7015 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7017 gcc_assert (current_namespace
!= global_namespace
);
7018 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
7019 /* The binding level is not popped, as it might be re-opened later. */
7022 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7025 /* External entry points for do_{push_to/pop_from}_top_level. */
7028 push_to_top_level (void)
7030 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7031 do_push_to_top_level ();
7032 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7036 pop_from_top_level (void)
7038 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7039 do_pop_from_top_level ();
7040 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7043 /* External entry points for do_{push,pop}_nested_namespace. */
7046 push_nested_namespace (tree ns
)
7048 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7049 do_push_nested_namespace (ns
);
7050 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7054 pop_nested_namespace (tree ns
)
7056 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7057 gcc_assert (current_namespace
== ns
);
7058 do_pop_nested_namespace (ns
);
7059 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7062 /* Pop off extraneous binding levels left over due to syntax errors.
7063 We don't pop past namespaces, as they might be valid. */
7066 pop_everything (void)
7068 if (ENABLE_SCOPE_CHECKING
)
7069 verbatim ("XXX entering pop_everything ()\n");
7070 while (!namespace_bindings_p ())
7072 if (current_binding_level
->kind
== sk_class
)
7073 pop_nested_class ();
7077 if (ENABLE_SCOPE_CHECKING
)
7078 verbatim ("XXX leaving pop_everything ()\n");
7081 /* Emit debugging information for using declarations and directives.
7082 If input tree is overloaded fn then emit debug info for all
7086 cp_emit_debug_info_for_using (tree t
, tree context
)
7088 /* Don't try to emit any debug information if we have errors. */
7092 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7093 of a builtin function. */
7094 if (TREE_CODE (t
) == FUNCTION_DECL
7095 && DECL_EXTERNAL (t
)
7096 && DECL_BUILT_IN (t
))
7099 /* Do not supply context to imported_module_or_decl, if
7100 it is a global namespace. */
7101 if (context
== global_namespace
)
7102 context
= NULL_TREE
;
7104 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7106 /* FIXME: Handle TEMPLATE_DECLs. */
7107 for (lkp_iterator
iter (t
); iter
; ++iter
)
7110 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
7112 if (building_stmt_list_p ())
7113 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7115 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7121 #include "gt-cp-name-lookup.h"