1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2020 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"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
35 #include "c-family/name-hint.h"
36 #include "c-family/known-headers.h"
37 #include "c-family/c-spellcheck.h"
39 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
40 static cp_binding_level
*innermost_nonclass_level (void);
41 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
43 static name_hint
maybe_suggest_missing_std_header (location_t location
,
45 static name_hint
suggest_alternatives_for_1 (location_t location
, tree name
,
46 bool suggest_misspellings
);
48 /* Create an overload suitable for recording an artificial TYPE_DECL
49 and another decl. We use this machanism to implement the struct
50 stat hack within a namespace. It'd be nice to use it everywhere. */
52 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
53 #define STAT_TYPE(N) TREE_TYPE (N)
54 #define STAT_DECL(N) OVL_FUNCTION (N)
55 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
56 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
58 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
62 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
64 tree result
= make_node (OVERLOAD
);
66 /* Mark this as a lookup, so we can tell this is a stat hack. */
67 OVL_LOOKUP_P (result
) = true;
68 STAT_DECL (result
) = decl
;
69 STAT_TYPE (result
) = type
;
73 /* Create a local binding level for NAME. */
76 create_local_binding (cp_binding_level
*level
, tree name
)
78 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
80 INHERITED_VALUE_BINDING_P (binding
) = false;
81 LOCAL_BINDING_P (binding
) = true;
82 binding
->scope
= level
;
83 binding
->previous
= IDENTIFIER_BINDING (name
);
85 IDENTIFIER_BINDING (name
) = binding
;
90 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
91 make an empty binding if there wasn't one. */
94 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
96 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
97 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
98 create_p
? INSERT
: NO_INSERT
);
103 find_namespace_value (tree ns
, tree name
)
105 tree
*b
= find_namespace_slot (ns
, name
);
107 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
110 /* Add DECL to the list of things declared in B. */
113 add_decl_to_level (cp_binding_level
*b
, tree decl
)
115 gcc_assert (b
->kind
!= sk_class
);
117 /* Make sure we don't create a circular list. xref_tag can end
118 up pushing the same artificial decl more than once. We
119 should have already detected that in update_binding. */
120 gcc_assert (b
->names
!= decl
);
122 /* We build up the list in reverse order, and reverse it later if
124 TREE_CHAIN (decl
) = b
->names
;
127 /* If appropriate, add decl to separate list of statics. We
128 include extern variables because they might turn out to be
129 static later. It's OK for this list to contain a few false
131 if (b
->kind
== sk_namespace
133 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
134 || (TREE_CODE (decl
) == FUNCTION_DECL
135 && (!TREE_PUBLIC (decl
)
136 || decl_anon_ns_mem_p (decl
)
137 || DECL_DECLARED_INLINE_P (decl
)))))
138 vec_safe_push (static_decls
, decl
);
141 /* Find the binding for NAME in the local binding level B. */
144 find_local_binding (cp_binding_level
*b
, tree name
)
146 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
147 for (;; b
= b
->level_chain
)
149 if (binding
->scope
== b
)
152 /* Cleanup contours are transparent to the language. */
153 if (b
->kind
!= sk_cleanup
)
162 typedef std::pair
<tree
, tree
> using_pair
;
163 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
166 tree name
; /* The identifier being looked for. */
167 tree value
; /* A (possibly ambiguous) set of things found. */
168 tree type
; /* A type that has been found. */
169 int flags
; /* Lookup flags. */
170 bool deduping
; /* Full deduping is needed because using declarations
172 vec
<tree
, va_heap
, vl_embed
> *scopes
;
173 name_lookup
*previous
; /* Previously active lookup. */
176 /* Marked scope stack for outermost name lookup. */
177 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
178 /* Currently active lookup. */
179 static name_lookup
*active
;
182 name_lookup (tree n
, int f
= 0)
183 : name (n
), value (NULL_TREE
), type (NULL_TREE
), flags (f
),
184 deduping (false), scopes (NULL
), previous (NULL
)
193 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
194 name_lookup (const name_lookup
&);
195 name_lookup
&operator= (const name_lookup
&);
198 static bool seen_p (tree scope
)
200 return LOOKUP_SEEN_P (scope
);
202 static bool found_p (tree scope
)
204 return LOOKUP_FOUND_P (scope
);
207 void mark_seen (tree scope
); /* Mark and add to scope vector. */
208 static void mark_found (tree scope
)
210 gcc_checking_assert (seen_p (scope
));
211 LOOKUP_FOUND_P (scope
) = true;
213 bool see_and_mark (tree scope
)
215 bool ret
= seen_p (scope
);
220 bool find_and_mark (tree scope
);
223 void preserve_state ();
224 void restore_state ();
227 static tree
ambiguous (tree thing
, tree current
);
228 void add_overload (tree fns
);
229 void add_value (tree new_val
);
230 void add_type (tree new_type
);
231 bool process_binding (tree val_bind
, tree type_bind
);
233 /* Look in only namespace. */
234 bool search_namespace_only (tree scope
);
235 /* Look in namespace and its (recursive) inlines. Ignore using
236 directives. Return true if something found (inc dups). */
237 bool search_namespace (tree scope
);
238 /* Look in the using directives of namespace + inlines using
239 qualified lookup rules. */
240 bool search_usings (tree scope
);
243 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
244 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
245 vec
<tree
, va_gc
> *usings
);
246 using_queue
*queue_usings (using_queue
*queue
, int depth
,
247 vec
<tree
, va_gc
> *usings
)
250 queue
= do_queue_usings (queue
, depth
, usings
);
257 void adl_expr (tree
);
258 void adl_type (tree
);
259 void adl_template_arg (tree
);
260 void adl_class (tree
);
261 void adl_bases (tree
);
262 void adl_class_only (tree
);
263 void adl_namespace (tree
);
264 void adl_namespace_only (tree
);
267 /* Search namespace + inlines + maybe usings as qualified lookup. */
268 bool search_qualified (tree scope
, bool usings
= true);
270 /* Search namespace + inlines + usings as unqualified lookup. */
271 bool search_unqualified (tree scope
, cp_binding_level
*);
273 /* ADL lookup of ARGS. */
274 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
277 /* Scope stack shared by all outermost lookups. This avoids us
278 allocating and freeing on every single lookup. */
279 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
281 /* Currently active lookup. */
282 name_lookup
*name_lookup::active
;
284 /* Name lookup is recursive, becase ADL can cause template
285 instatiation. This is of course a rare event, so we optimize for
286 it not happening. When we discover an active name-lookup, which
287 must be an ADL lookup, we need to unmark the marked scopes and also
288 unmark the lookup we might have been accumulating. */
291 name_lookup::preserve_state ()
296 unsigned length
= vec_safe_length (previous
->scopes
);
297 vec_safe_reserve (previous
->scopes
, length
* 2);
298 for (unsigned ix
= length
; ix
--;)
300 tree decl
= (*previous
->scopes
)[ix
];
302 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
303 LOOKUP_SEEN_P (decl
) = false;
305 /* Preserve the FOUND_P state on the interrupted lookup's
307 if (LOOKUP_FOUND_P (decl
))
309 LOOKUP_FOUND_P (decl
) = false;
310 previous
->scopes
->quick_push (decl
);
314 /* Unmark the outer partial lookup. */
315 if (previous
->deduping
)
316 lookup_mark (previous
->value
, false);
319 scopes
= shared_scopes
;
323 /* Restore the marking state of a lookup we interrupted. */
326 name_lookup::restore_state ()
329 lookup_mark (value
, false);
331 /* Unmark and empty this lookup's scope stack. */
332 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
334 tree decl
= scopes
->pop ();
335 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
336 LOOKUP_SEEN_P (decl
) = false;
337 LOOKUP_FOUND_P (decl
) = false;
345 unsigned length
= vec_safe_length (previous
->scopes
);
346 for (unsigned ix
= 0; ix
!= length
; ix
++)
348 tree decl
= (*previous
->scopes
)[ix
];
349 if (LOOKUP_SEEN_P (decl
))
351 /* The remainder of the scope stack must be recording
352 FOUND_P decls, which we want to pop off. */
355 tree decl
= previous
->scopes
->pop ();
356 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
357 && !LOOKUP_FOUND_P (decl
));
358 LOOKUP_FOUND_P (decl
) = true;
360 while (++ix
!= length
);
364 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
365 LOOKUP_SEEN_P (decl
) = true;
368 /* Remark the outer partial lookup. */
369 if (previous
->deduping
)
370 lookup_mark (previous
->value
, true);
373 shared_scopes
= scopes
;
377 name_lookup::mark_seen (tree scope
)
379 gcc_checking_assert (!seen_p (scope
));
380 LOOKUP_SEEN_P (scope
) = true;
381 vec_safe_push (scopes
, scope
);
385 name_lookup::find_and_mark (tree scope
)
387 bool result
= LOOKUP_FOUND_P (scope
);
390 LOOKUP_FOUND_P (scope
) = true;
391 if (!LOOKUP_SEEN_P (scope
))
392 vec_safe_push (scopes
, scope
);
398 /* THING and CURRENT are ambiguous, concatenate them. */
401 name_lookup::ambiguous (tree thing
, tree current
)
403 if (TREE_CODE (current
) != TREE_LIST
)
405 current
= build_tree_list (NULL_TREE
, current
);
406 TREE_TYPE (current
) = error_mark_node
;
408 current
= tree_cons (NULL_TREE
, thing
, current
);
409 TREE_TYPE (current
) = error_mark_node
;
414 /* FNS is a new overload set to add to the exising set. */
417 name_lookup::add_overload (tree fns
)
419 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
422 if (flags
& LOOKUP_HIDDEN
)
423 probe
= ovl_skip_hidden (probe
);
424 if (probe
&& TREE_CODE (probe
) == OVERLOAD
425 && OVL_DEDUP_P (probe
))
427 /* We're about to add something found by a using
428 declaration, so need to engage deduping mode. */
429 lookup_mark (value
, true);
434 value
= lookup_maybe_add (fns
, value
, deduping
);
437 /* Add a NEW_VAL, a found value binding into the current value binding. */
440 name_lookup::add_value (tree new_val
)
442 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
443 add_overload (new_val
);
446 else if (value
== new_val
)
448 else if ((TREE_CODE (value
) == TYPE_DECL
449 && TREE_CODE (new_val
) == TYPE_DECL
450 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
451 /* Typedefs to the same type. */;
452 else if (TREE_CODE (value
) == NAMESPACE_DECL
453 && TREE_CODE (new_val
) == NAMESPACE_DECL
454 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
455 /* Namespace (possibly aliased) to the same namespace. Locate
457 value
= ORIGINAL_NAMESPACE (value
);
462 /* Disengage deduping mode. */
463 lookup_mark (value
, false);
466 value
= ambiguous (new_val
, value
);
470 /* Add a NEW_TYPE, a found type binding into the current type binding. */
473 name_lookup::add_type (tree new_type
)
477 else if (TREE_CODE (type
) == TREE_LIST
478 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
479 type
= ambiguous (new_type
, type
);
482 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
483 true if we actually found something noteworthy. */
486 name_lookup::process_binding (tree new_val
, tree new_type
)
488 /* Did we really see a type? */
490 && (LOOKUP_NAMESPACES_ONLY (flags
)
491 || (!(flags
& LOOKUP_HIDDEN
)
492 && DECL_LANG_SPECIFIC (new_type
)
493 && DECL_ANTICIPATED (new_type
))))
494 new_type
= NULL_TREE
;
496 if (new_val
&& !(flags
& LOOKUP_HIDDEN
))
497 new_val
= ovl_skip_hidden (new_val
);
499 /* Do we really see a value? */
501 switch (TREE_CODE (new_val
))
504 /* If we expect types or namespaces, and not templates,
505 or this is not a template class. */
506 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
507 && !DECL_TYPE_TEMPLATE_P (new_val
)))
511 if (LOOKUP_NAMESPACES_ONLY (flags
)
512 || (new_type
&& (flags
& LOOKUP_PREFER_TYPES
)))
516 if (LOOKUP_TYPES_ONLY (flags
))
520 if (LOOKUP_QUALIFIERS_ONLY (flags
))
527 new_type
= NULL_TREE
;
530 /* Merge into the lookup */
536 return new_val
!= NULL_TREE
;
539 /* Look in exactly namespace SCOPE. */
542 name_lookup::search_namespace_only (tree scope
)
546 if (tree
*binding
= find_namespace_slot (scope
, name
))
547 found
|= process_binding (MAYBE_STAT_DECL (*binding
),
548 MAYBE_STAT_TYPE (*binding
));
553 /* Conditionally look in namespace SCOPE and inline children. */
556 name_lookup::search_namespace (tree scope
)
558 if (see_and_mark (scope
))
559 /* We've visited this scope before. Return what we found then. */
560 return found_p (scope
);
562 /* Look in exactly namespace. */
563 bool found
= search_namespace_only (scope
);
565 /* Don't look into inline children, if we're looking for an
566 anonymous name -- it must be in the current scope, if anywhere. */
568 /* Recursively look in its inline children. */
569 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
570 for (unsigned ix
= inlinees
->length (); ix
--;)
571 found
|= search_namespace ((*inlinees
)[ix
]);
579 /* Recursively follow using directives of SCOPE & its inline children.
580 Such following is essentially a flood-fill algorithm. */
583 name_lookup::search_usings (tree scope
)
585 /* We do not check seen_p here, as that was already set during the
586 namespace_only walk. */
591 if (vec
<tree
, va_gc
> *usings
= NAMESPACE_LEVEL (scope
)->using_directives
)
592 for (unsigned ix
= usings
->length (); ix
--;)
593 found
|= search_qualified ((*usings
)[ix
], true);
595 /* Look in its inline children. */
596 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
597 for (unsigned ix
= inlinees
->length (); ix
--;)
598 found
|= search_usings ((*inlinees
)[ix
]);
606 /* Qualified namespace lookup in SCOPE.
607 1) Look in SCOPE (+inlines). If found, we're done.
608 2) Otherwise, if USINGS is true,
609 recurse for every using directive of SCOPE (+inlines).
611 Trickiness is (a) loops and (b) multiple paths to same namespace.
612 In both cases we want to not repeat any lookups, and know whether
613 to stop the caller's step #2. Do this via the FOUND_P marker. */
616 name_lookup::search_qualified (tree scope
, bool usings
)
621 found
= found_p (scope
);
624 found
= search_namespace (scope
);
625 if (!found
&& usings
)
626 found
= search_usings (scope
);
632 /* Add SCOPE to the unqualified search queue, recursively add its
633 inlines and those via using directives. */
635 name_lookup::using_queue
*
636 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
638 if (see_and_mark (scope
))
643 while (SCOPE_DEPTH (common
) > depth
)
644 common
= CP_DECL_CONTEXT (common
);
645 vec_safe_push (queue
, using_pair (common
, scope
));
647 /* Queue its inline children. */
648 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
649 for (unsigned ix
= inlinees
->length (); ix
--;)
650 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
652 /* Queue its using targets. */
653 queue
= queue_usings (queue
, depth
, NAMESPACE_LEVEL (scope
)->using_directives
);
658 /* Add the namespaces in USINGS to the unqualified search queue. */
660 name_lookup::using_queue
*
661 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
662 vec
<tree
, va_gc
> *usings
)
664 for (unsigned ix
= usings
->length (); ix
--;)
665 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
670 /* Unqualified namespace lookup in SCOPE.
671 1) add scope+inlins to worklist.
672 2) recursively add target of every using directive
673 3) for each worklist item where SCOPE is common ancestor, search it
674 4) if nothing find, scope=parent, goto 1. */
677 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
679 /* Make static to avoid continual reallocation. We're not
681 static using_queue
*queue
= NULL
;
683 int length
= vec_safe_length (queue
);
685 /* Queue local using-directives. */
686 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
687 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
689 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
691 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
692 int depth
= SCOPE_DEPTH (scope
);
694 /* Queue namespaces reachable from SCOPE. */
695 queue
= queue_namespace (queue
, depth
, scope
);
697 /* Search every queued namespace where SCOPE is the common
698 ancestor. Adjust the others. */
699 unsigned ix
= length
;
702 using_pair
&pair
= (*queue
)[ix
];
703 while (pair
.first
== scope
)
705 found
|= search_namespace_only (pair
.second
);
706 pair
= queue
->pop ();
707 if (ix
== queue
->length ())
710 /* The depth is the same as SCOPE, find the parent scope. */
711 if (SCOPE_DEPTH (pair
.first
) == depth
)
712 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
715 while (ix
< queue
->length ());
717 if (scope
== global_namespace
)
720 /* If looking for hidden names, we only look in the innermost
721 namespace scope. [namespace.memdef]/3 If a friend
722 declaration in a non-local class first declares a class,
723 function, class template or function template the friend is a
724 member of the innermost enclosing namespace. See also
725 [basic.lookup.unqual]/7 */
726 if (flags
& LOOKUP_HIDDEN
)
730 vec_safe_truncate (queue
, length
);
735 /* FNS is a value binding. If it is a (set of overloaded) functions,
736 add them into the current value. */
739 name_lookup::add_fns (tree fns
)
743 else if (TREE_CODE (fns
) == OVERLOAD
)
745 if (TREE_TYPE (fns
) != unknown_type_node
)
746 fns
= OVL_FUNCTION (fns
);
748 else if (!DECL_DECLARES_FUNCTION_P (fns
))
754 /* Add functions of a namespace to the lookup structure. */
757 name_lookup::adl_namespace_only (tree scope
)
761 /* Look down into inline namespaces. */
762 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
763 for (unsigned ix
= inlinees
->length (); ix
--;)
764 adl_namespace_only ((*inlinees
)[ix
]);
766 if (tree fns
= find_namespace_value (scope
, name
))
767 add_fns (ovl_skip_hidden (fns
));
770 /* Find the containing non-inlined namespace, add it and all its
774 name_lookup::adl_namespace (tree scope
)
779 /* Find the containing non-inline namespace. */
780 while (DECL_NAMESPACE_INLINE_P (scope
))
781 scope
= CP_DECL_CONTEXT (scope
);
783 adl_namespace_only (scope
);
786 /* Adds the class and its friends to the lookup structure. */
789 name_lookup::adl_class_only (tree type
)
791 /* Backend-built structures, such as __builtin_va_list, aren't
792 affected by all this. */
793 if (!CLASS_TYPE_P (type
))
796 type
= TYPE_MAIN_VARIANT (type
);
798 if (see_and_mark (type
))
801 tree context
= decl_namespace_context (type
);
802 adl_namespace (context
);
804 complete_type (type
);
807 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
808 list
= TREE_CHAIN (list
))
809 if (name
== FRIEND_NAME (list
))
810 for (tree friends
= FRIEND_DECLS (list
); friends
;
811 friends
= TREE_CHAIN (friends
))
813 tree fn
= TREE_VALUE (friends
);
815 /* Only interested in global functions with potentially hidden
816 (i.e. unqualified) declarations. */
817 if (CP_DECL_CONTEXT (fn
) != context
)
820 /* Only interested in anticipated friends. (Non-anticipated
821 ones will have been inserted during the namespace
823 if (!DECL_ANTICIPATED (fn
))
826 /* Template specializations are never found by name lookup.
827 (Templates themselves can be found, but not template
829 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
836 /* Adds the class and its bases to the lookup structure.
837 Returns true on error. */
840 name_lookup::adl_bases (tree type
)
842 adl_class_only (type
);
844 /* Process baseclasses. */
845 if (tree binfo
= TYPE_BINFO (type
))
850 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
851 adl_bases (BINFO_TYPE (base_binfo
));
855 /* Adds everything associated with a class argument type to the lookup
856 structure. Returns true on error.
858 If T is a class type (including unions), its associated classes are: the
859 class itself; the class of which it is a member, if any; and its direct
860 and indirect base classes. Its associated namespaces are the namespaces
861 of which its associated classes are members. Furthermore, if T is a
862 class template specialization, its associated namespaces and classes
863 also include: the namespaces and classes associated with the types of
864 the template arguments provided for template type parameters (excluding
865 template template parameters); the namespaces of which any template
866 template arguments are members; and the classes of which any member
867 templates used as template template arguments are members. [ Note:
868 non-type template arguments do not contribute to the set of associated
869 namespaces. --end note] */
872 name_lookup::adl_class (tree type
)
874 /* Backend build structures, such as __builtin_va_list, aren't
875 affected by all this. */
876 if (!CLASS_TYPE_P (type
))
879 type
= TYPE_MAIN_VARIANT (type
);
880 /* We don't set found here because we have to have set seen first,
881 which is done in the adl_bases walk. */
888 if (TYPE_CLASS_SCOPE_P (type
))
889 adl_class_only (TYPE_CONTEXT (type
));
891 /* Process template arguments. */
892 if (CLASSTYPE_TEMPLATE_INFO (type
)
893 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
895 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
896 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
897 adl_template_arg (TREE_VEC_ELT (list
, i
));
902 name_lookup::adl_expr (tree expr
)
907 gcc_assert (!TYPE_P (expr
));
909 if (TREE_TYPE (expr
) != unknown_type_node
)
911 adl_type (unlowered_expr_type (expr
));
915 if (TREE_CODE (expr
) == ADDR_EXPR
)
916 expr
= TREE_OPERAND (expr
, 0);
917 if (TREE_CODE (expr
) == COMPONENT_REF
918 || TREE_CODE (expr
) == OFFSET_REF
)
919 expr
= TREE_OPERAND (expr
, 1);
920 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
923 for (lkp_iterator
iter (expr
); iter
; ++iter
)
924 adl_type (TREE_TYPE (*iter
));
925 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
927 /* The working paper doesn't currently say how to handle
928 template-id arguments. The sensible thing would seem to be
929 to handle the list of template candidates like a normal
930 overload set, and handle the template arguments like we do
931 for class template specializations. */
933 /* First the templates. */
934 adl_expr (TREE_OPERAND (expr
, 0));
936 /* Now the arguments. */
937 if (tree args
= TREE_OPERAND (expr
, 1))
938 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
939 adl_template_arg (TREE_VEC_ELT (args
, ix
));
944 name_lookup::adl_type (tree type
)
949 if (TYPE_PTRDATAMEM_P (type
))
951 /* Pointer to member: associate class type and value type. */
952 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
953 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
957 switch (TREE_CODE (type
))
960 if (TYPE_PTRMEMFUNC_P (type
))
962 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
971 /* The basetype is referenced in the first arg type, so just
974 /* Associate the parameter types. */
975 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
976 adl_type (TREE_VALUE (args
));
982 adl_type (TREE_TYPE (type
));
986 if (TYPE_CLASS_SCOPE_P (type
))
987 adl_class_only (TYPE_CONTEXT (type
));
988 adl_namespace (decl_namespace_context (type
));
992 gcc_assert (type
== unknown_type_node
993 || type
== init_list_type_node
);
996 case TYPE_PACK_EXPANSION
:
997 adl_type (PACK_EXPANSION_PATTERN (type
));
1005 /* Adds everything associated with a template argument to the lookup
1009 name_lookup::adl_template_arg (tree arg
)
1011 /* [basic.lookup.koenig]
1013 If T is a template-id, its associated namespaces and classes are
1014 ... the namespaces and classes associated with the types of the
1015 template arguments provided for template type parameters
1016 (excluding template template parameters); the namespaces in which
1017 any template template arguments are defined; and the classes in
1018 which any member templates used as template template arguments
1019 are defined. [Note: non-type template arguments do not
1020 contribute to the set of associated namespaces. ] */
1022 /* Consider first template template arguments. */
1023 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1024 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1026 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1028 tree ctx
= CP_DECL_CONTEXT (arg
);
1030 /* It's not a member template. */
1031 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1032 adl_namespace (ctx
);
1033 /* Otherwise, it must be member template. */
1035 adl_class_only (ctx
);
1037 /* It's an argument pack; handle it recursively. */
1038 else if (ARGUMENT_PACK_P (arg
))
1040 tree args
= ARGUMENT_PACK_ARGS (arg
);
1041 int i
, len
= TREE_VEC_LENGTH (args
);
1042 for (i
= 0; i
< len
; ++i
)
1043 adl_template_arg (TREE_VEC_ELT (args
, i
));
1045 /* It's not a template template argument, but it is a type template
1047 else if (TYPE_P (arg
))
1051 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1052 the call arguments. */
1055 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1060 lookup_mark (fns
, true);
1067 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1068 /* OMP reduction operators put an ADL-significant type as the
1080 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1081 static void consider_binding_level (tree name
,
1082 best_match
<tree
, const char *> &bm
,
1083 cp_binding_level
*lvl
,
1084 bool look_within_fields
,
1085 enum lookup_name_fuzzy_kind kind
);
1086 static void diagnose_name_conflict (tree
, tree
);
1088 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1089 don't add duplicates to it. ARGS is the vector of call
1090 arguments (which will not be empty). */
1093 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1095 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1096 name_lookup
lookup (name
);
1097 fns
= lookup
.search_adl (fns
, args
);
1098 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1102 /* FNS is an overload set of conversion functions. Return the
1103 overloads converting to TYPE. */
1106 extract_conversion_operator (tree fns
, tree type
)
1108 tree convs
= NULL_TREE
;
1109 tree tpls
= NULL_TREE
;
1111 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1113 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1114 convs
= lookup_add (*iter
, convs
);
1116 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1117 tpls
= lookup_add (*iter
, tpls
);
1126 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1129 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1131 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1133 unsigned mid
= (lo
+ hi
) / 2;
1134 tree binding
= (*member_vec
)[mid
];
1135 tree binding_name
= OVL_NAME (binding
);
1137 if (binding_name
> name
)
1139 else if (binding_name
< name
)
1148 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1151 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1153 for (int ix
= member_vec
->length (); ix
--;)
1154 if (tree binding
= (*member_vec
)[ix
])
1155 if (OVL_NAME (binding
) == name
)
1161 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1164 fields_linear_search (tree klass
, tree name
, bool want_type
)
1166 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1170 if (TREE_CODE (decl
) == FIELD_DECL
1171 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1173 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
, want_type
))
1177 if (DECL_NAME (decl
) != name
)
1180 if (TREE_CODE (decl
) == USING_DECL
)
1182 decl
= strip_using_decl (decl
);
1183 if (is_overloaded_fn (decl
))
1187 if (DECL_DECLARES_FUNCTION_P (decl
))
1188 /* Functions are found separately. */
1191 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1198 /* Look for NAME member inside of anonymous aggregate ANON. Although
1199 such things should only contain FIELD_DECLs, we check that too
1200 late, and would give very confusing errors if we weren't
1204 search_anon_aggr (tree anon
, tree name
, bool want_type
)
1206 gcc_assert (COMPLETE_TYPE_P (anon
));
1207 tree ret
= get_class_binding_direct (anon
, name
, want_type
);
1211 /* Look for NAME as an immediate member of KLASS (including
1212 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1213 regular search. >0 to get a type binding (if there is one) and <0
1214 if you want (just) the member function binding.
1216 Use this if you do not want lazy member creation. */
1219 get_class_binding_direct (tree klass
, tree name
, bool want_type
)
1221 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1223 /* Conversion operators can only be found by the marker conversion
1225 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1226 tree lookup
= conv_op
? conv_op_identifier
: name
;
1227 tree val
= NULL_TREE
;
1228 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1230 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1232 val
= member_vec_binary_search (member_vec
, lookup
);
1235 else if (STAT_HACK_P (val
))
1236 val
= want_type
? STAT_TYPE (val
) : STAT_DECL (val
);
1237 else if (want_type
&& !DECL_DECLARES_TYPE_P (val
))
1242 if (member_vec
&& !want_type
)
1243 val
= member_vec_linear_search (member_vec
, lookup
);
1245 if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_DEDUP_P (val
)))
1246 /* Dependent using declarations are a 'field', make sure we
1247 return that even if we saw an overload already. */
1248 if (tree field_val
= fields_linear_search (klass
, lookup
, want_type
))
1252 else if (TREE_CODE (field_val
) == USING_DECL
)
1253 val
= ovl_make (field_val
, val
);
1257 /* Extract the conversion operators asked for, unless the general
1258 conversion operator was requested. */
1261 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1262 val
= OVL_CHAIN (val
);
1263 if (tree type
= TREE_TYPE (name
))
1264 val
= extract_conversion_operator (val
, type
);
1270 /* Look for NAME's binding in exactly KLASS. See
1271 get_class_binding_direct for argument description. Does lazy
1272 special function creation as necessary. */
1275 get_class_binding (tree klass
, tree name
, bool want_type
/*=false*/)
1277 klass
= complete_type (klass
);
1279 if (COMPLETE_TYPE_P (klass
))
1281 /* Lazily declare functions, if we're going to search these. */
1282 if (IDENTIFIER_CTOR_P (name
))
1284 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1285 lazily_declare_fn (sfk_constructor
, klass
);
1286 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1287 lazily_declare_fn (sfk_copy_constructor
, klass
);
1288 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1289 lazily_declare_fn (sfk_move_constructor
, klass
);
1291 else if (IDENTIFIER_DTOR_P (name
))
1293 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1294 lazily_declare_fn (sfk_destructor
, klass
);
1296 else if (name
== assign_op_identifier
)
1298 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1299 lazily_declare_fn (sfk_copy_assignment
, klass
);
1300 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1301 lazily_declare_fn (sfk_move_assignment
, klass
);
1305 return get_class_binding_direct (klass
, name
, want_type
);
1308 /* Find the slot containing overloads called 'NAME'. If there is no
1309 such slot and the class is complete, create an empty one, at the
1310 correct point in the sorted member vector. Otherwise return NULL.
1311 Deals with conv_op marker handling. */
1314 find_member_slot (tree klass
, tree name
)
1316 bool complete_p
= COMPLETE_TYPE_P (klass
);
1318 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1321 vec_alloc (member_vec
, 8);
1322 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1325 /* If the class is complete but had no member_vec, we need
1326 to add the TYPE_FIELDS into it. We're also most likely
1327 to be adding ctors & dtors, so ask for 6 spare slots (the
1328 abstract cdtors and their clones). */
1329 set_class_bindings (klass
, 6);
1330 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1334 if (IDENTIFIER_CONV_OP_P (name
))
1335 name
= conv_op_identifier
;
1337 unsigned ix
, length
= member_vec
->length ();
1338 for (ix
= 0; ix
< length
; ix
++)
1340 tree
*slot
= &(*member_vec
)[ix
];
1341 tree fn_name
= OVL_NAME (*slot
);
1343 if (fn_name
== name
)
1345 /* If we found an existing slot, it must be a function set.
1346 Even with insertion after completion, because those only
1347 happen with artificial fns that have unspellable names.
1348 This means we do not have to deal with the stat hack
1350 gcc_checking_assert (OVL_P (*slot
));
1351 if (name
== conv_op_identifier
)
1353 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1354 /* Skip the conv-op marker. */
1355 slot
= &OVL_CHAIN (*slot
);
1360 if (complete_p
&& fn_name
> name
)
1364 /* No slot found, add one if the class is complete. */
1367 /* Do exact allocation, as we don't expect to add many. */
1368 gcc_assert (name
!= conv_op_identifier
);
1369 vec_safe_reserve_exact (member_vec
, 1);
1370 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1371 member_vec
->quick_insert (ix
, NULL_TREE
);
1372 return &(*member_vec
)[ix
];
1378 /* KLASS is an incomplete class to which we're adding a method NAME.
1379 Add a slot and deal with conv_op marker handling. */
1382 add_member_slot (tree klass
, tree name
)
1384 gcc_assert (!COMPLETE_TYPE_P (klass
));
1386 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1387 vec_safe_push (member_vec
, NULL_TREE
);
1388 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1390 tree
*slot
= &member_vec
->last ();
1391 if (IDENTIFIER_CONV_OP_P (name
))
1393 /* Install the marker prefix. */
1394 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1395 slot
= &OVL_CHAIN (*slot
);
1401 /* Comparison function to compare two MEMBER_VEC entries by name.
1402 Because we can have duplicates during insertion of TYPE_FIELDS, we
1403 do extra checking so deduping doesn't have to deal with so many
1407 member_name_cmp (const void *a_p
, const void *b_p
)
1409 tree a
= *(const tree
*)a_p
;
1410 tree b
= *(const tree
*)b_p
;
1411 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1412 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1414 gcc_checking_assert (name_a
&& name_b
);
1415 if (name_a
!= name_b
)
1416 return name_a
< name_b
? -1 : +1;
1418 if (name_a
== conv_op_identifier
)
1420 /* Strip the conv-op markers. */
1421 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1422 && OVL_FUNCTION (b
) == conv_op_marker
);
1427 if (TREE_CODE (a
) == OVERLOAD
)
1428 a
= OVL_FUNCTION (a
);
1429 if (TREE_CODE (b
) == OVERLOAD
)
1430 b
= OVL_FUNCTION (b
);
1432 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1433 if (TREE_CODE (a
) != TREE_CODE (b
))
1435 /* If one of them is a TYPE_DECL, it loses. */
1436 if (TREE_CODE (a
) == TYPE_DECL
)
1438 else if (TREE_CODE (b
) == TYPE_DECL
)
1441 /* If one of them is a USING_DECL, it loses. */
1442 if (TREE_CODE (a
) == USING_DECL
)
1444 else if (TREE_CODE (b
) == USING_DECL
)
1447 /* There are no other cases with different kinds of decls, as
1448 duplicate detection should have kicked in earlier. However,
1449 some erroneous cases get though. */
1450 gcc_assert (errorcount
);
1453 /* Using source location would be the best thing here, but we can
1454 get identically-located decls in the following circumstances:
1456 1) duplicate artificial type-decls for the same type.
1458 2) pack expansions of using-decls.
1460 We should not be doing #1, but in either case it doesn't matter
1461 how we order these. Use UID as a proxy for source ordering, so
1462 that identically-located decls still have a well-defined stable
1464 if (DECL_UID (a
) != DECL_UID (b
))
1465 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1466 gcc_assert (a
== b
);
1471 gt_pointer_operator new_value
;
1475 /* This routine compares two fields like member_name_cmp but using the
1476 pointer operator in resort_field_decl_data. We don't have to deal
1477 with duplicates here. */
1480 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1482 tree a
= *(const tree
*)a_p
;
1483 tree b
= *(const tree
*)b_p
;
1484 tree name_a
= OVL_NAME (a
);
1485 tree name_b
= OVL_NAME (b
);
1487 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1488 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1490 gcc_checking_assert (name_a
!= name_b
);
1492 return name_a
< name_b
? -1 : +1;
1495 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1498 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1499 gt_pointer_operator new_value
, void* cookie
)
1501 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1503 resort_data
.new_value
= new_value
;
1504 resort_data
.cookie
= cookie
;
1505 member_vec
->qsort (resort_member_name_cmp
);
1509 /* Recursively count the number of fields in KLASS, including anonymous
1513 count_class_fields (tree klass
)
1515 unsigned n_fields
= 0;
1517 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1518 if (DECL_DECLARES_FUNCTION_P (fields
))
1519 /* Functions are dealt with separately. */;
1520 else if (TREE_CODE (fields
) == FIELD_DECL
1521 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1522 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1523 else if (DECL_NAME (fields
))
1529 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1530 Recurse for anonymous members. MEMBER_VEC must have space. */
1533 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1535 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1536 if (DECL_DECLARES_FUNCTION_P (fields
))
1537 /* Functions are handled separately. */;
1538 else if (TREE_CODE (fields
) == FIELD_DECL
1539 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1540 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1541 else if (DECL_NAME (fields
))
1543 tree field
= fields
;
1544 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1545 if (TREE_CODE (field
) == USING_DECL
1546 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1547 field
= ovl_make (conv_op_marker
, field
);
1548 member_vec
->quick_push (field
);
1552 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1553 MEMBER_VEC must have space. */
1556 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1558 for (tree values
= TYPE_VALUES (enumtype
);
1559 values
; values
= TREE_CHAIN (values
))
1560 member_vec
->quick_push (TREE_VALUE (values
));
1563 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1564 DeDup adjacent DECLS of the same name. We already dealt with
1565 conflict resolution when adding the fields or methods themselves.
1566 There are three cases (which could all be combined):
1567 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1568 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1569 it wins. Otherwise the OVERLOAD does.
1570 3) two USING_DECLS. ...
1572 member_name_cmp will have ordered duplicates as
1573 <fns><using><type> */
1576 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1578 unsigned len
= member_vec
->length ();
1584 tree name
= OVL_NAME ((*member_vec
)[0]);
1585 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
1587 tree current
= NULL_TREE
;
1588 tree to_type
= NULL_TREE
;
1589 tree to_using
= NULL_TREE
;
1590 tree marker
= NULL_TREE
;
1592 for (jx
= ix
; jx
< len
; jx
++)
1594 tree next
= (*member_vec
)[jx
];
1597 tree next_name
= OVL_NAME (next
);
1598 if (next_name
!= name
)
1605 if (IDENTIFIER_CONV_OP_P (name
))
1608 next
= OVL_CHAIN (next
);
1611 if (TREE_CODE (next
) == USING_DECL
)
1613 if (IDENTIFIER_CTOR_P (name
))
1614 /* Dependent inherited ctor. */
1617 next
= strip_using_decl (next
);
1618 if (TREE_CODE (next
) == USING_DECL
)
1624 if (is_overloaded_fn (next
))
1628 if (DECL_DECLARES_TYPE_P (next
))
1643 current
= ovl_make (to_using
, current
);
1651 current
= stat_hack (current
, to_type
);
1658 OVL_CHAIN (marker
) = current
;
1661 (*member_vec
)[store
++] = current
;
1665 while (store
++ < len
)
1669 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1670 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1671 know there must be at least 1 field -- the self-reference
1672 TYPE_DECL, except for anon aggregates, which will have at least
1676 set_class_bindings (tree klass
, unsigned extra
)
1678 unsigned n_fields
= count_class_fields (klass
);
1679 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1681 if (member_vec
|| n_fields
>= 8)
1683 /* Append the new fields. */
1684 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1685 member_vec_append_class_fields (member_vec
, klass
);
1690 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1691 member_vec
->qsort (member_name_cmp
);
1692 member_vec_dedup (member_vec
);
1696 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1699 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1702 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1704 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1705 count them twice. */
1707 n_fields
= count_class_fields (klass
);
1709 n_fields
= list_length (TYPE_VALUES (enumtype
));
1711 if (member_vec
|| n_fields
>= 8)
1713 vec_safe_reserve_exact (member_vec
, n_fields
);
1714 if (CLASSTYPE_MEMBER_VEC (klass
))
1715 member_vec_append_enum_values (member_vec
, enumtype
);
1717 member_vec_append_class_fields (member_vec
, klass
);
1718 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1719 member_vec
->qsort (member_name_cmp
);
1720 member_vec_dedup (member_vec
);
1724 /* Compute the chain index of a binding_entry given the HASH value of its
1725 name and the total COUNT of chains. COUNT is assumed to be a power
1728 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1730 /* A free list of "binding_entry"s awaiting for re-use. */
1732 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1734 /* The binding oracle; see cp-tree.h. */
1736 cp_binding_oracle_function
*cp_binding_oracle
;
1738 /* If we have a binding oracle, ask it for all namespace-scoped
1739 definitions of NAME. */
1742 query_oracle (tree name
)
1744 if (!cp_binding_oracle
)
1747 /* LOOKED_UP holds the set of identifiers that we have already
1748 looked up with the oracle. */
1749 static hash_set
<tree
> looked_up
;
1750 if (looked_up
.add (name
))
1753 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1756 /* Create a binding_entry object for (NAME, TYPE). */
1758 static inline binding_entry
1759 binding_entry_make (tree name
, tree type
)
1761 binding_entry entry
;
1763 if (free_binding_entry
)
1765 entry
= free_binding_entry
;
1766 free_binding_entry
= entry
->chain
;
1769 entry
= ggc_alloc
<binding_entry_s
> ();
1773 entry
->chain
= NULL
;
1778 /* Put ENTRY back on the free list. */
1781 binding_entry_free (binding_entry entry
)
1785 entry
->chain
= free_binding_entry
;
1786 free_binding_entry
= entry
;
1790 /* The datatype used to implement the mapping from names to types at
1792 struct GTY(()) binding_table_s
{
1793 /* Array of chains of "binding_entry"s */
1794 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1796 /* The number of chains in this table. This is the length of the
1797 member "chain" considered as an array. */
1800 /* Number of "binding_entry"s in this table. */
1804 /* Construct TABLE with an initial CHAIN_COUNT. */
1807 binding_table_construct (binding_table table
, size_t chain_count
)
1809 table
->chain_count
= chain_count
;
1810 table
->entry_count
= 0;
1811 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1814 /* Make TABLE's entries ready for reuse. */
1817 binding_table_free (binding_table table
)
1825 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1827 binding_entry temp
= table
->chain
[i
];
1828 while (temp
!= NULL
)
1830 binding_entry entry
= temp
;
1831 temp
= entry
->chain
;
1832 binding_entry_free (entry
);
1834 table
->chain
[i
] = NULL
;
1836 table
->entry_count
= 0;
1840 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1842 static inline binding_table
1843 binding_table_new (size_t chain_count
)
1845 binding_table table
= ggc_alloc
<binding_table_s
> ();
1846 table
->chain
= NULL
;
1847 binding_table_construct (table
, chain_count
);
1851 /* Expand TABLE to twice its current chain_count. */
1854 binding_table_expand (binding_table table
)
1856 const size_t old_chain_count
= table
->chain_count
;
1857 const size_t old_entry_count
= table
->entry_count
;
1858 const size_t new_chain_count
= 2 * old_chain_count
;
1859 binding_entry
*old_chains
= table
->chain
;
1862 binding_table_construct (table
, new_chain_count
);
1863 for (i
= 0; i
< old_chain_count
; ++i
)
1865 binding_entry entry
= old_chains
[i
];
1866 for (; entry
!= NULL
; entry
= old_chains
[i
])
1868 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1869 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1871 old_chains
[i
] = entry
->chain
;
1872 entry
->chain
= table
->chain
[j
];
1873 table
->chain
[j
] = entry
;
1876 table
->entry_count
= old_entry_count
;
1879 /* Insert a binding for NAME to TYPE into TABLE. */
1882 binding_table_insert (binding_table table
, tree name
, tree type
)
1884 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1885 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1886 binding_entry entry
= binding_entry_make (name
, type
);
1888 entry
->chain
= table
->chain
[i
];
1889 table
->chain
[i
] = entry
;
1890 ++table
->entry_count
;
1892 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1893 binding_table_expand (table
);
1896 /* Return the binding_entry, if any, that maps NAME. */
1899 binding_table_find (binding_table table
, tree name
)
1901 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1902 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1904 while (entry
!= NULL
&& entry
->name
!= name
)
1905 entry
= entry
->chain
;
1910 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1913 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1921 chain_count
= table
->chain_count
;
1922 for (i
= 0; i
< chain_count
; ++i
)
1924 binding_entry entry
= table
->chain
[i
];
1925 for (; entry
!= NULL
; entry
= entry
->chain
)
1930 #ifndef ENABLE_SCOPE_CHECKING
1931 # define ENABLE_SCOPE_CHECKING 0
1933 # define ENABLE_SCOPE_CHECKING 1
1936 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1938 static GTY((deletable
)) cxx_binding
*free_bindings
;
1940 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1944 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1946 binding
->value
= value
;
1947 binding
->type
= type
;
1948 binding
->previous
= NULL
;
1951 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1953 static cxx_binding
*
1954 cxx_binding_make (tree value
, tree type
)
1956 cxx_binding
*binding
;
1959 binding
= free_bindings
;
1960 free_bindings
= binding
->previous
;
1963 binding
= ggc_alloc
<cxx_binding
> ();
1965 cxx_binding_init (binding
, value
, type
);
1970 /* Put BINDING back on the free list. */
1973 cxx_binding_free (cxx_binding
*binding
)
1975 binding
->scope
= NULL
;
1976 binding
->previous
= free_bindings
;
1977 free_bindings
= binding
;
1980 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1981 bindings) in the class scope indicated by SCOPE. */
1983 static cxx_binding
*
1984 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
1986 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
1987 cxx_binding
*binding
= cb
.base
;
1988 vec_safe_push (scope
->class_shadowed
, cb
);
1989 binding
->scope
= scope
;
1993 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1994 level at which this declaration is being bound. */
1997 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
1999 cxx_binding
*binding
;
2001 if (level
!= class_binding_level
)
2003 binding
= cxx_binding_make (decl
, NULL_TREE
);
2004 binding
->scope
= level
;
2007 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2009 /* Now, fill in the binding information. */
2010 binding
->previous
= IDENTIFIER_BINDING (id
);
2011 INHERITED_VALUE_BINDING_P (binding
) = 0;
2012 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2014 /* And put it on the front of the list of bindings for ID. */
2015 IDENTIFIER_BINDING (id
) = binding
;
2018 /* Remove the binding for DECL which should be the innermost binding
2022 pop_local_binding (tree id
, tree decl
)
2024 cxx_binding
*binding
;
2026 if (id
== NULL_TREE
)
2027 /* It's easiest to write the loops that call this function without
2028 checking whether or not the entities involved have names. We
2029 get here for such an entity. */
2032 /* Get the innermost binding for ID. */
2033 binding
= IDENTIFIER_BINDING (id
);
2035 /* The name should be bound. */
2036 gcc_assert (binding
!= NULL
);
2038 /* The DECL will be either the ordinary binding or the type
2039 binding for this identifier. Remove that binding. */
2040 if (binding
->value
== decl
)
2041 binding
->value
= NULL_TREE
;
2044 gcc_assert (binding
->type
== decl
);
2045 binding
->type
= NULL_TREE
;
2048 if (!binding
->value
&& !binding
->type
)
2050 /* We're completely done with the innermost binding for this
2051 identifier. Unhook it from the list of bindings. */
2052 IDENTIFIER_BINDING (id
) = binding
->previous
;
2054 /* Add it to the free list. */
2055 cxx_binding_free (binding
);
2059 /* Remove the bindings for the decls of the current level and leave
2060 the current scope. */
2063 pop_bindings_and_leave_scope (void)
2065 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2067 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2068 tree name
= OVL_NAME (decl
);
2070 pop_local_binding (name
, decl
);
2076 /* Strip non dependent using declarations. If DECL is dependent,
2077 surreptitiously create a typename_type and return it. */
2080 strip_using_decl (tree decl
)
2082 if (decl
== NULL_TREE
)
2085 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2086 decl
= USING_DECL_DECLS (decl
);
2088 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2089 && USING_DECL_TYPENAME_P (decl
))
2091 /* We have found a type introduced by a using
2092 declaration at class scope that refers to a dependent
2095 using typename :: [opt] nested-name-specifier unqualified-id ;
2097 decl
= make_typename_type (USING_DECL_SCOPE (decl
),
2099 typename_type
, tf_error
);
2100 if (decl
!= error_mark_node
)
2101 decl
= TYPE_NAME (decl
);
2107 /* Return true if OVL is an overload for an anticipated builtin. */
2110 anticipated_builtin_p (tree ovl
)
2112 if (TREE_CODE (ovl
) != OVERLOAD
)
2115 if (!OVL_HIDDEN_P (ovl
))
2118 tree fn
= OVL_FUNCTION (ovl
);
2119 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2121 if (DECL_HIDDEN_FRIEND_P (fn
))
2127 /* BINDING records an existing declaration for a name in the current scope.
2128 But, DECL is another declaration for that same identifier in the
2129 same scope. This is the `struct stat' hack whereby a non-typedef
2130 class name or enum-name can be bound at the same level as some other
2134 A class name (9.1) or enumeration name (7.2) can be hidden by the
2135 name of an object, function, or enumerator declared in the same scope.
2136 If a class or enumeration name and an object, function, or enumerator
2137 are declared in the same scope (in any order) with the same name, the
2138 class or enumeration name is hidden wherever the object, function, or
2139 enumerator name is visible.
2141 It's the responsibility of the caller to check that
2142 inserting this name is valid here. Returns nonzero if the new binding
2146 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2148 tree bval
= binding
->value
;
2150 tree target_bval
= strip_using_decl (bval
);
2151 tree target_decl
= strip_using_decl (decl
);
2153 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2154 && target_decl
!= target_bval
2155 && (TREE_CODE (target_bval
) != TYPE_DECL
2156 /* We allow pushing an enum multiple times in a class
2157 template in order to handle late matching of underlying
2158 type on an opaque-enum-declaration followed by an
2160 || (processing_template_decl
2161 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2162 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2163 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2164 (TREE_TYPE (target_decl
)))
2165 || dependent_type_p (ENUM_UNDERLYING_TYPE
2166 (TREE_TYPE (target_bval
)))))))
2167 /* The new name is the type name. */
2168 binding
->type
= decl
;
2169 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2170 an inherited type-binding out of the way to make room
2171 for a new value binding. */
2173 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2174 has been used in a non-class scope prior declaration.
2175 In that case, we should have already issued a
2176 diagnostic; for graceful error recovery purpose, pretend
2177 this was the intended declaration for that name. */
2178 || target_bval
== error_mark_node
2179 /* If TARGET_BVAL is anticipated but has not yet been
2180 declared, pretend it is not there at all. */
2181 || anticipated_builtin_p (target_bval
))
2182 binding
->value
= decl
;
2183 else if (TREE_CODE (target_bval
) == TYPE_DECL
2184 && DECL_ARTIFICIAL (target_bval
)
2185 && target_decl
!= target_bval
2186 && (TREE_CODE (target_decl
) != TYPE_DECL
2187 || same_type_p (TREE_TYPE (target_decl
),
2188 TREE_TYPE (target_bval
))))
2190 /* The old binding was a type name. It was placed in
2191 VALUE field because it was thought, at the point it was
2192 declared, to be the only entity with such a name. Move the
2193 type name into the type slot; it is now hidden by the new
2195 binding
->type
= bval
;
2196 binding
->value
= decl
;
2197 binding
->value_is_inherited
= false;
2199 else if (TREE_CODE (target_bval
) == TYPE_DECL
2200 && TREE_CODE (target_decl
) == TYPE_DECL
2201 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2202 && binding
->scope
->kind
!= sk_class
2203 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2204 /* If either type involves template parameters, we must
2205 wait until instantiation. */
2206 || uses_template_parms (TREE_TYPE (target_decl
))
2207 || uses_template_parms (TREE_TYPE (target_bval
))))
2208 /* We have two typedef-names, both naming the same type to have
2209 the same name. In general, this is OK because of:
2213 In a given scope, a typedef specifier can be used to redefine
2214 the name of any type declared in that scope to refer to the
2215 type to which it already refers.
2217 However, in class scopes, this rule does not apply due to the
2218 stricter language in [class.mem] prohibiting redeclarations of
2221 /* There can be two block-scope declarations of the same variable,
2222 so long as they are `extern' declarations. However, there cannot
2223 be two declarations of the same static data member:
2227 A member shall not be declared twice in the
2228 member-specification. */
2229 else if (VAR_P (target_decl
)
2230 && VAR_P (target_bval
)
2231 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2232 && !DECL_CLASS_SCOPE_P (target_decl
))
2234 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
2237 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2238 && TREE_CODE (bval
) == NAMESPACE_DECL
2239 && DECL_NAMESPACE_ALIAS (decl
)
2240 && DECL_NAMESPACE_ALIAS (bval
)
2241 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2242 /* [namespace.alias]
2244 In a declarative region, a namespace-alias-definition can be
2245 used to redefine a namespace-alias declared in that declarative
2246 region to refer only to the namespace to which it already
2251 if (!error_operand_p (bval
))
2252 diagnose_name_conflict (decl
, bval
);
2259 /* Diagnose a name conflict between DECL and BVAL. */
2262 diagnose_name_conflict (tree decl
, tree bval
)
2264 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2265 && TREE_CODE (decl
) != NAMESPACE_DECL
2266 && !DECL_DECLARES_FUNCTION_P (decl
)
2267 && (TREE_CODE (decl
) != TYPE_DECL
2268 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2269 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2271 if (concept_definition_p (decl
))
2272 error ("redeclaration of %q#D with different template parameters",
2275 error ("redeclaration of %q#D", decl
);
2278 error ("%q#D conflicts with a previous declaration", decl
);
2280 inform (location_of (bval
), "previous declaration %q#D", bval
);
2283 /* Wrapper for supplement_binding_1. */
2286 supplement_binding (cxx_binding
*binding
, tree decl
)
2289 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2290 ret
= supplement_binding_1 (binding
, decl
);
2291 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2295 /* Replace BINDING's current value on its scope's name list with
2299 update_local_overload (cxx_binding
*binding
, tree newval
)
2303 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2304 if (*d
== binding
->value
)
2306 /* Stitch new list node in. */
2307 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
2310 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2313 TREE_VALUE (*d
) = newval
;
2316 /* Compares the parameter-type-lists of ONE and TWO and
2317 returns false if they are different. If the DECLs are template
2318 functions, the return types and the template parameter lists are
2319 compared too (DR 565). */
2322 matching_fn_p (tree one
, tree two
)
2324 if (TREE_CODE (one
) != TREE_CODE (two
))
2327 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2328 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2331 if (TREE_CODE (one
) == TEMPLATE_DECL
)
2333 /* Compare template parms. */
2334 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2335 DECL_TEMPLATE_PARMS (two
)))
2338 /* And return type. */
2339 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2340 TREE_TYPE (TREE_TYPE (two
))))
2344 if (!equivalently_constrained (one
, two
))
2350 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2351 binding value (possibly with anticipated builtins stripped).
2352 Diagnose conflicts and return updated decl. */
2355 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2356 tree old
, tree decl
, bool is_friend
)
2359 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
2360 tree to_type
= old_type
;
2362 gcc_assert (level
->kind
== sk_namespace
? !binding
2363 : level
->kind
!= sk_class
&& !slot
);
2364 if (old
== error_mark_node
)
2367 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
2369 tree other
= to_type
;
2371 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2374 /* Pushing an artificial typedef. See if this matches either
2375 the type slot or the old value slot. */
2378 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
2379 /* Two artificial decls to same type. Do nothing. */
2386 /* Slide decl into the type slot, keep old unaltered */
2393 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
2395 /* Slide old into the type slot. */
2400 if (DECL_DECLARES_FUNCTION_P (decl
))
2404 else if (OVL_P (old
))
2406 for (ovl_iterator
iter (old
); iter
; ++iter
)
2410 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2412 /* If a function declaration in namespace scope or
2413 block scope has the same name and the same
2414 parameter-type- list (8.3.5) as a function
2415 introduced by a using-declaration, and the
2416 declarations do not declare the same function,
2417 the program is ill-formed. [namespace.udecl]/14 */
2418 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
2421 /* FIXME: To preserve existing error behavior, we
2422 still push the decl. This might change. */
2423 diagnose_name_conflict (decl
, fn
);
2430 if (to_type
!= old_type
2432 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2433 && !(DECL_IN_SYSTEM_HEADER (decl
)
2434 && DECL_IN_SYSTEM_HEADER (to_type
)))
2435 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2438 to_val
= ovl_insert (decl
, old
);
2442 else if (TREE_CODE (old
) != TREE_CODE (decl
))
2443 /* Different kinds of decls conflict. */
2445 else if (TREE_CODE (old
) == TYPE_DECL
)
2447 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2448 /* Two type decls to the same type. Do nothing. */
2453 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2455 /* Two maybe-aliased namespaces. If they're to the same target
2456 namespace, that's ok. */
2457 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2460 /* The new one must be an alias at this point. */
2461 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2464 else if (TREE_CODE (old
) == VAR_DECL
)
2466 /* There can be two block-scope declarations of the same
2467 variable, so long as they are `extern' declarations. */
2468 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2470 else if (tree match
= duplicate_decls (decl
, old
, false))
2478 diagnose_name_conflict (decl
, old
);
2485 if (level
->kind
== sk_namespace
|| to_type
== decl
|| to_val
== decl
)
2486 add_decl_to_level (level
, decl
);
2489 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2490 update_local_overload (binding
, to_val
);
2495 if (STAT_HACK_P (*slot
))
2497 STAT_TYPE (*slot
) = to_type
;
2498 STAT_DECL (*slot
) = to_val
;
2501 *slot
= stat_hack (to_val
, to_type
);
2507 binding
->type
= to_type
;
2508 binding
->value
= to_val
;
2515 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2517 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2519 /* DECL has C linkage. If we have an existing instance, make sure the
2520 new one is compatible. Make sure it has the same exception
2521 specification [7.5, 7.6]. Add DECL to the map. */
2524 check_extern_c_conflict (tree decl
)
2526 /* Ignore artificial or system header decls. */
2527 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2530 /* This only applies to decls at namespace scope. */
2531 if (!DECL_NAMESPACE_SCOPE_P (decl
))
2534 if (!extern_c_decls
)
2535 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2537 tree
*slot
= extern_c_decls
2538 ->find_slot_with_hash (DECL_NAME (decl
),
2539 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2540 if (tree old
= *slot
)
2542 if (TREE_CODE (old
) == OVERLOAD
)
2543 old
= OVL_FUNCTION (old
);
2546 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2547 ; /* If they're in the same context, we'll have already complained
2548 about a (possible) mismatch, when inserting the decl. */
2549 else if (!decls_match (decl
, old
))
2551 else if (TREE_CODE (decl
) == FUNCTION_DECL
2552 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2553 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2556 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2557 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2561 auto_diagnostic_group d
;
2562 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2563 "conflicting C language linkage declaration %q#D", decl
);
2564 inform (DECL_SOURCE_LOCATION (old
),
2565 "previous declaration %q#D", old
);
2567 inform (DECL_SOURCE_LOCATION (decl
),
2568 "due to different exception specifications");
2573 /* The hash table expects OVERLOADS, so construct one with
2574 OLD as both the function and the chain. This allocate
2575 an excess OVERLOAD node, but it's rare to have multiple
2576 extern "C" decls of the same name. And we save
2577 complicating the hash table logic (which is used
2579 *slot
= ovl_make (old
, old
);
2581 slot
= &OVL_CHAIN (*slot
);
2583 /* Chain it on for c_linkage_binding's use. */
2584 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2591 /* Returns a list of C-linkage decls with the name NAME. Used in
2592 c-family/c-pragma.c to implement redefine_extname pragma. */
2595 c_linkage_bindings (tree name
)
2598 if (tree
*slot
= extern_c_decls
2599 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2601 tree result
= *slot
;
2602 if (TREE_CODE (result
) == OVERLOAD
)
2603 result
= OVL_CHAIN (result
);
2610 /* Subroutine of check_local_shadow. */
2613 inform_shadowed (tree shadowed
)
2615 inform (DECL_SOURCE_LOCATION (shadowed
),
2616 "shadowed declaration is here");
2619 /* DECL is being declared at a local scope. Emit suitable shadow
2623 check_local_shadow (tree decl
)
2625 /* Don't complain about the parms we push and then pop
2626 while tentatively parsing a function declarator. */
2627 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2630 /* External decls are something else. */
2631 if (DECL_EXTERNAL (decl
))
2634 tree old
= NULL_TREE
;
2635 cp_binding_level
*old_scope
= NULL
;
2636 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2638 old
= binding
->value
;
2639 old_scope
= binding
->scope
;
2643 && (TREE_CODE (old
) == PARM_DECL
2645 || (TREE_CODE (old
) == TYPE_DECL
2646 && (!DECL_ARTIFICIAL (old
)
2647 || TREE_CODE (decl
) == TYPE_DECL
)))
2648 && DECL_FUNCTION_SCOPE_P (old
)
2649 && (!DECL_ARTIFICIAL (decl
)
2650 || is_capture_proxy (decl
)
2651 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2652 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2654 /* DECL shadows a local thing possibly of interest. */
2656 /* DR 2211: check that captures and parameters
2657 do not have the same name. */
2658 if (is_capture_proxy (decl
))
2660 if (current_lambda_expr ()
2661 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
2662 && TREE_CODE (old
) == PARM_DECL
2663 && DECL_NAME (decl
) != this_identifier
)
2665 error_at (DECL_SOURCE_LOCATION (old
),
2666 "lambda parameter %qD "
2667 "previously declared as a capture", old
);
2671 /* Don't complain if it's from an enclosing function. */
2672 else 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. */
2684 /* [basic.scope.param] A parameter name shall not be redeclared
2685 in the outermost block of the function definition. */
2686 if (b
->kind
== sk_function_parms
)
2688 error_at (DECL_SOURCE_LOCATION (decl
),
2689 "declaration of %q#D shadows a parameter", decl
);
2690 inform (DECL_SOURCE_LOCATION (old
),
2691 "%q#D previously declared here", old
);
2696 /* The local structure or class can't use parameters of
2697 the containing function anyway. */
2698 if (DECL_CONTEXT (old
) != current_function_decl
)
2700 for (cp_binding_level
*scope
= current_binding_level
;
2701 scope
!= old_scope
; scope
= scope
->level_chain
)
2702 if (scope
->kind
== sk_class
2703 && !LAMBDA_TYPE_P (scope
->this_entity
))
2706 /* Error if redeclaring a local declared in a
2707 init-statement or in the condition of an if or
2708 switch statement when the new declaration is in the
2709 outermost block of the controlled statement.
2710 Redeclaring a variable from a for or while condition is
2711 detected elsewhere. */
2712 else if (VAR_P (old
)
2713 && old_scope
== current_binding_level
->level_chain
2714 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2716 auto_diagnostic_group d
;
2717 error_at (DECL_SOURCE_LOCATION (decl
),
2718 "redeclaration of %q#D", decl
);
2719 inform (DECL_SOURCE_LOCATION (old
),
2720 "%q#D previously declared here", old
);
2724 3.3.3/3: The name declared in an exception-declaration (...)
2725 shall not be redeclared in the outermost block of the handler.
2726 3.3.3/2: A parameter name shall not be redeclared (...) in
2727 the outermost block of any handler associated with a
2729 3.4.1/15: The function parameter names shall not be redeclared
2730 in the exception-declaration nor in the outermost block of a
2731 handler for the function-try-block. */
2732 else if ((TREE_CODE (old
) == VAR_DECL
2733 && old_scope
== current_binding_level
->level_chain
2734 && old_scope
->kind
== sk_catch
)
2735 || (TREE_CODE (old
) == PARM_DECL
2736 && (current_binding_level
->kind
== sk_catch
2737 || current_binding_level
->level_chain
->kind
== sk_catch
)
2738 && in_function_try_handler
))
2740 auto_diagnostic_group d
;
2741 if (permerror (DECL_SOURCE_LOCATION (decl
),
2742 "redeclaration of %q#D", decl
))
2743 inform (DECL_SOURCE_LOCATION (old
),
2744 "%q#D previously declared here", old
);
2748 /* If '-Wshadow=compatible-local' is specified without other
2749 -Wshadow= flags, we will warn only when the type of the
2750 shadowing variable (DECL) can be converted to that of the
2751 shadowed parameter (OLD_LOCAL). The reason why we only check
2752 if DECL's type can be converted to OLD_LOCAL's type (but not the
2753 other way around) is because when users accidentally shadow a
2754 parameter, more than often they would use the variable
2755 thinking (mistakenly) it's still the parameter. It would be
2756 rare that users would use the variable in the place that
2757 expects the parameter but thinking it's a new decl.
2758 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
2759 warns regardless of whether one of the types involved
2760 is a subclass of the other, since that is never okay. */
2762 enum opt_code warning_code
;
2764 warning_code
= OPT_Wshadow
;
2765 else if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
))
2766 || TREE_CODE (decl
) == TYPE_DECL
2767 || TREE_CODE (old
) == TYPE_DECL
2768 || (!dependent_type_p (TREE_TYPE (decl
))
2769 && !dependent_type_p (TREE_TYPE (old
))
2770 /* If the new decl uses auto, we don't yet know
2771 its type (the old type cannot be using auto
2772 at this point, without also being
2773 dependent). This is an indication we're
2774 (now) doing the shadow checking too
2776 && !type_uses_auto (TREE_TYPE (decl
))
2777 && can_convert_arg (TREE_TYPE (old
), TREE_TYPE (decl
),
2778 decl
, LOOKUP_IMPLICIT
, tf_none
)))
2779 warning_code
= OPT_Wshadow_compatible_local
;
2781 warning_code
= OPT_Wshadow_local
;
2784 if (TREE_CODE (old
) == PARM_DECL
)
2785 msg
= "declaration of %q#D shadows a parameter";
2786 else if (is_capture_proxy (old
))
2787 msg
= "declaration of %qD shadows a lambda capture";
2789 msg
= "declaration of %qD shadows a previous local";
2791 auto_diagnostic_group d
;
2792 if (warning_at (DECL_SOURCE_LOCATION (decl
), warning_code
, msg
, decl
))
2793 inform_shadowed (old
);
2800 /* Don't warn for artificial things that are not implicit typedefs. */
2801 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2804 if (nonlambda_method_basetype ())
2805 if (tree member
= lookup_member (current_nonlambda_class_type (),
2806 DECL_NAME (decl
), /*protect=*/0,
2807 /*want_type=*/false, tf_warning_or_error
))
2809 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2811 /* Warn if a variable shadows a non-function, or the variable
2812 is a function or a pointer-to-function. */
2814 || TREE_CODE (decl
) == FUNCTION_DECL
2815 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2816 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2818 auto_diagnostic_group d
;
2819 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
2820 "declaration of %qD shadows a member of %qT",
2821 decl
, current_nonlambda_class_type ())
2823 inform_shadowed (member
);
2828 /* Now look for a namespace shadow. */
2829 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2832 || (TREE_CODE (old
) == TYPE_DECL
2833 && (!DECL_ARTIFICIAL (old
)
2834 || TREE_CODE (decl
) == TYPE_DECL
)))
2835 && !instantiating_current_function_p ())
2836 /* XXX shadow warnings in outer-more namespaces */
2838 auto_diagnostic_group d
;
2839 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
2840 "declaration of %qD shadows a global declaration",
2842 inform_shadowed (old
);
2849 /* DECL is being pushed inside function CTX. Set its context, if
2853 set_decl_context_in_fn (tree ctx
, tree decl
)
2855 if (!DECL_CONTEXT (decl
)
2856 /* A local declaration for a function doesn't constitute
2858 && TREE_CODE (decl
) != FUNCTION_DECL
2859 /* A local declaration for an `extern' variable is in the
2860 scope of the current namespace, not the current
2862 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2863 /* When parsing the parameter list of a function declarator,
2864 don't set DECL_CONTEXT to an enclosing function. When we
2865 push the PARM_DECLs in order to process the function body,
2866 current_binding_level->this_entity will be set. */
2867 && !(TREE_CODE (decl
) == PARM_DECL
2868 && current_binding_level
->kind
== sk_function_parms
2869 && current_binding_level
->this_entity
== NULL
))
2870 DECL_CONTEXT (decl
) = ctx
;
2872 /* If this is the declaration for a namespace-scope function,
2873 but the declaration itself is in a local scope, mark the
2875 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2876 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2879 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2880 name is already bound at the current level.
2882 [basic.link] If there is a visible declaration of an entity with
2883 linkage having the same name and type, ignoring entities declared
2884 outside the innermost enclosing namespace scope, the block scope
2885 declaration declares that same entity and receives the linkage of
2886 the previous declaration.
2888 Also, make sure that this decl matches any existing external decl
2889 in the enclosing namespace. */
2892 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2894 tree ns_value
= decl
; /* Unique marker. */
2898 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2902 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2903 loc_value
= ns_value
;
2905 if (loc_value
== error_mark_node
2906 /* An ambiguous lookup. */
2907 || (loc_value
&& TREE_CODE (loc_value
) == TREE_LIST
))
2908 loc_value
= NULL_TREE
;
2910 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2911 if (!iter
.hidden_p ()
2912 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2913 && decls_match (*iter
, decl
))
2915 /* The standard only says that the local extern inherits
2916 linkage from the previous decl; in particular, default
2917 args are not shared. Add the decl into a hash table to
2918 make sure only the previous decl in this case is seen
2919 by the middle end. */
2920 struct cxx_int_tree_map
*h
;
2922 /* We inherit the outer decl's linkage. But we're a
2924 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2926 if (cp_function_chain
->extern_decl_map
== NULL
)
2927 cp_function_chain
->extern_decl_map
2928 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2930 h
= ggc_alloc
<cxx_int_tree_map
> ();
2931 h
->uid
= DECL_UID (decl
);
2933 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2934 ->find_slot (h
, INSERT
);
2940 if (TREE_PUBLIC (decl
))
2942 /* DECL is externally visible. Make sure it matches a matching
2943 decl in the namespace scope. We only really need to check
2944 this when inserting the decl, not when we find an existing
2945 match in the current scope. However, in practice we're
2946 going to be inserting a new decl in the majority of cases --
2947 who writes multiple extern decls for the same thing in the
2948 same local scope? Doing it here often avoids a duplicate
2949 namespace lookup. */
2951 /* Avoid repeating a lookup. */
2952 if (ns_value
== decl
)
2953 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2955 if (ns_value
== error_mark_node
2956 || (ns_value
&& TREE_CODE (ns_value
) == TREE_LIST
))
2957 ns_value
= NULL_TREE
;
2959 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2963 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2964 ; /* Not externally visible. */
2965 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2966 ; /* Both are extern "C", we'll check via that mechanism. */
2967 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2968 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2969 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2970 COMPARE_REDECLARATION
)))
2972 auto_diagnostic_group d
;
2973 if (permerror (DECL_SOURCE_LOCATION (decl
),
2974 "local external declaration %q#D", decl
))
2975 inform (DECL_SOURCE_LOCATION (other
),
2976 "does not match previous declaration %q#D", other
);
2983 /* Record DECL as belonging to the current lexical scope. Check for
2984 errors (such as an incompatible declaration for the same name
2985 already seen in the same scope). IS_FRIEND is true if DECL is
2986 declared as a friend.
2988 Returns either DECL or an old decl for the same name. If an old
2989 decl is returned, it may have been smashed to agree with what DECL
2993 do_pushdecl (tree decl
, bool is_friend
)
2995 if (decl
== error_mark_node
)
2996 return error_mark_node
;
2998 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2999 set_decl_context_in_fn (current_function_decl
, decl
);
3001 /* The binding level we will be pushing into. During local class
3002 pushing, we want to push to the containing scope. */
3003 cp_binding_level
*level
= current_binding_level
;
3004 while (level
->kind
== sk_class
3005 || level
->kind
== sk_cleanup
)
3006 level
= level
->level_chain
;
3008 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3009 insert it. Other NULL-named decls, not so much. */
3010 tree name
= DECL_NAME (decl
);
3011 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
3013 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3014 tree ns
= NULL_TREE
; /* Searched namespace. */
3015 tree
*slot
= NULL
; /* Binding slot in namespace. */
3016 tree old
= NULL_TREE
;
3018 if (level
->kind
== sk_namespace
)
3020 /* We look in the decl's namespace for an existing
3021 declaration, even though we push into the current
3023 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3024 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3025 /* Create the binding, if this is current namespace, because
3026 that's where we'll be pushing anyway. */
3027 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3029 old
= MAYBE_STAT_DECL (*slot
);
3033 binding
= find_local_binding (level
, name
);
3035 old
= binding
->value
;
3038 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3039 && DECL_EXTERNAL (decl
))
3040 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3042 if (old
== error_mark_node
)
3045 for (ovl_iterator
iter (old
); iter
; ++iter
)
3046 if (iter
.using_p ())
3047 ; /* Ignore using decls here. */
3048 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
3050 if (match
== error_mark_node
)
3052 else if (TREE_CODE (match
) == TYPE_DECL
)
3053 /* The IDENTIFIER will have the type referring to the
3054 now-smashed TYPE_DECL, because ...? Reset it. */
3055 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3056 else if (iter
.hidden_p () && !DECL_HIDDEN_P (match
))
3058 /* Unhiding a previously hidden decl. */
3059 tree head
= iter
.reveal_node (old
);
3064 update_local_overload (binding
, head
);
3065 binding
->value
= head
;
3067 else if (STAT_HACK_P (*slot
))
3068 STAT_DECL (*slot
) = head
;
3072 if (DECL_EXTERN_C_P (match
))
3073 /* We need to check and register the decl now. */
3074 check_extern_c_conflict (match
);
3079 /* We are pushing a new decl. */
3081 /* Skip a hidden builtin we failed to match already. There can
3083 if (old
&& anticipated_builtin_p (old
))
3084 old
= OVL_CHAIN (old
);
3086 check_template_shadow (decl
);
3088 if (DECL_DECLARES_FUNCTION_P (decl
))
3090 check_default_args (decl
);
3094 if (level
->kind
!= sk_namespace
)
3096 /* In a local class, a friend function declaration must
3097 find a matching decl in the innermost non-class scope.
3098 [class.friend/11] */
3099 error_at (DECL_SOURCE_LOCATION (decl
),
3100 "friend declaration %qD in local class without "
3101 "prior local declaration", decl
);
3102 /* Don't attempt to push it. */
3103 return error_mark_node
;
3105 /* Hide it from ordinary lookup. */
3106 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
3110 if (level
->kind
!= sk_namespace
)
3112 check_local_shadow (decl
);
3114 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3115 /* A local namespace alias. */
3116 set_identifier_type_value (name
, NULL_TREE
);
3119 binding
= create_local_binding (level
, name
);
3123 ns
= current_namespace
;
3124 slot
= find_namespace_slot (ns
, name
, true);
3125 /* Update OLD to reflect the namespace we're going to be
3127 old
= MAYBE_STAT_DECL (*slot
);
3130 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
3133 /* An existing decl matched, use it. */
3135 else if (TREE_CODE (decl
) == TYPE_DECL
)
3137 tree type
= TREE_TYPE (decl
);
3139 if (type
!= error_mark_node
)
3141 if (TYPE_NAME (type
) != decl
)
3142 set_underlying_type (decl
);
3145 set_identifier_type_value_with_scope (name
, decl
, level
);
3147 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
3150 /* If this is a locally defined typedef in a function that
3151 is not a template instantation, record it to implement
3152 -Wunused-local-typedefs. */
3153 if (!instantiating_current_function_p ())
3154 record_locally_defined_typedef (decl
);
3156 else if (VAR_P (decl
))
3157 maybe_register_incomplete_var (decl
);
3159 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3160 && DECL_EXTERN_C_P (decl
))
3161 check_extern_c_conflict (decl
);
3164 add_decl_to_level (level
, decl
);
3169 /* Record a decl-node X as belonging to the current lexical scope.
3170 It's a friend if IS_FRIEND is true -- which affects exactly where
3174 pushdecl (tree x
, bool is_friend
)
3176 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3177 tree ret
= do_pushdecl (x
, is_friend
);
3178 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3182 /* Enter DECL into the symbol table, if that's appropriate. Returns
3183 DECL, or a modified version thereof. */
3186 maybe_push_decl (tree decl
)
3188 tree type
= TREE_TYPE (decl
);
3190 /* Add this decl to the current binding level, but not if it comes
3191 from another scope, e.g. a static member variable. TEM may equal
3192 DECL or it may be a previous decl of the same name. */
3193 if (decl
== error_mark_node
3194 || (TREE_CODE (decl
) != PARM_DECL
3195 && DECL_CONTEXT (decl
) != NULL_TREE
3196 /* Definitions of namespace members outside their namespace are
3198 && !DECL_NAMESPACE_SCOPE_P (decl
))
3199 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3200 || type
== unknown_type_node
3201 /* The declaration of a template specialization does not affect
3202 the functions available for overload resolution, so we do not
3204 || (TREE_CODE (decl
) == FUNCTION_DECL
3205 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3208 return pushdecl (decl
);
3211 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3212 binding level. If IS_USING is true, DECL got here through a
3213 using-declaration. */
3216 push_local_binding (tree id
, tree decl
, bool is_using
)
3218 /* Skip over any local classes. This makes sense if we call
3219 push_local_binding with a friend decl of a local class. */
3220 cp_binding_level
*b
= innermost_nonclass_level ();
3222 gcc_assert (b
->kind
!= sk_namespace
);
3223 if (find_local_binding (b
, id
))
3225 /* Supplement the existing binding. */
3226 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3227 /* It didn't work. Something else must be bound at this
3228 level. Do not add DECL to the list of things to pop
3233 /* Create a new binding. */
3234 push_binding (id
, decl
, b
);
3236 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3237 /* We must put the OVERLOAD or using into a TREE_LIST since we
3238 cannot use the decl's chain itself. */
3239 decl
= build_tree_list (NULL_TREE
, decl
);
3241 /* And put DECL on the list of things declared by the current
3243 add_decl_to_level (b
, decl
);
3247 /* true means unconditionally make a BLOCK for the next level pushed. */
3249 static bool keep_next_level_flag
;
3251 static int binding_depth
= 0;
3258 for (i
= 0; i
< depth
* 2; i
++)
3262 /* Return a string describing the kind of SCOPE we have. */
3264 cp_binding_level_descriptor (cp_binding_level
*scope
)
3266 /* The order of this table must match the "scope_kind"
3268 static const char* scope_kind_names
[] = {
3274 "function-parameter-scope",
3277 "template-parameter-scope",
3278 "template-explicit-spec-scope"
3280 const scope_kind kind
= scope
->explicit_spec_p
3281 ? sk_template_spec
: scope
->kind
;
3283 return scope_kind_names
[kind
];
3286 /* Output a debugging information about SCOPE when performing
3289 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3291 const char *desc
= cp_binding_level_descriptor (scope
);
3292 if (scope
->this_entity
)
3293 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
3294 scope
->this_entity
, (void *) scope
, line
);
3296 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
3299 /* A chain of binding_level structures awaiting reuse. */
3301 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3303 /* Insert SCOPE as the innermost binding level. */
3306 push_binding_level (cp_binding_level
*scope
)
3308 /* Add it to the front of currently active scopes stack. */
3309 scope
->level_chain
= current_binding_level
;
3310 current_binding_level
= scope
;
3311 keep_next_level_flag
= false;
3313 if (ENABLE_SCOPE_CHECKING
)
3315 scope
->binding_depth
= binding_depth
;
3316 indent (binding_depth
);
3317 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3323 /* Create a new KIND scope and make it the top of the active scopes stack.
3324 ENTITY is the scope of the associated C++ entity (namespace, class,
3325 function, C++0x enumeration); it is NULL otherwise. */
3328 begin_scope (scope_kind kind
, tree entity
)
3330 cp_binding_level
*scope
;
3332 /* Reuse or create a struct for this binding level. */
3333 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3335 scope
= free_binding_level
;
3336 free_binding_level
= scope
->level_chain
;
3337 memset (scope
, 0, sizeof (cp_binding_level
));
3340 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3342 scope
->this_entity
= entity
;
3343 scope
->more_cleanups_ok
= true;
3350 case sk_template_spec
:
3351 scope
->explicit_spec_p
= true;
3352 kind
= sk_template_parms
;
3354 case sk_template_parms
:
3361 case sk_scoped_enum
:
3362 case sk_function_parms
:
3363 case sk_transaction
:
3365 scope
->keep
= keep_next_level_flag
;
3369 NAMESPACE_LEVEL (entity
) = scope
;
3373 /* Should not happen. */
3379 push_binding_level (scope
);
3384 /* We're about to leave current scope. Pop the top of the stack of
3385 currently active scopes. Return the enclosing scope, now active. */
3390 cp_binding_level
*scope
= current_binding_level
;
3392 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3393 current_binding_level
= class_binding_level
;
3395 /* We cannot leave a scope, if there are none left. */
3396 if (NAMESPACE_LEVEL (global_namespace
))
3397 gcc_assert (!global_scope_p (scope
));
3399 if (ENABLE_SCOPE_CHECKING
)
3401 indent (--binding_depth
);
3402 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3406 /* Move one nesting level up. */
3407 current_binding_level
= scope
->level_chain
;
3409 /* Namespace-scopes are left most probably temporarily, not
3410 completely; they can be reopened later, e.g. in namespace-extension
3411 or any name binding activity that requires us to resume a
3412 namespace. For classes, we cache some binding levels. For other
3413 scopes, we just make the structure available for reuse. */
3414 if (scope
->kind
!= sk_namespace
3415 && scope
!= previous_class_level
)
3417 scope
->level_chain
= free_binding_level
;
3418 gcc_assert (!ENABLE_SCOPE_CHECKING
3419 || scope
->binding_depth
== binding_depth
);
3420 free_binding_level
= scope
;
3423 if (scope
->kind
== sk_class
)
3425 /* Reset DEFINING_CLASS_P to allow for reuse of a
3426 class-defining scope in a non-defining context. */
3427 scope
->defining_class_p
= 0;
3429 /* Find the innermost enclosing class scope, and reset
3430 CLASS_BINDING_LEVEL appropriately. */
3431 class_binding_level
= NULL
;
3432 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3433 if (scope
->kind
== sk_class
)
3435 class_binding_level
= scope
;
3440 return current_binding_level
;
3443 /* When we exit a toplevel class scope, we save its binding level so
3444 that we can restore it quickly. Here, we've entered some other
3445 class, so we must invalidate our cache. */
3448 invalidate_class_lookup_cache (void)
3450 previous_class_level
->level_chain
= free_binding_level
;
3451 free_binding_level
= previous_class_level
;
3452 previous_class_level
= NULL
;
3456 resume_scope (cp_binding_level
* b
)
3458 /* Resuming binding levels is meant only for namespaces,
3459 and those cannot nest into classes. */
3460 gcc_assert (!class_binding_level
);
3461 /* Also, resuming a non-directly nested namespace is a no-no. */
3462 gcc_assert (b
->level_chain
== current_binding_level
);
3463 current_binding_level
= b
;
3464 if (ENABLE_SCOPE_CHECKING
)
3466 b
->binding_depth
= binding_depth
;
3467 indent (binding_depth
);
3468 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3473 /* Return the innermost binding level that is not for a class scope. */
3475 static cp_binding_level
*
3476 innermost_nonclass_level (void)
3478 cp_binding_level
*b
;
3480 b
= current_binding_level
;
3481 while (b
->kind
== sk_class
)
3487 /* We're defining an object of type TYPE. If it needs a cleanup, but
3488 we're not allowed to add any more objects with cleanups to the current
3489 scope, create a new binding level. */
3492 maybe_push_cleanup_level (tree type
)
3494 if (type
!= error_mark_node
3495 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3496 && current_binding_level
->more_cleanups_ok
== 0)
3498 begin_scope (sk_cleanup
, NULL
);
3499 current_binding_level
->statement_list
= push_stmt_list ();
3503 /* Return true if we are in the global binding level. */
3506 global_bindings_p (void)
3508 return global_scope_p (current_binding_level
);
3511 /* True if we are currently in a toplevel binding level. This
3512 means either the global binding level or a namespace in a toplevel
3513 binding level. Since there are no non-toplevel namespace levels,
3514 this really means any namespace or template parameter level. We
3515 also include a class whose context is toplevel. */
3518 toplevel_bindings_p (void)
3520 cp_binding_level
*b
= innermost_nonclass_level ();
3522 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3525 /* True if this is a namespace scope, or if we are defining a class
3526 which is itself at namespace scope, or whose enclosing class is
3527 such a class, etc. */
3530 namespace_bindings_p (void)
3532 cp_binding_level
*b
= innermost_nonclass_level ();
3534 return b
->kind
== sk_namespace
;
3537 /* True if the innermost non-class scope is a block scope. */
3540 local_bindings_p (void)
3542 cp_binding_level
*b
= innermost_nonclass_level ();
3543 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3546 /* True if the current level needs to have a BLOCK made. */
3551 return (current_binding_level
->blocks
!= NULL_TREE
3552 || current_binding_level
->keep
3553 || current_binding_level
->kind
== sk_cleanup
3554 || current_binding_level
->names
!= NULL_TREE
3555 || current_binding_level
->using_directives
);
3558 /* Returns the kind of the innermost scope. */
3561 innermost_scope_kind (void)
3563 return current_binding_level
->kind
;
3566 /* Returns true if this scope was created to store template parameters. */
3569 template_parm_scope_p (void)
3571 return innermost_scope_kind () == sk_template_parms
;
3574 /* If KEEP is true, make a BLOCK node for the next binding level,
3575 unconditionally. Otherwise, use the normal logic to decide whether
3576 or not to create a BLOCK. */
3579 keep_next_level (bool keep
)
3581 keep_next_level_flag
= keep
;
3584 /* Return the list of declarations of the current local scope. */
3587 get_local_decls (void)
3589 gcc_assert (current_binding_level
->kind
!= sk_namespace
3590 && current_binding_level
->kind
!= sk_class
);
3591 return current_binding_level
->names
;
3594 /* Return how many function prototypes we are currently nested inside. */
3597 function_parm_depth (void)
3600 cp_binding_level
*b
;
3602 for (b
= current_binding_level
;
3603 b
->kind
== sk_function_parms
;
3610 /* For debugging. */
3611 static int no_print_functions
= 0;
3612 static int no_print_builtins
= 0;
3615 print_binding_level (cp_binding_level
* lvl
)
3619 if (lvl
->this_entity
)
3620 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
3621 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3622 if (lvl
->more_cleanups_ok
)
3623 fprintf (stderr
, " more-cleanups-ok");
3624 if (lvl
->have_cleanups
)
3625 fprintf (stderr
, " have-cleanups");
3626 fprintf (stderr
, "\n");
3629 fprintf (stderr
, " names:\t");
3630 /* We can probably fit 3 names to a line? */
3631 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3633 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3635 if (no_print_builtins
3636 && (TREE_CODE (t
) == TYPE_DECL
)
3637 && DECL_IS_BUILTIN (t
))
3640 /* Function decls tend to have longer names. */
3641 if (TREE_CODE (t
) == FUNCTION_DECL
)
3648 fprintf (stderr
, "\n\t");
3651 print_node_brief (stderr
, "", t
, 0);
3652 if (t
== error_mark_node
)
3656 fprintf (stderr
, "\n");
3658 if (vec_safe_length (lvl
->class_shadowed
))
3661 cp_class_binding
*b
;
3662 fprintf (stderr
, " class-shadowed:");
3663 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3664 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3665 fprintf (stderr
, "\n");
3667 if (lvl
->type_shadowed
)
3669 fprintf (stderr
, " type-shadowed:");
3670 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3672 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3674 fprintf (stderr
, "\n");
3679 debug (cp_binding_level
&ref
)
3681 print_binding_level (&ref
);
3685 debug (cp_binding_level
*ptr
)
3690 fprintf (stderr
, "<nil>\n");
3695 print_other_binding_stack (cp_binding_level
*stack
)
3697 cp_binding_level
*level
;
3698 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3700 fprintf (stderr
, "binding level %p\n", (void *) level
);
3701 print_binding_level (level
);
3706 print_binding_stack (void)
3708 cp_binding_level
*b
;
3709 fprintf (stderr
, "current_binding_level=%p\n"
3710 "class_binding_level=%p\n"
3711 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3712 (void *) current_binding_level
, (void *) class_binding_level
,
3713 (void *) NAMESPACE_LEVEL (global_namespace
));
3714 if (class_binding_level
)
3716 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3717 if (b
== current_binding_level
)
3720 b
= class_binding_level
;
3722 b
= current_binding_level
;
3725 b
= current_binding_level
;
3726 print_other_binding_stack (b
);
3727 fprintf (stderr
, "global:\n");
3728 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3731 /* Return the type associated with ID. */
3734 identifier_type_value_1 (tree id
)
3736 /* There is no type with that name, anywhere. */
3737 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3739 /* This is not the type marker, but the real thing. */
3740 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3741 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3742 /* Have to search for it. It must be on the global level, now.
3743 Ask lookup_name not to return non-types. */
3744 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3746 return TREE_TYPE (id
);
3750 /* Wrapper for identifier_type_value_1. */
3753 identifier_type_value (tree id
)
3756 timevar_start (TV_NAME_LOOKUP
);
3757 ret
= identifier_type_value_1 (id
);
3758 timevar_stop (TV_NAME_LOOKUP
);
3762 /* Push a definition of struct, union or enum tag named ID. into
3763 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3764 the tag ID is not already defined. */
3767 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3771 if (b
->kind
!= sk_namespace
)
3773 /* Shadow the marker, not the real thing, so that the marker
3774 gets restored later. */
3775 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3777 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3778 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3779 TREE_TYPE (b
->type_shadowed
) = type
;
3783 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3785 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3787 /* Store marker instead of real type. */
3788 type
= global_type_node
;
3790 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3793 /* As set_identifier_type_value_with_scope, but using
3794 current_binding_level. */
3797 set_identifier_type_value (tree id
, tree decl
)
3799 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3802 /* Return the name for the constructor (or destructor) for the
3806 constructor_name (tree type
)
3808 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3810 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3813 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3814 which must be a class type. */
3817 constructor_name_p (tree name
, tree type
)
3819 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3821 /* These don't have names. */
3822 if (TREE_CODE (type
) == DECLTYPE_TYPE
3823 || TREE_CODE (type
) == TYPEOF_TYPE
)
3826 if (name
&& name
== constructor_name (type
))
3832 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3833 caller to set DECL_CONTEXT properly.
3835 Note that this must only be used when X will be the new innermost
3836 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3837 without checking to see if the current IDENTIFIER_BINDING comes from a
3838 closer binding level than LEVEL. */
3841 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3843 cp_binding_level
*b
;
3845 if (level
->kind
== sk_class
)
3847 b
= class_binding_level
;
3848 class_binding_level
= level
;
3849 pushdecl_class_level (x
);
3850 class_binding_level
= b
;
3854 tree function_decl
= current_function_decl
;
3855 if (level
->kind
== sk_namespace
)
3856 current_function_decl
= NULL_TREE
;
3857 b
= current_binding_level
;
3858 current_binding_level
= level
;
3859 x
= pushdecl (x
, is_friend
);
3860 current_binding_level
= b
;
3861 current_function_decl
= function_decl
;
3866 /* Inject X into the local scope just before the function parms. */
3869 pushdecl_outermost_localscope (tree x
)
3871 cp_binding_level
*b
= NULL
;
3872 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3874 /* Find the scope just inside the function parms. */
3875 for (cp_binding_level
*n
= current_binding_level
;
3876 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3879 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3880 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3885 /* Process a local-scope or namespace-scope using declaration. LOOKUP
3886 is the result of qualified lookup (both value & type are
3887 significant). FN_SCOPE_P indicates if we're at function-scope (as
3888 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
3889 bindings, which are altered to reflect the newly brought in
3893 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
3894 tree
*value_p
, tree
*type_p
)
3896 tree value
= *value_p
;
3897 tree type
= *type_p
;
3898 bool failed
= false;
3900 /* Shift the old and new bindings around so we're comparing class and
3901 enumeration names to each other. */
3902 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
3908 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
3910 lookup
.type
= lookup
.value
;
3911 lookup
.value
= NULL_TREE
;
3916 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
3918 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
3920 tree new_fn
= *usings
;
3922 /* [namespace.udecl]
3924 If a function declaration in namespace scope or block
3925 scope has the same name and the same parameter types as a
3926 function introduced by a using declaration the program is
3929 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
3933 if (new_fn
== old_fn
)
3935 /* The function already exists in the current
3940 else if (old
.using_p ())
3941 continue; /* This is a using decl. */
3942 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
3943 continue; /* This is an anticipated builtin. */
3944 else if (!matching_fn_p (new_fn
, old_fn
))
3945 continue; /* Parameters do not match. */
3946 else if (decls_match (new_fn
, old_fn
))
3948 /* Extern "C" in different namespaces. */
3954 diagnose_name_conflict (new_fn
, old_fn
);
3962 /* Unlike the decl-pushing case we don't drop anticipated
3963 builtins here. They don't cause a problem, and we'd
3964 like to match them with a future declaration. */
3965 value
= ovl_insert (new_fn
, value
, true);
3969 /* Ignore anticipated builtins. */
3970 && !anticipated_builtin_p (value
)
3971 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
3973 diagnose_name_conflict (lookup
.value
, value
);
3977 value
= lookup
.value
;
3979 if (lookup
.type
&& lookup
.type
!= type
)
3981 if (type
&& !decls_match (lookup
.type
, type
))
3983 diagnose_name_conflict (lookup
.type
, type
);
3990 /* If value is empty, shift any class or enumeration name back. */
4002 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4003 Both are namespaces. */
4006 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4008 int depth
= SCOPE_DEPTH (ancestor
);
4010 if (!depth
&& !inline_only
)
4011 /* The global namespace encloses everything. */
4014 while (SCOPE_DEPTH (descendant
) > depth
4015 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4016 descendant
= CP_DECL_CONTEXT (descendant
);
4018 return ancestor
== descendant
;
4021 /* Returns true if ROOT (a non-alias namespace, class, or function)
4022 encloses CHILD. CHILD may be either a class type or a namespace
4026 is_ancestor (tree root
, tree child
)
4028 gcc_checking_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4029 && !DECL_NAMESPACE_ALIAS (root
))
4030 || TREE_CODE (root
) == FUNCTION_DECL
4031 || CLASS_TYPE_P (root
));
4032 gcc_checking_assert (TREE_CODE (child
) == NAMESPACE_DECL
4033 || CLASS_TYPE_P (child
));
4035 /* The global namespace encloses everything. Early-out for the
4037 if (root
== global_namespace
)
4040 /* Search CHILD until we reach namespace scope. */
4041 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4043 /* If we've reached the ROOT, it encloses CHILD. */
4047 /* Go out one level. */
4049 child
= TYPE_NAME (child
);
4050 child
= CP_DECL_CONTEXT (child
);
4053 if (TREE_CODE (root
) != NAMESPACE_DECL
)
4054 /* Failed to meet the non-namespace we were looking for. */
4057 if (tree alias
= DECL_NAMESPACE_ALIAS (child
))
4060 return is_nested_namespace (root
, child
);
4063 /* Enter the class or namespace scope indicated by T suitable for name
4064 lookup. T can be arbitrary scope, not necessary nested inside the
4065 current scope. Returns a non-null scope to pop iff pop_scope
4066 should be called later to exit this scope. */
4071 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4072 push_decl_namespace (t
);
4073 else if (CLASS_TYPE_P (t
))
4075 if (!at_class_scope_p ()
4076 || !same_type_p (current_class_type
, t
))
4077 push_nested_class (t
);
4079 /* T is the same as the current scope. There is therefore no
4080 need to re-enter the scope. Since we are not actually
4081 pushing a new scope, our caller should not call
4089 /* Leave scope pushed by push_scope. */
4096 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4097 pop_decl_namespace ();
4098 else if CLASS_TYPE_P (t
)
4099 pop_nested_class ();
4102 /* Subroutine of push_inner_scope. */
4105 push_inner_scope_r (tree outer
, tree inner
)
4110 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4113 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4115 push_inner_scope_r (outer
, prev
);
4116 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4118 cp_binding_level
*save_template_parm
= 0;
4119 /* Temporary take out template parameter scopes. They are saved
4120 in reversed order in save_template_parm. */
4121 while (current_binding_level
->kind
== sk_template_parms
)
4123 cp_binding_level
*b
= current_binding_level
;
4124 current_binding_level
= b
->level_chain
;
4125 b
->level_chain
= save_template_parm
;
4126 save_template_parm
= b
;
4129 resume_scope (NAMESPACE_LEVEL (inner
));
4130 current_namespace
= inner
;
4132 /* Restore template parameter scopes. */
4133 while (save_template_parm
)
4135 cp_binding_level
*b
= save_template_parm
;
4136 save_template_parm
= b
->level_chain
;
4137 b
->level_chain
= current_binding_level
;
4138 current_binding_level
= b
;
4145 /* Enter the scope INNER from current scope. INNER must be a scope
4146 nested inside current scope. This works with both name lookup and
4147 pushing name into scope. In case a template parameter scope is present,
4148 namespace is pushed under the template parameter scope according to
4149 name lookup rule in 14.6.1/6.
4151 Return the former current scope suitable for pop_inner_scope. */
4154 push_inner_scope (tree inner
)
4156 tree outer
= current_scope ();
4158 outer
= current_namespace
;
4160 push_inner_scope_r (outer
, inner
);
4164 /* Exit the current scope INNER back to scope OUTER. */
4167 pop_inner_scope (tree outer
, tree inner
)
4170 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4173 while (outer
!= inner
)
4175 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4177 cp_binding_level
*save_template_parm
= 0;
4178 /* Temporary take out template parameter scopes. They are saved
4179 in reversed order in save_template_parm. */
4180 while (current_binding_level
->kind
== sk_template_parms
)
4182 cp_binding_level
*b
= current_binding_level
;
4183 current_binding_level
= b
->level_chain
;
4184 b
->level_chain
= save_template_parm
;
4185 save_template_parm
= b
;
4190 /* Restore template parameter scopes. */
4191 while (save_template_parm
)
4193 cp_binding_level
*b
= save_template_parm
;
4194 save_template_parm
= b
->level_chain
;
4195 b
->level_chain
= current_binding_level
;
4196 current_binding_level
= b
;
4202 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4206 /* Do a pushlevel for class declarations. */
4209 pushlevel_class (void)
4211 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4214 /* ...and a poplevel for class declarations. */
4217 poplevel_class (void)
4219 cp_binding_level
*level
= class_binding_level
;
4220 cp_class_binding
*cb
;
4224 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4225 gcc_assert (level
!= 0);
4227 /* If we're leaving a toplevel class, cache its binding level. */
4228 if (current_class_depth
== 1)
4229 previous_class_level
= level
;
4230 for (shadowed
= level
->type_shadowed
;
4232 shadowed
= TREE_CHAIN (shadowed
))
4233 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4235 /* Remove the bindings for all of the class-level declarations. */
4236 if (level
->class_shadowed
)
4238 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4240 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4241 cxx_binding_free (cb
->base
);
4243 ggc_free (level
->class_shadowed
);
4244 level
->class_shadowed
= NULL
;
4247 /* Now, pop out of the binding level which we created up in the
4248 `pushlevel_class' routine. */
4249 gcc_assert (current_binding_level
== level
);
4251 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4254 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4255 appropriate. DECL is the value to which a name has just been
4256 bound. CLASS_TYPE is the class in which the lookup occurred. */
4259 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4262 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4266 if (TREE_CODE (decl
) == OVERLOAD
)
4267 context
= ovl_scope (decl
);
4270 gcc_assert (DECL_P (decl
));
4271 context
= context_for_name_lookup (decl
);
4274 if (is_properly_derived_from (class_type
, context
))
4275 INHERITED_VALUE_BINDING_P (binding
) = 1;
4277 INHERITED_VALUE_BINDING_P (binding
) = 0;
4279 else if (binding
->value
== decl
)
4280 /* We only encounter a TREE_LIST when there is an ambiguity in the
4281 base classes. Such an ambiguity can be overridden by a
4282 definition in this class. */
4283 INHERITED_VALUE_BINDING_P (binding
) = 1;
4285 INHERITED_VALUE_BINDING_P (binding
) = 0;
4288 /* Make the declaration of X appear in CLASS scope. */
4291 pushdecl_class_level (tree x
)
4293 bool is_valid
= true;
4296 /* Do nothing if we're adding to an outer lambda closure type,
4297 outer_binding will add it later if it's needed. */
4298 if (current_class_type
!= class_binding_level
->this_entity
)
4301 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4302 /* Get the name of X. */
4303 tree name
= OVL_NAME (x
);
4307 is_valid
= push_class_level_binding (name
, x
);
4308 if (TREE_CODE (x
) == TYPE_DECL
)
4309 set_identifier_type_value (name
, x
);
4311 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4313 /* If X is an anonymous aggregate, all of its members are
4314 treated as if they were members of the class containing the
4315 aggregate, for naming purposes. */
4316 location_t save_location
= input_location
;
4317 tree anon
= TREE_TYPE (x
);
4318 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
4319 for (unsigned ix
= member_vec
->length (); ix
--;)
4321 tree binding
= (*member_vec
)[ix
];
4322 if (STAT_HACK_P (binding
))
4324 if (!pushdecl_class_level (STAT_TYPE (binding
)))
4326 binding
= STAT_DECL (binding
);
4328 if (!pushdecl_class_level (binding
))
4332 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
4333 if (TREE_CODE (f
) == FIELD_DECL
)
4335 input_location
= DECL_SOURCE_LOCATION (f
);
4336 if (!pushdecl_class_level (f
))
4339 input_location
= save_location
;
4341 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4345 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4346 scope. If the value returned is non-NULL, and the PREVIOUS field
4347 is not set, callers must set the PREVIOUS field explicitly. */
4349 static cxx_binding
*
4350 get_class_binding (tree name
, cp_binding_level
*scope
)
4355 cxx_binding
*binding
;
4357 class_type
= scope
->this_entity
;
4359 /* Get the type binding. */
4360 type_binding
= lookup_member (class_type
, name
,
4361 /*protect=*/2, /*want_type=*/true,
4362 tf_warning_or_error
);
4363 /* Get the value binding. */
4364 value_binding
= lookup_member (class_type
, name
,
4365 /*protect=*/2, /*want_type=*/false,
4366 tf_warning_or_error
);
4369 && (TREE_CODE (value_binding
) == TYPE_DECL
4370 || DECL_CLASS_TEMPLATE_P (value_binding
)
4371 || (TREE_CODE (value_binding
) == TREE_LIST
4372 && TREE_TYPE (value_binding
) == error_mark_node
4373 && (TREE_CODE (TREE_VALUE (value_binding
))
4375 /* We found a type binding, even when looking for a non-type
4376 binding. This means that we already processed this binding
4379 else if (value_binding
)
4381 if (TREE_CODE (value_binding
) == TREE_LIST
4382 && TREE_TYPE (value_binding
) == error_mark_node
)
4383 /* NAME is ambiguous. */
4385 else if (BASELINK_P (value_binding
))
4386 /* NAME is some overloaded functions. */
4387 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4390 /* If we found either a type binding or a value binding, create a
4391 new binding object. */
4392 if (type_binding
|| value_binding
)
4394 binding
= new_class_binding (name
,
4398 /* This is a class-scope binding, not a block-scope binding. */
4399 LOCAL_BINDING_P (binding
) = 0;
4400 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4408 /* Make the declaration(s) of X appear in CLASS scope under the name
4409 NAME. Returns true if the binding is valid. */
4412 push_class_level_binding_1 (tree name
, tree x
)
4414 cxx_binding
*binding
;
4418 /* The class_binding_level will be NULL if x is a template
4419 parameter name in a member template. */
4420 if (!class_binding_level
)
4423 if (name
== error_mark_node
)
4426 /* Can happen for an erroneous declaration (c++/60384). */
4427 if (!identifier_p (name
))
4429 gcc_assert (errorcount
|| sorrycount
);
4433 /* Check for invalid member names. But don't worry about a default
4434 argument-scope lambda being pushed after the class is complete. */
4435 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4436 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4437 /* Check that we're pushing into the right binding level. */
4438 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4440 /* We could have been passed a tree list if this is an ambiguous
4441 declaration. If so, pull the declaration out because
4442 check_template_shadow will not handle a TREE_LIST. */
4443 if (TREE_CODE (decl
) == TREE_LIST
4444 && TREE_TYPE (decl
) == error_mark_node
)
4445 decl
= TREE_VALUE (decl
);
4447 if (!check_template_shadow (decl
))
4452 If T is the name of a class, then each of the following shall
4453 have a name different from T:
4455 -- every static data member of class T;
4457 -- every member of class T that is itself a type;
4459 -- every enumerator of every member of class T that is an
4462 -- every member of every anonymous union that is a member of
4465 (Non-static data members were also forbidden to have the same
4466 name as T until TC1.) */
4468 || TREE_CODE (x
) == CONST_DECL
4469 || (TREE_CODE (x
) == TYPE_DECL
4470 && !DECL_SELF_REFERENCE_P (x
))
4471 /* A data member of an anonymous union. */
4472 || (TREE_CODE (x
) == FIELD_DECL
4473 && DECL_CONTEXT (x
) != current_class_type
))
4474 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4476 tree scope
= context_for_name_lookup (x
);
4477 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4479 error_at (DECL_SOURCE_LOCATION (x
),
4480 "%qD has the same name as the class in which it is "
4486 /* Get the current binding for NAME in this class, if any. */
4487 binding
= IDENTIFIER_BINDING (name
);
4488 if (!binding
|| binding
->scope
!= class_binding_level
)
4490 binding
= get_class_binding (name
, class_binding_level
);
4491 /* If a new binding was created, put it at the front of the
4492 IDENTIFIER_BINDING list. */
4495 binding
->previous
= IDENTIFIER_BINDING (name
);
4496 IDENTIFIER_BINDING (name
) = binding
;
4500 /* If there is already a binding, then we may need to update the
4502 if (binding
&& binding
->value
)
4504 tree bval
= binding
->value
;
4505 tree old_decl
= NULL_TREE
;
4506 tree target_decl
= strip_using_decl (decl
);
4507 tree target_bval
= strip_using_decl (bval
);
4509 if (INHERITED_VALUE_BINDING_P (binding
))
4511 /* If the old binding was from a base class, and was for a
4512 tag name, slide it over to make room for the new binding.
4513 The old binding is still visible if explicitly qualified
4514 with a class-key. */
4515 if (TREE_CODE (target_bval
) == TYPE_DECL
4516 && DECL_ARTIFICIAL (target_bval
)
4517 && !(TREE_CODE (target_decl
) == TYPE_DECL
4518 && DECL_ARTIFICIAL (target_decl
)))
4520 old_decl
= binding
->type
;
4521 binding
->type
= bval
;
4522 binding
->value
= NULL_TREE
;
4523 INHERITED_VALUE_BINDING_P (binding
) = 0;
4528 /* Any inherited type declaration is hidden by the type
4529 declaration in the derived class. */
4530 if (TREE_CODE (target_decl
) == TYPE_DECL
4531 && DECL_ARTIFICIAL (target_decl
))
4532 binding
->type
= NULL_TREE
;
4535 else if (TREE_CODE (decl
) == USING_DECL
4536 && TREE_CODE (bval
) == USING_DECL
4537 && same_type_p (USING_DECL_SCOPE (decl
),
4538 USING_DECL_SCOPE (bval
)))
4539 /* This is a using redeclaration that will be diagnosed later
4540 in supplement_binding */
4542 else if (TREE_CODE (decl
) == USING_DECL
4543 && TREE_CODE (bval
) == USING_DECL
4544 && DECL_DEPENDENT_P (decl
)
4545 && DECL_DEPENDENT_P (bval
))
4547 else if (TREE_CODE (decl
) == USING_DECL
4548 && OVL_P (target_bval
))
4550 else if (TREE_CODE (bval
) == USING_DECL
4551 && OVL_P (target_decl
))
4553 else if (OVL_P (target_decl
)
4554 && OVL_P (target_bval
))
4557 if (old_decl
&& binding
->scope
== class_binding_level
)
4560 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4561 here. This function is only used to register bindings
4562 from with the class definition itself. */
4563 INHERITED_VALUE_BINDING_P (binding
) = 0;
4568 /* Note that we declared this value so that we can issue an error if
4569 this is an invalid redeclaration of a name already used for some
4571 note_name_declared_in_class (name
, decl
);
4573 /* If we didn't replace an existing binding, put the binding on the
4574 stack of bindings for the identifier, and update the shadowed
4576 if (binding
&& binding
->scope
== class_binding_level
)
4577 /* Supplement the existing binding. */
4578 ok
= supplement_binding (binding
, decl
);
4581 /* Create a new binding. */
4582 push_binding (name
, decl
, class_binding_level
);
4589 /* Wrapper for push_class_level_binding_1. */
4592 push_class_level_binding (tree name
, tree x
)
4595 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4596 ret
= push_class_level_binding_1 (name
, x
);
4597 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4601 /* Process and lookup a using decl SCOPE::lookup.name, filling in
4602 lookup.values & lookup.type. Return true if ok. */
4605 lookup_using_decl (tree scope
, name_lookup
&lookup
)
4607 tree current
= current_scope ();
4608 bool dependent_p
= false;
4610 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
4612 /* Naming a namespace member. */
4613 if (TYPE_P (current
))
4615 error ("using-declaration for non-member at class scope");
4619 qualified_namespace_lookup (scope
, &lookup
);
4621 else if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
4623 error ("using-declaration may not name enumerator %<%E::%D%>",
4624 scope
, lookup
.name
);
4629 /* Naming a class member. */
4630 if (!TYPE_P (current
))
4632 error ("using-declaration for member at non-class scope");
4636 /* Make sure the name is not invalid */
4637 if (TREE_CODE (lookup
.name
) == BIT_NOT_EXPR
)
4639 error ("%<%T::%D%> names destructor", scope
, lookup
.name
);
4643 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4644 if (MAYBE_CLASS_TYPE_P (scope
)
4645 && (lookup
.name
== TYPE_IDENTIFIER (scope
)
4646 || constructor_name_p (lookup
.name
, scope
)))
4648 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4649 lookup
.name
= ctor_identifier
;
4650 CLASSTYPE_NON_AGGREGATE (current
) = true;
4653 /* Cannot introduce a constructor name. */
4654 if (constructor_name_p (lookup
.name
, current
))
4656 error ("%<%T::%D%> names constructor in %qT",
4657 scope
, lookup
.name
, current
);
4661 /* Member using decls finish processing when completing the
4663 /* From [namespace.udecl]:
4665 A using-declaration used as a member-declaration shall refer
4666 to a member of a base class of the class being defined.
4668 In general, we cannot check this constraint in a template
4669 because we do not know the entire set of base classes of the
4670 current class type. Morover, if SCOPE is dependent, it might
4671 match a non-dependent base. */
4673 dependent_p
= dependent_scope_p (scope
);
4677 tree binfo
= lookup_base (current
, scope
, ba_any
, &b_kind
,
4678 tf_warning_or_error
);
4679 if (b_kind
< bk_proper_base
)
4681 /* If there are dependent bases, scope might resolve at
4682 instantiation time, even if it isn't exactly one of
4683 the dependent bases. */
4684 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4686 error_not_base_type (scope
, current
);
4689 /* Treat as-if dependent. */
4692 else if (lookup
.name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4694 error ("cannot inherit constructors from indirect base %qT",
4698 else if (IDENTIFIER_CONV_OP_P (lookup
.name
)
4699 && dependent_type_p (TREE_TYPE (lookup
.name
)))
4702 lookup
.value
= lookup_member (binfo
, lookup
.name
, 0,
4703 false, tf_warning_or_error
);
4711 error ("%qD has not been declared in %qE", lookup
.name
, scope
);
4715 if (TREE_CODE (lookup
.value
) == TREE_LIST
4716 /* We can (independently) have ambiguous implicit typedefs. */
4717 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
4719 error ("reference to %qD is ambiguous", lookup
.name
);
4720 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
4721 ? lookup
.value
: lookup
.type
);
4725 if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
4727 error ("using-declaration may not name namespace %qD", lookup
.value
);
4735 /* Process "using SCOPE::NAME" in a class scope. Return the
4736 USING_DECL created. */
4739 do_class_using_decl (tree scope
, tree name
)
4741 if (name
== error_mark_node
4742 || scope
== error_mark_node
)
4745 name_lookup
lookup (name
, 0);
4746 if (!lookup_using_decl (scope
, lookup
))
4749 tree found
= lookup
.value
;
4750 if (found
&& BASELINK_P (found
))
4751 /* The binfo from which the functions came does not matter. */
4752 found
= BASELINK_FUNCTIONS (found
);
4754 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
4755 USING_DECL_SCOPE (using_decl
) = scope
;
4756 USING_DECL_DECLS (using_decl
) = found
;
4757 DECL_DEPENDENT_P (using_decl
) = !found
;
4763 /* Return the binding for NAME in NS. If NS is NULL, look in
4764 global_namespace. */
4767 get_namespace_binding (tree ns
, tree name
)
4769 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4771 ns
= global_namespace
;
4772 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4773 tree ret
= find_namespace_value (ns
, name
);
4774 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4778 /* Push internal DECL into the global namespace. Does not do the
4779 full overload fn handling and does not add it to the list of things
4780 in the namespace. */
4783 set_global_binding (tree decl
)
4785 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4787 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4790 /* The user's placed something in the implementor's namespace. */
4791 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4793 /* Force the binding, so compiler internals continue to work. */
4796 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4799 /* Set the context of a declaration to scope. Complain if we are not
4803 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4805 /* Get rid of namespace aliases. */
4806 scope
= ORIGINAL_NAMESPACE (scope
);
4808 /* It is ok for friends to be qualified in parallel space. */
4809 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4810 error ("declaration of %qD not in a namespace surrounding %qD",
4812 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4814 /* See whether this has been declared in the namespace or inline
4816 tree old
= NULL_TREE
;
4818 name_lookup
lookup (DECL_NAME (decl
), LOOKUP_HIDDEN
);
4819 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4820 /* No old declaration at all. */
4825 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4826 if (TREE_CODE (old
) == TREE_LIST
)
4829 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4830 error ("reference to %qD is ambiguous", decl
);
4831 print_candidates (old
);
4835 if (!DECL_DECLARES_FUNCTION_P (decl
))
4837 /* Don't compare non-function decls with decls_match here, since
4838 it can't check for the correct constness at this
4839 point. pushdecl will find those errors later. */
4841 /* We might have found it in an inline namespace child of SCOPE. */
4842 if (TREE_CODE (decl
) == TREE_CODE (old
))
4843 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4846 /* Writing "N::i" to declare something directly in "N" is invalid. */
4847 if (CP_DECL_CONTEXT (decl
) == current_namespace
4848 && at_namespace_scope_p ())
4849 error_at (DECL_SOURCE_LOCATION (decl
),
4850 "explicit qualification in declaration of %qD", decl
);
4854 /* Since decl is a function, old should contain a function decl. */
4858 /* We handle these in check_explicit_instantiation_namespace. */
4859 if (processing_explicit_instantiation
)
4861 if (processing_template_decl
|| processing_specialization
)
4862 /* We have not yet called push_template_decl to turn a
4863 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4864 match. But, we'll check later, when we construct the
4867 /* Instantiations or specializations of templates may be declared as
4868 friends in any namespace. */
4869 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4875 for (lkp_iterator
iter (old
); iter
; ++iter
)
4877 if (iter
.using_p ())
4882 /* Adjust DECL_CONTEXT first so decls_match will return true
4883 if DECL will match a declaration in an inline namespace. */
4884 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4885 if (decls_match (decl
, ofn
))
4889 /* We found more than one matching declaration. */
4890 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4899 if (DECL_HIDDEN_FRIEND_P (found
))
4901 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4902 "%qD has not been declared within %qD", decl
, scope
);
4903 inform (DECL_SOURCE_LOCATION (found
),
4904 "only here as a %<friend%>");
4906 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4911 /* It didn't work, go back to the explicit scope. */
4912 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4913 error ("%qD should have been declared inside %qD", decl
, scope
);
4916 /* Return the namespace where the current declaration is declared. */
4919 current_decl_namespace (void)
4922 /* If we have been pushed into a different namespace, use it. */
4923 if (!vec_safe_is_empty (decl_namespace_list
))
4924 return decl_namespace_list
->last ();
4926 if (current_class_type
)
4927 result
= decl_namespace_context (current_class_type
);
4928 else if (current_function_decl
)
4929 result
= decl_namespace_context (current_function_decl
);
4931 result
= current_namespace
;
4935 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4936 attribute visibility is seen. */
4939 handle_namespace_attrs (tree ns
, tree attributes
)
4942 bool saw_vis
= false;
4944 if (attributes
== error_mark_node
)
4947 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
4949 tree name
= get_attribute_name (d
);
4950 tree args
= TREE_VALUE (d
);
4952 if (is_attribute_p ("visibility", name
))
4954 /* attribute visibility is a property of the syntactic block
4955 rather than the namespace as a whole, so we don't touch the
4956 NAMESPACE_DECL at all. */
4957 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
4958 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
4960 warning (OPT_Wattributes
,
4961 "%qD attribute requires a single NTBS argument",
4966 if (!TREE_PUBLIC (ns
))
4967 warning (OPT_Wattributes
,
4968 "%qD attribute is meaningless since members of the "
4969 "anonymous namespace get local symbols", name
);
4971 push_visibility (TREE_STRING_POINTER (x
), 1);
4974 else if (is_attribute_p ("abi_tag", name
))
4976 if (!DECL_NAME (ns
))
4978 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
4982 if (!DECL_NAMESPACE_INLINE_P (ns
))
4984 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
4990 tree dn
= DECL_NAME (ns
);
4991 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
4992 IDENTIFIER_POINTER (dn
));
4993 TREE_TYPE (args
) = char_array_type_node
;
4994 args
= fix_string_type (args
);
4995 args
= build_tree_list (NULL_TREE
, args
);
4997 if (check_abi_tag_args (args
, name
))
4998 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
4999 DECL_ATTRIBUTES (ns
));
5001 else if (is_attribute_p ("deprecated", name
))
5003 if (!DECL_NAME (ns
))
5005 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5009 if (args
&& TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
5011 error ("deprecated message is not a string");
5014 TREE_DEPRECATED (ns
) = 1;
5016 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5017 DECL_ATTRIBUTES (ns
));
5021 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5030 /* Temporarily set the namespace for the current declaration. */
5033 push_decl_namespace (tree decl
)
5035 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5036 decl
= decl_namespace_context (decl
);
5037 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5040 /* [namespace.memdef]/2 */
5043 pop_decl_namespace (void)
5045 decl_namespace_list
->pop ();
5048 /* Process a namespace-alias declaration. */
5051 do_namespace_alias (tree alias
, tree name_space
)
5053 if (name_space
== error_mark_node
)
5056 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5058 name_space
= ORIGINAL_NAMESPACE (name_space
);
5060 /* Build the alias. */
5061 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5062 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5063 DECL_EXTERNAL (alias
) = 1;
5064 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5067 /* Emit debug info for namespace alias. */
5068 if (!building_stmt_list_p ())
5069 (*debug_hooks
->early_global_decl
) (alias
);
5072 /* Like pushdecl, only it places X in the current namespace,
5076 pushdecl_namespace_level (tree x
, bool is_friend
)
5078 cp_binding_level
*b
= current_binding_level
;
5081 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5082 t
= do_pushdecl_with_scope
5083 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
5085 /* Now, the type_shadowed stack may screw us. Munge it so it does
5087 if (TREE_CODE (t
) == TYPE_DECL
)
5089 tree name
= DECL_NAME (t
);
5091 tree
*ptr
= (tree
*)0;
5092 for (; !global_scope_p (b
); b
= b
->level_chain
)
5094 tree shadowed
= b
->type_shadowed
;
5095 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5096 if (TREE_PURPOSE (shadowed
) == name
)
5098 ptr
= &TREE_VALUE (shadowed
);
5099 /* Can't break out of the loop here because sometimes
5100 a binding level will have duplicate bindings for
5101 PT names. It's gross, but I haven't time to fix it. */
5104 newval
= TREE_TYPE (t
);
5105 if (ptr
== (tree
*)0)
5107 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5108 up here if this is changed to an assertion. --KR */
5109 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5116 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5120 /* Process a using declaration in non-class scope. */
5123 finish_nonmember_using_decl (tree scope
, tree name
)
5125 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
5127 if (scope
== error_mark_node
|| name
== error_mark_node
)
5130 name_lookup
lookup (name
, 0);
5132 if (!lookup_using_decl (scope
, lookup
))
5135 /* Emit debug info. */
5136 if (!processing_template_decl
)
5137 cp_emit_debug_info_for_using (lookup
.value
,
5138 current_binding_level
->this_entity
);
5140 if (current_binding_level
->kind
== sk_namespace
)
5142 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5144 tree value
= MAYBE_STAT_DECL (*slot
);
5145 tree type
= MAYBE_STAT_TYPE (*slot
);
5147 do_nonmember_using_decl (lookup
, false, &value
, &type
);
5149 if (STAT_HACK_P (*slot
))
5151 STAT_DECL (*slot
) = value
;
5152 STAT_TYPE (*slot
) = type
;
5155 *slot
= stat_hack (value
, type
);
5161 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
5162 USING_DECL_SCOPE (using_decl
) = scope
;
5163 add_decl_expr (using_decl
);
5165 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5170 value
= binding
->value
;
5171 type
= binding
->type
;
5174 /* DR 36 questions why using-decls at function scope may not be
5175 duplicates. Disallow it, as C++11 claimed and PR 20420
5177 do_nonmember_using_decl (lookup
, true, &value
, &type
);
5181 else if (binding
&& value
== binding
->value
)
5183 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5185 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5186 IDENTIFIER_BINDING (name
)->value
= value
;
5189 /* Install the new binding. */
5190 push_local_binding (name
, value
, true);
5194 else if (binding
&& type
== binding
->type
)
5198 push_local_binding (name
, type
, true);
5199 set_identifier_type_value (name
, type
);
5204 /* Return the declarations that are members of the namespace NS. */
5207 cp_namespace_decls (tree ns
)
5209 return NAMESPACE_LEVEL (ns
)->names
;
5212 /* Combine prefer_type and namespaces_only into flags. */
5215 lookup_flags (int prefer_type
, int namespaces_only
)
5217 if (namespaces_only
)
5218 return LOOKUP_PREFER_NAMESPACES
;
5219 if (prefer_type
> 1)
5220 return LOOKUP_PREFER_TYPES
;
5221 if (prefer_type
> 0)
5222 return LOOKUP_PREFER_BOTH
;
5226 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5227 ignore it or not. Subroutine of lookup_name_real and
5228 lookup_type_scope. */
5231 qualify_lookup (tree val
, int flags
)
5233 if (val
== NULL_TREE
)
5235 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
5237 if (flags
& LOOKUP_PREFER_TYPES
)
5239 tree target_val
= strip_using_decl (val
);
5240 if (TREE_CODE (target_val
) == TYPE_DECL
5241 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
5244 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
5246 /* Look through lambda things that we shouldn't be able to see. */
5247 if (!(flags
& LOOKUP_HIDDEN
) && is_lambda_ignored_entity (val
))
5252 /* Is there a "using namespace std;" directive within USINGS? */
5255 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
5260 for (unsigned ix
= usings
->length (); ix
--;)
5261 if ((*usings
)[ix
] == std_node
)
5267 /* Is there a "using namespace std;" directive within the current
5268 namespace (or its ancestors)?
5269 Compare with name_lookup::search_unqualified. */
5272 has_using_namespace_std_directive_p ()
5274 /* Look at local using-directives. */
5275 for (cp_binding_level
*level
= current_binding_level
;
5277 level
= level
->level_chain
)
5278 if (using_directives_contain_std_p (level
->using_directives
))
5284 /* Subclass of deferred_diagnostic, for issuing a note when
5285 --param cxx-max-namespaces-for-diagnostic-help is reached.
5287 The note should be issued after the error, but before any other
5288 deferred diagnostics. This is handled by decorating a wrapped
5289 deferred_diagnostic, and emitting a note before that wrapped note is
5292 class namespace_limit_reached
: public deferred_diagnostic
5295 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
5296 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
5297 : deferred_diagnostic (loc
),
5298 m_limit (limit
), m_name (name
),
5299 m_wrapped (move (wrapped
))
5303 ~namespace_limit_reached ()
5305 /* Unconditionally warn that the search was truncated. */
5306 inform (get_location (),
5307 "maximum limit of %d namespaces searched for %qE",
5309 /* m_wrapped will be implicitly deleted after this, emitting any followup
5310 diagnostic after the above note. */
5316 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
5319 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5320 Emit a note showing the location of the declaration of the suggestion. */
5322 class show_candidate_location
: public deferred_diagnostic
5325 show_candidate_location (location_t loc
, tree candidate
)
5326 : deferred_diagnostic (loc
),
5327 m_candidate (candidate
)
5331 ~show_candidate_location ()
5333 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
5340 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5341 to be suggested by suggest_alternatives_for.
5343 Emit a series of notes showing the various suggestions. */
5345 class suggest_alternatives
: public deferred_diagnostic
5348 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
5349 : deferred_diagnostic (loc
),
5350 m_candidates (candidates
)
5354 ~suggest_alternatives ()
5356 if (m_candidates
.length ())
5358 inform_n (get_location (), m_candidates
.length (),
5359 "suggested alternative:",
5360 "suggested alternatives:");
5361 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
5363 tree val
= m_candidates
[ix
];
5365 inform (location_of (val
), " %qE", val
);
5368 m_candidates
.release ();
5372 vec
<tree
> m_candidates
;
5375 /* A class for encapsulating the result of a search across
5376 multiple namespaces (and scoped enums within them) for an
5377 unrecognized name seen at a given source location. */
5379 class namespace_hints
5382 namespace_hints (location_t loc
, tree name
);
5384 name_hint
convert_candidates_to_name_hint ();
5385 name_hint
maybe_decorate_with_limit (name_hint
);
5388 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
5392 vec
<tree
> m_candidates
;
5394 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5397 /* Was the limit reached? */
5401 /* Constructor for namespace_hints. Search namespaces and scoped enums,
5402 looking for an exact match for unrecognized NAME seen at LOC. */
5404 namespace_hints::namespace_hints (location_t loc
, tree name
)
5405 : m_loc(loc
), m_name (name
)
5407 auto_vec
<tree
> worklist
;
5409 m_candidates
= vNULL
;
5411 m_limit
= param_cxx_max_namespaces_for_diagnostic_help
;
5413 /* Breadth-first search of namespaces. Up to limit namespaces
5414 searched (limit zero == unlimited). */
5415 worklist
.safe_push (global_namespace
);
5416 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5418 tree ns
= worklist
[ix
];
5419 name_lookup
lookup (name
);
5421 if (lookup
.search_qualified (ns
, false))
5422 m_candidates
.safe_push (lookup
.value
);
5426 /* Look for child namespaces. We have to do this
5427 indirectly because they are chained in reverse order,
5428 which is confusing to the user. */
5429 auto_vec
<tree
> children
;
5431 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5432 decl
; decl
= TREE_CHAIN (decl
))
5434 if (TREE_CODE (decl
) == NAMESPACE_DECL
5435 && !DECL_NAMESPACE_ALIAS (decl
)
5436 && !DECL_NAMESPACE_INLINE_P (decl
))
5437 children
.safe_push (decl
);
5439 /* Look for exact matches for NAME within scoped enums.
5440 These aren't added to the worklist, and so don't count
5441 against the search limit. */
5442 if (TREE_CODE (decl
) == TYPE_DECL
)
5444 tree type
= TREE_TYPE (decl
);
5445 if (SCOPED_ENUM_P (type
))
5446 maybe_add_candidate_for_scoped_enum (type
, name
);
5450 while (!m_limited
&& !children
.is_empty ())
5452 if (worklist
.length () == m_limit
)
5455 worklist
.safe_push (children
.pop ());
5461 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5462 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5464 If m_candidates is non-empty, use it to generate a suggestion and/or
5465 a deferred diagnostic that lists the possible candidate(s).
5469 namespace_hints::convert_candidates_to_name_hint ()
5471 /* How many candidates do we have? */
5473 /* If we have just one candidate, issue a name_hint with it as a suggestion
5474 (so that consumers are able to suggest it within the error message and emit
5475 it as a fix-it hint), and with a note showing the candidate's location. */
5476 if (m_candidates
.length () == 1)
5478 tree candidate
= m_candidates
[0];
5479 /* Clean up CANDIDATES. */
5480 m_candidates
.release ();
5481 return name_hint (expr_to_string (candidate
),
5482 new show_candidate_location (m_loc
, candidate
));
5484 else if (m_candidates
.length () > 1)
5485 /* If we have more than one candidate, issue a name_hint without a single
5486 "suggestion", but with a deferred diagnostic that lists the
5487 various candidates. This takes ownership of m_candidates. */
5488 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
5490 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5491 gcc_assert (m_candidates
.length () == 0);
5492 gcc_assert (m_candidates
== vNULL
);
5494 return name_hint ();
5497 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5498 then we want to emit a note about after the error, but before
5499 any other deferred diagnostics.
5501 Handle this by figuring out what hint is needed, then optionally
5502 decorating HINT with a namespace_limit_reached wrapper. */
5505 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
5508 return name_hint (hint
.suggestion (),
5509 new namespace_limit_reached (m_loc
, m_limit
,
5511 hint
.take_deferred ()));
5516 /* Look inside SCOPED_ENUM for exact matches for NAME.
5517 If one is found, add its CONST_DECL to m_candidates. */
5520 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
5523 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5525 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5527 tree id
= TREE_PURPOSE (iter
);
5530 m_candidates
.safe_push (TREE_VALUE (iter
));
5536 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5539 Search through all available namespaces and any scoped enums within them
5540 and generate a suggestion and/or a deferred diagnostic that lists possible
5543 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5544 look for near-matches and suggest the best near-match, if there is one.
5546 If nothing is found, then an empty name_hint is returned. */
5549 suggest_alternatives_for (location_t location
, tree name
,
5550 bool suggest_misspellings
)
5552 /* First, search for exact matches in other namespaces. */
5553 namespace_hints
ns_hints (location
, name
);
5554 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5556 /* Otherwise, try other approaches. */
5558 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
5560 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5563 /* The second half of suggest_alternatives_for, for when no exact matches
5564 were found in other namespaces. */
5567 suggest_alternatives_for_1 (location_t location
, tree name
,
5568 bool suggest_misspellings
)
5570 /* No candidates were found in the available namespaces. */
5572 /* If there's a "using namespace std;" active, and this
5573 is one of the most common "std::" names, then it's probably a
5574 missing #include. */
5575 if (has_using_namespace_std_directive_p ())
5577 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
5582 /* Otherwise, consider misspellings. */
5583 if (!suggest_misspellings
)
5584 return name_hint ();
5586 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
5589 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5592 Search through all available namespaces and generate a suggestion and/or
5593 a deferred diagnostic that lists possible candidate(s).
5595 This is similiar to suggest_alternatives_for, but doesn't fallback to
5596 the other approaches used by that function. */
5599 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
5601 namespace_hints
ns_hints (location
, name
);
5603 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5605 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5608 /* A well-known name within the C++ standard library, returned by
5609 get_std_name_hint. */
5611 struct std_name_hint
5613 /* A name within "std::". */
5616 /* The header name defining it within the C++ Standard Library
5617 (with '<' and '>'). */
5620 /* The dialect of C++ in which this was added. */
5621 enum cxx_dialect min_dialect
;
5624 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5625 for some of the most common names within "std::".
5626 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5628 static const std_name_hint
*
5629 get_std_name_hint (const char *name
)
5631 static const std_name_hint hints
[] = {
5633 {"any", "<any>", cxx17
},
5634 {"any_cast", "<any>", cxx17
},
5635 {"make_any", "<any>", cxx17
},
5637 {"array", "<array>", cxx11
},
5638 {"to_array", "<array>", cxx20
},
5640 {"atomic", "<atomic>", cxx11
},
5641 {"atomic_flag", "<atomic>", cxx11
},
5642 {"atomic_ref", "<atomic>", cxx20
},
5644 {"bitset", "<bitset>", cxx11
},
5646 {"weak_equality", "<compare>", cxx20
},
5647 {"strong_equality", "<compare>", cxx20
},
5648 {"partial_ordering", "<compare>", cxx20
},
5649 {"weak_ordering", "<compare>", cxx20
},
5650 {"strong_ordering", "<compare>", cxx20
},
5652 {"complex", "<complex>", cxx98
},
5653 {"complex_literals", "<complex>", cxx14
},
5654 /* <condition_variable>. */
5655 {"condition_variable", "<condition_variable>", cxx11
},
5656 {"condition_variable_any", "<condition_variable>", cxx11
},
5658 {"byte", "<cstddef>", cxx17
},
5660 {"deque", "<deque>", cxx98
},
5661 /* <forward_list>. */
5662 {"forward_list", "<forward_list>", cxx11
},
5664 {"basic_filebuf", "<fstream>", cxx98
},
5665 {"basic_ifstream", "<fstream>", cxx98
},
5666 {"basic_ofstream", "<fstream>", cxx98
},
5667 {"basic_fstream", "<fstream>", cxx98
},
5668 {"fstream", "<fstream>", cxx98
},
5669 {"ifstream", "<fstream>", cxx98
},
5670 {"ofstream", "<fstream>", cxx98
},
5672 {"bind", "<functional>", cxx11
},
5673 {"bind_front", "<functional>", cxx20
},
5674 {"function", "<functional>", cxx11
},
5675 {"hash", "<functional>", cxx11
},
5676 {"invoke", "<functional>", cxx17
},
5677 {"mem_fn", "<functional>", cxx11
},
5678 {"not_fn", "<functional>", cxx17
},
5679 {"reference_wrapper", "<functional>", cxx11
},
5680 {"unwrap_reference", "<functional>", cxx20
},
5681 {"unwrap_reference_t", "<functional>", cxx20
},
5682 {"unwrap_ref_decay", "<functional>", cxx20
},
5683 {"unwrap_ref_decay_t", "<functional>", cxx20
},
5685 {"async", "<future>", cxx11
},
5686 {"future", "<future>", cxx11
},
5687 {"packaged_task", "<future>", cxx11
},
5688 {"promise", "<future>", cxx11
},
5690 {"cin", "<iostream>", cxx98
},
5691 {"cout", "<iostream>", cxx98
},
5692 {"cerr", "<iostream>", cxx98
},
5693 {"clog", "<iostream>", cxx98
},
5694 {"wcin", "<iostream>", cxx98
},
5695 {"wcout", "<iostream>", cxx98
},
5696 {"wclog", "<iostream>", cxx98
},
5698 {"istream", "<istream>", cxx98
},
5700 {"advance", "<iterator>", cxx98
},
5701 {"back_inserter", "<iterator>", cxx98
},
5702 {"begin", "<iterator>", cxx11
},
5703 {"distance", "<iterator>", cxx98
},
5704 {"end", "<iterator>", cxx11
},
5705 {"front_inserter", "<iterator>", cxx98
},
5706 {"inserter", "<iterator>", cxx98
},
5707 {"istream_iterator", "<iterator>", cxx98
},
5708 {"istreambuf_iterator", "<iterator>", cxx98
},
5709 {"iterator_traits", "<iterator>", cxx98
},
5710 {"move_iterator", "<iterator>", cxx11
},
5711 {"next", "<iterator>", cxx11
},
5712 {"ostream_iterator", "<iterator>", cxx98
},
5713 {"ostreambuf_iterator", "<iterator>", cxx98
},
5714 {"prev", "<iterator>", cxx11
},
5715 {"reverse_iterator", "<iterator>", cxx98
},
5717 {"ostream", "<ostream>", cxx98
},
5719 {"list", "<list>", cxx98
},
5721 {"map", "<map>", cxx98
},
5722 {"multimap", "<map>", cxx98
},
5724 {"allocate_shared", "<memory>", cxx11
},
5725 {"allocator", "<memory>", cxx98
},
5726 {"allocator_traits", "<memory>", cxx11
},
5727 {"make_shared", "<memory>", cxx11
},
5728 {"make_unique", "<memory>", cxx14
},
5729 {"shared_ptr", "<memory>", cxx11
},
5730 {"unique_ptr", "<memory>", cxx11
},
5731 {"weak_ptr", "<memory>", cxx11
},
5732 /* <memory_resource>. */
5733 {"pmr", "<memory_resource>", cxx17
},
5735 {"mutex", "<mutex>", cxx11
},
5736 {"timed_mutex", "<mutex>", cxx11
},
5737 {"recursive_mutex", "<mutex>", cxx11
},
5738 {"recursive_timed_mutex", "<mutex>", cxx11
},
5739 {"once_flag", "<mutex>", cxx11
},
5740 {"call_once,", "<mutex>", cxx11
},
5741 {"lock", "<mutex>", cxx11
},
5742 {"scoped_lock", "<mutex>", cxx17
},
5743 {"try_lock", "<mutex>", cxx11
},
5744 {"lock_guard", "<mutex>", cxx11
},
5745 {"unique_lock", "<mutex>", cxx11
},
5747 {"optional", "<optional>", cxx17
},
5748 {"make_optional", "<optional>", cxx17
},
5750 {"ostream", "<ostream>", cxx98
},
5751 {"wostream", "<ostream>", cxx98
},
5752 {"ends", "<ostream>", cxx98
},
5753 {"flush", "<ostream>", cxx98
},
5754 {"endl", "<ostream>", cxx98
},
5756 {"queue", "<queue>", cxx98
},
5757 {"priority_queue", "<queue>", cxx98
},
5759 {"set", "<set>", cxx98
},
5760 {"multiset", "<set>", cxx98
},
5761 /* <shared_mutex>. */
5762 {"shared_lock", "<shared_mutex>", cxx14
},
5763 {"shared_mutex", "<shared_mutex>", cxx17
},
5764 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
5765 /* <source_location>. */
5766 {"source_location", "<source_location>", cxx20
},
5768 {"basic_stringbuf", "<sstream>", cxx98
},
5769 {"basic_istringstream", "<sstream>", cxx98
},
5770 {"basic_ostringstream", "<sstream>", cxx98
},
5771 {"basic_stringstream", "<sstream>", cxx98
},
5772 {"istringstream", "<sstream>", cxx98
},
5773 {"ostringstream", "<sstream>", cxx98
},
5774 {"stringstream", "<sstream>", cxx98
},
5776 {"stack", "<stack>", cxx98
},
5778 {"basic_string", "<string>", cxx98
},
5779 {"string", "<string>", cxx98
},
5780 {"wstring", "<string>", cxx98
},
5781 {"u8string", "<string>", cxx20
},
5782 {"u16string", "<string>", cxx11
},
5783 {"u32string", "<string>", cxx11
},
5784 /* <string_view>. */
5785 {"basic_string_view", "<string_view>", cxx17
},
5786 {"string_view", "<string_view>", cxx17
},
5788 {"thread", "<thread>", cxx11
},
5789 {"this_thread", "<thread>", cxx11
},
5791 {"apply", "<tuple>", cxx17
},
5792 {"forward_as_tuple", "<tuple>", cxx11
},
5793 {"make_from_tuple", "<tuple>", cxx17
},
5794 {"make_tuple", "<tuple>", cxx11
},
5795 {"tie", "<tuple>", cxx11
},
5796 {"tuple", "<tuple>", cxx11
},
5797 {"tuple_cat", "<tuple>", cxx11
},
5798 {"tuple_element", "<tuple>", cxx11
},
5799 {"tuple_element_t", "<tuple>", cxx14
},
5800 {"tuple_size", "<tuple>", cxx11
},
5801 {"tuple_size_v", "<tuple>", cxx17
},
5802 /* <type_traits>. */
5803 {"enable_if", "<type_traits>", cxx11
},
5804 {"enable_if_t", "<type_traits>", cxx14
},
5805 {"invoke_result", "<type_traits>", cxx17
},
5806 {"invoke_result_t", "<type_traits>", cxx17
},
5807 {"remove_cvref", "<type_traits>", cxx20
},
5808 {"remove_cvref_t", "<type_traits>", cxx20
},
5809 {"type_identity", "<type_traits>", cxx20
},
5810 {"type_identity_t", "<type_traits>", cxx20
},
5811 {"void_t", "<type_traits>", cxx17
},
5812 {"conjunction", "<type_traits>", cxx17
},
5813 {"conjunction_v", "<type_traits>", cxx17
},
5814 {"disjunction", "<type_traits>", cxx17
},
5815 {"disjunction_v", "<type_traits>", cxx17
},
5816 {"negation", "<type_traits>", cxx17
},
5817 {"negation_v", "<type_traits>", cxx17
},
5818 /* <unordered_map>. */
5819 {"unordered_map", "<unordered_map>", cxx11
},
5820 {"unordered_multimap", "<unordered_map>", cxx11
},
5821 /* <unordered_set>. */
5822 {"unordered_set", "<unordered_set>", cxx11
},
5823 {"unordered_multiset", "<unordered_set>", cxx11
},
5825 {"declval", "<utility>", cxx11
},
5826 {"forward", "<utility>", cxx11
},
5827 {"make_pair", "<utility>", cxx98
},
5828 {"move", "<utility>", cxx11
},
5829 {"pair", "<utility>", cxx98
},
5831 {"variant", "<variant>", cxx17
},
5832 {"visit", "<variant>", cxx17
},
5834 {"vector", "<vector>", cxx98
},
5836 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5837 for (size_t i
= 0; i
< num_hints
; i
++)
5839 if (strcmp (name
, hints
[i
].name
) == 0)
5845 /* Describe DIALECT. */
5848 get_cxx_dialect_name (enum cxx_dialect dialect
)
5867 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5868 that weren't recognized, but for which we know which header it ought to be
5871 Emit a note either suggesting the header to be included, or noting that
5872 the current dialect is too early for the given name. */
5874 class missing_std_header
: public deferred_diagnostic
5877 missing_std_header (location_t loc
,
5878 const char *name_str
,
5879 const std_name_hint
*header_hint
)
5880 : deferred_diagnostic (loc
),
5881 m_name_str (name_str
),
5882 m_header_hint (header_hint
)
5884 ~missing_std_header ()
5886 gcc_rich_location
richloc (get_location ());
5887 if (cxx_dialect
>= m_header_hint
->min_dialect
)
5889 const char *header
= m_header_hint
->header
;
5890 maybe_add_include_fixit (&richloc
, header
, true);
5892 "%<std::%s%> is defined in header %qs;"
5893 " did you forget to %<#include %s%>?",
5894 m_name_str
, header
, header
);
5898 "%<std::%s%> is only available from %s onwards",
5899 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
5903 const char *m_name_str
;
5904 const std_name_hint
*m_header_hint
;
5907 /* Attempt to generate a name_hint that suggests pertinent header files
5908 for NAME at LOCATION, for common names within the "std" namespace,
5909 or an empty name_hint if this isn't applicable. */
5912 maybe_suggest_missing_std_header (location_t location
, tree name
)
5914 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5916 const char *name_str
= IDENTIFIER_POINTER (name
);
5917 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
5919 return name_hint ();
5921 return name_hint (NULL
, new missing_std_header (location
, name_str
,
5925 /* Attempt to generate a name_hint that suggests a missing header file
5926 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5930 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5932 if (scope
== NULL_TREE
)
5933 return name_hint ();
5934 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5935 return name_hint ();
5936 /* We only offer suggestions for the "std" namespace. */
5937 if (scope
!= std_node
)
5938 return name_hint ();
5939 return maybe_suggest_missing_std_header (location
, name
);
5942 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
5943 lookup failed within the explicitly provided SCOPE.
5945 Suggest the best meaningful candidates (if any), otherwise
5946 an empty name_hint is returned. */
5949 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
5952 /* Something went very wrong; don't suggest anything. */
5953 if (name
== error_mark_node
)
5954 return name_hint ();
5956 /* Resolve any namespace aliases. */
5957 scope
= ORIGINAL_NAMESPACE (scope
);
5959 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
5963 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
5965 best_match
<tree
, const char *> bm (name
);
5966 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
5968 /* See if we have a good suggesion for the user. */
5969 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
5971 return name_hint (fuzzy_name
, NULL
);
5973 return name_hint ();
5976 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
5980 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
5982 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5984 best_match
<tree
, const char *> bm (name
);
5985 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5987 tree id
= TREE_PURPOSE (iter
);
5988 bm
.consider (IDENTIFIER_POINTER (id
));
5990 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
5993 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5996 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
5997 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
5999 Returns a DECL (or OVERLOAD, or BASELINK) representing the
6000 declaration found. If no suitable declaration can be found,
6001 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
6002 neither a class-type nor a namespace a diagnostic is issued. */
6005 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
6006 bool find_hidden
/*=false*/)
6010 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
6012 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
6014 flags
|= LOOKUP_HIDDEN
;
6015 name_lookup
lookup (name
, flags
);
6017 if (qualified_namespace_lookup (scope
, &lookup
))
6020 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
6021 t
= lookup_enumerator (scope
, name
);
6022 else if (is_class_type (scope
, complain
))
6023 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
6026 return error_mark_node
;
6030 /* Wrapper for the above that takes a string argument. The function name is
6031 not at the beginning of the line to keep this wrapper out of etags. */
6033 tree
lookup_qualified_name (tree t
, const char *p
, int wt
, bool c
, bool fh
)
6034 { return lookup_qualified_name (t
, get_identifier (p
), wt
, c
, fh
); }
6037 Accepts the NAME to lookup and its qualifying SCOPE.
6038 Returns the name/type pair found into the cxx_binding *RESULT,
6039 or false on error. */
6042 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
6044 timevar_start (TV_NAME_LOOKUP
);
6045 query_oracle (lookup
->name
);
6046 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
6047 timevar_stop (TV_NAME_LOOKUP
);
6051 /* Helper function for lookup_name_fuzzy.
6052 Traverse binding level LVL, looking for good name matches for NAME
6055 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
6056 cp_binding_level
*lvl
, bool look_within_fields
,
6057 enum lookup_name_fuzzy_kind kind
)
6059 if (look_within_fields
)
6060 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
6062 tree type
= lvl
->this_entity
;
6063 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
6064 tree best_matching_field
6065 = lookup_member_fuzzy (type
, name
, want_type_p
);
6066 if (best_matching_field
)
6067 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
6070 /* Only suggest names reserved for the implementation if NAME begins
6071 with an underscore. */
6072 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
6074 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
6078 /* OVERLOADs or decls from using declaration are wrapped into
6080 if (TREE_CODE (d
) == TREE_LIST
)
6081 d
= OVL_FIRST (TREE_VALUE (d
));
6083 /* Don't use bindings from implicitly declared functions,
6084 as they were likely misspellings themselves. */
6085 if (TREE_TYPE (d
) == error_mark_node
)
6088 /* Skip anticipated decls of builtin functions. */
6089 if (TREE_CODE (d
) == FUNCTION_DECL
6090 && fndecl_built_in_p (d
)
6091 && DECL_ANTICIPATED (d
))
6094 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6095 within range for). */
6096 if (TREE_CODE (d
) == VAR_DECL
6097 && DECL_ARTIFICIAL (d
))
6100 tree suggestion
= DECL_NAME (d
);
6104 /* Don't suggest names that are for anonymous aggregate types, as
6105 they are an implementation detail generated by the compiler. */
6106 if (IDENTIFIER_ANON_P (suggestion
))
6109 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
6111 /* Ignore internal names with spaces in them. */
6112 if (strchr (suggestion_str
, ' '))
6115 /* Don't suggest names that are reserved for use by the
6116 implementation, unless NAME began with an underscore. */
6117 if (name_reserved_for_implementation_p (suggestion_str
)
6118 && !consider_implementation_names
)
6121 bm
.consider (suggestion_str
);
6125 /* Subclass of deferred_diagnostic. Notify the user that the
6126 given macro was used before it was defined.
6127 This can be done in the C++ frontend since tokenization happens
6130 class macro_use_before_def
: public deferred_diagnostic
6133 /* Factory function. Return a new macro_use_before_def instance if
6134 appropriate, or return NULL. */
6135 static macro_use_before_def
*
6136 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
6138 location_t def_loc
= cpp_macro_definition_location (macro
);
6139 if (def_loc
== UNKNOWN_LOCATION
)
6142 /* We only want to issue a note if the macro was used *before* it was
6144 We don't want to issue a note for cases where a macro was incorrectly
6145 used, leaving it unexpanded (e.g. by using the wrong argument
6147 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
6150 return new macro_use_before_def (use_loc
, macro
);
6154 /* Ctor. LOC is the location of the usage. MACRO is the
6155 macro that was used. */
6156 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
6157 : deferred_diagnostic (loc
), m_macro (macro
)
6162 ~macro_use_before_def ()
6164 if (is_suppressed_p ())
6167 inform (get_location (), "the macro %qs had not yet been defined",
6168 (const char *)m_macro
->ident
.str
);
6169 inform (cpp_macro_definition_location (m_macro
),
6170 "it was later defined here");
6174 cpp_hashnode
*m_macro
;
6177 /* Determine if it can ever make sense to offer RID as a suggestion for
6180 Subroutine of lookup_name_fuzzy. */
6183 suggest_rid_p (enum rid rid
)
6187 /* Support suggesting function-like keywords. */
6188 case RID_STATIC_ASSERT
:
6192 /* Support suggesting the various decl-specifier words, to handle
6193 e.g. "singed" vs "signed" typos. */
6194 if (cp_keyword_starts_decl_specifier_p (rid
))
6197 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6198 and "do" for short misspellings, which are likely to lead to
6199 nonsensical results. */
6204 /* Search for near-matches for NAME within the current bindings, and within
6205 macro names, returning the best match as a const char *, or NULL if
6206 no reasonable match is found.
6208 Use LOC for any deferred diagnostics. */
6211 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
6213 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
6215 /* First, try some well-known names in the C++ standard library, in case
6216 the user forgot a #include. */
6217 const char *header_hint
6218 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
6220 return name_hint (NULL
,
6221 new suggest_missing_header (loc
,
6222 IDENTIFIER_POINTER (name
),
6225 best_match
<tree
, const char *> bm (name
);
6227 cp_binding_level
*lvl
;
6228 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
6229 consider_binding_level (name
, bm
, lvl
, true, kind
);
6231 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
6232 consider_binding_level (name
, bm
, lvl
, false, kind
);
6234 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6236 x = SOME_OTHER_MACRO (y);
6237 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6238 as a misspelled identifier.
6240 Use the best distance so far so that a candidate is only set if
6241 a macro is better than anything so far. This allows early rejection
6242 (without calculating the edit distance) of macro names that must have
6243 distance >= bm.get_best_distance (), and means that we only get a
6244 non-NULL result for best_macro_match if it's better than any of
6245 the identifiers already checked. */
6246 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
6247 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
6248 /* If a macro is the closest so far to NAME, consider it. */
6250 bm
.consider ((const char *)best_macro
->ident
.str
);
6251 else if (bmm
.get_best_distance () == 0)
6253 /* If we have an exact match for a macro name, then either the
6254 macro was used with the wrong argument count, or the macro
6255 has been used before it was defined. */
6256 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
6257 if (cpp_user_macro_p (macro
))
6258 return name_hint (NULL
,
6259 macro_use_before_def::maybe_make (loc
, macro
));
6262 /* Try the "starts_decl_specifier_p" keywords to detect
6263 "singed" vs "signed" typos. */
6264 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
6266 const c_common_resword
*resword
= &c_common_reswords
[i
];
6268 if (!suggest_rid_p (resword
->rid
))
6271 tree resword_identifier
= ridpointers
[resword
->rid
];
6272 if (!resword_identifier
)
6274 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
6276 /* Only consider reserved words that survived the
6277 filtering in init_reswords (e.g. for -std). */
6278 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
6281 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
6284 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
6287 /* Subroutine of outer_binding.
6289 Returns TRUE if BINDING is a binding to a template parameter of
6290 SCOPE. In that case SCOPE is the scope of a primary template
6291 parameter -- in the sense of G++, i.e, a template that has its own
6294 Returns FALSE otherwise. */
6297 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
6298 cp_binding_level
*scope
)
6300 tree binding_value
, tmpl
, tinfo
;
6303 if (!binding
|| !scope
|| !scope
->this_entity
)
6306 binding_value
= binding
->value
? binding
->value
: binding
->type
;
6307 tinfo
= get_template_info (scope
->this_entity
);
6309 /* BINDING_VALUE must be a template parm. */
6310 if (binding_value
== NULL_TREE
6311 || (!DECL_P (binding_value
)
6312 || !DECL_TEMPLATE_PARM_P (binding_value
)))
6315 /* The level of BINDING_VALUE. */
6317 template_type_parameter_p (binding_value
)
6318 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6319 (TREE_TYPE (binding_value
)))
6320 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
6322 /* The template of the current scope, iff said scope is a primary
6325 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
6326 ? TI_TEMPLATE (tinfo
)
6329 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6330 then BINDING_VALUE is a parameter of TMPL. */
6331 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
6334 /* Return the innermost non-namespace binding for NAME from a scope
6335 containing BINDING, or, if BINDING is NULL, the current scope.
6336 Please note that for a given template, the template parameters are
6337 considered to be in the scope containing the current scope.
6338 If CLASS_P is false, then class bindings are ignored. */
6341 outer_binding (tree name
,
6342 cxx_binding
*binding
,
6346 cp_binding_level
*scope
;
6347 cp_binding_level
*outer_scope
;
6351 scope
= binding
->scope
->level_chain
;
6352 outer
= binding
->previous
;
6356 scope
= current_binding_level
;
6357 outer
= IDENTIFIER_BINDING (name
);
6359 outer_scope
= outer
? outer
->scope
: NULL
;
6361 /* Because we create class bindings lazily, we might be missing a
6362 class binding for NAME. If there are any class binding levels
6363 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6364 declared, we must lookup NAME in those class scopes. */
6366 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
6368 if (scope
->kind
== sk_class
)
6370 cxx_binding
*class_binding
;
6372 class_binding
= get_class_binding (name
, scope
);
6375 /* Thread this new class-scope binding onto the
6376 IDENTIFIER_BINDING list so that future lookups
6378 class_binding
->previous
= outer
;
6380 binding
->previous
= class_binding
;
6382 IDENTIFIER_BINDING (name
) = class_binding
;
6383 return class_binding
;
6386 /* If we are in a member template, the template parms of the member
6387 template are considered to be inside the scope of the containing
6388 class, but within G++ the class bindings are all pushed between the
6389 template parms and the function body. So if the outer binding is
6390 a template parm for the current scope, return it now rather than
6391 look for a class binding. */
6392 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
6393 && binding_to_template_parms_of_scope_p (outer
, scope
))
6396 scope
= scope
->level_chain
;
6402 /* Return the innermost block-scope or class-scope value binding for
6403 NAME, or NULL_TREE if there is no such binding. */
6406 innermost_non_namespace_value (tree name
)
6408 cxx_binding
*binding
;
6409 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
6410 return binding
? binding
->value
: NULL_TREE
;
6413 /* Look up NAME in the current binding level and its superiors in the
6414 namespace of variables, functions and typedefs. Return a ..._DECL
6415 node of some kind representing its definition if there is only one
6416 such declaration, or return a TREE_LIST with all the overloaded
6417 definitions if there are many, or return 0 if it is undefined.
6418 Hidden name, either friend declaration or built-in function, are
6421 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6422 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6423 Otherwise we prefer non-TYPE_DECLs.
6425 If NONCLASS is nonzero, bindings in class scopes are ignored. If
6426 BLOCK_P is false, bindings in block scopes are ignored. */
6429 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6430 int namespaces_only
, int flags
)
6433 tree val
= NULL_TREE
;
6435 query_oracle (name
);
6437 /* Conversion operators are handled specially because ordinary
6438 unqualified name lookup will not find template conversion
6440 if (IDENTIFIER_CONV_OP_P (name
))
6442 cp_binding_level
*level
;
6444 for (level
= current_binding_level
;
6445 level
&& level
->kind
!= sk_namespace
;
6446 level
= level
->level_chain
)
6451 /* A conversion operator can only be declared in a class
6453 if (level
->kind
!= sk_class
)
6456 /* Lookup the conversion operator in the class. */
6457 class_type
= level
->this_entity
;
6458 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0,
6459 tf_warning_or_error
);
6467 flags
|= lookup_flags (prefer_type
, namespaces_only
);
6469 /* First, look in non-namespace scopes. */
6471 if (current_class_type
== NULL_TREE
)
6474 if (block_p
|| !nonclass
)
6475 for (iter
= outer_binding (name
, NULL
, !nonclass
);
6477 iter
= outer_binding (name
, iter
, !nonclass
))
6481 /* Skip entities we don't want. */
6482 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
6485 /* If this is the kind of thing we're looking for, we're done. */
6486 if (qualify_lookup (iter
->value
, flags
))
6487 binding
= iter
->value
;
6488 else if ((flags
& LOOKUP_PREFER_TYPES
)
6489 && qualify_lookup (iter
->type
, flags
))
6490 binding
= iter
->type
;
6492 binding
= NULL_TREE
;
6496 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
6498 /* A non namespace-scope binding can only be hidden in the
6499 presence of a local class, due to friend declarations.
6501 In particular, consider:
6509 B* b; // error: B is hidden
6510 C* c; // OK, finds ::C
6513 B *b; // error: B is hidden
6514 C *c; // OK, finds ::C
6519 The standard says that "B" is a local class in "f"
6520 (but not nested within "A") -- but that name lookup
6521 for "B" does not find this declaration until it is
6522 declared directly with "f".
6528 If a friend declaration appears in a local class and
6529 the name specified is an unqualified name, a prior
6530 declaration is looked up without considering scopes
6531 that are outside the innermost enclosing non-class
6532 scope. For a friend function declaration, if there is
6533 no prior declaration, the program is ill-formed. For a
6534 friend class declaration, if there is no prior
6535 declaration, the class that is specified belongs to the
6536 innermost enclosing non-class scope, but if it is
6537 subsequently referenced, its name is not found by name
6538 lookup until a matching declaration is provided in the
6539 innermost enclosing nonclass scope.
6541 So just keep looking for a non-hidden binding.
6543 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
6551 /* Now lookup in namespace scopes. */
6554 name_lookup
lookup (name
, flags
);
6555 if (lookup
.search_unqualified
6556 (current_decl_namespace (), current_binding_level
))
6560 /* If we have a single function from a using decl, pull it out. */
6561 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6562 val
= OVL_FUNCTION (val
);
6567 /* Wrapper for lookup_name_real_1. */
6570 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
6571 int namespaces_only
, int flags
)
6574 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6575 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
6576 namespaces_only
, flags
);
6577 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6582 lookup_name_nonclass (tree name
)
6584 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
6588 lookup_name (tree name
)
6590 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
6594 lookup_name_prefer_type (tree name
, int prefer_type
)
6596 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
6599 /* Look up NAME for type used in elaborated name specifier in
6600 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
6601 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
6602 name, more scopes are checked if cleanup or template parameter
6603 scope is encountered.
6605 Unlike lookup_name_real, we make sure that NAME is actually
6606 declared in the desired scope, not from inheritance, nor using
6607 directive. For using declaration, there is DR138 still waiting
6608 to be resolved. Hidden name coming from an earlier friend
6609 declaration is also returned.
6611 A TYPE_DECL best matching the NAME is returned. Catching error
6612 and issuing diagnostics are caller's responsibility. */
6615 lookup_type_scope_1 (tree name
, tag_scope scope
)
6617 cp_binding_level
*b
= current_binding_level
;
6619 if (b
->kind
!= sk_namespace
)
6620 /* Look in non-namespace scopes. */
6621 for (cxx_binding
*iter
= NULL
;
6622 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
6624 /* First check we're supposed to be looking in this scope --
6625 if we're not, we're done. */
6626 for (; b
!= iter
->scope
; b
= b
->level_chain
)
6627 if (!(b
->kind
== sk_cleanup
6628 || b
->kind
== sk_template_parms
6629 || b
->kind
== sk_function_parms
6630 || (b
->kind
== sk_class
6631 && scope
== ts_within_enclosing_non_class
)))
6634 /* Check if this is the kind of thing we're looking for. If
6635 SCOPE is TS_CURRENT, also make sure it doesn't come from
6636 base class. For ITER->VALUE, we can simply use
6637 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to
6640 We check ITER->TYPE before ITER->VALUE in order to handle
6641 typedef struct C {} C;
6643 if (tree type
= iter
->type
)
6644 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
)
6645 && (scope
!= ts_current
6646 || LOCAL_BINDING_P (iter
)
6647 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
6650 if (qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
)
6651 && (scope
!= ts_current
6652 || !INHERITED_VALUE_BINDING_P (iter
)))
6656 /* Now check if we can look in namespace scope. */
6657 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
6658 if (!(b
->kind
== sk_cleanup
6659 || b
->kind
== sk_template_parms
6660 || b
->kind
== sk_function_parms
6661 || (b
->kind
== sk_class
6662 && scope
== ts_within_enclosing_non_class
)))
6665 /* Look in the innermost namespace. */
6666 tree ns
= b
->this_entity
;
6667 if (tree
*slot
= find_namespace_slot (ns
, name
))
6669 /* If this is the kind of thing we're looking for, we're done. */
6670 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6671 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
6674 if (tree decl
= MAYBE_STAT_DECL (*slot
))
6675 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
6682 /* Wrapper for lookup_type_scope_1. */
6685 lookup_type_scope (tree name
, tag_scope scope
)
6688 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6689 ret
= lookup_type_scope_1 (name
, scope
);
6690 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6694 /* Returns true iff DECL is a block-scope extern declaration of a function
6698 is_local_extern (tree decl
)
6700 cxx_binding
*binding
;
6702 /* For functions, this is easy. */
6703 if (TREE_CODE (decl
) == FUNCTION_DECL
)
6704 return DECL_LOCAL_FUNCTION_P (decl
);
6708 if (!current_function_decl
)
6711 /* For variables, this is not easy. We need to look at the binding stack
6712 for the identifier to see whether the decl we have is a local. */
6713 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
6714 binding
&& binding
->scope
->kind
!= sk_namespace
;
6715 binding
= binding
->previous
)
6716 if (binding
->value
== decl
)
6717 return LOCAL_BINDING_P (binding
);
6722 /* The type TYPE is being declared. If it is a class template, or a
6723 specialization of a class template, do any processing required and
6724 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6725 being declared a friend. B is the binding level at which this TYPE
6728 Returns the TYPE_DECL for TYPE, which may have been altered by this
6732 maybe_process_template_type_declaration (tree type
, int is_friend
,
6733 cp_binding_level
*b
)
6735 tree decl
= TYPE_NAME (type
);
6737 if (processing_template_parmlist
)
6738 /* You can't declare a new template type in a template parameter
6739 list. But, you can declare a non-template type:
6741 template <class A*> struct S;
6743 is a forward-declaration of `A'. */
6745 else if (b
->kind
== sk_namespace
6746 && current_binding_level
->kind
!= sk_namespace
)
6747 /* If this new type is being injected into a containing scope,
6748 then it's not a template type. */
6752 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6753 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6755 if (processing_template_decl
)
6757 /* This may change after the call to
6758 push_template_decl_real, but we want the original value. */
6759 tree name
= DECL_NAME (decl
);
6761 decl
= push_template_decl_real (decl
, is_friend
);
6762 if (decl
== error_mark_node
)
6763 return error_mark_node
;
6765 /* If the current binding level is the binding level for the
6766 template parameters (see the comment in
6767 begin_template_parm_list) and the enclosing level is a class
6768 scope, and we're not looking at a friend, push the
6769 declaration of the member class into the class scope. In the
6770 friend case, push_template_decl will already have put the
6771 friend into global scope, if appropriate. */
6772 if (TREE_CODE (type
) != ENUMERAL_TYPE
6773 && !is_friend
&& b
->kind
== sk_template_parms
6774 && b
->level_chain
->kind
== sk_class
)
6776 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6778 if (!COMPLETE_TYPE_P (current_class_type
))
6780 maybe_add_class_template_decl_list (current_class_type
,
6781 type
, /*friend_p=*/0);
6782 /* Put this UTD in the table of UTDs for the class. */
6783 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6784 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6785 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6787 binding_table_insert
6788 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6797 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6798 that the NAME is a class template, the tag is processed but not pushed.
6800 The pushed scope depend on the SCOPE parameter:
6801 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6803 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6804 non-template-parameter scope. This case is needed for forward
6806 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6807 TS_GLOBAL case except that names within template-parameter scopes
6808 are not pushed at all.
6810 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6813 do_pushtag (tree name
, tree type
, tag_scope scope
)
6817 cp_binding_level
*b
= current_binding_level
;
6820 if (/* Cleanup scopes are not scopes from the point of view of
6822 b
->kind
== sk_cleanup
6823 /* Neither are function parameter scopes. */
6824 || b
->kind
== sk_function_parms
6825 /* Neither are the scopes used to hold template parameters
6826 for an explicit specialization. For an ordinary template
6827 declaration, these scopes are not scopes from the point of
6828 view of the language. */
6829 || (b
->kind
== sk_template_parms
6830 && (b
->explicit_spec_p
|| scope
== ts_global
)))
6832 else if (b
->kind
== sk_class
6833 && scope
!= ts_current
)
6836 if (b
->kind
== sk_template_parms
)
6843 gcc_assert (identifier_p (name
));
6845 /* Do C++ gratuitous typedefing. */
6846 if (identifier_type_value_1 (name
) != type
)
6850 tree context
= TYPE_CONTEXT (type
);
6854 cp_binding_level
*cb
= b
;
6855 while (cb
->kind
!= sk_namespace
6856 && cb
->kind
!= sk_class
6857 && (cb
->kind
!= sk_function_parms
6858 || !cb
->this_entity
))
6859 cb
= cb
->level_chain
;
6860 tree cs
= cb
->this_entity
;
6862 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
6863 ? cs
== current_function_decl
6864 : TYPE_P (cs
) ? cs
== current_class_type
6865 : cs
== current_namespace
);
6867 if (scope
== ts_current
6868 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6870 else if (cs
&& TYPE_P (cs
))
6871 /* When declaring a friend class of a local class, we want
6872 to inject the newly named class into the scope
6873 containing the local class, not the namespace
6875 context
= decl_function_context (get_type_decl (cs
));
6878 context
= current_namespace
;
6880 if (b
->kind
== sk_class
6881 || (b
->kind
== sk_template_parms
6882 && b
->level_chain
->kind
== sk_class
))
6885 tdef
= create_implicit_typedef (name
, type
);
6886 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6887 if (scope
== ts_within_enclosing_non_class
)
6889 /* This is a friend. Make this TYPE_DECL node hidden from
6890 ordinary name lookup. Its corresponding TEMPLATE_DECL
6891 will be marked in push_template_decl_real. */
6892 retrofit_lang_decl (tdef
);
6893 DECL_ANTICIPATED (tdef
) = 1;
6894 DECL_FRIEND_P (tdef
) = 1;
6897 decl
= maybe_process_template_type_declaration
6898 (type
, scope
== ts_within_enclosing_non_class
, b
);
6899 if (decl
== error_mark_node
)
6902 if (b
->kind
== sk_class
)
6904 if (!TYPE_BEING_DEFINED (current_class_type
))
6905 /* Don't push anywhere if the class is complete; a lambda in an
6906 NSDMI is not a member of the class. */
6908 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6909 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6910 class. But if it's a member template class, we want
6911 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6913 finish_member_declaration (decl
);
6915 pushdecl_class_level (decl
);
6917 else if (b
->kind
!= sk_template_parms
)
6919 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
6920 if (decl
== error_mark_node
)
6923 if (DECL_CONTEXT (decl
) == std_node
6924 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
6925 && !CLASSTYPE_TEMPLATE_INFO (type
))
6927 error ("declaration of %<std::initializer_list%> does not match "
6928 "%<#include <initializer_list>%>, isn%'t a template");
6929 return error_mark_node
;
6934 set_identifier_type_value_with_scope (name
, tdef
, b
);
6936 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
6938 /* If this is a local class, keep track of it. We need this
6939 information for name-mangling, and so that it is possible to
6940 find all function definitions in a translation unit in a
6941 convenient way. (It's otherwise tricky to find a member
6942 function definition it's only pointed to from within a local
6944 if (TYPE_FUNCTION_SCOPE_P (type
))
6946 if (processing_template_decl
)
6948 /* Push a DECL_EXPR so we call pushtag at the right time in
6949 template instantiation rather than in some nested context. */
6950 add_decl_expr (decl
);
6952 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
6953 else if (!LAMBDA_TYPE_P (type
))
6954 determine_local_discriminator (TYPE_NAME (type
));
6958 if (b
->kind
== sk_class
6959 && !COMPLETE_TYPE_P (current_class_type
))
6961 maybe_add_class_template_decl_list (current_class_type
,
6962 type
, /*friend_p=*/0);
6964 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6965 CLASSTYPE_NESTED_UTDS (current_class_type
)
6966 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6968 binding_table_insert
6969 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6972 decl
= TYPE_NAME (type
);
6973 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
6975 /* Set type visibility now if this is a forward declaration. */
6976 TREE_PUBLIC (decl
) = 1;
6977 determine_visibility (decl
);
6982 /* Wrapper for do_pushtag. */
6985 pushtag (tree name
, tree type
, tag_scope scope
)
6988 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6989 ret
= do_pushtag (name
, type
, scope
);
6990 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6995 /* Subroutines for reverting temporarily to top-level for instantiation
6996 of templates and such. We actually need to clear out the class- and
6997 local-value slots of all identifiers, so that only the global values
6998 are at all visible. Simply setting current_binding_level to the global
6999 scope isn't enough, because more binding levels may be pushed. */
7000 struct saved_scope
*scope_chain
;
7002 /* Return true if ID has not already been marked. */
7005 store_binding_p (tree id
)
7007 if (!id
|| !IDENTIFIER_BINDING (id
))
7010 if (IDENTIFIER_MARKED (id
))
7016 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
7017 have enough space reserved. */
7020 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7022 cxx_saved_binding saved
;
7024 gcc_checking_assert (store_binding_p (id
));
7026 IDENTIFIER_MARKED (id
) = 1;
7028 saved
.identifier
= id
;
7029 saved
.binding
= IDENTIFIER_BINDING (id
);
7030 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
7031 (*old_bindings
)->quick_push (saved
);
7032 IDENTIFIER_BINDING (id
) = NULL
;
7036 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7038 static vec
<tree
> bindings_need_stored
;
7042 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7043 for (t
= names
; t
; t
= TREE_CHAIN (t
))
7045 if (TREE_CODE (t
) == TREE_LIST
)
7046 id
= TREE_PURPOSE (t
);
7050 if (store_binding_p (id
))
7051 bindings_need_stored
.safe_push (id
);
7053 if (!bindings_need_stored
.is_empty ())
7055 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7056 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7058 /* We can apparently have duplicates in NAMES. */
7059 if (store_binding_p (id
))
7060 store_binding (id
, old_bindings
);
7062 bindings_need_stored
.truncate (0);
7064 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7067 /* Like store_bindings, but NAMES is a vector of cp_class_binding
7068 objects, rather than a TREE_LIST. */
7071 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
7072 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7074 static vec
<tree
> bindings_need_stored
;
7076 cp_class_binding
*cb
;
7078 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
7079 if (store_binding_p (cb
->identifier
))
7080 bindings_need_stored
.safe_push (cb
->identifier
);
7081 if (!bindings_need_stored
.is_empty ())
7084 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7085 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7086 store_binding (id
, old_bindings
);
7087 bindings_need_stored
.truncate (0);
7091 /* A chain of saved_scope structures awaiting reuse. */
7093 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
7096 do_push_to_top_level (void)
7098 struct saved_scope
*s
;
7099 cp_binding_level
*b
;
7100 cxx_saved_binding
*sb
;
7104 /* Reuse or create a new structure for this saved scope. */
7105 if (free_saved_scope
!= NULL
)
7107 s
= free_saved_scope
;
7108 free_saved_scope
= s
->prev
;
7110 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
7111 memset (s
, 0, sizeof (*s
));
7112 /* Also reuse the structure's old_bindings vector. */
7113 vec_safe_truncate (old_bindings
, 0);
7114 s
->old_bindings
= old_bindings
;
7117 s
= ggc_cleared_alloc
<saved_scope
> ();
7119 b
= scope_chain
? current_binding_level
: 0;
7121 /* If we're in the middle of some function, save our state. */
7125 push_function_context ();
7130 if (scope_chain
&& previous_class_level
)
7131 store_class_bindings (previous_class_level
->class_shadowed
,
7134 /* Have to include the global scope, because class-scope decls
7135 aren't listed anywhere useful. */
7136 for (; b
; b
= b
->level_chain
)
7140 /* Template IDs are inserted into the global level. If they were
7141 inserted into namespace level, finish_file wouldn't find them
7142 when doing pending instantiations. Therefore, don't stop at
7143 namespace level, but continue until :: . */
7144 if (global_scope_p (b
))
7147 store_bindings (b
->names
, &s
->old_bindings
);
7148 /* We also need to check class_shadowed to save class-level type
7149 bindings, since pushclass doesn't fill in b->names. */
7150 if (b
->kind
== sk_class
)
7151 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
7153 /* Unwind type-value slots back to top level. */
7154 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
7155 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
7158 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
7159 IDENTIFIER_MARKED (sb
->identifier
) = 0;
7161 s
->prev
= scope_chain
;
7163 s
->need_pop_function_context
= need_pop
;
7164 s
->function_decl
= current_function_decl
;
7165 s
->unevaluated_operand
= cp_unevaluated_operand
;
7166 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7167 s
->suppress_location_wrappers
= suppress_location_wrappers
;
7168 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
7171 current_function_decl
= NULL_TREE
;
7172 current_lang_base
= NULL
;
7173 current_lang_name
= lang_name_cplusplus
;
7174 current_namespace
= global_namespace
;
7175 push_class_stack ();
7176 cp_unevaluated_operand
= 0;
7177 c_inhibit_evaluation_warnings
= 0;
7178 suppress_location_wrappers
= 0;
7182 do_pop_from_top_level (void)
7184 struct saved_scope
*s
= scope_chain
;
7185 cxx_saved_binding
*saved
;
7188 /* Clear out class-level bindings cache. */
7189 if (previous_class_level
)
7190 invalidate_class_lookup_cache ();
7193 release_tree_vector (current_lang_base
);
7195 scope_chain
= s
->prev
;
7196 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
7198 tree id
= saved
->identifier
;
7200 IDENTIFIER_BINDING (id
) = saved
->binding
;
7201 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
7204 /* If we were in the middle of compiling a function, restore our
7206 if (s
->need_pop_function_context
)
7207 pop_function_context ();
7208 current_function_decl
= s
->function_decl
;
7209 cp_unevaluated_operand
= s
->unevaluated_operand
;
7210 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
7211 suppress_location_wrappers
= s
->suppress_location_wrappers
;
7213 /* Make this saved_scope structure available for reuse by
7214 push_to_top_level. */
7215 s
->prev
= free_saved_scope
;
7216 free_saved_scope
= s
;
7219 /* Push into the scope of the namespace NS, even if it is deeply
7220 nested within another namespace. */
7223 do_push_nested_namespace (tree ns
)
7225 if (ns
== global_namespace
)
7226 do_push_to_top_level ();
7229 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
7231 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
7232 resume_scope (NAMESPACE_LEVEL (ns
));
7233 current_namespace
= ns
;
7237 /* Pop back from the scope of the namespace NS, which was previously
7238 entered with push_nested_namespace. */
7241 do_pop_nested_namespace (tree ns
)
7243 while (ns
!= global_namespace
)
7245 ns
= CP_DECL_CONTEXT (ns
);
7246 current_namespace
= ns
;
7250 do_pop_from_top_level ();
7253 /* Add TARGET to USINGS, if it does not already exist there.
7254 We used to build the complete graph of usings at this point, from
7255 the POV of the source namespaces. Now we build that as we perform
7256 the unqualified search. */
7259 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
7262 for (unsigned ix
= usings
->length (); ix
--;)
7263 if ((*usings
)[ix
] == target
)
7266 vec_safe_push (usings
, target
);
7269 /* Tell the debug system of a using directive. */
7272 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
7274 /* Emit debugging info. */
7275 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
7276 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
7280 /* Process a using directive. */
7283 finish_using_directive (tree target
, tree attribs
)
7285 if (target
== error_mark_node
)
7288 if (current_binding_level
->kind
!= sk_namespace
)
7289 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
7291 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
7292 ORIGINAL_NAMESPACE (target
), false);
7294 add_using_namespace (current_binding_level
->using_directives
,
7295 ORIGINAL_NAMESPACE (target
));
7297 if (attribs
!= error_mark_node
)
7298 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
7300 tree name
= get_attribute_name (a
);
7301 if (current_binding_level
->kind
== sk_namespace
7302 && is_attribute_p ("strong", name
))
7304 if (warning (0, "%<strong%> using directive no longer supported")
7305 && CP_DECL_CONTEXT (target
) == current_namespace
)
7306 inform (DECL_SOURCE_LOCATION (target
),
7307 "you can use an inline namespace instead");
7310 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
7314 /* Pushes X into the global namespace. */
7317 pushdecl_top_level (tree x
, bool is_friend
)
7319 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7320 do_push_to_top_level ();
7321 x
= pushdecl_namespace_level (x
, is_friend
);
7322 do_pop_from_top_level ();
7323 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7327 /* Pushes X into the global namespace and calls cp_finish_decl to
7328 register the variable, initializing it with INIT. */
7331 pushdecl_top_level_and_finish (tree x
, tree init
)
7333 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7334 do_push_to_top_level ();
7335 x
= pushdecl_namespace_level (x
, false);
7336 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
7337 do_pop_from_top_level ();
7338 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7342 /* Enter the namespaces from current_namerspace to NS. */
7345 push_inline_namespaces (tree ns
)
7348 if (ns
!= current_namespace
)
7350 gcc_assert (ns
!= global_namespace
);
7351 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7352 resume_scope (NAMESPACE_LEVEL (ns
));
7353 current_namespace
= ns
;
7359 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7360 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7361 we create an inline namespace (it is up to the caller to check upon
7362 redefinition). Return the number of namespaces entered. */
7365 push_namespace (tree name
, bool make_inline
)
7367 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7370 /* We should not get here if the global_namespace is not yet constructed
7371 nor if NAME designates the global namespace: The global scope is
7372 constructed elsewhere. */
7373 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
7375 tree ns
= NULL_TREE
;
7377 name_lookup
lookup (name
, 0);
7378 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
7380 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
7382 /* An ambiguous lookup. If exactly one is a namespace, we
7383 want that. If more than one is a namespace, error, but
7384 pick one of them. */
7385 /* DR2061 can cause us to find multiple namespaces of the same
7386 name. We must treat that carefully and avoid thinking we
7387 need to push a new (possibly) duplicate namespace. Hey,
7388 if you want to use the same identifier within an inline
7389 nest, knock yourself out. */
7390 for (tree
*chain
= &lookup
.value
, next
; (next
= *chain
);)
7392 tree decl
= TREE_VALUE (next
);
7393 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
7397 else if (SCOPE_DEPTH (ns
) >= SCOPE_DEPTH (decl
))
7401 chain
= &TREE_CHAIN (next
);
7405 *chain
= TREE_CHAIN (next
);
7408 if (TREE_CHAIN (lookup
.value
))
7410 error ("%<namespace %E%> is ambiguous", name
);
7411 print_candidates (lookup
.value
);
7414 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
7418 if (tree dna
= DECL_NAMESPACE_ALIAS (ns
))
7420 /* A namespace alias is not allowed here, but if the alias
7421 is for a namespace also inside the current scope,
7422 accept it with a diagnostic. That's better than dying
7424 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
7426 error ("namespace alias %qD not allowed here, "
7427 "assuming %qD", ns
, dna
);
7435 bool new_ns
= false;
7437 /* DR2061. NS might be a member of an inline namespace. We
7438 need to push into those namespaces. */
7439 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7442 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
7443 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
7444 if (!SCOPE_DEPTH (ns
))
7445 /* We only allow depth 255. */
7446 sorry ("cannot nest more than %d namespaces",
7447 SCOPE_DEPTH (current_namespace
));
7448 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
7451 if (pushdecl (ns
) == error_mark_node
)
7457 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
7460 add_using_namespace (current_binding_level
->using_directives
,
7463 else if (TREE_PUBLIC (current_namespace
))
7464 TREE_PUBLIC (ns
) = 1;
7468 DECL_NAMESPACE_INLINE_P (ns
) = true;
7469 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
7472 if (!name
|| make_inline
)
7473 emit_debug_info_using_namespace (current_namespace
, ns
, true);
7479 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
7481 error ("inline namespace must be specified at initial definition");
7482 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
7485 begin_scope (sk_namespace
, ns
);
7487 resume_scope (NAMESPACE_LEVEL (ns
));
7488 current_namespace
= ns
;
7492 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7496 /* Pop from the scope of the current namespace. */
7499 pop_namespace (void)
7501 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7503 gcc_assert (current_namespace
!= global_namespace
);
7504 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
7505 /* The binding level is not popped, as it might be re-opened later. */
7508 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7511 /* External entry points for do_{push_to/pop_from}_top_level. */
7514 push_to_top_level (void)
7516 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7517 do_push_to_top_level ();
7518 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7522 pop_from_top_level (void)
7524 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7525 do_pop_from_top_level ();
7526 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7529 /* External entry points for do_{push,pop}_nested_namespace. */
7532 push_nested_namespace (tree ns
)
7534 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7535 do_push_nested_namespace (ns
);
7536 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7540 pop_nested_namespace (tree ns
)
7542 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7543 gcc_assert (current_namespace
== ns
);
7544 do_pop_nested_namespace (ns
);
7545 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7548 /* Pop off extraneous binding levels left over due to syntax errors.
7549 We don't pop past namespaces, as they might be valid. */
7552 pop_everything (void)
7554 if (ENABLE_SCOPE_CHECKING
)
7555 verbatim ("XXX entering %<pop_everything ()%>");
7556 while (!namespace_bindings_p ())
7558 if (current_binding_level
->kind
== sk_class
)
7559 pop_nested_class ();
7563 if (ENABLE_SCOPE_CHECKING
)
7564 verbatim ("XXX leaving %<pop_everything ()%>");
7567 /* Emit debugging information for using declarations and directives.
7568 If input tree is overloaded fn then emit debug info for all
7572 cp_emit_debug_info_for_using (tree t
, tree context
)
7574 /* Don't try to emit any debug information if we have errors. */
7578 /* Do not supply context to imported_module_or_decl, if
7579 it is a global namespace. */
7580 if (context
== global_namespace
)
7581 context
= NULL_TREE
;
7583 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7585 for (lkp_iterator
iter (t
); iter
; ++iter
)
7589 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7590 /* FIXME: Handle TEMPLATE_DECLs. */
7593 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7594 of a builtin function. */
7595 if (TREE_CODE (fn
) == FUNCTION_DECL
7596 && DECL_EXTERNAL (fn
)
7597 && fndecl_built_in_p (fn
))
7600 if (building_stmt_list_p ())
7601 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7603 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7608 /* Return the result of unqualified lookup for the overloaded operator
7609 designated by CODE, if we are in a template and the binding we find is
7613 op_unqualified_lookup (tree fnname
)
7615 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
7617 cp_binding_level
*l
= binding
->scope
;
7618 while (l
&& !l
->this_entity
)
7620 if (l
&& uses_template_parms (l
->this_entity
))
7621 /* Don't preserve decls from an uninstantiated template,
7622 wait until that template is instantiated. */
7625 tree fns
= lookup_name (fnname
);
7626 if (fns
&& fns
== get_global_binding (fnname
))
7627 /* The instantiation can find these. */
7632 /* E is an expression representing an operation with dependent type, so we
7633 don't know yet whether it will use the built-in meaning of the operator or a
7634 function. Remember declarations of that operator in scope. */
7636 const char *const op_bind_attrname
= "operator bindings";
7639 maybe_save_operator_binding (tree e
)
7641 /* This is only useful in a generic lambda. */
7642 if (!processing_template_decl
)
7644 tree cfn
= current_function_decl
;
7648 /* Let's only do this for generic lambdas for now, we could do it for all
7649 function templates if we wanted to. */
7650 if (!current_lambda_expr())
7653 tree fnname
= ovl_op_identifier (false, TREE_CODE (e
));
7657 tree attributes
= DECL_ATTRIBUTES (cfn
);
7658 tree attr
= lookup_attribute (op_bind_attrname
, attributes
);
7659 tree bindings
= NULL_TREE
;
7660 tree fns
= NULL_TREE
;
7663 bindings
= TREE_VALUE (attr
);
7664 if (tree elt
= purpose_member (fnname
, bindings
))
7665 fns
= TREE_VALUE (elt
);
7668 if (!fns
&& (fns
= op_unqualified_lookup (fnname
)))
7670 tree d
= is_overloaded_fn (fns
) ? get_first_fn (fns
) : fns
;
7671 if (DECL_P (d
) && DECL_CLASS_SCOPE_P (d
))
7672 /* We don't need to remember class-scope functions or declarations,
7673 normal unqualified lookup will find them again. */
7676 bindings
= tree_cons (fnname
, fns
, bindings
);
7678 TREE_VALUE (attr
) = bindings
;
7680 DECL_ATTRIBUTES (cfn
)
7681 = tree_cons (get_identifier (op_bind_attrname
),
7687 /* Called from cp_free_lang_data so we don't put this into LTO. */
7690 discard_operator_bindings (tree decl
)
7692 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
7693 DECL_ATTRIBUTES (decl
));
7696 /* Subroutine of start_preparsed_function: push the bindings we saved away in
7697 maybe_save_op_lookup into the function parameter binding level. */
7700 push_operator_bindings ()
7702 tree decl1
= current_function_decl
;
7703 if (tree attr
= lookup_attribute (op_bind_attrname
,
7704 DECL_ATTRIBUTES (decl1
)))
7705 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
7707 tree name
= TREE_PURPOSE (binds
);
7708 tree val
= TREE_VALUE (binds
);
7709 push_local_binding (name
, val
, /*using*/true);
7713 #include "gt-cp-name-lookup.h"