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
);
3929 if (IDENTIFIER_CONV_OP_P (name
))
3931 gcc_checking_assert (name
== conv_op_identifier
);
3933 found
= OVL_CHAIN (found
);
3938 gcc_checking_assert (IS_FAKE_BASE_TYPE (klass
)
3939 || TYPE_PTRMEMFUNC_P (klass
));
3940 found
= fields_linear_search (klass
, name
, false);
3946 /* Given a namespace-level binding BINDING, walk it, calling CALLBACK
3947 for all decls of the current module. When partitions are involved,
3948 decls might be mentioned more than once. */
3951 walk_module_binding (tree binding
, bitmap partitions
,
3952 bool (*callback
) (tree decl
, WMB_Flags
, void *data
),
3955 // FIXME: We don't quite deal with using decls naming stat hack
3956 // type. Also using decls exporting something from the same scope.
3957 tree current
= binding
;
3960 if (TREE_CODE (binding
) == BINDING_VECTOR
)
3961 current
= BINDING_VECTOR_CLUSTER (binding
, 0).slots
[BINDING_SLOT_CURRENT
];
3963 bool decl_hidden
= false;
3964 if (tree type
= MAYBE_STAT_TYPE (current
))
3966 WMB_Flags flags
= WMB_None
;
3967 if (STAT_TYPE_HIDDEN_P (current
))
3968 flags
= WMB_Flags (flags
| WMB_Hidden
);
3969 count
+= callback (type
, flags
, data
);
3970 decl_hidden
= STAT_DECL_HIDDEN_P (current
);
3973 for (ovl_iterator
iter (MAYBE_STAT_DECL (current
)); iter
; ++iter
)
3975 if (iter
.hidden_p ())
3977 if (!(decl_hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)))
3979 WMB_Flags flags
= WMB_None
;
3981 flags
= WMB_Flags (flags
| WMB_Hidden
);
3982 if (iter
.using_p ())
3984 flags
= WMB_Flags (flags
| WMB_Using
);
3985 if (iter
.exporting_p ())
3986 flags
= WMB_Flags (flags
| WMB_Export
);
3988 count
+= callback (*iter
, flags
, data
);
3990 decl_hidden
= false;
3993 if (partitions
&& TREE_CODE (binding
) == BINDING_VECTOR
)
3995 /* Process partition slots. */
3996 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
3997 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
3998 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
4004 bool maybe_dups
= BINDING_VECTOR_PARTITION_DUPS_P (binding
);
4006 for (; ix
--; cluster
++)
4007 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
4008 if (!cluster
->slots
[jx
].is_lazy ())
4009 if (tree bind
= cluster
->slots
[jx
])
4011 if (TREE_CODE (bind
) == NAMESPACE_DECL
4012 && !DECL_NAMESPACE_ALIAS (bind
))
4014 if (unsigned base
= cluster
->indices
[jx
].base
)
4015 if (unsigned span
= cluster
->indices
[jx
].span
)
4017 if (bitmap_bit_p (partitions
, base
))
4019 while (++base
, --span
);
4020 /* Not a partition's namespace. */
4024 WMB_Flags flags
= WMB_None
;
4026 flags
= WMB_Flags (flags
| WMB_Dups
);
4027 count
+= callback (bind
, flags
, data
);
4029 else if (STAT_HACK_P (bind
) && MODULE_BINDING_PARTITION_P (bind
))
4031 if (tree btype
= STAT_TYPE (bind
))
4033 WMB_Flags flags
= WMB_None
;
4035 flags
= WMB_Flags (flags
| WMB_Dups
);
4036 if (STAT_TYPE_HIDDEN_P (bind
))
4037 flags
= WMB_Flags (flags
| WMB_Hidden
);
4039 count
+= callback (btype
, flags
, data
);
4041 bool hidden
= STAT_DECL_HIDDEN_P (bind
);
4042 for (ovl_iterator
iter (MAYBE_STAT_DECL (STAT_DECL (bind
)));
4045 if (iter
.hidden_p ())
4048 (!(hidden
&& DECL_IS_UNDECLARED_BUILTIN (*iter
)));
4050 WMB_Flags flags
= WMB_None
;
4052 flags
= WMB_Flags (flags
| WMB_Dups
);
4054 flags
= WMB_Flags (flags
| WMB_Hidden
);
4055 if (iter
.using_p ())
4057 flags
= WMB_Flags (flags
| WMB_Using
);
4058 if (iter
.exporting_p ())
4059 flags
= WMB_Flags (flags
| WMB_Export
);
4061 count
+= callback (*iter
, flags
, data
);
4071 /* Imported module MOD has a binding to NS::NAME, stored in section
4075 import_module_binding (tree ns
, tree name
, unsigned mod
, unsigned snum
)
4077 tree
*slot
= find_namespace_slot (ns
, name
, true);
4078 binding_slot
*mslot
= append_imported_binding_slot (slot
, name
, mod
);
4080 if (mslot
->is_lazy () || *mslot
)
4081 /* Oops, something was already there. */
4084 mslot
->set_lazy (snum
);
4088 /* An import of MODULE is binding NS::NAME. There should be no
4089 existing binding for >= MODULE. MOD_GLOB indicates whether MODULE
4090 is a header_unit (-1) or part of the current module (+1). VALUE
4091 and TYPE are the value and type bindings. VISIBLE are the value
4092 bindings being exported. */
4095 set_module_binding (tree ns
, tree name
, unsigned mod
, int mod_glob
,
4096 tree value
, tree type
, tree visible
)
4099 /* Bogus BMIs could give rise to nothing to bind. */
4102 gcc_assert (TREE_CODE (value
) != NAMESPACE_DECL
4103 || DECL_NAMESPACE_ALIAS (value
));
4104 gcc_checking_assert (mod
);
4106 tree
*slot
= find_namespace_slot (ns
, name
, true);
4107 binding_slot
*mslot
= search_imported_binding_slot (slot
, mod
);
4109 if (!mslot
|| !mslot
->is_lazy ())
4110 /* Again, bogus BMI could give find to missing or already loaded slot. */
4114 if (type
|| visible
!= bind
|| mod_glob
)
4116 bind
= stat_hack (bind
, type
);
4117 STAT_VISIBLE (bind
) = visible
;
4118 if ((mod_glob
> 0 && TREE_PUBLIC (ns
))
4119 || (type
&& DECL_MODULE_EXPORT_P (type
)))
4120 STAT_TYPE_VISIBLE_P (bind
) = true;
4123 /* Note if this is this-module or global binding. */
4125 MODULE_BINDING_PARTITION_P (bind
) = true;
4126 else if (mod_glob
< 0)
4127 MODULE_BINDING_GLOBAL_P (bind
) = true;
4135 note_pending_specializations (tree ns
, tree name
, bool is_header
)
4137 if (tree
*slot
= find_namespace_slot (ns
, name
, false))
4138 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
4141 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec
) = true;
4143 BINDING_VECTOR_PENDING_IS_HEADER_P (vec
) = true;
4145 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
) = true;
4150 load_pending_specializations (tree ns
, tree name
)
4152 tree
*slot
= find_namespace_slot (ns
, name
, false);
4154 if (!slot
|| TREE_CODE (*slot
) != BINDING_VECTOR
4155 || !BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (*slot
))
4159 BINDING_VECTOR_PENDING_SPECIALIZATIONS_P (vec
) = false;
4161 bool do_header
= BINDING_VECTOR_PENDING_IS_HEADER_P (vec
);
4162 bool do_partition
= BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
);
4163 BINDING_VECTOR_PENDING_IS_HEADER_P (vec
) = false;
4164 BINDING_VECTOR_PENDING_IS_PARTITION_P (vec
) = false;
4166 gcc_checking_assert (do_header
| do_partition
);
4167 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (vec
);
4168 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (vec
);
4169 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
4175 for (; ix
--; cluster
++)
4176 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
4177 if (cluster
->indices
[jx
].span
4178 && cluster
->slots
[jx
].is_lazy ()
4179 && lazy_specializations_p (cluster
->indices
[jx
].base
,
4180 do_header
, do_partition
))
4181 lazy_load_binding (cluster
->indices
[jx
].base
, ns
, name
,
4182 &cluster
->slots
[jx
]);
4186 add_module_decl (tree ns
, tree name
, tree decl
)
4188 gcc_assert (!DECL_CHAIN (decl
));
4189 add_decl_to_level (NAMESPACE_LEVEL (ns
), decl
);
4190 newbinding_bookkeeping (name
, decl
, NAMESPACE_LEVEL (ns
));
4193 /* Enter DECL into the symbol table, if that's appropriate. Returns
4194 DECL, or a modified version thereof. */
4197 maybe_push_decl (tree decl
)
4199 tree type
= TREE_TYPE (decl
);
4201 /* Add this decl to the current binding level, but not if it comes
4202 from another scope, e.g. a static member variable. TEM may equal
4203 DECL or it may be a previous decl of the same name. */
4204 if (decl
== error_mark_node
4205 || (TREE_CODE (decl
) != PARM_DECL
4206 && DECL_CONTEXT (decl
) != NULL_TREE
4207 /* Definitions of namespace members outside their namespace are
4209 && !DECL_NAMESPACE_SCOPE_P (decl
))
4210 || (TREE_CODE (decl
) == TEMPLATE_DECL
&& !namespace_bindings_p ())
4211 || type
== unknown_type_node
4212 /* The declaration of a template specialization does not affect
4213 the functions available for overload resolution, so we do not
4215 || (TREE_CODE (decl
) == FUNCTION_DECL
4216 && DECL_TEMPLATE_SPECIALIZATION (decl
)))
4219 return pushdecl (decl
);
4222 /* Bind DECL to ID in the current_binding_level, assumed to be a local
4223 binding level. If IS_USING is true, DECL got here through a
4224 using-declaration. */
4227 push_local_binding (tree id
, tree decl
, bool is_using
)
4229 /* Skip over any local classes. This makes sense if we call
4230 push_local_binding with a friend decl of a local class. */
4231 cp_binding_level
*b
= innermost_nonclass_level ();
4233 gcc_assert (b
->kind
!= sk_namespace
);
4234 if (find_local_binding (b
, id
))
4236 /* Supplement the existing binding. */
4237 if (!supplement_binding (IDENTIFIER_BINDING (id
), decl
))
4238 /* It didn't work. Something else must be bound at this
4239 level. Do not add DECL to the list of things to pop
4244 /* Create a new binding. */
4245 push_binding (id
, decl
, b
);
4247 if (TREE_CODE (decl
) == OVERLOAD
|| is_using
)
4248 /* We must put the OVERLOAD or using into a TREE_LIST since we
4249 cannot use the decl's chain itself. */
4250 decl
= build_tree_list (id
, decl
);
4252 /* And put DECL on the list of things declared by the current
4254 add_decl_to_level (b
, decl
);
4258 /* true means unconditionally make a BLOCK for the next level pushed. */
4260 static bool keep_next_level_flag
;
4262 static int binding_depth
= 0;
4269 for (i
= 0; i
< depth
* 2; i
++)
4273 /* Return a string describing the kind of SCOPE we have. */
4275 cp_binding_level_descriptor (cp_binding_level
*scope
)
4277 /* The order of this table must match the "scope_kind"
4279 static const char* scope_kind_names
[] = {
4285 "function-parameter-scope",
4288 "template-parameter-scope",
4289 "template-explicit-spec-scope"
4291 const scope_kind kind
= scope
->explicit_spec_p
4292 ? sk_template_spec
: scope
->kind
;
4294 return scope_kind_names
[kind
];
4297 /* Output a debugging information about SCOPE when performing
4300 cp_binding_level_debug (cp_binding_level
*scope
, int line
, const char *action
)
4302 const char *desc
= cp_binding_level_descriptor (scope
);
4303 if (scope
->this_entity
)
4304 verbatim ("%s %<%s(%E)%> %p %d", action
, desc
,
4305 scope
->this_entity
, (void *) scope
, line
);
4307 verbatim ("%s %s %p %d", action
, desc
, (void *) scope
, line
);
4310 /* A chain of binding_level structures awaiting reuse. */
4312 static GTY((deletable
)) cp_binding_level
*free_binding_level
;
4314 /* Insert SCOPE as the innermost binding level. */
4317 push_binding_level (cp_binding_level
*scope
)
4319 /* Add it to the front of currently active scopes stack. */
4320 scope
->level_chain
= current_binding_level
;
4321 current_binding_level
= scope
;
4322 keep_next_level_flag
= false;
4324 if (ENABLE_SCOPE_CHECKING
)
4326 scope
->binding_depth
= binding_depth
;
4327 indent (binding_depth
);
4328 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4334 /* Create a new KIND scope and make it the top of the active scopes stack.
4335 ENTITY is the scope of the associated C++ entity (namespace, class,
4336 function, C++0x enumeration); it is NULL otherwise. */
4339 begin_scope (scope_kind kind
, tree entity
)
4341 cp_binding_level
*scope
;
4343 /* Reuse or create a struct for this binding level. */
4344 if (!ENABLE_SCOPE_CHECKING
&& free_binding_level
)
4346 scope
= free_binding_level
;
4347 free_binding_level
= scope
->level_chain
;
4348 memset (scope
, 0, sizeof (cp_binding_level
));
4351 scope
= ggc_cleared_alloc
<cp_binding_level
> ();
4353 scope
->this_entity
= entity
;
4354 scope
->more_cleanups_ok
= true;
4361 case sk_template_spec
:
4362 scope
->explicit_spec_p
= true;
4363 kind
= sk_template_parms
;
4365 case sk_template_parms
:
4372 case sk_scoped_enum
:
4373 case sk_transaction
:
4375 scope
->keep
= keep_next_level_flag
;
4378 case sk_function_parms
:
4379 scope
->keep
= keep_next_level_flag
;
4381 scope
->immediate_fn_ctx_p
= DECL_IMMEDIATE_FUNCTION_P (entity
);
4385 NAMESPACE_LEVEL (entity
) = scope
;
4389 /* Should not happen. */
4395 push_binding_level (scope
);
4400 /* We're about to leave current scope. Pop the top of the stack of
4401 currently active scopes. Return the enclosing scope, now active. */
4406 cp_binding_level
*scope
= current_binding_level
;
4408 if (scope
->kind
== sk_namespace
&& class_binding_level
)
4409 current_binding_level
= class_binding_level
;
4411 /* We cannot leave a scope, if there are none left. */
4412 if (NAMESPACE_LEVEL (global_namespace
))
4413 gcc_assert (!global_scope_p (scope
));
4415 if (ENABLE_SCOPE_CHECKING
)
4417 indent (--binding_depth
);
4418 cp_binding_level_debug (scope
, LOCATION_LINE (input_location
),
4422 /* Move one nesting level up. */
4423 current_binding_level
= scope
->level_chain
;
4425 /* Namespace-scopes are left most probably temporarily, not
4426 completely; they can be reopened later, e.g. in namespace-extension
4427 or any name binding activity that requires us to resume a
4428 namespace. For classes, we cache some binding levels. For other
4429 scopes, we just make the structure available for reuse. */
4430 if (scope
->kind
!= sk_namespace
4431 && scope
!= previous_class_level
)
4433 scope
->level_chain
= free_binding_level
;
4434 gcc_assert (!ENABLE_SCOPE_CHECKING
4435 || scope
->binding_depth
== binding_depth
);
4436 free_binding_level
= scope
;
4439 if (scope
->kind
== sk_class
)
4441 /* Reset DEFINING_CLASS_P to allow for reuse of a
4442 class-defining scope in a non-defining context. */
4443 scope
->defining_class_p
= 0;
4445 /* Find the innermost enclosing class scope, and reset
4446 CLASS_BINDING_LEVEL appropriately. */
4447 class_binding_level
= NULL
;
4448 for (scope
= current_binding_level
; scope
; scope
= scope
->level_chain
)
4449 if (scope
->kind
== sk_class
)
4451 class_binding_level
= scope
;
4456 return current_binding_level
;
4459 /* When we exit a toplevel class scope, we save its binding level so
4460 that we can restore it quickly. Here, we've entered some other
4461 class, so we must invalidate our cache. */
4464 invalidate_class_lookup_cache (void)
4466 previous_class_level
->level_chain
= free_binding_level
;
4467 free_binding_level
= previous_class_level
;
4468 previous_class_level
= NULL
;
4472 resume_scope (cp_binding_level
* b
)
4474 /* Resuming binding levels is meant only for namespaces,
4475 and those cannot nest into classes. */
4476 gcc_assert (!class_binding_level
);
4477 /* Also, resuming a non-directly nested namespace is a no-no. */
4478 gcc_assert (b
->level_chain
== current_binding_level
);
4479 current_binding_level
= b
;
4480 if (ENABLE_SCOPE_CHECKING
)
4482 b
->binding_depth
= binding_depth
;
4483 indent (binding_depth
);
4484 cp_binding_level_debug (b
, LOCATION_LINE (input_location
), "resume");
4489 /* Return the innermost binding level that is not for a class scope. */
4491 static cp_binding_level
*
4492 innermost_nonclass_level (void)
4494 cp_binding_level
*b
;
4496 b
= current_binding_level
;
4497 while (b
->kind
== sk_class
)
4503 /* We're defining an object of type TYPE. If it needs a cleanup, but
4504 we're not allowed to add any more objects with cleanups to the current
4505 scope, create a new binding level. */
4508 maybe_push_cleanup_level (tree type
)
4510 if (type
!= error_mark_node
4511 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
)
4512 && current_binding_level
->more_cleanups_ok
== 0)
4514 begin_scope (sk_cleanup
, NULL
);
4515 current_binding_level
->statement_list
= push_stmt_list ();
4519 /* Return true if we are in the global binding level. */
4522 global_bindings_p (void)
4524 return global_scope_p (current_binding_level
);
4527 /* True if we are currently in a toplevel binding level. This
4528 means either the global binding level or a namespace in a toplevel
4529 binding level. Since there are no non-toplevel namespace levels,
4530 this really means any namespace or template parameter level. We
4531 also include a class whose context is toplevel. */
4534 toplevel_bindings_p (void)
4536 cp_binding_level
*b
= innermost_nonclass_level ();
4538 return b
->kind
== sk_namespace
|| b
->kind
== sk_template_parms
;
4541 /* True if this is a namespace scope, or if we are defining a class
4542 which is itself at namespace scope, or whose enclosing class is
4543 such a class, etc. */
4546 namespace_bindings_p (void)
4548 cp_binding_level
*b
= innermost_nonclass_level ();
4550 return b
->kind
== sk_namespace
;
4553 /* True if the innermost non-class scope is a block scope. */
4556 local_bindings_p (void)
4558 cp_binding_level
*b
= innermost_nonclass_level ();
4559 return b
->kind
< sk_function_parms
|| b
->kind
== sk_omp
;
4562 /* True if the current level needs to have a BLOCK made. */
4567 return (current_binding_level
->blocks
!= NULL_TREE
4568 || current_binding_level
->keep
4569 || current_binding_level
->kind
== sk_cleanup
4570 || current_binding_level
->names
!= NULL_TREE
4571 || current_binding_level
->using_directives
);
4574 /* Returns the kind of the innermost scope. */
4577 innermost_scope_kind (void)
4579 return current_binding_level
->kind
;
4582 /* Returns true if this scope was created to store template parameters. */
4585 template_parm_scope_p (void)
4587 return innermost_scope_kind () == sk_template_parms
;
4590 /* If KEEP is true, make a BLOCK node for the next binding level,
4591 unconditionally. Otherwise, use the normal logic to decide whether
4592 or not to create a BLOCK. */
4595 keep_next_level (bool keep
)
4597 keep_next_level_flag
= keep
;
4600 /* Return the list of declarations of the current local scope. */
4603 get_local_decls (void)
4605 gcc_assert (current_binding_level
->kind
!= sk_namespace
4606 && current_binding_level
->kind
!= sk_class
);
4607 return current_binding_level
->names
;
4610 /* Return how many function prototypes we are currently nested inside. */
4613 function_parm_depth (void)
4616 cp_binding_level
*b
;
4618 for (b
= current_binding_level
;
4619 b
->kind
== sk_function_parms
;
4626 /* For debugging. */
4627 static int no_print_functions
= 0;
4628 static int no_print_builtins
= 0;
4631 print_binding_level (cp_binding_level
* lvl
)
4635 if (lvl
->this_entity
)
4636 print_node_brief (stderr
, "entity=", lvl
->this_entity
, 1);
4637 fprintf (stderr
, " blocks=%p", (void *) lvl
->blocks
);
4638 if (lvl
->more_cleanups_ok
)
4639 fprintf (stderr
, " more-cleanups-ok");
4640 if (lvl
->have_cleanups
)
4641 fprintf (stderr
, " have-cleanups");
4642 fprintf (stderr
, "\n");
4645 fprintf (stderr
, " names:\t");
4646 /* We can probably fit 3 names to a line? */
4647 for (t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
4649 if (no_print_functions
&& (TREE_CODE (t
) == FUNCTION_DECL
))
4651 if (no_print_builtins
4652 && (TREE_CODE (t
) == TYPE_DECL
)
4653 && DECL_IS_UNDECLARED_BUILTIN (t
))
4656 /* Function decls tend to have longer names. */
4657 if (TREE_CODE (t
) == FUNCTION_DECL
)
4664 fprintf (stderr
, "\n\t");
4667 print_node_brief (stderr
, "", t
, 0);
4668 if (t
== error_mark_node
)
4672 fprintf (stderr
, "\n");
4674 if (vec_safe_length (lvl
->class_shadowed
))
4677 cp_class_binding
*b
;
4678 fprintf (stderr
, " class-shadowed:");
4679 FOR_EACH_VEC_ELT (*lvl
->class_shadowed
, i
, b
)
4680 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (b
->identifier
));
4681 fprintf (stderr
, "\n");
4683 if (lvl
->type_shadowed
)
4685 fprintf (stderr
, " type-shadowed:");
4686 for (t
= lvl
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
4688 fprintf (stderr
, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t
)));
4690 fprintf (stderr
, "\n");
4695 debug (cp_binding_level
&ref
)
4697 print_binding_level (&ref
);
4701 debug (cp_binding_level
*ptr
)
4706 fprintf (stderr
, "<nil>\n");
4710 print_other_binding_stack (cp_binding_level
*stack
)
4712 cp_binding_level
*level
;
4713 for (level
= stack
; !global_scope_p (level
); level
= level
->level_chain
)
4715 fprintf (stderr
, "binding level %p\n", (void *) level
);
4716 print_binding_level (level
);
4721 print_binding_stack (void)
4723 cp_binding_level
*b
;
4724 fprintf (stderr
, "current_binding_level=%p\n"
4725 "class_binding_level=%p\n"
4726 "NAMESPACE_LEVEL (global_namespace)=%p\n",
4727 (void *) current_binding_level
, (void *) class_binding_level
,
4728 (void *) NAMESPACE_LEVEL (global_namespace
));
4729 if (class_binding_level
)
4731 for (b
= class_binding_level
; b
; b
= b
->level_chain
)
4732 if (b
== current_binding_level
)
4735 b
= class_binding_level
;
4737 b
= current_binding_level
;
4740 b
= current_binding_level
;
4741 print_other_binding_stack (b
);
4742 fprintf (stderr
, "global:\n");
4743 print_binding_level (NAMESPACE_LEVEL (global_namespace
));
4746 /* Return the type associated with ID. */
4749 identifier_type_value_1 (tree id
)
4751 /* There is no type with that name, anywhere. */
4752 if (REAL_IDENTIFIER_TYPE_VALUE (id
) == NULL_TREE
)
4754 /* This is not the type marker, but the real thing. */
4755 if (REAL_IDENTIFIER_TYPE_VALUE (id
) != global_type_node
)
4756 return REAL_IDENTIFIER_TYPE_VALUE (id
);
4757 /* Have to search for it. It must be on the global level, now.
4758 Ask lookup_name not to return non-types. */
4759 id
= lookup_name (id
, LOOK_where::BLOCK_NAMESPACE
, LOOK_want::TYPE
);
4761 return TREE_TYPE (id
);
4765 /* Wrapper for identifier_type_value_1. */
4768 identifier_type_value (tree id
)
4771 timevar_start (TV_NAME_LOOKUP
);
4772 ret
= identifier_type_value_1 (id
);
4773 timevar_stop (TV_NAME_LOOKUP
);
4777 /* Push a definition of struct, union or enum tag named ID. into
4778 binding_level B. DECL is a TYPE_DECL for the type. DECL has
4779 already been pushed into its binding level. This is bookkeeping to
4783 set_identifier_type_value_with_scope (tree id
, tree decl
, cp_binding_level
*b
)
4787 if (b
->kind
!= sk_namespace
)
4789 /* Shadow the marker, not the real thing, so that the marker
4790 gets restored later. */
4791 tree old_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
4792 b
->type_shadowed
= tree_cons (id
, old_type_value
, b
->type_shadowed
);
4793 type
= decl
? TREE_TYPE (decl
) : NULL_TREE
;
4794 TREE_TYPE (b
->type_shadowed
) = type
;
4800 /* Store marker instead of real type. */
4801 type
= global_type_node
;
4804 SET_IDENTIFIER_TYPE_VALUE (id
, type
);
4807 /* As set_identifier_type_value_with_scope, but using
4808 current_binding_level. */
4811 set_identifier_type_value (tree id
, tree decl
)
4813 set_identifier_type_value_with_scope (id
, decl
, current_binding_level
);
4816 /* Return the name for the constructor (or destructor) for the
4820 constructor_name (tree type
)
4822 tree decl
= TYPE_NAME (TYPE_MAIN_VARIANT (type
));
4824 return decl
? DECL_NAME (decl
) : NULL_TREE
;
4827 /* Returns TRUE if NAME is the name for the constructor for TYPE,
4828 which must be a class type. */
4831 constructor_name_p (tree name
, tree type
)
4833 gcc_assert (MAYBE_CLASS_TYPE_P (type
));
4835 /* These don't have names. */
4836 if (TREE_CODE (type
) == DECLTYPE_TYPE
4837 || TREE_CODE (type
) == TYPEOF_TYPE
)
4840 if (name
&& name
== constructor_name (type
))
4846 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4847 caller to set DECL_CONTEXT properly.
4849 Warning: For class and block-scope this must only be used when X
4850 will be the new innermost binding for its name, as we tack it onto
4851 the front of IDENTIFIER_BINDING without checking to see if the
4852 current IDENTIFIER_BINDING comes from a closer binding level than
4855 Warning: For namespace scope, this will look in LEVEL for an
4856 existing binding to match, but if not found will push the decl into
4857 CURRENT_NAMESPACE. Use push_nested_namespace/pushdecl/
4858 pop_nested_namespace if you really need to push it into a foreign
4862 do_pushdecl_with_scope (tree x
, cp_binding_level
*level
, bool hiding
= false)
4864 cp_binding_level
*b
;
4866 if (level
->kind
== sk_class
)
4868 gcc_checking_assert (!hiding
);
4869 b
= class_binding_level
;
4870 class_binding_level
= level
;
4871 pushdecl_class_level (x
);
4872 class_binding_level
= b
;
4876 tree function_decl
= current_function_decl
;
4877 if (level
->kind
== sk_namespace
)
4878 current_function_decl
= NULL_TREE
;
4879 b
= current_binding_level
;
4880 current_binding_level
= level
;
4881 x
= do_pushdecl (x
, hiding
);
4882 current_binding_level
= b
;
4883 current_function_decl
= function_decl
;
4888 /* Inject X into the local scope just before the function parms. */
4891 pushdecl_outermost_localscope (tree x
)
4893 cp_binding_level
*b
= NULL
;
4894 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
4896 /* Find the scope just inside the function parms. */
4897 for (cp_binding_level
*n
= current_binding_level
;
4898 n
->kind
!= sk_function_parms
; n
= b
->level_chain
)
4901 tree ret
= b
? do_pushdecl_with_scope (x
, b
) : error_mark_node
;
4902 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
4907 /* Process a local-scope or namespace-scope using declaration. LOOKUP
4908 is the result of qualified lookup (both value & type are
4909 significant). FN_SCOPE_P indicates if we're at function-scope (as
4910 opposed to namespace-scope). *VALUE_P and *TYPE_P are the current
4911 bindings, which are altered to reflect the newly brought in
4915 do_nonmember_using_decl (name_lookup
&lookup
, bool fn_scope_p
,
4916 bool insert_p
, tree
*value_p
, tree
*type_p
)
4918 tree value
= *value_p
;
4919 tree type
= *type_p
;
4920 bool failed
= false;
4922 /* Shift the old and new bindings around so we're comparing class and
4923 enumeration names to each other. */
4924 if (value
&& DECL_IMPLICIT_TYPEDEF_P (value
))
4930 if (lookup
.value
&& DECL_IMPLICIT_TYPEDEF_P (lookup
.value
))
4932 lookup
.type
= lookup
.value
;
4933 lookup
.value
= NULL_TREE
;
4936 /* Only process exporting if we're going to be inserting. */
4937 bool revealing_p
= insert_p
&& !fn_scope_p
&& module_has_cmi_p ();
4939 /* First do the value binding. */
4941 /* Nothing (only implicit typedef found). */
4942 gcc_checking_assert (lookup
.type
);
4943 else if (OVL_P (lookup
.value
) && (!value
|| OVL_P (value
)))
4945 for (lkp_iterator
usings (lookup
.value
); usings
; ++usings
)
4947 tree new_fn
= *usings
;
4948 bool exporting
= revealing_p
&& module_exporting_p ();
4951 /* If the using decl is exported, the things it refers
4952 to must also be exported (or not in module purview). */
4953 if (!DECL_MODULE_EXPORT_P (new_fn
)
4954 && (DECL_LANG_SPECIFIC (new_fn
)
4955 && DECL_MODULE_PURVIEW_P (new_fn
)))
4957 error ("%q#D does not have external linkage", new_fn
);
4958 inform (DECL_SOURCE_LOCATION (new_fn
),
4959 "%q#D declared here", new_fn
);
4964 /* [namespace.udecl]
4966 If a function declaration in namespace scope or block
4967 scope has the same name and the same parameter types as a
4968 function introduced by a using declaration the program is
4970 /* This seems overreaching, asking core -- why do we care
4971 about decls in the namespace that we cannot name (because
4972 they are not transitively imported. We just check the
4973 decls that are in this TU. */
4975 for (ovl_iterator
old (value
); !found
&& old
; ++old
)
4979 if (new_fn
== old_fn
)
4981 /* The function already exists in the current
4982 namespace. We will still want to insert it if
4983 it is revealing a not-revealed thing. */
4987 else if (old
.using_p ())
4990 /* Update in place. 'tis ok. */
4991 OVL_EXPORT_P (old
.get_using ()) = true;
4994 else if (DECL_MODULE_EXPORT_P (new_fn
))
4998 value
= old
.remove_node (value
);
5003 else if (old
.using_p ())
5004 continue; /* This is a using decl. */
5005 else if (old
.hidden_p () && DECL_IS_UNDECLARED_BUILTIN (old_fn
))
5006 continue; /* This is an anticipated builtin. */
5007 else if (!matching_fn_p (new_fn
, old_fn
))
5008 continue; /* Parameters do not match. */
5009 else if (decls_match (new_fn
, old_fn
))
5011 /* Extern "C" in different namespaces. */
5017 diagnose_name_conflict (new_fn
, old_fn
);
5024 if (!found
&& insert_p
)
5025 /* Unlike the decl-pushing case we don't drop anticipated
5026 builtins here. They don't cause a problem, and we'd
5027 like to match them with a future declaration. */
5028 value
= ovl_insert (new_fn
, value
, 1 + exporting
);
5032 /* Ignore anticipated builtins. */
5033 && !anticipated_builtin_p (value
)
5034 && (fn_scope_p
|| !decls_match (lookup
.value
, value
)))
5036 diagnose_name_conflict (lookup
.value
, value
);
5040 // FIXME:what if we're newly exporting lookup.value
5041 value
= lookup
.value
;
5043 /* Now the type binding. */
5044 if (lookup
.type
&& lookup
.type
!= type
)
5046 // FIXME: What if we're exporting lookup.type?
5047 if (type
&& !decls_match (lookup
.type
, type
))
5049 diagnose_name_conflict (lookup
.type
, type
);
5058 /* If value is empty, shift any class or enumeration name back. */
5071 /* Returns true if ANCESTOR encloses DESCENDANT, including matching.
5072 Both are namespaces. */
5075 is_nested_namespace (tree ancestor
, tree descendant
, bool inline_only
)
5077 int depth
= SCOPE_DEPTH (ancestor
);
5079 if (!depth
&& !inline_only
)
5080 /* The global namespace encloses everything. */
5083 while (SCOPE_DEPTH (descendant
) > depth
5084 && (!inline_only
|| DECL_NAMESPACE_INLINE_P (descendant
)))
5085 descendant
= CP_DECL_CONTEXT (descendant
);
5087 return ancestor
== descendant
;
5090 /* Returns true if ROOT (a non-alias namespace, class, or function)
5091 encloses CHILD. CHILD may be either a class type or a namespace
5095 is_ancestor (tree root
, tree child
)
5097 gcc_checking_assert ((TREE_CODE (root
) == NAMESPACE_DECL
5098 && !DECL_NAMESPACE_ALIAS (root
))
5099 || TREE_CODE (root
) == FUNCTION_DECL
5100 || CLASS_TYPE_P (root
));
5101 gcc_checking_assert (TREE_CODE (child
) == NAMESPACE_DECL
5102 || CLASS_TYPE_P (child
));
5104 /* The global namespace encloses everything. Early-out for the
5106 if (root
== global_namespace
)
5109 /* Search CHILD until we reach namespace scope. */
5110 while (TREE_CODE (child
) != NAMESPACE_DECL
)
5112 /* If we've reached the ROOT, it encloses CHILD. */
5116 /* Go out one level. */
5118 child
= TYPE_NAME (child
);
5119 child
= CP_DECL_CONTEXT (child
);
5122 if (TREE_CODE (root
) != NAMESPACE_DECL
)
5123 /* Failed to meet the non-namespace we were looking for. */
5126 if (tree alias
= DECL_NAMESPACE_ALIAS (child
))
5129 return is_nested_namespace (root
, child
);
5132 /* Enter the class or namespace scope indicated by T suitable for name
5133 lookup. T can be arbitrary scope, not necessary nested inside the
5134 current scope. Returns a non-null scope to pop iff pop_scope
5135 should be called later to exit this scope. */
5140 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5141 push_decl_namespace (t
);
5142 else if (CLASS_TYPE_P (t
))
5144 if (!at_class_scope_p ()
5145 || !same_type_p (current_class_type
, t
))
5146 push_nested_class (t
);
5148 /* T is the same as the current scope. There is therefore no
5149 need to re-enter the scope. Since we are not actually
5150 pushing a new scope, our caller should not call
5158 /* Leave scope pushed by push_scope. */
5165 if (TREE_CODE (t
) == NAMESPACE_DECL
)
5166 pop_decl_namespace ();
5167 else if CLASS_TYPE_P (t
)
5168 pop_nested_class ();
5171 /* Subroutine of push_inner_scope. */
5174 push_inner_scope_r (tree outer
, tree inner
)
5179 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5182 prev
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5184 push_inner_scope_r (outer
, prev
);
5185 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5187 cp_binding_level
*save_template_parm
= 0;
5188 /* Temporary take out template parameter scopes. They are saved
5189 in reversed order in save_template_parm. */
5190 while (current_binding_level
->kind
== sk_template_parms
)
5192 cp_binding_level
*b
= current_binding_level
;
5193 current_binding_level
= b
->level_chain
;
5194 b
->level_chain
= save_template_parm
;
5195 save_template_parm
= b
;
5198 resume_scope (NAMESPACE_LEVEL (inner
));
5199 current_namespace
= inner
;
5201 /* Restore template parameter scopes. */
5202 while (save_template_parm
)
5204 cp_binding_level
*b
= save_template_parm
;
5205 save_template_parm
= b
->level_chain
;
5206 b
->level_chain
= current_binding_level
;
5207 current_binding_level
= b
;
5214 /* Enter the scope INNER from current scope. INNER must be a scope
5215 nested inside current scope. This works with both name lookup and
5216 pushing name into scope. In case a template parameter scope is present,
5217 namespace is pushed under the template parameter scope according to
5218 name lookup rule in 14.6.1/6.
5220 Return the former current scope suitable for pop_inner_scope. */
5223 push_inner_scope (tree inner
)
5225 tree outer
= current_scope ();
5227 outer
= current_namespace
;
5229 push_inner_scope_r (outer
, inner
);
5233 /* Exit the current scope INNER back to scope OUTER. */
5236 pop_inner_scope (tree outer
, tree inner
)
5239 || (TREE_CODE (inner
) != NAMESPACE_DECL
&& !CLASS_TYPE_P (inner
)))
5242 while (outer
!= inner
)
5244 if (TREE_CODE (inner
) == NAMESPACE_DECL
)
5246 cp_binding_level
*save_template_parm
= 0;
5247 /* Temporary take out template parameter scopes. They are saved
5248 in reversed order in save_template_parm. */
5249 while (current_binding_level
->kind
== sk_template_parms
)
5251 cp_binding_level
*b
= current_binding_level
;
5252 current_binding_level
= b
->level_chain
;
5253 b
->level_chain
= save_template_parm
;
5254 save_template_parm
= b
;
5259 /* Restore template parameter scopes. */
5260 while (save_template_parm
)
5262 cp_binding_level
*b
= save_template_parm
;
5263 save_template_parm
= b
->level_chain
;
5264 b
->level_chain
= current_binding_level
;
5265 current_binding_level
= b
;
5271 inner
= CP_DECL_CONTEXT (TREE_CODE (inner
) == NAMESPACE_DECL
? inner
: TYPE_NAME (inner
));
5275 /* Do a pushlevel for class declarations. */
5278 pushlevel_class (void)
5280 class_binding_level
= begin_scope (sk_class
, current_class_type
);
5283 /* ...and a poplevel for class declarations. */
5286 poplevel_class (void)
5288 cp_binding_level
*level
= class_binding_level
;
5289 cp_class_binding
*cb
;
5293 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5294 gcc_assert (level
!= 0);
5296 /* If we're leaving a toplevel class, cache its binding level. */
5297 if (current_class_depth
== 1)
5298 previous_class_level
= level
;
5299 for (shadowed
= level
->type_shadowed
;
5301 shadowed
= TREE_CHAIN (shadowed
))
5302 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed
), TREE_VALUE (shadowed
));
5304 /* Remove the bindings for all of the class-level declarations. */
5305 if (level
->class_shadowed
)
5307 FOR_EACH_VEC_ELT (*level
->class_shadowed
, i
, cb
)
5309 IDENTIFIER_BINDING (cb
->identifier
) = cb
->base
->previous
;
5310 cxx_binding_free (cb
->base
);
5312 ggc_free (level
->class_shadowed
);
5313 level
->class_shadowed
= NULL
;
5316 /* Now, pop out of the binding level which we created up in the
5317 `pushlevel_class' routine. */
5318 gcc_assert (current_binding_level
== level
);
5320 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5323 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
5324 appropriate. DECL is the value to which a name has just been
5325 bound. CLASS_TYPE is the class in which the lookup occurred. */
5328 set_inherited_value_binding_p (cxx_binding
*binding
, tree decl
,
5331 if (binding
->value
== decl
&& TREE_CODE (decl
) != TREE_LIST
)
5335 if (TREE_CODE (decl
) == OVERLOAD
)
5336 context
= ovl_scope (decl
);
5339 gcc_assert (DECL_P (decl
));
5340 context
= context_for_name_lookup (decl
);
5343 if (is_properly_derived_from (class_type
, context
))
5344 INHERITED_VALUE_BINDING_P (binding
) = 1;
5346 INHERITED_VALUE_BINDING_P (binding
) = 0;
5348 else if (binding
->value
== decl
)
5349 /* We only encounter a TREE_LIST when there is an ambiguity in the
5350 base classes. Such an ambiguity can be overridden by a
5351 definition in this class. */
5352 INHERITED_VALUE_BINDING_P (binding
) = 1;
5354 INHERITED_VALUE_BINDING_P (binding
) = 0;
5357 /* Make the declaration of X appear in CLASS scope. */
5360 pushdecl_class_level (tree x
)
5362 bool is_valid
= true;
5365 /* Do nothing if we're adding to an outer lambda closure type,
5366 outer_binding will add it later if it's needed. */
5367 if (current_class_type
!= class_binding_level
->this_entity
)
5370 subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5371 /* Get the name of X. */
5372 tree name
= OVL_NAME (x
);
5376 is_valid
= push_class_level_binding (name
, x
);
5377 if (TREE_CODE (x
) == TYPE_DECL
)
5378 set_identifier_type_value (name
, x
);
5380 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
5382 /* If X is an anonymous aggregate, all of its members are
5383 treated as if they were members of the class containing the
5384 aggregate, for naming purposes. */
5385 location_t save_location
= input_location
;
5386 tree anon
= TREE_TYPE (x
);
5387 if (vec
<tree
, va_gc
> *member_vec
= CLASSTYPE_MEMBER_VEC (anon
))
5388 for (unsigned ix
= member_vec
->length (); ix
--;)
5390 tree binding
= (*member_vec
)[ix
];
5391 if (STAT_HACK_P (binding
))
5393 if (!pushdecl_class_level (STAT_TYPE (binding
)))
5395 binding
= STAT_DECL (binding
);
5397 if (!pushdecl_class_level (binding
))
5401 for (tree f
= TYPE_FIELDS (anon
); f
; f
= DECL_CHAIN (f
))
5402 if (TREE_CODE (f
) == FIELD_DECL
)
5404 input_location
= DECL_SOURCE_LOCATION (f
);
5405 if (!pushdecl_class_level (f
))
5408 input_location
= save_location
;
5410 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5414 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
5415 scope. If the value returned is non-NULL, and the PREVIOUS field
5416 is not set, callers must set the PREVIOUS field explicitly. */
5418 static cxx_binding
*
5419 get_class_binding (tree name
, cp_binding_level
*scope
)
5424 cxx_binding
*binding
;
5426 class_type
= scope
->this_entity
;
5428 /* Get the type binding. */
5429 type_binding
= lookup_member (class_type
, name
,
5430 /*protect=*/2, /*want_type=*/true,
5431 tf_warning_or_error
);
5432 /* Get the value binding. */
5433 value_binding
= lookup_member (class_type
, name
,
5434 /*protect=*/2, /*want_type=*/false,
5435 tf_warning_or_error
);
5438 && (TREE_CODE (value_binding
) == TYPE_DECL
5439 || DECL_CLASS_TEMPLATE_P (value_binding
)
5440 || (TREE_CODE (value_binding
) == TREE_LIST
5441 && TREE_TYPE (value_binding
) == error_mark_node
5442 && (TREE_CODE (TREE_VALUE (value_binding
))
5444 /* We found a type binding, even when looking for a non-type
5445 binding. This means that we already processed this binding
5448 else if (value_binding
)
5450 if (TREE_CODE (value_binding
) == TREE_LIST
5451 && TREE_TYPE (value_binding
) == error_mark_node
)
5452 /* NAME is ambiguous. */
5454 else if (BASELINK_P (value_binding
))
5455 /* NAME is some overloaded functions. */
5456 value_binding
= BASELINK_FUNCTIONS (value_binding
);
5459 /* If we found either a type binding or a value binding, create a
5460 new binding object. */
5461 if (type_binding
|| value_binding
)
5463 binding
= new_class_binding (name
,
5467 set_inherited_value_binding_p (binding
, value_binding
, class_type
);
5475 /* Make the declaration(s) of X appear in CLASS scope under the name
5476 NAME. Returns true if the binding is valid. */
5479 push_class_level_binding_1 (tree name
, tree x
)
5481 cxx_binding
*binding
;
5485 /* The class_binding_level will be NULL if x is a template
5486 parameter name in a member template. */
5487 if (!class_binding_level
)
5490 if (name
== error_mark_node
)
5493 /* Can happen for an erroneous declaration (c++/60384). */
5494 if (!identifier_p (name
))
5496 gcc_assert (errorcount
|| sorrycount
);
5500 /* Check for invalid member names. But don't worry about a default
5501 argument-scope lambda being pushed after the class is complete. */
5502 gcc_assert (TYPE_BEING_DEFINED (current_class_type
)
5503 || LAMBDA_TYPE_P (TREE_TYPE (decl
)));
5504 /* Check that we're pushing into the right binding level. */
5505 gcc_assert (current_class_type
== class_binding_level
->this_entity
);
5507 /* We could have been passed a tree list if this is an ambiguous
5508 declaration. If so, pull the declaration out because
5509 check_template_shadow will not handle a TREE_LIST. */
5510 if (TREE_CODE (decl
) == TREE_LIST
5511 && TREE_TYPE (decl
) == error_mark_node
)
5512 decl
= TREE_VALUE (decl
);
5514 if (!check_template_shadow (decl
))
5519 If T is the name of a class, then each of the following shall
5520 have a name different from T:
5522 -- every static data member of class T;
5524 -- every member of class T that is itself a type;
5526 -- every enumerator of every member of class T that is an
5529 -- every member of every anonymous union that is a member of
5532 (Non-static data members were also forbidden to have the same
5533 name as T until TC1.) */
5535 || TREE_CODE (x
) == CONST_DECL
5536 || (TREE_CODE (x
) == TYPE_DECL
5537 && !DECL_SELF_REFERENCE_P (x
))
5538 /* A data member of an anonymous union. */
5539 || (TREE_CODE (x
) == FIELD_DECL
5540 && DECL_CONTEXT (x
) != current_class_type
))
5541 && DECL_NAME (x
) == DECL_NAME (TYPE_NAME (current_class_type
)))
5543 tree scope
= context_for_name_lookup (x
);
5544 if (TYPE_P (scope
) && same_type_p (scope
, current_class_type
))
5546 error_at (DECL_SOURCE_LOCATION (x
),
5547 "%qD has the same name as the class in which it is "
5553 /* Get the current binding for NAME in this class, if any. */
5554 binding
= IDENTIFIER_BINDING (name
);
5555 if (!binding
|| binding
->scope
!= class_binding_level
)
5557 binding
= get_class_binding (name
, class_binding_level
);
5558 /* If a new binding was created, put it at the front of the
5559 IDENTIFIER_BINDING list. */
5562 binding
->previous
= IDENTIFIER_BINDING (name
);
5563 IDENTIFIER_BINDING (name
) = binding
;
5567 /* If there is already a binding, then we may need to update the
5569 if (binding
&& binding
->value
)
5571 tree bval
= binding
->value
;
5572 tree old_decl
= NULL_TREE
;
5573 tree target_decl
= strip_using_decl (decl
);
5574 tree target_bval
= strip_using_decl (bval
);
5576 if (INHERITED_VALUE_BINDING_P (binding
))
5578 /* If the old binding was from a base class, and was for a
5579 tag name, slide it over to make room for the new binding.
5580 The old binding is still visible if explicitly qualified
5581 with a class-key. */
5582 if (TREE_CODE (target_bval
) == TYPE_DECL
5583 && DECL_ARTIFICIAL (target_bval
)
5584 && !(TREE_CODE (target_decl
) == TYPE_DECL
5585 && DECL_ARTIFICIAL (target_decl
)))
5587 old_decl
= binding
->type
;
5588 binding
->type
= bval
;
5589 binding
->value
= NULL_TREE
;
5590 INHERITED_VALUE_BINDING_P (binding
) = 0;
5595 /* Any inherited type declaration is hidden by the type
5596 declaration in the derived class. */
5597 if (TREE_CODE (target_decl
) == TYPE_DECL
5598 && DECL_ARTIFICIAL (target_decl
))
5599 binding
->type
= NULL_TREE
;
5602 else if (TREE_CODE (decl
) == USING_DECL
5603 && TREE_CODE (bval
) == USING_DECL
5604 && same_type_p (USING_DECL_SCOPE (decl
),
5605 USING_DECL_SCOPE (bval
)))
5606 /* This is a using redeclaration that will be diagnosed later
5607 in supplement_binding */
5609 else if (TREE_CODE (decl
) == USING_DECL
5610 && TREE_CODE (bval
) == USING_DECL
5611 && DECL_DEPENDENT_P (decl
)
5612 && DECL_DEPENDENT_P (bval
))
5614 else if (TREE_CODE (decl
) == USING_DECL
5615 && OVL_P (target_bval
))
5617 else if (TREE_CODE (bval
) == USING_DECL
5618 && OVL_P (target_decl
))
5620 else if (OVL_P (target_decl
)
5621 && OVL_P (target_bval
))
5624 if (old_decl
&& binding
->scope
== class_binding_level
)
5627 /* It is always safe to clear INHERITED_VALUE_BINDING_P
5628 here. This function is only used to register bindings
5629 from with the class definition itself. */
5630 INHERITED_VALUE_BINDING_P (binding
) = 0;
5635 /* Note that we declared this value so that we can issue an error if
5636 this is an invalid redeclaration of a name already used for some
5638 note_name_declared_in_class (name
, decl
);
5640 /* If we didn't replace an existing binding, put the binding on the
5641 stack of bindings for the identifier, and update the shadowed
5643 if (binding
&& binding
->scope
== class_binding_level
)
5644 /* Supplement the existing binding. */
5645 ok
= supplement_binding (binding
, decl
);
5648 /* Create a new binding. */
5649 push_binding (name
, decl
, class_binding_level
);
5656 /* Wrapper for push_class_level_binding_1. */
5659 push_class_level_binding (tree name
, tree x
)
5662 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5663 ret
= push_class_level_binding_1 (name
, x
);
5664 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5668 /* Process and lookup a using decl SCOPE::lookup.name, filling in
5669 lookup.values & lookup.type. Return a USING_DECL, or NULL_TREE on
5673 lookup_using_decl (tree scope
, name_lookup
&lookup
)
5675 tree current
= current_scope ();
5676 bool dependent_p
= false;
5677 tree binfo
= NULL_TREE
;
5678 base_kind b_kind
= bk_not_base
;
5680 /* Because C++20 breaks the invariant that only member using-decls
5681 refer to members and only non-member using-decls refer to
5682 non-members, we first do the lookups, and then do validation that
5683 what we found is ok. */
5685 if (TREE_CODE (scope
) == ENUMERAL_TYPE
5686 && cxx_dialect
< cxx20
5687 && UNSCOPED_ENUM_P (scope
)
5688 && !TYPE_FUNCTION_SCOPE_P (scope
))
5690 /* PR c++/60265 argued that since C++11 added explicit enum scope, we
5691 should allow it as meaning the enclosing scope. I don't see any
5692 justification for this in C++11, but let's keep allowing it. */
5693 tree ctx
= CP_TYPE_CONTEXT (scope
);
5694 if (CLASS_TYPE_P (ctx
) == CLASS_TYPE_P (current
))
5698 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
5700 /* Naming a namespace member. */
5701 qualified_namespace_lookup (scope
, &lookup
);
5703 if (TYPE_P (current
)
5706 || cxx_dialect
< cxx20
5707 || TREE_CODE (lookup
.value
) != CONST_DECL
))
5709 error ("using-declaration for non-member at class scope");
5713 else if (TREE_CODE (scope
) == ENUMERAL_TYPE
)
5715 /* Naming an enumeration member. */
5716 if (cxx_dialect
< cxx20
)
5717 error ("%<using%> with enumeration scope %q#T "
5718 "only available with %<-std=c++20%> or %<-std=gnu++20%>",
5720 lookup
.value
= lookup_enumerator (scope
, lookup
.name
);
5724 /* Naming a class member. This is awkward in C++20, because we
5725 might be naming an enumerator of an unrelated class. */
5727 /* You cannot using-decl a destructor. */
5728 if (TREE_CODE (lookup
.name
) == BIT_NOT_EXPR
)
5730 error ("%<%T::%D%> names destructor", scope
, lookup
.name
);
5734 /* Using T::T declares inheriting ctors, even if T is a typedef. */
5735 if (MAYBE_CLASS_TYPE_P (scope
)
5736 && (lookup
.name
== TYPE_IDENTIFIER (scope
)
5737 || constructor_name_p (lookup
.name
, scope
)))
5739 if (!TYPE_P (current
))
5741 error ("non-member using-declaration names constructor of %qT",
5745 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS
);
5746 lookup
.name
= ctor_identifier
;
5747 CLASSTYPE_NON_AGGREGATE (current
) = true;
5750 if (!MAYBE_CLASS_TYPE_P (scope
))
5752 else if (TYPE_P (current
))
5754 dependent_p
= dependent_scope_p (scope
);
5757 binfo
= lookup_base (current
, scope
, ba_any
, &b_kind
, tf_none
);
5758 gcc_checking_assert (b_kind
>= bk_not_base
);
5760 if (lookup
.name
== ctor_identifier
)
5762 /* Even if there are dependent bases, SCOPE will not
5763 be direct base, no matter. */
5764 if (b_kind
< bk_proper_base
|| !binfo_direct_p (binfo
))
5766 error ("%qT is not a direct base of %qT", scope
, current
);
5770 else if (b_kind
< bk_proper_base
)
5771 binfo
= TYPE_BINFO (scope
);
5772 else if (IDENTIFIER_CONV_OP_P (lookup
.name
)
5773 && dependent_type_p (TREE_TYPE (lookup
.name
)))
5778 binfo
= TYPE_BINFO (scope
);
5783 lookup
.value
= lookup_member (binfo
, lookup
.name
, /*protect=*/2,
5784 /*want_type=*/false, tf_none
);
5786 tree saved_value
= lookup
.value
;
5788 && b_kind
< bk_proper_base
)
5790 if (cxx_dialect
>= cxx20
5791 && TREE_CODE (lookup
.value
) == CONST_DECL
)
5793 /* Using an unrelated enum; check access here rather
5794 than separately for class and non-class using. */
5795 perform_or_defer_access_check
5796 (binfo
, lookup
.value
, lookup
.value
, tf_warning_or_error
);
5797 /* And then if this is a copy from handle_using_decl, look
5798 through to the original enumerator. */
5799 if (CONST_DECL_USING_P (lookup
.value
))
5800 lookup
.value
= DECL_ABSTRACT_ORIGIN (lookup
.value
);
5803 lookup
.value
= NULL_TREE
;
5808 if (!TYPE_P (current
))
5810 error ("using-declaration for member at non-class scope");
5814 if (b_kind
< bk_proper_base
)
5816 if (b_kind
== bk_not_base
&& any_dependent_bases_p ())
5817 /* Treat as-if dependent. */
5821 auto_diagnostic_group g
;
5822 error_not_base_type (scope
, current
);
5823 if (saved_value
&& DECL_IMPLICIT_TYPEDEF_P (saved_value
)
5824 && (TREE_CODE (TREE_TYPE (saved_value
))
5826 inform (input_location
,
5827 "did you mean %<using enum %T::%D%>?",
5828 scope
, lookup
.name
);
5836 /* Did we find anything sane? */
5839 else if (!lookup
.value
)
5841 error ("%qD has not been declared in %qD", lookup
.name
, scope
);
5844 else if (TREE_CODE (lookup
.value
) == TREE_LIST
5845 /* We can (independently) have ambiguous implicit typedefs. */
5846 || (lookup
.type
&& TREE_CODE (lookup
.type
) == TREE_LIST
))
5848 error ("reference to %qD is ambiguous", lookup
.name
);
5849 print_candidates (TREE_CODE (lookup
.value
) == TREE_LIST
5850 ? lookup
.value
: lookup
.type
);
5853 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
5855 error ("using-declaration may not name namespace %qD", lookup
.value
);
5859 if (TYPE_P (current
))
5861 /* In class scope. */
5863 /* Cannot introduce a constructor name. */
5864 if (constructor_name_p (lookup
.name
, current
))
5866 error ("%<%T::%D%> names constructor in %qT",
5867 scope
, lookup
.name
, current
);
5871 if (lookup
.value
&& BASELINK_P (lookup
.value
))
5872 /* The binfo from which the functions came does not matter. */
5873 lookup
.value
= BASELINK_FUNCTIONS (lookup
.value
);
5876 tree using_decl
= build_lang_decl (USING_DECL
, lookup
.name
, NULL_TREE
);
5877 USING_DECL_SCOPE (using_decl
) = scope
;
5878 USING_DECL_DECLS (using_decl
) = lookup
.value
;
5879 DECL_DEPENDENT_P (using_decl
) = dependent_p
;
5880 DECL_CONTEXT (using_decl
) = current
;
5881 if (TYPE_P (current
) && b_kind
== bk_not_base
)
5882 USING_DECL_UNRELATED_P (using_decl
) = true;
5887 /* Process "using SCOPE::NAME" in a class scope. Return the
5888 USING_DECL created. */
5891 do_class_using_decl (tree scope
, tree name
)
5893 if (name
== error_mark_node
5894 || scope
== error_mark_node
)
5897 name_lookup
lookup (name
);
5898 return lookup_using_decl (scope
, lookup
);
5902 /* Return the binding for NAME in NS in the current TU. If NS is
5903 NULL, look in global_namespace. We will not find declarations
5904 from imports. Users of this who, having found nothing, push a new
5905 decl must be prepared for that pushing to match an existing decl. */
5908 get_namespace_binding (tree ns
, tree name
)
5910 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5912 ns
= global_namespace
;
5913 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns
));
5914 tree ret
= NULL_TREE
;
5916 if (tree
*b
= find_namespace_slot (ns
, name
))
5920 if (TREE_CODE (ret
) == BINDING_VECTOR
)
5921 ret
= BINDING_VECTOR_CLUSTER (ret
, 0).slots
[0];
5923 ret
= MAYBE_STAT_DECL (ret
);
5926 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5930 /* Push internal DECL into the global namespace. Does not do the
5931 full overload fn handling and does not add it to the list of things
5932 in the namespace. */
5935 set_global_binding (tree decl
)
5937 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
5939 tree
*slot
= find_namespace_slot (global_namespace
, DECL_NAME (decl
), true);
5942 /* The user's placed something in the implementor's namespace. */
5943 diagnose_name_conflict (decl
, MAYBE_STAT_DECL (*slot
));
5945 /* Force the binding, so compiler internals continue to work. */
5948 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
5951 /* Set the context of a declaration to scope. Complain if we are not
5955 set_decl_namespace (tree decl
, tree scope
, bool friendp
)
5957 /* Get rid of namespace aliases. */
5958 scope
= ORIGINAL_NAMESPACE (scope
);
5960 /* It is ok for friends to be qualified in parallel space. */
5961 if (!friendp
&& !is_nested_namespace (current_namespace
, scope
))
5962 error ("declaration of %qD not in a namespace surrounding %qD",
5964 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5966 /* See whether this has been declared in the namespace or inline
5968 tree old
= NULL_TREE
;
5970 name_lookup
lookup (DECL_NAME (decl
),
5971 LOOK_want::NORMAL
| LOOK_want::HIDDEN_FRIEND
);
5972 if (!lookup
.search_qualified (scope
, /*usings=*/false))
5973 /* No old declaration at all. */
5978 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
5979 if (TREE_CODE (old
) == TREE_LIST
)
5982 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
5983 error ("reference to %qD is ambiguous", decl
);
5984 print_candidates (old
);
5988 if (!DECL_DECLARES_FUNCTION_P (decl
))
5990 /* Don't compare non-function decls with decls_match here, since
5991 it can't check for the correct constness at this
5992 point. pushdecl will find those errors later. */
5994 /* We might have found it in an inline namespace child of SCOPE. */
5995 if (TREE_CODE (decl
) == TREE_CODE (old
))
5996 DECL_CONTEXT (decl
) = DECL_CONTEXT (old
);
5999 /* Writing "N::i" to declare something directly in "N" is invalid. */
6000 if (CP_DECL_CONTEXT (decl
) == current_namespace
6001 && at_namespace_scope_p ())
6002 error_at (DECL_SOURCE_LOCATION (decl
),
6003 "explicit qualification in declaration of %qD", decl
);
6007 /* Since decl is a function, old should contain a function decl. */
6011 /* It didn't work, go back to the explicit scope. */
6012 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
6013 error ("%qD should have been declared inside %qD", decl
, scope
);
6018 /* We handle these in check_explicit_instantiation_namespace. */
6019 if (processing_explicit_instantiation
)
6021 if (processing_template_decl
|| processing_specialization
)
6022 /* We have not yet called push_template_decl to turn a
6023 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
6024 match. But, we'll check later, when we construct the
6028 /* Instantiations or specializations of templates may be declared as
6029 friends in any namespace. */
6030 if (friendp
&& DECL_USE_TEMPLATE (decl
))
6033 tree found
= NULL_TREE
;
6034 bool hidden_p
= false;
6036 for (lkp_iterator
iter (old
); iter
; ++iter
)
6038 if (iter
.using_p ())
6043 /* Adjust DECL_CONTEXT first so decls_match will return true
6044 if DECL will match a declaration in an inline namespace. */
6045 DECL_CONTEXT (decl
) = DECL_CONTEXT (ofn
);
6046 if (decls_match (decl
, ofn
))
6050 /* We found more than one matching declaration. This
6051 can happen if we have two inline namespace children,
6052 each containing a suitable declaration. */
6053 DECL_CONTEXT (decl
) = FROB_CONTEXT (scope
);
6057 hidden_p
= iter
.hidden_p ();
6065 pedwarn (DECL_SOURCE_LOCATION (decl
), 0,
6066 "%qD has not been declared within %qD", decl
, scope
);
6067 inform (DECL_SOURCE_LOCATION (found
),
6068 "only here as a %<friend%>");
6070 DECL_CONTEXT (decl
) = DECL_CONTEXT (found
);
6077 /* Return the namespace where the current declaration is declared. */
6080 current_decl_namespace (void)
6083 /* If we have been pushed into a different namespace, use it. */
6084 if (!vec_safe_is_empty (decl_namespace_list
))
6085 return decl_namespace_list
->last ();
6087 if (current_class_type
)
6088 result
= decl_namespace_context (current_class_type
);
6089 else if (current_function_decl
)
6090 result
= decl_namespace_context (current_function_decl
);
6092 result
= current_namespace
;
6096 /* Process any ATTRIBUTES on a namespace definition. Returns true if
6097 attribute visibility is seen. */
6100 handle_namespace_attrs (tree ns
, tree attributes
)
6103 bool saw_vis
= false;
6105 if (attributes
== error_mark_node
)
6108 for (d
= attributes
; d
; d
= TREE_CHAIN (d
))
6110 tree name
= get_attribute_name (d
);
6111 tree args
= TREE_VALUE (d
);
6113 if (is_attribute_p ("visibility", name
))
6115 /* attribute visibility is a property of the syntactic block
6116 rather than the namespace as a whole, so we don't touch the
6117 NAMESPACE_DECL at all. */
6118 tree x
= args
? TREE_VALUE (args
) : NULL_TREE
;
6119 if (x
== NULL_TREE
|| TREE_CODE (x
) != STRING_CST
|| TREE_CHAIN (args
))
6121 warning (OPT_Wattributes
,
6122 "%qD attribute requires a single NTBS argument",
6127 if (!TREE_PUBLIC (ns
))
6128 warning (OPT_Wattributes
,
6129 "%qD attribute is meaningless since members of the "
6130 "anonymous namespace get local symbols", name
);
6132 push_visibility (TREE_STRING_POINTER (x
), 1);
6135 else if (is_attribute_p ("abi_tag", name
))
6137 if (!DECL_NAME (ns
))
6139 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
6143 if (!DECL_NAMESPACE_INLINE_P (ns
))
6145 warning (OPT_Wattributes
, "ignoring %qD attribute on non-inline "
6151 tree dn
= DECL_NAME (ns
);
6152 args
= build_string (IDENTIFIER_LENGTH (dn
) + 1,
6153 IDENTIFIER_POINTER (dn
));
6154 TREE_TYPE (args
) = char_array_type_node
;
6155 args
= fix_string_type (args
);
6156 args
= build_tree_list (NULL_TREE
, args
);
6158 if (check_abi_tag_args (args
, name
))
6159 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6160 DECL_ATTRIBUTES (ns
));
6162 else if (is_attribute_p ("deprecated", name
))
6164 if (!DECL_NAME (ns
))
6166 warning (OPT_Wattributes
, "ignoring %qD attribute on anonymous "
6170 if (args
&& TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
6172 error ("deprecated message is not a string");
6175 TREE_DEPRECATED (ns
) = 1;
6177 DECL_ATTRIBUTES (ns
) = tree_cons (name
, args
,
6178 DECL_ATTRIBUTES (ns
));
6182 warning (OPT_Wattributes
, "%qD attribute directive ignored",
6191 /* Temporarily set the namespace for the current declaration. */
6194 push_decl_namespace (tree decl
)
6196 if (TREE_CODE (decl
) != NAMESPACE_DECL
)
6197 decl
= decl_namespace_context (decl
);
6198 vec_safe_push (decl_namespace_list
, ORIGINAL_NAMESPACE (decl
));
6201 /* [namespace.memdef]/2 */
6204 pop_decl_namespace (void)
6206 decl_namespace_list
->pop ();
6209 /* Process a namespace-alias declaration. */
6212 do_namespace_alias (tree alias
, tree name_space
)
6214 if (name_space
== error_mark_node
)
6217 gcc_assert (TREE_CODE (name_space
) == NAMESPACE_DECL
);
6219 name_space
= ORIGINAL_NAMESPACE (name_space
);
6221 /* Build the alias. */
6222 alias
= build_lang_decl (NAMESPACE_DECL
, alias
, void_type_node
);
6223 DECL_NAMESPACE_ALIAS (alias
) = name_space
;
6224 DECL_EXTERNAL (alias
) = 1;
6225 DECL_CONTEXT (alias
) = FROB_CONTEXT (current_scope ());
6226 set_originating_module (alias
);
6230 /* Emit debug info for namespace alias. */
6231 if (!building_stmt_list_p ())
6232 (*debug_hooks
->early_global_decl
) (alias
);
6235 /* Like pushdecl, only it places X in the current namespace,
6239 pushdecl_namespace_level (tree x
, bool hiding
)
6241 cp_binding_level
*b
= current_binding_level
;
6244 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
6245 t
= do_pushdecl_with_scope (x
, NAMESPACE_LEVEL (current_namespace
), hiding
);
6247 /* Now, the type_shadowed stack may screw us. Munge it so it does
6249 if (TREE_CODE (t
) == TYPE_DECL
)
6251 tree name
= DECL_NAME (t
);
6253 tree
*ptr
= (tree
*)0;
6254 for (; !global_scope_p (b
); b
= b
->level_chain
)
6256 tree shadowed
= b
->type_shadowed
;
6257 for (; shadowed
; shadowed
= TREE_CHAIN (shadowed
))
6258 if (TREE_PURPOSE (shadowed
) == name
)
6260 ptr
= &TREE_VALUE (shadowed
);
6261 /* Can't break out of the loop here because sometimes
6262 a binding level will have duplicate bindings for
6263 PT names. It's gross, but I haven't time to fix it. */
6266 newval
= TREE_TYPE (t
);
6267 if (ptr
== (tree
*)0)
6269 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
6270 up here if this is changed to an assertion. --KR */
6271 SET_IDENTIFIER_TYPE_VALUE (name
, t
);
6278 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
6282 /* Wrapper around push_local_binding to push the bindings for
6283 a non-member USING_DECL with NAME and VALUE. LOOKUP, if non-null,
6284 is the result of name lookup during template parsing. */
6287 push_using_decl_bindings (name_lookup
*lookup
, tree name
, tree value
)
6289 tree type
= NULL_TREE
;
6291 cxx_binding
*binding
= find_local_binding (current_binding_level
, name
);
6294 value
= binding
->value
;
6295 type
= binding
->type
;
6298 /* DR 36 questions why using-decls at function scope may not be
6299 duplicates. Disallow it, as C++11 claimed and PR 20420
6302 do_nonmember_using_decl (*lookup
, true, true, &value
, &type
);
6306 else if (binding
&& value
== binding
->value
)
6307 /* Redeclaration of this USING_DECL. */;
6308 else if (binding
&& binding
->value
&& TREE_CODE (value
) == OVERLOAD
)
6310 /* We already have this binding, so replace it. */
6311 update_local_overload (IDENTIFIER_BINDING (name
), value
);
6312 IDENTIFIER_BINDING (name
)->value
= value
;
6315 /* Install the new binding. */
6316 push_local_binding (name
, value
, /*using=*/true);
6320 else if (binding
&& type
== binding
->type
)
6324 push_local_binding (name
, type
, /*using=*/true);
6325 set_identifier_type_value (name
, type
);
6329 /* Overload for push_using_decl_bindings that doesn't take a name_lookup. */
6332 push_using_decl_bindings (tree name
, tree value
)
6334 push_using_decl_bindings (nullptr, name
, value
);
6337 /* Process a using declaration in non-class scope. */
6340 finish_nonmember_using_decl (tree scope
, tree name
)
6342 gcc_checking_assert (current_binding_level
->kind
!= sk_class
);
6344 if (scope
== error_mark_node
|| name
== error_mark_node
)
6347 name_lookup
lookup (name
);
6349 tree using_decl
= lookup_using_decl (scope
, lookup
);
6353 /* Emit debug info. */
6354 if (!processing_template_decl
)
6355 cp_emit_debug_info_for_using (lookup
.value
,
6356 current_binding_level
->this_entity
);
6358 if (current_binding_level
->kind
== sk_namespace
)
6360 tree
*slot
= find_namespace_slot (current_namespace
, name
, true);
6361 tree
*mslot
= get_fixed_binding_slot (slot
, name
,
6362 BINDING_SLOT_CURRENT
, true);
6363 bool failed
= false;
6367 /* A module vector. I presume the binding list is going to
6368 be sparser than the import bitmap. Hence iterate over
6369 the former checking for bits set in the bitmap. */
6370 bitmap imports
= get_import_bitmap ();
6371 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
6373 /* Scan the imported bindings. */
6374 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
6375 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
6381 /* Do this in forward order, so we load modules in an order
6382 the user expects. */
6383 for (; ix
--; cluster
++)
6384 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
6386 /* Are we importing this module? */
6387 if (unsigned base
= cluster
->indices
[jx
].base
)
6388 if (unsigned span
= cluster
->indices
[jx
].span
)
6390 if (bitmap_bit_p (imports
, base
))
6392 while (++base
, --span
);
6397 if (cluster
->slots
[jx
].is_lazy ())
6399 gcc_assert (cluster
->indices
[jx
].span
== 1);
6400 lazy_load_binding (cluster
->indices
[jx
].base
,
6401 scope
, name
, &cluster
->slots
[jx
]);
6404 tree value
= cluster
->slots
[jx
];
6406 /* Load errors could mean there's nothing here. */
6409 /* Extract what we can see from here. If there's no
6410 stat_hack, then everything was exported. */
6411 tree type
= NULL_TREE
;
6413 /* If no stat hack, everything is visible. */
6414 if (STAT_HACK_P (value
))
6416 if (STAT_TYPE_VISIBLE_P (value
))
6417 type
= STAT_TYPE (value
);
6418 value
= STAT_VISIBLE (value
);
6421 if (do_nonmember_using_decl (lookup
, false, false,
6432 /* Now do the current slot. */
6433 tree value
= MAYBE_STAT_DECL (*mslot
);
6434 tree type
= MAYBE_STAT_TYPE (*mslot
);
6436 do_nonmember_using_decl (lookup
, false, true, &value
, &type
);
6438 // FIXME: Partition mergeableness?
6439 if (STAT_HACK_P (*mslot
))
6441 STAT_DECL (*mslot
) = value
;
6442 STAT_TYPE (*mslot
) = type
;
6445 *mslot
= stat_hack (value
, type
);
6452 add_decl_expr (using_decl
);
6453 push_using_decl_bindings (&lookup
, name
, NULL_TREE
);
6457 /* Return the declarations that are members of the namespace NS. */
6460 cp_namespace_decls (tree ns
)
6462 return NAMESPACE_LEVEL (ns
)->names
;
6465 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
6466 ignore it or not. Subroutine of lookup_name_1 and lookup_type_scope. */
6469 qualify_lookup (tree val
, LOOK_want want
)
6471 if (val
== NULL_TREE
)
6474 if (bool (want
& LOOK_want::TYPE
))
6476 tree target_val
= strip_using_decl (val
);
6478 if (TREE_CODE (STRIP_TEMPLATE (target_val
)) == TYPE_DECL
)
6482 if (bool (want
& LOOK_want::TYPE_NAMESPACE
))
6483 return TREE_CODE (val
) == NAMESPACE_DECL
;
6488 /* Is there a "using namespace std;" directive within USINGS? */
6491 using_directives_contain_std_p (vec
<tree
, va_gc
> *usings
)
6496 for (unsigned ix
= usings
->length (); ix
--;)
6497 if ((*usings
)[ix
] == std_node
)
6503 /* Is there a "using namespace std;" directive within the current
6504 namespace (or its ancestors)?
6505 Compare with name_lookup::search_unqualified. */
6508 has_using_namespace_std_directive_p ()
6510 for (cp_binding_level
*level
= current_binding_level
;
6512 level
= level
->level_chain
)
6513 if (using_directives_contain_std_p (level
->using_directives
))
6519 /* Subclass of deferred_diagnostic, for issuing a note when
6520 --param cxx-max-namespaces-for-diagnostic-help is reached.
6522 The note should be issued after the error, but before any other
6523 deferred diagnostics. This is handled by decorating a wrapped
6524 deferred_diagnostic, and emitting a note before that wrapped note is
6527 class namespace_limit_reached
: public deferred_diagnostic
6530 namespace_limit_reached (location_t loc
, unsigned limit
, tree name
,
6531 gnu::unique_ptr
<deferred_diagnostic
> wrapped
)
6532 : deferred_diagnostic (loc
),
6533 m_limit (limit
), m_name (name
),
6534 m_wrapped (move (wrapped
))
6538 ~namespace_limit_reached ()
6540 /* Unconditionally warn that the search was truncated. */
6541 inform (get_location (),
6542 "maximum limit of %d namespaces searched for %qE",
6544 /* m_wrapped will be implicitly deleted after this, emitting any followup
6545 diagnostic after the above note. */
6551 gnu::unique_ptr
<deferred_diagnostic
> m_wrapped
;
6554 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion.
6555 Emit a note showing the location of the declaration of the suggestion. */
6557 class show_candidate_location
: public deferred_diagnostic
6560 show_candidate_location (location_t loc
, tree candidate
)
6561 : deferred_diagnostic (loc
),
6562 m_candidate (candidate
)
6566 ~show_candidate_location ()
6568 inform (location_of (m_candidate
), "%qE declared here", m_candidate
);
6575 /* Subclass of deferred_diagnostic, for use when there are multiple candidates
6576 to be suggested by suggest_alternatives_for.
6578 Emit a series of notes showing the various suggestions. */
6580 class suggest_alternatives
: public deferred_diagnostic
6583 suggest_alternatives (location_t loc
, vec
<tree
> candidates
)
6584 : deferred_diagnostic (loc
),
6585 m_candidates (candidates
)
6589 ~suggest_alternatives ()
6591 if (m_candidates
.length ())
6593 inform_n (get_location (), m_candidates
.length (),
6594 "suggested alternative:",
6595 "suggested alternatives:");
6596 for (unsigned ix
= 0; ix
!= m_candidates
.length (); ix
++)
6598 tree val
= m_candidates
[ix
];
6600 inform (location_of (val
), " %qE", val
);
6603 m_candidates
.release ();
6607 vec
<tree
> m_candidates
;
6610 /* A class for encapsulating the result of a search across
6611 multiple namespaces (and scoped enums within them) for an
6612 unrecognized name seen at a given source location. */
6614 class namespace_hints
6617 namespace_hints (location_t loc
, tree name
);
6619 name_hint
convert_candidates_to_name_hint ();
6620 name_hint
maybe_decorate_with_limit (name_hint
);
6623 void maybe_add_candidate_for_scoped_enum (tree scoped_enum
, tree name
);
6627 vec
<tree
> m_candidates
;
6629 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */
6632 /* Was the limit reached? */
6636 /* Constructor for namespace_hints. Search namespaces and scoped enums,
6637 looking for an exact match for unrecognized NAME seen at LOC. */
6639 namespace_hints::namespace_hints (location_t loc
, tree name
)
6640 : m_loc(loc
), m_name (name
)
6642 auto_vec
<tree
> worklist
;
6644 m_candidates
= vNULL
;
6646 m_limit
= param_cxx_max_namespaces_for_diagnostic_help
;
6648 /* Breadth-first search of namespaces. Up to limit namespaces
6649 searched (limit zero == unlimited). */
6650 worklist
.safe_push (global_namespace
);
6651 for (unsigned ix
= 0; ix
!= worklist
.length (); ix
++)
6653 tree ns
= worklist
[ix
];
6654 name_lookup
lookup (name
);
6656 if (lookup
.search_qualified (ns
, false))
6657 m_candidates
.safe_push (lookup
.value
);
6661 /* Look for child namespaces. We have to do this
6662 indirectly because they are chained in reverse order,
6663 which is confusing to the user. */
6664 auto_vec
<tree
> children
;
6666 for (tree decl
= NAMESPACE_LEVEL (ns
)->names
;
6667 decl
; decl
= TREE_CHAIN (decl
))
6669 if (TREE_CODE (decl
) == NAMESPACE_DECL
6670 && !DECL_NAMESPACE_ALIAS (decl
)
6671 && !DECL_NAMESPACE_INLINE_P (decl
))
6672 children
.safe_push (decl
);
6674 /* Look for exact matches for NAME within scoped enums.
6675 These aren't added to the worklist, and so don't count
6676 against the search limit. */
6677 if (TREE_CODE (decl
) == TYPE_DECL
)
6679 tree type
= TREE_TYPE (decl
);
6680 if (SCOPED_ENUM_P (type
))
6681 maybe_add_candidate_for_scoped_enum (type
, name
);
6685 while (!m_limited
&& !children
.is_empty ())
6687 if (worklist
.length () == m_limit
)
6690 worklist
.safe_push (children
.pop ());
6696 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc
6697 for m_name, an IDENTIFIER_NODE for which name lookup failed.
6699 If m_candidates is non-empty, use it to generate a suggestion and/or
6700 a deferred diagnostic that lists the possible candidate(s).
6704 namespace_hints::convert_candidates_to_name_hint ()
6706 /* How many candidates do we have? */
6708 /* If we have just one candidate, issue a name_hint with it as a suggestion
6709 (so that consumers are able to suggest it within the error message and emit
6710 it as a fix-it hint), and with a note showing the candidate's location. */
6711 if (m_candidates
.length () == 1)
6713 tree candidate
= m_candidates
[0];
6714 /* Clean up CANDIDATES. */
6715 m_candidates
.release ();
6716 return name_hint (expr_to_string (candidate
),
6717 new show_candidate_location (m_loc
, candidate
));
6719 else if (m_candidates
.length () > 1)
6720 /* If we have more than one candidate, issue a name_hint without a single
6721 "suggestion", but with a deferred diagnostic that lists the
6722 various candidates. This takes ownership of m_candidates. */
6723 return name_hint (NULL
, new suggest_alternatives (m_loc
, m_candidates
));
6725 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */
6726 gcc_assert (m_candidates
.length () == 0);
6727 gcc_assert (m_candidates
== vNULL
);
6729 return name_hint ();
6732 /* If --param cxx-max-namespaces-for-diagnostic-help was reached,
6733 then we want to emit a note about after the error, but before
6734 any other deferred diagnostics.
6736 Handle this by figuring out what hint is needed, then optionally
6737 decorating HINT with a namespace_limit_reached wrapper. */
6740 namespace_hints::maybe_decorate_with_limit (name_hint hint
)
6743 return name_hint (hint
.suggestion (),
6744 new namespace_limit_reached (m_loc
, m_limit
,
6746 hint
.take_deferred ()));
6751 /* Look inside SCOPED_ENUM for exact matches for NAME.
6752 If one is found, add its CONST_DECL to m_candidates. */
6755 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum
,
6758 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
6760 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
6762 tree id
= TREE_PURPOSE (iter
);
6765 m_candidates
.safe_push (TREE_VALUE (iter
));
6771 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6774 Search through all available namespaces and any scoped enums within them
6775 and generate a suggestion and/or a deferred diagnostic that lists possible
6778 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also
6779 look for near-matches and suggest the best near-match, if there is one.
6781 If nothing is found, then an empty name_hint is returned. */
6784 suggest_alternatives_for (location_t location
, tree name
,
6785 bool suggest_misspellings
)
6787 /* First, search for exact matches in other namespaces. */
6788 namespace_hints
ns_hints (location
, name
);
6789 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6791 /* Otherwise, try other approaches. */
6793 result
= suggest_alternatives_for_1 (location
, name
, suggest_misspellings
);
6795 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6798 /* The second half of suggest_alternatives_for, for when no exact matches
6799 were found in other namespaces. */
6802 suggest_alternatives_for_1 (location_t location
, tree name
,
6803 bool suggest_misspellings
)
6805 /* No candidates were found in the available namespaces. */
6807 /* If there's a "using namespace std;" active, and this
6808 is one of the most common "std::" names, then it's probably a
6809 missing #include. */
6810 if (has_using_namespace_std_directive_p ())
6812 name_hint hint
= maybe_suggest_missing_std_header (location
, name
);
6817 /* Otherwise, consider misspellings. */
6818 if (!suggest_misspellings
)
6819 return name_hint ();
6821 return lookup_name_fuzzy (name
, FUZZY_LOOKUP_NAME
, location
);
6824 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which
6827 Search through all available namespaces and generate a suggestion and/or
6828 a deferred diagnostic that lists possible candidate(s).
6830 This is similiar to suggest_alternatives_for, but doesn't fallback to
6831 the other approaches used by that function. */
6834 suggest_alternatives_in_other_namespaces (location_t location
, tree name
)
6836 namespace_hints
ns_hints (location
, name
);
6838 name_hint result
= ns_hints
.convert_candidates_to_name_hint ();
6840 return ns_hints
.maybe_decorate_with_limit (gnu::move (result
));
6843 /* A well-known name within the C++ standard library, returned by
6844 get_std_name_hint. */
6846 struct std_name_hint
6848 /* A name within "std::". */
6851 /* The header name defining it within the C++ Standard Library
6852 (with '<' and '>'). */
6855 /* The dialect of C++ in which this was added. */
6856 enum cxx_dialect min_dialect
;
6859 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names
6860 for some of the most common names within "std::".
6861 Given non-NULL NAME, return the std_name_hint for it, or NULL. */
6863 static const std_name_hint
*
6864 get_std_name_hint (const char *name
)
6866 static const std_name_hint hints
[] = {
6868 {"any", "<any>", cxx17
},
6869 {"any_cast", "<any>", cxx17
},
6870 {"make_any", "<any>", cxx17
},
6872 {"array", "<array>", cxx11
},
6873 {"to_array", "<array>", cxx20
},
6875 {"atomic", "<atomic>", cxx11
},
6876 {"atomic_flag", "<atomic>", cxx11
},
6877 {"atomic_ref", "<atomic>", cxx20
},
6879 {"bitset", "<bitset>", cxx11
},
6881 {"weak_equality", "<compare>", cxx20
},
6882 {"strong_equality", "<compare>", cxx20
},
6883 {"partial_ordering", "<compare>", cxx20
},
6884 {"weak_ordering", "<compare>", cxx20
},
6885 {"strong_ordering", "<compare>", cxx20
},
6887 {"complex", "<complex>", cxx98
},
6888 {"complex_literals", "<complex>", cxx14
},
6889 /* <condition_variable>. */
6890 {"condition_variable", "<condition_variable>", cxx11
},
6891 {"condition_variable_any", "<condition_variable>", cxx11
},
6893 {"byte", "<cstddef>", cxx17
},
6895 {"deque", "<deque>", cxx98
},
6896 /* <forward_list>. */
6897 {"forward_list", "<forward_list>", cxx11
},
6899 {"basic_filebuf", "<fstream>", cxx98
},
6900 {"basic_ifstream", "<fstream>", cxx98
},
6901 {"basic_ofstream", "<fstream>", cxx98
},
6902 {"basic_fstream", "<fstream>", cxx98
},
6903 {"fstream", "<fstream>", cxx98
},
6904 {"ifstream", "<fstream>", cxx98
},
6905 {"ofstream", "<fstream>", cxx98
},
6907 {"bind", "<functional>", cxx11
},
6908 {"bind_front", "<functional>", cxx20
},
6909 {"function", "<functional>", cxx11
},
6910 {"hash", "<functional>", cxx11
},
6911 {"invoke", "<functional>", cxx17
},
6912 {"mem_fn", "<functional>", cxx11
},
6913 {"not_fn", "<functional>", cxx17
},
6914 {"reference_wrapper", "<functional>", cxx11
},
6915 {"unwrap_reference", "<functional>", cxx20
},
6916 {"unwrap_reference_t", "<functional>", cxx20
},
6917 {"unwrap_ref_decay", "<functional>", cxx20
},
6918 {"unwrap_ref_decay_t", "<functional>", cxx20
},
6920 {"async", "<future>", cxx11
},
6921 {"future", "<future>", cxx11
},
6922 {"packaged_task", "<future>", cxx11
},
6923 {"promise", "<future>", cxx11
},
6925 {"cin", "<iostream>", cxx98
},
6926 {"cout", "<iostream>", cxx98
},
6927 {"cerr", "<iostream>", cxx98
},
6928 {"clog", "<iostream>", cxx98
},
6929 {"wcin", "<iostream>", cxx98
},
6930 {"wcout", "<iostream>", cxx98
},
6931 {"wclog", "<iostream>", cxx98
},
6933 {"istream", "<istream>", cxx98
},
6935 {"advance", "<iterator>", cxx98
},
6936 {"back_inserter", "<iterator>", cxx98
},
6937 {"begin", "<iterator>", cxx11
},
6938 {"distance", "<iterator>", cxx98
},
6939 {"end", "<iterator>", cxx11
},
6940 {"front_inserter", "<iterator>", cxx98
},
6941 {"inserter", "<iterator>", cxx98
},
6942 {"istream_iterator", "<iterator>", cxx98
},
6943 {"istreambuf_iterator", "<iterator>", cxx98
},
6944 {"iterator_traits", "<iterator>", cxx98
},
6945 {"move_iterator", "<iterator>", cxx11
},
6946 {"next", "<iterator>", cxx11
},
6947 {"ostream_iterator", "<iterator>", cxx98
},
6948 {"ostreambuf_iterator", "<iterator>", cxx98
},
6949 {"prev", "<iterator>", cxx11
},
6950 {"reverse_iterator", "<iterator>", cxx98
},
6952 {"ostream", "<ostream>", cxx98
},
6954 {"list", "<list>", cxx98
},
6956 {"map", "<map>", cxx98
},
6957 {"multimap", "<map>", cxx98
},
6959 {"allocate_shared", "<memory>", cxx11
},
6960 {"allocator", "<memory>", cxx98
},
6961 {"allocator_traits", "<memory>", cxx11
},
6962 {"make_shared", "<memory>", cxx11
},
6963 {"make_unique", "<memory>", cxx14
},
6964 {"shared_ptr", "<memory>", cxx11
},
6965 {"unique_ptr", "<memory>", cxx11
},
6966 {"weak_ptr", "<memory>", cxx11
},
6967 /* <memory_resource>. */
6968 {"pmr", "<memory_resource>", cxx17
},
6970 {"mutex", "<mutex>", cxx11
},
6971 {"timed_mutex", "<mutex>", cxx11
},
6972 {"recursive_mutex", "<mutex>", cxx11
},
6973 {"recursive_timed_mutex", "<mutex>", cxx11
},
6974 {"once_flag", "<mutex>", cxx11
},
6975 {"call_once,", "<mutex>", cxx11
},
6976 {"lock", "<mutex>", cxx11
},
6977 {"scoped_lock", "<mutex>", cxx17
},
6978 {"try_lock", "<mutex>", cxx11
},
6979 {"lock_guard", "<mutex>", cxx11
},
6980 {"unique_lock", "<mutex>", cxx11
},
6982 {"optional", "<optional>", cxx17
},
6983 {"make_optional", "<optional>", cxx17
},
6985 {"ostream", "<ostream>", cxx98
},
6986 {"wostream", "<ostream>", cxx98
},
6987 {"ends", "<ostream>", cxx98
},
6988 {"flush", "<ostream>", cxx98
},
6989 {"endl", "<ostream>", cxx98
},
6991 {"queue", "<queue>", cxx98
},
6992 {"priority_queue", "<queue>", cxx98
},
6994 {"set", "<set>", cxx98
},
6995 {"multiset", "<set>", cxx98
},
6996 /* <shared_mutex>. */
6997 {"shared_lock", "<shared_mutex>", cxx14
},
6998 {"shared_mutex", "<shared_mutex>", cxx17
},
6999 {"shared_timed_mutex", "<shared_mutex>", cxx14
},
7000 /* <source_location>. */
7001 {"source_location", "<source_location>", cxx20
},
7003 {"basic_stringbuf", "<sstream>", cxx98
},
7004 {"basic_istringstream", "<sstream>", cxx98
},
7005 {"basic_ostringstream", "<sstream>", cxx98
},
7006 {"basic_stringstream", "<sstream>", cxx98
},
7007 {"istringstream", "<sstream>", cxx98
},
7008 {"ostringstream", "<sstream>", cxx98
},
7009 {"stringstream", "<sstream>", cxx98
},
7011 {"stack", "<stack>", cxx98
},
7013 {"basic_string", "<string>", cxx98
},
7014 {"string", "<string>", cxx98
},
7015 {"wstring", "<string>", cxx98
},
7016 {"u8string", "<string>", cxx20
},
7017 {"u16string", "<string>", cxx11
},
7018 {"u32string", "<string>", cxx11
},
7019 /* <string_view>. */
7020 {"basic_string_view", "<string_view>", cxx17
},
7021 {"string_view", "<string_view>", cxx17
},
7023 {"thread", "<thread>", cxx11
},
7024 {"this_thread", "<thread>", cxx11
},
7026 {"apply", "<tuple>", cxx17
},
7027 {"forward_as_tuple", "<tuple>", cxx11
},
7028 {"make_from_tuple", "<tuple>", cxx17
},
7029 {"make_tuple", "<tuple>", cxx11
},
7030 {"tie", "<tuple>", cxx11
},
7031 {"tuple", "<tuple>", cxx11
},
7032 {"tuple_cat", "<tuple>", cxx11
},
7033 {"tuple_element", "<tuple>", cxx11
},
7034 {"tuple_element_t", "<tuple>", cxx14
},
7035 {"tuple_size", "<tuple>", cxx11
},
7036 {"tuple_size_v", "<tuple>", cxx17
},
7037 /* <type_traits>. */
7038 {"enable_if", "<type_traits>", cxx11
},
7039 {"enable_if_t", "<type_traits>", cxx14
},
7040 {"invoke_result", "<type_traits>", cxx17
},
7041 {"invoke_result_t", "<type_traits>", cxx17
},
7042 {"remove_cvref", "<type_traits>", cxx20
},
7043 {"remove_cvref_t", "<type_traits>", cxx20
},
7044 {"type_identity", "<type_traits>", cxx20
},
7045 {"type_identity_t", "<type_traits>", cxx20
},
7046 {"void_t", "<type_traits>", cxx17
},
7047 {"conjunction", "<type_traits>", cxx17
},
7048 {"conjunction_v", "<type_traits>", cxx17
},
7049 {"disjunction", "<type_traits>", cxx17
},
7050 {"disjunction_v", "<type_traits>", cxx17
},
7051 {"negation", "<type_traits>", cxx17
},
7052 {"negation_v", "<type_traits>", cxx17
},
7053 /* <unordered_map>. */
7054 {"unordered_map", "<unordered_map>", cxx11
},
7055 {"unordered_multimap", "<unordered_map>", cxx11
},
7056 /* <unordered_set>. */
7057 {"unordered_set", "<unordered_set>", cxx11
},
7058 {"unordered_multiset", "<unordered_set>", cxx11
},
7060 {"declval", "<utility>", cxx11
},
7061 {"forward", "<utility>", cxx11
},
7062 {"make_pair", "<utility>", cxx98
},
7063 {"move", "<utility>", cxx11
},
7064 {"pair", "<utility>", cxx98
},
7066 {"variant", "<variant>", cxx17
},
7067 {"visit", "<variant>", cxx17
},
7069 {"vector", "<vector>", cxx98
},
7071 const size_t num_hints
= sizeof (hints
) / sizeof (hints
[0]);
7072 for (size_t i
= 0; i
< num_hints
; i
++)
7074 if (strcmp (name
, hints
[i
].name
) == 0)
7080 /* Describe DIALECT. */
7083 get_cxx_dialect_name (enum cxx_dialect dialect
)
7102 /* Subclass of deferred_diagnostic for use for names in the "std" namespace
7103 that weren't recognized, but for which we know which header it ought to be
7106 Emit a note either suggesting the header to be included, or noting that
7107 the current dialect is too early for the given name. */
7109 class missing_std_header
: public deferred_diagnostic
7112 missing_std_header (location_t loc
,
7113 const char *name_str
,
7114 const std_name_hint
*header_hint
)
7115 : deferred_diagnostic (loc
),
7116 m_name_str (name_str
),
7117 m_header_hint (header_hint
)
7119 ~missing_std_header ()
7121 gcc_rich_location
richloc (get_location ());
7122 if (cxx_dialect
>= m_header_hint
->min_dialect
)
7124 const char *header
= m_header_hint
->header
;
7125 maybe_add_include_fixit (&richloc
, header
, true);
7127 "%<std::%s%> is defined in header %qs;"
7128 " did you forget to %<#include %s%>?",
7129 m_name_str
, header
, header
);
7133 "%<std::%s%> is only available from %s onwards",
7134 m_name_str
, get_cxx_dialect_name (m_header_hint
->min_dialect
));
7138 const char *m_name_str
;
7139 const std_name_hint
*m_header_hint
;
7142 /* Attempt to generate a name_hint that suggests pertinent header files
7143 for NAME at LOCATION, for common names within the "std" namespace,
7144 or an empty name_hint if this isn't applicable. */
7147 maybe_suggest_missing_std_header (location_t location
, tree name
)
7149 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
7151 const char *name_str
= IDENTIFIER_POINTER (name
);
7152 const std_name_hint
*header_hint
= get_std_name_hint (name_str
);
7154 return name_hint ();
7156 return name_hint (NULL
, new missing_std_header (location
, name_str
,
7160 /* Attempt to generate a name_hint that suggests a missing header file
7161 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't
7165 maybe_suggest_missing_header (location_t location
, tree name
, tree scope
)
7167 if (scope
== NULL_TREE
)
7168 return name_hint ();
7169 if (TREE_CODE (scope
) != NAMESPACE_DECL
)
7170 return name_hint ();
7171 /* We only offer suggestions for the "std" namespace. */
7172 if (scope
!= std_node
)
7173 return name_hint ();
7174 return maybe_suggest_missing_std_header (location
, name
);
7177 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name
7178 lookup failed within the explicitly provided SCOPE.
7180 Suggest the best meaningful candidates (if any), otherwise
7181 an empty name_hint is returned. */
7184 suggest_alternative_in_explicit_scope (location_t location
, tree name
,
7187 /* Something went very wrong; don't suggest anything. */
7188 if (name
== error_mark_node
)
7189 return name_hint ();
7191 /* Resolve any namespace aliases. */
7192 scope
= ORIGINAL_NAMESPACE (scope
);
7194 name_hint hint
= maybe_suggest_missing_header (location
, name
, scope
);
7198 cp_binding_level
*level
= NAMESPACE_LEVEL (scope
);
7200 best_match
<tree
, const char *> bm (name
);
7201 consider_binding_level (name
, bm
, level
, false, FUZZY_LOOKUP_NAME
);
7203 /* See if we have a good suggesion for the user. */
7204 const char *fuzzy_name
= bm
.get_best_meaningful_candidate ();
7206 return name_hint (fuzzy_name
, NULL
);
7208 return name_hint ();
7211 /* Given NAME, look within SCOPED_ENUM for possible spell-correction
7215 suggest_alternative_in_scoped_enum (tree name
, tree scoped_enum
)
7217 gcc_assert (SCOPED_ENUM_P (scoped_enum
));
7219 best_match
<tree
, const char *> bm (name
);
7220 for (tree iter
= TYPE_VALUES (scoped_enum
); iter
; iter
= TREE_CHAIN (iter
))
7222 tree id
= TREE_PURPOSE (iter
);
7223 bm
.consider (IDENTIFIER_POINTER (id
));
7225 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7228 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
7231 WANT as for lookup_name_1.
7233 Returns a DECL (or OVERLOAD, or BASELINK) representing the
7234 declaration found. If no suitable declaration can be found,
7235 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is
7236 neither a class-type nor a namespace a diagnostic is issued. */
7239 lookup_qualified_name (tree scope
, tree name
, LOOK_want want
, bool complain
)
7243 if (TREE_CODE (scope
) == NAMESPACE_DECL
)
7245 name_lookup
lookup (name
, want
);
7247 if (qualified_namespace_lookup (scope
, &lookup
))
7251 /* If we have a known type overload, pull it out. This can happen
7253 if (TREE_CODE (t
) == OVERLOAD
&& TREE_TYPE (t
) != unknown_type_node
)
7254 t
= OVL_FUNCTION (t
);
7257 else if (cxx_dialect
!= cxx98
&& TREE_CODE (scope
) == ENUMERAL_TYPE
)
7258 t
= lookup_enumerator (scope
, name
);
7259 else if (is_class_type (scope
, complain
))
7260 t
= lookup_member (scope
, name
, 2, bool (want
& LOOK_want::TYPE
),
7261 tf_warning_or_error
);
7264 return error_mark_node
;
7268 /* Wrapper for the above that takes a string argument. The function name is
7269 not at the beginning of the line to keep this wrapper out of etags. */
7271 tree
lookup_qualified_name (tree t
, const char *p
, LOOK_want w
, bool c
)
7273 return lookup_qualified_name (t
, get_identifier (p
), w
, c
);
7277 Accepts the NAME to lookup and its qualifying SCOPE.
7278 Returns the name/type pair found into the cxx_binding *RESULT,
7279 or false on error. */
7282 qualified_namespace_lookup (tree scope
, name_lookup
*lookup
)
7284 timevar_start (TV_NAME_LOOKUP
);
7285 query_oracle (lookup
->name
);
7286 bool found
= lookup
->search_qualified (ORIGINAL_NAMESPACE (scope
));
7287 timevar_stop (TV_NAME_LOOKUP
);
7291 /* If DECL is suitably visible to the user, consider its name for
7292 spelling correction. */
7295 consider_decl (tree decl
, best_match
<tree
, const char *> &bm
,
7296 bool consider_impl_names
)
7298 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7299 within range for). */
7300 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7303 tree suggestion
= DECL_NAME (decl
);
7307 /* Don't suggest names that are for anonymous aggregate types, as
7308 they are an implementation detail generated by the compiler. */
7309 if (IDENTIFIER_ANON_P (suggestion
))
7312 const char *suggestion_str
= IDENTIFIER_POINTER (suggestion
);
7314 /* Ignore internal names with spaces in them. */
7315 if (strchr (suggestion_str
, ' '))
7318 /* Don't suggest names that are reserved for use by the
7319 implementation, unless NAME began with an underscore. */
7320 if (!consider_impl_names
7321 && name_reserved_for_implementation_p (suggestion_str
))
7324 bm
.consider (suggestion_str
);
7327 /* If DECL is suitably visible to the user, add its name to VEC and
7328 return true. Otherwise return false. */
7331 maybe_add_fuzzy_decl (auto_vec
<tree
> &vec
, tree decl
)
7333 /* Skip compiler-generated variables (e.g. __for_begin/__for_end
7334 within range for). */
7335 if (TREE_CODE (decl
) == VAR_DECL
&& DECL_ARTIFICIAL (decl
))
7338 tree suggestion
= DECL_NAME (decl
);
7342 /* Don't suggest names that are for anonymous aggregate types, as
7343 they are an implementation detail generated by the compiler. */
7344 if (IDENTIFIER_ANON_P (suggestion
))
7347 vec
.safe_push (suggestion
);
7352 /* Examing the namespace binding BINDING, and add at most one instance
7353 of the name, if it contains a visible entity of interest. Return
7354 true if we added something. */
7357 maybe_add_fuzzy_binding (auto_vec
<tree
> &vec
, tree binding
,
7358 lookup_name_fuzzy_kind kind
)
7360 tree value
= NULL_TREE
;
7362 if (STAT_HACK_P (binding
))
7364 if (!STAT_TYPE_HIDDEN_P (binding
)
7365 && STAT_TYPE (binding
))
7367 if (maybe_add_fuzzy_decl (vec
, STAT_TYPE (binding
)))
7370 else if (!STAT_DECL_HIDDEN_P (binding
))
7371 value
= STAT_DECL (binding
);
7376 value
= ovl_skip_hidden (value
);
7379 value
= OVL_FIRST (value
);
7380 if (kind
!= FUZZY_LOOKUP_TYPENAME
7381 || TREE_CODE (STRIP_TEMPLATE (value
)) == TYPE_DECL
)
7382 if (maybe_add_fuzzy_decl (vec
, value
))
7386 /* Nothing found. */
7390 /* Helper function for lookup_name_fuzzy.
7391 Traverse binding level LVL, looking for good name matches for NAME
7394 consider_binding_level (tree name
, best_match
<tree
, const char *> &bm
,
7395 cp_binding_level
*lvl
, bool look_within_fields
,
7396 enum lookup_name_fuzzy_kind kind
)
7398 if (look_within_fields
)
7399 if (lvl
->this_entity
&& TREE_CODE (lvl
->this_entity
) == RECORD_TYPE
)
7401 tree type
= lvl
->this_entity
;
7402 bool want_type_p
= (kind
== FUZZY_LOOKUP_TYPENAME
);
7403 tree best_matching_field
7404 = lookup_member_fuzzy (type
, name
, want_type_p
);
7405 if (best_matching_field
)
7406 bm
.consider (IDENTIFIER_POINTER (best_matching_field
));
7409 /* Only suggest names reserved for the implementation if NAME begins
7410 with an underscore. */
7411 bool consider_implementation_names
= (IDENTIFIER_POINTER (name
)[0] == '_');
7413 if (lvl
->kind
!= sk_namespace
)
7414 for (tree t
= lvl
->names
; t
; t
= TREE_CHAIN (t
))
7418 /* OVERLOADs or decls from using declaration are wrapped into
7420 if (TREE_CODE (d
) == TREE_LIST
)
7421 d
= OVL_FIRST (TREE_VALUE (d
));
7423 /* Don't use bindings from implicitly declared functions,
7424 as they were likely misspellings themselves. */
7425 if (TREE_TYPE (d
) == error_mark_node
)
7428 /* If we want a typename, ignore non-types. */
7429 if (kind
== FUZZY_LOOKUP_TYPENAME
7430 && TREE_CODE (STRIP_TEMPLATE (d
)) != TYPE_DECL
)
7433 consider_decl (d
, bm
, consider_implementation_names
);
7437 /* We need to iterate over the namespace hash table, in order to
7438 not mention hidden entities. But hash table iteration is
7439 (essentially) unpredictable, our correction-distance measure
7440 is very granular, and we pick the first of equal distances.
7441 Hence, we need to call the distance-measurer in a predictable
7442 order. So, iterate over the namespace hash, inserting
7443 visible names into a vector. Then sort the vector. Then
7444 determine spelling distance. */
7446 tree ns
= lvl
->this_entity
;
7449 hash_table
<named_decl_hash
>::iterator end
7450 (DECL_NAMESPACE_BINDINGS (ns
)->end ());
7451 for (hash_table
<named_decl_hash
>::iterator iter
7452 (DECL_NAMESPACE_BINDINGS (ns
)->begin ()); iter
!= end
; ++iter
)
7454 tree binding
= *iter
;
7456 if (TREE_CODE (binding
) == BINDING_VECTOR
)
7458 bitmap imports
= get_import_bitmap ();
7459 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (binding
);
7461 if (tree bind
= cluster
->slots
[BINDING_SLOT_CURRENT
])
7462 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7465 /* Scan the imported bindings. */
7466 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (binding
);
7467 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
7473 for (; ix
--; cluster
++)
7474 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
7477 /* Are we importing this module? */
7478 if (unsigned base
= cluster
->indices
[jx
].base
)
7479 if (unsigned span
= cluster
->indices
[jx
].span
)
7481 if (bitmap_bit_p (imports
, base
))
7483 while (++base
, --span
);
7488 if (cluster
->slots
[jx
].is_lazy ())
7489 /* Let's not read in everything on the first
7492 if (tree bind
= cluster
->slots
[jx
])
7493 if (maybe_add_fuzzy_binding (vec
, bind
, kind
))
7498 maybe_add_fuzzy_binding (vec
, binding
, kind
);
7501 vec
.qsort ([] (const void *a_
, const void *b_
)
7503 return strcmp (IDENTIFIER_POINTER (*(const tree
*)a_
),
7504 IDENTIFIER_POINTER (*(const tree
*)b_
));
7507 /* Examine longest to shortest. */
7508 for (unsigned ix
= vec
.length (); ix
--;)
7510 const char *str
= IDENTIFIER_POINTER (vec
[ix
]);
7512 /* Ignore internal names with spaces in them. */
7513 if (strchr (str
, ' '))
7516 /* Don't suggest names that are reserved for use by the
7517 implementation, unless NAME began with an underscore. */
7518 if (!consider_implementation_names
7519 && name_reserved_for_implementation_p (str
))
7527 /* Subclass of deferred_diagnostic. Notify the user that the
7528 given macro was used before it was defined.
7529 This can be done in the C++ frontend since tokenization happens
7532 class macro_use_before_def
: public deferred_diagnostic
7535 /* Factory function. Return a new macro_use_before_def instance if
7536 appropriate, or return NULL. */
7537 static macro_use_before_def
*
7538 maybe_make (location_t use_loc
, cpp_hashnode
*macro
)
7540 location_t def_loc
= cpp_macro_definition_location (macro
);
7541 if (def_loc
== UNKNOWN_LOCATION
)
7544 /* We only want to issue a note if the macro was used *before* it was
7546 We don't want to issue a note for cases where a macro was incorrectly
7547 used, leaving it unexpanded (e.g. by using the wrong argument
7549 if (!linemap_location_before_p (line_table
, use_loc
, def_loc
))
7552 return new macro_use_before_def (use_loc
, macro
);
7556 /* Ctor. LOC is the location of the usage. MACRO is the
7557 macro that was used. */
7558 macro_use_before_def (location_t loc
, cpp_hashnode
*macro
)
7559 : deferred_diagnostic (loc
), m_macro (macro
)
7564 ~macro_use_before_def ()
7566 if (is_suppressed_p ())
7569 inform (get_location (), "the macro %qs had not yet been defined",
7570 (const char *)m_macro
->ident
.str
);
7571 inform (cpp_macro_definition_location (m_macro
),
7572 "it was later defined here");
7576 cpp_hashnode
*m_macro
;
7579 /* Determine if it can ever make sense to offer RID as a suggestion for
7582 Subroutine of lookup_name_fuzzy. */
7585 suggest_rid_p (enum rid rid
)
7589 /* Support suggesting function-like keywords. */
7590 case RID_STATIC_ASSERT
:
7594 /* Support suggesting the various decl-specifier words, to handle
7595 e.g. "singed" vs "signed" typos. */
7596 if (cp_keyword_starts_decl_specifier_p (rid
))
7599 /* Otherwise, don't offer it. This avoids suggesting e.g. "if"
7600 and "do" for short misspellings, which are likely to lead to
7601 nonsensical results. */
7606 /* Search for near-matches for NAME within the current bindings, and within
7607 macro names, returning the best match as a const char *, or NULL if
7608 no reasonable match is found.
7610 Use LOC for any deferred diagnostics. */
7613 lookup_name_fuzzy (tree name
, enum lookup_name_fuzzy_kind kind
, location_t loc
)
7615 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
7617 /* First, try some well-known names in the C++ standard library, in case
7618 the user forgot a #include. */
7619 const char *header_hint
7620 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name
));
7622 return name_hint (NULL
,
7623 new suggest_missing_header (loc
,
7624 IDENTIFIER_POINTER (name
),
7627 best_match
<tree
, const char *> bm (name
);
7629 cp_binding_level
*lvl
;
7630 for (lvl
= scope_chain
->class_bindings
; lvl
; lvl
= lvl
->level_chain
)
7631 consider_binding_level (name
, bm
, lvl
, true, kind
);
7633 for (lvl
= current_binding_level
; lvl
; lvl
= lvl
->level_chain
)
7634 consider_binding_level (name
, bm
, lvl
, false, kind
);
7636 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO"
7638 x = SOME_OTHER_MACRO (y);
7639 then "SOME_OTHER_MACRO" will survive to the frontend and show up
7640 as a misspelled identifier.
7642 Use the best distance so far so that a candidate is only set if
7643 a macro is better than anything so far. This allows early rejection
7644 (without calculating the edit distance) of macro names that must have
7645 distance >= bm.get_best_distance (), and means that we only get a
7646 non-NULL result for best_macro_match if it's better than any of
7647 the identifiers already checked. */
7648 best_macro_match
bmm (name
, bm
.get_best_distance (), parse_in
);
7649 cpp_hashnode
*best_macro
= bmm
.get_best_meaningful_candidate ();
7650 /* If a macro is the closest so far to NAME, consider it. */
7652 bm
.consider ((const char *)best_macro
->ident
.str
);
7653 else if (bmm
.get_best_distance () == 0)
7655 /* If we have an exact match for a macro name, then either the
7656 macro was used with the wrong argument count, or the macro
7657 has been used before it was defined. */
7658 if (cpp_hashnode
*macro
= bmm
.blithely_get_best_candidate ())
7659 if (cpp_user_macro_p (macro
))
7660 return name_hint (NULL
,
7661 macro_use_before_def::maybe_make (loc
, macro
));
7664 /* Try the "starts_decl_specifier_p" keywords to detect
7665 "singed" vs "signed" typos. */
7666 for (unsigned i
= 0; i
< num_c_common_reswords
; i
++)
7668 const c_common_resword
*resword
= &c_common_reswords
[i
];
7670 if (!suggest_rid_p (resword
->rid
))
7673 tree resword_identifier
= ridpointers
[resword
->rid
];
7674 if (!resword_identifier
)
7676 gcc_assert (TREE_CODE (resword_identifier
) == IDENTIFIER_NODE
);
7678 /* Only consider reserved words that survived the
7679 filtering in init_reswords (e.g. for -std). */
7680 if (!IDENTIFIER_KEYWORD_P (resword_identifier
))
7683 bm
.consider (IDENTIFIER_POINTER (resword_identifier
));
7686 return name_hint (bm
.get_best_meaningful_candidate (), NULL
);
7689 /* Subroutine of outer_binding.
7691 Returns TRUE if BINDING is a binding to a template parameter of
7692 SCOPE. In that case SCOPE is the scope of a primary template
7693 parameter -- in the sense of G++, i.e, a template that has its own
7696 Returns FALSE otherwise. */
7699 binding_to_template_parms_of_scope_p (cxx_binding
*binding
,
7700 cp_binding_level
*scope
)
7702 tree binding_value
, tmpl
, tinfo
;
7705 if (!binding
|| !scope
|| !scope
->this_entity
)
7708 binding_value
= binding
->value
? binding
->value
: binding
->type
;
7709 tinfo
= get_template_info (scope
->this_entity
);
7711 /* BINDING_VALUE must be a template parm. */
7712 if (binding_value
== NULL_TREE
7713 || (!DECL_P (binding_value
)
7714 || !DECL_TEMPLATE_PARM_P (binding_value
)))
7717 /* The level of BINDING_VALUE. */
7719 template_type_parameter_p (binding_value
)
7720 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX
7721 (TREE_TYPE (binding_value
)))
7722 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value
));
7724 /* The template of the current scope, iff said scope is a primary
7727 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo
))
7728 ? TI_TEMPLATE (tinfo
)
7731 /* If the level of the parm BINDING_VALUE equals the depth of TMPL,
7732 then BINDING_VALUE is a parameter of TMPL. */
7733 return (tmpl
&& level
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl
)));
7736 /* Return the innermost non-namespace binding for NAME from a scope
7737 containing BINDING, or, if BINDING is NULL, the current scope.
7738 Please note that for a given template, the template parameters are
7739 considered to be in the scope containing the current scope.
7740 If CLASS_P is false, then class bindings are ignored. */
7743 outer_binding (tree name
,
7744 cxx_binding
*binding
,
7748 cp_binding_level
*scope
;
7749 cp_binding_level
*outer_scope
;
7753 scope
= binding
->scope
->level_chain
;
7754 outer
= binding
->previous
;
7758 scope
= current_binding_level
;
7759 outer
= IDENTIFIER_BINDING (name
);
7761 outer_scope
= outer
? outer
->scope
: NULL
;
7763 /* Because we create class bindings lazily, we might be missing a
7764 class binding for NAME. If there are any class binding levels
7765 between the LAST_BINDING_LEVEL and the scope in which OUTER was
7766 declared, we must lookup NAME in those class scopes. */
7768 while (scope
&& scope
!= outer_scope
&& scope
->kind
!= sk_namespace
)
7770 if (scope
->kind
== sk_class
)
7772 cxx_binding
*class_binding
;
7774 class_binding
= get_class_binding (name
, scope
);
7777 /* Thread this new class-scope binding onto the
7778 IDENTIFIER_BINDING list so that future lookups
7780 class_binding
->previous
= outer
;
7782 binding
->previous
= class_binding
;
7784 IDENTIFIER_BINDING (name
) = class_binding
;
7785 return class_binding
;
7788 /* If we are in a member template, the template parms of the member
7789 template are considered to be inside the scope of the containing
7790 class, but within G++ the class bindings are all pushed between the
7791 template parms and the function body. So if the outer binding is
7792 a template parm for the current scope, return it now rather than
7793 look for a class binding. */
7794 if (outer_scope
&& outer_scope
->kind
== sk_template_parms
7795 && binding_to_template_parms_of_scope_p (outer
, scope
))
7798 scope
= scope
->level_chain
;
7804 /* Return the innermost block-scope or class-scope value binding for
7805 NAME, or NULL_TREE if there is no such binding. */
7808 innermost_non_namespace_value (tree name
)
7810 cxx_binding
*binding
;
7811 binding
= outer_binding (name
, /*binding=*/NULL
, /*class_p=*/true);
7812 return binding
? binding
->value
: NULL_TREE
;
7815 /* Look up NAME in the current binding level and its superiors in the
7816 namespace of variables, functions and typedefs. Return a ..._DECL
7817 node of some kind representing its definition if there is only one
7818 such declaration, or return a TREE_LIST with all the overloaded
7819 definitions if there are many, or return NULL_TREE if it is undefined.
7820 Hidden name, either friend declaration or built-in function, are
7823 WHERE controls which scopes are considered. It is a bit mask of
7824 LOOK_where::BLOCK (look in block scope), LOOK_where::CLASS
7825 (look in class scopes) & LOOK_where::NAMESPACE (look in namespace
7826 scopes). It is an error for no bits to be set. These scopes are
7827 searched from innermost to outermost.
7829 WANT controls what kind of entity we'd happy with.
7830 LOOK_want::NORMAL for normal lookup (implicit typedefs can be
7831 hidden). LOOK_want::TYPE for only TYPE_DECLS, LOOK_want::NAMESPACE
7832 for only NAMESPACE_DECLS. These two can be bit-ored to find
7835 WANT can also have LOOK_want::HIDDEN_FRIEND or
7836 LOOK_want::HIDDEN_LAMBDa added to it. */
7839 lookup_name_1 (tree name
, LOOK_where where
, LOOK_want want
)
7841 tree val
= NULL_TREE
;
7843 gcc_checking_assert (unsigned (where
) != 0);
7844 /* If we're looking for hidden lambda things, we shouldn't be
7845 looking in namespace scope. */
7846 gcc_checking_assert (!bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7847 || !bool (where
& LOOK_where::NAMESPACE
));
7848 query_oracle (name
);
7850 /* Conversion operators are handled specially because ordinary
7851 unqualified name lookup will not find template conversion
7853 if (IDENTIFIER_CONV_OP_P (name
))
7855 cp_binding_level
*level
;
7857 for (level
= current_binding_level
;
7858 level
&& level
->kind
!= sk_namespace
;
7859 level
= level
->level_chain
)
7864 /* A conversion operator can only be declared in a class
7866 if (level
->kind
!= sk_class
)
7869 /* Lookup the conversion operator in the class. */
7870 class_type
= level
->this_entity
;
7871 operators
= lookup_fnfields (class_type
, name
, /*protect=*/0,
7872 tf_warning_or_error
);
7880 /* First, look in non-namespace scopes. */
7882 if (current_class_type
== NULL_TREE
)
7883 /* Maybe avoid searching the binding stack at all. */
7884 where
= LOOK_where (unsigned (where
) & ~unsigned (LOOK_where::CLASS
));
7886 if (bool (where
& (LOOK_where::BLOCK
| LOOK_where::CLASS
)))
7887 for (cxx_binding
*iter
= nullptr;
7888 (iter
= outer_binding (name
, iter
, bool (where
& LOOK_where::CLASS
)));)
7890 /* Skip entities we don't want. */
7891 if (!bool (where
& (LOCAL_BINDING_P (iter
)
7892 ? LOOK_where::BLOCK
: LOOK_where::CLASS
)))
7895 /* If this is the kind of thing we're looking for, we're done. */
7898 tree binding
= NULL_TREE
;
7900 if (!(!iter
->type
&& HIDDEN_TYPE_BINDING_P (iter
))
7901 && (bool (want
& LOOK_want::HIDDEN_LAMBDA
)
7902 || !is_lambda_ignored_entity (iter
->value
))
7903 && qualify_lookup (iter
->value
, want
))
7904 binding
= iter
->value
;
7905 else if (bool (want
& LOOK_want::TYPE
)
7906 && !HIDDEN_TYPE_BINDING_P (iter
)
7908 binding
= iter
->type
;
7912 /* The saved lookups for an operator record 'nothing
7913 found' as error_mark_node. We need to stop the search
7914 here, but not return the error mark node. */
7915 if (binding
== error_mark_node
)
7916 binding
= NULL_TREE
;
7924 /* Now lookup in namespace scopes. */
7925 if (bool (where
& LOOK_where::NAMESPACE
))
7927 name_lookup
lookup (name
, want
);
7928 if (lookup
.search_unqualified
7929 (current_decl_namespace (), current_binding_level
))
7935 /* If we have a known type overload, pull it out. This can happen
7936 for both using decls and unhidden functions. */
7937 if (val
&& TREE_CODE (val
) == OVERLOAD
&& TREE_TYPE (val
) != unknown_type_node
)
7938 val
= OVL_FUNCTION (val
);
7943 /* Wrapper for lookup_name_1. */
7946 lookup_name (tree name
, LOOK_where where
, LOOK_want want
)
7948 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
7949 tree ret
= lookup_name_1 (name
, where
, want
);
7950 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
7955 lookup_name (tree name
)
7957 return lookup_name (name
, LOOK_where::ALL
, LOOK_want::NORMAL
);
7960 /* Look up NAME for type used in elaborated name specifier in
7961 the scopes given by HOW.
7963 Unlike lookup_name_1, we make sure that NAME is actually
7964 declared in the desired scope, not from inheritance, nor using
7965 directive. For using declaration, there is DR138 still waiting
7966 to be resolved. Hidden name coming from an earlier friend
7967 declaration is also returned, and will be made visible unless HOW
7968 is TAG_how::HIDDEN_FRIEND.
7970 A TYPE_DECL best matching the NAME is returned. Catching error
7971 and issuing diagnostics are caller's responsibility. */
7974 lookup_elaborated_type_1 (tree name
, TAG_how how
)
7976 cp_binding_level
*b
= current_binding_level
;
7978 if (b
->kind
!= sk_namespace
)
7979 /* Look in non-namespace scopes. */
7980 for (cxx_binding
*iter
= NULL
;
7981 (iter
= outer_binding (name
, iter
, /*class_p=*/ true)); )
7983 /* First check we're supposed to be looking in this scope --
7984 if we're not, we're done. */
7985 for (; b
!= iter
->scope
; b
= b
->level_chain
)
7986 if (!(b
->kind
== sk_cleanup
7987 || b
->kind
== sk_template_parms
7988 || b
->kind
== sk_function_parms
7989 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
7992 /* Check if this is the kind of thing we're looking for. If
7993 HOW is TAG_how::CURRENT_ONLY, also make sure it doesn't
7994 come from base class. For ITER->VALUE, we can simply use
7995 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use
7998 We check ITER->TYPE before ITER->VALUE in order to handle
7999 typedef struct C {} C;
8002 if (tree type
= iter
->type
)
8004 if (qualify_lookup (type
, LOOK_want::TYPE
)
8005 && (how
!= TAG_how::CURRENT_ONLY
8006 || LOCAL_BINDING_P (iter
)
8007 || DECL_CONTEXT (type
) == iter
->scope
->this_entity
))
8009 if (how
!= TAG_how::HIDDEN_FRIEND
)
8010 /* It is no longer a hidden binding. */
8011 HIDDEN_TYPE_BINDING_P (iter
) = false;
8018 if (qualify_lookup (iter
->value
, LOOK_want::TYPE
)
8019 && (how
!= TAG_how::CURRENT_ONLY
8020 || !INHERITED_VALUE_BINDING_P (iter
)))
8022 if (how
!= TAG_how::HIDDEN_FRIEND
&& !iter
->type
)
8023 /* It is no longer a hidden binding. */
8024 HIDDEN_TYPE_BINDING_P (iter
) = false;
8031 /* Now check if we can look in namespace scope. */
8032 for (; b
->kind
!= sk_namespace
; b
= b
->level_chain
)
8033 if (!(b
->kind
== sk_cleanup
8034 || b
->kind
== sk_template_parms
8035 || b
->kind
== sk_function_parms
8036 || (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)))
8039 /* Look in the innermost namespace. */
8040 tree ns
= b
->this_entity
;
8041 if (tree
*slot
= find_namespace_slot (ns
, name
))
8044 if (TREE_CODE (bind
) == BINDING_VECTOR
)
8045 bind
= BINDING_VECTOR_CLUSTER (bind
, 0).slots
[BINDING_SLOT_CURRENT
];
8049 /* If this is the kind of thing we're looking for, we're done. */
8050 if (tree type
= MAYBE_STAT_TYPE (bind
))
8052 if (how
!= TAG_how::HIDDEN_FRIEND
)
8053 /* No longer hidden. */
8054 STAT_TYPE_HIDDEN_P (*slot
) = false;
8058 else if (tree decl
= MAYBE_STAT_DECL (bind
))
8060 if (qualify_lookup (decl
, LOOK_want::TYPE
))
8062 if (how
!= TAG_how::HIDDEN_FRIEND
&& STAT_HACK_P (bind
)
8063 && STAT_DECL_HIDDEN_P (bind
))
8065 if (STAT_TYPE (bind
))
8066 STAT_DECL_HIDDEN_P (bind
) = false;
8069 /* There is no type, just remove the stat
8074 BINDING_VECTOR_CLUSTER (bind
, 0)
8075 .slots
[BINDING_SLOT_CURRENT
] = decl
;
8083 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
8085 /* We could be redeclaring a global module entity, (from GMF
8086 or header unit), or from another partition, or
8087 specializing an imported template. */
8088 bitmap imports
= get_import_bitmap ();
8089 binding_cluster
*cluster
= BINDING_VECTOR_CLUSTER_BASE (*slot
);
8091 /* Scan the imported bindings. */
8092 unsigned ix
= BINDING_VECTOR_NUM_CLUSTERS (*slot
);
8093 if (BINDING_VECTOR_SLOTS_PER_CLUSTER
== BINDING_SLOTS_FIXED
)
8099 /* Do this in forward order, so we load modules in an order
8100 the user expects. */
8101 for (; ix
--; cluster
++)
8102 for (unsigned jx
= 0; jx
!= BINDING_VECTOR_SLOTS_PER_CLUSTER
; jx
++)
8104 /* Are we importing this module? */
8105 if (unsigned base
= cluster
->indices
[jx
].base
)
8106 if (unsigned span
= cluster
->indices
[jx
].span
)
8108 if (bitmap_bit_p (imports
, base
))
8110 while (++base
, --span
);
8115 if (cluster
->slots
[jx
].is_lazy ())
8117 gcc_assert (cluster
->indices
[jx
].span
== 1);
8118 lazy_load_binding (cluster
->indices
[jx
].base
,
8119 ns
, name
, &cluster
->slots
[jx
]);
8121 tree bind
= cluster
->slots
[jx
];
8123 /* Load errors could mean there's nothing here. */
8126 /* Extract what we can see from here. If there's no
8127 stat_hack, then everything was exported. */
8128 tree type
= NULL_TREE
;
8130 /* If no stat hack, everything is visible. */
8131 if (STAT_HACK_P (bind
))
8133 if (STAT_TYPE_VISIBLE_P (bind
))
8134 type
= STAT_TYPE (bind
);
8135 bind
= STAT_VISIBLE (bind
);
8138 if (type
&& qualify_lookup (type
, LOOK_want::TYPE
))
8141 if (bind
&& qualify_lookup (bind
, LOOK_want::TYPE
))
8145 if (!module_purview_p ())
8147 /* We're in the global module, perhaps there's a tag
8149 // FIXME: This isn't quite right, if we find something
8150 // here, from the language PoV we're not supposed to
8159 /* Wrapper for lookup_type_scope_1. */
8162 lookup_elaborated_type (tree name
, TAG_how how
)
8164 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8165 tree ret
= lookup_elaborated_type_1 (name
, how
);
8166 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8170 /* The type TYPE is being declared. If it is a class template, or a
8171 specialization of a class template, do any processing required and
8172 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
8173 being declared a friend. B is the binding level at which this TYPE
8176 Returns the TYPE_DECL for TYPE, which may have been altered by this
8180 maybe_process_template_type_declaration (tree type
, int is_friend
,
8181 cp_binding_level
*b
)
8183 tree decl
= TYPE_NAME (type
);
8185 if (processing_template_parmlist
)
8186 /* You can't declare a new template type in a template parameter
8187 list. But, you can declare a non-template type:
8189 template <class A*> struct S;
8191 is a forward-declaration of `A'. */
8193 else if (b
->kind
== sk_namespace
8194 && current_binding_level
->kind
!= sk_namespace
)
8195 /* If this new type is being injected into a containing scope,
8196 then it's not a template type. */
8200 gcc_assert (MAYBE_CLASS_TYPE_P (type
)
8201 || TREE_CODE (type
) == ENUMERAL_TYPE
);
8203 if (processing_template_decl
)
8205 decl
= push_template_decl (decl
, is_friend
);
8206 if (decl
== error_mark_node
)
8207 return error_mark_node
;
8209 /* If the current binding level is the binding level for the
8210 template parameters (see the comment in
8211 begin_template_parm_list) and the enclosing level is a class
8212 scope, and we're not looking at a friend, push the
8213 declaration of the member class into the class scope. In the
8214 friend case, push_template_decl will already have put the
8215 friend into global scope, if appropriate. */
8216 if (TREE_CODE (type
) != ENUMERAL_TYPE
8217 && !is_friend
&& b
->kind
== sk_template_parms
8218 && b
->level_chain
->kind
== sk_class
)
8220 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type
));
8222 if (!COMPLETE_TYPE_P (current_class_type
))
8223 maybe_add_class_template_decl_list (current_class_type
,
8224 type
, /*friend_p=*/0);
8232 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case
8233 that the NAME is a class template, the tag is processed but not pushed.
8235 The pushed scope depend on the SCOPE parameter:
8236 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
8238 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
8239 non-template-parameter scope. This case is needed for forward
8241 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
8242 TS_GLOBAL case except that names within template-parameter scopes
8243 are not pushed at all.
8245 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */
8248 do_pushtag (tree name
, tree type
, TAG_how how
)
8252 cp_binding_level
*b
= current_binding_level
;
8255 if (/* Cleanup scopes are not scopes from the point of view of
8257 b
->kind
== sk_cleanup
8258 /* Neither are function parameter scopes. */
8259 || b
->kind
== sk_function_parms
8260 /* Neither are the scopes used to hold template parameters
8261 for an explicit specialization. For an ordinary template
8262 declaration, these scopes are not scopes from the point of
8263 view of the language. */
8264 || (b
->kind
== sk_template_parms
8265 && (b
->explicit_spec_p
|| how
== TAG_how::GLOBAL
)))
8267 else if (b
->kind
== sk_class
&& how
!= TAG_how::CURRENT_ONLY
)
8270 if (b
->kind
== sk_template_parms
)
8277 gcc_assert (identifier_p (name
));
8279 /* Do C++ gratuitous typedefing. */
8280 if (identifier_type_value_1 (name
) != type
)
8283 tree context
= TYPE_CONTEXT (type
);
8287 cp_binding_level
*cb
= b
;
8288 while (cb
->kind
!= sk_namespace
8289 && cb
->kind
!= sk_class
8290 && (cb
->kind
!= sk_function_parms
8291 || !cb
->this_entity
))
8292 cb
= cb
->level_chain
;
8293 tree cs
= cb
->this_entity
;
8295 gcc_checking_assert (TREE_CODE (cs
) == FUNCTION_DECL
8296 ? cs
== current_function_decl
8297 : TYPE_P (cs
) ? cs
== current_class_type
8298 : cs
== current_namespace
);
8300 if (how
== TAG_how::CURRENT_ONLY
8301 || (cs
&& TREE_CODE (cs
) == FUNCTION_DECL
))
8303 else if (cs
&& TYPE_P (cs
))
8304 /* When declaring a friend class of a local class, we want
8305 to inject the newly named class into the scope
8306 containing the local class, not the namespace
8308 context
= decl_function_context (get_type_decl (cs
));
8311 context
= current_namespace
;
8313 tdef
= create_implicit_typedef (name
, type
);
8314 DECL_CONTEXT (tdef
) = FROB_CONTEXT (context
);
8315 set_originating_module (tdef
);
8317 decl
= maybe_process_template_type_declaration
8318 (type
, how
== TAG_how::HIDDEN_FRIEND
, b
);
8319 if (decl
== error_mark_node
)
8322 bool in_class
= false;
8323 if (b
->kind
== sk_class
)
8326 if (!TYPE_BEING_DEFINED (current_class_type
))
8327 /* Don't push anywhere if the class is complete; a lambda in an
8328 NSDMI is not a member of the class. */
8330 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
8331 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
8332 class. But if it's a member template class, we want
8333 the TEMPLATE_DECL, not the TYPE_DECL, so this is done
8335 finish_member_declaration (decl
);
8337 pushdecl_class_level (decl
);
8339 else if (b
->kind
== sk_template_parms
)
8340 in_class
= b
->level_chain
->kind
== sk_class
;
8343 decl
= do_pushdecl_with_scope
8344 (decl
, b
, /*hiding=*/(how
== TAG_how::HIDDEN_FRIEND
));
8345 if (decl
== error_mark_node
)
8348 if (DECL_CONTEXT (decl
) == std_node
8349 && init_list_identifier
== DECL_NAME (TYPE_NAME (type
))
8350 && !CLASSTYPE_TEMPLATE_INFO (type
))
8352 error ("declaration of %<std::initializer_list%> does not match "
8353 "%<#include <initializer_list>%>, isn%'t a template");
8354 return error_mark_node
;
8359 set_identifier_type_value_with_scope (name
, tdef
, b
);
8361 TYPE_CONTEXT (type
) = DECL_CONTEXT (decl
);
8363 /* If this is a local class, keep track of it. We need this
8364 information for name-mangling, and so that it is possible to
8365 find all function definitions in a translation unit in a
8366 convenient way. (It's otherwise tricky to find a member
8367 function definition it's only pointed to from within a local
8369 if (TYPE_FUNCTION_SCOPE_P (type
))
8371 if (processing_template_decl
)
8373 /* Push a DECL_EXPR so we call pushtag at the right time in
8374 template instantiation rather than in some nested context. */
8375 add_decl_expr (decl
);
8377 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */
8378 else if (!LAMBDA_TYPE_P (type
))
8379 determine_local_discriminator (TYPE_NAME (type
));
8383 if (b
->kind
== sk_class
8384 && !COMPLETE_TYPE_P (current_class_type
))
8385 maybe_add_class_template_decl_list (current_class_type
,
8386 type
, /*friend_p=*/0);
8388 decl
= TYPE_NAME (type
);
8389 gcc_assert (TREE_CODE (decl
) == TYPE_DECL
);
8391 /* Set type visibility now if this is a forward declaration. */
8392 TREE_PUBLIC (decl
) = 1;
8393 determine_visibility (decl
);
8398 /* Wrapper for do_pushtag. */
8401 pushtag (tree name
, tree type
, TAG_how how
)
8403 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8404 tree ret
= do_pushtag (name
, type
, how
);
8405 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8410 /* Subroutines for reverting temporarily to top-level for instantiation
8411 of templates and such. We actually need to clear out the class- and
8412 local-value slots of all identifiers, so that only the global values
8413 are at all visible. Simply setting current_binding_level to the global
8414 scope isn't enough, because more binding levels may be pushed. */
8415 struct saved_scope
*scope_chain
;
8417 /* Return true if ID has not already been marked. */
8420 store_binding_p (tree id
)
8422 if (!id
|| !IDENTIFIER_BINDING (id
))
8425 if (IDENTIFIER_MARKED (id
))
8431 /* Add an appropriate binding to *OLD_BINDINGS which needs to already
8432 have enough space reserved. */
8435 store_binding (tree id
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8437 cxx_saved_binding saved
;
8439 gcc_checking_assert (store_binding_p (id
));
8441 IDENTIFIER_MARKED (id
) = 1;
8443 saved
.identifier
= id
;
8444 saved
.binding
= IDENTIFIER_BINDING (id
);
8445 saved
.real_type_value
= REAL_IDENTIFIER_TYPE_VALUE (id
);
8446 (*old_bindings
)->quick_push (saved
);
8447 IDENTIFIER_BINDING (id
) = NULL
;
8451 store_bindings (tree names
, vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8453 static vec
<tree
> bindings_need_stored
;
8457 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8458 for (t
= names
; t
; t
= TREE_CHAIN (t
))
8460 if (TREE_CODE (t
) == TREE_LIST
)
8461 id
= TREE_PURPOSE (t
);
8465 if (store_binding_p (id
))
8466 bindings_need_stored
.safe_push (id
);
8468 if (!bindings_need_stored
.is_empty ())
8470 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8471 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8473 /* We can apparently have duplicates in NAMES. */
8474 if (store_binding_p (id
))
8475 store_binding (id
, old_bindings
);
8477 bindings_need_stored
.truncate (0);
8479 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8482 /* Like store_bindings, but NAMES is a vector of cp_class_binding
8483 objects, rather than a TREE_LIST. */
8486 store_class_bindings (vec
<cp_class_binding
, va_gc
> *names
,
8487 vec
<cxx_saved_binding
, va_gc
> **old_bindings
)
8489 static vec
<tree
> bindings_need_stored
;
8491 cp_class_binding
*cb
;
8493 for (i
= 0; vec_safe_iterate (names
, i
, &cb
); ++i
)
8494 if (store_binding_p (cb
->identifier
))
8495 bindings_need_stored
.safe_push (cb
->identifier
);
8496 if (!bindings_need_stored
.is_empty ())
8499 vec_safe_reserve_exact (*old_bindings
, bindings_need_stored
.length ());
8500 for (i
= 0; bindings_need_stored
.iterate (i
, &id
); ++i
)
8501 store_binding (id
, old_bindings
);
8502 bindings_need_stored
.truncate (0);
8506 /* A chain of saved_scope structures awaiting reuse. */
8508 static GTY((deletable
)) struct saved_scope
*free_saved_scope
;
8511 do_push_to_top_level (void)
8513 struct saved_scope
*s
;
8514 cp_binding_level
*b
;
8515 cxx_saved_binding
*sb
;
8519 /* Reuse or create a new structure for this saved scope. */
8520 if (free_saved_scope
!= NULL
)
8522 s
= free_saved_scope
;
8523 free_saved_scope
= s
->prev
;
8525 vec
<cxx_saved_binding
, va_gc
> *old_bindings
= s
->old_bindings
;
8526 memset (s
, 0, sizeof (*s
));
8527 /* Also reuse the structure's old_bindings vector. */
8528 vec_safe_truncate (old_bindings
, 0);
8529 s
->old_bindings
= old_bindings
;
8532 s
= ggc_cleared_alloc
<saved_scope
> ();
8534 b
= scope_chain
? current_binding_level
: 0;
8536 /* If we're in the middle of some function, save our state. */
8540 push_function_context ();
8545 if (scope_chain
&& previous_class_level
)
8546 store_class_bindings (previous_class_level
->class_shadowed
,
8549 /* Have to include the global scope, because class-scope decls
8550 aren't listed anywhere useful. */
8551 for (; b
; b
= b
->level_chain
)
8555 /* Template IDs are inserted into the global level. If they were
8556 inserted into namespace level, finish_file wouldn't find them
8557 when doing pending instantiations. Therefore, don't stop at
8558 namespace level, but continue until :: . */
8559 if (global_scope_p (b
))
8562 store_bindings (b
->names
, &s
->old_bindings
);
8563 /* We also need to check class_shadowed to save class-level type
8564 bindings, since pushclass doesn't fill in b->names. */
8565 if (b
->kind
== sk_class
)
8566 store_class_bindings (b
->class_shadowed
, &s
->old_bindings
);
8568 /* Unwind type-value slots back to top level. */
8569 for (t
= b
->type_shadowed
; t
; t
= TREE_CHAIN (t
))
8570 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t
), TREE_VALUE (t
));
8573 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, sb
)
8574 IDENTIFIER_MARKED (sb
->identifier
) = 0;
8576 s
->prev
= scope_chain
;
8578 s
->need_pop_function_context
= need_pop
;
8579 s
->function_decl
= current_function_decl
;
8580 s
->unevaluated_operand
= cp_unevaluated_operand
;
8581 s
->inhibit_evaluation_warnings
= c_inhibit_evaluation_warnings
;
8582 s
->suppress_location_wrappers
= suppress_location_wrappers
;
8583 s
->x_stmt_tree
.stmts_are_full_exprs_p
= true;
8586 current_function_decl
= NULL_TREE
;
8587 current_lang_base
= NULL
;
8588 current_lang_name
= lang_name_cplusplus
;
8589 current_namespace
= global_namespace
;
8590 push_class_stack ();
8591 cp_unevaluated_operand
= 0;
8592 c_inhibit_evaluation_warnings
= 0;
8593 suppress_location_wrappers
= 0;
8597 do_pop_from_top_level (void)
8599 struct saved_scope
*s
= scope_chain
;
8600 cxx_saved_binding
*saved
;
8603 /* Clear out class-level bindings cache. */
8604 if (previous_class_level
)
8605 invalidate_class_lookup_cache ();
8608 release_tree_vector (current_lang_base
);
8610 scope_chain
= s
->prev
;
8611 FOR_EACH_VEC_SAFE_ELT (s
->old_bindings
, i
, saved
)
8613 tree id
= saved
->identifier
;
8615 IDENTIFIER_BINDING (id
) = saved
->binding
;
8616 SET_IDENTIFIER_TYPE_VALUE (id
, saved
->real_type_value
);
8619 /* If we were in the middle of compiling a function, restore our
8621 if (s
->need_pop_function_context
)
8622 pop_function_context ();
8623 current_function_decl
= s
->function_decl
;
8624 cp_unevaluated_operand
= s
->unevaluated_operand
;
8625 c_inhibit_evaluation_warnings
= s
->inhibit_evaluation_warnings
;
8626 suppress_location_wrappers
= s
->suppress_location_wrappers
;
8628 /* Make this saved_scope structure available for reuse by
8629 push_to_top_level. */
8630 s
->prev
= free_saved_scope
;
8631 free_saved_scope
= s
;
8634 /* Push into the scope of the namespace NS, even if it is deeply
8635 nested within another namespace. */
8638 do_push_nested_namespace (tree ns
)
8640 if (ns
== global_namespace
)
8641 do_push_to_top_level ();
8644 do_push_nested_namespace (CP_DECL_CONTEXT (ns
));
8645 resume_scope (NAMESPACE_LEVEL (ns
));
8646 current_namespace
= ns
;
8650 /* Pop back from the scope of the namespace NS, which was previously
8651 entered with push_nested_namespace. */
8654 do_pop_nested_namespace (tree ns
)
8656 while (ns
!= global_namespace
)
8658 ns
= CP_DECL_CONTEXT (ns
);
8659 current_namespace
= ns
;
8663 do_pop_from_top_level ();
8666 /* Add TARGET to USINGS, if it does not already exist there. We used
8667 to build the complete graph of usings at this point, from the POV
8668 of the source namespaces. Now we build that as we perform the
8669 unqualified search. */
8672 add_using_namespace (vec
<tree
, va_gc
> *&usings
, tree target
)
8675 for (unsigned ix
= usings
->length (); ix
--;)
8676 if ((*usings
)[ix
] == target
)
8679 vec_safe_push (usings
, target
);
8682 /* Tell the debug system of a using directive. */
8685 emit_debug_info_using_namespace (tree from
, tree target
, bool implicit
)
8687 /* Emit debugging info. */
8688 tree context
= from
!= global_namespace
? from
: NULL_TREE
;
8689 debug_hooks
->imported_module_or_decl (target
, NULL_TREE
, context
, false,
8693 /* Process a using directive. */
8696 finish_using_directive (tree target
, tree attribs
)
8698 if (target
== error_mark_node
)
8701 if (current_binding_level
->kind
!= sk_namespace
)
8702 add_stmt (build_stmt (input_location
, USING_STMT
, target
));
8704 emit_debug_info_using_namespace (current_binding_level
->this_entity
,
8705 ORIGINAL_NAMESPACE (target
), false);
8707 add_using_namespace (current_binding_level
->using_directives
,
8708 ORIGINAL_NAMESPACE (target
));
8710 if (attribs
!= error_mark_node
)
8711 for (tree a
= attribs
; a
; a
= TREE_CHAIN (a
))
8713 tree name
= get_attribute_name (a
);
8714 if (current_binding_level
->kind
== sk_namespace
8715 && is_attribute_p ("strong", name
))
8717 if (warning (0, "%<strong%> using directive no longer supported")
8718 && CP_DECL_CONTEXT (target
) == current_namespace
)
8719 inform (DECL_SOURCE_LOCATION (target
),
8720 "you can use an inline namespace instead");
8723 warning (OPT_Wattributes
, "%qD attribute directive ignored", name
);
8727 /* Pushes X into the global namespace. */
8730 pushdecl_top_level (tree x
)
8732 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8733 do_push_to_top_level ();
8734 gcc_checking_assert (!DECL_CONTEXT (x
));
8735 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8736 x
= pushdecl_namespace_level (x
);
8737 do_pop_from_top_level ();
8738 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8742 /* Pushes X into the global namespace and calls cp_finish_decl to
8743 register the variable, initializing it with INIT. */
8746 pushdecl_top_level_and_finish (tree x
, tree init
)
8748 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8749 do_push_to_top_level ();
8750 gcc_checking_assert (!DECL_CONTEXT (x
));
8751 DECL_CONTEXT (x
) = FROB_CONTEXT (global_namespace
);
8752 x
= pushdecl_namespace_level (x
);
8753 cp_finish_decl (x
, init
, false, NULL_TREE
, 0);
8754 do_pop_from_top_level ();
8755 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
8759 /* Enter the namespaces from current_namerspace to NS. */
8762 push_inline_namespaces (tree ns
)
8765 if (ns
!= current_namespace
)
8767 gcc_assert (ns
!= global_namespace
);
8768 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8769 resume_scope (NAMESPACE_LEVEL (ns
));
8770 current_namespace
= ns
;
8776 /* SLOT is the (possibly empty) binding slot for NAME in CTX.
8777 Reuse or create a namespace NAME. NAME is null for the anonymous
8781 reuse_namespace (tree
*slot
, tree ctx
, tree name
)
8783 if (modules_p () && *slot
&& TREE_PUBLIC (ctx
) && name
)
8785 /* Public namespace. Shared. */
8786 tree
*global_slot
= slot
;
8787 if (TREE_CODE (*slot
) == BINDING_VECTOR
)
8788 global_slot
= get_fixed_binding_slot (slot
, name
,
8789 BINDING_SLOT_GLOBAL
, false);
8791 for (ovl_iterator
iter (*global_slot
); iter
; ++iter
)
8795 if (TREE_CODE (decl
) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl
))
8803 make_namespace (tree ctx
, tree name
, location_t loc
, bool inline_p
)
8805 /* Create the namespace. */
8806 tree ns
= build_lang_decl (NAMESPACE_DECL
, name
, void_type_node
);
8807 DECL_SOURCE_LOCATION (ns
) = loc
;
8808 SCOPE_DEPTH (ns
) = SCOPE_DEPTH (ctx
) + 1;
8809 if (!SCOPE_DEPTH (ns
))
8810 /* We only allow depth 255. */
8811 sorry ("cannot nest more than %d namespaces", SCOPE_DEPTH (ctx
));
8812 DECL_CONTEXT (ns
) = FROB_CONTEXT (ctx
);
8815 /* Anon-namespaces in different header-unit imports are distinct.
8816 But that's ok as their contents all have internal linkage.
8817 (This is different to how they'd behave as textual includes,
8818 but doing this at all is really odd source.) */
8819 SET_DECL_ASSEMBLER_NAME (ns
, anon_identifier
);
8820 else if (TREE_PUBLIC (ctx
))
8821 TREE_PUBLIC (ns
) = true;
8824 DECL_NAMESPACE_INLINE_P (ns
) = true;
8829 /* NS was newly created, finish off making it. */
8832 make_namespace_finish (tree ns
, tree
*slot
, bool from_import
= false)
8834 if (modules_p () && TREE_PUBLIC (ns
) && (from_import
|| *slot
!= ns
))
8836 /* Merge into global slot. */
8837 tree
*gslot
= get_fixed_binding_slot (slot
, DECL_NAME (ns
),
8838 BINDING_SLOT_GLOBAL
, true);
8842 tree ctx
= CP_DECL_CONTEXT (ns
);
8843 cp_binding_level
*scope
= ggc_cleared_alloc
<cp_binding_level
> ();
8844 scope
->this_entity
= ns
;
8845 scope
->more_cleanups_ok
= true;
8846 scope
->kind
= sk_namespace
;
8847 scope
->level_chain
= NAMESPACE_LEVEL (ctx
);
8848 NAMESPACE_LEVEL (ns
) = scope
;
8850 if (DECL_NAMESPACE_INLINE_P (ns
))
8851 vec_safe_push (DECL_NAMESPACE_INLINEES (ctx
), ns
);
8853 if (DECL_NAMESPACE_INLINE_P (ns
) || !DECL_NAME (ns
))
8854 emit_debug_info_using_namespace (ctx
, ns
, true);
8857 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE,
8858 then we enter an anonymous namespace. If MAKE_INLINE is true, then
8859 we create an inline namespace (it is up to the caller to check upon
8860 redefinition). Return the number of namespaces entered. */
8863 push_namespace (tree name
, bool make_inline
)
8865 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
8868 /* We should not get here if the global_namespace is not yet constructed
8869 nor if NAME designates the global namespace: The global scope is
8870 constructed elsewhere. */
8871 gcc_checking_assert (global_namespace
!= NULL
&& name
!= global_identifier
);
8873 tree ns
= NULL_TREE
;
8875 name_lookup
lookup (name
);
8876 if (!lookup
.search_qualified (current_namespace
, /*usings=*/false))
8878 else if (TREE_CODE (lookup
.value
) == TREE_LIST
)
8880 /* An ambiguous lookup. If exactly one is a namespace, we
8881 want that. If more than one is a namespace, error, but
8882 pick one of them. */
8883 /* DR2061 can cause us to find multiple namespaces of the same
8884 name. We must treat that carefully and avoid thinking we
8885 need to push a new (possibly) duplicate namespace. Hey,
8886 if you want to use the same identifier within an inline
8887 nest, knock yourself out. */
8888 for (tree
*chain
= &lookup
.value
, next
; (next
= *chain
);)
8890 tree decl
= TREE_VALUE (next
);
8891 if (TREE_CODE (decl
) == NAMESPACE_DECL
)
8895 else if (SCOPE_DEPTH (ns
) >= SCOPE_DEPTH (decl
))
8899 chain
= &TREE_CHAIN (next
);
8903 *chain
= TREE_CHAIN (next
);
8906 if (TREE_CHAIN (lookup
.value
))
8908 error ("%<namespace %E%> is ambiguous", name
);
8909 print_candidates (lookup
.value
);
8912 else if (TREE_CODE (lookup
.value
) == NAMESPACE_DECL
)
8916 if (tree dna
= DECL_NAMESPACE_ALIAS (ns
))
8918 /* A namespace alias is not allowed here, but if the alias
8919 is for a namespace also inside the current scope,
8920 accept it with a diagnostic. That's better than dying
8922 if (is_nested_namespace (current_namespace
, CP_DECL_CONTEXT (dna
)))
8924 error ("namespace alias %qD not allowed here, "
8925 "assuming %qD", ns
, dna
);
8935 /* DR2061. NS might be a member of an inline namespace. We
8936 need to push into those namespaces. */
8939 for (tree parent
, ctx
= ns
; ctx
!= current_namespace
;
8942 parent
= CP_DECL_CONTEXT (ctx
);
8944 tree bind
= *find_namespace_slot (parent
, DECL_NAME (ctx
), false);
8947 auto &cluster
= BINDING_VECTOR_CLUSTER (bind
, 0);
8948 binding_slot
&slot
= cluster
.slots
[BINDING_SLOT_CURRENT
];
8949 gcc_checking_assert (!(tree
)slot
|| (tree
)slot
== ctx
);
8955 count
+= push_inline_namespaces (CP_DECL_CONTEXT (ns
));
8956 if (DECL_SOURCE_LOCATION (ns
) == BUILTINS_LOCATION
)
8957 /* It's not builtin now. */
8958 DECL_SOURCE_LOCATION (ns
) = input_location
;
8962 /* Before making a new namespace, see if we already have one in
8963 the existing partitions of the current namespace. */
8964 tree
*slot
= find_namespace_slot (current_namespace
, name
, false);
8966 ns
= reuse_namespace (slot
, current_namespace
, name
);
8968 ns
= make_namespace (current_namespace
, name
,
8969 input_location
, make_inline
);
8971 if (pushdecl (ns
) == error_mark_node
)
8975 /* Finish up making the namespace. */
8976 add_decl_to_level (NAMESPACE_LEVEL (current_namespace
), ns
);
8979 slot
= find_namespace_slot (current_namespace
, name
);
8980 /* This should find the slot created by pushdecl. */
8981 gcc_checking_assert (slot
&& *slot
== ns
);
8983 make_namespace_finish (ns
, slot
);
8985 /* Add the anon using-directive here, we don't do it in
8986 make_namespace_finish. */
8987 if (!DECL_NAMESPACE_INLINE_P (ns
) && !name
)
8988 add_using_namespace (current_binding_level
->using_directives
, ns
);
8994 /* A public namespace is exported only if explicitly marked, or
8995 it contains exported entities. */
8996 if (!DECL_MODULE_EXPORT_P (ns
) && TREE_PUBLIC (ns
)
8997 && module_exporting_p ())
8998 implicitly_export_namespace (ns
);
9000 if (make_inline
&& !DECL_NAMESPACE_INLINE_P (ns
))
9002 error_at (input_location
,
9003 "inline namespace must be specified at initial definition");
9004 inform (DECL_SOURCE_LOCATION (ns
), "%qD defined here", ns
);
9006 resume_scope (NAMESPACE_LEVEL (ns
));
9007 current_namespace
= ns
;
9011 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9015 /* Pop from the scope of the current namespace. */
9018 pop_namespace (void)
9020 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9022 gcc_assert (current_namespace
!= global_namespace
);
9023 current_namespace
= CP_DECL_CONTEXT (current_namespace
);
9024 /* The binding level is not popped, as it might be re-opened later. */
9027 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9030 /* An import is defining namespace NAME inside CTX. Find or create
9031 that namespace and add it to the container's binding-vector. */
9034 add_imported_namespace (tree ctx
, tree name
, unsigned origin
, location_t loc
,
9035 bool visible_p
, bool inline_p
)
9037 // FIXME: Something is not correct about the VISIBLE_P handling. We
9038 // need to insert this namespace into
9039 // (a) the GLOBAL or PARTITION slot, if it is TREE_PUBLIC
9040 // (b) The importing module's slot (always)
9041 // (c) Do we need to put it in the CURRENT slot? This is the
9044 gcc_checking_assert (origin
);
9045 tree
*slot
= find_namespace_slot (ctx
, name
, true);
9046 tree decl
= reuse_namespace (slot
, ctx
, name
);
9049 decl
= make_namespace (ctx
, name
, loc
, inline_p
);
9050 DECL_MODULE_IMPORT_P (decl
) = true;
9051 make_namespace_finish (decl
, slot
, true);
9053 else if (DECL_NAMESPACE_INLINE_P (decl
) != inline_p
)
9055 error_at (loc
, "%s namespace %qD conflicts with reachable definition",
9056 inline_p
? "inline" : "non-inline", decl
);
9057 inform (DECL_SOURCE_LOCATION (decl
), "reachable %s definition here",
9058 inline_p
? "non-inline" : "inline");
9061 if (TREE_PUBLIC (decl
) && TREE_CODE (*slot
) == BINDING_VECTOR
)
9063 /* See if we can extend the final slot. */
9064 binding_cluster
*last
= BINDING_VECTOR_CLUSTER_LAST (*slot
);
9065 gcc_checking_assert (last
->indices
[0].span
);
9066 unsigned jx
= BINDING_VECTOR_SLOTS_PER_CLUSTER
;
9069 if (last
->indices
[jx
].span
)
9071 tree final
= last
->slots
[jx
];
9072 if (visible_p
== !STAT_HACK_P (final
)
9073 && MAYBE_STAT_DECL (final
) == decl
9074 && last
->indices
[jx
].base
+ last
->indices
[jx
].span
== origin
9075 && (BINDING_VECTOR_NUM_CLUSTERS (*slot
) > 1
9076 || (BINDING_VECTOR_SLOTS_PER_CLUSTER
> BINDING_SLOTS_FIXED
9077 && jx
>= BINDING_SLOTS_FIXED
)))
9079 last
->indices
[jx
].span
++;
9084 /* Append a new slot. */
9085 tree
*mslot
= &(tree
&)*append_imported_binding_slot (slot
, name
, origin
);
9087 gcc_assert (!*mslot
);
9088 *mslot
= visible_p
? decl
: stat_hack (decl
, NULL_TREE
);
9093 /* External entry points for do_{push_to/pop_from}_top_level. */
9096 push_to_top_level (void)
9098 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9099 do_push_to_top_level ();
9100 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9104 pop_from_top_level (void)
9106 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9107 do_pop_from_top_level ();
9108 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9111 /* External entry points for do_{push,pop}_nested_namespace. */
9114 push_nested_namespace (tree ns
)
9116 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9117 do_push_nested_namespace (ns
);
9118 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9122 pop_nested_namespace (tree ns
)
9124 bool subtime
= timevar_cond_start (TV_NAME_LOOKUP
);
9125 gcc_assert (current_namespace
== ns
);
9126 do_pop_nested_namespace (ns
);
9127 timevar_cond_stop (TV_NAME_LOOKUP
, subtime
);
9130 /* Pop off extraneous binding levels left over due to syntax errors.
9131 We don't pop past namespaces, as they might be valid. */
9134 pop_everything (void)
9136 if (ENABLE_SCOPE_CHECKING
)
9137 verbatim ("XXX entering %<pop_everything ()%>");
9138 while (!namespace_bindings_p ())
9140 if (current_binding_level
->kind
== sk_class
)
9141 pop_nested_class ();
9145 if (ENABLE_SCOPE_CHECKING
)
9146 verbatim ("XXX leaving %<pop_everything ()%>");
9149 /* Emit debugging information for using declarations and directives.
9150 If input tree is overloaded fn then emit debug info for all
9154 cp_emit_debug_info_for_using (tree t
, tree context
)
9156 /* Don't try to emit any debug information if we have errors. */
9160 /* Do not supply context to imported_module_or_decl, if
9161 it is a global namespace. */
9162 if (context
== global_namespace
)
9163 context
= NULL_TREE
;
9165 t
= MAYBE_BASELINK_FUNCTIONS (t
);
9167 for (lkp_iterator
iter (t
); iter
; ++iter
)
9171 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
9172 /* FIXME: Handle TEMPLATE_DECLs. */
9175 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
9176 of a builtin function. */
9177 if (TREE_CODE (fn
) == FUNCTION_DECL
9178 && DECL_EXTERNAL (fn
)
9179 && fndecl_built_in_p (fn
))
9182 if (building_stmt_list_p ())
9183 add_stmt (build_stmt (input_location
, USING_STMT
, fn
));
9185 debug_hooks
->imported_module_or_decl (fn
, NULL_TREE
, context
,
9190 /* Return the result of unqualified lookup for the overloaded operator
9191 designated by CODE, if we are in a template and the binding we find is
9195 op_unqualified_lookup (tree fnname
)
9197 if (cxx_binding
*binding
= IDENTIFIER_BINDING (fnname
))
9199 cp_binding_level
*l
= binding
->scope
;
9200 while (l
&& !l
->this_entity
)
9203 if (l
&& uses_template_parms (l
->this_entity
))
9204 /* Don't preserve decls from an uninstantiated template,
9205 wait until that template is instantiated. */
9209 tree fns
= lookup_name (fnname
);
9211 /* Remember we found nothing! */
9212 return error_mark_node
;
9214 tree d
= is_overloaded_fn (fns
) ? get_first_fn (fns
) : fns
;
9215 if (DECL_CLASS_SCOPE_P (d
))
9216 /* We don't need to remember class-scope functions or declarations,
9217 normal unqualified lookup will find them again. */
9223 /* E is an expression representing an operation with dependent type, so we
9224 don't know yet whether it will use the built-in meaning of the operator or a
9225 function. Remember declarations of that operator in scope.
9227 We then inject a fake binding of that lookup into the
9228 instantiation's parameter scope. This approach fails if the user
9229 has different using declarations or directives in different local
9230 binding of the current function from whence we need to do lookups
9231 (we'll cache what we see on the first lookup). */
9233 static const char *const op_bind_attrname
= "operator bindings";
9236 maybe_save_operator_binding (tree e
)
9238 /* This is only useful in a generic lambda. */
9239 if (!processing_template_decl
)
9242 tree cfn
= current_function_decl
;
9246 /* Do this for lambdas and code that will emit a CMI. In a module's
9247 GMF we don't yet know whether there will be a CMI. */
9248 if (!module_has_cmi_p () && !global_purview_p () && !current_lambda_expr())
9251 tree fnname
= ovl_op_identifier (false, TREE_CODE (e
));
9255 tree attributes
= DECL_ATTRIBUTES (cfn
);
9256 tree op_attr
= lookup_attribute (op_bind_attrname
, attributes
);
9259 op_attr
= tree_cons (get_identifier (op_bind_attrname
),
9260 NULL_TREE
, attributes
);
9261 DECL_ATTRIBUTES (cfn
) = op_attr
;
9264 tree op_bind
= purpose_member (fnname
, TREE_VALUE (op_attr
));
9267 tree fns
= op_unqualified_lookup (fnname
);
9269 /* Always record, so we don't keep looking for this
9271 TREE_VALUE (op_attr
) = tree_cons (fnname
, fns
, TREE_VALUE (op_attr
));
9275 /* Called from cp_free_lang_data so we don't put this into LTO. */
9278 discard_operator_bindings (tree decl
)
9280 DECL_ATTRIBUTES (decl
) = remove_attribute (op_bind_attrname
,
9281 DECL_ATTRIBUTES (decl
));
9284 /* Subroutine of start_preparsed_function: push the bindings we saved away in
9285 maybe_save_op_lookup into the function parameter binding level. */
9288 push_operator_bindings ()
9290 tree decl1
= current_function_decl
;
9291 if (tree attr
= lookup_attribute (op_bind_attrname
,
9292 DECL_ATTRIBUTES (decl1
)))
9293 for (tree binds
= TREE_VALUE (attr
); binds
; binds
= TREE_CHAIN (binds
))
9294 if (tree val
= TREE_VALUE (binds
))
9296 tree name
= TREE_PURPOSE (binds
);
9297 push_local_binding (name
, val
, /*using*/true);
9301 #include "gt-cp-name-lookup.h"