1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "stringpool.h"
27 #include "print-tree.h"
30 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
36 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
37 static cp_binding_level
*innermost_nonclass_level (void);
38 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
41 /* Create an overload suitable for recording an artificial TYPE_DECL
42 and another decl. We use this machanism to implement the struct
43 stat hack within a namespace. It'd be nice to use it everywhere. */
45 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
46 #define STAT_TYPE(N) TREE_TYPE (N)
47 #define STAT_DECL(N) OVL_FUNCTION (N)
48 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
49 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
51 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
55 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
57 tree result
= make_node (OVERLOAD
);
59 /* Mark this as a lookup, so we can tell this is a stat hack. */
60 OVL_LOOKUP_P (result
) = true;
61 STAT_DECL (result
) = decl
;
62 STAT_TYPE (result
) = type
;
66 /* Create a local binding level for NAME. */
69 create_local_binding (cp_binding_level
*level
, tree name
)
71 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
73 INHERITED_VALUE_BINDING_P (binding
) = false;
74 LOCAL_BINDING_P (binding
) = true;
75 binding
->scope
= level
;
76 binding
->previous
= IDENTIFIER_BINDING (name
);
78 IDENTIFIER_BINDING (name
) = binding
;
83 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
84 make an empty binding if there wasn't one. */
87 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
94 slot
= &DECL_NAMESPACE_BINDINGS (ns
)->get_or_insert (name
, &existed
);
99 slot
= DECL_NAMESPACE_BINDINGS (ns
)->get (name
);
104 find_namespace_value (tree ns
, tree name
)
106 tree
*b
= find_namespace_slot (ns
, name
);
108 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
111 /* Add DECL to the list of things declared in B. */
114 add_decl_to_level (cp_binding_level
*b
, tree decl
)
116 gcc_assert (b
->kind
!= sk_class
);
118 /* Make sure we don't create a circular list. xref_tag can end
119 up pushing the same artificial decl more than once. We
120 should have already detected that in update_binding. */
121 gcc_assert (b
->names
!= decl
);
123 /* We build up the list in reverse order, and reverse it later if
125 TREE_CHAIN (decl
) = b
->names
;
128 /* If appropriate, add decl to separate list of statics. We
129 include extern variables because they might turn out to be
130 static later. It's OK for this list to contain a few false
132 if (b
->kind
== sk_namespace
134 && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
135 || (TREE_CODE (decl
) == FUNCTION_DECL
136 && (!TREE_PUBLIC (decl
)
137 || decl_anon_ns_mem_p (decl
)
138 || DECL_DECLARED_INLINE_P (decl
)))))
139 vec_safe_push (static_decls
, decl
);
142 /* Find the binding for NAME in the local binding level B. */
145 find_local_binding (cp_binding_level
*b
, tree name
)
147 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
148 for (;; b
= b
->level_chain
)
150 if (binding
->scope
== b
151 && !(VAR_P (binding
->value
)
152 && DECL_DEAD_FOR_LOCAL (binding
->value
)))
155 /* Cleanup contours are transparent to the language. */
156 if (b
->kind
!= sk_cleanup
)
165 typedef std::pair
<tree
, tree
> using_pair
;
166 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
169 tree name
; /* The identifier being looked for. */
170 tree value
; /* A (possibly ambiguous) set of things found. */
171 tree type
; /* A type that has been found. */
172 int flags
; /* Lookup flags. */
173 bool deduping
; /* Full deduping is needed because using declarations
175 vec
<tree
, va_heap
, vl_embed
> *scopes
;
176 name_lookup
*previous
; /* Previously active lookup. */
179 /* Marked scope stack for outermost name lookup. */
180 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
181 /* Currently active lookup. */
182 static name_lookup
*active
;
185 name_lookup (tree n
, int f
= 0)
186 : name (n
), value (NULL_TREE
), type (NULL_TREE
), flags (f
),
187 deduping (false), scopes (NULL
), previous (NULL
)
196 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
197 name_lookup (const name_lookup
&);
198 name_lookup
&operator= (const name_lookup
&);
201 static bool seen_p (tree scope
)
203 return LOOKUP_SEEN_P (scope
);
205 static bool found_p (tree scope
)
207 return LOOKUP_FOUND_P (scope
);
210 void mark_seen (tree scope
); /* Mark and add to scope vector. */
211 static void mark_found (tree scope
)
213 gcc_checking_assert (seen_p (scope
));
214 LOOKUP_FOUND_P (scope
) = true;
216 bool see_and_mark (tree scope
)
218 bool ret
= seen_p (scope
);
223 bool find_and_mark (tree scope
);
226 void preserve_state ();
227 void restore_state ();
230 static tree
ambiguous (tree thing
, tree current
);
231 void add_overload (tree fns
);
232 void add_value (tree new_val
);
233 void add_type (tree new_type
);
234 bool process_binding (tree val_bind
, tree type_bind
);
236 /* Look in only namespace. */
237 bool search_namespace_only (tree scope
);
238 /* Look in namespace and its (recursive) inlines. Ignore using
239 directives. Return true if something found (inc dups). */
240 bool search_namespace (tree scope
);
241 /* Look in the using directives of namespace + inlines using
242 qualified lookup rules. */
243 bool search_usings (tree scope
);
246 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
247 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
248 vec
<tree
, va_gc
> *usings
);
249 using_queue
*queue_usings (using_queue
*queue
, int depth
,
250 vec
<tree
, va_gc
> *usings
)
253 queue
= do_queue_usings (queue
, depth
, usings
);
260 void adl_expr (tree
);
261 void adl_type (tree
);
262 void adl_template_arg (tree
);
263 void adl_class (tree
);
264 void adl_bases (tree
);
265 void adl_class_only (tree
);
266 void adl_namespace (tree
);
267 void adl_namespace_only (tree
);
270 /* Search namespace + inlines + maybe usings as qualified lookup. */
271 bool search_qualified (tree scope
, bool usings
= true);
273 /* Search namespace + inlines + usings as unqualified lookup. */
274 bool search_unqualified (tree scope
, cp_binding_level
*);
276 /* ADL lookup of ARGS. */
277 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
280 /* Scope stack shared by all outermost lookups. This avoids us
281 allocating and freeing on every single lookup. */
282 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
284 /* Currently active lookup. */
285 name_lookup
*name_lookup::active
;
287 /* Name lookup is recursive, becase ADL can cause template
288 instatiation. This is of course a rare event, so we optimize for
289 it not happening. When we discover an active name-lookup, which
290 must be an ADL lookup, we need to unmark the marked scopes and also
291 unmark the lookup we might have been accumulating. */
294 name_lookup::preserve_state ()
299 unsigned length
= vec_safe_length (previous
->scopes
);
300 vec_safe_reserve (previous
->scopes
, length
* 2);
301 for (unsigned ix
= length
; ix
--;)
303 tree decl
= (*previous
->scopes
)[ix
];
305 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
306 LOOKUP_SEEN_P (decl
) = false;
308 /* Preserve the FOUND_P state on the interrupted lookup's
310 if (LOOKUP_FOUND_P (decl
))
312 LOOKUP_FOUND_P (decl
) = false;
313 previous
->scopes
->quick_push (decl
);
317 /* Unmark the outer partial lookup. */
318 if (previous
->deduping
)
319 lookup_mark (previous
->value
, false);
322 scopes
= shared_scopes
;
326 /* Restore the marking state of a lookup we interrupted. */
329 name_lookup::restore_state ()
332 lookup_mark (value
, false);
334 /* Unmark and empty this lookup's scope stack. */
335 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
337 tree decl
= scopes
->pop ();
338 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
339 LOOKUP_SEEN_P (decl
) = false;
340 LOOKUP_FOUND_P (decl
) = false;
348 unsigned length
= vec_safe_length (previous
->scopes
);
349 for (unsigned ix
= 0; ix
!= length
; ix
++)
351 tree decl
= (*previous
->scopes
)[ix
];
352 if (LOOKUP_SEEN_P (decl
))
354 /* The remainder of the scope stack must be recording
355 FOUND_P decls, which we want to pop off. */
358 tree decl
= previous
->scopes
->pop ();
359 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
360 && !LOOKUP_FOUND_P (decl
));
361 LOOKUP_FOUND_P (decl
) = true;
363 while (++ix
!= length
);
367 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
368 LOOKUP_SEEN_P (decl
) = true;
371 /* Remark the outer partial lookup. */
372 if (previous
->deduping
)
373 lookup_mark (previous
->value
, true);
376 shared_scopes
= scopes
;
380 name_lookup::mark_seen (tree scope
)
382 gcc_checking_assert (!seen_p (scope
));
383 LOOKUP_SEEN_P (scope
) = true;
384 vec_safe_push (scopes
, scope
);
388 name_lookup::find_and_mark (tree scope
)
390 bool result
= LOOKUP_FOUND_P (scope
);
393 LOOKUP_FOUND_P (scope
) = true;
394 if (!LOOKUP_SEEN_P (scope
))
395 vec_safe_push (scopes
, scope
);
401 /* THING and CURRENT are ambiguous, concatenate them. */
404 name_lookup::ambiguous (tree thing
, tree current
)
406 if (TREE_CODE (current
) != TREE_LIST
)
408 current
= build_tree_list (NULL_TREE
, current
);
409 TREE_TYPE (current
) = error_mark_node
;
411 current
= tree_cons (NULL_TREE
, thing
, current
);
412 TREE_TYPE (current
) = error_mark_node
;
417 /* FNS is a new overload set to add to the exising set. */
420 name_lookup::add_overload (tree fns
)
422 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
425 if (flags
& LOOKUP_HIDDEN
)
426 probe
= ovl_skip_hidden (probe
);
427 if (probe
&& TREE_CODE (probe
) == OVERLOAD
&& OVL_USING_P (probe
))
429 /* We're about to add something found by a using
430 declaration, so need to engage deduping mode. */
431 lookup_mark (value
, true);
436 value
= lookup_maybe_add (fns
, value
, deduping
);
439 /* Add a NEW_VAL, a found value binding into the current value binding. */
442 name_lookup::add_value (tree new_val
)
444 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
445 add_overload (new_val
);
448 else if (value
== new_val
)
450 else if ((TREE_CODE (value
) == TYPE_DECL
451 && TREE_CODE (new_val
) == TYPE_DECL
452 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
458 /* Disengage deduping mode. */
459 lookup_mark (value
, false);
462 value
= ambiguous (new_val
, value
);
466 /* Add a NEW_TYPE, a found type binding into the current type binding. */
469 name_lookup::add_type (tree new_type
)
473 else if (TREE_CODE (type
) == TREE_LIST
474 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
475 type
= ambiguous (new_type
, type
);
478 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
479 true if we actually found something noteworthy. */
482 name_lookup::process_binding (tree new_val
, tree new_type
)
484 /* Did we really see a type? */
486 && (LOOKUP_NAMESPACES_ONLY (flags
)
487 || (!(flags
& LOOKUP_HIDDEN
)
488 && DECL_LANG_SPECIFIC (new_type
)
489 && DECL_ANTICIPATED (new_type
))))
490 new_type
= NULL_TREE
;
492 if (new_val
&& !(flags
& LOOKUP_HIDDEN
))
493 new_val
= ovl_skip_hidden (new_val
);
495 /* Do we really see a value? */
497 switch (TREE_CODE (new_val
))
500 /* If we expect types or namespaces, and not templates,
501 or this is not a template class. */
502 if ((LOOKUP_QUALIFIERS_ONLY (flags
)
503 && !DECL_TYPE_TEMPLATE_P (new_val
)))
507 if (LOOKUP_NAMESPACES_ONLY (flags
)
508 || (new_type
&& (flags
& LOOKUP_PREFER_TYPES
)))
512 if (LOOKUP_TYPES_ONLY (flags
))
516 if (LOOKUP_QUALIFIERS_ONLY (flags
))
523 new_type
= NULL_TREE
;
526 /* Merge into the lookup */
532 return new_val
!= NULL_TREE
;
535 /* Look in exactly namespace SCOPE. */
538 name_lookup::search_namespace_only (tree scope
)
542 if (tree
*binding
= find_namespace_slot (scope
, name
))
543 found
|= process_binding (MAYBE_STAT_DECL (*binding
),
544 MAYBE_STAT_TYPE (*binding
));
549 /* Conditionally look in namespace SCOPE and inline children. */
552 name_lookup::search_namespace (tree scope
)
554 if (see_and_mark (scope
))
555 /* We've visited this scope before. Return what we found then. */
556 return found_p (scope
);
558 /* Look in exactly namespace. */
559 bool found
= search_namespace_only (scope
);
561 /* Recursively look in its inline children. */
562 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
563 for (unsigned ix
= inlinees
->length (); ix
--;)
564 found
|= search_namespace ((*inlinees
)[ix
]);
572 /* Recursively follow using directives of SCOPE & its inline children.
573 Such following is essentially a flood-fill algorithm. */
576 name_lookup::search_usings (tree scope
)
578 /* We do not check seen_p here, as that was already set during the
579 namespace_only walk. */
584 if (vec
<tree
, va_gc
> *usings
= DECL_NAMESPACE_USING (scope
))
585 for (unsigned ix
= usings
->length (); ix
--;)
586 found
|= search_qualified ((*usings
)[ix
], true);
588 /* Look in its inline children. */
589 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
590 for (unsigned ix
= inlinees
->length (); ix
--;)
591 found
|= search_usings ((*inlinees
)[ix
]);
599 /* Qualified namespace lookup in SCOPE.
600 1) Look in SCOPE (+inlines). If found, we're done.
601 2) Otherwise, if USINGS is true,
602 recurse for every using directive of SCOPE (+inlines).
604 Trickiness is (a) loops and (b) multiple paths to same namespace.
605 In both cases we want to not repeat any lookups, and know whether
606 to stop the caller's step #2. Do this via the FOUND_P marker. */
609 name_lookup::search_qualified (tree scope
, bool usings
)
614 found
= found_p (scope
);
617 found
= search_namespace (scope
);
618 if (!found
&& usings
)
619 found
= search_usings (scope
);
625 /* Add SCOPE to the unqualified search queue, recursively add its
626 inlines and those via using directives. */
628 name_lookup::using_queue
*
629 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
631 if (see_and_mark (scope
))
636 while (SCOPE_DEPTH (common
) > depth
)
637 common
= CP_DECL_CONTEXT (common
);
638 vec_safe_push (queue
, using_pair (common
, scope
));
640 /* Queue its inline children. */
641 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
642 for (unsigned ix
= inlinees
->length (); ix
--;)
643 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
645 /* Queue its using targets. */
646 queue
= queue_usings (queue
, depth
, DECL_NAMESPACE_USING (scope
));
651 /* Add the namespaces in USINGS to the unqualified search queue. */
653 name_lookup::using_queue
*
654 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
655 vec
<tree
, va_gc
> *usings
)
657 for (unsigned ix
= usings
->length (); ix
--;)
658 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
663 /* Unqualified namespace lookup in SCOPE.
664 1) add scope+inlins to worklist.
665 2) recursively add target of every using directive
666 3) for each worklist item where SCOPE is common ancestor, search it
667 4) if nothing find, scope=parent, goto 1. */
670 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
672 /* Make static to avoid continual reallocation. We're not
674 static using_queue
*queue
= NULL
;
676 int length
= vec_safe_length (queue
);
678 /* Queue local using-directives. */
679 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
680 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
682 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
684 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
685 int depth
= SCOPE_DEPTH (scope
);
687 /* Queue namespaces reachable from SCOPE. */
688 queue
= queue_namespace (queue
, depth
, scope
);
690 /* Search every queued namespace where SCOPE is the common
691 ancestor. Adjust the others. */
692 unsigned ix
= length
;
695 using_pair
&pair
= (*queue
)[ix
];
696 while (pair
.first
== scope
)
698 found
|= search_namespace_only (pair
.second
);
699 pair
= queue
->pop ();
700 if (ix
== queue
->length ())
703 /* The depth is the same as SCOPE, find the parent scope. */
704 if (SCOPE_DEPTH (pair
.first
) == depth
)
705 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
708 while (ix
< queue
->length ());
710 if (scope
== global_namespace
)
714 vec_safe_truncate (queue
, length
);
719 /* FNS is a value binding. If it is a (set of overloaded) functions,
720 add them into the current value. */
723 name_lookup::add_fns (tree fns
)
727 else if (TREE_CODE (fns
) == OVERLOAD
)
729 if (TREE_TYPE (fns
) != unknown_type_node
)
730 fns
= OVL_FUNCTION (fns
);
732 else if (!DECL_DECLARES_FUNCTION_P (fns
))
738 /* Add functions of a namespace to the lookup structure. */
741 name_lookup::adl_namespace_only (tree scope
)
745 /* Look down into inline namespaces. */
746 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
747 for (unsigned ix
= inlinees
->length (); ix
--;)
748 adl_namespace_only ((*inlinees
)[ix
]);
750 if (tree fns
= find_namespace_value (scope
, name
))
751 add_fns (ovl_skip_hidden (fns
));
754 /* Find the containing non-inlined namespace, add it and all its
758 name_lookup::adl_namespace (tree scope
)
763 /* Find the containing non-inline namespace. */
764 while (DECL_NAMESPACE_INLINE_P (scope
))
765 scope
= CP_DECL_CONTEXT (scope
);
767 adl_namespace_only (scope
);
770 /* Adds the class and its friends to the lookup structure. */
773 name_lookup::adl_class_only (tree type
)
775 /* Backend-built structures, such as __builtin_va_list, aren't
776 affected by all this. */
777 if (!CLASS_TYPE_P (type
))
780 type
= TYPE_MAIN_VARIANT (type
);
782 if (see_and_mark (type
))
785 tree context
= decl_namespace_context (type
);
786 adl_namespace (context
);
788 complete_type (type
);
791 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
)); list
;
792 list
= TREE_CHAIN (list
))
793 if (name
== FRIEND_NAME (list
))
794 for (tree friends
= FRIEND_DECLS (list
); friends
;
795 friends
= TREE_CHAIN (friends
))
797 tree fn
= TREE_VALUE (friends
);
799 /* Only interested in global functions with potentially hidden
800 (i.e. unqualified) declarations. */
801 if (CP_DECL_CONTEXT (fn
) != context
)
804 /* Only interested in anticipated friends. (Non-anticipated
805 ones will have been inserted during the namespace
807 if (!DECL_ANTICIPATED (fn
))
810 /* Template specializations are never found by name lookup.
811 (Templates themselves can be found, but not template
813 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
820 /* Adds the class and its bases to the lookup structure.
821 Returns true on error. */
824 name_lookup::adl_bases (tree type
)
826 adl_class_only (type
);
828 /* Process baseclasses. */
829 if (tree binfo
= TYPE_BINFO (type
))
834 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
835 adl_bases (BINFO_TYPE (base_binfo
));
839 /* Adds everything associated with a class argument type to the lookup
840 structure. Returns true on error.
842 If T is a class type (including unions), its associated classes are: the
843 class itself; the class of which it is a member, if any; and its direct
844 and indirect base classes. Its associated namespaces are the namespaces
845 of which its associated classes are members. Furthermore, if T is a
846 class template specialization, its associated namespaces and classes
847 also include: the namespaces and classes associated with the types of
848 the template arguments provided for template type parameters (excluding
849 template template parameters); the namespaces of which any template
850 template arguments are members; and the classes of which any member
851 templates used as template template arguments are members. [ Note:
852 non-type template arguments do not contribute to the set of associated
853 namespaces. --end note] */
856 name_lookup::adl_class (tree type
)
858 /* Backend build structures, such as __builtin_va_list, aren't
859 affected by all this. */
860 if (!CLASS_TYPE_P (type
))
863 type
= TYPE_MAIN_VARIANT (type
);
864 /* We don't set found here because we have to have set seen first,
865 which is done in the adl_bases walk. */
872 if (TYPE_CLASS_SCOPE_P (type
))
873 adl_class_only (TYPE_CONTEXT (type
));
875 /* Process template arguments. */
876 if (CLASSTYPE_TEMPLATE_INFO (type
)
877 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
879 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
880 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
881 adl_template_arg (TREE_VEC_ELT (list
, i
));
886 name_lookup::adl_expr (tree expr
)
891 gcc_assert (!TYPE_P (expr
));
893 if (TREE_TYPE (expr
) != unknown_type_node
)
895 adl_type (TREE_TYPE (expr
));
899 if (TREE_CODE (expr
) == ADDR_EXPR
)
900 expr
= TREE_OPERAND (expr
, 0);
901 if (TREE_CODE (expr
) == COMPONENT_REF
902 || TREE_CODE (expr
) == OFFSET_REF
)
903 expr
= TREE_OPERAND (expr
, 1);
904 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
907 for (lkp_iterator
iter (expr
); iter
; ++iter
)
908 adl_type (TREE_TYPE (*iter
));
909 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
911 /* The working paper doesn't currently say how to handle
912 template-id arguments. The sensible thing would seem to be
913 to handle the list of template candidates like a normal
914 overload set, and handle the template arguments like we do
915 for class template specializations. */
917 /* First the templates. */
918 adl_expr (TREE_OPERAND (expr
, 0));
920 /* Now the arguments. */
921 if (tree args
= TREE_OPERAND (expr
, 1))
922 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
923 adl_template_arg (TREE_VEC_ELT (args
, ix
));
928 name_lookup::adl_type (tree type
)
933 if (TYPE_PTRDATAMEM_P (type
))
935 /* Pointer to member: associate class type and value type. */
936 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
937 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
941 switch (TREE_CODE (type
))
944 if (TYPE_PTRMEMFUNC_P (type
))
946 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
955 /* The basetype is referenced in the first arg type, so just
958 /* Associate the parameter types. */
959 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
960 adl_type (TREE_VALUE (args
));
966 adl_type (TREE_TYPE (type
));
970 if (TYPE_CLASS_SCOPE_P (type
))
971 adl_class_only (TYPE_CONTEXT (type
));
972 adl_namespace (decl_namespace_context (type
));
976 gcc_assert (type
== unknown_type_node
977 || type
== init_list_type_node
);
980 case TYPE_PACK_EXPANSION
:
981 adl_type (PACK_EXPANSION_PATTERN (type
));
989 /* Adds everything associated with a template argument to the lookup
993 name_lookup::adl_template_arg (tree arg
)
995 /* [basic.lookup.koenig]
997 If T is a template-id, its associated namespaces and classes are
998 ... the namespaces and classes associated with the types of the
999 template arguments provided for template type parameters
1000 (excluding template template parameters); the namespaces in which
1001 any template template arguments are defined; and the classes in
1002 which any member templates used as template template arguments
1003 are defined. [Note: non-type template arguments do not
1004 contribute to the set of associated namespaces. ] */
1006 /* Consider first template template arguments. */
1007 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1008 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1010 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1012 tree ctx
= CP_DECL_CONTEXT (arg
);
1014 /* It's not a member template. */
1015 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1016 adl_namespace (ctx
);
1017 /* Otherwise, it must be member template. */
1019 adl_class_only (ctx
);
1021 /* It's an argument pack; handle it recursively. */
1022 else if (ARGUMENT_PACK_P (arg
))
1024 tree args
= ARGUMENT_PACK_ARGS (arg
);
1025 int i
, len
= TREE_VEC_LENGTH (args
);
1026 for (i
= 0; i
< len
; ++i
)
1027 adl_template_arg (TREE_VEC_ELT (args
, i
));
1029 /* It's not a template template argument, but it is a type template
1031 else if (TYPE_P (arg
))
1035 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1036 the call arguments. */
1039 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1044 lookup_mark (fns
, true);
1051 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1052 /* OMP reduction operators put an ADL-significant type as the
1064 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1065 static void consider_binding_level (tree name
,
1066 best_match
<tree
, const char *> &bm
,
1067 cp_binding_level
*lvl
,
1068 bool look_within_fields
,
1069 enum lookup_name_fuzzy_kind kind
);
1070 static void diagnose_name_conflict (tree
, tree
);
1072 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1073 don't add duplicates to it. ARGS is the vector of call
1074 arguments (which will not be empty). */
1077 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1079 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1080 name_lookup
lookup (name
);
1081 fns
= lookup
.search_adl (fns
, args
);
1082 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1086 /* Compute the chain index of a binding_entry given the HASH value of its
1087 name and the total COUNT of chains. COUNT is assumed to be a power
1090 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
1092 /* A free list of "binding_entry"s awaiting for re-use. */
1094 static GTY((deletable
)) binding_entry free_binding_entry
= NULL
;
1096 /* The binding oracle; see cp-tree.h. */
1098 cp_binding_oracle_function
*cp_binding_oracle
;
1100 /* If we have a binding oracle, ask it for all namespace-scoped
1101 definitions of NAME. */
1104 query_oracle (tree name
)
1106 if (!cp_binding_oracle
)
1109 /* LOOKED_UP holds the set of identifiers that we have already
1110 looked up with the oracle. */
1111 static hash_set
<tree
> looked_up
;
1112 if (looked_up
.add (name
))
1115 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
1118 /* Create a binding_entry object for (NAME, TYPE). */
1120 static inline binding_entry
1121 binding_entry_make (tree name
, tree type
)
1123 binding_entry entry
;
1125 if (free_binding_entry
)
1127 entry
= free_binding_entry
;
1128 free_binding_entry
= entry
->chain
;
1131 entry
= ggc_alloc
<binding_entry_s
> ();
1135 entry
->chain
= NULL
;
1140 /* Put ENTRY back on the free list. */
1143 binding_entry_free (binding_entry entry
)
1147 entry
->chain
= free_binding_entry
;
1148 free_binding_entry
= entry
;
1152 /* The datatype used to implement the mapping from names to types at
1154 struct GTY(()) binding_table_s
{
1155 /* Array of chains of "binding_entry"s */
1156 binding_entry
* GTY((length ("%h.chain_count"))) chain
;
1158 /* The number of chains in this table. This is the length of the
1159 member "chain" considered as an array. */
1162 /* Number of "binding_entry"s in this table. */
1166 /* Construct TABLE with an initial CHAIN_COUNT. */
1169 binding_table_construct (binding_table table
, size_t chain_count
)
1171 table
->chain_count
= chain_count
;
1172 table
->entry_count
= 0;
1173 table
->chain
= ggc_cleared_vec_alloc
<binding_entry
> (table
->chain_count
);
1176 /* Make TABLE's entries ready for reuse. */
1179 binding_table_free (binding_table table
)
1187 for (i
= 0, count
= table
->chain_count
; i
< count
; ++i
)
1189 binding_entry temp
= table
->chain
[i
];
1190 while (temp
!= NULL
)
1192 binding_entry entry
= temp
;
1193 temp
= entry
->chain
;
1194 binding_entry_free (entry
);
1196 table
->chain
[i
] = NULL
;
1198 table
->entry_count
= 0;
1202 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */
1204 static inline binding_table
1205 binding_table_new (size_t chain_count
)
1207 binding_table table
= ggc_alloc
<binding_table_s
> ();
1208 table
->chain
= NULL
;
1209 binding_table_construct (table
, chain_count
);
1213 /* Expand TABLE to twice its current chain_count. */
1216 binding_table_expand (binding_table table
)
1218 const size_t old_chain_count
= table
->chain_count
;
1219 const size_t old_entry_count
= table
->entry_count
;
1220 const size_t new_chain_count
= 2 * old_chain_count
;
1221 binding_entry
*old_chains
= table
->chain
;
1224 binding_table_construct (table
, new_chain_count
);
1225 for (i
= 0; i
< old_chain_count
; ++i
)
1227 binding_entry entry
= old_chains
[i
];
1228 for (; entry
!= NULL
; entry
= old_chains
[i
])
1230 const unsigned int hash
= IDENTIFIER_HASH_VALUE (entry
->name
);
1231 const size_t j
= ENTRY_INDEX (hash
, new_chain_count
);
1233 old_chains
[i
] = entry
->chain
;
1234 entry
->chain
= table
->chain
[j
];
1235 table
->chain
[j
] = entry
;
1238 table
->entry_count
= old_entry_count
;
1241 /* Insert a binding for NAME to TYPE into TABLE. */
1244 binding_table_insert (binding_table table
, tree name
, tree type
)
1246 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1247 const size_t i
= ENTRY_INDEX (hash
, table
->chain_count
);
1248 binding_entry entry
= binding_entry_make (name
, type
);
1250 entry
->chain
= table
->chain
[i
];
1251 table
->chain
[i
] = entry
;
1252 ++table
->entry_count
;
1254 if (3 * table
->chain_count
< 5 * table
->entry_count
)
1255 binding_table_expand (table
);
1258 /* Return the binding_entry, if any, that maps NAME. */
1261 binding_table_find (binding_table table
, tree name
)
1263 const unsigned int hash
= IDENTIFIER_HASH_VALUE (name
);
1264 binding_entry entry
= table
->chain
[ENTRY_INDEX (hash
, table
->chain_count
)];
1266 while (entry
!= NULL
&& entry
->name
!= name
)
1267 entry
= entry
->chain
;
1272 /* Apply PROC -- with DATA -- to all entries in TABLE. */
1275 binding_table_foreach (binding_table table
, bt_foreach_proc proc
, void *data
)
1283 chain_count
= table
->chain_count
;
1284 for (i
= 0; i
< chain_count
; ++i
)
1286 binding_entry entry
= table
->chain
[i
];
1287 for (; entry
!= NULL
; entry
= entry
->chain
)
1292 #ifndef ENABLE_SCOPE_CHECKING
1293 # define ENABLE_SCOPE_CHECKING 0
1295 # define ENABLE_SCOPE_CHECKING 1
1298 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
1300 static GTY((deletable
)) cxx_binding
*free_bindings
;
1302 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
1306 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
1308 binding
->value
= value
;
1309 binding
->type
= type
;
1310 binding
->previous
= NULL
;
1313 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
1315 static cxx_binding
*
1316 cxx_binding_make (tree value
, tree type
)
1318 cxx_binding
*binding
;
1321 binding
= free_bindings
;
1322 free_bindings
= binding
->previous
;
1325 binding
= ggc_alloc
<cxx_binding
> ();
1327 cxx_binding_init (binding
, value
, type
);
1332 /* Put BINDING back on the free list. */
1335 cxx_binding_free (cxx_binding
*binding
)
1337 binding
->scope
= NULL
;
1338 binding
->previous
= free_bindings
;
1339 free_bindings
= binding
;
1342 /* Create a new binding for NAME (with the indicated VALUE and TYPE
1343 bindings) in the class scope indicated by SCOPE. */
1345 static cxx_binding
*
1346 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
1348 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
1349 cxx_binding
*binding
= cb
.base
;
1350 vec_safe_push (scope
->class_shadowed
, cb
);
1351 binding
->scope
= scope
;
1355 /* Make DECL the innermost binding for ID. The LEVEL is the binding
1356 level at which this declaration is being bound. */
1359 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
1361 cxx_binding
*binding
;
1363 if (level
!= class_binding_level
)
1365 binding
= cxx_binding_make (decl
, NULL_TREE
);
1366 binding
->scope
= level
;
1369 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
1371 /* Now, fill in the binding information. */
1372 binding
->previous
= IDENTIFIER_BINDING (id
);
1373 INHERITED_VALUE_BINDING_P (binding
) = 0;
1374 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
1376 /* And put it on the front of the list of bindings for ID. */
1377 IDENTIFIER_BINDING (id
) = binding
;
1380 /* Remove the binding for DECL which should be the innermost binding
1384 pop_local_binding (tree id
, tree decl
)
1386 cxx_binding
*binding
;
1388 if (id
== NULL_TREE
)
1389 /* It's easiest to write the loops that call this function without
1390 checking whether or not the entities involved have names. We
1391 get here for such an entity. */
1394 /* Get the innermost binding for ID. */
1395 binding
= IDENTIFIER_BINDING (id
);
1397 /* The name should be bound. */
1398 gcc_assert (binding
!= NULL
);
1400 /* The DECL will be either the ordinary binding or the type
1401 binding for this identifier. Remove that binding. */
1402 if (binding
->value
== decl
)
1403 binding
->value
= NULL_TREE
;
1406 gcc_assert (binding
->type
== decl
);
1407 binding
->type
= NULL_TREE
;
1410 if (!binding
->value
&& !binding
->type
)
1412 /* We're completely done with the innermost binding for this
1413 identifier. Unhook it from the list of bindings. */
1414 IDENTIFIER_BINDING (id
) = binding
->previous
;
1416 /* Add it to the free list. */
1417 cxx_binding_free (binding
);
1421 /* Remove the bindings for the decls of the current level and leave
1422 the current scope. */
1425 pop_bindings_and_leave_scope (void)
1427 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
1429 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
1430 tree name
= OVL_NAME (decl
);
1432 pop_local_binding (name
, decl
);
1438 /* Strip non dependent using declarations. If DECL is dependent,
1439 surreptitiously create a typename_type and return it. */
1442 strip_using_decl (tree decl
)
1444 if (decl
== NULL_TREE
)
1447 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
1448 decl
= USING_DECL_DECLS (decl
);
1450 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
1451 && USING_DECL_TYPENAME_P (decl
))
1453 /* We have found a type introduced by a using
1454 declaration at class scope that refers to a dependent
1457 using typename :: [opt] nested-name-specifier unqualified-id ;
1459 decl
= make_typename_type (TREE_TYPE (decl
),
1461 typename_type
, tf_error
);
1462 if (decl
!= error_mark_node
)
1463 decl
= TYPE_NAME (decl
);
1469 /* Return true if OVL is an overload for an anticipated builtin. */
1472 anticipated_builtin_p (tree ovl
)
1474 if (TREE_CODE (ovl
) != OVERLOAD
)
1477 if (!OVL_HIDDEN_P (ovl
))
1480 tree fn
= OVL_FUNCTION (ovl
);
1481 gcc_checking_assert (DECL_ANTICIPATED (fn
));
1483 if (DECL_HIDDEN_FRIEND_P (fn
))
1489 /* BINDING records an existing declaration for a name in the current scope.
1490 But, DECL is another declaration for that same identifier in the
1491 same scope. This is the `struct stat' hack whereby a non-typedef
1492 class name or enum-name can be bound at the same level as some other
1496 A class name (9.1) or enumeration name (7.2) can be hidden by the
1497 name of an object, function, or enumerator declared in the same scope.
1498 If a class or enumeration name and an object, function, or enumerator
1499 are declared in the same scope (in any order) with the same name, the
1500 class or enumeration name is hidden wherever the object, function, or
1501 enumerator name is visible.
1503 It's the responsibility of the caller to check that
1504 inserting this name is valid here. Returns nonzero if the new binding
1508 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
1510 tree bval
= binding
->value
;
1512 tree target_bval
= strip_using_decl (bval
);
1513 tree target_decl
= strip_using_decl (decl
);
1515 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
1516 && target_decl
!= target_bval
1517 && (TREE_CODE (target_bval
) != TYPE_DECL
1518 /* We allow pushing an enum multiple times in a class
1519 template in order to handle late matching of underlying
1520 type on an opaque-enum-declaration followed by an
1522 || (processing_template_decl
1523 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
1524 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
1525 && (dependent_type_p (ENUM_UNDERLYING_TYPE
1526 (TREE_TYPE (target_decl
)))
1527 || dependent_type_p (ENUM_UNDERLYING_TYPE
1528 (TREE_TYPE (target_bval
)))))))
1529 /* The new name is the type name. */
1530 binding
->type
= decl
;
1531 else if (/* TARGET_BVAL is null when push_class_level_binding moves
1532 an inherited type-binding out of the way to make room
1533 for a new value binding. */
1535 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
1536 has been used in a non-class scope prior declaration.
1537 In that case, we should have already issued a
1538 diagnostic; for graceful error recovery purpose, pretend
1539 this was the intended declaration for that name. */
1540 || target_bval
== error_mark_node
1541 /* If TARGET_BVAL is anticipated but has not yet been
1542 declared, pretend it is not there at all. */
1543 || anticipated_builtin_p (target_bval
))
1544 binding
->value
= decl
;
1545 else if (TREE_CODE (target_bval
) == TYPE_DECL
1546 && DECL_ARTIFICIAL (target_bval
)
1547 && target_decl
!= target_bval
1548 && (TREE_CODE (target_decl
) != TYPE_DECL
1549 || same_type_p (TREE_TYPE (target_decl
),
1550 TREE_TYPE (target_bval
))))
1552 /* The old binding was a type name. It was placed in
1553 VALUE field because it was thought, at the point it was
1554 declared, to be the only entity with such a name. Move the
1555 type name into the type slot; it is now hidden by the new
1557 binding
->type
= bval
;
1558 binding
->value
= decl
;
1559 binding
->value_is_inherited
= false;
1561 else if (TREE_CODE (target_bval
) == TYPE_DECL
1562 && TREE_CODE (target_decl
) == TYPE_DECL
1563 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
1564 && binding
->scope
->kind
!= sk_class
1565 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
1566 /* If either type involves template parameters, we must
1567 wait until instantiation. */
1568 || uses_template_parms (TREE_TYPE (target_decl
))
1569 || uses_template_parms (TREE_TYPE (target_bval
))))
1570 /* We have two typedef-names, both naming the same type to have
1571 the same name. In general, this is OK because of:
1575 In a given scope, a typedef specifier can be used to redefine
1576 the name of any type declared in that scope to refer to the
1577 type to which it already refers.
1579 However, in class scopes, this rule does not apply due to the
1580 stricter language in [class.mem] prohibiting redeclarations of
1583 /* There can be two block-scope declarations of the same variable,
1584 so long as they are `extern' declarations. However, there cannot
1585 be two declarations of the same static data member:
1589 A member shall not be declared twice in the
1590 member-specification. */
1591 else if (VAR_P (target_decl
)
1592 && VAR_P (target_bval
)
1593 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
1594 && !DECL_CLASS_SCOPE_P (target_decl
))
1596 duplicate_decls (decl
, binding
->value
, /*newdecl_is_friend=*/false);
1599 else if (TREE_CODE (decl
) == NAMESPACE_DECL
1600 && TREE_CODE (bval
) == NAMESPACE_DECL
1601 && DECL_NAMESPACE_ALIAS (decl
)
1602 && DECL_NAMESPACE_ALIAS (bval
)
1603 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
1604 /* [namespace.alias]
1606 In a declarative region, a namespace-alias-definition can be
1607 used to redefine a namespace-alias declared in that declarative
1608 region to refer only to the namespace to which it already
1611 else if (maybe_remove_implicit_alias (bval
))
1613 /* There was a mangling compatibility alias using this mangled name,
1614 but now we have a real decl that wants to use it instead. */
1615 binding
->value
= decl
;
1619 if (!error_operand_p (bval
))
1620 diagnose_name_conflict (decl
, bval
);
1627 /* Diagnose a name conflict between DECL and BVAL. */
1630 diagnose_name_conflict (tree decl
, tree bval
)
1632 if (TREE_CODE (decl
) == TREE_CODE (bval
)
1633 && (TREE_CODE (decl
) != TYPE_DECL
1634 || (DECL_ARTIFICIAL (decl
) && DECL_ARTIFICIAL (bval
))
1635 || (!DECL_ARTIFICIAL (decl
) && !DECL_ARTIFICIAL (bval
)))
1636 && !DECL_DECLARES_FUNCTION_P (decl
)
1637 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
1638 error ("redeclaration of %q#D", decl
);
1640 error ("%q#D conflicts with a previous declaration", decl
);
1642 inform (location_of (bval
), "previous declaration %q#D", bval
);
1645 /* Wrapper for supplement_binding_1. */
1648 supplement_binding (cxx_binding
*binding
, tree decl
)
1651 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1652 ret
= supplement_binding_1 (binding
, decl
);
1653 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1657 /* Replace BINDING's current value on its scope's name list with
1661 update_local_overload (cxx_binding
*binding
, tree newval
)
1665 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
1666 if (*d
== binding
->value
)
1668 /* Stitch new list node in. */
1669 *d
= tree_cons (NULL_TREE
, NULL_TREE
, TREE_CHAIN (*d
));
1672 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
1675 TREE_VALUE (*d
) = newval
;
1678 /* Compares the parameter-type-lists of ONE and TWO and
1679 returns false if they are different. If the DECLs are template
1680 functions, the return types and the template parameter lists are
1681 compared too (DR 565). */
1684 matching_fn_p (tree one
, tree two
)
1686 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
1687 TYPE_ARG_TYPES (TREE_TYPE (two
))))
1690 if (TREE_CODE (one
) == TEMPLATE_DECL
1691 && TREE_CODE (two
) == TEMPLATE_DECL
)
1693 /* Compare template parms. */
1694 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
1695 DECL_TEMPLATE_PARMS (two
)))
1698 /* And return type. */
1699 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
1700 TREE_TYPE (TREE_TYPE (two
))))
1707 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
1708 binding value (possibly with anticipated builtins stripped).
1709 Diagnose conflicts and return updated decl. */
1712 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
1713 tree old
, tree decl
, bool is_friend
)
1716 tree old_type
= slot
? MAYBE_STAT_TYPE (*slot
) : binding
->type
;
1717 tree to_type
= old_type
;
1719 gcc_assert (level
->kind
== sk_namespace
? !binding
1720 : level
->kind
!= sk_class
&& !slot
);
1721 if (old
== error_mark_node
)
1724 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1726 tree other
= to_type
;
1728 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
1731 /* Pushing an artificial typedef. See if this matches either
1732 the type slot or the old value slot. */
1735 else if (same_type_p (TREE_TYPE (other
), TREE_TYPE (decl
)))
1736 /* Two artificial decls to same type. Do nothing. */
1743 /* Slide decl into the type slot, keep old unaltered */
1750 if (old
&& TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
1752 /* Slide old into the type slot. */
1757 if (DECL_DECLARES_FUNCTION_P (decl
))
1761 else if (OVL_P (old
))
1763 for (ovl_iterator
iter (old
); iter
; ++iter
)
1767 if (iter
.using_p () && matching_fn_p (fn
, decl
))
1769 /* If a function declaration in namespace scope or
1770 block scope has the same name and the same
1771 parameter-type- list (8.3.5) as a function
1772 introduced by a using-declaration, and the
1773 declarations do not declare the same function,
1774 the program is ill-formed. [namespace.udecl]/14 */
1775 if (tree match
= duplicate_decls (decl
, fn
, is_friend
))
1778 /* FIXME: To preserve existing error behavior, we
1779 still push the decl. This might change. */
1780 diagnose_name_conflict (decl
, fn
);
1787 if (to_type
!= old_type
1789 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
1790 && !(DECL_IN_SYSTEM_HEADER (decl
)
1791 && DECL_IN_SYSTEM_HEADER (to_type
)))
1792 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
1795 to_val
= ovl_insert (decl
, old
);
1799 else if (TREE_CODE (old
) != TREE_CODE (decl
))
1800 /* Different kinds of decls conflict. */
1802 else if (TREE_CODE (old
) == TYPE_DECL
)
1804 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
1805 /* Two type decls to the same type. Do nothing. */
1810 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
1812 if (DECL_NAMESPACE_ALIAS (old
) && DECL_NAMESPACE_ALIAS (decl
)
1813 && ORIGINAL_NAMESPACE (old
) == ORIGINAL_NAMESPACE (decl
))
1814 /* In a declarative region, a namespace-alias-definition can be
1815 used to redefine a namespace-alias declared in that declarative
1816 region to refer only to the namespace to which it already
1817 refers. [namespace.alias] */
1822 else if (TREE_CODE (old
) == VAR_DECL
)
1824 /* There can be two block-scope declarations of the same
1825 variable, so long as they are `extern' declarations. */
1826 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
1828 else if (tree match
= duplicate_decls (decl
, old
, false))
1836 diagnose_name_conflict (decl
, old
);
1843 if (level
->kind
!= sk_namespace
1844 && !to_type
&& binding
->value
&& OVL_P (to_val
))
1845 update_local_overload (binding
, to_val
);
1848 tree to_add
= to_val
;
1850 if (level
->kind
== sk_namespace
)
1852 else if (to_type
== decl
)
1854 else if (TREE_CODE (to_add
) == OVERLOAD
)
1855 to_add
= build_tree_list (NULL_TREE
, to_add
);
1857 add_decl_to_level (level
, to_add
);
1862 if (STAT_HACK_P (*slot
))
1864 STAT_TYPE (*slot
) = to_type
;
1865 STAT_DECL (*slot
) = to_val
;
1868 *slot
= stat_hack (to_val
, to_type
);
1874 binding
->type
= to_type
;
1875 binding
->value
= to_val
;
1882 /* Map of identifiers to extern C functions (or LISTS thereof). */
1884 static GTY(()) hash_map
<lang_identifier
*, tree
> *extern_c_fns
;
1886 /* DECL has C linkage. If we have an existing instance, make sure it
1887 has the same exception specification [7.5, 7.6]. If there's no
1888 instance, add DECL to the map. */
1891 check_extern_c_conflict (tree decl
)
1893 /* Ignore artificial or system header decls. */
1894 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
1898 extern_c_fns
= hash_map
<lang_identifier
*,tree
>::create_ggc (127);
1901 tree
*slot
= &extern_c_fns
->get_or_insert (DECL_NAME (decl
), &existed
);
1907 if (TREE_CODE (old
) == TREE_LIST
)
1908 old
= TREE_VALUE (old
);
1911 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
1912 ; /* If they're in the same context, we'll have already complained
1913 about a (possible) mismatch, when inserting the decl. */
1914 else if (!decls_match (decl
, old
))
1916 else if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
1917 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
1920 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
1921 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
1925 pedwarn (input_location
, 0,
1926 "declaration of %q#D with C language linkage", decl
);
1927 pedwarn (DECL_SOURCE_LOCATION (old
), 0,
1928 "conflicts with previous declaration %q#D", old
);
1930 pedwarn (input_location
, 0,
1931 "due to different exception specifications");
1934 /* Chain it on for c_linkage_binding's use. */
1935 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
1939 /* Returns a list of C-linkage decls with the name NAME. Used in
1940 c-family/c-pragma.c to implement redefine_extname pragma. */
1943 c_linkage_bindings (tree name
)
1946 if (tree
*slot
= extern_c_fns
->get (name
))
1951 /* DECL is being declared at a local scope. Emit suitable shadow
1955 check_local_shadow (tree decl
)
1957 /* Don't complain about the parms we push and then pop
1958 while tentatively parsing a function declarator. */
1959 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
1962 /* Inline decls shadow nothing. */
1963 if (DECL_FROM_INLINE (decl
))
1966 /* External decls are something else. */
1967 if (DECL_EXTERNAL (decl
))
1970 tree old
= NULL_TREE
;
1971 cp_binding_level
*old_scope
= NULL
;
1972 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
1974 old
= binding
->value
;
1975 old_scope
= binding
->scope
;
1977 while (old
&& VAR_P (old
) && DECL_DEAD_FOR_LOCAL (old
))
1978 old
= DECL_SHADOWED_FOR_VAR (old
);
1980 tree shadowed
= NULL_TREE
;
1982 && (TREE_CODE (old
) == PARM_DECL
1984 || (TREE_CODE (old
) == TYPE_DECL
1985 && (!DECL_ARTIFICIAL (old
)
1986 || TREE_CODE (decl
) == TYPE_DECL
)))
1987 && (!DECL_ARTIFICIAL (decl
)
1988 || DECL_IMPLICIT_TYPEDEF_P (decl
)
1989 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
1991 /* DECL shadows a local thing possibly of interest. */
1993 /* Don't complain if it's from an enclosing function. */
1994 if (DECL_CONTEXT (old
) == current_function_decl
1995 && TREE_CODE (decl
) != PARM_DECL
1996 && TREE_CODE (old
) == PARM_DECL
)
1998 /* Go to where the parms should be and see if we find
2000 cp_binding_level
*b
= current_binding_level
->level_chain
;
2002 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
2003 /* Skip the ctor/dtor cleanup level. */
2007 if (b
->kind
== sk_function_parms
)
2009 error ("declaration of %q#D shadows a parameter", decl
);
2014 /* The local structure or class can't use parameters of
2015 the containing function anyway. */
2016 if (DECL_CONTEXT (old
) != current_function_decl
)
2018 for (cp_binding_level
*scope
= current_binding_level
;
2019 scope
!= old_scope
; scope
= scope
->level_chain
)
2020 if (scope
->kind
== sk_class
2021 && !LAMBDA_TYPE_P (scope
->this_entity
))
2024 /* Error if redeclaring a local declared in a
2025 init-statement or in the condition of an if or
2026 switch statement when the new declaration is in the
2027 outermost block of the controlled statement.
2028 Redeclaring a variable from a for or while condition is
2029 detected elsewhere. */
2030 else if (VAR_P (old
)
2031 && old_scope
== current_binding_level
->level_chain
2032 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
2034 error ("redeclaration of %q#D", decl
);
2035 inform (DECL_SOURCE_LOCATION (old
),
2036 "%q#D previously declared here", old
);
2040 3.3.3/3: The name declared in an exception-declaration (...)
2041 shall not be redeclared in the outermost block of the handler.
2042 3.3.3/2: A parameter name shall not be redeclared (...) in
2043 the outermost block of any handler associated with a
2045 3.4.1/15: The function parameter names shall not be redeclared
2046 in the exception-declaration nor in the outermost block of a
2047 handler for the function-try-block. */
2048 else if ((TREE_CODE (old
) == VAR_DECL
2049 && old_scope
== current_binding_level
->level_chain
2050 && old_scope
->kind
== sk_catch
)
2051 || (TREE_CODE (old
) == PARM_DECL
2052 && (current_binding_level
->kind
== sk_catch
2053 || current_binding_level
->level_chain
->kind
== sk_catch
)
2054 && in_function_try_handler
))
2056 if (permerror (input_location
, "redeclaration of %q#D", decl
))
2057 inform (DECL_SOURCE_LOCATION (old
),
2058 "%q#D previously declared here", old
);
2062 /* If '-Wshadow=compatible-local' is specified without other
2063 -Wshadow= flags, we will warn only when the type of the
2064 shadowing variable (DECL) can be converted to that of the
2065 shadowed parameter (OLD_LOCAL). The reason why we only check
2066 if DECL's type can be converted to OLD_LOCAL's type (but not the
2067 other way around) is because when users accidentally shadow a
2068 parameter, more than often they would use the variable
2069 thinking (mistakenly) it's still the parameter. It would be
2070 rare that users would use the variable in the place that
2071 expects the parameter but thinking it's a new decl. */
2073 enum opt_code warning_code
;
2075 warning_code
= OPT_Wshadow
;
2076 else if (warn_shadow_local
)
2077 warning_code
= OPT_Wshadow_local
;
2078 else if (warn_shadow_compatible_local
2079 && can_convert (TREE_TYPE (old
), TREE_TYPE (decl
), tf_none
))
2080 warning_code
= OPT_Wshadow_compatible_local
;
2085 if (TREE_CODE (old
) == PARM_DECL
)
2086 msg
= "declaration of %q#D shadows a parameter";
2087 else if (is_capture_proxy (old
))
2088 msg
= "declaration of %qD shadows a lambda capture";
2090 msg
= "declaration of %qD shadows a previous local";
2092 if (warning_at (input_location
, warning_code
, msg
, decl
))
2095 goto inform_shadowed
;
2103 /* Don't warn for artificial things that are not implicit typedefs. */
2104 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
2107 if (nonlambda_method_basetype ())
2108 if (tree member
= lookup_member (current_nonlambda_class_type (),
2109 DECL_NAME (decl
), /*protect=*/0,
2110 /*want_type=*/false, tf_warning_or_error
))
2112 member
= MAYBE_BASELINK_FUNCTIONS (member
);
2114 /* Warn if a variable shadows a non-function, or the variable
2115 is a function or a pointer-to-function. */
2117 || TREE_CODE (decl
) == FUNCTION_DECL
2118 || TYPE_PTRFN_P (TREE_TYPE (decl
))
2119 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
2121 if (warning_at (input_location
, OPT_Wshadow
,
2122 "declaration of %qD shadows a member of %qT",
2123 decl
, current_nonlambda_class_type ())
2127 goto inform_shadowed
;
2133 /* Now look for a namespace shadow. */
2134 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2137 || (TREE_CODE (old
) == TYPE_DECL
2138 && (!DECL_ARTIFICIAL (old
)
2139 || TREE_CODE (decl
) == TYPE_DECL
)))
2140 && !instantiating_current_function_p ())
2141 /* XXX shadow warnings in outer-more namespaces */
2143 if (warning_at (input_location
, OPT_Wshadow
,
2144 "declaration of %qD shadows a global declaration",
2148 goto inform_shadowed
;
2156 inform (DECL_SOURCE_LOCATION (shadowed
), "shadowed declaration is here");
2159 /* DECL is being pushed inside function CTX. Set its context, if
2163 set_decl_context_in_fn (tree ctx
, tree decl
)
2165 if (!DECL_CONTEXT (decl
)
2166 /* A local declaration for a function doesn't constitute
2168 && TREE_CODE (decl
) != FUNCTION_DECL
2169 /* A local declaration for an `extern' variable is in the
2170 scope of the current namespace, not the current
2172 && !(VAR_P (decl
) && DECL_EXTERNAL (decl
))
2173 /* When parsing the parameter list of a function declarator,
2174 don't set DECL_CONTEXT to an enclosing function. When we
2175 push the PARM_DECLs in order to process the function body,
2176 current_binding_level->this_entity will be set. */
2177 && !(TREE_CODE (decl
) == PARM_DECL
2178 && current_binding_level
->kind
== sk_function_parms
2179 && current_binding_level
->this_entity
== NULL
))
2180 DECL_CONTEXT (decl
) = ctx
;
2182 /* If this is the declaration for a namespace-scope function,
2183 but the declaration itself is in a local scope, mark the
2185 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (decl
))
2186 DECL_LOCAL_FUNCTION_P (decl
) = 1;
2189 /* DECL is a local-scope decl with linkage. SHADOWED is true if the
2190 name is already bound at the current level.
2192 [basic.link] If there is a visible declaration of an entity with
2193 linkage having the same name and type, ignoring entities declared
2194 outside the innermost enclosing namespace scope, the block scope
2195 declaration declares that same entity and receives the linkage of
2196 the previous declaration.
2198 Also, make sure that this decl matches any existing external decl
2199 in the enclosing namespace. */
2202 set_local_extern_decl_linkage (tree decl
, bool shadowed
)
2204 tree ns_value
= decl
; /* Unique marker. */
2208 tree loc_value
= innermost_non_namespace_value (DECL_NAME (decl
));
2212 = find_namespace_value (current_namespace
, DECL_NAME (decl
));
2213 loc_value
= ns_value
;
2215 if (loc_value
== error_mark_node
)
2216 loc_value
= NULL_TREE
;
2218 for (ovl_iterator
iter (loc_value
); iter
; ++iter
)
2219 if (!iter
.hidden_p ()
2220 && (TREE_STATIC (*iter
) || DECL_EXTERNAL (*iter
))
2221 && decls_match (*iter
, decl
))
2223 /* The standard only says that the local extern inherits
2224 linkage from the previous decl; in particular, default
2225 args are not shared. Add the decl into a hash table to
2226 make sure only the previous decl in this case is seen
2227 by the middle end. */
2228 struct cxx_int_tree_map
*h
;
2230 /* We inherit the outer decl's linkage. But we're a
2232 TREE_PUBLIC (decl
) = TREE_PUBLIC (*iter
);
2234 if (cp_function_chain
->extern_decl_map
== NULL
)
2235 cp_function_chain
->extern_decl_map
2236 = hash_table
<cxx_int_tree_map_hasher
>::create_ggc (20);
2238 h
= ggc_alloc
<cxx_int_tree_map
> ();
2239 h
->uid
= DECL_UID (decl
);
2241 cxx_int_tree_map
**loc
= cp_function_chain
->extern_decl_map
2242 ->find_slot (h
, INSERT
);
2248 if (TREE_PUBLIC (decl
))
2250 /* DECL is externally visible. Make sure it matches a matching
2251 decl in the namespace scope. We only really need to check
2252 this when inserting the decl, not when we find an existing
2253 match in the current scope. However, in practice we're
2254 going to be inserting a new decl in the majority of cases --
2255 who writes multiple extern decls for the same thing in the
2256 same local scope? Doing it here often avoids a duplicate
2257 namespace lookup. */
2259 /* Avoid repeating a lookup. */
2260 if (ns_value
== decl
)
2261 ns_value
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2263 if (ns_value
== error_mark_node
)
2264 ns_value
= NULL_TREE
;
2266 for (ovl_iterator
iter (ns_value
); iter
; ++iter
)
2270 if (!(TREE_PUBLIC (other
) || DECL_EXTERNAL (other
)))
2271 ; /* Not externally visible. */
2272 else if (DECL_EXTERN_C_P (decl
) && DECL_EXTERN_C_P (other
))
2273 ; /* Both are extern "C", we'll check via that mechanism. */
2274 else if (TREE_CODE (other
) != TREE_CODE (decl
)
2275 || ((VAR_P (decl
) || matching_fn_p (other
, decl
))
2276 && !comptypes (TREE_TYPE (decl
), TREE_TYPE (other
),
2277 COMPARE_REDECLARATION
)))
2279 if (permerror (DECL_SOURCE_LOCATION (decl
),
2280 "local external declaration %q#D", decl
))
2281 inform (DECL_SOURCE_LOCATION (other
),
2282 "does not match previous declaration %q#D", other
);
2289 /* Record DECL as belonging to the current lexical scope. Check for
2290 errors (such as an incompatible declaration for the same name
2291 already seen in the same scope). IS_FRIEND is true if DECL is
2292 declared as a friend.
2294 Returns either DECL or an old decl for the same name. If an old
2295 decl is returned, it may have been smashed to agree with what DECL
2299 do_pushdecl (tree decl
, bool is_friend
)
2301 if (decl
== error_mark_node
)
2302 return error_mark_node
;
2304 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
)
2305 set_decl_context_in_fn (current_function_decl
, decl
);
2307 /* The binding level we will be pushing into. During local class
2308 pushing, we want to push to the containing scope. */
2309 cp_binding_level
*level
= current_binding_level
;
2310 while (level
->kind
== sk_class
)
2311 level
= level
->level_chain
;
2313 if (tree name
= DECL_NAME (decl
))
2315 cxx_binding
*binding
= NULL
; /* Local scope binding. */
2316 tree ns
= NULL_TREE
; /* Searched namespace. */
2317 tree
*slot
= NULL
; /* Binding slot in namespace. */
2318 tree old
= NULL_TREE
;
2320 if (level
->kind
== sk_namespace
)
2322 /* We look in the decl's namespace for an existing
2323 declaration, even though we push into the current
2325 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
2326 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
2327 /* Create the binding, if this is current namespace, because
2328 that's where we'll be pushing anyway. */
2329 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
2331 old
= MAYBE_STAT_DECL (*slot
);
2335 binding
= find_local_binding (level
, name
);
2337 old
= binding
->value
;
2340 if (current_function_decl
&& VAR_OR_FUNCTION_DECL_P (decl
)
2341 && DECL_EXTERNAL (decl
))
2342 set_local_extern_decl_linkage (decl
, old
!= NULL_TREE
);
2344 if (old
== error_mark_node
)
2347 for (ovl_iterator
iter (old
); iter
; ++iter
)
2348 if (iter
.using_p ())
2349 ; /* Ignore using decls here. */
2350 else if (tree match
= duplicate_decls (decl
, *iter
, is_friend
))
2352 if (iter
.hidden_p ()
2353 && match
!= error_mark_node
2354 && !DECL_HIDDEN_P (match
))
2356 /* Unhiding a previously hidden decl. */
2357 tree head
= iter
.reveal_node (old
);
2362 update_local_overload (binding
, head
);
2363 binding
->value
= head
;
2365 else if (STAT_HACK_P (*slot
))
2366 STAT_DECL (*slot
) = head
;
2370 if (TREE_CODE (match
) == FUNCTION_DECL
2371 && DECL_EXTERN_C_P (match
))
2372 /* We need to check and register the fn now. */
2373 check_extern_c_conflict (match
);
2378 /* We are pushing a new decl. */
2380 /* Skip a hidden builtin we failed to match already. There can
2382 if (old
&& anticipated_builtin_p (old
))
2383 old
= OVL_CHAIN (old
);
2385 check_template_shadow (decl
);
2387 if (DECL_DECLARES_FUNCTION_P (decl
))
2389 check_default_args (decl
);
2393 if (level
->kind
!= sk_namespace
)
2394 /* In a local class, a friend function declaration must
2395 find a matching decl in the innermost non-class scope.
2396 [class.friend/11] */
2397 error ("friend declaration %qD in local class without "
2398 "prior local declaration", decl
);
2399 else if (!flag_friend_injection
)
2400 /* Hide it from ordinary lookup. */
2401 DECL_ANTICIPATED (decl
) = DECL_HIDDEN_FRIEND_P (decl
) = true;
2405 if (level
->kind
!= sk_namespace
)
2407 check_local_shadow (decl
);
2409 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
2410 /* A local namespace alias. */
2411 set_identifier_type_value (name
, NULL_TREE
);
2414 binding
= create_local_binding (level
, name
);
2418 ns
= current_namespace
;
2419 slot
= find_namespace_slot (ns
, name
, true);
2422 old
= update_binding (level
, binding
, slot
, old
, decl
, is_friend
);
2425 /* An existing decl matched, use it. */
2427 else if (TREE_CODE (decl
) == TYPE_DECL
)
2429 tree type
= TREE_TYPE (decl
);
2431 if (type
!= error_mark_node
)
2433 if (TYPE_NAME (type
) != decl
)
2434 set_underlying_type (decl
);
2437 set_identifier_type_value_with_scope (name
, decl
, level
);
2439 SET_IDENTIFIER_TYPE_VALUE (name
, global_type_node
);
2442 /* If this is a locally defined typedef in a function that
2443 is not a template instantation, record it to implement
2444 -Wunused-local-typedefs. */
2445 if (!instantiating_current_function_p ())
2446 record_locally_defined_typedef (decl
);
2448 else if (VAR_P (decl
))
2449 maybe_register_incomplete_var (decl
);
2450 else if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_EXTERN_C_P (decl
))
2451 check_extern_c_conflict (decl
);
2454 add_decl_to_level (level
, decl
);
2459 /* Record a decl-node X as belonging to the current lexical scope.
2460 It's a friend if IS_FRIEND is true -- which affects exactly where
2464 pushdecl (tree x
, bool is_friend
)
2466 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2467 tree ret
= do_pushdecl (x
, is_friend
);
2468 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2472 /* Enter DECL into the symbol table, if that's appropriate. Returns
2473 DECL, or a modified version thereof. */
2476 maybe_push_decl (tree decl
)
2478 tree type
= TREE_TYPE (decl
);
2480 /* Add this decl to the current binding level, but not if it comes
2481 from another scope, e.g. a static member variable. TEM may equal
2482 DECL or it may be a previous decl of the same name. */
2483 if (decl
== error_mark_node
2484 || (TREE_CODE (decl
) != PARM_DECL
2485 && DECL_CONTEXT (decl
) != NULL_TREE
2486 /* Definitions of namespace members outside their namespace are
2488 && !DECL_NAMESPACE_SCOPE_P (decl
))
2489 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
2490 || type
== unknown_type_node
2491 /* The declaration of a template specialization does not affect
2492 the functions available for overload resolution, so we do not
2494 || (TREE_CODE (decl
) == FUNCTION_DECL
2495 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
2498 return pushdecl (decl
);
2501 /* Bind DECL to ID in the current_binding_level, assumed to be a local
2502 binding level. If IS_USING is true, DECL got here through a
2503 using-declaration. */
2506 push_local_binding (tree id
, tree decl
, bool is_using
)
2508 /* Skip over any local classes. This makes sense if we call
2509 push_local_binding with a friend decl of a local class. */
2510 cp_binding_level
*b
= innermost_nonclass_level ();
2512 gcc_assert (b
->kind
!= sk_namespace
);
2513 if (find_local_binding (b
, id
))
2515 /* Supplement the existing binding. */
2516 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
2517 /* It didn't work. Something else must be bound at this
2518 level. Do not add DECL to the list of things to pop
2523 /* Create a new binding. */
2524 push_binding (id
, decl
, b
);
2526 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
2527 /* We must put the OVERLOAD or using into a TREE_LIST since we
2528 cannot use the decl's chain itself. */
2529 decl
= build_tree_list (NULL_TREE
, decl
);
2531 /* And put DECL on the list of things declared by the current
2533 add_decl_to_level (b
, decl
);
2536 /* Check to see whether or not DECL is a variable that would have been
2537 in scope under the ARM, but is not in scope under the ANSI/ISO
2538 standard. If so, issue an error message. If name lookup would
2539 work in both cases, but return a different result, this function
2540 returns the result of ANSI/ISO lookup. Otherwise, it returns
2544 check_for_out_of_scope_variable (tree decl
)
2548 /* We only care about out of scope variables. */
2549 if (!(VAR_P (decl
) && DECL_DEAD_FOR_LOCAL (decl
)))
2552 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (decl
)
2553 ? DECL_SHADOWED_FOR_VAR (decl
) : NULL_TREE
;
2554 while (shadowed
!= NULL_TREE
&& VAR_P (shadowed
)
2555 && DECL_DEAD_FOR_LOCAL (shadowed
))
2556 shadowed
= DECL_HAS_SHADOWED_FOR_VAR_P (shadowed
)
2557 ? DECL_SHADOWED_FOR_VAR (shadowed
) : NULL_TREE
;
2559 shadowed
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
2562 if (!DECL_ERROR_REPORTED (decl
))
2564 warning (0, "name lookup of %qD changed", DECL_NAME (decl
));
2565 warning_at (DECL_SOURCE_LOCATION (shadowed
), 0,
2566 " matches this %qD under ISO standard rules",
2568 warning_at (DECL_SOURCE_LOCATION (decl
), 0,
2569 " matches this %qD under old rules", decl
);
2570 DECL_ERROR_REPORTED (decl
) = 1;
2575 /* If we have already complained about this declaration, there's no
2576 need to do it again. */
2577 if (DECL_ERROR_REPORTED (decl
))
2580 DECL_ERROR_REPORTED (decl
) = 1;
2582 if (TREE_TYPE (decl
) == error_mark_node
)
2585 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl
)))
2587 error ("name lookup of %qD changed for ISO %<for%> scoping",
2589 error (" cannot use obsolete binding at %q+D because "
2590 "it has a destructor", decl
);
2591 return error_mark_node
;
2595 permerror (input_location
, "name lookup of %qD changed for ISO %<for%> scoping",
2597 if (flag_permissive
)
2598 permerror (DECL_SOURCE_LOCATION (decl
),
2599 " using obsolete binding at %qD", decl
);
2605 inform (input_location
, "(if you use %<-fpermissive%> G++ will accept your code)");
2614 /* true means unconditionally make a BLOCK for the next level pushed. */
2616 static bool keep_next_level_flag
;
2618 static int binding_depth
= 0;
2625 for (i
= 0; i
< depth
* 2; i
++)
2629 /* Return a string describing the kind of SCOPE we have. */
2631 cp_binding_level_descriptor (cp_binding_level
*scope
)
2633 /* The order of this table must match the "scope_kind"
2635 static const char* scope_kind_names
[] = {
2641 "function-parameter-scope",
2644 "template-parameter-scope",
2645 "template-explicit-spec-scope"
2647 const scope_kind kind
= scope
->explicit_spec_p
2648 ? sk_template_spec
: scope
->kind
;
2650 return scope_kind_names
[kind
];
2653 /* Output a debugging information about SCOPE when performing
2656 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
2658 const char *desc
= cp_binding_level_descriptor (scope
);
2659 if (scope
->this_entity
)
2660 verbatim ("%s %<%s(%E)%> %p %d\n", action
, desc
,
2661 scope
->this_entity
, (void *) scope
, line
);
2663 verbatim ("%s %s %p %d\n", action
, desc
, (void *) scope
, line
);
2666 /* Return the estimated initial size of the hashtable of a NAMESPACE
2669 static inline size_t
2670 namespace_scope_ht_size (tree ns
)
2672 tree name
= DECL_NAME (ns
);
2674 return name
== std_identifier
2675 ? NAMESPACE_STD_HT_SIZE
2676 : (name
== global_identifier
2677 ? GLOBAL_SCOPE_HT_SIZE
2678 : NAMESPACE_ORDINARY_HT_SIZE
);
2681 /* A chain of binding_level structures awaiting reuse. */
2683 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
2685 /* Insert SCOPE as the innermost binding level. */
2688 push_binding_level (cp_binding_level
*scope
)
2690 /* Add it to the front of currently active scopes stack. */
2691 scope
->level_chain
= current_binding_level
;
2692 current_binding_level
= scope
;
2693 keep_next_level_flag
= false;
2695 if (ENABLE_SCOPE_CHECKING
)
2697 scope
->binding_depth
= binding_depth
;
2698 indent (binding_depth
);
2699 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
2705 /* Create a new KIND scope and make it the top of the active scopes stack.
2706 ENTITY is the scope of the associated C++ entity (namespace, class,
2707 function, C++0x enumeration); it is NULL otherwise. */
2710 begin_scope (scope_kind kind
, tree entity
)
2712 cp_binding_level
*scope
;
2714 /* Reuse or create a struct for this binding level. */
2715 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
2717 scope
= free_binding_level
;
2718 free_binding_level
= scope
->level_chain
;
2719 memset (scope
, 0, sizeof (cp_binding_level
));
2722 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
2724 scope
->this_entity
= entity
;
2725 scope
->more_cleanups_ok
= true;
2732 case sk_template_spec
:
2733 scope
->explicit_spec_p
= true;
2734 kind
= sk_template_parms
;
2736 case sk_template_parms
:
2743 case sk_scoped_enum
:
2744 case sk_function_parms
:
2745 case sk_transaction
:
2747 scope
->keep
= keep_next_level_flag
;
2751 NAMESPACE_LEVEL (entity
) = scope
;
2755 /* Should not happen. */
2761 push_binding_level (scope
);
2766 /* We're about to leave current scope. Pop the top of the stack of
2767 currently active scopes. Return the enclosing scope, now active. */
2772 cp_binding_level
*scope
= current_binding_level
;
2774 if (scope
->kind
== sk_namespace
&& class_binding_level
)
2775 current_binding_level
= class_binding_level
;
2777 /* We cannot leave a scope, if there are none left. */
2778 if (NAMESPACE_LEVEL (global_namespace
))
2779 gcc_assert (!global_scope_p (scope
));
2781 if (ENABLE_SCOPE_CHECKING
)
2783 indent (--binding_depth
);
2784 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
2788 /* Move one nesting level up. */
2789 current_binding_level
= scope
->level_chain
;
2791 /* Namespace-scopes are left most probably temporarily, not
2792 completely; they can be reopened later, e.g. in namespace-extension
2793 or any name binding activity that requires us to resume a
2794 namespace. For classes, we cache some binding levels. For other
2795 scopes, we just make the structure available for reuse. */
2796 if (scope
->kind
!= sk_namespace
2797 && scope
->kind
!= sk_class
)
2799 scope
->level_chain
= free_binding_level
;
2800 gcc_assert (!ENABLE_SCOPE_CHECKING
2801 || scope
->binding_depth
== binding_depth
);
2802 free_binding_level
= scope
;
2805 if (scope
->kind
== sk_class
)
2807 /* Reset DEFINING_CLASS_P to allow for reuse of a
2808 class-defining scope in a non-defining context. */
2809 scope
->defining_class_p
= 0;
2811 /* Find the innermost enclosing class scope, and reset
2812 CLASS_BINDING_LEVEL appropriately. */
2813 class_binding_level
= NULL
;
2814 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
2815 if (scope
->kind
== sk_class
)
2817 class_binding_level
= scope
;
2822 return current_binding_level
;
2826 resume_scope (cp_binding_level
* b
)
2828 /* Resuming binding levels is meant only for namespaces,
2829 and those cannot nest into classes. */
2830 gcc_assert (!class_binding_level
);
2831 /* Also, resuming a non-directly nested namespace is a no-no. */
2832 gcc_assert (b
->level_chain
== current_binding_level
);
2833 current_binding_level
= b
;
2834 if (ENABLE_SCOPE_CHECKING
)
2836 b
->binding_depth
= binding_depth
;
2837 indent (binding_depth
);
2838 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
2843 /* Return the innermost binding level that is not for a class scope. */
2845 static cp_binding_level
*
2846 innermost_nonclass_level (void)
2848 cp_binding_level
*b
;
2850 b
= current_binding_level
;
2851 while (b
->kind
== sk_class
)
2857 /* We're defining an object of type TYPE. If it needs a cleanup, but
2858 we're not allowed to add any more objects with cleanups to the current
2859 scope, create a new binding level. */
2862 maybe_push_cleanup_level (tree type
)
2864 if (type
!= error_mark_node
2865 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
2866 && current_binding_level
->more_cleanups_ok
== 0)
2868 begin_scope (sk_cleanup
, NULL
);
2869 current_binding_level
->statement_list
= push_stmt_list ();
2873 /* Return true if we are in the global binding level. */
2876 global_bindings_p (void)
2878 return global_scope_p (current_binding_level
);
2881 /* True if we are currently in a toplevel binding level. This
2882 means either the global binding level or a namespace in a toplevel
2883 binding level. Since there are no non-toplevel namespace levels,
2884 this really means any namespace or template parameter level. We
2885 also include a class whose context is toplevel. */
2888 toplevel_bindings_p (void)
2890 cp_binding_level
*b
= innermost_nonclass_level ();
2892 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
2895 /* True if this is a namespace scope, or if we are defining a class
2896 which is itself at namespace scope, or whose enclosing class is
2897 such a class, etc. */
2900 namespace_bindings_p (void)
2902 cp_binding_level
*b
= innermost_nonclass_level ();
2904 return b
->kind
== sk_namespace
;
2907 /* True if the innermost non-class scope is a block scope. */
2910 local_bindings_p (void)
2912 cp_binding_level
*b
= innermost_nonclass_level ();
2913 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
2916 /* True if the current level needs to have a BLOCK made. */
2921 return (current_binding_level
->blocks
!= NULL_TREE
2922 || current_binding_level
->keep
2923 || current_binding_level
->kind
== sk_cleanup
2924 || current_binding_level
->names
!= NULL_TREE
2925 || current_binding_level
->using_directives
);
2928 /* Returns the kind of the innermost scope. */
2931 innermost_scope_kind (void)
2933 return current_binding_level
->kind
;
2936 /* Returns true if this scope was created to store template parameters. */
2939 template_parm_scope_p (void)
2941 return innermost_scope_kind () == sk_template_parms
;
2944 /* If KEEP is true, make a BLOCK node for the next binding level,
2945 unconditionally. Otherwise, use the normal logic to decide whether
2946 or not to create a BLOCK. */
2949 keep_next_level (bool keep
)
2951 keep_next_level_flag
= keep
;
2954 /* Return the list of declarations of the current local scope. */
2957 get_local_decls (void)
2959 gcc_assert (current_binding_level
->kind
!= sk_namespace
2960 && current_binding_level
->kind
!= sk_class
);
2961 return current_binding_level
->names
;
2964 /* Return how many function prototypes we are currently nested inside. */
2967 function_parm_depth (void)
2970 cp_binding_level
*b
;
2972 for (b
= current_binding_level
;
2973 b
->kind
== sk_function_parms
;
2980 /* For debugging. */
2981 static int no_print_functions
= 0;
2982 static int no_print_builtins
= 0;
2985 print_binding_level (cp_binding_level
* lvl
)
2989 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
2990 if (lvl
->more_cleanups_ok
)
2991 fprintf (stderr
, " more-cleanups-ok");
2992 if (lvl
->have_cleanups
)
2993 fprintf (stderr
, " have-cleanups");
2994 fprintf (stderr
, "\n");
2997 fprintf (stderr
, " names:\t");
2998 /* We can probably fit 3 names to a line? */
2999 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
3001 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
3003 if (no_print_builtins
3004 && (TREE_CODE (t
) == TYPE_DECL
)
3005 && DECL_IS_BUILTIN (t
))
3008 /* Function decls tend to have longer names. */
3009 if (TREE_CODE (t
) == FUNCTION_DECL
)
3016 fprintf (stderr
, "\n\t");
3019 print_node_brief (stderr
, "", t
, 0);
3020 if (t
== error_mark_node
)
3024 fprintf (stderr
, "\n");
3026 if (vec_safe_length (lvl
->class_shadowed
))
3029 cp_class_binding
*b
;
3030 fprintf (stderr
, " class-shadowed:");
3031 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
3032 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
3033 fprintf (stderr
, "\n");
3035 if (lvl
->type_shadowed
)
3037 fprintf (stderr
, " type-shadowed:");
3038 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
3040 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
3042 fprintf (stderr
, "\n");
3047 debug (cp_binding_level
&ref
)
3049 print_binding_level (&ref
);
3053 debug (cp_binding_level
*ptr
)
3058 fprintf (stderr
, "<nil>\n");
3063 print_other_binding_stack (cp_binding_level
*stack
)
3065 cp_binding_level
*level
;
3066 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
3068 fprintf (stderr
, "binding level %p\n", (void *) level
);
3069 print_binding_level (level
);
3074 print_binding_stack (void)
3076 cp_binding_level
*b
;
3077 fprintf (stderr
, "current_binding_level=%p\n"
3078 "class_binding_level=%p\n"
3079 "NAMESPACE_LEVEL (global_namespace)=%p\n",
3080 (void *) current_binding_level
, (void *) class_binding_level
,
3081 (void *) NAMESPACE_LEVEL (global_namespace
));
3082 if (class_binding_level
)
3084 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
3085 if (b
== current_binding_level
)
3088 b
= class_binding_level
;
3090 b
= current_binding_level
;
3093 b
= current_binding_level
;
3094 print_other_binding_stack (b
);
3095 fprintf (stderr
, "global:\n");
3096 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
3099 /* Return the type associated with ID. */
3102 identifier_type_value_1 (tree id
)
3104 /* There is no type with that name, anywhere. */
3105 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
3107 /* This is not the type marker, but the real thing. */
3108 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
3109 return REAL_IDENTIFIER_TYPE_VALUE (id
);
3110 /* Have to search for it. It must be on the global level, now.
3111 Ask lookup_name not to return non-types. */
3112 id
= lookup_name_real (id
, 2, 1, /*block_p=*/true, 0, 0);
3114 return TREE_TYPE (id
);
3118 /* Wrapper for identifier_type_value_1. */
3121 identifier_type_value (tree id
)
3124 timevar_start (TV_NAME_LOOKUP
);
3125 ret
= identifier_type_value_1 (id
);
3126 timevar_stop (TV_NAME_LOOKUP
);
3131 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
3132 the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */
3135 identifier_global_value (tree t
)
3137 return IDENTIFIER_GLOBAL_VALUE (t
);
3140 /* Push a definition of struct, union or enum tag named ID. into
3141 binding_level B. DECL is a TYPE_DECL for the type. We assume that
3142 the tag ID is not already defined. */
3145 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
3149 if (b
->kind
!= sk_namespace
)
3151 /* Shadow the marker, not the real thing, so that the marker
3152 gets restored later. */
3153 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
3155 = tree_cons (id
, old_type_value
, b
->type_shadowed
);
3156 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
3157 TREE_TYPE (b
->type_shadowed
) = type
;
3161 tree
*slot
= find_namespace_slot (current_namespace
, id
, true);
3163 update_binding (b
, NULL
, slot
, MAYBE_STAT_DECL (*slot
), decl
, false);
3165 /* Store marker instead of real type. */
3166 type
= global_type_node
;
3168 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
3171 /* As set_identifier_type_value_with_scope, but using
3172 current_binding_level. */
3175 set_identifier_type_value (tree id
, tree decl
)
3177 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
3180 /* Return the name for the constructor (or destructor) for the
3181 specified class TYPE. When given a template, this routine doesn't
3182 lose the specialization. */
3185 constructor_name_full (tree type
)
3187 return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type
));
3190 /* Return the name for the constructor (or destructor) for the
3191 specified class. When given a template, return the plain
3192 unspecialized name. */
3195 constructor_name (tree type
)
3198 name
= constructor_name_full (type
);
3199 if (IDENTIFIER_TEMPLATE (name
))
3200 name
= IDENTIFIER_TEMPLATE (name
);
3204 /* Returns TRUE if NAME is the name for the constructor for TYPE,
3205 which must be a class type. */
3208 constructor_name_p (tree name
, tree type
)
3212 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
3217 if (!identifier_p (name
))
3220 /* These don't have names. */
3221 if (TREE_CODE (type
) == DECLTYPE_TYPE
3222 || TREE_CODE (type
) == TYPEOF_TYPE
)
3225 ctor_name
= constructor_name_full (type
);
3226 if (name
== ctor_name
)
3228 if (IDENTIFIER_TEMPLATE (ctor_name
)
3229 && name
== IDENTIFIER_TEMPLATE (ctor_name
))
3234 /* Counter used to create anonymous type names. */
3236 static GTY(()) int anon_cnt
;
3238 /* Return an IDENTIFIER which can be used as a name for
3239 unnamed structs and unions. */
3242 make_anon_name (void)
3246 sprintf (buf
, anon_aggrname_format (), anon_cnt
++);
3247 return get_identifier (buf
);
3250 /* This code is practically identical to that for creating
3251 anonymous names, but is just used for lambdas instead. This isn't really
3252 necessary, but it's convenient to avoid treating lambdas like other
3255 static GTY(()) int lambda_cnt
= 0;
3258 make_lambda_name (void)
3262 sprintf (buf
, LAMBDANAME_FORMAT
, lambda_cnt
++);
3263 return get_identifier (buf
);
3266 /* Insert another USING_DECL into the current binding level, returning
3267 this declaration. If this is a redeclaration, do nothing, and
3268 return NULL_TREE if this not in namespace scope (in namespace
3269 scope, a using decl might extend any previous bindings). */
3272 push_using_decl_1 (tree scope
, tree name
)
3276 gcc_assert (TREE_CODE (scope
) == NAMESPACE_DECL
);
3277 gcc_assert (identifier_p (name
));
3278 for (decl
= current_binding_level
->usings
; decl
; decl
= DECL_CHAIN (decl
))
3279 if (USING_DECL_SCOPE (decl
) == scope
&& DECL_NAME (decl
) == name
)
3282 return namespace_bindings_p () ? decl
: NULL_TREE
;
3283 decl
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
3284 USING_DECL_SCOPE (decl
) = scope
;
3285 DECL_CHAIN (decl
) = current_binding_level
->usings
;
3286 current_binding_level
->usings
= decl
;
3290 /* Wrapper for push_using_decl_1. */
3293 push_using_decl (tree scope
, tree name
)
3296 timevar_start (TV_NAME_LOOKUP
);
3297 ret
= push_using_decl_1 (scope
, name
);
3298 timevar_stop (TV_NAME_LOOKUP
);
3302 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3303 caller to set DECL_CONTEXT properly.
3305 Note that this must only be used when X will be the new innermost
3306 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
3307 without checking to see if the current IDENTIFIER_BINDING comes from a
3308 closer binding level than LEVEL. */
3311 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool is_friend
)
3313 cp_binding_level
*b
;
3314 tree function_decl
= current_function_decl
;
3316 current_function_decl
= NULL_TREE
;
3317 if (level
->kind
== sk_class
)
3319 b
= class_binding_level
;
3320 class_binding_level
= level
;
3321 pushdecl_class_level (x
);
3322 class_binding_level
= b
;
3326 b
= current_binding_level
;
3327 current_binding_level
= level
;
3328 x
= pushdecl (x
, is_friend
);
3329 current_binding_level
= b
;
3331 current_function_decl
= function_decl
;
3335 /* Inject X into the local scope just before the function parms. */
3338 pushdecl_outermost_localscope (tree x
)
3340 cp_binding_level
*b
= NULL
;
3341 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3343 /* Find the scope just inside the function parms. */
3344 for (cp_binding_level
*n
= current_binding_level
;
3345 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
3348 tree ret
= b
? do_pushdecl_with_scope (x
, b
, false) : error_mark_node
;
3349 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3354 /* Check a non-member using-declaration. Return the name and scope
3355 being used, and the USING_DECL, or NULL_TREE on failure. */
3358 validate_nonmember_using_decl (tree decl
, tree scope
, tree name
)
3360 /* [namespace.udecl]
3361 A using-declaration for a class member shall be a
3362 member-declaration. */
3365 error ("%qT is not a namespace or unscoped enum", scope
);
3368 else if (scope
== error_mark_node
)
3371 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
)
3374 A using-declaration shall not name a template-id. */
3375 error ("a using-declaration cannot specify a template-id. "
3376 "Try %<using %D%>", name
);
3380 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3382 error ("namespace %qD not allowed in using-declaration", decl
);
3386 if (TREE_CODE (decl
) == SCOPE_REF
)
3388 /* It's a nested name with template parameter dependent scope.
3389 This can only be using-declaration for class member. */
3390 error ("%qT is not a namespace", TREE_OPERAND (decl
, 0));
3394 decl
= OVL_FIRST (decl
);
3396 /* Make a USING_DECL. */
3397 tree using_decl
= push_using_decl (scope
, name
);
3399 if (using_decl
== NULL_TREE
3400 && at_function_scope_p ()
3402 /* C++11 7.3.3/10. */
3403 error ("%qD is already declared in this scope", name
);
3408 /* Process a local-scope or namespace-scope using declaration. SCOPE
3409 is the nominated scope to search for NAME. VALUE_P and TYPE_P
3410 point to the binding for NAME in the current scope and are
3414 do_nonmember_using_decl (tree scope
, tree name
, tree
*value_p
, tree
*type_p
)
3416 name_lookup
lookup (name
, 0);
3418 if (!qualified_namespace_lookup (scope
, &lookup
))
3420 error ("%qD not declared", name
);
3423 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
3425 error ("reference to %qD is ambiguous", name
);
3426 print_candidates (lookup
.value
);
3427 lookup
.value
= NULL_TREE
;
3430 if (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
)
3432 error ("reference to %qD is ambiguous", name
);
3433 print_candidates (lookup
.type
);
3434 lookup
.type
= NULL_TREE
;
3437 tree value
= *value_p
;
3438 tree type
= *type_p
;
3440 /* Shift the old and new bindings around so we're comparing class and
3441 enumeration names to each other. */
3442 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
3448 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
3450 lookup
.type
= lookup
.value
;
3451 lookup
.value
= NULL_TREE
;
3454 if (lookup
.value
&& lookup
.value
!= value
)
3456 /* Check for using functions. */
3457 if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
3459 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
3461 tree new_fn
= *usings
;
3463 /* [namespace.udecl]
3465 If a function declaration in namespace scope or block
3466 scope has the same name and the same parameter types as a
3467 function introduced by a using declaration the program is
3470 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
3474 if (new_fn
== old_fn
)
3475 /* The function already exists in the current
3478 else if (old
.using_p ())
3479 continue; /* This is a using decl. */
3480 else if (old
.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn
))
3481 continue; /* This is an anticipated builtin. */
3482 else if (!matching_fn_p (new_fn
, old_fn
))
3483 continue; /* Parameters do not match. */
3484 else if (decls_match (new_fn
, old_fn
))
3488 diagnose_name_conflict (new_fn
, old_fn
);
3494 /* Unlike the overload case we don't drop anticipated
3495 builtins here. They don't cause a problem, and
3496 we'd like to match them with a future
3498 value
= ovl_insert (new_fn
, value
, true);
3502 /* Ignore anticipated builtins. */
3503 && !anticipated_builtin_p (value
)
3504 && !decls_match (lookup
.value
, value
))
3505 diagnose_name_conflict (lookup
.value
, value
);
3507 value
= lookup
.value
;
3510 if (lookup
.type
&& lookup
.type
!= type
)
3512 if (type
&& !decls_match (lookup
.type
, type
))
3513 diagnose_name_conflict (lookup
.type
, type
);
3518 /* If bind->value is empty, shift any class or enumeration name back. */
3529 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
3530 Both are namespaces. */
3533 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
3535 int depth
= SCOPE_DEPTH (ancestor
);
3537 if (!depth
&& !inline_only
)
3538 /* The global namespace encloses everything. */
3541 while (SCOPE_DEPTH (descendant
) > depth
3542 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
3543 descendant
= CP_DECL_CONTEXT (descendant
);
3545 return ancestor
== descendant
;
3548 /* Returns true if ROOT (a namespace, class, or function) encloses
3549 CHILD. CHILD may be either a class type or a namespace. */
3552 is_ancestor (tree root
, tree child
)
3554 gcc_assert ((TREE_CODE (root
) == NAMESPACE_DECL
3555 || TREE_CODE (root
) == FUNCTION_DECL
3556 || CLASS_TYPE_P (root
)));
3557 gcc_assert ((TREE_CODE (child
) == NAMESPACE_DECL
3558 || CLASS_TYPE_P (child
)));
3560 /* The global namespace encloses everything. */
3561 if (root
== global_namespace
)
3564 /* Search until we reach namespace scope. */
3565 while (TREE_CODE (child
) != NAMESPACE_DECL
)
3567 /* If we've reached the ROOT, it encloses CHILD. */
3570 /* Go out one level. */
3572 child
= TYPE_NAME (child
);
3573 child
= CP_DECL_CONTEXT (child
);
3576 if (TREE_CODE (root
) == NAMESPACE_DECL
)
3577 return is_nested_namespace (root
, child
);
3582 /* Enter the class or namespace scope indicated by T suitable for name
3583 lookup. T can be arbitrary scope, not necessary nested inside the
3584 current scope. Returns a non-null scope to pop iff pop_scope
3585 should be called later to exit this scope. */
3590 if (TREE_CODE (t
) == NAMESPACE_DECL
)
3591 push_decl_namespace (t
);
3592 else if (CLASS_TYPE_P (t
))
3594 if (!at_class_scope_p ()
3595 || !same_type_p (current_class_type
, t
))
3596 push_nested_class (t
);
3598 /* T is the same as the current scope. There is therefore no
3599 need to re-enter the scope. Since we are not actually
3600 pushing a new scope, our caller should not call
3608 /* Leave scope pushed by push_scope. */
3615 if (TREE_CODE (t
) == NAMESPACE_DECL
)
3616 pop_decl_namespace ();
3617 else if CLASS_TYPE_P (t
)
3618 pop_nested_class ();
3621 /* Subroutine of push_inner_scope. */
3624 push_inner_scope_r (tree outer
, tree inner
)
3629 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
3632 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
3634 push_inner_scope_r (outer
, prev
);
3635 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
3637 cp_binding_level
*save_template_parm
= 0;
3638 /* Temporary take out template parameter scopes. They are saved
3639 in reversed order in save_template_parm. */
3640 while (current_binding_level
->kind
== sk_template_parms
)
3642 cp_binding_level
*b
= current_binding_level
;
3643 current_binding_level
= b
->level_chain
;
3644 b
->level_chain
= save_template_parm
;
3645 save_template_parm
= b
;
3648 resume_scope (NAMESPACE_LEVEL (inner
));
3649 current_namespace
= inner
;
3651 /* Restore template parameter scopes. */
3652 while (save_template_parm
)
3654 cp_binding_level
*b
= save_template_parm
;
3655 save_template_parm
= b
->level_chain
;
3656 b
->level_chain
= current_binding_level
;
3657 current_binding_level
= b
;
3664 /* Enter the scope INNER from current scope. INNER must be a scope
3665 nested inside current scope. This works with both name lookup and
3666 pushing name into scope. In case a template parameter scope is present,
3667 namespace is pushed under the template parameter scope according to
3668 name lookup rule in 14.6.1/6.
3670 Return the former current scope suitable for pop_inner_scope. */
3673 push_inner_scope (tree inner
)
3675 tree outer
= current_scope ();
3677 outer
= current_namespace
;
3679 push_inner_scope_r (outer
, inner
);
3683 /* Exit the current scope INNER back to scope OUTER. */
3686 pop_inner_scope (tree outer
, tree inner
)
3689 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
3692 while (outer
!= inner
)
3694 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
3696 cp_binding_level
*save_template_parm
= 0;
3697 /* Temporary take out template parameter scopes. They are saved
3698 in reversed order in save_template_parm. */
3699 while (current_binding_level
->kind
== sk_template_parms
)
3701 cp_binding_level
*b
= current_binding_level
;
3702 current_binding_level
= b
->level_chain
;
3703 b
->level_chain
= save_template_parm
;
3704 save_template_parm
= b
;
3709 /* Restore template parameter scopes. */
3710 while (save_template_parm
)
3712 cp_binding_level
*b
= save_template_parm
;
3713 save_template_parm
= b
->level_chain
;
3714 b
->level_chain
= current_binding_level
;
3715 current_binding_level
= b
;
3721 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
3725 /* Do a pushlevel for class declarations. */
3728 pushlevel_class (void)
3730 class_binding_level
= begin_scope (sk_class
, current_class_type
);
3733 /* ...and a poplevel for class declarations. */
3736 poplevel_class (void)
3738 cp_binding_level
*level
= class_binding_level
;
3739 cp_class_binding
*cb
;
3743 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3744 gcc_assert (level
!= 0);
3746 /* If we're leaving a toplevel class, cache its binding level. */
3747 if (current_class_depth
== 1)
3748 previous_class_level
= level
;
3749 for (shadowed
= level
->type_shadowed
;
3751 shadowed
= TREE_CHAIN (shadowed
))
3752 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
3754 /* Remove the bindings for all of the class-level declarations. */
3755 if (level
->class_shadowed
)
3757 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
3759 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
3760 cxx_binding_free (cb
->base
);
3762 ggc_free (level
->class_shadowed
);
3763 level
->class_shadowed
= NULL
;
3766 /* Now, pop out of the binding level which we created up in the
3767 `pushlevel_class' routine. */
3768 gcc_assert (current_binding_level
== level
);
3770 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3773 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
3774 appropriate. DECL is the value to which a name has just been
3775 bound. CLASS_TYPE is the class in which the lookup occurred. */
3778 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
3781 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
3785 if (TREE_CODE (decl
) == OVERLOAD
)
3786 context
= ovl_scope (decl
);
3789 gcc_assert (DECL_P (decl
));
3790 context
= context_for_name_lookup (decl
);
3793 if (is_properly_derived_from (class_type
, context
))
3794 INHERITED_VALUE_BINDING_P (binding
) = 1;
3796 INHERITED_VALUE_BINDING_P (binding
) = 0;
3798 else if (binding
->value
== decl
)
3799 /* We only encounter a TREE_LIST when there is an ambiguity in the
3800 base classes. Such an ambiguity can be overridden by a
3801 definition in this class. */
3802 INHERITED_VALUE_BINDING_P (binding
) = 1;
3804 INHERITED_VALUE_BINDING_P (binding
) = 0;
3807 /* Make the declaration of X appear in CLASS scope. */
3810 pushdecl_class_level (tree x
)
3812 bool is_valid
= true;
3815 /* Do nothing if we're adding to an outer lambda closure type,
3816 outer_binding will add it later if it's needed. */
3817 if (current_class_type
!= class_binding_level
->this_entity
)
3820 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3821 /* Get the name of X. */
3822 tree name
= OVL_NAME (x
);
3826 is_valid
= push_class_level_binding (name
, x
);
3827 if (TREE_CODE (x
) == TYPE_DECL
)
3828 set_identifier_type_value (name
, x
);
3830 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3832 /* If X is an anonymous aggregate, all of its members are
3833 treated as if they were members of the class containing the
3834 aggregate, for naming purposes. */
3837 for (f
= TYPE_FIELDS (TREE_TYPE (x
)); f
; f
= DECL_CHAIN (f
))
3839 location_t save_location
= input_location
;
3840 input_location
= DECL_SOURCE_LOCATION (f
);
3841 if (!pushdecl_class_level (f
))
3843 input_location
= save_location
;
3846 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3850 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
3851 scope. If the value returned is non-NULL, and the PREVIOUS field
3852 is not set, callers must set the PREVIOUS field explicitly. */
3854 static cxx_binding
*
3855 get_class_binding (tree name
, cp_binding_level
*scope
)
3860 cxx_binding
*binding
;
3862 class_type
= scope
->this_entity
;
3864 /* Get the type binding. */
3865 type_binding
= lookup_member (class_type
, name
,
3866 /*protect=*/2, /*want_type=*/true,
3867 tf_warning_or_error
);
3868 /* Get the value binding. */
3869 value_binding
= lookup_member (class_type
, name
,
3870 /*protect=*/2, /*want_type=*/false,
3871 tf_warning_or_error
);
3874 && (TREE_CODE (value_binding
) == TYPE_DECL
3875 || DECL_CLASS_TEMPLATE_P (value_binding
)
3876 || (TREE_CODE (value_binding
) == TREE_LIST
3877 && TREE_TYPE (value_binding
) == error_mark_node
3878 && (TREE_CODE (TREE_VALUE (value_binding
))
3880 /* We found a type binding, even when looking for a non-type
3881 binding. This means that we already processed this binding
3884 else if (value_binding
)
3886 if (TREE_CODE (value_binding
) == TREE_LIST
3887 && TREE_TYPE (value_binding
) == error_mark_node
)
3888 /* NAME is ambiguous. */
3890 else if (BASELINK_P (value_binding
))
3891 /* NAME is some overloaded functions. */
3892 value_binding
= BASELINK_FUNCTIONS (value_binding
);
3895 /* If we found either a type binding or a value binding, create a
3896 new binding object. */
3897 if (type_binding
|| value_binding
)
3899 binding
= new_class_binding (name
,
3903 /* This is a class-scope binding, not a block-scope binding. */
3904 LOCAL_BINDING_P (binding
) = 0;
3905 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
3913 /* Make the declaration(s) of X appear in CLASS scope under the name
3914 NAME. Returns true if the binding is valid. */
3917 push_class_level_binding_1 (tree name
, tree x
)
3919 cxx_binding
*binding
;
3923 /* The class_binding_level will be NULL if x is a template
3924 parameter name in a member template. */
3925 if (!class_binding_level
)
3928 if (name
== error_mark_node
)
3931 /* Can happen for an erroneous declaration (c++/60384). */
3932 if (!identifier_p (name
))
3934 gcc_assert (errorcount
|| sorrycount
);
3938 /* Check for invalid member names. But don't worry about a default
3939 argument-scope lambda being pushed after the class is complete. */
3940 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
3941 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
3942 /* Check that we're pushing into the right binding level. */
3943 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
3945 /* We could have been passed a tree list if this is an ambiguous
3946 declaration. If so, pull the declaration out because
3947 check_template_shadow will not handle a TREE_LIST. */
3948 if (TREE_CODE (decl
) == TREE_LIST
3949 && TREE_TYPE (decl
) == error_mark_node
)
3950 decl
= TREE_VALUE (decl
);
3952 if (!check_template_shadow (decl
))
3957 If T is the name of a class, then each of the following shall
3958 have a name different from T:
3960 -- every static data member of class T;
3962 -- every member of class T that is itself a type;
3964 -- every enumerator of every member of class T that is an
3967 -- every member of every anonymous union that is a member of
3970 (Non-static data members were also forbidden to have the same
3971 name as T until TC1.) */
3973 || TREE_CODE (x
) == CONST_DECL
3974 || (TREE_CODE (x
) == TYPE_DECL
3975 && !DECL_SELF_REFERENCE_P (x
))
3976 /* A data member of an anonymous union. */
3977 || (TREE_CODE (x
) == FIELD_DECL
3978 && DECL_CONTEXT (x
) != current_class_type
))
3979 && DECL_NAME (x
) == constructor_name (current_class_type
))
3981 tree scope
= context_for_name_lookup (x
);
3982 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
3984 error ("%qD has the same name as the class in which it is "
3991 /* Get the current binding for NAME in this class, if any. */
3992 binding
= IDENTIFIER_BINDING (name
);
3993 if (!binding
|| binding
->scope
!= class_binding_level
)
3995 binding
= get_class_binding (name
, class_binding_level
);
3996 /* If a new binding was created, put it at the front of the
3997 IDENTIFIER_BINDING list. */
4000 binding
->previous
= IDENTIFIER_BINDING (name
);
4001 IDENTIFIER_BINDING (name
) = binding
;
4005 /* If there is already a binding, then we may need to update the
4007 if (binding
&& binding
->value
)
4009 tree bval
= binding
->value
;
4010 tree old_decl
= NULL_TREE
;
4011 tree target_decl
= strip_using_decl (decl
);
4012 tree target_bval
= strip_using_decl (bval
);
4014 if (INHERITED_VALUE_BINDING_P (binding
))
4016 /* If the old binding was from a base class, and was for a
4017 tag name, slide it over to make room for the new binding.
4018 The old binding is still visible if explicitly qualified
4019 with a class-key. */
4020 if (TREE_CODE (target_bval
) == TYPE_DECL
4021 && DECL_ARTIFICIAL (target_bval
)
4022 && !(TREE_CODE (target_decl
) == TYPE_DECL
4023 && DECL_ARTIFICIAL (target_decl
)))
4025 old_decl
= binding
->type
;
4026 binding
->type
= bval
;
4027 binding
->value
= NULL_TREE
;
4028 INHERITED_VALUE_BINDING_P (binding
) = 0;
4033 /* Any inherited type declaration is hidden by the type
4034 declaration in the derived class. */
4035 if (TREE_CODE (target_decl
) == TYPE_DECL
4036 && DECL_ARTIFICIAL (target_decl
))
4037 binding
->type
= NULL_TREE
;
4040 else if (TREE_CODE (target_decl
) == OVERLOAD
4041 && OVL_P (target_bval
))
4043 else if (TREE_CODE (decl
) == USING_DECL
4044 && TREE_CODE (bval
) == USING_DECL
4045 && same_type_p (USING_DECL_SCOPE (decl
),
4046 USING_DECL_SCOPE (bval
)))
4047 /* This is a using redeclaration that will be diagnosed later
4048 in supplement_binding */
4050 else if (TREE_CODE (decl
) == USING_DECL
4051 && TREE_CODE (bval
) == USING_DECL
4052 && DECL_DEPENDENT_P (decl
)
4053 && DECL_DEPENDENT_P (bval
))
4055 else if (TREE_CODE (decl
) == USING_DECL
4056 && OVL_P (target_bval
))
4058 else if (TREE_CODE (bval
) == USING_DECL
4059 && OVL_P (target_decl
))
4062 if (old_decl
&& binding
->scope
== class_binding_level
)
4065 /* It is always safe to clear INHERITED_VALUE_BINDING_P
4066 here. This function is only used to register bindings
4067 from with the class definition itself. */
4068 INHERITED_VALUE_BINDING_P (binding
) = 0;
4073 /* Note that we declared this value so that we can issue an error if
4074 this is an invalid redeclaration of a name already used for some
4076 note_name_declared_in_class (name
, decl
);
4078 /* If we didn't replace an existing binding, put the binding on the
4079 stack of bindings for the identifier, and update the shadowed
4081 if (binding
&& binding
->scope
== class_binding_level
)
4082 /* Supplement the existing binding. */
4083 ok
= supplement_binding (binding
, decl
);
4086 /* Create a new binding. */
4087 push_binding (name
, decl
, class_binding_level
);
4094 /* Wrapper for push_class_level_binding_1. */
4097 push_class_level_binding (tree name
, tree x
)
4100 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4101 ret
= push_class_level_binding_1 (name
, x
);
4102 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4106 /* Process "using SCOPE::NAME" in a class scope. Return the
4107 USING_DECL created. */
4110 do_class_using_decl (tree scope
, tree name
)
4112 /* The USING_DECL returned by this function. */
4114 /* The declaration (or declarations) name by this using
4115 declaration. NULL if we are in a template and cannot figure out
4116 what has been named. */
4118 /* True if SCOPE is a dependent type. */
4119 bool scope_dependent_p
;
4120 /* True if SCOPE::NAME is dependent. */
4121 bool name_dependent_p
;
4122 /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */
4123 bool bases_dependent_p
;
4126 if (name
== error_mark_node
)
4129 if (!scope
|| !TYPE_P (scope
))
4131 error ("using-declaration for non-member at class scope");
4135 /* Make sure the name is not invalid */
4136 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4138 error ("%<%T::%D%> names destructor", scope
, name
);
4141 /* Using T::T declares inheriting ctors, even if T is a typedef. */
4142 if (MAYBE_CLASS_TYPE_P (scope
)
4143 && (name
== TYPE_IDENTIFIER (scope
)
4144 || constructor_name_p (name
, scope
)))
4146 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
4147 name
= ctor_identifier
;
4148 CLASSTYPE_NON_AGGREGATE (current_class_type
) = true;
4150 if (constructor_name_p (name
, current_class_type
))
4152 error ("%<%T::%D%> names constructor in %qT",
4153 scope
, name
, current_class_type
);
4157 scope_dependent_p
= dependent_scope_p (scope
);
4158 name_dependent_p
= (scope_dependent_p
4159 || (IDENTIFIER_TYPENAME_P (name
)
4160 && dependent_type_p (TREE_TYPE (name
))));
4162 bases_dependent_p
= any_dependent_bases_p ();
4166 /* From [namespace.udecl]:
4168 A using-declaration used as a member-declaration shall refer to a
4169 member of a base class of the class being defined.
4171 In general, we cannot check this constraint in a template because
4172 we do not know the entire set of base classes of the current
4173 class type. Morover, if SCOPE is dependent, it might match a
4174 non-dependent base. */
4176 if (!scope_dependent_p
)
4179 binfo
= lookup_base (current_class_type
, scope
, ba_any
, &b_kind
,
4180 tf_warning_or_error
);
4181 if (b_kind
< bk_proper_base
)
4183 if (!bases_dependent_p
|| b_kind
== bk_same_type
)
4185 error_not_base_type (scope
, current_class_type
);
4189 else if (name
== ctor_identifier
4190 && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo
)))
4192 error ("cannot inherit constructors from indirect base %qT", scope
);
4195 else if (!name_dependent_p
)
4197 decl
= lookup_member (binfo
, name
, 0, false, tf_warning_or_error
);
4200 error ("no members matching %<%T::%D%> in %q#T", scope
, name
,
4204 /* The binfo from which the functions came does not matter. */
4205 if (BASELINK_P (decl
))
4206 decl
= BASELINK_FUNCTIONS (decl
);
4210 value
= build_lang_decl (USING_DECL
, name
, NULL_TREE
);
4211 USING_DECL_DECLS (value
) = decl
;
4212 USING_DECL_SCOPE (value
) = scope
;
4213 DECL_DEPENDENT_P (value
) = !decl
;
4219 /* Return the binding for NAME in NS. If NS is NULL, look in
4220 global_namespace. */
4223 get_namespace_binding (tree ns
, tree name
)
4225 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4227 ns
= global_namespace
;
4228 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
4229 tree ret
= find_namespace_value (ns
, name
);
4230 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4234 /* Set value binding of NAME in the global namespace to VAL. Does not
4235 add it to the list of things in the namespace. */
4238 set_global_binding (tree name
, tree val
)
4240 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4242 tree
*slot
= find_namespace_slot (global_namespace
, name
, true);
4243 tree old
= MAYBE_STAT_DECL (*slot
);
4247 else if (old
== val
)
4249 else if (!STAT_HACK_P (*slot
)
4250 && TREE_CODE (val
) == TYPE_DECL
&& DECL_ARTIFICIAL (val
))
4251 *slot
= stat_hack (old
, val
);
4252 else if (!STAT_HACK_P (*slot
)
4253 && TREE_CODE (old
) == TYPE_DECL
&& DECL_ARTIFICIAL (old
))
4254 *slot
= stat_hack (val
, old
);
4256 /* The user's placed something in the implementor's
4258 diagnose_name_conflict (val
, old
);
4260 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4263 /* Set the context of a declaration to scope. Complain if we are not
4267 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
4271 /* Get rid of namespace aliases. */
4272 scope
= ORIGINAL_NAMESPACE (scope
);
4274 /* It is ok for friends to be qualified in parallel space. */
4275 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
4276 error ("declaration of %qD not in a namespace surrounding %qD",
4278 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4280 /* Writing "int N::i" to declare a variable within "N" is invalid. */
4281 if (scope
== current_namespace
)
4283 if (at_namespace_scope_p ())
4284 error ("explicit qualification in declaration of %qD",
4289 /* See whether this has been declared in the namespace. */
4290 old
= lookup_qualified_name (scope
, DECL_NAME (decl
), /*type*/false,
4291 /*complain*/true, /*hidden*/true);
4292 if (old
== error_mark_node
)
4293 /* No old declaration at all. */
4295 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
4296 if (TREE_CODE (old
) == TREE_LIST
)
4298 error ("reference to %qD is ambiguous", decl
);
4299 print_candidates (old
);
4304 /* We might have found OLD in an inline namespace inside SCOPE. */
4305 if (TREE_CODE (decl
) == TREE_CODE (old
))
4306 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4307 /* Don't compare non-function decls with decls_match here, since
4308 it can't check for the correct constness at this
4309 point. pushdecl will find those errors later. */
4312 /* Since decl is a function, old should contain a function decl. */
4315 /* We handle these in check_explicit_instantiation_namespace. */
4316 if (processing_explicit_instantiation
)
4318 if (processing_template_decl
|| processing_specialization
)
4319 /* We have not yet called push_template_decl to turn a
4320 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
4321 match. But, we'll check later, when we construct the
4324 /* Instantiations or specializations of templates may be declared as
4325 friends in any namespace. */
4326 if (friendp
&& DECL_USE_TEMPLATE (decl
))
4330 tree found
= NULL_TREE
;
4332 for (ovl_iterator
iter (old
); iter
; ++iter
)
4335 /* Adjust DECL_CONTEXT first so decls_match will return true
4336 if DECL will match a declaration in an inline namespace. */
4337 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
4338 if (decls_match (decl
, ofn
))
4340 if (found
&& !decls_match (found
, ofn
))
4342 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4343 error ("reference to %qD is ambiguous", decl
);
4344 print_candidates (old
);
4352 if (!is_nested_namespace (scope
, CP_DECL_CONTEXT (found
), true))
4354 if (DECL_HIDDEN_FRIEND_P (found
))
4356 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
4357 "%qD has not been declared within %qD", decl
, scope
);
4358 inform (DECL_SOURCE_LOCATION (found
),
4359 "only here as a %<friend%>");
4361 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
4367 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
4368 if (decls_match (decl
, old
))
4372 /* It didn't work, go back to the explicit scope. */
4373 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
4375 error ("%qD should have been declared inside %qD", decl
, scope
);
4378 /* Return the namespace where the current declaration is declared. */
4381 current_decl_namespace (void)
4384 /* If we have been pushed into a different namespace, use it. */
4385 if (!vec_safe_is_empty (decl_namespace_list
))
4386 return decl_namespace_list
->last ();
4388 if (current_class_type
)
4389 result
= decl_namespace_context (current_class_type
);
4390 else if (current_function_decl
)
4391 result
= decl_namespace_context (current_function_decl
);
4393 result
= current_namespace
;
4397 /* Process any ATTRIBUTES on a namespace definition. Returns true if
4398 attribute visibility is seen. */
4401 handle_namespace_attrs (tree ns
, tree attributes
)
4404 bool saw_vis
= false;
4406 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
4408 tree name
= get_attribute_name (d
);
4409 tree args
= TREE_VALUE (d
);
4411 if (is_attribute_p ("visibility", name
))
4413 /* attribute visibility is a property of the syntactic block
4414 rather than the namespace as a whole, so we don't touch the
4415 NAMESPACE_DECL at all. */
4416 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
4417 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
4419 warning (OPT_Wattributes
,
4420 "%qD attribute requires a single NTBS argument",
4425 if (!TREE_PUBLIC (ns
))
4426 warning (OPT_Wattributes
,
4427 "%qD attribute is meaningless since members of the "
4428 "anonymous namespace get local symbols", name
);
4430 push_visibility (TREE_STRING_POINTER (x
), 1);
4433 else if (is_attribute_p ("abi_tag", name
))
4435 if (!DECL_NAME (ns
))
4437 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
4441 if (!DECL_NAMESPACE_INLINE_P (ns
))
4443 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
4449 tree dn
= DECL_NAME (ns
);
4450 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
4451 IDENTIFIER_POINTER (dn
));
4452 TREE_TYPE (args
) = char_array_type_node
;
4453 args
= fix_string_type (args
);
4454 args
= build_tree_list (NULL_TREE
, args
);
4456 if (check_abi_tag_args (args
, name
))
4457 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
4458 DECL_ATTRIBUTES (ns
));
4462 warning (OPT_Wattributes
, "%qD attribute directive ignored",
4471 /* Temporarily set the namespace for the current declaration. */
4474 push_decl_namespace (tree decl
)
4476 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
4477 decl
= decl_namespace_context (decl
);
4478 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
4481 /* [namespace.memdef]/2 */
4484 pop_decl_namespace (void)
4486 decl_namespace_list
->pop ();
4489 /* Process a namespace-alias declaration. */
4492 do_namespace_alias (tree alias
, tree name_space
)
4494 if (name_space
== error_mark_node
)
4497 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
4499 name_space
= ORIGINAL_NAMESPACE (name_space
);
4501 /* Build the alias. */
4502 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
4503 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
4504 DECL_EXTERNAL (alias
) = 1;
4505 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
4508 /* Emit debug info for namespace alias. */
4509 if (!building_stmt_list_p ())
4510 (*debug_hooks
->early_global_decl
) (alias
);
4513 /* Like pushdecl, only it places X in the current namespace,
4517 pushdecl_namespace_level (tree x
, bool is_friend
)
4519 cp_binding_level
*b
= current_binding_level
;
4522 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4523 t
= do_pushdecl_with_scope
4524 (x
, NAMESPACE_LEVEL (current_namespace
), is_friend
);
4526 /* Now, the type_shadowed stack may screw us. Munge it so it does
4528 if (TREE_CODE (t
) == TYPE_DECL
)
4530 tree name
= DECL_NAME (t
);
4532 tree
*ptr
= (tree
*)0;
4533 for (; !global_scope_p (b
); b
= b
->level_chain
)
4535 tree shadowed
= b
->type_shadowed
;
4536 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
4537 if (TREE_PURPOSE (shadowed
) == name
)
4539 ptr
= &TREE_VALUE (shadowed
);
4540 /* Can't break out of the loop here because sometimes
4541 a binding level will have duplicate bindings for
4542 PT names. It's gross, but I haven't time to fix it. */
4545 newval
= TREE_TYPE (t
);
4546 if (ptr
== (tree
*)0)
4548 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4549 up here if this is changed to an assertion. --KR */
4550 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
4557 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4561 /* Process a using-declaration appearing in namespace scope. */
4564 finish_namespace_using_decl (tree decl
, tree scope
, tree name
)
4566 tree orig_decl
= decl
;
4568 gcc_checking_assert (current_binding_level
->kind
== sk_namespace
4569 && !processing_template_decl
);
4570 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
4571 if (decl
== NULL_TREE
)
4574 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
4575 tree val
= slot
? MAYBE_STAT_DECL (*slot
) : NULL_TREE
;
4576 tree type
= slot
? MAYBE_STAT_TYPE (*slot
) : NULL_TREE
;
4577 do_nonmember_using_decl (scope
, name
, &val
, &type
);
4578 if (STAT_HACK_P (*slot
))
4580 STAT_DECL (*slot
) = val
;
4581 STAT_TYPE (*slot
) = type
;
4584 *slot
= stat_hack (val
, type
);
4588 /* Emit debug info. */
4589 cp_emit_debug_info_for_using (orig_decl
, current_namespace
);
4592 /* Process a using-declaration at function scope. */
4595 finish_local_using_decl (tree decl
, tree scope
, tree name
)
4597 tree orig_decl
= decl
;
4599 gcc_checking_assert (current_binding_level
->kind
!= sk_class
4600 && current_binding_level
->kind
!= sk_namespace
);
4601 decl
= validate_nonmember_using_decl (decl
, scope
, name
);
4602 if (decl
== NULL_TREE
)
4605 add_decl_expr (decl
);
4607 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
4608 tree value
= binding
? binding
->value
: NULL_TREE
;
4609 tree type
= binding
? binding
->type
: NULL_TREE
;
4611 do_nonmember_using_decl (scope
, name
, &value
, &type
);
4615 else if (binding
&& value
== binding
->value
)
4617 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
4619 update_local_overload (IDENTIFIER_BINDING (name
), value
);
4620 IDENTIFIER_BINDING (name
)->value
= value
;
4623 /* Install the new binding. */
4624 push_local_binding (name
, value
, true);
4628 else if (binding
&& type
== binding
->type
)
4632 push_local_binding (name
, type
, true);
4633 set_identifier_type_value (name
, type
);
4636 /* Emit debug info. */
4637 if (!processing_template_decl
)
4638 cp_emit_debug_info_for_using (orig_decl
, current_scope ());
4641 /* Return the declarations that are members of the namespace NS. */
4644 cp_namespace_decls (tree ns
)
4646 return NAMESPACE_LEVEL (ns
)->names
;
4649 /* Combine prefer_type and namespaces_only into flags. */
4652 lookup_flags (int prefer_type
, int namespaces_only
)
4654 if (namespaces_only
)
4655 return LOOKUP_PREFER_NAMESPACES
;
4656 if (prefer_type
> 1)
4657 return LOOKUP_PREFER_TYPES
;
4658 if (prefer_type
> 0)
4659 return LOOKUP_PREFER_BOTH
;
4663 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
4664 ignore it or not. Subroutine of lookup_name_real and
4665 lookup_type_scope. */
4668 qualify_lookup (tree val
, int flags
)
4670 if (val
== NULL_TREE
)
4672 if ((flags
& LOOKUP_PREFER_NAMESPACES
) && TREE_CODE (val
) == NAMESPACE_DECL
)
4674 if (flags
& LOOKUP_PREFER_TYPES
)
4676 tree target_val
= strip_using_decl (val
);
4677 if (TREE_CODE (target_val
) == TYPE_DECL
4678 || TREE_CODE (target_val
) == TEMPLATE_DECL
)
4681 if (flags
& (LOOKUP_PREFER_NAMESPACES
| LOOKUP_PREFER_TYPES
))
4683 /* Look through lambda things that we shouldn't be able to see. */
4684 if (is_lambda_ignored_entity (val
))
4689 /* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4690 lookup failed. Search through all available namespaces and print out
4691 possible candidates. If no exact matches are found, and
4692 SUGGEST_MISSPELLINGS is true, then also look for near-matches and
4693 suggest the best near-match, if there is one. */
4696 suggest_alternatives_for (location_t location
, tree name
,
4697 bool suggest_misspellings
)
4699 vec
<tree
> candidates
= vNULL
;
4700 vec
<tree
> worklist
= vNULL
;
4701 unsigned limit
= PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP
);
4702 bool limited
= false;
4704 /* Breadth-first search of namespaces. Up to limit namespaces
4705 searched (limit zero == unlimited). */
4706 worklist
.safe_push (global_namespace
);
4707 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
4709 tree ns
= worklist
[ix
];
4710 name_lookup
lookup (name
);
4712 if (lookup
.search_qualified (ns
, false))
4713 candidates
.safe_push (lookup
.value
);
4717 /* Look for child namespaces. We have to do this
4718 indirectly because they are chained in reverse order,
4719 which is confusing to the user. */
4720 vec
<tree
> children
= vNULL
;
4722 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
4723 decl
; decl
= TREE_CHAIN (decl
))
4724 if (TREE_CODE (decl
) == NAMESPACE_DECL
4725 && !DECL_NAMESPACE_ALIAS (decl
)
4726 && !DECL_NAMESPACE_INLINE_P (decl
))
4727 children
.safe_push (decl
);
4729 while (!limited
&& !children
.is_empty ())
4731 if (worklist
.length () == limit
)
4733 /* Unconditionally warn that the search was truncated. */
4735 "maximum limit of %d namespaces searched for %qE",
4740 worklist
.safe_push (children
.pop ());
4742 children
.release ();
4745 worklist
.release ();
4747 if (candidates
.length ())
4749 inform_n (location
, candidates
.length (),
4750 "suggested alternative:",
4751 "suggested alternatives:");
4752 for (unsigned ix
= 0; ix
!= candidates
.length (); ix
++)
4754 tree val
= candidates
[ix
];
4756 inform (location_of (val
), " %qE", val
);
4758 candidates
.release ();
4760 else if (!suggest_misspellings
)
4762 else if (const char *fuzzy
= lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
))
4764 /* Show a spelling correction. */
4765 gcc_rich_location
richloc (location
);
4767 richloc
.add_fixit_replace (fuzzy
);
4768 inform_at_rich_loc (&richloc
, "suggested alternative: %qs", fuzzy
);
4772 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
4773 for some of the most common names within "std::".
4774 Given non-NULL NAME, a name for lookup within "std::", return the header
4775 name defining it within the C++ Standard Library (without '<' and '>'),
4779 get_std_name_hint (const char *name
)
4781 struct std_name_hint
4786 static const std_name_hint hints
[] = {
4788 {"array", "array"}, // C++11
4791 /* <forward_list>. */
4792 {"forward_list", "forward_list"}, // C++11
4794 {"basic_filebuf", "fstream"},
4795 {"basic_ifstream", "fstream"},
4796 {"basic_ofstream", "fstream"},
4797 {"basic_fstream", "fstream"},
4799 {"cin", "iostream"},
4800 {"cout", "iostream"},
4801 {"cerr", "iostream"},
4802 {"clog", "iostream"},
4803 {"wcin", "iostream"},
4804 {"wcout", "iostream"},
4805 {"wclog", "iostream"},
4810 {"multimap", "map"},
4813 {"priority_queue", "queue"},
4815 {"ostream", "ostream"},
4816 {"wostream", "ostream"},
4817 {"ends", "ostream"},
4818 {"flush", "ostream"},
4819 {"endl", "ostream"},
4822 {"multiset", "set"},
4824 {"basic_stringbuf", "sstream"},
4825 {"basic_istringstream", "sstream"},
4826 {"basic_ostringstream", "sstream"},
4827 {"basic_stringstream", "sstream"},
4831 {"string", "string"},
4832 {"wstring", "string"},
4833 {"u16string", "string"},
4834 {"u32string", "string"},
4835 /* <unordered_map>. */
4836 {"unordered_map", "unordered_map"}, // C++11
4837 {"unordered_multimap", "unordered_map"}, // C++11
4838 /* <unordered_set>. */
4839 {"unordered_set", "unordered_set"}, // C++11
4840 {"unordered_multiset", "unordered_set"}, // C++11
4842 {"vector", "vector"},
4844 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
4845 for (size_t i
= 0; i
< num_hints
; i
++)
4847 if (0 == strcmp (name
, hints
[i
].name
))
4848 return hints
[i
].header
;
4853 /* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no
4854 suggestions to offer.
4855 If SCOPE is the "std" namespace, then suggest pertinent header
4859 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
4861 if (scope
== NULL_TREE
)
4863 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
4865 /* We only offer suggestions for the "std" namespace. */
4866 if (scope
!= std_node
)
4868 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
4870 const char *name_str
= IDENTIFIER_POINTER (name
);
4871 const char *header_hint
= get_std_name_hint (name_str
);
4874 "%<std::%s%> is defined in header %<<%s>%>;"
4875 " did you forget to %<#include <%s>%>?",
4876 name_str
, header_hint
, header_hint
);
4879 /* Look for alternatives for NAME, an IDENTIFIER_NODE for which name
4880 lookup failed within the explicitly provided SCOPE. Suggest the
4881 the best meaningful candidates (if any) as a fix-it hint.
4882 Return true iff a suggestion was provided. */
4885 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
4888 /* Resolve any namespace aliases. */
4889 scope
= ORIGINAL_NAMESPACE (scope
);
4891 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
4893 best_match
<tree
, const char *> bm (name
);
4894 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
4896 /* See if we have a good suggesion for the user. */
4897 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
4900 gcc_rich_location
richloc (location
);
4901 richloc
.add_fixit_replace (fuzzy_name
);
4902 inform_at_rich_loc (&richloc
, "suggested alternative: %qs",
4907 maybe_suggest_missing_header (location
, name
, scope
);
4912 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4915 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces.
4916 If PREFER_TYPE is > 1, we only return TYPE_DECLs.
4918 Returns a DECL (or OVERLOAD, or BASELINK) representing the
4919 declaration found. If no suitable declaration can be found,
4920 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
4921 neither a class-type nor a namespace a diagnostic is issued. */
4924 lookup_qualified_name (tree scope
, tree name
, int prefer_type
, bool complain
,
4929 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
4931 int flags
= lookup_flags (prefer_type
, /*namespaces_only*/false);
4933 flags
|= LOOKUP_HIDDEN
;
4934 name_lookup
lookup (name
, flags
);
4936 if (qualified_namespace_lookup (scope
, &lookup
))
4939 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
4940 t
= lookup_enumerator (scope
, name
);
4941 else if (is_class_type (scope
, complain
))
4942 t
= lookup_member (scope
, name
, 2, prefer_type
, tf_warning_or_error
);
4945 return error_mark_node
;
4950 Accepts the NAME to lookup and its qualifying SCOPE.
4951 Returns the name/type pair found into the cxx_binding *RESULT,
4952 or false on error. */
4955 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
4957 timevar_start (TV_NAME_LOOKUP
);
4958 query_oracle (lookup
->name
);
4959 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
4960 timevar_stop (TV_NAME_LOOKUP
);
4964 /* Helper function for lookup_name_fuzzy.
4965 Traverse binding level LVL, looking for good name matches for NAME
4968 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
4969 cp_binding_level
*lvl
, bool look_within_fields
,
4970 enum lookup_name_fuzzy_kind kind
)
4972 if (look_within_fields
)
4973 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
4975 tree type
= lvl
->this_entity
;
4976 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
4977 tree best_matching_field
4978 = lookup_member_fuzzy (type
, name
, want_type_p
);
4979 if (best_matching_field
)
4980 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
4983 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
4987 /* OVERLOADs or decls from using declaration are wrapped into
4989 if (TREE_CODE (d
) == TREE_LIST
)
4990 d
= OVL_FIRST (TREE_VALUE (d
));
4992 /* Don't use bindings from implicitly declared functions,
4993 as they were likely misspellings themselves. */
4994 if (TREE_TYPE (d
) == error_mark_node
)
4997 /* Skip anticipated decls of builtin functions. */
4998 if (TREE_CODE (d
) == FUNCTION_DECL
4999 && DECL_BUILT_IN (d
)
5000 && DECL_ANTICIPATED (d
))
5003 if (tree name
= DECL_NAME (d
))
5004 /* Ignore internal names with spaces in them. */
5005 if (!strchr (IDENTIFIER_POINTER (name
), ' '))
5006 bm
.consider (IDENTIFIER_POINTER (name
));
5010 /* Search for near-matches for NAME within the current bindings, and within
5011 macro names, returning the best match as a const char *, or NULL if
5012 no reasonable match is found. */
5015 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
)
5017 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
5019 best_match
<tree
, const char *> bm (name
);
5021 cp_binding_level
*lvl
;
5022 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
5023 consider_binding_level (name
, bm
, lvl
, true, kind
);
5025 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
5026 consider_binding_level (name
, bm
, lvl
, false, kind
);
5028 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
5030 x = SOME_OTHER_MACRO (y);
5031 then "SOME_OTHER_MACRO" will survive to the frontend and show up
5032 as a misspelled identifier.
5034 Use the best distance so far so that a candidate is only set if
5035 a macro is better than anything so far. This allows early rejection
5036 (without calculating the edit distance) of macro names that must have
5037 distance >= bm.get_best_distance (), and means that we only get a
5038 non-NULL result for best_macro_match if it's better than any of
5039 the identifiers already checked. */
5040 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
5041 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
5042 /* If a macro is the closest so far to NAME, consider it. */
5044 bm
.consider ((const char *)best_macro
->ident
.str
);
5046 /* Try the "starts_decl_specifier_p" keywords to detect
5047 "singed" vs "signed" typos. */
5048 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
5050 const c_common_resword
*resword
= &c_common_reswords
[i
];
5052 if (kind
== FUZZY_LOOKUP_TYPENAME
)
5053 if (!cp_keyword_starts_decl_specifier_p (resword
->rid
))
5056 tree resword_identifier
= ridpointers
[resword
->rid
];
5057 if (!resword_identifier
)
5059 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
5061 /* Only consider reserved words that survived the
5062 filtering in init_reswords (e.g. for -std). */
5063 if (!C_IS_RESERVED_WORD (resword_identifier
))
5066 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
5069 return bm
.get_best_meaningful_candidate ();
5072 /* Subroutine of outer_binding.
5074 Returns TRUE if BINDING is a binding to a template parameter of
5075 SCOPE. In that case SCOPE is the scope of a primary template
5076 parameter -- in the sense of G++, i.e, a template that has its own
5079 Returns FALSE otherwise. */
5082 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
5083 cp_binding_level
*scope
)
5085 tree binding_value
, tmpl
, tinfo
;
5088 if (!binding
|| !scope
|| !scope
->this_entity
)
5091 binding_value
= binding
->value
? binding
->value
: binding
->type
;
5092 tinfo
= get_template_info (scope
->this_entity
);
5094 /* BINDING_VALUE must be a template parm. */
5095 if (binding_value
== NULL_TREE
5096 || (!DECL_P (binding_value
)
5097 || !DECL_TEMPLATE_PARM_P (binding_value
)))
5100 /* The level of BINDING_VALUE. */
5102 template_type_parameter_p (binding_value
)
5103 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
5104 (TREE_TYPE (binding_value
)))
5105 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
5107 /* The template of the current scope, iff said scope is a primary
5110 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
5111 ? TI_TEMPLATE (tinfo
)
5114 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
5115 then BINDING_VALUE is a parameter of TMPL. */
5116 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
5119 /* Return the innermost non-namespace binding for NAME from a scope
5120 containing BINDING, or, if BINDING is NULL, the current scope.
5121 Please note that for a given template, the template parameters are
5122 considered to be in the scope containing the current scope.
5123 If CLASS_P is false, then class bindings are ignored. */
5126 outer_binding (tree name
,
5127 cxx_binding
*binding
,
5131 cp_binding_level
*scope
;
5132 cp_binding_level
*outer_scope
;
5136 scope
= binding
->scope
->level_chain
;
5137 outer
= binding
->previous
;
5141 scope
= current_binding_level
;
5142 outer
= IDENTIFIER_BINDING (name
);
5144 outer_scope
= outer
? outer
->scope
: NULL
;
5146 /* Because we create class bindings lazily, we might be missing a
5147 class binding for NAME. If there are any class binding levels
5148 between the LAST_BINDING_LEVEL and the scope in which OUTER was
5149 declared, we must lookup NAME in those class scopes. */
5151 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
5153 if (scope
->kind
== sk_class
)
5155 cxx_binding
*class_binding
;
5157 class_binding
= get_class_binding (name
, scope
);
5160 /* Thread this new class-scope binding onto the
5161 IDENTIFIER_BINDING list so that future lookups
5163 class_binding
->previous
= outer
;
5165 binding
->previous
= class_binding
;
5167 IDENTIFIER_BINDING (name
) = class_binding
;
5168 return class_binding
;
5171 /* If we are in a member template, the template parms of the member
5172 template are considered to be inside the scope of the containing
5173 class, but within G++ the class bindings are all pushed between the
5174 template parms and the function body. So if the outer binding is
5175 a template parm for the current scope, return it now rather than
5176 look for a class binding. */
5177 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
5178 && binding_to_template_parms_of_scope_p (outer
, scope
))
5181 scope
= scope
->level_chain
;
5187 /* Return the innermost block-scope or class-scope value binding for
5188 NAME, or NULL_TREE if there is no such binding. */
5191 innermost_non_namespace_value (tree name
)
5193 cxx_binding
*binding
;
5194 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
5195 return binding
? binding
->value
: NULL_TREE
;
5198 /* Look up NAME in the current binding level and its superiors in the
5199 namespace of variables, functions and typedefs. Return a ..._DECL
5200 node of some kind representing its definition if there is only one
5201 such declaration, or return a TREE_LIST with all the overloaded
5202 definitions if there are many, or return 0 if it is undefined.
5203 Hidden name, either friend declaration or built-in function, are
5206 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5207 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5208 Otherwise we prefer non-TYPE_DECLs.
5210 If NONCLASS is nonzero, bindings in class scopes are ignored. If
5211 BLOCK_P is false, bindings in block scopes are ignored. */
5214 lookup_name_real_1 (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5215 int namespaces_only
, int flags
)
5218 tree val
= NULL_TREE
;
5220 query_oracle (name
);
5222 /* Conversion operators are handled specially because ordinary
5223 unqualified name lookup will not find template conversion
5225 if (IDENTIFIER_TYPENAME_P (name
))
5227 cp_binding_level
*level
;
5229 for (level
= current_binding_level
;
5230 level
&& level
->kind
!= sk_namespace
;
5231 level
= level
->level_chain
)
5236 /* A conversion operator can only be declared in a class
5238 if (level
->kind
!= sk_class
)
5241 /* Lookup the conversion operator in the class. */
5242 class_type
= level
->this_entity
;
5243 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0);
5251 flags
|= lookup_flags (prefer_type
, namespaces_only
);
5253 /* First, look in non-namespace scopes. */
5255 if (current_class_type
== NULL_TREE
)
5258 if (block_p
|| !nonclass
)
5259 for (iter
= outer_binding (name
, NULL
, !nonclass
);
5261 iter
= outer_binding (name
, iter
, !nonclass
))
5265 /* Skip entities we don't want. */
5266 if (LOCAL_BINDING_P (iter
) ? !block_p
: nonclass
)
5269 /* If this is the kind of thing we're looking for, we're done. */
5270 if (qualify_lookup (iter
->value
, flags
))
5271 binding
= iter
->value
;
5272 else if ((flags
& LOOKUP_PREFER_TYPES
)
5273 && qualify_lookup (iter
->type
, flags
))
5274 binding
= iter
->type
;
5276 binding
= NULL_TREE
;
5280 if (TREE_CODE (binding
) == TYPE_DECL
&& DECL_HIDDEN_P (binding
))
5282 /* A non namespace-scope binding can only be hidden in the
5283 presence of a local class, due to friend declarations.
5285 In particular, consider:
5293 B* b; // error: B is hidden
5294 C* c; // OK, finds ::C
5297 B *b; // error: B is hidden
5298 C *c; // OK, finds ::C
5303 The standard says that "B" is a local class in "f"
5304 (but not nested within "A") -- but that name lookup
5305 for "B" does not find this declaration until it is
5306 declared directly with "f".
5312 If a friend declaration appears in a local class and
5313 the name specified is an unqualified name, a prior
5314 declaration is looked up without considering scopes
5315 that are outside the innermost enclosing non-class
5316 scope. For a friend function declaration, if there is
5317 no prior declaration, the program is ill-formed. For a
5318 friend class declaration, if there is no prior
5319 declaration, the class that is specified belongs to the
5320 innermost enclosing non-class scope, but if it is
5321 subsequently referenced, its name is not found by name
5322 lookup until a matching declaration is provided in the
5323 innermost enclosing nonclass scope.
5325 So just keep looking for a non-hidden binding.
5327 gcc_assert (TREE_CODE (binding
) == TYPE_DECL
);
5335 /* Now lookup in namespace scopes. */
5338 name_lookup
lookup (name
, flags
);
5339 if (lookup
.search_unqualified
5340 (current_decl_namespace (), current_binding_level
))
5344 /* If we have a single function from a using decl, pull it out. */
5345 if (val
&& TREE_CODE (val
) == OVERLOAD
&& !really_overloaded_fn (val
))
5346 val
= OVL_FUNCTION (val
);
5351 /* Wrapper for lookup_name_real_1. */
5354 lookup_name_real (tree name
, int prefer_type
, int nonclass
, bool block_p
,
5355 int namespaces_only
, int flags
)
5358 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5359 ret
= lookup_name_real_1 (name
, prefer_type
, nonclass
, block_p
,
5360 namespaces_only
, flags
);
5361 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5366 lookup_name_nonclass (tree name
)
5368 return lookup_name_real (name
, 0, 1, /*block_p=*/true, 0, 0);
5372 lookup_name (tree name
)
5374 return lookup_name_real (name
, 0, 0, /*block_p=*/true, 0, 0);
5378 lookup_name_prefer_type (tree name
, int prefer_type
)
5380 return lookup_name_real (name
, prefer_type
, 0, /*block_p=*/true, 0, 0);
5383 /* Look up NAME for type used in elaborated name specifier in
5384 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or
5385 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the
5386 name, more scopes are checked if cleanup or template parameter
5387 scope is encountered.
5389 Unlike lookup_name_real, we make sure that NAME is actually
5390 declared in the desired scope, not from inheritance, nor using
5391 directive. For using declaration, there is DR138 still waiting
5392 to be resolved. Hidden name coming from an earlier friend
5393 declaration is also returned.
5395 A TYPE_DECL best matching the NAME is returned. Catching error
5396 and issuing diagnostics are caller's responsibility. */
5399 lookup_type_scope_1 (tree name
, tag_scope scope
)
5401 cxx_binding
*iter
= NULL
;
5402 tree val
= NULL_TREE
;
5403 cp_binding_level
*level
= NULL
;
5405 /* Look in non-namespace scope first. */
5406 if (current_binding_level
->kind
!= sk_namespace
)
5407 iter
= outer_binding (name
, NULL
, /*class_p=*/ true);
5408 for (; iter
; iter
= outer_binding (name
, iter
, /*class_p=*/ true))
5410 /* Check if this is the kind of thing we're looking for.
5411 If SCOPE is TS_CURRENT, also make sure it doesn't come from
5412 base class. For ITER->VALUE, we can simply use
5413 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
5416 We check ITER->TYPE before ITER->VALUE in order to handle
5417 typedef struct C {} C;
5420 if (qualify_lookup (iter
->type
, LOOKUP_PREFER_TYPES
)
5421 && (scope
!= ts_current
5422 || LOCAL_BINDING_P (iter
)
5423 || DECL_CONTEXT (iter
->type
) == iter
->scope
->this_entity
))
5425 else if ((scope
!= ts_current
5426 || !INHERITED_VALUE_BINDING_P (iter
))
5427 && qualify_lookup (iter
->value
, LOOKUP_PREFER_TYPES
))
5434 /* Look in namespace scope. */
5436 level
= iter
->scope
;
5439 tree ns
= current_decl_namespace ();
5441 if (tree
*slot
= find_namespace_slot (ns
, name
))
5443 /* If this is the kind of thing we're looking for, we're done. */
5444 if (tree type
= MAYBE_STAT_TYPE (*slot
))
5445 if (qualify_lookup (type
, LOOKUP_PREFER_TYPES
))
5449 if (tree decl
= MAYBE_STAT_DECL (*slot
))
5450 if (qualify_lookup (decl
, LOOKUP_PREFER_TYPES
))
5453 level
= NAMESPACE_LEVEL (ns
);
5457 /* Type found, check if it is in the allowed scopes, ignoring cleanup
5458 and template parameter scopes. */
5461 cp_binding_level
*b
= current_binding_level
;
5467 if (b
->kind
== sk_cleanup
|| b
->kind
== sk_template_parms
5468 || b
->kind
== sk_function_parms
)
5470 else if (b
->kind
== sk_class
5471 && scope
== ts_within_enclosing_non_class
)
5481 /* Wrapper for lookup_type_scope_1. */
5484 lookup_type_scope (tree name
, tag_scope scope
)
5487 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5488 ret
= lookup_type_scope_1 (name
, scope
);
5489 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5493 /* Returns true iff DECL is a block-scope extern declaration of a function
5497 is_local_extern (tree decl
)
5499 cxx_binding
*binding
;
5501 /* For functions, this is easy. */
5502 if (TREE_CODE (decl
) == FUNCTION_DECL
)
5503 return DECL_LOCAL_FUNCTION_P (decl
);
5507 if (!current_function_decl
)
5510 /* For variables, this is not easy. We need to look at the binding stack
5511 for the identifier to see whether the decl we have is a local. */
5512 for (binding
= IDENTIFIER_BINDING (DECL_NAME (decl
));
5513 binding
&& binding
->scope
->kind
!= sk_namespace
;
5514 binding
= binding
->previous
)
5515 if (binding
->value
== decl
)
5516 return LOCAL_BINDING_P (binding
);
5521 /* The type TYPE is being declared. If it is a class template, or a
5522 specialization of a class template, do any processing required and
5523 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
5524 being declared a friend. B is the binding level at which this TYPE
5527 Returns the TYPE_DECL for TYPE, which may have been altered by this
5531 maybe_process_template_type_declaration (tree type
, int is_friend
,
5532 cp_binding_level
*b
)
5534 tree decl
= TYPE_NAME (type
);
5536 if (processing_template_parmlist
)
5537 /* You can't declare a new template type in a template parameter
5538 list. But, you can declare a non-template type:
5540 template <class A*> struct S;
5542 is a forward-declaration of `A'. */
5544 else if (b
->kind
== sk_namespace
5545 && current_binding_level
->kind
!= sk_namespace
)
5546 /* If this new type is being injected into a containing scope,
5547 then it's not a template type. */
5551 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
5552 || TREE_CODE (type
) == ENUMERAL_TYPE
);
5554 if (processing_template_decl
)
5556 /* This may change after the call to
5557 push_template_decl_real, but we want the original value. */
5558 tree name
= DECL_NAME (decl
);
5560 decl
= push_template_decl_real (decl
, is_friend
);
5561 if (decl
== error_mark_node
)
5562 return error_mark_node
;
5564 /* If the current binding level is the binding level for the
5565 template parameters (see the comment in
5566 begin_template_parm_list) and the enclosing level is a class
5567 scope, and we're not looking at a friend, push the
5568 declaration of the member class into the class scope. In the
5569 friend case, push_template_decl will already have put the
5570 friend into global scope, if appropriate. */
5571 if (TREE_CODE (type
) != ENUMERAL_TYPE
5572 && !is_friend
&& b
->kind
== sk_template_parms
5573 && b
->level_chain
->kind
== sk_class
)
5575 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
5577 if (!COMPLETE_TYPE_P (current_class_type
))
5579 maybe_add_class_template_decl_list (current_class_type
,
5580 type
, /*friend_p=*/0);
5581 /* Put this UTD in the table of UTDs for the class. */
5582 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
5583 CLASSTYPE_NESTED_UTDS (current_class_type
) =
5584 binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
5586 binding_table_insert
5587 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
5596 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
5597 that the NAME is a class template, the tag is processed but not pushed.
5599 The pushed scope depend on the SCOPE parameter:
5600 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5602 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5603 non-template-parameter scope. This case is needed for forward
5605 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5606 TS_GLOBAL case except that names within template-parameter scopes
5607 are not pushed at all.
5609 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
5612 do_pushtag (tree name
, tree type
, tag_scope scope
)
5616 cp_binding_level
*b
= current_binding_level
;
5617 while (/* Cleanup scopes are not scopes from the point of view of
5619 b
->kind
== sk_cleanup
5620 /* Neither are function parameter scopes. */
5621 || b
->kind
== sk_function_parms
5622 /* Neither are the scopes used to hold template parameters
5623 for an explicit specialization. For an ordinary template
5624 declaration, these scopes are not scopes from the point of
5625 view of the language. */
5626 || (b
->kind
== sk_template_parms
5627 && (b
->explicit_spec_p
|| scope
== ts_global
))
5628 /* Pushing into a class is ok for lambdas or when we want current */
5629 || (b
->kind
== sk_class
5630 && scope
!= ts_lambda
5631 && (scope
!= ts_current
5632 /* We may be defining a new type in the initializer
5633 of a static member variable. We allow this when
5634 not pedantic, and it is particularly useful for
5635 type punning via an anonymous union. */
5636 || COMPLETE_TYPE_P (b
->this_entity
))))
5639 gcc_assert (identifier_p (name
));
5641 /* Do C++ gratuitous typedefing. */
5642 if (identifier_type_value_1 (name
) != type
)
5646 tree context
= TYPE_CONTEXT (type
);
5650 tree cs
= current_scope ();
5652 if (scope
== ts_current
5653 || scope
== ts_lambda
5654 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
5656 else if (cs
&& TYPE_P (cs
))
5657 /* When declaring a friend class of a local class, we want
5658 to inject the newly named class into the scope
5659 containing the local class, not the namespace
5661 context
= decl_function_context (get_type_decl (cs
));
5664 context
= current_namespace
;
5666 if (b
->kind
== sk_class
5667 || (b
->kind
== sk_template_parms
5668 && b
->level_chain
->kind
== sk_class
))
5671 tdef
= create_implicit_typedef (name
, type
);
5672 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
5673 if (scope
== ts_within_enclosing_non_class
)
5675 /* This is a friend. Make this TYPE_DECL node hidden from
5676 ordinary name lookup. Its corresponding TEMPLATE_DECL
5677 will be marked in push_template_decl_real. */
5678 retrofit_lang_decl (tdef
);
5679 DECL_ANTICIPATED (tdef
) = 1;
5680 DECL_FRIEND_P (tdef
) = 1;
5683 decl
= maybe_process_template_type_declaration
5684 (type
, scope
== ts_within_enclosing_non_class
, b
);
5685 if (decl
== error_mark_node
)
5688 if (b
->kind
== sk_class
)
5690 if (!TYPE_BEING_DEFINED (current_class_type
)
5691 && scope
!= ts_lambda
)
5692 return error_mark_node
;
5694 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5695 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5696 class. But if it's a member template class, we want
5697 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5699 finish_member_declaration (decl
);
5701 pushdecl_class_level (decl
);
5703 else if (b
->kind
!= sk_template_parms
)
5705 decl
= do_pushdecl_with_scope (decl
, b
, /*is_friend=*/false);
5706 if (decl
== error_mark_node
)
5709 if (DECL_CONTEXT (decl
) == std_node
5710 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
5711 && !CLASSTYPE_TEMPLATE_INFO (type
))
5713 error ("declaration of std::initializer_list does not match "
5714 "#include <initializer_list>, isn't a template");
5715 return error_mark_node
;
5720 set_identifier_type_value_with_scope (name
, tdef
, b
);
5722 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
5724 /* If this is a local class, keep track of it. We need this
5725 information for name-mangling, and so that it is possible to
5726 find all function definitions in a translation unit in a
5727 convenient way. (It's otherwise tricky to find a member
5728 function definition it's only pointed to from within a local
5730 if (TYPE_FUNCTION_SCOPE_P (type
))
5732 if (processing_template_decl
)
5734 /* Push a DECL_EXPR so we call pushtag at the right time in
5735 template instantiation rather than in some nested context. */
5736 add_decl_expr (decl
);
5739 vec_safe_push (local_classes
, type
);
5743 if (b
->kind
== sk_class
5744 && !COMPLETE_TYPE_P (current_class_type
))
5746 maybe_add_class_template_decl_list (current_class_type
,
5747 type
, /*friend_p=*/0);
5749 if (CLASSTYPE_NESTED_UTDS (current_class_type
) == NULL
)
5750 CLASSTYPE_NESTED_UTDS (current_class_type
)
5751 = binding_table_new (SCOPE_DEFAULT_HT_SIZE
);
5753 binding_table_insert
5754 (CLASSTYPE_NESTED_UTDS (current_class_type
), name
, type
);
5757 decl
= TYPE_NAME (type
);
5758 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
5760 /* Set type visibility now if this is a forward declaration. */
5761 TREE_PUBLIC (decl
) = 1;
5762 determine_visibility (decl
);
5767 /* Wrapper for do_pushtag. */
5770 pushtag (tree name
, tree type
, tag_scope scope
)
5773 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5774 ret
= do_pushtag (name
, type
, scope
);
5775 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5780 /* Subroutines for reverting temporarily to top-level for instantiation
5781 of templates and such. We actually need to clear out the class- and
5782 local-value slots of all identifiers, so that only the global values
5783 are at all visible. Simply setting current_binding_level to the global
5784 scope isn't enough, because more binding levels may be pushed. */
5785 struct saved_scope
*scope_chain
;
5787 /* Return true if ID has not already been marked. */
5790 store_binding_p (tree id
)
5792 if (!id
|| !IDENTIFIER_BINDING (id
))
5795 if (IDENTIFIER_MARKED (id
))
5801 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
5802 have enough space reserved. */
5805 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
5807 cxx_saved_binding saved
;
5809 gcc_checking_assert (store_binding_p (id
));
5811 IDENTIFIER_MARKED (id
) = 1;
5813 saved
.identifier
= id
;
5814 saved
.binding
= IDENTIFIER_BINDING (id
);
5815 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
5816 (*old_bindings
)->quick_push (saved
);
5817 IDENTIFIER_BINDING (id
) = NULL
;
5821 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
5823 static vec
<tree
> bindings_need_stored
;
5827 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5828 for (t
= names
; t
; t
= TREE_CHAIN (t
))
5830 if (TREE_CODE (t
) == TREE_LIST
)
5831 id
= TREE_PURPOSE (t
);
5835 if (store_binding_p (id
))
5836 bindings_need_stored
.safe_push (id
);
5838 if (!bindings_need_stored
.is_empty ())
5840 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
5841 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
5843 /* We can apparently have duplicates in NAMES. */
5844 if (store_binding_p (id
))
5845 store_binding (id
, old_bindings
);
5847 bindings_need_stored
.truncate (0);
5849 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5852 /* Like store_bindings, but NAMES is a vector of cp_class_binding
5853 objects, rather than a TREE_LIST. */
5856 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
5857 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
5859 static vec
<tree
> bindings_need_stored
;
5861 cp_class_binding
*cb
;
5863 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
5864 if (store_binding_p (cb
->identifier
))
5865 bindings_need_stored
.safe_push (cb
->identifier
);
5866 if (!bindings_need_stored
.is_empty ())
5869 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
5870 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
5871 store_binding (id
, old_bindings
);
5872 bindings_need_stored
.truncate (0);
5876 /* A chain of saved_scope structures awaiting reuse. */
5878 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
5881 do_push_to_top_level (void)
5883 struct saved_scope
*s
;
5884 cp_binding_level
*b
;
5885 cxx_saved_binding
*sb
;
5889 /* Reuse or create a new structure for this saved scope. */
5890 if (free_saved_scope
!= NULL
)
5892 s
= free_saved_scope
;
5893 free_saved_scope
= s
->prev
;
5895 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
5896 memset (s
, 0, sizeof (*s
));
5897 /* Also reuse the structure's old_bindings vector. */
5898 vec_safe_truncate (old_bindings
, 0);
5899 s
->old_bindings
= old_bindings
;
5902 s
= ggc_cleared_alloc
<saved_scope
> ();
5904 b
= scope_chain
? current_binding_level
: 0;
5906 /* If we're in the middle of some function, save our state. */
5910 push_function_context ();
5915 if (scope_chain
&& previous_class_level
)
5916 store_class_bindings (previous_class_level
->class_shadowed
,
5919 /* Have to include the global scope, because class-scope decls
5920 aren't listed anywhere useful. */
5921 for (; b
; b
= b
->level_chain
)
5925 /* Template IDs are inserted into the global level. If they were
5926 inserted into namespace level, finish_file wouldn't find them
5927 when doing pending instantiations. Therefore, don't stop at
5928 namespace level, but continue until :: . */
5929 if (global_scope_p (b
))
5932 store_bindings (b
->names
, &s
->old_bindings
);
5933 /* We also need to check class_shadowed to save class-level type
5934 bindings, since pushclass doesn't fill in b->names. */
5935 if (b
->kind
== sk_class
)
5936 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
5938 /* Unwind type-value slots back to top level. */
5939 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
5940 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
5943 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
5944 IDENTIFIER_MARKED (sb
->identifier
) = 0;
5946 s
->prev
= scope_chain
;
5948 s
->need_pop_function_context
= need_pop
;
5949 s
->function_decl
= current_function_decl
;
5950 s
->unevaluated_operand
= cp_unevaluated_operand
;
5951 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
5952 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
5955 current_function_decl
= NULL_TREE
;
5956 vec_alloc (current_lang_base
, 10);
5957 current_lang_name
= lang_name_cplusplus
;
5958 current_namespace
= global_namespace
;
5959 push_class_stack ();
5960 cp_unevaluated_operand
= 0;
5961 c_inhibit_evaluation_warnings
= 0;
5965 do_pop_from_top_level (void)
5967 struct saved_scope
*s
= scope_chain
;
5968 cxx_saved_binding
*saved
;
5971 /* Clear out class-level bindings cache. */
5972 if (previous_class_level
)
5973 invalidate_class_lookup_cache ();
5976 current_lang_base
= 0;
5978 scope_chain
= s
->prev
;
5979 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
5981 tree id
= saved
->identifier
;
5983 IDENTIFIER_BINDING (id
) = saved
->binding
;
5984 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
5987 /* If we were in the middle of compiling a function, restore our
5989 if (s
->need_pop_function_context
)
5990 pop_function_context ();
5991 current_function_decl
= s
->function_decl
;
5992 cp_unevaluated_operand
= s
->unevaluated_operand
;
5993 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
5995 /* Make this saved_scope structure available for reuse by
5996 push_to_top_level. */
5997 s
->prev
= free_saved_scope
;
5998 free_saved_scope
= s
;
6001 /* Push into the scope of the namespace NS, even if it is deeply
6002 nested within another namespace. */
6005 do_push_nested_namespace (tree ns
)
6007 if (ns
== global_namespace
)
6008 do_push_to_top_level ();
6011 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
6013 (find_namespace_value (current_namespace
,
6014 DECL_NAME (ns
) ? DECL_NAME (ns
)
6015 : anon_identifier
) == ns
);
6016 resume_scope (NAMESPACE_LEVEL (ns
));
6017 current_namespace
= ns
;
6021 /* Pop back from the scope of the namespace NS, which was previously
6022 entered with push_nested_namespace. */
6025 do_pop_nested_namespace (tree ns
)
6027 while (ns
!= global_namespace
)
6029 ns
= CP_DECL_CONTEXT (ns
);
6030 current_namespace
= ns
;
6034 do_pop_from_top_level ();
6037 /* Add TARGET to USINGS, if it does not already exist there.
6038 We used to build the complete graph of usings at this point, from
6039 the POV of the source namespaces. Now we build that as we perform
6040 the unqualified search. */
6043 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
6046 for (unsigned ix
= usings
->length (); ix
--;)
6047 if ((*usings
)[ix
] == target
)
6050 vec_safe_push (usings
, target
);
6053 /* Tell the debug system of a using directive. */
6056 emit_debug_info_using_namespace (tree from
, tree target
)
6058 /* Emit debugging info. */
6059 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
6060 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false);
6063 /* Process a namespace-scope using directive. */
6066 finish_namespace_using_directive (tree target
, tree attribs
)
6068 gcc_checking_assert (namespace_bindings_p ());
6069 if (target
== error_mark_node
)
6072 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6073 ORIGINAL_NAMESPACE (target
));
6074 emit_debug_info_using_namespace (current_namespace
,
6075 ORIGINAL_NAMESPACE (target
));
6077 if (attribs
== error_mark_node
)
6080 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
6082 tree name
= get_attribute_name (a
);
6083 if (is_attribute_p ("strong", name
))
6085 warning (0, "strong using directive no longer supported");
6086 if (CP_DECL_CONTEXT (target
) == current_namespace
)
6087 inform (DECL_SOURCE_LOCATION (target
),
6088 "you may use an inline namespace instead");
6091 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
6095 /* Process a function-scope using-directive. */
6098 finish_local_using_directive (tree target
, tree attribs
)
6100 gcc_checking_assert (local_bindings_p ());
6101 if (target
== error_mark_node
)
6105 warning (OPT_Wattributes
, "attributes ignored on local using directive");
6107 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
6109 add_using_namespace (current_binding_level
->using_directives
,
6110 ORIGINAL_NAMESPACE (target
));
6113 /* Pushes X into the global namespace. */
6116 pushdecl_top_level (tree x
, bool is_friend
)
6118 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6119 do_push_to_top_level ();
6120 x
= pushdecl_namespace_level (x
, is_friend
);
6121 do_pop_from_top_level ();
6122 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6126 /* Pushes X into the global namespace and calls cp_finish_decl to
6127 register the variable, initializing it with INIT. */
6130 pushdecl_top_level_and_finish (tree x
, tree init
)
6132 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6133 do_push_to_top_level ();
6134 x
= pushdecl_namespace_level (x
, false);
6135 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
6136 do_pop_from_top_level ();
6137 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6141 /* Enter the namespaces from current_namerspace to NS. */
6144 push_inline_namespaces (tree ns
)
6147 if (ns
!= current_namespace
)
6149 gcc_assert (ns
!= global_namespace
);
6150 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6151 resume_scope (NAMESPACE_LEVEL (ns
));
6152 current_namespace
= ns
;
6158 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
6159 then we enter an anonymous namespace. If MAKE_INLINE is true, then
6160 we create an inline namespace (it is up to the caller to check upon
6161 redefinition). Return the number of namespaces entered. */
6164 push_namespace (tree name
, bool make_inline
)
6166 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6169 /* We should not get here if the global_namespace is not yet constructed
6170 nor if NAME designates the global namespace: The global scope is
6171 constructed elsewhere. */
6172 gcc_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
6175 name
= anon_identifier
;
6177 tree ns
= NULL_TREE
;
6179 name_lookup
lookup (name
, 0);
6180 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
6182 else if (TREE_CODE (lookup
.value
) != NAMESPACE_DECL
)
6184 else if (tree dna
= DECL_NAMESPACE_ALIAS (lookup
.value
))
6186 /* A namespace alias is not allowed here, but if the alias
6187 is for a namespace also inside the current scope,
6188 accept it with a diagnostic. That's better than dying
6190 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
6192 error ("namespace alias %qD not allowed here, "
6193 "assuming %qD", lookup
.value
, dna
);
6201 bool new_ns
= false;
6203 /* DR2061. NS might be a member of an inline namespace. We
6204 need to push into those namespaces. */
6205 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
6208 ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
6209 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (current_namespace
) + 1;
6210 if (!SCOPE_DEPTH (ns
))
6211 /* We only allow depth 255. */
6212 sorry ("cannot nest more than %d namespaces",
6213 SCOPE_DEPTH (current_namespace
));
6214 DECL_CONTEXT (ns
) = FROB_CONTEXT (current_namespace
);
6217 if (pushdecl (ns
) == error_mark_node
)
6221 if (name
== anon_identifier
)
6223 /* Clear DECL_NAME for the benefit of debugging back ends. */
6224 SET_DECL_ASSEMBLER_NAME (ns
, name
);
6225 DECL_NAME (ns
) = NULL_TREE
;
6228 add_using_namespace (DECL_NAMESPACE_USING (current_namespace
),
6231 else if (TREE_PUBLIC (current_namespace
))
6232 TREE_PUBLIC (ns
) = 1;
6234 if (name
== anon_identifier
|| make_inline
)
6235 emit_debug_info_using_namespace (current_namespace
, ns
);
6239 DECL_NAMESPACE_INLINE_P (ns
) = true;
6240 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace
), ns
);
6247 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
6249 error ("inline namespace must be specified at initial definition");
6250 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
6253 begin_scope (sk_namespace
, ns
);
6255 resume_scope (NAMESPACE_LEVEL (ns
));
6256 current_namespace
= ns
;
6260 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6264 /* Pop from the scope of the current namespace. */
6267 pop_namespace (void)
6269 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6271 gcc_assert (current_namespace
!= global_namespace
);
6272 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
6273 /* The binding level is not popped, as it might be re-opened later. */
6276 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6279 /* External entry points for do_{push_to/pop_from}_top_level. */
6282 push_to_top_level (void)
6284 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6285 do_push_to_top_level ();
6286 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6290 pop_from_top_level (void)
6292 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6293 do_pop_from_top_level ();
6294 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6297 /* External entry points for do_{push,pop}_nested_namespace. */
6300 push_nested_namespace (tree ns
)
6302 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6303 do_push_nested_namespace (ns
);
6304 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6308 pop_nested_namespace (tree ns
)
6310 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6311 gcc_assert (current_namespace
== ns
);
6312 do_pop_nested_namespace (ns
);
6313 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6316 /* Pop off extraneous binding levels left over due to syntax errors.
6317 We don't pop past namespaces, as they might be valid. */
6320 pop_everything (void)
6322 if (ENABLE_SCOPE_CHECKING
)
6323 verbatim ("XXX entering pop_everything ()\n");
6324 while (!namespace_bindings_p ())
6326 if (current_binding_level
->kind
== sk_class
)
6327 pop_nested_class ();
6331 if (ENABLE_SCOPE_CHECKING
)
6332 verbatim ("XXX leaving pop_everything ()\n");
6335 /* Emit debugging information for using declarations and directives.
6336 If input tree is overloaded fn then emit debug info for all
6340 cp_emit_debug_info_for_using (tree t
, tree context
)
6342 /* Don't try to emit any debug information if we have errors. */
6346 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6347 of a builtin function. */
6348 if (TREE_CODE (t
) == FUNCTION_DECL
6349 && DECL_EXTERNAL (t
)
6350 && DECL_BUILT_IN (t
))
6353 /* Do not supply context to imported_module_or_decl, if
6354 it is a global namespace. */
6355 if (context
== global_namespace
)
6356 context
= NULL_TREE
;
6358 t
= MAYBE_BASELINK_FUNCTIONS (t
);
6360 /* FIXME: Handle TEMPLATE_DECLs. */
6361 for (lkp_iterator
iter (t
); iter
; ++iter
)
6364 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
6366 if (building_stmt_list_p ())
6367 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
6369 debug_hooks
->imported_module_or_decl (fn
,
6370 NULL_TREE
, context
, false);
6375 #include "gt-cp-name-lookup.h"