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 /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
59 But we also need to indicate hiddenness on implicit type decls
60 (injected friend classes), and (coming soon) decls injected from
61 block-scope externs. It is too awkward to press the existing
62 overload marking for that. If we have a hidden non-function, we
63 always create a STAT_HACK, and use these two markers as needed. */
64 #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
65 #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
67 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
71 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
73 tree result
= make_node (OVERLOAD
);
75 /* Mark this as a lookup, so we can tell this is a stat hack. */
76 OVL_LOOKUP_P (result
) = true;
77 STAT_DECL (result
) = decl
;
78 STAT_TYPE (result
) = type
;
82 /* Create a local binding level for NAME. */
85 create_local_binding (cp_binding_level
*level
, tree name
)
87 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
89 LOCAL_BINDING_P (binding
) = true;
90 binding
->scope
= level
;
91 binding
->previous
= IDENTIFIER_BINDING (name
);
93 IDENTIFIER_BINDING (name
) = binding
;
98 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
99 make an empty binding if there wasn't one. */
102 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
104 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
105 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
106 create_p
? INSERT
: NO_INSERT
);
111 find_namespace_value (tree ns
, tree name
)
113 tree
*b
= find_namespace_slot (ns
, name
);
115 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
118 /* Add DECL to the list of things declared in B. */
121 add_decl_to_level (cp_binding_level
*b
, tree decl
)
123 gcc_assert (b
->kind
!= sk_class
);
125 /* Make sure we don't create a circular list. xref_tag can end
126 up pushing the same artificial decl more than once. We
127 should have already detected that in update_binding. */
128 gcc_assert (b
->names
!= decl
);
130 /* We build up the list in reverse order, and reverse it later if
132 TREE_CHAIN (decl
) = b
->names
;
135 /* If appropriate, add decl to separate list of statics. We include
136 extern variables because they might turn out to be static later.
137 It's OK for this list to contain a few false positives. */
138 if (b
->kind
== sk_namespace
139 && ((VAR_P (decl
) && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
140 || (TREE_CODE (decl
) == FUNCTION_DECL
141 && (!TREE_PUBLIC (decl
)
142 || decl_anon_ns_mem_p (decl
)
143 || DECL_DECLARED_INLINE_P (decl
)))))
144 vec_safe_push (static_decls
, decl
);
147 /* Find the binding for NAME in the local binding level B. */
150 find_local_binding (cp_binding_level
*b
, tree name
)
152 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
153 for (;; b
= b
->level_chain
)
155 if (binding
->scope
== b
)
158 /* Cleanup contours are transparent to the language. */
159 if (b
->kind
!= sk_cleanup
)
168 typedef std::pair
<tree
, tree
> using_pair
;
169 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
172 tree name
; /* The identifier being looked for. */
173 tree value
; /* A (possibly ambiguous) set of things found. */
174 tree type
; /* A type that has been found. */
175 LOOK_want want
; /* What kind of entity we want. */
177 bool deduping
; /* Full deduping is needed because using declarations
179 vec
<tree
, va_heap
, vl_embed
> *scopes
;
180 name_lookup
*previous
; /* Previously active lookup. */
183 /* Marked scope stack for outermost name lookup. */
184 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
185 /* Currently active lookup. */
186 static name_lookup
*active
;
189 name_lookup (tree n
, LOOK_want w
= LOOK_want::NORMAL
)
190 : name (n
), value (NULL_TREE
), type (NULL_TREE
),
192 deduping (false), scopes (NULL
), previous (NULL
)
201 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
202 name_lookup (const name_lookup
&);
203 name_lookup
&operator= (const name_lookup
&);
206 static bool seen_p (tree scope
)
208 return LOOKUP_SEEN_P (scope
);
210 static bool found_p (tree scope
)
212 return LOOKUP_FOUND_P (scope
);
215 void mark_seen (tree scope
); /* Mark and add to scope vector. */
216 static void mark_found (tree scope
)
218 gcc_checking_assert (seen_p (scope
));
219 LOOKUP_FOUND_P (scope
) = true;
221 bool see_and_mark (tree scope
)
223 bool ret
= seen_p (scope
);
228 bool find_and_mark (tree scope
);
231 void preserve_state ();
232 void restore_state ();
235 static tree
ambiguous (tree thing
, tree current
);
236 void add_overload (tree fns
);
237 void add_value (tree new_val
);
238 void add_type (tree new_type
);
239 bool process_binding (tree val_bind
, tree type_bind
);
241 /* Look in only namespace. */
242 bool search_namespace_only (tree scope
);
243 /* Look in namespace and its (recursive) inlines. Ignore using
244 directives. Return true if something found (inc dups). */
245 bool search_namespace (tree scope
);
246 /* Look in the using directives of namespace + inlines using
247 qualified lookup rules. */
248 bool search_usings (tree scope
);
251 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
252 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
253 vec
<tree
, va_gc
> *usings
);
254 using_queue
*queue_usings (using_queue
*queue
, int depth
,
255 vec
<tree
, va_gc
> *usings
)
258 queue
= do_queue_usings (queue
, depth
, usings
);
265 void adl_expr (tree
);
266 void adl_type (tree
);
267 void adl_template_arg (tree
);
268 void adl_class (tree
);
269 void adl_bases (tree
);
270 void adl_class_only (tree
);
271 void adl_namespace (tree
);
272 void adl_namespace_only (tree
);
275 /* Search namespace + inlines + maybe usings as qualified lookup. */
276 bool search_qualified (tree scope
, bool usings
= true);
278 /* Search namespace + inlines + usings as unqualified lookup. */
279 bool search_unqualified (tree scope
, cp_binding_level
*);
281 /* ADL lookup of ARGS. */
282 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
285 /* Scope stack shared by all outermost lookups. This avoids us
286 allocating and freeing on every single lookup. */
287 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
289 /* Currently active lookup. */
290 name_lookup
*name_lookup::active
;
292 /* Name lookup is recursive, becase ADL can cause template
293 instatiation. This is of course a rare event, so we optimize for
294 it not happening. When we discover an active name-lookup, which
295 must be an ADL lookup, we need to unmark the marked scopes and also
296 unmark the lookup we might have been accumulating. */
299 name_lookup::preserve_state ()
304 unsigned length
= vec_safe_length (previous
->scopes
);
305 vec_safe_reserve (previous
->scopes
, length
* 2);
306 for (unsigned ix
= length
; ix
--;)
308 tree decl
= (*previous
->scopes
)[ix
];
310 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
311 LOOKUP_SEEN_P (decl
) = false;
313 /* Preserve the FOUND_P state on the interrupted lookup's
315 if (LOOKUP_FOUND_P (decl
))
317 LOOKUP_FOUND_P (decl
) = false;
318 previous
->scopes
->quick_push (decl
);
322 /* Unmark the outer partial lookup. */
323 if (previous
->deduping
)
324 lookup_mark (previous
->value
, false);
327 scopes
= shared_scopes
;
331 /* Restore the marking state of a lookup we interrupted. */
334 name_lookup::restore_state ()
337 lookup_mark (value
, false);
339 /* Unmark and empty this lookup's scope stack. */
340 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
342 tree decl
= scopes
->pop ();
343 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
344 LOOKUP_SEEN_P (decl
) = false;
345 LOOKUP_FOUND_P (decl
) = false;
353 unsigned length
= vec_safe_length (previous
->scopes
);
354 for (unsigned ix
= 0; ix
!= length
; ix
++)
356 tree decl
= (*previous
->scopes
)[ix
];
357 if (LOOKUP_SEEN_P (decl
))
359 /* The remainder of the scope stack must be recording
360 FOUND_P decls, which we want to pop off. */
363 tree decl
= previous
->scopes
->pop ();
364 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
365 && !LOOKUP_FOUND_P (decl
));
366 LOOKUP_FOUND_P (decl
) = true;
368 while (++ix
!= length
);
372 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
373 LOOKUP_SEEN_P (decl
) = true;
376 /* Remark the outer partial lookup. */
377 if (previous
->deduping
)
378 lookup_mark (previous
->value
, true);
381 shared_scopes
= scopes
;
385 name_lookup::mark_seen (tree scope
)
387 gcc_checking_assert (!seen_p (scope
));
388 LOOKUP_SEEN_P (scope
) = true;
389 vec_safe_push (scopes
, scope
);
393 name_lookup::find_and_mark (tree scope
)
395 bool result
= LOOKUP_FOUND_P (scope
);
398 LOOKUP_FOUND_P (scope
) = true;
399 if (!LOOKUP_SEEN_P (scope
))
400 vec_safe_push (scopes
, scope
);
406 /* THING and CURRENT are ambiguous, concatenate them. */
409 name_lookup::ambiguous (tree thing
, tree current
)
411 if (TREE_CODE (current
) != TREE_LIST
)
413 current
= build_tree_list (NULL_TREE
, current
);
414 TREE_TYPE (current
) = error_mark_node
;
416 current
= tree_cons (NULL_TREE
, thing
, current
);
417 TREE_TYPE (current
) = error_mark_node
;
422 /* FNS is a new overload set to add to the exising set. */
425 name_lookup::add_overload (tree fns
)
427 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
430 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
431 probe
= ovl_skip_hidden (probe
);
432 if (probe
&& TREE_CODE (probe
) == OVERLOAD
433 && OVL_DEDUP_P (probe
))
435 /* We're about to add something found by a using
436 declaration, so need to engage deduping mode. */
437 lookup_mark (value
, true);
442 value
= lookup_maybe_add (fns
, value
, deduping
);
445 /* Add a NEW_VAL, a found value binding into the current value binding. */
448 name_lookup::add_value (tree new_val
)
450 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
451 add_overload (new_val
);
454 else if (value
== new_val
)
456 else if ((TREE_CODE (value
) == TYPE_DECL
457 && TREE_CODE (new_val
) == TYPE_DECL
458 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
459 /* Typedefs to the same type. */;
460 else if (TREE_CODE (value
) == NAMESPACE_DECL
461 && TREE_CODE (new_val
) == NAMESPACE_DECL
462 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
463 /* Namespace (possibly aliased) to the same namespace. Locate
465 value
= ORIGINAL_NAMESPACE (value
);
470 /* Disengage deduping mode. */
471 lookup_mark (value
, false);
474 value
= ambiguous (new_val
, value
);
478 /* Add a NEW_TYPE, a found type binding into the current type binding. */
481 name_lookup::add_type (tree new_type
)
485 else if (TREE_CODE (type
) == TREE_LIST
486 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
487 type
= ambiguous (new_type
, type
);
490 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
491 true if we actually found something noteworthy. Hiddenness has
492 already been handled in the caller. */
495 name_lookup::process_binding (tree new_val
, tree new_type
)
497 /* Did we really see a type? */
499 && (want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
)
500 new_type
= NULL_TREE
;
502 /* Do we really see a value? */
504 switch (TREE_CODE (new_val
))
507 /* If we expect types or namespaces, and not templates,
508 or this is not a template class. */
509 if (bool (want
& LOOK_want::TYPE_NAMESPACE
)
510 && !DECL_TYPE_TEMPLATE_P (new_val
))
514 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
515 || (new_type
&& bool (want
& LOOK_want::TYPE
)))
519 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::TYPE
)
523 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
530 new_type
= NULL_TREE
;
533 /* Merge into the lookup */
539 return new_val
!= NULL_TREE
;
542 /* Look in exactly namespace SCOPE. */
545 name_lookup::search_namespace_only (tree scope
)
549 if (tree
*binding
= find_namespace_slot (scope
, name
))
551 tree value
= *binding
, type
= NULL_TREE
;
553 if (STAT_HACK_P (value
))
555 type
= STAT_TYPE (value
);
556 value
= STAT_DECL (value
);
558 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
560 if (STAT_TYPE_HIDDEN_P (*binding
))
562 if (STAT_DECL_HIDDEN_P (*binding
))
565 value
= ovl_skip_hidden (value
);
568 else if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
569 value
= ovl_skip_hidden (value
);
571 found
|= process_binding (value
, type
);
577 /* Conditionally look in namespace SCOPE and inline children. */
580 name_lookup::search_namespace (tree scope
)
582 if (see_and_mark (scope
))
583 /* We've visited this scope before. Return what we found then. */
584 return found_p (scope
);
586 /* Look in exactly namespace. */
587 bool found
= search_namespace_only (scope
);
589 /* Don't look into inline children, if we're looking for an
590 anonymous name -- it must be in the current scope, if anywhere. */
592 /* Recursively look in its inline children. */
593 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
594 for (unsigned ix
= inlinees
->length (); ix
--;)
595 found
|= search_namespace ((*inlinees
)[ix
]);
603 /* Recursively follow using directives of SCOPE & its inline children.
604 Such following is essentially a flood-fill algorithm. */
607 name_lookup::search_usings (tree scope
)
609 /* We do not check seen_p here, as that was already set during the
610 namespace_only walk. */
615 if (vec
<tree
, va_gc
> *usings
= NAMESPACE_LEVEL (scope
)->using_directives
)
616 for (unsigned ix
= usings
->length (); ix
--;)
617 found
|= search_qualified ((*usings
)[ix
], true);
619 /* Look in its inline children. */
620 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
621 for (unsigned ix
= inlinees
->length (); ix
--;)
622 found
|= search_usings ((*inlinees
)[ix
]);
630 /* Qualified namespace lookup in SCOPE.
631 1) Look in SCOPE (+inlines). If found, we're done.
632 2) Otherwise, if USINGS is true,
633 recurse for every using directive of SCOPE (+inlines).
635 Trickiness is (a) loops and (b) multiple paths to same namespace.
636 In both cases we want to not repeat any lookups, and know whether
637 to stop the caller's step #2. Do this via the FOUND_P marker. */
640 name_lookup::search_qualified (tree scope
, bool usings
)
645 found
= found_p (scope
);
648 found
= search_namespace (scope
);
649 if (!found
&& usings
)
650 found
= search_usings (scope
);
656 /* Add SCOPE to the unqualified search queue, recursively add its
657 inlines and those via using directives. */
659 name_lookup::using_queue
*
660 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
662 if (see_and_mark (scope
))
667 while (SCOPE_DEPTH (common
) > depth
)
668 common
= CP_DECL_CONTEXT (common
);
669 vec_safe_push (queue
, using_pair (common
, scope
));
671 /* Queue its inline children. */
672 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
673 for (unsigned ix
= inlinees
->length (); ix
--;)
674 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
676 /* Queue its using targets. */
677 queue
= queue_usings (queue
, depth
, NAMESPACE_LEVEL (scope
)->using_directives
);
682 /* Add the namespaces in USINGS to the unqualified search queue. */
684 name_lookup::using_queue
*
685 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
686 vec
<tree
, va_gc
> *usings
)
688 for (unsigned ix
= usings
->length (); ix
--;)
689 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
694 /* Unqualified namespace lookup in SCOPE.
695 1) add scope+inlins to worklist.
696 2) recursively add target of every using directive
697 3) for each worklist item where SCOPE is common ancestor, search it
698 4) if nothing find, scope=parent, goto 1. */
701 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
703 /* Make static to avoid continual reallocation. We're not
705 static using_queue
*queue
= NULL
;
707 int length
= vec_safe_length (queue
);
709 /* Queue local using-directives. */
710 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
711 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
713 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
715 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
716 int depth
= SCOPE_DEPTH (scope
);
718 /* Queue namespaces reachable from SCOPE. */
719 queue
= queue_namespace (queue
, depth
, scope
);
721 /* Search every queued namespace where SCOPE is the common
722 ancestor. Adjust the others. */
723 unsigned ix
= length
;
726 using_pair
&pair
= (*queue
)[ix
];
727 while (pair
.first
== scope
)
729 found
|= search_namespace_only (pair
.second
);
730 pair
= queue
->pop ();
731 if (ix
== queue
->length ())
734 /* The depth is the same as SCOPE, find the parent scope. */
735 if (SCOPE_DEPTH (pair
.first
) == depth
)
736 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
739 while (ix
< queue
->length ());
741 if (scope
== global_namespace
)
744 /* If looking for hidden friends, we only look in the innermost
745 namespace scope. [namespace.memdef]/3 If a friend
746 declaration in a non-local class first declares a class,
747 function, class template or function template the friend is a
748 member of the innermost enclosing namespace. See also
749 [basic.lookup.unqual]/7 */
750 if (bool (want
& LOOK_want::HIDDEN_FRIEND
))
754 /* Restore to incoming length. */
755 vec_safe_truncate (queue
, length
);
760 /* FNS is a value binding. If it is a (set of overloaded) functions,
761 add them into the current value. */
764 name_lookup::add_fns (tree fns
)
768 else if (TREE_CODE (fns
) == OVERLOAD
)
770 if (TREE_TYPE (fns
) != unknown_type_node
)
771 fns
= OVL_FUNCTION (fns
);
773 else if (!DECL_DECLARES_FUNCTION_P (fns
))
779 /* Add functions of a namespace to the lookup structure. */
782 name_lookup::adl_namespace_only (tree scope
)
786 /* Look down into inline namespaces. */
787 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
788 for (unsigned ix
= inlinees
->length (); ix
--;)
789 adl_namespace_only ((*inlinees
)[ix
]);
791 if (tree fns
= find_namespace_value (scope
, name
))
792 add_fns (ovl_skip_hidden (fns
));
795 /* Find the containing non-inlined namespace, add it and all its
799 name_lookup::adl_namespace (tree scope
)
804 /* Find the containing non-inline namespace. */
805 while (DECL_NAMESPACE_INLINE_P (scope
))
806 scope
= CP_DECL_CONTEXT (scope
);
808 adl_namespace_only (scope
);
811 /* Adds the class and its friends to the lookup structure. */
814 name_lookup::adl_class_only (tree type
)
816 /* Backend-built structures, such as __builtin_va_list, aren't
817 affected by all this. */
818 if (!CLASS_TYPE_P (type
))
821 type
= TYPE_MAIN_VARIANT (type
);
823 if (see_and_mark (type
))
826 tree context
= decl_namespace_context (type
);
827 adl_namespace (context
);
829 complete_type (type
);
832 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
833 list
= TREE_CHAIN (list
))
834 if (name
== FRIEND_NAME (list
))
835 for (tree friends
= FRIEND_DECLS (list
); friends
;
836 friends
= TREE_CHAIN (friends
))
838 tree fn
= TREE_VALUE (friends
);
840 /* Only interested in global functions with potentially hidden
841 (i.e. unqualified) declarations. */
842 if (CP_DECL_CONTEXT (fn
) != context
)
845 /* Only interested in anticipated friends. (Non-anticipated
846 ones will have been inserted during the namespace
848 if (!DECL_ANTICIPATED (fn
))
851 /* Template specializations are never found by name lookup.
852 (Templates themselves can be found, but not template
854 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
861 /* Adds the class and its bases to the lookup structure.
862 Returns true on error. */
865 name_lookup::adl_bases (tree type
)
867 adl_class_only (type
);
869 /* Process baseclasses. */
870 if (tree binfo
= TYPE_BINFO (type
))
875 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
876 adl_bases (BINFO_TYPE (base_binfo
));
880 /* Adds everything associated with a class argument type to the lookup
881 structure. Returns true on error.
883 If T is a class type (including unions), its associated classes are: the
884 class itself; the class of which it is a member, if any; and its direct
885 and indirect base classes. Its associated namespaces are the namespaces
886 of which its associated classes are members. Furthermore, if T is a
887 class template specialization, its associated namespaces and classes
888 also include: the namespaces and classes associated with the types of
889 the template arguments provided for template type parameters (excluding
890 template template parameters); the namespaces of which any template
891 template arguments are members; and the classes of which any member
892 templates used as template template arguments are members. [ Note:
893 non-type template arguments do not contribute to the set of associated
894 namespaces. --end note] */
897 name_lookup::adl_class (tree type
)
899 /* Backend build structures, such as __builtin_va_list, aren't
900 affected by all this. */
901 if (!CLASS_TYPE_P (type
))
904 type
= TYPE_MAIN_VARIANT (type
);
905 /* We don't set found here because we have to have set seen first,
906 which is done in the adl_bases walk. */
913 if (TYPE_CLASS_SCOPE_P (type
))
914 adl_class_only (TYPE_CONTEXT (type
));
916 /* Process template arguments. */
917 if (CLASSTYPE_TEMPLATE_INFO (type
)
918 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
920 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
921 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
922 adl_template_arg (TREE_VEC_ELT (list
, i
));
927 name_lookup::adl_expr (tree expr
)
932 gcc_assert (!TYPE_P (expr
));
934 if (TREE_TYPE (expr
) != unknown_type_node
)
936 adl_type (unlowered_expr_type (expr
));
940 if (TREE_CODE (expr
) == ADDR_EXPR
)
941 expr
= TREE_OPERAND (expr
, 0);
942 if (TREE_CODE (expr
) == COMPONENT_REF
943 || TREE_CODE (expr
) == OFFSET_REF
)
944 expr
= TREE_OPERAND (expr
, 1);
945 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
948 for (lkp_iterator
iter (expr
); iter
; ++iter
)
949 adl_type (TREE_TYPE (*iter
));
950 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
952 /* The working paper doesn't currently say how to handle
953 template-id arguments. The sensible thing would seem to be
954 to handle the list of template candidates like a normal
955 overload set, and handle the template arguments like we do
956 for class template specializations. */
958 /* First the templates. */
959 adl_expr (TREE_OPERAND (expr
, 0));
961 /* Now the arguments. */
962 if (tree args
= TREE_OPERAND (expr
, 1))
963 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
964 adl_template_arg (TREE_VEC_ELT (args
, ix
));
969 name_lookup::adl_type (tree type
)
974 if (TYPE_PTRDATAMEM_P (type
))
976 /* Pointer to member: associate class type and value type. */
977 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
978 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
982 switch (TREE_CODE (type
))
985 if (TYPE_PTRMEMFUNC_P (type
))
987 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
996 /* The basetype is referenced in the first arg type, so just
999 /* Associate the parameter types. */
1000 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
1001 adl_type (TREE_VALUE (args
));
1005 case REFERENCE_TYPE
:
1007 adl_type (TREE_TYPE (type
));
1011 if (TYPE_CLASS_SCOPE_P (type
))
1012 adl_class_only (TYPE_CONTEXT (type
));
1013 adl_namespace (decl_namespace_context (type
));
1017 gcc_assert (type
== unknown_type_node
1018 || type
== init_list_type_node
);
1021 case TYPE_PACK_EXPANSION
:
1022 adl_type (PACK_EXPANSION_PATTERN (type
));
1030 /* Adds everything associated with a template argument to the lookup
1034 name_lookup::adl_template_arg (tree arg
)
1036 /* [basic.lookup.koenig]
1038 If T is a template-id, its associated namespaces and classes are
1039 ... the namespaces and classes associated with the types of the
1040 template arguments provided for template type parameters
1041 (excluding template template parameters); the namespaces in which
1042 any template template arguments are defined; and the classes in
1043 which any member templates used as template template arguments
1044 are defined. [Note: non-type template arguments do not
1045 contribute to the set of associated namespaces. ] */
1047 /* Consider first template template arguments. */
1048 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1049 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1051 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1053 tree ctx
= CP_DECL_CONTEXT (arg
);
1055 /* It's not a member template. */
1056 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1057 adl_namespace (ctx
);
1058 /* Otherwise, it must be member template. */
1060 adl_class_only (ctx
);
1062 /* It's an argument pack; handle it recursively. */
1063 else if (ARGUMENT_PACK_P (arg
))
1065 tree args
= ARGUMENT_PACK_ARGS (arg
);
1066 int i
, len
= TREE_VEC_LENGTH (args
);
1067 for (i
= 0; i
< len
; ++i
)
1068 adl_template_arg (TREE_VEC_ELT (args
, i
));
1070 /* It's not a template template argument, but it is a type template
1072 else if (TYPE_P (arg
))
1076 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1077 the call arguments. */
1080 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1085 lookup_mark (fns
, true);
1092 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1093 /* OMP reduction operators put an ADL-significant type as the
1105 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1106 static void consider_binding_level (tree name
,
1107 best_match
<tree
, const char *> &bm
,
1108 cp_binding_level
*lvl
,
1109 bool look_within_fields
,
1110 enum lookup_name_fuzzy_kind kind
);
1111 static void diagnose_name_conflict (tree
, tree
);
1113 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1114 don't add duplicates to it. ARGS is the vector of call
1115 arguments (which will not be empty). */
1118 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1120 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1121 name_lookup
lookup (name
);
1122 fns
= lookup
.search_adl (fns
, args
);
1123 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1127 /* FNS is an overload set of conversion functions. Return the
1128 overloads converting to TYPE. */
1131 extract_conversion_operator (tree fns
, tree type
)
1133 tree convs
= NULL_TREE
;
1134 tree tpls
= NULL_TREE
;
1136 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1138 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1139 convs
= lookup_add (*iter
, convs
);
1141 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1142 tpls
= lookup_add (*iter
, tpls
);
1151 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1154 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1156 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1158 unsigned mid
= (lo
+ hi
) / 2;
1159 tree binding
= (*member_vec
)[mid
];
1160 tree binding_name
= OVL_NAME (binding
);
1162 if (binding_name
> name
)
1164 else if (binding_name
< name
)
1173 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1176 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1178 for (int ix
= member_vec
->length (); ix
--;)
1179 if (tree binding
= (*member_vec
)[ix
])
1180 if (OVL_NAME (binding
) == name
)
1186 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1189 fields_linear_search (tree klass
, tree name
, bool want_type
)
1191 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1195 if (TREE_CODE (decl
) == FIELD_DECL
1196 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1198 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
, want_type
))
1202 if (DECL_NAME (decl
) != name
)
1205 if (TREE_CODE (decl
) == USING_DECL
)
1207 decl
= strip_using_decl (decl
);
1208 if (is_overloaded_fn (decl
))
1212 if (DECL_DECLARES_FUNCTION_P (decl
))
1213 /* Functions are found separately. */
1216 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1223 /* Look for NAME member inside of anonymous aggregate ANON. Although
1224 such things should only contain FIELD_DECLs, we check that too
1225 late, and would give very confusing errors if we weren't
1229 search_anon_aggr (tree anon
, tree name
, bool want_type
)
1231 gcc_assert (COMPLETE_TYPE_P (anon
));
1232 tree ret
= get_class_binding_direct (anon
, name
, want_type
);
1236 /* Look for NAME as an immediate member of KLASS (including
1237 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1238 regular search. >0 to get a type binding (if there is one) and <0
1239 if you want (just) the member function binding.
1241 Use this if you do not want lazy member creation. */
1244 get_class_binding_direct (tree klass
, tree name
, bool want_type
)
1246 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1248 /* Conversion operators can only be found by the marker conversion
1250 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1251 tree lookup
= conv_op
? conv_op_identifier
: name
;
1252 tree val
= NULL_TREE
;
1253 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1255 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1257 val
= member_vec_binary_search (member_vec
, lookup
);
1260 else if (STAT_HACK_P (val
))
1261 val
= want_type
? STAT_TYPE (val
) : STAT_DECL (val
);
1262 else if (want_type
&& !DECL_DECLARES_TYPE_P (val
))
1267 if (member_vec
&& !want_type
)
1268 val
= member_vec_linear_search (member_vec
, lookup
);
1270 if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_DEDUP_P (val
)))
1271 /* Dependent using declarations are a 'field', make sure we
1272 return that even if we saw an overload already. */
1273 if (tree field_val
= fields_linear_search (klass
, lookup
, want_type
))
1277 else if (TREE_CODE (field_val
) == USING_DECL
)
1278 val
= ovl_make (field_val
, val
);
1282 /* Extract the conversion operators asked for, unless the general
1283 conversion operator was requested. */
1286 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1287 val
= OVL_CHAIN (val
);
1288 if (tree type
= TREE_TYPE (name
))
1289 val
= extract_conversion_operator (val
, type
);
1295 /* Look for NAME's binding in exactly KLASS. See
1296 get_class_binding_direct for argument description. Does lazy
1297 special function creation as necessary. */
1300 get_class_binding (tree klass
, tree name
, bool want_type
/*=false*/)
1302 klass
= complete_type (klass
);
1304 if (COMPLETE_TYPE_P (klass
))
1306 /* Lazily declare functions, if we're going to search these. */
1307 if (IDENTIFIER_CTOR_P (name
))
1309 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1310 lazily_declare_fn (sfk_constructor
, klass
);
1311 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1312 lazily_declare_fn (sfk_copy_constructor
, klass
);
1313 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1314 lazily_declare_fn (sfk_move_constructor
, klass
);
1316 else if (IDENTIFIER_DTOR_P (name
))
1318 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1319 lazily_declare_fn (sfk_destructor
, klass
);
1321 else if (name
== assign_op_identifier
)
1323 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1324 lazily_declare_fn (sfk_copy_assignment
, klass
);
1325 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1326 lazily_declare_fn (sfk_move_assignment
, klass
);
1330 return get_class_binding_direct (klass
, name
, want_type
);
1333 /* Find the slot containing overloads called 'NAME'. If there is no
1334 such slot and the class is complete, create an empty one, at the
1335 correct point in the sorted member vector. Otherwise return NULL.
1336 Deals with conv_op marker handling. */
1339 find_member_slot (tree klass
, tree name
)
1341 bool complete_p
= COMPLETE_TYPE_P (klass
);
1343 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1346 vec_alloc (member_vec
, 8);
1347 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1350 /* If the class is complete but had no member_vec, we need
1351 to add the TYPE_FIELDS into it. We're also most likely
1352 to be adding ctors & dtors, so ask for 6 spare slots (the
1353 abstract cdtors and their clones). */
1354 set_class_bindings (klass
, 6);
1355 member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1359 if (IDENTIFIER_CONV_OP_P (name
))
1360 name
= conv_op_identifier
;
1362 unsigned ix
, length
= member_vec
->length ();
1363 for (ix
= 0; ix
< length
; ix
++)
1365 tree
*slot
= &(*member_vec
)[ix
];
1366 tree fn_name
= OVL_NAME (*slot
);
1368 if (fn_name
== name
)
1370 /* If we found an existing slot, it must be a function set.
1371 Even with insertion after completion, because those only
1372 happen with artificial fns that have unspellable names.
1373 This means we do not have to deal with the stat hack
1375 gcc_checking_assert (OVL_P (*slot
));
1376 if (name
== conv_op_identifier
)
1378 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1379 /* Skip the conv-op marker. */
1380 slot
= &OVL_CHAIN (*slot
);
1385 if (complete_p
&& fn_name
> name
)
1389 /* No slot found, add one if the class is complete. */
1392 /* Do exact allocation, as we don't expect to add many. */
1393 gcc_assert (name
!= conv_op_identifier
);
1394 vec_safe_reserve_exact (member_vec
, 1);
1395 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1396 member_vec
->quick_insert (ix
, NULL_TREE
);
1397 return &(*member_vec
)[ix
];
1403 /* KLASS is an incomplete class to which we're adding a method NAME.
1404 Add a slot and deal with conv_op marker handling. */
1407 add_member_slot (tree klass
, tree name
)
1409 gcc_assert (!COMPLETE_TYPE_P (klass
));
1411 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1412 vec_safe_push (member_vec
, NULL_TREE
);
1413 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1415 tree
*slot
= &member_vec
->last ();
1416 if (IDENTIFIER_CONV_OP_P (name
))
1418 /* Install the marker prefix. */
1419 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
1420 slot
= &OVL_CHAIN (*slot
);
1426 /* Comparison function to compare two MEMBER_VEC entries by name.
1427 Because we can have duplicates during insertion of TYPE_FIELDS, we
1428 do extra checking so deduping doesn't have to deal with so many
1432 member_name_cmp (const void *a_p
, const void *b_p
)
1434 tree a
= *(const tree
*)a_p
;
1435 tree b
= *(const tree
*)b_p
;
1436 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
1437 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
1439 gcc_checking_assert (name_a
&& name_b
);
1440 if (name_a
!= name_b
)
1441 return name_a
< name_b
? -1 : +1;
1443 if (name_a
== conv_op_identifier
)
1445 /* Strip the conv-op markers. */
1446 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
1447 && OVL_FUNCTION (b
) == conv_op_marker
);
1452 if (TREE_CODE (a
) == OVERLOAD
)
1453 a
= OVL_FUNCTION (a
);
1454 if (TREE_CODE (b
) == OVERLOAD
)
1455 b
= OVL_FUNCTION (b
);
1457 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
1458 if (TREE_CODE (a
) != TREE_CODE (b
))
1460 /* If one of them is a TYPE_DECL, it loses. */
1461 if (TREE_CODE (a
) == TYPE_DECL
)
1463 else if (TREE_CODE (b
) == TYPE_DECL
)
1466 /* If one of them is a USING_DECL, it loses. */
1467 if (TREE_CODE (a
) == USING_DECL
)
1469 else if (TREE_CODE (b
) == USING_DECL
)
1472 /* There are no other cases with different kinds of decls, as
1473 duplicate detection should have kicked in earlier. However,
1474 some erroneous cases get though. */
1475 gcc_assert (errorcount
);
1478 /* Using source location would be the best thing here, but we can
1479 get identically-located decls in the following circumstances:
1481 1) duplicate artificial type-decls for the same type.
1483 2) pack expansions of using-decls.
1485 We should not be doing #1, but in either case it doesn't matter
1486 how we order these. Use UID as a proxy for source ordering, so
1487 that identically-located decls still have a well-defined stable
1489 if (DECL_UID (a
) != DECL_UID (b
))
1490 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
1491 gcc_assert (a
== b
);
1496 gt_pointer_operator new_value
;
1500 /* This routine compares two fields like member_name_cmp but using the
1501 pointer operator in resort_field_decl_data. We don't have to deal
1502 with duplicates here. */
1505 resort_member_name_cmp (const void *a_p
, const void *b_p
)
1507 tree a
= *(const tree
*)a_p
;
1508 tree b
= *(const tree
*)b_p
;
1509 tree name_a
= OVL_NAME (a
);
1510 tree name_b
= OVL_NAME (b
);
1512 resort_data
.new_value (&name_a
, resort_data
.cookie
);
1513 resort_data
.new_value (&name_b
, resort_data
.cookie
);
1515 gcc_checking_assert (name_a
!= name_b
);
1517 return name_a
< name_b
? -1 : +1;
1520 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
1523 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
1524 gt_pointer_operator new_value
, void* cookie
)
1526 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
1528 resort_data
.new_value
= new_value
;
1529 resort_data
.cookie
= cookie
;
1530 member_vec
->qsort (resort_member_name_cmp
);
1534 /* Recursively count the number of fields in KLASS, including anonymous
1538 count_class_fields (tree klass
)
1540 unsigned n_fields
= 0;
1542 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1543 if (DECL_DECLARES_FUNCTION_P (fields
))
1544 /* Functions are dealt with separately. */;
1545 else if (TREE_CODE (fields
) == FIELD_DECL
1546 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1547 n_fields
+= count_class_fields (TREE_TYPE (fields
));
1548 else if (DECL_NAME (fields
))
1554 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
1555 Recurse for anonymous members. MEMBER_VEC must have space. */
1558 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
1560 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1561 if (DECL_DECLARES_FUNCTION_P (fields
))
1562 /* Functions are handled separately. */;
1563 else if (TREE_CODE (fields
) == FIELD_DECL
1564 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
1565 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
1566 else if (DECL_NAME (fields
))
1568 tree field
= fields
;
1569 /* Mark a conv-op USING_DECL with the conv-op-marker. */
1570 if (TREE_CODE (field
) == USING_DECL
1571 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
1572 field
= ovl_make (conv_op_marker
, field
);
1573 member_vec
->quick_push (field
);
1577 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
1578 MEMBER_VEC must have space. */
1581 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
1583 for (tree values
= TYPE_VALUES (enumtype
);
1584 values
; values
= TREE_CHAIN (values
))
1585 member_vec
->quick_push (TREE_VALUE (values
));
1588 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
1589 DeDup adjacent DECLS of the same name. We already dealt with
1590 conflict resolution when adding the fields or methods themselves.
1591 There are three cases (which could all be combined):
1592 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
1593 2) a USING_DECL and an overload. If the USING_DECL is dependent,
1594 it wins. Otherwise the OVERLOAD does.
1595 3) two USING_DECLS. ...
1597 member_name_cmp will have ordered duplicates as
1598 <fns><using><type> */
1601 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
1603 unsigned len
= member_vec
->length ();
1609 tree name
= OVL_NAME ((*member_vec
)[0]);
1610 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
1612 tree current
= NULL_TREE
;
1613 tree to_type
= NULL_TREE
;
1614 tree to_using
= NULL_TREE
;
1615 tree marker
= NULL_TREE
;
1617 for (jx
= ix
; jx
< len
; jx
++)
1619 tree next
= (*member_vec
)[jx
];
1622 tree next_name
= OVL_NAME (next
);
1623 if (next_name
!= name
)
1630 if (IDENTIFIER_CONV_OP_P (name
))
1633 next
= OVL_CHAIN (next
);
1636 if (TREE_CODE (next
) == USING_DECL
)
1638 if (IDENTIFIER_CTOR_P (name
))
1639 /* Dependent inherited ctor. */
1642 next
= strip_using_decl (next
);
1643 if (TREE_CODE (next
) == USING_DECL
)
1649 if (is_overloaded_fn (next
))
1653 if (DECL_DECLARES_TYPE_P (next
))
1668 current
= ovl_make (to_using
, current
);
1676 current
= stat_hack (current
, to_type
);
1683 OVL_CHAIN (marker
) = current
;
1686 (*member_vec
)[store
++] = current
;
1690 while (store
++ < len
)
1694 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
1695 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
1696 know there must be at least 1 field -- the self-reference
1697 TYPE_DECL, except for anon aggregates, which will have at least
1698 one field anyway. */
1701 set_class_bindings (tree klass
, unsigned extra
)
1703 unsigned n_fields
= count_class_fields (klass
);
1704 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1706 if (member_vec
|| n_fields
>= 8)
1708 /* Append the new fields. */
1709 vec_safe_reserve_exact (member_vec
, extra
+ n_fields
);
1710 member_vec_append_class_fields (member_vec
, klass
);
1715 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1716 member_vec
->qsort (member_name_cmp
);
1717 member_vec_dedup (member_vec
);
1721 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
1724 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
1727 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1729 /* The enum bindings will already be on the TYPE_FIELDS, so don't
1730 count them twice. */
1732 n_fields
= count_class_fields (klass
);
1734 n_fields
= list_length (TYPE_VALUES (enumtype
));
1736 if (member_vec
|| n_fields
>= 8)
1738 vec_safe_reserve_exact (member_vec
, n_fields
);
1739 if (CLASSTYPE_MEMBER_VEC (klass
))
1740 member_vec_append_enum_values (member_vec
, enumtype
);
1742 member_vec_append_class_fields (member_vec
, klass
);
1743 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1744 member_vec
->qsort (member_name_cmp
);
1745 member_vec_dedup (member_vec
);
1749 /* Compute the chain index of a binding_entry given the HASH value of its
1750 name and the total COUNT of chains. COUNT is assumed to be a power
1753 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1755 /* A free list of "binding_entry"s awaiting for re-use. */
1757 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1759 /* The binding oracle; see cp-tree.h. */
1761 cp_binding_oracle_function
*cp_binding_oracle
;
1763 /* If we have a binding oracle, ask it for all namespace-scoped
1764 definitions of NAME. */
1767 query_oracle (tree name
)
1769 if (!cp_binding_oracle
)
1772 /* LOOKED_UP holds the set of identifiers that we have already
1773 looked up with the oracle. */
1774 static hash_set
<tree
> looked_up
;
1775 if (looked_up
.add (name
))
1778 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1781 /* Create a binding_entry object for (NAME, TYPE). */
1783 static inline binding_entry
1784 binding_entry_make (tree name
, tree type
)
1786 binding_entry entry
;
1788 if (free_binding_entry
)
1790 entry
= free_binding_entry
;
1791 free_binding_entry
= entry
->chain
;
1794 entry
= ggc_alloc
<binding_entry_s
> ();
1798 entry
->chain
= NULL
;
1803 /* Put ENTRY back on the free list. */
1806 binding_entry_free (binding_entry entry
)
1810 entry
->chain
= free_binding_entry
;
1811 free_binding_entry
= entry
;
1815 /* The datatype used to implement the mapping from names to types at
1817 struct GTY(()) binding_table_s
{
1818 /* Array of chains of "binding_entry"s */
1819 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1821 /* The number of chains in this table. This is the length of the
1822 member "chain" considered as an array. */
1825 /* Number of "binding_entry"s in this table. */
1829 /* Construct TABLE with an initial CHAIN_COUNT. */
1832 binding_table_construct (binding_table table
, size_t chain_count
)
1834 table
->chain_count
= chain_count
;
1835 table
->entry_count
= 0;
1836 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1839 /* Make TABLE's entries ready for reuse. */
1842 binding_table_free (binding_table table
)
1850 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1852 binding_entry temp
= table
->chain
[i
];
1853 while (temp
!= NULL
)
1855 binding_entry entry
= temp
;
1856 temp
= entry
->chain
;
1857 binding_entry_free (entry
);
1859 table
->chain
[i
] = NULL
;
1861 table
->entry_count
= 0;
1865 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1867 static inline binding_table
1868 binding_table_new (size_t chain_count
)
1870 binding_table table
= ggc_alloc
<binding_table_s
> ();
1871 table
->chain
= NULL
;
1872 binding_table_construct (table
, chain_count
);
1876 /* Expand TABLE to twice its current chain_count. */
1879 binding_table_expand (binding_table table
)
1881 const size_t old_chain_count
= table
->chain_count
;
1882 const size_t old_entry_count
= table
->entry_count
;
1883 const size_t new_chain_count
= 2 * old_chain_count
;
1884 binding_entry
*old_chains
= table
->chain
;
1887 binding_table_construct (table
, new_chain_count
);
1888 for (i
= 0; i
< old_chain_count
; ++i
)
1890 binding_entry entry
= old_chains
[i
];
1891 for (; entry
!= NULL
; entry
= old_chains
[i
])
1893 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1894 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1896 old_chains
[i
] = entry
->chain
;
1897 entry
->chain
= table
->chain
[j
];
1898 table
->chain
[j
] = entry
;
1901 table
->entry_count
= old_entry_count
;
1904 /* Insert a binding for NAME to TYPE into TABLE. */
1907 binding_table_insert (binding_table table
, tree name
, tree type
)
1909 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1910 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1911 binding_entry entry
= binding_entry_make (name
, type
);
1913 entry
->chain
= table
->chain
[i
];
1914 table
->chain
[i
] = entry
;
1915 ++table
->entry_count
;
1917 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1918 binding_table_expand (table
);
1921 /* Return the binding_entry, if any, that maps NAME. */
1924 binding_table_find (binding_table table
, tree name
)
1926 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1927 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1929 while (entry
!= NULL
&& entry
->name
!= name
)
1930 entry
= entry
->chain
;
1935 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1938 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1946 chain_count
= table
->chain_count
;
1947 for (i
= 0; i
< chain_count
; ++i
)
1949 binding_entry entry
= table
->chain
[i
];
1950 for (; entry
!= NULL
; entry
= entry
->chain
)
1955 #ifndef ENABLE_SCOPE_CHECKING
1956 # define ENABLE_SCOPE_CHECKING 0
1958 # define ENABLE_SCOPE_CHECKING 1
1961 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1963 static GTY((deletable
)) cxx_binding
*free_bindings
;
1965 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1969 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1971 binding
->value
= value
;
1972 binding
->type
= type
;
1973 binding
->previous
= NULL
;
1976 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1978 static cxx_binding
*
1979 cxx_binding_make (tree value
, tree type
)
1981 cxx_binding
*binding
= free_bindings
;
1984 free_bindings
= binding
->previous
;
1986 binding
= ggc_alloc
<cxx_binding
> ();
1988 /* Clear flags by default. */
1989 LOCAL_BINDING_P (binding
) = false;
1990 INHERITED_VALUE_BINDING_P (binding
) = false;
1991 HIDDEN_TYPE_BINDING_P (binding
) = false;
1993 cxx_binding_init (binding
, value
, type
);
1998 /* Put BINDING back on the free list. */
2001 cxx_binding_free (cxx_binding
*binding
)
2003 binding
->scope
= NULL
;
2004 binding
->previous
= free_bindings
;
2005 free_bindings
= binding
;
2008 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2009 bindings) in the class scope indicated by SCOPE. */
2011 static cxx_binding
*
2012 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
2014 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
2015 cxx_binding
*binding
= cb
.base
;
2016 vec_safe_push (scope
->class_shadowed
, cb
);
2017 binding
->scope
= scope
;
2021 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2022 level at which this declaration is being bound. */
2025 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2027 cxx_binding
*binding
;
2029 if (level
!= class_binding_level
)
2031 binding
= cxx_binding_make (decl
, NULL_TREE
);
2032 binding
->scope
= level
;
2035 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2037 /* Now, fill in the binding information. */
2038 binding
->previous
= IDENTIFIER_BINDING (id
);
2039 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2041 /* And put it on the front of the list of bindings for ID. */
2042 IDENTIFIER_BINDING (id
) = binding
;
2045 /* Remove the binding for DECL which should be the innermost binding
2049 pop_local_binding (tree id
, tree decl
)
2051 if (id
== NULL_TREE
)
2052 /* It's easiest to write the loops that call this function without
2053 checking whether or not the entities involved have names. We
2054 get here for such an entity. */
2057 /* Get the innermost binding for ID. */
2058 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2060 /* The name should be bound. */
2061 gcc_assert (binding
!= NULL
);
2063 /* The DECL will be either the ordinary binding or the type binding
2064 for this identifier. Remove that binding. We don't have to
2065 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2067 if (binding
->value
== decl
)
2068 binding
->value
= NULL_TREE
;
2071 gcc_checking_assert (binding
->type
== decl
);
2072 binding
->type
= NULL_TREE
;
2075 if (!binding
->value
&& !binding
->type
)
2077 /* We're completely done with the innermost binding for this
2078 identifier. Unhook it from the list of bindings. */
2079 IDENTIFIER_BINDING (id
) = binding
->previous
;
2081 /* Add it to the free list. */
2082 cxx_binding_free (binding
);
2086 /* Remove the bindings for the decls of the current level and leave
2087 the current scope. */
2090 pop_bindings_and_leave_scope (void)
2092 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2094 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2095 tree name
= OVL_NAME (decl
);
2097 pop_local_binding (name
, decl
);
2103 /* Strip non dependent using declarations. If DECL is dependent,
2104 surreptitiously create a typename_type and return it. */
2107 strip_using_decl (tree decl
)
2109 if (decl
== NULL_TREE
)
2112 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2113 decl
= USING_DECL_DECLS (decl
);
2115 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2116 && USING_DECL_TYPENAME_P (decl
))
2118 /* We have found a type introduced by a using
2119 declaration at class scope that refers to a dependent
2122 using typename :: [opt] nested-name-specifier unqualified-id ;
2124 decl
= make_typename_type (USING_DECL_SCOPE (decl
),
2126 typename_type
, tf_error
);
2127 if (decl
!= error_mark_node
)
2128 decl
= TYPE_NAME (decl
);
2134 /* Return true if OVL is an overload for an anticipated builtin. */
2137 anticipated_builtin_p (tree ovl
)
2139 if (TREE_CODE (ovl
) != OVERLOAD
)
2142 if (!OVL_HIDDEN_P (ovl
))
2145 tree fn
= OVL_FUNCTION (ovl
);
2146 gcc_checking_assert (DECL_ANTICIPATED (fn
));
2148 if (DECL_BUILTIN_P (fn
))
2154 /* BINDING records an existing declaration for a name in the current scope.
2155 But, DECL is another declaration for that same identifier in the
2156 same scope. This is the `struct stat' hack whereby a non-typedef
2157 class name or enum-name can be bound at the same level as some other
2161 A class name (9.1) or enumeration name (7.2) can be hidden by the
2162 name of an object, function, or enumerator declared in the same scope.
2163 If a class or enumeration name and an object, function, or enumerator
2164 are declared in the same scope (in any order) with the same name, the
2165 class or enumeration name is hidden wherever the object, function, or
2166 enumerator name is visible.
2168 It's the responsibility of the caller to check that
2169 inserting this name is valid here. Returns nonzero if the new binding
2173 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2175 tree bval
= binding
->value
;
2177 tree target_bval
= strip_using_decl (bval
);
2178 tree target_decl
= strip_using_decl (decl
);
2180 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2181 && target_decl
!= target_bval
2182 && (TREE_CODE (target_bval
) != TYPE_DECL
2183 /* We allow pushing an enum multiple times in a class
2184 template in order to handle late matching of underlying
2185 type on an opaque-enum-declaration followed by an
2187 || (processing_template_decl
2188 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2189 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2190 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2191 (TREE_TYPE (target_decl
)))
2192 || dependent_type_p (ENUM_UNDERLYING_TYPE
2193 (TREE_TYPE (target_bval
)))))))
2194 /* The new name is the type name. */
2195 binding
->type
= decl
;
2196 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2197 an inherited type-binding out of the way to make room
2198 for a new value binding. */
2200 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2201 has been used in a non-class scope prior declaration.
2202 In that case, we should have already issued a
2203 diagnostic; for graceful error recovery purpose, pretend
2204 this was the intended declaration for that name. */
2205 || target_bval
== error_mark_node
2206 /* If TARGET_BVAL is anticipated but has not yet been
2207 declared, pretend it is not there at all. */
2208 || anticipated_builtin_p (target_bval
))
2209 binding
->value
= decl
;
2210 else if (TREE_CODE (target_bval
) == TYPE_DECL
2211 && DECL_ARTIFICIAL (target_bval
)
2212 && target_decl
!= target_bval
2213 && (TREE_CODE (target_decl
) != TYPE_DECL
2214 || same_type_p (TREE_TYPE (target_decl
),
2215 TREE_TYPE (target_bval
))))
2217 /* The old binding was a type name. It was placed in
2218 VALUE field because it was thought, at the point it was
2219 declared, to be the only entity with such a name. Move the
2220 type name into the type slot; it is now hidden by the new
2222 binding
->type
= bval
;
2223 binding
->value
= decl
;
2224 binding
->value_is_inherited
= false;
2226 else if (TREE_CODE (target_bval
) == TYPE_DECL
2227 && TREE_CODE (target_decl
) == TYPE_DECL
2228 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2229 && binding
->scope
->kind
!= sk_class
2230 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2231 /* If either type involves template parameters, we must
2232 wait until instantiation. */
2233 || uses_template_parms (TREE_TYPE (target_decl
))
2234 || uses_template_parms (TREE_TYPE (target_bval
))))
2235 /* We have two typedef-names, both naming the same type to have
2236 the same name. In general, this is OK because of:
2240 In a given scope, a typedef specifier can be used to redefine
2241 the name of any type declared in that scope to refer to the
2242 type to which it already refers.
2244 However, in class scopes, this rule does not apply due to the
2245 stricter language in [class.mem] prohibiting redeclarations of
2248 /* There can be two block-scope declarations of the same variable,
2249 so long as they are `extern' declarations. However, there cannot
2250 be two declarations of the same static data member:
2254 A member shall not be declared twice in the
2255 member-specification. */
2256 else if (VAR_P (target_decl
)
2257 && VAR_P (target_bval
)
2258 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2259 && !DECL_CLASS_SCOPE_P (target_decl
))
2261 duplicate_decls (decl
, binding
->value
);
2264 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2265 && TREE_CODE (bval
) == NAMESPACE_DECL
2266 && DECL_NAMESPACE_ALIAS (decl
)
2267 && DECL_NAMESPACE_ALIAS (bval
)
2268 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2269 /* [namespace.alias]
2271 In a declarative region, a namespace-alias-definition can be
2272 used to redefine a namespace-alias declared in that declarative
2273 region to refer only to the namespace to which it already
2278 if (!error_operand_p (bval
))
2279 diagnose_name_conflict (decl
, bval
);
2286 /* Diagnose a name conflict between DECL and BVAL. */
2289 diagnose_name_conflict (tree decl
, tree bval
)
2291 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2292 && TREE_CODE (decl
) != NAMESPACE_DECL
2293 && !DECL_DECLARES_FUNCTION_P (decl
)
2294 && (TREE_CODE (decl
) != TYPE_DECL
2295 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2296 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2298 if (concept_definition_p (decl
))
2299 error ("redeclaration of %q#D with different template parameters",
2302 error ("redeclaration of %q#D", decl
);
2305 error ("%q#D conflicts with a previous declaration", decl
);
2307 inform (location_of (bval
), "previous declaration %q#D", bval
);
2310 /* Wrapper for supplement_binding_1. */
2313 supplement_binding (cxx_binding
*binding
, tree decl
)
2316 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2317 ret
= supplement_binding_1 (binding
, decl
);
2318 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2322 /* Replace BINDING's current value on its scope's name list with
2326 update_local_overload (cxx_binding
*binding
, tree newval
)
2330 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2331 if (*d
== binding
->value
)
2333 /* Stitch new list node in. */
2334 *d
= tree_cons (DECL_NAME (*d
), NULL_TREE
, TREE_CHAIN (*d
));
2337 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2340 TREE_VALUE (*d
) = newval
;
2343 /* Compares the parameter-type-lists of ONE and TWO and
2344 returns false if they are different. If the DECLs are template
2345 functions, the return types and the template parameter lists are
2346 compared too (DR 565). */
2349 matching_fn_p (tree one
, tree two
)
2351 if (TREE_CODE (one
) != TREE_CODE (two
))
2354 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2355 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2358 if (TREE_CODE (one
) == TEMPLATE_DECL
)
2360 /* Compare template parms. */
2361 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2362 DECL_TEMPLATE_PARMS (two
)))
2365 /* And return type. */
2366 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2367 TREE_TYPE (TREE_TYPE (two
))))
2371 if (!equivalently_constrained (one
, two
))
2377 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2378 binding value (possibly with anticipated builtins stripped).
2379 Diagnose conflicts and return updated decl. */
2382 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2383 tree old
, tree decl
, bool hiding
= false)
2385 tree old_type
= NULL_TREE
;
2386 bool hide_type
= false;
2387 bool hide_value
= false;
2391 old_type
= binding
->type
;
2392 hide_type
= HIDDEN_TYPE_BINDING_P (binding
);
2394 hide_value
= hide_type
, hide_type
= false;
2396 else if (STAT_HACK_P (*slot
))
2398 old_type
= STAT_TYPE (*slot
);
2399 hide_type
= STAT_TYPE_HIDDEN_P (*slot
);
2400 hide_value
= STAT_DECL_HIDDEN_P (*slot
);
2404 tree to_type
= old_type
;
2405 bool local_overload
= false;
2407 gcc_assert (level
->kind
== sk_namespace
? !binding
2408 : level
->kind
!= sk_class
&& !slot
);
2409 if (old
== error_mark_node
)
2412 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2414 /* Pushing an artificial decl. We should not find another
2415 artificial decl here already -- lookup_elaborated_type will
2416 have already found it. */
2417 gcc_checking_assert (!to_type
2418 && !(old
&& DECL_IMPLICIT_TYPEDEF_P (old
)));
2422 /* Put DECL into the type slot. */
2423 gcc_checking_assert (!to_type
);
2429 hide_value
= hiding
;
2434 if (old
&& DECL_IMPLICIT_TYPEDEF_P (old
))
2436 /* OLD is an implicit typedef. Move it to to_type. */
2437 gcc_checking_assert (!to_type
);
2440 hide_type
= hide_value
;
2445 if (DECL_DECLARES_FUNCTION_P (decl
))
2449 else if (OVL_P (old
))
2451 for (ovl_iterator
iter (old
); iter
; ++iter
)
2455 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2457 gcc_checking_assert (!iter
.hidden_p ());
2458 /* If a function declaration in namespace scope or
2459 block scope has the same name and the same
2460 parameter-type- list (8.3.5) as a function
2461 introduced by a using-declaration, and the
2462 declarations do not declare the same function,
2463 the program is ill-formed. [namespace.udecl]/14 */
2464 if (tree match
= duplicate_decls (decl
, fn
, hiding
))
2467 /* FIXME: To preserve existing error behavior, we
2468 still push the decl. This might change. */
2469 diagnose_name_conflict (decl
, fn
);
2476 if (to_type
!= old_type
2478 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2479 && !(DECL_IN_SYSTEM_HEADER (decl
)
2480 && DECL_IN_SYSTEM_HEADER (to_type
)))
2481 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2484 local_overload
= old
&& level
->kind
!= sk_namespace
;
2485 to_val
= ovl_insert (decl
, old
, -int (hiding
));
2489 if (TREE_CODE (old
) != TREE_CODE (decl
))
2490 /* Different kinds of decls conflict. */
2492 else if (TREE_CODE (old
) == TYPE_DECL
)
2494 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2496 /* Two type decls to the same type. Do nothing. */
2497 gcc_checking_assert (!hiding
);
2503 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2505 /* Two maybe-aliased namespaces. If they're to the same target
2506 namespace, that's ok. */
2507 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2510 /* The new one must be an alias at this point. */
2511 gcc_assert (DECL_NAMESPACE_ALIAS (decl
) && !hiding
);
2514 else if (TREE_CODE (old
) == VAR_DECL
)
2516 /* There can be two block-scope declarations of the same
2517 variable, so long as they are `extern' declarations. */
2518 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2520 else if (tree match
= duplicate_decls (decl
, old
))
2522 gcc_checking_assert (!hide_value
&& !hiding
);
2531 diagnose_name_conflict (decl
, old
);
2543 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2544 update_local_overload (binding
, to_val
);
2547 add_decl_to_level (level
, decl
);
2551 if (STAT_HACK_P (*slot
))
2553 STAT_TYPE (*slot
) = to_type
;
2554 STAT_DECL (*slot
) = to_val
;
2555 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2556 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
2558 else if (to_type
|| hide_value
)
2560 *slot
= stat_hack (to_val
, to_type
);
2561 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2562 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
2566 gcc_checking_assert (!hide_type
);
2572 binding
->type
= to_type
;
2573 binding
->value
= to_val
;
2574 HIDDEN_TYPE_BINDING_P (binding
) = hide_type
|| hide_value
;
2581 /* Table of identifiers to extern C declarations (or LISTS thereof). */
2583 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
2585 /* DECL has C linkage. If we have an existing instance, make sure the
2586 new one is compatible. Make sure it has the same exception
2587 specification [7.5, 7.6]. Add DECL to the map. */
2590 check_extern_c_conflict (tree decl
)
2592 /* Ignore artificial or system header decls. */
2593 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
2596 /* This only applies to decls at namespace scope. */
2597 if (!DECL_NAMESPACE_SCOPE_P (decl
))
2600 if (!extern_c_decls
)
2601 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
2603 tree
*slot
= extern_c_decls
2604 ->find_slot_with_hash (DECL_NAME (decl
),
2605 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
2606 if (tree old
= *slot
)
2608 if (TREE_CODE (old
) == OVERLOAD
)
2609 old
= OVL_FUNCTION (old
);
2612 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
2613 ; /* If they're in the same context, we'll have already complained
2614 about a (possible) mismatch, when inserting the decl. */
2615 else if (!decls_match (decl
, old
))
2617 else if (TREE_CODE (decl
) == FUNCTION_DECL
2618 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
2619 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2622 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
2623 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
2627 auto_diagnostic_group d
;
2628 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
2629 "conflicting C language linkage declaration %q#D", decl
);
2630 inform (DECL_SOURCE_LOCATION (old
),
2631 "previous declaration %q#D", old
);
2633 inform (DECL_SOURCE_LOCATION (decl
),
2634 "due to different exception specifications");
2639 /* The hash table expects OVERLOADS, so construct one with
2640 OLD as both the function and the chain. This allocate
2641 an excess OVERLOAD node, but it's rare to have multiple
2642 extern "C" decls of the same name. And we save
2643 complicating the hash table logic (which is used
2645 *slot
= ovl_make (old
, old
);
2647 slot
= &OVL_CHAIN (*slot
);
2649 /* Chain it on for c_linkage_binding's use. */
2650 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
2657 /* Returns a list of C-linkage decls with the name NAME. Used in
2658 c-family/c-pragma.c to implement redefine_extname pragma. */
2661 c_linkage_bindings (tree name
)
2664 if (tree
*slot
= extern_c_decls
2665 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
2667 tree result
= *slot
;
2668 if (TREE_CODE (result
) == OVERLOAD
)
2669 result
= OVL_CHAIN (result
);
2676 /* Subroutine of check_local_shadow. */
2679 inform_shadowed (tree shadowed
)
2681 inform (DECL_SOURCE_LOCATION (shadowed
),
2682 "shadowed declaration is here");
2685 /* DECL is being declared at a local scope. Emit suitable shadow
2689 check_local_shadow (tree decl
)
2691 /* Don't complain about the parms we push and then pop
2692 while tentatively parsing a function declarator. */
2693 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
2696 /* External decls are something else. */
2697 if (DECL_EXTERNAL (decl
))
2700 tree old
= NULL_TREE
;
2701 cp_binding_level
*old_scope
= NULL
;
2702 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
2704 old
= binding
->value
;
2705 old_scope
= binding
->scope
;
2709 && (TREE_CODE (old
) == PARM_DECL
2711 || (TREE_CODE (old
) == TYPE_DECL
2712 && (!DECL_ARTIFICIAL (old
)
2713 || TREE_CODE (decl
) == TYPE_DECL
)))
2714 && DECL_FUNCTION_SCOPE_P (old
)
2715 && (!DECL_ARTIFICIAL (decl
)
2716 || is_capture_proxy (decl
)
2717 || DECL_IMPLICIT_TYPEDEF_P (decl
)
2718 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
2720 /* DECL shadows a local thing possibly of interest. */
2722 /* DR 2211: check that captures and parameters
2723 do not have the same name. */
2724 if (is_capture_proxy (decl
))
2726 if (current_lambda_expr ()
2727 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
2728 && TREE_CODE (old
) == PARM_DECL
2729 && DECL_NAME (decl
) != this_identifier
)
2731 error_at (DECL_SOURCE_LOCATION (old
),
2732 "lambda parameter %qD "
2733 "previously declared as a capture", old
);
2737 /* Don't complain if it's from an enclosing function. */
2738 else if (DECL_CONTEXT (old
) == current_function_decl
2739 && TREE_CODE (decl
) != PARM_DECL
2740 && TREE_CODE (old
) == PARM_DECL
)
2742 /* Go to where the parms should be and see if we find
2744 cp_binding_level
*b
= current_binding_level
->level_chain
;
2746 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2747 /* Skip the ctor/dtor cleanup level. */
2750 /* [basic.scope.param] A parameter name shall not be redeclared
2751 in the outermost block of the function definition. */
2752 if (b
->kind
== sk_function_parms
)
2754 error_at (DECL_SOURCE_LOCATION (decl
),
2755 "declaration of %q#D shadows a parameter", decl
);
2756 inform (DECL_SOURCE_LOCATION (old
),
2757 "%q#D previously declared here", old
);
2762 /* The local structure or class can't use parameters of
2763 the containing function anyway. */
2764 if (DECL_CONTEXT (old
) != current_function_decl
)
2766 for (cp_binding_level
*scope
= current_binding_level
;
2767 scope
!= old_scope
; scope
= scope
->level_chain
)
2768 if (scope
->kind
== sk_class
2769 && !LAMBDA_TYPE_P (scope
->this_entity
))
2772 /* Error if redeclaring a local declared in a
2773 init-statement or in the condition of an if or
2774 switch statement when the new declaration is in the
2775 outermost block of the controlled statement.
2776 Redeclaring a variable from a for or while condition is
2777 detected elsewhere. */
2778 else if (VAR_P (old
)
2779 && old_scope
== current_binding_level
->level_chain
2780 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2782 auto_diagnostic_group d
;
2783 error_at (DECL_SOURCE_LOCATION (decl
),
2784 "redeclaration of %q#D", decl
);
2785 inform (DECL_SOURCE_LOCATION (old
),
2786 "%q#D previously declared here", old
);
2790 3.3.3/3: The name declared in an exception-declaration (...)
2791 shall not be redeclared in the outermost block of the handler.
2792 3.3.3/2: A parameter name shall not be redeclared (...) in
2793 the outermost block of any handler associated with a
2795 3.4.1/15: The function parameter names shall not be redeclared
2796 in the exception-declaration nor in the outermost block of a
2797 handler for the function-try-block. */
2798 else if ((TREE_CODE (old
) == VAR_DECL
2799 && old_scope
== current_binding_level
->level_chain
2800 && old_scope
->kind
== sk_catch
)
2801 || (TREE_CODE (old
) == PARM_DECL
2802 && (current_binding_level
->kind
== sk_catch
2803 || current_binding_level
->level_chain
->kind
== sk_catch
)
2804 && in_function_try_handler
))
2806 auto_diagnostic_group d
;
2807 if (permerror (DECL_SOURCE_LOCATION (decl
),
2808 "redeclaration of %q#D", decl
))
2809 inform (DECL_SOURCE_LOCATION (old
),
2810 "%q#D previously declared here", old
);
2814 /* If '-Wshadow=compatible-local' is specified without other
2815 -Wshadow= flags, we will warn only when the type of the
2816 shadowing variable (DECL) can be converted to that of the
2817 shadowed parameter (OLD_LOCAL). The reason why we only check
2818 if DECL's type can be converted to OLD_LOCAL's type (but not the
2819 other way around) is because when users accidentally shadow a
2820 parameter, more than often they would use the variable
2821 thinking (mistakenly) it's still the parameter. It would be
2822 rare that users would use the variable in the place that
2823 expects the parameter but thinking it's a new decl.
2824 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
2825 warns regardless of whether one of the types involved
2826 is a subclass of the other, since that is never okay. */
2828 enum opt_code warning_code
;
2830 warning_code
= OPT_Wshadow
;
2831 else if ((TREE_TYPE (old
)
2833 && same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2834 || TREE_CODE (decl
) == TYPE_DECL
2835 || TREE_CODE (old
) == TYPE_DECL
2836 || (!dependent_type_p (TREE_TYPE (decl
))
2837 && !dependent_type_p (TREE_TYPE (old
))
2838 /* If the new decl uses auto, we don't yet know
2839 its type (the old type cannot be using auto
2840 at this point, without also being
2841 dependent). This is an indication we're
2842 (now) doing the shadow checking too
2844 && !type_uses_auto (TREE_TYPE (decl
))
2845 && can_convert_arg (TREE_TYPE (old
), TREE_TYPE (decl
),
2846 decl
, LOOKUP_IMPLICIT
, tf_none
)))
2847 warning_code
= OPT_Wshadow_compatible_local
;
2849 warning_code
= OPT_Wshadow_local
;
2852 if (TREE_CODE (old
) == PARM_DECL
)
2853 msg
= "declaration of %q#D shadows a parameter";
2854 else if (is_capture_proxy (old
))
2855 msg
= "declaration of %qD shadows a lambda capture";
2857 msg
= "declaration of %qD shadows a previous local";
2859 auto_diagnostic_group d
;
2860 if (warning_at (DECL_SOURCE_LOCATION (decl
), warning_code
, msg
, decl
))
2861 inform_shadowed (old
);
2868 /* Don't warn for artificial things that are not implicit typedefs. */
2869 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2872 if (nonlambda_method_basetype ())
2873 if (tree member
= lookup_member (current_nonlambda_class_type (),
2874 DECL_NAME (decl
), /*protect=*/0,
2875 /*want_type=*/false, tf_warning_or_error
))
2877 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2879 /* Warn if a variable shadows a non-function, or the variable
2880 is a function or a pointer-to-function. */
2882 || TREE_CODE (decl
) == FUNCTION_DECL
2883 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2884 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2886 auto_diagnostic_group d
;
2887 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
2888 "declaration of %qD shadows a member of %qT",
2889 decl
, current_nonlambda_class_type ())
2891 inform_shadowed (member
);
2896 /* Now look for a namespace shadow. */
2897 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2900 || (TREE_CODE (old
) == TYPE_DECL
2901 && (!DECL_ARTIFICIAL (old
)
2902 || TREE_CODE (decl
) == TYPE_DECL
)))
2903 && !instantiating_current_function_p ())
2904 /* XXX shadow warnings in outer-more namespaces */
2906 auto_diagnostic_group d
;
2907 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
2908 "declaration of %qD shadows a global declaration",
2910 inform_shadowed (old
);
2917 /* DECL is being pushed inside function CTX. Set its context, if
2921 set_decl_context_in_fn (tree ctx
, tree decl
)
2923 if (TREE_CODE (decl
) == FUNCTION_DECL
2924 || (VAR_P (decl
) && DECL_EXTERNAL (decl
)))
2925 /* Make sure local externs are marked as such. OMP UDRs really
2926 are nested functions. */
2927 gcc_checking_assert (DECL_LOCAL_DECL_P (decl
)
2928 && (DECL_NAMESPACE_SCOPE_P (decl
)
2929 || (TREE_CODE (decl
) == FUNCTION_DECL
2930 && DECL_OMP_DECLARE_REDUCTION_P (decl
))));
2932 if (!DECL_CONTEXT (decl
)
2933 /* When parsing the parameter list of a function declarator,
2934 don't set DECL_CONTEXT to an enclosing function. When we
2935 push the PARM_DECLs in order to process the function body,
2936 current_binding_level->this_entity will be set. */
2937 && !(TREE_CODE (decl
) == PARM_DECL
2938 && current_binding_level
->kind
== sk_function_parms
2939 && current_binding_level
->this_entity
== NULL
))
2940 DECL_CONTEXT (decl
) = ctx
;
2943 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2944 name is already bound at the current level.
2946 [basic.link] If there is a visible declaration of an entity with
2947 linkage having the same name and type, ignoring entities declared
2948 outside the innermost enclosing namespace scope, the block scope
2949 declaration declares that same entity and receives the linkage of
2950 the previous declaration.
2952 Also, make sure that this decl matches any existing external decl
2953 in the enclosing namespace. */
2956 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2958 tree ns_value
= decl
; /* Unique marker. */
2962 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2966 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2967 loc_value
= ns_value
;
2969 if (loc_value
== error_mark_node
2970 /* An ambiguous lookup. */
2971 || (loc_value
&& TREE_CODE (loc_value
) == TREE_LIST
))
2972 loc_value
= NULL_TREE
;
2974 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2975 if (!iter
.hidden_p ()
2976 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2977 && decls_match (*iter
, decl
))
2979 /* The standard only says that the local extern inherits
2980 linkage from the previous decl; in particular, default
2981 args are not shared. Add the decl into a hash table to
2982 make sure only the previous decl in this case is seen
2983 by the middle end. */
2984 struct cxx_int_tree_map
*h
;
2986 /* We inherit the outer decl's linkage. But we're a
2988 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2990 if (cp_function_chain
->extern_decl_map
== NULL
)
2991 cp_function_chain
->extern_decl_map
2992 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2994 h
= ggc_alloc
<cxx_int_tree_map
> ();
2995 h
->uid
= DECL_UID (decl
);
2997 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2998 ->find_slot (h
, INSERT
);
3004 if (TREE_PUBLIC (decl
))
3006 /* DECL is externally visible. Make sure it matches a matching
3007 decl in the namespace scope. We only really need to check
3008 this when inserting the decl, not when we find an existing
3009 match in the current scope. However, in practice we're
3010 going to be inserting a new decl in the majority of cases --
3011 who writes multiple extern decls for the same thing in the
3012 same local scope? Doing it here often avoids a duplicate
3013 namespace lookup. */
3015 /* Avoid repeating a lookup. */
3016 if (ns_value
== decl
)
3017 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3019 if (ns_value
== error_mark_node
3020 || (ns_value
&& TREE_CODE (ns_value
) == TREE_LIST
))
3021 ns_value
= NULL_TREE
;
3023 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
3027 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
3028 ; /* Not externally visible. */
3029 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
3030 ; /* Both are extern "C", we'll check via that mechanism. */
3031 else if (TREE_CODE (other
) != TREE_CODE (decl
)
3032 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
3033 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
3034 COMPARE_REDECLARATION
)))
3036 auto_diagnostic_group d
;
3037 if (permerror (DECL_SOURCE_LOCATION (decl
),
3038 "local external declaration %q#D", decl
))
3039 inform (DECL_SOURCE_LOCATION (other
),
3040 "does not match previous declaration %q#D", other
);
3047 /* Record DECL as belonging to the current lexical scope. Check for
3048 errors (such as an incompatible declaration for the same name
3049 already seen in the same scope). IS_FRIEND is true if DECL is
3050 declared as a friend.
3052 Returns either DECL or an old decl for the same name. If an old
3053 decl is returned, it may have been smashed to agree with what DECL
3057 do_pushdecl (tree decl
, bool hiding
)
3059 if (decl
== error_mark_node
)
3060 return error_mark_node
;
3062 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
&& !hiding
)
3063 set_decl_context_in_fn (current_function_decl
, decl
);
3065 /* The binding level we will be pushing into. During local class
3066 pushing, we want to push to the containing scope. */
3067 cp_binding_level
*level
= current_binding_level
;
3068 while (level
->kind
== sk_class
3069 || level
->kind
== sk_cleanup
)
3070 level
= level
->level_chain
;
3072 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3073 insert it. Other NULL-named decls, not so much. */
3074 tree name
= DECL_NAME (decl
);
3075 if (name
|| TREE_CODE (decl
) == NAMESPACE_DECL
)
3077 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3078 tree ns
= NULL_TREE
; /* Searched namespace. */
3079 tree
*slot
= NULL
; /* Binding slot in namespace. */
3080 tree old
= NULL_TREE
;
3083 /* We should never unknownly push an anticipated decl. */
3084 gcc_checking_assert (!((TREE_CODE (decl
) == TYPE_DECL
3085 || TREE_CODE (decl
) == FUNCTION_DECL
3086 || TREE_CODE (decl
) == TEMPLATE_DECL
)
3087 && DECL_LANG_SPECIFIC (decl
)
3088 && DECL_ANTICIPATED (decl
)));
3090 if (level
->kind
== sk_namespace
)
3092 /* We look in the decl's namespace for an existing
3093 declaration, even though we push into the current
3095 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3096 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3097 /* Create the binding, if this is current namespace, because
3098 that's where we'll be pushing anyway. */
3099 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3101 old
= MAYBE_STAT_DECL (*slot
);
3105 binding
= find_local_binding (level
, name
);
3107 old
= binding
->value
;
3110 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
3111 && DECL_EXTERNAL (decl
))
3112 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
3114 if (old
== error_mark_node
)
3117 for (ovl_iterator
iter (old
); iter
; ++iter
)
3118 if (iter
.using_p ())
3119 ; /* Ignore using decls here. */
3121 = duplicate_decls (decl
, *iter
, hiding
, iter
.hidden_p ()))
3123 if (match
== error_mark_node
)
3125 else if (TREE_CODE (match
) == TYPE_DECL
)
3126 /* The IDENTIFIER will have the type referring to the
3127 now-smashed TYPE_DECL, because ...? Reset it. */
3128 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3129 else if (iter
.hidden_p () && !hiding
)
3131 /* Unhiding a previously hidden decl. */
3132 tree head
= iter
.reveal_node (old
);
3135 gcc_checking_assert (ns
);
3136 if (STAT_HACK_P (*slot
))
3137 STAT_DECL (*slot
) = head
;
3141 if (DECL_EXTERN_C_P (match
))
3142 /* We need to check and register the decl now. */
3143 check_extern_c_conflict (match
);
3148 /* We are pushing a new decl. */
3150 /* Skip a hidden builtin we failed to match already. There can
3152 if (old
&& anticipated_builtin_p (old
))
3153 old
= OVL_CHAIN (old
);
3155 check_template_shadow (decl
);
3157 if (DECL_DECLARES_FUNCTION_P (decl
))
3159 check_default_args (decl
);
3163 if (level
->kind
!= sk_namespace
)
3165 /* In a local class, a friend function declaration must
3166 find a matching decl in the innermost non-class scope.
3167 [class.friend/11] */
3168 error_at (DECL_SOURCE_LOCATION (decl
),
3169 "friend declaration %qD in local class without "
3170 "prior local declaration", decl
);
3171 /* Don't attempt to push it. */
3172 return error_mark_node
;
3174 /* Hide it from ordinary lookup. */
3175 DECL_ANTICIPATED (decl
) = true;
3179 if (level
->kind
!= sk_namespace
)
3181 check_local_shadow (decl
);
3183 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3184 /* A local namespace alias. */
3185 set_identifier_type_value_with_scope (name
, NULL_TREE
, level
);
3188 binding
= create_local_binding (level
, name
);
3192 ns
= current_namespace
;
3193 slot
= find_namespace_slot (ns
, name
, true);
3194 /* Update OLD to reflect the namespace we're going to be
3196 old
= MAYBE_STAT_DECL (*slot
);
3199 old
= update_binding (level
, binding
, slot
, old
, decl
, hiding
);
3202 /* An existing decl matched, use it. */
3204 else if (TREE_CODE (decl
) == TYPE_DECL
)
3206 tree type
= TREE_TYPE (decl
);
3208 if (type
!= error_mark_node
)
3210 if (TYPE_NAME (type
) != decl
)
3211 set_underlying_type (decl
);
3213 set_identifier_type_value_with_scope (name
, decl
, level
);
3216 /* If this is a locally defined typedef in a function that
3217 is not a template instantation, record it to implement
3218 -Wunused-local-typedefs. */
3219 if (!instantiating_current_function_p ())
3220 record_locally_defined_typedef (decl
);
3222 else if (VAR_P (decl
))
3223 maybe_register_incomplete_var (decl
);
3225 if ((VAR_P (decl
) || TREE_CODE (decl
) == FUNCTION_DECL
)
3226 && DECL_EXTERN_C_P (decl
))
3227 check_extern_c_conflict (decl
);
3230 add_decl_to_level (level
, decl
);
3235 /* Record a decl-node X as belonging to the current lexical scope.
3236 It's a friend if IS_FRIEND is true -- which affects exactly where
3240 pushdecl (tree x
, bool hiding
)
3242 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3243 tree ret
= do_pushdecl (x
, hiding
);
3244 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3248 /* Enter DECL into the symbol table, if that's appropriate. Returns
3249 DECL, or a modified version thereof. */
3252 maybe_push_decl (tree decl
)
3254 tree type
= TREE_TYPE (decl
);
3256 /* Add this decl to the current binding level, but not if it comes
3257 from another scope, e.g. a static member variable. TEM may equal
3258 DECL or it may be a previous decl of the same name. */
3259 if (decl
== error_mark_node
3260 || (TREE_CODE (decl
) != PARM_DECL
3261 && DECL_CONTEXT (decl
) != NULL_TREE
3262 /* Definitions of namespace members outside their namespace are
3264 && !DECL_NAMESPACE_SCOPE_P (decl
))
3265 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
3266 || type
== unknown_type_node
3267 /* The declaration of a template specialization does not affect
3268 the functions available for overload resolution, so we do not
3270 || (TREE_CODE (decl
) == FUNCTION_DECL
3271 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
3274 return pushdecl (decl
);
3277 /* Bind DECL to ID in the current_binding_level, assumed to be a local
3278 binding level. If IS_USING is true, DECL got here through a
3279 using-declaration. */
3282 push_local_binding (tree id
, tree decl
, bool is_using
)
3284 /* Skip over any local classes. This makes sense if we call
3285 push_local_binding with a friend decl of a local class. */
3286 cp_binding_level
*b
= innermost_nonclass_level ();
3288 gcc_assert (b
->kind
!= sk_namespace
);
3289 if (find_local_binding (b
, id
))
3291 /* Supplement the existing binding. */
3292 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
3293 /* It didn't work. Something else must be bound at this
3294 level. Do not add DECL to the list of things to pop
3299 /* Create a new binding. */
3300 push_binding (id
, decl
, b
);
3302 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
3303 /* We must put the OVERLOAD or using into a TREE_LIST since we
3304 cannot use the decl's chain itself. */
3305 decl
= build_tree_list (id
, decl
);
3307 /* And put DECL on the list of things declared by the current
3309 add_decl_to_level (b
, decl
);
3313 /* true means unconditionally make a BLOCK for the next level pushed. */
3315 static bool keep_next_level_flag
;
3317 static int binding_depth
= 0;
3324 for (i
= 0; i
< depth
* 2; i
++)
3328 /* Return a string describing the kind of SCOPE we have. */
3330 cp_binding_level_descriptor (cp_binding_level
*scope
)
3332 /* The order of this table must match the "scope_kind"
3334 static const char* scope_kind_names
[] = {
3340 "function-parameter-scope",
3343 "template-parameter-scope",
3344 "template-explicit-spec-scope"
3346 const scope_kind kind
= scope
->explicit_spec_p
3347 ? sk_template_spec
: scope
->kind
;
3349 return scope_kind_names
[kind
];
3352 /* Output a debugging information about SCOPE when performing
3355 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
3357 const char *desc
= cp_binding_level_descriptor (scope
);
3358 if (scope
->this_entity
)
3359 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
3360 scope
->this_entity
, (void *) scope
, line
);
3362 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
3365 /* A chain of binding_level structures awaiting reuse. */
3367 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
3369 /* Insert SCOPE as the innermost binding level. */
3372 push_binding_level (cp_binding_level
*scope
)
3374 /* Add it to the front of currently active scopes stack. */
3375 scope
->level_chain
= current_binding_level
;
3376 current_binding_level
= scope
;
3377 keep_next_level_flag
= false;
3379 if (ENABLE_SCOPE_CHECKING
)
3381 scope
->binding_depth
= binding_depth
;
3382 indent (binding_depth
);
3383 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3389 /* Create a new KIND scope and make it the top of the active scopes stack.
3390 ENTITY is the scope of the associated C++ entity (namespace, class,
3391 function, C++0x enumeration); it is NULL otherwise. */
3394 begin_scope (scope_kind kind
, tree entity
)
3396 cp_binding_level
*scope
;
3398 /* Reuse or create a struct for this binding level. */
3399 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
3401 scope
= free_binding_level
;
3402 free_binding_level
= scope
->level_chain
;
3403 memset (scope
, 0, sizeof (cp_binding_level
));
3406 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
3408 scope
->this_entity
= entity
;
3409 scope
->more_cleanups_ok
= true;
3416 case sk_template_spec
:
3417 scope
->explicit_spec_p
= true;
3418 kind
= sk_template_parms
;
3420 case sk_template_parms
:
3427 case sk_scoped_enum
:
3428 case sk_function_parms
:
3429 case sk_transaction
:
3431 scope
->keep
= keep_next_level_flag
;
3435 NAMESPACE_LEVEL (entity
) = scope
;
3439 /* Should not happen. */
3445 push_binding_level (scope
);
3450 /* We're about to leave current scope. Pop the top of the stack of
3451 currently active scopes. Return the enclosing scope, now active. */
3456 cp_binding_level
*scope
= current_binding_level
;
3458 if (scope
->kind
== sk_namespace
&& class_binding_level
)
3459 current_binding_level
= class_binding_level
;
3461 /* We cannot leave a scope, if there are none left. */
3462 if (NAMESPACE_LEVEL (global_namespace
))
3463 gcc_assert (!global_scope_p (scope
));
3465 if (ENABLE_SCOPE_CHECKING
)
3467 indent (--binding_depth
);
3468 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
3472 /* Move one nesting level up. */
3473 current_binding_level
= scope
->level_chain
;
3475 /* Namespace-scopes are left most probably temporarily, not
3476 completely; they can be reopened later, e.g. in namespace-extension
3477 or any name binding activity that requires us to resume a
3478 namespace. For classes, we cache some binding levels. For other
3479 scopes, we just make the structure available for reuse. */
3480 if (scope
->kind
!= sk_namespace
3481 && scope
!= previous_class_level
)
3483 scope
->level_chain
= free_binding_level
;
3484 gcc_assert (!ENABLE_SCOPE_CHECKING
3485 || scope
->binding_depth
== binding_depth
);
3486 free_binding_level
= scope
;
3489 if (scope
->kind
== sk_class
)
3491 /* Reset DEFINING_CLASS_P to allow for reuse of a
3492 class-defining scope in a non-defining context. */
3493 scope
->defining_class_p
= 0;
3495 /* Find the innermost enclosing class scope, and reset
3496 CLASS_BINDING_LEVEL appropriately. */
3497 class_binding_level
= NULL
;
3498 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
3499 if (scope
->kind
== sk_class
)
3501 class_binding_level
= scope
;
3506 return current_binding_level
;
3509 /* When we exit a toplevel class scope, we save its binding level so
3510 that we can restore it quickly. Here, we've entered some other
3511 class, so we must invalidate our cache. */
3514 invalidate_class_lookup_cache (void)
3516 previous_class_level
->level_chain
= free_binding_level
;
3517 free_binding_level
= previous_class_level
;
3518 previous_class_level
= NULL
;
3522 resume_scope (cp_binding_level
* b
)
3524 /* Resuming binding levels is meant only for namespaces,
3525 and those cannot nest into classes. */
3526 gcc_assert (!class_binding_level
);
3527 /* Also, resuming a non-directly nested namespace is a no-no. */
3528 gcc_assert (b
->level_chain
== current_binding_level
);
3529 current_binding_level
= b
;
3530 if (ENABLE_SCOPE_CHECKING
)
3532 b
->binding_depth
= binding_depth
;
3533 indent (binding_depth
);
3534 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
3539 /* Return the innermost binding level that is not for a class scope. */
3541 static cp_binding_level
*
3542 innermost_nonclass_level (void)
3544 cp_binding_level
*b
;
3546 b
= current_binding_level
;
3547 while (b
->kind
== sk_class
)
3553 /* We're defining an object of type TYPE. If it needs a cleanup, but
3554 we're not allowed to add any more objects with cleanups to the current
3555 scope, create a new binding level. */
3558 maybe_push_cleanup_level (tree type
)
3560 if (type
!= error_mark_node
3561 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
3562 && current_binding_level
->more_cleanups_ok
== 0)
3564 begin_scope (sk_cleanup
, NULL
);
3565 current_binding_level
->statement_list
= push_stmt_list ();
3569 /* Return true if we are in the global binding level. */
3572 global_bindings_p (void)
3574 return global_scope_p (current_binding_level
);
3577 /* True if we are currently in a toplevel binding level. This
3578 means either the global binding level or a namespace in a toplevel
3579 binding level. Since there are no non-toplevel namespace levels,
3580 this really means any namespace or template parameter level. We
3581 also include a class whose context is toplevel. */
3584 toplevel_bindings_p (void)
3586 cp_binding_level
*b
= innermost_nonclass_level ();
3588 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
3591 /* True if this is a namespace scope, or if we are defining a class
3592 which is itself at namespace scope, or whose enclosing class is
3593 such a class, etc. */
3596 namespace_bindings_p (void)
3598 cp_binding_level
*b
= innermost_nonclass_level ();
3600 return b
->kind
== sk_namespace
;
3603 /* True if the innermost non-class scope is a block scope. */
3606 local_bindings_p (void)
3608 cp_binding_level
*b
= innermost_nonclass_level ();
3609 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
3612 /* True if the current level needs to have a BLOCK made. */
3617 return (current_binding_level
->blocks
!= NULL_TREE
3618 || current_binding_level
->keep
3619 || current_binding_level
->kind
== sk_cleanup
3620 || current_binding_level
->names
!= NULL_TREE
3621 || current_binding_level
->using_directives
);
3624 /* Returns the kind of the innermost scope. */
3627 innermost_scope_kind (void)
3629 return current_binding_level
->kind
;
3632 /* Returns true if this scope was created to store template parameters. */
3635 template_parm_scope_p (void)
3637 return innermost_scope_kind () == sk_template_parms
;
3640 /* If KEEP is true, make a BLOCK node for the next binding level,
3641 unconditionally. Otherwise, use the normal logic to decide whether
3642 or not to create a BLOCK. */
3645 keep_next_level (bool keep
)
3647 keep_next_level_flag
= keep
;
3650 /* Return the list of declarations of the current local scope. */
3653 get_local_decls (void)
3655 gcc_assert (current_binding_level
->kind
!= sk_namespace
3656 && current_binding_level
->kind
!= sk_class
);
3657 return current_binding_level
->names
;
3660 /* Return how many function prototypes we are currently nested inside. */
3663 function_parm_depth (void)
3666 cp_binding_level
*b
;
3668 for (b
= current_binding_level
;
3669 b
->kind
== sk_function_parms
;
3676 /* For debugging. */
3677 static int no_print_functions
= 0;
3678 static int no_print_builtins
= 0;
3681 print_binding_level (cp_binding_level
* lvl
)
3685 if (lvl
->this_entity
)
3686 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
3687 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
3688 if (lvl
->more_cleanups_ok
)
3689 fprintf (stderr
, " more-cleanups-ok");
3690 if (lvl
->have_cleanups
)
3691 fprintf (stderr
, " have-cleanups");
3692 fprintf (stderr
, "\n");
3695 fprintf (stderr
, " names:\t");
3696 /* We can probably fit 3 names to a line? */
3697 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3699 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3701 if (no_print_builtins
3702 && (TREE_CODE (t
) == TYPE_DECL
)
3703 && DECL_IS_BUILTIN (t
))
3706 /* Function decls tend to have longer names. */
3707 if (TREE_CODE (t
) == FUNCTION_DECL
)
3714 fprintf (stderr
, "\n\t");
3717 print_node_brief (stderr
, "", t
, 0);
3718 if (t
== error_mark_node
)
3722 fprintf (stderr
, "\n");
3724 if (vec_safe_length (lvl
->class_shadowed
))
3727 cp_class_binding
*b
;
3728 fprintf (stderr
, " class-shadowed:");
3729 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3730 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3731 fprintf (stderr
, "\n");
3733 if (lvl
->type_shadowed
)
3735 fprintf (stderr
, " type-shadowed:");
3736 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3738 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3740 fprintf (stderr
, "\n");
3745 debug (cp_binding_level
&ref
)
3747 print_binding_level (&ref
);
3751 debug (cp_binding_level
*ptr
)
3756 fprintf (stderr
, "<nil>\n");
3760 print_other_binding_stack (cp_binding_level
*stack
)
3762 cp_binding_level
*level
;
3763 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3765 fprintf (stderr
, "binding level %p\n", (void *) level
);
3766 print_binding_level (level
);
3771 print_binding_stack (void)
3773 cp_binding_level
*b
;
3774 fprintf (stderr
, "current_binding_level=%p\n"
3775 "class_binding_level=%p\n"
3776 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3777 (void *) current_binding_level
, (void *) class_binding_level
,
3778 (void *) NAMESPACE_LEVEL (global_namespace
));
3779 if (class_binding_level
)
3781 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3782 if (b
== current_binding_level
)
3785 b
= class_binding_level
;
3787 b
= current_binding_level
;
3790 b
= current_binding_level
;
3791 print_other_binding_stack (b
);
3792 fprintf (stderr
, "global:\n");
3793 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3796 /* Return the type associated with ID. */
3799 identifier_type_value_1 (tree id
)
3801 /* There is no type with that name, anywhere. */
3802 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3804 /* This is not the type marker, but the real thing. */
3805 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3806 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3807 /* Have to search for it. It must be on the global level, now.
3808 Ask lookup_name not to return non-types. */
3809 id
= lookup_name (id
, LOOK_where::BLOCK_NAMESPACE
, LOOK_want::TYPE
);
3811 return TREE_TYPE (id
);
3815 /* Wrapper for identifier_type_value_1. */
3818 identifier_type_value (tree id
)
3821 timevar_start (TV_NAME_LOOKUP
);
3822 ret
= identifier_type_value_1 (id
);
3823 timevar_stop (TV_NAME_LOOKUP
);
3827 /* Push a definition of struct, union or enum tag named ID. into
3828 binding_level B. DECL is a TYPE_DECL for the type. DECL has
3829 already been pushed into its binding level. This is bookkeeping to
3833 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3837 if (b
->kind
!= sk_namespace
)
3839 /* Shadow the marker, not the real thing, so that the marker
3840 gets restored later. */
3841 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3842 b
->type_shadowed
= tree_cons (id
, old_type_value
, b
->type_shadowed
);
3843 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3844 TREE_TYPE (b
->type_shadowed
) = type
;
3851 tree
*slot
= find_namespace_slot (current_namespace
, id
);
3852 gcc_checking_assert (slot
3853 && (decl
== MAYBE_STAT_TYPE (*slot
)
3854 || decl
== MAYBE_STAT_DECL (*slot
)));
3857 /* Store marker instead of real type. */
3858 type
= global_type_node
;
3861 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3864 /* As set_identifier_type_value_with_scope, but using
3865 current_binding_level. */
3868 set_identifier_type_value (tree id
, tree decl
)
3870 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3873 /* Return the name for the constructor (or destructor) for the
3877 constructor_name (tree type
)
3879 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
3881 return decl
? DECL_NAME (decl
) : NULL_TREE
;
3884 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3885 which must be a class type. */
3888 constructor_name_p (tree name
, tree type
)
3890 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3892 /* These don't have names. */
3893 if (TREE_CODE (type
) == DECLTYPE_TYPE
3894 || TREE_CODE (type
) == TYPEOF_TYPE
)
3897 if (name
&& name
== constructor_name (type
))
3903 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3904 caller to set DECL_CONTEXT properly.
3906 Note that this must only be used when X will be the new innermost
3907 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3908 without checking to see if the current IDENTIFIER_BINDING comes from a
3909 closer binding level than LEVEL. */
3912 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool hiding
= false)
3914 cp_binding_level
*b
;
3916 if (level
->kind
== sk_class
)
3918 gcc_checking_assert (!hiding
);
3919 b
= class_binding_level
;
3920 class_binding_level
= level
;
3921 pushdecl_class_level (x
);
3922 class_binding_level
= b
;
3926 tree function_decl
= current_function_decl
;
3927 if (level
->kind
== sk_namespace
)
3928 current_function_decl
= NULL_TREE
;
3929 b
= current_binding_level
;
3930 current_binding_level
= level
;
3931 x
= do_pushdecl (x
, hiding
);
3932 current_binding_level
= b
;
3933 current_function_decl
= function_decl
;
3938 /* Inject X into the local scope just before the function parms. */
3941 pushdecl_outermost_localscope (tree x
)
3943 cp_binding_level
*b
= NULL
;
3944 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3946 /* Find the scope just inside the function parms. */
3947 for (cp_binding_level
*n
= current_binding_level
;
3948 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3951 tree ret
= b
? do_pushdecl_with_scope (x
, b
) : error_mark_node
;
3952 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3957 /* Process a local-scope or namespace-scope using declaration. LOOKUP
3958 is the result of qualified lookup (both value & type are
3959 significant). FN_SCOPE_P indicates if we're at function-scope (as
3960 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
3961 bindings, which are altered to reflect the newly brought in
3965 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
3966 tree
*value_p
, tree
*type_p
)
3968 tree value
= *value_p
;
3969 tree type
= *type_p
;
3970 bool failed
= false;
3972 /* Shift the old and new bindings around so we're comparing class and
3973 enumeration names to each other. */
3974 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
3980 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
3982 lookup
.type
= lookup
.value
;
3983 lookup
.value
= NULL_TREE
;
3988 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
3990 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
3992 tree new_fn
= *usings
;
3994 /* [namespace.udecl]
3996 If a function declaration in namespace scope or block
3997 scope has the same name and the same parameter types as a
3998 function introduced by a using declaration the program is
4001 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4005 if (new_fn
== old_fn
)
4007 /* The function already exists in the current
4012 else if (old
.using_p ())
4013 continue; /* This is a using decl. */
4014 else if (old
.hidden_p () && DECL_BUILTIN_P (old_fn
))
4015 continue; /* This is an anticipated builtin. */
4016 else if (!matching_fn_p (new_fn
, old_fn
))
4017 continue; /* Parameters do not match. */
4018 else if (decls_match (new_fn
, old_fn
))
4020 /* Extern "C" in different namespaces. */
4026 diagnose_name_conflict (new_fn
, old_fn
);
4034 /* Unlike the decl-pushing case we don't drop anticipated
4035 builtins here. They don't cause a problem, and we'd
4036 like to match them with a future declaration. */
4037 value
= ovl_insert (new_fn
, value
, true);
4041 /* Ignore anticipated builtins. */
4042 && !anticipated_builtin_p (value
)
4043 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
4045 diagnose_name_conflict (lookup
.value
, value
);
4049 value
= lookup
.value
;
4051 if (lookup
.type
&& lookup
.type
!= type
)
4053 if (type
&& !decls_match (lookup
.type
, type
))
4055 diagnose_name_conflict (lookup
.type
, type
);
4062 /* If value is empty, shift any class or enumeration name back. */
4074 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4075 Both are namespaces. */
4078 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4080 int depth
= SCOPE_DEPTH (ancestor
);
4082 if (!depth
&& !inline_only
)
4083 /* The global namespace encloses everything. */
4086 while (SCOPE_DEPTH (descendant
) > depth
4087 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4088 descendant
= CP_DECL_CONTEXT (descendant
);
4090 return ancestor
== descendant
;
4093 /* Returns true if ROOT (a non-alias namespace, class, or function)
4094 encloses CHILD. CHILD may be either a class type or a namespace
4098 is_ancestor (tree root
, tree child
)
4100 gcc_checking_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4101 && !DECL_NAMESPACE_ALIAS (root
))
4102 || TREE_CODE (root
) == FUNCTION_DECL
4103 || CLASS_TYPE_P (root
));
4104 gcc_checking_assert (TREE_CODE (child
) == NAMESPACE_DECL
4105 || CLASS_TYPE_P (child
));
4107 /* The global namespace encloses everything. Early-out for the
4109 if (root
== global_namespace
)
4112 /* Search CHILD until we reach namespace scope. */
4113 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4115 /* If we've reached the ROOT, it encloses CHILD. */
4119 /* Go out one level. */
4121 child
= TYPE_NAME (child
);
4122 child
= CP_DECL_CONTEXT (child
);
4125 if (TREE_CODE (root
) != NAMESPACE_DECL
)
4126 /* Failed to meet the non-namespace we were looking for. */
4129 if (tree alias
= DECL_NAMESPACE_ALIAS (child
))
4132 return is_nested_namespace (root
, child
);
4135 /* Enter the class or namespace scope indicated by T suitable for name
4136 lookup. T can be arbitrary scope, not necessary nested inside the
4137 current scope. Returns a non-null scope to pop iff pop_scope
4138 should be called later to exit this scope. */
4143 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4144 push_decl_namespace (t
);
4145 else if (CLASS_TYPE_P (t
))
4147 if (!at_class_scope_p ()
4148 || !same_type_p (current_class_type
, t
))
4149 push_nested_class (t
);
4151 /* T is the same as the current scope. There is therefore no
4152 need to re-enter the scope. Since we are not actually
4153 pushing a new scope, our caller should not call
4161 /* Leave scope pushed by push_scope. */
4168 if (TREE_CODE (t
) == NAMESPACE_DECL
)
4169 pop_decl_namespace ();
4170 else if CLASS_TYPE_P (t
)
4171 pop_nested_class ();
4174 /* Subroutine of push_inner_scope. */
4177 push_inner_scope_r (tree outer
, tree inner
)
4182 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4185 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4187 push_inner_scope_r (outer
, prev
);
4188 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4190 cp_binding_level
*save_template_parm
= 0;
4191 /* Temporary take out template parameter scopes. They are saved
4192 in reversed order in save_template_parm. */
4193 while (current_binding_level
->kind
== sk_template_parms
)
4195 cp_binding_level
*b
= current_binding_level
;
4196 current_binding_level
= b
->level_chain
;
4197 b
->level_chain
= save_template_parm
;
4198 save_template_parm
= b
;
4201 resume_scope (NAMESPACE_LEVEL (inner
));
4202 current_namespace
= inner
;
4204 /* Restore template parameter scopes. */
4205 while (save_template_parm
)
4207 cp_binding_level
*b
= save_template_parm
;
4208 save_template_parm
= b
->level_chain
;
4209 b
->level_chain
= current_binding_level
;
4210 current_binding_level
= b
;
4217 /* Enter the scope INNER from current scope. INNER must be a scope
4218 nested inside current scope. This works with both name lookup and
4219 pushing name into scope. In case a template parameter scope is present,
4220 namespace is pushed under the template parameter scope according to
4221 name lookup rule in 14.6.1/6.
4223 Return the former current scope suitable for pop_inner_scope. */
4226 push_inner_scope (tree inner
)
4228 tree outer
= current_scope ();
4230 outer
= current_namespace
;
4232 push_inner_scope_r (outer
, inner
);
4236 /* Exit the current scope INNER back to scope OUTER. */
4239 pop_inner_scope (tree outer
, tree inner
)
4242 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
4245 while (outer
!= inner
)
4247 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
4249 cp_binding_level
*save_template_parm
= 0;
4250 /* Temporary take out template parameter scopes. They are saved
4251 in reversed order in save_template_parm. */
4252 while (current_binding_level
->kind
== sk_template_parms
)
4254 cp_binding_level
*b
= current_binding_level
;
4255 current_binding_level
= b
->level_chain
;
4256 b
->level_chain
= save_template_parm
;
4257 save_template_parm
= b
;
4262 /* Restore template parameter scopes. */
4263 while (save_template_parm
)
4265 cp_binding_level
*b
= save_template_parm
;
4266 save_template_parm
= b
->level_chain
;
4267 b
->level_chain
= current_binding_level
;
4268 current_binding_level
= b
;
4274 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
4278 /* Do a pushlevel for class declarations. */
4281 pushlevel_class (void)
4283 class_binding_level
= begin_scope (sk_class
, current_class_type
);
4286 /* ...and a poplevel for class declarations. */
4289 poplevel_class (void)
4291 cp_binding_level
*level
= class_binding_level
;
4292 cp_class_binding
*cb
;
4296 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4297 gcc_assert (level
!= 0);
4299 /* If we're leaving a toplevel class, cache its binding level. */
4300 if (current_class_depth
== 1)
4301 previous_class_level
= level
;
4302 for (shadowed
= level
->type_shadowed
;
4304 shadowed
= TREE_CHAIN (shadowed
))
4305 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
4307 /* Remove the bindings for all of the class-level declarations. */
4308 if (level
->class_shadowed
)
4310 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
4312 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
4313 cxx_binding_free (cb
->base
);
4315 ggc_free (level
->class_shadowed
);
4316 level
->class_shadowed
= NULL
;
4319 /* Now, pop out of the binding level which we created up in the
4320 `pushlevel_class' routine. */
4321 gcc_assert (current_binding_level
== level
);
4323 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4326 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
4327 appropriate. DECL is the value to which a name has just been
4328 bound. CLASS_TYPE is the class in which the lookup occurred. */
4331 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
4334 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
4338 if (TREE_CODE (decl
) == OVERLOAD
)
4339 context
= ovl_scope (decl
);
4342 gcc_assert (DECL_P (decl
));
4343 context
= context_for_name_lookup (decl
);
4346 if (is_properly_derived_from (class_type
, context
))
4347 INHERITED_VALUE_BINDING_P (binding
) = 1;
4349 INHERITED_VALUE_BINDING_P (binding
) = 0;
4351 else if (binding
->value
== decl
)
4352 /* We only encounter a TREE_LIST when there is an ambiguity in the
4353 base classes. Such an ambiguity can be overridden by a
4354 definition in this class. */
4355 INHERITED_VALUE_BINDING_P (binding
) = 1;
4357 INHERITED_VALUE_BINDING_P (binding
) = 0;
4360 /* Make the declaration of X appear in CLASS scope. */
4363 pushdecl_class_level (tree x
)
4365 bool is_valid
= true;
4368 /* Do nothing if we're adding to an outer lambda closure type,
4369 outer_binding will add it later if it's needed. */
4370 if (current_class_type
!= class_binding_level
->this_entity
)
4373 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4374 /* Get the name of X. */
4375 tree name
= OVL_NAME (x
);
4379 is_valid
= push_class_level_binding (name
, x
);
4380 if (TREE_CODE (x
) == TYPE_DECL
)
4381 set_identifier_type_value (name
, x
);
4383 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
4385 /* If X is an anonymous aggregate, all of its members are
4386 treated as if they were members of the class containing the
4387 aggregate, for naming purposes. */
4388 location_t save_location
= input_location
;
4389 tree anon
= TREE_TYPE (x
);
4390 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
4391 for (unsigned ix
= member_vec
->length (); ix
--;)
4393 tree binding
= (*member_vec
)[ix
];
4394 if (STAT_HACK_P (binding
))
4396 if (!pushdecl_class_level (STAT_TYPE (binding
)))
4398 binding
= STAT_DECL (binding
);
4400 if (!pushdecl_class_level (binding
))
4404 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
4405 if (TREE_CODE (f
) == FIELD_DECL
)
4407 input_location
= DECL_SOURCE_LOCATION (f
);
4408 if (!pushdecl_class_level (f
))
4411 input_location
= save_location
;
4413 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4417 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
4418 scope. If the value returned is non-NULL, and the PREVIOUS field
4419 is not set, callers must set the PREVIOUS field explicitly. */
4421 static cxx_binding
*
4422 get_class_binding (tree name
, cp_binding_level
*scope
)
4427 cxx_binding
*binding
;
4429 class_type
= scope
->this_entity
;
4431 /* Get the type binding. */
4432 type_binding
= lookup_member (class_type
, name
,
4433 /*protect=*/2, /*want_type=*/true,
4434 tf_warning_or_error
);
4435 /* Get the value binding. */
4436 value_binding
= lookup_member (class_type
, name
,
4437 /*protect=*/2, /*want_type=*/false,
4438 tf_warning_or_error
);
4441 && (TREE_CODE (value_binding
) == TYPE_DECL
4442 || DECL_CLASS_TEMPLATE_P (value_binding
)
4443 || (TREE_CODE (value_binding
) == TREE_LIST
4444 && TREE_TYPE (value_binding
) == error_mark_node
4445 && (TREE_CODE (TREE_VALUE (value_binding
))
4447 /* We found a type binding, even when looking for a non-type
4448 binding. This means that we already processed this binding
4451 else if (value_binding
)
4453 if (TREE_CODE (value_binding
) == TREE_LIST
4454 && TREE_TYPE (value_binding
) == error_mark_node
)
4455 /* NAME is ambiguous. */
4457 else if (BASELINK_P (value_binding
))
4458 /* NAME is some overloaded functions. */
4459 value_binding
= BASELINK_FUNCTIONS (value_binding
);
4462 /* If we found either a type binding or a value binding, create a
4463 new binding object. */
4464 if (type_binding
|| value_binding
)
4466 binding
= new_class_binding (name
,
4470 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
4478 /* Make the declaration(s) of X appear in CLASS scope under the name
4479 NAME. Returns true if the binding is valid. */
4482 push_class_level_binding_1 (tree name
, tree x
)
4484 cxx_binding
*binding
;
4488 /* The class_binding_level will be NULL if x is a template
4489 parameter name in a member template. */
4490 if (!class_binding_level
)
4493 if (name
== error_mark_node
)
4496 /* Can happen for an erroneous declaration (c++/60384). */
4497 if (!identifier_p (name
))
4499 gcc_assert (errorcount
|| sorrycount
);
4503 /* Check for invalid member names. But don't worry about a default
4504 argument-scope lambda being pushed after the class is complete. */
4505 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
4506 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
4507 /* Check that we're pushing into the right binding level. */
4508 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
4510 /* We could have been passed a tree list if this is an ambiguous
4511 declaration. If so, pull the declaration out because
4512 check_template_shadow will not handle a TREE_LIST. */
4513 if (TREE_CODE (decl
) == TREE_LIST
4514 && TREE_TYPE (decl
) == error_mark_node
)
4515 decl
= TREE_VALUE (decl
);
4517 if (!check_template_shadow (decl
))
4522 If T is the name of a class, then each of the following shall
4523 have a name different from T:
4525 -- every static data member of class T;
4527 -- every member of class T that is itself a type;
4529 -- every enumerator of every member of class T that is an
4532 -- every member of every anonymous union that is a member of
4535 (Non-static data members were also forbidden to have the same
4536 name as T until TC1.) */
4538 || TREE_CODE (x
) == CONST_DECL
4539 || (TREE_CODE (x
) == TYPE_DECL
4540 && !DECL_SELF_REFERENCE_P (x
))
4541 /* A data member of an anonymous union. */
4542 || (TREE_CODE (x
) == FIELD_DECL
4543 && DECL_CONTEXT (x
) != current_class_type
))
4544 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
4546 tree scope
= context_for_name_lookup (x
);
4547 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
4549 error_at (DECL_SOURCE_LOCATION (x
),
4550 "%qD has the same name as the class in which it is "
4556 /* Get the current binding for NAME in this class, if any. */
4557 binding
= IDENTIFIER_BINDING (name
);
4558 if (!binding
|| binding
->scope
!= class_binding_level
)
4560 binding
= get_class_binding (name
, class_binding_level
);
4561 /* If a new binding was created, put it at the front of the
4562 IDENTIFIER_BINDING list. */
4565 binding
->previous
= IDENTIFIER_BINDING (name
);
4566 IDENTIFIER_BINDING (name
) = binding
;
4570 /* If there is already a binding, then we may need to update the
4572 if (binding
&& binding
->value
)
4574 tree bval
= binding
->value
;
4575 tree old_decl
= NULL_TREE
;
4576 tree target_decl
= strip_using_decl (decl
);
4577 tree target_bval
= strip_using_decl (bval
);
4579 if (INHERITED_VALUE_BINDING_P (binding
))
4581 /* If the old binding was from a base class, and was for a
4582 tag name, slide it over to make room for the new binding.
4583 The old binding is still visible if explicitly qualified
4584 with a class-key. */
4585 if (TREE_CODE (target_bval
) == TYPE_DECL
4586 && DECL_ARTIFICIAL (target_bval
)
4587 && !(TREE_CODE (target_decl
) == TYPE_DECL
4588 && DECL_ARTIFICIAL (target_decl
)))
4590 old_decl
= binding
->type
;
4591 binding
->type
= bval
;
4592 binding
->value
= NULL_TREE
;
4593 INHERITED_VALUE_BINDING_P (binding
) = 0;
4598 /* Any inherited type declaration is hidden by the type
4599 declaration in the derived class. */
4600 if (TREE_CODE (target_decl
) == TYPE_DECL
4601 && DECL_ARTIFICIAL (target_decl
))
4602 binding
->type
= NULL_TREE
;
4605 else if (TREE_CODE (decl
) == USING_DECL
4606 && TREE_CODE (bval
) == USING_DECL
4607 && same_type_p (USING_DECL_SCOPE (decl
),
4608 USING_DECL_SCOPE (bval
)))
4609 /* This is a using redeclaration that will be diagnosed later
4610 in supplement_binding */
4612 else if (TREE_CODE (decl
) == USING_DECL
4613 && TREE_CODE (bval
) == USING_DECL
4614 && DECL_DEPENDENT_P (decl
)
4615 && DECL_DEPENDENT_P (bval
))
4617 else if (TREE_CODE (decl
) == USING_DECL
4618 && OVL_P (target_bval
))
4620 else if (TREE_CODE (bval
) == USING_DECL
4621 && OVL_P (target_decl
))
4623 else if (OVL_P (target_decl
)
4624 && OVL_P (target_bval
))
4627 if (old_decl
&& binding
->scope
== class_binding_level
)
4630 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4631 here. This function is only used to register bindings
4632 from with the class definition itself. */
4633 INHERITED_VALUE_BINDING_P (binding
) = 0;
4638 /* Note that we declared this value so that we can issue an error if
4639 this is an invalid redeclaration of a name already used for some
4641 note_name_declared_in_class (name
, decl
);
4643 /* If we didn't replace an existing binding, put the binding on the
4644 stack of bindings for the identifier, and update the shadowed
4646 if (binding
&& binding
->scope
== class_binding_level
)
4647 /* Supplement the existing binding. */
4648 ok
= supplement_binding (binding
, decl
);
4651 /* Create a new binding. */
4652 push_binding (name
, decl
, class_binding_level
);
4659 /* Wrapper for push_class_level_binding_1. */
4662 push_class_level_binding (tree name
, tree x
)
4665 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4666 ret
= push_class_level_binding_1 (name
, x
);
4667 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4671 /* Process and lookup a using decl SCOPE::lookup.name, filling in
4672 lookup.values & lookup.type. Return true if ok. */
4675 lookup_using_decl (tree scope
, name_lookup
&lookup
)
4677 tree current
= current_scope ();
4678 bool dependent_p
= false;
4680 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
4682 /* Naming a namespace member. */
4683 if (TYPE_P (current
))
4685 error ("using-declaration for non-member at class scope");
4689 qualified_namespace_lookup (scope
, &lookup
);
4691 else if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
4693 error ("using-declaration may not name enumerator %<%E::%D%>",
4694 scope
, lookup
.name
);
4699 /* Naming a class member. */
4700 if (!TYPE_P (current
))
4702 error ("using-declaration for member at non-class scope");
4706 /* Make sure the name is not invalid */
4707 if (TREE_CODE (lookup
.name
) == BIT_NOT_EXPR
)
4709 error ("%<%T::%D%> names destructor", scope
, lookup
.name
);
4713 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4714 if (MAYBE_CLASS_TYPE_P (scope
)
4715 && (lookup
.name
== TYPE_IDENTIFIER (scope
)
4716 || constructor_name_p (lookup
.name
, scope
)))
4718 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4719 lookup
.name
= ctor_identifier
;
4720 CLASSTYPE_NON_AGGREGATE (current
) = true;
4723 /* Cannot introduce a constructor name. */
4724 if (constructor_name_p (lookup
.name
, current
))
4726 error ("%<%T::%D%> names constructor in %qT",
4727 scope
, lookup
.name
, current
);
4731 /* Member using decls finish processing when completing the
4733 /* From [namespace.udecl]:
4735 A using-declaration used as a member-declaration shall refer
4736 to a member of a base class of the class being defined.
4738 In general, we cannot check this constraint in a template
4739 because we do not know the entire set of base classes of the
4740 current class type. Morover, if SCOPE is dependent, it might
4741 match a non-dependent base. */
4743 dependent_p
= dependent_scope_p (scope
);
4747 tree binfo
= lookup_base (current
, scope
, ba_any
, &b_kind
,
4748 tf_warning_or_error
);
4749 if (b_kind
< bk_proper_base
)
4751 /* If there are dependent bases, scope might resolve at
4752 instantiation time, even if it isn't exactly one of
4753 the dependent bases. */
4754 if (b_kind
== bk_same_type
|| !any_dependent_bases_p ())
4756 error_not_base_type (scope
, current
);
4759 /* Treat as-if dependent. */
4762 else if (lookup
.name
== ctor_identifier
&& !binfo_direct_p (binfo
))
4764 error ("cannot inherit constructors from indirect base %qT",
4768 else if (IDENTIFIER_CONV_OP_P (lookup
.name
)
4769 && dependent_type_p (TREE_TYPE (lookup
.name
)))
4772 lookup
.value
= lookup_member (binfo
, lookup
.name
, 0,
4773 false, tf_warning_or_error
);
4781 error ("%qD has not been declared in %qE", lookup
.name
, scope
);
4785 if (TREE_CODE (lookup
.value
) == TREE_LIST
4786 /* We can (independently) have ambiguous implicit typedefs. */
4787 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
4789 error ("reference to %qD is ambiguous", lookup
.name
);
4790 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
4791 ? lookup
.value
: lookup
.type
);
4795 if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
4797 error ("using-declaration may not name namespace %qD", lookup
.value
);
4805 /* Process "using SCOPE::NAME" in a class scope. Return the
4806 USING_DECL created. */
4809 do_class_using_decl (tree scope
, tree name
)
4811 if (name
== error_mark_node
4812 || scope
== error_mark_node
)
4815 name_lookup
lookup (name
);
4816 if (!lookup_using_decl (scope
, lookup
))
4819 tree found
= lookup
.value
;
4820 if (found
&& BASELINK_P (found
))
4821 /* The binfo from which the functions came does not matter. */
4822 found
= BASELINK_FUNCTIONS (found
);
4824 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
4825 USING_DECL_SCOPE (using_decl
) = scope
;
4826 USING_DECL_DECLS (using_decl
) = found
;
4827 DECL_DEPENDENT_P (using_decl
) = !found
;
4833 /* Return the binding for NAME in NS. If NS is NULL, look in
4834 global_namespace. */
4837 get_namespace_binding (tree ns
, tree name
)
4839 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4841 ns
= global_namespace
;
4842 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4843 tree ret
= find_namespace_value (ns
, name
);
4844 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4848 /* Push internal DECL into the global namespace. Does not do the
4849 full overload fn handling and does not add it to the list of things
4850 in the namespace. */
4853 set_global_binding (tree decl
)
4855 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4857 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
4860 /* The user's placed something in the implementor's namespace. */
4861 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
4863 /* Force the binding, so compiler internals continue to work. */
4866 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4869 /* Set the context of a declaration to scope. Complain if we are not
4873 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4875 /* Get rid of namespace aliases. */
4876 scope
= ORIGINAL_NAMESPACE (scope
);
4878 /* It is ok for friends to be qualified in parallel space. */
4879 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4880 error ("declaration of %qD not in a namespace surrounding %qD",
4882 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4884 /* See whether this has been declared in the namespace or inline
4886 tree old
= NULL_TREE
;
4888 name_lookup
lookup (DECL_NAME (decl
),
4889 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
4890 if (!lookup
.search_qualified (scope
, /*usings=*/false))
4891 /* No old declaration at all. */
4896 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4897 if (TREE_CODE (old
) == TREE_LIST
)
4900 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4901 error ("reference to %qD is ambiguous", decl
);
4902 print_candidates (old
);
4906 if (!DECL_DECLARES_FUNCTION_P (decl
))
4908 /* Don't compare non-function decls with decls_match here, since
4909 it can't check for the correct constness at this
4910 point. pushdecl will find those errors later. */
4912 /* We might have found it in an inline namespace child of SCOPE. */
4913 if (TREE_CODE (decl
) == TREE_CODE (old
))
4914 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4917 /* Writing "N::i" to declare something directly in "N" is invalid. */
4918 if (CP_DECL_CONTEXT (decl
) == current_namespace
4919 && at_namespace_scope_p ())
4920 error_at (DECL_SOURCE_LOCATION (decl
),
4921 "explicit qualification in declaration of %qD", decl
);
4925 /* Since decl is a function, old should contain a function decl. */
4929 /* It didn't work, go back to the explicit scope. */
4930 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4931 error ("%qD should have been declared inside %qD", decl
, scope
);
4936 /* We handle these in check_explicit_instantiation_namespace. */
4937 if (processing_explicit_instantiation
)
4939 if (processing_template_decl
|| processing_specialization
)
4940 /* We have not yet called push_template_decl to turn a
4941 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4942 match. But, we'll check later, when we construct the
4946 /* Instantiations or specializations of templates may be declared as
4947 friends in any namespace. */
4948 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4951 tree found
= NULL_TREE
;
4952 bool hidden_p
= false;
4954 for (lkp_iterator
iter (old
); iter
; ++iter
)
4956 if (iter
.using_p ())
4961 /* Adjust DECL_CONTEXT first so decls_match will return true
4962 if DECL will match a declaration in an inline namespace. */
4963 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4964 if (decls_match (decl
, ofn
))
4968 /* We found more than one matching declaration. This
4969 can happen if we have two inline namespace children,
4970 each containing a suitable declaration. */
4971 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4975 hidden_p
= iter
.hidden_p ();
4983 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4984 "%qD has not been declared within %qD", decl
, scope
);
4985 inform (DECL_SOURCE_LOCATION (found
),
4986 "only here as a %<friend%>");
4988 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4995 /* Return the namespace where the current declaration is declared. */
4998 current_decl_namespace (void)
5001 /* If we have been pushed into a different namespace, use it. */
5002 if (!vec_safe_is_empty (decl_namespace_list
))
5003 return decl_namespace_list
->last ();
5005 if (current_class_type
)
5006 result
= decl_namespace_context (current_class_type
);
5007 else if (current_function_decl
)
5008 result
= decl_namespace_context (current_function_decl
);
5010 result
= current_namespace
;
5014 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5015 attribute visibility is seen. */
5018 handle_namespace_attrs (tree ns
, tree attributes
)
5021 bool saw_vis
= false;
5023 if (attributes
== error_mark_node
)
5026 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
5028 tree name
= get_attribute_name (d
);
5029 tree args
= TREE_VALUE (d
);
5031 if (is_attribute_p ("visibility", name
))
5033 /* attribute visibility is a property of the syntactic block
5034 rather than the namespace as a whole, so we don't touch the
5035 NAMESPACE_DECL at all. */
5036 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
5037 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
5039 warning (OPT_Wattributes
,
5040 "%qD attribute requires a single NTBS argument",
5045 if (!TREE_PUBLIC (ns
))
5046 warning (OPT_Wattributes
,
5047 "%qD attribute is meaningless since members of the "
5048 "anonymous namespace get local symbols", name
);
5050 push_visibility (TREE_STRING_POINTER (x
), 1);
5053 else if (is_attribute_p ("abi_tag", name
))
5055 if (!DECL_NAME (ns
))
5057 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5061 if (!DECL_NAMESPACE_INLINE_P (ns
))
5063 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5069 tree dn
= DECL_NAME (ns
);
5070 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5071 IDENTIFIER_POINTER (dn
));
5072 TREE_TYPE (args
) = char_array_type_node
;
5073 args
= fix_string_type (args
);
5074 args
= build_tree_list (NULL_TREE
, args
);
5076 if (check_abi_tag_args (args
, name
))
5077 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5078 DECL_ATTRIBUTES (ns
));
5080 else if (is_attribute_p ("deprecated", name
))
5082 if (!DECL_NAME (ns
))
5084 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5088 if (args
&& TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
5090 error ("deprecated message is not a string");
5093 TREE_DEPRECATED (ns
) = 1;
5095 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
5096 DECL_ATTRIBUTES (ns
));
5100 warning (OPT_Wattributes
, "%qD attribute directive ignored",
5109 /* Temporarily set the namespace for the current declaration. */
5112 push_decl_namespace (tree decl
)
5114 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
5115 decl
= decl_namespace_context (decl
);
5116 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
5119 /* [namespace.memdef]/2 */
5122 pop_decl_namespace (void)
5124 decl_namespace_list
->pop ();
5127 /* Process a namespace-alias declaration. */
5130 do_namespace_alias (tree alias
, tree name_space
)
5132 if (name_space
== error_mark_node
)
5135 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
5137 name_space
= ORIGINAL_NAMESPACE (name_space
);
5139 /* Build the alias. */
5140 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
5141 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
5142 DECL_EXTERNAL (alias
) = 1;
5143 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
5146 /* Emit debug info for namespace alias. */
5147 if (!building_stmt_list_p ())
5148 (*debug_hooks
->early_global_decl
) (alias
);
5151 /* Like pushdecl, only it places X in the current namespace,
5155 pushdecl_namespace_level (tree x
, bool hiding
)
5157 cp_binding_level
*b
= current_binding_level
;
5160 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5161 t
= do_pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
), hiding
);
5163 /* Now, the type_shadowed stack may screw us. Munge it so it does
5165 if (TREE_CODE (t
) == TYPE_DECL
)
5167 tree name
= DECL_NAME (t
);
5169 tree
*ptr
= (tree
*)0;
5170 for (; !global_scope_p (b
); b
= b
->level_chain
)
5172 tree shadowed
= b
->type_shadowed
;
5173 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
5174 if (TREE_PURPOSE (shadowed
) == name
)
5176 ptr
= &TREE_VALUE (shadowed
);
5177 /* Can't break out of the loop here because sometimes
5178 a binding level will have duplicate bindings for
5179 PT names. It's gross, but I haven't time to fix it. */
5182 newval
= TREE_TYPE (t
);
5183 if (ptr
== (tree
*)0)
5185 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
5186 up here if this is changed to an assertion. --KR */
5187 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
5194 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5198 /* Process a using declaration in non-class scope. */
5201 finish_nonmember_using_decl (tree scope
, tree name
)
5203 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
5205 if (scope
== error_mark_node
|| name
== error_mark_node
)
5208 name_lookup
lookup (name
);
5210 if (!lookup_using_decl (scope
, lookup
))
5213 /* Emit debug info. */
5214 if (!processing_template_decl
)
5215 cp_emit_debug_info_for_using (lookup
.value
,
5216 current_binding_level
->this_entity
);
5218 if (current_binding_level
->kind
== sk_namespace
)
5220 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
5222 tree value
= MAYBE_STAT_DECL (*slot
);
5223 tree type
= MAYBE_STAT_TYPE (*slot
);
5225 do_nonmember_using_decl (lookup
, false, &value
, &type
);
5227 if (STAT_HACK_P (*slot
))
5229 STAT_DECL (*slot
) = value
;
5230 STAT_TYPE (*slot
) = type
;
5233 *slot
= stat_hack (value
, type
);
5239 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
5240 USING_DECL_SCOPE (using_decl
) = scope
;
5241 add_decl_expr (using_decl
);
5243 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
5248 value
= binding
->value
;
5249 type
= binding
->type
;
5252 /* DR 36 questions why using-decls at function scope may not be
5253 duplicates. Disallow it, as C++11 claimed and PR 20420
5255 do_nonmember_using_decl (lookup
, true, &value
, &type
);
5259 else if (binding
&& value
== binding
->value
)
5261 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
5263 update_local_overload (IDENTIFIER_BINDING (name
), value
);
5264 IDENTIFIER_BINDING (name
)->value
= value
;
5267 /* Install the new binding. */
5268 push_local_binding (name
, value
, true);
5272 else if (binding
&& type
== binding
->type
)
5276 push_local_binding (name
, type
, true);
5277 set_identifier_type_value (name
, type
);
5282 /* Return the declarations that are members of the namespace NS. */
5285 cp_namespace_decls (tree ns
)
5287 return NAMESPACE_LEVEL (ns
)->names
;
5290 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5291 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
5294 qualify_lookup (tree val
, LOOK_want want
)
5296 if (val
== NULL_TREE
)
5299 if (bool (want
& LOOK_want::TYPE
))
5301 tree target_val
= strip_using_decl (val
);
5303 if (TREE_CODE (STRIP_TEMPLATE (target_val
)) == TYPE_DECL
)
5307 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
5308 return TREE_CODE (val
) == NAMESPACE_DECL
;
5313 /* Is there a "using namespace std;" directive within USINGS? */
5316 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
5321 for (unsigned ix
= usings
->length (); ix
--;)
5322 if ((*usings
)[ix
] == std_node
)
5328 /* Is there a "using namespace std;" directive within the current
5329 namespace (or its ancestors)?
5330 Compare with name_lookup::search_unqualified. */
5333 has_using_namespace_std_directive_p ()
5335 for (cp_binding_level
*level
= current_binding_level
;
5337 level
= level
->level_chain
)
5338 if (using_directives_contain_std_p (level
->using_directives
))
5344 /* Subclass of deferred_diagnostic, for issuing a note when
5345 --param cxx-max-namespaces-for-diagnostic-help is reached.
5347 The note should be issued after the error, but before any other
5348 deferred diagnostics. This is handled by decorating a wrapped
5349 deferred_diagnostic, and emitting a note before that wrapped note is
5352 class namespace_limit_reached
: public deferred_diagnostic
5355 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
5356 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
5357 : deferred_diagnostic (loc
),
5358 m_limit (limit
), m_name (name
),
5359 m_wrapped (move (wrapped
))
5363 ~namespace_limit_reached ()
5365 /* Unconditionally warn that the search was truncated. */
5366 inform (get_location (),
5367 "maximum limit of %d namespaces searched for %qE",
5369 /* m_wrapped will be implicitly deleted after this, emitting any followup
5370 diagnostic after the above note. */
5376 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
5379 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
5380 Emit a note showing the location of the declaration of the suggestion. */
5382 class show_candidate_location
: public deferred_diagnostic
5385 show_candidate_location (location_t loc
, tree candidate
)
5386 : deferred_diagnostic (loc
),
5387 m_candidate (candidate
)
5391 ~show_candidate_location ()
5393 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
5400 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
5401 to be suggested by suggest_alternatives_for.
5403 Emit a series of notes showing the various suggestions. */
5405 class suggest_alternatives
: public deferred_diagnostic
5408 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
5409 : deferred_diagnostic (loc
),
5410 m_candidates (candidates
)
5414 ~suggest_alternatives ()
5416 if (m_candidates
.length ())
5418 inform_n (get_location (), m_candidates
.length (),
5419 "suggested alternative:",
5420 "suggested alternatives:");
5421 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
5423 tree val
= m_candidates
[ix
];
5425 inform (location_of (val
), " %qE", val
);
5428 m_candidates
.release ();
5432 vec
<tree
> m_candidates
;
5435 /* A class for encapsulating the result of a search across
5436 multiple namespaces (and scoped enums within them) for an
5437 unrecognized name seen at a given source location. */
5439 class namespace_hints
5442 namespace_hints (location_t loc
, tree name
);
5444 name_hint
convert_candidates_to_name_hint ();
5445 name_hint
maybe_decorate_with_limit (name_hint
);
5448 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
5452 vec
<tree
> m_candidates
;
5454 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
5457 /* Was the limit reached? */
5461 /* Constructor for namespace_hints. Search namespaces and scoped enums,
5462 looking for an exact match for unrecognized NAME seen at LOC. */
5464 namespace_hints::namespace_hints (location_t loc
, tree name
)
5465 : m_loc(loc
), m_name (name
)
5467 auto_vec
<tree
> worklist
;
5469 m_candidates
= vNULL
;
5471 m_limit
= param_cxx_max_namespaces_for_diagnostic_help
;
5473 /* Breadth-first search of namespaces. Up to limit namespaces
5474 searched (limit zero == unlimited). */
5475 worklist
.safe_push (global_namespace
);
5476 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
5478 tree ns
= worklist
[ix
];
5479 name_lookup
lookup (name
);
5481 if (lookup
.search_qualified (ns
, false))
5482 m_candidates
.safe_push (lookup
.value
);
5486 /* Look for child namespaces. We have to do this
5487 indirectly because they are chained in reverse order,
5488 which is confusing to the user. */
5489 auto_vec
<tree
> children
;
5491 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
5492 decl
; decl
= TREE_CHAIN (decl
))
5494 if (TREE_CODE (decl
) == NAMESPACE_DECL
5495 && !DECL_NAMESPACE_ALIAS (decl
)
5496 && !DECL_NAMESPACE_INLINE_P (decl
))
5497 children
.safe_push (decl
);
5499 /* Look for exact matches for NAME within scoped enums.
5500 These aren't added to the worklist, and so don't count
5501 against the search limit. */
5502 if (TREE_CODE (decl
) == TYPE_DECL
)
5504 tree type
= TREE_TYPE (decl
);
5505 if (SCOPED_ENUM_P (type
))
5506 maybe_add_candidate_for_scoped_enum (type
, name
);
5510 while (!m_limited
&& !children
.is_empty ())
5512 if (worklist
.length () == m_limit
)
5515 worklist
.safe_push (children
.pop ());
5521 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
5522 for m_name, an IDENTIFIER_NODE for which name lookup failed.
5524 If m_candidates is non-empty, use it to generate a suggestion and/or
5525 a deferred diagnostic that lists the possible candidate(s).
5529 namespace_hints::convert_candidates_to_name_hint ()
5531 /* How many candidates do we have? */
5533 /* If we have just one candidate, issue a name_hint with it as a suggestion
5534 (so that consumers are able to suggest it within the error message and emit
5535 it as a fix-it hint), and with a note showing the candidate's location. */
5536 if (m_candidates
.length () == 1)
5538 tree candidate
= m_candidates
[0];
5539 /* Clean up CANDIDATES. */
5540 m_candidates
.release ();
5541 return name_hint (expr_to_string (candidate
),
5542 new show_candidate_location (m_loc
, candidate
));
5544 else if (m_candidates
.length () > 1)
5545 /* If we have more than one candidate, issue a name_hint without a single
5546 "suggestion", but with a deferred diagnostic that lists the
5547 various candidates. This takes ownership of m_candidates. */
5548 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
5550 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
5551 gcc_assert (m_candidates
.length () == 0);
5552 gcc_assert (m_candidates
== vNULL
);
5554 return name_hint ();
5557 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
5558 then we want to emit a note about after the error, but before
5559 any other deferred diagnostics.
5561 Handle this by figuring out what hint is needed, then optionally
5562 decorating HINT with a namespace_limit_reached wrapper. */
5565 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
5568 return name_hint (hint
.suggestion (),
5569 new namespace_limit_reached (m_loc
, m_limit
,
5571 hint
.take_deferred ()));
5576 /* Look inside SCOPED_ENUM for exact matches for NAME.
5577 If one is found, add its CONST_DECL to m_candidates. */
5580 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
5583 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
5585 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
5587 tree id
= TREE_PURPOSE (iter
);
5590 m_candidates
.safe_push (TREE_VALUE (iter
));
5596 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5599 Search through all available namespaces and any scoped enums within them
5600 and generate a suggestion and/or a deferred diagnostic that lists possible
5603 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
5604 look for near-matches and suggest the best near-match, if there is one.
5606 If nothing is found, then an empty name_hint is returned. */
5609 suggest_alternatives_for (location_t location
, tree name
,
5610 bool suggest_misspellings
)
5612 /* First, search for exact matches in other namespaces. */
5613 namespace_hints
ns_hints (location
, name
);
5614 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5616 /* Otherwise, try other approaches. */
5618 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
5620 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5623 /* The second half of suggest_alternatives_for, for when no exact matches
5624 were found in other namespaces. */
5627 suggest_alternatives_for_1 (location_t location
, tree name
,
5628 bool suggest_misspellings
)
5630 /* No candidates were found in the available namespaces. */
5632 /* If there's a "using namespace std;" active, and this
5633 is one of the most common "std::" names, then it's probably a
5634 missing #include. */
5635 if (has_using_namespace_std_directive_p ())
5637 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
5642 /* Otherwise, consider misspellings. */
5643 if (!suggest_misspellings
)
5644 return name_hint ();
5646 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
5649 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
5652 Search through all available namespaces and generate a suggestion and/or
5653 a deferred diagnostic that lists possible candidate(s).
5655 This is similiar to suggest_alternatives_for, but doesn't fallback to
5656 the other approaches used by that function. */
5659 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
5661 namespace_hints
ns_hints (location
, name
);
5663 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
5665 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
5668 /* A well-known name within the C++ standard library, returned by
5669 get_std_name_hint. */
5671 struct std_name_hint
5673 /* A name within "std::". */
5676 /* The header name defining it within the C++ Standard Library
5677 (with '<' and '>'). */
5680 /* The dialect of C++ in which this was added. */
5681 enum cxx_dialect min_dialect
;
5684 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
5685 for some of the most common names within "std::".
5686 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
5688 static const std_name_hint
*
5689 get_std_name_hint (const char *name
)
5691 static const std_name_hint hints
[] = {
5693 {"any", "<any>", cxx17
},
5694 {"any_cast", "<any>", cxx17
},
5695 {"make_any", "<any>", cxx17
},
5697 {"array", "<array>", cxx11
},
5698 {"to_array", "<array>", cxx20
},
5700 {"atomic", "<atomic>", cxx11
},
5701 {"atomic_flag", "<atomic>", cxx11
},
5702 {"atomic_ref", "<atomic>", cxx20
},
5704 {"bitset", "<bitset>", cxx11
},
5706 {"weak_equality", "<compare>", cxx20
},
5707 {"strong_equality", "<compare>", cxx20
},
5708 {"partial_ordering", "<compare>", cxx20
},
5709 {"weak_ordering", "<compare>", cxx20
},
5710 {"strong_ordering", "<compare>", cxx20
},
5712 {"complex", "<complex>", cxx98
},
5713 {"complex_literals", "<complex>", cxx14
},
5714 /* <condition_variable>. */
5715 {"condition_variable", "<condition_variable>", cxx11
},
5716 {"condition_variable_any", "<condition_variable>", cxx11
},
5718 {"byte", "<cstddef>", cxx17
},
5720 {"deque", "<deque>", cxx98
},
5721 /* <forward_list>. */
5722 {"forward_list", "<forward_list>", cxx11
},
5724 {"basic_filebuf", "<fstream>", cxx98
},
5725 {"basic_ifstream", "<fstream>", cxx98
},
5726 {"basic_ofstream", "<fstream>", cxx98
},
5727 {"basic_fstream", "<fstream>", cxx98
},
5728 {"fstream", "<fstream>", cxx98
},
5729 {"ifstream", "<fstream>", cxx98
},
5730 {"ofstream", "<fstream>", cxx98
},
5732 {"bind", "<functional>", cxx11
},
5733 {"bind_front", "<functional>", cxx20
},
5734 {"function", "<functional>", cxx11
},
5735 {"hash", "<functional>", cxx11
},
5736 {"invoke", "<functional>", cxx17
},
5737 {"mem_fn", "<functional>", cxx11
},
5738 {"not_fn", "<functional>", cxx17
},
5739 {"reference_wrapper", "<functional>", cxx11
},
5740 {"unwrap_reference", "<functional>", cxx20
},
5741 {"unwrap_reference_t", "<functional>", cxx20
},
5742 {"unwrap_ref_decay", "<functional>", cxx20
},
5743 {"unwrap_ref_decay_t", "<functional>", cxx20
},
5745 {"async", "<future>", cxx11
},
5746 {"future", "<future>", cxx11
},
5747 {"packaged_task", "<future>", cxx11
},
5748 {"promise", "<future>", cxx11
},
5750 {"cin", "<iostream>", cxx98
},
5751 {"cout", "<iostream>", cxx98
},
5752 {"cerr", "<iostream>", cxx98
},
5753 {"clog", "<iostream>", cxx98
},
5754 {"wcin", "<iostream>", cxx98
},
5755 {"wcout", "<iostream>", cxx98
},
5756 {"wclog", "<iostream>", cxx98
},
5758 {"istream", "<istream>", cxx98
},
5760 {"advance", "<iterator>", cxx98
},
5761 {"back_inserter", "<iterator>", cxx98
},
5762 {"begin", "<iterator>", cxx11
},
5763 {"distance", "<iterator>", cxx98
},
5764 {"end", "<iterator>", cxx11
},
5765 {"front_inserter", "<iterator>", cxx98
},
5766 {"inserter", "<iterator>", cxx98
},
5767 {"istream_iterator", "<iterator>", cxx98
},
5768 {"istreambuf_iterator", "<iterator>", cxx98
},
5769 {"iterator_traits", "<iterator>", cxx98
},
5770 {"move_iterator", "<iterator>", cxx11
},
5771 {"next", "<iterator>", cxx11
},
5772 {"ostream_iterator", "<iterator>", cxx98
},
5773 {"ostreambuf_iterator", "<iterator>", cxx98
},
5774 {"prev", "<iterator>", cxx11
},
5775 {"reverse_iterator", "<iterator>", cxx98
},
5777 {"ostream", "<ostream>", cxx98
},
5779 {"list", "<list>", cxx98
},
5781 {"map", "<map>", cxx98
},
5782 {"multimap", "<map>", cxx98
},
5784 {"allocate_shared", "<memory>", cxx11
},
5785 {"allocator", "<memory>", cxx98
},
5786 {"allocator_traits", "<memory>", cxx11
},
5787 {"make_shared", "<memory>", cxx11
},
5788 {"make_unique", "<memory>", cxx14
},
5789 {"shared_ptr", "<memory>", cxx11
},
5790 {"unique_ptr", "<memory>", cxx11
},
5791 {"weak_ptr", "<memory>", cxx11
},
5792 /* <memory_resource>. */
5793 {"pmr", "<memory_resource>", cxx17
},
5795 {"mutex", "<mutex>", cxx11
},
5796 {"timed_mutex", "<mutex>", cxx11
},
5797 {"recursive_mutex", "<mutex>", cxx11
},
5798 {"recursive_timed_mutex", "<mutex>", cxx11
},
5799 {"once_flag", "<mutex>", cxx11
},
5800 {"call_once,", "<mutex>", cxx11
},
5801 {"lock", "<mutex>", cxx11
},
5802 {"scoped_lock", "<mutex>", cxx17
},
5803 {"try_lock", "<mutex>", cxx11
},
5804 {"lock_guard", "<mutex>", cxx11
},
5805 {"unique_lock", "<mutex>", cxx11
},
5807 {"optional", "<optional>", cxx17
},
5808 {"make_optional", "<optional>", cxx17
},
5810 {"ostream", "<ostream>", cxx98
},
5811 {"wostream", "<ostream>", cxx98
},
5812 {"ends", "<ostream>", cxx98
},
5813 {"flush", "<ostream>", cxx98
},
5814 {"endl", "<ostream>", cxx98
},
5816 {"queue", "<queue>", cxx98
},
5817 {"priority_queue", "<queue>", cxx98
},
5819 {"set", "<set>", cxx98
},
5820 {"multiset", "<set>", cxx98
},
5821 /* <shared_mutex>. */
5822 {"shared_lock", "<shared_mutex>", cxx14
},
5823 {"shared_mutex", "<shared_mutex>", cxx17
},
5824 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
5825 /* <source_location>. */
5826 {"source_location", "<source_location>", cxx20
},
5828 {"basic_stringbuf", "<sstream>", cxx98
},
5829 {"basic_istringstream", "<sstream>", cxx98
},
5830 {"basic_ostringstream", "<sstream>", cxx98
},
5831 {"basic_stringstream", "<sstream>", cxx98
},
5832 {"istringstream", "<sstream>", cxx98
},
5833 {"ostringstream", "<sstream>", cxx98
},
5834 {"stringstream", "<sstream>", cxx98
},
5836 {"stack", "<stack>", cxx98
},
5838 {"basic_string", "<string>", cxx98
},
5839 {"string", "<string>", cxx98
},
5840 {"wstring", "<string>", cxx98
},
5841 {"u8string", "<string>", cxx20
},
5842 {"u16string", "<string>", cxx11
},
5843 {"u32string", "<string>", cxx11
},
5844 /* <string_view>. */
5845 {"basic_string_view", "<string_view>", cxx17
},
5846 {"string_view", "<string_view>", cxx17
},
5848 {"thread", "<thread>", cxx11
},
5849 {"this_thread", "<thread>", cxx11
},
5851 {"apply", "<tuple>", cxx17
},
5852 {"forward_as_tuple", "<tuple>", cxx11
},
5853 {"make_from_tuple", "<tuple>", cxx17
},
5854 {"make_tuple", "<tuple>", cxx11
},
5855 {"tie", "<tuple>", cxx11
},
5856 {"tuple", "<tuple>", cxx11
},
5857 {"tuple_cat", "<tuple>", cxx11
},
5858 {"tuple_element", "<tuple>", cxx11
},
5859 {"tuple_element_t", "<tuple>", cxx14
},
5860 {"tuple_size", "<tuple>", cxx11
},
5861 {"tuple_size_v", "<tuple>", cxx17
},
5862 /* <type_traits>. */
5863 {"enable_if", "<type_traits>", cxx11
},
5864 {"enable_if_t", "<type_traits>", cxx14
},
5865 {"invoke_result", "<type_traits>", cxx17
},
5866 {"invoke_result_t", "<type_traits>", cxx17
},
5867 {"remove_cvref", "<type_traits>", cxx20
},
5868 {"remove_cvref_t", "<type_traits>", cxx20
},
5869 {"type_identity", "<type_traits>", cxx20
},
5870 {"type_identity_t", "<type_traits>", cxx20
},
5871 {"void_t", "<type_traits>", cxx17
},
5872 {"conjunction", "<type_traits>", cxx17
},
5873 {"conjunction_v", "<type_traits>", cxx17
},
5874 {"disjunction", "<type_traits>", cxx17
},
5875 {"disjunction_v", "<type_traits>", cxx17
},
5876 {"negation", "<type_traits>", cxx17
},
5877 {"negation_v", "<type_traits>", cxx17
},
5878 /* <unordered_map>. */
5879 {"unordered_map", "<unordered_map>", cxx11
},
5880 {"unordered_multimap", "<unordered_map>", cxx11
},
5881 /* <unordered_set>. */
5882 {"unordered_set", "<unordered_set>", cxx11
},
5883 {"unordered_multiset", "<unordered_set>", cxx11
},
5885 {"declval", "<utility>", cxx11
},
5886 {"forward", "<utility>", cxx11
},
5887 {"make_pair", "<utility>", cxx98
},
5888 {"move", "<utility>", cxx11
},
5889 {"pair", "<utility>", cxx98
},
5891 {"variant", "<variant>", cxx17
},
5892 {"visit", "<variant>", cxx17
},
5894 {"vector", "<vector>", cxx98
},
5896 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
5897 for (size_t i
= 0; i
< num_hints
; i
++)
5899 if (strcmp (name
, hints
[i
].name
) == 0)
5905 /* Describe DIALECT. */
5908 get_cxx_dialect_name (enum cxx_dialect dialect
)
5927 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
5928 that weren't recognized, but for which we know which header it ought to be
5931 Emit a note either suggesting the header to be included, or noting that
5932 the current dialect is too early for the given name. */
5934 class missing_std_header
: public deferred_diagnostic
5937 missing_std_header (location_t loc
,
5938 const char *name_str
,
5939 const std_name_hint
*header_hint
)
5940 : deferred_diagnostic (loc
),
5941 m_name_str (name_str
),
5942 m_header_hint (header_hint
)
5944 ~missing_std_header ()
5946 gcc_rich_location
richloc (get_location ());
5947 if (cxx_dialect
>= m_header_hint
->min_dialect
)
5949 const char *header
= m_header_hint
->header
;
5950 maybe_add_include_fixit (&richloc
, header
, true);
5952 "%<std::%s%> is defined in header %qs;"
5953 " did you forget to %<#include %s%>?",
5954 m_name_str
, header
, header
);
5958 "%<std::%s%> is only available from %s onwards",
5959 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
5963 const char *m_name_str
;
5964 const std_name_hint
*m_header_hint
;
5967 /* Attempt to generate a name_hint that suggests pertinent header files
5968 for NAME at LOCATION, for common names within the "std" namespace,
5969 or an empty name_hint if this isn't applicable. */
5972 maybe_suggest_missing_std_header (location_t location
, tree name
)
5974 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5976 const char *name_str
= IDENTIFIER_POINTER (name
);
5977 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
5979 return name_hint ();
5981 return name_hint (NULL
, new missing_std_header (location
, name_str
,
5985 /* Attempt to generate a name_hint that suggests a missing header file
5986 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
5990 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
5992 if (scope
== NULL_TREE
)
5993 return name_hint ();
5994 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
5995 return name_hint ();
5996 /* We only offer suggestions for the "std" namespace. */
5997 if (scope
!= std_node
)
5998 return name_hint ();
5999 return maybe_suggest_missing_std_header (location
, name
);
6002 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
6003 lookup failed within the explicitly provided SCOPE.
6005 Suggest the best meaningful candidates (if any), otherwise
6006 an empty name_hint is returned. */
6009 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
6012 /* Something went very wrong; don't suggest anything. */
6013 if (name
== error_mark_node
)
6014 return name_hint ();
6016 /* Resolve any namespace aliases. */
6017 scope
= ORIGINAL_NAMESPACE (scope
);
6019 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
6023 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
6025 best_match
<tree
, const char *> bm (name
);
6026 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
6028 /* See if we have a good suggesion for the user. */
6029 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
6031 return name_hint (fuzzy_name
, NULL
);
6033 return name_hint ();
6036 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
6040 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
6042 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
6044 best_match
<tree
, const char *> bm (name
);
6045 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
6047 tree id
= TREE_PURPOSE (iter
);
6048 bm
.consider (IDENTIFIER_POINTER (id
));
6050 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
6053 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
6056 WANT as for lookup_name_1.
6058 Returns a DECL (or OVERLOAD, or BASELINK) representing the
6059 declaration found. If no suitable declaration can be found,
6060 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
6061 neither a class-type nor a namespace a diagnostic is issued. */
6064 lookup_qualified_name (tree scope
, tree name
, LOOK_want want
, bool complain
)
6068 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
6070 name_lookup
lookup (name
, want
);
6072 if (qualified_namespace_lookup (scope
, &lookup
))
6075 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
6076 t
= lookup_enumerator (scope
, name
);
6077 else if (is_class_type (scope
, complain
))
6078 t
= lookup_member (scope
, name
, 2, bool (want
& LOOK_want::TYPE
),
6079 tf_warning_or_error
);
6082 return error_mark_node
;
6086 /* Wrapper for the above that takes a string argument. The function name is
6087 not at the beginning of the line to keep this wrapper out of etags. */
6089 tree
lookup_qualified_name (tree t
, const char *p
, LOOK_want w
, bool c
)
6091 return lookup_qualified_name (t
, get_identifier (p
), w
, c
);
6095 Accepts the NAME to lookup and its qualifying SCOPE.
6096 Returns the name/type pair found into the cxx_binding *RESULT,
6097 or false on error. */
6100 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
6102 timevar_start (TV_NAME_LOOKUP
);
6103 query_oracle (lookup
->name
);
6104 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
6105 timevar_stop (TV_NAME_LOOKUP
);
6109 /* Helper function for lookup_name_fuzzy.
6110 Traverse binding level LVL, looking for good name matches for NAME
6113 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
6114 cp_binding_level
*lvl
, bool look_within_fields
,
6115 enum lookup_name_fuzzy_kind kind
)
6117 if (look_within_fields
)
6118 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
6120 tree type
= lvl
->this_entity
;
6121 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
6122 tree best_matching_field
6123 = lookup_member_fuzzy (type
, name
, want_type_p
);
6124 if (best_matching_field
)
6125 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
6128 /* Only suggest names reserved for the implementation if NAME begins
6129 with an underscore. */
6130 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
6132 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
6136 /* OVERLOADs or decls from using declaration are wrapped into
6138 if (TREE_CODE (d
) == TREE_LIST
)
6139 d
= OVL_FIRST (TREE_VALUE (d
));
6141 /* Don't use bindings from implicitly declared functions,
6142 as they were likely misspellings themselves. */
6143 if (TREE_TYPE (d
) == error_mark_node
)
6146 /* Skip anticipated decls of builtin functions. */
6147 if (TREE_CODE (d
) == FUNCTION_DECL
6148 && fndecl_built_in_p (d
)
6149 && DECL_ANTICIPATED (d
))
6152 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
6153 within range for). */
6154 if (TREE_CODE (d
) == VAR_DECL
6155 && DECL_ARTIFICIAL (d
))
6158 tree suggestion
= DECL_NAME (d
);
6162 /* Don't suggest names that are for anonymous aggregate types, as
6163 they are an implementation detail generated by the compiler. */
6164 if (IDENTIFIER_ANON_P (suggestion
))
6167 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
6169 /* Ignore internal names with spaces in them. */
6170 if (strchr (suggestion_str
, ' '))
6173 /* Don't suggest names that are reserved for use by the
6174 implementation, unless NAME began with an underscore. */
6175 if (name_reserved_for_implementation_p (suggestion_str
)
6176 && !consider_implementation_names
)
6179 bm
.consider (suggestion_str
);
6183 /* Subclass of deferred_diagnostic. Notify the user that the
6184 given macro was used before it was defined.
6185 This can be done in the C++ frontend since tokenization happens
6188 class macro_use_before_def
: public deferred_diagnostic
6191 /* Factory function. Return a new macro_use_before_def instance if
6192 appropriate, or return NULL. */
6193 static macro_use_before_def
*
6194 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
6196 location_t def_loc
= cpp_macro_definition_location (macro
);
6197 if (def_loc
== UNKNOWN_LOCATION
)
6200 /* We only want to issue a note if the macro was used *before* it was
6202 We don't want to issue a note for cases where a macro was incorrectly
6203 used, leaving it unexpanded (e.g. by using the wrong argument
6205 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
6208 return new macro_use_before_def (use_loc
, macro
);
6212 /* Ctor. LOC is the location of the usage. MACRO is the
6213 macro that was used. */
6214 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
6215 : deferred_diagnostic (loc
), m_macro (macro
)
6220 ~macro_use_before_def ()
6222 if (is_suppressed_p ())
6225 inform (get_location (), "the macro %qs had not yet been defined",
6226 (const char *)m_macro
->ident
.str
);
6227 inform (cpp_macro_definition_location (m_macro
),
6228 "it was later defined here");
6232 cpp_hashnode
*m_macro
;
6235 /* Determine if it can ever make sense to offer RID as a suggestion for
6238 Subroutine of lookup_name_fuzzy. */
6241 suggest_rid_p (enum rid rid
)
6245 /* Support suggesting function-like keywords. */
6246 case RID_STATIC_ASSERT
:
6250 /* Support suggesting the various decl-specifier words, to handle
6251 e.g. "singed" vs "signed" typos. */
6252 if (cp_keyword_starts_decl_specifier_p (rid
))
6255 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
6256 and "do" for short misspellings, which are likely to lead to
6257 nonsensical results. */
6262 /* Search for near-matches for NAME within the current bindings, and within
6263 macro names, returning the best match as a const char *, or NULL if
6264 no reasonable match is found.
6266 Use LOC for any deferred diagnostics. */
6269 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
6271 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
6273 /* First, try some well-known names in the C++ standard library, in case
6274 the user forgot a #include. */
6275 const char *header_hint
6276 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
6278 return name_hint (NULL
,
6279 new suggest_missing_header (loc
,
6280 IDENTIFIER_POINTER (name
),
6283 best_match
<tree
, const char *> bm (name
);
6285 cp_binding_level
*lvl
;
6286 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
6287 consider_binding_level (name
, bm
, lvl
, true, kind
);
6289 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
6290 consider_binding_level (name
, bm
, lvl
, false, kind
);
6292 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
6294 x = SOME_OTHER_MACRO (y);
6295 then "SOME_OTHER_MACRO" will survive to the frontend and show up
6296 as a misspelled identifier.
6298 Use the best distance so far so that a candidate is only set if
6299 a macro is better than anything so far. This allows early rejection
6300 (without calculating the edit distance) of macro names that must have
6301 distance >= bm.get_best_distance (), and means that we only get a
6302 non-NULL result for best_macro_match if it's better than any of
6303 the identifiers already checked. */
6304 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
6305 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
6306 /* If a macro is the closest so far to NAME, consider it. */
6308 bm
.consider ((const char *)best_macro
->ident
.str
);
6309 else if (bmm
.get_best_distance () == 0)
6311 /* If we have an exact match for a macro name, then either the
6312 macro was used with the wrong argument count, or the macro
6313 has been used before it was defined. */
6314 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
6315 if (cpp_user_macro_p (macro
))
6316 return name_hint (NULL
,
6317 macro_use_before_def::maybe_make (loc
, macro
));
6320 /* Try the "starts_decl_specifier_p" keywords to detect
6321 "singed" vs "signed" typos. */
6322 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
6324 const c_common_resword
*resword
= &c_common_reswords
[i
];
6326 if (!suggest_rid_p (resword
->rid
))
6329 tree resword_identifier
= ridpointers
[resword
->rid
];
6330 if (!resword_identifier
)
6332 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
6334 /* Only consider reserved words that survived the
6335 filtering in init_reswords (e.g. for -std). */
6336 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
6339 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
6342 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
6345 /* Subroutine of outer_binding.
6347 Returns TRUE if BINDING is a binding to a template parameter of
6348 SCOPE. In that case SCOPE is the scope of a primary template
6349 parameter -- in the sense of G++, i.e, a template that has its own
6352 Returns FALSE otherwise. */
6355 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
6356 cp_binding_level
*scope
)
6358 tree binding_value
, tmpl
, tinfo
;
6361 if (!binding
|| !scope
|| !scope
->this_entity
)
6364 binding_value
= binding
->value
? binding
->value
: binding
->type
;
6365 tinfo
= get_template_info (scope
->this_entity
);
6367 /* BINDING_VALUE must be a template parm. */
6368 if (binding_value
== NULL_TREE
6369 || (!DECL_P (binding_value
)
6370 || !DECL_TEMPLATE_PARM_P (binding_value
)))
6373 /* The level of BINDING_VALUE. */
6375 template_type_parameter_p (binding_value
)
6376 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
6377 (TREE_TYPE (binding_value
)))
6378 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
6380 /* The template of the current scope, iff said scope is a primary
6383 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
6384 ? TI_TEMPLATE (tinfo
)
6387 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
6388 then BINDING_VALUE is a parameter of TMPL. */
6389 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
6392 /* Return the innermost non-namespace binding for NAME from a scope
6393 containing BINDING, or, if BINDING is NULL, the current scope.
6394 Please note that for a given template, the template parameters are
6395 considered to be in the scope containing the current scope.
6396 If CLASS_P is false, then class bindings are ignored. */
6399 outer_binding (tree name
,
6400 cxx_binding
*binding
,
6404 cp_binding_level
*scope
;
6405 cp_binding_level
*outer_scope
;
6409 scope
= binding
->scope
->level_chain
;
6410 outer
= binding
->previous
;
6414 scope
= current_binding_level
;
6415 outer
= IDENTIFIER_BINDING (name
);
6417 outer_scope
= outer
? outer
->scope
: NULL
;
6419 /* Because we create class bindings lazily, we might be missing a
6420 class binding for NAME. If there are any class binding levels
6421 between the LAST_BINDING_LEVEL and the scope in which OUTER was
6422 declared, we must lookup NAME in those class scopes. */
6424 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
6426 if (scope
->kind
== sk_class
)
6428 cxx_binding
*class_binding
;
6430 class_binding
= get_class_binding (name
, scope
);
6433 /* Thread this new class-scope binding onto the
6434 IDENTIFIER_BINDING list so that future lookups
6436 class_binding
->previous
= outer
;
6438 binding
->previous
= class_binding
;
6440 IDENTIFIER_BINDING (name
) = class_binding
;
6441 return class_binding
;
6444 /* If we are in a member template, the template parms of the member
6445 template are considered to be inside the scope of the containing
6446 class, but within G++ the class bindings are all pushed between the
6447 template parms and the function body. So if the outer binding is
6448 a template parm for the current scope, return it now rather than
6449 look for a class binding. */
6450 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
6451 && binding_to_template_parms_of_scope_p (outer
, scope
))
6454 scope
= scope
->level_chain
;
6460 /* Return the innermost block-scope or class-scope value binding for
6461 NAME, or NULL_TREE if there is no such binding. */
6464 innermost_non_namespace_value (tree name
)
6466 cxx_binding
*binding
;
6467 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
6468 return binding
? binding
->value
: NULL_TREE
;
6471 /* Look up NAME in the current binding level and its superiors in the
6472 namespace of variables, functions and typedefs. Return a ..._DECL
6473 node of some kind representing its definition if there is only one
6474 such declaration, or return a TREE_LIST with all the overloaded
6475 definitions if there are many, or return NULL_TREE if it is undefined.
6476 Hidden name, either friend declaration or built-in function, are
6479 WHERE controls which scopes are considered. It is a bit mask of
6480 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
6481 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
6482 scopes). It is an error for no bits to be set. These scopes are
6483 searched from innermost to outermost.
6485 WANT controls what kind of entity we'd happy with.
6486 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
6487 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
6488 for only NAMESPACE_DECLS. These two can be bit-ored to find
6491 WANT can also have LOOK_want::HIDDEN_FRIEND or
6492 LOOK_want::HIDDEN_LAMBDa added to it. */
6495 lookup_name_1 (tree name
, LOOK_where where
, LOOK_want want
)
6497 tree val
= NULL_TREE
;
6499 gcc_checking_assert (unsigned (where
) != 0);
6500 /* If we're looking for hidden lambda things, we shouldn't be
6501 looking in namespace scope. */
6502 gcc_checking_assert (!bool (want
& LOOK_want::HIDDEN_LAMBDA
)
6503 || !bool (where
& LOOK_where::NAMESPACE
));
6504 query_oracle (name
);
6506 /* Conversion operators are handled specially because ordinary
6507 unqualified name lookup will not find template conversion
6509 if (IDENTIFIER_CONV_OP_P (name
))
6511 cp_binding_level
*level
;
6513 for (level
= current_binding_level
;
6514 level
&& level
->kind
!= sk_namespace
;
6515 level
= level
->level_chain
)
6520 /* A conversion operator can only be declared in a class
6522 if (level
->kind
!= sk_class
)
6525 /* Lookup the conversion operator in the class. */
6526 class_type
= level
->this_entity
;
6527 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0,
6528 tf_warning_or_error
);
6536 /* First, look in non-namespace scopes. */
6538 if (current_class_type
== NULL_TREE
)
6539 /* Maybe avoid searching the binding stack at all. */
6540 where
= LOOK_where (unsigned (where
) & ~unsigned (LOOK_where::CLASS
));
6542 if (bool (where
& (LOOK_where::BLOCK
| LOOK_where::CLASS
)))
6543 for (cxx_binding
*iter
= nullptr;
6544 (iter
= outer_binding (name
, iter
, bool (where
& LOOK_where::CLASS
)));)
6546 /* Skip entities we don't want. */
6547 if (!bool (where
& (LOCAL_BINDING_P (iter
)
6548 ? LOOK_where::BLOCK
: LOOK_where::CLASS
)))
6551 /* If this is the kind of thing we're looking for, we're done. */
6554 tree binding
= NULL_TREE
;
6556 if (!(!iter
->type
&& HIDDEN_TYPE_BINDING_P (iter
))
6557 && (bool (want
& LOOK_want::HIDDEN_LAMBDA
)
6558 || !is_lambda_ignored_entity (iter
->value
))
6559 && qualify_lookup (iter
->value
, want
))
6560 binding
= iter
->value
;
6561 else if (bool (want
& LOOK_want::TYPE
)
6562 && !HIDDEN_TYPE_BINDING_P (iter
)
6564 binding
= iter
->type
;
6568 /* The saved lookups for an operator record 'nothing
6569 found' as error_mark_node. We need to stop the search
6570 here, but not return the error mark node. */
6571 if (binding
== error_mark_node
)
6572 binding
= NULL_TREE
;
6580 /* Now lookup in namespace scopes. */
6581 if (bool (where
& LOOK_where::NAMESPACE
))
6583 name_lookup
lookup (name
, want
);
6584 if (lookup
.search_unqualified
6585 (current_decl_namespace (), current_binding_level
))
6591 /* If we have a single function from a using decl, pull it out. */
6592 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
6593 val
= OVL_FUNCTION (val
);
6598 /* Wrapper for lookup_name_1. */
6601 lookup_name (tree name
, LOOK_where where
, LOOK_want want
)
6603 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6604 tree ret
= lookup_name_1 (name
, where
, want
);
6605 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6610 lookup_name (tree name
)
6612 return lookup_name (name
, LOOK_where::ALL
, LOOK_want::NORMAL
);
6615 /* Look up NAME for type used in elaborated name specifier in
6616 the scopes given by HOW.
6618 Unlike lookup_name_1, we make sure that NAME is actually
6619 declared in the desired scope, not from inheritance, nor using
6620 directive. For using declaration, there is DR138 still waiting
6621 to be resolved. Hidden name coming from an earlier friend
6622 declaration is also returned, and will be made visible unless HOW
6623 is TAG_how::HIDDEN_FRIEND.
6625 A TYPE_DECL best matching the NAME is returned. Catching error
6626 and issuing diagnostics are caller's responsibility. */
6629 lookup_elaborated_type_1 (tree name
, TAG_how how
)
6631 cp_binding_level
*b
= current_binding_level
;
6633 if (b
->kind
!= sk_namespace
)
6634 /* Look in non-namespace scopes. */
6635 for (cxx_binding
*iter
= NULL
;
6636 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
6638 /* First check we're supposed to be looking in this scope --
6639 if we're not, we're done. */
6640 for (; b
!= iter
->scope
; b
= b
->level_chain
)
6641 if (!(b
->kind
== sk_cleanup
6642 || b
->kind
== sk_template_parms
6643 || b
->kind
== sk_function_parms
6644 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
6647 /* Check if this is the kind of thing we're looking for. If
6648 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
6649 come from base class. For ITER->VALUE, we can simply use
6650 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
6653 We check ITER->TYPE before ITER->VALUE in order to handle
6654 typedef struct C {} C;
6657 tree found
= NULL_TREE
;
6658 bool reveal
= false;
6659 if (tree type
= iter
->type
)
6661 if (qualify_lookup (type
, LOOK_want::TYPE
)
6662 && (how
!= TAG_how::CURRENT_ONLY
6663 || LOCAL_BINDING_P (iter
)
6664 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
6667 if (how
!= TAG_how::HIDDEN_FRIEND
)
6668 reveal
= HIDDEN_TYPE_BINDING_P (iter
);
6673 if (qualify_lookup (iter
->value
, LOOK_want::TYPE
)
6674 && (how
!= TAG_how::CURRENT_ONLY
6675 || !INHERITED_VALUE_BINDING_P (iter
)))
6677 found
= iter
->value
;
6678 if (how
!= TAG_how::HIDDEN_FRIEND
)
6679 reveal
= !iter
->type
&& HIDDEN_TYPE_BINDING_P (iter
);
6687 /* It is no longer a hidden binding. */
6688 HIDDEN_TYPE_BINDING_P (iter
) = false;
6690 /* Unanticipate the decl itself. */
6691 DECL_ANTICIPATED (found
) = false;
6692 DECL_FRIEND_P (found
) = false;
6694 gcc_checking_assert (TREE_CODE (found
) != TEMPLATE_DECL
);
6696 if (tree ti
= TYPE_TEMPLATE_INFO (TREE_TYPE (found
)))
6698 tree tmpl
= TI_TEMPLATE (ti
);
6699 DECL_ANTICIPATED (tmpl
) = false;
6700 DECL_FRIEND_P (tmpl
) = false;
6708 /* Now check if we can look in namespace scope. */
6709 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
6710 if (!(b
->kind
== sk_cleanup
6711 || b
->kind
== sk_template_parms
6712 || b
->kind
== sk_function_parms
6713 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
6716 /* Look in the innermost namespace. */
6717 tree ns
= b
->this_entity
;
6718 if (tree
*slot
= find_namespace_slot (ns
, name
))
6720 /* If this is the kind of thing we're looking for, we're done. */
6721 tree found
= NULL_TREE
;
6722 bool reveal
= false;
6724 if (tree type
= MAYBE_STAT_TYPE (*slot
))
6727 if (how
!= TAG_how::HIDDEN_FRIEND
)
6729 reveal
= STAT_TYPE_HIDDEN_P (*slot
);
6730 STAT_TYPE_HIDDEN_P (*slot
) = false;
6733 else if (tree decl
= MAYBE_STAT_DECL (*slot
))
6735 if (qualify_lookup (decl
, LOOK_want::TYPE
))
6739 if (how
!= TAG_how::HIDDEN_FRIEND
&& STAT_HACK_P (*slot
))
6741 reveal
= STAT_DECL_HIDDEN_P (*slot
);
6744 if (STAT_TYPE (*slot
))
6745 STAT_DECL_HIDDEN_P (*slot
) = false;
6747 /* There is no type, just remove the stat
6759 /* Reveal the previously hidden thing. */
6760 DECL_ANTICIPATED (found
) = false;
6761 DECL_FRIEND_P (found
) = false;
6763 if (TREE_CODE (found
) == TEMPLATE_DECL
)
6765 DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (found
)) = false;
6766 DECL_FRIEND_P (DECL_TEMPLATE_RESULT (found
)) = false;
6768 else if (tree ti
= TYPE_TEMPLATE_INFO (TREE_TYPE (found
)))
6770 tree tmpl
= TI_TEMPLATE (ti
);
6771 DECL_ANTICIPATED (tmpl
) = false;
6772 DECL_FRIEND_P (tmpl
) = false;
6783 /* Wrapper for lookup_type_scope_1. */
6786 lookup_elaborated_type (tree name
, TAG_how how
)
6788 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6789 tree ret
= lookup_elaborated_type_1 (name
, how
);
6790 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6794 /* Returns true iff DECL is a block-scope extern declaration of a function
6795 or variable. We will already have determined validity of the decl
6796 when pushing it. So we do not have to redo that lookup. */
6799 is_local_extern (tree decl
)
6801 if ((TREE_CODE (decl
) == FUNCTION_DECL
6802 || TREE_CODE (decl
) == VAR_DECL
))
6803 return DECL_LOCAL_DECL_P (decl
);
6808 /* The type TYPE is being declared. If it is a class template, or a
6809 specialization of a class template, do any processing required and
6810 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
6811 being declared a friend. B is the binding level at which this TYPE
6814 Returns the TYPE_DECL for TYPE, which may have been altered by this
6818 maybe_process_template_type_declaration (tree type
, int is_friend
,
6819 cp_binding_level
*b
)
6821 tree decl
= TYPE_NAME (type
);
6823 if (processing_template_parmlist
)
6824 /* You can't declare a new template type in a template parameter
6825 list. But, you can declare a non-template type:
6827 template <class A*> struct S;
6829 is a forward-declaration of `A'. */
6831 else if (b
->kind
== sk_namespace
6832 && current_binding_level
->kind
!= sk_namespace
)
6833 /* If this new type is being injected into a containing scope,
6834 then it's not a template type. */
6838 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
6839 || TREE_CODE (type
) == ENUMERAL_TYPE
);
6841 if (processing_template_decl
)
6843 /* This may change after the call to push_template_decl, but
6844 we want the original value. */
6845 tree name
= DECL_NAME (decl
);
6847 decl
= push_template_decl (decl
, is_friend
);
6848 if (decl
== error_mark_node
)
6849 return error_mark_node
;
6851 /* If the current binding level is the binding level for the
6852 template parameters (see the comment in
6853 begin_template_parm_list) and the enclosing level is a class
6854 scope, and we're not looking at a friend, push the
6855 declaration of the member class into the class scope. In the
6856 friend case, push_template_decl will already have put the
6857 friend into global scope, if appropriate. */
6858 if (TREE_CODE (type
) != ENUMERAL_TYPE
6859 && !is_friend
&& b
->kind
== sk_template_parms
6860 && b
->level_chain
->kind
== sk_class
)
6862 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
6864 if (!COMPLETE_TYPE_P (current_class_type
))
6866 maybe_add_class_template_decl_list (current_class_type
,
6867 type
, /*friend_p=*/0);
6868 /* Put this UTD in the table of UTDs for the class. */
6869 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
6870 CLASSTYPE_NESTED_UTDS (current_class_type
) =
6871 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
6873 binding_table_insert
6874 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
6883 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
6884 that the NAME is a class template, the tag is processed but not pushed.
6886 The pushed scope depend on the SCOPE parameter:
6887 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
6889 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
6890 non-template-parameter scope. This case is needed for forward
6892 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
6893 TS_GLOBAL case except that names within template-parameter scopes
6894 are not pushed at all.
6896 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
6899 do_pushtag (tree name
, tree type
, TAG_how how
)
6903 cp_binding_level
*b
= current_binding_level
;
6906 if (/* Cleanup scopes are not scopes from the point of view of
6908 b
->kind
== sk_cleanup
6909 /* Neither are function parameter scopes. */
6910 || b
->kind
== sk_function_parms
6911 /* Neither are the scopes used to hold template parameters
6912 for an explicit specialization. For an ordinary template
6913 declaration, these scopes are not scopes from the point of
6914 view of the language. */
6915 || (b
->kind
== sk_template_parms
6916 && (b
->explicit_spec_p
|| how
== TAG_how::GLOBAL
)))
6918 else if (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)
6921 if (b
->kind
== sk_template_parms
)
6928 gcc_assert (identifier_p (name
));
6930 /* Do C++ gratuitous typedefing. */
6931 if (identifier_type_value_1 (name
) != type
)
6935 tree context
= TYPE_CONTEXT (type
);
6939 cp_binding_level
*cb
= b
;
6940 while (cb
->kind
!= sk_namespace
6941 && cb
->kind
!= sk_class
6942 && (cb
->kind
!= sk_function_parms
6943 || !cb
->this_entity
))
6944 cb
= cb
->level_chain
;
6945 tree cs
= cb
->this_entity
;
6947 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
6948 ? cs
== current_function_decl
6949 : TYPE_P (cs
) ? cs
== current_class_type
6950 : cs
== current_namespace
);
6952 if (how
== TAG_how::CURRENT_ONLY
6953 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
6955 else if (cs
&& TYPE_P (cs
))
6956 /* When declaring a friend class of a local class, we want
6957 to inject the newly named class into the scope
6958 containing the local class, not the namespace
6960 context
= decl_function_context (get_type_decl (cs
));
6963 context
= current_namespace
;
6965 if (b
->kind
== sk_class
6966 || (b
->kind
== sk_template_parms
6967 && b
->level_chain
->kind
== sk_class
))
6970 tdef
= create_implicit_typedef (name
, type
);
6971 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
6972 bool is_friend
= how
== TAG_how::HIDDEN_FRIEND
;
6975 // FIXME: can go away
6976 /* This is a friend. Make this TYPE_DECL node hidden from
6977 ordinary name lookup. Its corresponding TEMPLATE_DECL
6978 will be marked in push_template_decl. */
6979 retrofit_lang_decl (tdef
);
6980 DECL_ANTICIPATED (tdef
) = 1;
6981 DECL_FRIEND_P (tdef
) = 1;
6984 decl
= maybe_process_template_type_declaration (type
, is_friend
, b
);
6985 if (decl
== error_mark_node
)
6988 if (b
->kind
== sk_class
)
6990 if (!TYPE_BEING_DEFINED (current_class_type
))
6991 /* Don't push anywhere if the class is complete; a lambda in an
6992 NSDMI is not a member of the class. */
6994 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
6995 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
6996 class. But if it's a member template class, we want
6997 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
6999 finish_member_declaration (decl
);
7001 pushdecl_class_level (decl
);
7003 else if (b
->kind
!= sk_template_parms
)
7005 decl
= do_pushdecl_with_scope
7006 (decl
, b
, /*hiding=*/(how
== TAG_how::HIDDEN_FRIEND
));
7007 if (decl
== error_mark_node
)
7010 if (DECL_CONTEXT (decl
) == std_node
7011 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
7012 && !CLASSTYPE_TEMPLATE_INFO (type
))
7014 error ("declaration of %<std::initializer_list%> does not match "
7015 "%<#include <initializer_list>%>, isn%'t a template");
7016 return error_mark_node
;
7021 set_identifier_type_value_with_scope (name
, tdef
, b
);
7023 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
7025 /* If this is a local class, keep track of it. We need this
7026 information for name-mangling, and so that it is possible to
7027 find all function definitions in a translation unit in a
7028 convenient way. (It's otherwise tricky to find a member
7029 function definition it's only pointed to from within a local
7031 if (TYPE_FUNCTION_SCOPE_P (type
))
7033 if (processing_template_decl
)
7035 /* Push a DECL_EXPR so we call pushtag at the right time in
7036 template instantiation rather than in some nested context. */
7037 add_decl_expr (decl
);
7039 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
7040 else if (!LAMBDA_TYPE_P (type
))
7041 determine_local_discriminator (TYPE_NAME (type
));
7045 if (b
->kind
== sk_class
7046 && !COMPLETE_TYPE_P (current_class_type
))
7048 maybe_add_class_template_decl_list (current_class_type
,
7049 type
, /*friend_p=*/0);
7051 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
7052 CLASSTYPE_NESTED_UTDS (current_class_type
)
7053 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
7055 binding_table_insert
7056 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
7059 decl
= TYPE_NAME (type
);
7060 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
7062 /* Set type visibility now if this is a forward declaration. */
7063 TREE_PUBLIC (decl
) = 1;
7064 determine_visibility (decl
);
7069 /* Wrapper for do_pushtag. */
7072 pushtag (tree name
, tree type
, TAG_how how
)
7074 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7075 tree ret
= do_pushtag (name
, type
, how
);
7076 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7081 /* Subroutines for reverting temporarily to top-level for instantiation
7082 of templates and such. We actually need to clear out the class- and
7083 local-value slots of all identifiers, so that only the global values
7084 are at all visible. Simply setting current_binding_level to the global
7085 scope isn't enough, because more binding levels may be pushed. */
7086 struct saved_scope
*scope_chain
;
7088 /* Return true if ID has not already been marked. */
7091 store_binding_p (tree id
)
7093 if (!id
|| !IDENTIFIER_BINDING (id
))
7096 if (IDENTIFIER_MARKED (id
))
7102 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
7103 have enough space reserved. */
7106 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7108 cxx_saved_binding saved
;
7110 gcc_checking_assert (store_binding_p (id
));
7112 IDENTIFIER_MARKED (id
) = 1;
7114 saved
.identifier
= id
;
7115 saved
.binding
= IDENTIFIER_BINDING (id
);
7116 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
7117 (*old_bindings
)->quick_push (saved
);
7118 IDENTIFIER_BINDING (id
) = NULL
;
7122 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7124 static vec
<tree
> bindings_need_stored
;
7128 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7129 for (t
= names
; t
; t
= TREE_CHAIN (t
))
7131 if (TREE_CODE (t
) == TREE_LIST
)
7132 id
= TREE_PURPOSE (t
);
7136 if (store_binding_p (id
))
7137 bindings_need_stored
.safe_push (id
);
7139 if (!bindings_need_stored
.is_empty ())
7141 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7142 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7144 /* We can apparently have duplicates in NAMES. */
7145 if (store_binding_p (id
))
7146 store_binding (id
, old_bindings
);
7148 bindings_need_stored
.truncate (0);
7150 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7153 /* Like store_bindings, but NAMES is a vector of cp_class_binding
7154 objects, rather than a TREE_LIST. */
7157 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
7158 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
7160 static vec
<tree
> bindings_need_stored
;
7162 cp_class_binding
*cb
;
7164 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
7165 if (store_binding_p (cb
->identifier
))
7166 bindings_need_stored
.safe_push (cb
->identifier
);
7167 if (!bindings_need_stored
.is_empty ())
7170 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
7171 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
7172 store_binding (id
, old_bindings
);
7173 bindings_need_stored
.truncate (0);
7177 /* A chain of saved_scope structures awaiting reuse. */
7179 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
7182 do_push_to_top_level (void)
7184 struct saved_scope
*s
;
7185 cp_binding_level
*b
;
7186 cxx_saved_binding
*sb
;
7190 /* Reuse or create a new structure for this saved scope. */
7191 if (free_saved_scope
!= NULL
)
7193 s
= free_saved_scope
;
7194 free_saved_scope
= s
->prev
;
7196 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
7197 memset (s
, 0, sizeof (*s
));
7198 /* Also reuse the structure's old_bindings vector. */
7199 vec_safe_truncate (old_bindings
, 0);
7200 s
->old_bindings
= old_bindings
;
7203 s
= ggc_cleared_alloc
<saved_scope
> ();
7205 b
= scope_chain
? current_binding_level
: 0;
7207 /* If we're in the middle of some function, save our state. */
7211 push_function_context ();
7216 if (scope_chain
&& previous_class_level
)
7217 store_class_bindings (previous_class_level
->class_shadowed
,
7220 /* Have to include the global scope, because class-scope decls
7221 aren't listed anywhere useful. */
7222 for (; b
; b
= b
->level_chain
)
7226 /* Template IDs are inserted into the global level. If they were
7227 inserted into namespace level, finish_file wouldn't find them
7228 when doing pending instantiations. Therefore, don't stop at
7229 namespace level, but continue until :: . */
7230 if (global_scope_p (b
))
7233 store_bindings (b
->names
, &s
->old_bindings
);
7234 /* We also need to check class_shadowed to save class-level type
7235 bindings, since pushclass doesn't fill in b->names. */
7236 if (b
->kind
== sk_class
)
7237 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
7239 /* Unwind type-value slots back to top level. */
7240 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
7241 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
7244 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
7245 IDENTIFIER_MARKED (sb
->identifier
) = 0;
7247 s
->prev
= scope_chain
;
7249 s
->need_pop_function_context
= need_pop
;
7250 s
->function_decl
= current_function_decl
;
7251 s
->unevaluated_operand
= cp_unevaluated_operand
;
7252 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
7253 s
->suppress_location_wrappers
= suppress_location_wrappers
;
7254 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
7257 current_function_decl
= NULL_TREE
;
7258 current_lang_base
= NULL
;
7259 current_lang_name
= lang_name_cplusplus
;
7260 current_namespace
= global_namespace
;
7261 push_class_stack ();
7262 cp_unevaluated_operand
= 0;
7263 c_inhibit_evaluation_warnings
= 0;
7264 suppress_location_wrappers
= 0;
7268 do_pop_from_top_level (void)
7270 struct saved_scope
*s
= scope_chain
;
7271 cxx_saved_binding
*saved
;
7274 /* Clear out class-level bindings cache. */
7275 if (previous_class_level
)
7276 invalidate_class_lookup_cache ();
7279 release_tree_vector (current_lang_base
);
7281 scope_chain
= s
->prev
;
7282 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
7284 tree id
= saved
->identifier
;
7286 IDENTIFIER_BINDING (id
) = saved
->binding
;
7287 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
7290 /* If we were in the middle of compiling a function, restore our
7292 if (s
->need_pop_function_context
)
7293 pop_function_context ();
7294 current_function_decl
= s
->function_decl
;
7295 cp_unevaluated_operand
= s
->unevaluated_operand
;
7296 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
7297 suppress_location_wrappers
= s
->suppress_location_wrappers
;
7299 /* Make this saved_scope structure available for reuse by
7300 push_to_top_level. */
7301 s
->prev
= free_saved_scope
;
7302 free_saved_scope
= s
;
7305 /* Push into the scope of the namespace NS, even if it is deeply
7306 nested within another namespace. */
7309 do_push_nested_namespace (tree ns
)
7311 if (ns
== global_namespace
)
7312 do_push_to_top_level ();
7315 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
7317 (find_namespace_value (current_namespace
, DECL_NAME (ns
)) == ns
);
7318 resume_scope (NAMESPACE_LEVEL (ns
));
7319 current_namespace
= ns
;
7323 /* Pop back from the scope of the namespace NS, which was previously
7324 entered with push_nested_namespace. */
7327 do_pop_nested_namespace (tree ns
)
7329 while (ns
!= global_namespace
)
7331 ns
= CP_DECL_CONTEXT (ns
);
7332 current_namespace
= ns
;
7336 do_pop_from_top_level ();
7339 /* Add TARGET to USINGS, if it does not already exist there.
7340 We used to build the complete graph of usings at this point, from
7341 the POV of the source namespaces. Now we build that as we perform
7342 the unqualified search. */
7345 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
7348 for (unsigned ix
= usings
->length (); ix
--;)
7349 if ((*usings
)[ix
] == target
)
7352 vec_safe_push (usings
, target
);
7355 /* Tell the debug system of a using directive. */
7358 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
7360 /* Emit debugging info. */
7361 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
7362 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
7366 /* Process a using directive. */
7369 finish_using_directive (tree target
, tree attribs
)
7371 if (target
== error_mark_node
)
7374 if (current_binding_level
->kind
!= sk_namespace
)
7375 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
7377 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
7378 ORIGINAL_NAMESPACE (target
), false);
7380 add_using_namespace (current_binding_level
->using_directives
,
7381 ORIGINAL_NAMESPACE (target
));
7383 if (attribs
!= error_mark_node
)
7384 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
7386 tree name
= get_attribute_name (a
);
7387 if (current_binding_level
->kind
== sk_namespace
7388 && is_attribute_p ("strong", name
))
7390 if (warning (0, "%<strong%> using directive no longer supported")
7391 && CP_DECL_CONTEXT (target
) == current_namespace
)
7392 inform (DECL_SOURCE_LOCATION (target
),
7393 "you can use an inline namespace instead");
7396 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
7400 /* Pushes X into the global namespace. */
7403 pushdecl_top_level (tree x
)
7405 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7406 do_push_to_top_level ();
7407 x
= pushdecl_namespace_level (x
);
7408 do_pop_from_top_level ();
7409 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7413 /* Pushes X into the global namespace and calls cp_finish_decl to
7414 register the variable, initializing it with INIT. */
7417 pushdecl_top_level_and_finish (tree x
, tree init
)
7419 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7420 do_push_to_top_level ();
7421 x
= pushdecl_namespace_level (x
);
7422 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
7423 do_pop_from_top_level ();
7424 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7428 /* Enter the namespaces from current_namerspace to NS. */
7431 push_inline_namespaces (tree ns
)
7434 if (ns
!= current_namespace
)
7436 gcc_assert (ns
!= global_namespace
);
7437 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7438 resume_scope (NAMESPACE_LEVEL (ns
));
7439 current_namespace
= ns
;
7445 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
7446 then we enter an anonymous namespace. If MAKE_INLINE is true, then
7447 we create an inline namespace (it is up to the caller to check upon
7448 redefinition). Return the number of namespaces entered. */
7451 push_namespace (tree name
, bool make_inline
)
7453 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7456 /* We should not get here if the global_namespace is not yet constructed
7457 nor if NAME designates the global namespace: The global scope is
7458 constructed elsewhere. */
7459 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
7461 tree ns
= NULL_TREE
;
7463 name_lookup
lookup (name
);
7464 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
7466 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
7468 /* An ambiguous lookup. If exactly one is a namespace, we
7469 want that. If more than one is a namespace, error, but
7470 pick one of them. */
7471 /* DR2061 can cause us to find multiple namespaces of the same
7472 name. We must treat that carefully and avoid thinking we
7473 need to push a new (possibly) duplicate namespace. Hey,
7474 if you want to use the same identifier within an inline
7475 nest, knock yourself out. */
7476 for (tree
*chain
= &lookup
.value
, next
; (next
= *chain
);)
7478 tree decl
= TREE_VALUE (next
);
7479 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
7483 else if (SCOPE_DEPTH (ns
) >= SCOPE_DEPTH (decl
))
7487 chain
= &TREE_CHAIN (next
);
7491 *chain
= TREE_CHAIN (next
);
7494 if (TREE_CHAIN (lookup
.value
))
7496 error ("%<namespace %E%> is ambiguous", name
);
7497 print_candidates (lookup
.value
);
7500 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
7504 if (tree dna
= DECL_NAMESPACE_ALIAS (ns
))
7506 /* A namespace alias is not allowed here, but if the alias
7507 is for a namespace also inside the current scope,
7508 accept it with a diagnostic. That's better than dying
7510 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
7512 error ("namespace alias %qD not allowed here, "
7513 "assuming %qD", ns
, dna
);
7521 bool new_ns
= false;
7523 /* DR2061. NS might be a member of an inline namespace. We
7524 need to push into those namespaces. */
7525 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
7528 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
7529 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
7530 if (!SCOPE_DEPTH (ns
))
7531 /* We only allow depth 255. */
7532 sorry ("cannot nest more than %d namespaces",
7533 SCOPE_DEPTH (current_namespace
));
7534 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
7537 if (pushdecl (ns
) == error_mark_node
)
7543 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
7546 add_using_namespace (current_binding_level
->using_directives
,
7549 else if (TREE_PUBLIC (current_namespace
))
7550 TREE_PUBLIC (ns
) = 1;
7554 DECL_NAMESPACE_INLINE_P (ns
) = true;
7555 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
7558 if (!name
|| make_inline
)
7559 emit_debug_info_using_namespace (current_namespace
, ns
, true);
7565 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
7567 error ("inline namespace must be specified at initial definition");
7568 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
7571 begin_scope (sk_namespace
, ns
);
7573 resume_scope (NAMESPACE_LEVEL (ns
));
7574 current_namespace
= ns
;
7578 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7582 /* Pop from the scope of the current namespace. */
7585 pop_namespace (void)
7587 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7589 gcc_assert (current_namespace
!= global_namespace
);
7590 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
7591 /* The binding level is not popped, as it might be re-opened later. */
7594 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7597 /* External entry points for do_{push_to/pop_from}_top_level. */
7600 push_to_top_level (void)
7602 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7603 do_push_to_top_level ();
7604 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7608 pop_from_top_level (void)
7610 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7611 do_pop_from_top_level ();
7612 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7615 /* External entry points for do_{push,pop}_nested_namespace. */
7618 push_nested_namespace (tree ns
)
7620 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7621 do_push_nested_namespace (ns
);
7622 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7626 pop_nested_namespace (tree ns
)
7628 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7629 gcc_assert (current_namespace
== ns
);
7630 do_pop_nested_namespace (ns
);
7631 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7634 /* Pop off extraneous binding levels left over due to syntax errors.
7635 We don't pop past namespaces, as they might be valid. */
7638 pop_everything (void)
7640 if (ENABLE_SCOPE_CHECKING
)
7641 verbatim ("XXX entering %<pop_everything ()%>");
7642 while (!namespace_bindings_p ())
7644 if (current_binding_level
->kind
== sk_class
)
7645 pop_nested_class ();
7649 if (ENABLE_SCOPE_CHECKING
)
7650 verbatim ("XXX leaving %<pop_everything ()%>");
7653 /* Emit debugging information for using declarations and directives.
7654 If input tree is overloaded fn then emit debug info for all
7658 cp_emit_debug_info_for_using (tree t
, tree context
)
7660 /* Don't try to emit any debug information if we have errors. */
7664 /* Do not supply context to imported_module_or_decl, if
7665 it is a global namespace. */
7666 if (context
== global_namespace
)
7667 context
= NULL_TREE
;
7669 t
= MAYBE_BASELINK_FUNCTIONS (t
);
7671 for (lkp_iterator
iter (t
); iter
; ++iter
)
7675 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
7676 /* FIXME: Handle TEMPLATE_DECLs. */
7679 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
7680 of a builtin function. */
7681 if (TREE_CODE (fn
) == FUNCTION_DECL
7682 && DECL_EXTERNAL (fn
)
7683 && fndecl_built_in_p (fn
))
7686 if (building_stmt_list_p ())
7687 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
7689 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
7694 /* Return the result of unqualified lookup for the overloaded operator
7695 designated by CODE, if we are in a template and the binding we find is
7699 op_unqualified_lookup (tree fnname
)
7701 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
7703 cp_binding_level
*l
= binding
->scope
;
7704 while (l
&& !l
->this_entity
)
7707 if (l
&& uses_template_parms (l
->this_entity
))
7708 /* Don't preserve decls from an uninstantiated template,
7709 wait until that template is instantiated. */
7713 tree fns
= lookup_name (fnname
);
7715 /* Remember we found nothing! */
7716 return error_mark_node
;
7718 tree d
= is_overloaded_fn (fns
) ? get_first_fn (fns
) : fns
;
7719 if (DECL_CLASS_SCOPE_P (d
))
7720 /* We don't need to remember class-scope functions or declarations,
7721 normal unqualified lookup will find them again. */
7727 /* E is an expression representing an operation with dependent type, so we
7728 don't know yet whether it will use the built-in meaning of the operator or a
7729 function. Remember declarations of that operator in scope.
7731 We then inject a fake binding of that lookup into the
7732 instantiation's parameter scope. This approach fails if the user
7733 has different using declarations or directives in different local
7734 binding of the current function from whence we need to do lookups
7735 (we'll cache what we see on the first lookup). */
7737 static const char *const op_bind_attrname
= "operator bindings";
7740 maybe_save_operator_binding (tree e
)
7742 /* This is only useful in a generic lambda. */
7743 if (!processing_template_decl
)
7746 tree cfn
= current_function_decl
;
7750 /* Do this for lambdas and code that will emit a CMI. In a module's
7751 GMF we don't yet know whether there will be a CMI. */
7752 if (!current_lambda_expr ())
7755 tree fnname
= ovl_op_identifier (false, TREE_CODE (e
));
7759 tree attributes
= DECL_ATTRIBUTES (cfn
);
7760 tree op_attr
= lookup_attribute (op_bind_attrname
, attributes
);
7763 op_attr
= tree_cons (get_identifier (op_bind_attrname
),
7764 NULL_TREE
, attributes
);
7765 DECL_ATTRIBUTES (cfn
) = op_attr
;
7768 tree op_bind
= purpose_member (fnname
, TREE_VALUE (op_attr
));
7771 tree fns
= op_unqualified_lookup (fnname
);
7773 /* Always record, so we don't keep looking for this
7775 TREE_VALUE (op_attr
) = tree_cons (fnname
, fns
, TREE_VALUE (op_attr
));
7779 /* Called from cp_free_lang_data so we don't put this into LTO. */
7782 discard_operator_bindings (tree decl
)
7784 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
7785 DECL_ATTRIBUTES (decl
));
7788 /* Subroutine of start_preparsed_function: push the bindings we saved away in
7789 maybe_save_op_lookup into the function parameter binding level. */
7792 push_operator_bindings ()
7794 tree decl1
= current_function_decl
;
7795 if (tree attr
= lookup_attribute (op_bind_attrname
,
7796 DECL_ATTRIBUTES (decl1
)))
7797 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
7798 if (tree val
= TREE_VALUE (binds
))
7800 tree name
= TREE_PURPOSE (binds
);
7801 push_local_binding (name
, val
, /*using*/true);
7805 #include "gt-cp-name-lookup.h"