1 /* Definitions for C++ name lookup routines.
2 Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
27 #include "stringpool.h"
28 #include "print-tree.h"
31 #include "c-family/c-pragma.h"
32 #include "gcc-rich-location.h"
33 #include "spellcheck-tree.h"
35 #include "c-family/name-hint.h"
36 #include "c-family/known-headers.h"
37 #include "c-family/c-spellcheck.h"
40 static cxx_binding
*cxx_binding_make (tree value
, tree type
);
41 static cp_binding_level
*innermost_nonclass_level (void);
42 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
44 static name_hint
maybe_suggest_missing_std_header (location_t location
,
46 static name_hint
suggest_alternatives_for_1 (location_t location
, tree name
,
47 bool suggest_misspellings
);
49 /* Slots in BINDING_VECTOR. */
52 BINDING_SLOT_CURRENT
, /* Slot for current TU. */
53 BINDING_SLOT_GLOBAL
, /* Slot for merged global module. */
54 BINDING_SLOT_PARTITION
, /* Slot for merged partition entities
57 /* Number of always-allocated slots. */
58 BINDING_SLOTS_FIXED
= BINDING_SLOT_GLOBAL
+ 1
61 /* Create an overload suitable for recording an artificial TYPE_DECL
62 and another decl. We use this machanism to implement the struct
65 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
66 #define STAT_TYPE_VISIBLE_P(N) TREE_USED (OVERLOAD_CHECK (N))
67 #define STAT_TYPE(N) TREE_TYPE (N)
68 #define STAT_DECL(N) OVL_FUNCTION (N)
69 #define STAT_VISIBLE(N) OVL_CHAIN (N)
70 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
71 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
73 /* When a STAT_HACK_P is true, OVL_USING_P and OVL_EXPORT_P are valid
74 and apply to the hacked type. */
76 /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
77 But we also need to indicate hiddenness on implicit type decls
78 (injected friend classes), and (coming soon) decls injected from
79 block-scope externs. It is too awkward to press the existing
80 overload marking for that. If we have a hidden non-function, we
81 always create a STAT_HACK, and use these two markers as needed. */
82 #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
83 #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
85 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
89 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
91 tree result
= make_node (OVERLOAD
);
93 /* Mark this as a lookup, so we can tell this is a stat hack. */
94 OVL_LOOKUP_P (result
) = true;
95 STAT_DECL (result
) = decl
;
96 STAT_TYPE (result
) = type
;
100 /* Create a local binding level for NAME. */
103 create_local_binding (cp_binding_level
*level
, tree name
)
105 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
107 LOCAL_BINDING_P (binding
) = true;
108 binding
->scope
= level
;
109 binding
->previous
= IDENTIFIER_BINDING (name
);
111 IDENTIFIER_BINDING (name
) = binding
;
116 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
117 make an empty binding if there wasn't one. */
120 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
122 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
123 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
124 create_p
? INSERT
: NO_INSERT
);
129 find_namespace_value (tree ns
, tree name
)
131 tree
*b
= find_namespace_slot (ns
, name
);
133 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
136 /* Look in *SLOT for a the binding of NAME in imported module IX.
137 Returns pointer to binding's slot, or NULL if not found. Does a
138 binary search, as this is mainly used for random access during
139 importing. Do not use for the fixed slots. */
141 static binding_slot
*
142 search_imported_binding_slot (tree
*slot
, unsigned ix
)
149 if (TREE_CODE (*slot
) != BINDING_VECTOR
)
152 unsigned clusters
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
153 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
155 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
163 unsigned half
= clusters
/ 2;
164 gcc_checking_assert (cluster
[half
].indices
[0].span
);
165 if (cluster
[half
].indices
[0].base
> ix
)
175 /* Is it in this cluster? */
176 for (unsigned off
= 0; off
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; off
++)
178 if (!cluster
->indices
[off
].span
)
180 if (cluster
->indices
[off
].base
> ix
)
183 if (cluster
->indices
[off
].base
+ cluster
->indices
[off
].span
> ix
)
184 return &cluster
->slots
[off
];
191 init_global_partition (binding_cluster
*cluster
, tree decl
)
195 if (header_module_p ())
197 else if (TREE_PUBLIC (decl
)
198 && TREE_CODE (decl
) == NAMESPACE_DECL
199 && !DECL_NAMESPACE_ALIAS (decl
))
201 else if (!get_originating_module (decl
))
206 mslot
= &cluster
[0].slots
[BINDING_SLOT_GLOBAL
];
208 mslot
= &cluster
[BINDING_SLOT_PARTITION
209 / BINDING_VECTOR_SLOTS_PER_CLUSTER
]
210 .slots
[BINDING_SLOT_PARTITION
211 % BINDING_VECTOR_SLOTS_PER_CLUSTER
];
214 decl
= ovl_make (decl
, *mslot
);
217 if (TREE_CODE (decl
) == CONST_DECL
)
219 tree type
= TREE_TYPE (decl
);
220 if (TREE_CODE (type
) == ENUMERAL_TYPE
221 && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type
)))
222 && decl
== TREE_VALUE (TYPE_VALUES (type
)))
223 /* Anonymous enums are keyed by their first enumerator, put
224 the TYPE_DECL here too. */
225 *mslot
= ovl_make (TYPE_NAME (type
), *mslot
);
229 /* Get the fixed binding slot IX. Creating the vector if CREATE is
230 non-zero. If CREATE is < 0, make sure there is at least 1 spare
231 slot for an import. (It is an error for CREATE < 0 and the slot to
235 get_fixed_binding_slot (tree
*slot
, tree name
, unsigned ix
, int create
)
237 gcc_checking_assert (ix
<= BINDING_SLOT_PARTITION
);
239 /* An assumption is that the fixed slots all reside in one cluster. */
240 gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER
>= BINDING_SLOTS_FIXED
);
242 if (!*slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
)
244 if (ix
== BINDING_SLOT_CURRENT
)
245 /* The current TU can just use slot directly. */
251 /* The partition slot is only needed when we know we're a named
253 bool partition_slot
= named_module_p ();
254 unsigned want
= ((BINDING_SLOTS_FIXED
+ partition_slot
+ (create
< 0)
255 + BINDING_VECTOR_SLOTS_PER_CLUSTER
- 1)
256 / BINDING_VECTOR_SLOTS_PER_CLUSTER
);
257 tree new_vec
= make_binding_vec (name
, want
);
258 BINDING_VECTOR_NUM_CLUSTERS (new_vec
) = want
;
259 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (new_vec
);
261 /* Initialize the fixed slots. */
262 for (unsigned jx
= BINDING_SLOTS_FIXED
; jx
--;)
264 cluster
[0].indices
[jx
].base
= 0;
265 cluster
[0].indices
[jx
].span
= 1;
266 cluster
[0].slots
[jx
] = NULL_TREE
;
271 unsigned off
= BINDING_SLOT_PARTITION
% BINDING_VECTOR_SLOTS_PER_CLUSTER
;
272 unsigned ind
= BINDING_SLOT_PARTITION
/ BINDING_VECTOR_SLOTS_PER_CLUSTER
;
273 cluster
[ind
].indices
[off
].base
= 0;
274 cluster
[ind
].indices
[off
].span
= 1;
275 cluster
[ind
].slots
[off
] = NULL_TREE
;
278 if (tree orig
= *slot
)
280 /* Propagate existing value to current slot. */
282 /* Propagate global & module entities to the global and
284 if (tree type
= MAYBE_STAT_TYPE (orig
))
285 init_global_partition (cluster
, type
);
287 for (ovl_iterator
iter (MAYBE_STAT_DECL (orig
)); iter
; ++iter
)
291 /* Internal linkage entities are in deduplicateable. */
292 init_global_partition (cluster
, decl
);
295 if (cluster
[0].slots
[BINDING_SLOT_GLOBAL
]
296 && !(TREE_CODE (orig
) == NAMESPACE_DECL
297 && !DECL_NAMESPACE_ALIAS (orig
)))
299 /* Note that we had some GMF entries. */
300 if (!STAT_HACK_P (orig
))
301 orig
= stat_hack (orig
);
303 MODULE_BINDING_GLOBAL_P (orig
) = true;
306 cluster
[0].slots
[BINDING_SLOT_CURRENT
] = orig
;
312 gcc_checking_assert (create
>= 0);
314 unsigned off
= ix
% BINDING_VECTOR_SLOTS_PER_CLUSTER
;
315 binding_cluster
&cluster
316 = BINDING_VECTOR_CLUSTER (*slot
, ix
/ BINDING_VECTOR_SLOTS_PER_CLUSTER
);
318 /* There must always be slots for these indices */
319 gcc_checking_assert (cluster
.indices
[off
].span
== 1
320 && !cluster
.indices
[off
].base
321 && !cluster
.slots
[off
].is_lazy ());
323 return reinterpret_cast<tree
*> (&cluster
.slots
[off
]);
326 /* *SLOT is a namespace binding slot. Append a slot for imported
329 static binding_slot
*
330 append_imported_binding_slot (tree
*slot
, tree name
, unsigned ix
)
332 gcc_checking_assert (ix
);
334 if (!*slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
)
335 /* Make an initial module vector. */
336 get_fixed_binding_slot (slot
, name
, BINDING_SLOT_GLOBAL
, -1);
337 else if (!BINDING_VECTOR_CLUSTER_LAST (*slot
)
338 ->indices
[BINDING_VECTOR_SLOTS_PER_CLUSTER
- 1].span
)
339 /* There is space in the last cluster. */;
340 else if (BINDING_VECTOR_NUM_CLUSTERS (*slot
)
341 != BINDING_VECTOR_ALLOC_CLUSTERS (*slot
))
342 /* There is space in the vector. */
343 BINDING_VECTOR_NUM_CLUSTERS (*slot
)++;
346 /* Extend the vector. */
347 unsigned have
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
348 unsigned want
= (have
* 3 + 1) / 2;
350 if (want
> (unsigned short)~0)
351 want
= (unsigned short)~0;
353 tree new_vec
= make_binding_vec (name
, want
);
354 BINDING_VECTOR_NUM_CLUSTERS (new_vec
) = have
+ 1;
355 memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec
),
356 BINDING_VECTOR_CLUSTER_BASE (*slot
),
357 have
* sizeof (binding_cluster
));
361 binding_cluster
*last
= BINDING_VECTOR_CLUSTER_LAST (*slot
);
362 for (unsigned off
= 0; off
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; off
++)
363 if (!last
->indices
[off
].span
)
365 /* Fill the free slot of the cluster. */
366 last
->indices
[off
].base
= ix
;
367 last
->indices
[off
].span
= 1;
368 last
->slots
[off
] = NULL_TREE
;
369 /* Check monotonicity. */
370 gcc_checking_assert (last
[off
? 0 : -1]
371 .indices
[off
? off
- 1
372 : BINDING_VECTOR_SLOTS_PER_CLUSTER
- 1]
374 return &last
->slots
[off
];
380 /* Add DECL to the list of things declared in binding level B. */
383 add_decl_to_level (cp_binding_level
*b
, tree decl
)
385 gcc_assert (b
->kind
!= sk_class
);
387 /* Make sure we don't create a circular list. xref_tag can end
388 up pushing the same artificial decl more than once. We
389 should have already detected that in update_binding. (This isn't a
390 complete verification of non-circularity.) */
391 gcc_assert (b
->names
!= decl
);
393 /* We build up the list in reverse order, and reverse it later if
395 TREE_CHAIN (decl
) = b
->names
;
398 /* If appropriate, add decl to separate list of statics. We include
399 extern variables because they might turn out to be static later.
400 It's OK for this list to contain a few false positives. */
401 if (b
->kind
== sk_namespace
402 && ((VAR_P (decl
) && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
403 || (TREE_CODE (decl
) == FUNCTION_DECL
404 && (!TREE_PUBLIC (decl
)
405 || decl_anon_ns_mem_p (decl
)
406 || DECL_DECLARED_INLINE_P (decl
)))))
407 vec_safe_push (static_decls
, decl
);
410 /* Find the binding for NAME in the local binding level B. */
413 find_local_binding (cp_binding_level
*b
, tree name
)
415 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
416 for (;; b
= b
->level_chain
)
418 if (binding
->scope
== b
)
421 /* Cleanup contours are transparent to the language. */
422 if (b
->kind
!= sk_cleanup
)
431 typedef std::pair
<tree
, tree
> using_pair
;
432 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
435 tree name
; /* The identifier being looked for. */
437 /* Usually we just add things to the VALUE binding, but we record
438 (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
439 using-decl resolution. */
440 tree value
; /* A (possibly ambiguous) set of things found. */
441 tree type
; /* A type that has been found. */
443 LOOK_want want
; /* What kind of entity we want. */
445 bool deduping
; /* Full deduping is needed because using declarations
447 vec
<tree
, va_heap
, vl_embed
> *scopes
;
448 name_lookup
*previous
; /* Previously active lookup. */
451 /* Marked scope stack for outermost name lookup. */
452 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
453 /* Currently active lookup. */
454 static name_lookup
*active
;
457 name_lookup (tree n
, LOOK_want w
= LOOK_want::NORMAL
)
458 : name (n
), value (NULL_TREE
), type (NULL_TREE
),
460 deduping (false), scopes (NULL
), previous (NULL
)
466 gcc_checking_assert (!deduping
);
470 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
471 name_lookup (const name_lookup
&);
472 name_lookup
&operator= (const name_lookup
&);
475 /* Turn on or off deduping mode. */
476 void dedup (bool state
)
478 if (deduping
!= state
)
481 lookup_mark (value
, state
);
486 static bool seen_p (tree scope
)
488 return LOOKUP_SEEN_P (scope
);
490 static bool found_p (tree scope
)
492 return LOOKUP_FOUND_P (scope
);
495 void mark_seen (tree scope
); /* Mark and add to scope vector. */
496 static void mark_found (tree scope
)
498 gcc_checking_assert (seen_p (scope
));
499 LOOKUP_FOUND_P (scope
) = true;
501 bool see_and_mark (tree scope
)
503 bool ret
= seen_p (scope
);
508 bool find_and_mark (tree scope
);
511 void preserve_state ();
512 void restore_state ();
515 static tree
ambiguous (tree thing
, tree current
);
516 void add_overload (tree fns
);
517 void add_value (tree new_val
);
518 void add_type (tree new_type
);
519 bool process_binding (tree val_bind
, tree type_bind
);
520 unsigned process_module_binding (tree val_bind
, tree type_bind
, unsigned);
521 /* Look in only namespace. */
522 bool search_namespace_only (tree scope
);
523 /* Look in namespace and its (recursive) inlines. Ignore using
524 directives. Return true if something found (inc dups). */
525 bool search_namespace (tree scope
);
526 /* Look in the using directives of namespace + inlines using
527 qualified lookup rules. */
528 bool search_usings (tree scope
);
531 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
532 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
533 vec
<tree
, va_gc
> *usings
);
534 using_queue
*queue_usings (using_queue
*queue
, int depth
,
535 vec
<tree
, va_gc
> *usings
)
538 queue
= do_queue_usings (queue
, depth
, usings
);
546 void adl_expr (tree
);
547 void adl_type (tree
);
548 void adl_template_arg (tree
);
549 void adl_class (tree
);
550 void adl_enum (tree
);
551 void adl_bases (tree
);
552 void adl_class_only (tree
);
553 void adl_namespace (tree
);
554 void adl_class_fns (tree
);
555 void adl_namespace_fns (tree
, bitmap
);
558 /* Search namespace + inlines + maybe usings as qualified lookup. */
559 bool search_qualified (tree scope
, bool usings
= true);
561 /* Search namespace + inlines + usings as unqualified lookup. */
562 bool search_unqualified (tree scope
, cp_binding_level
*);
564 /* ADL lookup of ARGS. */
565 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
568 /* Scope stack shared by all outermost lookups. This avoids us
569 allocating and freeing on every single lookup. */
570 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
572 /* Currently active lookup. */
573 name_lookup
*name_lookup::active
;
575 /* Name lookup is recursive, becase ADL can cause template
576 instatiation. This is of course a rare event, so we optimize for
577 it not happening. When we discover an active name-lookup, which
578 must be an ADL lookup, we need to unmark the marked scopes and also
579 unmark the lookup we might have been accumulating. */
582 name_lookup::preserve_state ()
587 unsigned length
= vec_safe_length (previous
->scopes
);
588 vec_safe_reserve (previous
->scopes
, length
* 2);
589 for (unsigned ix
= length
; ix
--;)
591 tree decl
= (*previous
->scopes
)[ix
];
593 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
594 LOOKUP_SEEN_P (decl
) = false;
596 /* Preserve the FOUND_P state on the interrupted lookup's
598 if (LOOKUP_FOUND_P (decl
))
600 LOOKUP_FOUND_P (decl
) = false;
601 previous
->scopes
->quick_push (decl
);
605 /* Unmark the outer partial lookup. */
606 if (previous
->deduping
)
607 lookup_mark (previous
->value
, false);
610 scopes
= shared_scopes
;
614 /* Restore the marking state of a lookup we interrupted. */
617 name_lookup::restore_state ()
619 gcc_checking_assert (!deduping
);
621 /* Unmark and empty this lookup's scope stack. */
622 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
624 tree decl
= scopes
->pop ();
625 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
626 LOOKUP_SEEN_P (decl
) = false;
627 LOOKUP_FOUND_P (decl
) = false;
635 unsigned length
= vec_safe_length (previous
->scopes
);
636 for (unsigned ix
= 0; ix
!= length
; ix
++)
638 tree decl
= (*previous
->scopes
)[ix
];
639 if (LOOKUP_SEEN_P (decl
))
641 /* The remainder of the scope stack must be recording
642 FOUND_P decls, which we want to pop off. */
645 tree decl
= previous
->scopes
->pop ();
646 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
647 && !LOOKUP_FOUND_P (decl
));
648 LOOKUP_FOUND_P (decl
) = true;
650 while (++ix
!= length
);
654 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
655 LOOKUP_SEEN_P (decl
) = true;
658 /* Remark the outer partial lookup. */
659 if (previous
->deduping
)
660 lookup_mark (previous
->value
, true);
663 shared_scopes
= scopes
;
667 name_lookup::mark_seen (tree scope
)
669 gcc_checking_assert (!seen_p (scope
));
670 LOOKUP_SEEN_P (scope
) = true;
671 vec_safe_push (scopes
, scope
);
675 name_lookup::find_and_mark (tree scope
)
677 bool result
= LOOKUP_FOUND_P (scope
);
680 LOOKUP_FOUND_P (scope
) = true;
681 if (!LOOKUP_SEEN_P (scope
))
682 vec_safe_push (scopes
, scope
);
688 /* THING and CURRENT are ambiguous, concatenate them. */
691 name_lookup::ambiguous (tree thing
, tree current
)
693 if (TREE_CODE (current
) != TREE_LIST
)
695 current
= build_tree_list (NULL_TREE
, current
);
696 TREE_TYPE (current
) = error_mark_node
;
698 current
= tree_cons (NULL_TREE
, thing
, current
);
699 TREE_TYPE (current
) = error_mark_node
;
704 /* FNS is a new overload set to add to the exising set. */
707 name_lookup::add_overload (tree fns
)
709 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
712 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
713 probe
= ovl_skip_hidden (probe
);
714 if (probe
&& TREE_CODE (probe
) == OVERLOAD
715 && OVL_DEDUP_P (probe
))
716 /* We're about to add something found by multiple paths, so need to
717 engage deduping mode. */
721 value
= lookup_maybe_add (fns
, value
, deduping
);
724 /* Add a NEW_VAL, a found value binding into the current value binding. */
727 name_lookup::add_value (tree new_val
)
729 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
730 add_overload (new_val
);
733 else if (value
== new_val
)
735 else if ((TREE_CODE (value
) == TYPE_DECL
736 && TREE_CODE (new_val
) == TYPE_DECL
737 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
738 /* Typedefs to the same type. */;
739 else if (TREE_CODE (value
) == NAMESPACE_DECL
740 && TREE_CODE (new_val
) == NAMESPACE_DECL
741 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
742 /* Namespace (possibly aliased) to the same namespace. Locate
744 value
= ORIGINAL_NAMESPACE (value
);
747 /* Disengage deduping mode. */
749 value
= ambiguous (new_val
, value
);
753 /* Add a NEW_TYPE, a found type binding into the current type binding. */
756 name_lookup::add_type (tree new_type
)
760 else if (TREE_CODE (type
) == TREE_LIST
761 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
762 type
= ambiguous (new_type
, type
);
765 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
766 true if we actually found something noteworthy. Hiddenness has
767 already been handled in the caller. */
770 name_lookup::process_binding (tree new_val
, tree new_type
)
772 /* Did we really see a type? */
774 && (want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
)
775 new_type
= NULL_TREE
;
777 /* Do we really see a value? */
779 switch (TREE_CODE (new_val
))
782 /* If we expect types or namespaces, and not templates,
783 or this is not a template class. */
784 if (bool (want
& LOOK_want::TYPE_NAMESPACE
)
785 && !DECL_TYPE_TEMPLATE_P (new_val
))
789 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
790 || (new_type
&& bool (want
& LOOK_want::TYPE
)))
794 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::TYPE
)
798 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
805 new_type
= NULL_TREE
;
808 /* Merge into the lookup */
814 return new_val
!= NULL_TREE
;
817 /* If we're importing a module containing this binding, add it to the
818 lookup set. The trickiness is with namespaces, we only want to
822 name_lookup::process_module_binding (tree new_val
, tree new_type
,
825 /* Optimize for (re-)finding a public namespace. We only need to
827 if (new_val
&& !new_type
828 && TREE_CODE (new_val
) == NAMESPACE_DECL
829 && TREE_PUBLIC (new_val
)
830 && !DECL_NAMESPACE_ALIAS (new_val
))
837 if (new_type
|| new_val
)
838 marker
|= process_binding (new_val
, new_type
);
843 /* Look in exactly namespace SCOPE. */
846 name_lookup::search_namespace_only (tree scope
)
849 if (tree
*binding
= find_namespace_slot (scope
, name
))
852 if (TREE_CODE (val
) == BINDING_VECTOR
)
854 /* I presume the binding list is going to be sparser than
855 the import bitmap. Hence iterate over the former
856 checking for bits set in the bitmap. */
857 bitmap imports
= get_import_bitmap ();
858 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (val
);
862 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
866 if (named_module_purview_p ())
870 if (STAT_HACK_P (bind
) && MODULE_BINDING_GLOBAL_P (bind
))
876 tree type
= NULL_TREE
;
879 if (STAT_HACK_P (bind
))
881 type
= STAT_TYPE (bind
);
882 value
= STAT_DECL (bind
);
884 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
886 if (STAT_TYPE_HIDDEN_P (bind
))
888 if (STAT_DECL_HIDDEN_P (bind
))
891 value
= ovl_skip_hidden (value
);
894 else if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
895 value
= ovl_skip_hidden (value
);
897 marker
= process_module_binding (value
, type
, marker
);
900 /* Scan the imported bindings. */
901 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (val
);
902 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
908 /* Do this in forward order, so we load modules in an order
910 for (; ix
--; cluster
++)
911 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
913 /* Are we importing this module? */
914 if (unsigned base
= cluster
->indices
[jx
].base
)
915 if (unsigned span
= cluster
->indices
[jx
].span
)
917 if (bitmap_bit_p (imports
, base
))
919 while (++base
, --span
);
924 if (cluster
->slots
[jx
].is_lazy ())
926 gcc_assert (cluster
->indices
[jx
].span
== 1);
927 lazy_load_binding (cluster
->indices
[jx
].base
,
928 scope
, name
, &cluster
->slots
[jx
]);
930 tree bind
= cluster
->slots
[jx
];
932 /* Load errors could mean there's nothing here. */
935 /* Extract what we can see from here. If there's no
936 stat_hack, then everything was exported. */
937 tree type
= NULL_TREE
;
940 /* If STAT_HACK_P is false, everything is visible, and
941 there's no duplication possibilities. */
942 if (STAT_HACK_P (bind
))
946 /* Do we need to engage deduplication? */
948 if (MODULE_BINDING_GLOBAL_P (bind
))
950 else if (MODULE_BINDING_PARTITION_P (bind
))
952 if (unsigned hit
= dup_detect
& dup
)
954 if ((hit
& 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val
))
956 && BINDING_VECTOR_PARTITION_DUPS_P (val
)))
962 if (STAT_TYPE_VISIBLE_P (bind
))
963 type
= STAT_TYPE (bind
);
964 bind
= STAT_VISIBLE (bind
);
967 /* And process it. */
968 marker
= process_module_binding (bind
, type
, marker
);
974 /* Only a current module binding, visible from the current module. */
975 tree bind
= *binding
;
976 tree value
= bind
, type
= NULL_TREE
;
978 if (STAT_HACK_P (bind
))
980 type
= STAT_TYPE (bind
);
981 value
= STAT_DECL (bind
);
983 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
985 if (STAT_TYPE_HIDDEN_P (bind
))
987 if (STAT_DECL_HIDDEN_P (bind
))
990 value
= ovl_skip_hidden (value
);
993 else if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
994 value
= ovl_skip_hidden (value
);
996 found
|= process_binding (value
, type
);
1003 /* Conditionally look in namespace SCOPE and inline children. */
1006 name_lookup::search_namespace (tree scope
)
1008 if (see_and_mark (scope
))
1009 /* We've visited this scope before. Return what we found then. */
1010 return found_p (scope
);
1012 /* Look in exactly namespace. */
1013 bool found
= search_namespace_only (scope
);
1015 /* Don't look into inline children, if we're looking for an
1016 anonymous name -- it must be in the current scope, if anywhere. */
1018 /* Recursively look in its inline children. */
1019 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1020 for (unsigned ix
= inlinees
->length (); ix
--;)
1021 found
|= search_namespace ((*inlinees
)[ix
]);
1029 /* Recursively follow using directives of SCOPE & its inline children.
1030 Such following is essentially a flood-fill algorithm. */
1033 name_lookup::search_usings (tree scope
)
1035 /* We do not check seen_p here, as that was already set during the
1036 namespace_only walk. */
1037 if (found_p (scope
))
1041 if (vec
<tree
, va_gc
> *usings
= NAMESPACE_LEVEL (scope
)->using_directives
)
1042 for (unsigned ix
= usings
->length (); ix
--;)
1043 found
|= search_qualified ((*usings
)[ix
], true);
1045 /* Look in its inline children. */
1046 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1047 for (unsigned ix
= inlinees
->length (); ix
--;)
1048 found
|= search_usings ((*inlinees
)[ix
]);
1056 /* Qualified namespace lookup in SCOPE.
1057 1) Look in SCOPE (+inlines). If found, we're done.
1058 2) Otherwise, if USINGS is true,
1059 recurse for every using directive of SCOPE (+inlines).
1061 Trickiness is (a) loops and (b) multiple paths to same namespace.
1062 In both cases we want to not repeat any lookups, and know whether
1063 to stop the caller's step #2. Do this via the FOUND_P marker. */
1066 name_lookup::search_qualified (tree scope
, bool usings
)
1071 found
= found_p (scope
);
1074 found
= search_namespace (scope
);
1075 if (!found
&& usings
)
1076 found
= search_usings (scope
);
1084 /* Add SCOPE to the unqualified search queue, recursively add its
1085 inlines and those via using directives. */
1087 name_lookup::using_queue
*
1088 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
1090 if (see_and_mark (scope
))
1094 tree common
= scope
;
1095 while (SCOPE_DEPTH (common
) > depth
)
1096 common
= CP_DECL_CONTEXT (common
);
1097 vec_safe_push (queue
, using_pair (common
, scope
));
1099 /* Queue its inline children. */
1100 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1101 for (unsigned ix
= inlinees
->length (); ix
--;)
1102 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
1104 /* Queue its using targets. */
1105 queue
= queue_usings (queue
, depth
, NAMESPACE_LEVEL (scope
)->using_directives
);
1110 /* Add the namespaces in USINGS to the unqualified search queue. */
1112 name_lookup::using_queue
*
1113 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
1114 vec
<tree
, va_gc
> *usings
)
1116 for (unsigned ix
= usings
->length (); ix
--;)
1117 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
1122 /* Unqualified namespace lookup in SCOPE.
1123 1) add scope+inlins to worklist.
1124 2) recursively add target of every using directive
1125 3) for each worklist item where SCOPE is common ancestor, search it
1126 4) if nothing find, scope=parent, goto 1. */
1129 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
1131 /* Make static to avoid continual reallocation. We're not
1133 static using_queue
*queue
= NULL
;
1135 int length
= vec_safe_length (queue
);
1137 /* Queue local using-directives. */
1138 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
1139 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
1141 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
1143 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
1144 int depth
= SCOPE_DEPTH (scope
);
1146 /* Queue namespaces reachable from SCOPE. */
1147 queue
= queue_namespace (queue
, depth
, scope
);
1149 /* Search every queued namespace where SCOPE is the common
1150 ancestor. Adjust the others. */
1151 unsigned ix
= length
;
1154 using_pair
&pair
= (*queue
)[ix
];
1155 while (pair
.first
== scope
)
1157 found
|= search_namespace_only (pair
.second
);
1158 pair
= queue
->pop ();
1159 if (ix
== queue
->length ())
1162 /* The depth is the same as SCOPE, find the parent scope. */
1163 if (SCOPE_DEPTH (pair
.first
) == depth
)
1164 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
1167 while (ix
< queue
->length ());
1169 if (scope
== global_namespace
)
1172 /* If looking for hidden friends, we only look in the innermost
1173 namespace scope. [namespace.memdef]/3 If a friend
1174 declaration in a non-local class first declares a class,
1175 function, class template or function template the friend is a
1176 member of the innermost enclosing namespace. See also
1177 [basic.lookup.unqual]/7 */
1178 if (bool (want
& LOOK_want::HIDDEN_FRIEND
))
1184 /* Restore to incoming length. */
1185 vec_safe_truncate (queue
, length
);
1190 /* FNS is a value binding. If it is a (set of overloaded) functions,
1191 add them into the current value. */
1194 name_lookup::add_fns (tree fns
)
1198 else if (TREE_CODE (fns
) == OVERLOAD
)
1200 if (TREE_TYPE (fns
) != unknown_type_node
)
1201 fns
= OVL_FUNCTION (fns
);
1203 else if (!DECL_DECLARES_FUNCTION_P (fns
))
1209 /* Add the overloaded fns of SCOPE. */
1212 name_lookup::adl_namespace_fns (tree scope
, bitmap imports
)
1214 if (tree
*binding
= find_namespace_slot (scope
, name
))
1216 tree val
= *binding
;
1217 if (TREE_CODE (val
) != BINDING_VECTOR
)
1218 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val
)));
1221 /* I presume the binding list is going to be sparser than
1222 the import bitmap. Hence iterate over the former
1223 checking for bits set in the bitmap. */
1224 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (val
);
1227 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
1229 /* The current TU's bindings must be visible, we don't
1230 need to check the bitmaps. */
1234 if (named_module_purview_p ())
1238 if (STAT_HACK_P (bind
) && MODULE_BINDING_GLOBAL_P (bind
))
1245 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind
)));
1248 /* Scan the imported bindings. */
1249 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (val
);
1250 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
1256 /* Do this in forward order, so we load modules in an order
1257 the user expects. */
1258 for (; ix
--; cluster
++)
1259 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
1261 /* Functions are never on merged slots. */
1262 if (!cluster
->indices
[jx
].base
1263 || cluster
->indices
[jx
].span
!= 1)
1266 /* Is this slot visible? */
1267 if (!bitmap_bit_p (imports
, cluster
->indices
[jx
].base
))
1271 if (cluster
->slots
[jx
].is_lazy ())
1272 lazy_load_binding (cluster
->indices
[jx
].base
,
1273 scope
, name
, &cluster
->slots
[jx
]);
1275 tree bind
= cluster
->slots
[jx
];
1277 /* Load errors could mean there's nothing here. */
1280 if (STAT_HACK_P (bind
))
1284 /* Do we need to engage deduplication? */
1286 if (MODULE_BINDING_GLOBAL_P (bind
))
1288 else if (MODULE_BINDING_PARTITION_P (bind
))
1290 if (unsigned hit
= dup_detect
& dup
)
1291 if ((hit
& 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val
))
1293 && BINDING_VECTOR_PARTITION_DUPS_P (val
)))
1298 bind
= STAT_VISIBLE (bind
);
1307 /* Add the hidden friends of SCOPE. */
1310 name_lookup::adl_class_fns (tree type
)
1313 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
1314 list
; list
= TREE_CHAIN (list
))
1315 if (name
== FRIEND_NAME (list
))
1317 tree context
= NULL_TREE
; /* Lazily computed. */
1318 for (tree friends
= FRIEND_DECLS (list
); friends
;
1319 friends
= TREE_CHAIN (friends
))
1321 tree fn
= TREE_VALUE (friends
);
1323 /* Only interested in global functions with potentially hidden
1324 (i.e. unqualified) declarations. */
1326 context
= decl_namespace_context (type
);
1327 if (CP_DECL_CONTEXT (fn
) != context
)
1332 /* Template specializations are never found by name lookup.
1333 (Templates themselves can be found, but not template
1334 specializations.) */
1335 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
1343 /* Find the containing non-inlined namespace, add it and all its
1347 name_lookup::adl_namespace (tree scope
)
1349 if (see_and_mark (scope
))
1352 /* Look down into inline namespaces. */
1353 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1354 for (unsigned ix
= inlinees
->length (); ix
--;)
1355 adl_namespace ((*inlinees
)[ix
]);
1357 if (DECL_NAMESPACE_INLINE_P (scope
))
1359 adl_namespace (CP_DECL_CONTEXT (scope
));
1362 /* Adds the class and its friends to the lookup structure. */
1365 name_lookup::adl_class_only (tree type
)
1367 /* Backend-built structures, such as __builtin_va_list, aren't
1368 affected by all this. */
1369 if (!CLASS_TYPE_P (type
))
1372 type
= TYPE_MAIN_VARIANT (type
);
1374 if (see_and_mark (type
))
1377 tree context
= decl_namespace_context (type
);
1378 adl_namespace (context
);
1381 /* Adds the class and its bases to the lookup structure.
1382 Returns true on error. */
1385 name_lookup::adl_bases (tree type
)
1387 adl_class_only (type
);
1389 /* Process baseclasses. */
1390 if (tree binfo
= TYPE_BINFO (type
))
1395 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1396 adl_bases (BINFO_TYPE (base_binfo
));
1400 /* Adds everything associated with a class argument type to the lookup
1403 If T is a class type (including unions), its associated classes are: the
1404 class itself; the class of which it is a member, if any; and its direct
1405 and indirect base classes. Its associated namespaces are the namespaces
1406 of which its associated classes are members. Furthermore, if T is a
1407 class template specialization, its associated namespaces and classes
1408 also include: the namespaces and classes associated with the types of
1409 the template arguments provided for template type parameters (excluding
1410 template template parameters); the namespaces of which any template
1411 template arguments are members; and the classes of which any member
1412 templates used as template template arguments are members. [ Note:
1413 non-type template arguments do not contribute to the set of associated
1414 namespaces. --end note] */
1417 name_lookup::adl_class (tree type
)
1419 /* Backend build structures, such as __builtin_va_list, aren't
1420 affected by all this. */
1421 if (!CLASS_TYPE_P (type
))
1424 type
= TYPE_MAIN_VARIANT (type
);
1426 /* We don't set found here because we have to have set seen first,
1427 which is done in the adl_bases walk. */
1431 complete_type (type
);
1435 if (TYPE_CLASS_SCOPE_P (type
))
1436 adl_class_only (TYPE_CONTEXT (type
));
1438 /* Process template arguments. */
1439 if (CLASSTYPE_TEMPLATE_INFO (type
)
1440 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
1442 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1443 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
1444 adl_template_arg (TREE_VEC_ELT (list
, i
));
1449 name_lookup::adl_enum (tree type
)
1451 type
= TYPE_MAIN_VARIANT (type
);
1452 if (see_and_mark (type
))
1455 if (TYPE_CLASS_SCOPE_P (type
))
1456 adl_class_only (TYPE_CONTEXT (type
));
1458 adl_namespace (decl_namespace_context (type
));
1462 name_lookup::adl_expr (tree expr
)
1467 gcc_assert (!TYPE_P (expr
));
1469 if (TREE_TYPE (expr
) != unknown_type_node
)
1471 adl_type (unlowered_expr_type (expr
));
1475 if (TREE_CODE (expr
) == ADDR_EXPR
)
1476 expr
= TREE_OPERAND (expr
, 0);
1477 if (TREE_CODE (expr
) == COMPONENT_REF
1478 || TREE_CODE (expr
) == OFFSET_REF
)
1479 expr
= TREE_OPERAND (expr
, 1);
1480 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
1483 for (lkp_iterator
iter (expr
); iter
; ++iter
)
1484 adl_type (TREE_TYPE (*iter
));
1485 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
1487 /* The working paper doesn't currently say how to handle
1488 template-id arguments. The sensible thing would seem to be
1489 to handle the list of template candidates like a normal
1490 overload set, and handle the template arguments like we do
1491 for class template specializations. */
1493 /* First the templates. */
1494 adl_expr (TREE_OPERAND (expr
, 0));
1496 /* Now the arguments. */
1497 if (tree args
= TREE_OPERAND (expr
, 1))
1498 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
1499 adl_template_arg (TREE_VEC_ELT (args
, ix
));
1504 name_lookup::adl_type (tree type
)
1509 if (TYPE_PTRDATAMEM_P (type
))
1511 /* Pointer to member: associate class type and value type. */
1512 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
1513 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
1517 switch (TREE_CODE (type
))
1520 if (TYPE_PTRMEMFUNC_P (type
))
1522 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
1531 /* The basetype is referenced in the first arg type, so just
1534 /* Associate the parameter types. */
1535 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
1536 adl_type (TREE_VALUE (args
));
1540 case REFERENCE_TYPE
:
1542 adl_type (TREE_TYPE (type
));
1550 gcc_assert (type
== unknown_type_node
1551 || type
== init_list_type_node
);
1554 case TYPE_PACK_EXPANSION
:
1555 adl_type (PACK_EXPANSION_PATTERN (type
));
1563 /* Adds everything associated with a template argument to the lookup
1567 name_lookup::adl_template_arg (tree arg
)
1569 /* [basic.lookup.koenig]
1571 If T is a template-id, its associated namespaces and classes are
1572 ... the namespaces and classes associated with the types of the
1573 template arguments provided for template type parameters
1574 (excluding template template parameters); the namespaces in which
1575 any template template arguments are defined; and the classes in
1576 which any member templates used as template template arguments
1577 are defined. [Note: non-type template arguments do not
1578 contribute to the set of associated namespaces. ] */
1580 /* Consider first template template arguments. */
1581 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1582 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1584 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1586 tree ctx
= CP_DECL_CONTEXT (arg
);
1588 /* It's not a member template. */
1589 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1590 adl_namespace (ctx
);
1591 /* Otherwise, it must be member template. */
1593 adl_class_only (ctx
);
1595 /* It's an argument pack; handle it recursively. */
1596 else if (ARGUMENT_PACK_P (arg
))
1598 tree args
= ARGUMENT_PACK_ARGS (arg
);
1599 int i
, len
= TREE_VEC_LENGTH (args
);
1600 for (i
= 0; i
< len
; ++i
)
1601 adl_template_arg (TREE_VEC_ELT (args
, i
));
1603 /* It's not a template template argument, but it is a type template
1605 else if (TYPE_P (arg
))
1609 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1610 the call arguments. */
1613 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1615 gcc_checking_assert (!vec_safe_length (scopes
));
1617 /* Gather each associated entity onto the lookup's scope list. */
1621 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1622 /* OMP reduction operators put an ADL-significant type as the
1629 if (vec_safe_length (scopes
))
1631 /* Now do the lookups. */
1636 /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1637 bitmap inst_path
= NULL
;
1638 /* VISIBLE is the regular import bitmap. */
1639 bitmap visible
= visible_instantiation_path (&inst_path
);
1641 for (unsigned ix
= scopes
->length (); ix
--;)
1643 tree scope
= (*scopes
)[ix
];
1644 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
1645 adl_namespace_fns (scope
, visible
);
1648 if (RECORD_OR_UNION_TYPE_P (scope
))
1649 adl_class_fns (scope
);
1651 /* During 2nd phase ADL: Any exported declaration D in N
1652 declared within the purview of a named module M
1653 (10.2) is visible if there is an associated entity
1654 attached to M with the same innermost enclosing
1655 non-inline namespace as D.
1656 [basic.lookup.argdep]/4.4 */
1659 /* Not 2nd phase. */
1662 tree ctx
= CP_DECL_CONTEXT (TYPE_NAME (scope
));
1663 if (TREE_CODE (ctx
) != NAMESPACE_DECL
)
1664 /* Not namespace-scope class. */
1667 tree origin
= get_originating_module_decl (TYPE_NAME (scope
));
1668 tree not_tmpl
= STRIP_TEMPLATE (origin
);
1669 if (!DECL_LANG_SPECIFIC (not_tmpl
)
1670 || !DECL_MODULE_IMPORT_P (not_tmpl
))
1674 unsigned module
= get_importing_module (origin
);
1676 if (!bitmap_bit_p (inst_path
, module
))
1677 /* Not on path of instantiation. */
1680 if (bitmap_bit_p (visible
, module
))
1681 /* If the module was in the visible set, we'll look at
1682 its namespace partition anyway. */
1685 if (tree
*slot
= find_namespace_slot (ctx
, name
, false))
1686 if (binding_slot
*mslot
= search_imported_binding_slot (slot
, module
))
1688 if (mslot
->is_lazy ())
1689 lazy_load_binding (module
, ctx
, name
, mslot
);
1691 if (tree bind
= *mslot
)
1693 /* We must turn on deduping, because some other class
1694 from this module might also be in this namespace. */
1697 /* Add the exported fns */
1698 if (STAT_HACK_P (bind
))
1699 add_fns (STAT_VISIBLE (bind
));
1712 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1713 static void consider_binding_level (tree name
,
1714 best_match
<tree
, const char *> &bm
,
1715 cp_binding_level
*lvl
,
1716 bool look_within_fields
,
1717 enum lookup_name_fuzzy_kind kind
);
1718 static void diagnose_name_conflict (tree
, tree
);
1720 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1721 don't add duplicates to it. ARGS is the vector of call
1722 arguments (which will not be empty). */
1725 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1727 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
1728 name_lookup
lookup (name
);
1729 return lookup
.search_adl (fns
, args
);
1732 /* FNS is an overload set of conversion functions. Return the
1733 overloads converting to TYPE. */
1736 extract_conversion_operator (tree fns
, tree type
)
1738 tree convs
= NULL_TREE
;
1739 tree tpls
= NULL_TREE
;
1741 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1743 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1744 convs
= lookup_add (*iter
, convs
);
1746 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1747 tpls
= lookup_add (*iter
, tpls
);
1756 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1759 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1761 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1763 unsigned mid
= (lo
+ hi
) / 2;
1764 tree binding
= (*member_vec
)[mid
];
1765 tree binding_name
= OVL_NAME (binding
);
1767 if (binding_name
> name
)
1769 else if (binding_name
< name
)
1778 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1781 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1783 for (int ix
= member_vec
->length (); ix
--;)
1784 if (tree binding
= (*member_vec
)[ix
])
1785 if (OVL_NAME (binding
) == name
)
1791 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1794 fields_linear_search (tree klass
, tree name
, bool want_type
)
1796 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1800 if (TREE_CODE (decl
) == FIELD_DECL
1801 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1803 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
, want_type
))
1807 if (DECL_NAME (decl
) != name
)
1810 if (TREE_CODE (decl
) == USING_DECL
)
1812 decl
= strip_using_decl (decl
);
1813 if (is_overloaded_fn (decl
))
1817 if (DECL_DECLARES_FUNCTION_P (decl
))
1818 /* Functions are found separately. */
1821 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1828 /* Look for NAME member inside of anonymous aggregate ANON. Although
1829 such things should only contain FIELD_DECLs, we check that too
1830 late, and would give very confusing errors if we weren't
1834 search_anon_aggr (tree anon
, tree name
, bool want_type
)
1836 gcc_assert (COMPLETE_TYPE_P (anon
));
1837 tree ret
= get_class_binding_direct (anon
, name
, want_type
);
1841 /* Look for NAME as an immediate member of KLASS (including
1842 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1843 regular search. >0 to get a type binding (if there is one) and <0
1844 if you want (just) the member function binding.
1846 Use this if you do not want lazy member creation. */
1849 get_class_binding_direct (tree klass
, tree name
, bool want_type
)
1851 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1853 /* Conversion operators can only be found by the marker conversion
1855 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1856 tree lookup
= conv_op
? conv_op_identifier
: name
;
1857 tree val
= NULL_TREE
;
1858 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1860 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1862 val
= member_vec_binary_search (member_vec
, lookup
);
1865 else if (STAT_HACK_P (val
))
1866 val
= want_type
? STAT_TYPE (val
) : STAT_DECL (val
);
1867 else if (want_type
&& !DECL_DECLARES_TYPE_P (val
))
1872 if (member_vec
&& !want_type
)
1873 val
= member_vec_linear_search (member_vec
, lookup
);
1875 if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_DEDUP_P (val
)))
1876 /* Dependent using declarations are a 'field', make sure we
1877 return that even if we saw an overload already. */
1878 if (tree field_val
= fields_linear_search (klass
, lookup
, want_type
))
1882 else if (TREE_CODE (field_val
) == USING_DECL
)
1883 val
= ovl_make (field_val
, val
);
1887 /* Extract the conversion operators asked for, unless the general
1888 conversion operator was requested. */
1891 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1892 val
= OVL_CHAIN (val
);
1893 if (tree type
= TREE_TYPE (name
))
1894 val
= extract_conversion_operator (val
, type
);
1900 /* We're about to lookup NAME in KLASS. Make sure any lazily declared
1901 members are now declared. */
1904 maybe_lazily_declare (tree klass
, tree name
)
1906 /* See big comment anout module_state::write_pendings regarding adding a check
1909 lazy_load_pendings (TYPE_NAME (klass
));
1911 /* Lazily declare functions, if we're going to search these. */
1912 if (IDENTIFIER_CTOR_P (name
))
1914 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1915 lazily_declare_fn (sfk_constructor
, klass
);
1916 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1917 lazily_declare_fn (sfk_copy_constructor
, klass
);
1918 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1919 lazily_declare_fn (sfk_move_constructor
, klass
);
1921 else if (IDENTIFIER_DTOR_P (name
))
1923 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1924 lazily_declare_fn (sfk_destructor
, klass
);
1926 else if (name
== assign_op_identifier
)
1928 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1929 lazily_declare_fn (sfk_copy_assignment
, klass
);
1930 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1931 lazily_declare_fn (sfk_move_assignment
, klass
);
1935 /* Look for NAME's binding in exactly KLASS. See
1936 get_class_binding_direct for argument description. Does lazy
1937 special function creation as necessary. */
1940 get_class_binding (tree klass
, tree name
, bool want_type
/*=false*/)
1942 klass
= complete_type (klass
);
1944 if (COMPLETE_TYPE_P (klass
))
1945 maybe_lazily_declare (klass
, name
);
1947 return get_class_binding_direct (klass
, name
, want_type
);
1950 /* Find the slot containing overloads called 'NAME'. If there is no
1951 such slot and the class is complete, create an empty one, at the
1952 correct point in the sorted member vector. Otherwise return NULL.
1953 Deals with conv_op marker handling. */
1956 find_member_slot (tree klass
, tree name
)
1958 bool complete_p
= COMPLETE_TYPE_P (klass
);
1960 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1963 vec_alloc (member_vec
, 8);
1964 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1966 /* If the class is complete but had no member_vec, we need to
1967 add the TYPE_FIELDS into it. We're also most likely to be
1968 adding ctors & dtors, so ask for 6 spare slots (the
1969 abstract cdtors and their clones). */
1970 member_vec
= set_class_bindings (klass
, 6);
1973 if (IDENTIFIER_CONV_OP_P (name
))
1974 name
= conv_op_identifier
;
1976 unsigned ix
, length
= member_vec
->length ();
1977 for (ix
= 0; ix
< length
; ix
++)
1979 tree
*slot
= &(*member_vec
)[ix
];
1980 tree fn_name
= OVL_NAME (*slot
);
1982 if (fn_name
== name
)
1984 /* If we found an existing slot, it must be a function set.
1985 Even with insertion after completion, because those only
1986 happen with artificial fns that have unspellable names.
1987 This means we do not have to deal with the stat hack
1989 gcc_checking_assert (OVL_P (*slot
));
1990 if (name
== conv_op_identifier
)
1992 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
1993 /* Skip the conv-op marker. */
1994 slot
= &OVL_CHAIN (*slot
);
1999 if (complete_p
&& fn_name
> name
)
2003 /* No slot found, add one if the class is complete. */
2006 /* Do exact allocation, as we don't expect to add many. */
2007 gcc_assert (name
!= conv_op_identifier
);
2008 vec_safe_reserve_exact (member_vec
, 1);
2009 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2010 member_vec
->quick_insert (ix
, NULL_TREE
);
2011 return &(*member_vec
)[ix
];
2017 /* KLASS is an incomplete class to which we're adding a method NAME.
2018 Add a slot and deal with conv_op marker handling. */
2021 add_member_slot (tree klass
, tree name
)
2023 gcc_assert (!COMPLETE_TYPE_P (klass
));
2025 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2026 vec_safe_push (member_vec
, NULL_TREE
);
2027 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2029 tree
*slot
= &member_vec
->last ();
2030 if (IDENTIFIER_CONV_OP_P (name
))
2032 /* Install the marker prefix. */
2033 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
2034 slot
= &OVL_CHAIN (*slot
);
2040 /* Comparison function to compare two MEMBER_VEC entries by name.
2041 Because we can have duplicates during insertion of TYPE_FIELDS, we
2042 do extra checking so deduping doesn't have to deal with so many
2046 member_name_cmp (const void *a_p
, const void *b_p
)
2048 tree a
= *(const tree
*)a_p
;
2049 tree b
= *(const tree
*)b_p
;
2050 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
2051 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
2053 gcc_checking_assert (name_a
&& name_b
);
2054 if (name_a
!= name_b
)
2055 return name_a
< name_b
? -1 : +1;
2057 if (name_a
== conv_op_identifier
)
2059 /* Strip the conv-op markers. */
2060 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
2061 && OVL_FUNCTION (b
) == conv_op_marker
);
2066 if (TREE_CODE (a
) == OVERLOAD
)
2067 a
= OVL_FUNCTION (a
);
2068 if (TREE_CODE (b
) == OVERLOAD
)
2069 b
= OVL_FUNCTION (b
);
2071 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
2072 if (TREE_CODE (a
) != TREE_CODE (b
))
2074 /* If one of them is a TYPE_DECL, it loses. */
2075 if (TREE_CODE (a
) == TYPE_DECL
)
2077 else if (TREE_CODE (b
) == TYPE_DECL
)
2080 /* If one of them is a USING_DECL, it loses. */
2081 if (TREE_CODE (a
) == USING_DECL
)
2083 else if (TREE_CODE (b
) == USING_DECL
)
2086 /* There are no other cases with different kinds of decls, as
2087 duplicate detection should have kicked in earlier. However,
2088 some erroneous cases get though. */
2089 gcc_assert (errorcount
);
2092 /* Using source location would be the best thing here, but we can
2093 get identically-located decls in the following circumstances:
2095 1) duplicate artificial type-decls for the same type.
2097 2) pack expansions of using-decls.
2099 We should not be doing #1, but in either case it doesn't matter
2100 how we order these. Use UID as a proxy for source ordering, so
2101 that identically-located decls still have a well-defined stable
2103 if (DECL_UID (a
) != DECL_UID (b
))
2104 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
2105 gcc_assert (a
== b
);
2110 gt_pointer_operator new_value
;
2114 /* This routine compares two fields like member_name_cmp but using the
2115 pointer operator in resort_field_decl_data. We don't have to deal
2116 with duplicates here. */
2119 resort_member_name_cmp (const void *a_p
, const void *b_p
)
2121 tree a
= *(const tree
*)a_p
;
2122 tree b
= *(const tree
*)b_p
;
2123 tree name_a
= OVL_NAME (a
);
2124 tree name_b
= OVL_NAME (b
);
2126 resort_data
.new_value (&name_a
, resort_data
.cookie
);
2127 resort_data
.new_value (&name_b
, resort_data
.cookie
);
2129 gcc_checking_assert (name_a
!= name_b
);
2131 return name_a
< name_b
? -1 : +1;
2134 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2137 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
2138 gt_pointer_operator new_value
, void* cookie
)
2140 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
2142 resort_data
.new_value
= new_value
;
2143 resort_data
.cookie
= cookie
;
2144 member_vec
->qsort (resort_member_name_cmp
);
2148 /* Recursively count the number of fields in KLASS, including anonymous
2152 count_class_fields (tree klass
)
2154 unsigned n_fields
= 0;
2156 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
2157 if (DECL_DECLARES_FUNCTION_P (fields
))
2158 /* Functions are dealt with separately. */;
2159 else if (TREE_CODE (fields
) == FIELD_DECL
2160 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2161 n_fields
+= count_class_fields (TREE_TYPE (fields
));
2162 else if (DECL_NAME (fields
))
2168 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
2169 Recurse for anonymous members. MEMBER_VEC must have space. */
2172 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
2174 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
2175 if (DECL_DECLARES_FUNCTION_P (fields
))
2176 /* Functions are handled separately. */;
2177 else if (TREE_CODE (fields
) == FIELD_DECL
2178 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2179 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
2180 else if (DECL_NAME (fields
))
2182 tree field
= fields
;
2183 /* Mark a conv-op USING_DECL with the conv-op-marker. */
2184 if (TREE_CODE (field
) == USING_DECL
2185 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
2186 field
= ovl_make (conv_op_marker
, field
);
2187 member_vec
->quick_push (field
);
2191 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
2192 MEMBER_VEC must have space. */
2195 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
2197 for (tree values
= TYPE_VALUES (enumtype
);
2198 values
; values
= TREE_CHAIN (values
))
2199 member_vec
->quick_push (TREE_VALUE (values
));
2202 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
2203 DeDup adjacent DECLS of the same name. We already dealt with
2204 conflict resolution when adding the fields or methods themselves.
2205 There are three cases (which could all be combined):
2206 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
2207 2) a USING_DECL and an overload. If the USING_DECL is dependent,
2208 it wins. Otherwise the OVERLOAD does.
2209 3) two USING_DECLS. ...
2211 member_name_cmp will have ordered duplicates as
2212 <fns><using><type> */
2215 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
2217 unsigned len
= member_vec
->length ();
2223 tree name
= OVL_NAME ((*member_vec
)[0]);
2224 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
2226 tree current
= NULL_TREE
;
2227 tree to_type
= NULL_TREE
;
2228 tree to_using
= NULL_TREE
;
2229 tree marker
= NULL_TREE
;
2231 for (jx
= ix
; jx
< len
; jx
++)
2233 tree next
= (*member_vec
)[jx
];
2236 tree next_name
= OVL_NAME (next
);
2237 if (next_name
!= name
)
2244 if (IDENTIFIER_CONV_OP_P (name
))
2247 next
= OVL_CHAIN (next
);
2250 if (TREE_CODE (next
) == USING_DECL
)
2252 if (IDENTIFIER_CTOR_P (name
))
2253 /* Dependent inherited ctor. */
2256 next
= strip_using_decl (next
);
2257 if (TREE_CODE (next
) == USING_DECL
)
2263 if (is_overloaded_fn (next
))
2267 if (DECL_DECLARES_TYPE_P (next
))
2282 current
= ovl_make (to_using
, current
);
2290 current
= stat_hack (current
, to_type
);
2297 OVL_CHAIN (marker
) = current
;
2300 (*member_vec
)[store
++] = current
;
2304 while (store
++ < len
)
2308 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
2309 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
2310 know there must be at least 1 field -- the self-reference
2311 TYPE_DECL, except for anon aggregates, which will have at least
2312 one field anyway. If EXTRA < 0, always create the vector. */
2315 set_class_bindings (tree klass
, int extra
)
2317 unsigned n_fields
= count_class_fields (klass
);
2318 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2320 if (member_vec
|| n_fields
>= 8 || extra
< 0)
2322 /* Append the new fields. */
2323 vec_safe_reserve_exact (member_vec
, n_fields
+ (extra
>= 0 ? extra
: 0));
2324 member_vec_append_class_fields (member_vec
, klass
);
2329 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2330 member_vec
->qsort (member_name_cmp
);
2331 member_vec_dedup (member_vec
);
2337 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
2340 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
2343 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2345 /* The enum bindings will already be on the TYPE_FIELDS, so don't
2346 count them twice. */
2348 n_fields
= count_class_fields (klass
);
2350 n_fields
= list_length (TYPE_VALUES (enumtype
));
2352 if (member_vec
|| n_fields
>= 8)
2354 vec_safe_reserve_exact (member_vec
, n_fields
);
2355 if (CLASSTYPE_MEMBER_VEC (klass
))
2356 member_vec_append_enum_values (member_vec
, enumtype
);
2358 member_vec_append_class_fields (member_vec
, klass
);
2359 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2360 member_vec
->qsort (member_name_cmp
);
2361 member_vec_dedup (member_vec
);
2365 /* The binding oracle; see cp-tree.h. */
2367 cp_binding_oracle_function
*cp_binding_oracle
;
2369 /* If we have a binding oracle, ask it for all namespace-scoped
2370 definitions of NAME. */
2373 query_oracle (tree name
)
2375 if (!cp_binding_oracle
)
2378 /* LOOKED_UP holds the set of identifiers that we have already
2379 looked up with the oracle. */
2380 static hash_set
<tree
> looked_up
;
2381 if (looked_up
.add (name
))
2384 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
2387 #ifndef ENABLE_SCOPE_CHECKING
2388 # define ENABLE_SCOPE_CHECKING 0
2390 # define ENABLE_SCOPE_CHECKING 1
2393 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
2395 static GTY((deletable
)) cxx_binding
*free_bindings
;
2397 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
2401 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
2403 binding
->value
= value
;
2404 binding
->type
= type
;
2405 binding
->previous
= NULL
;
2408 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2410 static cxx_binding
*
2411 cxx_binding_make (tree value
, tree type
)
2413 cxx_binding
*binding
= free_bindings
;
2416 free_bindings
= binding
->previous
;
2418 binding
= ggc_alloc
<cxx_binding
> ();
2420 /* Clear flags by default. */
2421 LOCAL_BINDING_P (binding
) = false;
2422 INHERITED_VALUE_BINDING_P (binding
) = false;
2423 HIDDEN_TYPE_BINDING_P (binding
) = false;
2425 cxx_binding_init (binding
, value
, type
);
2430 /* Put BINDING back on the free list. */
2433 cxx_binding_free (cxx_binding
*binding
)
2435 binding
->scope
= NULL
;
2436 binding
->previous
= free_bindings
;
2437 free_bindings
= binding
;
2440 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2441 bindings) in the class scope indicated by SCOPE. */
2443 static cxx_binding
*
2444 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
2446 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
2447 cxx_binding
*binding
= cb
.base
;
2448 vec_safe_push (scope
->class_shadowed
, cb
);
2449 binding
->scope
= scope
;
2453 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2454 level at which this declaration is being bound. */
2457 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2459 cxx_binding
*binding
;
2461 if (level
!= class_binding_level
)
2463 binding
= cxx_binding_make (decl
, NULL_TREE
);
2464 binding
->scope
= level
;
2467 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2469 /* Now, fill in the binding information. */
2470 binding
->previous
= IDENTIFIER_BINDING (id
);
2471 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2473 /* And put it on the front of the list of bindings for ID. */
2474 IDENTIFIER_BINDING (id
) = binding
;
2477 /* Remove the binding for DECL which should be the innermost binding
2481 pop_local_binding (tree id
, tree decl
)
2483 if (!id
|| IDENTIFIER_ANON_P (id
))
2484 /* It's easiest to write the loops that call this function without
2485 checking whether or not the entities involved have names. We
2486 get here for such an entity. */
2489 /* Get the innermost binding for ID. */
2490 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2492 /* The name should be bound. */
2493 gcc_assert (binding
!= NULL
);
2495 /* The DECL will be either the ordinary binding or the type binding
2496 for this identifier. Remove that binding. We don't have to
2497 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2499 if (binding
->value
== decl
)
2500 binding
->value
= NULL_TREE
;
2503 gcc_checking_assert (binding
->type
== decl
);
2504 binding
->type
= NULL_TREE
;
2507 if (!binding
->value
&& !binding
->type
)
2509 /* We're completely done with the innermost binding for this
2510 identifier. Unhook it from the list of bindings. */
2511 IDENTIFIER_BINDING (id
) = binding
->previous
;
2513 /* Add it to the free list. */
2514 cxx_binding_free (binding
);
2518 /* Remove the bindings for the decls of the current level and leave
2519 the current scope. */
2522 pop_bindings_and_leave_scope (void)
2524 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2526 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2527 tree name
= OVL_NAME (decl
);
2529 pop_local_binding (name
, decl
);
2535 /* Strip non dependent using declarations. If DECL is dependent,
2536 surreptitiously create a typename_type and return it. */
2539 strip_using_decl (tree decl
)
2541 if (decl
== NULL_TREE
)
2544 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2545 decl
= USING_DECL_DECLS (decl
);
2547 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2548 && USING_DECL_TYPENAME_P (decl
))
2550 /* We have found a type introduced by a using
2551 declaration at class scope that refers to a dependent
2554 using typename :: [opt] nested-name-specifier unqualified-id ;
2556 decl
= make_typename_type (USING_DECL_SCOPE (decl
),
2558 typename_type
, tf_error
);
2559 if (decl
!= error_mark_node
)
2560 decl
= TYPE_NAME (decl
);
2566 /* Return true if OVL is an overload for an anticipated builtin. */
2569 anticipated_builtin_p (tree ovl
)
2571 return (TREE_CODE (ovl
) == OVERLOAD
2572 && OVL_HIDDEN_P (ovl
)
2573 && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl
)));
2576 /* BINDING records an existing declaration for a name in the current scope.
2577 But, DECL is another declaration for that same identifier in the
2578 same scope. This is the `struct stat' hack whereby a non-typedef
2579 class name or enum-name can be bound at the same level as some other
2583 A class name (9.1) or enumeration name (7.2) can be hidden by the
2584 name of an object, function, or enumerator declared in the same scope.
2585 If a class or enumeration name and an object, function, or enumerator
2586 are declared in the same scope (in any order) with the same name, the
2587 class or enumeration name is hidden wherever the object, function, or
2588 enumerator name is visible.
2590 It's the responsibility of the caller to check that
2591 inserting this name is valid here. Returns nonzero if the new binding
2595 supplement_binding (cxx_binding
*binding
, tree decl
)
2597 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
2599 tree bval
= binding
->value
;
2601 tree target_bval
= strip_using_decl (bval
);
2602 tree target_decl
= strip_using_decl (decl
);
2604 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2605 && target_decl
!= target_bval
2606 && (TREE_CODE (target_bval
) != TYPE_DECL
2607 /* We allow pushing an enum multiple times in a class
2608 template in order to handle late matching of underlying
2609 type on an opaque-enum-declaration followed by an
2611 || (processing_template_decl
2612 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2613 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2614 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2615 (TREE_TYPE (target_decl
)))
2616 || dependent_type_p (ENUM_UNDERLYING_TYPE
2617 (TREE_TYPE (target_bval
)))))))
2618 /* The new name is the type name. */
2619 binding
->type
= decl
;
2620 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2621 an inherited type-binding out of the way to make room
2622 for a new value binding. */
2624 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2625 has been used in a non-class scope prior declaration.
2626 In that case, we should have already issued a
2627 diagnostic; for graceful error recovery purpose, pretend
2628 this was the intended declaration for that name. */
2629 || target_bval
== error_mark_node
2630 /* If TARGET_BVAL is anticipated but has not yet been
2631 declared, pretend it is not there at all. */
2632 || anticipated_builtin_p (target_bval
))
2633 binding
->value
= decl
;
2634 else if (TREE_CODE (target_bval
) == TYPE_DECL
2635 && DECL_ARTIFICIAL (target_bval
)
2636 && target_decl
!= target_bval
2637 && (TREE_CODE (target_decl
) != TYPE_DECL
2638 || same_type_p (TREE_TYPE (target_decl
),
2639 TREE_TYPE (target_bval
))))
2641 /* The old binding was a type name. It was placed in
2642 VALUE field because it was thought, at the point it was
2643 declared, to be the only entity with such a name. Move the
2644 type name into the type slot; it is now hidden by the new
2646 binding
->type
= bval
;
2647 binding
->value
= decl
;
2648 binding
->value_is_inherited
= false;
2650 else if (TREE_CODE (target_bval
) == TYPE_DECL
2651 && TREE_CODE (target_decl
) == TYPE_DECL
2652 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2653 && binding
->scope
->kind
!= sk_class
2654 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2655 /* If either type involves template parameters, we must
2656 wait until instantiation. */
2657 || uses_template_parms (TREE_TYPE (target_decl
))
2658 || uses_template_parms (TREE_TYPE (target_bval
))))
2659 /* We have two typedef-names, both naming the same type to have
2660 the same name. In general, this is OK because of:
2664 In a given scope, a typedef specifier can be used to redefine
2665 the name of any type declared in that scope to refer to the
2666 type to which it already refers.
2668 However, in class scopes, this rule does not apply due to the
2669 stricter language in [class.mem] prohibiting redeclarations of
2672 /* There can be two block-scope declarations of the same variable,
2673 so long as they are `extern' declarations. However, there cannot
2674 be two declarations of the same static data member:
2678 A member shall not be declared twice in the
2679 member-specification. */
2680 else if (VAR_P (target_decl
)
2681 && VAR_P (target_bval
)
2682 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2683 && !DECL_CLASS_SCOPE_P (target_decl
))
2685 duplicate_decls (decl
, binding
->value
);
2688 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2689 && TREE_CODE (bval
) == NAMESPACE_DECL
2690 && DECL_NAMESPACE_ALIAS (decl
)
2691 && DECL_NAMESPACE_ALIAS (bval
)
2692 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2693 /* [namespace.alias]
2695 In a declarative region, a namespace-alias-definition can be
2696 used to redefine a namespace-alias declared in that declarative
2697 region to refer only to the namespace to which it already
2700 else if (TREE_CODE (bval
) == USING_DECL
2701 && CONST_DECL_USING_P (decl
))
2702 /* Let the clone hide the using-decl that introduced it. */
2703 binding
->value
= decl
;
2706 if (!error_operand_p (bval
))
2707 diagnose_name_conflict (decl
, bval
);
2714 /* Diagnose a name conflict between DECL and BVAL. */
2717 diagnose_name_conflict (tree decl
, tree bval
)
2719 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2720 && TREE_CODE (decl
) != NAMESPACE_DECL
2721 && !DECL_DECLARES_FUNCTION_P (decl
)
2722 && (TREE_CODE (decl
) != TYPE_DECL
2723 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2724 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2726 if (concept_definition_p (decl
))
2727 error ("redeclaration of %q#D with different template parameters",
2730 error ("redeclaration of %q#D", decl
);
2733 error ("%q#D conflicts with a previous declaration", decl
);
2735 inform (location_of (bval
), "previous declaration %q#D", bval
);
2738 /* Replace BINDING's current value on its scope's name list with
2742 update_local_overload (cxx_binding
*binding
, tree newval
)
2746 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2747 if (*d
== binding
->value
)
2749 /* Stitch new list node in. */
2750 *d
= tree_cons (DECL_NAME (*d
), NULL_TREE
, TREE_CHAIN (*d
));
2753 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2756 TREE_VALUE (*d
) = newval
;
2759 /* Compares the parameter-type-lists of ONE and TWO and
2760 returns false if they are different. If the DECLs are template
2761 functions, the return types and the template parameter lists are
2762 compared too (DR 565). */
2765 matching_fn_p (tree one
, tree two
)
2767 if (TREE_CODE (one
) != TREE_CODE (two
))
2770 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2771 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2774 if (TREE_CODE (one
) == TEMPLATE_DECL
)
2776 /* Compare template parms. */
2777 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2778 DECL_TEMPLATE_PARMS (two
)))
2781 /* And return type. */
2782 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2783 TREE_TYPE (TREE_TYPE (two
))))
2787 if (!equivalently_constrained (one
, two
))
2793 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2794 binding value (possibly with anticipated builtins stripped).
2795 Diagnose conflicts and return updated decl. */
2798 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2799 tree old
, tree decl
, bool hiding
= false)
2801 tree old_type
= NULL_TREE
;
2802 bool hide_type
= false;
2803 bool hide_value
= false;
2807 old_type
= binding
->type
;
2808 hide_type
= HIDDEN_TYPE_BINDING_P (binding
);
2810 hide_value
= hide_type
, hide_type
= false;
2812 else if (STAT_HACK_P (*slot
))
2814 old_type
= STAT_TYPE (*slot
);
2815 hide_type
= STAT_TYPE_HIDDEN_P (*slot
);
2816 hide_value
= STAT_DECL_HIDDEN_P (*slot
);
2820 tree to_type
= old_type
;
2821 bool local_overload
= false;
2823 gcc_assert (!level
|| level
->kind
== sk_namespace
? !binding
2824 : level
->kind
!= sk_class
&& !slot
);
2826 if (old
== error_mark_node
)
2829 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2831 /* Pushing an artificial decl. We should not find another
2832 artificial decl here already -- lookup_elaborated_type will
2833 have already found it. */
2834 gcc_checking_assert (!to_type
2835 && !(old
&& DECL_IMPLICIT_TYPEDEF_P (old
)));
2839 /* Put DECL into the type slot. */
2840 gcc_checking_assert (!to_type
);
2846 hide_value
= hiding
;
2851 if (old
&& DECL_IMPLICIT_TYPEDEF_P (old
))
2853 /* OLD is an implicit typedef. Move it to to_type. */
2854 gcc_checking_assert (!to_type
);
2857 hide_type
= hide_value
;
2862 if (DECL_DECLARES_FUNCTION_P (decl
))
2866 else if (OVL_P (old
))
2868 for (ovl_iterator
iter (old
); iter
; ++iter
)
2872 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2874 gcc_checking_assert (!iter
.hidden_p ());
2875 /* If a function declaration in namespace scope or
2876 block scope has the same name and the same
2877 parameter-type- list (8.3.5) as a function
2878 introduced by a using-declaration, and the
2879 declarations do not declare the same function,
2880 the program is ill-formed. [namespace.udecl]/14 */
2881 if (tree match
= duplicate_decls (decl
, fn
, hiding
))
2884 /* FIXME: To preserve existing error behavior, we
2885 still push the decl. This might change. */
2886 diagnose_name_conflict (decl
, fn
);
2893 if (to_type
!= old_type
2895 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2896 && !(DECL_IN_SYSTEM_HEADER (decl
)
2897 && DECL_IN_SYSTEM_HEADER (to_type
)))
2898 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2901 local_overload
= old
&& level
&& level
->kind
!= sk_namespace
;
2902 to_val
= ovl_insert (decl
, old
, -int (hiding
));
2906 if (TREE_CODE (old
) != TREE_CODE (decl
))
2907 /* Different kinds of decls conflict. */
2909 else if (TREE_CODE (old
) == TYPE_DECL
)
2911 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2912 /* Two type decls to the same type. Do nothing. */
2917 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2919 /* Two maybe-aliased namespaces. If they're to the same target
2920 namespace, that's ok. */
2921 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2924 /* The new one must be an alias at this point. */
2925 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2928 else if (TREE_CODE (old
) == VAR_DECL
)
2930 /* There can be two block-scope declarations of the same
2931 variable, so long as they are `extern' declarations. */
2932 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2934 else if (tree match
= duplicate_decls (decl
, old
))
2936 gcc_checking_assert (!hide_value
&& !hiding
);
2945 diagnose_name_conflict (decl
, old
);
2957 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2958 update_local_overload (binding
, to_val
);
2961 && !(TREE_CODE (decl
) == NAMESPACE_DECL
2962 && !DECL_NAMESPACE_ALIAS (decl
)))
2963 /* Don't add namespaces here. They're done in
2965 add_decl_to_level (level
, decl
);
2969 if (STAT_HACK_P (*slot
))
2971 STAT_TYPE (*slot
) = to_type
;
2972 STAT_DECL (*slot
) = to_val
;
2973 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2974 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
2976 else if (to_type
|| hide_value
)
2978 *slot
= stat_hack (to_val
, to_type
);
2979 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2980 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
2984 gcc_checking_assert (!hide_type
);
2990 binding
->type
= to_type
;
2991 binding
->value
= to_val
;
2992 HIDDEN_TYPE_BINDING_P (binding
) = hide_type
|| hide_value
;
2999 /* Table of identifiers to extern C declarations (or LISTS thereof). */
3001 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
3003 /* DECL has C linkage. If we have an existing instance, make sure the
3004 new one is compatible. Make sure it has the same exception
3005 specification [7.5, 7.6]. Add DECL to the map. */
3008 check_extern_c_conflict (tree decl
)
3010 /* Ignore artificial or system header decls. */
3011 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
3014 /* This only applies to decls at namespace scope. */
3015 if (!DECL_NAMESPACE_SCOPE_P (decl
))
3018 if (!extern_c_decls
)
3019 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
3021 tree
*slot
= extern_c_decls
3022 ->find_slot_with_hash (DECL_NAME (decl
),
3023 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
3024 if (tree old
= *slot
)
3026 if (TREE_CODE (old
) == OVERLOAD
)
3027 old
= OVL_FUNCTION (old
);
3030 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
3031 ; /* If they're in the same context, we'll have already complained
3032 about a (possible) mismatch, when inserting the decl. */
3033 else if (!decls_match (decl
, old
))
3035 else if (TREE_CODE (decl
) == FUNCTION_DECL
3036 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
3037 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
3040 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
3041 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
3045 auto_diagnostic_group d
;
3046 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3047 "conflicting C language linkage declaration %q#D", decl
);
3048 inform (DECL_SOURCE_LOCATION (old
),
3049 "previous declaration %q#D", old
);
3051 inform (DECL_SOURCE_LOCATION (decl
),
3052 "due to different exception specifications");
3057 /* The hash table expects OVERLOADS, so construct one with
3058 OLD as both the function and the chain. This allocate
3059 an excess OVERLOAD node, but it's rare to have multiple
3060 extern "C" decls of the same name. And we save
3061 complicating the hash table logic (which is used
3063 *slot
= ovl_make (old
, old
);
3065 slot
= &OVL_CHAIN (*slot
);
3067 /* Chain it on for c_linkage_binding's use. */
3068 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
3075 /* Returns a list of C-linkage decls with the name NAME. Used in
3076 c-family/c-pragma.c to implement redefine_extname pragma. */
3079 c_linkage_bindings (tree name
)
3082 if (tree
*slot
= extern_c_decls
3083 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
3085 tree result
= *slot
;
3086 if (TREE_CODE (result
) == OVERLOAD
)
3087 result
= OVL_CHAIN (result
);
3094 /* Subroutine of check_local_shadow. */
3097 inform_shadowed (tree shadowed
)
3099 inform (DECL_SOURCE_LOCATION (shadowed
),
3100 "shadowed declaration is here");
3103 /* DECL is being declared at a local scope. Emit suitable shadow
3107 check_local_shadow (tree decl
)
3109 /* Don't complain about the parms we push and then pop
3110 while tentatively parsing a function declarator. */
3111 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
3114 /* External decls are something else. */
3115 if (DECL_EXTERNAL (decl
))
3118 tree old
= NULL_TREE
;
3119 cp_binding_level
*old_scope
= NULL
;
3120 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
3122 old
= binding
->value
;
3123 old_scope
= binding
->scope
;
3127 && (TREE_CODE (old
) == PARM_DECL
3129 || (TREE_CODE (old
) == TYPE_DECL
3130 && (!DECL_ARTIFICIAL (old
)
3131 || TREE_CODE (decl
) == TYPE_DECL
)))
3132 && DECL_FUNCTION_SCOPE_P (old
)
3133 && (!DECL_ARTIFICIAL (decl
)
3134 || is_capture_proxy (decl
)
3135 || DECL_IMPLICIT_TYPEDEF_P (decl
)
3136 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
3138 /* DECL shadows a local thing possibly of interest. */
3140 /* DR 2211: check that captures and parameters
3141 do not have the same name. */
3142 if (is_capture_proxy (decl
))
3144 if (current_lambda_expr ()
3145 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
3146 && TREE_CODE (old
) == PARM_DECL
3147 && DECL_NAME (decl
) != this_identifier
)
3149 error_at (DECL_SOURCE_LOCATION (old
),
3150 "lambda parameter %qD "
3151 "previously declared as a capture", old
);
3155 /* Don't complain if it's from an enclosing function. */
3156 else if (DECL_CONTEXT (old
) == current_function_decl
3157 && TREE_CODE (decl
) != PARM_DECL
3158 && TREE_CODE (old
) == PARM_DECL
)
3160 /* Go to where the parms should be and see if we find
3162 cp_binding_level
*b
= current_binding_level
->level_chain
;
3164 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
3165 /* Skip the ctor/dtor cleanup level. */
3168 /* [basic.scope.param] A parameter name shall not be redeclared
3169 in the outermost block of the function definition. */
3170 if (b
->kind
== sk_function_parms
)
3172 error_at (DECL_SOURCE_LOCATION (decl
),
3173 "declaration of %q#D shadows a parameter", decl
);
3174 inform (DECL_SOURCE_LOCATION (old
),
3175 "%q#D previously declared here", old
);
3180 /* The local structure or class can't use parameters of
3181 the containing function anyway. */
3182 if (DECL_CONTEXT (old
) != current_function_decl
)
3184 for (cp_binding_level
*scope
= current_binding_level
;
3185 scope
!= old_scope
; scope
= scope
->level_chain
)
3186 if (scope
->kind
== sk_class
3187 && !LAMBDA_TYPE_P (scope
->this_entity
))
3190 /* Error if redeclaring a local declared in a
3191 init-statement or in the condition of an if or
3192 switch statement when the new declaration is in the
3193 outermost block of the controlled statement.
3194 Redeclaring a variable from a for or while condition is
3195 detected elsewhere. */
3196 else if (VAR_P (old
)
3197 && old_scope
== current_binding_level
->level_chain
3198 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
3200 auto_diagnostic_group d
;
3201 error_at (DECL_SOURCE_LOCATION (decl
),
3202 "redeclaration of %q#D", decl
);
3203 inform (DECL_SOURCE_LOCATION (old
),
3204 "%q#D previously declared here", old
);
3208 3.3.3/3: The name declared in an exception-declaration (...)
3209 shall not be redeclared in the outermost block of the handler.
3210 3.3.3/2: A parameter name shall not be redeclared (...) in
3211 the outermost block of any handler associated with a
3213 3.4.1/15: The function parameter names shall not be redeclared
3214 in the exception-declaration nor in the outermost block of a
3215 handler for the function-try-block. */
3216 else if ((TREE_CODE (old
) == VAR_DECL
3217 && old_scope
== current_binding_level
->level_chain
3218 && old_scope
->kind
== sk_catch
)
3219 || (TREE_CODE (old
) == PARM_DECL
3220 && (current_binding_level
->kind
== sk_catch
3221 || current_binding_level
->level_chain
->kind
== sk_catch
)
3222 && in_function_try_handler
))
3224 auto_diagnostic_group d
;
3225 if (permerror (DECL_SOURCE_LOCATION (decl
),
3226 "redeclaration of %q#D", decl
))
3227 inform (DECL_SOURCE_LOCATION (old
),
3228 "%q#D previously declared here", old
);
3232 /* If '-Wshadow=compatible-local' is specified without other
3233 -Wshadow= flags, we will warn only when the type of the
3234 shadowing variable (DECL) can be converted to that of the
3235 shadowed parameter (OLD_LOCAL). The reason why we only check
3236 if DECL's type can be converted to OLD_LOCAL's type (but not the
3237 other way around) is because when users accidentally shadow a
3238 parameter, more than often they would use the variable
3239 thinking (mistakenly) it's still the parameter. It would be
3240 rare that users would use the variable in the place that
3241 expects the parameter but thinking it's a new decl.
3242 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3243 warns regardless of whether one of the types involved
3244 is a subclass of the other, since that is never okay. */
3246 enum opt_code warning_code
;
3248 warning_code
= OPT_Wshadow
;
3249 else if ((TREE_TYPE (old
)
3251 && same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
3252 || TREE_CODE (decl
) == TYPE_DECL
3253 || TREE_CODE (old
) == TYPE_DECL
3254 || (!dependent_type_p (TREE_TYPE (decl
))
3255 && !dependent_type_p (TREE_TYPE (old
))
3256 /* If the new decl uses auto, we don't yet know
3257 its type (the old type cannot be using auto
3258 at this point, without also being
3259 dependent). This is an indication we're
3260 (now) doing the shadow checking too
3262 && !type_uses_auto (TREE_TYPE (decl
))
3263 && can_convert_arg (TREE_TYPE (old
), TREE_TYPE (decl
),
3264 decl
, LOOKUP_IMPLICIT
, tf_none
)))
3265 warning_code
= OPT_Wshadow_compatible_local
;
3267 warning_code
= OPT_Wshadow_local
;
3270 if (TREE_CODE (old
) == PARM_DECL
)
3271 msg
= "declaration of %q#D shadows a parameter";
3272 else if (is_capture_proxy (old
))
3273 msg
= "declaration of %qD shadows a lambda capture";
3275 msg
= "declaration of %qD shadows a previous local";
3277 auto_diagnostic_group d
;
3278 if (warning_at (DECL_SOURCE_LOCATION (decl
), warning_code
, msg
, decl
))
3279 inform_shadowed (old
);
3286 /* Don't warn for artificial things that are not implicit typedefs. */
3287 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
3290 if (nonlambda_method_basetype ())
3291 if (tree member
= lookup_member (current_nonlambda_class_type (),
3292 DECL_NAME (decl
), /*protect=*/0,
3293 /*want_type=*/false, tf_warning_or_error
))
3295 member
= MAYBE_BASELINK_FUNCTIONS (member
);
3297 /* Warn if a variable shadows a non-function, or the variable
3298 is a function or a pointer-to-function. */
3300 || TREE_CODE (decl
) == FUNCTION_DECL
3301 || (TREE_TYPE (decl
)
3302 && (TYPE_PTRFN_P (TREE_TYPE (decl
))
3303 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))))
3305 auto_diagnostic_group d
;
3306 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
3307 "declaration of %qD shadows a member of %qT",
3308 decl
, current_nonlambda_class_type ())
3310 inform_shadowed (member
);
3315 /* Now look for a namespace shadow. */
3316 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3319 || (TREE_CODE (old
) == TYPE_DECL
3320 && (!DECL_ARTIFICIAL (old
)
3321 || TREE_CODE (decl
) == TYPE_DECL
)))
3322 && !instantiating_current_function_p ())
3323 /* XXX shadow warnings in outer-more namespaces */
3325 auto_diagnostic_group d
;
3326 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
3327 "declaration of %qD shadows a global declaration",
3329 inform_shadowed (old
);
3336 /* DECL is being pushed inside function CTX. Set its context, if
3340 set_decl_context_in_fn (tree ctx
, tree decl
)
3342 if (TREE_CODE (decl
) == FUNCTION_DECL
3343 || (VAR_P (decl
) && DECL_EXTERNAL (decl
)))
3344 /* Make sure local externs are marked as such. OMP UDRs really
3345 are nested functions. */
3346 gcc_checking_assert (DECL_LOCAL_DECL_P (decl
)
3347 && (DECL_NAMESPACE_SCOPE_P (decl
)
3348 || (TREE_CODE (decl
) == FUNCTION_DECL
3349 && DECL_OMP_DECLARE_REDUCTION_P (decl
))));
3351 if (!DECL_CONTEXT (decl
)
3352 /* When parsing the parameter list of a function declarator,
3353 don't set DECL_CONTEXT to an enclosing function. */
3354 && !(TREE_CODE (decl
) == PARM_DECL
3355 && parsing_function_declarator ()))
3356 DECL_CONTEXT (decl
) = ctx
;
3359 /* DECL is a local extern decl. Find or create the namespace-scope
3360 decl that it aliases. Also, determines the linkage of DECL. */
3363 push_local_extern_decl_alias (tree decl
)
3365 if (dependent_type_p (TREE_TYPE (decl
))
3366 || (processing_template_decl
3368 && CP_DECL_THREAD_LOCAL_P (decl
)))
3370 /* EH specs were not part of the function type prior to c++17, but
3371 we still can't go pushing dependent eh specs into the namespace. */
3372 if (cxx_dialect
< cxx17
3373 && TREE_CODE (decl
) == FUNCTION_DECL
3374 && (value_dependent_expression_p
3375 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)))))
3378 gcc_checking_assert (!DECL_LANG_SPECIFIC (decl
)
3379 || !DECL_TEMPLATE_INFO (decl
));
3380 if (DECL_LANG_SPECIFIC (decl
) && DECL_LOCAL_DECL_ALIAS (decl
))
3381 /* We're instantiating a non-dependent local decl, it already
3385 tree alias
= NULL_TREE
;
3387 if (DECL_SIZE (decl
) && !TREE_CONSTANT (DECL_SIZE (decl
)))
3388 /* Do not let a VLA creep into a namespace. Diagnostic will be
3389 emitted in layout_var_decl later. */
3390 alias
= error_mark_node
;
3393 /* First look for a decl that matches. */
3394 tree ns
= CP_DECL_CONTEXT (decl
);
3395 tree binding
= find_namespace_value (ns
, DECL_NAME (decl
));
3397 if (binding
&& TREE_CODE (binding
) != TREE_LIST
)
3398 for (ovl_iterator
iter (binding
); iter
; ++iter
)
3399 if (decls_match (decl
, *iter
, /*record_versions*/false))
3407 /* No existing namespace-scope decl. Make one. */
3408 alias
= copy_decl (decl
);
3409 if (TREE_CODE (alias
) == FUNCTION_DECL
)
3411 /* Recontextualize the parms. */
3412 for (tree
*chain
= &DECL_ARGUMENTS (alias
);
3413 *chain
; chain
= &DECL_CHAIN (*chain
))
3415 *chain
= copy_decl (*chain
);
3416 DECL_CONTEXT (*chain
) = alias
;
3419 tree type
= TREE_TYPE (alias
);
3420 for (tree args
= TYPE_ARG_TYPES (type
);
3421 args
; args
= TREE_CHAIN (args
))
3422 if (TREE_PURPOSE (args
))
3424 /* There are default args. Lose them. */
3425 tree nargs
= NULL_TREE
;
3426 tree
*chain
= &nargs
;
3427 for (args
= TYPE_ARG_TYPES (type
);
3428 args
; args
= TREE_CHAIN (args
))
3429 if (args
== void_list_node
)
3437 = build_tree_list (NULL_TREE
, TREE_VALUE (args
));
3438 chain
= &TREE_CHAIN (*chain
);
3441 tree fn_type
= build_function_type (TREE_TYPE (type
), nargs
);
3443 fn_type
= apply_memfn_quals
3444 (fn_type
, type_memfn_quals (type
));
3446 fn_type
= build_cp_fntype_variant
3447 (fn_type
, type_memfn_rqual (type
),
3448 TYPE_RAISES_EXCEPTIONS (type
),
3449 TYPE_HAS_LATE_RETURN_TYPE (type
));
3451 TREE_TYPE (alias
) = fn_type
;
3456 /* This is the real thing. */
3457 DECL_LOCAL_DECL_P (alias
) = false;
3459 /* Expected default linkage is from the namespace. */
3460 TREE_PUBLIC (alias
) = TREE_PUBLIC (ns
);
3461 push_nested_namespace (ns
);
3462 alias
= pushdecl (alias
, /* hiding= */true);
3463 pop_nested_namespace (ns
);
3465 && CP_DECL_THREAD_LOCAL_P (decl
)
3466 && alias
!= error_mark_node
)
3467 set_decl_tls_model (alias
, DECL_TLS_MODEL (decl
));
3471 retrofit_lang_decl (decl
);
3472 DECL_LOCAL_DECL_ALIAS (decl
) = alias
;
3475 /* DECL is a global or module-purview entity. If it has non-internal
3476 linkage, and we have a module vector, record it in the appropriate
3477 slot. We have already checked for duplicates. */
3480 maybe_record_mergeable_decl (tree
*slot
, tree name
, tree decl
)
3482 if (TREE_CODE (*slot
) != BINDING_VECTOR
)
3485 if (!TREE_PUBLIC (CP_DECL_CONTEXT (decl
)))
3486 /* Member of internal namespace. */
3489 tree not_tmpl
= STRIP_TEMPLATE (decl
);
3490 if ((TREE_CODE (not_tmpl
) == FUNCTION_DECL
3491 || TREE_CODE (not_tmpl
) == VAR_DECL
)
3492 && DECL_THIS_STATIC (not_tmpl
))
3493 /* Internal linkage. */
3496 bool partition
= named_module_p ();
3497 tree
*gslot
= get_fixed_binding_slot
3498 (slot
, name
, partition
? BINDING_SLOT_PARTITION
: BINDING_SLOT_GLOBAL
, true);
3503 = BINDING_VECTOR_CLUSTER (*slot
, 0).slots
[BINDING_SLOT_CURRENT
];
3505 if (!STAT_HACK_P (tree (orig
)))
3506 orig
= stat_hack (tree (orig
));
3508 MODULE_BINDING_GLOBAL_P (tree (orig
)) = true;
3511 add_mergeable_namespace_entity (gslot
, decl
);
3514 /* DECL is being pushed. Check whether it hides or ambiguates
3515 something seen as an import. This include decls seen in our own
3516 interface, which is OK. Also, check for merging a
3517 global/partition decl. */
3520 check_module_override (tree decl
, tree mvec
, bool hiding
,
3521 tree scope
, tree name
)
3523 tree match
= NULL_TREE
;
3524 bitmap imports
= get_import_bitmap ();
3525 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (mvec
);
3526 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (mvec
);
3528 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
3534 for (; ix
--; cluster
++)
3535 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
3537 /* Are we importing this module? */
3538 if (cluster
->indices
[jx
].span
!= 1)
3540 if (!cluster
->indices
[jx
].base
)
3542 if (!bitmap_bit_p (imports
, cluster
->indices
[jx
].base
))
3545 if (cluster
->slots
[jx
].is_lazy ())
3547 gcc_assert (cluster
->indices
[jx
].span
== 1);
3548 lazy_load_binding (cluster
->indices
[jx
].base
,
3549 scope
, name
, &cluster
->slots
[jx
]);
3551 tree bind
= cluster
->slots
[jx
];
3553 /* Errors could cause there to be nothing. */
3556 if (STAT_HACK_P (bind
))
3557 /* We do not have to check STAT_TYPE here, the xref_tag
3558 machinery deals with that problem. */
3559 bind
= STAT_VISIBLE (bind
);
3561 for (ovl_iterator
iter (bind
); iter
; ++iter
)
3562 if (!iter
.using_p ())
3564 match
= duplicate_decls (decl
, *iter
, hiding
);
3570 if (TREE_PUBLIC (scope
) && TREE_PUBLIC (STRIP_TEMPLATE (decl
))
3571 /* Namespaces are dealt with specially in
3572 make_namespace_finish. */
3573 && !(TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
)))
3575 /* Look in the appropriate mergeable decl slot. */
3576 tree mergeable
= NULL_TREE
;
3577 if (named_module_p ())
3578 mergeable
= BINDING_VECTOR_CLUSTER (mvec
, BINDING_SLOT_PARTITION
3579 / BINDING_VECTOR_SLOTS_PER_CLUSTER
)
3580 .slots
[BINDING_SLOT_PARTITION
% BINDING_VECTOR_SLOTS_PER_CLUSTER
];
3582 mergeable
= BINDING_VECTOR_CLUSTER (mvec
, 0).slots
[BINDING_SLOT_GLOBAL
];
3584 for (ovl_iterator
iter (mergeable
); iter
; ++iter
)
3586 match
= duplicate_decls (decl
, *iter
, hiding
);
3595 if (match
!= error_mark_node
)
3597 if (named_module_p ())
3598 BINDING_VECTOR_PARTITION_DUPS_P (mvec
) = true;
3600 BINDING_VECTOR_GLOBAL_DUPS_P (mvec
) = true;
3608 /* Record DECL as belonging to the current lexical scope. Check for
3609 errors (such as an incompatible declaration for the same name
3610 already seen in the same scope).
3612 The new binding is hidden if HIDING is true (an anticipated builtin
3615 Returns either DECL or an old decl for the same name. If an old
3616 decl is returned, it may have been smashed to agree with what DECL
3620 pushdecl (tree decl
, bool hiding
)
3622 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
3624 if (decl
== error_mark_node
)
3625 return error_mark_node
;
3627 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
&& !hiding
)
3628 set_decl_context_in_fn (current_function_decl
, decl
);
3630 /* The binding level we will be pushing into. During local class
3631 pushing, we want to push to the containing scope. */
3632 cp_binding_level
*level
= current_binding_level
;
3633 while (level
->kind
== sk_class
3634 || level
->kind
== sk_cleanup
)
3635 level
= level
->level_chain
;
3637 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3638 insert it. Other NULL-named decls, not so much. */
3639 tree name
= DECL_NAME (decl
);
3640 if (name
? !IDENTIFIER_ANON_P (name
) : TREE_CODE (decl
) == NAMESPACE_DECL
)
3642 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3643 tree ns
= NULL_TREE
; /* Searched namespace. */
3644 tree
*slot
= NULL
; /* Binding slot in namespace. */
3645 tree
*mslot
= NULL
; /* Current module slot in namespace. */
3646 tree old
= NULL_TREE
;
3648 if (level
->kind
== sk_namespace
)
3650 /* We look in the decl's namespace for an existing
3651 declaration, even though we push into the current
3653 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3654 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3655 /* Create the binding, if this is current namespace, because
3656 that's where we'll be pushing anyway. */
3657 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3660 mslot
= get_fixed_binding_slot (slot
, name
, BINDING_SLOT_CURRENT
,
3661 ns
== current_namespace
);
3662 old
= MAYBE_STAT_DECL (*mslot
);
3667 binding
= find_local_binding (level
, name
);
3669 old
= binding
->value
;
3672 if (old
== error_mark_node
)
3675 for (ovl_iterator
iter (old
); iter
; ++iter
)
3676 if (iter
.using_p ())
3677 ; /* Ignore using decls here. */
3678 else if (iter
.hidden_p ()
3679 && TREE_CODE (*iter
) == FUNCTION_DECL
3680 && DECL_LANG_SPECIFIC (*iter
)
3681 && DECL_MODULE_IMPORT_P (*iter
))
3682 ; /* An undeclared builtin imported from elsewhere. */
3684 = duplicate_decls (decl
, *iter
, hiding
, iter
.hidden_p ()))
3686 if (match
== error_mark_node
)
3688 else if (TREE_CODE (match
) == TYPE_DECL
)
3689 gcc_checking_assert (REAL_IDENTIFIER_TYPE_VALUE (name
)
3690 == (level
->kind
== sk_namespace
3691 ? NULL_TREE
: TREE_TYPE (match
)));
3692 else if (iter
.hidden_p () && !hiding
)
3694 /* Unhiding a previously hidden decl. */
3695 tree head
= iter
.reveal_node (old
);
3698 gcc_checking_assert (ns
);
3699 if (STAT_HACK_P (*slot
))
3700 STAT_DECL (*slot
) = head
;
3704 if (DECL_EXTERN_C_P (match
))
3705 /* We need to check and register the decl now. */
3706 check_extern_c_conflict (match
);
3708 else if (slot
&& !hiding
3709 && STAT_HACK_P (*slot
) && STAT_DECL_HIDDEN_P (*slot
))
3711 /* Unhide the non-function. */
3712 gcc_checking_assert (old
== match
);
3713 if (!STAT_TYPE (*slot
))
3716 STAT_DECL (*slot
) = match
;
3721 /* Check for redeclaring an import. */
3722 if (slot
&& *slot
&& TREE_CODE (*slot
) == BINDING_VECTOR
)
3724 = check_module_override (decl
, *slot
, hiding
, ns
, name
))
3726 if (match
== error_mark_node
)
3729 /* We found a decl in an interface, push it into this
3731 decl
= update_binding (NULL
, binding
, mslot
, old
,
3737 /* We are pushing a new decl. */
3739 /* Skip a hidden builtin we failed to match already. There can
3741 if (old
&& anticipated_builtin_p (old
))
3742 old
= OVL_CHAIN (old
);
3744 check_template_shadow (decl
);
3746 if (DECL_DECLARES_FUNCTION_P (decl
))
3748 check_default_args (decl
);
3752 if (level
->kind
!= sk_namespace
)
3754 /* In a local class, a friend function declaration must
3755 find a matching decl in the innermost non-class scope.
3756 [class.friend/11] */
3757 error_at (DECL_SOURCE_LOCATION (decl
),
3758 "friend declaration %qD in local class without "
3759 "prior local declaration", decl
);
3760 /* Don't attempt to push it. */
3761 return error_mark_node
;
3766 if (level
->kind
!= sk_namespace
)
3768 check_local_shadow (decl
);
3770 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3771 /* A local namespace alias. */
3772 set_identifier_type_value_with_scope (name
, NULL_TREE
, level
);
3775 binding
= create_local_binding (level
, name
);
3779 ns
= current_namespace
;
3780 slot
= find_namespace_slot (ns
, name
, true);
3781 mslot
= get_fixed_binding_slot (slot
, name
, BINDING_SLOT_CURRENT
, true);
3782 /* Update OLD to reflect the namespace we're going to be
3784 old
= MAYBE_STAT_DECL (*mslot
);
3787 old
= update_binding (level
, binding
, mslot
, old
, decl
, hiding
);
3790 /* An existing decl matched, use it. */
3794 if (TREE_CODE (decl
) == TYPE_DECL
)
3796 tree type
= TREE_TYPE (decl
);
3798 if (type
!= error_mark_node
)
3800 if (TYPE_NAME (type
) != decl
)
3801 set_underlying_type (decl
);
3803 set_identifier_type_value_with_scope (name
, decl
, level
);
3805 if (level
->kind
!= sk_namespace
3806 && !instantiating_current_function_p ())
3807 /* This is a locally defined typedef in a function that
3808 is not a template instantation, record it to implement
3809 -Wunused-local-typedefs. */
3810 record_locally_defined_typedef (decl
);
3813 else if (VAR_OR_FUNCTION_DECL_P (decl
))
3815 if (DECL_EXTERN_C_P (decl
))
3816 check_extern_c_conflict (decl
);
3818 if (!DECL_LOCAL_DECL_P (decl
)
3820 maybe_register_incomplete_var (decl
);
3822 if (DECL_LOCAL_DECL_P (decl
)
3823 && NAMESPACE_SCOPE_P (decl
))
3824 push_local_extern_decl_alias (decl
);
3827 if (level
->kind
== sk_namespace
3828 && TREE_PUBLIC (level
->this_entity
)
3829 && !not_module_p ())
3830 maybe_record_mergeable_decl (slot
, name
, decl
);
3834 add_decl_to_level (level
, decl
);
3839 /* A mergeable entity is being loaded into namespace NS slot NAME.
3840 Create and return the appropriate vector slot for that. Either a
3841 GMF slot or a module-specific one. */
3844 mergeable_namespace_slots (tree ns
, tree name
, bool is_global
, tree
*vec
)
3846 tree
*mslot
= find_namespace_slot (ns
, name
, true);
3847 tree
*vslot
= get_fixed_binding_slot
3848 (mslot
, name
, is_global
? BINDING_SLOT_GLOBAL
: BINDING_SLOT_PARTITION
, true);
3850 gcc_checking_assert (TREE_CODE (*mslot
) == BINDING_VECTOR
);
3856 /* DECL is a new mergeable namespace-scope decl. Add it to the
3857 mergeable entities on GSLOT. */
3860 add_mergeable_namespace_entity (tree
*gslot
, tree decl
)
3862 *gslot
= ovl_make (decl
, *gslot
);
3865 /* A mergeable entity of KLASS called NAME is being loaded. Return
3866 the set of things it could be. All such non-as_base classes have
3867 been given a member vec. */
3870 lookup_class_binding (tree klass
, tree name
)
3872 tree found
= NULL_TREE
;
3874 if (!COMPLETE_TYPE_P (klass
))
3876 else if (TYPE_LANG_SPECIFIC (klass
))
3878 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
3880 found
= member_vec_binary_search (member_vec
, name
);
3883 else if (STAT_HACK_P (found
))
3884 /* Rearrange the stat hack so that we don't need to expose that
3886 found
= ovl_make (STAT_TYPE (found
), STAT_DECL (found
));
3887 else if (IDENTIFIER_CONV_OP_P (name
))
3889 gcc_checking_assert (name
== conv_op_identifier
);
3890 found
= OVL_CHAIN (found
);
3895 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass
)
3896 || TYPE_PTRMEMFUNC_P (klass
));
3897 found
= fields_linear_search (klass
, name
, false);
3903 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
3904 for all decls of the current module. When partitions are involved,
3905 decls might be mentioned more than once. Return the accumulation of
3906 CALLBACK results. */
3909 walk_module_binding (tree binding
, bitmap partitions
,
3910 bool (*callback
) (tree decl
, WMB_Flags
, void *data
),
3913 // FIXME: We don't quite deal with using decls naming stat hack
3914 // type. Also using decls exporting something from the same scope.
3915 tree current
= binding
;
3918 if (TREE_CODE (binding
) == BINDING_VECTOR
)
3919 current
= BINDING_VECTOR_CLUSTER (binding
, 0).slots
[BINDING_SLOT_CURRENT
];
3921 bool decl_hidden
= false;
3922 if (tree type
= MAYBE_STAT_TYPE (current
))
3924 WMB_Flags flags
= WMB_None
;
3925 if (STAT_TYPE_HIDDEN_P (current
))
3926 flags
= WMB_Flags (flags
| WMB_Hidden
);
3927 count
+= callback (type
, flags
, data
);
3928 decl_hidden
= STAT_DECL_HIDDEN_P (current
);
3931 for (ovl_iterator
iter (MAYBE_STAT_DECL (current
)); iter
; ++iter
)
3933 if (iter
.hidden_p ())
3935 if (!(decl_hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)))
3937 WMB_Flags flags
= WMB_None
;
3939 flags
= WMB_Flags (flags
| WMB_Hidden
);
3940 if (iter
.using_p ())
3942 flags
= WMB_Flags (flags
| WMB_Using
);
3943 if (iter
.exporting_p ())
3944 flags
= WMB_Flags (flags
| WMB_Export
);
3946 count
+= callback (*iter
, flags
, data
);
3948 decl_hidden
= false;
3951 if (partitions
&& TREE_CODE (binding
) == BINDING_VECTOR
)
3953 /* Process partition slots. */
3954 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
3955 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
3956 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
3962 bool maybe_dups
= BINDING_VECTOR_PARTITION_DUPS_P (binding
);
3964 for (; ix
--; cluster
++)
3965 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
3966 if (!cluster
->slots
[jx
].is_lazy ())
3967 if (tree bind
= cluster
->slots
[jx
])
3969 if (TREE_CODE (bind
) == NAMESPACE_DECL
3970 && !DECL_NAMESPACE_ALIAS (bind
))
3972 if (unsigned base
= cluster
->indices
[jx
].base
)
3973 if (unsigned span
= cluster
->indices
[jx
].span
)
3975 if (bitmap_bit_p (partitions
, base
))
3977 while (++base
, --span
);
3978 /* Not a partition's namespace. */
3982 WMB_Flags flags
= WMB_None
;
3984 flags
= WMB_Flags (flags
| WMB_Dups
);
3985 count
+= callback (bind
, flags
, data
);
3987 else if (STAT_HACK_P (bind
) && MODULE_BINDING_PARTITION_P (bind
))
3989 if (tree btype
= STAT_TYPE (bind
))
3991 WMB_Flags flags
= WMB_None
;
3993 flags
= WMB_Flags (flags
| WMB_Dups
);
3994 if (STAT_TYPE_HIDDEN_P (bind
))
3995 flags
= WMB_Flags (flags
| WMB_Hidden
);
3997 count
+= callback (btype
, flags
, data
);
3999 bool hidden
= STAT_DECL_HIDDEN_P (bind
);
4000 for (ovl_iterator
iter (MAYBE_STAT_DECL (STAT_DECL (bind
)));
4003 if (iter
.hidden_p ())
4006 (!(hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)));
4008 WMB_Flags flags
= WMB_None
;
4010 flags
= WMB_Flags (flags
| WMB_Dups
);
4012 flags
= WMB_Flags (flags
| WMB_Hidden
);
4013 if (iter
.using_p ())
4015 flags
= WMB_Flags (flags
| WMB_Using
);
4016 if (iter
.exporting_p ())
4017 flags
= WMB_Flags (flags
| WMB_Export
);
4019 count
+= callback (*iter
, flags
, data
);
4029 /* Imported module MOD has a binding to NS::NAME, stored in section
4033 import_module_binding (tree ns
, tree name
, unsigned mod
, unsigned snum
)
4035 tree
*slot
= find_namespace_slot (ns
, name
, true);
4036 binding_slot
*mslot
= append_imported_binding_slot (slot
, name
, mod
);
4038 if (mslot
->is_lazy () || *mslot
)
4039 /* Oops, something was already there. */
4042 mslot
->set_lazy (snum
);
4046 /* An import of MODULE is binding NS::NAME. There should be no
4047 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4048 is a header_unit (-1) or part of the current module (+1). VALUE
4049 and TYPE are the value and type bindings. VISIBLE are the value
4050 bindings being exported. */
4053 set_module_binding (tree ns
, tree name
, unsigned mod
, int mod_glob
,
4054 tree value
, tree type
, tree visible
)
4057 /* Bogus BMIs could give rise to nothing to bind. */
4060 gcc_assert (TREE_CODE (value
) != NAMESPACE_DECL
4061 || DECL_NAMESPACE_ALIAS (value
));
4062 gcc_checking_assert (mod
);
4064 tree
*slot
= find_namespace_slot (ns
, name
, true);
4065 binding_slot
*mslot
= search_imported_binding_slot (slot
, mod
);
4067 if (!mslot
|| !mslot
->is_lazy ())
4068 /* Again, bogus BMI could give find to missing or already loaded slot. */
4072 if (type
|| visible
!= bind
|| mod_glob
)
4074 bind
= stat_hack (bind
, type
);
4075 STAT_VISIBLE (bind
) = visible
;
4076 if ((mod_glob
> 0 && TREE_PUBLIC (ns
))
4077 || (type
&& DECL_MODULE_EXPORT_P (type
)))
4078 STAT_TYPE_VISIBLE_P (bind
) = true;
4081 /* Note if this is this-module or global binding. */
4083 MODULE_BINDING_PARTITION_P (bind
) = true;
4084 else if (mod_glob
< 0)
4085 MODULE_BINDING_GLOBAL_P (bind
) = true;
4093 add_module_namespace_decl (tree ns
, tree decl
)
4095 gcc_assert (!DECL_CHAIN (decl
));
4096 gcc_checking_assert (!(VAR_OR_FUNCTION_DECL_P (decl
)
4097 && DECL_LOCAL_DECL_P (decl
)));
4099 /* Expensive already-there? check. */
4100 for (auto probe
= NAMESPACE_LEVEL (ns
)->names
; probe
;
4101 probe
= DECL_CHAIN (probe
))
4102 gcc_assert (decl
!= probe
);
4104 add_decl_to_level (NAMESPACE_LEVEL (ns
), decl
);
4107 maybe_register_incomplete_var (decl
);
4109 if (VAR_OR_FUNCTION_DECL_P (decl
)
4110 && DECL_EXTERN_C_P (decl
))
4111 check_extern_c_conflict (decl
);
4114 /* Enter DECL into the symbol table, if that's appropriate. Returns
4115 DECL, or a modified version thereof. */
4118 maybe_push_decl (tree decl
)
4120 tree type
= TREE_TYPE (decl
);
4122 /* Add this decl to the current binding level, but not if it comes
4123 from another scope, e.g. a static member variable. TEM may equal
4124 DECL or it may be a previous decl of the same name. */
4125 if (decl
== error_mark_node
4126 || (TREE_CODE (decl
) != PARM_DECL
4127 && DECL_CONTEXT (decl
) != NULL_TREE
4128 /* Definitions of namespace members outside their namespace are
4130 && !DECL_NAMESPACE_SCOPE_P (decl
))
4131 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
4132 || type
== unknown_type_node
4133 /* The declaration of a template specialization does not affect
4134 the functions available for overload resolution, so we do not
4136 || (TREE_CODE (decl
) == FUNCTION_DECL
4137 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
4140 return pushdecl (decl
);
4143 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4144 binding level. If IS_USING is true, DECL got here through a
4145 using-declaration. */
4148 push_local_binding (tree id
, tree decl
, bool is_using
)
4150 /* Skip over any local classes. This makes sense if we call
4151 push_local_binding with a friend decl of a local class. */
4152 cp_binding_level
*b
= innermost_nonclass_level ();
4154 gcc_assert (b
->kind
!= sk_namespace
);
4155 if (find_local_binding (b
, id
))
4157 /* Supplement the existing binding. */
4158 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
4159 /* It didn't work. Something else must be bound at this
4160 level. Do not add DECL to the list of things to pop
4165 /* Create a new binding. */
4166 push_binding (id
, decl
, b
);
4168 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
4169 /* We must put the OVERLOAD or using into a TREE_LIST since we
4170 cannot use the decl's chain itself. */
4171 decl
= build_tree_list (id
, decl
);
4173 /* And put DECL on the list of things declared by the current
4175 add_decl_to_level (b
, decl
);
4179 /* true means unconditionally make a BLOCK for the next level pushed. */
4181 static bool keep_next_level_flag
;
4183 static int binding_depth
= 0;
4190 for (i
= 0; i
< depth
* 2; i
++)
4194 /* Return a string describing the kind of SCOPE we have. */
4196 cp_binding_level_descriptor (cp_binding_level
*scope
)
4198 /* The order of this table must match the "scope_kind"
4200 static const char* scope_kind_names
[] = {
4206 "function-parameter-scope",
4209 "template-parameter-scope",
4210 "template-explicit-spec-scope"
4212 const scope_kind kind
= scope
->explicit_spec_p
4213 ? sk_template_spec
: scope
->kind
;
4215 return scope_kind_names
[kind
];
4218 /* Output a debugging information about SCOPE when performing
4221 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
4223 const char *desc
= cp_binding_level_descriptor (scope
);
4224 if (scope
->this_entity
)
4225 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
4226 scope
->this_entity
, (void *) scope
, line
);
4228 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
4231 /* A chain of binding_level structures awaiting reuse. */
4233 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
4235 /* Insert SCOPE as the innermost binding level. */
4238 push_binding_level (cp_binding_level
*scope
)
4240 /* Add it to the front of currently active scopes stack. */
4241 scope
->level_chain
= current_binding_level
;
4242 current_binding_level
= scope
;
4243 keep_next_level_flag
= false;
4245 if (ENABLE_SCOPE_CHECKING
)
4247 scope
->binding_depth
= binding_depth
;
4248 indent (binding_depth
);
4249 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4255 /* Create a new KIND scope and make it the top of the active scopes stack.
4256 ENTITY is the scope of the associated C++ entity (namespace, class,
4257 function, C++0x enumeration); it is NULL otherwise. */
4260 begin_scope (scope_kind kind
, tree entity
)
4262 cp_binding_level
*scope
;
4264 /* Reuse or create a struct for this binding level. */
4265 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
4267 scope
= free_binding_level
;
4268 free_binding_level
= scope
->level_chain
;
4269 memset (scope
, 0, sizeof (cp_binding_level
));
4272 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
4274 scope
->this_entity
= entity
;
4275 scope
->more_cleanups_ok
= true;
4282 case sk_template_spec
:
4283 scope
->explicit_spec_p
= true;
4284 kind
= sk_template_parms
;
4286 case sk_template_parms
:
4293 case sk_scoped_enum
:
4294 case sk_transaction
:
4296 scope
->keep
= keep_next_level_flag
;
4299 case sk_function_parms
:
4300 scope
->keep
= keep_next_level_flag
;
4302 scope
->immediate_fn_ctx_p
= DECL_IMMEDIATE_FUNCTION_P (entity
);
4306 NAMESPACE_LEVEL (entity
) = scope
;
4310 /* Should not happen. */
4316 push_binding_level (scope
);
4321 /* We're about to leave current scope. Pop the top of the stack of
4322 currently active scopes. Return the enclosing scope, now active. */
4327 cp_binding_level
*scope
= current_binding_level
;
4329 if (scope
->kind
== sk_namespace
&& class_binding_level
)
4330 current_binding_level
= class_binding_level
;
4332 /* We cannot leave a scope, if there are none left. */
4333 if (NAMESPACE_LEVEL (global_namespace
))
4334 gcc_assert (!global_scope_p (scope
));
4336 if (ENABLE_SCOPE_CHECKING
)
4338 indent (--binding_depth
);
4339 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4343 /* Move one nesting level up. */
4344 current_binding_level
= scope
->level_chain
;
4346 /* Namespace-scopes are left most probably temporarily, not
4347 completely; they can be reopened later, e.g. in namespace-extension
4348 or any name binding activity that requires us to resume a
4349 namespace. For classes, we cache some binding levels. For other
4350 scopes, we just make the structure available for reuse. */
4351 if (scope
->kind
!= sk_namespace
4352 && scope
!= previous_class_level
)
4354 scope
->level_chain
= free_binding_level
;
4355 gcc_assert (!ENABLE_SCOPE_CHECKING
4356 || scope
->binding_depth
== binding_depth
);
4357 free_binding_level
= scope
;
4360 if (scope
->kind
== sk_class
)
4362 /* Reset DEFINING_CLASS_P to allow for reuse of a
4363 class-defining scope in a non-defining context. */
4364 scope
->defining_class_p
= 0;
4366 /* Find the innermost enclosing class scope, and reset
4367 CLASS_BINDING_LEVEL appropriately. */
4368 class_binding_level
= NULL
;
4369 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
4370 if (scope
->kind
== sk_class
)
4372 class_binding_level
= scope
;
4377 return current_binding_level
;
4380 /* When we exit a toplevel class scope, we save its binding level so
4381 that we can restore it quickly. Here, we've entered some other
4382 class, so we must invalidate our cache. */
4385 invalidate_class_lookup_cache (void)
4387 previous_class_level
->level_chain
= free_binding_level
;
4388 free_binding_level
= previous_class_level
;
4389 previous_class_level
= NULL
;
4393 resume_scope (cp_binding_level
* b
)
4395 /* Resuming binding levels is meant only for namespaces,
4396 and those cannot nest into classes. */
4397 gcc_assert (!class_binding_level
);
4398 /* Also, resuming a non-directly nested namespace is a no-no. */
4399 gcc_assert (b
->level_chain
== current_binding_level
);
4400 current_binding_level
= b
;
4401 if (ENABLE_SCOPE_CHECKING
)
4403 b
->binding_depth
= binding_depth
;
4404 indent (binding_depth
);
4405 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
4410 /* Return the innermost binding level that is not for a class scope. */
4412 static cp_binding_level
*
4413 innermost_nonclass_level (void)
4415 cp_binding_level
*b
;
4417 b
= current_binding_level
;
4418 while (b
->kind
== sk_class
)
4424 /* We're defining an object of type TYPE. If it needs a cleanup, but
4425 we're not allowed to add any more objects with cleanups to the current
4426 scope, create a new binding level. */
4429 maybe_push_cleanup_level (tree type
)
4431 if (type
!= error_mark_node
4432 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4433 && current_binding_level
->more_cleanups_ok
== 0)
4435 begin_scope (sk_cleanup
, NULL
);
4436 current_binding_level
->statement_list
= push_stmt_list ();
4440 /* Return true if we are in the global binding level. */
4443 global_bindings_p (void)
4445 return global_scope_p (current_binding_level
);
4448 /* True if we are currently in a toplevel binding level. This
4449 means either the global binding level or a namespace in a toplevel
4450 binding level. Since there are no non-toplevel namespace levels,
4451 this really means any namespace or template parameter level. We
4452 also include a class whose context is toplevel. */
4455 toplevel_bindings_p (void)
4457 cp_binding_level
*b
= innermost_nonclass_level ();
4459 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
4462 /* True if this is a namespace scope, or if we are defining a class
4463 which is itself at namespace scope, or whose enclosing class is
4464 such a class, etc. */
4467 namespace_bindings_p (void)
4469 cp_binding_level
*b
= innermost_nonclass_level ();
4471 return b
->kind
== sk_namespace
;
4474 /* True if the innermost non-class scope is a block scope. */
4477 local_bindings_p (void)
4479 cp_binding_level
*b
= innermost_nonclass_level ();
4480 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
4483 /* True if the current level needs to have a BLOCK made. */
4488 return (current_binding_level
->blocks
!= NULL_TREE
4489 || current_binding_level
->keep
4490 || current_binding_level
->kind
== sk_cleanup
4491 || current_binding_level
->names
!= NULL_TREE
4492 || current_binding_level
->using_directives
);
4495 /* Returns the kind of the innermost scope. */
4498 innermost_scope_kind (void)
4500 return current_binding_level
->kind
;
4503 /* Returns true if this scope was created to store template parameters. */
4506 template_parm_scope_p (void)
4508 return innermost_scope_kind () == sk_template_parms
;
4511 /* If KEEP is true, make a BLOCK node for the next binding level,
4512 unconditionally. Otherwise, use the normal logic to decide whether
4513 or not to create a BLOCK. */
4516 keep_next_level (bool keep
)
4518 keep_next_level_flag
= keep
;
4521 /* Return the list of declarations of the current local scope. */
4524 get_local_decls (void)
4526 gcc_assert (current_binding_level
->kind
!= sk_namespace
4527 && current_binding_level
->kind
!= sk_class
);
4528 return current_binding_level
->names
;
4531 /* Return how many function prototypes we are currently nested inside. */
4534 function_parm_depth (void)
4537 cp_binding_level
*b
;
4539 for (b
= current_binding_level
;
4540 b
->kind
== sk_function_parms
;
4547 /* For debugging. */
4548 static int no_print_functions
= 0;
4549 static int no_print_builtins
= 0;
4552 print_binding_level (cp_binding_level
* lvl
)
4556 if (lvl
->this_entity
)
4557 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
4558 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
4559 if (lvl
->more_cleanups_ok
)
4560 fprintf (stderr
, " more-cleanups-ok");
4561 if (lvl
->have_cleanups
)
4562 fprintf (stderr
, " have-cleanups");
4563 fprintf (stderr
, "\n");
4566 fprintf (stderr
, " names:\t");
4567 /* We can probably fit 3 names to a line? */
4568 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
4570 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
4572 if (no_print_builtins
4573 && (TREE_CODE (t
) == TYPE_DECL
)
4574 && DECL_IS_UNDECLARED_BUILTIN (t
))
4577 /* Function decls tend to have longer names. */
4578 if (TREE_CODE (t
) == FUNCTION_DECL
)
4585 fprintf (stderr
, "\n\t");
4588 print_node_brief (stderr
, "", t
, 0);
4589 if (t
== error_mark_node
)
4593 fprintf (stderr
, "\n");
4595 if (vec_safe_length (lvl
->class_shadowed
))
4598 cp_class_binding
*b
;
4599 fprintf (stderr
, " class-shadowed:");
4600 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
4601 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
4602 fprintf (stderr
, "\n");
4604 if (lvl
->type_shadowed
)
4606 fprintf (stderr
, " type-shadowed:");
4607 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4609 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
4611 fprintf (stderr
, "\n");
4616 debug (cp_binding_level
&ref
)
4618 print_binding_level (&ref
);
4622 debug (cp_binding_level
*ptr
)
4627 fprintf (stderr
, "<nil>\n");
4631 print_other_binding_stack (cp_binding_level
*stack
)
4633 cp_binding_level
*level
;
4634 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
4636 fprintf (stderr
, "binding level %p\n", (void *) level
);
4637 print_binding_level (level
);
4642 print_binding_stack (void)
4644 cp_binding_level
*b
;
4645 fprintf (stderr
, "current_binding_level=%p\n"
4646 "class_binding_level=%p\n"
4647 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4648 (void *) current_binding_level
, (void *) class_binding_level
,
4649 (void *) NAMESPACE_LEVEL (global_namespace
));
4650 if (class_binding_level
)
4652 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
4653 if (b
== current_binding_level
)
4656 b
= class_binding_level
;
4658 b
= current_binding_level
;
4661 b
= current_binding_level
;
4662 print_other_binding_stack (b
);
4663 fprintf (stderr
, "global:\n");
4664 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
4667 /* Push a definition of struct, union or enum tag named ID. into
4668 binding_level B. DECL is a TYPE_DECL for the type. DECL has
4669 already been pushed into its binding level. This is bookkeeping to
4673 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
4675 if (b
->kind
== sk_namespace
)
4676 /* At namespace scope we should not see an identifier type value. */
4677 gcc_checking_assert (!REAL_IDENTIFIER_TYPE_VALUE (id
)
4678 /* We could be pushing a friend underneath a template
4679 parm (ill-formed). */
4681 (TYPE_NAME (REAL_IDENTIFIER_TYPE_VALUE (id
)))));
4684 /* Push the current type value, so we can restore it later */
4685 tree old
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4686 b
->type_shadowed
= tree_cons (id
, old
, b
->type_shadowed
);
4687 tree type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
4688 TREE_TYPE (b
->type_shadowed
) = type
;
4689 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
4693 /* As set_identifier_type_value_with_scope, but using
4694 current_binding_level. */
4697 set_identifier_type_value (tree id
, tree decl
)
4699 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
4702 /* Return the name for the constructor (or destructor) for the
4706 constructor_name (tree type
)
4708 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
4710 return decl
? DECL_NAME (decl
) : NULL_TREE
;
4713 /* Returns TRUE if NAME is the name for the constructor for TYPE,
4714 which must be a class type. */
4717 constructor_name_p (tree name
, tree type
)
4719 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
4721 /* These don't have names. */
4722 if (TREE_CODE (type
) == DECLTYPE_TYPE
4723 || TREE_CODE (type
) == TYPEOF_TYPE
)
4726 if (name
&& name
== constructor_name (type
))
4732 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4733 caller to set DECL_CONTEXT properly.
4735 Warning: For class and block-scope this must only be used when X
4736 will be the new innermost binding for its name, as we tack it onto
4737 the front of IDENTIFIER_BINDING without checking to see if the
4738 current IDENTIFIER_BINDING comes from a closer binding level than
4741 Warning: For namespace scope, this will look in LEVEL for an
4742 existing binding to match, but if not found will push the decl into
4743 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
4744 pop_nested_namespace if you really need to push it into a foreign
4748 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool hiding
= false)
4750 cp_binding_level
*b
;
4752 if (level
->kind
== sk_class
)
4754 gcc_checking_assert (!hiding
);
4755 b
= class_binding_level
;
4756 class_binding_level
= level
;
4757 pushdecl_class_level (x
);
4758 class_binding_level
= b
;
4762 tree function_decl
= current_function_decl
;
4763 if (level
->kind
== sk_namespace
)
4764 current_function_decl
= NULL_TREE
;
4765 b
= current_binding_level
;
4766 current_binding_level
= level
;
4767 x
= pushdecl (x
, hiding
);
4768 current_binding_level
= b
;
4769 current_function_decl
= function_decl
;
4774 /* Inject X into the local scope just before the function parms. */
4777 pushdecl_outermost_localscope (tree x
)
4779 cp_binding_level
*b
= NULL
;
4780 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
4782 /* Find the scope just inside the function parms. */
4783 for (cp_binding_level
*n
= current_binding_level
;
4784 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
4787 return b
? do_pushdecl_with_scope (x
, b
) : error_mark_node
;
4790 /* Process a local-scope or namespace-scope using declaration. LOOKUP
4791 is the result of qualified lookup (both value & type are
4792 significant). FN_SCOPE_P indicates if we're at function-scope (as
4793 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
4794 bindings, which are altered to reflect the newly brought in
4798 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
4799 bool insert_p
, tree
*value_p
, tree
*type_p
)
4801 tree value
= *value_p
;
4802 tree type
= *type_p
;
4803 bool failed
= false;
4805 /* Shift the old and new bindings around so we're comparing class and
4806 enumeration names to each other. */
4807 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4813 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4815 lookup
.type
= lookup
.value
;
4816 lookup
.value
= NULL_TREE
;
4819 /* Only process exporting if we're going to be inserting. */
4820 bool revealing_p
= insert_p
&& !fn_scope_p
&& module_has_cmi_p ();
4822 /* First do the value binding. */
4824 /* Nothing (only implicit typedef found). */
4825 gcc_checking_assert (lookup
.type
);
4826 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4828 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4830 tree new_fn
= *usings
;
4831 bool exporting
= revealing_p
&& module_exporting_p ();
4834 /* If the using decl is exported, the things it refers
4835 to must also be exported (or not in module purview). */
4836 if (!DECL_MODULE_EXPORT_P (new_fn
)
4837 && (DECL_LANG_SPECIFIC (new_fn
)
4838 && DECL_MODULE_PURVIEW_P (new_fn
)))
4840 error ("%q#D does not have external linkage", new_fn
);
4841 inform (DECL_SOURCE_LOCATION (new_fn
),
4842 "%q#D declared here", new_fn
);
4847 /* [namespace.udecl]
4849 If a function declaration in namespace scope or block
4850 scope has the same name and the same parameter types as a
4851 function introduced by a using declaration the program is
4853 /* This seems overreaching, asking core -- why do we care
4854 about decls in the namespace that we cannot name (because
4855 they are not transitively imported. We just check the
4856 decls that are in this TU. */
4858 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4862 if (new_fn
== old_fn
)
4864 /* The function already exists in the current
4865 namespace. We will still want to insert it if
4866 it is revealing a not-revealed thing. */
4870 else if (old
.using_p ())
4873 /* Update in place. 'tis ok. */
4874 OVL_EXPORT_P (old
.get_using ()) = true;
4877 else if (DECL_MODULE_EXPORT_P (new_fn
))
4881 value
= old
.remove_node (value
);
4886 else if (old
.using_p ())
4887 continue; /* This is a using decl. */
4888 else if (old
.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn
))
4889 continue; /* This is an anticipated builtin. */
4890 else if (!matching_fn_p (new_fn
, old_fn
))
4891 continue; /* Parameters do not match. */
4892 else if (decls_match (new_fn
, old_fn
))
4894 /* Extern "C" in different namespaces. */
4900 diagnose_name_conflict (new_fn
, old_fn
);
4907 if (!found
&& insert_p
)
4908 /* Unlike the decl-pushing case we don't drop anticipated
4909 builtins here. They don't cause a problem, and we'd
4910 like to match them with a future declaration. */
4911 value
= ovl_insert (new_fn
, value
, 1 + exporting
);
4915 /* Ignore anticipated builtins. */
4916 && !anticipated_builtin_p (value
)
4917 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
4919 diagnose_name_conflict (lookup
.value
, value
);
4923 // FIXME:what if we're newly exporting lookup.value
4924 value
= lookup
.value
;
4926 /* Now the type binding. */
4927 if (lookup
.type
&& lookup
.type
!= type
)
4929 // FIXME: What if we're exporting lookup.type?
4930 if (type
&& !decls_match (lookup
.type
, type
))
4932 diagnose_name_conflict (lookup
.type
, type
);
4941 /* If value is empty, shift any class or enumeration name back. */
4954 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
4955 Both are namespaces. */
4958 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
4960 int depth
= SCOPE_DEPTH (ancestor
);
4962 if (!depth
&& !inline_only
)
4963 /* The global namespace encloses everything. */
4966 while (SCOPE_DEPTH (descendant
) > depth
4967 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
4968 descendant
= CP_DECL_CONTEXT (descendant
);
4970 return ancestor
== descendant
;
4973 /* Returns true if ROOT (a non-alias namespace, class, or function)
4974 encloses CHILD. CHILD may be either a class type or a namespace
4978 is_ancestor (tree root
, tree child
)
4980 gcc_checking_assert ((TREE_CODE (root
) == NAMESPACE_DECL
4981 && !DECL_NAMESPACE_ALIAS (root
))
4982 || TREE_CODE (root
) == FUNCTION_DECL
4983 || CLASS_TYPE_P (root
));
4984 gcc_checking_assert (TREE_CODE (child
) == NAMESPACE_DECL
4985 || CLASS_TYPE_P (child
));
4987 /* The global namespace encloses everything. Early-out for the
4989 if (root
== global_namespace
)
4992 /* Search CHILD until we reach namespace scope. */
4993 while (TREE_CODE (child
) != NAMESPACE_DECL
)
4995 /* If we've reached the ROOT, it encloses CHILD. */
4999 /* Go out one level. */
5001 child
= TYPE_NAME (child
);
5002 child
= CP_DECL_CONTEXT (child
);
5005 if (TREE_CODE (root
) != NAMESPACE_DECL
)
5006 /* Failed to meet the non-namespace we were looking for. */
5009 if (tree alias
= DECL_NAMESPACE_ALIAS (child
))
5012 return is_nested_namespace (root
, child
);
5015 /* Enter the class or namespace scope indicated by T suitable for name
5016 lookup. T can be arbitrary scope, not necessary nested inside the
5017 current scope. Returns a non-null scope to pop iff pop_scope
5018 should be called later to exit this scope. */
5023 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5024 push_decl_namespace (t
);
5025 else if (CLASS_TYPE_P (t
))
5027 if (!at_class_scope_p ()
5028 || !same_type_p (current_class_type
, t
))
5029 push_nested_class (t
);
5031 /* T is the same as the current scope. There is therefore no
5032 need to re-enter the scope. Since we are not actually
5033 pushing a new scope, our caller should not call
5041 /* Leave scope pushed by push_scope. */
5048 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5049 pop_decl_namespace ();
5050 else if CLASS_TYPE_P (t
)
5051 pop_nested_class ();
5054 /* Subroutine of push_inner_scope. */
5057 push_inner_scope_r (tree outer
, tree inner
)
5062 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5065 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5067 push_inner_scope_r (outer
, prev
);
5068 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5070 cp_binding_level
*save_template_parm
= 0;
5071 /* Temporary take out template parameter scopes. They are saved
5072 in reversed order in save_template_parm. */
5073 while (current_binding_level
->kind
== sk_template_parms
)
5075 cp_binding_level
*b
= current_binding_level
;
5076 current_binding_level
= b
->level_chain
;
5077 b
->level_chain
= save_template_parm
;
5078 save_template_parm
= b
;
5081 resume_scope (NAMESPACE_LEVEL (inner
));
5082 current_namespace
= inner
;
5084 /* Restore template parameter scopes. */
5085 while (save_template_parm
)
5087 cp_binding_level
*b
= save_template_parm
;
5088 save_template_parm
= b
->level_chain
;
5089 b
->level_chain
= current_binding_level
;
5090 current_binding_level
= b
;
5097 /* Enter the scope INNER from current scope. INNER must be a scope
5098 nested inside current scope. This works with both name lookup and
5099 pushing name into scope. In case a template parameter scope is present,
5100 namespace is pushed under the template parameter scope according to
5101 name lookup rule in 14.6.1/6.
5103 Return the former current scope suitable for pop_inner_scope. */
5106 push_inner_scope (tree inner
)
5108 tree outer
= current_scope ();
5110 outer
= current_namespace
;
5112 push_inner_scope_r (outer
, inner
);
5116 /* Exit the current scope INNER back to scope OUTER. */
5119 pop_inner_scope (tree outer
, tree inner
)
5122 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5125 while (outer
!= inner
)
5127 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5129 cp_binding_level
*save_template_parm
= 0;
5130 /* Temporary take out template parameter scopes. They are saved
5131 in reversed order in save_template_parm. */
5132 while (current_binding_level
->kind
== sk_template_parms
)
5134 cp_binding_level
*b
= current_binding_level
;
5135 current_binding_level
= b
->level_chain
;
5136 b
->level_chain
= save_template_parm
;
5137 save_template_parm
= b
;
5142 /* Restore template parameter scopes. */
5143 while (save_template_parm
)
5145 cp_binding_level
*b
= save_template_parm
;
5146 save_template_parm
= b
->level_chain
;
5147 b
->level_chain
= current_binding_level
;
5148 current_binding_level
= b
;
5154 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5158 /* Do a pushlevel for class declarations. */
5161 pushlevel_class (void)
5163 class_binding_level
= begin_scope (sk_class
, current_class_type
);
5166 /* ...and a poplevel for class declarations. */
5169 poplevel_class (void)
5171 cp_binding_level
*level
= class_binding_level
;
5172 cp_class_binding
*cb
;
5176 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
5177 gcc_assert (level
!= 0);
5179 /* If we're leaving a toplevel class, cache its binding level. */
5180 if (current_class_depth
== 1)
5181 previous_class_level
= level
;
5182 for (shadowed
= level
->type_shadowed
;
5184 shadowed
= TREE_CHAIN (shadowed
))
5185 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
5187 /* Remove the bindings for all of the class-level declarations. */
5188 if (level
->class_shadowed
)
5190 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
5192 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
5193 cxx_binding_free (cb
->base
);
5195 ggc_free (level
->class_shadowed
);
5196 level
->class_shadowed
= NULL
;
5199 /* Now, pop out of the binding level which we created up in the
5200 `pushlevel_class' routine. */
5201 gcc_assert (current_binding_level
== level
);
5205 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5206 appropriate. DECL is the value to which a name has just been
5207 bound. CLASS_TYPE is the class in which the lookup occurred. */
5210 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
5213 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
5217 if (is_overloaded_fn (decl
))
5218 context
= ovl_scope (decl
);
5221 gcc_assert (DECL_P (decl
));
5222 context
= context_for_name_lookup (decl
);
5225 if (is_properly_derived_from (class_type
, context
))
5226 INHERITED_VALUE_BINDING_P (binding
) = 1;
5228 INHERITED_VALUE_BINDING_P (binding
) = 0;
5230 else if (binding
->value
== decl
)
5231 /* We only encounter a TREE_LIST when there is an ambiguity in the
5232 base classes. Such an ambiguity can be overridden by a
5233 definition in this class. */
5234 INHERITED_VALUE_BINDING_P (binding
) = 1;
5236 INHERITED_VALUE_BINDING_P (binding
) = 0;
5239 /* Make the declaration of X appear in CLASS scope. */
5242 pushdecl_class_level (tree x
)
5244 bool is_valid
= true;
5246 /* Do nothing if we're adding to an outer lambda closure type,
5247 outer_binding will add it later if it's needed. */
5248 if (current_class_type
!= class_binding_level
->this_entity
)
5251 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
5252 /* Get the name of X. */
5253 tree name
= OVL_NAME (x
);
5257 is_valid
= push_class_level_binding (name
, x
);
5258 if (TREE_CODE (x
) == TYPE_DECL
)
5259 set_identifier_type_value (name
, x
);
5261 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
5263 /* If X is an anonymous aggregate, all of its members are
5264 treated as if they were members of the class containing the
5265 aggregate, for naming purposes. */
5266 location_t save_location
= input_location
;
5267 tree anon
= TREE_TYPE (x
);
5268 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
5269 for (unsigned ix
= member_vec
->length (); ix
--;)
5271 tree binding
= (*member_vec
)[ix
];
5272 if (STAT_HACK_P (binding
))
5274 if (!pushdecl_class_level (STAT_TYPE (binding
)))
5276 binding
= STAT_DECL (binding
);
5278 if (!pushdecl_class_level (binding
))
5282 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
5283 if (TREE_CODE (f
) == FIELD_DECL
)
5285 input_location
= DECL_SOURCE_LOCATION (f
);
5286 if (!pushdecl_class_level (f
))
5289 input_location
= save_location
;
5294 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5295 scope. If the value returned is non-NULL, and the PREVIOUS field
5296 is not set, callers must set the PREVIOUS field explicitly. */
5298 static cxx_binding
*
5299 get_class_binding (tree name
, cp_binding_level
*scope
)
5304 cxx_binding
*binding
;
5306 class_type
= scope
->this_entity
;
5308 /* Get the type binding. */
5309 type_binding
= lookup_member (class_type
, name
,
5310 /*protect=*/2, /*want_type=*/true,
5311 tf_warning_or_error
);
5312 /* Get the value binding. */
5313 value_binding
= lookup_member (class_type
, name
,
5314 /*protect=*/2, /*want_type=*/false,
5315 tf_warning_or_error
);
5317 /* If we found either a type binding or a value binding, create a
5318 new binding object. */
5319 if (type_binding
|| value_binding
)
5321 binding
= new_class_binding (name
,
5325 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
5333 /* Make the declaration(s) of X appear in CLASS scope under the name
5334 NAME. Returns true if the binding is valid. */
5337 push_class_level_binding (tree name
, tree x
)
5339 cxx_binding
*binding
;
5343 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
5345 /* The class_binding_level will be NULL if x is a template
5346 parameter name in a member template. */
5347 if (!class_binding_level
)
5350 if (name
== error_mark_node
)
5353 /* Can happen for an erroneous declaration (c++/60384). */
5354 if (!identifier_p (name
))
5356 gcc_assert (errorcount
|| sorrycount
);
5360 /* Check for invalid member names. But don't worry about a default
5361 argument-scope lambda being pushed after the class is complete. */
5362 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
5363 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
5364 /* Check that we're pushing into the right binding level. */
5365 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
5367 /* We could have been passed a tree list if this is an ambiguous
5368 declaration. If so, pull the declaration out because
5369 check_template_shadow will not handle a TREE_LIST. */
5370 if (TREE_CODE (decl
) == TREE_LIST
5371 && TREE_TYPE (decl
) == error_mark_node
)
5372 decl
= TREE_VALUE (decl
);
5374 if (!check_template_shadow (decl
))
5379 If T is the name of a class, then each of the following shall
5380 have a name different from T:
5382 -- every static data member of class T;
5384 -- every member of class T that is itself a type;
5386 -- every enumerator of every member of class T that is an
5389 -- every member of every anonymous union that is a member of
5392 (Non-static data members were also forbidden to have the same
5393 name as T until TC1.) */
5395 || TREE_CODE (x
) == CONST_DECL
5396 || (TREE_CODE (x
) == TYPE_DECL
5397 && !DECL_SELF_REFERENCE_P (x
))
5398 /* A data member of an anonymous union. */
5399 || (TREE_CODE (x
) == FIELD_DECL
5400 && DECL_CONTEXT (x
) != current_class_type
))
5401 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
5403 tree scope
= context_for_name_lookup (x
);
5404 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
5406 error_at (DECL_SOURCE_LOCATION (x
),
5407 "%qD has the same name as the class in which it is "
5413 /* Get the current binding for NAME in this class, if any. */
5414 binding
= IDENTIFIER_BINDING (name
);
5415 if (!binding
|| binding
->scope
!= class_binding_level
)
5417 binding
= get_class_binding (name
, class_binding_level
);
5418 /* If a new binding was created, put it at the front of the
5419 IDENTIFIER_BINDING list. */
5422 binding
->previous
= IDENTIFIER_BINDING (name
);
5423 IDENTIFIER_BINDING (name
) = binding
;
5427 /* If there is already a binding, then we may need to update the
5429 if (binding
&& binding
->value
)
5431 tree bval
= binding
->value
;
5432 tree old_decl
= NULL_TREE
;
5433 tree target_decl
= strip_using_decl (decl
);
5434 tree target_bval
= strip_using_decl (bval
);
5436 if (INHERITED_VALUE_BINDING_P (binding
))
5438 /* If the old binding was from a base class, and was for a
5439 tag name, slide it over to make room for the new binding.
5440 The old binding is still visible if explicitly qualified
5441 with a class-key. */
5442 if (TREE_CODE (target_bval
) == TYPE_DECL
5443 && DECL_ARTIFICIAL (target_bval
)
5444 && !(TREE_CODE (target_decl
) == TYPE_DECL
5445 && DECL_ARTIFICIAL (target_decl
)))
5447 old_decl
= binding
->type
;
5448 binding
->type
= bval
;
5449 binding
->value
= NULL_TREE
;
5450 INHERITED_VALUE_BINDING_P (binding
) = 0;
5455 /* Any inherited type declaration is hidden by the type
5456 declaration in the derived class. */
5457 if (TREE_CODE (target_decl
) == TYPE_DECL
5458 && DECL_ARTIFICIAL (target_decl
))
5459 binding
->type
= NULL_TREE
;
5462 else if (TREE_CODE (decl
) == USING_DECL
5463 && TREE_CODE (bval
) == USING_DECL
5464 && same_type_p (USING_DECL_SCOPE (decl
),
5465 USING_DECL_SCOPE (bval
)))
5466 /* This is a using redeclaration that will be diagnosed later
5467 in supplement_binding */
5469 else if (TREE_CODE (decl
) == USING_DECL
5470 && TREE_CODE (bval
) == USING_DECL
5471 && DECL_DEPENDENT_P (decl
)
5472 && DECL_DEPENDENT_P (bval
))
5474 else if (TREE_CODE (decl
) == USING_DECL
5475 && OVL_P (target_bval
))
5477 else if (TREE_CODE (bval
) == USING_DECL
5478 && OVL_P (target_decl
))
5480 else if (OVL_P (target_decl
)
5481 && OVL_P (target_bval
))
5484 if (old_decl
&& binding
->scope
== class_binding_level
)
5487 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5488 here. This function is only used to register bindings
5489 from with the class definition itself. */
5490 INHERITED_VALUE_BINDING_P (binding
) = 0;
5495 /* Note that we declared this value so that we can issue an error if
5496 this is an invalid redeclaration of a name already used for some
5498 note_name_declared_in_class (name
, decl
);
5500 /* If we didn't replace an existing binding, put the binding on the
5501 stack of bindings for the identifier, and update the shadowed
5503 if (binding
&& binding
->scope
== class_binding_level
)
5504 /* Supplement the existing binding. */
5505 ok
= supplement_binding (binding
, decl
);
5508 /* Create a new binding. */
5509 push_binding (name
, decl
, class_binding_level
);
5516 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5517 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5521 lookup_using_decl (tree scope
, name_lookup
&lookup
)
5523 tree current
= current_scope ();
5524 bool dependent_p
= false;
5525 tree binfo
= NULL_TREE
;
5526 base_kind b_kind
= bk_not_base
;
5528 /* Because C++20 breaks the invariant that only member using-decls
5529 refer to members and only non-member using-decls refer to
5530 non-members, we first do the lookups, and then do validation that
5531 what we found is ok. */
5533 if (TREE_CODE (scope
) == ENUMERAL_TYPE
5534 && cxx_dialect
< cxx20
5535 && UNSCOPED_ENUM_P (scope
)
5536 && !TYPE_FUNCTION_SCOPE_P (scope
))
5538 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5539 should allow it as meaning the enclosing scope. I don't see any
5540 justification for this in C++11, but let's keep allowing it. */
5541 tree ctx
= CP_TYPE_CONTEXT (scope
);
5542 if (CLASS_TYPE_P (ctx
) == CLASS_TYPE_P (current
))
5546 /* You cannot using-decl a destructor. */
5547 if (TREE_CODE (lookup
.name
) == BIT_NOT_EXPR
)
5549 error ("%<%T%s%D%> names destructor", scope
,
5550 &"::"[scope
== global_namespace
? 2 : 0], lookup
.name
);
5554 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5556 /* Naming a namespace member. */
5557 qualified_namespace_lookup (scope
, &lookup
);
5559 if (TYPE_P (current
)
5562 || cxx_dialect
< cxx20
5563 || TREE_CODE (lookup
.value
) != CONST_DECL
))
5565 error ("using-declaration for non-member at class scope");
5569 else if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
5571 /* Naming an enumeration member. */
5572 if (cxx_dialect
< cxx20
)
5573 error ("%<using%> with enumeration scope %q#T "
5574 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5576 lookup
.value
= lookup_enumerator (scope
, lookup
.name
);
5580 /* Naming a class member. This is awkward in C++20, because we
5581 might be naming an enumerator of an unrelated class. */
5583 tree npscope
= scope
;
5584 if (PACK_EXPANSION_P (scope
))
5585 npscope
= PACK_EXPANSION_PATTERN (scope
);
5587 if (!MAYBE_CLASS_TYPE_P (npscope
))
5589 error ("%qT is not a class, namespace, or enumeration", npscope
);
5593 /* Using T::T declares inheriting ctors, even if T is a typedef. */
5594 if (lookup
.name
== TYPE_IDENTIFIER (npscope
)
5595 || constructor_name_p (lookup
.name
, npscope
))
5597 if (!TYPE_P (current
))
5599 error ("non-member using-declaration names constructor of %qT",
5603 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
5604 lookup
.name
= ctor_identifier
;
5605 CLASSTYPE_NON_AGGREGATE (current
) = true;
5608 if (!TYPE_P (current
) && cxx_dialect
< cxx20
)
5610 error ("using-declaration for member at non-class scope");
5614 bool depscope
= dependent_scope_p (scope
);
5617 /* Leave binfo null. */;
5618 else if (TYPE_P (current
))
5620 binfo
= lookup_base (current
, scope
, ba_any
, &b_kind
, tf_none
);
5621 gcc_checking_assert (b_kind
>= bk_not_base
);
5623 if (b_kind
== bk_not_base
&& any_dependent_bases_p ())
5624 /* Treat as-if dependent. */
5626 else if (lookup
.name
== ctor_identifier
5627 && (b_kind
< bk_proper_base
|| !binfo_direct_p (binfo
)))
5629 if (any_dependent_bases_p ())
5633 error ("%qT is not a direct base of %qT", scope
, current
);
5638 if (b_kind
< bk_proper_base
)
5639 binfo
= TYPE_BINFO (scope
);
5642 binfo
= TYPE_BINFO (scope
);
5644 dependent_p
= (depscope
5645 || (IDENTIFIER_CONV_OP_P (lookup
.name
)
5646 && dependent_type_p (TREE_TYPE (lookup
.name
))));
5649 lookup
.value
= lookup_member (binfo
, lookup
.name
, /*protect=*/2,
5650 /*want_type=*/false, tf_none
);
5652 if (!depscope
&& b_kind
< bk_proper_base
)
5654 if (cxx_dialect
>= cxx20
&& lookup
.value
5655 && TREE_CODE (lookup
.value
) == CONST_DECL
)
5657 /* Using an unrelated enum; check access here rather
5658 than separately for class and non-class using. */
5659 perform_or_defer_access_check
5660 (binfo
, lookup
.value
, lookup
.value
, tf_warning_or_error
);
5661 /* And then if this is a copy from handle_using_decl, look
5662 through to the original enumerator. */
5663 if (CONST_DECL_USING_P (lookup
.value
))
5664 lookup
.value
= DECL_ABSTRACT_ORIGIN (lookup
.value
);
5666 else if (!TYPE_P (current
))
5668 error ("using-declaration for member at non-class scope");
5673 auto_diagnostic_group g
;
5674 error_not_base_type (scope
, current
);
5675 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
)
5676 && TREE_CODE (TREE_TYPE (lookup
.value
)) == ENUMERAL_TYPE
)
5677 inform (input_location
,
5678 "did you mean %<using enum %T::%D%>?",
5679 scope
, lookup
.name
);
5685 /* Did we find anything sane? */
5688 else if (!lookup
.value
)
5690 error ("%qD has not been declared in %qD", lookup
.name
, scope
);
5693 else if (TREE_CODE (lookup
.value
) == TREE_LIST
5694 /* We can (independently) have ambiguous implicit typedefs. */
5695 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
5697 error ("reference to %qD is ambiguous", lookup
.name
);
5698 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
5699 ? lookup
.value
: lookup
.type
);
5702 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
5704 error ("using-declaration may not name namespace %qD", lookup
.value
);
5708 if (TYPE_P (current
))
5710 /* In class scope. */
5712 /* Cannot introduce a constructor name. */
5713 if (constructor_name_p (lookup
.name
, current
))
5715 error ("%<%T::%D%> names constructor in %qT",
5716 scope
, lookup
.name
, current
);
5720 if (lookup
.value
&& BASELINK_P (lookup
.value
))
5721 /* The binfo from which the functions came does not matter. */
5722 lookup
.value
= BASELINK_FUNCTIONS (lookup
.value
);
5725 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
5726 USING_DECL_SCOPE (using_decl
) = scope
;
5727 USING_DECL_DECLS (using_decl
) = lookup
.value
;
5728 DECL_DEPENDENT_P (using_decl
) = dependent_p
;
5729 DECL_CONTEXT (using_decl
) = current
;
5730 if (TYPE_P (current
) && b_kind
== bk_not_base
)
5731 USING_DECL_UNRELATED_P (using_decl
) = true;
5736 /* Process "using SCOPE::NAME" in a class scope. Return the
5737 USING_DECL created. */
5740 do_class_using_decl (tree scope
, tree name
)
5742 if (name
== error_mark_node
5743 || scope
== error_mark_node
)
5746 name_lookup
lookup (name
);
5747 return lookup_using_decl (scope
, lookup
);
5751 /* Return the binding for NAME in NS in the current TU. If NS is
5752 NULL, look in global_namespace. We will not find declarations
5753 from imports. Users of this who, having found nothing, push a new
5754 decl must be prepared for that pushing to match an existing decl. */
5757 get_namespace_binding (tree ns
, tree name
)
5759 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
5761 ns
= global_namespace
;
5762 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
5763 tree ret
= NULL_TREE
;
5765 if (tree
*b
= find_namespace_slot (ns
, name
))
5769 if (TREE_CODE (ret
) == BINDING_VECTOR
)
5770 ret
= BINDING_VECTOR_CLUSTER (ret
, 0).slots
[0];
5772 ret
= MAYBE_STAT_DECL (ret
);
5778 /* Push internal DECL into the global namespace. Does not do the
5779 full overload fn handling and does not add it to the list of things
5780 in the namespace. */
5783 set_global_binding (tree decl
)
5785 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
5787 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
5790 /* The user's placed something in the implementor's namespace. */
5791 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
5793 /* Force the binding, so compiler internals continue to work. */
5797 /* Set the context of a declaration to scope. Complain if we are not
5801 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
5803 /* Get rid of namespace aliases. */
5804 scope
= ORIGINAL_NAMESPACE (scope
);
5806 /* It is ok for friends to be qualified in parallel space. */
5807 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
5808 error ("declaration of %qD not in a namespace surrounding %qD",
5810 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5812 /* See whether this has been declared in the namespace or inline
5814 tree old
= NULL_TREE
;
5816 name_lookup
lookup (DECL_NAME (decl
),
5817 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
5818 if (!lookup
.search_qualified (scope
, /*usings=*/false))
5819 /* No old declaration at all. */
5824 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
5825 if (TREE_CODE (old
) == TREE_LIST
)
5828 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5829 error ("reference to %qD is ambiguous", decl
);
5830 print_candidates (old
);
5834 if (!DECL_DECLARES_FUNCTION_P (decl
))
5836 /* Don't compare non-function decls with decls_match here, since
5837 it can't check for the correct constness at this
5838 point. pushdecl will find those errors later. */
5840 /* We might have found it in an inline namespace child of SCOPE. */
5841 if (TREE_CODE (decl
) == TREE_CODE (old
))
5842 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
5845 /* Writing "N::i" to declare something directly in "N" is invalid. */
5846 if (CP_DECL_CONTEXT (decl
) == current_namespace
5847 && at_namespace_scope_p ())
5848 error_at (DECL_SOURCE_LOCATION (decl
),
5849 "explicit qualification in declaration of %qD", decl
);
5853 /* Since decl is a function, old should contain a function decl. */
5857 /* It didn't work, go back to the explicit scope. */
5858 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5859 error ("%qD should have been declared inside %qD", decl
, scope
);
5864 /* We handle these in check_explicit_instantiation_namespace. */
5865 if (processing_explicit_instantiation
)
5867 if (processing_template_decl
|| processing_specialization
)
5868 /* We have not yet called push_template_decl to turn a
5869 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
5870 match. But, we'll check later, when we construct the
5874 /* Instantiations or specializations of templates may be declared as
5875 friends in any namespace. */
5876 if (friendp
&& DECL_USE_TEMPLATE (decl
))
5879 tree found
= NULL_TREE
;
5880 bool hidden_p
= false;
5882 for (lkp_iterator
iter (old
); iter
; ++iter
)
5884 if (iter
.using_p ())
5889 /* Adjust DECL_CONTEXT first so decls_match will return true
5890 if DECL will match a declaration in an inline namespace. */
5891 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
5892 if (decls_match (decl
, ofn
))
5896 /* We found more than one matching declaration. This
5897 can happen if we have two inline namespace children,
5898 each containing a suitable declaration. */
5899 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5903 hidden_p
= iter
.hidden_p ();
5911 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
5912 "%qD has not been declared within %qD", decl
, scope
);
5913 inform (DECL_SOURCE_LOCATION (found
),
5914 "only here as a %<friend%>");
5916 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
5923 /* Return the namespace where the current declaration is declared. */
5926 current_decl_namespace (void)
5929 /* If we have been pushed into a different namespace, use it. */
5930 if (!vec_safe_is_empty (decl_namespace_list
))
5931 return decl_namespace_list
->last ();
5933 if (current_class_type
)
5934 result
= decl_namespace_context (current_class_type
);
5935 else if (current_function_decl
)
5936 result
= decl_namespace_context (current_function_decl
);
5938 result
= current_namespace
;
5942 /* Process any ATTRIBUTES on a namespace definition. Returns true if
5943 attribute visibility is seen. */
5946 handle_namespace_attrs (tree ns
, tree attributes
)
5949 bool saw_vis
= false;
5951 if (attributes
== error_mark_node
)
5954 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
5956 tree name
= get_attribute_name (d
);
5957 tree args
= TREE_VALUE (d
);
5959 if (is_attribute_p ("visibility", name
))
5961 /* attribute visibility is a property of the syntactic block
5962 rather than the namespace as a whole, so we don't touch the
5963 NAMESPACE_DECL at all. */
5964 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
5965 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
5967 warning (OPT_Wattributes
,
5968 "%qD attribute requires a single NTBS argument",
5973 if (!TREE_PUBLIC (ns
))
5974 warning (OPT_Wattributes
,
5975 "%qD attribute is meaningless since members of the "
5976 "anonymous namespace get local symbols", name
);
5978 push_visibility (TREE_STRING_POINTER (x
), 1);
5981 else if (is_attribute_p ("abi_tag", name
))
5983 if (!DECL_NAME (ns
))
5985 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
5989 if (!DECL_NAMESPACE_INLINE_P (ns
))
5991 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
5997 tree dn
= DECL_NAME (ns
);
5998 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
5999 IDENTIFIER_POINTER (dn
));
6000 TREE_TYPE (args
) = char_array_type_node
;
6001 args
= fix_string_type (args
);
6002 args
= build_tree_list (NULL_TREE
, args
);
6004 if (check_abi_tag_args (args
, name
))
6005 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6006 DECL_ATTRIBUTES (ns
));
6008 else if (is_attribute_p ("deprecated", name
))
6010 if (!DECL_NAME (ns
))
6012 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
6016 if (args
&& TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
6018 error ("deprecated message is not a string");
6021 TREE_DEPRECATED (ns
) = 1;
6023 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6024 DECL_ATTRIBUTES (ns
));
6028 warning (OPT_Wattributes
, "%qD attribute directive ignored",
6037 /* Temporarily set the namespace for the current declaration. */
6040 push_decl_namespace (tree decl
)
6042 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
6043 decl
= decl_namespace_context (decl
);
6044 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
6047 /* [namespace.memdef]/2 */
6050 pop_decl_namespace (void)
6052 decl_namespace_list
->pop ();
6055 /* Process a namespace-alias declaration. */
6058 do_namespace_alias (tree alias
, tree name_space
)
6060 if (name_space
== error_mark_node
)
6063 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
6065 name_space
= ORIGINAL_NAMESPACE (name_space
);
6067 /* Build the alias. */
6068 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
6069 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
6070 DECL_EXTERNAL (alias
) = 1;
6071 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
6072 set_originating_module (alias
);
6076 /* Emit debug info for namespace alias. */
6077 if (!building_stmt_list_p ())
6078 (*debug_hooks
->early_global_decl
) (alias
);
6081 /* Like pushdecl, only it places DECL in the current namespace,
6085 pushdecl_namespace_level (tree decl
, bool hiding
)
6087 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
6088 return do_pushdecl_with_scope (decl
, NAMESPACE_LEVEL (current_namespace
),
6092 /* Wrapper around push_local_binding to push the bindings for
6093 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6094 is the result of name lookup during template parsing. */
6097 push_using_decl_bindings (name_lookup
*lookup
, tree name
, tree value
)
6099 tree type
= NULL_TREE
;
6101 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
6104 value
= binding
->value
;
6105 type
= binding
->type
;
6108 /* DR 36 questions why using-decls at function scope may not be
6109 duplicates. Disallow it, as C++11 claimed and PR 20420
6112 do_nonmember_using_decl (*lookup
, true, true, &value
, &type
);
6116 else if (binding
&& value
== binding
->value
)
6117 /* Redeclaration of this USING_DECL. */;
6118 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
6120 /* We already have this binding, so replace it. */
6121 update_local_overload (IDENTIFIER_BINDING (name
), value
);
6122 IDENTIFIER_BINDING (name
)->value
= value
;
6125 /* Install the new binding. */
6126 push_local_binding (name
, value
, /*using=*/true);
6130 else if (binding
&& type
== binding
->type
)
6134 push_local_binding (name
, type
, /*using=*/true);
6135 set_identifier_type_value (name
, type
);
6139 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6142 push_using_decl_bindings (tree name
, tree value
)
6144 push_using_decl_bindings (nullptr, name
, value
);
6147 /* Process a using declaration in non-class scope. */
6150 finish_nonmember_using_decl (tree scope
, tree name
)
6152 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
6154 if (scope
== error_mark_node
|| name
== error_mark_node
)
6157 name_lookup
lookup (name
);
6159 tree using_decl
= lookup_using_decl (scope
, lookup
);
6163 /* Emit debug info. */
6164 if (!processing_template_decl
)
6165 cp_emit_debug_info_for_using (lookup
.value
,
6166 current_binding_level
->this_entity
);
6168 if (current_binding_level
->kind
== sk_namespace
)
6170 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
6171 tree
*mslot
= get_fixed_binding_slot (slot
, name
,
6172 BINDING_SLOT_CURRENT
, true);
6173 bool failed
= false;
6177 /* A module vector. I presume the binding list is going to
6178 be sparser than the import bitmap. Hence iterate over
6179 the former checking for bits set in the bitmap. */
6180 bitmap imports
= get_import_bitmap ();
6181 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
6183 /* Scan the imported bindings. */
6184 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
6185 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
6191 /* Do this in forward order, so we load modules in an order
6192 the user expects. */
6193 for (; ix
--; cluster
++)
6194 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
6196 /* Are we importing this module? */
6197 if (unsigned base
= cluster
->indices
[jx
].base
)
6198 if (unsigned span
= cluster
->indices
[jx
].span
)
6200 if (bitmap_bit_p (imports
, base
))
6202 while (++base
, --span
);
6207 if (cluster
->slots
[jx
].is_lazy ())
6209 gcc_assert (cluster
->indices
[jx
].span
== 1);
6210 lazy_load_binding (cluster
->indices
[jx
].base
,
6211 scope
, name
, &cluster
->slots
[jx
]);
6214 tree value
= cluster
->slots
[jx
];
6216 /* Load errors could mean there's nothing here. */
6219 /* Extract what we can see from here. If there's no
6220 stat_hack, then everything was exported. */
6221 tree type
= NULL_TREE
;
6223 /* If no stat hack, everything is visible. */
6224 if (STAT_HACK_P (value
))
6226 if (STAT_TYPE_VISIBLE_P (value
))
6227 type
= STAT_TYPE (value
);
6228 value
= STAT_VISIBLE (value
);
6231 if (do_nonmember_using_decl (lookup
, false, false,
6242 /* Now do the current slot. */
6243 tree value
= MAYBE_STAT_DECL (*mslot
);
6244 tree type
= MAYBE_STAT_TYPE (*mslot
);
6246 do_nonmember_using_decl (lookup
, false, true, &value
, &type
);
6248 // FIXME: Partition mergeableness?
6249 if (STAT_HACK_P (*mslot
))
6251 STAT_DECL (*mslot
) = value
;
6252 STAT_TYPE (*mslot
) = type
;
6255 *mslot
= stat_hack (value
, type
);
6262 add_decl_expr (using_decl
);
6263 if (DECL_DEPENDENT_P (using_decl
))
6264 lookup
.value
= using_decl
;
6265 push_using_decl_bindings (&lookup
, name
, NULL_TREE
);
6269 /* Return the declarations that are members of the namespace NS. */
6272 cp_namespace_decls (tree ns
)
6274 return NAMESPACE_LEVEL (ns
)->names
;
6277 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6278 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6281 qualify_lookup (tree val
, LOOK_want want
)
6283 if (val
== NULL_TREE
)
6286 if (bool (want
& LOOK_want::TYPE
))
6288 tree target_val
= strip_using_decl (val
);
6290 if (TREE_CODE (STRIP_TEMPLATE (target_val
)) == TYPE_DECL
)
6294 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
6295 return TREE_CODE (val
) == NAMESPACE_DECL
;
6300 /* Is there a "using namespace std;" directive within USINGS? */
6303 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
6308 for (unsigned ix
= usings
->length (); ix
--;)
6309 if ((*usings
)[ix
] == std_node
)
6315 /* Is there a "using namespace std;" directive within the current
6316 namespace (or its ancestors)?
6317 Compare with name_lookup::search_unqualified. */
6320 has_using_namespace_std_directive_p ()
6322 for (cp_binding_level
*level
= current_binding_level
;
6324 level
= level
->level_chain
)
6325 if (using_directives_contain_std_p (level
->using_directives
))
6331 /* Subclass of deferred_diagnostic, for issuing a note when
6332 --param cxx-max-namespaces-for-diagnostic-help is reached.
6334 The note should be issued after the error, but before any other
6335 deferred diagnostics. This is handled by decorating a wrapped
6336 deferred_diagnostic, and emitting a note before that wrapped note is
6339 class namespace_limit_reached
: public deferred_diagnostic
6342 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
6343 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
6344 : deferred_diagnostic (loc
),
6345 m_limit (limit
), m_name (name
),
6346 m_wrapped (move (wrapped
))
6350 ~namespace_limit_reached ()
6352 /* Unconditionally warn that the search was truncated. */
6353 inform (get_location (),
6354 "maximum limit of %d namespaces searched for %qE",
6356 /* m_wrapped will be implicitly deleted after this, emitting any followup
6357 diagnostic after the above note. */
6363 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
6366 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6367 Emit a note showing the location of the declaration of the suggestion. */
6369 class show_candidate_location
: public deferred_diagnostic
6372 show_candidate_location (location_t loc
, tree candidate
)
6373 : deferred_diagnostic (loc
),
6374 m_candidate (candidate
)
6378 ~show_candidate_location ()
6380 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
6387 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6388 to be suggested by suggest_alternatives_for.
6390 Emit a series of notes showing the various suggestions. */
6392 class suggest_alternatives
: public deferred_diagnostic
6395 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
6396 : deferred_diagnostic (loc
),
6397 m_candidates (candidates
)
6401 ~suggest_alternatives ()
6403 if (m_candidates
.length ())
6405 inform_n (get_location (), m_candidates
.length (),
6406 "suggested alternative:",
6407 "suggested alternatives:");
6408 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
6410 tree val
= m_candidates
[ix
];
6412 inform (location_of (val
), " %qE", val
);
6415 m_candidates
.release ();
6419 vec
<tree
> m_candidates
;
6422 /* A class for encapsulating the result of a search across
6423 multiple namespaces (and scoped enums within them) for an
6424 unrecognized name seen at a given source location. */
6426 class namespace_hints
6429 namespace_hints (location_t loc
, tree name
);
6431 name_hint
convert_candidates_to_name_hint ();
6432 name_hint
maybe_decorate_with_limit (name_hint
);
6435 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
6439 vec
<tree
> m_candidates
;
6441 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6444 /* Was the limit reached? */
6448 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6449 looking for an exact match for unrecognized NAME seen at LOC. */
6451 namespace_hints::namespace_hints (location_t loc
, tree name
)
6452 : m_loc(loc
), m_name (name
)
6454 auto_vec
<tree
> worklist
;
6456 m_candidates
= vNULL
;
6458 m_limit
= param_cxx_max_namespaces_for_diagnostic_help
;
6460 /* Breadth-first search of namespaces. Up to limit namespaces
6461 searched (limit zero == unlimited). */
6462 worklist
.safe_push (global_namespace
);
6463 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
6465 tree ns
= worklist
[ix
];
6466 name_lookup
lookup (name
);
6468 if (lookup
.search_qualified (ns
, false))
6469 m_candidates
.safe_push (lookup
.value
);
6473 /* Look for child namespaces. We have to do this
6474 indirectly because they are chained in reverse order,
6475 which is confusing to the user. */
6476 auto_vec
<tree
> children
;
6478 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
6479 decl
; decl
= TREE_CHAIN (decl
))
6481 if (TREE_CODE (decl
) == NAMESPACE_DECL
6482 && !DECL_NAMESPACE_ALIAS (decl
)
6483 && !DECL_NAMESPACE_INLINE_P (decl
))
6484 children
.safe_push (decl
);
6486 /* Look for exact matches for NAME within scoped enums.
6487 These aren't added to the worklist, and so don't count
6488 against the search limit. */
6489 if (TREE_CODE (decl
) == TYPE_DECL
)
6491 tree type
= TREE_TYPE (decl
);
6492 if (SCOPED_ENUM_P (type
))
6493 maybe_add_candidate_for_scoped_enum (type
, name
);
6497 while (!m_limited
&& !children
.is_empty ())
6499 if (worklist
.length () == m_limit
)
6502 worklist
.safe_push (children
.pop ());
6508 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6509 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6511 If m_candidates is non-empty, use it to generate a suggestion and/or
6512 a deferred diagnostic that lists the possible candidate(s).
6516 namespace_hints::convert_candidates_to_name_hint ()
6518 /* How many candidates do we have? */
6520 /* If we have just one candidate, issue a name_hint with it as a suggestion
6521 (so that consumers are able to suggest it within the error message and emit
6522 it as a fix-it hint), and with a note showing the candidate's location. */
6523 if (m_candidates
.length () == 1)
6525 tree candidate
= m_candidates
[0];
6526 /* Clean up CANDIDATES. */
6527 m_candidates
.release ();
6528 return name_hint (expr_to_string (candidate
),
6529 new show_candidate_location (m_loc
, candidate
));
6531 else if (m_candidates
.length () > 1)
6532 /* If we have more than one candidate, issue a name_hint without a single
6533 "suggestion", but with a deferred diagnostic that lists the
6534 various candidates. This takes ownership of m_candidates. */
6535 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
6537 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6538 gcc_assert (m_candidates
.length () == 0);
6539 gcc_assert (m_candidates
== vNULL
);
6541 return name_hint ();
6544 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6545 then we want to emit a note about after the error, but before
6546 any other deferred diagnostics.
6548 Handle this by figuring out what hint is needed, then optionally
6549 decorating HINT with a namespace_limit_reached wrapper. */
6552 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
6555 return name_hint (hint
.suggestion (),
6556 new namespace_limit_reached (m_loc
, m_limit
,
6558 hint
.take_deferred ()));
6563 /* Look inside SCOPED_ENUM for exact matches for NAME.
6564 If one is found, add its CONST_DECL to m_candidates. */
6567 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
6570 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
6572 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
6574 tree id
= TREE_PURPOSE (iter
);
6577 m_candidates
.safe_push (TREE_VALUE (iter
));
6583 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6586 Search through all available namespaces and any scoped enums within them
6587 and generate a suggestion and/or a deferred diagnostic that lists possible
6590 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
6591 look for near-matches and suggest the best near-match, if there is one.
6593 If nothing is found, then an empty name_hint is returned. */
6596 suggest_alternatives_for (location_t location
, tree name
,
6597 bool suggest_misspellings
)
6599 /* First, search for exact matches in other namespaces. */
6600 namespace_hints
ns_hints (location
, name
);
6601 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6603 /* Otherwise, try other approaches. */
6605 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
6607 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6610 /* The second half of suggest_alternatives_for, for when no exact matches
6611 were found in other namespaces. */
6614 suggest_alternatives_for_1 (location_t location
, tree name
,
6615 bool suggest_misspellings
)
6617 /* No candidates were found in the available namespaces. */
6619 /* If there's a "using namespace std;" active, and this
6620 is one of the most common "std::" names, then it's probably a
6621 missing #include. */
6622 if (has_using_namespace_std_directive_p ())
6624 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
6629 /* Otherwise, consider misspellings. */
6630 if (!suggest_misspellings
)
6631 return name_hint ();
6633 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
6636 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6639 Search through all available namespaces and generate a suggestion and/or
6640 a deferred diagnostic that lists possible candidate(s).
6642 This is similiar to suggest_alternatives_for, but doesn't fallback to
6643 the other approaches used by that function. */
6646 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
6648 namespace_hints
ns_hints (location
, name
);
6650 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6652 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6655 /* A well-known name within the C++ standard library, returned by
6656 get_std_name_hint. */
6658 struct std_name_hint
6660 /* A name within "std::". */
6663 /* The header name defining it within the C++ Standard Library
6664 (with '<' and '>'). */
6667 /* The dialect of C++ in which this was added. */
6668 enum cxx_dialect min_dialect
;
6671 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
6672 for some of the most common names within "std::".
6673 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
6675 static const std_name_hint
*
6676 get_std_name_hint (const char *name
)
6678 static const std_name_hint hints
[] = {
6680 {"any", "<any>", cxx17
},
6681 {"any_cast", "<any>", cxx17
},
6682 {"make_any", "<any>", cxx17
},
6684 {"array", "<array>", cxx11
},
6685 {"to_array", "<array>", cxx20
},
6687 {"atomic", "<atomic>", cxx11
},
6688 {"atomic_flag", "<atomic>", cxx11
},
6689 {"atomic_ref", "<atomic>", cxx20
},
6691 {"bitset", "<bitset>", cxx11
},
6693 {"weak_equality", "<compare>", cxx20
},
6694 {"strong_equality", "<compare>", cxx20
},
6695 {"partial_ordering", "<compare>", cxx20
},
6696 {"weak_ordering", "<compare>", cxx20
},
6697 {"strong_ordering", "<compare>", cxx20
},
6699 {"complex", "<complex>", cxx98
},
6700 {"complex_literals", "<complex>", cxx14
},
6701 /* <condition_variable>. */
6702 {"condition_variable", "<condition_variable>", cxx11
},
6703 {"condition_variable_any", "<condition_variable>", cxx11
},
6705 {"byte", "<cstddef>", cxx17
},
6707 {"deque", "<deque>", cxx98
},
6708 /* <forward_list>. */
6709 {"forward_list", "<forward_list>", cxx11
},
6711 {"basic_filebuf", "<fstream>", cxx98
},
6712 {"basic_ifstream", "<fstream>", cxx98
},
6713 {"basic_ofstream", "<fstream>", cxx98
},
6714 {"basic_fstream", "<fstream>", cxx98
},
6715 {"fstream", "<fstream>", cxx98
},
6716 {"ifstream", "<fstream>", cxx98
},
6717 {"ofstream", "<fstream>", cxx98
},
6719 {"bind", "<functional>", cxx11
},
6720 {"bind_front", "<functional>", cxx20
},
6721 {"function", "<functional>", cxx11
},
6722 {"hash", "<functional>", cxx11
},
6723 {"invoke", "<functional>", cxx17
},
6724 {"mem_fn", "<functional>", cxx11
},
6725 {"not_fn", "<functional>", cxx17
},
6726 {"reference_wrapper", "<functional>", cxx11
},
6727 {"unwrap_reference", "<functional>", cxx20
},
6728 {"unwrap_reference_t", "<functional>", cxx20
},
6729 {"unwrap_ref_decay", "<functional>", cxx20
},
6730 {"unwrap_ref_decay_t", "<functional>", cxx20
},
6732 {"async", "<future>", cxx11
},
6733 {"future", "<future>", cxx11
},
6734 {"packaged_task", "<future>", cxx11
},
6735 {"promise", "<future>", cxx11
},
6737 {"cin", "<iostream>", cxx98
},
6738 {"cout", "<iostream>", cxx98
},
6739 {"cerr", "<iostream>", cxx98
},
6740 {"clog", "<iostream>", cxx98
},
6741 {"wcin", "<iostream>", cxx98
},
6742 {"wcout", "<iostream>", cxx98
},
6743 {"wclog", "<iostream>", cxx98
},
6745 {"istream", "<istream>", cxx98
},
6747 {"advance", "<iterator>", cxx98
},
6748 {"back_inserter", "<iterator>", cxx98
},
6749 {"begin", "<iterator>", cxx11
},
6750 {"distance", "<iterator>", cxx98
},
6751 {"end", "<iterator>", cxx11
},
6752 {"front_inserter", "<iterator>", cxx98
},
6753 {"inserter", "<iterator>", cxx98
},
6754 {"istream_iterator", "<iterator>", cxx98
},
6755 {"istreambuf_iterator", "<iterator>", cxx98
},
6756 {"iterator_traits", "<iterator>", cxx98
},
6757 {"move_iterator", "<iterator>", cxx11
},
6758 {"next", "<iterator>", cxx11
},
6759 {"ostream_iterator", "<iterator>", cxx98
},
6760 {"ostreambuf_iterator", "<iterator>", cxx98
},
6761 {"prev", "<iterator>", cxx11
},
6762 {"reverse_iterator", "<iterator>", cxx98
},
6764 {"ostream", "<ostream>", cxx98
},
6766 {"list", "<list>", cxx98
},
6768 {"map", "<map>", cxx98
},
6769 {"multimap", "<map>", cxx98
},
6771 {"allocate_shared", "<memory>", cxx11
},
6772 {"allocator", "<memory>", cxx98
},
6773 {"allocator_traits", "<memory>", cxx11
},
6774 {"make_shared", "<memory>", cxx11
},
6775 {"make_unique", "<memory>", cxx14
},
6776 {"shared_ptr", "<memory>", cxx11
},
6777 {"unique_ptr", "<memory>", cxx11
},
6778 {"weak_ptr", "<memory>", cxx11
},
6779 /* <memory_resource>. */
6780 {"pmr", "<memory_resource>", cxx17
},
6782 {"mutex", "<mutex>", cxx11
},
6783 {"timed_mutex", "<mutex>", cxx11
},
6784 {"recursive_mutex", "<mutex>", cxx11
},
6785 {"recursive_timed_mutex", "<mutex>", cxx11
},
6786 {"once_flag", "<mutex>", cxx11
},
6787 {"call_once,", "<mutex>", cxx11
},
6788 {"lock", "<mutex>", cxx11
},
6789 {"scoped_lock", "<mutex>", cxx17
},
6790 {"try_lock", "<mutex>", cxx11
},
6791 {"lock_guard", "<mutex>", cxx11
},
6792 {"unique_lock", "<mutex>", cxx11
},
6794 {"optional", "<optional>", cxx17
},
6795 {"make_optional", "<optional>", cxx17
},
6797 {"ostream", "<ostream>", cxx98
},
6798 {"wostream", "<ostream>", cxx98
},
6799 {"ends", "<ostream>", cxx98
},
6800 {"flush", "<ostream>", cxx98
},
6801 {"endl", "<ostream>", cxx98
},
6803 {"queue", "<queue>", cxx98
},
6804 {"priority_queue", "<queue>", cxx98
},
6806 {"set", "<set>", cxx98
},
6807 {"multiset", "<set>", cxx98
},
6808 /* <shared_mutex>. */
6809 {"shared_lock", "<shared_mutex>", cxx14
},
6810 {"shared_mutex", "<shared_mutex>", cxx17
},
6811 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
6812 /* <source_location>. */
6813 {"source_location", "<source_location>", cxx20
},
6815 {"basic_stringbuf", "<sstream>", cxx98
},
6816 {"basic_istringstream", "<sstream>", cxx98
},
6817 {"basic_ostringstream", "<sstream>", cxx98
},
6818 {"basic_stringstream", "<sstream>", cxx98
},
6819 {"istringstream", "<sstream>", cxx98
},
6820 {"ostringstream", "<sstream>", cxx98
},
6821 {"stringstream", "<sstream>", cxx98
},
6823 {"stack", "<stack>", cxx98
},
6825 {"basic_string", "<string>", cxx98
},
6826 {"string", "<string>", cxx98
},
6827 {"wstring", "<string>", cxx98
},
6828 {"u8string", "<string>", cxx20
},
6829 {"u16string", "<string>", cxx11
},
6830 {"u32string", "<string>", cxx11
},
6831 /* <string_view>. */
6832 {"basic_string_view", "<string_view>", cxx17
},
6833 {"string_view", "<string_view>", cxx17
},
6835 {"thread", "<thread>", cxx11
},
6836 {"this_thread", "<thread>", cxx11
},
6838 {"apply", "<tuple>", cxx17
},
6839 {"forward_as_tuple", "<tuple>", cxx11
},
6840 {"make_from_tuple", "<tuple>", cxx17
},
6841 {"make_tuple", "<tuple>", cxx11
},
6842 {"tie", "<tuple>", cxx11
},
6843 {"tuple", "<tuple>", cxx11
},
6844 {"tuple_cat", "<tuple>", cxx11
},
6845 {"tuple_element", "<tuple>", cxx11
},
6846 {"tuple_element_t", "<tuple>", cxx14
},
6847 {"tuple_size", "<tuple>", cxx11
},
6848 {"tuple_size_v", "<tuple>", cxx17
},
6849 /* <type_traits>. */
6850 {"enable_if", "<type_traits>", cxx11
},
6851 {"enable_if_t", "<type_traits>", cxx14
},
6852 {"invoke_result", "<type_traits>", cxx17
},
6853 {"invoke_result_t", "<type_traits>", cxx17
},
6854 {"remove_cvref", "<type_traits>", cxx20
},
6855 {"remove_cvref_t", "<type_traits>", cxx20
},
6856 {"type_identity", "<type_traits>", cxx20
},
6857 {"type_identity_t", "<type_traits>", cxx20
},
6858 {"void_t", "<type_traits>", cxx17
},
6859 {"conjunction", "<type_traits>", cxx17
},
6860 {"conjunction_v", "<type_traits>", cxx17
},
6861 {"disjunction", "<type_traits>", cxx17
},
6862 {"disjunction_v", "<type_traits>", cxx17
},
6863 {"negation", "<type_traits>", cxx17
},
6864 {"negation_v", "<type_traits>", cxx17
},
6865 /* <unordered_map>. */
6866 {"unordered_map", "<unordered_map>", cxx11
},
6867 {"unordered_multimap", "<unordered_map>", cxx11
},
6868 /* <unordered_set>. */
6869 {"unordered_set", "<unordered_set>", cxx11
},
6870 {"unordered_multiset", "<unordered_set>", cxx11
},
6872 {"declval", "<utility>", cxx11
},
6873 {"forward", "<utility>", cxx11
},
6874 {"make_pair", "<utility>", cxx98
},
6875 {"move", "<utility>", cxx11
},
6876 {"pair", "<utility>", cxx98
},
6878 {"variant", "<variant>", cxx17
},
6879 {"visit", "<variant>", cxx17
},
6881 {"vector", "<vector>", cxx98
},
6883 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
6884 for (size_t i
= 0; i
< num_hints
; i
++)
6886 if (strcmp (name
, hints
[i
].name
) == 0)
6892 /* Describe DIALECT. */
6895 get_cxx_dialect_name (enum cxx_dialect dialect
)
6916 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
6917 that weren't recognized, but for which we know which header it ought to be
6920 Emit a note either suggesting the header to be included, or noting that
6921 the current dialect is too early for the given name. */
6923 class missing_std_header
: public deferred_diagnostic
6926 missing_std_header (location_t loc
,
6927 const char *name_str
,
6928 const std_name_hint
*header_hint
)
6929 : deferred_diagnostic (loc
),
6930 m_name_str (name_str
),
6931 m_header_hint (header_hint
)
6933 ~missing_std_header ()
6935 gcc_rich_location
richloc (get_location ());
6936 if (cxx_dialect
>= m_header_hint
->min_dialect
)
6938 const char *header
= m_header_hint
->header
;
6939 maybe_add_include_fixit (&richloc
, header
, true);
6941 "%<std::%s%> is defined in header %qs;"
6942 " did you forget to %<#include %s%>?",
6943 m_name_str
, header
, header
);
6947 "%<std::%s%> is only available from %s onwards",
6948 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
6952 const char *m_name_str
;
6953 const std_name_hint
*m_header_hint
;
6956 /* Attempt to generate a name_hint that suggests pertinent header files
6957 for NAME at LOCATION, for common names within the "std" namespace,
6958 or an empty name_hint if this isn't applicable. */
6961 maybe_suggest_missing_std_header (location_t location
, tree name
)
6963 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
6965 const char *name_str
= IDENTIFIER_POINTER (name
);
6966 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
6968 return name_hint ();
6970 return name_hint (NULL
, new missing_std_header (location
, name_str
,
6974 /* Attempt to generate a name_hint that suggests a missing header file
6975 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
6979 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
6981 if (scope
== NULL_TREE
)
6982 return name_hint ();
6983 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
6984 return name_hint ();
6985 /* We only offer suggestions for the "std" namespace. */
6986 if (scope
!= std_node
)
6987 return name_hint ();
6988 return maybe_suggest_missing_std_header (location
, name
);
6991 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
6992 lookup failed within the explicitly provided SCOPE.
6994 Suggest the best meaningful candidates (if any), otherwise
6995 an empty name_hint is returned. */
6998 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
7001 /* Something went very wrong; don't suggest anything. */
7002 if (name
== error_mark_node
)
7003 return name_hint ();
7005 /* Resolve any namespace aliases. */
7006 scope
= ORIGINAL_NAMESPACE (scope
);
7008 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
7012 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
7014 best_match
<tree
, const char *> bm (name
);
7015 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
7017 /* See if we have a good suggesion for the user. */
7018 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
7020 return name_hint (fuzzy_name
, NULL
);
7022 return name_hint ();
7025 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7029 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
7031 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
7033 best_match
<tree
, const char *> bm (name
);
7034 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
7036 tree id
= TREE_PURPOSE (iter
);
7037 bm
.consider (IDENTIFIER_POINTER (id
));
7039 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7042 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7045 WANT as for lookup_name_1.
7047 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7048 declaration found. If no suitable declaration can be found,
7049 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7050 neither a class-type nor a namespace a diagnostic is issued. */
7053 lookup_qualified_name (tree scope
, tree name
, LOOK_want want
, bool complain
)
7057 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
7059 name_lookup
lookup (name
, want
);
7061 if (qualified_namespace_lookup (scope
, &lookup
))
7065 /* If we have a known type overload, pull it out. This can happen
7067 if (TREE_CODE (t
) == OVERLOAD
&& TREE_TYPE (t
) != unknown_type_node
)
7068 t
= OVL_FUNCTION (t
);
7071 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
7072 t
= lookup_enumerator (scope
, name
);
7073 else if (is_class_type (scope
, complain
))
7074 t
= lookup_member (scope
, name
, 2, bool (want
& LOOK_want::TYPE
),
7075 tf_warning_or_error
);
7078 return error_mark_node
;
7082 /* Wrapper for the above that takes a string argument. The function name is
7083 not at the beginning of the line to keep this wrapper out of etags. */
7085 tree
lookup_qualified_name (tree t
, const char *p
, LOOK_want w
, bool c
)
7087 return lookup_qualified_name (t
, get_identifier (p
), w
, c
);
7091 Accepts the NAME to lookup and its qualifying SCOPE.
7092 Returns the name/type pair found into the cxx_binding *RESULT,
7093 or false on error. */
7096 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
7098 timevar_start (TV_NAME_LOOKUP
);
7099 query_oracle (lookup
->name
);
7100 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
7101 timevar_stop (TV_NAME_LOOKUP
);
7105 /* If DECL is suitably visible to the user, consider its name for
7106 spelling correction. */
7109 consider_decl (tree decl
, best_match
<tree
, const char *> &bm
,
7110 bool consider_impl_names
)
7112 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7113 within range for). */
7114 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7117 tree suggestion
= DECL_NAME (decl
);
7121 /* Don't suggest names that are for anonymous aggregate types, as
7122 they are an implementation detail generated by the compiler. */
7123 if (IDENTIFIER_ANON_P (suggestion
))
7126 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
7128 /* Ignore internal names with spaces in them. */
7129 if (strchr (suggestion_str
, ' '))
7132 /* Don't suggest names that are reserved for use by the
7133 implementation, unless NAME began with an underscore. */
7134 if (!consider_impl_names
7135 && name_reserved_for_implementation_p (suggestion_str
))
7138 bm
.consider (suggestion_str
);
7141 /* If DECL is suitably visible to the user, add its name to VEC and
7142 return true. Otherwise return false. */
7145 maybe_add_fuzzy_decl (auto_vec
<tree
> &vec
, tree decl
)
7147 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7148 within range for). */
7149 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7152 tree suggestion
= DECL_NAME (decl
);
7156 /* Don't suggest names that are for anonymous aggregate types, as
7157 they are an implementation detail generated by the compiler. */
7158 if (IDENTIFIER_ANON_P (suggestion
))
7161 vec
.safe_push (suggestion
);
7166 /* Examing the namespace binding BINDING, and add at most one instance
7167 of the name, if it contains a visible entity of interest. Return
7168 true if we added something. */
7171 maybe_add_fuzzy_binding (auto_vec
<tree
> &vec
, tree binding
,
7172 lookup_name_fuzzy_kind kind
)
7174 tree value
= NULL_TREE
;
7176 if (STAT_HACK_P (binding
))
7178 if (!STAT_TYPE_HIDDEN_P (binding
)
7179 && STAT_TYPE (binding
))
7181 if (maybe_add_fuzzy_decl (vec
, STAT_TYPE (binding
)))
7184 else if (!STAT_DECL_HIDDEN_P (binding
))
7185 value
= STAT_DECL (binding
);
7190 value
= ovl_skip_hidden (value
);
7193 value
= OVL_FIRST (value
);
7194 if (kind
!= FUZZY_LOOKUP_TYPENAME
7195 || TREE_CODE (STRIP_TEMPLATE (value
)) == TYPE_DECL
)
7196 if (maybe_add_fuzzy_decl (vec
, value
))
7200 /* Nothing found. */
7204 /* Helper function for lookup_name_fuzzy.
7205 Traverse binding level LVL, looking for good name matches for NAME
7208 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
7209 cp_binding_level
*lvl
, bool look_within_fields
,
7210 enum lookup_name_fuzzy_kind kind
)
7212 if (look_within_fields
)
7213 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
7215 tree type
= lvl
->this_entity
;
7216 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
7217 tree best_matching_field
7218 = lookup_member_fuzzy (type
, name
, want_type_p
);
7219 if (best_matching_field
)
7220 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
7223 /* Only suggest names reserved for the implementation if NAME begins
7224 with an underscore. */
7225 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
7227 if (lvl
->kind
!= sk_namespace
)
7228 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
7232 /* OVERLOADs or decls from using declaration are wrapped into
7234 if (TREE_CODE (d
) == TREE_LIST
)
7235 d
= OVL_FIRST (TREE_VALUE (d
));
7237 /* Don't use bindings from implicitly declared functions,
7238 as they were likely misspellings themselves. */
7239 if (TREE_TYPE (d
) == error_mark_node
)
7242 /* If we want a typename, ignore non-types. */
7243 if (kind
== FUZZY_LOOKUP_TYPENAME
7244 && TREE_CODE (STRIP_TEMPLATE (d
)) != TYPE_DECL
)
7247 consider_decl (d
, bm
, consider_implementation_names
);
7251 /* We need to iterate over the namespace hash table, in order to
7252 not mention hidden entities. But hash table iteration is
7253 (essentially) unpredictable, our correction-distance measure
7254 is very granular, and we pick the first of equal distances.
7255 Hence, we need to call the distance-measurer in a predictable
7256 order. So, iterate over the namespace hash, inserting
7257 visible names into a vector. Then sort the vector. Then
7258 determine spelling distance. */
7260 tree ns
= lvl
->this_entity
;
7263 hash_table
<named_decl_hash
>::iterator end
7264 (DECL_NAMESPACE_BINDINGS (ns
)->end ());
7265 for (hash_table
<named_decl_hash
>::iterator iter
7266 (DECL_NAMESPACE_BINDINGS (ns
)->begin ()); iter
!= end
; ++iter
)
7268 tree binding
= *iter
;
7270 if (TREE_CODE (binding
) == BINDING_VECTOR
)
7272 bitmap imports
= get_import_bitmap ();
7273 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
7275 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
7276 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7279 /* Scan the imported bindings. */
7280 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
7281 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
7287 for (; ix
--; cluster
++)
7288 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
7291 /* Are we importing this module? */
7292 if (unsigned base
= cluster
->indices
[jx
].base
)
7293 if (unsigned span
= cluster
->indices
[jx
].span
)
7295 if (bitmap_bit_p (imports
, base
))
7297 while (++base
, --span
);
7302 if (cluster
->slots
[jx
].is_lazy ())
7303 /* Let's not read in everything on the first
7306 if (tree bind
= cluster
->slots
[jx
])
7307 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7312 maybe_add_fuzzy_binding (vec
, binding
, kind
);
7315 vec
.qsort ([] (const void *a_
, const void *b_
)
7317 return strcmp (IDENTIFIER_POINTER (*(const tree
*)a_
),
7318 IDENTIFIER_POINTER (*(const tree
*)b_
));
7321 /* Examine longest to shortest. */
7322 for (unsigned ix
= vec
.length (); ix
--;)
7324 const char *str
= IDENTIFIER_POINTER (vec
[ix
]);
7326 /* Ignore internal names with spaces in them. */
7327 if (strchr (str
, ' '))
7330 /* Don't suggest names that are reserved for use by the
7331 implementation, unless NAME began with an underscore. */
7332 if (!consider_implementation_names
7333 && name_reserved_for_implementation_p (str
))
7341 /* Subclass of deferred_diagnostic. Notify the user that the
7342 given macro was used before it was defined.
7343 This can be done in the C++ frontend since tokenization happens
7346 class macro_use_before_def
: public deferred_diagnostic
7349 /* Factory function. Return a new macro_use_before_def instance if
7350 appropriate, or return NULL. */
7351 static macro_use_before_def
*
7352 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
7354 location_t def_loc
= cpp_macro_definition_location (macro
);
7355 if (def_loc
== UNKNOWN_LOCATION
)
7358 /* We only want to issue a note if the macro was used *before* it was
7360 We don't want to issue a note for cases where a macro was incorrectly
7361 used, leaving it unexpanded (e.g. by using the wrong argument
7363 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
7366 return new macro_use_before_def (use_loc
, macro
);
7370 /* Ctor. LOC is the location of the usage. MACRO is the
7371 macro that was used. */
7372 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
7373 : deferred_diagnostic (loc
), m_macro (macro
)
7378 ~macro_use_before_def ()
7380 if (is_suppressed_p ())
7383 inform (get_location (), "the macro %qs had not yet been defined",
7384 (const char *)m_macro
->ident
.str
);
7385 inform (cpp_macro_definition_location (m_macro
),
7386 "it was later defined here");
7390 cpp_hashnode
*m_macro
;
7393 /* Determine if it can ever make sense to offer RID as a suggestion for
7396 Subroutine of lookup_name_fuzzy. */
7399 suggest_rid_p (enum rid rid
)
7403 /* Support suggesting function-like keywords. */
7404 case RID_STATIC_ASSERT
:
7408 /* Support suggesting the various decl-specifier words, to handle
7409 e.g. "singed" vs "signed" typos. */
7410 if (cp_keyword_starts_decl_specifier_p (rid
))
7413 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7414 and "do" for short misspellings, which are likely to lead to
7415 nonsensical results. */
7420 /* Search for near-matches for NAME within the current bindings, and within
7421 macro names, returning the best match as a const char *, or NULL if
7422 no reasonable match is found.
7424 Use LOC for any deferred diagnostics. */
7427 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
7429 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
7431 /* First, try some well-known names in the C++ standard library, in case
7432 the user forgot a #include. */
7433 const char *header_hint
7434 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
7436 return name_hint (NULL
,
7437 new suggest_missing_header (loc
,
7438 IDENTIFIER_POINTER (name
),
7441 best_match
<tree
, const char *> bm (name
);
7443 cp_binding_level
*lvl
;
7444 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
7445 consider_binding_level (name
, bm
, lvl
, true, kind
);
7447 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
7448 consider_binding_level (name
, bm
, lvl
, false, kind
);
7450 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7452 x = SOME_OTHER_MACRO (y);
7453 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7454 as a misspelled identifier.
7456 Use the best distance so far so that a candidate is only set if
7457 a macro is better than anything so far. This allows early rejection
7458 (without calculating the edit distance) of macro names that must have
7459 distance >= bm.get_best_distance (), and means that we only get a
7460 non-NULL result for best_macro_match if it's better than any of
7461 the identifiers already checked. */
7462 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
7463 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
7464 /* If a macro is the closest so far to NAME, consider it. */
7466 bm
.consider ((const char *)best_macro
->ident
.str
);
7467 else if (bmm
.get_best_distance () == 0)
7469 /* If we have an exact match for a macro name, then either the
7470 macro was used with the wrong argument count, or the macro
7471 has been used before it was defined. */
7472 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
7473 if (cpp_user_macro_p (macro
))
7474 return name_hint (NULL
,
7475 macro_use_before_def::maybe_make (loc
, macro
));
7478 /* Try the "starts_decl_specifier_p" keywords to detect
7479 "singed" vs "signed" typos. */
7480 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
7482 const c_common_resword
*resword
= &c_common_reswords
[i
];
7484 if (!suggest_rid_p (resword
->rid
))
7487 tree resword_identifier
= ridpointers
[resword
->rid
];
7488 if (!resword_identifier
)
7490 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
7492 /* Only consider reserved words that survived the
7493 filtering in init_reswords (e.g. for -std). */
7494 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
7497 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
7500 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7503 /* Subroutine of outer_binding.
7505 Returns TRUE if BINDING is a binding to a template parameter of
7506 SCOPE. In that case SCOPE is the scope of a primary template
7507 parameter -- in the sense of G++, i.e, a template that has its own
7510 Returns FALSE otherwise. */
7513 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
7514 cp_binding_level
*scope
)
7516 tree binding_value
, tmpl
, tinfo
;
7519 if (!binding
|| !scope
|| !scope
->this_entity
)
7522 binding_value
= binding
->value
? binding
->value
: binding
->type
;
7523 tinfo
= get_template_info (scope
->this_entity
);
7525 /* BINDING_VALUE must be a template parm. */
7526 if (binding_value
== NULL_TREE
7527 || (!DECL_P (binding_value
)
7528 || !DECL_TEMPLATE_PARM_P (binding_value
)))
7531 /* The level of BINDING_VALUE. */
7533 template_type_parameter_p (binding_value
)
7534 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7535 (TREE_TYPE (binding_value
)))
7536 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
7538 /* The template of the current scope, iff said scope is a primary
7541 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
7542 ? TI_TEMPLATE (tinfo
)
7545 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7546 then BINDING_VALUE is a parameter of TMPL. */
7547 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
7550 /* Return the innermost non-namespace binding for NAME from a scope
7551 containing BINDING, or, if BINDING is NULL, the current scope.
7552 Please note that for a given template, the template parameters are
7553 considered to be in the scope containing the current scope.
7554 If CLASS_P is false, then class bindings are ignored. */
7557 outer_binding (tree name
,
7558 cxx_binding
*binding
,
7562 cp_binding_level
*scope
;
7563 cp_binding_level
*outer_scope
;
7567 scope
= binding
->scope
->level_chain
;
7568 outer
= binding
->previous
;
7572 scope
= current_binding_level
;
7573 outer
= IDENTIFIER_BINDING (name
);
7575 outer_scope
= outer
? outer
->scope
: NULL
;
7577 /* Because we create class bindings lazily, we might be missing a
7578 class binding for NAME. If there are any class binding levels
7579 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7580 declared, we must lookup NAME in those class scopes. */
7582 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
7584 if (scope
->kind
== sk_class
)
7586 cxx_binding
*class_binding
;
7588 class_binding
= get_class_binding (name
, scope
);
7591 /* Thread this new class-scope binding onto the
7592 IDENTIFIER_BINDING list so that future lookups
7594 class_binding
->previous
= outer
;
7596 binding
->previous
= class_binding
;
7598 IDENTIFIER_BINDING (name
) = class_binding
;
7599 return class_binding
;
7602 /* If we are in a member template, the template parms of the member
7603 template are considered to be inside the scope of the containing
7604 class, but within G++ the class bindings are all pushed between the
7605 template parms and the function body. So if the outer binding is
7606 a template parm for the current scope, return it now rather than
7607 look for a class binding. */
7608 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
7609 && binding_to_template_parms_of_scope_p (outer
, scope
))
7612 scope
= scope
->level_chain
;
7618 /* Return the innermost block-scope or class-scope value binding for
7619 NAME, or NULL_TREE if there is no such binding. */
7622 innermost_non_namespace_value (tree name
)
7624 cxx_binding
*binding
;
7625 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
7626 return binding
? binding
->value
: NULL_TREE
;
7629 /* Look up NAME in the current binding level and its superiors in the
7630 namespace of variables, functions and typedefs. Return a ..._DECL
7631 node of some kind representing its definition if there is only one
7632 such declaration, or return a TREE_LIST with all the overloaded
7633 definitions if there are many, or return NULL_TREE if it is undefined.
7634 Hidden name, either friend declaration or built-in function, are
7637 WHERE controls which scopes are considered. It is a bit mask of
7638 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7639 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7640 scopes). It is an error for no bits to be set. These scopes are
7641 searched from innermost to outermost.
7643 WANT controls what kind of entity we'd happy with.
7644 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7645 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7646 for only NAMESPACE_DECLS. These two can be bit-ored to find
7649 WANT can also have LOOK_want::HIDDEN_FRIEND or
7650 LOOK_want::HIDDEN_LAMBDa added to it. */
7653 lookup_name (tree name
, LOOK_where where
, LOOK_want want
)
7655 tree val
= NULL_TREE
;
7657 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
7659 gcc_checking_assert (unsigned (where
) != 0);
7660 /* If we're looking for hidden lambda things, we shouldn't be
7661 looking in namespace scope. */
7662 gcc_checking_assert (!bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7663 || !bool (where
& LOOK_where::NAMESPACE
));
7664 query_oracle (name
);
7666 /* Conversion operators are handled specially because ordinary
7667 unqualified name lookup will not find template conversion
7669 if (IDENTIFIER_CONV_OP_P (name
))
7671 cp_binding_level
*level
;
7673 for (level
= current_binding_level
;
7674 level
&& level
->kind
!= sk_namespace
;
7675 level
= level
->level_chain
)
7680 /* A conversion operator can only be declared in a class
7682 if (level
->kind
!= sk_class
)
7685 /* Lookup the conversion operator in the class. */
7686 class_type
= level
->this_entity
;
7687 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0,
7688 tf_warning_or_error
);
7696 /* First, look in non-namespace scopes. */
7698 if (current_class_type
== NULL_TREE
)
7699 /* Maybe avoid searching the binding stack at all. */
7700 where
= LOOK_where (unsigned (where
) & ~unsigned (LOOK_where::CLASS
));
7702 if (bool (where
& (LOOK_where::BLOCK
| LOOK_where::CLASS
)))
7703 for (cxx_binding
*iter
= nullptr;
7704 (iter
= outer_binding (name
, iter
, bool (where
& LOOK_where::CLASS
)));)
7706 /* Skip entities we don't want. */
7707 if (!bool (where
& (LOCAL_BINDING_P (iter
)
7708 ? LOOK_where::BLOCK
: LOOK_where::CLASS
)))
7711 /* If this is the kind of thing we're looking for, we're done. */
7714 tree binding
= NULL_TREE
;
7716 if (!(!iter
->type
&& HIDDEN_TYPE_BINDING_P (iter
))
7717 && (bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7718 || !is_lambda_ignored_entity (iter
->value
))
7719 && qualify_lookup (iter
->value
, want
))
7720 binding
= iter
->value
;
7721 else if (bool (want
& LOOK_want::TYPE
)
7722 && !HIDDEN_TYPE_BINDING_P (iter
)
7724 binding
= iter
->type
;
7728 /* The saved lookups for an operator record 'nothing
7729 found' as error_mark_node. We need to stop the search
7730 here, but not return the error mark node. */
7731 if (binding
== error_mark_node
)
7732 binding
= NULL_TREE
;
7740 /* Now lookup in namespace scopes. */
7741 if (bool (where
& LOOK_where::NAMESPACE
))
7743 name_lookup
lookup (name
, want
);
7744 if (lookup
.search_unqualified
7745 (current_decl_namespace (), current_binding_level
))
7751 /* If we have a known type overload, pull it out. This can happen
7752 for both using decls and unhidden functions. */
7753 if (val
&& TREE_CODE (val
) == OVERLOAD
&& TREE_TYPE (val
) != unknown_type_node
)
7754 val
= OVL_FUNCTION (val
);
7760 lookup_name (tree name
)
7762 return lookup_name (name
, LOOK_where::ALL
, LOOK_want::NORMAL
);
7765 /* Look up NAME for type used in elaborated name specifier in
7766 the scopes given by HOW.
7768 Unlike lookup_name_1, we make sure that NAME is actually
7769 declared in the desired scope, not from inheritance, nor using
7770 directive. For using declaration, there is DR138 still waiting
7771 to be resolved. Hidden name coming from an earlier friend
7772 declaration is also returned, and will be made visible unless HOW
7773 is TAG_how::HIDDEN_FRIEND.
7775 A TYPE_DECL best matching the NAME is returned. Catching error
7776 and issuing diagnostics are caller's responsibility. */
7779 lookup_elaborated_type (tree name
, TAG_how how
)
7781 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
7783 cp_binding_level
*b
= current_binding_level
;
7785 if (b
->kind
!= sk_namespace
)
7786 /* Look in non-namespace scopes. */
7787 for (cxx_binding
*iter
= NULL
;
7788 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
7790 /* First check we're supposed to be looking in this scope --
7791 if we're not, we're done. */
7792 for (; b
!= iter
->scope
; b
= b
->level_chain
)
7793 if (!(b
->kind
== sk_cleanup
7794 || b
->kind
== sk_template_parms
7795 || b
->kind
== sk_function_parms
7796 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
7799 /* Check if this is the kind of thing we're looking for. If
7800 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
7801 come from base class. For ITER->VALUE, we can simply use
7802 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
7805 We check ITER->TYPE before ITER->VALUE in order to handle
7806 typedef struct C {} C;
7809 if (tree type
= iter
->type
)
7811 if (qualify_lookup (type
, LOOK_want::TYPE
)
7812 && (how
!= TAG_how::CURRENT_ONLY
7813 || LOCAL_BINDING_P (iter
)
7814 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
7816 if (how
!= TAG_how::HIDDEN_FRIEND
)
7817 /* It is no longer a hidden binding. */
7818 HIDDEN_TYPE_BINDING_P (iter
) = false;
7825 if (qualify_lookup (iter
->value
, LOOK_want::TYPE
)
7826 && (how
!= TAG_how::CURRENT_ONLY
7827 || !INHERITED_VALUE_BINDING_P (iter
)))
7829 if (how
!= TAG_how::HIDDEN_FRIEND
&& !iter
->type
)
7830 /* It is no longer a hidden binding. */
7831 HIDDEN_TYPE_BINDING_P (iter
) = false;
7838 /* Now check if we can look in namespace scope. */
7839 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
7840 if (!(b
->kind
== sk_cleanup
7841 || b
->kind
== sk_template_parms
7842 || b
->kind
== sk_function_parms
7843 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
7846 /* Look in the innermost namespace. */
7847 tree ns
= b
->this_entity
;
7848 if (tree
*slot
= find_namespace_slot (ns
, name
))
7851 if (TREE_CODE (bind
) == BINDING_VECTOR
)
7852 bind
= BINDING_VECTOR_CLUSTER (bind
, 0).slots
[BINDING_SLOT_CURRENT
];
7856 /* If this is the kind of thing we're looking for, we're done. */
7857 if (tree type
= MAYBE_STAT_TYPE (bind
))
7859 if (how
!= TAG_how::HIDDEN_FRIEND
)
7860 /* No longer hidden. */
7861 STAT_TYPE_HIDDEN_P (*slot
) = false;
7865 else if (tree decl
= MAYBE_STAT_DECL (bind
))
7867 if (qualify_lookup (decl
, LOOK_want::TYPE
))
7869 if (how
!= TAG_how::HIDDEN_FRIEND
&& STAT_HACK_P (bind
)
7870 && STAT_DECL_HIDDEN_P (bind
))
7872 if (STAT_TYPE (bind
))
7873 STAT_DECL_HIDDEN_P (bind
) = false;
7876 /* There is no type, just remove the stat
7881 BINDING_VECTOR_CLUSTER (*slot
, 0)
7882 .slots
[BINDING_SLOT_CURRENT
] = decl
;
7890 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
7892 /* We could be redeclaring a global module entity, (from GMF
7893 or header unit), or from another partition, or
7894 specializing an imported template. */
7895 bitmap imports
= get_import_bitmap ();
7896 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
7898 /* Scan the imported bindings. */
7899 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
7900 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
7906 /* Do this in forward order, so we load modules in an order
7907 the user expects. */
7908 for (; ix
--; cluster
++)
7909 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
7911 /* Are we importing this module? */
7912 if (unsigned base
= cluster
->indices
[jx
].base
)
7913 if (unsigned span
= cluster
->indices
[jx
].span
)
7915 if (bitmap_bit_p (imports
, base
))
7917 while (++base
, --span
);
7922 if (cluster
->slots
[jx
].is_lazy ())
7924 gcc_assert (cluster
->indices
[jx
].span
== 1);
7925 lazy_load_binding (cluster
->indices
[jx
].base
,
7926 ns
, name
, &cluster
->slots
[jx
]);
7928 tree bind
= cluster
->slots
[jx
];
7930 /* Load errors could mean there's nothing here. */
7933 /* Extract what we can see from here. If there's no
7934 stat_hack, then everything was exported. */
7935 tree type
= NULL_TREE
;
7937 /* If no stat hack, everything is visible. */
7938 if (STAT_HACK_P (bind
))
7940 if (STAT_TYPE_VISIBLE_P (bind
))
7941 type
= STAT_TYPE (bind
);
7942 bind
= STAT_VISIBLE (bind
);
7945 if (type
&& qualify_lookup (type
, LOOK_want::TYPE
))
7948 if (bind
&& qualify_lookup (bind
, LOOK_want::TYPE
))
7952 if (!module_purview_p ())
7954 /* We're in the global module, perhaps there's a tag
7956 // FIXME: This isn't quite right, if we find something
7957 // here, from the language PoV we're not supposed to
7966 /* The type TYPE is being declared. If it is a class template, or a
7967 specialization of a class template, do any processing required and
7968 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
7969 being declared a friend. B is the binding level at which this TYPE
7972 Returns the TYPE_DECL for TYPE, which may have been altered by this
7976 maybe_process_template_type_declaration (tree type
, int is_friend
,
7977 cp_binding_level
*b
)
7979 tree decl
= TYPE_NAME (type
);
7981 if (processing_template_parmlist
)
7982 /* You can't declare a new template type in a template parameter
7983 list. But, you can declare a non-template type:
7985 template <class A*> struct S;
7987 is a forward-declaration of `A'. */
7989 else if (b
->kind
== sk_namespace
7990 && current_binding_level
->kind
!= sk_namespace
)
7991 /* If this new type is being injected into a containing scope,
7992 then it's not a template type. */
7996 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
7997 || TREE_CODE (type
) == ENUMERAL_TYPE
);
7999 if (processing_template_decl
)
8001 decl
= push_template_decl (decl
, is_friend
);
8002 if (decl
== error_mark_node
)
8003 return error_mark_node
;
8005 /* If the current binding level is the binding level for the
8006 template parameters (see the comment in
8007 begin_template_parm_list) and the enclosing level is a class
8008 scope, and we're not looking at a friend, push the
8009 declaration of the member class into the class scope. In the
8010 friend case, push_template_decl will already have put the
8011 friend into global scope, if appropriate. */
8012 if (TREE_CODE (type
) != ENUMERAL_TYPE
8013 && !is_friend
&& b
->kind
== sk_template_parms
8014 && b
->level_chain
->kind
== sk_class
)
8016 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
8018 if (!COMPLETE_TYPE_P (current_class_type
))
8019 maybe_add_class_template_decl_list (current_class_type
,
8020 type
, /*friend_p=*/0);
8028 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8029 that the NAME is a class template, the tag is processed but not pushed.
8031 The pushed scope depend on the SCOPE parameter:
8032 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8034 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8035 non-template-parameter scope. This case is needed for forward
8037 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8038 TS_GLOBAL case except that names within template-parameter scopes
8039 are not pushed at all.
8041 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8044 pushtag (tree name
, tree type
, TAG_how how
)
8048 gcc_assert (identifier_p (name
));
8050 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8052 cp_binding_level
*b
= current_binding_level
;
8055 if (/* Cleanup scopes are not scopes from the point of view of
8057 b
->kind
== sk_cleanup
8058 /* Neither are function parameter scopes. */
8059 || b
->kind
== sk_function_parms
8060 /* Neither are the scopes used to hold template parameters
8061 for an explicit specialization. For an ordinary template
8062 declaration, these scopes are not scopes from the point of
8063 view of the language. */
8064 || (b
->kind
== sk_template_parms
8065 && (b
->explicit_spec_p
|| how
== TAG_how::GLOBAL
)))
8067 else if (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)
8070 if (b
->kind
== sk_template_parms
)
8077 /* Do C++ gratuitous typedefing. */
8078 if (REAL_IDENTIFIER_TYPE_VALUE (name
) != type
)
8081 tree context
= TYPE_CONTEXT (type
);
8085 cp_binding_level
*cb
= b
;
8086 while (cb
->kind
!= sk_namespace
8087 && cb
->kind
!= sk_class
8088 && (cb
->kind
!= sk_function_parms
8089 || !cb
->this_entity
))
8090 cb
= cb
->level_chain
;
8091 tree cs
= cb
->this_entity
;
8093 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
8094 ? cs
== current_function_decl
8095 : TYPE_P (cs
) ? cs
== current_class_type
8096 : cs
== current_namespace
);
8098 if (how
== TAG_how::CURRENT_ONLY
8099 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
8101 else if (cs
&& TYPE_P (cs
))
8102 /* When declaring a friend class of a local class, we want
8103 to inject the newly named class into the scope
8104 containing the local class, not the namespace
8106 context
= decl_function_context (get_type_decl (cs
));
8109 context
= current_namespace
;
8111 tdef
= create_implicit_typedef (name
, type
);
8112 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
8113 set_originating_module (tdef
);
8115 decl
= maybe_process_template_type_declaration
8116 (type
, how
== TAG_how::HIDDEN_FRIEND
, b
);
8117 if (decl
== error_mark_node
)
8120 if (b
->kind
== sk_class
)
8122 if (!TYPE_BEING_DEFINED (current_class_type
))
8123 /* Don't push anywhere if the class is complete; a lambda in an
8124 NSDMI is not a member of the class. */
8126 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8127 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8128 class. But if it's a member template class, we want
8129 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8131 finish_member_declaration (decl
);
8133 pushdecl_class_level (decl
);
8135 else if (b
->kind
== sk_template_parms
)
8137 /* Do not push the tag here -- we'll want to push the
8139 if (b
->level_chain
->kind
!= sk_class
)
8140 set_identifier_type_value_with_scope (name
, tdef
, b
->level_chain
);
8144 decl
= do_pushdecl_with_scope
8145 (decl
, b
, /*hiding=*/(how
== TAG_how::HIDDEN_FRIEND
));
8146 if (decl
== error_mark_node
)
8149 if (DECL_CONTEXT (decl
) == std_node
8150 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
8151 && !CLASSTYPE_TEMPLATE_INFO (type
))
8153 error ("declaration of %<std::initializer_list%> does not match "
8154 "%<#include <initializer_list>%>, isn%'t a template");
8155 return error_mark_node
;
8159 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
8161 /* If this is a local class, keep track of it. We need this
8162 information for name-mangling, and so that it is possible to
8163 find all function definitions in a translation unit in a
8164 convenient way. (It's otherwise tricky to find a member
8165 function definition it's only pointed to from within a local
8167 if (TYPE_FUNCTION_SCOPE_P (type
))
8169 if (processing_template_decl
)
8171 /* Push a DECL_EXPR so we call pushtag at the right time in
8172 template instantiation rather than in some nested context. */
8173 add_decl_expr (decl
);
8175 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8176 else if (!LAMBDA_TYPE_P (type
))
8177 determine_local_discriminator (TYPE_NAME (type
));
8181 if (b
->kind
== sk_class
8182 && !COMPLETE_TYPE_P (current_class_type
))
8183 maybe_add_class_template_decl_list (current_class_type
,
8184 type
, /*friend_p=*/0);
8186 decl
= TYPE_NAME (type
);
8187 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
8189 /* Set type visibility now if this is a forward declaration. */
8190 TREE_PUBLIC (decl
) = 1;
8191 determine_visibility (decl
);
8196 /* Subroutines for reverting temporarily to top-level for instantiation
8197 of templates and such. We actually need to clear out the class- and
8198 local-value slots of all identifiers, so that only the global values
8199 are at all visible. Simply setting current_binding_level to the global
8200 scope isn't enough, because more binding levels may be pushed. */
8201 struct saved_scope
*scope_chain
;
8203 /* Return true if ID has not already been marked. */
8206 store_binding_p (tree id
)
8208 if (!id
|| !IDENTIFIER_BINDING (id
))
8211 if (IDENTIFIER_MARKED (id
))
8217 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8218 have enough space reserved. */
8221 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8223 cxx_saved_binding saved
;
8225 gcc_checking_assert (store_binding_p (id
));
8227 IDENTIFIER_MARKED (id
) = 1;
8229 saved
.identifier
= id
;
8230 saved
.binding
= IDENTIFIER_BINDING (id
);
8231 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
8232 (*old_bindings
)->quick_push (saved
);
8233 IDENTIFIER_BINDING (id
) = NULL
;
8237 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8239 static vec
<tree
> bindings_need_stored
;
8243 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8244 for (t
= names
; t
; t
= TREE_CHAIN (t
))
8246 if (TREE_CODE (t
) == TREE_LIST
)
8247 id
= TREE_PURPOSE (t
);
8251 if (store_binding_p (id
))
8252 bindings_need_stored
.safe_push (id
);
8254 if (!bindings_need_stored
.is_empty ())
8256 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8257 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8259 /* We can apparently have duplicates in NAMES. */
8260 if (store_binding_p (id
))
8261 store_binding (id
, old_bindings
);
8263 bindings_need_stored
.truncate (0);
8267 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8268 objects, rather than a TREE_LIST. */
8271 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
8272 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8274 static vec
<tree
> bindings_need_stored
;
8276 cp_class_binding
*cb
;
8278 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
8279 if (store_binding_p (cb
->identifier
))
8280 bindings_need_stored
.safe_push (cb
->identifier
);
8281 if (!bindings_need_stored
.is_empty ())
8284 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8285 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8286 store_binding (id
, old_bindings
);
8287 bindings_need_stored
.truncate (0);
8291 /* A chain of saved_scope structures awaiting reuse. */
8293 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
8296 push_to_top_level (void)
8298 struct saved_scope
*s
;
8299 cp_binding_level
*b
;
8300 cxx_saved_binding
*sb
;
8304 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8306 /* Reuse or create a new structure for this saved scope. */
8307 if (free_saved_scope
!= NULL
)
8309 s
= free_saved_scope
;
8310 free_saved_scope
= s
->prev
;
8312 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
8313 memset (s
, 0, sizeof (*s
));
8314 /* Also reuse the structure's old_bindings vector. */
8315 vec_safe_truncate (old_bindings
, 0);
8316 s
->old_bindings
= old_bindings
;
8319 s
= ggc_cleared_alloc
<saved_scope
> ();
8321 b
= scope_chain
? current_binding_level
: 0;
8323 /* If we're in the middle of some function, save our state. */
8327 push_function_context ();
8332 if (scope_chain
&& previous_class_level
)
8333 store_class_bindings (previous_class_level
->class_shadowed
,
8336 /* Have to include the global scope, because class-scope decls
8337 aren't listed anywhere useful. */
8338 for (; b
; b
= b
->level_chain
)
8342 /* Template IDs are inserted into the global level. If they were
8343 inserted into namespace level, finish_file wouldn't find them
8344 when doing pending instantiations. Therefore, don't stop at
8345 namespace level, but continue until :: . */
8346 if (global_scope_p (b
))
8349 store_bindings (b
->names
, &s
->old_bindings
);
8350 /* We also need to check class_shadowed to save class-level type
8351 bindings, since pushclass doesn't fill in b->names. */
8352 if (b
->kind
== sk_class
)
8353 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
8355 /* Unwind type-value slots back to top level. */
8356 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
8357 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
8360 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
8361 IDENTIFIER_MARKED (sb
->identifier
) = 0;
8363 s
->prev
= scope_chain
;
8365 s
->need_pop_function_context
= need_pop
;
8366 s
->function_decl
= current_function_decl
;
8367 s
->unevaluated_operand
= cp_unevaluated_operand
;
8368 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8369 s
->suppress_location_wrappers
= suppress_location_wrappers
;
8370 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
8373 current_function_decl
= NULL_TREE
;
8374 current_lang_base
= NULL
;
8375 current_lang_name
= lang_name_cplusplus
;
8376 current_namespace
= global_namespace
;
8377 push_class_stack ();
8378 cp_unevaluated_operand
= 0;
8379 c_inhibit_evaluation_warnings
= 0;
8380 suppress_location_wrappers
= 0;
8384 pop_from_top_level (void)
8386 struct saved_scope
*s
= scope_chain
;
8387 cxx_saved_binding
*saved
;
8390 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8392 /* Clear out class-level bindings cache. */
8393 if (previous_class_level
)
8394 invalidate_class_lookup_cache ();
8397 release_tree_vector (current_lang_base
);
8399 scope_chain
= s
->prev
;
8400 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
8402 tree id
= saved
->identifier
;
8404 IDENTIFIER_BINDING (id
) = saved
->binding
;
8405 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
8408 /* If we were in the middle of compiling a function, restore our
8410 if (s
->need_pop_function_context
)
8411 pop_function_context ();
8412 current_function_decl
= s
->function_decl
;
8413 cp_unevaluated_operand
= s
->unevaluated_operand
;
8414 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
8415 suppress_location_wrappers
= s
->suppress_location_wrappers
;
8417 /* Make this saved_scope structure available for reuse by
8418 push_to_top_level. */
8419 s
->prev
= free_saved_scope
;
8420 free_saved_scope
= s
;
8423 /* Push into the scope of the namespace NS, even if it is deeply
8424 nested within another namespace. */
8427 push_nested_namespace (tree ns
)
8429 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8430 if (ns
== global_namespace
)
8431 push_to_top_level ();
8434 push_nested_namespace (CP_DECL_CONTEXT (ns
));
8435 resume_scope (NAMESPACE_LEVEL (ns
));
8436 current_namespace
= ns
;
8440 /* Pop back from the scope of the namespace NS, which was previously
8441 entered with push_nested_namespace. */
8444 pop_nested_namespace (tree ns
)
8446 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8447 while (ns
!= global_namespace
)
8449 ns
= CP_DECL_CONTEXT (ns
);
8450 current_namespace
= ns
;
8454 pop_from_top_level ();
8457 /* Add TARGET to USINGS, if it does not already exist there. We used
8458 to build the complete graph of usings at this point, from the POV
8459 of the source namespaces. Now we build that as we perform the
8460 unqualified search. */
8463 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
8466 for (unsigned ix
= usings
->length (); ix
--;)
8467 if ((*usings
)[ix
] == target
)
8470 vec_safe_push (usings
, target
);
8473 /* Tell the debug system of a using directive. */
8476 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
8478 /* Emit debugging info. */
8479 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
8480 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
8484 /* Process a using directive. */
8487 finish_using_directive (tree target
, tree attribs
)
8489 if (target
== error_mark_node
)
8492 if (current_binding_level
->kind
!= sk_namespace
)
8493 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
8495 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
8496 ORIGINAL_NAMESPACE (target
), false);
8498 add_using_namespace (current_binding_level
->using_directives
,
8499 ORIGINAL_NAMESPACE (target
));
8501 bool diagnosed
= false;
8502 if (attribs
!= error_mark_node
)
8503 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
8505 tree name
= get_attribute_name (a
);
8506 if (current_binding_level
->kind
== sk_namespace
8507 && is_attribute_p ("strong", name
))
8509 if (warning (0, "%<strong%> using directive no longer supported")
8510 && CP_DECL_CONTEXT (target
) == current_namespace
)
8511 inform (DECL_SOURCE_LOCATION (target
),
8512 "you can use an inline namespace instead");
8514 else if ((flag_openmp
|| flag_openmp_simd
)
8515 && get_attribute_namespace (a
) == omp_identifier
8516 && (is_attribute_p ("directive", name
)
8517 || is_attribute_p ("sequence", name
)))
8520 error ("%<omp::%E%> not allowed to be specified in this "
8525 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
8529 /* Pushes X into the global namespace. */
8532 pushdecl_top_level (tree x
)
8534 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8535 push_to_top_level ();
8536 gcc_checking_assert (!DECL_CONTEXT (x
));
8537 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8538 x
= pushdecl_namespace_level (x
);
8539 pop_from_top_level ();
8543 /* Pushes X into the global namespace and calls cp_finish_decl to
8544 register the variable, initializing it with INIT. */
8547 pushdecl_top_level_and_finish (tree x
, tree init
)
8549 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8550 push_to_top_level ();
8551 gcc_checking_assert (!DECL_CONTEXT (x
));
8552 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8553 x
= pushdecl_namespace_level (x
);
8554 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
8555 pop_from_top_level ();
8559 /* Enter the namespaces from current_namerspace to NS. */
8562 push_inline_namespaces (tree ns
)
8565 if (ns
!= current_namespace
)
8567 gcc_assert (ns
!= global_namespace
);
8568 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8569 resume_scope (NAMESPACE_LEVEL (ns
));
8570 current_namespace
= ns
;
8576 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8577 Reuse or create a namespace NAME. NAME is null for the anonymous
8581 reuse_namespace (tree
*slot
, tree ctx
, tree name
)
8583 if (modules_p () && *slot
&& TREE_PUBLIC (ctx
) && name
)
8585 /* Public namespace. Shared. */
8586 tree
*global_slot
= slot
;
8587 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
8588 global_slot
= get_fixed_binding_slot (slot
, name
,
8589 BINDING_SLOT_GLOBAL
, false);
8591 for (ovl_iterator
iter (*global_slot
); iter
; ++iter
)
8595 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
8603 make_namespace (tree ctx
, tree name
, location_t loc
, bool inline_p
)
8605 /* Create the namespace. */
8606 tree ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
8607 DECL_SOURCE_LOCATION (ns
) = loc
;
8608 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (ctx
) + 1;
8609 if (!SCOPE_DEPTH (ns
))
8610 /* We only allow depth 255. */
8611 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx
));
8612 DECL_CONTEXT (ns
) = FROB_CONTEXT (ctx
);
8615 /* Anon-namespaces in different header-unit imports are distinct.
8616 But that's ok as their contents all have internal linkage.
8617 (This is different to how they'd behave as textual includes,
8618 but doing this at all is really odd source.) */
8619 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
8620 else if (TREE_PUBLIC (ctx
))
8621 TREE_PUBLIC (ns
) = true;
8624 DECL_NAMESPACE_INLINE_P (ns
) = true;
8629 /* NS was newly created, finish off making it. */
8632 make_namespace_finish (tree ns
, tree
*slot
, bool from_import
= false)
8634 if (modules_p () && TREE_PUBLIC (ns
) && (from_import
|| *slot
!= ns
))
8636 /* Merge into global slot. */
8637 tree
*gslot
= get_fixed_binding_slot (slot
, DECL_NAME (ns
),
8638 BINDING_SLOT_GLOBAL
, true);
8642 tree ctx
= CP_DECL_CONTEXT (ns
);
8643 cp_binding_level
*scope
= ggc_cleared_alloc
<cp_binding_level
> ();
8644 scope
->this_entity
= ns
;
8645 scope
->more_cleanups_ok
= true;
8646 scope
->kind
= sk_namespace
;
8647 scope
->level_chain
= NAMESPACE_LEVEL (ctx
);
8648 NAMESPACE_LEVEL (ns
) = scope
;
8650 if (DECL_NAMESPACE_INLINE_P (ns
))
8651 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx
), ns
);
8653 if (DECL_NAMESPACE_INLINE_P (ns
) || !DECL_NAME (ns
))
8654 emit_debug_info_using_namespace (ctx
, ns
, true);
8657 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
8658 then we enter an anonymous namespace. If MAKE_INLINE is true, then
8659 we create an inline namespace (it is up to the caller to check upon
8660 redefinition). Return the number of namespaces entered. */
8663 push_namespace (tree name
, bool make_inline
)
8665 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8668 /* We should not get here if the global_namespace is not yet constructed
8669 nor if NAME designates the global namespace: The global scope is
8670 constructed elsewhere. */
8671 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
8673 tree ns
= NULL_TREE
;
8675 name_lookup
lookup (name
);
8676 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
8678 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
8680 /* An ambiguous lookup. If exactly one is a namespace, we
8681 want that. If more than one is a namespace, error, but
8682 pick one of them. */
8683 /* DR2061 can cause us to find multiple namespaces of the same
8684 name. We must treat that carefully and avoid thinking we
8685 need to push a new (possibly) duplicate namespace. Hey,
8686 if you want to use the same identifier within an inline
8687 nest, knock yourself out. */
8688 for (tree
*chain
= &lookup
.value
, next
; (next
= *chain
);)
8690 tree decl
= TREE_VALUE (next
);
8691 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
8695 else if (SCOPE_DEPTH (ns
) >= SCOPE_DEPTH (decl
))
8699 chain
= &TREE_CHAIN (next
);
8703 *chain
= TREE_CHAIN (next
);
8706 if (TREE_CHAIN (lookup
.value
))
8708 error ("%<namespace %E%> is ambiguous", name
);
8709 print_candidates (lookup
.value
);
8712 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
8716 if (tree dna
= DECL_NAMESPACE_ALIAS (ns
))
8718 /* A namespace alias is not allowed here, but if the alias
8719 is for a namespace also inside the current scope,
8720 accept it with a diagnostic. That's better than dying
8722 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
8724 error ("namespace alias %qD not allowed here, "
8725 "assuming %qD", ns
, dna
);
8735 /* DR2061. NS might be a member of an inline namespace. We
8736 need to push into those namespaces. */
8739 for (tree parent
, ctx
= ns
; ctx
!= current_namespace
;
8742 parent
= CP_DECL_CONTEXT (ctx
);
8744 tree bind
= *find_namespace_slot (parent
, DECL_NAME (ctx
), false);
8747 auto &cluster
= BINDING_VECTOR_CLUSTER (bind
, 0);
8748 binding_slot
&slot
= cluster
.slots
[BINDING_SLOT_CURRENT
];
8749 gcc_checking_assert (!(tree
)slot
|| (tree
)slot
== ctx
);
8755 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8756 if (DECL_SOURCE_LOCATION (ns
) == BUILTINS_LOCATION
)
8757 /* It's not builtin now. */
8758 DECL_SOURCE_LOCATION (ns
) = input_location
;
8762 /* Before making a new namespace, see if we already have one in
8763 the existing partitions of the current namespace. */
8764 tree
*slot
= find_namespace_slot (current_namespace
, name
, false);
8766 ns
= reuse_namespace (slot
, current_namespace
, name
);
8768 ns
= make_namespace (current_namespace
, name
,
8769 input_location
, make_inline
);
8771 if (pushdecl (ns
) == error_mark_node
)
8775 /* Finish up making the namespace. */
8776 add_decl_to_level (NAMESPACE_LEVEL (current_namespace
), ns
);
8779 slot
= find_namespace_slot (current_namespace
, name
);
8780 /* This should find the slot created by pushdecl. */
8781 gcc_checking_assert (slot
&& *slot
== ns
);
8783 make_namespace_finish (ns
, slot
);
8785 /* Add the anon using-directive here, we don't do it in
8786 make_namespace_finish. */
8787 if (!DECL_NAMESPACE_INLINE_P (ns
) && !name
)
8788 add_using_namespace (current_binding_level
->using_directives
, ns
);
8794 /* A public namespace is exported only if explicitly marked, or
8795 it contains exported entities. */
8796 if (TREE_PUBLIC (ns
) && module_exporting_p ())
8797 DECL_MODULE_EXPORT_P (ns
) = true;
8798 if (module_purview_p ())
8799 DECL_MODULE_PURVIEW_P (ns
) = true;
8801 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
8803 error_at (input_location
,
8804 "inline namespace must be specified at initial definition");
8805 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
8807 resume_scope (NAMESPACE_LEVEL (ns
));
8808 current_namespace
= ns
;
8815 /* Pop from the scope of the current namespace. */
8818 pop_namespace (void)
8820 auto_cond_timevar
tv (TV_NAME_LOOKUP
);
8822 gcc_assert (current_namespace
!= global_namespace
);
8823 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
8824 /* The binding level is not popped, as it might be re-opened later. */
8828 /* An IMPORT is an import that is defining namespace NAME inside CTX. Find or
8829 create that namespace and add it to the container's binding-vector. */
8832 add_imported_namespace (tree ctx
, tree name
, location_t loc
, unsigned import
,
8833 bool inline_p
, bool visible_p
)
8835 // FIXME: Something is not correct about the VISIBLE_P handling. We
8836 // need to insert this namespace into
8837 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
8838 // (b) The importing module's slot (always)
8839 // (c) Do we need to put it in the CURRENT slot? This is the
8842 tree
*slot
= find_namespace_slot (ctx
, name
, true);
8843 tree decl
= reuse_namespace (slot
, ctx
, name
);
8845 /* Creating and binding. */
8848 decl
= make_namespace (ctx
, name
, loc
, inline_p
);
8849 DECL_MODULE_IMPORT_P (decl
) = true;
8850 make_namespace_finish (decl
, slot
, true);
8852 else if (DECL_NAMESPACE_INLINE_P (decl
) != inline_p
)
8854 error_at (loc
, "%s namespace %qD conflicts with reachable definition",
8855 inline_p
? "inline" : "non-inline", decl
);
8856 inform (DECL_SOURCE_LOCATION (decl
), "reachable %s definition here",
8857 inline_p
? "non-inline" : "inline");
8860 if (TREE_PUBLIC (decl
) && TREE_CODE (*slot
) == BINDING_VECTOR
)
8862 /* See if we can extend the final slot. */
8863 binding_cluster
*last
= BINDING_VECTOR_CLUSTER_LAST (*slot
);
8864 gcc_checking_assert (last
->indices
[0].span
);
8865 unsigned jx
= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
8868 if (last
->indices
[jx
].span
)
8870 tree final
= last
->slots
[jx
];
8871 if (visible_p
== !STAT_HACK_P (final
)
8872 && MAYBE_STAT_DECL (final
) == decl
8873 && last
->indices
[jx
].base
+ last
->indices
[jx
].span
== import
8874 && (BINDING_VECTOR_NUM_CLUSTERS (*slot
) > 1
8875 || (BINDING_VECTOR_SLOTS_PER_CLUSTER
> BINDING_SLOTS_FIXED
8876 && jx
>= BINDING_SLOTS_FIXED
)))
8878 last
->indices
[jx
].span
++;
8883 /* Append a new slot. */
8884 tree
*mslot
= &(tree
&)*append_imported_binding_slot (slot
, name
, import
);
8886 gcc_assert (!*mslot
);
8887 *mslot
= visible_p
? decl
: stat_hack (decl
, NULL_TREE
);
8892 /* Pop off extraneous binding levels left over due to syntax errors.
8893 We don't pop past namespaces, as they might be valid. */
8896 pop_everything (void)
8898 if (ENABLE_SCOPE_CHECKING
)
8899 verbatim ("XXX entering %<pop_everything ()%>");
8900 while (!namespace_bindings_p ())
8902 if (current_binding_level
->kind
== sk_class
)
8903 pop_nested_class ();
8907 if (ENABLE_SCOPE_CHECKING
)
8908 verbatim ("XXX leaving %<pop_everything ()%>");
8911 /* Emit debugging information for using declarations and directives.
8912 If input tree is overloaded fn then emit debug info for all
8916 cp_emit_debug_info_for_using (tree t
, tree context
)
8918 /* Don't try to emit any debug information if we have errors. */
8922 /* Do not supply context to imported_module_or_decl, if
8923 it is a global namespace. */
8924 if (context
== global_namespace
)
8925 context
= NULL_TREE
;
8927 t
= MAYBE_BASELINK_FUNCTIONS (t
);
8929 for (lkp_iterator
iter (t
); iter
; ++iter
)
8933 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
8934 /* FIXME: Handle TEMPLATE_DECLs. */
8937 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
8938 of a builtin function. */
8939 if (TREE_CODE (fn
) == FUNCTION_DECL
8940 && DECL_EXTERNAL (fn
)
8941 && fndecl_built_in_p (fn
))
8944 if (building_stmt_list_p ())
8945 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
8947 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
8952 /* Return the result of unqualified lookup for the overloaded operator
8953 designated by CODE, if we are in a template and the binding we find is
8957 op_unqualified_lookup (tree fnname
)
8959 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
8961 cp_binding_level
*l
= binding
->scope
;
8962 while (l
&& !l
->this_entity
)
8965 if (l
&& uses_template_parms (l
->this_entity
))
8966 /* Don't preserve decls from an uninstantiated template,
8967 wait until that template is instantiated. */
8971 tree fns
= lookup_name (fnname
);
8973 /* Remember we found nothing! */
8974 return error_mark_node
;
8977 if (TREE_CODE (d
) == TREE_LIST
)
8979 if (is_overloaded_fn (d
))
8980 d
= get_first_fn (d
);
8981 if (DECL_CLASS_SCOPE_P (d
))
8982 /* We don't need to remember class-scope functions or declarations,
8983 normal unqualified lookup will find them again. */
8989 /* E is an expression representing an operation with dependent type, so we
8990 don't know yet whether it will use the built-in meaning of the operator or a
8991 function. Remember declarations of that operator in scope.
8993 We then inject a fake binding of that lookup into the
8994 instantiation's parameter scope. This approach fails if the user
8995 has different using declarations or directives in different local
8996 binding of the current function from whence we need to do lookups
8997 (we'll cache what we see on the first lookup). */
8999 static const char *const op_bind_attrname
= "operator bindings";
9002 maybe_save_operator_binding (tree e
)
9004 /* This is only useful in a template. */
9005 if (!processing_template_decl
)
9008 tree cfn
= current_function_decl
;
9013 if(TREE_CODE (e
) == MODOP_EXPR
)
9014 fnname
= ovl_op_identifier (true, TREE_CODE (TREE_OPERAND (e
, 1)));
9016 fnname
= ovl_op_identifier (false, TREE_CODE (e
));
9017 if (!fnname
|| fnname
== assign_op_identifier
)
9020 tree attributes
= DECL_ATTRIBUTES (cfn
);
9021 tree op_attr
= lookup_attribute (op_bind_attrname
, attributes
);
9024 tree
*ap
= &DECL_ATTRIBUTES (cfn
);
9025 while (*ap
&& ATTR_IS_DEPENDENT (*ap
))
9026 ap
= &TREE_CHAIN (*ap
);
9027 op_attr
= tree_cons (get_identifier (op_bind_attrname
),
9032 tree op_bind
= purpose_member (fnname
, TREE_VALUE (op_attr
));
9035 tree fns
= op_unqualified_lookup (fnname
);
9037 /* Always record, so we don't keep looking for this
9039 TREE_VALUE (op_attr
) = tree_cons (fnname
, fns
, TREE_VALUE (op_attr
));
9043 /* Called from cp_free_lang_data so we don't put this into LTO. */
9046 discard_operator_bindings (tree decl
)
9048 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
9049 DECL_ATTRIBUTES (decl
));
9052 /* Subroutine of start_preparsed_function: push the bindings we saved away in
9053 maybe_save_op_lookup into the function parameter binding level. */
9056 push_operator_bindings ()
9058 tree decl1
= current_function_decl
;
9059 if (tree attr
= lookup_attribute (op_bind_attrname
,
9060 DECL_ATTRIBUTES (decl1
)))
9061 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
9062 if (tree val
= TREE_VALUE (binds
))
9064 tree name
= TREE_PURPOSE (binds
);
9065 if (TREE_CODE (val
) == TREE_LIST
)
9066 for (tree v
= val
; v
; v
= TREE_CHAIN (v
))
9067 push_local_binding (name
, TREE_VALUE (v
), /*using*/true);
9069 push_local_binding (name
, val
, /*using*/true);
9073 #include "gt-cp-name-lookup.h"