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 tree
do_pushdecl (tree decl
, bool hiding
);
43 static void set_identifier_type_value_with_scope (tree id
, tree decl
,
45 static name_hint
maybe_suggest_missing_std_header (location_t location
,
47 static name_hint
suggest_alternatives_for_1 (location_t location
, tree name
,
48 bool suggest_misspellings
);
50 /* Slots in BINDING_VECTOR. */
53 BINDING_SLOT_CURRENT
, /* Slot for current TU. */
54 BINDING_SLOT_GLOBAL
, /* Slot for merged global module. */
55 BINDING_SLOT_PARTITION
, /* Slot for merged partition entities
58 /* Number of always-allocated slots. */
59 BINDING_SLOTS_FIXED
= BINDING_SLOT_GLOBAL
+ 1
62 /* Create an overload suitable for recording an artificial TYPE_DECL
63 and another decl. We use this machanism to implement the struct
66 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N))
67 #define STAT_TYPE_VISIBLE_P(N) TREE_USED (OVERLOAD_CHECK (N))
68 #define STAT_TYPE(N) TREE_TYPE (N)
69 #define STAT_DECL(N) OVL_FUNCTION (N)
70 #define STAT_VISIBLE(N) OVL_CHAIN (N)
71 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N)
72 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE)
74 /* When a STAT_HACK_P is true, OVL_USING_P and OVL_EXPORT_P are valid
75 and apply to the hacked type. */
77 /* For regular (maybe) overloaded functions, we have OVL_HIDDEN_P.
78 But we also need to indicate hiddenness on implicit type decls
79 (injected friend classes), and (coming soon) decls injected from
80 block-scope externs. It is too awkward to press the existing
81 overload marking for that. If we have a hidden non-function, we
82 always create a STAT_HACK, and use these two markers as needed. */
83 #define STAT_TYPE_HIDDEN_P(N) OVL_HIDDEN_P (N)
84 #define STAT_DECL_HIDDEN_P(N) OVL_DEDUP_P (N)
86 /* Create a STAT_HACK node with DECL as the value binding and TYPE as
90 stat_hack (tree decl
= NULL_TREE
, tree type
= NULL_TREE
)
92 tree result
= make_node (OVERLOAD
);
94 /* Mark this as a lookup, so we can tell this is a stat hack. */
95 OVL_LOOKUP_P (result
) = true;
96 STAT_DECL (result
) = decl
;
97 STAT_TYPE (result
) = type
;
101 /* Create a local binding level for NAME. */
104 create_local_binding (cp_binding_level
*level
, tree name
)
106 cxx_binding
*binding
= cxx_binding_make (NULL
, NULL
);
108 LOCAL_BINDING_P (binding
) = true;
109 binding
->scope
= level
;
110 binding
->previous
= IDENTIFIER_BINDING (name
);
112 IDENTIFIER_BINDING (name
) = binding
;
117 /* Find the binding for NAME in namespace NS. If CREATE_P is true,
118 make an empty binding if there wasn't one. */
121 find_namespace_slot (tree ns
, tree name
, bool create_p
= false)
123 tree
*slot
= DECL_NAMESPACE_BINDINGS (ns
)
124 ->find_slot_with_hash (name
, name
? IDENTIFIER_HASH_VALUE (name
) : 0,
125 create_p
? INSERT
: NO_INSERT
);
130 find_namespace_value (tree ns
, tree name
)
132 tree
*b
= find_namespace_slot (ns
, name
);
134 return b
? MAYBE_STAT_DECL (*b
) : NULL_TREE
;
137 /* Look in *SLOT for a the binding of NAME in imported module IX.
138 Returns pointer to binding's slot, or NULL if not found. Does a
139 binary search, as this is mainly used for random access during
140 importing. Do not use for the fixed slots. */
142 static binding_slot
*
143 search_imported_binding_slot (tree
*slot
, unsigned ix
)
150 if (TREE_CODE (*slot
) != BINDING_VECTOR
)
153 unsigned clusters
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
154 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
156 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
164 unsigned half
= clusters
/ 2;
165 gcc_checking_assert (cluster
[half
].indices
[0].span
);
166 if (cluster
[half
].indices
[0].base
> ix
)
176 /* Is it in this cluster? */
177 for (unsigned off
= 0; off
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; off
++)
179 if (!cluster
->indices
[off
].span
)
181 if (cluster
->indices
[off
].base
> ix
)
184 if (cluster
->indices
[off
].base
+ cluster
->indices
[off
].span
> ix
)
185 return &cluster
->slots
[off
];
192 init_global_partition (binding_cluster
*cluster
, tree decl
)
196 if (header_module_p ())
198 else if (TREE_PUBLIC (decl
)
199 && TREE_CODE (decl
) == NAMESPACE_DECL
200 && !DECL_NAMESPACE_ALIAS (decl
))
202 else if (!get_originating_module (decl
))
207 mslot
= &cluster
[0].slots
[BINDING_SLOT_GLOBAL
];
209 mslot
= &cluster
[BINDING_SLOT_PARTITION
210 / BINDING_VECTOR_SLOTS_PER_CLUSTER
]
211 .slots
[BINDING_SLOT_PARTITION
212 % BINDING_VECTOR_SLOTS_PER_CLUSTER
];
215 decl
= ovl_make (decl
, *mslot
);
218 if (TREE_CODE (decl
) == CONST_DECL
)
220 tree type
= TREE_TYPE (decl
);
221 if (TREE_CODE (type
) == ENUMERAL_TYPE
222 && IDENTIFIER_ANON_P (DECL_NAME (TYPE_NAME (type
)))
223 && decl
== TREE_VALUE (TYPE_VALUES (type
)))
224 /* Anonymous enums are keyed by their first enumerator, put
225 the TYPE_DECL here too. */
226 *mslot
= ovl_make (TYPE_NAME (type
), *mslot
);
230 /* Get the fixed binding slot IX. Creating the vector if CREATE is
231 non-zero. If CREATE is < 0, make sure there is at least 1 spare
232 slot for an import. (It is an error for CREATE < 0 and the slot to
236 get_fixed_binding_slot (tree
*slot
, tree name
, unsigned ix
, int create
)
238 gcc_checking_assert (ix
<= BINDING_SLOT_PARTITION
);
240 /* An assumption is that the fixed slots all reside in one cluster. */
241 gcc_checking_assert (BINDING_VECTOR_SLOTS_PER_CLUSTER
>= BINDING_SLOTS_FIXED
);
243 if (!*slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
)
245 if (ix
== BINDING_SLOT_CURRENT
)
246 /* The current TU can just use slot directly. */
252 /* The partition slot is only needed when we know we're a named
254 bool partition_slot
= named_module_p ();
255 unsigned want
= ((BINDING_SLOTS_FIXED
+ partition_slot
+ (create
< 0)
256 + BINDING_VECTOR_SLOTS_PER_CLUSTER
- 1)
257 / BINDING_VECTOR_SLOTS_PER_CLUSTER
);
258 tree new_vec
= make_binding_vec (name
, want
);
259 BINDING_VECTOR_NUM_CLUSTERS (new_vec
) = want
;
260 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (new_vec
);
262 /* Initialize the fixed slots. */
263 for (unsigned jx
= BINDING_SLOTS_FIXED
; jx
--;)
265 cluster
[0].indices
[jx
].base
= 0;
266 cluster
[0].indices
[jx
].span
= 1;
267 cluster
[0].slots
[jx
] = NULL_TREE
;
272 unsigned off
= BINDING_SLOT_PARTITION
% BINDING_VECTOR_SLOTS_PER_CLUSTER
;
273 unsigned ind
= BINDING_SLOT_PARTITION
/ BINDING_VECTOR_SLOTS_PER_CLUSTER
;
274 cluster
[ind
].indices
[off
].base
= 0;
275 cluster
[ind
].indices
[off
].span
= 1;
276 cluster
[ind
].slots
[off
] = NULL_TREE
;
279 if (tree orig
= *slot
)
281 /* Propagate existing value to current slot. */
283 /* Propagate global & module entities to the global and
285 if (tree type
= MAYBE_STAT_TYPE (orig
))
286 init_global_partition (cluster
, type
);
288 for (ovl_iterator
iter (MAYBE_STAT_DECL (orig
)); iter
; ++iter
)
292 /* Internal linkage entities are in deduplicateable. */
293 init_global_partition (cluster
, decl
);
296 if (cluster
[0].slots
[BINDING_SLOT_GLOBAL
]
297 && !(TREE_CODE (orig
) == NAMESPACE_DECL
298 && !DECL_NAMESPACE_ALIAS (orig
)))
300 /* Note that we had some GMF entries. */
301 if (!STAT_HACK_P (orig
))
302 orig
= stat_hack (orig
);
304 MODULE_BINDING_GLOBAL_P (orig
) = true;
307 cluster
[0].slots
[BINDING_SLOT_CURRENT
] = orig
;
313 gcc_checking_assert (create
>= 0);
315 unsigned off
= ix
% BINDING_VECTOR_SLOTS_PER_CLUSTER
;
316 binding_cluster
&cluster
317 = BINDING_VECTOR_CLUSTER (*slot
, ix
/ BINDING_VECTOR_SLOTS_PER_CLUSTER
);
319 /* There must always be slots for these indices */
320 gcc_checking_assert (cluster
.indices
[off
].span
== 1
321 && !cluster
.indices
[off
].base
322 && !cluster
.slots
[off
].is_lazy ());
324 return reinterpret_cast<tree
*> (&cluster
.slots
[off
]);
327 /* *SLOT is a namespace binding slot. Append a slot for imported
330 static binding_slot
*
331 append_imported_binding_slot (tree
*slot
, tree name
, unsigned ix
)
333 gcc_checking_assert (ix
);
335 if (!*slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
)
336 /* Make an initial module vector. */
337 get_fixed_binding_slot (slot
, name
, BINDING_SLOT_GLOBAL
, -1);
338 else if (!BINDING_VECTOR_CLUSTER_LAST (*slot
)
339 ->indices
[BINDING_VECTOR_SLOTS_PER_CLUSTER
- 1].span
)
340 /* There is space in the last cluster. */;
341 else if (BINDING_VECTOR_NUM_CLUSTERS (*slot
)
342 != BINDING_VECTOR_ALLOC_CLUSTERS (*slot
))
343 /* There is space in the vector. */
344 BINDING_VECTOR_NUM_CLUSTERS (*slot
)++;
347 /* Extend the vector. */
348 unsigned have
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
349 unsigned want
= (have
* 3 + 1) / 2;
351 if (want
> (unsigned short)~0)
352 want
= (unsigned short)~0;
354 tree new_vec
= make_binding_vec (name
, want
);
355 BINDING_VECTOR_NUM_CLUSTERS (new_vec
) = have
+ 1;
356 memcpy (BINDING_VECTOR_CLUSTER_BASE (new_vec
),
357 BINDING_VECTOR_CLUSTER_BASE (*slot
),
358 have
* sizeof (binding_cluster
));
362 binding_cluster
*last
= BINDING_VECTOR_CLUSTER_LAST (*slot
);
363 for (unsigned off
= 0; off
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; off
++)
364 if (!last
->indices
[off
].span
)
366 /* Fill the free slot of the cluster. */
367 last
->indices
[off
].base
= ix
;
368 last
->indices
[off
].span
= 1;
369 last
->slots
[off
] = NULL_TREE
;
370 return &last
->slots
[off
];
376 /* Add DECL to the list of things declared in binding level B. */
379 add_decl_to_level (cp_binding_level
*b
, tree decl
)
381 gcc_assert (b
->kind
!= sk_class
);
383 /* Make sure we don't create a circular list. xref_tag can end
384 up pushing the same artificial decl more than once. We
385 should have already detected that in update_binding. */
386 gcc_assert (b
->names
!= decl
);
388 /* We build up the list in reverse order, and reverse it later if
390 TREE_CHAIN (decl
) = b
->names
;
393 /* If appropriate, add decl to separate list of statics. We include
394 extern variables because they might turn out to be static later.
395 It's OK for this list to contain a few false positives. */
396 if (b
->kind
== sk_namespace
397 && ((VAR_P (decl
) && (TREE_STATIC (decl
) || DECL_EXTERNAL (decl
)))
398 || (TREE_CODE (decl
) == FUNCTION_DECL
399 && (!TREE_PUBLIC (decl
)
400 || decl_anon_ns_mem_p (decl
)
401 || DECL_DECLARED_INLINE_P (decl
)))))
402 vec_safe_push (static_decls
, decl
);
405 /* Find the binding for NAME in the local binding level B. */
408 find_local_binding (cp_binding_level
*b
, tree name
)
410 if (cxx_binding
*binding
= IDENTIFIER_BINDING (name
))
411 for (;; b
= b
->level_chain
)
413 if (binding
->scope
== b
)
416 /* Cleanup contours are transparent to the language. */
417 if (b
->kind
!= sk_cleanup
)
426 typedef std::pair
<tree
, tree
> using_pair
;
427 typedef vec
<using_pair
, va_heap
, vl_embed
> using_queue
;
430 tree name
; /* The identifier being looked for. */
432 /* Usually we just add things to the VALUE binding, but we record
433 (hidden) IMPLICIT_TYPEDEFs on the type binding, which is used for
434 using-decl resolution. */
435 tree value
; /* A (possibly ambiguous) set of things found. */
436 tree type
; /* A type that has been found. */
438 LOOK_want want
; /* What kind of entity we want. */
440 bool deduping
; /* Full deduping is needed because using declarations
442 vec
<tree
, va_heap
, vl_embed
> *scopes
;
443 name_lookup
*previous
; /* Previously active lookup. */
446 /* Marked scope stack for outermost name lookup. */
447 static vec
<tree
, va_heap
, vl_embed
> *shared_scopes
;
448 /* Currently active lookup. */
449 static name_lookup
*active
;
452 name_lookup (tree n
, LOOK_want w
= LOOK_want::NORMAL
)
453 : name (n
), value (NULL_TREE
), type (NULL_TREE
),
455 deduping (false), scopes (NULL
), previous (NULL
)
464 private: /* Uncopyable, unmovable, unassignable. I am a rock. */
465 name_lookup (const name_lookup
&);
466 name_lookup
&operator= (const name_lookup
&);
469 static bool seen_p (tree scope
)
471 return LOOKUP_SEEN_P (scope
);
473 static bool found_p (tree scope
)
475 return LOOKUP_FOUND_P (scope
);
478 void mark_seen (tree scope
); /* Mark and add to scope vector. */
479 static void mark_found (tree scope
)
481 gcc_checking_assert (seen_p (scope
));
482 LOOKUP_FOUND_P (scope
) = true;
484 bool see_and_mark (tree scope
)
486 bool ret
= seen_p (scope
);
491 bool find_and_mark (tree scope
);
494 void preserve_state ();
495 void restore_state ();
498 static tree
ambiguous (tree thing
, tree current
);
499 void add_overload (tree fns
);
500 void add_value (tree new_val
);
501 void add_type (tree new_type
);
502 bool process_binding (tree val_bind
, tree type_bind
);
503 unsigned process_module_binding (tree val_bind
, tree type_bind
, unsigned);
504 /* Look in only namespace. */
505 bool search_namespace_only (tree scope
);
506 /* Look in namespace and its (recursive) inlines. Ignore using
507 directives. Return true if something found (inc dups). */
508 bool search_namespace (tree scope
);
509 /* Look in the using directives of namespace + inlines using
510 qualified lookup rules. */
511 bool search_usings (tree scope
);
514 using_queue
*queue_namespace (using_queue
*queue
, int depth
, tree scope
);
515 using_queue
*do_queue_usings (using_queue
*queue
, int depth
,
516 vec
<tree
, va_gc
> *usings
);
517 using_queue
*queue_usings (using_queue
*queue
, int depth
,
518 vec
<tree
, va_gc
> *usings
)
521 queue
= do_queue_usings (queue
, depth
, usings
);
529 void adl_expr (tree
);
530 void adl_type (tree
);
531 void adl_template_arg (tree
);
532 void adl_class (tree
);
533 void adl_enum (tree
);
534 void adl_bases (tree
);
535 void adl_class_only (tree
);
536 void adl_namespace (tree
);
537 void adl_class_fns (tree
);
538 void adl_namespace_fns (tree
, bitmap
);
541 /* Search namespace + inlines + maybe usings as qualified lookup. */
542 bool search_qualified (tree scope
, bool usings
= true);
544 /* Search namespace + inlines + usings as unqualified lookup. */
545 bool search_unqualified (tree scope
, cp_binding_level
*);
547 /* ADL lookup of ARGS. */
548 tree
search_adl (tree fns
, vec
<tree
, va_gc
> *args
);
551 /* Scope stack shared by all outermost lookups. This avoids us
552 allocating and freeing on every single lookup. */
553 vec
<tree
, va_heap
, vl_embed
> *name_lookup::shared_scopes
;
555 /* Currently active lookup. */
556 name_lookup
*name_lookup::active
;
558 /* Name lookup is recursive, becase ADL can cause template
559 instatiation. This is of course a rare event, so we optimize for
560 it not happening. When we discover an active name-lookup, which
561 must be an ADL lookup, we need to unmark the marked scopes and also
562 unmark the lookup we might have been accumulating. */
565 name_lookup::preserve_state ()
570 unsigned length
= vec_safe_length (previous
->scopes
);
571 vec_safe_reserve (previous
->scopes
, length
* 2);
572 for (unsigned ix
= length
; ix
--;)
574 tree decl
= (*previous
->scopes
)[ix
];
576 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
577 LOOKUP_SEEN_P (decl
) = false;
579 /* Preserve the FOUND_P state on the interrupted lookup's
581 if (LOOKUP_FOUND_P (decl
))
583 LOOKUP_FOUND_P (decl
) = false;
584 previous
->scopes
->quick_push (decl
);
588 /* Unmark the outer partial lookup. */
589 if (previous
->deduping
)
590 lookup_mark (previous
->value
, false);
593 scopes
= shared_scopes
;
597 /* Restore the marking state of a lookup we interrupted. */
600 name_lookup::restore_state ()
603 lookup_mark (value
, false);
605 /* Unmark and empty this lookup's scope stack. */
606 for (unsigned ix
= vec_safe_length (scopes
); ix
--;)
608 tree decl
= scopes
->pop ();
609 gcc_checking_assert (LOOKUP_SEEN_P (decl
));
610 LOOKUP_SEEN_P (decl
) = false;
611 LOOKUP_FOUND_P (decl
) = false;
619 unsigned length
= vec_safe_length (previous
->scopes
);
620 for (unsigned ix
= 0; ix
!= length
; ix
++)
622 tree decl
= (*previous
->scopes
)[ix
];
623 if (LOOKUP_SEEN_P (decl
))
625 /* The remainder of the scope stack must be recording
626 FOUND_P decls, which we want to pop off. */
629 tree decl
= previous
->scopes
->pop ();
630 gcc_checking_assert (LOOKUP_SEEN_P (decl
)
631 && !LOOKUP_FOUND_P (decl
));
632 LOOKUP_FOUND_P (decl
) = true;
634 while (++ix
!= length
);
638 gcc_checking_assert (!LOOKUP_FOUND_P (decl
));
639 LOOKUP_SEEN_P (decl
) = true;
642 /* Remark the outer partial lookup. */
643 if (previous
->deduping
)
644 lookup_mark (previous
->value
, true);
647 shared_scopes
= scopes
;
651 name_lookup::mark_seen (tree scope
)
653 gcc_checking_assert (!seen_p (scope
));
654 LOOKUP_SEEN_P (scope
) = true;
655 vec_safe_push (scopes
, scope
);
659 name_lookup::find_and_mark (tree scope
)
661 bool result
= LOOKUP_FOUND_P (scope
);
664 LOOKUP_FOUND_P (scope
) = true;
665 if (!LOOKUP_SEEN_P (scope
))
666 vec_safe_push (scopes
, scope
);
672 /* THING and CURRENT are ambiguous, concatenate them. */
675 name_lookup::ambiguous (tree thing
, tree current
)
677 if (TREE_CODE (current
) != TREE_LIST
)
679 current
= build_tree_list (NULL_TREE
, current
);
680 TREE_TYPE (current
) = error_mark_node
;
682 current
= tree_cons (NULL_TREE
, thing
, current
);
683 TREE_TYPE (current
) = error_mark_node
;
688 /* FNS is a new overload set to add to the exising set. */
691 name_lookup::add_overload (tree fns
)
693 if (!deduping
&& TREE_CODE (fns
) == OVERLOAD
)
696 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
697 probe
= ovl_skip_hidden (probe
);
698 if (probe
&& TREE_CODE (probe
) == OVERLOAD
699 && OVL_DEDUP_P (probe
))
701 /* We're about to add something found by multiple paths, so
702 need to engage deduping mode. */
703 lookup_mark (value
, true);
708 value
= lookup_maybe_add (fns
, value
, deduping
);
711 /* Add a NEW_VAL, a found value binding into the current value binding. */
714 name_lookup::add_value (tree new_val
)
716 if (OVL_P (new_val
) && (!value
|| OVL_P (value
)))
717 add_overload (new_val
);
720 else if (value
== new_val
)
722 else if ((TREE_CODE (value
) == TYPE_DECL
723 && TREE_CODE (new_val
) == TYPE_DECL
724 && same_type_p (TREE_TYPE (value
), TREE_TYPE (new_val
))))
725 /* Typedefs to the same type. */;
726 else if (TREE_CODE (value
) == NAMESPACE_DECL
727 && TREE_CODE (new_val
) == NAMESPACE_DECL
728 && ORIGINAL_NAMESPACE (value
) == ORIGINAL_NAMESPACE (new_val
))
729 /* Namespace (possibly aliased) to the same namespace. Locate
731 value
= ORIGINAL_NAMESPACE (value
);
736 /* Disengage deduping mode. */
737 lookup_mark (value
, false);
740 value
= ambiguous (new_val
, value
);
744 /* Add a NEW_TYPE, a found type binding into the current type binding. */
747 name_lookup::add_type (tree new_type
)
751 else if (TREE_CODE (type
) == TREE_LIST
752 || !same_type_p (TREE_TYPE (type
), TREE_TYPE (new_type
)))
753 type
= ambiguous (new_type
, type
);
756 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns
757 true if we actually found something noteworthy. Hiddenness has
758 already been handled in the caller. */
761 name_lookup::process_binding (tree new_val
, tree new_type
)
763 /* Did we really see a type? */
765 && (want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
)
766 new_type
= NULL_TREE
;
768 /* Do we really see a value? */
770 switch (TREE_CODE (new_val
))
773 /* If we expect types or namespaces, and not templates,
774 or this is not a template class. */
775 if (bool (want
& LOOK_want::TYPE_NAMESPACE
)
776 && !DECL_TYPE_TEMPLATE_P (new_val
))
780 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::NAMESPACE
781 || (new_type
&& bool (want
& LOOK_want::TYPE
)))
785 if ((want
& LOOK_want::TYPE_NAMESPACE
) == LOOK_want::TYPE
)
789 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
796 new_type
= NULL_TREE
;
799 /* Merge into the lookup */
805 return new_val
!= NULL_TREE
;
808 /* If we're importing a module containing this binding, add it to the
809 lookup set. The trickiness is with namespaces, we only want to
813 name_lookup::process_module_binding (tree new_val
, tree new_type
,
816 /* Optimize for (re-)finding a public namespace. We only need to
818 if (new_val
&& !new_type
819 && TREE_CODE (new_val
) == NAMESPACE_DECL
820 && TREE_PUBLIC (new_val
)
821 && !DECL_NAMESPACE_ALIAS (new_val
))
828 if (new_type
|| new_val
)
829 marker
|= process_binding (new_val
, new_type
);
834 /* Look in exactly namespace SCOPE. */
837 name_lookup::search_namespace_only (tree scope
)
840 if (tree
*binding
= find_namespace_slot (scope
, name
))
843 if (TREE_CODE (val
) == BINDING_VECTOR
)
845 /* I presume the binding list is going to be sparser than
846 the import bitmap. Hence iterate over the former
847 checking for bits set in the bitmap. */
848 bitmap imports
= get_import_bitmap ();
849 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (val
);
853 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
857 if (named_module_purview_p ())
861 if (STAT_HACK_P (bind
) && MODULE_BINDING_GLOBAL_P (bind
))
867 tree type
= NULL_TREE
;
870 if (STAT_HACK_P (bind
))
872 type
= STAT_TYPE (bind
);
873 value
= STAT_DECL (bind
);
875 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
877 if (STAT_TYPE_HIDDEN_P (bind
))
879 if (STAT_DECL_HIDDEN_P (bind
))
882 value
= ovl_skip_hidden (value
);
885 else if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
886 value
= ovl_skip_hidden (value
);
888 marker
= process_module_binding (value
, type
, marker
);
891 /* Scan the imported bindings. */
892 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (val
);
893 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
899 /* Do this in forward order, so we load modules in an order
901 for (; ix
--; cluster
++)
902 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
904 /* Are we importing this module? */
905 if (unsigned base
= cluster
->indices
[jx
].base
)
906 if (unsigned span
= cluster
->indices
[jx
].span
)
908 if (bitmap_bit_p (imports
, base
))
910 while (++base
, --span
);
915 if (cluster
->slots
[jx
].is_lazy ())
917 gcc_assert (cluster
->indices
[jx
].span
== 1);
918 lazy_load_binding (cluster
->indices
[jx
].base
,
919 scope
, name
, &cluster
->slots
[jx
]);
921 tree bind
= cluster
->slots
[jx
];
923 /* Load errors could mean there's nothing here. */
926 /* Extract what we can see from here. If there's no
927 stat_hack, then everything was exported. */
928 tree type
= NULL_TREE
;
931 /* If STAT_HACK_P is false, everything is visible, and
932 there's no duplication possibilities. */
933 if (STAT_HACK_P (bind
))
937 /* Do we need to engage deduplication? */
939 if (MODULE_BINDING_GLOBAL_P (bind
))
941 else if (MODULE_BINDING_PARTITION_P (bind
))
943 if (unsigned hit
= dup_detect
& dup
)
945 if ((hit
& 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val
))
947 && BINDING_VECTOR_PARTITION_DUPS_P (val
)))
949 lookup_mark (value
, true);
956 if (STAT_TYPE_VISIBLE_P (bind
))
957 type
= STAT_TYPE (bind
);
958 bind
= STAT_VISIBLE (bind
);
961 /* And process it. */
962 marker
= process_module_binding (bind
, type
, marker
);
968 /* Only a current module binding, visible from the current module. */
969 tree bind
= *binding
;
970 tree value
= bind
, type
= NULL_TREE
;
972 if (STAT_HACK_P (bind
))
974 type
= STAT_TYPE (bind
);
975 value
= STAT_DECL (bind
);
977 if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
979 if (STAT_TYPE_HIDDEN_P (bind
))
981 if (STAT_DECL_HIDDEN_P (bind
))
984 value
= ovl_skip_hidden (value
);
987 else if (!bool (want
& LOOK_want::HIDDEN_FRIEND
))
988 value
= ovl_skip_hidden (value
);
990 found
|= process_binding (value
, type
);
997 /* Conditionally look in namespace SCOPE and inline children. */
1000 name_lookup::search_namespace (tree scope
)
1002 if (see_and_mark (scope
))
1003 /* We've visited this scope before. Return what we found then. */
1004 return found_p (scope
);
1006 /* Look in exactly namespace. */
1007 bool found
= search_namespace_only (scope
);
1009 /* Don't look into inline children, if we're looking for an
1010 anonymous name -- it must be in the current scope, if anywhere. */
1012 /* Recursively look in its inline children. */
1013 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1014 for (unsigned ix
= inlinees
->length (); ix
--;)
1015 found
|= search_namespace ((*inlinees
)[ix
]);
1023 /* Recursively follow using directives of SCOPE & its inline children.
1024 Such following is essentially a flood-fill algorithm. */
1027 name_lookup::search_usings (tree scope
)
1029 /* We do not check seen_p here, as that was already set during the
1030 namespace_only walk. */
1031 if (found_p (scope
))
1035 if (vec
<tree
, va_gc
> *usings
= NAMESPACE_LEVEL (scope
)->using_directives
)
1036 for (unsigned ix
= usings
->length (); ix
--;)
1037 found
|= search_qualified ((*usings
)[ix
], true);
1039 /* Look in its inline children. */
1040 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1041 for (unsigned ix
= inlinees
->length (); ix
--;)
1042 found
|= search_usings ((*inlinees
)[ix
]);
1050 /* Qualified namespace lookup in SCOPE.
1051 1) Look in SCOPE (+inlines). If found, we're done.
1052 2) Otherwise, if USINGS is true,
1053 recurse for every using directive of SCOPE (+inlines).
1055 Trickiness is (a) loops and (b) multiple paths to same namespace.
1056 In both cases we want to not repeat any lookups, and know whether
1057 to stop the caller's step #2. Do this via the FOUND_P marker. */
1060 name_lookup::search_qualified (tree scope
, bool usings
)
1065 found
= found_p (scope
);
1068 found
= search_namespace (scope
);
1069 if (!found
&& usings
)
1070 found
= search_usings (scope
);
1076 /* Add SCOPE to the unqualified search queue, recursively add its
1077 inlines and those via using directives. */
1079 name_lookup::using_queue
*
1080 name_lookup::queue_namespace (using_queue
*queue
, int depth
, tree scope
)
1082 if (see_and_mark (scope
))
1086 tree common
= scope
;
1087 while (SCOPE_DEPTH (common
) > depth
)
1088 common
= CP_DECL_CONTEXT (common
);
1089 vec_safe_push (queue
, using_pair (common
, scope
));
1091 /* Queue its inline children. */
1092 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1093 for (unsigned ix
= inlinees
->length (); ix
--;)
1094 queue
= queue_namespace (queue
, depth
, (*inlinees
)[ix
]);
1096 /* Queue its using targets. */
1097 queue
= queue_usings (queue
, depth
, NAMESPACE_LEVEL (scope
)->using_directives
);
1102 /* Add the namespaces in USINGS to the unqualified search queue. */
1104 name_lookup::using_queue
*
1105 name_lookup::do_queue_usings (using_queue
*queue
, int depth
,
1106 vec
<tree
, va_gc
> *usings
)
1108 for (unsigned ix
= usings
->length (); ix
--;)
1109 queue
= queue_namespace (queue
, depth
, (*usings
)[ix
]);
1114 /* Unqualified namespace lookup in SCOPE.
1115 1) add scope+inlins to worklist.
1116 2) recursively add target of every using directive
1117 3) for each worklist item where SCOPE is common ancestor, search it
1118 4) if nothing find, scope=parent, goto 1. */
1121 name_lookup::search_unqualified (tree scope
, cp_binding_level
*level
)
1123 /* Make static to avoid continual reallocation. We're not
1125 static using_queue
*queue
= NULL
;
1127 int length
= vec_safe_length (queue
);
1129 /* Queue local using-directives. */
1130 for (; level
->kind
!= sk_namespace
; level
= level
->level_chain
)
1131 queue
= queue_usings (queue
, SCOPE_DEPTH (scope
), level
->using_directives
);
1133 for (; !found
; scope
= CP_DECL_CONTEXT (scope
))
1135 gcc_assert (!DECL_NAMESPACE_ALIAS (scope
));
1136 int depth
= SCOPE_DEPTH (scope
);
1138 /* Queue namespaces reachable from SCOPE. */
1139 queue
= queue_namespace (queue
, depth
, scope
);
1141 /* Search every queued namespace where SCOPE is the common
1142 ancestor. Adjust the others. */
1143 unsigned ix
= length
;
1146 using_pair
&pair
= (*queue
)[ix
];
1147 while (pair
.first
== scope
)
1149 found
|= search_namespace_only (pair
.second
);
1150 pair
= queue
->pop ();
1151 if (ix
== queue
->length ())
1154 /* The depth is the same as SCOPE, find the parent scope. */
1155 if (SCOPE_DEPTH (pair
.first
) == depth
)
1156 pair
.first
= CP_DECL_CONTEXT (pair
.first
);
1159 while (ix
< queue
->length ());
1161 if (scope
== global_namespace
)
1164 /* If looking for hidden friends, we only look in the innermost
1165 namespace scope. [namespace.memdef]/3 If a friend
1166 declaration in a non-local class first declares a class,
1167 function, class template or function template the friend is a
1168 member of the innermost enclosing namespace. See also
1169 [basic.lookup.unqual]/7 */
1170 if (bool (want
& LOOK_want::HIDDEN_FRIEND
))
1174 /* Restore to incoming length. */
1175 vec_safe_truncate (queue
, length
);
1180 /* FNS is a value binding. If it is a (set of overloaded) functions,
1181 add them into the current value. */
1184 name_lookup::add_fns (tree fns
)
1188 else if (TREE_CODE (fns
) == OVERLOAD
)
1190 if (TREE_TYPE (fns
) != unknown_type_node
)
1191 fns
= OVL_FUNCTION (fns
);
1193 else if (!DECL_DECLARES_FUNCTION_P (fns
))
1199 /* Add the overloaded fns of SCOPE. */
1202 name_lookup::adl_namespace_fns (tree scope
, bitmap imports
)
1204 if (tree
*binding
= find_namespace_slot (scope
, name
))
1206 tree val
= *binding
;
1207 if (TREE_CODE (val
) != BINDING_VECTOR
)
1208 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (val
)));
1211 /* I presume the binding list is going to be sparser than
1212 the import bitmap. Hence iterate over the former
1213 checking for bits set in the bitmap. */
1214 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (val
);
1217 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
1219 /* The current TU's bindings must be visible, we don't
1220 need to check the bitmaps. */
1224 if (named_module_purview_p ())
1228 if (STAT_HACK_P (bind
) && MODULE_BINDING_GLOBAL_P (bind
))
1235 add_fns (ovl_skip_hidden (MAYBE_STAT_DECL (bind
)));
1238 /* Scan the imported bindings. */
1239 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (val
);
1240 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
1246 /* Do this in forward order, so we load modules in an order
1247 the user expects. */
1248 for (; ix
--; cluster
++)
1249 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
1251 /* Functions are never on merged slots. */
1252 if (!cluster
->indices
[jx
].base
1253 || cluster
->indices
[jx
].span
!= 1)
1256 /* Is this slot visible? */
1257 if (!bitmap_bit_p (imports
, cluster
->indices
[jx
].base
))
1261 if (cluster
->slots
[jx
].is_lazy ())
1262 lazy_load_binding (cluster
->indices
[jx
].base
,
1263 scope
, name
, &cluster
->slots
[jx
]);
1265 tree bind
= cluster
->slots
[jx
];
1267 /* Load errors could mean there's nothing here. */
1270 if (STAT_HACK_P (bind
))
1274 /* Do we need to engage deduplication? */
1276 if (MODULE_BINDING_GLOBAL_P (bind
))
1278 else if (MODULE_BINDING_PARTITION_P (bind
))
1280 if (unsigned hit
= dup_detect
& dup
)
1282 if ((hit
& 1 && BINDING_VECTOR_GLOBAL_DUPS_P (val
))
1284 && BINDING_VECTOR_PARTITION_DUPS_P (val
)))
1286 lookup_mark (value
, true);
1293 bind
= STAT_VISIBLE (bind
);
1302 /* Add the hidden friends of SCOPE. */
1305 name_lookup::adl_class_fns (tree type
)
1308 for (tree list
= DECL_FRIENDLIST (TYPE_MAIN_DECL (type
));
1309 list
; list
= TREE_CHAIN (list
))
1310 if (name
== FRIEND_NAME (list
))
1312 tree context
= NULL_TREE
; /* Lazily computed. */
1313 for (tree friends
= FRIEND_DECLS (list
); friends
;
1314 friends
= TREE_CHAIN (friends
))
1316 tree fn
= TREE_VALUE (friends
);
1318 /* Only interested in global functions with potentially hidden
1319 (i.e. unqualified) declarations. */
1321 context
= decl_namespace_context (type
);
1322 if (CP_DECL_CONTEXT (fn
) != context
)
1327 lookup_mark (value
, true);
1331 /* Template specializations are never found by name lookup.
1332 (Templates themselves can be found, but not template
1333 specializations.) */
1334 if (TREE_CODE (fn
) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn
))
1342 /* Find the containing non-inlined namespace, add it and all its
1346 name_lookup::adl_namespace (tree scope
)
1348 if (see_and_mark (scope
))
1351 /* Look down into inline namespaces. */
1352 if (vec
<tree
, va_gc
> *inlinees
= DECL_NAMESPACE_INLINEES (scope
))
1353 for (unsigned ix
= inlinees
->length (); ix
--;)
1354 adl_namespace ((*inlinees
)[ix
]);
1356 if (DECL_NAMESPACE_INLINE_P (scope
))
1358 adl_namespace (CP_DECL_CONTEXT (scope
));
1361 /* Adds the class and its friends to the lookup structure. */
1364 name_lookup::adl_class_only (tree type
)
1366 /* Backend-built structures, such as __builtin_va_list, aren't
1367 affected by all this. */
1368 if (!CLASS_TYPE_P (type
))
1371 type
= TYPE_MAIN_VARIANT (type
);
1373 if (see_and_mark (type
))
1376 tree context
= decl_namespace_context (type
);
1377 adl_namespace (context
);
1380 /* Adds the class and its bases to the lookup structure.
1381 Returns true on error. */
1384 name_lookup::adl_bases (tree type
)
1386 adl_class_only (type
);
1388 /* Process baseclasses. */
1389 if (tree binfo
= TYPE_BINFO (type
))
1394 for (i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
1395 adl_bases (BINFO_TYPE (base_binfo
));
1399 /* Adds everything associated with a class argument type to the lookup
1402 If T is a class type (including unions), its associated classes are: the
1403 class itself; the class of which it is a member, if any; and its direct
1404 and indirect base classes. Its associated namespaces are the namespaces
1405 of which its associated classes are members. Furthermore, if T is a
1406 class template specialization, its associated namespaces and classes
1407 also include: the namespaces and classes associated with the types of
1408 the template arguments provided for template type parameters (excluding
1409 template template parameters); the namespaces of which any template
1410 template arguments are members; and the classes of which any member
1411 templates used as template template arguments are members. [ Note:
1412 non-type template arguments do not contribute to the set of associated
1413 namespaces. --end note] */
1416 name_lookup::adl_class (tree type
)
1418 /* Backend build structures, such as __builtin_va_list, aren't
1419 affected by all this. */
1420 if (!CLASS_TYPE_P (type
))
1423 type
= TYPE_MAIN_VARIANT (type
);
1425 /* We don't set found here because we have to have set seen first,
1426 which is done in the adl_bases walk. */
1430 complete_type (type
);
1434 if (TYPE_CLASS_SCOPE_P (type
))
1435 adl_class_only (TYPE_CONTEXT (type
));
1437 /* Process template arguments. */
1438 if (CLASSTYPE_TEMPLATE_INFO (type
)
1439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
1441 tree list
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type
));
1442 for (int i
= 0; i
< TREE_VEC_LENGTH (list
); ++i
)
1443 adl_template_arg (TREE_VEC_ELT (list
, i
));
1448 name_lookup::adl_enum (tree type
)
1450 type
= TYPE_MAIN_VARIANT (type
);
1451 if (see_and_mark (type
))
1454 if (TYPE_CLASS_SCOPE_P (type
))
1455 adl_class_only (TYPE_CONTEXT (type
));
1457 adl_namespace (decl_namespace_context (type
));
1461 name_lookup::adl_expr (tree expr
)
1466 gcc_assert (!TYPE_P (expr
));
1468 if (TREE_TYPE (expr
) != unknown_type_node
)
1470 adl_type (unlowered_expr_type (expr
));
1474 if (TREE_CODE (expr
) == ADDR_EXPR
)
1475 expr
= TREE_OPERAND (expr
, 0);
1476 if (TREE_CODE (expr
) == COMPONENT_REF
1477 || TREE_CODE (expr
) == OFFSET_REF
)
1478 expr
= TREE_OPERAND (expr
, 1);
1479 expr
= MAYBE_BASELINK_FUNCTIONS (expr
);
1482 for (lkp_iterator
iter (expr
); iter
; ++iter
)
1483 adl_type (TREE_TYPE (*iter
));
1484 else if (TREE_CODE (expr
) == TEMPLATE_ID_EXPR
)
1486 /* The working paper doesn't currently say how to handle
1487 template-id arguments. The sensible thing would seem to be
1488 to handle the list of template candidates like a normal
1489 overload set, and handle the template arguments like we do
1490 for class template specializations. */
1492 /* First the templates. */
1493 adl_expr (TREE_OPERAND (expr
, 0));
1495 /* Now the arguments. */
1496 if (tree args
= TREE_OPERAND (expr
, 1))
1497 for (int ix
= TREE_VEC_LENGTH (args
); ix
--;)
1498 adl_template_arg (TREE_VEC_ELT (args
, ix
));
1503 name_lookup::adl_type (tree type
)
1508 if (TYPE_PTRDATAMEM_P (type
))
1510 /* Pointer to member: associate class type and value type. */
1511 adl_type (TYPE_PTRMEM_CLASS_TYPE (type
));
1512 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type
));
1516 switch (TREE_CODE (type
))
1519 if (TYPE_PTRMEMFUNC_P (type
))
1521 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type
));
1530 /* The basetype is referenced in the first arg type, so just
1533 /* Associate the parameter types. */
1534 for (tree args
= TYPE_ARG_TYPES (type
); args
; args
= TREE_CHAIN (args
))
1535 adl_type (TREE_VALUE (args
));
1539 case REFERENCE_TYPE
:
1541 adl_type (TREE_TYPE (type
));
1549 gcc_assert (type
== unknown_type_node
1550 || type
== init_list_type_node
);
1553 case TYPE_PACK_EXPANSION
:
1554 adl_type (PACK_EXPANSION_PATTERN (type
));
1562 /* Adds everything associated with a template argument to the lookup
1566 name_lookup::adl_template_arg (tree arg
)
1568 /* [basic.lookup.koenig]
1570 If T is a template-id, its associated namespaces and classes are
1571 ... the namespaces and classes associated with the types of the
1572 template arguments provided for template type parameters
1573 (excluding template template parameters); the namespaces in which
1574 any template template arguments are defined; and the classes in
1575 which any member templates used as template template arguments
1576 are defined. [Note: non-type template arguments do not
1577 contribute to the set of associated namespaces. ] */
1579 /* Consider first template template arguments. */
1580 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1581 || TREE_CODE (arg
) == UNBOUND_CLASS_TEMPLATE
)
1583 else if (TREE_CODE (arg
) == TEMPLATE_DECL
)
1585 tree ctx
= CP_DECL_CONTEXT (arg
);
1587 /* It's not a member template. */
1588 if (TREE_CODE (ctx
) == NAMESPACE_DECL
)
1589 adl_namespace (ctx
);
1590 /* Otherwise, it must be member template. */
1592 adl_class_only (ctx
);
1594 /* It's an argument pack; handle it recursively. */
1595 else if (ARGUMENT_PACK_P (arg
))
1597 tree args
= ARGUMENT_PACK_ARGS (arg
);
1598 int i
, len
= TREE_VEC_LENGTH (args
);
1599 for (i
= 0; i
< len
; ++i
)
1600 adl_template_arg (TREE_VEC_ELT (args
, i
));
1602 /* It's not a template template argument, but it is a type template
1604 else if (TYPE_P (arg
))
1608 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are
1609 the call arguments. */
1612 name_lookup::search_adl (tree fns
, vec
<tree
, va_gc
> *args
)
1614 gcc_checking_assert (!vec_safe_length (scopes
));
1616 /* Gather each associated entity onto the lookup's scope list. */
1620 FOR_EACH_VEC_ELT_REVERSE (*args
, ix
, arg
)
1621 /* OMP reduction operators put an ADL-significant type as the
1628 if (vec_safe_length (scopes
))
1630 /* Now do the lookups. */
1634 lookup_mark (fns
, true);
1638 /* INST_PATH will be NULL, if this is /not/ 2nd-phase ADL. */
1639 bitmap inst_path
= NULL
;
1640 /* VISIBLE is the regular import bitmap. */
1641 bitmap visible
= visible_instantiation_path (&inst_path
);
1643 for (unsigned ix
= scopes
->length (); ix
--;)
1645 tree scope
= (*scopes
)[ix
];
1646 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
1647 adl_namespace_fns (scope
, visible
);
1650 if (RECORD_OR_UNION_TYPE_P (scope
))
1651 adl_class_fns (scope
);
1653 /* During 2nd phase ADL: Any exported declaration D in N
1654 declared within the purview of a named module M
1655 (10.2) is visible if there is an associated entity
1656 attached to M with the same innermost enclosing
1657 non-inline namespace as D.
1658 [basic.lookup.argdep]/4.4 */
1661 /* Not 2nd phase. */
1664 tree ctx
= CP_DECL_CONTEXT (TYPE_NAME (scope
));
1665 if (TREE_CODE (ctx
) != NAMESPACE_DECL
)
1666 /* Not namespace-scope class. */
1669 tree origin
= get_originating_module_decl (TYPE_NAME (scope
));
1670 if (!DECL_LANG_SPECIFIC (origin
)
1671 || !DECL_MODULE_IMPORT_P (origin
))
1675 unsigned module
= get_importing_module (origin
);
1677 if (!bitmap_bit_p (inst_path
, module
))
1678 /* Not on path of instantiation. */
1681 if (bitmap_bit_p (visible
, module
))
1682 /* If the module was in the visible set, we'll look at
1683 its namespace partition anyway. */
1686 if (tree
*slot
= find_namespace_slot (ctx
, name
, false))
1687 if (binding_slot
*mslot
= search_imported_binding_slot (slot
, module
))
1689 if (mslot
->is_lazy ())
1690 lazy_load_binding (module
, ctx
, name
, mslot
);
1692 if (tree bind
= *mslot
)
1696 /* We must turn on deduping, because some
1697 other class from this module might also
1698 be in this namespace. */
1700 lookup_mark (value
, true);
1703 /* Add the exported fns */
1704 if (STAT_HACK_P (bind
))
1705 add_fns (STAT_VISIBLE (bind
));
1717 static bool qualified_namespace_lookup (tree
, name_lookup
*);
1718 static void consider_binding_level (tree name
,
1719 best_match
<tree
, const char *> &bm
,
1720 cp_binding_level
*lvl
,
1721 bool look_within_fields
,
1722 enum lookup_name_fuzzy_kind kind
);
1723 static void diagnose_name_conflict (tree
, tree
);
1725 /* ADL lookup of NAME. FNS is the result of regular lookup, and we
1726 don't add duplicates to it. ARGS is the vector of call
1727 arguments (which will not be empty). */
1730 lookup_arg_dependent (tree name
, tree fns
, vec
<tree
, va_gc
> *args
)
1732 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
1733 name_lookup
lookup (name
);
1734 fns
= lookup
.search_adl (fns
, args
);
1735 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
1739 /* FNS is an overload set of conversion functions. Return the
1740 overloads converting to TYPE. */
1743 extract_conversion_operator (tree fns
, tree type
)
1745 tree convs
= NULL_TREE
;
1746 tree tpls
= NULL_TREE
;
1748 for (ovl_iterator
iter (fns
); iter
; ++iter
)
1750 if (same_type_p (DECL_CONV_FN_TYPE (*iter
), type
))
1751 convs
= lookup_add (*iter
, convs
);
1753 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
1754 tpls
= lookup_add (*iter
, tpls
);
1763 /* Binary search of (ordered) MEMBER_VEC for NAME. */
1766 member_vec_binary_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1768 for (unsigned lo
= 0, hi
= member_vec
->length (); lo
< hi
;)
1770 unsigned mid
= (lo
+ hi
) / 2;
1771 tree binding
= (*member_vec
)[mid
];
1772 tree binding_name
= OVL_NAME (binding
);
1774 if (binding_name
> name
)
1776 else if (binding_name
< name
)
1785 /* Linear search of (unordered) MEMBER_VEC for NAME. */
1788 member_vec_linear_search (vec
<tree
, va_gc
> *member_vec
, tree name
)
1790 for (int ix
= member_vec
->length (); ix
--;)
1791 if (tree binding
= (*member_vec
)[ix
])
1792 if (OVL_NAME (binding
) == name
)
1798 /* Linear search of (partially ordered) fields of KLASS for NAME. */
1801 fields_linear_search (tree klass
, tree name
, bool want_type
)
1803 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
1807 if (TREE_CODE (decl
) == FIELD_DECL
1808 && ANON_AGGR_TYPE_P (TREE_TYPE (decl
)))
1810 if (tree temp
= search_anon_aggr (TREE_TYPE (decl
), name
, want_type
))
1814 if (DECL_NAME (decl
) != name
)
1817 if (TREE_CODE (decl
) == USING_DECL
)
1819 decl
= strip_using_decl (decl
);
1820 if (is_overloaded_fn (decl
))
1824 if (DECL_DECLARES_FUNCTION_P (decl
))
1825 /* Functions are found separately. */
1828 if (!want_type
|| DECL_DECLARES_TYPE_P (decl
))
1835 /* Look for NAME member inside of anonymous aggregate ANON. Although
1836 such things should only contain FIELD_DECLs, we check that too
1837 late, and would give very confusing errors if we weren't
1841 search_anon_aggr (tree anon
, tree name
, bool want_type
)
1843 gcc_assert (COMPLETE_TYPE_P (anon
));
1844 tree ret
= get_class_binding_direct (anon
, name
, want_type
);
1848 /* Look for NAME as an immediate member of KLASS (including
1849 anon-members or unscoped enum member). TYPE_OR_FNS is zero for
1850 regular search. >0 to get a type binding (if there is one) and <0
1851 if you want (just) the member function binding.
1853 Use this if you do not want lazy member creation. */
1856 get_class_binding_direct (tree klass
, tree name
, bool want_type
)
1858 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass
));
1860 /* Conversion operators can only be found by the marker conversion
1862 bool conv_op
= IDENTIFIER_CONV_OP_P (name
);
1863 tree lookup
= conv_op
? conv_op_identifier
: name
;
1864 tree val
= NULL_TREE
;
1865 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1867 if (COMPLETE_TYPE_P (klass
) && member_vec
)
1869 val
= member_vec_binary_search (member_vec
, lookup
);
1872 else if (STAT_HACK_P (val
))
1873 val
= want_type
? STAT_TYPE (val
) : STAT_DECL (val
);
1874 else if (want_type
&& !DECL_DECLARES_TYPE_P (val
))
1879 if (member_vec
&& !want_type
)
1880 val
= member_vec_linear_search (member_vec
, lookup
);
1882 if (!val
|| (TREE_CODE (val
) == OVERLOAD
&& OVL_DEDUP_P (val
)))
1883 /* Dependent using declarations are a 'field', make sure we
1884 return that even if we saw an overload already. */
1885 if (tree field_val
= fields_linear_search (klass
, lookup
, want_type
))
1889 else if (TREE_CODE (field_val
) == USING_DECL
)
1890 val
= ovl_make (field_val
, val
);
1894 /* Extract the conversion operators asked for, unless the general
1895 conversion operator was requested. */
1898 gcc_checking_assert (OVL_FUNCTION (val
) == conv_op_marker
);
1899 val
= OVL_CHAIN (val
);
1900 if (tree type
= TREE_TYPE (name
))
1901 val
= extract_conversion_operator (val
, type
);
1907 /* We're about to lookup NAME in KLASS. Make sure any lazily declared
1908 members are now declared. */
1911 maybe_lazily_declare (tree klass
, tree name
)
1913 tree main_decl
= TYPE_NAME (TYPE_MAIN_VARIANT (klass
));
1914 if (DECL_LANG_SPECIFIC (main_decl
)
1915 && DECL_MODULE_PENDING_MEMBERS_P (main_decl
))
1916 lazy_load_members (main_decl
);
1918 /* Lazily declare functions, if we're going to search these. */
1919 if (IDENTIFIER_CTOR_P (name
))
1921 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass
))
1922 lazily_declare_fn (sfk_constructor
, klass
);
1923 if (CLASSTYPE_LAZY_COPY_CTOR (klass
))
1924 lazily_declare_fn (sfk_copy_constructor
, klass
);
1925 if (CLASSTYPE_LAZY_MOVE_CTOR (klass
))
1926 lazily_declare_fn (sfk_move_constructor
, klass
);
1928 else if (IDENTIFIER_DTOR_P (name
))
1930 if (CLASSTYPE_LAZY_DESTRUCTOR (klass
))
1931 lazily_declare_fn (sfk_destructor
, klass
);
1933 else if (name
== assign_op_identifier
)
1935 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass
))
1936 lazily_declare_fn (sfk_copy_assignment
, klass
);
1937 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass
))
1938 lazily_declare_fn (sfk_move_assignment
, klass
);
1942 /* Look for NAME's binding in exactly KLASS. See
1943 get_class_binding_direct for argument description. Does lazy
1944 special function creation as necessary. */
1947 get_class_binding (tree klass
, tree name
, bool want_type
/*=false*/)
1949 klass
= complete_type (klass
);
1951 if (COMPLETE_TYPE_P (klass
))
1952 maybe_lazily_declare (klass
, name
);
1954 return get_class_binding_direct (klass
, name
, want_type
);
1957 /* Find the slot containing overloads called 'NAME'. If there is no
1958 such slot and the class is complete, create an empty one, at the
1959 correct point in the sorted member vector. Otherwise return NULL.
1960 Deals with conv_op marker handling. */
1963 find_member_slot (tree klass
, tree name
)
1965 bool complete_p
= COMPLETE_TYPE_P (klass
);
1967 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
1970 vec_alloc (member_vec
, 8);
1971 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
1973 /* If the class is complete but had no member_vec, we need to
1974 add the TYPE_FIELDS into it. We're also most likely to be
1975 adding ctors & dtors, so ask for 6 spare slots (the
1976 abstract cdtors and their clones). */
1977 member_vec
= set_class_bindings (klass
, 6);
1980 if (IDENTIFIER_CONV_OP_P (name
))
1981 name
= conv_op_identifier
;
1983 unsigned ix
, length
= member_vec
->length ();
1984 for (ix
= 0; ix
< length
; ix
++)
1986 tree
*slot
= &(*member_vec
)[ix
];
1987 tree fn_name
= OVL_NAME (*slot
);
1989 if (fn_name
== name
)
1991 /* If we found an existing slot, it must be a function set.
1992 Even with insertion after completion, because those only
1993 happen with artificial fns that have unspellable names.
1994 This means we do not have to deal with the stat hack
1996 gcc_checking_assert (OVL_P (*slot
));
1997 if (name
== conv_op_identifier
)
1999 gcc_checking_assert (OVL_FUNCTION (*slot
) == conv_op_marker
);
2000 /* Skip the conv-op marker. */
2001 slot
= &OVL_CHAIN (*slot
);
2006 if (complete_p
&& fn_name
> name
)
2010 /* No slot found, add one if the class is complete. */
2013 /* Do exact allocation, as we don't expect to add many. */
2014 gcc_assert (name
!= conv_op_identifier
);
2015 vec_safe_reserve_exact (member_vec
, 1);
2016 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2017 member_vec
->quick_insert (ix
, NULL_TREE
);
2018 return &(*member_vec
)[ix
];
2024 /* KLASS is an incomplete class to which we're adding a method NAME.
2025 Add a slot and deal with conv_op marker handling. */
2028 add_member_slot (tree klass
, tree name
)
2030 gcc_assert (!COMPLETE_TYPE_P (klass
));
2032 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2033 vec_safe_push (member_vec
, NULL_TREE
);
2034 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2036 tree
*slot
= &member_vec
->last ();
2037 if (IDENTIFIER_CONV_OP_P (name
))
2039 /* Install the marker prefix. */
2040 *slot
= ovl_make (conv_op_marker
, NULL_TREE
);
2041 slot
= &OVL_CHAIN (*slot
);
2047 /* Comparison function to compare two MEMBER_VEC entries by name.
2048 Because we can have duplicates during insertion of TYPE_FIELDS, we
2049 do extra checking so deduping doesn't have to deal with so many
2053 member_name_cmp (const void *a_p
, const void *b_p
)
2055 tree a
= *(const tree
*)a_p
;
2056 tree b
= *(const tree
*)b_p
;
2057 tree name_a
= DECL_NAME (TREE_CODE (a
) == OVERLOAD
? OVL_FUNCTION (a
) : a
);
2058 tree name_b
= DECL_NAME (TREE_CODE (b
) == OVERLOAD
? OVL_FUNCTION (b
) : b
);
2060 gcc_checking_assert (name_a
&& name_b
);
2061 if (name_a
!= name_b
)
2062 return name_a
< name_b
? -1 : +1;
2064 if (name_a
== conv_op_identifier
)
2066 /* Strip the conv-op markers. */
2067 gcc_checking_assert (OVL_FUNCTION (a
) == conv_op_marker
2068 && OVL_FUNCTION (b
) == conv_op_marker
);
2073 if (TREE_CODE (a
) == OVERLOAD
)
2074 a
= OVL_FUNCTION (a
);
2075 if (TREE_CODE (b
) == OVERLOAD
)
2076 b
= OVL_FUNCTION (b
);
2078 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */
2079 if (TREE_CODE (a
) != TREE_CODE (b
))
2081 /* If one of them is a TYPE_DECL, it loses. */
2082 if (TREE_CODE (a
) == TYPE_DECL
)
2084 else if (TREE_CODE (b
) == TYPE_DECL
)
2087 /* If one of them is a USING_DECL, it loses. */
2088 if (TREE_CODE (a
) == USING_DECL
)
2090 else if (TREE_CODE (b
) == USING_DECL
)
2093 /* There are no other cases with different kinds of decls, as
2094 duplicate detection should have kicked in earlier. However,
2095 some erroneous cases get though. */
2096 gcc_assert (errorcount
);
2099 /* Using source location would be the best thing here, but we can
2100 get identically-located decls in the following circumstances:
2102 1) duplicate artificial type-decls for the same type.
2104 2) pack expansions of using-decls.
2106 We should not be doing #1, but in either case it doesn't matter
2107 how we order these. Use UID as a proxy for source ordering, so
2108 that identically-located decls still have a well-defined stable
2110 if (DECL_UID (a
) != DECL_UID (b
))
2111 return DECL_UID (a
) < DECL_UID (b
) ? -1 : +1;
2112 gcc_assert (a
== b
);
2117 gt_pointer_operator new_value
;
2121 /* This routine compares two fields like member_name_cmp but using the
2122 pointer operator in resort_field_decl_data. We don't have to deal
2123 with duplicates here. */
2126 resort_member_name_cmp (const void *a_p
, const void *b_p
)
2128 tree a
= *(const tree
*)a_p
;
2129 tree b
= *(const tree
*)b_p
;
2130 tree name_a
= OVL_NAME (a
);
2131 tree name_b
= OVL_NAME (b
);
2133 resort_data
.new_value (&name_a
, resort_data
.cookie
);
2134 resort_data
.new_value (&name_b
, resort_data
.cookie
);
2136 gcc_checking_assert (name_a
!= name_b
);
2138 return name_a
< name_b
? -1 : +1;
2141 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */
2144 resort_type_member_vec (void *obj
, void */
*orig_obj*/
,
2145 gt_pointer_operator new_value
, void* cookie
)
2147 if (vec
<tree
, va_gc
> *member_vec
= (vec
<tree
, va_gc
> *) obj
)
2149 resort_data
.new_value
= new_value
;
2150 resort_data
.cookie
= cookie
;
2151 member_vec
->qsort (resort_member_name_cmp
);
2155 /* Recursively count the number of fields in KLASS, including anonymous
2159 count_class_fields (tree klass
)
2161 unsigned n_fields
= 0;
2163 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
2164 if (DECL_DECLARES_FUNCTION_P (fields
))
2165 /* Functions are dealt with separately. */;
2166 else if (TREE_CODE (fields
) == FIELD_DECL
2167 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2168 n_fields
+= count_class_fields (TREE_TYPE (fields
));
2169 else if (DECL_NAME (fields
))
2175 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC.
2176 Recurse for anonymous members. MEMBER_VEC must have space. */
2179 member_vec_append_class_fields (vec
<tree
, va_gc
> *member_vec
, tree klass
)
2181 for (tree fields
= TYPE_FIELDS (klass
); fields
; fields
= DECL_CHAIN (fields
))
2182 if (DECL_DECLARES_FUNCTION_P (fields
))
2183 /* Functions are handled separately. */;
2184 else if (TREE_CODE (fields
) == FIELD_DECL
2185 && ANON_AGGR_TYPE_P (TREE_TYPE (fields
)))
2186 member_vec_append_class_fields (member_vec
, TREE_TYPE (fields
));
2187 else if (DECL_NAME (fields
))
2189 tree field
= fields
;
2190 /* Mark a conv-op USING_DECL with the conv-op-marker. */
2191 if (TREE_CODE (field
) == USING_DECL
2192 && IDENTIFIER_CONV_OP_P (DECL_NAME (field
)))
2193 field
= ovl_make (conv_op_marker
, field
);
2194 member_vec
->quick_push (field
);
2198 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC.
2199 MEMBER_VEC must have space. */
2202 member_vec_append_enum_values (vec
<tree
, va_gc
> *member_vec
, tree enumtype
)
2204 for (tree values
= TYPE_VALUES (enumtype
);
2205 values
; values
= TREE_CHAIN (values
))
2206 member_vec
->quick_push (TREE_VALUE (values
));
2209 /* MEMBER_VEC has just had new DECLs added to it, but is sorted.
2210 DeDup adjacent DECLS of the same name. We already dealt with
2211 conflict resolution when adding the fields or methods themselves.
2212 There are three cases (which could all be combined):
2213 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate.
2214 2) a USING_DECL and an overload. If the USING_DECL is dependent,
2215 it wins. Otherwise the OVERLOAD does.
2216 3) two USING_DECLS. ...
2218 member_name_cmp will have ordered duplicates as
2219 <fns><using><type> */
2222 member_vec_dedup (vec
<tree
, va_gc
> *member_vec
)
2224 unsigned len
= member_vec
->length ();
2230 tree name
= OVL_NAME ((*member_vec
)[0]);
2231 for (unsigned jx
, ix
= 0; ix
< len
; ix
= jx
)
2233 tree current
= NULL_TREE
;
2234 tree to_type
= NULL_TREE
;
2235 tree to_using
= NULL_TREE
;
2236 tree marker
= NULL_TREE
;
2238 for (jx
= ix
; jx
< len
; jx
++)
2240 tree next
= (*member_vec
)[jx
];
2243 tree next_name
= OVL_NAME (next
);
2244 if (next_name
!= name
)
2251 if (IDENTIFIER_CONV_OP_P (name
))
2254 next
= OVL_CHAIN (next
);
2257 if (TREE_CODE (next
) == USING_DECL
)
2259 if (IDENTIFIER_CTOR_P (name
))
2260 /* Dependent inherited ctor. */
2263 next
= strip_using_decl (next
);
2264 if (TREE_CODE (next
) == USING_DECL
)
2270 if (is_overloaded_fn (next
))
2274 if (DECL_DECLARES_TYPE_P (next
))
2289 current
= ovl_make (to_using
, current
);
2297 current
= stat_hack (current
, to_type
);
2304 OVL_CHAIN (marker
) = current
;
2307 (*member_vec
)[store
++] = current
;
2311 while (store
++ < len
)
2315 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is
2316 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We
2317 know there must be at least 1 field -- the self-reference
2318 TYPE_DECL, except for anon aggregates, which will have at least
2319 one field anyway. If EXTRA < 0, always create the vector. */
2322 set_class_bindings (tree klass
, int extra
)
2324 unsigned n_fields
= count_class_fields (klass
);
2325 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2327 if (member_vec
|| n_fields
>= 8 || extra
< 0)
2329 /* Append the new fields. */
2330 vec_safe_reserve_exact (member_vec
, n_fields
+ (extra
>= 0 ? extra
: 0));
2331 member_vec_append_class_fields (member_vec
, klass
);
2336 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2337 member_vec
->qsort (member_name_cmp
);
2338 member_vec_dedup (member_vec
);
2344 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */
2347 insert_late_enum_def_bindings (tree klass
, tree enumtype
)
2350 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
2352 /* The enum bindings will already be on the TYPE_FIELDS, so don't
2353 count them twice. */
2355 n_fields
= count_class_fields (klass
);
2357 n_fields
= list_length (TYPE_VALUES (enumtype
));
2359 if (member_vec
|| n_fields
>= 8)
2361 vec_safe_reserve_exact (member_vec
, n_fields
);
2362 if (CLASSTYPE_MEMBER_VEC (klass
))
2363 member_vec_append_enum_values (member_vec
, enumtype
);
2365 member_vec_append_class_fields (member_vec
, klass
);
2366 CLASSTYPE_MEMBER_VEC (klass
) = member_vec
;
2367 member_vec
->qsort (member_name_cmp
);
2368 member_vec_dedup (member_vec
);
2372 /* The binding oracle; see cp-tree.h. */
2374 cp_binding_oracle_function
*cp_binding_oracle
;
2376 /* If we have a binding oracle, ask it for all namespace-scoped
2377 definitions of NAME. */
2380 query_oracle (tree name
)
2382 if (!cp_binding_oracle
)
2385 /* LOOKED_UP holds the set of identifiers that we have already
2386 looked up with the oracle. */
2387 static hash_set
<tree
> looked_up
;
2388 if (looked_up
.add (name
))
2391 cp_binding_oracle (CP_ORACLE_IDENTIFIER
, name
);
2394 #ifndef ENABLE_SCOPE_CHECKING
2395 # define ENABLE_SCOPE_CHECKING 0
2397 # define ENABLE_SCOPE_CHECKING 1
2400 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */
2402 static GTY((deletable
)) cxx_binding
*free_bindings
;
2404 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
2408 cxx_binding_init (cxx_binding
*binding
, tree value
, tree type
)
2410 binding
->value
= value
;
2411 binding
->type
= type
;
2412 binding
->previous
= NULL
;
2415 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */
2417 static cxx_binding
*
2418 cxx_binding_make (tree value
, tree type
)
2420 cxx_binding
*binding
= free_bindings
;
2423 free_bindings
= binding
->previous
;
2425 binding
= ggc_alloc
<cxx_binding
> ();
2427 /* Clear flags by default. */
2428 LOCAL_BINDING_P (binding
) = false;
2429 INHERITED_VALUE_BINDING_P (binding
) = false;
2430 HIDDEN_TYPE_BINDING_P (binding
) = false;
2432 cxx_binding_init (binding
, value
, type
);
2437 /* Put BINDING back on the free list. */
2440 cxx_binding_free (cxx_binding
*binding
)
2442 binding
->scope
= NULL
;
2443 binding
->previous
= free_bindings
;
2444 free_bindings
= binding
;
2447 /* Create a new binding for NAME (with the indicated VALUE and TYPE
2448 bindings) in the class scope indicated by SCOPE. */
2450 static cxx_binding
*
2451 new_class_binding (tree name
, tree value
, tree type
, cp_binding_level
*scope
)
2453 cp_class_binding cb
= {cxx_binding_make (value
, type
), name
};
2454 cxx_binding
*binding
= cb
.base
;
2455 vec_safe_push (scope
->class_shadowed
, cb
);
2456 binding
->scope
= scope
;
2460 /* Make DECL the innermost binding for ID. The LEVEL is the binding
2461 level at which this declaration is being bound. */
2464 push_binding (tree id
, tree decl
, cp_binding_level
* level
)
2466 cxx_binding
*binding
;
2468 if (level
!= class_binding_level
)
2470 binding
= cxx_binding_make (decl
, NULL_TREE
);
2471 binding
->scope
= level
;
2474 binding
= new_class_binding (id
, decl
, /*type=*/NULL_TREE
, level
);
2476 /* Now, fill in the binding information. */
2477 binding
->previous
= IDENTIFIER_BINDING (id
);
2478 LOCAL_BINDING_P (binding
) = (level
!= class_binding_level
);
2480 /* And put it on the front of the list of bindings for ID. */
2481 IDENTIFIER_BINDING (id
) = binding
;
2484 /* Remove the binding for DECL which should be the innermost binding
2488 pop_local_binding (tree id
, tree decl
)
2490 if (!id
|| IDENTIFIER_ANON_P (id
))
2491 /* It's easiest to write the loops that call this function without
2492 checking whether or not the entities involved have names. We
2493 get here for such an entity. */
2496 /* Get the innermost binding for ID. */
2497 cxx_binding
*binding
= IDENTIFIER_BINDING (id
);
2499 /* The name should be bound. */
2500 gcc_assert (binding
!= NULL
);
2502 /* The DECL will be either the ordinary binding or the type binding
2503 for this identifier. Remove that binding. We don't have to
2504 clear HIDDEN_TYPE_BINDING_P, as the whole binding will be going
2506 if (binding
->value
== decl
)
2507 binding
->value
= NULL_TREE
;
2510 gcc_checking_assert (binding
->type
== decl
);
2511 binding
->type
= NULL_TREE
;
2514 if (!binding
->value
&& !binding
->type
)
2516 /* We're completely done with the innermost binding for this
2517 identifier. Unhook it from the list of bindings. */
2518 IDENTIFIER_BINDING (id
) = binding
->previous
;
2520 /* Add it to the free list. */
2521 cxx_binding_free (binding
);
2525 /* Remove the bindings for the decls of the current level and leave
2526 the current scope. */
2529 pop_bindings_and_leave_scope (void)
2531 for (tree t
= get_local_decls (); t
; t
= DECL_CHAIN (t
))
2533 tree decl
= TREE_CODE (t
) == TREE_LIST
? TREE_VALUE (t
) : t
;
2534 tree name
= OVL_NAME (decl
);
2536 pop_local_binding (name
, decl
);
2542 /* Strip non dependent using declarations. If DECL is dependent,
2543 surreptitiously create a typename_type and return it. */
2546 strip_using_decl (tree decl
)
2548 if (decl
== NULL_TREE
)
2551 while (TREE_CODE (decl
) == USING_DECL
&& !DECL_DEPENDENT_P (decl
))
2552 decl
= USING_DECL_DECLS (decl
);
2554 if (TREE_CODE (decl
) == USING_DECL
&& DECL_DEPENDENT_P (decl
)
2555 && USING_DECL_TYPENAME_P (decl
))
2557 /* We have found a type introduced by a using
2558 declaration at class scope that refers to a dependent
2561 using typename :: [opt] nested-name-specifier unqualified-id ;
2563 decl
= make_typename_type (USING_DECL_SCOPE (decl
),
2565 typename_type
, tf_error
);
2566 if (decl
!= error_mark_node
)
2567 decl
= TYPE_NAME (decl
);
2573 /* Return true if OVL is an overload for an anticipated builtin. */
2576 anticipated_builtin_p (tree ovl
)
2578 return (TREE_CODE (ovl
) == OVERLOAD
2579 && OVL_HIDDEN_P (ovl
)
2580 && DECL_IS_UNDECLARED_BUILTIN (OVL_FUNCTION (ovl
)));
2583 /* BINDING records an existing declaration for a name in the current scope.
2584 But, DECL is another declaration for that same identifier in the
2585 same scope. This is the `struct stat' hack whereby a non-typedef
2586 class name or enum-name can be bound at the same level as some other
2590 A class name (9.1) or enumeration name (7.2) can be hidden by the
2591 name of an object, function, or enumerator declared in the same scope.
2592 If a class or enumeration name and an object, function, or enumerator
2593 are declared in the same scope (in any order) with the same name, the
2594 class or enumeration name is hidden wherever the object, function, or
2595 enumerator name is visible.
2597 It's the responsibility of the caller to check that
2598 inserting this name is valid here. Returns nonzero if the new binding
2602 supplement_binding_1 (cxx_binding
*binding
, tree decl
)
2604 tree bval
= binding
->value
;
2606 tree target_bval
= strip_using_decl (bval
);
2607 tree target_decl
= strip_using_decl (decl
);
2609 if (TREE_CODE (target_decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (target_decl
)
2610 && target_decl
!= target_bval
2611 && (TREE_CODE (target_bval
) != TYPE_DECL
2612 /* We allow pushing an enum multiple times in a class
2613 template in order to handle late matching of underlying
2614 type on an opaque-enum-declaration followed by an
2616 || (processing_template_decl
2617 && TREE_CODE (TREE_TYPE (target_decl
)) == ENUMERAL_TYPE
2618 && TREE_CODE (TREE_TYPE (target_bval
)) == ENUMERAL_TYPE
2619 && (dependent_type_p (ENUM_UNDERLYING_TYPE
2620 (TREE_TYPE (target_decl
)))
2621 || dependent_type_p (ENUM_UNDERLYING_TYPE
2622 (TREE_TYPE (target_bval
)))))))
2623 /* The new name is the type name. */
2624 binding
->type
= decl
;
2625 else if (/* TARGET_BVAL is null when push_class_level_binding moves
2626 an inherited type-binding out of the way to make room
2627 for a new value binding. */
2629 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
2630 has been used in a non-class scope prior declaration.
2631 In that case, we should have already issued a
2632 diagnostic; for graceful error recovery purpose, pretend
2633 this was the intended declaration for that name. */
2634 || target_bval
== error_mark_node
2635 /* If TARGET_BVAL is anticipated but has not yet been
2636 declared, pretend it is not there at all. */
2637 || anticipated_builtin_p (target_bval
))
2638 binding
->value
= decl
;
2639 else if (TREE_CODE (target_bval
) == TYPE_DECL
2640 && DECL_ARTIFICIAL (target_bval
)
2641 && target_decl
!= target_bval
2642 && (TREE_CODE (target_decl
) != TYPE_DECL
2643 || same_type_p (TREE_TYPE (target_decl
),
2644 TREE_TYPE (target_bval
))))
2646 /* The old binding was a type name. It was placed in
2647 VALUE field because it was thought, at the point it was
2648 declared, to be the only entity with such a name. Move the
2649 type name into the type slot; it is now hidden by the new
2651 binding
->type
= bval
;
2652 binding
->value
= decl
;
2653 binding
->value_is_inherited
= false;
2655 else if (TREE_CODE (target_bval
) == TYPE_DECL
2656 && TREE_CODE (target_decl
) == TYPE_DECL
2657 && DECL_NAME (target_decl
) == DECL_NAME (target_bval
)
2658 && binding
->scope
->kind
!= sk_class
2659 && (same_type_p (TREE_TYPE (target_decl
), TREE_TYPE (target_bval
))
2660 /* If either type involves template parameters, we must
2661 wait until instantiation. */
2662 || uses_template_parms (TREE_TYPE (target_decl
))
2663 || uses_template_parms (TREE_TYPE (target_bval
))))
2664 /* We have two typedef-names, both naming the same type to have
2665 the same name. In general, this is OK because of:
2669 In a given scope, a typedef specifier can be used to redefine
2670 the name of any type declared in that scope to refer to the
2671 type to which it already refers.
2673 However, in class scopes, this rule does not apply due to the
2674 stricter language in [class.mem] prohibiting redeclarations of
2677 /* There can be two block-scope declarations of the same variable,
2678 so long as they are `extern' declarations. However, there cannot
2679 be two declarations of the same static data member:
2683 A member shall not be declared twice in the
2684 member-specification. */
2685 else if (VAR_P (target_decl
)
2686 && VAR_P (target_bval
)
2687 && DECL_EXTERNAL (target_decl
) && DECL_EXTERNAL (target_bval
)
2688 && !DECL_CLASS_SCOPE_P (target_decl
))
2690 duplicate_decls (decl
, binding
->value
);
2693 else if (TREE_CODE (decl
) == NAMESPACE_DECL
2694 && TREE_CODE (bval
) == NAMESPACE_DECL
2695 && DECL_NAMESPACE_ALIAS (decl
)
2696 && DECL_NAMESPACE_ALIAS (bval
)
2697 && ORIGINAL_NAMESPACE (bval
) == ORIGINAL_NAMESPACE (decl
))
2698 /* [namespace.alias]
2700 In a declarative region, a namespace-alias-definition can be
2701 used to redefine a namespace-alias declared in that declarative
2702 region to refer only to the namespace to which it already
2705 else if (TREE_CODE (bval
) == USING_DECL
2706 && CONST_DECL_USING_P (decl
))
2707 /* Let the clone hide the using-decl that introduced it. */
2708 binding
->value
= decl
;
2711 if (!error_operand_p (bval
))
2712 diagnose_name_conflict (decl
, bval
);
2719 /* Diagnose a name conflict between DECL and BVAL. */
2722 diagnose_name_conflict (tree decl
, tree bval
)
2724 if (TREE_CODE (decl
) == TREE_CODE (bval
)
2725 && TREE_CODE (decl
) != NAMESPACE_DECL
2726 && !DECL_DECLARES_FUNCTION_P (decl
)
2727 && (TREE_CODE (decl
) != TYPE_DECL
2728 || DECL_ARTIFICIAL (decl
) == DECL_ARTIFICIAL (bval
))
2729 && CP_DECL_CONTEXT (decl
) == CP_DECL_CONTEXT (bval
))
2731 if (concept_definition_p (decl
))
2732 error ("redeclaration of %q#D with different template parameters",
2735 error ("redeclaration of %q#D", decl
);
2738 error ("%q#D conflicts with a previous declaration", decl
);
2740 inform (location_of (bval
), "previous declaration %q#D", bval
);
2743 /* Wrapper for supplement_binding_1. */
2746 supplement_binding (cxx_binding
*binding
, tree decl
)
2749 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
2750 ret
= supplement_binding_1 (binding
, decl
);
2751 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
2755 /* Replace BINDING's current value on its scope's name list with
2759 update_local_overload (cxx_binding
*binding
, tree newval
)
2763 for (d
= &binding
->scope
->names
; ; d
= &TREE_CHAIN (*d
))
2764 if (*d
== binding
->value
)
2766 /* Stitch new list node in. */
2767 *d
= tree_cons (DECL_NAME (*d
), NULL_TREE
, TREE_CHAIN (*d
));
2770 else if (TREE_CODE (*d
) == TREE_LIST
&& TREE_VALUE (*d
) == binding
->value
)
2773 TREE_VALUE (*d
) = newval
;
2776 /* Compares the parameter-type-lists of ONE and TWO and
2777 returns false if they are different. If the DECLs are template
2778 functions, the return types and the template parameter lists are
2779 compared too (DR 565). */
2782 matching_fn_p (tree one
, tree two
)
2784 if (TREE_CODE (one
) != TREE_CODE (two
))
2787 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one
)),
2788 TYPE_ARG_TYPES (TREE_TYPE (two
))))
2791 if (TREE_CODE (one
) == TEMPLATE_DECL
)
2793 /* Compare template parms. */
2794 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one
),
2795 DECL_TEMPLATE_PARMS (two
)))
2798 /* And return type. */
2799 if (!same_type_p (TREE_TYPE (TREE_TYPE (one
)),
2800 TREE_TYPE (TREE_TYPE (two
))))
2804 if (!equivalently_constrained (one
, two
))
2810 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current
2811 binding value (possibly with anticipated builtins stripped).
2812 Diagnose conflicts and return updated decl. */
2815 update_binding (cp_binding_level
*level
, cxx_binding
*binding
, tree
*slot
,
2816 tree old
, tree decl
, bool hiding
= false)
2818 tree old_type
= NULL_TREE
;
2819 bool hide_type
= false;
2820 bool hide_value
= false;
2824 old_type
= binding
->type
;
2825 hide_type
= HIDDEN_TYPE_BINDING_P (binding
);
2827 hide_value
= hide_type
, hide_type
= false;
2829 else if (STAT_HACK_P (*slot
))
2831 old_type
= STAT_TYPE (*slot
);
2832 hide_type
= STAT_TYPE_HIDDEN_P (*slot
);
2833 hide_value
= STAT_DECL_HIDDEN_P (*slot
);
2837 tree to_type
= old_type
;
2838 bool local_overload
= false;
2840 gcc_assert (!level
|| level
->kind
== sk_namespace
? !binding
2841 : level
->kind
!= sk_class
&& !slot
);
2843 if (old
== error_mark_node
)
2846 if (DECL_IMPLICIT_TYPEDEF_P (decl
))
2848 /* Pushing an artificial decl. We should not find another
2849 artificial decl here already -- lookup_elaborated_type will
2850 have already found it. */
2851 gcc_checking_assert (!to_type
2852 && !(old
&& DECL_IMPLICIT_TYPEDEF_P (old
)));
2856 /* Put DECL into the type slot. */
2857 gcc_checking_assert (!to_type
);
2863 hide_value
= hiding
;
2868 if (old
&& DECL_IMPLICIT_TYPEDEF_P (old
))
2870 /* OLD is an implicit typedef. Move it to to_type. */
2871 gcc_checking_assert (!to_type
);
2874 hide_type
= hide_value
;
2879 if (DECL_DECLARES_FUNCTION_P (decl
))
2883 else if (OVL_P (old
))
2885 for (ovl_iterator
iter (old
); iter
; ++iter
)
2889 if (iter
.using_p () && matching_fn_p (fn
, decl
))
2891 gcc_checking_assert (!iter
.hidden_p ());
2892 /* If a function declaration in namespace scope or
2893 block scope has the same name and the same
2894 parameter-type- list (8.3.5) as a function
2895 introduced by a using-declaration, and the
2896 declarations do not declare the same function,
2897 the program is ill-formed. [namespace.udecl]/14 */
2898 if (tree match
= duplicate_decls (decl
, fn
, hiding
))
2901 /* FIXME: To preserve existing error behavior, we
2902 still push the decl. This might change. */
2903 diagnose_name_conflict (decl
, fn
);
2910 if (to_type
!= old_type
2912 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type
))
2913 && !(DECL_IN_SYSTEM_HEADER (decl
)
2914 && DECL_IN_SYSTEM_HEADER (to_type
)))
2915 warning (OPT_Wshadow
, "%q#D hides constructor for %q#D",
2918 local_overload
= old
&& level
&& level
->kind
!= sk_namespace
;
2919 to_val
= ovl_insert (decl
, old
, -int (hiding
));
2923 if (TREE_CODE (old
) != TREE_CODE (decl
))
2924 /* Different kinds of decls conflict. */
2926 else if (TREE_CODE (old
) == TYPE_DECL
)
2928 if (same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
2929 /* Two type decls to the same type. Do nothing. */
2934 else if (TREE_CODE (old
) == NAMESPACE_DECL
)
2936 /* Two maybe-aliased namespaces. If they're to the same target
2937 namespace, that's ok. */
2938 if (ORIGINAL_NAMESPACE (old
) != ORIGINAL_NAMESPACE (decl
))
2941 /* The new one must be an alias at this point. */
2942 gcc_assert (DECL_NAMESPACE_ALIAS (decl
));
2945 else if (TREE_CODE (old
) == VAR_DECL
)
2947 /* There can be two block-scope declarations of the same
2948 variable, so long as they are `extern' declarations. */
2949 if (!DECL_EXTERNAL (old
) || !DECL_EXTERNAL (decl
))
2951 else if (tree match
= duplicate_decls (decl
, old
))
2953 gcc_checking_assert (!hide_value
&& !hiding
);
2962 diagnose_name_conflict (decl
, old
);
2974 gcc_checking_assert (binding
->value
&& OVL_P (binding
->value
));
2975 update_local_overload (binding
, to_val
);
2978 && !(TREE_CODE (decl
) == NAMESPACE_DECL
2979 && !DECL_NAMESPACE_ALIAS (decl
)))
2980 /* Don't add namespaces here. They're done in
2982 add_decl_to_level (level
, decl
);
2986 if (STAT_HACK_P (*slot
))
2988 STAT_TYPE (*slot
) = to_type
;
2989 STAT_DECL (*slot
) = to_val
;
2990 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2991 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
2993 else if (to_type
|| hide_value
)
2995 *slot
= stat_hack (to_val
, to_type
);
2996 STAT_TYPE_HIDDEN_P (*slot
) = hide_type
;
2997 STAT_DECL_HIDDEN_P (*slot
) = hide_value
;
3001 gcc_checking_assert (!hide_type
);
3007 binding
->type
= to_type
;
3008 binding
->value
= to_val
;
3009 HIDDEN_TYPE_BINDING_P (binding
) = hide_type
|| hide_value
;
3016 /* Table of identifiers to extern C declarations (or LISTS thereof). */
3018 static GTY(()) hash_table
<named_decl_hash
> *extern_c_decls
;
3020 /* DECL has C linkage. If we have an existing instance, make sure the
3021 new one is compatible. Make sure it has the same exception
3022 specification [7.5, 7.6]. Add DECL to the map. */
3025 check_extern_c_conflict (tree decl
)
3027 /* Ignore artificial or system header decls. */
3028 if (DECL_ARTIFICIAL (decl
) || DECL_IN_SYSTEM_HEADER (decl
))
3031 /* This only applies to decls at namespace scope. */
3032 if (!DECL_NAMESPACE_SCOPE_P (decl
))
3035 if (!extern_c_decls
)
3036 extern_c_decls
= hash_table
<named_decl_hash
>::create_ggc (127);
3038 tree
*slot
= extern_c_decls
3039 ->find_slot_with_hash (DECL_NAME (decl
),
3040 IDENTIFIER_HASH_VALUE (DECL_NAME (decl
)), INSERT
);
3041 if (tree old
= *slot
)
3043 if (TREE_CODE (old
) == OVERLOAD
)
3044 old
= OVL_FUNCTION (old
);
3047 if (DECL_CONTEXT (old
) == DECL_CONTEXT (decl
))
3048 ; /* If they're in the same context, we'll have already complained
3049 about a (possible) mismatch, when inserting the decl. */
3050 else if (!decls_match (decl
, old
))
3052 else if (TREE_CODE (decl
) == FUNCTION_DECL
3053 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old
)),
3054 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
3057 else if (DECL_ASSEMBLER_NAME_SET_P (old
))
3058 SET_DECL_ASSEMBLER_NAME (decl
, DECL_ASSEMBLER_NAME (old
));
3062 auto_diagnostic_group d
;
3063 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
3064 "conflicting C language linkage declaration %q#D", decl
);
3065 inform (DECL_SOURCE_LOCATION (old
),
3066 "previous declaration %q#D", old
);
3068 inform (DECL_SOURCE_LOCATION (decl
),
3069 "due to different exception specifications");
3074 /* The hash table expects OVERLOADS, so construct one with
3075 OLD as both the function and the chain. This allocate
3076 an excess OVERLOAD node, but it's rare to have multiple
3077 extern "C" decls of the same name. And we save
3078 complicating the hash table logic (which is used
3080 *slot
= ovl_make (old
, old
);
3082 slot
= &OVL_CHAIN (*slot
);
3084 /* Chain it on for c_linkage_binding's use. */
3085 *slot
= tree_cons (NULL_TREE
, decl
, *slot
);
3092 /* Returns a list of C-linkage decls with the name NAME. Used in
3093 c-family/c-pragma.c to implement redefine_extname pragma. */
3096 c_linkage_bindings (tree name
)
3099 if (tree
*slot
= extern_c_decls
3100 ->find_slot_with_hash (name
, IDENTIFIER_HASH_VALUE (name
), NO_INSERT
))
3102 tree result
= *slot
;
3103 if (TREE_CODE (result
) == OVERLOAD
)
3104 result
= OVL_CHAIN (result
);
3111 /* Subroutine of check_local_shadow. */
3114 inform_shadowed (tree shadowed
)
3116 inform (DECL_SOURCE_LOCATION (shadowed
),
3117 "shadowed declaration is here");
3120 /* DECL is being declared at a local scope. Emit suitable shadow
3124 check_local_shadow (tree decl
)
3126 /* Don't complain about the parms we push and then pop
3127 while tentatively parsing a function declarator. */
3128 if (TREE_CODE (decl
) == PARM_DECL
&& !DECL_CONTEXT (decl
))
3131 /* External decls are something else. */
3132 if (DECL_EXTERNAL (decl
))
3135 tree old
= NULL_TREE
;
3136 cp_binding_level
*old_scope
= NULL
;
3137 if (cxx_binding
*binding
= outer_binding (DECL_NAME (decl
), NULL
, true))
3139 old
= binding
->value
;
3140 old_scope
= binding
->scope
;
3144 && (TREE_CODE (old
) == PARM_DECL
3146 || (TREE_CODE (old
) == TYPE_DECL
3147 && (!DECL_ARTIFICIAL (old
)
3148 || TREE_CODE (decl
) == TYPE_DECL
)))
3149 && DECL_FUNCTION_SCOPE_P (old
)
3150 && (!DECL_ARTIFICIAL (decl
)
3151 || is_capture_proxy (decl
)
3152 || DECL_IMPLICIT_TYPEDEF_P (decl
)
3153 || (VAR_P (decl
) && DECL_ANON_UNION_VAR_P (decl
))))
3155 /* DECL shadows a local thing possibly of interest. */
3157 /* DR 2211: check that captures and parameters
3158 do not have the same name. */
3159 if (is_capture_proxy (decl
))
3161 if (current_lambda_expr ()
3162 && DECL_CONTEXT (old
) == lambda_function (current_lambda_expr ())
3163 && TREE_CODE (old
) == PARM_DECL
3164 && DECL_NAME (decl
) != this_identifier
)
3166 error_at (DECL_SOURCE_LOCATION (old
),
3167 "lambda parameter %qD "
3168 "previously declared as a capture", old
);
3172 /* Don't complain if it's from an enclosing function. */
3173 else if (DECL_CONTEXT (old
) == current_function_decl
3174 && TREE_CODE (decl
) != PARM_DECL
3175 && TREE_CODE (old
) == PARM_DECL
)
3177 /* Go to where the parms should be and see if we find
3179 cp_binding_level
*b
= current_binding_level
->level_chain
;
3181 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl
))
3182 /* Skip the ctor/dtor cleanup level. */
3185 /* [basic.scope.param] A parameter name shall not be redeclared
3186 in the outermost block of the function definition. */
3187 if (b
->kind
== sk_function_parms
)
3189 error_at (DECL_SOURCE_LOCATION (decl
),
3190 "declaration of %q#D shadows a parameter", decl
);
3191 inform (DECL_SOURCE_LOCATION (old
),
3192 "%q#D previously declared here", old
);
3197 /* The local structure or class can't use parameters of
3198 the containing function anyway. */
3199 if (DECL_CONTEXT (old
) != current_function_decl
)
3201 for (cp_binding_level
*scope
= current_binding_level
;
3202 scope
!= old_scope
; scope
= scope
->level_chain
)
3203 if (scope
->kind
== sk_class
3204 && !LAMBDA_TYPE_P (scope
->this_entity
))
3207 /* Error if redeclaring a local declared in a
3208 init-statement or in the condition of an if or
3209 switch statement when the new declaration is in the
3210 outermost block of the controlled statement.
3211 Redeclaring a variable from a for or while condition is
3212 detected elsewhere. */
3213 else if (VAR_P (old
)
3214 && old_scope
== current_binding_level
->level_chain
3215 && (old_scope
->kind
== sk_cond
|| old_scope
->kind
== sk_for
))
3217 auto_diagnostic_group d
;
3218 error_at (DECL_SOURCE_LOCATION (decl
),
3219 "redeclaration of %q#D", decl
);
3220 inform (DECL_SOURCE_LOCATION (old
),
3221 "%q#D previously declared here", old
);
3225 3.3.3/3: The name declared in an exception-declaration (...)
3226 shall not be redeclared in the outermost block of the handler.
3227 3.3.3/2: A parameter name shall not be redeclared (...) in
3228 the outermost block of any handler associated with a
3230 3.4.1/15: The function parameter names shall not be redeclared
3231 in the exception-declaration nor in the outermost block of a
3232 handler for the function-try-block. */
3233 else if ((TREE_CODE (old
) == VAR_DECL
3234 && old_scope
== current_binding_level
->level_chain
3235 && old_scope
->kind
== sk_catch
)
3236 || (TREE_CODE (old
) == PARM_DECL
3237 && (current_binding_level
->kind
== sk_catch
3238 || current_binding_level
->level_chain
->kind
== sk_catch
)
3239 && in_function_try_handler
))
3241 auto_diagnostic_group d
;
3242 if (permerror (DECL_SOURCE_LOCATION (decl
),
3243 "redeclaration of %q#D", decl
))
3244 inform (DECL_SOURCE_LOCATION (old
),
3245 "%q#D previously declared here", old
);
3249 /* If '-Wshadow=compatible-local' is specified without other
3250 -Wshadow= flags, we will warn only when the type of the
3251 shadowing variable (DECL) can be converted to that of the
3252 shadowed parameter (OLD_LOCAL). The reason why we only check
3253 if DECL's type can be converted to OLD_LOCAL's type (but not the
3254 other way around) is because when users accidentally shadow a
3255 parameter, more than often they would use the variable
3256 thinking (mistakenly) it's still the parameter. It would be
3257 rare that users would use the variable in the place that
3258 expects the parameter but thinking it's a new decl.
3259 If either object is a TYPE_DECL, '-Wshadow=compatible-local'
3260 warns regardless of whether one of the types involved
3261 is a subclass of the other, since that is never okay. */
3263 enum opt_code warning_code
;
3265 warning_code
= OPT_Wshadow
;
3266 else if ((TREE_TYPE (old
)
3268 && same_type_p (TREE_TYPE (old
), TREE_TYPE (decl
)))
3269 || TREE_CODE (decl
) == TYPE_DECL
3270 || TREE_CODE (old
) == TYPE_DECL
3271 || (!dependent_type_p (TREE_TYPE (decl
))
3272 && !dependent_type_p (TREE_TYPE (old
))
3273 /* If the new decl uses auto, we don't yet know
3274 its type (the old type cannot be using auto
3275 at this point, without also being
3276 dependent). This is an indication we're
3277 (now) doing the shadow checking too
3279 && !type_uses_auto (TREE_TYPE (decl
))
3280 && can_convert_arg (TREE_TYPE (old
), TREE_TYPE (decl
),
3281 decl
, LOOKUP_IMPLICIT
, tf_none
)))
3282 warning_code
= OPT_Wshadow_compatible_local
;
3284 warning_code
= OPT_Wshadow_local
;
3287 if (TREE_CODE (old
) == PARM_DECL
)
3288 msg
= "declaration of %q#D shadows a parameter";
3289 else if (is_capture_proxy (old
))
3290 msg
= "declaration of %qD shadows a lambda capture";
3292 msg
= "declaration of %qD shadows a previous local";
3294 auto_diagnostic_group d
;
3295 if (warning_at (DECL_SOURCE_LOCATION (decl
), warning_code
, msg
, decl
))
3296 inform_shadowed (old
);
3303 /* Don't warn for artificial things that are not implicit typedefs. */
3304 if (DECL_ARTIFICIAL (decl
) && !DECL_IMPLICIT_TYPEDEF_P (decl
))
3307 if (nonlambda_method_basetype ())
3308 if (tree member
= lookup_member (current_nonlambda_class_type (),
3309 DECL_NAME (decl
), /*protect=*/0,
3310 /*want_type=*/false, tf_warning_or_error
))
3312 member
= MAYBE_BASELINK_FUNCTIONS (member
);
3314 /* Warn if a variable shadows a non-function, or the variable
3315 is a function or a pointer-to-function. */
3317 || TREE_CODE (decl
) == FUNCTION_DECL
3318 || TYPE_PTRFN_P (TREE_TYPE (decl
))
3319 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl
)))
3321 auto_diagnostic_group d
;
3322 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
3323 "declaration of %qD shadows a member of %qT",
3324 decl
, current_nonlambda_class_type ())
3326 inform_shadowed (member
);
3331 /* Now look for a namespace shadow. */
3332 old
= find_namespace_value (current_namespace
, DECL_NAME (decl
));
3335 || (TREE_CODE (old
) == TYPE_DECL
3336 && (!DECL_ARTIFICIAL (old
)
3337 || TREE_CODE (decl
) == TYPE_DECL
)))
3338 && !instantiating_current_function_p ())
3339 /* XXX shadow warnings in outer-more namespaces */
3341 auto_diagnostic_group d
;
3342 if (warning_at (DECL_SOURCE_LOCATION (decl
), OPT_Wshadow
,
3343 "declaration of %qD shadows a global declaration",
3345 inform_shadowed (old
);
3352 /* DECL is being pushed inside function CTX. Set its context, if
3356 set_decl_context_in_fn (tree ctx
, tree decl
)
3358 if (TREE_CODE (decl
) == FUNCTION_DECL
3359 || (VAR_P (decl
) && DECL_EXTERNAL (decl
)))
3360 /* Make sure local externs are marked as such. OMP UDRs really
3361 are nested functions. */
3362 gcc_checking_assert (DECL_LOCAL_DECL_P (decl
)
3363 && (DECL_NAMESPACE_SCOPE_P (decl
)
3364 || (TREE_CODE (decl
) == FUNCTION_DECL
3365 && DECL_OMP_DECLARE_REDUCTION_P (decl
))));
3367 if (!DECL_CONTEXT (decl
)
3368 /* When parsing the parameter list of a function declarator,
3369 don't set DECL_CONTEXT to an enclosing function. When we
3370 push the PARM_DECLs in order to process the function body,
3371 current_binding_level->this_entity will be set. */
3372 && !(TREE_CODE (decl
) == PARM_DECL
3373 && current_binding_level
->kind
== sk_function_parms
3374 && current_binding_level
->this_entity
== NULL
))
3375 DECL_CONTEXT (decl
) = ctx
;
3378 /* DECL is a local extern decl. Find or create the namespace-scope
3379 decl that it aliases. Also, determines the linkage of DECL. */
3382 push_local_extern_decl_alias (tree decl
)
3384 if (dependent_type_p (TREE_TYPE (decl
)))
3386 /* EH specs were not part of the function type prior to c++17, but
3387 we still can't go pushing dependent eh specs into the namespace. */
3388 if (cxx_dialect
< cxx17
3389 && TREE_CODE (decl
) == FUNCTION_DECL
3390 && (value_dependent_expression_p
3391 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)))))
3394 gcc_checking_assert (!DECL_LANG_SPECIFIC (decl
)
3395 || !DECL_TEMPLATE_INFO (decl
));
3396 if (DECL_LANG_SPECIFIC (decl
) && DECL_LOCAL_DECL_ALIAS (decl
))
3397 /* We're instantiating a non-dependent local decl, it already
3401 tree alias
= NULL_TREE
;
3403 if (DECL_SIZE (decl
) && !TREE_CONSTANT (DECL_SIZE (decl
)))
3404 /* Do not let a VLA creep into a namespace. Diagnostic will be
3405 emitted in layout_var_decl later. */
3406 alias
= error_mark_node
;
3409 /* First look for a decl that matches. */
3410 tree ns
= CP_DECL_CONTEXT (decl
);
3411 tree binding
= find_namespace_value (ns
, DECL_NAME (decl
));
3413 if (binding
&& TREE_CODE (binding
) != TREE_LIST
)
3414 for (ovl_iterator
iter (binding
); iter
; ++iter
)
3415 if (decls_match (*iter
, decl
))
3423 /* No existing namespace-scope decl. Make one. */
3424 alias
= copy_decl (decl
);
3425 if (TREE_CODE (alias
) == FUNCTION_DECL
)
3427 /* Recontextualize the parms. */
3428 for (tree
*chain
= &DECL_ARGUMENTS (alias
);
3429 *chain
; chain
= &DECL_CHAIN (*chain
))
3431 *chain
= copy_decl (*chain
);
3432 DECL_CONTEXT (*chain
) = alias
;
3435 tree type
= TREE_TYPE (alias
);
3436 for (tree args
= TYPE_ARG_TYPES (type
);
3437 args
; args
= TREE_CHAIN (args
))
3438 if (TREE_PURPOSE (args
))
3440 /* There are default args. Lose them. */
3441 tree nargs
= NULL_TREE
;
3442 tree
*chain
= &nargs
;
3443 for (args
= TYPE_ARG_TYPES (type
);
3444 args
; args
= TREE_CHAIN (args
))
3445 if (args
== void_list_node
)
3453 = build_tree_list (NULL_TREE
, TREE_VALUE (args
));
3454 chain
= &TREE_CHAIN (*chain
);
3457 tree fn_type
= build_function_type (TREE_TYPE (type
), nargs
);
3459 fn_type
= apply_memfn_quals
3460 (fn_type
, type_memfn_quals (type
));
3462 fn_type
= build_cp_fntype_variant
3463 (fn_type
, type_memfn_rqual (type
),
3464 TYPE_RAISES_EXCEPTIONS (type
),
3465 TYPE_HAS_LATE_RETURN_TYPE (type
));
3467 TREE_TYPE (alias
) = fn_type
;
3472 /* This is the real thing. */
3473 DECL_LOCAL_DECL_P (alias
) = false;
3475 /* Expected default linkage is from the namespace. */
3476 TREE_PUBLIC (alias
) = TREE_PUBLIC (ns
);
3477 push_nested_namespace (ns
);
3478 alias
= do_pushdecl (alias
, /* hiding= */true);
3479 pop_nested_namespace (ns
);
3483 retrofit_lang_decl (decl
);
3484 DECL_LOCAL_DECL_ALIAS (decl
) = alias
;
3487 /* NS needs to be exported, mark it and all its parents as exported. */
3490 implicitly_export_namespace (tree ns
)
3492 while (!DECL_MODULE_EXPORT_P (ns
))
3494 DECL_MODULE_EXPORT_P (ns
) = true;
3495 ns
= CP_DECL_CONTEXT (ns
);
3499 /* DECL has just been bound at LEVEL. finish up the bookkeeping. */
3502 newbinding_bookkeeping (tree name
, tree decl
, cp_binding_level
*level
)
3504 if (TREE_CODE (decl
) == TYPE_DECL
)
3506 tree type
= TREE_TYPE (decl
);
3508 if (type
!= error_mark_node
)
3510 if (TYPE_NAME (type
) != decl
)
3511 set_underlying_type (decl
);
3513 set_identifier_type_value_with_scope (name
, decl
, level
);
3515 if (level
->kind
!= sk_namespace
3516 && !instantiating_current_function_p ())
3517 /* This is a locally defined typedef in a function that
3518 is not a template instantation, record it to implement
3519 -Wunused-local-typedefs. */
3520 record_locally_defined_typedef (decl
);
3525 if (VAR_P (decl
) && !DECL_LOCAL_DECL_P (decl
))
3526 maybe_register_incomplete_var (decl
);
3528 if (VAR_OR_FUNCTION_DECL_P (decl
)
3529 && DECL_EXTERN_C_P (decl
))
3530 check_extern_c_conflict (decl
);
3534 /* DECL is a global or module-purview entity. If it has non-internal
3535 linkage, and we have a module vector, record it in the appropriate
3536 slot. We have already checked for duplicates. */
3539 maybe_record_mergeable_decl (tree
*slot
, tree name
, tree decl
)
3541 if (TREE_CODE (*slot
) != BINDING_VECTOR
)
3544 if (!TREE_PUBLIC (CP_DECL_CONTEXT (decl
)))
3545 /* Member of internal namespace. */
3548 tree not_tmpl
= STRIP_TEMPLATE (decl
);
3549 if ((TREE_CODE (not_tmpl
) == FUNCTION_DECL
3550 || TREE_CODE (not_tmpl
) == VAR_DECL
)
3551 && DECL_THIS_STATIC (not_tmpl
))
3552 /* Internal linkage. */
3555 bool partition
= named_module_p ();
3556 tree
*gslot
= get_fixed_binding_slot
3557 (slot
, name
, partition
? BINDING_SLOT_PARTITION
: BINDING_SLOT_GLOBAL
, true);
3562 = BINDING_VECTOR_CLUSTER (*gslot
, 0).slots
[BINDING_SLOT_CURRENT
];
3564 if (!STAT_HACK_P (tree (orig
)))
3565 orig
= stat_hack (tree (orig
));
3567 MODULE_BINDING_GLOBAL_P (tree (orig
)) = true;
3570 add_mergeable_namespace_entity (gslot
, decl
);
3573 /* DECL is being pushed. Check whether it hides or ambiguates
3574 something seen as an import. This include decls seen in our own
3575 interface, which is OK. Also, check for merging a
3576 global/partition decl. */
3579 check_module_override (tree decl
, tree mvec
, bool hiding
,
3580 tree scope
, tree name
)
3582 bitmap imports
= get_import_bitmap ();
3583 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (mvec
);
3584 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (mvec
);
3586 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
3592 for (; ix
--; cluster
++)
3593 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
3595 /* Are we importing this module? */
3596 if (cluster
->indices
[jx
].span
!= 1)
3598 if (!cluster
->indices
[jx
].base
)
3600 if (!bitmap_bit_p (imports
, cluster
->indices
[jx
].base
))
3603 if (cluster
->slots
[jx
].is_lazy ())
3605 gcc_assert (cluster
->indices
[jx
].span
== 1);
3606 lazy_load_binding (cluster
->indices
[jx
].base
,
3607 scope
, name
, &cluster
->slots
[jx
]);
3609 tree bind
= cluster
->slots
[jx
];
3611 /* Errors could cause there to be nothing. */
3614 if (STAT_HACK_P (bind
))
3615 /* We do not have to check STAT_TYPE here, the xref_tag
3616 machinery deals with that problem. */
3617 bind
= STAT_VISIBLE (bind
);
3619 for (ovl_iterator
iter (bind
); iter
; ++iter
)
3620 if (iter
.using_p ())
3622 else if (tree match
= duplicate_decls (decl
, *iter
, hiding
))
3624 if (TREE_CODE (match
) == TYPE_DECL
)
3625 /* The IDENTIFIER will have the type referring to the
3626 now-smashed TYPE_DECL, because ...? Reset it. */
3627 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3633 if (TREE_PUBLIC (scope
) && TREE_PUBLIC (decl
) && !not_module_p ()
3634 /* Namespaces are dealt with specially in
3635 make_namespace_finish. */
3636 && !(TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
)))
3638 /* Look in the appropriate mergeable decl slot. */
3639 tree mergeable
= NULL_TREE
;
3640 if (named_module_p ())
3641 mergeable
= BINDING_VECTOR_CLUSTER (mvec
, BINDING_SLOT_PARTITION
3642 / BINDING_VECTOR_SLOTS_PER_CLUSTER
)
3643 .slots
[BINDING_SLOT_PARTITION
% BINDING_VECTOR_SLOTS_PER_CLUSTER
];
3645 mergeable
= BINDING_VECTOR_CLUSTER (mvec
, 0).slots
[BINDING_SLOT_GLOBAL
];
3647 for (ovl_iterator
iter (mergeable
); iter
; ++iter
)
3651 if (duplicate_decls (decl
, match
, hiding
))
3653 if (TREE_CODE (match
) == TYPE_DECL
)
3654 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3663 /* Record DECL as belonging to the current lexical scope. Check for
3664 errors (such as an incompatible declaration for the same name
3665 already seen in the same scope). IS_FRIEND is true if DECL is
3666 declared as a friend.
3668 Returns either DECL or an old decl for the same name. If an old
3669 decl is returned, it may have been smashed to agree with what DECL
3673 do_pushdecl (tree decl
, bool hiding
)
3675 if (decl
== error_mark_node
)
3676 return error_mark_node
;
3678 if (!DECL_TEMPLATE_PARM_P (decl
) && current_function_decl
&& !hiding
)
3679 set_decl_context_in_fn (current_function_decl
, decl
);
3681 /* The binding level we will be pushing into. During local class
3682 pushing, we want to push to the containing scope. */
3683 cp_binding_level
*level
= current_binding_level
;
3684 while (level
->kind
== sk_class
3685 || level
->kind
== sk_cleanup
)
3686 level
= level
->level_chain
;
3688 /* An anonymous namespace has a NULL DECL_NAME, but we still want to
3689 insert it. Other NULL-named decls, not so much. */
3690 tree name
= DECL_NAME (decl
);
3691 if (name
? !IDENTIFIER_ANON_P (name
) : TREE_CODE (decl
) == NAMESPACE_DECL
)
3693 cxx_binding
*binding
= NULL
; /* Local scope binding. */
3694 tree ns
= NULL_TREE
; /* Searched namespace. */
3695 tree
*slot
= NULL
; /* Binding slot in namespace. */
3696 tree
*mslot
= NULL
; /* Current module slot in namespace. */
3697 tree old
= NULL_TREE
;
3699 if (level
->kind
== sk_namespace
)
3701 /* We look in the decl's namespace for an existing
3702 declaration, even though we push into the current
3704 ns
= (DECL_NAMESPACE_SCOPE_P (decl
)
3705 ? CP_DECL_CONTEXT (decl
) : current_namespace
);
3706 /* Create the binding, if this is current namespace, because
3707 that's where we'll be pushing anyway. */
3708 slot
= find_namespace_slot (ns
, name
, ns
== current_namespace
);
3711 mslot
= get_fixed_binding_slot (slot
, name
, BINDING_SLOT_CURRENT
,
3712 ns
== current_namespace
);
3713 old
= MAYBE_STAT_DECL (*mslot
);
3718 binding
= find_local_binding (level
, name
);
3720 old
= binding
->value
;
3723 if (old
== error_mark_node
)
3726 for (ovl_iterator
iter (old
); iter
; ++iter
)
3727 if (iter
.using_p ())
3728 ; /* Ignore using decls here. */
3729 else if (iter
.hidden_p ()
3730 && DECL_LANG_SPECIFIC (*iter
)
3731 && DECL_MODULE_IMPORT_P (*iter
))
3732 ; /* An undeclared builtin imported from elsewhere. */
3734 = duplicate_decls (decl
, *iter
, hiding
, iter
.hidden_p ()))
3736 if (match
== error_mark_node
)
3738 else if (TREE_CODE (match
) == TYPE_DECL
)
3739 /* The IDENTIFIER will have the type referring to the
3740 now-smashed TYPE_DECL, because ...? Reset it. */
3741 SET_IDENTIFIER_TYPE_VALUE (name
, TREE_TYPE (match
));
3742 else if (iter
.hidden_p () && !hiding
)
3744 /* Unhiding a previously hidden decl. */
3745 tree head
= iter
.reveal_node (old
);
3748 gcc_checking_assert (ns
);
3749 if (STAT_HACK_P (*slot
))
3750 STAT_DECL (*slot
) = head
;
3754 if (DECL_EXTERN_C_P (match
))
3755 /* We need to check and register the decl now. */
3756 check_extern_c_conflict (match
);
3758 else if (slot
&& !hiding
3759 && STAT_HACK_P (*slot
) && STAT_DECL_HIDDEN_P (*slot
))
3761 /* Unhide the non-function. */
3762 gcc_checking_assert (old
== match
);
3763 if (!STAT_TYPE (*slot
))
3766 STAT_DECL (*slot
) = match
;
3771 /* Check for redeclaring an import. */
3772 if (slot
&& *slot
&& TREE_CODE (*slot
) == BINDING_VECTOR
)
3774 = check_module_override (decl
, *slot
, hiding
, ns
, name
))
3776 if (match
== error_mark_node
)
3779 /* We found a decl in an interface, push it into this
3781 decl
= update_binding (NULL
, binding
, mslot
, old
,
3784 if (match
== decl
&& DECL_MODULE_EXPORT_P (decl
)
3785 && !DECL_MODULE_EXPORT_P (level
->this_entity
))
3786 implicitly_export_namespace (level
->this_entity
);
3791 /* We are pushing a new decl. */
3793 /* Skip a hidden builtin we failed to match already. There can
3795 if (old
&& anticipated_builtin_p (old
))
3796 old
= OVL_CHAIN (old
);
3798 check_template_shadow (decl
);
3800 if (DECL_DECLARES_FUNCTION_P (decl
))
3802 check_default_args (decl
);
3806 if (level
->kind
!= sk_namespace
)
3808 /* In a local class, a friend function declaration must
3809 find a matching decl in the innermost non-class scope.
3810 [class.friend/11] */
3811 error_at (DECL_SOURCE_LOCATION (decl
),
3812 "friend declaration %qD in local class without "
3813 "prior local declaration", decl
);
3814 /* Don't attempt to push it. */
3815 return error_mark_node
;
3820 if (level
->kind
!= sk_namespace
)
3822 check_local_shadow (decl
);
3824 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
3825 /* A local namespace alias. */
3826 set_identifier_type_value_with_scope (name
, NULL_TREE
, level
);
3829 binding
= create_local_binding (level
, name
);
3833 ns
= current_namespace
;
3834 slot
= find_namespace_slot (ns
, name
, true);
3835 mslot
= get_fixed_binding_slot (slot
, name
, BINDING_SLOT_CURRENT
, true);
3836 /* Update OLD to reflect the namespace we're going to be
3838 old
= MAYBE_STAT_DECL (*mslot
);
3841 old
= update_binding (level
, binding
, mslot
, old
, decl
, hiding
);
3844 /* An existing decl matched, use it. */
3848 newbinding_bookkeeping (name
, decl
, level
);
3850 if (VAR_OR_FUNCTION_DECL_P (decl
)
3851 && DECL_LOCAL_DECL_P (decl
)
3852 && TREE_CODE (CP_DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
3853 push_local_extern_decl_alias (decl
);
3855 if (level
->kind
== sk_namespace
3856 && TREE_PUBLIC (level
->this_entity
))
3858 if (TREE_CODE (decl
) != CONST_DECL
3859 && DECL_MODULE_EXPORT_P (decl
)
3860 && !DECL_MODULE_EXPORT_P (level
->this_entity
))
3861 implicitly_export_namespace (level
->this_entity
);
3863 if (!not_module_p ())
3864 maybe_record_mergeable_decl (slot
, name
, decl
);
3869 add_decl_to_level (level
, decl
);
3874 /* Record a decl-node X as belonging to the current lexical scope.
3875 The new binding is hidden if HIDING is true (an anticipated builtin
3876 or hidden friend). */
3879 pushdecl (tree x
, bool hiding
)
3881 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
3882 tree ret
= do_pushdecl (x
, hiding
);
3883 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
3887 /* A mergeable entity is being loaded into namespace NS slot NAME.
3888 Create and return the appropriate vector slot for that. Either a
3889 GMF slot or a module-specific one. */
3892 mergeable_namespace_slots (tree ns
, tree name
, bool is_global
, tree
*vec
)
3894 tree
*mslot
= find_namespace_slot (ns
, name
, true);
3895 tree
*vslot
= get_fixed_binding_slot
3896 (mslot
, name
, is_global
? BINDING_SLOT_GLOBAL
: BINDING_SLOT_PARTITION
, true);
3898 gcc_checking_assert (TREE_CODE (*mslot
) == BINDING_VECTOR
);
3904 /* DECL is a new mergeable namespace-scope decl. Add it to the
3905 mergeable entities on GSLOT. */
3908 add_mergeable_namespace_entity (tree
*gslot
, tree decl
)
3910 *gslot
= ovl_make (decl
, *gslot
);
3913 /* A mergeable entity of KLASS called NAME is being loaded. Return
3914 the set of things it could be. All such non-as_base classes have
3915 been given a member vec. */
3918 lookup_class_binding (tree klass
, tree name
)
3920 tree found
= NULL_TREE
;
3922 if (!COMPLETE_TYPE_P (klass
))
3924 else if (TYPE_LANG_SPECIFIC (klass
))
3926 vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (klass
);
3928 found
= member_vec_binary_search (member_vec
, name
);
3931 else if (STAT_HACK_P (found
))
3932 /* Rearrange the stat hack so that we don't need to expose that
3934 found
= ovl_make (STAT_TYPE (found
), STAT_DECL (found
));
3935 else if (IDENTIFIER_CONV_OP_P (name
))
3937 gcc_checking_assert (name
== conv_op_identifier
);
3938 found
= OVL_CHAIN (found
);
3943 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass
)
3944 || TYPE_PTRMEMFUNC_P (klass
));
3945 found
= fields_linear_search (klass
, name
, false);
3951 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
3952 for all decls of the current module. When partitions are involved,
3953 decls might be mentioned more than once. */
3956 walk_module_binding (tree binding
, bitmap partitions
,
3957 bool (*callback
) (tree decl
, WMB_Flags
, void *data
),
3960 // FIXME: We don't quite deal with using decls naming stat hack
3961 // type. Also using decls exporting something from the same scope.
3962 tree current
= binding
;
3965 if (TREE_CODE (binding
) == BINDING_VECTOR
)
3966 current
= BINDING_VECTOR_CLUSTER (binding
, 0).slots
[BINDING_SLOT_CURRENT
];
3968 bool decl_hidden
= false;
3969 if (tree type
= MAYBE_STAT_TYPE (current
))
3971 WMB_Flags flags
= WMB_None
;
3972 if (STAT_TYPE_HIDDEN_P (current
))
3973 flags
= WMB_Flags (flags
| WMB_Hidden
);
3974 count
+= callback (type
, flags
, data
);
3975 decl_hidden
= STAT_DECL_HIDDEN_P (current
);
3978 for (ovl_iterator
iter (MAYBE_STAT_DECL (current
)); iter
; ++iter
)
3980 if (iter
.hidden_p ())
3982 if (!(decl_hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)))
3984 WMB_Flags flags
= WMB_None
;
3986 flags
= WMB_Flags (flags
| WMB_Hidden
);
3987 if (iter
.using_p ())
3989 flags
= WMB_Flags (flags
| WMB_Using
);
3990 if (iter
.exporting_p ())
3991 flags
= WMB_Flags (flags
| WMB_Export
);
3993 count
+= callback (*iter
, flags
, data
);
3995 decl_hidden
= false;
3998 if (partitions
&& TREE_CODE (binding
) == BINDING_VECTOR
)
4000 /* Process partition slots. */
4001 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
4002 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
4003 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
4009 bool maybe_dups
= BINDING_VECTOR_PARTITION_DUPS_P (binding
);
4011 for (; ix
--; cluster
++)
4012 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
4013 if (!cluster
->slots
[jx
].is_lazy ())
4014 if (tree bind
= cluster
->slots
[jx
])
4016 if (TREE_CODE (bind
) == NAMESPACE_DECL
4017 && !DECL_NAMESPACE_ALIAS (bind
))
4019 if (unsigned base
= cluster
->indices
[jx
].base
)
4020 if (unsigned span
= cluster
->indices
[jx
].span
)
4022 if (bitmap_bit_p (partitions
, base
))
4024 while (++base
, --span
);
4025 /* Not a partition's namespace. */
4029 WMB_Flags flags
= WMB_None
;
4031 flags
= WMB_Flags (flags
| WMB_Dups
);
4032 count
+= callback (bind
, flags
, data
);
4034 else if (STAT_HACK_P (bind
) && MODULE_BINDING_PARTITION_P (bind
))
4036 if (tree btype
= STAT_TYPE (bind
))
4038 WMB_Flags flags
= WMB_None
;
4040 flags
= WMB_Flags (flags
| WMB_Dups
);
4041 if (STAT_TYPE_HIDDEN_P (bind
))
4042 flags
= WMB_Flags (flags
| WMB_Hidden
);
4044 count
+= callback (btype
, flags
, data
);
4046 bool hidden
= STAT_DECL_HIDDEN_P (bind
);
4047 for (ovl_iterator
iter (MAYBE_STAT_DECL (STAT_DECL (bind
)));
4050 if (iter
.hidden_p ())
4053 (!(hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)));
4055 WMB_Flags flags
= WMB_None
;
4057 flags
= WMB_Flags (flags
| WMB_Dups
);
4059 flags
= WMB_Flags (flags
| WMB_Hidden
);
4060 if (iter
.using_p ())
4062 flags
= WMB_Flags (flags
| WMB_Using
);
4063 if (iter
.exporting_p ())
4064 flags
= WMB_Flags (flags
| WMB_Export
);
4066 count
+= callback (*iter
, flags
, data
);
4076 /* Imported module MOD has a binding to NS::NAME, stored in section
4080 import_module_binding (tree ns
, tree name
, unsigned mod
, unsigned snum
)
4082 tree
*slot
= find_namespace_slot (ns
, name
, true);
4083 binding_slot
*mslot
= append_imported_binding_slot (slot
, name
, mod
);
4085 if (mslot
->is_lazy () || *mslot
)
4086 /* Oops, something was already there. */
4089 mslot
->set_lazy (snum
);
4093 /* An import of MODULE is binding NS::NAME. There should be no
4094 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4095 is a header_unit (-1) or part of the current module (+1). VALUE
4096 and TYPE are the value and type bindings. VISIBLE are the value
4097 bindings being exported. */
4100 set_module_binding (tree ns
, tree name
, unsigned mod
, int mod_glob
,
4101 tree value
, tree type
, tree visible
)
4104 /* Bogus BMIs could give rise to nothing to bind. */
4107 gcc_assert (TREE_CODE (value
) != NAMESPACE_DECL
4108 || DECL_NAMESPACE_ALIAS (value
));
4109 gcc_checking_assert (mod
);
4111 tree
*slot
= find_namespace_slot (ns
, name
, true);
4112 binding_slot
*mslot
= search_imported_binding_slot (slot
, mod
);
4114 if (!mslot
|| !mslot
->is_lazy ())
4115 /* Again, bogus BMI could give find to missing or already loaded slot. */
4119 if (type
|| visible
!= bind
|| mod_glob
)
4121 bind
= stat_hack (bind
, type
);
4122 STAT_VISIBLE (bind
) = visible
;
4123 if ((mod_glob
> 0 && TREE_PUBLIC (ns
))
4124 || (type
&& DECL_MODULE_EXPORT_P (type
)))
4125 STAT_TYPE_VISIBLE_P (bind
) = true;
4128 /* Note if this is this-module or global binding. */
4130 MODULE_BINDING_PARTITION_P (bind
) = true;
4131 else if (mod_glob
< 0)
4132 MODULE_BINDING_GLOBAL_P (bind
) = true;
4140 note_pending_specializations (tree ns
, tree name
, bool is_header
)
4142 if (tree
*slot
= find_namespace_slot (ns
, name
, false))
4143 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
4146 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec
) = true;
4148 BINDING_VECTOR_PENDING_IS_HEADER_P (vec
) = true;
4150 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
) = true;
4155 load_pending_specializations (tree ns
, tree name
)
4157 tree
*slot
= find_namespace_slot (ns
, name
, false);
4159 if (!slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
4160 || !BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (*slot
))
4164 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec
) = false;
4166 bool do_header
= BINDING_VECTOR_PENDING_IS_HEADER_P (vec
);
4167 bool do_partition
= BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
);
4168 BINDING_VECTOR_PENDING_IS_HEADER_P (vec
) = false;
4169 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
) = false;
4171 gcc_checking_assert (do_header
| do_partition
);
4172 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (vec
);
4173 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (vec
);
4174 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
4180 for (; ix
--; cluster
++)
4181 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
4182 if (cluster
->indices
[jx
].span
4183 && cluster
->slots
[jx
].is_lazy ()
4184 && lazy_specializations_p (cluster
->indices
[jx
].base
,
4185 do_header
, do_partition
))
4186 lazy_load_binding (cluster
->indices
[jx
].base
, ns
, name
,
4187 &cluster
->slots
[jx
]);
4191 add_module_decl (tree ns
, tree name
, tree decl
)
4193 gcc_assert (!DECL_CHAIN (decl
));
4194 add_decl_to_level (NAMESPACE_LEVEL (ns
), decl
);
4195 newbinding_bookkeeping (name
, decl
, NAMESPACE_LEVEL (ns
));
4198 /* Enter DECL into the symbol table, if that's appropriate. Returns
4199 DECL, or a modified version thereof. */
4202 maybe_push_decl (tree decl
)
4204 tree type
= TREE_TYPE (decl
);
4206 /* Add this decl to the current binding level, but not if it comes
4207 from another scope, e.g. a static member variable. TEM may equal
4208 DECL or it may be a previous decl of the same name. */
4209 if (decl
== error_mark_node
4210 || (TREE_CODE (decl
) != PARM_DECL
4211 && DECL_CONTEXT (decl
) != NULL_TREE
4212 /* Definitions of namespace members outside their namespace are
4214 && !DECL_NAMESPACE_SCOPE_P (decl
))
4215 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
4216 || type
== unknown_type_node
4217 /* The declaration of a template specialization does not affect
4218 the functions available for overload resolution, so we do not
4220 || (TREE_CODE (decl
) == FUNCTION_DECL
4221 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
4224 return pushdecl (decl
);
4227 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4228 binding level. If IS_USING is true, DECL got here through a
4229 using-declaration. */
4232 push_local_binding (tree id
, tree decl
, bool is_using
)
4234 /* Skip over any local classes. This makes sense if we call
4235 push_local_binding with a friend decl of a local class. */
4236 cp_binding_level
*b
= innermost_nonclass_level ();
4238 gcc_assert (b
->kind
!= sk_namespace
);
4239 if (find_local_binding (b
, id
))
4241 /* Supplement the existing binding. */
4242 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
4243 /* It didn't work. Something else must be bound at this
4244 level. Do not add DECL to the list of things to pop
4249 /* Create a new binding. */
4250 push_binding (id
, decl
, b
);
4252 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
4253 /* We must put the OVERLOAD or using into a TREE_LIST since we
4254 cannot use the decl's chain itself. */
4255 decl
= build_tree_list (id
, decl
);
4257 /* And put DECL on the list of things declared by the current
4259 add_decl_to_level (b
, decl
);
4263 /* true means unconditionally make a BLOCK for the next level pushed. */
4265 static bool keep_next_level_flag
;
4267 static int binding_depth
= 0;
4274 for (i
= 0; i
< depth
* 2; i
++)
4278 /* Return a string describing the kind of SCOPE we have. */
4280 cp_binding_level_descriptor (cp_binding_level
*scope
)
4282 /* The order of this table must match the "scope_kind"
4284 static const char* scope_kind_names
[] = {
4290 "function-parameter-scope",
4293 "template-parameter-scope",
4294 "template-explicit-spec-scope"
4296 const scope_kind kind
= scope
->explicit_spec_p
4297 ? sk_template_spec
: scope
->kind
;
4299 return scope_kind_names
[kind
];
4302 /* Output a debugging information about SCOPE when performing
4305 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
4307 const char *desc
= cp_binding_level_descriptor (scope
);
4308 if (scope
->this_entity
)
4309 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
4310 scope
->this_entity
, (void *) scope
, line
);
4312 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
4315 /* A chain of binding_level structures awaiting reuse. */
4317 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
4319 /* Insert SCOPE as the innermost binding level. */
4322 push_binding_level (cp_binding_level
*scope
)
4324 /* Add it to the front of currently active scopes stack. */
4325 scope
->level_chain
= current_binding_level
;
4326 current_binding_level
= scope
;
4327 keep_next_level_flag
= false;
4329 if (ENABLE_SCOPE_CHECKING
)
4331 scope
->binding_depth
= binding_depth
;
4332 indent (binding_depth
);
4333 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4339 /* Create a new KIND scope and make it the top of the active scopes stack.
4340 ENTITY is the scope of the associated C++ entity (namespace, class,
4341 function, C++0x enumeration); it is NULL otherwise. */
4344 begin_scope (scope_kind kind
, tree entity
)
4346 cp_binding_level
*scope
;
4348 /* Reuse or create a struct for this binding level. */
4349 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
4351 scope
= free_binding_level
;
4352 free_binding_level
= scope
->level_chain
;
4353 memset (scope
, 0, sizeof (cp_binding_level
));
4356 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
4358 scope
->this_entity
= entity
;
4359 scope
->more_cleanups_ok
= true;
4366 case sk_template_spec
:
4367 scope
->explicit_spec_p
= true;
4368 kind
= sk_template_parms
;
4370 case sk_template_parms
:
4377 case sk_scoped_enum
:
4378 case sk_transaction
:
4380 scope
->keep
= keep_next_level_flag
;
4383 case sk_function_parms
:
4384 scope
->keep
= keep_next_level_flag
;
4386 scope
->immediate_fn_ctx_p
= DECL_IMMEDIATE_FUNCTION_P (entity
);
4390 NAMESPACE_LEVEL (entity
) = scope
;
4394 /* Should not happen. */
4400 push_binding_level (scope
);
4405 /* We're about to leave current scope. Pop the top of the stack of
4406 currently active scopes. Return the enclosing scope, now active. */
4411 cp_binding_level
*scope
= current_binding_level
;
4413 if (scope
->kind
== sk_namespace
&& class_binding_level
)
4414 current_binding_level
= class_binding_level
;
4416 /* We cannot leave a scope, if there are none left. */
4417 if (NAMESPACE_LEVEL (global_namespace
))
4418 gcc_assert (!global_scope_p (scope
));
4420 if (ENABLE_SCOPE_CHECKING
)
4422 indent (--binding_depth
);
4423 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4427 /* Move one nesting level up. */
4428 current_binding_level
= scope
->level_chain
;
4430 /* Namespace-scopes are left most probably temporarily, not
4431 completely; they can be reopened later, e.g. in namespace-extension
4432 or any name binding activity that requires us to resume a
4433 namespace. For classes, we cache some binding levels. For other
4434 scopes, we just make the structure available for reuse. */
4435 if (scope
->kind
!= sk_namespace
4436 && scope
!= previous_class_level
)
4438 scope
->level_chain
= free_binding_level
;
4439 gcc_assert (!ENABLE_SCOPE_CHECKING
4440 || scope
->binding_depth
== binding_depth
);
4441 free_binding_level
= scope
;
4444 if (scope
->kind
== sk_class
)
4446 /* Reset DEFINING_CLASS_P to allow for reuse of a
4447 class-defining scope in a non-defining context. */
4448 scope
->defining_class_p
= 0;
4450 /* Find the innermost enclosing class scope, and reset
4451 CLASS_BINDING_LEVEL appropriately. */
4452 class_binding_level
= NULL
;
4453 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
4454 if (scope
->kind
== sk_class
)
4456 class_binding_level
= scope
;
4461 return current_binding_level
;
4464 /* When we exit a toplevel class scope, we save its binding level so
4465 that we can restore it quickly. Here, we've entered some other
4466 class, so we must invalidate our cache. */
4469 invalidate_class_lookup_cache (void)
4471 previous_class_level
->level_chain
= free_binding_level
;
4472 free_binding_level
= previous_class_level
;
4473 previous_class_level
= NULL
;
4477 resume_scope (cp_binding_level
* b
)
4479 /* Resuming binding levels is meant only for namespaces,
4480 and those cannot nest into classes. */
4481 gcc_assert (!class_binding_level
);
4482 /* Also, resuming a non-directly nested namespace is a no-no. */
4483 gcc_assert (b
->level_chain
== current_binding_level
);
4484 current_binding_level
= b
;
4485 if (ENABLE_SCOPE_CHECKING
)
4487 b
->binding_depth
= binding_depth
;
4488 indent (binding_depth
);
4489 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
4494 /* Return the innermost binding level that is not for a class scope. */
4496 static cp_binding_level
*
4497 innermost_nonclass_level (void)
4499 cp_binding_level
*b
;
4501 b
= current_binding_level
;
4502 while (b
->kind
== sk_class
)
4508 /* We're defining an object of type TYPE. If it needs a cleanup, but
4509 we're not allowed to add any more objects with cleanups to the current
4510 scope, create a new binding level. */
4513 maybe_push_cleanup_level (tree type
)
4515 if (type
!= error_mark_node
4516 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4517 && current_binding_level
->more_cleanups_ok
== 0)
4519 begin_scope (sk_cleanup
, NULL
);
4520 current_binding_level
->statement_list
= push_stmt_list ();
4524 /* Return true if we are in the global binding level. */
4527 global_bindings_p (void)
4529 return global_scope_p (current_binding_level
);
4532 /* True if we are currently in a toplevel binding level. This
4533 means either the global binding level or a namespace in a toplevel
4534 binding level. Since there are no non-toplevel namespace levels,
4535 this really means any namespace or template parameter level. We
4536 also include a class whose context is toplevel. */
4539 toplevel_bindings_p (void)
4541 cp_binding_level
*b
= innermost_nonclass_level ();
4543 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
4546 /* True if this is a namespace scope, or if we are defining a class
4547 which is itself at namespace scope, or whose enclosing class is
4548 such a class, etc. */
4551 namespace_bindings_p (void)
4553 cp_binding_level
*b
= innermost_nonclass_level ();
4555 return b
->kind
== sk_namespace
;
4558 /* True if the innermost non-class scope is a block scope. */
4561 local_bindings_p (void)
4563 cp_binding_level
*b
= innermost_nonclass_level ();
4564 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
4567 /* True if the current level needs to have a BLOCK made. */
4572 return (current_binding_level
->blocks
!= NULL_TREE
4573 || current_binding_level
->keep
4574 || current_binding_level
->kind
== sk_cleanup
4575 || current_binding_level
->names
!= NULL_TREE
4576 || current_binding_level
->using_directives
);
4579 /* Returns the kind of the innermost scope. */
4582 innermost_scope_kind (void)
4584 return current_binding_level
->kind
;
4587 /* Returns true if this scope was created to store template parameters. */
4590 template_parm_scope_p (void)
4592 return innermost_scope_kind () == sk_template_parms
;
4595 /* If KEEP is true, make a BLOCK node for the next binding level,
4596 unconditionally. Otherwise, use the normal logic to decide whether
4597 or not to create a BLOCK. */
4600 keep_next_level (bool keep
)
4602 keep_next_level_flag
= keep
;
4605 /* Return the list of declarations of the current local scope. */
4608 get_local_decls (void)
4610 gcc_assert (current_binding_level
->kind
!= sk_namespace
4611 && current_binding_level
->kind
!= sk_class
);
4612 return current_binding_level
->names
;
4615 /* Return how many function prototypes we are currently nested inside. */
4618 function_parm_depth (void)
4621 cp_binding_level
*b
;
4623 for (b
= current_binding_level
;
4624 b
->kind
== sk_function_parms
;
4631 /* For debugging. */
4632 static int no_print_functions
= 0;
4633 static int no_print_builtins
= 0;
4636 print_binding_level (cp_binding_level
* lvl
)
4640 if (lvl
->this_entity
)
4641 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
4642 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
4643 if (lvl
->more_cleanups_ok
)
4644 fprintf (stderr
, " more-cleanups-ok");
4645 if (lvl
->have_cleanups
)
4646 fprintf (stderr
, " have-cleanups");
4647 fprintf (stderr
, "\n");
4650 fprintf (stderr
, " names:\t");
4651 /* We can probably fit 3 names to a line? */
4652 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
4654 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
4656 if (no_print_builtins
4657 && (TREE_CODE (t
) == TYPE_DECL
)
4658 && DECL_IS_UNDECLARED_BUILTIN (t
))
4661 /* Function decls tend to have longer names. */
4662 if (TREE_CODE (t
) == FUNCTION_DECL
)
4669 fprintf (stderr
, "\n\t");
4672 print_node_brief (stderr
, "", t
, 0);
4673 if (t
== error_mark_node
)
4677 fprintf (stderr
, "\n");
4679 if (vec_safe_length (lvl
->class_shadowed
))
4682 cp_class_binding
*b
;
4683 fprintf (stderr
, " class-shadowed:");
4684 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
4685 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
4686 fprintf (stderr
, "\n");
4688 if (lvl
->type_shadowed
)
4690 fprintf (stderr
, " type-shadowed:");
4691 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4693 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
4695 fprintf (stderr
, "\n");
4700 debug (cp_binding_level
&ref
)
4702 print_binding_level (&ref
);
4706 debug (cp_binding_level
*ptr
)
4711 fprintf (stderr
, "<nil>\n");
4715 print_other_binding_stack (cp_binding_level
*stack
)
4717 cp_binding_level
*level
;
4718 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
4720 fprintf (stderr
, "binding level %p\n", (void *) level
);
4721 print_binding_level (level
);
4726 print_binding_stack (void)
4728 cp_binding_level
*b
;
4729 fprintf (stderr
, "current_binding_level=%p\n"
4730 "class_binding_level=%p\n"
4731 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4732 (void *) current_binding_level
, (void *) class_binding_level
,
4733 (void *) NAMESPACE_LEVEL (global_namespace
));
4734 if (class_binding_level
)
4736 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
4737 if (b
== current_binding_level
)
4740 b
= class_binding_level
;
4742 b
= current_binding_level
;
4745 b
= current_binding_level
;
4746 print_other_binding_stack (b
);
4747 fprintf (stderr
, "global:\n");
4748 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
4751 /* Return the type associated with ID. */
4754 identifier_type_value_1 (tree id
)
4756 /* There is no type with that name, anywhere. */
4757 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
4759 /* This is not the type marker, but the real thing. */
4760 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
4761 return REAL_IDENTIFIER_TYPE_VALUE (id
);
4762 /* Have to search for it. It must be on the global level, now.
4763 Ask lookup_name not to return non-types. */
4764 id
= lookup_name (id
, LOOK_where::BLOCK_NAMESPACE
, LOOK_want::TYPE
);
4766 return TREE_TYPE (id
);
4770 /* Wrapper for identifier_type_value_1. */
4773 identifier_type_value (tree id
)
4776 timevar_start (TV_NAME_LOOKUP
);
4777 ret
= identifier_type_value_1 (id
);
4778 timevar_stop (TV_NAME_LOOKUP
);
4782 /* Push a definition of struct, union or enum tag named ID. into
4783 binding_level B. DECL is a TYPE_DECL for the type. DECL has
4784 already been pushed into its binding level. This is bookkeeping to
4788 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
4792 if (b
->kind
!= sk_namespace
)
4794 /* Shadow the marker, not the real thing, so that the marker
4795 gets restored later. */
4796 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4797 b
->type_shadowed
= tree_cons (id
, old_type_value
, b
->type_shadowed
);
4798 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
4799 TREE_TYPE (b
->type_shadowed
) = type
;
4805 /* Store marker instead of real type. */
4806 type
= global_type_node
;
4809 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
4812 /* As set_identifier_type_value_with_scope, but using
4813 current_binding_level. */
4816 set_identifier_type_value (tree id
, tree decl
)
4818 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
4821 /* Return the name for the constructor (or destructor) for the
4825 constructor_name (tree type
)
4827 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
4829 return decl
? DECL_NAME (decl
) : NULL_TREE
;
4832 /* Returns TRUE if NAME is the name for the constructor for TYPE,
4833 which must be a class type. */
4836 constructor_name_p (tree name
, tree type
)
4838 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
4840 /* These don't have names. */
4841 if (TREE_CODE (type
) == DECLTYPE_TYPE
4842 || TREE_CODE (type
) == TYPEOF_TYPE
)
4845 if (name
&& name
== constructor_name (type
))
4851 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4852 caller to set DECL_CONTEXT properly.
4854 Warning: For class and block-scope this must only be used when X
4855 will be the new innermost binding for its name, as we tack it onto
4856 the front of IDENTIFIER_BINDING without checking to see if the
4857 current IDENTIFIER_BINDING comes from a closer binding level than
4860 Warning: For namespace scope, this will look in LEVEL for an
4861 existing binding to match, but if not found will push the decl into
4862 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
4863 pop_nested_namespace if you really need to push it into a foreign
4867 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool hiding
= false)
4869 cp_binding_level
*b
;
4871 if (level
->kind
== sk_class
)
4873 gcc_checking_assert (!hiding
);
4874 b
= class_binding_level
;
4875 class_binding_level
= level
;
4876 pushdecl_class_level (x
);
4877 class_binding_level
= b
;
4881 tree function_decl
= current_function_decl
;
4882 if (level
->kind
== sk_namespace
)
4883 current_function_decl
= NULL_TREE
;
4884 b
= current_binding_level
;
4885 current_binding_level
= level
;
4886 x
= do_pushdecl (x
, hiding
);
4887 current_binding_level
= b
;
4888 current_function_decl
= function_decl
;
4893 /* Inject X into the local scope just before the function parms. */
4896 pushdecl_outermost_localscope (tree x
)
4898 cp_binding_level
*b
= NULL
;
4899 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4901 /* Find the scope just inside the function parms. */
4902 for (cp_binding_level
*n
= current_binding_level
;
4903 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
4906 tree ret
= b
? do_pushdecl_with_scope (x
, b
) : error_mark_node
;
4907 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4912 /* Process a local-scope or namespace-scope using declaration. LOOKUP
4913 is the result of qualified lookup (both value & type are
4914 significant). FN_SCOPE_P indicates if we're at function-scope (as
4915 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
4916 bindings, which are altered to reflect the newly brought in
4920 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
4921 bool insert_p
, tree
*value_p
, tree
*type_p
)
4923 tree value
= *value_p
;
4924 tree type
= *type_p
;
4925 bool failed
= false;
4927 /* Shift the old and new bindings around so we're comparing class and
4928 enumeration names to each other. */
4929 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4935 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4937 lookup
.type
= lookup
.value
;
4938 lookup
.value
= NULL_TREE
;
4941 /* Only process exporting if we're going to be inserting. */
4942 bool revealing_p
= insert_p
&& !fn_scope_p
&& module_has_cmi_p ();
4944 /* First do the value binding. */
4946 /* Nothing (only implicit typedef found). */
4947 gcc_checking_assert (lookup
.type
);
4948 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4950 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4952 tree new_fn
= *usings
;
4953 bool exporting
= revealing_p
&& module_exporting_p ();
4956 /* If the using decl is exported, the things it refers
4957 to must also be exported (or not in module purview). */
4958 if (!DECL_MODULE_EXPORT_P (new_fn
)
4959 && (DECL_LANG_SPECIFIC (new_fn
)
4960 && DECL_MODULE_PURVIEW_P (new_fn
)))
4962 error ("%q#D does not have external linkage", new_fn
);
4963 inform (DECL_SOURCE_LOCATION (new_fn
),
4964 "%q#D declared here", new_fn
);
4969 /* [namespace.udecl]
4971 If a function declaration in namespace scope or block
4972 scope has the same name and the same parameter types as a
4973 function introduced by a using declaration the program is
4975 /* This seems overreaching, asking core -- why do we care
4976 about decls in the namespace that we cannot name (because
4977 they are not transitively imported. We just check the
4978 decls that are in this TU. */
4980 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4984 if (new_fn
== old_fn
)
4986 /* The function already exists in the current
4987 namespace. We will still want to insert it if
4988 it is revealing a not-revealed thing. */
4992 else if (old
.using_p ())
4995 /* Update in place. 'tis ok. */
4996 OVL_EXPORT_P (old
.get_using ()) = true;
4999 else if (DECL_MODULE_EXPORT_P (new_fn
))
5003 value
= old
.remove_node (value
);
5008 else if (old
.using_p ())
5009 continue; /* This is a using decl. */
5010 else if (old
.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn
))
5011 continue; /* This is an anticipated builtin. */
5012 else if (!matching_fn_p (new_fn
, old_fn
))
5013 continue; /* Parameters do not match. */
5014 else if (decls_match (new_fn
, old_fn
))
5016 /* Extern "C" in different namespaces. */
5022 diagnose_name_conflict (new_fn
, old_fn
);
5029 if (!found
&& insert_p
)
5030 /* Unlike the decl-pushing case we don't drop anticipated
5031 builtins here. They don't cause a problem, and we'd
5032 like to match them with a future declaration. */
5033 value
= ovl_insert (new_fn
, value
, 1 + exporting
);
5037 /* Ignore anticipated builtins. */
5038 && !anticipated_builtin_p (value
)
5039 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
5041 diagnose_name_conflict (lookup
.value
, value
);
5045 // FIXME:what if we're newly exporting lookup.value
5046 value
= lookup
.value
;
5048 /* Now the type binding. */
5049 if (lookup
.type
&& lookup
.type
!= type
)
5051 // FIXME: What if we're exporting lookup.type?
5052 if (type
&& !decls_match (lookup
.type
, type
))
5054 diagnose_name_conflict (lookup
.type
, type
);
5063 /* If value is empty, shift any class or enumeration name back. */
5076 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5077 Both are namespaces. */
5080 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
5082 int depth
= SCOPE_DEPTH (ancestor
);
5084 if (!depth
&& !inline_only
)
5085 /* The global namespace encloses everything. */
5088 while (SCOPE_DEPTH (descendant
) > depth
5089 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
5090 descendant
= CP_DECL_CONTEXT (descendant
);
5092 return ancestor
== descendant
;
5095 /* Returns true if ROOT (a non-alias namespace, class, or function)
5096 encloses CHILD. CHILD may be either a class type or a namespace
5100 is_ancestor (tree root
, tree child
)
5102 gcc_checking_assert ((TREE_CODE (root
) == NAMESPACE_DECL
5103 && !DECL_NAMESPACE_ALIAS (root
))
5104 || TREE_CODE (root
) == FUNCTION_DECL
5105 || CLASS_TYPE_P (root
));
5106 gcc_checking_assert (TREE_CODE (child
) == NAMESPACE_DECL
5107 || CLASS_TYPE_P (child
));
5109 /* The global namespace encloses everything. Early-out for the
5111 if (root
== global_namespace
)
5114 /* Search CHILD until we reach namespace scope. */
5115 while (TREE_CODE (child
) != NAMESPACE_DECL
)
5117 /* If we've reached the ROOT, it encloses CHILD. */
5121 /* Go out one level. */
5123 child
= TYPE_NAME (child
);
5124 child
= CP_DECL_CONTEXT (child
);
5127 if (TREE_CODE (root
) != NAMESPACE_DECL
)
5128 /* Failed to meet the non-namespace we were looking for. */
5131 if (tree alias
= DECL_NAMESPACE_ALIAS (child
))
5134 return is_nested_namespace (root
, child
);
5137 /* Enter the class or namespace scope indicated by T suitable for name
5138 lookup. T can be arbitrary scope, not necessary nested inside the
5139 current scope. Returns a non-null scope to pop iff pop_scope
5140 should be called later to exit this scope. */
5145 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5146 push_decl_namespace (t
);
5147 else if (CLASS_TYPE_P (t
))
5149 if (!at_class_scope_p ()
5150 || !same_type_p (current_class_type
, t
))
5151 push_nested_class (t
);
5153 /* T is the same as the current scope. There is therefore no
5154 need to re-enter the scope. Since we are not actually
5155 pushing a new scope, our caller should not call
5163 /* Leave scope pushed by push_scope. */
5170 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5171 pop_decl_namespace ();
5172 else if CLASS_TYPE_P (t
)
5173 pop_nested_class ();
5176 /* Subroutine of push_inner_scope. */
5179 push_inner_scope_r (tree outer
, tree inner
)
5184 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5187 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5189 push_inner_scope_r (outer
, prev
);
5190 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5192 cp_binding_level
*save_template_parm
= 0;
5193 /* Temporary take out template parameter scopes. They are saved
5194 in reversed order in save_template_parm. */
5195 while (current_binding_level
->kind
== sk_template_parms
)
5197 cp_binding_level
*b
= current_binding_level
;
5198 current_binding_level
= b
->level_chain
;
5199 b
->level_chain
= save_template_parm
;
5200 save_template_parm
= b
;
5203 resume_scope (NAMESPACE_LEVEL (inner
));
5204 current_namespace
= inner
;
5206 /* Restore template parameter scopes. */
5207 while (save_template_parm
)
5209 cp_binding_level
*b
= save_template_parm
;
5210 save_template_parm
= b
->level_chain
;
5211 b
->level_chain
= current_binding_level
;
5212 current_binding_level
= b
;
5219 /* Enter the scope INNER from current scope. INNER must be a scope
5220 nested inside current scope. This works with both name lookup and
5221 pushing name into scope. In case a template parameter scope is present,
5222 namespace is pushed under the template parameter scope according to
5223 name lookup rule in 14.6.1/6.
5225 Return the former current scope suitable for pop_inner_scope. */
5228 push_inner_scope (tree inner
)
5230 tree outer
= current_scope ();
5232 outer
= current_namespace
;
5234 push_inner_scope_r (outer
, inner
);
5238 /* Exit the current scope INNER back to scope OUTER. */
5241 pop_inner_scope (tree outer
, tree inner
)
5244 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5247 while (outer
!= inner
)
5249 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5251 cp_binding_level
*save_template_parm
= 0;
5252 /* Temporary take out template parameter scopes. They are saved
5253 in reversed order in save_template_parm. */
5254 while (current_binding_level
->kind
== sk_template_parms
)
5256 cp_binding_level
*b
= current_binding_level
;
5257 current_binding_level
= b
->level_chain
;
5258 b
->level_chain
= save_template_parm
;
5259 save_template_parm
= b
;
5264 /* Restore template parameter scopes. */
5265 while (save_template_parm
)
5267 cp_binding_level
*b
= save_template_parm
;
5268 save_template_parm
= b
->level_chain
;
5269 b
->level_chain
= current_binding_level
;
5270 current_binding_level
= b
;
5276 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5280 /* Do a pushlevel for class declarations. */
5283 pushlevel_class (void)
5285 class_binding_level
= begin_scope (sk_class
, current_class_type
);
5288 /* ...and a poplevel for class declarations. */
5291 poplevel_class (void)
5293 cp_binding_level
*level
= class_binding_level
;
5294 cp_class_binding
*cb
;
5298 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5299 gcc_assert (level
!= 0);
5301 /* If we're leaving a toplevel class, cache its binding level. */
5302 if (current_class_depth
== 1)
5303 previous_class_level
= level
;
5304 for (shadowed
= level
->type_shadowed
;
5306 shadowed
= TREE_CHAIN (shadowed
))
5307 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
5309 /* Remove the bindings for all of the class-level declarations. */
5310 if (level
->class_shadowed
)
5312 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
5314 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
5315 cxx_binding_free (cb
->base
);
5317 ggc_free (level
->class_shadowed
);
5318 level
->class_shadowed
= NULL
;
5321 /* Now, pop out of the binding level which we created up in the
5322 `pushlevel_class' routine. */
5323 gcc_assert (current_binding_level
== level
);
5325 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5328 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5329 appropriate. DECL is the value to which a name has just been
5330 bound. CLASS_TYPE is the class in which the lookup occurred. */
5333 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
5336 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
5340 if (TREE_CODE (decl
) == OVERLOAD
)
5341 context
= ovl_scope (decl
);
5344 gcc_assert (DECL_P (decl
));
5345 context
= context_for_name_lookup (decl
);
5348 if (is_properly_derived_from (class_type
, context
))
5349 INHERITED_VALUE_BINDING_P (binding
) = 1;
5351 INHERITED_VALUE_BINDING_P (binding
) = 0;
5353 else if (binding
->value
== decl
)
5354 /* We only encounter a TREE_LIST when there is an ambiguity in the
5355 base classes. Such an ambiguity can be overridden by a
5356 definition in this class. */
5357 INHERITED_VALUE_BINDING_P (binding
) = 1;
5359 INHERITED_VALUE_BINDING_P (binding
) = 0;
5362 /* Make the declaration of X appear in CLASS scope. */
5365 pushdecl_class_level (tree x
)
5367 bool is_valid
= true;
5370 /* Do nothing if we're adding to an outer lambda closure type,
5371 outer_binding will add it later if it's needed. */
5372 if (current_class_type
!= class_binding_level
->this_entity
)
5375 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5376 /* Get the name of X. */
5377 tree name
= OVL_NAME (x
);
5381 is_valid
= push_class_level_binding (name
, x
);
5382 if (TREE_CODE (x
) == TYPE_DECL
)
5383 set_identifier_type_value (name
, x
);
5385 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
5387 /* If X is an anonymous aggregate, all of its members are
5388 treated as if they were members of the class containing the
5389 aggregate, for naming purposes. */
5390 location_t save_location
= input_location
;
5391 tree anon
= TREE_TYPE (x
);
5392 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
5393 for (unsigned ix
= member_vec
->length (); ix
--;)
5395 tree binding
= (*member_vec
)[ix
];
5396 if (STAT_HACK_P (binding
))
5398 if (!pushdecl_class_level (STAT_TYPE (binding
)))
5400 binding
= STAT_DECL (binding
);
5402 if (!pushdecl_class_level (binding
))
5406 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
5407 if (TREE_CODE (f
) == FIELD_DECL
)
5409 input_location
= DECL_SOURCE_LOCATION (f
);
5410 if (!pushdecl_class_level (f
))
5413 input_location
= save_location
;
5415 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5419 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5420 scope. If the value returned is non-NULL, and the PREVIOUS field
5421 is not set, callers must set the PREVIOUS field explicitly. */
5423 static cxx_binding
*
5424 get_class_binding (tree name
, cp_binding_level
*scope
)
5429 cxx_binding
*binding
;
5431 class_type
= scope
->this_entity
;
5433 /* Get the type binding. */
5434 type_binding
= lookup_member (class_type
, name
,
5435 /*protect=*/2, /*want_type=*/true,
5436 tf_warning_or_error
);
5437 /* Get the value binding. */
5438 value_binding
= lookup_member (class_type
, name
,
5439 /*protect=*/2, /*want_type=*/false,
5440 tf_warning_or_error
);
5443 && (TREE_CODE (value_binding
) == TYPE_DECL
5444 || DECL_CLASS_TEMPLATE_P (value_binding
)
5445 || (TREE_CODE (value_binding
) == TREE_LIST
5446 && TREE_TYPE (value_binding
) == error_mark_node
5447 && (TREE_CODE (TREE_VALUE (value_binding
))
5449 /* We found a type binding, even when looking for a non-type
5450 binding. This means that we already processed this binding
5453 else if (value_binding
)
5455 if (TREE_CODE (value_binding
) == TREE_LIST
5456 && TREE_TYPE (value_binding
) == error_mark_node
)
5457 /* NAME is ambiguous. */
5459 else if (BASELINK_P (value_binding
))
5460 /* NAME is some overloaded functions. */
5461 value_binding
= BASELINK_FUNCTIONS (value_binding
);
5464 /* If we found either a type binding or a value binding, create a
5465 new binding object. */
5466 if (type_binding
|| value_binding
)
5468 binding
= new_class_binding (name
,
5472 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
5480 /* Make the declaration(s) of X appear in CLASS scope under the name
5481 NAME. Returns true if the binding is valid. */
5484 push_class_level_binding_1 (tree name
, tree x
)
5486 cxx_binding
*binding
;
5490 /* The class_binding_level will be NULL if x is a template
5491 parameter name in a member template. */
5492 if (!class_binding_level
)
5495 if (name
== error_mark_node
)
5498 /* Can happen for an erroneous declaration (c++/60384). */
5499 if (!identifier_p (name
))
5501 gcc_assert (errorcount
|| sorrycount
);
5505 /* Check for invalid member names. But don't worry about a default
5506 argument-scope lambda being pushed after the class is complete. */
5507 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
5508 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
5509 /* Check that we're pushing into the right binding level. */
5510 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
5512 /* We could have been passed a tree list if this is an ambiguous
5513 declaration. If so, pull the declaration out because
5514 check_template_shadow will not handle a TREE_LIST. */
5515 if (TREE_CODE (decl
) == TREE_LIST
5516 && TREE_TYPE (decl
) == error_mark_node
)
5517 decl
= TREE_VALUE (decl
);
5519 if (!check_template_shadow (decl
))
5524 If T is the name of a class, then each of the following shall
5525 have a name different from T:
5527 -- every static data member of class T;
5529 -- every member of class T that is itself a type;
5531 -- every enumerator of every member of class T that is an
5534 -- every member of every anonymous union that is a member of
5537 (Non-static data members were also forbidden to have the same
5538 name as T until TC1.) */
5540 || TREE_CODE (x
) == CONST_DECL
5541 || (TREE_CODE (x
) == TYPE_DECL
5542 && !DECL_SELF_REFERENCE_P (x
))
5543 /* A data member of an anonymous union. */
5544 || (TREE_CODE (x
) == FIELD_DECL
5545 && DECL_CONTEXT (x
) != current_class_type
))
5546 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
5548 tree scope
= context_for_name_lookup (x
);
5549 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
5551 error_at (DECL_SOURCE_LOCATION (x
),
5552 "%qD has the same name as the class in which it is "
5558 /* Get the current binding for NAME in this class, if any. */
5559 binding
= IDENTIFIER_BINDING (name
);
5560 if (!binding
|| binding
->scope
!= class_binding_level
)
5562 binding
= get_class_binding (name
, class_binding_level
);
5563 /* If a new binding was created, put it at the front of the
5564 IDENTIFIER_BINDING list. */
5567 binding
->previous
= IDENTIFIER_BINDING (name
);
5568 IDENTIFIER_BINDING (name
) = binding
;
5572 /* If there is already a binding, then we may need to update the
5574 if (binding
&& binding
->value
)
5576 tree bval
= binding
->value
;
5577 tree old_decl
= NULL_TREE
;
5578 tree target_decl
= strip_using_decl (decl
);
5579 tree target_bval
= strip_using_decl (bval
);
5581 if (INHERITED_VALUE_BINDING_P (binding
))
5583 /* If the old binding was from a base class, and was for a
5584 tag name, slide it over to make room for the new binding.
5585 The old binding is still visible if explicitly qualified
5586 with a class-key. */
5587 if (TREE_CODE (target_bval
) == TYPE_DECL
5588 && DECL_ARTIFICIAL (target_bval
)
5589 && !(TREE_CODE (target_decl
) == TYPE_DECL
5590 && DECL_ARTIFICIAL (target_decl
)))
5592 old_decl
= binding
->type
;
5593 binding
->type
= bval
;
5594 binding
->value
= NULL_TREE
;
5595 INHERITED_VALUE_BINDING_P (binding
) = 0;
5600 /* Any inherited type declaration is hidden by the type
5601 declaration in the derived class. */
5602 if (TREE_CODE (target_decl
) == TYPE_DECL
5603 && DECL_ARTIFICIAL (target_decl
))
5604 binding
->type
= NULL_TREE
;
5607 else if (TREE_CODE (decl
) == USING_DECL
5608 && TREE_CODE (bval
) == USING_DECL
5609 && same_type_p (USING_DECL_SCOPE (decl
),
5610 USING_DECL_SCOPE (bval
)))
5611 /* This is a using redeclaration that will be diagnosed later
5612 in supplement_binding */
5614 else if (TREE_CODE (decl
) == USING_DECL
5615 && TREE_CODE (bval
) == USING_DECL
5616 && DECL_DEPENDENT_P (decl
)
5617 && DECL_DEPENDENT_P (bval
))
5619 else if (TREE_CODE (decl
) == USING_DECL
5620 && OVL_P (target_bval
))
5622 else if (TREE_CODE (bval
) == USING_DECL
5623 && OVL_P (target_decl
))
5625 else if (OVL_P (target_decl
)
5626 && OVL_P (target_bval
))
5629 if (old_decl
&& binding
->scope
== class_binding_level
)
5632 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5633 here. This function is only used to register bindings
5634 from with the class definition itself. */
5635 INHERITED_VALUE_BINDING_P (binding
) = 0;
5640 /* Note that we declared this value so that we can issue an error if
5641 this is an invalid redeclaration of a name already used for some
5643 note_name_declared_in_class (name
, decl
);
5645 /* If we didn't replace an existing binding, put the binding on the
5646 stack of bindings for the identifier, and update the shadowed
5648 if (binding
&& binding
->scope
== class_binding_level
)
5649 /* Supplement the existing binding. */
5650 ok
= supplement_binding (binding
, decl
);
5653 /* Create a new binding. */
5654 push_binding (name
, decl
, class_binding_level
);
5661 /* Wrapper for push_class_level_binding_1. */
5664 push_class_level_binding (tree name
, tree x
)
5667 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5668 ret
= push_class_level_binding_1 (name
, x
);
5669 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5673 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5674 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5678 lookup_using_decl (tree scope
, name_lookup
&lookup
)
5680 tree current
= current_scope ();
5681 bool dependent_p
= false;
5682 tree binfo
= NULL_TREE
;
5683 base_kind b_kind
= bk_not_base
;
5685 /* Because C++20 breaks the invariant that only member using-decls
5686 refer to members and only non-member using-decls refer to
5687 non-members, we first do the lookups, and then do validation that
5688 what we found is ok. */
5690 if (TREE_CODE (scope
) == ENUMERAL_TYPE
5691 && cxx_dialect
< cxx20
5692 && UNSCOPED_ENUM_P (scope
)
5693 && !TYPE_FUNCTION_SCOPE_P (scope
))
5695 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5696 should allow it as meaning the enclosing scope. I don't see any
5697 justification for this in C++11, but let's keep allowing it. */
5698 tree ctx
= CP_TYPE_CONTEXT (scope
);
5699 if (CLASS_TYPE_P (ctx
) == CLASS_TYPE_P (current
))
5703 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5705 /* Naming a namespace member. */
5706 qualified_namespace_lookup (scope
, &lookup
);
5708 if (TYPE_P (current
)
5711 || cxx_dialect
< cxx20
5712 || TREE_CODE (lookup
.value
) != CONST_DECL
))
5714 error ("using-declaration for non-member at class scope");
5718 else if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
5720 /* Naming an enumeration member. */
5721 if (cxx_dialect
< cxx20
)
5722 error ("%<using%> with enumeration scope %q#T "
5723 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5725 lookup
.value
= lookup_enumerator (scope
, lookup
.name
);
5729 /* Naming a class member. This is awkward in C++20, because we
5730 might be naming an enumerator of an unrelated class. */
5732 /* You cannot using-decl a destructor. */
5733 if (TREE_CODE (lookup
.name
) == BIT_NOT_EXPR
)
5735 error ("%<%T::%D%> names destructor", scope
, lookup
.name
);
5739 /* Using T::T declares inheriting ctors, even if T is a typedef. */
5740 if (MAYBE_CLASS_TYPE_P (scope
)
5741 && (lookup
.name
== TYPE_IDENTIFIER (scope
)
5742 || constructor_name_p (lookup
.name
, scope
)))
5744 if (!TYPE_P (current
))
5746 error ("non-member using-declaration names constructor of %qT",
5750 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
5751 lookup
.name
= ctor_identifier
;
5752 CLASSTYPE_NON_AGGREGATE (current
) = true;
5755 if (!MAYBE_CLASS_TYPE_P (scope
))
5757 else if (TYPE_P (current
))
5759 dependent_p
= dependent_scope_p (scope
);
5762 binfo
= lookup_base (current
, scope
, ba_any
, &b_kind
, tf_none
);
5763 gcc_checking_assert (b_kind
>= bk_not_base
);
5765 if (lookup
.name
== ctor_identifier
)
5767 /* Even if there are dependent bases, SCOPE will not
5768 be direct base, no matter. */
5769 if (b_kind
< bk_proper_base
|| !binfo_direct_p (binfo
))
5771 error ("%qT is not a direct base of %qT", scope
, current
);
5775 else if (b_kind
< bk_proper_base
)
5776 binfo
= TYPE_BINFO (scope
);
5777 else if (IDENTIFIER_CONV_OP_P (lookup
.name
)
5778 && dependent_type_p (TREE_TYPE (lookup
.name
)))
5783 binfo
= TYPE_BINFO (scope
);
5788 lookup
.value
= lookup_member (binfo
, lookup
.name
, /*protect=*/2,
5789 /*want_type=*/false, tf_none
);
5791 tree saved_value
= lookup
.value
;
5793 && b_kind
< bk_proper_base
)
5795 if (cxx_dialect
>= cxx20
5796 && TREE_CODE (lookup
.value
) == CONST_DECL
)
5798 /* Using an unrelated enum; check access here rather
5799 than separately for class and non-class using. */
5800 perform_or_defer_access_check
5801 (binfo
, lookup
.value
, lookup
.value
, tf_warning_or_error
);
5802 /* And then if this is a copy from handle_using_decl, look
5803 through to the original enumerator. */
5804 if (CONST_DECL_USING_P (lookup
.value
))
5805 lookup
.value
= DECL_ABSTRACT_ORIGIN (lookup
.value
);
5808 lookup
.value
= NULL_TREE
;
5813 if (!TYPE_P (current
))
5815 error ("using-declaration for member at non-class scope");
5819 if (b_kind
< bk_proper_base
)
5821 if (b_kind
== bk_not_base
&& any_dependent_bases_p ())
5822 /* Treat as-if dependent. */
5826 auto_diagnostic_group g
;
5827 error_not_base_type (scope
, current
);
5828 if (saved_value
&& DECL_IMPLICIT_TYPEDEF_P (saved_value
)
5829 && (TREE_CODE (TREE_TYPE (saved_value
))
5831 inform (input_location
,
5832 "did you mean %<using enum %T::%D%>?",
5833 scope
, lookup
.name
);
5841 /* Did we find anything sane? */
5844 else if (!lookup
.value
)
5846 error ("%qD has not been declared in %qD", lookup
.name
, scope
);
5849 else if (TREE_CODE (lookup
.value
) == TREE_LIST
5850 /* We can (independently) have ambiguous implicit typedefs. */
5851 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
5853 error ("reference to %qD is ambiguous", lookup
.name
);
5854 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
5855 ? lookup
.value
: lookup
.type
);
5858 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
5860 error ("using-declaration may not name namespace %qD", lookup
.value
);
5864 if (TYPE_P (current
))
5866 /* In class scope. */
5868 /* Cannot introduce a constructor name. */
5869 if (constructor_name_p (lookup
.name
, current
))
5871 error ("%<%T::%D%> names constructor in %qT",
5872 scope
, lookup
.name
, current
);
5876 if (lookup
.value
&& BASELINK_P (lookup
.value
))
5877 /* The binfo from which the functions came does not matter. */
5878 lookup
.value
= BASELINK_FUNCTIONS (lookup
.value
);
5881 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
5882 USING_DECL_SCOPE (using_decl
) = scope
;
5883 USING_DECL_DECLS (using_decl
) = lookup
.value
;
5884 DECL_DEPENDENT_P (using_decl
) = dependent_p
;
5885 DECL_CONTEXT (using_decl
) = current
;
5886 if (TYPE_P (current
) && b_kind
== bk_not_base
)
5887 USING_DECL_UNRELATED_P (using_decl
) = true;
5892 /* Process "using SCOPE::NAME" in a class scope. Return the
5893 USING_DECL created. */
5896 do_class_using_decl (tree scope
, tree name
)
5898 if (name
== error_mark_node
5899 || scope
== error_mark_node
)
5902 name_lookup
lookup (name
);
5903 return lookup_using_decl (scope
, lookup
);
5907 /* Return the binding for NAME in NS in the current TU. If NS is
5908 NULL, look in global_namespace. We will not find declarations
5909 from imports. Users of this who, having found nothing, push a new
5910 decl must be prepared for that pushing to match an existing decl. */
5913 get_namespace_binding (tree ns
, tree name
)
5915 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5917 ns
= global_namespace
;
5918 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
5919 tree ret
= NULL_TREE
;
5921 if (tree
*b
= find_namespace_slot (ns
, name
))
5925 if (TREE_CODE (ret
) == BINDING_VECTOR
)
5926 ret
= BINDING_VECTOR_CLUSTER (ret
, 0).slots
[0];
5928 ret
= MAYBE_STAT_DECL (ret
);
5931 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5935 /* Push internal DECL into the global namespace. Does not do the
5936 full overload fn handling and does not add it to the list of things
5937 in the namespace. */
5940 set_global_binding (tree decl
)
5942 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5944 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
5947 /* The user's placed something in the implementor's namespace. */
5948 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
5950 /* Force the binding, so compiler internals continue to work. */
5953 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5956 /* Set the context of a declaration to scope. Complain if we are not
5960 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
5962 /* Get rid of namespace aliases. */
5963 scope
= ORIGINAL_NAMESPACE (scope
);
5965 /* It is ok for friends to be qualified in parallel space. */
5966 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
5967 error ("declaration of %qD not in a namespace surrounding %qD",
5969 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5971 /* See whether this has been declared in the namespace or inline
5973 tree old
= NULL_TREE
;
5975 name_lookup
lookup (DECL_NAME (decl
),
5976 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
5977 if (!lookup
.search_qualified (scope
, /*usings=*/false))
5978 /* No old declaration at all. */
5983 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
5984 if (TREE_CODE (old
) == TREE_LIST
)
5987 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5988 error ("reference to %qD is ambiguous", decl
);
5989 print_candidates (old
);
5993 if (!DECL_DECLARES_FUNCTION_P (decl
))
5995 /* Don't compare non-function decls with decls_match here, since
5996 it can't check for the correct constness at this
5997 point. pushdecl will find those errors later. */
5999 /* We might have found it in an inline namespace child of SCOPE. */
6000 if (TREE_CODE (decl
) == TREE_CODE (old
))
6001 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
6004 /* Writing "N::i" to declare something directly in "N" is invalid. */
6005 if (CP_DECL_CONTEXT (decl
) == current_namespace
6006 && at_namespace_scope_p ())
6007 error_at (DECL_SOURCE_LOCATION (decl
),
6008 "explicit qualification in declaration of %qD", decl
);
6012 /* Since decl is a function, old should contain a function decl. */
6016 /* It didn't work, go back to the explicit scope. */
6017 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
6018 error ("%qD should have been declared inside %qD", decl
, scope
);
6023 /* We handle these in check_explicit_instantiation_namespace. */
6024 if (processing_explicit_instantiation
)
6026 if (processing_template_decl
|| processing_specialization
)
6027 /* We have not yet called push_template_decl to turn a
6028 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6029 match. But, we'll check later, when we construct the
6033 /* Instantiations or specializations of templates may be declared as
6034 friends in any namespace. */
6035 if (friendp
&& DECL_USE_TEMPLATE (decl
))
6038 tree found
= NULL_TREE
;
6039 bool hidden_p
= false;
6041 for (lkp_iterator
iter (old
); iter
; ++iter
)
6043 if (iter
.using_p ())
6048 /* Adjust DECL_CONTEXT first so decls_match will return true
6049 if DECL will match a declaration in an inline namespace. */
6050 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
6051 if (decls_match (decl
, ofn
))
6055 /* We found more than one matching declaration. This
6056 can happen if we have two inline namespace children,
6057 each containing a suitable declaration. */
6058 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
6062 hidden_p
= iter
.hidden_p ();
6070 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
6071 "%qD has not been declared within %qD", decl
, scope
);
6072 inform (DECL_SOURCE_LOCATION (found
),
6073 "only here as a %<friend%>");
6075 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
6082 /* Return the namespace where the current declaration is declared. */
6085 current_decl_namespace (void)
6088 /* If we have been pushed into a different namespace, use it. */
6089 if (!vec_safe_is_empty (decl_namespace_list
))
6090 return decl_namespace_list
->last ();
6092 if (current_class_type
)
6093 result
= decl_namespace_context (current_class_type
);
6094 else if (current_function_decl
)
6095 result
= decl_namespace_context (current_function_decl
);
6097 result
= current_namespace
;
6101 /* Process any ATTRIBUTES on a namespace definition. Returns true if
6102 attribute visibility is seen. */
6105 handle_namespace_attrs (tree ns
, tree attributes
)
6108 bool saw_vis
= false;
6110 if (attributes
== error_mark_node
)
6113 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
6115 tree name
= get_attribute_name (d
);
6116 tree args
= TREE_VALUE (d
);
6118 if (is_attribute_p ("visibility", name
))
6120 /* attribute visibility is a property of the syntactic block
6121 rather than the namespace as a whole, so we don't touch the
6122 NAMESPACE_DECL at all. */
6123 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
6124 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
6126 warning (OPT_Wattributes
,
6127 "%qD attribute requires a single NTBS argument",
6132 if (!TREE_PUBLIC (ns
))
6133 warning (OPT_Wattributes
,
6134 "%qD attribute is meaningless since members of the "
6135 "anonymous namespace get local symbols", name
);
6137 push_visibility (TREE_STRING_POINTER (x
), 1);
6140 else if (is_attribute_p ("abi_tag", name
))
6142 if (!DECL_NAME (ns
))
6144 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
6148 if (!DECL_NAMESPACE_INLINE_P (ns
))
6150 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
6156 tree dn
= DECL_NAME (ns
);
6157 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
6158 IDENTIFIER_POINTER (dn
));
6159 TREE_TYPE (args
) = char_array_type_node
;
6160 args
= fix_string_type (args
);
6161 args
= build_tree_list (NULL_TREE
, args
);
6163 if (check_abi_tag_args (args
, name
))
6164 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6165 DECL_ATTRIBUTES (ns
));
6167 else if (is_attribute_p ("deprecated", name
))
6169 if (!DECL_NAME (ns
))
6171 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
6175 if (args
&& TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
6177 error ("deprecated message is not a string");
6180 TREE_DEPRECATED (ns
) = 1;
6182 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6183 DECL_ATTRIBUTES (ns
));
6187 warning (OPT_Wattributes
, "%qD attribute directive ignored",
6196 /* Temporarily set the namespace for the current declaration. */
6199 push_decl_namespace (tree decl
)
6201 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
6202 decl
= decl_namespace_context (decl
);
6203 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
6206 /* [namespace.memdef]/2 */
6209 pop_decl_namespace (void)
6211 decl_namespace_list
->pop ();
6214 /* Process a namespace-alias declaration. */
6217 do_namespace_alias (tree alias
, tree name_space
)
6219 if (name_space
== error_mark_node
)
6222 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
6224 name_space
= ORIGINAL_NAMESPACE (name_space
);
6226 /* Build the alias. */
6227 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
6228 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
6229 DECL_EXTERNAL (alias
) = 1;
6230 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
6231 set_originating_module (alias
);
6235 /* Emit debug info for namespace alias. */
6236 if (!building_stmt_list_p ())
6237 (*debug_hooks
->early_global_decl
) (alias
);
6240 /* Like pushdecl, only it places X in the current namespace,
6244 pushdecl_namespace_level (tree x
, bool hiding
)
6246 cp_binding_level
*b
= current_binding_level
;
6249 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6250 t
= do_pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
), hiding
);
6252 /* Now, the type_shadowed stack may screw us. Munge it so it does
6254 if (TREE_CODE (t
) == TYPE_DECL
)
6256 tree name
= DECL_NAME (t
);
6258 tree
*ptr
= (tree
*)0;
6259 for (; !global_scope_p (b
); b
= b
->level_chain
)
6261 tree shadowed
= b
->type_shadowed
;
6262 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
6263 if (TREE_PURPOSE (shadowed
) == name
)
6265 ptr
= &TREE_VALUE (shadowed
);
6266 /* Can't break out of the loop here because sometimes
6267 a binding level will have duplicate bindings for
6268 PT names. It's gross, but I haven't time to fix it. */
6271 newval
= TREE_TYPE (t
);
6272 if (ptr
== (tree
*)0)
6274 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
6275 up here if this is changed to an assertion. --KR */
6276 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
6283 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6287 /* Wrapper around push_local_binding to push the bindings for
6288 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6289 is the result of name lookup during template parsing. */
6292 push_using_decl_bindings (name_lookup
*lookup
, tree name
, tree value
)
6294 tree type
= NULL_TREE
;
6296 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
6299 value
= binding
->value
;
6300 type
= binding
->type
;
6303 /* DR 36 questions why using-decls at function scope may not be
6304 duplicates. Disallow it, as C++11 claimed and PR 20420
6307 do_nonmember_using_decl (*lookup
, true, true, &value
, &type
);
6311 else if (binding
&& value
== binding
->value
)
6312 /* Redeclaration of this USING_DECL. */;
6313 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
6315 /* We already have this binding, so replace it. */
6316 update_local_overload (IDENTIFIER_BINDING (name
), value
);
6317 IDENTIFIER_BINDING (name
)->value
= value
;
6320 /* Install the new binding. */
6321 push_local_binding (name
, value
, /*using=*/true);
6325 else if (binding
&& type
== binding
->type
)
6329 push_local_binding (name
, type
, /*using=*/true);
6330 set_identifier_type_value (name
, type
);
6334 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6337 push_using_decl_bindings (tree name
, tree value
)
6339 push_using_decl_bindings (nullptr, name
, value
);
6342 /* Process a using declaration in non-class scope. */
6345 finish_nonmember_using_decl (tree scope
, tree name
)
6347 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
6349 if (scope
== error_mark_node
|| name
== error_mark_node
)
6352 name_lookup
lookup (name
);
6354 tree using_decl
= lookup_using_decl (scope
, lookup
);
6358 /* Emit debug info. */
6359 if (!processing_template_decl
)
6360 cp_emit_debug_info_for_using (lookup
.value
,
6361 current_binding_level
->this_entity
);
6363 if (current_binding_level
->kind
== sk_namespace
)
6365 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
6366 tree
*mslot
= get_fixed_binding_slot (slot
, name
,
6367 BINDING_SLOT_CURRENT
, true);
6368 bool failed
= false;
6372 /* A module vector. I presume the binding list is going to
6373 be sparser than the import bitmap. Hence iterate over
6374 the former checking for bits set in the bitmap. */
6375 bitmap imports
= get_import_bitmap ();
6376 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
6378 /* Scan the imported bindings. */
6379 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
6380 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
6386 /* Do this in forward order, so we load modules in an order
6387 the user expects. */
6388 for (; ix
--; cluster
++)
6389 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
6391 /* Are we importing this module? */
6392 if (unsigned base
= cluster
->indices
[jx
].base
)
6393 if (unsigned span
= cluster
->indices
[jx
].span
)
6395 if (bitmap_bit_p (imports
, base
))
6397 while (++base
, --span
);
6402 if (cluster
->slots
[jx
].is_lazy ())
6404 gcc_assert (cluster
->indices
[jx
].span
== 1);
6405 lazy_load_binding (cluster
->indices
[jx
].base
,
6406 scope
, name
, &cluster
->slots
[jx
]);
6409 tree value
= cluster
->slots
[jx
];
6411 /* Load errors could mean there's nothing here. */
6414 /* Extract what we can see from here. If there's no
6415 stat_hack, then everything was exported. */
6416 tree type
= NULL_TREE
;
6418 /* If no stat hack, everything is visible. */
6419 if (STAT_HACK_P (value
))
6421 if (STAT_TYPE_VISIBLE_P (value
))
6422 type
= STAT_TYPE (value
);
6423 value
= STAT_VISIBLE (value
);
6426 if (do_nonmember_using_decl (lookup
, false, false,
6437 /* Now do the current slot. */
6438 tree value
= MAYBE_STAT_DECL (*mslot
);
6439 tree type
= MAYBE_STAT_TYPE (*mslot
);
6441 do_nonmember_using_decl (lookup
, false, true, &value
, &type
);
6443 // FIXME: Partition mergeableness?
6444 if (STAT_HACK_P (*mslot
))
6446 STAT_DECL (*mslot
) = value
;
6447 STAT_TYPE (*mslot
) = type
;
6450 *mslot
= stat_hack (value
, type
);
6457 add_decl_expr (using_decl
);
6458 push_using_decl_bindings (&lookup
, name
, NULL_TREE
);
6462 /* Return the declarations that are members of the namespace NS. */
6465 cp_namespace_decls (tree ns
)
6467 return NAMESPACE_LEVEL (ns
)->names
;
6470 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6471 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6474 qualify_lookup (tree val
, LOOK_want want
)
6476 if (val
== NULL_TREE
)
6479 if (bool (want
& LOOK_want::TYPE
))
6481 tree target_val
= strip_using_decl (val
);
6483 if (TREE_CODE (STRIP_TEMPLATE (target_val
)) == TYPE_DECL
)
6487 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
6488 return TREE_CODE (val
) == NAMESPACE_DECL
;
6493 /* Is there a "using namespace std;" directive within USINGS? */
6496 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
6501 for (unsigned ix
= usings
->length (); ix
--;)
6502 if ((*usings
)[ix
] == std_node
)
6508 /* Is there a "using namespace std;" directive within the current
6509 namespace (or its ancestors)?
6510 Compare with name_lookup::search_unqualified. */
6513 has_using_namespace_std_directive_p ()
6515 for (cp_binding_level
*level
= current_binding_level
;
6517 level
= level
->level_chain
)
6518 if (using_directives_contain_std_p (level
->using_directives
))
6524 /* Subclass of deferred_diagnostic, for issuing a note when
6525 --param cxx-max-namespaces-for-diagnostic-help is reached.
6527 The note should be issued after the error, but before any other
6528 deferred diagnostics. This is handled by decorating a wrapped
6529 deferred_diagnostic, and emitting a note before that wrapped note is
6532 class namespace_limit_reached
: public deferred_diagnostic
6535 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
6536 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
6537 : deferred_diagnostic (loc
),
6538 m_limit (limit
), m_name (name
),
6539 m_wrapped (move (wrapped
))
6543 ~namespace_limit_reached ()
6545 /* Unconditionally warn that the search was truncated. */
6546 inform (get_location (),
6547 "maximum limit of %d namespaces searched for %qE",
6549 /* m_wrapped will be implicitly deleted after this, emitting any followup
6550 diagnostic after the above note. */
6556 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
6559 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6560 Emit a note showing the location of the declaration of the suggestion. */
6562 class show_candidate_location
: public deferred_diagnostic
6565 show_candidate_location (location_t loc
, tree candidate
)
6566 : deferred_diagnostic (loc
),
6567 m_candidate (candidate
)
6571 ~show_candidate_location ()
6573 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
6580 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6581 to be suggested by suggest_alternatives_for.
6583 Emit a series of notes showing the various suggestions. */
6585 class suggest_alternatives
: public deferred_diagnostic
6588 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
6589 : deferred_diagnostic (loc
),
6590 m_candidates (candidates
)
6594 ~suggest_alternatives ()
6596 if (m_candidates
.length ())
6598 inform_n (get_location (), m_candidates
.length (),
6599 "suggested alternative:",
6600 "suggested alternatives:");
6601 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
6603 tree val
= m_candidates
[ix
];
6605 inform (location_of (val
), " %qE", val
);
6608 m_candidates
.release ();
6612 vec
<tree
> m_candidates
;
6615 /* A class for encapsulating the result of a search across
6616 multiple namespaces (and scoped enums within them) for an
6617 unrecognized name seen at a given source location. */
6619 class namespace_hints
6622 namespace_hints (location_t loc
, tree name
);
6624 name_hint
convert_candidates_to_name_hint ();
6625 name_hint
maybe_decorate_with_limit (name_hint
);
6628 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
6632 vec
<tree
> m_candidates
;
6634 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6637 /* Was the limit reached? */
6641 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6642 looking for an exact match for unrecognized NAME seen at LOC. */
6644 namespace_hints::namespace_hints (location_t loc
, tree name
)
6645 : m_loc(loc
), m_name (name
)
6647 auto_vec
<tree
> worklist
;
6649 m_candidates
= vNULL
;
6651 m_limit
= param_cxx_max_namespaces_for_diagnostic_help
;
6653 /* Breadth-first search of namespaces. Up to limit namespaces
6654 searched (limit zero == unlimited). */
6655 worklist
.safe_push (global_namespace
);
6656 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
6658 tree ns
= worklist
[ix
];
6659 name_lookup
lookup (name
);
6661 if (lookup
.search_qualified (ns
, false))
6662 m_candidates
.safe_push (lookup
.value
);
6666 /* Look for child namespaces. We have to do this
6667 indirectly because they are chained in reverse order,
6668 which is confusing to the user. */
6669 auto_vec
<tree
> children
;
6671 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
6672 decl
; decl
= TREE_CHAIN (decl
))
6674 if (TREE_CODE (decl
) == NAMESPACE_DECL
6675 && !DECL_NAMESPACE_ALIAS (decl
)
6676 && !DECL_NAMESPACE_INLINE_P (decl
))
6677 children
.safe_push (decl
);
6679 /* Look for exact matches for NAME within scoped enums.
6680 These aren't added to the worklist, and so don't count
6681 against the search limit. */
6682 if (TREE_CODE (decl
) == TYPE_DECL
)
6684 tree type
= TREE_TYPE (decl
);
6685 if (SCOPED_ENUM_P (type
))
6686 maybe_add_candidate_for_scoped_enum (type
, name
);
6690 while (!m_limited
&& !children
.is_empty ())
6692 if (worklist
.length () == m_limit
)
6695 worklist
.safe_push (children
.pop ());
6701 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6702 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6704 If m_candidates is non-empty, use it to generate a suggestion and/or
6705 a deferred diagnostic that lists the possible candidate(s).
6709 namespace_hints::convert_candidates_to_name_hint ()
6711 /* How many candidates do we have? */
6713 /* If we have just one candidate, issue a name_hint with it as a suggestion
6714 (so that consumers are able to suggest it within the error message and emit
6715 it as a fix-it hint), and with a note showing the candidate's location. */
6716 if (m_candidates
.length () == 1)
6718 tree candidate
= m_candidates
[0];
6719 /* Clean up CANDIDATES. */
6720 m_candidates
.release ();
6721 return name_hint (expr_to_string (candidate
),
6722 new show_candidate_location (m_loc
, candidate
));
6724 else if (m_candidates
.length () > 1)
6725 /* If we have more than one candidate, issue a name_hint without a single
6726 "suggestion", but with a deferred diagnostic that lists the
6727 various candidates. This takes ownership of m_candidates. */
6728 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
6730 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6731 gcc_assert (m_candidates
.length () == 0);
6732 gcc_assert (m_candidates
== vNULL
);
6734 return name_hint ();
6737 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6738 then we want to emit a note about after the error, but before
6739 any other deferred diagnostics.
6741 Handle this by figuring out what hint is needed, then optionally
6742 decorating HINT with a namespace_limit_reached wrapper. */
6745 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
6748 return name_hint (hint
.suggestion (),
6749 new namespace_limit_reached (m_loc
, m_limit
,
6751 hint
.take_deferred ()));
6756 /* Look inside SCOPED_ENUM for exact matches for NAME.
6757 If one is found, add its CONST_DECL to m_candidates. */
6760 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
6763 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
6765 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
6767 tree id
= TREE_PURPOSE (iter
);
6770 m_candidates
.safe_push (TREE_VALUE (iter
));
6776 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6779 Search through all available namespaces and any scoped enums within them
6780 and generate a suggestion and/or a deferred diagnostic that lists possible
6783 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
6784 look for near-matches and suggest the best near-match, if there is one.
6786 If nothing is found, then an empty name_hint is returned. */
6789 suggest_alternatives_for (location_t location
, tree name
,
6790 bool suggest_misspellings
)
6792 /* First, search for exact matches in other namespaces. */
6793 namespace_hints
ns_hints (location
, name
);
6794 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6796 /* Otherwise, try other approaches. */
6798 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
6800 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6803 /* The second half of suggest_alternatives_for, for when no exact matches
6804 were found in other namespaces. */
6807 suggest_alternatives_for_1 (location_t location
, tree name
,
6808 bool suggest_misspellings
)
6810 /* No candidates were found in the available namespaces. */
6812 /* If there's a "using namespace std;" active, and this
6813 is one of the most common "std::" names, then it's probably a
6814 missing #include. */
6815 if (has_using_namespace_std_directive_p ())
6817 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
6822 /* Otherwise, consider misspellings. */
6823 if (!suggest_misspellings
)
6824 return name_hint ();
6826 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
6829 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6832 Search through all available namespaces and generate a suggestion and/or
6833 a deferred diagnostic that lists possible candidate(s).
6835 This is similiar to suggest_alternatives_for, but doesn't fallback to
6836 the other approaches used by that function. */
6839 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
6841 namespace_hints
ns_hints (location
, name
);
6843 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6845 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6848 /* A well-known name within the C++ standard library, returned by
6849 get_std_name_hint. */
6851 struct std_name_hint
6853 /* A name within "std::". */
6856 /* The header name defining it within the C++ Standard Library
6857 (with '<' and '>'). */
6860 /* The dialect of C++ in which this was added. */
6861 enum cxx_dialect min_dialect
;
6864 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
6865 for some of the most common names within "std::".
6866 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
6868 static const std_name_hint
*
6869 get_std_name_hint (const char *name
)
6871 static const std_name_hint hints
[] = {
6873 {"any", "<any>", cxx17
},
6874 {"any_cast", "<any>", cxx17
},
6875 {"make_any", "<any>", cxx17
},
6877 {"array", "<array>", cxx11
},
6878 {"to_array", "<array>", cxx20
},
6880 {"atomic", "<atomic>", cxx11
},
6881 {"atomic_flag", "<atomic>", cxx11
},
6882 {"atomic_ref", "<atomic>", cxx20
},
6884 {"bitset", "<bitset>", cxx11
},
6886 {"weak_equality", "<compare>", cxx20
},
6887 {"strong_equality", "<compare>", cxx20
},
6888 {"partial_ordering", "<compare>", cxx20
},
6889 {"weak_ordering", "<compare>", cxx20
},
6890 {"strong_ordering", "<compare>", cxx20
},
6892 {"complex", "<complex>", cxx98
},
6893 {"complex_literals", "<complex>", cxx14
},
6894 /* <condition_variable>. */
6895 {"condition_variable", "<condition_variable>", cxx11
},
6896 {"condition_variable_any", "<condition_variable>", cxx11
},
6898 {"byte", "<cstddef>", cxx17
},
6900 {"deque", "<deque>", cxx98
},
6901 /* <forward_list>. */
6902 {"forward_list", "<forward_list>", cxx11
},
6904 {"basic_filebuf", "<fstream>", cxx98
},
6905 {"basic_ifstream", "<fstream>", cxx98
},
6906 {"basic_ofstream", "<fstream>", cxx98
},
6907 {"basic_fstream", "<fstream>", cxx98
},
6908 {"fstream", "<fstream>", cxx98
},
6909 {"ifstream", "<fstream>", cxx98
},
6910 {"ofstream", "<fstream>", cxx98
},
6912 {"bind", "<functional>", cxx11
},
6913 {"bind_front", "<functional>", cxx20
},
6914 {"function", "<functional>", cxx11
},
6915 {"hash", "<functional>", cxx11
},
6916 {"invoke", "<functional>", cxx17
},
6917 {"mem_fn", "<functional>", cxx11
},
6918 {"not_fn", "<functional>", cxx17
},
6919 {"reference_wrapper", "<functional>", cxx11
},
6920 {"unwrap_reference", "<functional>", cxx20
},
6921 {"unwrap_reference_t", "<functional>", cxx20
},
6922 {"unwrap_ref_decay", "<functional>", cxx20
},
6923 {"unwrap_ref_decay_t", "<functional>", cxx20
},
6925 {"async", "<future>", cxx11
},
6926 {"future", "<future>", cxx11
},
6927 {"packaged_task", "<future>", cxx11
},
6928 {"promise", "<future>", cxx11
},
6930 {"cin", "<iostream>", cxx98
},
6931 {"cout", "<iostream>", cxx98
},
6932 {"cerr", "<iostream>", cxx98
},
6933 {"clog", "<iostream>", cxx98
},
6934 {"wcin", "<iostream>", cxx98
},
6935 {"wcout", "<iostream>", cxx98
},
6936 {"wclog", "<iostream>", cxx98
},
6938 {"istream", "<istream>", cxx98
},
6940 {"advance", "<iterator>", cxx98
},
6941 {"back_inserter", "<iterator>", cxx98
},
6942 {"begin", "<iterator>", cxx11
},
6943 {"distance", "<iterator>", cxx98
},
6944 {"end", "<iterator>", cxx11
},
6945 {"front_inserter", "<iterator>", cxx98
},
6946 {"inserter", "<iterator>", cxx98
},
6947 {"istream_iterator", "<iterator>", cxx98
},
6948 {"istreambuf_iterator", "<iterator>", cxx98
},
6949 {"iterator_traits", "<iterator>", cxx98
},
6950 {"move_iterator", "<iterator>", cxx11
},
6951 {"next", "<iterator>", cxx11
},
6952 {"ostream_iterator", "<iterator>", cxx98
},
6953 {"ostreambuf_iterator", "<iterator>", cxx98
},
6954 {"prev", "<iterator>", cxx11
},
6955 {"reverse_iterator", "<iterator>", cxx98
},
6957 {"ostream", "<ostream>", cxx98
},
6959 {"list", "<list>", cxx98
},
6961 {"map", "<map>", cxx98
},
6962 {"multimap", "<map>", cxx98
},
6964 {"allocate_shared", "<memory>", cxx11
},
6965 {"allocator", "<memory>", cxx98
},
6966 {"allocator_traits", "<memory>", cxx11
},
6967 {"make_shared", "<memory>", cxx11
},
6968 {"make_unique", "<memory>", cxx14
},
6969 {"shared_ptr", "<memory>", cxx11
},
6970 {"unique_ptr", "<memory>", cxx11
},
6971 {"weak_ptr", "<memory>", cxx11
},
6972 /* <memory_resource>. */
6973 {"pmr", "<memory_resource>", cxx17
},
6975 {"mutex", "<mutex>", cxx11
},
6976 {"timed_mutex", "<mutex>", cxx11
},
6977 {"recursive_mutex", "<mutex>", cxx11
},
6978 {"recursive_timed_mutex", "<mutex>", cxx11
},
6979 {"once_flag", "<mutex>", cxx11
},
6980 {"call_once,", "<mutex>", cxx11
},
6981 {"lock", "<mutex>", cxx11
},
6982 {"scoped_lock", "<mutex>", cxx17
},
6983 {"try_lock", "<mutex>", cxx11
},
6984 {"lock_guard", "<mutex>", cxx11
},
6985 {"unique_lock", "<mutex>", cxx11
},
6987 {"optional", "<optional>", cxx17
},
6988 {"make_optional", "<optional>", cxx17
},
6990 {"ostream", "<ostream>", cxx98
},
6991 {"wostream", "<ostream>", cxx98
},
6992 {"ends", "<ostream>", cxx98
},
6993 {"flush", "<ostream>", cxx98
},
6994 {"endl", "<ostream>", cxx98
},
6996 {"queue", "<queue>", cxx98
},
6997 {"priority_queue", "<queue>", cxx98
},
6999 {"set", "<set>", cxx98
},
7000 {"multiset", "<set>", cxx98
},
7001 /* <shared_mutex>. */
7002 {"shared_lock", "<shared_mutex>", cxx14
},
7003 {"shared_mutex", "<shared_mutex>", cxx17
},
7004 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
7005 /* <source_location>. */
7006 {"source_location", "<source_location>", cxx20
},
7008 {"basic_stringbuf", "<sstream>", cxx98
},
7009 {"basic_istringstream", "<sstream>", cxx98
},
7010 {"basic_ostringstream", "<sstream>", cxx98
},
7011 {"basic_stringstream", "<sstream>", cxx98
},
7012 {"istringstream", "<sstream>", cxx98
},
7013 {"ostringstream", "<sstream>", cxx98
},
7014 {"stringstream", "<sstream>", cxx98
},
7016 {"stack", "<stack>", cxx98
},
7018 {"basic_string", "<string>", cxx98
},
7019 {"string", "<string>", cxx98
},
7020 {"wstring", "<string>", cxx98
},
7021 {"u8string", "<string>", cxx20
},
7022 {"u16string", "<string>", cxx11
},
7023 {"u32string", "<string>", cxx11
},
7024 /* <string_view>. */
7025 {"basic_string_view", "<string_view>", cxx17
},
7026 {"string_view", "<string_view>", cxx17
},
7028 {"thread", "<thread>", cxx11
},
7029 {"this_thread", "<thread>", cxx11
},
7031 {"apply", "<tuple>", cxx17
},
7032 {"forward_as_tuple", "<tuple>", cxx11
},
7033 {"make_from_tuple", "<tuple>", cxx17
},
7034 {"make_tuple", "<tuple>", cxx11
},
7035 {"tie", "<tuple>", cxx11
},
7036 {"tuple", "<tuple>", cxx11
},
7037 {"tuple_cat", "<tuple>", cxx11
},
7038 {"tuple_element", "<tuple>", cxx11
},
7039 {"tuple_element_t", "<tuple>", cxx14
},
7040 {"tuple_size", "<tuple>", cxx11
},
7041 {"tuple_size_v", "<tuple>", cxx17
},
7042 /* <type_traits>. */
7043 {"enable_if", "<type_traits>", cxx11
},
7044 {"enable_if_t", "<type_traits>", cxx14
},
7045 {"invoke_result", "<type_traits>", cxx17
},
7046 {"invoke_result_t", "<type_traits>", cxx17
},
7047 {"remove_cvref", "<type_traits>", cxx20
},
7048 {"remove_cvref_t", "<type_traits>", cxx20
},
7049 {"type_identity", "<type_traits>", cxx20
},
7050 {"type_identity_t", "<type_traits>", cxx20
},
7051 {"void_t", "<type_traits>", cxx17
},
7052 {"conjunction", "<type_traits>", cxx17
},
7053 {"conjunction_v", "<type_traits>", cxx17
},
7054 {"disjunction", "<type_traits>", cxx17
},
7055 {"disjunction_v", "<type_traits>", cxx17
},
7056 {"negation", "<type_traits>", cxx17
},
7057 {"negation_v", "<type_traits>", cxx17
},
7058 /* <unordered_map>. */
7059 {"unordered_map", "<unordered_map>", cxx11
},
7060 {"unordered_multimap", "<unordered_map>", cxx11
},
7061 /* <unordered_set>. */
7062 {"unordered_set", "<unordered_set>", cxx11
},
7063 {"unordered_multiset", "<unordered_set>", cxx11
},
7065 {"declval", "<utility>", cxx11
},
7066 {"forward", "<utility>", cxx11
},
7067 {"make_pair", "<utility>", cxx98
},
7068 {"move", "<utility>", cxx11
},
7069 {"pair", "<utility>", cxx98
},
7071 {"variant", "<variant>", cxx17
},
7072 {"visit", "<variant>", cxx17
},
7074 {"vector", "<vector>", cxx98
},
7076 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
7077 for (size_t i
= 0; i
< num_hints
; i
++)
7079 if (strcmp (name
, hints
[i
].name
) == 0)
7085 /* Describe DIALECT. */
7088 get_cxx_dialect_name (enum cxx_dialect dialect
)
7107 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7108 that weren't recognized, but for which we know which header it ought to be
7111 Emit a note either suggesting the header to be included, or noting that
7112 the current dialect is too early for the given name. */
7114 class missing_std_header
: public deferred_diagnostic
7117 missing_std_header (location_t loc
,
7118 const char *name_str
,
7119 const std_name_hint
*header_hint
)
7120 : deferred_diagnostic (loc
),
7121 m_name_str (name_str
),
7122 m_header_hint (header_hint
)
7124 ~missing_std_header ()
7126 gcc_rich_location
richloc (get_location ());
7127 if (cxx_dialect
>= m_header_hint
->min_dialect
)
7129 const char *header
= m_header_hint
->header
;
7130 maybe_add_include_fixit (&richloc
, header
, true);
7132 "%<std::%s%> is defined in header %qs;"
7133 " did you forget to %<#include %s%>?",
7134 m_name_str
, header
, header
);
7138 "%<std::%s%> is only available from %s onwards",
7139 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
7143 const char *m_name_str
;
7144 const std_name_hint
*m_header_hint
;
7147 /* Attempt to generate a name_hint that suggests pertinent header files
7148 for NAME at LOCATION, for common names within the "std" namespace,
7149 or an empty name_hint if this isn't applicable. */
7152 maybe_suggest_missing_std_header (location_t location
, tree name
)
7154 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
7156 const char *name_str
= IDENTIFIER_POINTER (name
);
7157 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
7159 return name_hint ();
7161 return name_hint (NULL
, new missing_std_header (location
, name_str
,
7165 /* Attempt to generate a name_hint that suggests a missing header file
7166 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7170 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
7172 if (scope
== NULL_TREE
)
7173 return name_hint ();
7174 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
7175 return name_hint ();
7176 /* We only offer suggestions for the "std" namespace. */
7177 if (scope
!= std_node
)
7178 return name_hint ();
7179 return maybe_suggest_missing_std_header (location
, name
);
7182 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7183 lookup failed within the explicitly provided SCOPE.
7185 Suggest the best meaningful candidates (if any), otherwise
7186 an empty name_hint is returned. */
7189 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
7192 /* Something went very wrong; don't suggest anything. */
7193 if (name
== error_mark_node
)
7194 return name_hint ();
7196 /* Resolve any namespace aliases. */
7197 scope
= ORIGINAL_NAMESPACE (scope
);
7199 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
7203 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
7205 best_match
<tree
, const char *> bm (name
);
7206 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
7208 /* See if we have a good suggesion for the user. */
7209 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
7211 return name_hint (fuzzy_name
, NULL
);
7213 return name_hint ();
7216 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7220 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
7222 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
7224 best_match
<tree
, const char *> bm (name
);
7225 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
7227 tree id
= TREE_PURPOSE (iter
);
7228 bm
.consider (IDENTIFIER_POINTER (id
));
7230 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7233 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7236 WANT as for lookup_name_1.
7238 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7239 declaration found. If no suitable declaration can be found,
7240 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7241 neither a class-type nor a namespace a diagnostic is issued. */
7244 lookup_qualified_name (tree scope
, tree name
, LOOK_want want
, bool complain
)
7248 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
7250 name_lookup
lookup (name
, want
);
7252 if (qualified_namespace_lookup (scope
, &lookup
))
7256 /* If we have a known type overload, pull it out. This can happen
7258 if (TREE_CODE (t
) == OVERLOAD
&& TREE_TYPE (t
) != unknown_type_node
)
7259 t
= OVL_FUNCTION (t
);
7262 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
7263 t
= lookup_enumerator (scope
, name
);
7264 else if (is_class_type (scope
, complain
))
7265 t
= lookup_member (scope
, name
, 2, bool (want
& LOOK_want::TYPE
),
7266 tf_warning_or_error
);
7269 return error_mark_node
;
7273 /* Wrapper for the above that takes a string argument. The function name is
7274 not at the beginning of the line to keep this wrapper out of etags. */
7276 tree
lookup_qualified_name (tree t
, const char *p
, LOOK_want w
, bool c
)
7278 return lookup_qualified_name (t
, get_identifier (p
), w
, c
);
7282 Accepts the NAME to lookup and its qualifying SCOPE.
7283 Returns the name/type pair found into the cxx_binding *RESULT,
7284 or false on error. */
7287 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
7289 timevar_start (TV_NAME_LOOKUP
);
7290 query_oracle (lookup
->name
);
7291 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
7292 timevar_stop (TV_NAME_LOOKUP
);
7296 /* If DECL is suitably visible to the user, consider its name for
7297 spelling correction. */
7300 consider_decl (tree decl
, best_match
<tree
, const char *> &bm
,
7301 bool consider_impl_names
)
7303 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7304 within range for). */
7305 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7308 tree suggestion
= DECL_NAME (decl
);
7312 /* Don't suggest names that are for anonymous aggregate types, as
7313 they are an implementation detail generated by the compiler. */
7314 if (IDENTIFIER_ANON_P (suggestion
))
7317 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
7319 /* Ignore internal names with spaces in them. */
7320 if (strchr (suggestion_str
, ' '))
7323 /* Don't suggest names that are reserved for use by the
7324 implementation, unless NAME began with an underscore. */
7325 if (!consider_impl_names
7326 && name_reserved_for_implementation_p (suggestion_str
))
7329 bm
.consider (suggestion_str
);
7332 /* If DECL is suitably visible to the user, add its name to VEC and
7333 return true. Otherwise return false. */
7336 maybe_add_fuzzy_decl (auto_vec
<tree
> &vec
, tree decl
)
7338 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7339 within range for). */
7340 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7343 tree suggestion
= DECL_NAME (decl
);
7347 /* Don't suggest names that are for anonymous aggregate types, as
7348 they are an implementation detail generated by the compiler. */
7349 if (IDENTIFIER_ANON_P (suggestion
))
7352 vec
.safe_push (suggestion
);
7357 /* Examing the namespace binding BINDING, and add at most one instance
7358 of the name, if it contains a visible entity of interest. Return
7359 true if we added something. */
7362 maybe_add_fuzzy_binding (auto_vec
<tree
> &vec
, tree binding
,
7363 lookup_name_fuzzy_kind kind
)
7365 tree value
= NULL_TREE
;
7367 if (STAT_HACK_P (binding
))
7369 if (!STAT_TYPE_HIDDEN_P (binding
)
7370 && STAT_TYPE (binding
))
7372 if (maybe_add_fuzzy_decl (vec
, STAT_TYPE (binding
)))
7375 else if (!STAT_DECL_HIDDEN_P (binding
))
7376 value
= STAT_DECL (binding
);
7381 value
= ovl_skip_hidden (value
);
7384 value
= OVL_FIRST (value
);
7385 if (kind
!= FUZZY_LOOKUP_TYPENAME
7386 || TREE_CODE (STRIP_TEMPLATE (value
)) == TYPE_DECL
)
7387 if (maybe_add_fuzzy_decl (vec
, value
))
7391 /* Nothing found. */
7395 /* Helper function for lookup_name_fuzzy.
7396 Traverse binding level LVL, looking for good name matches for NAME
7399 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
7400 cp_binding_level
*lvl
, bool look_within_fields
,
7401 enum lookup_name_fuzzy_kind kind
)
7403 if (look_within_fields
)
7404 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
7406 tree type
= lvl
->this_entity
;
7407 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
7408 tree best_matching_field
7409 = lookup_member_fuzzy (type
, name
, want_type_p
);
7410 if (best_matching_field
)
7411 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
7414 /* Only suggest names reserved for the implementation if NAME begins
7415 with an underscore. */
7416 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
7418 if (lvl
->kind
!= sk_namespace
)
7419 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
7423 /* OVERLOADs or decls from using declaration are wrapped into
7425 if (TREE_CODE (d
) == TREE_LIST
)
7426 d
= OVL_FIRST (TREE_VALUE (d
));
7428 /* Don't use bindings from implicitly declared functions,
7429 as they were likely misspellings themselves. */
7430 if (TREE_TYPE (d
) == error_mark_node
)
7433 /* If we want a typename, ignore non-types. */
7434 if (kind
== FUZZY_LOOKUP_TYPENAME
7435 && TREE_CODE (STRIP_TEMPLATE (d
)) != TYPE_DECL
)
7438 consider_decl (d
, bm
, consider_implementation_names
);
7442 /* We need to iterate over the namespace hash table, in order to
7443 not mention hidden entities. But hash table iteration is
7444 (essentially) unpredictable, our correction-distance measure
7445 is very granular, and we pick the first of equal distances.
7446 Hence, we need to call the distance-measurer in a predictable
7447 order. So, iterate over the namespace hash, inserting
7448 visible names into a vector. Then sort the vector. Then
7449 determine spelling distance. */
7451 tree ns
= lvl
->this_entity
;
7454 hash_table
<named_decl_hash
>::iterator end
7455 (DECL_NAMESPACE_BINDINGS (ns
)->end ());
7456 for (hash_table
<named_decl_hash
>::iterator iter
7457 (DECL_NAMESPACE_BINDINGS (ns
)->begin ()); iter
!= end
; ++iter
)
7459 tree binding
= *iter
;
7461 if (TREE_CODE (binding
) == BINDING_VECTOR
)
7463 bitmap imports
= get_import_bitmap ();
7464 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
7466 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
7467 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7470 /* Scan the imported bindings. */
7471 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
7472 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
7478 for (; ix
--; cluster
++)
7479 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
7482 /* Are we importing this module? */
7483 if (unsigned base
= cluster
->indices
[jx
].base
)
7484 if (unsigned span
= cluster
->indices
[jx
].span
)
7486 if (bitmap_bit_p (imports
, base
))
7488 while (++base
, --span
);
7493 if (cluster
->slots
[jx
].is_lazy ())
7494 /* Let's not read in everything on the first
7497 if (tree bind
= cluster
->slots
[jx
])
7498 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7503 maybe_add_fuzzy_binding (vec
, binding
, kind
);
7506 vec
.qsort ([] (const void *a_
, const void *b_
)
7508 return strcmp (IDENTIFIER_POINTER (*(const tree
*)a_
),
7509 IDENTIFIER_POINTER (*(const tree
*)b_
));
7512 /* Examine longest to shortest. */
7513 for (unsigned ix
= vec
.length (); ix
--;)
7515 const char *str
= IDENTIFIER_POINTER (vec
[ix
]);
7517 /* Ignore internal names with spaces in them. */
7518 if (strchr (str
, ' '))
7521 /* Don't suggest names that are reserved for use by the
7522 implementation, unless NAME began with an underscore. */
7523 if (!consider_implementation_names
7524 && name_reserved_for_implementation_p (str
))
7532 /* Subclass of deferred_diagnostic. Notify the user that the
7533 given macro was used before it was defined.
7534 This can be done in the C++ frontend since tokenization happens
7537 class macro_use_before_def
: public deferred_diagnostic
7540 /* Factory function. Return a new macro_use_before_def instance if
7541 appropriate, or return NULL. */
7542 static macro_use_before_def
*
7543 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
7545 location_t def_loc
= cpp_macro_definition_location (macro
);
7546 if (def_loc
== UNKNOWN_LOCATION
)
7549 /* We only want to issue a note if the macro was used *before* it was
7551 We don't want to issue a note for cases where a macro was incorrectly
7552 used, leaving it unexpanded (e.g. by using the wrong argument
7554 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
7557 return new macro_use_before_def (use_loc
, macro
);
7561 /* Ctor. LOC is the location of the usage. MACRO is the
7562 macro that was used. */
7563 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
7564 : deferred_diagnostic (loc
), m_macro (macro
)
7569 ~macro_use_before_def ()
7571 if (is_suppressed_p ())
7574 inform (get_location (), "the macro %qs had not yet been defined",
7575 (const char *)m_macro
->ident
.str
);
7576 inform (cpp_macro_definition_location (m_macro
),
7577 "it was later defined here");
7581 cpp_hashnode
*m_macro
;
7584 /* Determine if it can ever make sense to offer RID as a suggestion for
7587 Subroutine of lookup_name_fuzzy. */
7590 suggest_rid_p (enum rid rid
)
7594 /* Support suggesting function-like keywords. */
7595 case RID_STATIC_ASSERT
:
7599 /* Support suggesting the various decl-specifier words, to handle
7600 e.g. "singed" vs "signed" typos. */
7601 if (cp_keyword_starts_decl_specifier_p (rid
))
7604 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7605 and "do" for short misspellings, which are likely to lead to
7606 nonsensical results. */
7611 /* Search for near-matches for NAME within the current bindings, and within
7612 macro names, returning the best match as a const char *, or NULL if
7613 no reasonable match is found.
7615 Use LOC for any deferred diagnostics. */
7618 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
7620 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
7622 /* First, try some well-known names in the C++ standard library, in case
7623 the user forgot a #include. */
7624 const char *header_hint
7625 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
7627 return name_hint (NULL
,
7628 new suggest_missing_header (loc
,
7629 IDENTIFIER_POINTER (name
),
7632 best_match
<tree
, const char *> bm (name
);
7634 cp_binding_level
*lvl
;
7635 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
7636 consider_binding_level (name
, bm
, lvl
, true, kind
);
7638 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
7639 consider_binding_level (name
, bm
, lvl
, false, kind
);
7641 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7643 x = SOME_OTHER_MACRO (y);
7644 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7645 as a misspelled identifier.
7647 Use the best distance so far so that a candidate is only set if
7648 a macro is better than anything so far. This allows early rejection
7649 (without calculating the edit distance) of macro names that must have
7650 distance >= bm.get_best_distance (), and means that we only get a
7651 non-NULL result for best_macro_match if it's better than any of
7652 the identifiers already checked. */
7653 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
7654 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
7655 /* If a macro is the closest so far to NAME, consider it. */
7657 bm
.consider ((const char *)best_macro
->ident
.str
);
7658 else if (bmm
.get_best_distance () == 0)
7660 /* If we have an exact match for a macro name, then either the
7661 macro was used with the wrong argument count, or the macro
7662 has been used before it was defined. */
7663 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
7664 if (cpp_user_macro_p (macro
))
7665 return name_hint (NULL
,
7666 macro_use_before_def::maybe_make (loc
, macro
));
7669 /* Try the "starts_decl_specifier_p" keywords to detect
7670 "singed" vs "signed" typos. */
7671 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
7673 const c_common_resword
*resword
= &c_common_reswords
[i
];
7675 if (!suggest_rid_p (resword
->rid
))
7678 tree resword_identifier
= ridpointers
[resword
->rid
];
7679 if (!resword_identifier
)
7681 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
7683 /* Only consider reserved words that survived the
7684 filtering in init_reswords (e.g. for -std). */
7685 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
7688 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
7691 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7694 /* Subroutine of outer_binding.
7696 Returns TRUE if BINDING is a binding to a template parameter of
7697 SCOPE. In that case SCOPE is the scope of a primary template
7698 parameter -- in the sense of G++, i.e, a template that has its own
7701 Returns FALSE otherwise. */
7704 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
7705 cp_binding_level
*scope
)
7707 tree binding_value
, tmpl
, tinfo
;
7710 if (!binding
|| !scope
|| !scope
->this_entity
)
7713 binding_value
= binding
->value
? binding
->value
: binding
->type
;
7714 tinfo
= get_template_info (scope
->this_entity
);
7716 /* BINDING_VALUE must be a template parm. */
7717 if (binding_value
== NULL_TREE
7718 || (!DECL_P (binding_value
)
7719 || !DECL_TEMPLATE_PARM_P (binding_value
)))
7722 /* The level of BINDING_VALUE. */
7724 template_type_parameter_p (binding_value
)
7725 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7726 (TREE_TYPE (binding_value
)))
7727 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
7729 /* The template of the current scope, iff said scope is a primary
7732 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
7733 ? TI_TEMPLATE (tinfo
)
7736 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7737 then BINDING_VALUE is a parameter of TMPL. */
7738 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
7741 /* Return the innermost non-namespace binding for NAME from a scope
7742 containing BINDING, or, if BINDING is NULL, the current scope.
7743 Please note that for a given template, the template parameters are
7744 considered to be in the scope containing the current scope.
7745 If CLASS_P is false, then class bindings are ignored. */
7748 outer_binding (tree name
,
7749 cxx_binding
*binding
,
7753 cp_binding_level
*scope
;
7754 cp_binding_level
*outer_scope
;
7758 scope
= binding
->scope
->level_chain
;
7759 outer
= binding
->previous
;
7763 scope
= current_binding_level
;
7764 outer
= IDENTIFIER_BINDING (name
);
7766 outer_scope
= outer
? outer
->scope
: NULL
;
7768 /* Because we create class bindings lazily, we might be missing a
7769 class binding for NAME. If there are any class binding levels
7770 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7771 declared, we must lookup NAME in those class scopes. */
7773 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
7775 if (scope
->kind
== sk_class
)
7777 cxx_binding
*class_binding
;
7779 class_binding
= get_class_binding (name
, scope
);
7782 /* Thread this new class-scope binding onto the
7783 IDENTIFIER_BINDING list so that future lookups
7785 class_binding
->previous
= outer
;
7787 binding
->previous
= class_binding
;
7789 IDENTIFIER_BINDING (name
) = class_binding
;
7790 return class_binding
;
7793 /* If we are in a member template, the template parms of the member
7794 template are considered to be inside the scope of the containing
7795 class, but within G++ the class bindings are all pushed between the
7796 template parms and the function body. So if the outer binding is
7797 a template parm for the current scope, return it now rather than
7798 look for a class binding. */
7799 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
7800 && binding_to_template_parms_of_scope_p (outer
, scope
))
7803 scope
= scope
->level_chain
;
7809 /* Return the innermost block-scope or class-scope value binding for
7810 NAME, or NULL_TREE if there is no such binding. */
7813 innermost_non_namespace_value (tree name
)
7815 cxx_binding
*binding
;
7816 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
7817 return binding
? binding
->value
: NULL_TREE
;
7820 /* Look up NAME in the current binding level and its superiors in the
7821 namespace of variables, functions and typedefs. Return a ..._DECL
7822 node of some kind representing its definition if there is only one
7823 such declaration, or return a TREE_LIST with all the overloaded
7824 definitions if there are many, or return NULL_TREE if it is undefined.
7825 Hidden name, either friend declaration or built-in function, are
7828 WHERE controls which scopes are considered. It is a bit mask of
7829 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7830 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7831 scopes). It is an error for no bits to be set. These scopes are
7832 searched from innermost to outermost.
7834 WANT controls what kind of entity we'd happy with.
7835 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7836 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7837 for only NAMESPACE_DECLS. These two can be bit-ored to find
7840 WANT can also have LOOK_want::HIDDEN_FRIEND or
7841 LOOK_want::HIDDEN_LAMBDa added to it. */
7844 lookup_name_1 (tree name
, LOOK_where where
, LOOK_want want
)
7846 tree val
= NULL_TREE
;
7848 gcc_checking_assert (unsigned (where
) != 0);
7849 /* If we're looking for hidden lambda things, we shouldn't be
7850 looking in namespace scope. */
7851 gcc_checking_assert (!bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7852 || !bool (where
& LOOK_where::NAMESPACE
));
7853 query_oracle (name
);
7855 /* Conversion operators are handled specially because ordinary
7856 unqualified name lookup will not find template conversion
7858 if (IDENTIFIER_CONV_OP_P (name
))
7860 cp_binding_level
*level
;
7862 for (level
= current_binding_level
;
7863 level
&& level
->kind
!= sk_namespace
;
7864 level
= level
->level_chain
)
7869 /* A conversion operator can only be declared in a class
7871 if (level
->kind
!= sk_class
)
7874 /* Lookup the conversion operator in the class. */
7875 class_type
= level
->this_entity
;
7876 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0,
7877 tf_warning_or_error
);
7885 /* First, look in non-namespace scopes. */
7887 if (current_class_type
== NULL_TREE
)
7888 /* Maybe avoid searching the binding stack at all. */
7889 where
= LOOK_where (unsigned (where
) & ~unsigned (LOOK_where::CLASS
));
7891 if (bool (where
& (LOOK_where::BLOCK
| LOOK_where::CLASS
)))
7892 for (cxx_binding
*iter
= nullptr;
7893 (iter
= outer_binding (name
, iter
, bool (where
& LOOK_where::CLASS
)));)
7895 /* Skip entities we don't want. */
7896 if (!bool (where
& (LOCAL_BINDING_P (iter
)
7897 ? LOOK_where::BLOCK
: LOOK_where::CLASS
)))
7900 /* If this is the kind of thing we're looking for, we're done. */
7903 tree binding
= NULL_TREE
;
7905 if (!(!iter
->type
&& HIDDEN_TYPE_BINDING_P (iter
))
7906 && (bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7907 || !is_lambda_ignored_entity (iter
->value
))
7908 && qualify_lookup (iter
->value
, want
))
7909 binding
= iter
->value
;
7910 else if (bool (want
& LOOK_want::TYPE
)
7911 && !HIDDEN_TYPE_BINDING_P (iter
)
7913 binding
= iter
->type
;
7917 /* The saved lookups for an operator record 'nothing
7918 found' as error_mark_node. We need to stop the search
7919 here, but not return the error mark node. */
7920 if (binding
== error_mark_node
)
7921 binding
= NULL_TREE
;
7929 /* Now lookup in namespace scopes. */
7930 if (bool (where
& LOOK_where::NAMESPACE
))
7932 name_lookup
lookup (name
, want
);
7933 if (lookup
.search_unqualified
7934 (current_decl_namespace (), current_binding_level
))
7940 /* If we have a known type overload, pull it out. This can happen
7941 for both using decls and unhidden functions. */
7942 if (val
&& TREE_CODE (val
) == OVERLOAD
&& TREE_TYPE (val
) != unknown_type_node
)
7943 val
= OVL_FUNCTION (val
);
7948 /* Wrapper for lookup_name_1. */
7951 lookup_name (tree name
, LOOK_where where
, LOOK_want want
)
7953 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7954 tree ret
= lookup_name_1 (name
, where
, want
);
7955 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7960 lookup_name (tree name
)
7962 return lookup_name (name
, LOOK_where::ALL
, LOOK_want::NORMAL
);
7965 /* Look up NAME for type used in elaborated name specifier in
7966 the scopes given by HOW.
7968 Unlike lookup_name_1, we make sure that NAME is actually
7969 declared in the desired scope, not from inheritance, nor using
7970 directive. For using declaration, there is DR138 still waiting
7971 to be resolved. Hidden name coming from an earlier friend
7972 declaration is also returned, and will be made visible unless HOW
7973 is TAG_how::HIDDEN_FRIEND.
7975 A TYPE_DECL best matching the NAME is returned. Catching error
7976 and issuing diagnostics are caller's responsibility. */
7979 lookup_elaborated_type_1 (tree name
, TAG_how how
)
7981 cp_binding_level
*b
= current_binding_level
;
7983 if (b
->kind
!= sk_namespace
)
7984 /* Look in non-namespace scopes. */
7985 for (cxx_binding
*iter
= NULL
;
7986 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
7988 /* First check we're supposed to be looking in this scope --
7989 if we're not, we're done. */
7990 for (; b
!= iter
->scope
; b
= b
->level_chain
)
7991 if (!(b
->kind
== sk_cleanup
7992 || b
->kind
== sk_template_parms
7993 || b
->kind
== sk_function_parms
7994 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
7997 /* Check if this is the kind of thing we're looking for. If
7998 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
7999 come from base class. For ITER->VALUE, we can simply use
8000 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
8003 We check ITER->TYPE before ITER->VALUE in order to handle
8004 typedef struct C {} C;
8007 if (tree type
= iter
->type
)
8009 if (qualify_lookup (type
, LOOK_want::TYPE
)
8010 && (how
!= TAG_how::CURRENT_ONLY
8011 || LOCAL_BINDING_P (iter
)
8012 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
8014 if (how
!= TAG_how::HIDDEN_FRIEND
)
8015 /* It is no longer a hidden binding. */
8016 HIDDEN_TYPE_BINDING_P (iter
) = false;
8023 if (qualify_lookup (iter
->value
, LOOK_want::TYPE
)
8024 && (how
!= TAG_how::CURRENT_ONLY
8025 || !INHERITED_VALUE_BINDING_P (iter
)))
8027 if (how
!= TAG_how::HIDDEN_FRIEND
&& !iter
->type
)
8028 /* It is no longer a hidden binding. */
8029 HIDDEN_TYPE_BINDING_P (iter
) = false;
8036 /* Now check if we can look in namespace scope. */
8037 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
8038 if (!(b
->kind
== sk_cleanup
8039 || b
->kind
== sk_template_parms
8040 || b
->kind
== sk_function_parms
8041 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
8044 /* Look in the innermost namespace. */
8045 tree ns
= b
->this_entity
;
8046 if (tree
*slot
= find_namespace_slot (ns
, name
))
8049 if (TREE_CODE (bind
) == BINDING_VECTOR
)
8050 bind
= BINDING_VECTOR_CLUSTER (bind
, 0).slots
[BINDING_SLOT_CURRENT
];
8054 /* If this is the kind of thing we're looking for, we're done. */
8055 if (tree type
= MAYBE_STAT_TYPE (bind
))
8057 if (how
!= TAG_how::HIDDEN_FRIEND
)
8058 /* No longer hidden. */
8059 STAT_TYPE_HIDDEN_P (*slot
) = false;
8063 else if (tree decl
= MAYBE_STAT_DECL (bind
))
8065 if (qualify_lookup (decl
, LOOK_want::TYPE
))
8067 if (how
!= TAG_how::HIDDEN_FRIEND
&& STAT_HACK_P (bind
)
8068 && STAT_DECL_HIDDEN_P (bind
))
8070 if (STAT_TYPE (bind
))
8071 STAT_DECL_HIDDEN_P (bind
) = false;
8074 /* There is no type, just remove the stat
8079 BINDING_VECTOR_CLUSTER (bind
, 0)
8080 .slots
[BINDING_SLOT_CURRENT
] = decl
;
8088 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
8090 /* We could be redeclaring a global module entity, (from GMF
8091 or header unit), or from another partition, or
8092 specializing an imported template. */
8093 bitmap imports
= get_import_bitmap ();
8094 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
8096 /* Scan the imported bindings. */
8097 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
8098 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
8104 /* Do this in forward order, so we load modules in an order
8105 the user expects. */
8106 for (; ix
--; cluster
++)
8107 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
8109 /* Are we importing this module? */
8110 if (unsigned base
= cluster
->indices
[jx
].base
)
8111 if (unsigned span
= cluster
->indices
[jx
].span
)
8113 if (bitmap_bit_p (imports
, base
))
8115 while (++base
, --span
);
8120 if (cluster
->slots
[jx
].is_lazy ())
8122 gcc_assert (cluster
->indices
[jx
].span
== 1);
8123 lazy_load_binding (cluster
->indices
[jx
].base
,
8124 ns
, name
, &cluster
->slots
[jx
]);
8126 tree bind
= cluster
->slots
[jx
];
8128 /* Load errors could mean there's nothing here. */
8131 /* Extract what we can see from here. If there's no
8132 stat_hack, then everything was exported. */
8133 tree type
= NULL_TREE
;
8135 /* If no stat hack, everything is visible. */
8136 if (STAT_HACK_P (bind
))
8138 if (STAT_TYPE_VISIBLE_P (bind
))
8139 type
= STAT_TYPE (bind
);
8140 bind
= STAT_VISIBLE (bind
);
8143 if (type
&& qualify_lookup (type
, LOOK_want::TYPE
))
8146 if (bind
&& qualify_lookup (bind
, LOOK_want::TYPE
))
8150 if (!module_purview_p ())
8152 /* We're in the global module, perhaps there's a tag
8154 // FIXME: This isn't quite right, if we find something
8155 // here, from the language PoV we're not supposed to
8164 /* Wrapper for lookup_type_scope_1. */
8167 lookup_elaborated_type (tree name
, TAG_how how
)
8169 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8170 tree ret
= lookup_elaborated_type_1 (name
, how
);
8171 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8175 /* The type TYPE is being declared. If it is a class template, or a
8176 specialization of a class template, do any processing required and
8177 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8178 being declared a friend. B is the binding level at which this TYPE
8181 Returns the TYPE_DECL for TYPE, which may have been altered by this
8185 maybe_process_template_type_declaration (tree type
, int is_friend
,
8186 cp_binding_level
*b
)
8188 tree decl
= TYPE_NAME (type
);
8190 if (processing_template_parmlist
)
8191 /* You can't declare a new template type in a template parameter
8192 list. But, you can declare a non-template type:
8194 template <class A*> struct S;
8196 is a forward-declaration of `A'. */
8198 else if (b
->kind
== sk_namespace
8199 && current_binding_level
->kind
!= sk_namespace
)
8200 /* If this new type is being injected into a containing scope,
8201 then it's not a template type. */
8205 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
8206 || TREE_CODE (type
) == ENUMERAL_TYPE
);
8208 if (processing_template_decl
)
8210 decl
= push_template_decl (decl
, is_friend
);
8211 if (decl
== error_mark_node
)
8212 return error_mark_node
;
8214 /* If the current binding level is the binding level for the
8215 template parameters (see the comment in
8216 begin_template_parm_list) and the enclosing level is a class
8217 scope, and we're not looking at a friend, push the
8218 declaration of the member class into the class scope. In the
8219 friend case, push_template_decl will already have put the
8220 friend into global scope, if appropriate. */
8221 if (TREE_CODE (type
) != ENUMERAL_TYPE
8222 && !is_friend
&& b
->kind
== sk_template_parms
8223 && b
->level_chain
->kind
== sk_class
)
8225 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
8227 if (!COMPLETE_TYPE_P (current_class_type
))
8228 maybe_add_class_template_decl_list (current_class_type
,
8229 type
, /*friend_p=*/0);
8237 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8238 that the NAME is a class template, the tag is processed but not pushed.
8240 The pushed scope depend on the SCOPE parameter:
8241 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8243 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8244 non-template-parameter scope. This case is needed for forward
8246 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8247 TS_GLOBAL case except that names within template-parameter scopes
8248 are not pushed at all.
8250 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8253 do_pushtag (tree name
, tree type
, TAG_how how
)
8257 cp_binding_level
*b
= current_binding_level
;
8260 if (/* Cleanup scopes are not scopes from the point of view of
8262 b
->kind
== sk_cleanup
8263 /* Neither are function parameter scopes. */
8264 || b
->kind
== sk_function_parms
8265 /* Neither are the scopes used to hold template parameters
8266 for an explicit specialization. For an ordinary template
8267 declaration, these scopes are not scopes from the point of
8268 view of the language. */
8269 || (b
->kind
== sk_template_parms
8270 && (b
->explicit_spec_p
|| how
== TAG_how::GLOBAL
)))
8272 else if (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)
8275 if (b
->kind
== sk_template_parms
)
8282 gcc_assert (identifier_p (name
));
8284 /* Do C++ gratuitous typedefing. */
8285 if (identifier_type_value_1 (name
) != type
)
8288 tree context
= TYPE_CONTEXT (type
);
8292 cp_binding_level
*cb
= b
;
8293 while (cb
->kind
!= sk_namespace
8294 && cb
->kind
!= sk_class
8295 && (cb
->kind
!= sk_function_parms
8296 || !cb
->this_entity
))
8297 cb
= cb
->level_chain
;
8298 tree cs
= cb
->this_entity
;
8300 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
8301 ? cs
== current_function_decl
8302 : TYPE_P (cs
) ? cs
== current_class_type
8303 : cs
== current_namespace
);
8305 if (how
== TAG_how::CURRENT_ONLY
8306 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
8308 else if (cs
&& TYPE_P (cs
))
8309 /* When declaring a friend class of a local class, we want
8310 to inject the newly named class into the scope
8311 containing the local class, not the namespace
8313 context
= decl_function_context (get_type_decl (cs
));
8316 context
= current_namespace
;
8318 tdef
= create_implicit_typedef (name
, type
);
8319 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
8320 set_originating_module (tdef
);
8322 decl
= maybe_process_template_type_declaration
8323 (type
, how
== TAG_how::HIDDEN_FRIEND
, b
);
8324 if (decl
== error_mark_node
)
8327 bool in_class
= false;
8328 if (b
->kind
== sk_class
)
8331 if (!TYPE_BEING_DEFINED (current_class_type
))
8332 /* Don't push anywhere if the class is complete; a lambda in an
8333 NSDMI is not a member of the class. */
8335 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8336 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8337 class. But if it's a member template class, we want
8338 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8340 finish_member_declaration (decl
);
8342 pushdecl_class_level (decl
);
8344 else if (b
->kind
== sk_template_parms
)
8345 in_class
= b
->level_chain
->kind
== sk_class
;
8348 decl
= do_pushdecl_with_scope
8349 (decl
, b
, /*hiding=*/(how
== TAG_how::HIDDEN_FRIEND
));
8350 if (decl
== error_mark_node
)
8353 if (DECL_CONTEXT (decl
) == std_node
8354 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
8355 && !CLASSTYPE_TEMPLATE_INFO (type
))
8357 error ("declaration of %<std::initializer_list%> does not match "
8358 "%<#include <initializer_list>%>, isn%'t a template");
8359 return error_mark_node
;
8364 set_identifier_type_value_with_scope (name
, tdef
, b
);
8366 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
8368 /* If this is a local class, keep track of it. We need this
8369 information for name-mangling, and so that it is possible to
8370 find all function definitions in a translation unit in a
8371 convenient way. (It's otherwise tricky to find a member
8372 function definition it's only pointed to from within a local
8374 if (TYPE_FUNCTION_SCOPE_P (type
))
8376 if (processing_template_decl
)
8378 /* Push a DECL_EXPR so we call pushtag at the right time in
8379 template instantiation rather than in some nested context. */
8380 add_decl_expr (decl
);
8382 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8383 else if (!LAMBDA_TYPE_P (type
))
8384 determine_local_discriminator (TYPE_NAME (type
));
8388 if (b
->kind
== sk_class
8389 && !COMPLETE_TYPE_P (current_class_type
))
8390 maybe_add_class_template_decl_list (current_class_type
,
8391 type
, /*friend_p=*/0);
8393 decl
= TYPE_NAME (type
);
8394 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
8396 /* Set type visibility now if this is a forward declaration. */
8397 TREE_PUBLIC (decl
) = 1;
8398 determine_visibility (decl
);
8403 /* Wrapper for do_pushtag. */
8406 pushtag (tree name
, tree type
, TAG_how how
)
8408 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8409 tree ret
= do_pushtag (name
, type
, how
);
8410 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8415 /* Subroutines for reverting temporarily to top-level for instantiation
8416 of templates and such. We actually need to clear out the class- and
8417 local-value slots of all identifiers, so that only the global values
8418 are at all visible. Simply setting current_binding_level to the global
8419 scope isn't enough, because more binding levels may be pushed. */
8420 struct saved_scope
*scope_chain
;
8422 /* Return true if ID has not already been marked. */
8425 store_binding_p (tree id
)
8427 if (!id
|| !IDENTIFIER_BINDING (id
))
8430 if (IDENTIFIER_MARKED (id
))
8436 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8437 have enough space reserved. */
8440 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8442 cxx_saved_binding saved
;
8444 gcc_checking_assert (store_binding_p (id
));
8446 IDENTIFIER_MARKED (id
) = 1;
8448 saved
.identifier
= id
;
8449 saved
.binding
= IDENTIFIER_BINDING (id
);
8450 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
8451 (*old_bindings
)->quick_push (saved
);
8452 IDENTIFIER_BINDING (id
) = NULL
;
8456 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8458 static vec
<tree
> bindings_need_stored
;
8462 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8463 for (t
= names
; t
; t
= TREE_CHAIN (t
))
8465 if (TREE_CODE (t
) == TREE_LIST
)
8466 id
= TREE_PURPOSE (t
);
8470 if (store_binding_p (id
))
8471 bindings_need_stored
.safe_push (id
);
8473 if (!bindings_need_stored
.is_empty ())
8475 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8476 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8478 /* We can apparently have duplicates in NAMES. */
8479 if (store_binding_p (id
))
8480 store_binding (id
, old_bindings
);
8482 bindings_need_stored
.truncate (0);
8484 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8487 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8488 objects, rather than a TREE_LIST. */
8491 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
8492 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8494 static vec
<tree
> bindings_need_stored
;
8496 cp_class_binding
*cb
;
8498 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
8499 if (store_binding_p (cb
->identifier
))
8500 bindings_need_stored
.safe_push (cb
->identifier
);
8501 if (!bindings_need_stored
.is_empty ())
8504 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8505 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8506 store_binding (id
, old_bindings
);
8507 bindings_need_stored
.truncate (0);
8511 /* A chain of saved_scope structures awaiting reuse. */
8513 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
8516 do_push_to_top_level (void)
8518 struct saved_scope
*s
;
8519 cp_binding_level
*b
;
8520 cxx_saved_binding
*sb
;
8524 /* Reuse or create a new structure for this saved scope. */
8525 if (free_saved_scope
!= NULL
)
8527 s
= free_saved_scope
;
8528 free_saved_scope
= s
->prev
;
8530 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
8531 memset (s
, 0, sizeof (*s
));
8532 /* Also reuse the structure's old_bindings vector. */
8533 vec_safe_truncate (old_bindings
, 0);
8534 s
->old_bindings
= old_bindings
;
8537 s
= ggc_cleared_alloc
<saved_scope
> ();
8539 b
= scope_chain
? current_binding_level
: 0;
8541 /* If we're in the middle of some function, save our state. */
8545 push_function_context ();
8550 if (scope_chain
&& previous_class_level
)
8551 store_class_bindings (previous_class_level
->class_shadowed
,
8554 /* Have to include the global scope, because class-scope decls
8555 aren't listed anywhere useful. */
8556 for (; b
; b
= b
->level_chain
)
8560 /* Template IDs are inserted into the global level. If they were
8561 inserted into namespace level, finish_file wouldn't find them
8562 when doing pending instantiations. Therefore, don't stop at
8563 namespace level, but continue until :: . */
8564 if (global_scope_p (b
))
8567 store_bindings (b
->names
, &s
->old_bindings
);
8568 /* We also need to check class_shadowed to save class-level type
8569 bindings, since pushclass doesn't fill in b->names. */
8570 if (b
->kind
== sk_class
)
8571 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
8573 /* Unwind type-value slots back to top level. */
8574 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
8575 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
8578 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
8579 IDENTIFIER_MARKED (sb
->identifier
) = 0;
8581 s
->prev
= scope_chain
;
8583 s
->need_pop_function_context
= need_pop
;
8584 s
->function_decl
= current_function_decl
;
8585 s
->unevaluated_operand
= cp_unevaluated_operand
;
8586 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8587 s
->suppress_location_wrappers
= suppress_location_wrappers
;
8588 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
8591 current_function_decl
= NULL_TREE
;
8592 current_lang_base
= NULL
;
8593 current_lang_name
= lang_name_cplusplus
;
8594 current_namespace
= global_namespace
;
8595 push_class_stack ();
8596 cp_unevaluated_operand
= 0;
8597 c_inhibit_evaluation_warnings
= 0;
8598 suppress_location_wrappers
= 0;
8602 do_pop_from_top_level (void)
8604 struct saved_scope
*s
= scope_chain
;
8605 cxx_saved_binding
*saved
;
8608 /* Clear out class-level bindings cache. */
8609 if (previous_class_level
)
8610 invalidate_class_lookup_cache ();
8613 release_tree_vector (current_lang_base
);
8615 scope_chain
= s
->prev
;
8616 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
8618 tree id
= saved
->identifier
;
8620 IDENTIFIER_BINDING (id
) = saved
->binding
;
8621 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
8624 /* If we were in the middle of compiling a function, restore our
8626 if (s
->need_pop_function_context
)
8627 pop_function_context ();
8628 current_function_decl
= s
->function_decl
;
8629 cp_unevaluated_operand
= s
->unevaluated_operand
;
8630 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
8631 suppress_location_wrappers
= s
->suppress_location_wrappers
;
8633 /* Make this saved_scope structure available for reuse by
8634 push_to_top_level. */
8635 s
->prev
= free_saved_scope
;
8636 free_saved_scope
= s
;
8639 /* Push into the scope of the namespace NS, even if it is deeply
8640 nested within another namespace. */
8643 do_push_nested_namespace (tree ns
)
8645 if (ns
== global_namespace
)
8646 do_push_to_top_level ();
8649 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
8650 resume_scope (NAMESPACE_LEVEL (ns
));
8651 current_namespace
= ns
;
8655 /* Pop back from the scope of the namespace NS, which was previously
8656 entered with push_nested_namespace. */
8659 do_pop_nested_namespace (tree ns
)
8661 while (ns
!= global_namespace
)
8663 ns
= CP_DECL_CONTEXT (ns
);
8664 current_namespace
= ns
;
8668 do_pop_from_top_level ();
8671 /* Add TARGET to USINGS, if it does not already exist there. We used
8672 to build the complete graph of usings at this point, from the POV
8673 of the source namespaces. Now we build that as we perform the
8674 unqualified search. */
8677 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
8680 for (unsigned ix
= usings
->length (); ix
--;)
8681 if ((*usings
)[ix
] == target
)
8684 vec_safe_push (usings
, target
);
8687 /* Tell the debug system of a using directive. */
8690 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
8692 /* Emit debugging info. */
8693 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
8694 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
8698 /* Process a using directive. */
8701 finish_using_directive (tree target
, tree attribs
)
8703 if (target
== error_mark_node
)
8706 if (current_binding_level
->kind
!= sk_namespace
)
8707 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
8709 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
8710 ORIGINAL_NAMESPACE (target
), false);
8712 add_using_namespace (current_binding_level
->using_directives
,
8713 ORIGINAL_NAMESPACE (target
));
8715 if (attribs
!= error_mark_node
)
8716 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
8718 tree name
= get_attribute_name (a
);
8719 if (current_binding_level
->kind
== sk_namespace
8720 && is_attribute_p ("strong", name
))
8722 if (warning (0, "%<strong%> using directive no longer supported")
8723 && CP_DECL_CONTEXT (target
) == current_namespace
)
8724 inform (DECL_SOURCE_LOCATION (target
),
8725 "you can use an inline namespace instead");
8728 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
8732 /* Pushes X into the global namespace. */
8735 pushdecl_top_level (tree x
)
8737 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8738 do_push_to_top_level ();
8739 gcc_checking_assert (!DECL_CONTEXT (x
));
8740 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8741 x
= pushdecl_namespace_level (x
);
8742 do_pop_from_top_level ();
8743 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8747 /* Pushes X into the global namespace and calls cp_finish_decl to
8748 register the variable, initializing it with INIT. */
8751 pushdecl_top_level_and_finish (tree x
, tree init
)
8753 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8754 do_push_to_top_level ();
8755 gcc_checking_assert (!DECL_CONTEXT (x
));
8756 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8757 x
= pushdecl_namespace_level (x
);
8758 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
8759 do_pop_from_top_level ();
8760 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8764 /* Enter the namespaces from current_namerspace to NS. */
8767 push_inline_namespaces (tree ns
)
8770 if (ns
!= current_namespace
)
8772 gcc_assert (ns
!= global_namespace
);
8773 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8774 resume_scope (NAMESPACE_LEVEL (ns
));
8775 current_namespace
= ns
;
8781 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8782 Reuse or create a namespace NAME. NAME is null for the anonymous
8786 reuse_namespace (tree
*slot
, tree ctx
, tree name
)
8788 if (modules_p () && *slot
&& TREE_PUBLIC (ctx
) && name
)
8790 /* Public namespace. Shared. */
8791 tree
*global_slot
= slot
;
8792 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
8793 global_slot
= get_fixed_binding_slot (slot
, name
,
8794 BINDING_SLOT_GLOBAL
, false);
8796 for (ovl_iterator
iter (*global_slot
); iter
; ++iter
)
8800 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
8808 make_namespace (tree ctx
, tree name
, location_t loc
, bool inline_p
)
8810 /* Create the namespace. */
8811 tree ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
8812 DECL_SOURCE_LOCATION (ns
) = loc
;
8813 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (ctx
) + 1;
8814 if (!SCOPE_DEPTH (ns
))
8815 /* We only allow depth 255. */
8816 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx
));
8817 DECL_CONTEXT (ns
) = FROB_CONTEXT (ctx
);
8820 /* Anon-namespaces in different header-unit imports are distinct.
8821 But that's ok as their contents all have internal linkage.
8822 (This is different to how they'd behave as textual includes,
8823 but doing this at all is really odd source.) */
8824 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
8825 else if (TREE_PUBLIC (ctx
))
8826 TREE_PUBLIC (ns
) = true;
8829 DECL_NAMESPACE_INLINE_P (ns
) = true;
8834 /* NS was newly created, finish off making it. */
8837 make_namespace_finish (tree ns
, tree
*slot
, bool from_import
= false)
8839 if (modules_p () && TREE_PUBLIC (ns
) && (from_import
|| *slot
!= ns
))
8841 /* Merge into global slot. */
8842 tree
*gslot
= get_fixed_binding_slot (slot
, DECL_NAME (ns
),
8843 BINDING_SLOT_GLOBAL
, true);
8847 tree ctx
= CP_DECL_CONTEXT (ns
);
8848 cp_binding_level
*scope
= ggc_cleared_alloc
<cp_binding_level
> ();
8849 scope
->this_entity
= ns
;
8850 scope
->more_cleanups_ok
= true;
8851 scope
->kind
= sk_namespace
;
8852 scope
->level_chain
= NAMESPACE_LEVEL (ctx
);
8853 NAMESPACE_LEVEL (ns
) = scope
;
8855 if (DECL_NAMESPACE_INLINE_P (ns
))
8856 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx
), ns
);
8858 if (DECL_NAMESPACE_INLINE_P (ns
) || !DECL_NAME (ns
))
8859 emit_debug_info_using_namespace (ctx
, ns
, true);
8862 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
8863 then we enter an anonymous namespace. If MAKE_INLINE is true, then
8864 we create an inline namespace (it is up to the caller to check upon
8865 redefinition). Return the number of namespaces entered. */
8868 push_namespace (tree name
, bool make_inline
)
8870 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8873 /* We should not get here if the global_namespace is not yet constructed
8874 nor if NAME designates the global namespace: The global scope is
8875 constructed elsewhere. */
8876 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
8878 tree ns
= NULL_TREE
;
8880 name_lookup
lookup (name
);
8881 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
8883 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
8885 /* An ambiguous lookup. If exactly one is a namespace, we
8886 want that. If more than one is a namespace, error, but
8887 pick one of them. */
8888 /* DR2061 can cause us to find multiple namespaces of the same
8889 name. We must treat that carefully and avoid thinking we
8890 need to push a new (possibly) duplicate namespace. Hey,
8891 if you want to use the same identifier within an inline
8892 nest, knock yourself out. */
8893 for (tree
*chain
= &lookup
.value
, next
; (next
= *chain
);)
8895 tree decl
= TREE_VALUE (next
);
8896 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
8900 else if (SCOPE_DEPTH (ns
) >= SCOPE_DEPTH (decl
))
8904 chain
= &TREE_CHAIN (next
);
8908 *chain
= TREE_CHAIN (next
);
8911 if (TREE_CHAIN (lookup
.value
))
8913 error ("%<namespace %E%> is ambiguous", name
);
8914 print_candidates (lookup
.value
);
8917 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
8921 if (tree dna
= DECL_NAMESPACE_ALIAS (ns
))
8923 /* A namespace alias is not allowed here, but if the alias
8924 is for a namespace also inside the current scope,
8925 accept it with a diagnostic. That's better than dying
8927 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
8929 error ("namespace alias %qD not allowed here, "
8930 "assuming %qD", ns
, dna
);
8940 /* DR2061. NS might be a member of an inline namespace. We
8941 need to push into those namespaces. */
8944 for (tree parent
, ctx
= ns
; ctx
!= current_namespace
;
8947 parent
= CP_DECL_CONTEXT (ctx
);
8949 tree bind
= *find_namespace_slot (parent
, DECL_NAME (ctx
), false);
8952 auto &cluster
= BINDING_VECTOR_CLUSTER (bind
, 0);
8953 binding_slot
&slot
= cluster
.slots
[BINDING_SLOT_CURRENT
];
8954 gcc_checking_assert (!(tree
)slot
|| (tree
)slot
== ctx
);
8960 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8961 if (DECL_SOURCE_LOCATION (ns
) == BUILTINS_LOCATION
)
8962 /* It's not builtin now. */
8963 DECL_SOURCE_LOCATION (ns
) = input_location
;
8967 /* Before making a new namespace, see if we already have one in
8968 the existing partitions of the current namespace. */
8969 tree
*slot
= find_namespace_slot (current_namespace
, name
, false);
8971 ns
= reuse_namespace (slot
, current_namespace
, name
);
8973 ns
= make_namespace (current_namespace
, name
,
8974 input_location
, make_inline
);
8976 if (pushdecl (ns
) == error_mark_node
)
8980 /* Finish up making the namespace. */
8981 add_decl_to_level (NAMESPACE_LEVEL (current_namespace
), ns
);
8984 slot
= find_namespace_slot (current_namespace
, name
);
8985 /* This should find the slot created by pushdecl. */
8986 gcc_checking_assert (slot
&& *slot
== ns
);
8988 make_namespace_finish (ns
, slot
);
8990 /* Add the anon using-directive here, we don't do it in
8991 make_namespace_finish. */
8992 if (!DECL_NAMESPACE_INLINE_P (ns
) && !name
)
8993 add_using_namespace (current_binding_level
->using_directives
, ns
);
8999 /* A public namespace is exported only if explicitly marked, or
9000 it contains exported entities. */
9001 if (!DECL_MODULE_EXPORT_P (ns
) && TREE_PUBLIC (ns
)
9002 && module_exporting_p ())
9003 implicitly_export_namespace (ns
);
9005 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
9007 error_at (input_location
,
9008 "inline namespace must be specified at initial definition");
9009 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
9011 resume_scope (NAMESPACE_LEVEL (ns
));
9012 current_namespace
= ns
;
9016 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9020 /* Pop from the scope of the current namespace. */
9023 pop_namespace (void)
9025 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9027 gcc_assert (current_namespace
!= global_namespace
);
9028 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
9029 /* The binding level is not popped, as it might be re-opened later. */
9032 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9035 /* An import is defining namespace NAME inside CTX. Find or create
9036 that namespace and add it to the container's binding-vector. */
9039 add_imported_namespace (tree ctx
, tree name
, unsigned origin
, location_t loc
,
9040 bool visible_p
, bool inline_p
)
9042 // FIXME: Something is not correct about the VISIBLE_P handling. We
9043 // need to insert this namespace into
9044 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9045 // (b) The importing module's slot (always)
9046 // (c) Do we need to put it in the CURRENT slot? This is the
9049 gcc_checking_assert (origin
);
9050 tree
*slot
= find_namespace_slot (ctx
, name
, true);
9051 tree decl
= reuse_namespace (slot
, ctx
, name
);
9054 decl
= make_namespace (ctx
, name
, loc
, inline_p
);
9055 DECL_MODULE_IMPORT_P (decl
) = true;
9056 make_namespace_finish (decl
, slot
, true);
9058 else if (DECL_NAMESPACE_INLINE_P (decl
) != inline_p
)
9060 error_at (loc
, "%s namespace %qD conflicts with reachable definition",
9061 inline_p
? "inline" : "non-inline", decl
);
9062 inform (DECL_SOURCE_LOCATION (decl
), "reachable %s definition here",
9063 inline_p
? "non-inline" : "inline");
9066 if (TREE_PUBLIC (decl
) && TREE_CODE (*slot
) == BINDING_VECTOR
)
9068 /* See if we can extend the final slot. */
9069 binding_cluster
*last
= BINDING_VECTOR_CLUSTER_LAST (*slot
);
9070 gcc_checking_assert (last
->indices
[0].span
);
9071 unsigned jx
= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
9074 if (last
->indices
[jx
].span
)
9076 tree final
= last
->slots
[jx
];
9077 if (visible_p
== !STAT_HACK_P (final
)
9078 && MAYBE_STAT_DECL (final
) == decl
9079 && last
->indices
[jx
].base
+ last
->indices
[jx
].span
== origin
9080 && (BINDING_VECTOR_NUM_CLUSTERS (*slot
) > 1
9081 || (BINDING_VECTOR_SLOTS_PER_CLUSTER
> BINDING_SLOTS_FIXED
9082 && jx
>= BINDING_SLOTS_FIXED
)))
9084 last
->indices
[jx
].span
++;
9089 /* Append a new slot. */
9090 tree
*mslot
= &(tree
&)*append_imported_binding_slot (slot
, name
, origin
);
9092 gcc_assert (!*mslot
);
9093 *mslot
= visible_p
? decl
: stat_hack (decl
, NULL_TREE
);
9098 /* External entry points for do_{push_to/pop_from}_top_level. */
9101 push_to_top_level (void)
9103 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9104 do_push_to_top_level ();
9105 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9109 pop_from_top_level (void)
9111 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9112 do_pop_from_top_level ();
9113 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9116 /* External entry points for do_{push,pop}_nested_namespace. */
9119 push_nested_namespace (tree ns
)
9121 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9122 do_push_nested_namespace (ns
);
9123 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9127 pop_nested_namespace (tree ns
)
9129 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9130 gcc_assert (current_namespace
== ns
);
9131 do_pop_nested_namespace (ns
);
9132 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9135 /* Pop off extraneous binding levels left over due to syntax errors.
9136 We don't pop past namespaces, as they might be valid. */
9139 pop_everything (void)
9141 if (ENABLE_SCOPE_CHECKING
)
9142 verbatim ("XXX entering %<pop_everything ()%>");
9143 while (!namespace_bindings_p ())
9145 if (current_binding_level
->kind
== sk_class
)
9146 pop_nested_class ();
9150 if (ENABLE_SCOPE_CHECKING
)
9151 verbatim ("XXX leaving %<pop_everything ()%>");
9154 /* Emit debugging information for using declarations and directives.
9155 If input tree is overloaded fn then emit debug info for all
9159 cp_emit_debug_info_for_using (tree t
, tree context
)
9161 /* Don't try to emit any debug information if we have errors. */
9165 /* Do not supply context to imported_module_or_decl, if
9166 it is a global namespace. */
9167 if (context
== global_namespace
)
9168 context
= NULL_TREE
;
9170 t
= MAYBE_BASELINK_FUNCTIONS (t
);
9172 for (lkp_iterator
iter (t
); iter
; ++iter
)
9176 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
9177 /* FIXME: Handle TEMPLATE_DECLs. */
9180 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9181 of a builtin function. */
9182 if (TREE_CODE (fn
) == FUNCTION_DECL
9183 && DECL_EXTERNAL (fn
)
9184 && fndecl_built_in_p (fn
))
9187 if (building_stmt_list_p ())
9188 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
9190 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
9195 /* Return the result of unqualified lookup for the overloaded operator
9196 designated by CODE, if we are in a template and the binding we find is
9200 op_unqualified_lookup (tree fnname
)
9202 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
9204 cp_binding_level
*l
= binding
->scope
;
9205 while (l
&& !l
->this_entity
)
9208 if (l
&& uses_template_parms (l
->this_entity
))
9209 /* Don't preserve decls from an uninstantiated template,
9210 wait until that template is instantiated. */
9214 tree fns
= lookup_name (fnname
);
9216 /* Remember we found nothing! */
9217 return error_mark_node
;
9219 tree d
= is_overloaded_fn (fns
) ? get_first_fn (fns
) : fns
;
9220 if (DECL_CLASS_SCOPE_P (d
))
9221 /* We don't need to remember class-scope functions or declarations,
9222 normal unqualified lookup will find them again. */
9228 /* E is an expression representing an operation with dependent type, so we
9229 don't know yet whether it will use the built-in meaning of the operator or a
9230 function. Remember declarations of that operator in scope.
9232 We then inject a fake binding of that lookup into the
9233 instantiation's parameter scope. This approach fails if the user
9234 has different using declarations or directives in different local
9235 binding of the current function from whence we need to do lookups
9236 (we'll cache what we see on the first lookup). */
9238 static const char *const op_bind_attrname
= "operator bindings";
9241 maybe_save_operator_binding (tree e
)
9243 /* This is only useful in a generic lambda. */
9244 if (!processing_template_decl
)
9247 tree cfn
= current_function_decl
;
9251 /* Do this for lambdas and code that will emit a CMI. In a module's
9252 GMF we don't yet know whether there will be a CMI. */
9253 if (!module_has_cmi_p () && !global_purview_p () && !current_lambda_expr())
9256 tree fnname
= ovl_op_identifier (false, TREE_CODE (e
));
9260 tree attributes
= DECL_ATTRIBUTES (cfn
);
9261 tree op_attr
= lookup_attribute (op_bind_attrname
, attributes
);
9264 op_attr
= tree_cons (get_identifier (op_bind_attrname
),
9265 NULL_TREE
, attributes
);
9266 DECL_ATTRIBUTES (cfn
) = op_attr
;
9269 tree op_bind
= purpose_member (fnname
, TREE_VALUE (op_attr
));
9272 tree fns
= op_unqualified_lookup (fnname
);
9274 /* Always record, so we don't keep looking for this
9276 TREE_VALUE (op_attr
) = tree_cons (fnname
, fns
, TREE_VALUE (op_attr
));
9280 /* Called from cp_free_lang_data so we don't put this into LTO. */
9283 discard_operator_bindings (tree decl
)
9285 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
9286 DECL_ATTRIBUTES (decl
));
9289 /* Subroutine of start_preparsed_function: push the bindings we saved away in
9290 maybe_save_op_lookup into the function parameter binding level. */
9293 push_operator_bindings ()
9295 tree decl1
= current_function_decl
;
9296 if (tree attr
= lookup_attribute (op_bind_attrname
,
9297 DECL_ATTRIBUTES (decl1
)))
9298 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
9299 if (tree val
= TREE_VALUE (binds
))
9301 tree name
= TREE_PURPOSE (binds
);
9302 push_local_binding (name
, val
, /*using*/true);
9306 #include "gt-cp-name-lookup.h"